From: Gianluca Guida Date: Mon, 14 Feb 2011 19:36:03 +0000 (+0000) Subject: Import unmodified NetBSD libc in trunk X-Git-Tag: v3.2.0~672 X-Git-Url: http://zhaoyanbai.com/repos/doc/man.3.txt?a=commitdiff_plain;h=b6cbf7203b080219de306404f8022a65b7884f33;p=minix.git Import unmodified NetBSD libc in trunk This patch imports the unmodified current version of NetBSD libc. The NetBSD includes are in /nbsd_include, while the libc code itself is split between lib/nbsd_libc and common/lib/libc. --- diff --git a/common/lib/libc/Makefile.inc b/common/lib/libc/Makefile.inc new file mode 100644 index 000000000..1370eddbc --- /dev/null +++ b/common/lib/libc/Makefile.inc @@ -0,0 +1,37 @@ +# $NetBSD: Makefile.inc,v 1.10 2008/10/26 07:22:50 mrg Exp $ + +COMMON_DIR:=${.PARSEDIR} +COMMON_CODEDIRS=atomic gen gmon inet md net quad stdlib string sys +COMMON_CODEDIRS+=hash/sha1 hash/sha2 hash/rmd160 + +.if defined(COMMON_MACHINE_ARCH) && !empty(COMMON_MACHINE_ARCH) && \ + exists(${COMMON_DIR}/arch/${COMMON_MACHINE_ARCH}) +COMMON_ARCHSUBDIR= ${COMMON_MACHINE_ARCH} +.elif defined(LIBKERN_ARCH) && !empty(LIBKERN_ARCH) && \ + exists(${KERNDIR}/arch/${LIBKERN_ARCH}) +COMMON_ARCHSUBDIR= ${LIBKERN_ARCH} +.elif exists(${COMMON_DIR}/arch/${MACHINE_ARCH}) +COMMON_ARCHSUBDIR= ${MACHINE_ARCH} +.elif exists(${COMMON_DIR}/arch/${MACHINE_CPU}) +COMMON_ARCHSUBDIR= ${MACHINE_CPU} +.endif + +COMMON_ARCHDIR=${COMMON_DIR}/arch/${COMMON_ARCHSUBDIR} + +.for i in ${COMMON_CODEDIRS} +.if exists(${COMMON_DIR}/$i) +.PATH.c: ${COMMON_DIR}/$i +.endif +.if exists(${COMMON_ARCHDIR}/${i}/Makefile.inc) +.include "${COMMON_ARCHDIR}/${i}/Makefile.inc" +.endif +.if exists(${COMMON_ARCHDIR}/$i) +.PATH.c: ${COMMON_ARCHDIR}/$i +.PATH.S: ${COMMON_ARCHDIR}/$i +.endif +.endfor + +CPPFLAGS+=-I${COMMON_DIR}/quad -I${COMMON_DIR}/string +.if defined(COMMON_ARCHSUBDIR) +CPPFLAGS+=-I${COMMON_ARCHDIR}/string +.endif diff --git a/common/lib/libc/arch/alpha/atomic/Makefile.inc b/common/lib/libc/arch/alpha/atomic/Makefile.inc new file mode 100644 index 000000000..d0e048660 --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/Makefile.inc @@ -0,0 +1,15 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add.S atomic_and.S atomic_cas.S atomic_dec.S \ + atomic_inc.S atomic_or.S atomic_swap.S membar_ops.S + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_cas.c + +.endif diff --git a/common/lib/libc/arch/alpha/atomic/atomic_add.S b/common/lib/libc/arch/alpha/atomic/atomic_add.S new file mode 100644 index 000000000..e4fdb25f4 --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_add.S @@ -0,0 +1,88 @@ +/* $NetBSD: atomic_add.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +LEAF(_atomic_add_32, 2) +1: ldl_l t1, 0(a0) + addl t1, a1, t2 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_32,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) + +LEAF(_atomic_add_32_nv, 2) +1: ldl_l t1, 0(a0) + addl t1, a1, t2 + mov t2, v0 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_32_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_int_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv) + +LEAF(_atomic_add_64, 2) +1: ldq_l t1, 0(a0) + addq t1, a1, t2 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_add_64) +ATOMIC_OP_ALIAS(atomic_add_64,_atomic_add_64) +ATOMIC_OP_ALIAS(atomic_add_long,_atomic_add_64) +STRONG_ALIAS(_atomic_add_long,_atomic_add_64) +ATOMIC_OP_ALIAS(atomic_add_ptr,_atomic_add_64) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_64) + +LEAF(_atomic_add_64_nv, 2) +1: ldq_l t1, 0(a0) + addq t1, a1, t2 + mov t2, v0 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_add_64_nv) +ATOMIC_OP_ALIAS(atomic_add_64_nv,_atomic_add_64_nv) +ATOMIC_OP_ALIAS(atomic_add_long_nv,_atomic_add_64_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_64_nv) +ATOMIC_OP_ALIAS(atomic_add_ptr_nv,_atomic_add_64_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_64_nv) diff --git a/common/lib/libc/arch/alpha/atomic/atomic_and.S b/common/lib/libc/arch/alpha/atomic/atomic_and.S new file mode 100644 index 000000000..bc5daca3e --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_and.S @@ -0,0 +1,84 @@ +/* $NetBSD: atomic_and.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +LEAF(_atomic_and_32, 2) +1: ldl_l t1, 0(a0) + and t1, a1, t2 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_32,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) + +LEAF(_atomic_and_32_nv, 2) +1: ldl_l t1, 0(a0) + and t1, a1, t2 + mov t2, v0 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_32_nv,_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_uint_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv) + +LEAF(_atomic_and_64, 2) +1: ldq_l t1, 0(a0) + and t1, a1, t2 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_and_64) +ATOMIC_OP_ALIAS(atomic_and_64,_atomic_and_64) +ATOMIC_OP_ALIAS(atomic_and_ulong,_atomic_and_64) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_64) + +LEAF(_atomic_and_64_nv, 2) +1: ldq_l t1, 0(a0) + and t1, a1, t2 + mov t2, v0 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_and_64_nv) +ATOMIC_OP_ALIAS(atomic_and_64_nv,_atomic_and_64_nv) +ATOMIC_OP_ALIAS(atomic_and_ulong_nv,_atomic_and_64_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_64_nv) diff --git a/common/lib/libc/arch/alpha/atomic/atomic_cas.S b/common/lib/libc/arch/alpha/atomic/atomic_cas.S new file mode 100644 index 000000000..d959485c2 --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_cas.S @@ -0,0 +1,78 @@ +/* $NetBSD: atomic_cas.S,v 1.5 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +LEAF(_atomic_cas_32, 3) +1: mov a2, t2 + ldl_l v0, 0(a0) + cmpeq v0, a1, t1 + beq t1, 2f + stl_c t2, 0(a0) + beq t2, 3f +2: RET +3: br 1b + END(_atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_32,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_32_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_32_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32) + +LEAF(_atomic_cas_64, 3) +1: mov a2, t2 + ldq_l v0, 0(a0) + cmpeq v0, a1, t1 + beq t1, 2f + stq_c t2, 0(a0) + beq t2, 3f +2: RET +3: br 1b + END(_atomic_cas_64) + +ATOMIC_OP_ALIAS(atomic_cas_64,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ulong,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ptr,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_64) + +ATOMIC_OP_ALIAS(atomic_cas_64_ni,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_64_ni,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ulong_ni,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ptr_ni,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_64) diff --git a/common/lib/libc/arch/alpha/atomic/atomic_dec.S b/common/lib/libc/arch/alpha/atomic/atomic_dec.S new file mode 100644 index 000000000..982a1dab8 --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_dec.S @@ -0,0 +1,88 @@ +/* $NetBSD: atomic_dec.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +LEAF(_atomic_dec_32, 1) +1: ldl_l t1, 0(a0) + subl t1, 1, t2 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_32,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) + +LEAF(_atomic_dec_32_nv, 1) +1: ldl_l t1, 0(a0) + subl t1, 1, t2 + mov t2, v0 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv) + +LEAF(_atomic_dec_64, 1) +1: ldq_l t1, 0(a0) + subq t1, 1, t2 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_dec_64) +ATOMIC_OP_ALIAS(atomic_dec_64,_atomic_dec_64) +ATOMIC_OP_ALIAS(atomic_dec_ulong,_atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_64) +ATOMIC_OP_ALIAS(atomic_dec_ptr,_atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_64) + +LEAF(_atomic_dec_64_nv, 1) +1: ldq_l t1, 0(a0) + subq t1, 1, t2 + mov t2, v0 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_dec_64_nv) +ATOMIC_OP_ALIAS(atomic_dec_64_nv,_atomic_dec_64_nv) +ATOMIC_OP_ALIAS(atomic_dec_ulong_nv,_atomic_dec_64_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_64_nv) +ATOMIC_OP_ALIAS(atomic_dec_ptr_nv,_atomic_dec_64_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_64_nv) diff --git a/common/lib/libc/arch/alpha/atomic/atomic_inc.S b/common/lib/libc/arch/alpha/atomic/atomic_inc.S new file mode 100644 index 000000000..6796cbf1e --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_inc.S @@ -0,0 +1,88 @@ +/* $NetBSD: atomic_inc.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +LEAF(_atomic_inc_32, 1) +1: ldl_l t1, 0(a0) + addl t1, 1, t2 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_32,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) + +LEAF(_atomic_inc_32_nv, 1) +1: ldl_l t1, 0(a0) + addl t1, 1, t2 + mov t2, v0 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv) + +LEAF(_atomic_inc_64, 1) +1: ldq_l t1, 0(a0) + addq t1, 1, t2 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_inc_64) +ATOMIC_OP_ALIAS(atomic_inc_64,_atomic_inc_64) +ATOMIC_OP_ALIAS(atomic_inc_ulong,_atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_64) +ATOMIC_OP_ALIAS(atomic_inc_ptr,_atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_64) + +LEAF(_atomic_inc_64_nv, 1) +1: ldq_l t1, 0(a0) + addq t1, 1, t2 + mov t2, v0 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_inc_64_nv) +ATOMIC_OP_ALIAS(atomic_inc_64_nv,_atomic_inc_64_nv) +ATOMIC_OP_ALIAS(atomic_inc_ulong_nv,_atomic_inc_64_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_64_nv) +ATOMIC_OP_ALIAS(atomic_inc_ptr_nv,_atomic_inc_64_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_64_nv) diff --git a/common/lib/libc/arch/alpha/atomic/atomic_op_asm.h b/common/lib/libc/arch/alpha/atomic/atomic_op_asm.h new file mode 100644 index 000000000..5f01f3f9d --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_op_asm.h @@ -0,0 +1,47 @@ +/* $NetBSD: atomic_op_asm.h,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#endif /* _KERNEL */ + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/alpha/atomic/atomic_or.S b/common/lib/libc/arch/alpha/atomic/atomic_or.S new file mode 100644 index 000000000..a08a71cd7 --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_or.S @@ -0,0 +1,84 @@ +/* $NetBSD: atomic_or.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +LEAF(_atomic_or_32, 2) +1: ldl_l t1, 0(a0) + bis t1, a1, t2 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_32,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) + +LEAF(_atomic_or_32_nv, 2) +1: ldl_l t1, 0(a0) + bis t1, a1, t2 + mov t2, v0 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_32_nv,_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_uint_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv) + +LEAF(_atomic_or_64, 2) +1: ldq_l t1, 0(a0) + bis t1, a1, t2 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_or_64) +ATOMIC_OP_ALIAS(atomic_or_64,_atomic_or_64) +ATOMIC_OP_ALIAS(atomic_or_ulong,_atomic_or_64) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_64) + +LEAF(_atomic_or_64_nv, 2) +1: ldq_l t1, 0(a0) + bis t1, a1, t2 + mov t2, v0 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_or_64_nv) +ATOMIC_OP_ALIAS(atomic_or_64_nv,_atomic_or_64_nv) +ATOMIC_OP_ALIAS(atomic_or_ulong_nv,_atomic_or_64_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_64_nv) diff --git a/common/lib/libc/arch/alpha/atomic/atomic_swap.S b/common/lib/libc/arch/alpha/atomic/atomic_swap.S new file mode 100644 index 000000000..3ccfe2fc5 --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/atomic_swap.S @@ -0,0 +1,60 @@ +/* $NetBSD: atomic_swap.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +LEAF(_atomic_swap_32, 2) +1: ldl_l v0, 0(a0) + mov a1, t2 + stl_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_32,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) + +LEAF(_atomic_swap_64, 2) +1: ldq_l v0, 0(a0) + mov a1, t2 + stq_c t2, 0(a0) + beq t2, 2f + RET +2: br 1b + END(_atomic_swap_64) +ATOMIC_OP_ALIAS(atomic_swap_64,_atomic_swap_64) +ATOMIC_OP_ALIAS(atomic_swap_ulong,_atomic_swap_64) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_64) +ATOMIC_OP_ALIAS(atomic_swap_ptr,_atomic_swap_64) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_64) diff --git a/common/lib/libc/arch/alpha/atomic/membar_ops.S b/common/lib/libc/arch/alpha/atomic/membar_ops.S new file mode 100644 index 000000000..f0162ecea --- /dev/null +++ b/common/lib/libc/arch/alpha/atomic/membar_ops.S @@ -0,0 +1,89 @@ +/* $NetBSD: membar_ops.S,v 1.6 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +#ifdef _KERNEL + +/* + * We start out with no-op versions that do nothing. We hot-patch when + * we detect a MP system. + */ +LEAF(_membar_producer, 0) + RET + nop + END(_membar_producer) +EXPORT(_membar_producer_end) + +LEAF(_membar_sync, 0) + RET + nop + END(_membar_sync) +EXPORT(_membar_sync_end) + +LEAF(_membar_producer_mp, 0) + wmb + RET + END(_membar_producer_mp) +EXPORT(_membar_producer_mp_end) + +LEAF(_membar_sync_mp, 0) + mb + RET + END(_membar_sync_mp) +EXPORT(_membar_sync_mp_end) + +#else /* _KERNEL */ + +LEAF(_membar_producer, 0) + mb + RET + END(_membar_producer) +EXPORT(_membar_producer_end) + +LEAF(_membar_sync, 0) + mb + RET + END(_membar_sync) +EXPORT(_membar_sync_end) + +#endif /* _KERNEL */ + +ATOMIC_OP_ALIAS(membar_producer,_membar_producer) +ATOMIC_OP_ALIAS(membar_sync,_membar_sync) +ATOMIC_OP_ALIAS(membar_enter,_membar_sync) +STRONG_ALIAS(_membar_enter,_membar_sync) +ATOMIC_OP_ALIAS(membar_exit,_membar_sync) +STRONG_ALIAS(_membar_exit,_membar_sync) +ATOMIC_OP_ALIAS(membar_consumer,_membar_sync) +STRONG_ALIAS(_membar_consumer,_membar_sync) diff --git a/common/lib/libc/arch/alpha/gen/byte_swap_2.S b/common/lib/libc/arch/alpha/gen/byte_swap_2.S new file mode 100644 index 000000000..95dab09e9 --- /dev/null +++ b/common/lib/libc/arch/alpha/gen/byte_swap_2.S @@ -0,0 +1,49 @@ +/* $NetBSD: byte_swap_2.S,v 1.3 2008/02/16 17:37:13 apb Exp $ */ + +/* + * Copyright (c) 1996 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +/* + * Byte-swap a 2-byte quantity. (Convert 0x0123 to 0x2301.) + * + * Argument is an unsigned 2-byte integer (uint16_t). + */ +#if defined(_KERNEL) || defined(_STANDALONE) +#define BSWAP16 bswap16 +#else /* defined(_KERNEL) || defined(_STANDALONE) */ +#define BSWAP16 __bswap16 +#endif /* defined(_KERNEL) || defined(_STANDALONE) */ +LEAF(BSWAP16, 1) /* a0 contains 0x0123 */ +XLEAF(htons, 1) +XLEAF(ntohs, 1) + insbl a0, 1, t0 /* t0 = 0x23 */ + extbl a0, 1, t1 /* t1 = 0x 01 */ + or t0, t1, v0 /* v0 = 0x2301 */ + RET +END(BSWAP16) diff --git a/common/lib/libc/arch/alpha/gen/byte_swap_4.S b/common/lib/libc/arch/alpha/gen/byte_swap_4.S new file mode 100644 index 000000000..38b11f8f0 --- /dev/null +++ b/common/lib/libc/arch/alpha/gen/byte_swap_4.S @@ -0,0 +1,55 @@ +/* $NetBSD: byte_swap_4.S,v 1.3 2008/02/16 17:37:13 apb Exp $ */ + +/* + * Copyright (c) 1996 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +/* + * Byte-swap a 4-byte quantity. (Convert 0x01234567 to 0x67452301.) + * + * Argument is an unsigned 4-byte integer (uint32_t). + */ +#if defined(_KERNEL) || defined(_STANDALONE) +#define BSWAP32 bswap32 +#else /* defined(_KERNEL) || defined(_STANDALONE) */ +#define BSWAP32 __bswap32 +#endif /* defined(_KERNEL) || defined(_STANDALONE) */ +LEAF(BSWAP32, 1) /* a0 contains 0x01234567 */ +XLEAF(htonl, 1) +XLEAF(ntohl, 1) + insbl a0, 3, t0 /* t0 = 0x67 */ + extbl a0, 1, t1 /* t1 = 0x 45 */ + extbl a0, 2, t2 /* t2 = 0x 23 */ + extbl a0, 3, t3 /* t3 = 0x 01 */ + sll t1, 16, t1 /* t1 = 0x 45 */ + sll t2, 8, t2 /* t2 = 0x 23 */ + or t3, t0, v0 /* v0 = 0x67 01 */ + or t1, t2, t1 /* t1 = 0x 4523 */ + or t1, v0, v0 /* v0 = 0x67452301 */ + RET +END(BSWAP32) diff --git a/common/lib/libc/arch/alpha/gmon/_mcount.S b/common/lib/libc/arch/alpha/gmon/_mcount.S new file mode 100644 index 000000000..53d92e6b3 --- /dev/null +++ b/common/lib/libc/arch/alpha/gmon/_mcount.S @@ -0,0 +1,128 @@ +/* $NetBSD: _mcount.S,v 1.2 2005/12/21 18:11:11 christos Exp $ */ + +/* + * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#if defined(GPROF) && !defined(_STANDALONE) + +#include +#include + +#define OFFSET_AT (0 * 8) +#define OFFSET_V0 (1 * 8) +#define OFFSET_T0 (2 * 8) +#define OFFSET_T1 (3 * 8) +#define OFFSET_T2 (4 * 8) +#define OFFSET_T3 (5 * 8) +#define OFFSET_T4 (6 * 8) +#define OFFSET_T5 (7 * 8) +#define OFFSET_T6 (8 * 8) +#define OFFSET_T7 (9 * 8) +#define OFFSET_S6 (10 * 8) +#define OFFSET_A0 (11 * 8) +#define OFFSET_A1 (12 * 8) +#define OFFSET_A2 (13 * 8) +#define OFFSET_A3 (14 * 8) +#define OFFSET_A4 (15 * 8) +#define OFFSET_A5 (16 * 8) +#define OFFSET_T8 (17 * 8) +#define OFFSET_T9 (18 * 8) +#define OFFSET_T10 (19 * 8) +#define OFFSET_T11 (20 * 8) +#define OFFSET_RA (21 * 8) +#define OFFSET_T12 (22 * 8) +#define OFFSET_GP (23 * 8) +#define FRAME_SIZE (24 * 8) + +LEAF_NOPROFILE(_mcount,0) /* XXX */ + .set noat + .set noreorder + + lda sp, -FRAME_SIZE(sp) + + stq at_reg, OFFSET_AT(sp) + stq v0, OFFSET_V0(sp) + stq t0, OFFSET_T0(sp) + stq t1, OFFSET_T1(sp) + stq t2, OFFSET_T2(sp) + stq t3, OFFSET_T3(sp) + stq t4, OFFSET_T4(sp) + stq t5, OFFSET_T5(sp) + stq t6, OFFSET_T6(sp) + stq t7, OFFSET_T7(sp) + stq s6, OFFSET_S6(sp) /* XXX because run _after_ prologue. */ + stq a0, OFFSET_A0(sp) + stq a1, OFFSET_A1(sp) + stq a2, OFFSET_A2(sp) + stq a3, OFFSET_A3(sp) + stq a4, OFFSET_A4(sp) + stq a5, OFFSET_A5(sp) + stq t8, OFFSET_T8(sp) + stq t9, OFFSET_T9(sp) + stq t10, OFFSET_T10(sp) + stq t11, OFFSET_T11(sp) + stq ra, OFFSET_RA(sp) + stq t12, OFFSET_T12(sp) + stq gp, OFFSET_GP(sp) + + br pv, 1f +1: LDGP(pv) + mov ra, a0 + mov at_reg, a1 + CALL(_MCOUNT_FUNC) + + ldq v0, OFFSET_V0(sp) + ldq t0, OFFSET_T0(sp) + ldq t1, OFFSET_T1(sp) + ldq t2, OFFSET_T2(sp) + ldq t3, OFFSET_T3(sp) + ldq t4, OFFSET_T4(sp) + ldq t5, OFFSET_T5(sp) + ldq t6, OFFSET_T6(sp) + ldq t7, OFFSET_T7(sp) + ldq s6, OFFSET_S6(sp) /* XXX because run _after_ prologue. */ + ldq a0, OFFSET_A0(sp) + ldq a1, OFFSET_A1(sp) + ldq a2, OFFSET_A2(sp) + ldq a3, OFFSET_A3(sp) + ldq a4, OFFSET_A4(sp) + ldq a5, OFFSET_A5(sp) + ldq t8, OFFSET_T8(sp) + ldq t9, OFFSET_T9(sp) + ldq t10, OFFSET_T10(sp) + ldq t11, OFFSET_T11(sp) + ldq ra, OFFSET_RA(sp) + ldq t12, OFFSET_T12(sp) + ldq gp, OFFSET_GP(sp) + + ldq at_reg, OFFSET_AT(sp) + + lda sp, FRAME_SIZE(sp) + ret zero, (at_reg), 1 + + END(_mcount) +#endif diff --git a/common/lib/libc/arch/alpha/string/bcopy.S b/common/lib/libc/arch/alpha/string/bcopy.S new file mode 100644 index 000000000..785e354b7 --- /dev/null +++ b/common/lib/libc/arch/alpha/string/bcopy.S @@ -0,0 +1,288 @@ +/* $NetBSD: bcopy.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright (c) 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Trevor Blackwell. Support for use as memcpy() and memmove() + * added by Chris Demetriou. + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +#if defined(MEMCOPY) || defined(MEMMOVE) +#ifdef MEMCOPY +#define FUNCTION memcpy +#else +#define FUNCTION memmove +#endif +#define SRCREG a1 +#define DSTREG a0 +#else /* !(defined(MEMCOPY) || defined(MEMMOVE)) */ +#define FUNCTION bcopy +#define SRCREG a0 +#define DSTREG a1 +#endif /* !(defined(MEMCOPY) || defined(MEMMOVE)) */ + +#define SIZEREG a2 + +/* + * Copy bytes. + * + * void bcopy(char *from, char *to, size_t len); + * char *memcpy(void *to, const void *from, size_t len); + * char *memmove(void *to, const void *from, size_t len); + * + * No matter how invoked, the source and destination registers + * for calculation. There's no point in copying them to "working" + * registers, since the code uses their values "in place," and + * copying them would be slower. + */ + +LEAF(FUNCTION,3) + +#if defined(MEMCOPY) || defined(MEMMOVE) + /* set up return value, while we still can */ + mov DSTREG,v0 +#endif + + /* Check for negative length */ + ble SIZEREG,bcopy_done + + /* Check for overlap */ + subq DSTREG,SRCREG,t5 + cmpult t5,SIZEREG,t5 + bne t5,bcopy_overlap + + /* a3 = end address */ + addq SRCREG,SIZEREG,a3 + + /* Get the first word */ + ldq_u t2,0(SRCREG) + + /* Do they have the same alignment? */ + xor SRCREG,DSTREG,t0 + and t0,7,t0 + and DSTREG,7,t1 + bne t0,bcopy_different_alignment + + /* src & dst have same alignment */ + beq t1,bcopy_all_aligned + + ldq_u t3,0(DSTREG) + addq SIZEREG,t1,SIZEREG + mskqh t2,SRCREG,t2 + mskql t3,SRCREG,t3 + or t2,t3,t2 + + /* Dst is 8-byte aligned */ + +bcopy_all_aligned: + /* If less than 8 bytes,skip loop */ + subq SIZEREG,1,t0 + and SIZEREG,7,SIZEREG + bic t0,7,t0 + beq t0,bcopy_samealign_lp_end + +bcopy_samealign_lp: + stq_u t2,0(DSTREG) + addq DSTREG,8,DSTREG + ldq_u t2,8(SRCREG) + subq t0,8,t0 + addq SRCREG,8,SRCREG + bne t0,bcopy_samealign_lp + +bcopy_samealign_lp_end: + /* If we're done, exit */ + bne SIZEREG,bcopy_small_left + stq_u t2,0(DSTREG) + RET + +bcopy_small_left: + mskql t2,SIZEREG,t4 + ldq_u t3,0(DSTREG) + mskqh t3,SIZEREG,t3 + or t4,t3,t4 + stq_u t4,0(DSTREG) + RET + +bcopy_different_alignment: + /* + * this is the fun part + */ + addq SRCREG,SIZEREG,a3 + cmpule SIZEREG,8,t0 + bne t0,bcopy_da_finish + + beq t1,bcopy_da_noentry + + /* Do the initial partial word */ + subq zero,DSTREG,t0 + and t0,7,t0 + ldq_u t3,7(SRCREG) + extql t2,SRCREG,t2 + extqh t3,SRCREG,t3 + or t2,t3,t5 + insql t5,DSTREG,t5 + ldq_u t6,0(DSTREG) + mskql t6,DSTREG,t6 + or t5,t6,t5 + stq_u t5,0(DSTREG) + addq SRCREG,t0,SRCREG + addq DSTREG,t0,DSTREG + subq SIZEREG,t0,SIZEREG + ldq_u t2,0(SRCREG) + +bcopy_da_noentry: + subq SIZEREG,1,t0 + bic t0,7,t0 + and SIZEREG,7,SIZEREG + beq t0,bcopy_da_finish2 + +bcopy_da_lp: + ldq_u t3,7(SRCREG) + addq SRCREG,8,SRCREG + extql t2,SRCREG,t4 + extqh t3,SRCREG,t5 + subq t0,8,t0 + or t4,t5,t5 + stq t5,0(DSTREG) + addq DSTREG,8,DSTREG + beq t0,bcopy_da_finish1 + ldq_u t2,7(SRCREG) + addq SRCREG,8,SRCREG + extql t3,SRCREG,t4 + extqh t2,SRCREG,t5 + subq t0,8,t0 + or t4,t5,t5 + stq t5,0(DSTREG) + addq DSTREG,8,DSTREG + bne t0,bcopy_da_lp + +bcopy_da_finish2: + /* Do the last new word */ + mov t2,t3 + +bcopy_da_finish1: + /* Do the last partial word */ + ldq_u t2,-1(a3) + extql t3,SRCREG,t3 + extqh t2,SRCREG,t2 + or t2,t3,t2 + br zero,bcopy_samealign_lp_end + +bcopy_da_finish: + /* Do the last word in the next source word */ + ldq_u t3,-1(a3) + extql t2,SRCREG,t2 + extqh t3,SRCREG,t3 + or t2,t3,t2 + insqh t2,DSTREG,t3 + insql t2,DSTREG,t2 + lda t4,-1(zero) + mskql t4,SIZEREG,t5 + cmovne t5,t5,t4 + insqh t4,DSTREG,t5 + insql t4,DSTREG,t4 + addq DSTREG,SIZEREG,a4 + ldq_u t6,0(DSTREG) + ldq_u t7,-1(a4) + bic t6,t4,t6 + bic t7,t5,t7 + and t2,t4,t2 + and t3,t5,t3 + or t2,t6,t2 + or t3,t7,t3 + stq_u t3,-1(a4) + stq_u t2,0(DSTREG) + RET + +bcopy_overlap: + /* + * Basically equivalent to previous case, only backwards. + * Not quite as highly optimized + */ + addq SRCREG,SIZEREG,a3 + addq DSTREG,SIZEREG,a4 + + /* less than 8 bytes - don't worry about overlap */ + cmpule SIZEREG,8,t0 + bne t0,bcopy_ov_short + + /* Possibly do a partial first word */ + and a4,7,t4 + beq t4,bcopy_ov_nostart2 + subq a3,t4,a3 + subq a4,t4,a4 + ldq_u t1,0(a3) + subq SIZEREG,t4,SIZEREG + ldq_u t2,7(a3) + ldq t3,0(a4) + extql t1,a3,t1 + extqh t2,a3,t2 + or t1,t2,t1 + mskqh t3,t4,t3 + mskql t1,t4,t1 + or t1,t3,t1 + stq t1,0(a4) + +bcopy_ov_nostart2: + bic SIZEREG,7,t4 + and SIZEREG,7,SIZEREG + beq t4,bcopy_ov_lp_end + +bcopy_ov_lp: + /* This could be more pipelined, but it doesn't seem worth it */ + ldq_u t0,-8(a3) + subq a4,8,a4 + ldq_u t1,-1(a3) + subq a3,8,a3 + extql t0,a3,t0 + extqh t1,a3,t1 + subq t4,8,t4 + or t0,t1,t0 + stq t0,0(a4) + bne t4,bcopy_ov_lp + +bcopy_ov_lp_end: + beq SIZEREG,bcopy_done + + ldq_u t0,0(SRCREG) + ldq_u t1,7(SRCREG) + ldq_u t2,0(DSTREG) + extql t0,SRCREG,t0 + extqh t1,SRCREG,t1 + or t0,t1,t0 + insql t0,DSTREG,t0 + mskql t2,DSTREG,t2 + or t2,t0,t2 + stq_u t2,0(DSTREG) + +bcopy_done: + RET + +bcopy_ov_short: + ldq_u t2,0(SRCREG) + br zero,bcopy_da_finish + + END(FUNCTION) diff --git a/common/lib/libc/arch/alpha/string/bzero.S b/common/lib/libc/arch/alpha/string/bzero.S new file mode 100644 index 000000000..a808b0d95 --- /dev/null +++ b/common/lib/libc/arch/alpha/string/bzero.S @@ -0,0 +1,110 @@ +/* $NetBSD: bzero.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright (c) 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Trevor Blackwell + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +LEAF(bzero,2) + ble a1,bzero_done + bic a1,63,t3 /* t3 is # bytes to do 64 bytes at a time */ + + /* If nothing in first word, ignore it */ + subq zero,a0,t0 + and t0,7,t0 /* t0 = (0-size)%8 */ + beq t0,bzero_nostart1 + + cmpult a1,t0,t1 /* if size > size%8 goto noshort */ + beq t1,bzero_noshort + + /* + * The whole thing is less than a word. + * Mask off 1..7 bytes, and finish. + */ + ldq_u t2,0(a0) + lda t0,-1(zero) /* t0=-1 */ + mskql t0,a1,t0 /* Get ff in bytes (a0%8)..((a0+a1-1)%8) */ + insql t0,a0,t0 + bic t2,t0,t2 /* zero those bytes in word */ + stq_u t2,0(a0) + RET + +bzero_noshort: + /* Handle the first partial word */ + ldq_u t2,0(a0) + subq a1,t0,a1 + mskql t2,a0,t2 /* zero bytes (a0%8)..7 in word */ + stq_u t2,0(a0) + + addq a0,t0,a0 /* round a0 up to next word */ + bic a1,63,t3 /* recalc t3 (# bytes to do 64 bytes at a + time) */ + +bzero_nostart1: + /* + * Loop, zeroing 64 bytes at a time + */ + beq t3,bzero_lp_done +bzero_lp: + stq zero,0(a0) + stq zero,8(a0) + stq zero,16(a0) + stq zero,24(a0) + subq t3,64,t3 + stq zero,32(a0) + stq zero,40(a0) + stq zero,48(a0) + stq zero,56(a0) + addq a0,64,a0 + bne t3,bzero_lp + +bzero_lp_done: + /* + * Handle the last 0..7 words. + * We mask off the low bits, so we don't need an extra + * compare instruction for the loop (just a bne. heh-heh) + */ + and a1,0x38,t4 + beq t4,bzero_finish_lp_done +bzero_finish_lp: + stq zero,0(a0) + subq t4,8,t4 + addq a0,8,a0 + bne t4,bzero_finish_lp + + /* Do the last partial word */ +bzero_finish_lp_done: + and a1,7,t5 /* 0..7 bytes left */ + beq t5,bzero_done /* mskqh won't change t0 if t5==0, but I + don't want to touch, say, a new VM page */ + ldq t0,0(a0) + mskqh t0,t5,t0 + stq t0,0(a0) +bzero_done: + RET + + END(bzero) diff --git a/common/lib/libc/arch/alpha/string/ffs.S b/common/lib/libc/arch/alpha/string/ffs.S new file mode 100644 index 000000000..788089a9c --- /dev/null +++ b/common/lib/libc/arch/alpha/string/ffs.S @@ -0,0 +1,94 @@ +/* $NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright (c) 1995 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. + * + * <> + */ + +#include + +LEAF(ffs, 1) + addl a0, 0, t0 + beq t0, Lallzero + + /* + * Initialize return value (v0), and set up t1 so that it + * contains the mask with only the lowest bit set. + */ + subl zero, t0, t1 + ldil v0, 1 + and t0, t1, t1 + + and t1, 0xff, t2 + bne t2, Ldo8 + + /* + * If lower 16 bits empty, add 16 to result and use upper 16. + */ + zapnot t1, 0x03, t3 + bne t3, Ldo16 + sra t1, 16, t1 + addl v0, 16, v0 + +Ldo16: + /* + * If lower 8 bits empty, add 8 to result and use upper 8. + */ + and t1, 0xff, t4 + bne t4, Ldo8 + sra t1, 8, t1 + addl v0, 8, v0 + +Ldo8: + and t1, 0x0f, t5 /* lower 4 of 8 empty? */ + and t1, 0x33, t6 /* lower 2 of each 4 empty? */ + and t1, 0x55, t7 /* lower 1 of each 2 empty? */ + + /* If lower 4 bits empty, add 4 to result. */ + bne t5, Ldo4 + addl v0, 4, v0 + +Ldo4: /* If lower 2 bits of each 4 empty, add 2 to result. */ + bne t6, Ldo2 + addl v0, 2, v0 + +Ldo2: /* If lower bit of each 2 empty, add 1 to result. */ + bne t7, Ldone + addl v0, 1, v0 + +Ldone: + RET + +Lallzero: + bis zero, zero, v0 + RET +END(ffs) diff --git a/common/lib/libc/arch/alpha/string/memcpy.S b/common/lib/libc/arch/alpha/string/memcpy.S new file mode 100644 index 000000000..11245b492 --- /dev/null +++ b/common/lib/libc/arch/alpha/string/memcpy.S @@ -0,0 +1,4 @@ +/* $NetBSD: memcpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +#define MEMCOPY +#include "bcopy.S" diff --git a/common/lib/libc/arch/alpha/string/memmove.S b/common/lib/libc/arch/alpha/string/memmove.S new file mode 100644 index 000000000..d9d518633 --- /dev/null +++ b/common/lib/libc/arch/alpha/string/memmove.S @@ -0,0 +1,4 @@ +/* $NetBSD: memmove.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +#define MEMMOVE +#include "bcopy.S" diff --git a/common/lib/libc/arch/arm/atomic/Makefile.inc b/common/lib/libc/arch/arm/atomic/Makefile.inc new file mode 100644 index 000000000..9f0233780 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/Makefile.inc @@ -0,0 +1,19 @@ +# $NetBSD: Makefile.inc,v 1.8 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c atomic_and_32_cas.c \ + atomic_and_32_nv_cas.c atomic_dec_32_cas.c atomic_dec_32_nv_cas.c \ + atomic_inc_32_cas.c atomic_inc_32_nv_cas.c atomic_or_32_cas.c \ + atomic_or_32_nv_cas.c atomic_swap_32_cas.c membar_ops_nop.c + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_testset.c +SRCS+= atomic_cas_up.S +CPPFLAGS+= -D__HAVE_ASM_ATOMIC_CAS_UP + +.endif diff --git a/common/lib/libc/arch/arm/atomic/atomic_add_32.S b/common/lib/libc/arch/arm/atomic/atomic_add_32.S new file mode 100644 index 000000000..e03472c79 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_add_32.S @@ -0,0 +1,70 @@ +/* $NetBSD: atomic_add_32.S,v 1.2 2008/08/16 07:12:39 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "atomic_op_asm.h" + +#ifdef _ARM_ARCH_6 + +ENTRY_NP(_atomic_add_32) + mov r3, r0 /* need r0 for return value */ +1: ldrex r0, [r3] /* load old value (to be returned) */ + add r2, r0, r1 /* calculate new value */ + strex ip, r2, [r3] /* try to store */ + cmp ip, #0 /* succeed? */ + bne 1b /* no, try again */ + RET /* return old value */ + END(_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_32,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_int,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_long,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_ptr,_atomic_add_32) +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_long,_atomic_add_32) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32) + +ENTRY_NP(_atomic_add_32_nv) + mov r3, r0 /* need r0 for return value */ +1: ldrex r0, [r3] /* load old value */ + add r0, r0, r1 /* calculate new value (return value) */ + strex r2, r0, [r3] /* try to store */ + cmp r2, #0 /* succeed? */ + bne 1b /* no, try again? */ + RET /* return new value */ + END(_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_32_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_int_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_long_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv) + +#endif /* _ARM_ARCH_6 */ diff --git a/common/lib/libc/arch/arm/atomic/atomic_and_32.S b/common/lib/libc/arch/arm/atomic/atomic_and_32.S new file mode 100644 index 000000000..d34bf0d49 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_and_32.S @@ -0,0 +1,66 @@ +/* $NetBSD: atomic_and_32.S,v 1.2 2008/08/16 07:12:39 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "atomic_op_asm.h" + +#ifdef _ARM_ARCH_6 + +ENTRY_NP(_atomic_and_32) + mov r3, r0 /* need r0 for return value */ +1: ldrex r0, [r3] /* load old value (to be returned) */ + and r2, r0, r1 /* calculate new value */ + strex ip, r2, [r3] /* try to store */ + cmp ip, #0 /* succeed? */ + bne 1b /* no, try again */ + RET /* return old value */ + END(_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_32,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_uint,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_ulong,_atomic_and_32) +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32) + +ENTRY_NP(_atomic_and_32_nv) + mov r3, r0 /* need r0 for return value */ +1: ldrex r0, [r3] /* load old value */ + and r0, r0, r1 /* calculate new value (return value) */ + strex r2, r0, [r3] /* try to store */ + cmp r2, #0 /* succeed? */ + bne 1b /* no, try again? */ + RET /* return new value */ + END(_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_32_nv,_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_uint_nv,_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv) + +#endif /* _ARM_ARCH_6 */ diff --git a/common/lib/libc/arch/arm/atomic/atomic_cas_32.S b/common/lib/libc/arch/arm/atomic/atomic_cas_32.S new file mode 100644 index 000000000..a77cfbad9 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_cas_32.S @@ -0,0 +1,58 @@ +/* $NetBSD: atomic_cas_32.S,v 1.2 2008/08/16 07:12:39 matt Exp $ */ +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "atomic_op_asm.h" + +#if defined(_ARM_ARCH_6) +/* + * ARMv6 has load-exclusive/store-exclusive which works for both user + * and kernel. + */ +ENTRY_NP(_atomic_cas_32) + mov r3, r0 /* we need r0 for return value */ +1: + ldrex r0, [r3] /* load old value */ + teq r0, r1 /* compare? */ + RETc(ne) /* return if different */ + strex ip, r2, [r3] /* store new value */ + cmp ip, #0 /* succeed? */ + bne 1b /* nope, try again. */ + RET /* yes, return. */ + END(_atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_32,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ulong,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ptr,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32) + +#endif /* _ARCH_ARM_6 */ diff --git a/common/lib/libc/arch/arm/atomic/atomic_cas_8.S b/common/lib/libc/arch/arm/atomic/atomic_cas_8.S new file mode 100644 index 000000000..98ebf2152 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_cas_8.S @@ -0,0 +1,63 @@ +/* $NetBSD: atomic_cas_8.S,v 1.1 2008/11/18 15:22:56 matt Exp $ */ +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 + +RCSID("$NetBSD: atomic_cas_8.S,v 1.1 2008/11/18 15:22:56 matt Exp $") + +ENTRY(atomic_cas_8) + XPUSH {r4,r5} /* we need some more registers */ + and r3, r0, #3 /* which byte do we replace? */ +#if __ARMEB__ + eor r3, r3, #3 /* bytes are reversed on BE */ +#endif + mov r3, r3, lsl #3 /* multiply by 8 */ + mov r1, r1, lsl r3 /* mov old value to correct byte */ + eor r2, r1, r2, lsl r3 /* move new value to correct byte */ +/* eor r2, r2, r1 */ /* new value is now (old ^ new) */ + mov r5, #0xff /* load mask */ + mov r5, r5, lsl r3 /* and move to correct byte */ + mov r3, r0 /* move pointer */ + +1: ldrex r4, [r3] /* load 32bit value */ + and r0, r4, r5 /* clear other bytes */ + teq r0, r1 /* equal old value? */ + bne 2f /* nope, bail. */ + eor r4, r4, r2 /* new == old ^ (old ^ new) */ + strex ip, r4, [r3] /* attempt to store it */ + cmp ip, #0 /* succeed? */ + bne 1b /* nope, try again. */ + +2: XPOP {r4,r5} /* don't need these anymore */ + and r1, r3, #3 +#if __ARMEB__ + eor r1, r1, #3 +#endif + mov r0, r0, lsr r1 /* shift it back to lsb byte */ + RET diff --git a/common/lib/libc/arch/arm/atomic/atomic_cas_up.S b/common/lib/libc/arch/arm/atomic/atomic_cas_up.S new file mode 100644 index 000000000..9fe854ea2 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_cas_up.S @@ -0,0 +1,43 @@ +/* $NetBSD: atomic_cas_up.S,v 1.2 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Steve C. Woodford. + * + * 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 + +ENTRY(_atomic_cas_up) + .hidden _C_LABEL(_atomic_cas_up) + mov r3, r0 +RAS_START_ASM_HIDDEN(_atomic_cas) + ldr r0, [r3] + cmp r0, r1 + streq r2, [r3] +RAS_END_ASM_HIDDEN(_atomic_cas) + RET diff --git a/common/lib/libc/arch/arm/atomic/atomic_dec_32.S b/common/lib/libc/arch/arm/atomic/atomic_dec_32.S new file mode 100644 index 000000000..68466d2a0 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_dec_32.S @@ -0,0 +1,69 @@ +/* $NetBSD: atomic_dec_32.S,v 1.2 2008/08/16 07:12:39 matt Exp $ */ +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "atomic_op_asm.h" + +#ifdef _ARM_ARCH_6 + +ENTRY_NP(_atomic_dec_32) + mov r2, r0 /* need r0 for return value */ +1: ldrex r0, [r2] /* load old value (return value) */ + sub r1, r0, #1 /* calculate new value */ + strex r3, r1, [r2] /* try to store */ + cmp r3, #0 /* succeed? */ + bne 1b /* no, try again? */ + RET /* return new value */ + END(_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_32,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_uint,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_ulong,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_ptr,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32) + +ENTRY_NP(_atomic_dec_32_nv) + mov r2, r0 /* need r0 for return value */ +1: ldrex r0, [r2] /* load old value */ + sub r0, r0, #1 /* calculate new value (return value) */ + strex r1, r0, [r2] /* try to store */ + cmp r1, #0 /* succeed? */ + bne 1b /* no, try again? */ + RET /* return new value */ + END(_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv) + +#endif /* _ARM_ARCH_6 */ diff --git a/common/lib/libc/arch/arm/atomic/atomic_inc_32.S b/common/lib/libc/arch/arm/atomic/atomic_inc_32.S new file mode 100644 index 000000000..01aa13061 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_inc_32.S @@ -0,0 +1,69 @@ +/* $NetBSD: atomic_inc_32.S,v 1.2 2008/08/16 07:12:39 matt Exp $ */ +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "atomic_op_asm.h" + +#ifdef _ARM_ARCH_6 + +ENTRY_NP(_atomic_inc_32) + mov r2, r0 /* need r0 for return value */ +1: ldrex r0, [r2] /* load old value (return value) */ + add r1, r0, #1 /* calculate new value */ + strex r3, r1, [r2] /* try to store */ + cmp r3, #0 /* succeed? */ + bne 1b /* no, try again? */ + RET /* return new value */ + END(_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_32,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_uint,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_ulong,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_ptr,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32) + +ENTRY_NP(_atomic_inc_32_nv) + mov r2, r0 /* need r0 for return value */ +1: ldrex r0, [r2] /* load old value */ + add r0, r0, #1 /* calculate new value (return value) */ + strex r1, r0, [r2] /* try to store */ + cmp r1, #0 /* succeed? */ + bne 1b /* no, try again? */ + RET /* return new value */ + END(_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv) + +#endif /* _ARCH_ARM_6 */ diff --git a/common/lib/libc/arch/arm/atomic/atomic_op_asm.h b/common/lib/libc/arch/arm/atomic/atomic_op_asm.h new file mode 100644 index 000000000..40548fbb4 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_op_asm.h @@ -0,0 +1,54 @@ +/* $NetBSD: atomic_op_asm.h,v 1.2 2008/08/16 07:12:39 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#endif /* _KERNEL */ + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/arm/atomic/atomic_or_32.S b/common/lib/libc/arch/arm/atomic/atomic_or_32.S new file mode 100644 index 000000000..380f6f5ae --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_or_32.S @@ -0,0 +1,65 @@ +/* $NetBSD: atomic_or_32.S,v 1.2 2008/08/16 07:12:39 matt Exp $ */ +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "atomic_op_asm.h" + +#ifdef _ARM_ARCH_6 + +ENTRY_NP(_atomic_or_32) + mov r3, r0 /* need r0 for return value */ +1: ldrex r0, [r3] /* load old value (to be returned) */ + orr r2, r0, r1 /* calculate new value */ + strex ip, r2, [r3] /* try to store */ + cmp ip, #0 /* succeed? */ + bne 1b /* no, try again */ + RET /* return old value */ + END(_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_32,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_uint,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_ulong,_atomic_or_32) +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32) + +ENTRY_NP(_atomic_or_32_nv) + mov r3, r0 /* need r0 for return value */ +1: ldrex r0, [r3] /* load old value */ + orr r0, r0, r1 /* calculate new value (return value) */ + strex r2, r0, [r3] /* try to store */ + cmp r2, #0 /* succeed? */ + bne 1b /* no, try again? */ + RET /* return new value */ + END(_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_32_nv,_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_uint_nv,_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv) + +#endif /* _ARM_ARCH_6 */ diff --git a/common/lib/libc/arch/arm/atomic/atomic_swap.S b/common/lib/libc/arch/arm/atomic/atomic_swap.S new file mode 100644 index 000000000..42a789724 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/atomic_swap.S @@ -0,0 +1,61 @@ +/* $NetBSD: atomic_swap.S,v 1.2 2008/08/16 07:12:40 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe and Matt Thomas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 "atomic_op_asm.h" + +ENTRY_NP(_atomic_swap_32) + swp r0, r1, [r0] + RET + END(_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_32,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_uint,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_ulong,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_ptr,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32) + +ENTRY_NP(_atomic_swap_8) + swpb r0, r1, [r0] + RET + END(_atomic_swap_8) +ATOMIC_OP_ALIAS(atomic_swap_8,_atomic_swap_8) +ATOMIC_OP_ALIAS(atomic_swap_char,_atomic_swap_8) +ATOMIC_OP_ALIAS(atomic_swap_uchar,_atomic_swap_8) +STRONG_ALIAS(_atomic_swap_char,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_uchar,_atomic_swap_32) diff --git a/common/lib/libc/arch/arm/atomic/membar_ops.S b/common/lib/libc/arch/arm/atomic/membar_ops.S new file mode 100644 index 000000000..c8af9f9d7 --- /dev/null +++ b/common/lib/libc/arch/arm/atomic/membar_ops.S @@ -0,0 +1,57 @@ +/* $NetBSD: membar_ops.S,v 1.2 2008/08/16 07:12:40 matt Exp $ */ +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "atomic_op_asm.h" + +#ifdef _ARM_ARCH_6 + +ENTRY_NP(_membar_producer) + mcr p15, 0, r0, c7, c10, 4 /* Data Synchronization Barrier */ + RET + END(_membar_producer) +ATOMIC_OP_ALIAS(membar_producer,_membar_producer) +ATOMIC_OP_ALIAS(membar_write,_membar_producer) +STRONG_ALIAS(_membar_write,_membar_producer) + +ENTRY_NP(_membar_sync) + mcr p15, 0, r0, c7, c10, 5 /* Data Memory Barrier */ + RET + END(_membar_sync) +ATOMIC_OP_ALIAS(membar_sync,_membar_sync) +ATOMIC_OP_ALIAS(membar_enter,_membar_sync) +ATOMIC_OP_ALIAS(membar_exit,_membar_sync) +ATOMIC_OP_ALIAS(membar_consumer,_membar_sync) +ATOMIC_OP_ALIAS(membar_read,_membar_sync) +STRONG_ALIAS(_membar_enter,_membar_sync) +STRONG_ALIAS(_membar_exit,_membar_sync) +STRONG_ALIAS(_membar_consumer,_membar_sync) +STRONG_ALIAS(_membar_read,_membar_sync) + +#endif /* _ARM_ARCH_6 */ diff --git a/common/lib/libc/arch/arm/gen/byte_swap_2.S b/common/lib/libc/arch/arm/gen/byte_swap_2.S new file mode 100644 index 000000000..843bd9227 --- /dev/null +++ b/common/lib/libc/arch/arm/gen/byte_swap_2.S @@ -0,0 +1,47 @@ +/* $NetBSD: byte_swap_2.S,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles M. Hannum. + * + * 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 + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap16)) +#else +_ENTRY(_C_LABEL(__bswap16)) +#endif +#if BYTE_ORDER == LITTLE_ENDIAN +_ENTRY(_C_LABEL(ntohs)) +_ENTRY(_C_LABEL(htons)) +#endif +_PROF_PROLOGUE + and r1, r0, #0xff + mov r0, r0, lsr #8 + orr r0, r0, r1, lsl #8 + RET diff --git a/common/lib/libc/arch/arm/gen/byte_swap_4.S b/common/lib/libc/arch/arm/gen/byte_swap_4.S new file mode 100644 index 000000000..265b898d9 --- /dev/null +++ b/common/lib/libc/arch/arm/gen/byte_swap_4.S @@ -0,0 +1,48 @@ +/* $NetBSD: byte_swap_4.S,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Neil A. Carson + * + * 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 + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap32)) +#else +_ENTRY(_C_LABEL(__bswap32)) +#endif +#if BYTE_ORDER == LITTLE_ENDIAN +_ENTRY(_C_LABEL(ntohl)) +_ENTRY(_C_LABEL(htonl)) +#endif +_PROF_PROLOGUE + eor r1, r0, r0, ror #16 + bic r1, r1, #0x00FF0000 + mov r0, r0, ror #8 + eor r0, r0, r1, lsr #8 + RET diff --git a/common/lib/libc/arch/arm/gen/divsi3.S b/common/lib/libc/arch/arm/gen/divsi3.S new file mode 100644 index 000000000..75a927b32 --- /dev/null +++ b/common/lib/libc/arch/arm/gen/divsi3.S @@ -0,0 +1,386 @@ +/* $NetBSD: divsi3.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * 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 + +/* + * stack is aligned as there's a possibility of branching to .L_overflow + * which makes a C call + */ + +ENTRY(__umodsi3) + stmfd sp!, {lr} + sub sp, sp, #4 /* align stack */ + bl .L_udivide + add sp, sp, #4 /* unalign stack */ + mov r0, r1 + ldmfd sp!, {pc} + +ENTRY(__modsi3) + stmfd sp!, {lr} + sub sp, sp, #4 /* align stack */ + bl .L_divide + add sp, sp, #4 /* unalign stack */ + mov r0, r1 + ldmfd sp!, {pc} + +.L_overflow: +#if !defined(_KERNEL) && !defined(_STANDALONE) + mov r0, #8 /* SIGFPE */ + bl PIC_SYM(_C_LABEL(raise), PLT) /* raise it */ + mov r0, #0 +#else + /* XXX should cause a fatal error */ + mvn r0, #0 +#endif + RET + +ENTRY(__udivsi3) +.L_udivide: /* r0 = r0 / r1; r1 = r0 % r1 */ + eor r0, r1, r0 + eor r1, r0, r1 + eor r0, r1, r0 + /* r0 = r1 / r0; r1 = r1 % r0 */ + cmp r0, #1 + bcc .L_overflow + beq .L_divide_l0 + mov ip, #0 + movs r1, r1 + bpl .L_divide_l1 + orr ip, ip, #0x20000000 /* ip bit 0x20000000 = -ve r1 */ + movs r1, r1, lsr #1 + orrcs ip, ip, #0x10000000 /* ip bit 0x10000000 = bit 0 of r1 */ + b .L_divide_l1 + +.L_divide_l0: /* r0 == 1 */ + mov r0, r1 + mov r1, #0 + RET + +ENTRY(__divsi3) +.L_divide: /* r0 = r0 / r1; r1 = r0 % r1 */ + eor r0, r1, r0 + eor r1, r0, r1 + eor r0, r1, r0 + /* r0 = r1 / r0; r1 = r1 % r0 */ + cmp r0, #1 + bcc .L_overflow + beq .L_divide_l0 + ands ip, r0, #0x80000000 + rsbmi r0, r0, #0 + ands r2, r1, #0x80000000 + eor ip, ip, r2 + rsbmi r1, r1, #0 + orr ip, r2, ip, lsr #1 /* ip bit 0x40000000 = -ve division */ + /* ip bit 0x80000000 = -ve remainder */ + +.L_divide_l1: + mov r2, #1 + mov r3, #0 + + /* + * If the highest bit of the dividend is set, we have to be + * careful when shifting the divisor. Test this. + */ + movs r1,r1 + bpl .L_old_code + + /* + * At this point, the highest bit of r1 is known to be set. + * We abuse this below in the tst instructions. + */ + tst r1, r0 /*, lsl #0 */ + bmi .L_divide_b1 + tst r1, r0, lsl #1 + bmi .L_divide_b2 + tst r1, r0, lsl #2 + bmi .L_divide_b3 + tst r1, r0, lsl #3 + bmi .L_divide_b4 + tst r1, r0, lsl #4 + bmi .L_divide_b5 + tst r1, r0, lsl #5 + bmi .L_divide_b6 + tst r1, r0, lsl #6 + bmi .L_divide_b7 + tst r1, r0, lsl #7 + bmi .L_divide_b8 + tst r1, r0, lsl #8 + bmi .L_divide_b9 + tst r1, r0, lsl #9 + bmi .L_divide_b10 + tst r1, r0, lsl #10 + bmi .L_divide_b11 + tst r1, r0, lsl #11 + bmi .L_divide_b12 + tst r1, r0, lsl #12 + bmi .L_divide_b13 + tst r1, r0, lsl #13 + bmi .L_divide_b14 + tst r1, r0, lsl #14 + bmi .L_divide_b15 + tst r1, r0, lsl #15 + bmi .L_divide_b16 + tst r1, r0, lsl #16 + bmi .L_divide_b17 + tst r1, r0, lsl #17 + bmi .L_divide_b18 + tst r1, r0, lsl #18 + bmi .L_divide_b19 + tst r1, r0, lsl #19 + bmi .L_divide_b20 + tst r1, r0, lsl #20 + bmi .L_divide_b21 + tst r1, r0, lsl #21 + bmi .L_divide_b22 + tst r1, r0, lsl #22 + bmi .L_divide_b23 + tst r1, r0, lsl #23 + bmi .L_divide_b24 + tst r1, r0, lsl #24 + bmi .L_divide_b25 + tst r1, r0, lsl #25 + bmi .L_divide_b26 + tst r1, r0, lsl #26 + bmi .L_divide_b27 + tst r1, r0, lsl #27 + bmi .L_divide_b28 + tst r1, r0, lsl #28 + bmi .L_divide_b29 + tst r1, r0, lsl #29 + bmi .L_divide_b30 + tst r1, r0, lsl #30 + bmi .L_divide_b31 +/* + * instead of: + * tst r1, r0, lsl #31 + * bmi .L_divide_b32 + */ + b .L_divide_b32 + +.L_old_code: + cmp r1, r0 + bcc .L_divide_b0 + cmp r1, r0, lsl #1 + bcc .L_divide_b1 + cmp r1, r0, lsl #2 + bcc .L_divide_b2 + cmp r1, r0, lsl #3 + bcc .L_divide_b3 + cmp r1, r0, lsl #4 + bcc .L_divide_b4 + cmp r1, r0, lsl #5 + bcc .L_divide_b5 + cmp r1, r0, lsl #6 + bcc .L_divide_b6 + cmp r1, r0, lsl #7 + bcc .L_divide_b7 + cmp r1, r0, lsl #8 + bcc .L_divide_b8 + cmp r1, r0, lsl #9 + bcc .L_divide_b9 + cmp r1, r0, lsl #10 + bcc .L_divide_b10 + cmp r1, r0, lsl #11 + bcc .L_divide_b11 + cmp r1, r0, lsl #12 + bcc .L_divide_b12 + cmp r1, r0, lsl #13 + bcc .L_divide_b13 + cmp r1, r0, lsl #14 + bcc .L_divide_b14 + cmp r1, r0, lsl #15 + bcc .L_divide_b15 + cmp r1, r0, lsl #16 + bcc .L_divide_b16 + cmp r1, r0, lsl #17 + bcc .L_divide_b17 + cmp r1, r0, lsl #18 + bcc .L_divide_b18 + cmp r1, r0, lsl #19 + bcc .L_divide_b19 + cmp r1, r0, lsl #20 + bcc .L_divide_b20 + cmp r1, r0, lsl #21 + bcc .L_divide_b21 + cmp r1, r0, lsl #22 + bcc .L_divide_b22 + cmp r1, r0, lsl #23 + bcc .L_divide_b23 + cmp r1, r0, lsl #24 + bcc .L_divide_b24 + cmp r1, r0, lsl #25 + bcc .L_divide_b25 + cmp r1, r0, lsl #26 + bcc .L_divide_b26 + cmp r1, r0, lsl #27 + bcc .L_divide_b27 + cmp r1, r0, lsl #28 + bcc .L_divide_b28 + cmp r1, r0, lsl #29 + bcc .L_divide_b29 + cmp r1, r0, lsl #30 + bcc .L_divide_b30 +.L_divide_b32: + cmp r1, r0, lsl #31 + subhs r1, r1,r0, lsl #31 + addhs r3, r3,r2, lsl #31 +.L_divide_b31: + cmp r1, r0, lsl #30 + subhs r1, r1,r0, lsl #30 + addhs r3, r3,r2, lsl #30 +.L_divide_b30: + cmp r1, r0, lsl #29 + subhs r1, r1,r0, lsl #29 + addhs r3, r3,r2, lsl #29 +.L_divide_b29: + cmp r1, r0, lsl #28 + subhs r1, r1,r0, lsl #28 + addhs r3, r3,r2, lsl #28 +.L_divide_b28: + cmp r1, r0, lsl #27 + subhs r1, r1,r0, lsl #27 + addhs r3, r3,r2, lsl #27 +.L_divide_b27: + cmp r1, r0, lsl #26 + subhs r1, r1,r0, lsl #26 + addhs r3, r3,r2, lsl #26 +.L_divide_b26: + cmp r1, r0, lsl #25 + subhs r1, r1,r0, lsl #25 + addhs r3, r3,r2, lsl #25 +.L_divide_b25: + cmp r1, r0, lsl #24 + subhs r1, r1,r0, lsl #24 + addhs r3, r3,r2, lsl #24 +.L_divide_b24: + cmp r1, r0, lsl #23 + subhs r1, r1,r0, lsl #23 + addhs r3, r3,r2, lsl #23 +.L_divide_b23: + cmp r1, r0, lsl #22 + subhs r1, r1,r0, lsl #22 + addhs r3, r3,r2, lsl #22 +.L_divide_b22: + cmp r1, r0, lsl #21 + subhs r1, r1,r0, lsl #21 + addhs r3, r3,r2, lsl #21 +.L_divide_b21: + cmp r1, r0, lsl #20 + subhs r1, r1,r0, lsl #20 + addhs r3, r3,r2, lsl #20 +.L_divide_b20: + cmp r1, r0, lsl #19 + subhs r1, r1,r0, lsl #19 + addhs r3, r3,r2, lsl #19 +.L_divide_b19: + cmp r1, r0, lsl #18 + subhs r1, r1,r0, lsl #18 + addhs r3, r3,r2, lsl #18 +.L_divide_b18: + cmp r1, r0, lsl #17 + subhs r1, r1,r0, lsl #17 + addhs r3, r3,r2, lsl #17 +.L_divide_b17: + cmp r1, r0, lsl #16 + subhs r1, r1,r0, lsl #16 + addhs r3, r3,r2, lsl #16 +.L_divide_b16: + cmp r1, r0, lsl #15 + subhs r1, r1,r0, lsl #15 + addhs r3, r3,r2, lsl #15 +.L_divide_b15: + cmp r1, r0, lsl #14 + subhs r1, r1,r0, lsl #14 + addhs r3, r3,r2, lsl #14 +.L_divide_b14: + cmp r1, r0, lsl #13 + subhs r1, r1,r0, lsl #13 + addhs r3, r3,r2, lsl #13 +.L_divide_b13: + cmp r1, r0, lsl #12 + subhs r1, r1,r0, lsl #12 + addhs r3, r3,r2, lsl #12 +.L_divide_b12: + cmp r1, r0, lsl #11 + subhs r1, r1,r0, lsl #11 + addhs r3, r3,r2, lsl #11 +.L_divide_b11: + cmp r1, r0, lsl #10 + subhs r1, r1,r0, lsl #10 + addhs r3, r3,r2, lsl #10 +.L_divide_b10: + cmp r1, r0, lsl #9 + subhs r1, r1,r0, lsl #9 + addhs r3, r3,r2, lsl #9 +.L_divide_b9: + cmp r1, r0, lsl #8 + subhs r1, r1,r0, lsl #8 + addhs r3, r3,r2, lsl #8 +.L_divide_b8: + cmp r1, r0, lsl #7 + subhs r1, r1,r0, lsl #7 + addhs r3, r3,r2, lsl #7 +.L_divide_b7: + cmp r1, r0, lsl #6 + subhs r1, r1,r0, lsl #6 + addhs r3, r3,r2, lsl #6 +.L_divide_b6: + cmp r1, r0, lsl #5 + subhs r1, r1,r0, lsl #5 + addhs r3, r3,r2, lsl #5 +.L_divide_b5: + cmp r1, r0, lsl #4 + subhs r1, r1,r0, lsl #4 + addhs r3, r3,r2, lsl #4 +.L_divide_b4: + cmp r1, r0, lsl #3 + subhs r1, r1,r0, lsl #3 + addhs r3, r3,r2, lsl #3 +.L_divide_b3: + cmp r1, r0, lsl #2 + subhs r1, r1,r0, lsl #2 + addhs r3, r3,r2, lsl #2 +.L_divide_b2: + cmp r1, r0, lsl #1 + subhs r1, r1,r0, lsl #1 + addhs r3, r3,r2, lsl #1 +.L_divide_b1: + cmp r1, r0 + subhs r1, r1, r0 + addhs r3, r3, r2 +.L_divide_b0: + + tst ip, #0x20000000 + bne .L_udivide_l1 + mov r0, r3 + cmp ip, #0 + rsbmi r1, r1, #0 + movs ip, ip, lsl #1 + bicmi r0, r0, #0x80000000 /* Fix incase we divided 0x80000000 */ + rsbmi r0, r0, #0 + RET + +.L_udivide_l1: + tst ip, #0x10000000 + mov r1, r1, lsl #1 + orrne r1, r1, #1 + mov r3, r3, lsl #1 + cmp r1, r0 + subhs r1, r1, r0 + addhs r3, r3, r2 + mov r0, r3 + RET diff --git a/common/lib/libc/arch/arm/string/ffs.S b/common/lib/libc/arch/arm/string/ffs.S new file mode 100644 index 000000000..a1f108e1d --- /dev/null +++ b/common/lib/libc/arch/arm/string/ffs.S @@ -0,0 +1,85 @@ +/* $NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ +/* + * Copyright (c) 2001 Christopher Gilbert + * 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 company nor 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 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 + +RCSID("$NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos Exp $") + +/* + * ffs - find first set bit, this algorithm isolates the first set + * bit, then multiplies the number by 0x0450fbaf which leaves the top + * 6 bits as an index into the table. This algorithm should be a win + * over the checking each bit in turn as per the C compiled version. + * + * On ARMv5 we use CLZ (count leading Zero's) and then subtract the result + * from 32. + * + * This is the ffs algorithm devised by d.seal and posted to comp.sys.arm on + * 16 Feb 1994. + */ + +ENTRY(ffs) +#ifdef _ARM_ARCH_5 + /* (X & -X) gives LSB or zero. */ + rsb r1, r0, #0 + and r0, r0, r1 + clz r0, r0 + rsb r0, r0, #32 + RET +#else + /* Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry */ + rsb r1, r0, #0 + ands r0, r0, r1 + /* + * now r0 has at most one set bit, call this X + * if X = 0, all further instructions are skipped + */ + adrne r2, .L_ffs_table + orrne r0, r0, r0, lsl #4 /* r0 = X * 0x11 */ + orrne r0, r0, r0, lsl #6 /* r0 = X * 0x451 */ + rsbne r0, r0, r0, lsl #16 /* r0 = X * 0x0450fbaf */ + + /* now lookup in table indexed on top 6 bits of r0 */ + ldrneb r0, [ r2, r0, lsr #26 ] + + RET +.text; +.type .L_ffs_table, _ASM_TYPE_OBJECT; +.L_ffs_table: +/* 0 1 2 3 4 5 6 7 */ + .byte 0, 1, 2, 13, 3, 7, 0, 14 /* 0- 7 */ + .byte 4, 0, 8, 0, 0, 0, 0, 15 /* 8-15 */ + .byte 11, 5, 0, 0, 9, 0, 0, 26 /* 16-23 */ + .byte 0, 0, 0, 0, 0, 22, 28, 16 /* 24-31 */ + .byte 32, 12, 6, 0, 0, 0, 0, 0 /* 32-39 */ + .byte 10, 0, 0, 25, 0, 0, 21, 27 /* 40-47 */ + .byte 31, 0, 0, 0, 0, 24, 0, 20 /* 48-55 */ + .byte 30, 0, 23, 19, 29, 18, 17, 0 /* 56-63 */ +#endif diff --git a/common/lib/libc/arch/arm/string/memcmp.S b/common/lib/libc/arch/arm/string/memcmp.S new file mode 100644 index 000000000..eb2e0a872 --- /dev/null +++ b/common/lib/libc/arch/arm/string/memcmp.S @@ -0,0 +1,180 @@ +/* $NetBSD: memcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Steve C. Woodford for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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. + */ +/* + * Copyright (c) 2002 ARM Ltd + * 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 company may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY ARM LTD ``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 ARM LTD 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 + +RCSID("$NetBSD: memcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $") + +ENTRY(memcmp) + mov ip, r0 +#if defined(_KERNEL) && !defined(_STANDALONE) + cmp r2, #0x06 + beq .Lmemcmp_6bytes +#endif + mov r0, #0x00 + + /* Are both addresses aligned the same way? */ + cmp r2, #0x00 + eornes r3, ip, r1 + RETc(eq) /* len == 0, or same addresses! */ + tst r3, #0x03 + subne r2, r2, #0x01 + bne .Lmemcmp_bytewise2 /* Badly aligned. Do it the slow way */ + + /* Word-align the addresses, if necessary */ + sub r3, r1, #0x05 + ands r3, r3, #0x03 + add r3, r3, r3, lsl #1 + addne pc, pc, r3, lsl #3 + nop + + /* Compare up to 3 bytes */ + ldrb r0, [ip], #0x01 + ldrb r3, [r1], #0x01 + subs r0, r0, r3 + RETc(ne) + subs r2, r2, #0x01 + RETc(eq) + + /* Compare up to 2 bytes */ + ldrb r0, [ip], #0x01 + ldrb r3, [r1], #0x01 + subs r0, r0, r3 + RETc(ne) + subs r2, r2, #0x01 + RETc(eq) + + /* Compare 1 byte */ + ldrb r0, [ip], #0x01 + ldrb r3, [r1], #0x01 + subs r0, r0, r3 + RETc(ne) + subs r2, r2, #0x01 + RETc(eq) + + /* Compare 4 bytes at a time, if possible */ + subs r2, r2, #0x04 + bcc .Lmemcmp_bytewise +.Lmemcmp_word_aligned: + ldr r0, [ip], #0x04 + ldr r3, [r1], #0x04 + subs r2, r2, #0x04 + cmpcs r0, r3 + beq .Lmemcmp_word_aligned + sub r0, r0, r3 + + /* Correct for extra subtraction, and check if done */ + adds r2, r2, #0x04 + cmpeq r0, #0x00 /* If done, did all bytes match? */ + RETc(eq) /* Yup. Just return */ + + /* Re-do the final word byte-wise */ + sub ip, ip, #0x04 + sub r1, r1, #0x04 + +.Lmemcmp_bytewise: + add r2, r2, #0x03 +.Lmemcmp_bytewise2: + ldrb r0, [ip], #0x01 + ldrb r3, [r1], #0x01 + subs r2, r2, #0x01 + cmpcs r0, r3 + beq .Lmemcmp_bytewise2 + sub r0, r0, r3 + RET + +#if defined(_KERNEL) && !defined(_STANDALONE) + /* + * 6 byte compares are very common, thanks to the network stack. + * This code is hand-scheduled to reduce the number of stalls for + * load results. Everything else being equal, this will be ~32% + * faster than a byte-wise memcmp. + */ + .align 5 +.Lmemcmp_6bytes: + ldrb r3, [r1, #0x00] /* r3 = b2#0 */ + ldrb r0, [ip, #0x00] /* r0 = b1#0 */ + ldrb r2, [r1, #0x01] /* r2 = b2#1 */ + subs r0, r0, r3 /* r0 = b1#0 - b2#0 */ + ldreqb r3, [ip, #0x01] /* r3 = b1#1 */ + RETc(ne) /* Return if mismatch on #0 */ + subs r0, r3, r2 /* r0 = b1#1 - b2#1 */ + ldreqb r3, [r1, #0x02] /* r3 = b2#2 */ + ldreqb r0, [ip, #0x02] /* r0 = b1#2 */ + RETc(ne) /* Return if mismatch on #1 */ + ldrb r2, [r1, #0x03] /* r2 = b2#3 */ + subs r0, r0, r3 /* r0 = b1#2 - b2#2 */ + ldreqb r3, [ip, #0x03] /* r3 = b1#3 */ + RETc(ne) /* Return if mismatch on #2 */ + subs r0, r3, r2 /* r0 = b1#3 - b2#3 */ + ldreqb r3, [r1, #0x04] /* r3 = b2#4 */ + ldreqb r0, [ip, #0x04] /* r0 = b1#4 */ + RETc(ne) /* Return if mismatch on #3 */ + ldrb r2, [r1, #0x05] /* r2 = b2#5 */ + subs r0, r0, r3 /* r0 = b1#4 - b2#4 */ + ldreqb r3, [ip, #0x05] /* r3 = b1#5 */ + RETc(ne) /* Return if mismatch on #4 */ + sub r0, r3, r2 /* r0 = b1#5 - b2#5 */ + RET +#endif diff --git a/common/lib/libc/arch/arm/string/memcpy.S b/common/lib/libc/arch/arm/string/memcpy.S new file mode 100644 index 000000000..c2c65a0ab --- /dev/null +++ b/common/lib/libc/arch/arm/string/memcpy.S @@ -0,0 +1,7 @@ +/* $NetBSD: memcpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +#if !defined(__XSCALE__) || defined(_STANDALONE) +#include "memcpy_arm.S" +#else +#include "memcpy_xscale.S" +#endif diff --git a/common/lib/libc/arch/arm/string/memcpy_arm.S b/common/lib/libc/arch/arm/string/memcpy_arm.S new file mode 100644 index 000000000..81c1a55be --- /dev/null +++ b/common/lib/libc/arch/arm/string/memcpy_arm.S @@ -0,0 +1,332 @@ +/* $NetBSD: memcpy_arm.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Neil A. Carson and Mark Brinicombe + * + * 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 + +/* + * This is one fun bit of code ... + * Some easy listening music is suggested while trying to understand this + * code e.g. Iron Maiden + * + * For anyone attempting to understand it : + * + * The core code is implemented here with simple stubs for memcpy(). + * + * All local labels are prefixed with Lmemcpy_ + * Following the prefix a label starting f is used in the forward copy code + * while a label using b is used in the backwards copy code + * The source and destination addresses determine whether a forward or + * backward copy is performed. + * Separate bits of code are used to deal with the following situations + * for both the forward and backwards copy. + * unaligned source address + * unaligned destination address + * Separate copy routines are used to produce an optimised result for each + * of these cases. + * The copy code will use LDM/STM instructions to copy up to 32 bytes at + * a time where possible. + * + * Note: r12 (aka ip) can be trashed during the function along with + * r0-r3 although r0-r2 have defined uses i.e. src, dest, len through out. + * Additional registers are preserved prior to use i.e. r4, r5 & lr + * + * Apologies for the state of the comments ;-) + */ +/* LINTSTUB: Func: void *memcpy(void *dst, const void *src, size_t len) */ +ENTRY(memcpy) + /* save leaf functions having to store this away */ + stmdb sp!, {r0, lr} /* memcpy() returns dest addr */ + + subs r2, r2, #4 + blt .Lmemcpy_l4 /* less than 4 bytes */ + ands r12, r0, #3 + bne .Lmemcpy_destul /* oh unaligned destination addr */ + ands r12, r1, #3 + bne .Lmemcpy_srcul /* oh unaligned source addr */ + +.Lmemcpy_t8: + /* We have aligned source and destination */ + subs r2, r2, #8 + blt .Lmemcpy_l12 /* less than 12 bytes (4 from above) */ + subs r2, r2, #0x14 + blt .Lmemcpy_l32 /* less than 32 bytes (12 from above) */ + stmdb sp!, {r4} /* borrow r4 */ + + /* blat 32 bytes at a time */ + /* XXX for really big copies perhaps we should use more registers */ +.Lmemcpy_loop32: + ldmia r1!, {r3, r4, r12, lr} + stmia r0!, {r3, r4, r12, lr} + ldmia r1!, {r3, r4, r12, lr} + stmia r0!, {r3, r4, r12, lr} + subs r2, r2, #0x20 + bge .Lmemcpy_loop32 + + cmn r2, #0x10 + ldmgeia r1!, {r3, r4, r12, lr} /* blat a remaining 16 bytes */ + stmgeia r0!, {r3, r4, r12, lr} + subge r2, r2, #0x10 + ldmia sp!, {r4} /* return r4 */ + +.Lmemcpy_l32: + adds r2, r2, #0x14 + + /* blat 12 bytes at a time */ +.Lmemcpy_loop12: + ldmgeia r1!, {r3, r12, lr} + stmgeia r0!, {r3, r12, lr} + subges r2, r2, #0x0c + bge .Lmemcpy_loop12 + +.Lmemcpy_l12: + adds r2, r2, #8 + blt .Lmemcpy_l4 + + subs r2, r2, #4 + ldrlt r3, [r1], #4 + strlt r3, [r0], #4 + ldmgeia r1!, {r3, r12} + stmgeia r0!, {r3, r12} + subge r2, r2, #4 + +.Lmemcpy_l4: + /* less than 4 bytes to go */ + adds r2, r2, #4 +#ifdef __APCS_26_ + ldmeqia sp!, {r0, pc}^ /* done */ +#else + ldmeqia sp!, {r0, pc} /* done */ +#endif + /* copy the crud byte at a time */ + cmp r2, #2 + ldrb r3, [r1], #1 + strb r3, [r0], #1 + ldrgeb r3, [r1], #1 + strgeb r3, [r0], #1 + ldrgtb r3, [r1], #1 + strgtb r3, [r0], #1 + ldmia sp!, {r0, pc} + + /* erg - unaligned destination */ +.Lmemcpy_destul: + rsb r12, r12, #4 + cmp r12, #2 + + /* align destination with byte copies */ + ldrb r3, [r1], #1 + strb r3, [r0], #1 + ldrgeb r3, [r1], #1 + strgeb r3, [r0], #1 + ldrgtb r3, [r1], #1 + strgtb r3, [r0], #1 + subs r2, r2, r12 + blt .Lmemcpy_l4 /* less the 4 bytes */ + + ands r12, r1, #3 + beq .Lmemcpy_t8 /* we have an aligned source */ + + /* erg - unaligned source */ + /* This is where it gets nasty ... */ +.Lmemcpy_srcul: + bic r1, r1, #3 + ldr lr, [r1], #4 + cmp r12, #2 + bgt .Lmemcpy_srcul3 + beq .Lmemcpy_srcul2 + cmp r2, #0x0c + blt .Lmemcpy_srcul1loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5} + +.Lmemcpy_srcul1loop16: +#ifdef __ARMEB__ + mov r3, lr, lsl #8 +#else + mov r3, lr, lsr #8 +#endif + ldmia r1!, {r4, r5, r12, lr} +#ifdef __ARMEB__ + orr r3, r3, r4, lsr #24 + mov r4, r4, lsl #8 + orr r4, r4, r5, lsr #24 + mov r5, r5, lsl #8 + orr r5, r5, r12, lsr #24 + mov r12, r12, lsl #8 + orr r12, r12, lr, lsr #24 +#else + orr r3, r3, r4, lsl #24 + mov r4, r4, lsr #8 + orr r4, r4, r5, lsl #24 + mov r5, r5, lsr #8 + orr r5, r5, r12, lsl #24 + mov r12, r12, lsr #8 + orr r12, r12, lr, lsl #24 +#endif + stmia r0!, {r3-r5, r12} + subs r2, r2, #0x10 + bge .Lmemcpy_srcul1loop16 + ldmia sp!, {r4, r5} + adds r2, r2, #0x0c + blt .Lmemcpy_srcul1l4 + +.Lmemcpy_srcul1loop4: +#ifdef __ARMEB__ + mov r12, lr, lsl #8 +#else + mov r12, lr, lsr #8 +#endif + ldr lr, [r1], #4 +#ifdef __ARMEB__ + orr r12, r12, lr, lsr #24 +#else + orr r12, r12, lr, lsl #24 +#endif + str r12, [r0], #4 + subs r2, r2, #4 + bge .Lmemcpy_srcul1loop4 + +.Lmemcpy_srcul1l4: + sub r1, r1, #3 + b .Lmemcpy_l4 + +.Lmemcpy_srcul2: + cmp r2, #0x0c + blt .Lmemcpy_srcul2loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5} + +.Lmemcpy_srcul2loop16: +#ifdef __ARMEB__ + mov r3, lr, lsl #16 +#else + mov r3, lr, lsr #16 +#endif + ldmia r1!, {r4, r5, r12, lr} +#ifdef __ARMEB__ + orr r3, r3, r4, lsr #16 + mov r4, r4, lsl #16 + orr r4, r4, r5, lsr #16 + mov r5, r5, lsl #16 + orr r5, r5, r12, lsr #16 + mov r12, r12, lsl #16 + orr r12, r12, lr, lsr #16 +#else + orr r3, r3, r4, lsl #16 + mov r4, r4, lsr #16 + orr r4, r4, r5, lsl #16 + mov r5, r5, lsr #16 + orr r5, r5, r12, lsl #16 + mov r12, r12, lsr #16 + orr r12, r12, lr, lsl #16 +#endif + stmia r0!, {r3-r5, r12} + subs r2, r2, #0x10 + bge .Lmemcpy_srcul2loop16 + ldmia sp!, {r4, r5} + adds r2, r2, #0x0c + blt .Lmemcpy_srcul2l4 + +.Lmemcpy_srcul2loop4: +#ifdef __ARMEB__ + mov r12, lr, lsl #16 +#else + mov r12, lr, lsr #16 +#endif + ldr lr, [r1], #4 +#ifdef __ARMEB__ + orr r12, r12, lr, lsr #16 +#else + orr r12, r12, lr, lsl #16 +#endif + str r12, [r0], #4 + subs r2, r2, #4 + bge .Lmemcpy_srcul2loop4 + +.Lmemcpy_srcul2l4: + sub r1, r1, #2 + b .Lmemcpy_l4 + +.Lmemcpy_srcul3: + cmp r2, #0x0c + blt .Lmemcpy_srcul3loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5} + +.Lmemcpy_srcul3loop16: +#ifdef __ARMEB__ + mov r3, lr, lsl #24 +#else + mov r3, lr, lsr #24 +#endif + ldmia r1!, {r4, r5, r12, lr} +#ifdef __ARMEB__ + orr r3, r3, r4, lsr #8 + mov r4, r4, lsl #24 + orr r4, r4, r5, lsr #8 + mov r5, r5, lsl #24 + orr r5, r5, r12, lsr #8 + mov r12, r12, lsl #24 + orr r12, r12, lr, lsr #8 +#else + orr r3, r3, r4, lsl #8 + mov r4, r4, lsr #24 + orr r4, r4, r5, lsl #8 + mov r5, r5, lsr #24 + orr r5, r5, r12, lsl #8 + mov r12, r12, lsr #24 + orr r12, r12, lr, lsl #8 +#endif + stmia r0!, {r3-r5, r12} + subs r2, r2, #0x10 + bge .Lmemcpy_srcul3loop16 + ldmia sp!, {r4, r5} + adds r2, r2, #0x0c + blt .Lmemcpy_srcul3l4 + +.Lmemcpy_srcul3loop4: +#ifdef __ARMEB__ + mov r12, lr, lsl #24 +#else + mov r12, lr, lsr #24 +#endif + ldr lr, [r1], #4 +#ifdef __ARMEB__ + orr r12, r12, lr, lsr #8 +#else + orr r12, r12, lr, lsl #8 +#endif + str r12, [r0], #4 + subs r2, r2, #4 + bge .Lmemcpy_srcul3loop4 + +.Lmemcpy_srcul3l4: + sub r1, r1, #1 + b .Lmemcpy_l4 diff --git a/common/lib/libc/arch/arm/string/memcpy_xscale.S b/common/lib/libc/arch/arm/string/memcpy_xscale.S new file mode 100644 index 000000000..3e157d075 --- /dev/null +++ b/common/lib/libc/arch/arm/string/memcpy_xscale.S @@ -0,0 +1,1893 @@ +/* $NetBSD: memcpy_xscale.S,v 1.2 2007/06/21 21:37:04 scw Exp $ */ + +/* + * Copyright 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Steve C. Woodford for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 + +/* LINTSTUB: Func: void *memcpy(void *dst, const void *src, size_t len) */ +ENTRY(memcpy) + pld [r1] + cmp r2, #0x0c + ble .Lmemcpy_short /* <= 12 bytes */ + mov r3, r0 /* We must not clobber r0 */ + + /* Word-align the destination buffer */ + ands ip, r3, #0x03 /* Already word aligned? */ + beq .Lmemcpy_wordaligned /* Yup */ + cmp ip, #0x02 + ldrb ip, [r1], #0x01 + sub r2, r2, #0x01 + strb ip, [r3], #0x01 + ldrleb ip, [r1], #0x01 + suble r2, r2, #0x01 + strleb ip, [r3], #0x01 + ldrltb ip, [r1], #0x01 + sublt r2, r2, #0x01 + strltb ip, [r3], #0x01 + + /* Destination buffer is now word aligned */ +.Lmemcpy_wordaligned: + ands ip, r1, #0x03 /* Is src also word-aligned? */ + bne .Lmemcpy_bad_align /* Nope. Things just got bad */ + + /* Quad-align the destination buffer */ + tst r3, #0x07 /* Already quad aligned? */ + ldrne ip, [r1], #0x04 + stmfd sp!, {r4-r9} /* Free up some registers */ + subne r2, r2, #0x04 + strne ip, [r3], #0x04 + + /* Destination buffer quad aligned, source is at least word aligned */ + subs r2, r2, #0x80 + blt .Lmemcpy_w_lessthan128 + + /* Copy 128 bytes at a time */ +.Lmemcpy_w_loop128: + ldr r4, [r1], #0x04 /* LD:00-03 */ + ldr r5, [r1], #0x04 /* LD:04-07 */ + pld [r1, #0x18] /* Prefetch 0x20 */ + ldr r6, [r1], #0x04 /* LD:08-0b */ + ldr r7, [r1], #0x04 /* LD:0c-0f */ + ldr r8, [r1], #0x04 /* LD:10-13 */ + ldr r9, [r1], #0x04 /* LD:14-17 */ + strd r4, [r3], #0x08 /* ST:00-07 */ + ldr r4, [r1], #0x04 /* LD:18-1b */ + ldr r5, [r1], #0x04 /* LD:1c-1f */ + strd r6, [r3], #0x08 /* ST:08-0f */ + ldr r6, [r1], #0x04 /* LD:20-23 */ + ldr r7, [r1], #0x04 /* LD:24-27 */ + pld [r1, #0x18] /* Prefetch 0x40 */ + strd r8, [r3], #0x08 /* ST:10-17 */ + ldr r8, [r1], #0x04 /* LD:28-2b */ + ldr r9, [r1], #0x04 /* LD:2c-2f */ + strd r4, [r3], #0x08 /* ST:18-1f */ + ldr r4, [r1], #0x04 /* LD:30-33 */ + ldr r5, [r1], #0x04 /* LD:34-37 */ + strd r6, [r3], #0x08 /* ST:20-27 */ + ldr r6, [r1], #0x04 /* LD:38-3b */ + ldr r7, [r1], #0x04 /* LD:3c-3f */ + strd r8, [r3], #0x08 /* ST:28-2f */ + ldr r8, [r1], #0x04 /* LD:40-43 */ + ldr r9, [r1], #0x04 /* LD:44-47 */ + pld [r1, #0x18] /* Prefetch 0x60 */ + strd r4, [r3], #0x08 /* ST:30-37 */ + ldr r4, [r1], #0x04 /* LD:48-4b */ + ldr r5, [r1], #0x04 /* LD:4c-4f */ + strd r6, [r3], #0x08 /* ST:38-3f */ + ldr r6, [r1], #0x04 /* LD:50-53 */ + ldr r7, [r1], #0x04 /* LD:54-57 */ + strd r8, [r3], #0x08 /* ST:40-47 */ + ldr r8, [r1], #0x04 /* LD:58-5b */ + ldr r9, [r1], #0x04 /* LD:5c-5f */ + strd r4, [r3], #0x08 /* ST:48-4f */ + ldr r4, [r1], #0x04 /* LD:60-63 */ + ldr r5, [r1], #0x04 /* LD:64-67 */ + pld [r1, #0x18] /* Prefetch 0x80 */ + strd r6, [r3], #0x08 /* ST:50-57 */ + ldr r6, [r1], #0x04 /* LD:68-6b */ + ldr r7, [r1], #0x04 /* LD:6c-6f */ + strd r8, [r3], #0x08 /* ST:58-5f */ + ldr r8, [r1], #0x04 /* LD:70-73 */ + ldr r9, [r1], #0x04 /* LD:74-77 */ + strd r4, [r3], #0x08 /* ST:60-67 */ + ldr r4, [r1], #0x04 /* LD:78-7b */ + ldr r5, [r1], #0x04 /* LD:7c-7f */ + strd r6, [r3], #0x08 /* ST:68-6f */ + strd r8, [r3], #0x08 /* ST:70-77 */ + subs r2, r2, #0x80 + strd r4, [r3], #0x08 /* ST:78-7f */ + bge .Lmemcpy_w_loop128 + +.Lmemcpy_w_lessthan128: + adds r2, r2, #0x80 /* Adjust for extra sub */ + ldmeqfd sp!, {r4-r9} + bxeq lr /* Return now if done */ + subs r2, r2, #0x20 + blt .Lmemcpy_w_lessthan32 + + /* Copy 32 bytes at a time */ +.Lmemcpy_w_loop32: + ldr r4, [r1], #0x04 + ldr r5, [r1], #0x04 + pld [r1, #0x18] + ldr r6, [r1], #0x04 + ldr r7, [r1], #0x04 + ldr r8, [r1], #0x04 + ldr r9, [r1], #0x04 + strd r4, [r3], #0x08 + ldr r4, [r1], #0x04 + ldr r5, [r1], #0x04 + strd r6, [r3], #0x08 + strd r8, [r3], #0x08 + subs r2, r2, #0x20 + strd r4, [r3], #0x08 + bge .Lmemcpy_w_loop32 + +.Lmemcpy_w_lessthan32: + adds r2, r2, #0x20 /* Adjust for extra sub */ + ldmeqfd sp!, {r4-r9} + bxeq lr /* Return now if done */ + + and r4, r2, #0x18 + rsbs r4, r4, #0x18 + addne pc, pc, r4, lsl #1 + nop + + /* At least 24 bytes remaining */ + ldr r4, [r1], #0x04 + ldr r5, [r1], #0x04 + sub r2, r2, #0x08 + strd r4, [r3], #0x08 + + /* At least 16 bytes remaining */ + ldr r4, [r1], #0x04 + ldr r5, [r1], #0x04 + sub r2, r2, #0x08 + strd r4, [r3], #0x08 + + /* At least 8 bytes remaining */ + ldr r4, [r1], #0x04 + ldr r5, [r1], #0x04 + subs r2, r2, #0x08 + strd r4, [r3], #0x08 + + /* Less than 8 bytes remaining */ + ldmfd sp!, {r4-r9} + bxeq lr /* Return now if done */ + subs r2, r2, #0x04 + ldrge ip, [r1], #0x04 + strge ip, [r3], #0x04 + bxeq lr /* Return now if done */ + addlt r2, r2, #0x04 + ldrb ip, [r1], #0x01 + cmp r2, #0x02 + ldrgeb r2, [r1], #0x01 + strb ip, [r3], #0x01 + ldrgtb ip, [r1] + strgeb r2, [r3], #0x01 + strgtb ip, [r3] + bx lr + + +/* + * At this point, it has not been possible to word align both buffers. + * The destination buffer is word aligned, but the source buffer is not. + */ +.Lmemcpy_bad_align: + stmfd sp!, {r4-r7} + bic r1, r1, #0x03 + cmp ip, #2 + ldr ip, [r1], #0x04 + bgt .Lmemcpy_bad3 + beq .Lmemcpy_bad2 + b .Lmemcpy_bad1 + +.Lmemcpy_bad1_loop16: +#ifdef __ARMEB__ + mov r4, ip, lsl #8 +#else + mov r4, ip, lsr #8 +#endif + ldr r5, [r1], #0x04 + pld [r1, #0x018] + ldr r6, [r1], #0x04 + ldr r7, [r1], #0x04 + ldr ip, [r1], #0x04 +#ifdef __ARMEB__ + orr r4, r4, r5, lsr #24 + mov r5, r5, lsl #8 + orr r5, r5, r6, lsr #24 + mov r6, r6, lsl #8 + orr r6, r6, r7, lsr #24 + mov r7, r7, lsl #8 + orr r7, r7, ip, lsr #24 +#else + orr r4, r4, r5, lsl #24 + mov r5, r5, lsr #8 + orr r5, r5, r6, lsl #24 + mov r6, r6, lsr #8 + orr r6, r6, r7, lsl #24 + mov r7, r7, lsr #8 + orr r7, r7, ip, lsl #24 +#endif + str r4, [r3], #0x04 + str r5, [r3], #0x04 + str r6, [r3], #0x04 + str r7, [r3], #0x04 + sub r2, r2, #0x10 + +.Lmemcpy_bad1: + cmp r2, #0x20 + bge .Lmemcpy_bad1_loop16 + cmp r2, #0x10 + blt .Lmemcpy_bad1_loop16_short + + /* copy last 16 bytes (without preload) */ +#ifdef __ARMEB__ + mov r4, ip, lsl #8 +#else + mov r4, ip, lsr #8 +#endif + ldr r5, [r1], #0x04 + ldr r6, [r1], #0x04 + ldr r7, [r1], #0x04 + ldr ip, [r1], #0x04 +#ifdef __ARMEB__ + orr r4, r4, r5, lsr #24 + mov r5, r5, lsl #8 + orr r5, r5, r6, lsr #24 + mov r6, r6, lsl #8 + orr r6, r6, r7, lsr #24 + mov r7, r7, lsl #8 + orr r7, r7, ip, lsr #24 +#else + orr r4, r4, r5, lsl #24 + mov r5, r5, lsr #8 + orr r5, r5, r6, lsl #24 + mov r6, r6, lsr #8 + orr r6, r6, r7, lsl #24 + mov r7, r7, lsr #8 + orr r7, r7, ip, lsl #24 +#endif + str r4, [r3], #0x04 + str r5, [r3], #0x04 + str r6, [r3], #0x04 + str r7, [r3], #0x04 + subs r2, r2, #0x10 + ldmeqfd sp!, {r4-r7} + bxeq lr /* Return now if done */ + +.Lmemcpy_bad1_loop16_short: + subs r2, r2, #0x04 + sublt r1, r1, #0x03 + blt .Lmemcpy_bad_done + +.Lmemcpy_bad1_loop4: +#ifdef __ARMEB__ + mov r4, ip, lsl #8 +#else + mov r4, ip, lsr #8 +#endif + ldr ip, [r1], #0x04 + subs r2, r2, #0x04 +#ifdef __ARMEB__ + orr r4, r4, ip, lsr #24 +#else + orr r4, r4, ip, lsl #24 +#endif + str r4, [r3], #0x04 + bge .Lmemcpy_bad1_loop4 + sub r1, r1, #0x03 + b .Lmemcpy_bad_done + +.Lmemcpy_bad2_loop16: +#ifdef __ARMEB__ + mov r4, ip, lsl #16 +#else + mov r4, ip, lsr #16 +#endif + ldr r5, [r1], #0x04 + pld [r1, #0x018] + ldr r6, [r1], #0x04 + ldr r7, [r1], #0x04 + ldr ip, [r1], #0x04 +#ifdef __ARMEB__ + orr r4, r4, r5, lsr #16 + mov r5, r5, lsl #16 + orr r5, r5, r6, lsr #16 + mov r6, r6, lsl #16 + orr r6, r6, r7, lsr #16 + mov r7, r7, lsl #16 + orr r7, r7, ip, lsr #16 +#else + orr r4, r4, r5, lsl #16 + mov r5, r5, lsr #16 + orr r5, r5, r6, lsl #16 + mov r6, r6, lsr #16 + orr r6, r6, r7, lsl #16 + mov r7, r7, lsr #16 + orr r7, r7, ip, lsl #16 +#endif + str r4, [r3], #0x04 + str r5, [r3], #0x04 + str r6, [r3], #0x04 + str r7, [r3], #0x04 + sub r2, r2, #0x10 + +.Lmemcpy_bad2: + cmp r2, #0x20 + bge .Lmemcpy_bad2_loop16 + cmp r2, #0x10 + blt .Lmemcpy_bad2_loop16_short + + /* copy last 16 bytes (without preload) */ +#ifdef __ARMEB__ + mov r4, ip, lsl #16 +#else + mov r4, ip, lsr #16 +#endif + ldr r5, [r1], #0x04 + ldr r6, [r1], #0x04 + ldr r7, [r1], #0x04 + ldr ip, [r1], #0x04 +#ifdef __ARMEB__ + orr r4, r4, r5, lsr #16 + mov r5, r5, lsl #16 + orr r5, r5, r6, lsr #16 + mov r6, r6, lsl #16 + orr r6, r6, r7, lsr #16 + mov r7, r7, lsl #16 + orr r7, r7, ip, lsr #16 +#else + orr r4, r4, r5, lsl #16 + mov r5, r5, lsr #16 + orr r5, r5, r6, lsl #16 + mov r6, r6, lsr #16 + orr r6, r6, r7, lsl #16 + mov r7, r7, lsr #16 + orr r7, r7, ip, lsl #16 +#endif + str r4, [r3], #0x04 + str r5, [r3], #0x04 + str r6, [r3], #0x04 + str r7, [r3], #0x04 + subs r2, r2, #0x10 + ldmeqfd sp!, {r4-r7} + bxeq lr /* Return now if done */ + +.Lmemcpy_bad2_loop16_short: + subs r2, r2, #0x04 + sublt r1, r1, #0x02 + blt .Lmemcpy_bad_done + +.Lmemcpy_bad2_loop4: +#ifdef __ARMEB__ + mov r4, ip, lsl #16 +#else + mov r4, ip, lsr #16 +#endif + ldr ip, [r1], #0x04 + subs r2, r2, #0x04 +#ifdef __ARMEB__ + orr r4, r4, ip, lsr #16 +#else + orr r4, r4, ip, lsl #16 +#endif + str r4, [r3], #0x04 + bge .Lmemcpy_bad2_loop4 + sub r1, r1, #0x02 + b .Lmemcpy_bad_done + +.Lmemcpy_bad3_loop16: +#ifdef __ARMEB__ + mov r4, ip, lsl #24 +#else + mov r4, ip, lsr #24 +#endif + ldr r5, [r1], #0x04 + pld [r1, #0x018] + ldr r6, [r1], #0x04 + ldr r7, [r1], #0x04 + ldr ip, [r1], #0x04 +#ifdef __ARMEB__ + orr r4, r4, r5, lsr #8 + mov r5, r5, lsl #24 + orr r5, r5, r6, lsr #8 + mov r6, r6, lsl #24 + orr r6, r6, r7, lsr #8 + mov r7, r7, lsl #24 + orr r7, r7, ip, lsr #8 +#else + orr r4, r4, r5, lsl #8 + mov r5, r5, lsr #24 + orr r5, r5, r6, lsl #8 + mov r6, r6, lsr #24 + orr r6, r6, r7, lsl #8 + mov r7, r7, lsr #24 + orr r7, r7, ip, lsl #8 +#endif + str r4, [r3], #0x04 + str r5, [r3], #0x04 + str r6, [r3], #0x04 + str r7, [r3], #0x04 + sub r2, r2, #0x10 + +.Lmemcpy_bad3: + cmp r2, #0x20 + bge .Lmemcpy_bad3_loop16 + cmp r2, #0x10 + blt .Lmemcpy_bad3_loop16_short + + /* copy last 16 bytes (without preload) */ +#ifdef __ARMEB__ + mov r4, ip, lsl #24 +#else + mov r4, ip, lsr #24 +#endif + ldr r5, [r1], #0x04 + ldr r6, [r1], #0x04 + ldr r7, [r1], #0x04 + ldr ip, [r1], #0x04 +#ifdef __ARMEB__ + orr r4, r4, r5, lsr #8 + mov r5, r5, lsl #24 + orr r5, r5, r6, lsr #8 + mov r6, r6, lsl #24 + orr r6, r6, r7, lsr #8 + mov r7, r7, lsl #24 + orr r7, r7, ip, lsr #8 +#else + orr r4, r4, r5, lsl #8 + mov r5, r5, lsr #24 + orr r5, r5, r6, lsl #8 + mov r6, r6, lsr #24 + orr r6, r6, r7, lsl #8 + mov r7, r7, lsr #24 + orr r7, r7, ip, lsl #8 +#endif + str r4, [r3], #0x04 + str r5, [r3], #0x04 + str r6, [r3], #0x04 + str r7, [r3], #0x04 + subs r2, r2, #0x10 + ldmeqfd sp!, {r4-r7} + bxeq lr /* Return now if done */ + +.Lmemcpy_bad3_loop16_short: + subs r2, r2, #0x04 + sublt r1, r1, #0x01 + blt .Lmemcpy_bad_done + +.Lmemcpy_bad3_loop4: +#ifdef __ARMEB__ + mov r4, ip, lsl #24 +#else + mov r4, ip, lsr #24 +#endif + ldr ip, [r1], #0x04 + subs r2, r2, #0x04 +#ifdef __ARMEB__ + orr r4, r4, ip, lsr #8 +#else + orr r4, r4, ip, lsl #8 +#endif + str r4, [r3], #0x04 + bge .Lmemcpy_bad3_loop4 + sub r1, r1, #0x01 + +.Lmemcpy_bad_done: + ldmfd sp!, {r4-r7} + adds r2, r2, #0x04 + bxeq lr + ldrb ip, [r1], #0x01 + cmp r2, #0x02 + ldrgeb r2, [r1], #0x01 + strb ip, [r3], #0x01 + ldrgtb ip, [r1] + strgeb r2, [r3], #0x01 + strgtb ip, [r3] + bx lr + + +/* + * Handle short copies (less than 16 bytes), possibly misaligned. + * Some of these are *very* common, thanks to the network stack, + * and so are handled specially. + */ +.Lmemcpy_short: +#ifndef _STANDALONE + add pc, pc, r2, lsl #2 + nop + bx lr /* 0x00 */ + b .Lmemcpy_bytewise /* 0x01 */ + b .Lmemcpy_bytewise /* 0x02 */ + b .Lmemcpy_bytewise /* 0x03 */ + b .Lmemcpy_4 /* 0x04 */ + b .Lmemcpy_bytewise /* 0x05 */ + b .Lmemcpy_6 /* 0x06 */ + b .Lmemcpy_bytewise /* 0x07 */ + b .Lmemcpy_8 /* 0x08 */ + b .Lmemcpy_bytewise /* 0x09 */ + b .Lmemcpy_bytewise /* 0x0a */ + b .Lmemcpy_bytewise /* 0x0b */ + b .Lmemcpy_c /* 0x0c */ +#endif +.Lmemcpy_bytewise: + mov r3, r0 /* We must not clobber r0 */ + ldrb ip, [r1], #0x01 +1: subs r2, r2, #0x01 + strb ip, [r3], #0x01 + ldrneb ip, [r1], #0x01 + bne 1b + bx lr + +#ifndef _STANDALONE +/****************************************************************************** + * Special case for 4 byte copies + */ +#define LMEMCPY_4_LOG2 6 /* 64 bytes */ +#define LMEMCPY_4_PAD .align LMEMCPY_4_LOG2 + LMEMCPY_4_PAD +.Lmemcpy_4: + and r2, r1, #0x03 + orr r2, r2, r0, lsl #2 + ands r2, r2, #0x0f + sub r3, pc, #0x14 + addne pc, r3, r2, lsl #LMEMCPY_4_LOG2 + +/* + * 0000: dst is 32-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] + str r2, [r0] + bx lr + LMEMCPY_4_PAD + +/* + * 0001: dst is 32-bit aligned, src is 8-bit aligned + */ + ldr r3, [r1, #-1] /* BE:r3 = x012 LE:r3 = 210x */ + ldr r2, [r1, #3] /* BE:r2 = 3xxx LE:r2 = xxx3 */ +#ifdef __ARMEB__ + mov r3, r3, lsl #8 /* r3 = 012. */ + orr r3, r3, r2, lsr #24 /* r3 = 0123 */ +#else + mov r3, r3, lsr #8 /* r3 = .210 */ + orr r3, r3, r2, lsl #24 /* r3 = 3210 */ +#endif + str r3, [r0] + bx lr + LMEMCPY_4_PAD + +/* + * 0010: dst is 32-bit aligned, src is 16-bit aligned + */ +#ifdef __ARMEB__ + ldrh r3, [r1] + ldrh r2, [r1, #0x02] +#else + ldrh r3, [r1, #0x02] + ldrh r2, [r1] +#endif + orr r3, r2, r3, lsl #16 + str r3, [r0] + bx lr + LMEMCPY_4_PAD + +/* + * 0011: dst is 32-bit aligned, src is 8-bit aligned + */ + ldr r3, [r1, #-3] /* BE:r3 = xxx0 LE:r3 = 0xxx */ + ldr r2, [r1, #1] /* BE:r2 = 123x LE:r2 = x321 */ +#ifdef __ARMEB__ + mov r3, r3, lsl #24 /* r3 = 0... */ + orr r3, r3, r2, lsr #8 /* r3 = 0123 */ +#else + mov r3, r3, lsr #24 /* r3 = ...0 */ + orr r3, r3, r2, lsl #8 /* r3 = 3210 */ +#endif + str r3, [r0] + bx lr + LMEMCPY_4_PAD + +/* + * 0100: dst is 8-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] +#ifdef __ARMEB__ + strb r2, [r0, #0x03] + mov r3, r2, lsr #8 + mov r1, r2, lsr #24 + strb r1, [r0] +#else + strb r2, [r0] + mov r3, r2, lsr #8 + mov r1, r2, lsr #24 + strb r1, [r0, #0x03] +#endif + strh r3, [r0, #0x01] + bx lr + LMEMCPY_4_PAD + +/* + * 0101: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldrb r1, [r1, #0x03] + strb r2, [r0] + strh r3, [r0, #0x01] + strb r1, [r0, #0x03] + bx lr + LMEMCPY_4_PAD + +/* + * 0110: dst is 8-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldrh r3, [r1, #0x02] /* LE:r3 = ..23 LE:r3 = ..32 */ +#ifdef __ARMEB__ + mov r1, r2, lsr #8 /* r1 = ...0 */ + strb r1, [r0] + mov r2, r2, lsl #8 /* r2 = .01. */ + orr r2, r2, r3, lsr #8 /* r2 = .012 */ +#else + strb r2, [r0] + mov r2, r2, lsr #8 /* r2 = ...1 */ + orr r2, r2, r3, lsl #8 /* r2 = .321 */ + mov r3, r3, lsr #8 /* r3 = ...3 */ +#endif + strh r2, [r0, #0x01] + strb r3, [r0, #0x03] + bx lr + LMEMCPY_4_PAD + +/* + * 0111: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldrb r1, [r1, #0x03] + strb r2, [r0] + strh r3, [r0, #0x01] + strb r1, [r0, #0x03] + bx lr + LMEMCPY_4_PAD + +/* + * 1000: dst is 16-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] +#ifdef __ARMEB__ + strh r2, [r0, #0x02] + mov r3, r2, lsr #16 + strh r3, [r0] +#else + strh r2, [r0] + mov r3, r2, lsr #16 + strh r3, [r0, #0x02] +#endif + bx lr + LMEMCPY_4_PAD + +/* + * 1001: dst is 16-bit aligned, src is 8-bit aligned + */ + ldr r2, [r1, #-1] /* BE:r2 = x012 LE:r2 = 210x */ + ldr r3, [r1, #3] /* BE:r3 = 3xxx LE:r3 = xxx3 */ + mov r1, r2, lsr #8 /* BE:r1 = .x01 LE:r1 = .210 */ + strh r1, [r0] +#ifdef __ARMEB__ + mov r2, r2, lsl #8 /* r2 = 012. */ + orr r2, r2, r3, lsr #24 /* r2 = 0123 */ +#else + mov r2, r2, lsr #24 /* r2 = ...2 */ + orr r2, r2, r3, lsl #8 /* r2 = xx32 */ +#endif + strh r2, [r0, #0x02] + bx lr + LMEMCPY_4_PAD + +/* + * 1010: dst is 16-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] + ldrh r3, [r1, #0x02] + strh r2, [r0] + strh r3, [r0, #0x02] + bx lr + LMEMCPY_4_PAD + +/* + * 1011: dst is 16-bit aligned, src is 8-bit aligned + */ + ldr r3, [r1, #1] /* BE:r3 = 123x LE:r3 = x321 */ + ldr r2, [r1, #-3] /* BE:r2 = xxx0 LE:r2 = 0xxx */ + mov r1, r3, lsr #8 /* BE:r1 = .123 LE:r1 = .x32 */ + strh r1, [r0, #0x02] +#ifdef __ARMEB__ + mov r3, r3, lsr #24 /* r3 = ...1 */ + orr r3, r3, r2, lsl #8 /* r3 = xx01 */ +#else + mov r3, r3, lsl #8 /* r3 = 321. */ + orr r3, r3, r2, lsr #24 /* r3 = 3210 */ +#endif + strh r3, [r0] + bx lr + LMEMCPY_4_PAD + +/* + * 1100: dst is 8-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] /* BE:r2 = 0123 LE:r2 = 3210 */ +#ifdef __ARMEB__ + strb r2, [r0, #0x03] + mov r3, r2, lsr #8 + mov r1, r2, lsr #24 + strh r3, [r0, #0x01] + strb r1, [r0] +#else + strb r2, [r0] + mov r3, r2, lsr #8 + mov r1, r2, lsr #24 + strh r3, [r0, #0x01] + strb r1, [r0, #0x03] +#endif + bx lr + LMEMCPY_4_PAD + +/* + * 1101: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldrb r1, [r1, #0x03] + strb r2, [r0] + strh r3, [r0, #0x01] + strb r1, [r0, #0x03] + bx lr + LMEMCPY_4_PAD + +/* + * 1110: dst is 8-bit aligned, src is 16-bit aligned + */ +#ifdef __ARMEB__ + ldrh r3, [r1, #0x02] /* BE:r3 = ..23 LE:r3 = ..32 */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + strb r3, [r0, #0x03] + mov r3, r3, lsr #8 /* r3 = ...2 */ + orr r3, r3, r2, lsl #8 /* r3 = ..12 */ + strh r3, [r0, #0x01] + mov r2, r2, lsr #8 /* r2 = ...0 */ + strb r2, [r0] +#else + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldrh r3, [r1, #0x02] /* BE:r3 = ..23 LE:r3 = ..32 */ + strb r2, [r0] + mov r2, r2, lsr #8 /* r2 = ...1 */ + orr r2, r2, r3, lsl #8 /* r2 = .321 */ + strh r2, [r0, #0x01] + mov r3, r3, lsr #8 /* r3 = ...3 */ + strb r3, [r0, #0x03] +#endif + bx lr + LMEMCPY_4_PAD + +/* + * 1111: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldrb r1, [r1, #0x03] + strb r2, [r0] + strh r3, [r0, #0x01] + strb r1, [r0, #0x03] + bx lr + LMEMCPY_4_PAD + + +/****************************************************************************** + * Special case for 6 byte copies + */ +#define LMEMCPY_6_LOG2 6 /* 64 bytes */ +#define LMEMCPY_6_PAD .align LMEMCPY_6_LOG2 + LMEMCPY_6_PAD +.Lmemcpy_6: + and r2, r1, #0x03 + orr r2, r2, r0, lsl #2 + ands r2, r2, #0x0f + sub r3, pc, #0x14 + addne pc, r3, r2, lsl #LMEMCPY_6_LOG2 + +/* + * 0000: dst is 32-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] + ldrh r3, [r1, #0x04] + str r2, [r0] + strh r3, [r0, #0x04] + bx lr + LMEMCPY_6_PAD + +/* + * 0001: dst is 32-bit aligned, src is 8-bit aligned + */ + ldr r2, [r1, #-1] /* BE:r2 = x012 LE:r2 = 210x */ + ldr r3, [r1, #0x03] /* BE:r3 = 345x LE:r3 = x543 */ +#ifdef __ARMEB__ + mov r2, r2, lsl #8 /* r2 = 012. */ + orr r2, r2, r3, lsr #24 /* r2 = 0123 */ +#else + mov r2, r2, lsr #8 /* r2 = .210 */ + orr r2, r2, r3, lsl #24 /* r2 = 3210 */ +#endif + mov r3, r3, lsr #8 /* BE:r3 = .345 LE:r3 = .x54 */ + str r2, [r0] + strh r3, [r0, #0x04] + bx lr + LMEMCPY_6_PAD + +/* + * 0010: dst is 32-bit aligned, src is 16-bit aligned + */ + ldr r3, [r1, #0x02] /* BE:r3 = 2345 LE:r3 = 5432 */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ +#ifdef __ARMEB__ + mov r1, r3, lsr #16 /* r1 = ..23 */ + orr r1, r1, r2, lsl #16 /* r1 = 0123 */ + str r1, [r0] + strh r3, [r0, #0x04] +#else + mov r1, r3, lsr #16 /* r1 = ..54 */ + orr r2, r2, r3, lsl #16 /* r2 = 3210 */ + str r2, [r0] + strh r1, [r0, #0x04] +#endif + bx lr + LMEMCPY_6_PAD + +/* + * 0011: dst is 32-bit aligned, src is 8-bit aligned + */ + ldr r2, [r1, #-3] /* BE:r2 = xxx0 LE:r2 = 0xxx */ + ldr r3, [r1, #1] /* BE:r3 = 1234 LE:r3 = 4321 */ + ldr r1, [r1, #5] /* BE:r1 = 5xxx LE:r3 = xxx5 */ +#ifdef __ARMEB__ + mov r2, r2, lsl #24 /* r2 = 0... */ + orr r2, r2, r3, lsr #8 /* r2 = 0123 */ + mov r3, r3, lsl #8 /* r3 = 234. */ + orr r1, r3, r1, lsr #24 /* r1 = 2345 */ +#else + mov r2, r2, lsr #24 /* r2 = ...0 */ + orr r2, r2, r3, lsl #8 /* r2 = 3210 */ + mov r1, r1, lsl #8 /* r1 = xx5. */ + orr r1, r1, r3, lsr #24 /* r1 = xx54 */ +#endif + str r2, [r0] + strh r1, [r0, #0x04] + bx lr + LMEMCPY_6_PAD + +/* + * 0100: dst is 8-bit aligned, src is 32-bit aligned + */ + ldr r3, [r1] /* BE:r3 = 0123 LE:r3 = 3210 */ + ldrh r2, [r1, #0x04] /* BE:r2 = ..45 LE:r2 = ..54 */ + mov r1, r3, lsr #8 /* BE:r1 = .012 LE:r1 = .321 */ + strh r1, [r0, #0x01] +#ifdef __ARMEB__ + mov r1, r3, lsr #24 /* r1 = ...0 */ + strb r1, [r0] + mov r3, r3, lsl #8 /* r3 = 123. */ + orr r3, r3, r2, lsr #8 /* r3 = 1234 */ +#else + strb r3, [r0] + mov r3, r3, lsr #24 /* r3 = ...3 */ + orr r3, r3, r2, lsl #8 /* r3 = .543 */ + mov r2, r2, lsr #8 /* r2 = ...5 */ +#endif + strh r3, [r0, #0x03] + strb r2, [r0, #0x05] + bx lr + LMEMCPY_6_PAD + +/* + * 0101: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldrh ip, [r1, #0x03] + ldrb r1, [r1, #0x05] + strb r2, [r0] + strh r3, [r0, #0x01] + strh ip, [r0, #0x03] + strb r1, [r0, #0x05] + bx lr + LMEMCPY_6_PAD + +/* + * 0110: dst is 8-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldr r1, [r1, #0x02] /* BE:r1 = 2345 LE:r1 = 5432 */ +#ifdef __ARMEB__ + mov r3, r2, lsr #8 /* r3 = ...0 */ + strb r3, [r0] + strb r1, [r0, #0x05] + mov r3, r1, lsr #8 /* r3 = .234 */ + strh r3, [r0, #0x03] + mov r3, r2, lsl #8 /* r3 = .01. */ + orr r3, r3, r1, lsr #24 /* r3 = .012 */ + strh r3, [r0, #0x01] +#else + strb r2, [r0] + mov r3, r1, lsr #24 + strb r3, [r0, #0x05] + mov r3, r1, lsr #8 /* r3 = .543 */ + strh r3, [r0, #0x03] + mov r3, r2, lsr #8 /* r3 = ...1 */ + orr r3, r3, r1, lsl #8 /* r3 = 4321 */ + strh r3, [r0, #0x01] +#endif + bx lr + LMEMCPY_6_PAD + +/* + * 0111: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldrh ip, [r1, #0x03] + ldrb r1, [r1, #0x05] + strb r2, [r0] + strh r3, [r0, #0x01] + strh ip, [r0, #0x03] + strb r1, [r0, #0x05] + bx lr + LMEMCPY_6_PAD + +/* + * 1000: dst is 16-bit aligned, src is 32-bit aligned + */ +#ifdef __ARMEB__ + ldr r2, [r1] /* r2 = 0123 */ + ldrh r3, [r1, #0x04] /* r3 = ..45 */ + mov r1, r2, lsr #16 /* r1 = ..01 */ + orr r3, r3, r2, lsl#16 /* r3 = 2345 */ + strh r1, [r0] + str r3, [r0, #0x02] +#else + ldrh r2, [r1, #0x04] /* r2 = ..54 */ + ldr r3, [r1] /* r3 = 3210 */ + mov r2, r2, lsl #16 /* r2 = 54.. */ + orr r2, r2, r3, lsr #16 /* r2 = 5432 */ + strh r3, [r0] + str r2, [r0, #0x02] +#endif + bx lr + LMEMCPY_6_PAD + +/* + * 1001: dst is 16-bit aligned, src is 8-bit aligned + */ + ldr r3, [r1, #-1] /* BE:r3 = x012 LE:r3 = 210x */ + ldr r2, [r1, #3] /* BE:r2 = 345x LE:r2 = x543 */ + mov r1, r3, lsr #8 /* BE:r1 = .x01 LE:r1 = .210 */ +#ifdef __ARMEB__ + mov r2, r2, lsr #8 /* r2 = .345 */ + orr r2, r2, r3, lsl #24 /* r2 = 2345 */ +#else + mov r2, r2, lsl #8 /* r2 = 543. */ + orr r2, r2, r3, lsr #24 /* r2 = 5432 */ +#endif + strh r1, [r0] + str r2, [r0, #0x02] + bx lr + LMEMCPY_6_PAD + +/* + * 1010: dst is 16-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] + ldr r3, [r1, #0x02] + strh r2, [r0] + str r3, [r0, #0x02] + bx lr + LMEMCPY_6_PAD + +/* + * 1011: dst is 16-bit aligned, src is 8-bit aligned + */ + ldrb r3, [r1] /* r3 = ...0 */ + ldr r2, [r1, #0x01] /* BE:r2 = 1234 LE:r2 = 4321 */ + ldrb r1, [r1, #0x05] /* r1 = ...5 */ +#ifdef __ARMEB__ + mov r3, r3, lsl #8 /* r3 = ..0. */ + orr r3, r3, r2, lsr #24 /* r3 = ..01 */ + orr r1, r1, r2, lsl #8 /* r1 = 2345 */ +#else + orr r3, r3, r2, lsl #8 /* r3 = 3210 */ + mov r1, r1, lsl #24 /* r1 = 5... */ + orr r1, r1, r2, lsr #8 /* r1 = 5432 */ +#endif + strh r3, [r0] + str r1, [r0, #0x02] + bx lr + LMEMCPY_6_PAD + +/* + * 1100: dst is 8-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] /* BE:r2 = 0123 LE:r2 = 3210 */ + ldrh r1, [r1, #0x04] /* BE:r1 = ..45 LE:r1 = ..54 */ +#ifdef __ARMEB__ + mov r3, r2, lsr #24 /* r3 = ...0 */ + strb r3, [r0] + mov r2, r2, lsl #8 /* r2 = 123. */ + orr r2, r2, r1, lsr #8 /* r2 = 1234 */ +#else + strb r2, [r0] + mov r2, r2, lsr #8 /* r2 = .321 */ + orr r2, r2, r1, lsl #24 /* r2 = 4321 */ + mov r1, r1, lsr #8 /* r1 = ...5 */ +#endif + str r2, [r0, #0x01] + strb r1, [r0, #0x05] + bx lr + LMEMCPY_6_PAD + +/* + * 1101: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldrh ip, [r1, #0x03] + ldrb r1, [r1, #0x05] + strb r2, [r0] + strh r3, [r0, #0x01] + strh ip, [r0, #0x03] + strb r1, [r0, #0x05] + bx lr + LMEMCPY_6_PAD + +/* + * 1110: dst is 8-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldr r1, [r1, #0x02] /* BE:r1 = 2345 LE:r1 = 5432 */ +#ifdef __ARMEB__ + mov r3, r2, lsr #8 /* r3 = ...0 */ + strb r3, [r0] + mov r2, r2, lsl #24 /* r2 = 1... */ + orr r2, r2, r1, lsr #8 /* r2 = 1234 */ +#else + strb r2, [r0] + mov r2, r2, lsr #8 /* r2 = ...1 */ + orr r2, r2, r1, lsl #8 /* r2 = 4321 */ + mov r1, r1, lsr #24 /* r1 = ...5 */ +#endif + str r2, [r0, #0x01] + strb r1, [r0, #0x05] + bx lr + LMEMCPY_6_PAD + +/* + * 1111: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldr r3, [r1, #0x01] + ldrb r1, [r1, #0x05] + strb r2, [r0] + str r3, [r0, #0x01] + strb r1, [r0, #0x05] + bx lr + LMEMCPY_6_PAD + + +/****************************************************************************** + * Special case for 8 byte copies + */ +#define LMEMCPY_8_LOG2 6 /* 64 bytes */ +#define LMEMCPY_8_PAD .align LMEMCPY_8_LOG2 + LMEMCPY_8_PAD +.Lmemcpy_8: + and r2, r1, #0x03 + orr r2, r2, r0, lsl #2 + ands r2, r2, #0x0f + sub r3, pc, #0x14 + addne pc, r3, r2, lsl #LMEMCPY_8_LOG2 + +/* + * 0000: dst is 32-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] + ldr r3, [r1, #0x04] + str r2, [r0] + str r3, [r0, #0x04] + bx lr + LMEMCPY_8_PAD + +/* + * 0001: dst is 32-bit aligned, src is 8-bit aligned + */ + ldr r3, [r1, #-1] /* BE:r3 = x012 LE:r3 = 210x */ + ldr r2, [r1, #0x03] /* BE:r2 = 3456 LE:r2 = 6543 */ + ldrb r1, [r1, #0x07] /* r1 = ...7 */ +#ifdef __ARMEB__ + mov r3, r3, lsl #8 /* r3 = 012. */ + orr r3, r3, r2, lsr #24 /* r3 = 0123 */ + orr r2, r1, r2, lsl #8 /* r2 = 4567 */ +#else + mov r3, r3, lsr #8 /* r3 = .210 */ + orr r3, r3, r2, lsl #24 /* r3 = 3210 */ + mov r1, r1, lsl #24 /* r1 = 7... */ + orr r2, r1, r2, lsr #8 /* r2 = 7654 */ +#endif + str r3, [r0] + str r2, [r0, #0x04] + bx lr + LMEMCPY_8_PAD + +/* + * 0010: dst is 32-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldr r3, [r1, #0x02] /* BE:r3 = 2345 LE:r3 = 5432 */ + ldrh r1, [r1, #0x06] /* BE:r1 = ..67 LE:r1 = ..76 */ +#ifdef __ARMEB__ + mov r2, r2, lsl #16 /* r2 = 01.. */ + orr r2, r2, r3, lsr #16 /* r2 = 0123 */ + orr r3, r1, r3, lsl #16 /* r3 = 4567 */ +#else + orr r2, r2, r3, lsl #16 /* r2 = 3210 */ + mov r3, r3, lsr #16 /* r3 = ..54 */ + orr r3, r3, r1, lsl #16 /* r3 = 7654 */ +#endif + str r2, [r0] + str r3, [r0, #0x04] + bx lr + LMEMCPY_8_PAD + +/* + * 0011: dst is 32-bit aligned, src is 8-bit aligned + */ + ldrb r3, [r1] /* r3 = ...0 */ + ldr r2, [r1, #0x01] /* BE:r2 = 1234 LE:r2 = 4321 */ + ldr r1, [r1, #0x05] /* BE:r1 = 567x LE:r1 = x765 */ +#ifdef __ARMEB__ + mov r3, r3, lsl #24 /* r3 = 0... */ + orr r3, r3, r2, lsr #8 /* r3 = 0123 */ + mov r2, r2, lsl #24 /* r2 = 4... */ + orr r2, r2, r1, lsr #8 /* r2 = 4567 */ +#else + orr r3, r3, r2, lsl #8 /* r3 = 3210 */ + mov r2, r2, lsr #24 /* r2 = ...4 */ + orr r2, r2, r1, lsl #8 /* r2 = 7654 */ +#endif + str r3, [r0] + str r2, [r0, #0x04] + bx lr + LMEMCPY_8_PAD + +/* + * 0100: dst is 8-bit aligned, src is 32-bit aligned + */ + ldr r3, [r1] /* BE:r3 = 0123 LE:r3 = 3210 */ + ldr r2, [r1, #0x04] /* BE:r2 = 4567 LE:r2 = 7654 */ +#ifdef __ARMEB__ + mov r1, r3, lsr #24 /* r1 = ...0 */ + strb r1, [r0] + mov r1, r3, lsr #8 /* r1 = .012 */ + strb r2, [r0, #0x07] + mov r3, r3, lsl #24 /* r3 = 3... */ + orr r3, r3, r2, lsr #8 /* r3 = 3456 */ +#else + strb r3, [r0] + mov r1, r2, lsr #24 /* r1 = ...7 */ + strb r1, [r0, #0x07] + mov r1, r3, lsr #8 /* r1 = .321 */ + mov r3, r3, lsr #24 /* r3 = ...3 */ + orr r3, r3, r2, lsl #8 /* r3 = 6543 */ +#endif + strh r1, [r0, #0x01] + str r3, [r0, #0x03] + bx lr + LMEMCPY_8_PAD + +/* + * 0101: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldr ip, [r1, #0x03] + ldrb r1, [r1, #0x07] + strb r2, [r0] + strh r3, [r0, #0x01] + str ip, [r0, #0x03] + strb r1, [r0, #0x07] + bx lr + LMEMCPY_8_PAD + +/* + * 0110: dst is 8-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldr r3, [r1, #0x02] /* BE:r3 = 2345 LE:r3 = 5432 */ + ldrh r1, [r1, #0x06] /* BE:r1 = ..67 LE:r1 = ..76 */ +#ifdef __ARMEB__ + mov ip, r2, lsr #8 /* ip = ...0 */ + strb ip, [r0] + mov ip, r2, lsl #8 /* ip = .01. */ + orr ip, ip, r3, lsr #24 /* ip = .012 */ + strb r1, [r0, #0x07] + mov r3, r3, lsl #8 /* r3 = 345. */ + orr r3, r3, r1, lsr #8 /* r3 = 3456 */ +#else + strb r2, [r0] /* 0 */ + mov ip, r1, lsr #8 /* ip = ...7 */ + strb ip, [r0, #0x07] /* 7 */ + mov ip, r2, lsr #8 /* ip = ...1 */ + orr ip, ip, r3, lsl #8 /* ip = 4321 */ + mov r3, r3, lsr #8 /* r3 = .543 */ + orr r3, r3, r1, lsl #24 /* r3 = 6543 */ +#endif + strh ip, [r0, #0x01] + str r3, [r0, #0x03] + bx lr + LMEMCPY_8_PAD + +/* + * 0111: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r3, [r1] /* r3 = ...0 */ + ldr ip, [r1, #0x01] /* BE:ip = 1234 LE:ip = 4321 */ + ldrh r2, [r1, #0x05] /* BE:r2 = ..56 LE:r2 = ..65 */ + ldrb r1, [r1, #0x07] /* r1 = ...7 */ + strb r3, [r0] + mov r3, ip, lsr #16 /* BE:r3 = ..12 LE:r3 = ..43 */ +#ifdef __ARMEB__ + strh r3, [r0, #0x01] + orr r2, r2, ip, lsl #16 /* r2 = 3456 */ +#else + strh ip, [r0, #0x01] + orr r2, r3, r2, lsl #16 /* r2 = 6543 */ +#endif + str r2, [r0, #0x03] + strb r1, [r0, #0x07] + bx lr + LMEMCPY_8_PAD + +/* + * 1000: dst is 16-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] /* BE:r2 = 0123 LE:r2 = 3210 */ + ldr r3, [r1, #0x04] /* BE:r3 = 4567 LE:r3 = 7654 */ + mov r1, r2, lsr #16 /* BE:r1 = ..01 LE:r1 = ..32 */ +#ifdef __ARMEB__ + strh r1, [r0] + mov r1, r3, lsr #16 /* r1 = ..45 */ + orr r2, r1 ,r2, lsl #16 /* r2 = 2345 */ +#else + strh r2, [r0] + orr r2, r1, r3, lsl #16 /* r2 = 5432 */ + mov r3, r3, lsr #16 /* r3 = ..76 */ +#endif + str r2, [r0, #0x02] + strh r3, [r0, #0x06] + bx lr + LMEMCPY_8_PAD + +/* + * 1001: dst is 16-bit aligned, src is 8-bit aligned + */ + ldr r2, [r1, #-1] /* BE:r2 = x012 LE:r2 = 210x */ + ldr r3, [r1, #0x03] /* BE:r3 = 3456 LE:r3 = 6543 */ + ldrb ip, [r1, #0x07] /* ip = ...7 */ + mov r1, r2, lsr #8 /* BE:r1 = .x01 LE:r1 = .210 */ + strh r1, [r0] +#ifdef __ARMEB__ + mov r1, r2, lsl #24 /* r1 = 2... */ + orr r1, r1, r3, lsr #8 /* r1 = 2345 */ + orr r3, ip, r3, lsl #8 /* r3 = 4567 */ +#else + mov r1, r2, lsr #24 /* r1 = ...2 */ + orr r1, r1, r3, lsl #8 /* r1 = 5432 */ + mov r3, r3, lsr #24 /* r3 = ...6 */ + orr r3, r3, ip, lsl #8 /* r3 = ..76 */ +#endif + str r1, [r0, #0x02] + strh r3, [r0, #0x06] + bx lr + LMEMCPY_8_PAD + +/* + * 1010: dst is 16-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] + ldr ip, [r1, #0x02] + ldrh r3, [r1, #0x06] + strh r2, [r0] + str ip, [r0, #0x02] + strh r3, [r0, #0x06] + bx lr + LMEMCPY_8_PAD + +/* + * 1011: dst is 16-bit aligned, src is 8-bit aligned + */ + ldr r3, [r1, #0x05] /* BE:r3 = 567x LE:r3 = x765 */ + ldr r2, [r1, #0x01] /* BE:r2 = 1234 LE:r2 = 4321 */ + ldrb ip, [r1] /* ip = ...0 */ + mov r1, r3, lsr #8 /* BE:r1 = .567 LE:r1 = .x76 */ + strh r1, [r0, #0x06] +#ifdef __ARMEB__ + mov r3, r3, lsr #24 /* r3 = ...5 */ + orr r3, r3, r2, lsl #8 /* r3 = 2345 */ + mov r2, r2, lsr #24 /* r2 = ...1 */ + orr r2, r2, ip, lsl #8 /* r2 = ..01 */ +#else + mov r3, r3, lsl #24 /* r3 = 5... */ + orr r3, r3, r2, lsr #8 /* r3 = 5432 */ + orr r2, ip, r2, lsl #8 /* r2 = 3210 */ +#endif + str r3, [r0, #0x02] + strh r2, [r0] + bx lr + LMEMCPY_8_PAD + +/* + * 1100: dst is 8-bit aligned, src is 32-bit aligned + */ + ldr r3, [r1, #0x04] /* BE:r3 = 4567 LE:r3 = 7654 */ + ldr r2, [r1] /* BE:r2 = 0123 LE:r2 = 3210 */ + mov r1, r3, lsr #8 /* BE:r1 = .456 LE:r1 = .765 */ + strh r1, [r0, #0x05] +#ifdef __ARMEB__ + strb r3, [r0, #0x07] + mov r1, r2, lsr #24 /* r1 = ...0 */ + strb r1, [r0] + mov r2, r2, lsl #8 /* r2 = 123. */ + orr r2, r2, r3, lsr #24 /* r2 = 1234 */ + str r2, [r0, #0x01] +#else + strb r2, [r0] + mov r1, r3, lsr #24 /* r1 = ...7 */ + strb r1, [r0, #0x07] + mov r2, r2, lsr #8 /* r2 = .321 */ + orr r2, r2, r3, lsl #24 /* r2 = 4321 */ + str r2, [r0, #0x01] +#endif + bx lr + LMEMCPY_8_PAD + +/* + * 1101: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r3, [r1] /* r3 = ...0 */ + ldrh r2, [r1, #0x01] /* BE:r2 = ..12 LE:r2 = ..21 */ + ldr ip, [r1, #0x03] /* BE:ip = 3456 LE:ip = 6543 */ + ldrb r1, [r1, #0x07] /* r1 = ...7 */ + strb r3, [r0] + mov r3, ip, lsr #16 /* BE:r3 = ..34 LE:r3 = ..65 */ +#ifdef __ARMEB__ + strh ip, [r0, #0x05] + orr r2, r3, r2, lsl #16 /* r2 = 1234 */ +#else + strh r3, [r0, #0x05] + orr r2, r2, ip, lsl #16 /* r2 = 4321 */ +#endif + str r2, [r0, #0x01] + strb r1, [r0, #0x07] + bx lr + LMEMCPY_8_PAD + +/* + * 1110: dst is 8-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldr r3, [r1, #0x02] /* BE:r3 = 2345 LE:r3 = 5432 */ + ldrh r1, [r1, #0x06] /* BE:r1 = ..67 LE:r1 = ..76 */ +#ifdef __ARMEB__ + mov ip, r2, lsr #8 /* ip = ...0 */ + strb ip, [r0] + mov ip, r2, lsl #24 /* ip = 1... */ + orr ip, ip, r3, lsr #8 /* ip = 1234 */ + strb r1, [r0, #0x07] + mov r1, r1, lsr #8 /* r1 = ...6 */ + orr r1, r1, r3, lsl #8 /* r1 = 3456 */ +#else + strb r2, [r0] + mov ip, r2, lsr #8 /* ip = ...1 */ + orr ip, ip, r3, lsl #8 /* ip = 4321 */ + mov r2, r1, lsr #8 /* r2 = ...7 */ + strb r2, [r0, #0x07] + mov r1, r1, lsl #8 /* r1 = .76. */ + orr r1, r1, r3, lsr #24 /* r1 = .765 */ +#endif + str ip, [r0, #0x01] + strh r1, [r0, #0x05] + bx lr + LMEMCPY_8_PAD + +/* + * 1111: dst is 8-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] + ldr ip, [r1, #0x01] + ldrh r3, [r1, #0x05] + ldrb r1, [r1, #0x07] + strb r2, [r0] + str ip, [r0, #0x01] + strh r3, [r0, #0x05] + strb r1, [r0, #0x07] + bx lr + LMEMCPY_8_PAD + +/****************************************************************************** + * Special case for 12 byte copies + */ +#define LMEMCPY_C_LOG2 7 /* 128 bytes */ +#define LMEMCPY_C_PAD .align LMEMCPY_C_LOG2 + LMEMCPY_C_PAD +.Lmemcpy_c: + and r2, r1, #0x03 + orr r2, r2, r0, lsl #2 + ands r2, r2, #0x0f + sub r3, pc, #0x14 + addne pc, r3, r2, lsl #LMEMCPY_C_LOG2 + +/* + * 0000: dst is 32-bit aligned, src is 32-bit aligned + */ + ldr r2, [r1] + ldr r3, [r1, #0x04] + ldr r1, [r1, #0x08] + str r2, [r0] + str r3, [r0, #0x04] + str r1, [r0, #0x08] + bx lr + LMEMCPY_C_PAD + +/* + * 0001: dst is 32-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1, #0xb] /* r2 = ...B */ + ldr ip, [r1, #0x07] /* BE:ip = 789A LE:ip = A987 */ + ldr r3, [r1, #0x03] /* BE:r3 = 3456 LE:r3 = 6543 */ + ldr r1, [r1, #-1] /* BE:r1 = x012 LE:r1 = 210x */ +#ifdef __ARMEB__ + orr r2, r2, ip, lsl #8 /* r2 = 89AB */ + str r2, [r0, #0x08] + mov r2, ip, lsr #24 /* r2 = ...7 */ + orr r2, r2, r3, lsl #8 /* r2 = 4567 */ + mov r1, r1, lsl #8 /* r1 = 012. */ + orr r1, r1, r3, lsr #24 /* r1 = 0123 */ +#else + mov r2, r2, lsl #24 /* r2 = B... */ + orr r2, r2, ip, lsr #8 /* r2 = BA98 */ + str r2, [r0, #0x08] + mov r2, ip, lsl #24 /* r2 = 7... */ + orr r2, r2, r3, lsr #8 /* r2 = 7654 */ + mov r1, r1, lsr #8 /* r1 = .210 */ + orr r1, r1, r3, lsl #24 /* r1 = 3210 */ +#endif + str r2, [r0, #0x04] + str r1, [r0] + bx lr + LMEMCPY_C_PAD + +/* + * 0010: dst is 32-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldr r3, [r1, #0x02] /* BE:r3 = 2345 LE:r3 = 5432 */ + ldr ip, [r1, #0x06] /* BE:ip = 6789 LE:ip = 9876 */ + ldrh r1, [r1, #0x0a] /* BE:r1 = ..AB LE:r1 = ..BA */ +#ifdef __ARMEB__ + mov r2, r2, lsl #16 /* r2 = 01.. */ + orr r2, r2, r3, lsr #16 /* r2 = 0123 */ + str r2, [r0] + mov r3, r3, lsl #16 /* r3 = 45.. */ + orr r3, r3, ip, lsr #16 /* r3 = 4567 */ + orr r1, r1, ip, lsl #16 /* r1 = 89AB */ +#else + orr r2, r2, r3, lsl #16 /* r2 = 3210 */ + str r2, [r0] + mov r3, r3, lsr #16 /* r3 = ..54 */ + orr r3, r3, ip, lsl #16 /* r3 = 7654 */ + mov r1, r1, lsl #16 /* r1 = BA.. */ + orr r1, r1, ip, lsr #16 /* r1 = BA98 */ +#endif + str r3, [r0, #0x04] + str r1, [r0, #0x08] + bx lr + LMEMCPY_C_PAD + +/* + * 0011: dst is 32-bit aligned, src is 8-bit aligned + */ + ldrb r2, [r1] /* r2 = ...0 */ + ldr r3, [r1, #0x01] /* BE:r3 = 1234 LE:r3 = 4321 */ + ldr ip, [r1, #0x05] /* BE:ip = 5678 LE:ip = 8765 */ + ldr r1, [r1, #0x09] /* BE:r1 = 9ABx LE:r1 = xBA9 */ +#ifdef __ARMEB__ + mov r2, r2, lsl #24 /* r2 = 0... */ + orr r2, r2, r3, lsr #8 /* r2 = 0123 */ + str r2, [r0] + mov r3, r3, lsl #24 /* r3 = 4... */ + orr r3, r3, ip, lsr #8 /* r3 = 4567 */ + mov r1, r1, lsr #8 /* r1 = .9AB */ + orr r1, r1, ip, lsl #24 /* r1 = 89AB */ +#else + orr r2, r2, r3, lsl #8 /* r2 = 3210 */ + str r2, [r0] + mov r3, r3, lsr #24 /* r3 = ...4 */ + orr r3, r3, ip, lsl #8 /* r3 = 7654 */ + mov r1, r1, lsl #8 /* r1 = BA9. */ + orr r1, r1, ip, lsr #24 /* r1 = BA98 */ +#endif + str r3, [r0, #0x04] + str r1, [r0, #0x08] + bx lr + LMEMCPY_C_PAD + +/* + * 0100: dst is 8-bit aligned (byte 1), src is 32-bit aligned + */ + ldr r2, [r1] /* BE:r2 = 0123 LE:r2 = 3210 */ + ldr r3, [r1, #0x04] /* BE:r3 = 4567 LE:r3 = 7654 */ + ldr ip, [r1, #0x08] /* BE:ip = 89AB LE:ip = BA98 */ + mov r1, r2, lsr #8 /* BE:r1 = .012 LE:r1 = .321 */ + strh r1, [r0, #0x01] +#ifdef __ARMEB__ + mov r1, r2, lsr #24 /* r1 = ...0 */ + strb r1, [r0] + mov r1, r2, lsl #24 /* r1 = 3... */ + orr r2, r1, r3, lsr #8 /* r1 = 3456 */ + mov r1, r3, lsl #24 /* r1 = 7... */ + orr r1, r1, ip, lsr #8 /* r1 = 789A */ +#else + strb r2, [r0] + mov r1, r2, lsr #24 /* r1 = ...3 */ + orr r2, r1, r3, lsl #8 /* r1 = 6543 */ + mov r1, r3, lsr #24 /* r1 = ...7 */ + orr r1, r1, ip, lsl #8 /* r1 = A987 */ + mov ip, ip, lsr #24 /* ip = ...B */ +#endif + str r2, [r0, #0x03] + str r1, [r0, #0x07] + strb ip, [r0, #0x0b] + bx lr + LMEMCPY_C_PAD + +/* + * 0101: dst is 8-bit aligned (byte 1), src is 8-bit aligned (byte 1) + */ + ldrb r2, [r1] + ldrh r3, [r1, #0x01] + ldr ip, [r1, #0x03] + strb r2, [r0] + ldr r2, [r1, #0x07] + ldrb r1, [r1, #0x0b] + strh r3, [r0, #0x01] + str ip, [r0, #0x03] + str r2, [r0, #0x07] + strb r1, [r0, #0x0b] + bx lr + LMEMCPY_C_PAD + +/* + * 0110: dst is 8-bit aligned (byte 1), src is 16-bit aligned + */ + ldrh r2, [r1] /* BE:r2 = ..01 LE:r2 = ..10 */ + ldr r3, [r1, #0x02] /* BE:r3 = 2345 LE:r3 = 5432 */ + ldr ip, [r1, #0x06] /* BE:ip = 6789 LE:ip = 9876 */ + ldrh r1, [r1, #0x0a] /* BE:r1 = ..AB LE:r1 = ..BA */ +#ifdef __ARMEB__ + mov r2, r2, ror #8 /* r2 = 1..0 */ + strb r2, [r0] + mov r2, r2, lsr #16 /* r2 = ..1. */ + orr r2, r2, r3, lsr #24 /* r2 = ..12 */ + strh r2, [r0, #0x01] + mov r2, r3, lsl #8 /* r2 = 345. */ + orr r3, r2, ip, lsr #24 /* r3 = 3456 */ + mov r2, ip, lsl #8 /* r2 = 789. */ + orr r2, r2, r1, lsr #8 /* r2 = 789A */ +#else + strb r2, [r0] + mov r2, r2, lsr #8 /* r2 = ...1 */ + orr r2, r2, r3, lsl #8 /* r2 = 4321 */ + strh r2, [r0, #0x01] + mov r2, r3, lsr #8 /* r2 = .543 */ + orr r3, r2, ip, lsl #24 /* r3 = 6543 */ + mov r2, ip, lsr #8 /* r2 = .987 */ + orr r2, r2, r1, lsl #24 /* r2 = A987 */ + mov r1, r1, lsr #8 /* r1 = ...B */ +#endif + str r3, [r0, #0x03] + str r2, [r0, #0x07] + strb r1, [r0, #0x0b] + bx lr + LMEMCPY_C_PAD + +/* + * 0111: dst is 8-bit aligned (byte 1), src is 8-bit aligned (byte 3) + */ + ldrb r2, [r1] + ldr r3, [r1, #0x01] /* BE:r3 = 1234 LE:r3 = 4321 */ + ldr ip, [r1, #0x05] /* BE:ip = 5678 LE:ip = 8765 */ + ldr r1, [r1, #0x09] /* BE:r1 = 9ABx LE:r1 = xBA9 */ + strb r2, [r0] +#ifdef __ARMEB__ + mov r2, r3, lsr #16 /* r2 = ..12 */ + strh r2, [r0, #0x01] + mov r3, r3, lsl #16 /* r3 = 34.. */ + orr r3, r3, ip, lsr #16 /* r3 = 3456 */ + mov ip, ip, lsl #16 /* ip = 78.. */ + orr ip, ip, r1, lsr #16 /* ip = 789A */ + mov r1, r1, lsr #8 /* r1 = .9AB */ +#else + strh r3, [r0, #0x01] + mov r3, r3, lsr #16 /* r3 = ..43 */ + orr r3, r3, ip, lsl #16 /* r3 = 6543 */ + mov ip, ip, lsr #16 /* ip = ..87 */ + orr ip, ip, r1, lsl #16 /* ip = A987 */ + mov r1, r1, lsr #16 /* r1 = ..xB */ +#endif + str r3, [r0, #0x03] + str ip, [r0, #0x07] + strb r1, [r0, #0x0b] + bx lr + LMEMCPY_C_PAD + +/* + * 1000: dst is 16-bit aligned, src is 32-bit aligned + */ + ldr ip, [r1] /* BE:ip = 0123 LE:ip = 3210 */ + ldr r3, [r1, #0x04] /* BE:r3 = 4567 LE:r3 = 7654 */ + ldr r2, [r1, #0x08] /* BE:r2 = 89AB LE:r2 = BA98 */ + mov r1, ip, lsr #16 /* BE:r1 = ..01 LE:r1 = ..32 */ +#ifdef __ARMEB__ + strh r1, [r0] + mov r1, ip, lsl #16 /* r1 = 23.. */ + orr r1, r1, r3, lsr #16 /* r1 = 2345 */ + mov r3, r3, lsl #16 /* r3 = 67.. */ + orr r3, r3, r2, lsr #16 /* r3 = 6789 */ +#else + strh ip, [r0] + orr r1, r1, r3, lsl #16 /* r1 = 5432 */ + mov r3, r3, lsr #16 /* r3 = ..76 */ + orr r3, r3, r2, lsl #16 /* r3 = 9876 */ + mov r2, r2, lsr #16 /* r2 = ..BA */ +#endif + str r1, [r0, #0x02] + str r3, [r0, #0x06] + strh r2, [r0, #0x0a] + bx lr + LMEMCPY_C_PAD + +/* + * 1001: dst is 16-bit aligned, src is 8-bit aligned (byte 1) + */ + ldr r2, [r1, #-1] /* BE:r2 = x012 LE:r2 = 210x */ + ldr r3, [r1, #0x03] /* BE:r3 = 3456 LE:r3 = 6543 */ + mov ip, r2, lsr #8 /* BE:ip = .x01 LE:ip = .210 */ + strh ip, [r0] + ldr ip, [r1, #0x07] /* BE:ip = 789A LE:ip = A987 */ + ldrb r1, [r1, #0x0b] /* r1 = ...B */ +#ifdef __ARMEB__ + mov r2, r2, lsl #24 /* r2 = 2... */ + orr r2, r2, r3, lsr #8 /* r2 = 2345 */ + mov r3, r3, lsl #24 /* r3 = 6... */ + orr r3, r3, ip, lsr #8 /* r3 = 6789 */ + orr r1, r1, ip, lsl #8 /* r1 = 89AB */ +#else + mov r2, r2, lsr #24 /* r2 = ...2 */ + orr r2, r2, r3, lsl #8 /* r2 = 5432 */ + mov r3, r3, lsr #24 /* r3 = ...6 */ + orr r3, r3, ip, lsl #8 /* r3 = 9876 */ + mov r1, r1, lsl #8 /* r1 = ..B. */ + orr r1, r1, ip, lsr #24 /* r1 = ..BA */ +#endif + str r2, [r0, #0x02] + str r3, [r0, #0x06] + strh r1, [r0, #0x0a] + bx lr + LMEMCPY_C_PAD + +/* + * 1010: dst is 16-bit aligned, src is 16-bit aligned + */ + ldrh r2, [r1] + ldr r3, [r1, #0x02] + ldr ip, [r1, #0x06] + ldrh r1, [r1, #0x0a] + strh r2, [r0] + str r3, [r0, #0x02] + str ip, [r0, #0x06] + strh r1, [r0, #0x0a] + bx lr + LMEMCPY_C_PAD + +/* + * 1011: dst is 16-bit aligned, src is 8-bit aligned (byte 3) + */ + ldr r2, [r1, #0x09] /* BE:r2 = 9ABx LE:r2 = xBA9 */ + ldr r3, [r1, #0x05] /* BE:r3 = 5678 LE:r3 = 8765 */ + mov ip, r2, lsr #8 /* BE:ip = .9AB LE:ip = .xBA */ + strh ip, [r0, #0x0a] + ldr ip, [r1, #0x01] /* BE:ip = 1234 LE:ip = 4321 */ + ldrb r1, [r1] /* r1 = ...0 */ +#ifdef __ARMEB__ + mov r2, r2, lsr #24 /* r2 = ...9 */ + orr r2, r2, r3, lsl #8 /* r2 = 6789 */ + mov r3, r3, lsr #24 /* r3 = ...5 */ + orr r3, r3, ip, lsl #8 /* r3 = 2345 */ + mov r1, r1, lsl #8 /* r1 = ..0. */ + orr r1, r1, ip, lsr #24 /* r1 = ..01 */ +#else + mov r2, r2, lsl #24 /* r2 = 9... */ + orr r2, r2, r3, lsr #8 /* r2 = 9876 */ + mov r3, r3, lsl #24 /* r3 = 5... */ + orr r3, r3, ip, lsr #8 /* r3 = 5432 */ + orr r1, r1, ip, lsl #8 /* r1 = 3210 */ +#endif + str r2, [r0, #0x06] + str r3, [r0, #0x02] + strh r1, [r0] + bx lr + LMEMCPY_C_PAD + +/* + * 1100: dst is 8-bit aligned (byte 3), src is 32-bit aligned + */ + ldr r2, [r1] /* BE:r2 = 0123 LE:r2 = 3210 */ + ldr ip, [r1, #0x04] /* BE:ip = 4567 LE:ip = 7654 */ + ldr r1, [r1, #0x08] /* BE:r1 = 89AB LE:r1 = BA98 */ +#ifdef __ARMEB__ + mov r3, r2, lsr #24 /* r3 = ...0 */ + strb r3, [r0] + mov r2, r2, lsl #8 /* r2 = 123. */ + orr r2, r2, ip, lsr #24 /* r2 = 1234 */ + str r2, [r0, #0x01] + mov r2, ip, lsl #8 /* r2 = 567. */ + orr r2, r2, r1, lsr #24 /* r2 = 5678 */ + str r2, [r0, #0x05] + mov r2, r1, lsr #8 /* r2 = ..9A */ + strh r2, [r0, #0x09] + strb r1, [r0, #0x0b] +#else + strb r2, [r0] + mov r3, r2, lsr #8 /* r3 = .321 */ + orr r3, r3, ip, lsl #24 /* r3 = 4321 */ + str r3, [r0, #0x01] + mov r3, ip, lsr #8 /* r3 = .765 */ + orr r3, r3, r1, lsl #24 /* r3 = 8765 */ + str r3, [r0, #0x05] + mov r1, r1, lsr #8 /* r1 = .BA9 */ + strh r1, [r0, #0x09] + mov r1, r1, lsr #16 /* r1 = ...B */ + strb r1, [r0, #0x0b] +#endif + bx lr + LMEMCPY_C_PAD + +/* + * 1101: dst is 8-bit aligned (byte 3), src is 8-bit aligned (byte 1) + */ + ldrb r2, [r1, #0x0b] /* r2 = ...B */ + ldr r3, [r1, #0x07] /* BE:r3 = 789A LE:r3 = A987 */ + ldr ip, [r1, #0x03] /* BE:ip = 3456 LE:ip = 6543 */ + ldr r1, [r1, #-1] /* BE:r1 = x012 LE:r1 = 210x */ + strb r2, [r0, #0x0b] +#ifdef __ARMEB__ + strh r3, [r0, #0x09] + mov r3, r3, lsr #16 /* r3 = ..78 */ + orr r3, r3, ip, lsl #16 /* r3 = 5678 */ + mov ip, ip, lsr #16 /* ip = ..34 */ + orr ip, ip, r1, lsl #16 /* ip = 1234 */ + mov r1, r1, lsr #16 /* r1 = ..x0 */ +#else + mov r2, r3, lsr #16 /* r2 = ..A9 */ + strh r2, [r0, #0x09] + mov r3, r3, lsl #16 /* r3 = 87.. */ + orr r3, r3, ip, lsr #16 /* r3 = 8765 */ + mov ip, ip, lsl #16 /* ip = 43.. */ + orr ip, ip, r1, lsr #16 /* ip = 4321 */ + mov r1, r1, lsr #8 /* r1 = .210 */ +#endif + str r3, [r0, #0x05] + str ip, [r0, #0x01] + strb r1, [r0] + bx lr + LMEMCPY_C_PAD + +/* + * 1110: dst is 8-bit aligned (byte 3), src is 16-bit aligned + */ +#ifdef __ARMEB__ + ldrh r2, [r1, #0x0a] /* r2 = ..AB */ + ldr ip, [r1, #0x06] /* ip = 6789 */ + ldr r3, [r1, #0x02] /* r3 = 2345 */ + ldrh r1, [r1] /* r1 = ..01 */ + strb r2, [r0, #0x0b] + mov r2, r2, lsr #8 /* r2 = ...A */ + orr r2, r2, ip, lsl #8 /* r2 = 789A */ + mov ip, ip, lsr #8 /* ip = .678 */ + orr ip, ip, r3, lsl #24 /* ip = 5678 */ + mov r3, r3, lsr #8 /* r3 = .234 */ + orr r3, r3, r1, lsl #24 /* r3 = 1234 */ + mov r1, r1, lsr #8 /* r1 = ...0 */ + strb r1, [r0] + str r3, [r0, #0x01] + str ip, [r0, #0x05] + strh r2, [r0, #0x09] +#else + ldrh r2, [r1] /* r2 = ..10 */ + ldr r3, [r1, #0x02] /* r3 = 5432 */ + ldr ip, [r1, #0x06] /* ip = 9876 */ + ldrh r1, [r1, #0x0a] /* r1 = ..BA */ + strb r2, [r0] + mov r2, r2, lsr #8 /* r2 = ...1 */ + orr r2, r2, r3, lsl #8 /* r2 = 4321 */ + mov r3, r3, lsr #24 /* r3 = ...5 */ + orr r3, r3, ip, lsl #8 /* r3 = 8765 */ + mov ip, ip, lsr #24 /* ip = ...9 */ + orr ip, ip, r1, lsl #8 /* ip = .BA9 */ + mov r1, r1, lsr #8 /* r1 = ...B */ + str r2, [r0, #0x01] + str r3, [r0, #0x05] + strh ip, [r0, #0x09] + strb r1, [r0, #0x0b] +#endif + bx lr + LMEMCPY_C_PAD + +/* + * 1111: dst is 8-bit aligned (byte 3), src is 8-bit aligned (byte 3) + */ + ldrb r2, [r1] + ldr r3, [r1, #0x01] + ldr ip, [r1, #0x05] + strb r2, [r0] + ldrh r2, [r1, #0x09] + ldrb r1, [r1, #0x0b] + str r3, [r0, #0x01] + str ip, [r0, #0x05] + strh r2, [r0, #0x09] + strb r1, [r0, #0x0b] + bx lr +#endif /* !_STANDALONE */ diff --git a/common/lib/libc/arch/arm/string/memmove.S b/common/lib/libc/arch/arm/string/memmove.S new file mode 100644 index 000000000..606d7055f --- /dev/null +++ b/common/lib/libc/arch/arm/string/memmove.S @@ -0,0 +1,581 @@ +/* $NetBSD: memmove.S,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Neil A. Carson and Mark Brinicombe + * + * 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 + +#ifndef _BCOPY +/* LINTSTUB: Func: void *memmove(void *, const void *, size_t) */ +ENTRY(memmove) +#else +/* bcopy = memcpy/memmove with arguments reversed. */ +/* LINTSTUB: Func: void bcopy(void *, void *, size_t) */ +ENTRY(bcopy) + /* switch the source and destination registers */ + eor r0, r1, r0 + eor r1, r0, r1 + eor r0, r1, r0 +#endif + /* Do the buffers overlap? */ + cmp r0, r1 + RETc(eq) /* Bail now if src/dst are the same */ + subhs r3, r0, r1 /* if (dst > src) r3 = dst - src */ + sublo r3, r1, r0 /* if (src > dst) r3 = src - dst */ + cmp r3, r2 /* if (r3 >= len) we have an overlap */ + bhs PIC_SYM(_C_LABEL(memcpy), PLT) + + /* Determine copy direction */ + cmp r1, r0 + bcc .Lmemmove_backwards + + moveq r0, #0 /* Quick abort for len=0 */ + RETc(eq) + + stmdb sp!, {r0, lr} /* memmove() returns dest addr */ + subs r2, r2, #4 + blt .Lmemmove_fl4 /* less than 4 bytes */ + ands r12, r0, #3 + bne .Lmemmove_fdestul /* oh unaligned destination addr */ + ands r12, r1, #3 + bne .Lmemmove_fsrcul /* oh unaligned source addr */ + +.Lmemmove_ft8: + /* We have aligned source and destination */ + subs r2, r2, #8 + blt .Lmemmove_fl12 /* less than 12 bytes (4 from above) */ + subs r2, r2, #0x14 + blt .Lmemmove_fl32 /* less than 32 bytes (12 from above) */ + stmdb sp!, {r4} /* borrow r4 */ + + /* blat 32 bytes at a time */ + /* XXX for really big copies perhaps we should use more registers */ +.Lmemmove_floop32: + ldmia r1!, {r3, r4, r12, lr} + stmia r0!, {r3, r4, r12, lr} + ldmia r1!, {r3, r4, r12, lr} + stmia r0!, {r3, r4, r12, lr} + subs r2, r2, #0x20 + bge .Lmemmove_floop32 + + cmn r2, #0x10 + ldmgeia r1!, {r3, r4, r12, lr} /* blat a remaining 16 bytes */ + stmgeia r0!, {r3, r4, r12, lr} + subge r2, r2, #0x10 + ldmia sp!, {r4} /* return r4 */ + +.Lmemmove_fl32: + adds r2, r2, #0x14 + + /* blat 12 bytes at a time */ +.Lmemmove_floop12: + ldmgeia r1!, {r3, r12, lr} + stmgeia r0!, {r3, r12, lr} + subges r2, r2, #0x0c + bge .Lmemmove_floop12 + +.Lmemmove_fl12: + adds r2, r2, #8 + blt .Lmemmove_fl4 + + subs r2, r2, #4 + ldrlt r3, [r1], #4 + strlt r3, [r0], #4 + ldmgeia r1!, {r3, r12} + stmgeia r0!, {r3, r12} + subge r2, r2, #4 + +.Lmemmove_fl4: + /* less than 4 bytes to go */ + adds r2, r2, #4 + ldmeqia sp!, {r0, pc} /* done */ + + /* copy the crud byte at a time */ + cmp r2, #2 + ldrb r3, [r1], #1 + strb r3, [r0], #1 + ldrgeb r3, [r1], #1 + strgeb r3, [r0], #1 + ldrgtb r3, [r1], #1 + strgtb r3, [r0], #1 + ldmia sp!, {r0, pc} + + /* erg - unaligned destination */ +.Lmemmove_fdestul: + rsb r12, r12, #4 + cmp r12, #2 + + /* align destination with byte copies */ + ldrb r3, [r1], #1 + strb r3, [r0], #1 + ldrgeb r3, [r1], #1 + strgeb r3, [r0], #1 + ldrgtb r3, [r1], #1 + strgtb r3, [r0], #1 + subs r2, r2, r12 + blt .Lmemmove_fl4 /* less the 4 bytes */ + + ands r12, r1, #3 + beq .Lmemmove_ft8 /* we have an aligned source */ + + /* erg - unaligned source */ + /* This is where it gets nasty ... */ +.Lmemmove_fsrcul: + bic r1, r1, #3 + ldr lr, [r1], #4 + cmp r12, #2 + bgt .Lmemmove_fsrcul3 + beq .Lmemmove_fsrcul2 + cmp r2, #0x0c + blt .Lmemmove_fsrcul1loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5} + +.Lmemmove_fsrcul1loop16: +#ifdef __ARMEB__ + mov r3, lr, lsl #8 +#else + mov r3, lr, lsr #8 +#endif + ldmia r1!, {r4, r5, r12, lr} +#ifdef __ARMEB__ + orr r3, r3, r4, lsr #24 + mov r4, r4, lsl #8 + orr r4, r4, r5, lsr #24 + mov r5, r5, lsl #8 + orr r5, r5, r12, lsr #24 + mov r12, r12, lsl #8 + orr r12, r12, lr, lsr #24 +#else + orr r3, r3, r4, lsl #24 + mov r4, r4, lsr #8 + orr r4, r4, r5, lsl #24 + mov r5, r5, lsr #8 + orr r5, r5, r12, lsl #24 + mov r12, r12, lsr #8 + orr r12, r12, lr, lsl #24 +#endif + stmia r0!, {r3-r5, r12} + subs r2, r2, #0x10 + bge .Lmemmove_fsrcul1loop16 + ldmia sp!, {r4, r5} + adds r2, r2, #0x0c + blt .Lmemmove_fsrcul1l4 + +.Lmemmove_fsrcul1loop4: +#ifdef __ARMEB__ + mov r12, lr, lsl #8 +#else + mov r12, lr, lsr #8 +#endif + ldr lr, [r1], #4 +#ifdef __ARMEB__ + orr r12, r12, lr, lsr #24 +#else + orr r12, r12, lr, lsl #24 +#endif + str r12, [r0], #4 + subs r2, r2, #4 + bge .Lmemmove_fsrcul1loop4 + +.Lmemmove_fsrcul1l4: + sub r1, r1, #3 + b .Lmemmove_fl4 + +.Lmemmove_fsrcul2: + cmp r2, #0x0c + blt .Lmemmove_fsrcul2loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5} + +.Lmemmove_fsrcul2loop16: +#ifdef __ARMEB__ + mov r3, lr, lsl #16 +#else + mov r3, lr, lsr #16 +#endif + ldmia r1!, {r4, r5, r12, lr} +#ifdef __ARMEB__ + orr r3, r3, r4, lsr #16 + mov r4, r4, lsl #16 + orr r4, r4, r5, lsr #16 + mov r5, r5, lsl #16 + orr r5, r5, r12, lsr #16 + mov r12, r12, lsl #16 + orr r12, r12, lr, lsr #16 +#else + orr r3, r3, r4, lsl #16 + mov r4, r4, lsr #16 + orr r4, r4, r5, lsl #16 + mov r5, r5, lsr #16 + orr r5, r5, r12, lsl #16 + mov r12, r12, lsr #16 + orr r12, r12, lr, lsl #16 +#endif + stmia r0!, {r3-r5, r12} + subs r2, r2, #0x10 + bge .Lmemmove_fsrcul2loop16 + ldmia sp!, {r4, r5} + adds r2, r2, #0x0c + blt .Lmemmove_fsrcul2l4 + +.Lmemmove_fsrcul2loop4: +#ifdef __ARMEB__ + mov r12, lr, lsl #16 +#else + mov r12, lr, lsr #16 +#endif + ldr lr, [r1], #4 +#ifdef __ARMEB__ + orr r12, r12, lr, lsr #16 +#else + orr r12, r12, lr, lsl #16 +#endif + str r12, [r0], #4 + subs r2, r2, #4 + bge .Lmemmove_fsrcul2loop4 + +.Lmemmove_fsrcul2l4: + sub r1, r1, #2 + b .Lmemmove_fl4 + +.Lmemmove_fsrcul3: + cmp r2, #0x0c + blt .Lmemmove_fsrcul3loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5} + +.Lmemmove_fsrcul3loop16: +#ifdef __ARMEB__ + mov r3, lr, lsl #24 +#else + mov r3, lr, lsr #24 +#endif + ldmia r1!, {r4, r5, r12, lr} +#ifdef __ARMEB__ + orr r3, r3, r4, lsr #8 + mov r4, r4, lsl #24 + orr r4, r4, r5, lsr #8 + mov r5, r5, lsl #24 + orr r5, r5, r12, lsr #8 + mov r12, r12, lsl #24 + orr r12, r12, lr, lsr #8 +#else + orr r3, r3, r4, lsl #8 + mov r4, r4, lsr #24 + orr r4, r4, r5, lsl #8 + mov r5, r5, lsr #24 + orr r5, r5, r12, lsl #8 + mov r12, r12, lsr #24 + orr r12, r12, lr, lsl #8 +#endif + stmia r0!, {r3-r5, r12} + subs r2, r2, #0x10 + bge .Lmemmove_fsrcul3loop16 + ldmia sp!, {r4, r5} + adds r2, r2, #0x0c + blt .Lmemmove_fsrcul3l4 + +.Lmemmove_fsrcul3loop4: +#ifdef __ARMEB__ + mov r12, lr, lsl #24 +#else + mov r12, lr, lsr #24 +#endif + ldr lr, [r1], #4 +#ifdef __ARMEB__ + orr r12, r12, lr, lsr #8 +#else + orr r12, r12, lr, lsl #8 +#endif + str r12, [r0], #4 + subs r2, r2, #4 + bge .Lmemmove_fsrcul3loop4 + +.Lmemmove_fsrcul3l4: + sub r1, r1, #1 + b .Lmemmove_fl4 + +.Lmemmove_backwards: + add r1, r1, r2 + add r0, r0, r2 + subs r2, r2, #4 + blt .Lmemmove_bl4 /* less than 4 bytes */ + ands r12, r0, #3 + bne .Lmemmove_bdestul /* oh unaligned destination addr */ + ands r12, r1, #3 + bne .Lmemmove_bsrcul /* oh unaligned source addr */ + +.Lmemmove_bt8: + /* We have aligned source and destination */ + subs r2, r2, #8 + blt .Lmemmove_bl12 /* less than 12 bytes (4 from above) */ + stmdb sp!, {r4, lr} + subs r2, r2, #0x14 /* less than 32 bytes (12 from above) */ + blt .Lmemmove_bl32 + + /* blat 32 bytes at a time */ + /* XXX for really big copies perhaps we should use more registers */ +.Lmemmove_bloop32: + ldmdb r1!, {r3, r4, r12, lr} + stmdb r0!, {r3, r4, r12, lr} + ldmdb r1!, {r3, r4, r12, lr} + stmdb r0!, {r3, r4, r12, lr} + subs r2, r2, #0x20 + bge .Lmemmove_bloop32 + +.Lmemmove_bl32: + cmn r2, #0x10 + ldmgedb r1!, {r3, r4, r12, lr} /* blat a remaining 16 bytes */ + stmgedb r0!, {r3, r4, r12, lr} + subge r2, r2, #0x10 + adds r2, r2, #0x14 + ldmgedb r1!, {r3, r12, lr} /* blat a remaining 12 bytes */ + stmgedb r0!, {r3, r12, lr} + subge r2, r2, #0x0c + ldmia sp!, {r4, lr} + +.Lmemmove_bl12: + adds r2, r2, #8 + blt .Lmemmove_bl4 + subs r2, r2, #4 + ldrlt r3, [r1, #-4]! + strlt r3, [r0, #-4]! + ldmgedb r1!, {r3, r12} + stmgedb r0!, {r3, r12} + subge r2, r2, #4 + +.Lmemmove_bl4: + /* less than 4 bytes to go */ + adds r2, r2, #4 + RETc(eq) + + /* copy the crud byte at a time */ + cmp r2, #2 + ldrb r3, [r1, #-1]! + strb r3, [r0, #-1]! + ldrgeb r3, [r1, #-1]! + strgeb r3, [r0, #-1]! + ldrgtb r3, [r1, #-1]! + strgtb r3, [r0, #-1]! + RET + + /* erg - unaligned destination */ +.Lmemmove_bdestul: + cmp r12, #2 + + /* align destination with byte copies */ + ldrb r3, [r1, #-1]! + strb r3, [r0, #-1]! + ldrgeb r3, [r1, #-1]! + strgeb r3, [r0, #-1]! + ldrgtb r3, [r1, #-1]! + strgtb r3, [r0, #-1]! + subs r2, r2, r12 + blt .Lmemmove_bl4 /* less than 4 bytes to go */ + ands r12, r1, #3 + beq .Lmemmove_bt8 /* we have an aligned source */ + + /* erg - unaligned source */ + /* This is where it gets nasty ... */ +.Lmemmove_bsrcul: + bic r1, r1, #3 + ldr r3, [r1, #0] + cmp r12, #2 + blt .Lmemmove_bsrcul1 + beq .Lmemmove_bsrcul2 + cmp r2, #0x0c + blt .Lmemmove_bsrcul3loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5, lr} + +.Lmemmove_bsrcul3loop16: +#ifdef __ARMEB__ + mov lr, r3, lsr #8 +#else + mov lr, r3, lsl #8 +#endif + ldmdb r1!, {r3-r5, r12} +#ifdef __ARMEB__ + orr lr, lr, r12, lsl #24 + mov r12, r12, lsr #8 + orr r12, r12, r5, lsl #24 + mov r5, r5, lsr #8 + orr r5, r5, r4, lsl #24 + mov r4, r4, lsr #8 + orr r4, r4, r3, lsl #24 +#else + orr lr, lr, r12, lsr #24 + mov r12, r12, lsl #8 + orr r12, r12, r5, lsr #24 + mov r5, r5, lsl #8 + orr r5, r5, r4, lsr #24 + mov r4, r4, lsl #8 + orr r4, r4, r3, lsr #24 +#endif + stmdb r0!, {r4, r5, r12, lr} + subs r2, r2, #0x10 + bge .Lmemmove_bsrcul3loop16 + ldmia sp!, {r4, r5, lr} + adds r2, r2, #0x0c + blt .Lmemmove_bsrcul3l4 + +.Lmemmove_bsrcul3loop4: +#ifdef __ARMEB__ + mov r12, r3, lsr #8 +#else + mov r12, r3, lsl #8 +#endif + ldr r3, [r1, #-4]! +#ifdef __ARMEB__ + orr r12, r12, r3, lsl #24 +#else + orr r12, r12, r3, lsr #24 +#endif + str r12, [r0, #-4]! + subs r2, r2, #4 + bge .Lmemmove_bsrcul3loop4 + +.Lmemmove_bsrcul3l4: + add r1, r1, #3 + b .Lmemmove_bl4 + +.Lmemmove_bsrcul2: + cmp r2, #0x0c + blt .Lmemmove_bsrcul2loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5, lr} + +.Lmemmove_bsrcul2loop16: +#ifdef __ARMEB__ + mov lr, r3, lsr #16 +#else + mov lr, r3, lsl #16 +#endif + ldmdb r1!, {r3-r5, r12} +#ifdef __ARMEB__ + orr lr, lr, r12, lsl #16 + mov r12, r12, lsr #16 + orr r12, r12, r5, lsl #16 + mov r5, r5, lsr #16 + orr r5, r5, r4, lsl #16 + mov r4, r4, lsr #16 + orr r4, r4, r3, lsl #16 +#else + orr lr, lr, r12, lsr #16 + mov r12, r12, lsl #16 + orr r12, r12, r5, lsr #16 + mov r5, r5, lsl #16 + orr r5, r5, r4, lsr #16 + mov r4, r4, lsl #16 + orr r4, r4, r3, lsr #16 +#endif + stmdb r0!, {r4, r5, r12, lr} + subs r2, r2, #0x10 + bge .Lmemmove_bsrcul2loop16 + ldmia sp!, {r4, r5, lr} + adds r2, r2, #0x0c + blt .Lmemmove_bsrcul2l4 + +.Lmemmove_bsrcul2loop4: +#ifdef __ARMEB__ + mov r12, r3, lsr #16 +#else + mov r12, r3, lsl #16 +#endif + ldr r3, [r1, #-4]! +#ifdef __ARMEB__ + orr r12, r12, r3, lsl #16 +#else + orr r12, r12, r3, lsr #16 +#endif + str r12, [r0, #-4]! + subs r2, r2, #4 + bge .Lmemmove_bsrcul2loop4 + +.Lmemmove_bsrcul2l4: + add r1, r1, #2 + b .Lmemmove_bl4 + +.Lmemmove_bsrcul1: + cmp r2, #0x0c + blt .Lmemmove_bsrcul1loop4 + sub r2, r2, #0x0c + stmdb sp!, {r4, r5, lr} + +.Lmemmove_bsrcul1loop32: +#ifdef __ARMEB__ + mov lr, r3, lsr #24 +#else + mov lr, r3, lsl #24 +#endif + ldmdb r1!, {r3-r5, r12} +#ifdef __ARMEB__ + orr lr, lr, r12, lsl #8 + mov r12, r12, lsr #24 + orr r12, r12, r5, lsl #8 + mov r5, r5, lsr #24 + orr r5, r5, r4, lsl #8 + mov r4, r4, lsr #24 + orr r4, r4, r3, lsl #8 +#else + orr lr, lr, r12, lsr #8 + mov r12, r12, lsl #24 + orr r12, r12, r5, lsr #8 + mov r5, r5, lsl #24 + orr r5, r5, r4, lsr #8 + mov r4, r4, lsl #24 + orr r4, r4, r3, lsr #8 +#endif + stmdb r0!, {r4, r5, r12, lr} + subs r2, r2, #0x10 + bge .Lmemmove_bsrcul1loop32 + ldmia sp!, {r4, r5, lr} + adds r2, r2, #0x0c + blt .Lmemmove_bsrcul1l4 + +.Lmemmove_bsrcul1loop4: +#ifdef __ARMEB__ + mov r12, r3, lsr #24 +#else + mov r12, r3, lsl #24 +#endif + ldr r3, [r1, #-4]! +#ifdef __ARMEB__ + orr r12, r12, r3, lsl #8 +#else + orr r12, r12, r3, lsr #8 +#endif + str r12, [r0, #-4]! + subs r2, r2, #4 + bge .Lmemmove_bsrcul1loop4 + +.Lmemmove_bsrcul1l4: + add r1, r1, #1 + b .Lmemmove_bl4 diff --git a/common/lib/libc/arch/arm/string/memset.S b/common/lib/libc/arch/arm/string/memset.S new file mode 100644 index 000000000..cd9c6fa2d --- /dev/null +++ b/common/lib/libc/arch/arm/string/memset.S @@ -0,0 +1,235 @@ +/* $NetBSD: memset.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Steve C. Woodford for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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. + */ +/* + * Copyright (c) 1995 Mark Brinicombe. + * 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 by Mark Brinicombe. + * 4. The name of the company nor 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 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 + +/* + * memset: Sets a block of memory to the specified value + * + * On entry: + * r0 - dest address + * r1 - byte to write + * r2 - number of bytes to write + * + * On exit: + * r0 - dest address + */ +#ifdef _BZERO +/* LINTSTUB: Func: void bzero(void *, size_t) */ +ENTRY(bzero) + mov r3, #0x00 +#else +/* LINTSTUB: Func: void *memset(void *, int, size_t) */ +ENTRY(memset) + and r3, r1, #0xff /* We deal with bytes */ + mov r1, r2 +#endif + cmp r1, #0x04 /* Do we have less than 4 bytes */ + mov ip, r0 + blt .Lmemset_lessthanfour + + /* Ok first we will word align the address */ + ands r2, ip, #0x03 /* Get the bottom two bits */ + bne .Lmemset_wordunaligned /* The address is not word aligned */ + + /* We are now word aligned */ +.Lmemset_wordaligned: +#ifndef _BZERO + orr r3, r3, r3, lsl #8 /* Extend value to 16-bits */ +#endif +#ifdef __XSCALE__ + tst ip, #0x04 /* Quad-align for Xscale */ +#else + cmp r1, #0x10 +#endif +#ifndef _BZERO + orr r3, r3, r3, lsl #16 /* Extend value to 32-bits */ +#endif +#ifdef __XSCALE__ + subne r1, r1, #0x04 /* Quad-align if necessary */ + strne r3, [ip], #0x04 + cmp r1, #0x10 +#endif + blt .Lmemset_loop4 /* If less than 16 then use words */ + mov r2, r3 /* Duplicate data */ + cmp r1, #0x80 /* If < 128 then skip the big loop */ + blt .Lmemset_loop32 + + /* Do 128 bytes at a time */ +.Lmemset_loop128: + subs r1, r1, #0x80 +#ifdef __XSCALE__ + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 +#else + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} +#endif + bgt .Lmemset_loop128 + RETc(eq) /* Zero length so just exit */ + + add r1, r1, #0x80 /* Adjust for extra sub */ + + /* Do 32 bytes at a time */ +.Lmemset_loop32: + subs r1, r1, #0x20 +#ifdef __XSCALE__ + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 +#else + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} +#endif + bgt .Lmemset_loop32 + RETc(eq) /* Zero length so just exit */ + + adds r1, r1, #0x10 /* Partially adjust for extra sub */ + + /* Deal with 16 bytes or more */ +#ifdef __XSCALE__ + strged r2, [ip], #0x08 + strged r2, [ip], #0x08 +#else + stmgeia ip!, {r2-r3} + stmgeia ip!, {r2-r3} +#endif + RETc(eq) /* Zero length so just exit */ + + addlt r1, r1, #0x10 /* Possibly adjust for extra sub */ + + /* We have at least 4 bytes so copy as words */ +.Lmemset_loop4: + subs r1, r1, #0x04 + strge r3, [ip], #0x04 + bgt .Lmemset_loop4 + RETc(eq) /* Zero length so just exit */ + +#ifdef __XSCALE__ + /* Compensate for 64-bit alignment check */ + adds r1, r1, #0x04 + RETc(eq) + cmp r1, #2 +#else + cmp r1, #-2 +#endif + + strb r3, [ip], #0x01 /* Set 1 byte */ + strgeb r3, [ip], #0x01 /* Set another byte */ + strgtb r3, [ip] /* and a third */ + RET /* Exit */ + +.Lmemset_wordunaligned: + rsb r2, r2, #0x004 + strb r3, [ip], #0x01 /* Set 1 byte */ + cmp r2, #0x02 + strgeb r3, [ip], #0x01 /* Set another byte */ + sub r1, r1, r2 + strgtb r3, [ip], #0x01 /* and a third */ + cmp r1, #0x04 /* More than 4 bytes left? */ + bge .Lmemset_wordaligned /* Yup */ + +.Lmemset_lessthanfour: + cmp r1, #0x00 + RETc(eq) /* Zero length so exit */ + strb r3, [ip], #0x01 /* Set 1 byte */ + cmp r1, #0x02 + strgeb r3, [ip], #0x01 /* Set another byte */ + strgtb r3, [ip] /* and a third */ + RET /* Exit */ diff --git a/common/lib/libc/arch/arm/string/strcmp.S b/common/lib/libc/arch/arm/string/strcmp.S new file mode 100644 index 000000000..a00cbbf3b --- /dev/null +++ b/common/lib/libc/arch/arm/string/strcmp.S @@ -0,0 +1,43 @@ +/* $NetBSD: strcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright (c) 2002 ARM Ltd + * 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 company may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY ARM LTD ``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 ARM LTD 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 + +RCSID("$NetBSD: strcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $") + +ENTRY(strcmp) +1: + ldrb r2, [r0], #1 + ldrb r3, [r1], #1 + cmp r2, #1 + cmpcs r2, r3 + beq 1b + sub r0, r2, r3 + RET diff --git a/common/lib/libc/arch/arm/string/strncmp.S b/common/lib/libc/arch/arm/string/strncmp.S new file mode 100644 index 000000000..fe770525c --- /dev/null +++ b/common/lib/libc/arch/arm/string/strncmp.S @@ -0,0 +1,51 @@ +/* $NetBSD: strncmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright (c) 2002 ARM Ltd + * 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 company may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY ARM LTD ``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 ARM LTD 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 + +RCSID("$NetBSD: strncmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $") + +ENTRY(strncmp) +/* if ((len - 1) < 0) return 0 */ + subs r2, r2, #1 + movmi r0, #0 + RETc(mi) + +/* ip == last src address to compare */ + add ip, r0, r2 +1: + ldrb r2, [r0], #1 + ldrb r3, [r1], #1 + cmp ip, r0 + cmpcs r2, #1 + cmpcs r2, r3 + beq 1b + sub r0, r2, r3 + RET diff --git a/common/lib/libc/arch/hppa/atomic/Makefile.inc b/common/lib/libc/arch/hppa/atomic/Makefile.inc new file mode 100644 index 000000000..fe0a95052 --- /dev/null +++ b/common/lib/libc/arch/hppa/atomic/Makefile.inc @@ -0,0 +1,23 @@ +# $NetBSD: Makefile.inc,v 1.8 2011/01/17 07:29:17 skrll Exp $ + +.if defined(LIB) + +. if (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" || \ + ${LIB} == "rump") + +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c atomic_and_32_cas.c \ + atomic_and_32_nv_cas.c atomic_dec_32_cas.c atomic_dec_32_nv_cas.c \ + atomic_inc_32_cas.c atomic_inc_32_nv_cas.c atomic_or_32_cas.c \ + atomic_or_32_nv_cas.c atomic_swap_32_cas.c membar_ops_nop.c + +. endif + +. if (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_testset.c +SRCS+= atomic_cas_up.S +CPPFLAGS+= -D__HAVE_ASM_ATOMIC_CAS_UP + +. endif + +.endif diff --git a/common/lib/libc/arch/hppa/atomic/atomic_cas_up.S b/common/lib/libc/arch/hppa/atomic/atomic_cas_up.S new file mode 100644 index 000000000..8304631a4 --- /dev/null +++ b/common/lib/libc/arch/hppa/atomic/atomic_cas_up.S @@ -0,0 +1,46 @@ +/* $NetBSD: atomic_cas_up.S,v 1.2 2011/01/16 12:07:26 skrll Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nick Hudson + * + * 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 + +LEAF_ENTRY_NOPROFILE(_atomic_cas_up) + .hidden _C_LABEL(_atomic_cas_up) + +RAS_START_ASM_HIDDEN(_atomic_cas) + ldw 0(%arg0), %ret0 + comb,<>,n %arg1, %ret0, 1f + stw %arg2, 0(%arg0) +RAS_END_ASM_HIDDEN(_atomic_cas) +1: + bv,n %r0(%rp) + +EXIT(_atomic_cas_up) diff --git a/common/lib/libc/arch/hppa/atomic/membar_ops.S b/common/lib/libc/arch/hppa/atomic/membar_ops.S new file mode 100644 index 000000000..a732c642c --- /dev/null +++ b/common/lib/libc/arch/hppa/atomic/membar_ops.S @@ -0,0 +1,67 @@ +/* $NetBSD: membar_ops.S,v 1.1 2011/01/17 07:40:21 skrll Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe, and by Andrew Doran. + * + * 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 "atomic_op_asm.h" + +RCSID("$NetBSD: membar_ops.S,v 1.1 2011/01/17 07:40:21 skrll Exp $") + + .text + +LEAF_ENTRY(_membar_consumer) + sync + nop + nop + nop + nop + nop + bv %r0(%rp) + nop +EXIT(_membar_consumer) + +LEAF_ENTRY(_membar_producer) + sync + nop + nop + nop + nop + nop + bv %r0(%rp) + nop +EXIT(_membar_producer) + +ATOMIC_OP_ALIAS(membar_producer,_membar_producer) +ATOMIC_OP_ALIAS(membar_consumer,_membar_consumer) +ATOMIC_OP_ALIAS(membar_enter,_membar_consumer) +STRONG_ALIAS(_membar_enter,_membar_consumer) +ATOMIC_OP_ALIAS(membar_exit,_membar_producer) +STRONG_ALIAS(_membar_exit,_membar_producer) +ATOMIC_OP_ALIAS(membar_sync,_membar_producer) +STRONG_ALIAS(_membar_sync,_membar_producer) diff --git a/common/lib/libc/arch/i386/atomic/Makefile.inc b/common/lib/libc/arch/i386/atomic/Makefile.inc new file mode 100644 index 000000000..5a7ca58bf --- /dev/null +++ b/common/lib/libc/arch/i386/atomic/Makefile.inc @@ -0,0 +1,17 @@ +# $NetBSD: Makefile.inc,v 1.7 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add_64_cas.c atomic_add_64_nv_cas.c atomic_and_64_cas.c \ + atomic_and_64_nv_cas.c atomic_dec_64_cas.c atomic_dec_64_nv_cas.c \ + atomic_inc_64_cas.c atomic_inc_64_nv_cas.c atomic_or_64_cas.c \ + atomic_or_64_nv_cas.c atomic_swap_64_cas.c atomic.S + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_cas.c + +.endif diff --git a/common/lib/libc/arch/i386/atomic/atomic.S b/common/lib/libc/arch/i386/atomic/atomic.S new file mode 100644 index 000000000..81c321be8 --- /dev/null +++ b/common/lib/libc/arch/i386/atomic/atomic.S @@ -0,0 +1,384 @@ +/* $NetBSD: atomic.S,v 1.19 2011/01/12 23:12:10 joerg Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe, and by Andrew Doran. + * + * 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 + +#ifdef _KERNEL +#define ALIAS(f, t) STRONG_ALIAS(f,t) +#else +#define ALIAS(f, t) WEAK_ALIAS(f,t) +#endif + +#ifdef _HARDKERNEL +#define LOCK(n) .Lpatch ## n: lock +#define ENDLABEL(a) _ALIGN_TEXT; LABEL(a) +#else +#define LOCK(n) lock +#define ENDLABEL(a) /* nothing */ +#endif + + .text + +ENTRY(_atomic_add_32) + movl 4(%esp), %edx + movl 8(%esp), %eax + LOCK(1) + addl %eax, (%edx) + ret + +ENTRY(_atomic_add_32_nv) + movl 4(%esp), %edx + movl 8(%esp), %eax + movl %eax, %ecx + LOCK(2) + xaddl %eax, (%edx) + addl %ecx, %eax + ret + +ENTRY(_atomic_and_32) + movl 4(%esp), %edx + movl 8(%esp), %eax + LOCK(3) + andl %eax, (%edx) + ret + +ENTRY(_atomic_and_32_nv) + movl 4(%esp), %edx + movl (%edx), %eax +0: + movl %eax, %ecx + andl 8(%esp), %ecx + LOCK(4) + cmpxchgl %ecx, (%edx) + jnz 1f + movl %ecx, %eax + ret +1: + jmp 0b + +ENTRY(_atomic_dec_32) + movl 4(%esp), %edx + LOCK(5) + decl (%edx) + ret + +ENTRY(_atomic_dec_32_nv) + movl 4(%esp), %edx + movl $-1, %eax + LOCK(6) + xaddl %eax, (%edx) + decl %eax + ret + +ENTRY(_atomic_inc_32) + movl 4(%esp), %edx + LOCK(7) + incl (%edx) + ret + +ENTRY(_atomic_inc_32_nv) + movl 4(%esp), %edx + movl $1, %eax + LOCK(8) + xaddl %eax, (%edx) + incl %eax + ret + +ENTRY(_atomic_or_32) + movl 4(%esp), %edx + movl 8(%esp), %eax + LOCK(9) + orl %eax, (%edx) + ret + +ENTRY(_atomic_or_32_nv) + movl 4(%esp), %edx + movl (%edx), %eax +0: + movl %eax, %ecx + orl 8(%esp), %ecx + LOCK(10) + cmpxchgl %ecx, (%edx) + jnz 1f + movl %ecx, %eax + ret +1: + jmp 0b + +ENTRY(_atomic_swap_32) + movl 4(%esp), %edx + movl 8(%esp), %eax + xchgl %eax, (%edx) + ret + +ENTRY(_atomic_cas_32) + movl 4(%esp), %edx + movl 8(%esp), %eax + movl 12(%esp), %ecx + LOCK(12) + cmpxchgl %ecx, (%edx) + /* %eax now contains the old value */ + ret + +ENTRY(_atomic_cas_32_ni) + movl 4(%esp), %edx + movl 8(%esp), %eax + movl 12(%esp), %ecx + cmpxchgl %ecx, (%edx) + /* %eax now contains the old value */ + ret + +ENTRY(_membar_consumer) + LOCK(13) + addl $0, -4(%esp) + ret +ENDLABEL(membar_consumer_end) + +ENTRY(_membar_producer) + /* A store is enough */ + movl $0, -4(%esp) + ret +ENDLABEL(membar_producer_end) + +ENTRY(_membar_sync) + LOCK(14) + addl $0, -4(%esp) + ret +ENDLABEL(membar_sync_end) + +#ifdef _HARDKERNEL +ENTRY(_atomic_cas_64) + pushf + cli + pushl %edi + pushl %ebx + movl 12(%esp), %edi + movl 16(%esp), %eax + movl 20(%esp), %edx + movl 24(%esp), %ebx + movl 28(%esp), %ecx + cmpl 0(%edi), %eax + jne 2f + cmpl 4(%edi), %edx + jne 2f + movl %ebx, 0(%edi) + movl %ecx, 4(%edi) +1: + popl %ebx + popl %edi + popf + ret +2: + movl 0(%edi), %eax + movl 4(%edi), %edx + jmp 1b +ENDLABEL(_atomic_cas_64_end) + +ENTRY(_atomic_cas_cx8) + pushl %edi + pushl %ebx + movl 12(%esp), %edi + movl 16(%esp), %eax + movl 20(%esp), %edx + movl 24(%esp), %ebx + movl 28(%esp), %ecx + LOCK(15) + cmpxchg8b (%edi) + popl %ebx + popl %edi + ret +#ifdef GPROF + .space 16, 0x90 +#else + .space 32, 0x90 +#endif +ENDLABEL(_atomic_cas_cx8_end) + +ENTRY(sse2_lfence) + lfence + ret +ENDLABEL(sse2_lfence_end) + +ENTRY(sse2_mfence) + mfence + ret +ENDLABEL(sse2_mfence_end) + +atomic_lockpatch: + .globl atomic_lockpatch + .long .Lpatch1, .Lpatch2, .Lpatch3, .Lpatch4, .Lpatch5 + .long .Lpatch6, .Lpatch7, .Lpatch8, .Lpatch9, .Lpatch10 + .long .Lpatch12, .Lpatch13, .Lpatch14, .Lpatch15, 0 +#else +ENTRY(_atomic_cas_64) + pushl %edi + pushl %ebx + movl 12(%esp), %edi + movl 16(%esp), %eax + movl 20(%esp), %edx + movl 24(%esp), %ebx + movl 28(%esp), %ecx + lock + cmpxchg8b (%edi) + popl %ebx + popl %edi + ret +#endif /* _HARDKERNEL */ + +ALIAS(atomic_add_32,_atomic_add_32) +ALIAS(atomic_add_int,_atomic_add_32) +ALIAS(atomic_add_long,_atomic_add_32) +ALIAS(atomic_add_ptr,_atomic_add_32) + +ALIAS(atomic_add_32_nv,_atomic_add_32_nv) +ALIAS(atomic_add_int_nv,_atomic_add_32_nv) +ALIAS(atomic_add_long_nv,_atomic_add_32_nv) +ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv) + +ALIAS(atomic_and_32,_atomic_and_32) +ALIAS(atomic_and_uint,_atomic_and_32) +ALIAS(atomic_and_ulong,_atomic_and_32) +ALIAS(atomic_and_ptr,_atomic_and_32) + +ALIAS(atomic_and_32_nv,_atomic_and_32_nv) +ALIAS(atomic_and_uint_nv,_atomic_and_32_nv) +ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv) +ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv) + +ALIAS(atomic_dec_32,_atomic_dec_32) +ALIAS(atomic_dec_uint,_atomic_dec_32) +ALIAS(atomic_dec_ulong,_atomic_dec_32) +ALIAS(atomic_dec_ptr,_atomic_dec_32) + +ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv) +ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv) +ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv) +ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv) + +ALIAS(atomic_inc_32,_atomic_inc_32) +ALIAS(atomic_inc_uint,_atomic_inc_32) +ALIAS(atomic_inc_ulong,_atomic_inc_32) +ALIAS(atomic_inc_ptr,_atomic_inc_32) + +ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv) +ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv) +ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv) +ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv) + +ALIAS(atomic_or_32,_atomic_or_32) +ALIAS(atomic_or_uint,_atomic_or_32) +ALIAS(atomic_or_ulong,_atomic_or_32) +ALIAS(atomic_or_ptr,_atomic_or_32) + +ALIAS(atomic_or_32_nv,_atomic_or_32_nv) +ALIAS(atomic_or_uint_nv,_atomic_or_32_nv) +ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv) +ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv) + +ALIAS(atomic_swap_32,_atomic_swap_32) +ALIAS(atomic_swap_uint,_atomic_swap_32) +ALIAS(atomic_swap_ulong,_atomic_swap_32) +ALIAS(atomic_swap_ptr,_atomic_swap_32) + +ALIAS(atomic_cas_32,_atomic_cas_32) +ALIAS(atomic_cas_uint,_atomic_cas_32) +ALIAS(atomic_cas_ulong,_atomic_cas_32) +ALIAS(atomic_cas_ptr,_atomic_cas_32) + +ALIAS(atomic_cas_32_ni,_atomic_cas_32_ni) +ALIAS(atomic_cas_uint_ni,_atomic_cas_32_ni) +ALIAS(atomic_cas_ulong_ni,_atomic_cas_32_ni) +ALIAS(atomic_cas_ptr_ni,_atomic_cas_32_ni) + +ALIAS(atomic_cas_64,_atomic_cas_64) +ALIAS(atomic_cas_64_ni,_atomic_cas_64) + +ALIAS(membar_consumer,_membar_consumer) +ALIAS(membar_producer,_membar_producer) +ALIAS(membar_enter,_membar_consumer) +ALIAS(membar_exit,_membar_producer) +ALIAS(membar_sync,_membar_sync) + +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_long,_atomic_add_32) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32) + +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv) + +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ptr,_atomic_and_32) + +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_32_nv) + +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32) + +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv) + +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32) + +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv) + +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ptr,_atomic_or_32) + +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_32_nv) + +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32) + +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32) + +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32_ni) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_32_ni) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_32_ni) + +STRONG_ALIAS(_membar_enter,_membar_consumer) +STRONG_ALIAS(_membar_exit,_membar_producer) diff --git a/common/lib/libc/arch/i386/gen/byte_swap_2.S b/common/lib/libc/arch/i386/gen/byte_swap_2.S new file mode 100644 index 000000000..5150f1761 --- /dev/null +++ b/common/lib/libc/arch/i386/gen/byte_swap_2.S @@ -0,0 +1,52 @@ +/* $NetBSD: byte_swap_2.S,v 1.3 2007/11/28 17:03:35 ad Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)htons.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: byte_swap_2.S,v 1.3 2007/11/28 17:03:35 ad Exp $") +#endif + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap16)) +#else /* defined(_KERNEL) || defined(_STANDALONE) */ +_ENTRY(_C_LABEL(__bswap16)) +#endif /* defined(_KERNEL) || defined(_STANDALONE) */ +_ENTRY(_C_LABEL(ntohs)) +_ENTRY(_C_LABEL(htons)) +_PROF_PROLOGUE + movzwl 4(%esp),%eax + xchgb %al, %ah + ret diff --git a/common/lib/libc/arch/i386/gen/byte_swap_4.S b/common/lib/libc/arch/i386/gen/byte_swap_4.S new file mode 100644 index 000000000..0c48ae91d --- /dev/null +++ b/common/lib/libc/arch/i386/gen/byte_swap_4.S @@ -0,0 +1,52 @@ +/* $NetBSD: byte_swap_4.S,v 1.3 2007/11/28 17:03:35 ad Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)htonl.s 5.3 (Berkeley) 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: byte_swap_4.S,v 1.3 2007/11/28 17:03:35 ad Exp $") +#endif + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap32)) +#else /* defined(_KERNEL) || defined(_STANDALONE) */ +_ENTRY(_C_LABEL(__bswap32)) +#endif /* defined(_KERNEL) || defined(_STANDALONE) */ +_ENTRY(_C_LABEL(ntohl)) +_ENTRY(_C_LABEL(htonl)) +_PROF_PROLOGUE + movl 4(%esp),%eax + bswap %eax + ret diff --git a/common/lib/libc/arch/i386/string/ffs.S b/common/lib/libc/arch/i386/string/ffs.S new file mode 100644 index 000000000..d53226252 --- /dev/null +++ b/common/lib/libc/arch/i386/string/ffs.S @@ -0,0 +1,20 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif + +ENTRY(ffs) + bsfl 4(%esp),%eax + jz L1 /* ZF is set if all bits are 0 */ + incl %eax /* bits numbered from 1, not 0 */ + ret + + _ALIGN_TEXT +L1: xorl %eax,%eax /* clear result */ + ret diff --git a/common/lib/libc/arch/i386/string/memchr.S b/common/lib/libc/arch/i386/string/memchr.S new file mode 100644 index 000000000..4aaddf348 --- /dev/null +++ b/common/lib/libc/arch/i386/string/memchr.S @@ -0,0 +1,109 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: memchr.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif + +ENTRY(memchr) + pushl %esi + movl 8(%esp),%eax + movzbl 12(%esp),%ecx + movl 16(%esp),%esi + + /* + * Align to word boundary. + * Consider unrolling loop? + */ + testl %esi,%esi /* nbytes == 0? */ + je .Lzero +.Lalign: + testb $3,%al + je .Lword_aligned + cmpb (%eax),%cl + je .Ldone + incl %eax + decl %esi + jnz .Lalign + jmp .Lzero + +.Lword_aligned: + /* copy char to all bytes in word */ + movb %cl,%ch + movl %ecx,%edx + sall $16,%ecx + orl %edx,%ecx + + _ALIGN_TEXT +.Lloop: + cmpl $3,%esi /* nbytes > 4 */ + jbe .Lbyte + movl (%eax),%edx + addl $4,%eax + xorl %ecx,%edx + subl $4,%esi + subl $0x01010101,%edx + testl $0x80808080,%edx + je .Lloop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word are + * equal to ch. + */ + + /* + * High load-use latency on the Athlon leads to significant + * stalls, so we preload the next char as soon as possible + * instead of using cmp mem8, reg8. + * + * Alignment here avoids a stall on the Athlon, even though + * it's not a branch target. + */ + _ALIGN_TEXT + cmpb -4(%eax),%cl /* 1st byte == ch? */ + movb -3(%eax),%dl + jne 1f + subl $4,%eax + jmp .Ldone + + _ALIGN_TEXT +1: cmpb %dl,%cl /* 2nd byte == ch? */ + movb -2(%eax),%dl + jne 1f + subl $3,%eax + jmp .Ldone + + _ALIGN_TEXT +1: cmpb %dl,%cl /* 3rd byte == ch? */ + movb -1(%eax),%dl + jne 1f + subl $2,%eax + jmp .Ldone + + _ALIGN_TEXT +1: cmpb %dl,%cl /* 4th byte == ch? */ + jne .Lloop + decl %eax + jmp .Ldone + +.Lbyte: + testl %esi,%esi + je .Lzero +.Lbyte_loop: + cmpb (%eax),%cl + je .Ldone + incl %eax + decl %esi + jnz .Lbyte_loop + +.Lzero: + xorl %eax,%eax + +.Ldone: + popl %esi + ret diff --git a/common/lib/libc/arch/i386/string/memcmp.S b/common/lib/libc/arch/i386/string/memcmp.S new file mode 100644 index 000000000..a3ad4043b --- /dev/null +++ b/common/lib/libc/arch/i386/string/memcmp.S @@ -0,0 +1,47 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: memcmp.S,v 1.2 2007/11/12 18:41:59 ad Exp $") +#endif + +ENTRY(memcmp) + pushl %edi + pushl %esi + movl 12(%esp),%edi + movl 16(%esp),%esi + + movl 20(%esp),%ecx /* compare by words */ + shrl $2,%ecx + repe + cmpsl + jne L5 /* do we match so far? */ + + movl 20(%esp),%ecx /* compare remainder by bytes */ + andl $3,%ecx + repe + cmpsb + jne L6 /* do we match? */ + + xorl %eax,%eax /* we match, return zero */ + popl %esi + popl %edi + ret + +L5: movl $4,%ecx /* We know that one of the next */ + subl %ecx,%edi /* four pairs of bytes do not */ + subl %ecx,%esi /* match. */ + repe + cmpsb +L6: xorl %eax,%eax /* Perform unsigned comparison */ + movb -1(%edi),%al + xorl %edx,%edx + movb -1(%esi),%dl + subl %edx,%eax + popl %esi + popl %edi + ret diff --git a/common/lib/libc/arch/i386/string/memcpy.S b/common/lib/libc/arch/i386/string/memcpy.S new file mode 100644 index 000000000..abb1b97f4 --- /dev/null +++ b/common/lib/libc/arch/i386/string/memcpy.S @@ -0,0 +1,133 @@ +/* $NetBSD: memcpy.S,v 1.3 2007/11/12 18:41:59 ad Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from locore.s. + * Optimised by David Laight 2003 + * + * 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 + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: memcpy.S,v 1.3 2007/11/12 18:41:59 ad Exp $") +#endif + + /* + * (ov)bcopy (src,dst,cnt) + * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800 + */ + +#ifdef BCOPY +ENTRY(bcopy) +#else +#ifdef MEMMOVE +ENTRY(memmove) +#else +#define MEMCPY +#define NO_OVERLAP +ENTRY(memcpy) +#endif +#endif + push %esi + mov %edi,%edx +#if defined(MEMCPY) || defined(MEMMOVE) + movl 8(%esp),%edi + movl 12(%esp),%esi +#else + movl 8(%esp),%esi + movl 12(%esp),%edi +#endif + movl 16(%esp),%ecx +#if defined(NO_OVERLAP) + movl %ecx,%eax +#else + movl %edi,%eax + subl %esi,%eax + cmpl %ecx,%eax /* overlapping? */ + movl %ecx,%eax + jb .Lbackwards +#endif + /* nope, copy forwards. */ + shrl $2,%ecx /* copy by words */ + rep + movsl + and $3,%eax /* any bytes left? */ + jnz .Ltrailing +.Ldone: +#if defined(MEMCPY) || defined(MEMMOVE) + movl 8(%esp),%eax +#endif + mov %edx,%edi + pop %esi + ret + +.Ltrailing: + cmp $2,%eax + jb 1f + movw (%esi),%ax + movw %ax,(%edi) + je .Ldone + movb 2(%esi),%al + movb %al,2(%edi) + jmp .Ldone +1: movb (%esi),%al + movb %al,(%edi) + jmp .Ldone + +#if !defined(NO_OVERLAP) +.Lbackwards: + addl %ecx,%edi /* copy backwards. */ + addl %ecx,%esi + and $3,%eax /* any fractional bytes? */ + jnz .Lback_align +.Lback_aligned: + shrl $2,%ecx + subl $4,%esi + subl $4,%edi + std + rep + movsl + cld + jmp .Ldone + +.Lback_align: + sub %eax,%esi + sub %eax,%edi + cmp $2,%eax + jb 1f + je 2f + movb 2(%esi),%al + movb %al,2(%edi) +2: movw (%esi),%ax + movw %ax,(%edi) + jmp .Lback_aligned +1: movb (%esi),%al + movb %al,(%edi) + jmp .Lback_aligned +#endif diff --git a/common/lib/libc/arch/i386/string/memmove.S b/common/lib/libc/arch/i386/string/memmove.S new file mode 100644 index 000000000..2b8ea6dd2 --- /dev/null +++ b/common/lib/libc/arch/i386/string/memmove.S @@ -0,0 +1,4 @@ +/* $NetBSD: memmove.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +#define MEMMOVE +#include "memcpy.S" diff --git a/common/lib/libc/arch/i386/string/memset.S b/common/lib/libc/arch/i386/string/memset.S new file mode 100644 index 000000000..91009a321 --- /dev/null +++ b/common/lib/libc/arch/i386/string/memset.S @@ -0,0 +1,106 @@ +/* $NetBSD: memset.S,v 1.4 2008/04/29 06:53:01 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Laight. + * + * 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 + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: memset.S,v 1.4 2008/04/29 06:53:01 martin Exp $") +#endif + +#ifdef BZERO +ENTRY(bzero) +#else +ENTRY(memset) +#endif +#ifdef BZERO + movl 8(%esp),%ecx + xor %eax,%eax +#else + movl 12(%esp),%ecx + movzbl 8(%esp),%eax /* unsigned char, zero extend */ +#endif + cmpl $0x0f,%ecx /* avoid mispredicted branch... */ + + pushl %edi + movl 8(%esp),%edi + + /* + * if the string is too short, it's really not worth the overhead + * of aligning to word boundries, etc. So we jump to a plain + * unaligned set. + * + * NB aligning the transfer is actually pointless on my athlon 700, + * It does make a difference to a PII though. + * + * The PII, PIII and PIV all seem to have a massive performance + * drop when the initial target address is an odd multiple of 4. + */ + jbe .Lby_bytes + +#ifndef BZERO + movb %al,%ah /* copy char to all bytes in word */ + movl %eax,%edx + sall $16,%eax + orl %edx,%eax +#endif + + movl %edi,%edx /* detect misalignment */ + neg %edx + andl $7,%edx + jnz .Lalign +.Laligned: + movl %eax,-4(%edi,%ecx) /* zap last 4 bytes */ + shrl $2,%ecx /* zero by words */ + rep + stosl +.Ldone: +#ifndef BZERO + movl 8(%esp),%eax /* return address of buffer */ +#endif + pop %edi + ret + +.Lalign: + movl %eax,(%edi) /* zap first 8 bytes */ + movl %eax,4(%edi) + subl %edx,%ecx /* remove from main count */ + add %edx,%edi + jmp .Laligned + +.Lby_bytes: + rep + stosb + +#ifndef BZERO + movl 8(%esp),%eax /* return address of buffer */ +#endif + popl %edi + ret diff --git a/common/lib/libc/arch/i386/string/strcat.S b/common/lib/libc/arch/i386/string/strcat.S new file mode 100644 index 000000000..8196f4e91 --- /dev/null +++ b/common/lib/libc/arch/i386/string/strcat.S @@ -0,0 +1,127 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strcat.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif + +ENTRY(strcat) + pushl %ebx + movl 8(%esp),%ecx + movl 12(%esp),%eax + + /* + * Align destination to word boundary. + * Consider unrolling loop? + */ +.Lscan: +.Lscan_align: + testb $3,%cl + je .Lscan_aligned + cmpb $0,(%ecx) + je .Lcopy + incl %ecx + jmp .Lscan_align + + _ALIGN_TEXT +.Lscan_aligned: +.Lscan_loop: + movl (%ecx),%ebx + addl $4,%ecx + leal -0x01010101(%ebx),%edx + testl $0x80808080,%edx + je .Lscan_loop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word equal 0. + */ + + /* + * The optimal code for determining whether each byte is zero + * differs by processor. This space-optimized code should be + * acceptable on all, especially since we don't expect it to + * be run frequently, + */ + + testb %bl,%bl /* 1st byte == 0? */ + jne 1f + subl $4,%ecx + jmp .Lcopy + +1: testb %bh,%bh /* 2nd byte == 0? */ + jne 1f + subl $3,%ecx + jmp .Lcopy + +1: shrl $16,%ebx + testb %bl,%bl /* 3rd byte == 0? */ + jne 1f + subl $2,%ecx + jmp .Lcopy + +1: testb %bh,%bh /* 4th byte == 0? */ + jne .Lscan_loop + subl $1,%ecx + + /* + * Align source to a word boundary. + * Consider unrolling loop? + */ +.Lcopy: +.Lcopy_align: + testl $3,%eax + je .Lcopy_aligned + movb (%eax),%bl + incl %eax + movb %bl,(%ecx) + incl %ecx + testb %bl,%bl + jne .Lcopy_align + jmp .Ldone + + _ALIGN_TEXT +.Lcopy_loop: + movl %ebx,(%ecx) + addl $4,%ecx +.Lcopy_aligned: + movl (%eax),%ebx + addl $4,%eax + leal -0x01010101(%ebx),%edx + testl $0x80808080,%edx + je .Lcopy_loop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word equal 0. + */ + + movb %bl,(%ecx) + incl %ecx + testb %bl,%bl + je .Ldone + + movb %bh,(%ecx) + incl %ecx + testb %bh,%bh + je .Ldone + + shrl $16,%ebx + movb %bl,(%ecx) + incl %ecx + testb %bl,%bl + je .Ldone + + movb %bh,(%ecx) + incl %ecx + testb %bh,%bh + jne .Lcopy_aligned + +.Ldone: + movl 8(%esp),%eax + popl %ebx + ret diff --git a/common/lib/libc/arch/i386/string/strchr.S b/common/lib/libc/arch/i386/string/strchr.S new file mode 100644 index 000000000..400bcdad7 --- /dev/null +++ b/common/lib/libc/arch/i386/string/strchr.S @@ -0,0 +1,103 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $") +#endif + +ENTRY(strchr) + pushl %esi + pushl %ebx + movl 12(%esp),%eax + movzbl 16(%esp),%ecx + + /* + * Align to word boundary. + * Consider unrolling loop? + */ +.Lalign: + testb $3,%al + je .Lword_aligned + movb (%eax),%bl + cmpb %cl,%bl + je .Ldone + testb %bl,%bl + je .Lzero + incl %eax + jmp .Lalign + +.Lword_aligned: + /* copy char to all bytes in word */ + movb %cl,%ch + movl %ecx,%edx + sall $16,%ecx + orl %edx,%ecx + + /* Check whether any byte in the word is equal to ch or 0. */ + _ALIGN_TEXT +.Lloop: + movl (%eax),%ebx + addl $4,%eax + movl %ebx,%esi + leal -0x01010101(%ebx),%edx + xorl %ecx,%esi + subl $0x01010101,%esi + orl %esi,%edx + testl $0x80808080,%edx + je .Lloop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word match + * ch or are equal to 0. + */ + + /* + * Alignment here avoids a stall on the Athlon, even though + * it's not a branch target. + */ + + _ALIGN_TEXT + cmpb %cl,%bl /* 1st byte == ch? */ + jne 1f + subl $4,%eax + jmp .Ldone +1: testb %bl,%bl /* 1st byte == 0? */ + je .Lzero + + cmpb %cl,%bh /* 2nd byte == ch? */ + jne 1f + subl $3,%eax + jmp .Ldone +1: testb %bh,%bh /* 2nd byte == 0? */ + je .Lzero + + shrl $16,%ebx + cmpb %cl,%bl /* 3rd byte == ch? */ + jne 1f + subl $2,%eax + jmp .Ldone +1: testb %bl,%bl /* 3rd byte == 0? */ + je .Lzero + + cmpb %cl,%bh /* 4th byte == ch? */ + jne 1f + decl %eax + jmp .Ldone +1: testb %bh,%bh /* 4th byte == 0? */ + jne .Lloop + +.Lzero: + /* If a ch wasn't found, return 0. */ + xorl %eax,%eax + +.Ldone: + popl %ebx + popl %esi + ret + +STRONG_ALIAS(index,strchr) diff --git a/common/lib/libc/arch/i386/string/strcmp.S b/common/lib/libc/arch/i386/string/strcmp.S new file mode 100644 index 000000000..341ed826a --- /dev/null +++ b/common/lib/libc/arch/i386/string/strcmp.S @@ -0,0 +1,77 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif + +ENTRY(strcmp) + pushl %esi + pushl %ebx + movl 12(%esp),%ebx + movl 16(%esp),%esi + + /* + * Align s1 to word boundary. + * Consider unrolling loop? + */ +.Ls1align: + testb $3,%bl + je .Ls1aligned + movb (%ebx),%al + incl %ebx + movb (%esi),%dl + incl %esi + testb %al,%al + je .Ldone + cmpb %al,%dl + je .Ls1align + jmp .Ldone + + /* + * Check whether s2 is aligned to a word boundary. If it is, we + * can compare by words. Otherwise we have to compare by bytes. + */ +.Ls1aligned: + testl $3,%esi + jne .Lbyte_loop + + subl $4,%ebx + subl $4,%esi + + _ALIGN_TEXT +.Lword_loop: + movl 4(%ebx),%eax + addl $4,%ebx + movl 4(%esi),%edx + addl $4,%esi + cmpl %eax,%edx + jne .Lbyte_loop + subl $0x01010101,%edx + notl %eax + andl %eax,%edx + testl $0x80808080,%edx + je .Lword_loop + + _ALIGN_TEXT +.Lbyte_loop: + movb (%ebx),%al + incl %ebx + movb (%esi),%dl + incl %esi + testb %al,%al + je .Ldone + cmpb %al,%dl + je .Lbyte_loop + +.Ldone: + movzbl %al,%eax + movzbl %dl,%edx + subl %edx,%eax + popl %ebx + popl %esi + ret diff --git a/common/lib/libc/arch/i386/string/strcpy.S b/common/lib/libc/arch/i386/string/strcpy.S new file mode 100644 index 000000000..f14281aef --- /dev/null +++ b/common/lib/libc/arch/i386/string/strcpy.S @@ -0,0 +1,86 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strcpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif + +/* + * This strcpy implementation copies a byte at a time until the + * source pointer is aligned to a word boundary, it then copies by + * words until it finds a word containing a zero byte, and finally + * copies by bytes until the end of the string is reached. + * + * While this may result in unaligned stores if the source and + * destination pointers are unaligned with respect to each other, + * it is still faster than either byte copies or the overhead of + * an implementation suitable for machines with strict alignment + * requirements. + */ + +ENTRY(strcpy) + pushl %ebx + movl 8(%esp),%ecx + movl 12(%esp),%eax + + /* + * Align source to a word boundary. + * Consider unrolling loop? + */ + _ALIGN_TEXT +.Lalign: + testl $3,%eax + je .Lword_aligned + movb (%eax),%bl + incl %eax + movb %bl,(%ecx) + incl %ecx + testb %bl,%bl + jne .Lalign + jmp .Ldone + + _ALIGN_TEXT +.Lloop: + movl %ebx,(%ecx) + addl $4,%ecx +.Lword_aligned: + movl (%eax),%ebx + addl $4,%eax + leal -0x01010101(%ebx),%edx + testl $0x80808080,%edx + je .Lloop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word equal 0. + */ + + movb %bl,(%ecx) + incl %ecx + testb %bl,%bl + je .Ldone + + movb %bh,(%ecx) + incl %ecx + testb %bh,%bh + je .Ldone + + shrl $16,%ebx + movb %bl,(%ecx) + incl %ecx + testb %bl,%bl + je .Ldone + + movb %bh,(%ecx) + incl %ecx + testb %bh,%bh + jne .Lword_aligned + +.Ldone: + movl 8(%esp),%eax + popl %ebx + ret diff --git a/common/lib/libc/arch/i386/string/strlen.S b/common/lib/libc/arch/i386/string/strlen.S new file mode 100644 index 000000000..8914339a7 --- /dev/null +++ b/common/lib/libc/arch/i386/string/strlen.S @@ -0,0 +1,141 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strlen.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif + +ENTRY(strlen) + movl 4(%esp),%eax + +.Lalign: + /* Consider unrolling loop? */ + testb $3,%al + je .Lword_aligned + cmpb $0,(%eax) + je .Ldone + incl %eax + jmp .Lalign + + /* + * There are many well known branch-free sequences which are used + * for determining whether a zero-byte is contained within a word. + * These sequences are generally much more efficent than loading + * and comparing each byte individually. + * + * The expression [1,2]: + * + * (1) ~(((x & 0x7f7f7f7f) + 0x7f7f7f7f) | (x | 0x7f7f7f7f)) + * + * evaluates to a non-zero value if any of the bytes in the + * original word is zero. + * + * It also has the useful property that bytes in the result word + * that correspond to non-zero bytes in the original word have + * the value 0x00, while bytes corresponding to zero bytes have + * the value 0x80. This allows calculation of the first (and + * last) occurrence of a zero byte within the word (useful for C's + * str* primitives) by counting the number of leading (or + * trailing) zeros and dividing the result by 8. On machines + * without (or with slow) clz() / ctz() instructions, testing + * each byte in the result word for zero is necessary. + * + * This typically takes 4 instructions (5 on machines without + * "not-or") not including those needed to load the constant. + * + * + * The expression: + * + * (2) ((x - 0x01010101) & ~x & 0x80808080) + * + * evaluates to a non-zero value if any of the bytes in the + * original word is zero. + * + * On little endian machines, the first byte in the result word + * that corresponds to a zero byte in the original byte is 0x80, + * so clz() can be used as above. On big endian machines, and + * little endian machines without (or with a slow) clz() insn, + * testing each byte in the original for zero is necessary. + * + * This typically takes 3 instructions (4 on machines without + * "and with complement") not including those needed to load + * constants. + * + * + * The expression: + * + * (3) ((x - 0x01010101) & 0x80808080) + * + * always evaluates to a non-zero value if any of the bytes in + * the original word is zero. However, in rare cases, it also + * evaluates to a non-zero value when none of the bytes in the + * original word is zero. + * + * To account for possible false positives, each byte of the + * original word must be checked when the expression evaluates to + * a non-zero value. However, because it is simpler than those + * presented above, code that uses it will be faster as long as + * the rate of false positives is low. + * + * This is likely, because the the false positive can only occur + * if the most siginificant bit of a byte within the word is set. + * The expression will never fail for typical 7-bit ASCII strings. + * + * This typically takes 2 instructions not including those needed + * to load constants. + * + * + * [1] Henry S. Warren Jr., "Hacker's Delight", Addison-Westley 2003 + * + * [2] International Business Machines, "The PowerPC Compiler Writer's + * Guide", Warthman Associates, 1996 + */ + + _ALIGN_TEXT +.Lword_aligned: +.Lloop: + movl (%eax),%ecx + addl $4,%eax + leal -0x01010101(%ecx),%edx + testl $0x80808080,%edx + je .Lloop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word equal 0. + */ + + /* + * The optimal code for determining whether each byte is zero + * differs by processor. This space-optimized code should be + * acceptable on all, especially since we don't expect it to + * be run frequently, + */ + + testb %cl,%cl /* 1st byte == 0? */ + jne 1f + subl $4,%eax + jmp .Ldone + +1: testb %ch,%ch /* 2nd byte == 0? */ + jne 1f + subl $3,%eax + jmp .Ldone + +1: shrl $16,%ecx + testb %cl,%cl /* 3rd byte == 0? */ + jne 1f + subl $2,%eax + jmp .Ldone + +1: testb %ch,%ch /* 4th byte == 0? */ + jne .Lloop + decl %eax + +.Ldone: + subl 4(%esp),%eax + ret diff --git a/common/lib/libc/arch/i386/string/strrchr.S b/common/lib/libc/arch/i386/string/strrchr.S new file mode 100644 index 000000000..26ef5441d --- /dev/null +++ b/common/lib/libc/arch/i386/string/strrchr.S @@ -0,0 +1,96 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strrchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $") +#endif + +ENTRY(strrchr) + pushl %esi + pushl %edi + pushl %ebx + movl 16(%esp),%edx + movzbl 20(%esp),%ecx + + /* zero return value */ + xorl %eax,%eax + + /* + * Align to word boundary. + * Consider unrolling loop? + */ +.Lalign: + testb $3,%dl + je .Lword_aligned + movb (%edx),%bl + cmpb %cl,%bl + jne 1f + movl %edx,%eax +1: testb %bl,%bl + je .Ldone + incl %edx + jmp .Lalign + +.Lword_aligned: + /* copy char to all bytes in word */ + movb %cl,%ch + movl %ecx,%edi + sall $16,%ecx + orl %edi,%ecx + + /* Check whether any byte in the word is equal to ch or 0. */ + _ALIGN_TEXT +.Lloop: + movl (%edx),%ebx + addl $4,%edx + movl %ebx,%esi + leal -0x01010101(%ebx),%edi + xorl %ecx,%esi + subl $0x01010101,%esi + orl %esi,%edi + testl $0x80808080,%edi + je .Lloop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word match + * ch or are equal to 0. + */ + + _ALIGN_TEXT + cmpb %cl,%bl /* 1st byte == ch? */ + jne 1f + leal -4(%edx),%eax +1: testb %bl,%bl /* 1st byte == 0? */ + je .Ldone + + cmpb %cl,%bh /* 2nd byte == ch? */ + jne 1f + leal -3(%edx),%eax +1: testb %bh,%bh /* 2nd byte == 0? */ + je .Ldone + + shrl $16,%ebx + cmpb %cl,%bl /* 3rd byte == ch? */ + jne 1f + leal -2(%edx),%eax +1: testb %bl,%bl /* 3rd byte == 0? */ + je .Ldone + + cmpb %cl,%bh /* 4th byte == ch? */ + jne 1f + leal -1(%edx),%eax +1: testb %bh,%bh /* 4th byte == 0? */ + jne .Lloop + +.Ldone: + popl %ebx + popl %edi + popl %esi + ret + +STRONG_ALIAS(rindex,strrchr) diff --git a/common/lib/libc/arch/ia64/atomic/Makefile.inc b/common/lib/libc/arch/ia64/atomic/Makefile.inc new file mode 100644 index 000000000..2bef12c41 --- /dev/null +++ b/common/lib/libc/arch/ia64/atomic/Makefile.inc @@ -0,0 +1,21 @@ +# $NetBSD: Makefile.inc,v 1.3 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c \ + atomic_add_64_cas.c atomic_add_64_nv_cas.c \ + atomic_and_32_cas.c atomic_and_32_nv_cas.c \ + atomic_and_64_cas.c atomic_and_64_nv_cas.c \ + atomic_or_32_cas.c atomic_or_32_nv_cas.c \ + atomic_or_64_cas.c atomic_or_64_nv_cas.c + +SRCS+= atomic.S + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS += atomic_init_cas.c + +.endif diff --git a/common/lib/libc/arch/ia64/atomic/atomic.S b/common/lib/libc/arch/ia64/atomic/atomic.S new file mode 100644 index 000000000..192d2421a --- /dev/null +++ b/common/lib/libc/arch/ia64/atomic/atomic.S @@ -0,0 +1,270 @@ +/* $NetBSD: atomic.S,v 1.5 2009/11/09 14:22:02 skrll Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Takayoshi Kochi. + * + * 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 + +#ifdef _KERNEL +#define ALIAS(f, t) STRONG_ALIAS(f,t) +#else +#define ALIAS(f, t) WEAK_ALIAS(f,t) +#endif + + .text + +ENTRY(_atomic_dec_32,1) + fetchadd4.rel r8=[r32],-1 + br.ret.sptk rp +END(_atomic_dec_32) + +ENTRY(_atomic_dec_64,1) + fetchadd8.rel r8=[r32],-1 + br.ret.sptk rp +END(_atomic_dec_64) + +ENTRY(_atomic_dec_32_nv,1) + fetchadd4.rel r8=[r32],-1 + br.ret.sptk rp +END(_atomic_dec_32_nv) + +ENTRY(_atomic_dec_64_nv,1) + fetchadd8.rel r8=[r32],-1 + br.ret.sptk rp +END(_atomic_dec_64_nv) + +ENTRY(_atomic_inc_32,1) + fetchadd4.rel r8=[r32],1 + br.ret.sptk rp +END(_atomic_inc_32) + +ENTRY(_atomic_inc_64,1) + fetchadd8.rel r8=[r32],1 + br.ret.sptk rp +END(_atomic_inc_64) + +ENTRY(_atomic_inc_32_nv,1) + fetchadd4.rel r8=[r32],1 + br.ret.sptk rp +END(_atomic_inc_32_nv) + +ENTRY(_atomic_inc_64_nv,1) + fetchadd8.rel r8=[r32],1 + br.ret.sptk rp +END(_atomic_inc_64_nv) + +ENTRY(_atomic_swap_32,2) + xchg4 r8=[r32],r33 + ;; + mov r33=r8 + br.ret.sptk rp +END(_atomic_swap_32) + +ENTRY(_atomic_swap_64,2) + xchg8 r8=[r32],r33 + ;; + mov r33=r8 + br.ret.sptk rp +END(_atomic_swap_64) + +ENTRY(_atomic_cas_32,3) + mov ar.ccv=r33 + ;; + cmpxchg4.acq r8=[r32],r34,ar.ccv + br.ret.sptk rp +END(_atomic_cas_32) + +ENTRY(_atomic_cas_64,3) + mov ar.ccv=r33 + ;; + cmpxchg8.acq r8=[r32],r34,ar.ccv + br.ret.sptk rp +END(_atomic_cas_64) + +ENTRY(_membar_consumer,0) + mf + br.ret.sptk rp +END(_membar_consumer) + +ENTRY(_membar_producer,0) + mf + br.ret.sptk rp +END(_membar_producer) + +ENTRY(_membar_enter,0) + mf + br.ret.sptk rp +END(_membar_enter) + +ENTRY(_membar_exit,0) + mf + br.ret.sptk rp +END(_membar_exit) + +ENTRY(_membar_sync,0) + mf + br.ret.sptk rp +END(_membar_sync) + + +ALIAS(atomic_add_32,_atomic_add_32) +ALIAS(atomic_add_int,_atomic_add_32) +ALIAS(atomic_add_64,_atomic_add_64) +ALIAS(atomic_add_long,_atomic_add_64) +ALIAS(atomic_add_ptr,_atomic_add_64) + +ALIAS(atomic_add_32_nv,_atomic_add_32_nv) +ALIAS(atomic_add_int_nv,_atomic_add_32_nv) +ALIAS(atomic_add_64_nv,_atomic_add_64_nv) +ALIAS(atomic_add_long_nv,_atomic_add_64_nv) +ALIAS(atomic_add_ptr_nv,_atomic_add_64_nv) + +ALIAS(atomic_and_32,_atomic_and_32) +ALIAS(atomic_and_uint,_atomic_and_32) +ALIAS(atomic_and_64,_atomic_and_64) +ALIAS(atomic_and_ulong,_atomic_and_64) +ALIAS(atomic_and_ptr,_atomic_and_64) + +ALIAS(atomic_and_32_nv,_atomic_and_32_nv) +ALIAS(atomic_and_uint_nv,_atomic_and_32_nv) +ALIAS(atomic_and_64_nv,_atomic_and_64_nv) +ALIAS(atomic_and_ulong_nv,_atomic_and_64_nv) +ALIAS(atomic_and_ptr_nv,_atomic_and_64_nv) + +ALIAS(atomic_dec_32,_atomic_dec_32) +ALIAS(atomic_dec_uint,_atomic_dec_32) +ALIAS(atomic_dec_64,_atomic_dec_64) +ALIAS(atomic_dec_ulong,_atomic_dec_64) +ALIAS(atomic_dec_ptr,_atomic_dec_64) + +ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv) +ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv) +ALIAS(atomic_dec_64_nv,_atomic_dec_64_nv) +ALIAS(atomic_dec_ulong_nv,_atomic_dec_64_nv) +ALIAS(atomic_dec_ptr_nv,_atomic_dec_64_nv) + +ALIAS(atomic_inc_32,_atomic_inc_32) +ALIAS(atomic_inc_uint,_atomic_inc_32) +ALIAS(atomic_inc_64,_atomic_inc_64) +ALIAS(atomic_inc_ulong,_atomic_inc_64) +ALIAS(atomic_inc_ptr,_atomic_inc_64) + +ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv) +ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv) +ALIAS(atomic_inc_64_nv,_atomic_inc_64_nv) +ALIAS(atomic_inc_ulong_nv,_atomic_inc_64_nv) +ALIAS(atomic_inc_ptr_nv,_atomic_inc_64_nv) + +ALIAS(atomic_or_32,_atomic_or_32) +ALIAS(atomic_or_uint,_atomic_or_32) +ALIAS(atomic_or_64,_atomic_or_64) +ALIAS(atomic_or_ulong,_atomic_or_64) +ALIAS(atomic_or_ptr,_atomic_or_64) + +ALIAS(atomic_or_32_nv,_atomic_or_32_nv) +ALIAS(atomic_or_uint_nv,_atomic_or_32_nv) +ALIAS(atomic_or_64_nv,_atomic_or_64_nv) +ALIAS(atomic_or_ulong_nv,_atomic_or_64_nv) +ALIAS(atomic_or_ptr_nv,_atomic_or_64_nv) + +ALIAS(atomic_swap_32,_atomic_swap_32) +ALIAS(atomic_swap_uint,_atomic_swap_32) +ALIAS(atomic_swap_64,_atomic_swap_64) +ALIAS(atomic_swap_ulong,_atomic_swap_64) +ALIAS(atomic_swap_ptr,_atomic_swap_64) + +ALIAS(atomic_cas_32,_atomic_cas_32) +ALIAS(atomic_cas_uint,_atomic_cas_32) +ALIAS(atomic_cas_64,_atomic_cas_64) +ALIAS(atomic_cas_ulong,_atomic_cas_64) +ALIAS(atomic_cas_ptr,_atomic_cas_64) + +ALIAS(atomic_cas_32_ni,_atomic_cas_32) +ALIAS(atomic_cas_uint_ni,_atomic_cas_32) +ALIAS(atomic_cas_64_ni,_atomic_cas_64) +ALIAS(atomic_cas_ulong_ni,_atomic_cas_64) +ALIAS(atomic_cas_ptr_ni,_atomic_cas_64) + +ALIAS(membar_consumer,_membar_consumer) +ALIAS(membar_producer,_membar_producer) +ALIAS(membar_enter,_membar_enter) +ALIAS(membar_exit,_membar_exit) +ALIAS(membar_sync,_membar_sync) + +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_long,_atomic_add_64) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_64) + +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_64_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_64_nv) + +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_64) +STRONG_ALIAS(_atomic_and_ptr,_atomic_and_64) + +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_64_nv) +STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_64_nv) + +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_64) + +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_64_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_64_nv) + +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_64) + +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_64_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_64_nv) + +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_64) +STRONG_ALIAS(_atomic_or_ptr,_atomic_or_64) + +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_64_nv) +STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_64_nv) + +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_64) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_64) + +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_64) + +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_64) diff --git a/common/lib/libc/arch/m68k/atomic/Makefile.inc b/common/lib/libc/arch/m68k/atomic/Makefile.inc new file mode 100644 index 000000000..f4e367605 --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/Makefile.inc @@ -0,0 +1,41 @@ +# $NetBSD: Makefile.inc,v 1.9 2009/01/04 17:54:29 pooka Exp $ + +# +# Note: The atomic operations here in these assembly files are atomic +# only with respect to regular memory on uniprocessor systems. Since +# we don't support any MP m68k systems, this is just fine. If we ever +# do, then these routines will probably need to be replaced with CAS- +# based routines (CAS generates an atomic bus cycle, whereas these +# others are merely single-instruction). +# + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") +.if ${MACHINE_ARCH} != "m68000" + +SRCS+= atomic_add.S atomic_and.S atomic_cas.S atomic_dec.S \ + atomic_inc.S atomic_or.S atomic_swap.S membar_ops_nop.c + +.else + +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c atomic_and_32_cas.c \ + atomic_and_32_nv_cas.c atomic_dec_32_cas.c atomic_dec_32_nv_cas.c \ + atomic_inc_32_cas.c atomic_inc_32_nv_cas.c atomic_or_32_cas.c \ + atomic_or_32_nv_cas.c atomic_swap_32_cas.c membar_ops_nop.c + +.endif +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") +.if ${MACHINE_ARCH} != "m68000" + +SRCS+= atomic_init_cas.c + +.else + +SRCS+= atomic_init_testset.c +SRCS+= atomic_cas_68000.S +CPPFLAGS+= -D__HAVE_ASM_ATOMIC_CAS_UP + +.endif +.endif diff --git a/common/lib/libc/arch/m68k/atomic/atomic_add.S b/common/lib/libc/arch/m68k/atomic/atomic_add.S new file mode 100644 index 000000000..e8c5d907e --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_add.S @@ -0,0 +1,65 @@ +/* $NetBSD: atomic_add.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + .align 2 + +ENTRY(_atomic_add_32) + movl %sp@(4), %a0 + movl %sp@(8), %d0 + addl %d0, %a0@ + rts +ATOMIC_OP_ALIAS(atomic_add_32,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_long,_atomic_add_32) +STRONG_ALIAS(_atomic_add_long,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_ptr,_atomic_add_32) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32) + +ENTRY(_atomic_add_32_nv) + movl %sp@(4), %a0 +1: movl %a0@, %d0 + movl %d0, %d1 + addl %sp@(8), %d0 + casl %d1, %d0, %a0@ + bne 1b + movl %d0, %a0 /* pointers return also in %a0 */ + rts +ATOMIC_OP_ALIAS(atomic_add_32_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_int_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_long_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv) diff --git a/common/lib/libc/arch/m68k/atomic/atomic_and.S b/common/lib/libc/arch/m68k/atomic/atomic_and.S new file mode 100644 index 000000000..4426fd0db --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_and.S @@ -0,0 +1,63 @@ +/* $NetBSD: atomic_and.S,v 1.6 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_and_32) + movl %sp@(4), %a0 + movl %sp@(8), %d0 + andl %d0, %a0@ + rts +ATOMIC_OP_ALIAS(atomic_and_32,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_ulong,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_ptr,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ptr,_atomic_and_32) + +ENTRY(_atomic_and_32_nv) + movl %sp@(4), %a0 +1: movl %a0@, %d0 + movl %d0, %d1 + andl %sp@(8), %d0 + casl %d1, %d0, %a0@ + bne 1b + rts +ATOMIC_OP_ALIAS(atomic_and_32_nv,_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_uint_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_32_nv) diff --git a/common/lib/libc/arch/m68k/atomic/atomic_cas.S b/common/lib/libc/arch/m68k/atomic/atomic_cas.S new file mode 100644 index 000000000..c5f5036d2 --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_cas.S @@ -0,0 +1,60 @@ +/* $NetBSD: atomic_cas.S,v 1.5 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_cas_32) + movl %sp@(4), %a0 + movl %sp@(8), %d0 + movl %sp@(12), %d1 + casl %d0, %d1, %a0@ + /* %d0 now contains the old value */ + movl %d0, %a0 /* pointers return also in %a0 */ + rts + +ATOMIC_OP_ALIAS(atomic_cas_32,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ulong,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ptr,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_32_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_32_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ulong_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ptr_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_32) diff --git a/common/lib/libc/arch/m68k/atomic/atomic_cas_68000.S b/common/lib/libc/arch/m68k/atomic/atomic_cas_68000.S new file mode 100644 index 000000000..c3daeadb8 --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_cas_68000.S @@ -0,0 +1,49 @@ +/* $NetBSD: atomic_cas_68000.S,v 1.3 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Steve C. Woodford. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_cas_up) + .hidden _C_LABEL(_atomic_cas_up) + + movl %sp@(4), %a0 /* Fetch ptr */ + +RAS_START_ASM_HIDDEN(_atomic_cas) + movl %a0@, %d0 /* d0 = *ptr */ + cmpl %sp@(8), %d0 /* Same as old? */ + jne 1f /* Nope */ + movl %sp@(12), %a0@ /* *ptr = new */ +RAS_END_ASM_HIDDEN(_atomic_cas) +1: rts + diff --git a/common/lib/libc/arch/m68k/atomic/atomic_dec.S b/common/lib/libc/arch/m68k/atomic/atomic_dec.S new file mode 100644 index 000000000..d5cd3e7d1 --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_dec.S @@ -0,0 +1,63 @@ +/* $NetBSD: atomic_dec.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_dec_32) + movl %sp@(4), %a0 + subql #1, %a0@ + rts +ATOMIC_OP_ALIAS(atomic_dec_32,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_ulong,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_ptr,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32) + +ENTRY(_atomic_dec_32_nv) + movl %sp@(4), %a0 +1: movl %a0@, %d0 + movl %d0, %d1 + subql #1, %d0 + casl %d1, %d0, %a0@ + bne 1b + movl %d0, %a0 /* pointers return also in %a0 */ + rts +ATOMIC_OP_ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv) diff --git a/common/lib/libc/arch/m68k/atomic/atomic_inc.S b/common/lib/libc/arch/m68k/atomic/atomic_inc.S new file mode 100644 index 000000000..bed844a49 --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_inc.S @@ -0,0 +1,63 @@ +/* $NetBSD: atomic_inc.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_inc_32) + movl %sp@(4), %a0 + addql #1, %a0@ + rts +ATOMIC_OP_ALIAS(atomic_inc_32,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_ulong,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_ptr,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32) + +ENTRY(_atomic_inc_32_nv) + movl %sp@(4), %a0 +1: movl %a0@, %d0 + movl %d0, %d1 + addql #1, %d0 + casl %d1, %d0, %a0@ + bne 1b + movl %d0, %a0 /* pointers return also in %a0 */ + rts +ATOMIC_OP_ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv) diff --git a/common/lib/libc/arch/m68k/atomic/atomic_op_asm.h b/common/lib/libc/arch/m68k/atomic/atomic_op_asm.h new file mode 100644 index 000000000..56015db0b --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_op_asm.h @@ -0,0 +1,47 @@ +/* $NetBSD: atomic_op_asm.h,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#endif /* _KERNEL */ + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/m68k/atomic/atomic_or.S b/common/lib/libc/arch/m68k/atomic/atomic_or.S new file mode 100644 index 000000000..bb739b640 --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_or.S @@ -0,0 +1,63 @@ +/* $NetBSD: atomic_or.S,v 1.6 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_or_32) + movl %sp@(4), %a0 + movl %sp@(8), %d0 + orl %d0, %a0@ + rts +ATOMIC_OP_ALIAS(atomic_or_32,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_ulong,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_ptr,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ptr,_atomic_or_32) + +ENTRY(_atomic_or_32_nv) + movl %sp@(4), %a0 +1: movl %a0@, %d0 + movl %d0, %d1 + orl %sp@(8), %d0 + casl %d1, %d0, %a0@ + bne 1b + rts +ATOMIC_OP_ALIAS(atomic_or_32_nv,_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_uint_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_32_nv) diff --git a/common/lib/libc/arch/m68k/atomic/atomic_swap.S b/common/lib/libc/arch/m68k/atomic/atomic_swap.S new file mode 100644 index 000000000..53389aeed --- /dev/null +++ b/common/lib/libc/arch/m68k/atomic/atomic_swap.S @@ -0,0 +1,51 @@ +/* $NetBSD: atomic_swap.S,v 1.4 2008/05/25 15:56:11 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_swap_32) + movl %sp@(4), %a0 +1: movl %a0@, %d0 + movl %sp@(8), %d1 + casl %d0, %d1, %a0@ + bne 1b + /* %d0 now contains the old value */ + movl %d0, %a0 /* pointers return also in %a0 */ + rts +ATOMIC_OP_ALIAS(atomic_swap_32,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_ulong,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_ptr,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32) diff --git a/common/lib/libc/arch/m68k/gen/bswap16.S b/common/lib/libc/arch/m68k/gen/bswap16.S new file mode 100644 index 000000000..f7c84718a --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/bswap16.S @@ -0,0 +1,38 @@ +/* $NetBSD: bswap16.S,v 1.2 2007/09/19 20:31:34 he Exp $ */ + +/* + * Copyright (C) 1996 Scott Reynolds. 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 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. + */ + +#include + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap16)) +#else /* defined(_KERNEL) || defined(_STANDALONE) */ +_ENTRY(_C_LABEL(__bswap16)) +#endif /* defined(_KERNEL) || defined(_STANDALONE) */ + movl %sp@(4),%d0 + rolw #8,%d0 + rts diff --git a/common/lib/libc/arch/m68k/gen/bswap32.S b/common/lib/libc/arch/m68k/gen/bswap32.S new file mode 100644 index 000000000..f8b0b9206 --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/bswap32.S @@ -0,0 +1,40 @@ +/* $NetBSD: bswap32.S,v 1.2 2007/09/19 20:31:34 he Exp $ */ + +/* + * Copyright (C) 1996 Scott Reynolds. 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 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. + */ + +#include + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap32)) +#else /* defined(_KERNEL) || defined(_STANDALONE) */ +_ENTRY(_C_LABEL(__bswap32)) +#endif /* defined(_KERNEL) || defined(_STANDALONE) */ + movl %sp@(4),%d0 + rolw #8,%d0 + swap %d0 + rolw #8,%d0 + rts diff --git a/common/lib/libc/arch/m68k/gen/bswap64.S b/common/lib/libc/arch/m68k/gen/bswap64.S new file mode 100644 index 000000000..ebce236b0 --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/bswap64.S @@ -0,0 +1,40 @@ +/* $NetBSD: bswap64.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/* + * Copyright (C) 1996 Scott Reynolds. 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 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. + */ + +#include + +ENTRY(bswap64) + movl %sp@(4),%d1 + movl %sp@(8),%d0 + rolw #8,%d1 + rolw #8,%d0 + swap %d1 + swap %d0 + rolw #8,%d0 + rolw #8,%d1 + rts diff --git a/common/lib/libc/arch/m68k/gen/divsi3.S b/common/lib/libc/arch/m68k/gen/divsi3.S new file mode 100644 index 000000000..a49bec9fb --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/divsi3.S @@ -0,0 +1,67 @@ +/* $NetBSD: divsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)divsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: divsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* int / int */ +ENTRY(__divsi3) +#ifndef __mc68010__ + movel %sp@(4),%d0 + divsl %sp@(8),%d0 + rts +#else +| NB: this requires that __udivsi3 preserve %a0: + movel %sp@(4), %d1 | load the dividend + bpl 1f + negl %sp@(4) | store abs(dividend) +1: movel %sp@(8), %d0 | load the divisor + bpl 2f + negl %sp@(8) | store abs(divisor) +2: eorl %d1, %d0 + bpl 3f | branch if sgn(divisor) == sgn(dividend) + movel %sp@+, %a0 | pop return address + pea %pc@(Lret) | push our return address +3: jmp _C_LABEL(__udivsi3) +Lret: negl %d0 | negate quotient + jmp %a0@ +#endif /* __mc68010__ */ diff --git a/common/lib/libc/arch/m68k/gen/modsi3.S b/common/lib/libc/arch/m68k/gen/modsi3.S new file mode 100644 index 000000000..4808e8000 --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/modsi3.S @@ -0,0 +1,68 @@ +/* $NetBSD: modsi3.S,v 1.3 2006/01/13 16:07:59 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)modsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: modsi3.S,v 1.3 2006/01/13 16:07:59 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* int % int */ +ENTRY(__modsi3) +#ifndef __mc68010__ + movel %sp@(4),%d1 + divsll %sp@(8),%d0:%d1 + rts +#else +| NB: this requires that __udivsi3 preserve %a0 and return +| the modulus in %d1: + movel %sp@+, %a0 | pop return address + movel %sp@(4), %d1 | load the divisor + bpl 1f + negl %sp@(4) | store abs(divisor) +1: movel %sp@, %d0 | load the dividend + pea %pc@(Lret) | push our return address + bpl 2f + negl %sp@(4) | store abs(dividend) + subql #2, %sp@ | adjust return address +2: jmp _C_LABEL(__udivsi3) + negl %d1 | negate modulus +Lret: movl %d1, %d0 | move modulus into %d0 + jmp %a0@ +#endif /* __mc68010__ */ diff --git a/common/lib/libc/arch/m68k/gen/mulsi3.S b/common/lib/libc/arch/m68k/gen/mulsi3.S new file mode 100644 index 000000000..2a34faf19 --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/mulsi3.S @@ -0,0 +1,68 @@ +/* $NetBSD: mulsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)mulsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: mulsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* int * int */ +ENTRY(__mulsi3) +#ifndef __mc68010__ + movel %sp@(4),%d0 + mulsl %sp@(8),%d0 +#else +| NB: this requires that __udivsi3 preserve %a0 and return +| the modulus in %d1: + movew %sp@(6), %d0 + movel %d0, %a0 | save B + muluw %sp@(8), %d0 | %d0 holds B * C + movew %sp@(10), %d1 + movel %d1, %a1 | save D + muluw %sp@(4), %d1 | %d1 holds A * D + addw %d1, %d0 | %d0 holds (B * C) + (A * D) + swap %d0 + clrw %d0 | %d0 holds ((B * C) + (A * D)) << 16 + exg %a0, %d0 | restore B + movel %a1, %d1 | restore D + muluw %d1, %d0 | %d0 holds B * D + addl %a0, %d0 | final result +#endif /* __mc68010__ */ + rts diff --git a/common/lib/libc/arch/m68k/gen/udivsi3.S b/common/lib/libc/arch/m68k/gen/udivsi3.S new file mode 100644 index 000000000..d2da961a3 --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/udivsi3.S @@ -0,0 +1,126 @@ +/* $NetBSD: udivsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)udivsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: udivsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* unsigned / unsigned */ +ENTRY(__udivsi3) +#ifndef __mc68010__ + movel %sp@(4),%d0 + divul %sp@(8),%d0 + rts +#else + movel %d2, %sp@- | save %d2 + movel %sp@(12), %d0 | load divisor + movel %sp@(8), %d1 | load dividend + +| first, we divide the divisor and dividend by two until +| the divisor fits into 16 bits: +1: cmpil #0x10000, %d0 + bcs 2f + lsrl #1, %d0 + lsrl #1, %d1 + bra 1b +2: + +| now we can do the divide. to avoid overflow, we have to +| do the divide in two parts, high and low, and add the +| results together: + movew %d1, %d2 | save low(dividend) + clrw %d1 + swap %d1 | %d1 = dividend >> 16 + divuw %d0, %d1 | do the high divide + moveal %d1, %a1 | save high divide result + movew %d2, %d1 | concat(remainder, low(dividend)) + divuw %d0, %d1 | do the low divide + movel %a1, %d0 | recover high divide result + swap %d0 + clrw %d0 | %d0 = finished high divide result + andil #0xffff, %d1 | %d1 = finished low divide result + addl %d1, %d0 | %d0 = quotient guess + +| the quotient we have so far is only a guess. the divide we +| did above was really the divide of some dividendB by some +| divisorB, where the following hold: +| +| (dividend - divisor) <= dividendB <= dividend +| (divisor / 2) < divisorB <= divisor +| +| so our guess quotient cannot be less than our real desired +| quotient. however, it might be one too big. +| +| to adjust this quotient, we multiply it by the original +| divisor and subtract the result from the original dividend. +| if the result is nonnegative, our guessed quotient was +| correct, and the subtraction result is our remainder. +| if the result is negative, our guessed quotient was one +| too big, and the subtraction result plus the original +| divisor is our remainder. +| +| as in mulsi3, we have to do the multiply in stages to avoid +| overflow: + + movel %sp@(12), %d2 | load divisor + swap %d2 + movel %d0, %d1 + muluw %d2, %d1 | high(divisor) * low(guess) + moveal %d1, %a1 | save high(divisor) * low(guess) + swap %d2 + movel %d0, %d1 + swap %d1 + muluw %d2, %d1 | low(divisor) * high(guess) + addl %a1, %d1 + swap %d1 + clrw %d1 | %d1 = finished high multiply result + moveal %d2, %a1 | save original divisor + muluw %d0, %d2 | low(guess) * low(divisor) + addl %d1, %d2 | %d2 = guess * divisor + + movel %sp@(8), %d1 | load original dividend + subl %d2, %d1 | subtract + bcc 3f + subql #1, %d0 | adjust quotient + addl %a1, %d1 | adjust remainder +3: movel %sp@+, %d2 | restore %d2 + rts +#endif /* __mc68010__ */ diff --git a/common/lib/libc/arch/m68k/gen/umodsi3.S b/common/lib/libc/arch/m68k/gen/umodsi3.S new file mode 100644 index 000000000..c07b761b6 --- /dev/null +++ b/common/lib/libc/arch/m68k/gen/umodsi3.S @@ -0,0 +1,59 @@ +/* $NetBSD: umodsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)umodsi3.s 5.1 (Berkeley) 6/7/90")( +#else + RCSID("$NetBSD: umodsi3.S,v 1.2 2006/01/05 22:33:34 he Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* unsigned % unsigned */ +ENTRY(__umodsi3) +#ifndef __mc68010__ + movel %sp@(4),%d1 + divull %sp@(8),%d0:%d1 + rts +#else +| NB: this requires that __udivsi3 preserve the %a0 +| register, and that it returns the modulus in %d1: + movel %sp@+, %a0 | pop the return address + jsr _C_LABEL(__udivsi3) + movel %d1, %d0 | move the modulus into %d0 + jmp %a0@ | return +#endif /* __mc68010__ */ diff --git a/common/lib/libc/arch/m68k/net/htonl.S b/common/lib/libc/arch/m68k/net/htonl.S new file mode 100644 index 000000000..05c7ed85e --- /dev/null +++ b/common/lib/libc/arch/m68k/net/htonl.S @@ -0,0 +1,50 @@ +/* $NetBSD: htonl.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)htonl.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: htonl.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* netorder = htonl(hostorder) */ + +ENTRY(htonl) + movl %sp@(4),%d0 + rts diff --git a/common/lib/libc/arch/m68k/net/htons.S b/common/lib/libc/arch/m68k/net/htons.S new file mode 100644 index 000000000..306aee3c9 --- /dev/null +++ b/common/lib/libc/arch/m68k/net/htons.S @@ -0,0 +1,51 @@ +/* $NetBSD: htons.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)htons.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: htons.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* hostorder = htons(netorder) */ + +ENTRY(htons) + clrl %d0 + movw %sp@(6),%d0 + rts diff --git a/common/lib/libc/arch/m68k/net/ntohl.S b/common/lib/libc/arch/m68k/net/ntohl.S new file mode 100644 index 000000000..ed2a5fa20 --- /dev/null +++ b/common/lib/libc/arch/m68k/net/ntohl.S @@ -0,0 +1,50 @@ +/* $NetBSD: ntohl.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ntohl.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: ntohl.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* hostorder = ntohl(netorder) */ + +ENTRY(ntohl) + movl %sp@(4),%d0 + rts diff --git a/common/lib/libc/arch/m68k/net/ntohs.S b/common/lib/libc/arch/m68k/net/ntohs.S new file mode 100644 index 000000000..38438d67f --- /dev/null +++ b/common/lib/libc/arch/m68k/net/ntohs.S @@ -0,0 +1,51 @@ +/* $NetBSD: ntohs.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ntohs.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: ntohs.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* hostorder = ntohs(netorder) */ + +ENTRY(ntohs) + clrl %d0 + movw %sp@(6),%d0 + rts diff --git a/common/lib/libc/arch/m68k/string/bcmp.S b/common/lib/libc/arch/m68k/string/bcmp.S new file mode 100644 index 000000000..80a254cbf --- /dev/null +++ b/common/lib/libc/arch/m68k/string/bcmp.S @@ -0,0 +1,167 @@ +/* $NetBSD: bcmp.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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. + */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)bcmp.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: bcmp.S,v 1.2 2008/04/28 20:22:52 martin Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(bcmp) + movl %sp@(4),%a0 | string 1 + movl %sp@(8),%a1 | string 2 + movl %sp@(12),%d1 | length + + /* + * It isn't worth the overhead of aligning to {long}word boundries + * if the string is too short. + */ + cmpl #8,%d1 + jlt Lbcbyte + +#ifdef __mc68010__ + /* + * The 68010 cannot access a word or long on an odd boundary, + * period. If the source and the destination addresses aren't + * of the same evenness, we're forced to do a bytewise compare. + */ + movl %a0,%d0 + addl %a1,%d0 + btst #0,%d0 + jne Lbcbyte +#endif /* __mc68010__ */ + + /* word align */ + movl %a0,%d0 + btst #0,%d0 + jeq Lbcalgndw + cmpmb %a0@+,%a1@+ + jne Lbcnoteq + subql #1,%d1 +Lbcalgndw: + /* long word align */ + btst #1,%d0 + jeq Lbcalgndl + cmpmw %a0@+,%a1@+ + jne Lbcnoteq + subql #2,%d1 +Lbcalgndl: + /* compare by 8 longwords */ + movl %d1,%d0 + lsrl #5,%d0 | cnt = len / 32 + jeq Lbclong | if (cnt) + andl #31,%d1 | len %= 32 + subql #1,%d0 | set up for dbf +Lbc32loop: + cmpml %a0@+,%a1@+ | compare 8 longwords + jne Lbcnoteq | not equal, return non-zero + cmpml %a0@+,%a1@+ + jne Lbcnoteq + cmpml %a0@+,%a1@+ + jne Lbcnoteq + cmpml %a0@+,%a1@+ + jne Lbcnoteq + cmpml %a0@+,%a1@+ + jne Lbcnoteq + cmpml %a0@+,%a1@+ + jne Lbcnoteq + cmpml %a0@+,%a1@+ + jne Lbcnoteq + cmpml %a0@+,%a1@+ + jne Lbcnoteq + dbf %d0,Lbc32loop | till done + clrw %d0 + subql #1,%d0 + jcc Lbc32loop + +Lbclong: + /* compare by longwords */ + movl %d1,%d0 + lsrl #2,%d0 | cnt = len / 4 + jeq Lbcbyte | if (cnt) + subql #1,%d0 | set up for dbf +Lbclloop: + cmpml %a0@+,%a1@+ | compare a longword + jne Lbcnoteq | not equal, return non-zero + dbf %d0,Lbclloop | till done + andl #3,%d1 | len %= 4 + jeq Lbcdone + + subql #1,%d1 | set up for dbf +Lbcbloop: + cmpmb %a0@+,%a1@+ | compare a byte + jne Lbcnoteq | not equal, return non-zero +Lbcbyte: + dbf %d1,Lbcbloop +Lbcdone: + movql #0,%d0 + rts + +Lbcnoteq: + movql #1,%d0 + rts diff --git a/common/lib/libc/arch/m68k/string/bcopy.S b/common/lib/libc/arch/m68k/string/bcopy.S new file mode 100644 index 000000000..308294894 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/bcopy.S @@ -0,0 +1,256 @@ +/* $NetBSD: bcopy.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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. + */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)bcopy.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: bcopy.S,v 1.2 2008/04/28 20:22:52 martin Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + + +#ifdef MEMCOPY +ENTRY(memcpy) +#else +#ifdef MEMMOVE +ENTRY(memmove) +#else +ENTRY(bcopy) +#endif +#endif +#if defined(MEMCOPY) || defined(MEMMOVE) + movl %sp@(4),%a1 | dest address + movl %sp@(8),%a0 | src address +#else + movl %sp@(4),%a0 | src address + movl %sp@(8),%a1 | dest address +#endif + movl %sp@(12),%d1 | count + + cmpl %a1,%a0 | src after dest? + jlt Lbcback | yes, must copy backwards + + /* + * It isn't worth the overhead of aligning to {long}word boundries + * if the string is too short. + */ + cmpl #8,%d1 + jlt Lbcfbyte + +#ifdef __mc68010__ + /* + * The 68010 cannot access a word or long on an odd boundary, + * period. If the source and the destination addresses aren't + * of the same evenness, we're forced to do a bytewise copy. + */ + movl %a0,%d0 + addl %a1,%d0 + btst #0,%d0 + jne Lbcfbyte +#endif /* __mc68010__ */ + + /* word align */ + movl %a1,%d0 + btst #0,%d0 | if (dst & 1) + jeq Lbcfalgndw | + movb %a0@+,%a1@+ | *(char *)dst++ = *(char *) src++ + subql #1,%d1 | len-- +Lbcfalgndw: + /* long word align */ + btst #1,%d0 | if (dst & 2) + jeq Lbcfalgndl + movw %a0@+,%a1@+ | *(short *)dst++ = *(short *) dst++ + subql #2,%d1 | len -= 2 +Lbcfalgndl: + /* copy by 8 longwords */ + movel %d1,%d0 + lsrl #5,%d0 | cnt = len / 32 + jeq Lbcflong | if (cnt) + andl #31,%d1 | len %= 32 + subql #1,%d0 | set up for dbf +Lbcf32loop: + movl %a0@+,%a1@+ | copy 8 long words + movl %a0@+,%a1@+ + movl %a0@+,%a1@+ + movl %a0@+,%a1@+ + movl %a0@+,%a1@+ + movl %a0@+,%a1@+ + movl %a0@+,%a1@+ + movl %a0@+,%a1@+ + dbf %d0,Lbcf32loop | till done + clrw %d0 + subql #1,%d0 + jcc Lbcf32loop + +Lbcflong: + /* copy by longwords */ + movel %d1,%d0 + lsrl #2,%d0 | cnt = len / 4 + jeq Lbcfbyte | if (cnt) + subql #1,%d0 | set up for dbf +Lbcflloop: + movl %a0@+,%a1@+ | copy longwords + dbf %d0,Lbcflloop | til done + andl #3,%d1 | len %= 4 + jeq Lbcdone + + subql #1,%d1 | set up for dbf +Lbcfbloop: + movb %a0@+,%a1@+ | copy bytes +Lbcfbyte: + dbf %d1,Lbcfbloop | till done +Lbcdone: +#if defined(MEMCOPY) || defined(MEMMOVE) + movl %sp@(4),%d0 | dest address +#if defined(__SVR4_ABI__) + moveal %d0,%a0 +#endif +#endif + rts + + +Lbcback: + addl %d1,%a0 | src pointer to end + addl %d1,%a1 | dest pointer to end + + /* + * It isn't worth the overhead of aligning to {long}word boundries + * if the string is too short. + */ + cmpl #8,%d1 + jlt Lbcbbyte + +#ifdef __mc68010__ + /* + * The 68010 cannot access a word or long on an odd boundary, + * period. If the source and the destination addresses aren't + * of the same evenness, we're forced to do a bytewise copy. + */ + movl %a0,%d0 + addl %a1,%d0 + btst #0,%d0 + jne Lbcbbyte +#endif /* __mc68010__ */ + + /* word align */ + movl %a1,%d0 + btst #0,%d0 | if (dst & 1) + jeq Lbcbalgndw | + movb %a0@-,%a1@- | *(char *)dst-- = *(char *) src-- + subql #1,%d1 | len-- +Lbcbalgndw: + /* long word align */ + btst #1,%d0 | if (dst & 2) + jeq Lbcbalgndl + movw %a0@-,%a1@- | *(short *)dst-- = *(short *) dst-- + subql #2,%d1 | len -= 2 +Lbcbalgndl: + /* copy by 8 longwords */ + movel %d1,%d0 + lsrl #5,%d0 | cnt = len / 32 + jeq Lbcblong | if (cnt) + andl #31,%d1 | len %= 32 + subql #1,%d0 | set up for dbf +Lbcb32loop: + movl %a0@-,%a1@- | copy 8 long words + movl %a0@-,%a1@- + movl %a0@-,%a1@- + movl %a0@-,%a1@- + movl %a0@-,%a1@- + movl %a0@-,%a1@- + movl %a0@-,%a1@- + movl %a0@-,%a1@- + dbf %d0,Lbcb32loop | till done + clrw %d0 + subql #1,%d0 + jcc Lbcb32loop + +Lbcblong: + /* copy by longwords */ + movel %d1,%d0 + lsrl #2,%d0 | cnt = len / 4 + jeq Lbcbbyte | if (cnt) + subql #1,%d0 | set up for dbf +Lbcblloop: + movl %a0@-,%a1@- | copy longwords + dbf %d0,Lbcblloop | til done + andl #3,%d1 | len %= 4 + jeq Lbcdone + + subql #1,%d1 | set up for dbf +Lbcbbloop: + movb %a0@-,%a1@- | copy bytes +Lbcbbyte: + dbf %d1,Lbcbbloop | till done + +#if defined(MEMCOPY) || defined(MEMMOVE) + movl %sp@(4),%d0 | dest address +#if defined(__SVR4_ABI__) + moveal %d0,%a0 +#endif +#endif + rts diff --git a/common/lib/libc/arch/m68k/string/bzero.S b/common/lib/libc/arch/m68k/string/bzero.S new file mode 100644 index 000000000..4d04ddfd1 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/bzero.S @@ -0,0 +1,141 @@ +/* $NetBSD: bzero.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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. + */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)bzero.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: bzero.S,v 1.2 2008/04/28 20:22:52 martin Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(bzero) + movl %d2,%sp@- + movl %sp@(8),%a0 | destination + movl %sp@(12),%d1 | count + + movql #0,%d2 + + /* + * It isn't worth the overhead of aligning to {long}word boundries + * if the string is too short. + */ + cmpl #8,%d1 + jlt Lbzbyte + + /* word align */ + movl %a0,%d0 + btst #0,%d0 | if (dst & 1) + jeq Lbzalgndw | + movb %d2,%a0@+ | *(char *)dst++ = 0 + subql #1,%d1 | len-- +Lbzalgndw: + /* long word align */ + btst #1,%d0 | if (dst & 2) + jeq Lbzalgndl | + movw %d2,%a0@+ | *(short *)dst++ = 0 + subql #2,%d1 | len -= 2 +Lbzalgndl: + /* zero by 8 longwords */ + movel %d1,%d0 + lsrl #5,%d0 | cnt = len / 32 + jeq Lbzlong | if (cnt) + andl #31,%d1 | len %= 32 + subql #1,%d0 | set up for dbf +Lbz32loop: + movl %d2,%a0@+ | zero 8 long words + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + dbf %d0,Lbz32loop | till done + clrw %d0 + subql #1,%d0 + jcc Lbz32loop + +Lbzlong: + /* copy by longwords */ + movel %d1,%d0 + lsrl #2,%d0 | cnt = len / 4 + jeq Lbzbyte | if (cnt) + subql #1,%d0 | set up for dbf +Lbzlloop: + movl %d2,%a0@+ | clear longwords + dbf %d0,Lbzlloop | till done + andl #3,%d1 | len %= 4 + jeq Lbzdone + + subql #1,%d1 | set up for dbf +Lbzbloop: + movb %d2,%a0@+ | zero bytes +Lbzbyte: + dbf %d1,Lbzbloop | till done +Lbzdone: + movl %sp@+,%d2 + rts diff --git a/common/lib/libc/arch/m68k/string/ffs.S b/common/lib/libc/arch/m68k/string/ffs.S new file mode 100644 index 000000000..f29ff4776 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/ffs.S @@ -0,0 +1,74 @@ +/* $NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ffs.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* bit = ffs(value) */ + +#ifndef __mc68010__ + +ENTRY(ffs) + movl %sp@(4),%d0 + movl %d0,%d1 + negl %d0 + andl %d0,%d1 + movql #32,%d0 + bfffo %d1{#0:#32},%d1 + subl %d1,%d0 + rts + +#else /* __mc68010__ */ + +ENTRY(ffs) + movl %sp@(4),%d0 + jeq L2 + movql #31,%d1 +L1: + lsrl #1,%d0 + dbcs %d1,L1 + movql #32,%d0 + subl %d1,%d0 +L2: + rts + +#endif /* __mc68010__ */ diff --git a/common/lib/libc/arch/m68k/string/memcmp.S b/common/lib/libc/arch/m68k/string/memcmp.S new file mode 100644 index 000000000..760a1ffe9 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/memcmp.S @@ -0,0 +1,100 @@ +/* $NetBSD: memcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)bcmp.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: memcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* memcmp(s1, s2, n) */ + +/* + * This is probably not the best we can do, but it is still 2-10 times + * faster than the C version in the portable gen directory. + * + * Things that might help: + * - longword align when possible (only on the 68020) + * - use nested DBcc instructions or use one and limit size to 64K + */ +ENTRY(memcmp) + movl %sp@(4),%a0 | string 1 + movl %sp@(8),%a1 | string 2 + movl %sp@(12),%d0 | length + jeq bcdone | if zero, nothing to do + movl %a0,%d1 + btst #0,%d1 | string 1 address odd? + jeq bceven | no, skip alignment + cmpmb %a0@+,%a1@+ | yes, compare a byte + jne bcnoteq | not equal, return non-zero + subql #1,%d0 | adjust count + jeq bcdone | count 0, reutrn zero +bceven: + movl %a1,%d1 + btst #0,%d1 | string 2 address odd? + jne bcbloop | yes, no hope for alignment, compare bytes + movl %d0,%d1 | no, both even + lsrl #2,%d1 | convert count to longword count + jeq bcbloop | count 0, skip longword loop +bclloop: + cmpml %a0@+,%a1@+ | compare a longword + jne bcnoteql | not equal, return non-zero + subql #1,%d1 | adjust count + jne bclloop | still more, keep comparing + andl #3,%d0 | what remains + jeq bcdone | nothing, all done +bcbloop: + cmpmb %a0@+,%a1@+ | compare a byte + jne bcnoteq | not equal, return non-zero + subql #1,%d0 | adjust count + jne bcbloop | still more, keep going + rts +bcnoteql: + subql #4,%a0 + subql #4,%a1 + movl #4,%d0 + jra bcbloop +bcnoteq: + clrl %d0 + clrl %d1 + movb %a0@-,%d0 + movb %a1@-,%d1 + subl %d1,%d0 +bcdone: + rts diff --git a/common/lib/libc/arch/m68k/string/memcpy.S b/common/lib/libc/arch/m68k/string/memcpy.S new file mode 100644 index 000000000..11245b492 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/memcpy.S @@ -0,0 +1,4 @@ +/* $NetBSD: memcpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +#define MEMCOPY +#include "bcopy.S" diff --git a/common/lib/libc/arch/m68k/string/memmove.S b/common/lib/libc/arch/m68k/string/memmove.S new file mode 100644 index 000000000..d9d518633 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/memmove.S @@ -0,0 +1,4 @@ +/* $NetBSD: memmove.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +#define MEMMOVE +#include "bcopy.S" diff --git a/common/lib/libc/arch/m68k/string/memset.S b/common/lib/libc/arch/m68k/string/memset.S new file mode 100644 index 000000000..ff5d5b8f6 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/memset.S @@ -0,0 +1,162 @@ +/* $NetBSD: memset.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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. + */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)bzero.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: memset.S,v 1.2 2008/04/28 20:22:52 martin Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(memset) + movl %d2,%sp@- + movl %sp@(8),%a0 | destination + movl %sp@(16),%d1 | count + movb %sp@(15),%d2 | character + + /* + * It isn't worth the overhead of aligning to {long}word boundries + * if the string is too short. + */ + cmpl #15,%d1 + jlt Lbzbyte + + clrl %d0 | replicate byte to fill longword + movb %d2,%d0 + movl %d0,%d2 + lsll #8,%d0 + orl %d0,%d2 + lsll #8,%d0 + orl %d0,%d2 + lsll #8,%d0 + orl %d0,%d2 + + /* word align */ + movl %a0,%d0 + btst #0,%d0 | if (dst & 1) + jeq Lbzalgndw | + movb %d2,%a0@+ | *(char *)dst++ = X + subql #1,%d1 | len-- + addql #1,%d0 +Lbzalgndw: +#ifndef __mc68010__ + /* long word align */ + btst #1,%d0 | if (dst & 2) + jeq Lbzalgndl | + movw %d2,%a0@+ | *(short *)dst++ = X + subql #2,%d1 | len -= 2 +Lbzalgndl: + /* set by 8 longwords */ + movel %d1,%d0 + lsrl #5,%d0 | cnt = len / 32 + jeq Lbzlong | if (cnt) + andl #31,%d1 | len %= 32 + subql #1,%d0 | set up for dbf +Lbz32loop: + movl %d2,%a0@+ | set 8 long words + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + movl %d2,%a0@+ + dbf %d0,Lbz32loop | till done + clrw %d0 + subql #1,%d0 + jcc Lbz32loop +#endif /* !__mc68010__ */ + +Lbzlong: + /* set by longwords */ + movel %d1,%d0 + lsrl #2,%d0 | cnt = len / 4 + jeq Lbzbyte | if (cnt) + subql #1,%d0 | set up for dbf +Lbzlloop: + movl %d2,%a0@+ | clear longwords + dbf %d0,Lbzlloop | till done +#ifdef __mc68010__ + clrw %d0 + subql #1,%d0 + jcc Lbzlloop +#endif /* __mc68010__ */ + andl #3,%d1 | len %= 4 + jeq Lbzdone + + subql #1,%d1 | set up for dbf +Lbzbloop: + movb %d2,%a0@+ | set bytes +Lbzbyte: + dbf %d1,Lbzbloop | till done +Lbzdone: + movl %sp@(8),%d0 | return destination +#ifdef __SVR4_ABI__ + moveal %d0,%a0 +#endif + movl %sp@+,%d2 + rts diff --git a/common/lib/libc/arch/m68k/string/strcat.S b/common/lib/libc/arch/m68k/string/strcat.S new file mode 100644 index 000000000..0f144d178 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strcat.S @@ -0,0 +1,60 @@ +/* $NetBSD: strcat.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)strcpy.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: strcat.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strcat) + movl %sp@(8),%a0 | a0 = fromaddr + movl %sp@(4),%d0 | return value is toaddr + movl %d0,%a1 | a1 = toaddr +Lslloop: + tstb %a1@+ | null? + jne Lslloop | no, keep going + subql #1,%a1 +Lscloop: + movb %a0@+,%a1@+ | copy a byte + jne Lscloop | copied non-null, keep going +#ifdef __SVR4_ABI__ + moveal %d0,%a0 +#endif + rts diff --git a/common/lib/libc/arch/m68k/string/strchr.S b/common/lib/libc/arch/m68k/string/strchr.S new file mode 100644 index 000000000..10412c527 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strchr.S @@ -0,0 +1,63 @@ +/* $NetBSD: strchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)index.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: strchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strchr) + movl %sp@(4),%a0 | string + movb %sp@(11),%d0 | char to look for +ixloop: + cmpb %a0@,%d0 | found our char? + jeq ixfound | yes, break out + tstb %a0@+ | null? + jne ixloop | no, keep going + moveq #0,%d0 | not found, return null +#ifdef __SVR4_ABI__ + moveal %d0,%a0 +#endif + rts +ixfound: + movl %a0,%d0 | found, return pointer + rts + +STRONG_ALIAS(index,strchr) diff --git a/common/lib/libc/arch/m68k/string/strcmp.S b/common/lib/libc/arch/m68k/string/strcmp.S new file mode 100644 index 000000000..04bdbfc43 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strcmp.S @@ -0,0 +1,71 @@ +/* $NetBSD: strcmp.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Hiroshi Horimoto and + * by J.T. Conklin . + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: strcmp.S,v 1.2 2008/04/28 20:22:52 martin Exp $") +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strcmp) + movl %sp@(4),%a0 + movl %sp@(8),%a1 +L1: /* unrolled by 4 for 680[23]0's */ + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jne L3 + + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jne L3 + + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jne L3 + + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jeq L1 + +L3: scs %d0 + EXTBL(%d0) + movb %d1,%d0 + rts + +L2: movq #0,%d0 + movb %a1@,%d0 + negl %d0 + rts diff --git a/common/lib/libc/arch/m68k/string/strcpy.S b/common/lib/libc/arch/m68k/string/strcpy.S new file mode 100644 index 000000000..2aa681fb6 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strcpy.S @@ -0,0 +1,56 @@ +/* $NetBSD: strcpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)strcpy.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: strcpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strcpy) + movl %sp@(8),%a0 | a0 = fromaddr + movl %sp@(4),%d0 | return value is toaddr + movl %d0,%a1 | a1 = toaddr +Lscloop: + movb %a0@+,%a1@+ | copy a byte + jne Lscloop | copied non-null, keep going +#ifdef __SVR4_ABI__ + moveal %d0,%a0 +#endif + rts diff --git a/common/lib/libc/arch/m68k/string/strlen.S b/common/lib/libc/arch/m68k/string/strlen.S new file mode 100644 index 000000000..9a9a5894c --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strlen.S @@ -0,0 +1,54 @@ +/* $NetBSD: strlen.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)strlen.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: strlen.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strlen) + movl %sp@(4),%a0 | string + movl %a0,%d0 + notl %d0 +Lslloop: + tstb %a0@+ | null? + jne Lslloop | no, keep going + addl %a0,%d0 + rts diff --git a/common/lib/libc/arch/m68k/string/strncmp.S b/common/lib/libc/arch/m68k/string/strncmp.S new file mode 100644 index 000000000..7bee4c6a6 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strncmp.S @@ -0,0 +1,78 @@ +/* $NetBSD: strncmp.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Hiroshi Horimoto and + * by J.T. Conklin . + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: strncmp.S,v 1.2 2008/04/28 20:22:52 martin Exp $") +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strncmp) + movl %sp@(12),%d0 + jeq L4 + movl %sp@(4),%a0 + movl %sp@(8),%a1 +L1: /* unroll by 4 for m680[23]0's */ + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jne L3 + subql #1,%d0 + jeq L4 + + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jne L3 + subql #1,%d0 + jeq L4 + + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jne L3 + subql #1,%d0 + jeq L4 + + movb %a0@+,%d1 + jeq L2 + subb %a1@+,%d1 + jne L3 + subql #1,%d0 + jne L1 +L4: rts + +L2: subb %a1@,%d1 +L3: scs %d0 + EXTBL(%d0) + movb %d1,%d0 + rts diff --git a/common/lib/libc/arch/m68k/string/strncpy.S b/common/lib/libc/arch/m68k/string/strncpy.S new file mode 100644 index 000000000..d08c95b7f --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strncpy.S @@ -0,0 +1,73 @@ +/* $NetBSD: strncpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)strncpy.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: strncpy.S,v 1.1 2005/12/20 19:28:49 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strncpy) + movl %sp@(4),%d0 | return value is toaddr + movl %sp@(12),%d1 | count + jeq Lscdone | nothing to do + movl %sp@(8),%a0 | a0 = fromaddr + movl %d0,%a1 | a1 = toaddr + subql #1,%d1 +Lscloop: + movb %a0@+,%a1@+ | copy a byte + dbeq %d1,Lscloop | loop through low word of d1 + jeq Lscpadding | copied null, padding if necessary + clrw %d1 | clear low word of d1 + subql #1,%d1 | adjust count for long copies + jcc Lscloop | more room, keep going +Lscdone: +#ifdef __SVR4_ABI__ + moveal %d0,%a0 +#endif + rts + +Lscploop: + clrb %a1@+ | clear a byte + dbra %d1,Lscploop | keep going + clrw %d1 +Lscpadding: + subql #1,%d1 + jcc Lscploop + jra Lscdone diff --git a/common/lib/libc/arch/m68k/string/strrchr.S b/common/lib/libc/arch/m68k/string/strrchr.S new file mode 100644 index 000000000..c9b5f6be1 --- /dev/null +++ b/common/lib/libc/arch/m68k/string/strrchr.S @@ -0,0 +1,60 @@ +/* $NetBSD: strrchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)rindex.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: strrchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strrchr) + movl %sp@(4),%a1 | string + movb %sp@(11),%d0 | char to look for + subl %a0,%a0 | clear rindex pointer +rixloop: + cmpb %a1@,%d0 | found our char? + jne rixnope | no, check for null + movl %a1,%a0 | yes, remember location +rixnope: + tstb %a1@+ | null? + jne rixloop | no, keep going + movl %a0,%d0 | return value + rts + +STRONG_ALIAS(rindex,strrchr) diff --git a/common/lib/libc/arch/mips/atomic/Makefile.inc b/common/lib/libc/arch/mips/atomic/Makefile.inc new file mode 100644 index 000000000..aa0ef9027 --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/Makefile.inc @@ -0,0 +1,45 @@ +# $NetBSD: Makefile.inc,v 1.10 2009/12/14 00:38:59 matt Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +.if ${MACHINE_ARCH:Mmips64*} == "" +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c \ + atomic_and_32_cas.c atomic_and_32_nv_cas.c \ + atomic_dec_32_cas.c atomic_dec_32_nv_cas.c \ + atomic_inc_32_cas.c atomic_inc_32_nv_cas.c \ + atomic_or_32_cas.c atomic_or_32_nv_cas.c \ + atomic_swap_32_cas.c \ + atomic_add_64_cas.c atomic_add_64_nv_cas.c \ + atomic_and_64_cas.c atomic_and_64_nv_cas.c \ + atomic_dec_64_cas.c atomic_dec_64_nv_cas.c \ + atomic_inc_64_cas.c atomic_inc_64_nv_cas.c \ + atomic_or_64_cas.c atomic_or_64_nv_cas.c \ + atomic_swap_64_cas.c + +CPPFLAGS+= -D__HAVE_ASM_ATOMIC_CAS_UP + +.else +SRCS+= atomic_add.S atomic_dec.S atomic_inc.S +SRCS+= atomic_and.S atomic_or.S +SRCS+= atomic_swap.S +.endif + +SRCS+= membar_ops.S +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +.if ${MACHINE_ARCH:Mmips64*} == "" +SRCS+= atomic_init_testset.c atomic_cas_up.S +.else +SRCS+= atomic_cas.S atomic_init_cas.c +.endif + +.endif + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "rump") + +SRCS+= membar_ops.S + +.endif diff --git a/common/lib/libc/arch/mips/atomic/atomic_add.S b/common/lib/libc/arch/mips/atomic/atomic_add.S new file mode 100644 index 000000000..298ad243a --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_add.S @@ -0,0 +1,111 @@ +/* $NetBSD: atomic_add.S,v 1.2 2009/12/14 00:38:59 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + +RCSID("$NetBSD: atomic_add.S,v 1.2 2009/12/14 00:38:59 matt Exp $") + + .text + .set noat + .set noreorder + .set nomacro + +LEAF(_atomic_add_32) +1: INT_LL t0, 0(a0) + nop + INT_ADDU t0, a1 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_32, _atomic_add_32) + +LEAF(_atomic_add_32_nv) +1: INT_LL v0, 0(a0) + nop + INT_ADDU v0, a1 + move t0, v0 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_32_nv, _atomic_add_32_nv) + +#if !defined(__mips_o32) +LEAF(_atomic_add_64) +1: REG_LL t0, 0(a0) + nop + REG_ADDU t0, a1 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_add_64) +ATOMIC_OP_ALIAS(atomic_add_64, _atomic_add_64) + +LEAF(_atomic_add_64_nv) +1: REG_LL v0, 0(a0) + nop + REG_ADDU v0, a1 + move t0, v0 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_add_64_nv) +ATOMIC_OP_ALIAS(atomic_add_64_nv, _atomic_add_64_nv) +#endif + +#ifdef _LP64 +STRONG_ALIAS(_atomic_add_long, _atomic_add_64) +STRONG_ALIAS(_atomic_add_long_nv, _atomic_add_64_nv) +STRONG_ALIAS(_atomic_add_ptr, _atomic_add_64) +STRONG_ALIAS(_atomic_add_ptr_nv, _atomic_add_64_nv) +#else +STRONG_ALIAS(_atomic_add_long, _atomic_add_32) +STRONG_ALIAS(_atomic_add_long_nv, _atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_ptr, _atomic_add_32) +STRONG_ALIAS(_atomic_add_ptr_nv, _atomic_add_32_nv) +#endif +STRONG_ALIAS(_atomic_add_int, _atomic_add_32) +STRONG_ALIAS(_atomic_add_int_nv, _atomic_add_32_nv) + +ATOMIC_OP_ALIAS(atomic_add_int, _atomic_add_int) +ATOMIC_OP_ALIAS(atomic_add_int_nv, _atomic_add_int_nv) +ATOMIC_OP_ALIAS(atomic_add_ptr, _atomic_add_ptr) +ATOMIC_OP_ALIAS(atomic_add_ptr_nv, _atomic_add_ptr_nv) +ATOMIC_OP_ALIAS(atomic_add_long, _atomic_add_long) +ATOMIC_OP_ALIAS(atomic_add_long_nv, _atomic_add_long_nv) diff --git a/common/lib/libc/arch/mips/atomic/atomic_and.S b/common/lib/libc/arch/mips/atomic/atomic_and.S new file mode 100644 index 000000000..7d233d037 --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_and.S @@ -0,0 +1,105 @@ +/* $NetBSD: atomic_and.S,v 1.2 2009/12/14 00:38:59 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + +RCSID("$NetBSD: atomic_and.S,v 1.2 2009/12/14 00:38:59 matt Exp $") + + .text + .set noat + .set noreorder + .set nomacro + +LEAF(_atomic_and_32) +1: INT_LL t0, 0(a0) + nop + and t0, a1 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_32, _atomic_and_32) + +LEAF(_atomic_and_32_nv) +1: INT_LL v0, 0(a0) + nop + and v0, a1 + move t0, v0 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_32_nv, _atomic_and_32_nv) + +#if !defined(__mips_o32) +LEAF(_atomic_and_64) +1: REG_LL t0, 0(a0) + nop + and t0, a1 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_and_64) +ATOMIC_OP_ALIAS(atomic_and_64, _atomic_and_64) + +LEAF(_atomic_and_64_nv) +1: REG_LL v0, 0(a0) + nop + and v0, a1 + move t0, v0 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_and_64_nv) +ATOMIC_OP_ALIAS(atomic_and_64_nv, _atomic_and_64_nv) +#endif + +#ifdef _LP64 +STRONG_ALIAS(_atomic_and_ulong, _atomic_and_64) +STRONG_ALIAS(_atomic_and_ulong_nv, _atomic_and_64_nv) +#else +STRONG_ALIAS(_atomic_and_ulong, _atomic_and_32) +STRONG_ALIAS(_atomic_and_ulong_nv, _atomic_and_32_nv) +#endif +STRONG_ALIAS(_atomic_and_uint, _atomic_and_32) +STRONG_ALIAS(_atomic_and_uint_nv, _atomic_and_32_nv) + +ATOMIC_OP_ALIAS(atomic_and_uint, _atomic_and_uint) +ATOMIC_OP_ALIAS(atomic_and_uint_nv, _atomic_and_uint_nv) +ATOMIC_OP_ALIAS(atomic_and_ulong, _atomic_and_ulong) +ATOMIC_OP_ALIAS(atomic_and_ulong_nv, _atomic_and_ulong_nv) diff --git a/common/lib/libc/arch/mips/atomic/atomic_cas.S b/common/lib/libc/arch/mips/atomic/atomic_cas.S new file mode 100644 index 000000000..d1747f882 --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_cas.S @@ -0,0 +1,93 @@ +/* $NetBSD: atomic_cas.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + +RCSID("$NetBSD: atomic_cas.S,v 1.2 2009/12/14 00:39:00 matt Exp $") + + .text + .set noat + .set noreorder + .set nomacro + +LEAF(_atomic_cas_32) +1: INT_LL v0, 0(a0) + nop + bne v0, a1, 2f + nop + move t0, a2 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + move v0, a1 +2: + j ra + nop +END(_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_32, _atomic_cas_32) + +#if !defined(__mips_o32) +LEAF(_atomic_cas_64) +1: REG_LL v0, 0(a0) + nop + bne v0, a1, 2f + nop + move t0, a2 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + move v0, a1 +2: + j ra + nop +END(_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_64, _atomic_cas_64) +#endif + +#ifdef _LP64 +STRONG_ALIAS(_atomic_cas_ptr, _atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr_ni, _atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ulong, _atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ulong_ni, _atomic_cas_64) +#else +STRONG_ALIAS(_atomic_cas_ptr, _atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr_ni, _atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong, _atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong_ni, _atomic_cas_32) +#endif +STRONG_ALIAS(_atomic_cas_uint, _atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint_ni, _atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_ptr, _atomic_cas_ptr) +ATOMIC_OP_ALIAS(atomic_cas_ptr_ni, _atomic_cas_ptr_ni) +ATOMIC_OP_ALIAS(atomic_cas_uint, _atomic_cas_uint) +ATOMIC_OP_ALIAS(atomic_cas_uint_ni, _atomic_cas_uint_ni) +ATOMIC_OP_ALIAS(atomic_cas_ulong, _atomic_cas_ulong) +ATOMIC_OP_ALIAS(atomic_cas_ulong_ni, _atomic_cas_ulong_ni) diff --git a/common/lib/libc/arch/mips/atomic/atomic_cas_up.S b/common/lib/libc/arch/mips/atomic/atomic_cas_up.S new file mode 100644 index 000000000..6f3040d81 --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_cas_up.S @@ -0,0 +1,50 @@ +/* $NetBSD: atomic_cas_up.S,v 1.2 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + + .text + .set noat + .set noreorder + +LEAF(_atomic_cas_up) + .hidden _C_LABEL(_atomic_cas_up) +RAS_START_ASM_HIDDEN(_atomic_cas) + lw t0, (a0) + nop + bne t0, a1, 1f + nop + sw a2, (a0) +RAS_END_ASM_HIDDEN(_atomic_cas) + j ra + addu v0, zero, a1 +1: + j ra + addu v0, zero, t0 +END(_atomic_cas_up) diff --git a/common/lib/libc/arch/mips/atomic/atomic_dec.S b/common/lib/libc/arch/mips/atomic/atomic_dec.S new file mode 100644 index 000000000..f44850f54 --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_dec.S @@ -0,0 +1,111 @@ +/* $NetBSD: atomic_dec.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + +RCSID("$NetBSD: atomic_dec.S,v 1.2 2009/12/14 00:39:00 matt Exp $") + + .text + .set noat + .set noreorder + .set nomacro + +LEAF(_atomic_dec_32) +1: INT_LL t0, 0(a0) + nop + INT_ADDU t0, -1 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_32, _atomic_dec_32) + +LEAF(_atomic_dec_32_nv) +1: INT_LL v0, 0(a0) + nop + INT_ADDU v0, -1 + move t0, v0 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_32_nv, _atomic_dec_32_nv) + +#if !defined(__mips_o32) +LEAF(_atomic_dec_64) +1: REG_LL t0, 0(a0) + nop + REG_ADDU t0, -1 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_dec_64) +ATOMIC_OP_ALIAS(atomic_dec_64, _atomic_dec_64) + +LEAF(_atomic_dec_64_nv) +1: REG_LL v0, 0(a0) + nop + REG_ADDU v0, -1 + move t0, v0 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_dec_64_nv) +ATOMIC_OP_ALIAS(atomic_dec_64_nv, _atomic_dec_64_nv) +#endif + +#ifdef _LP64 +STRONG_ALIAS(_atomic_dec_ptr, _atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ptr_nv, _atomic_dec_64_nv) +STRONG_ALIAS(_atomic_dec_ulong, _atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ulong_nv, _atomic_dec_64_nv) +#else +STRONG_ALIAS(_atomic_dec_ptr, _atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ptr_nv, _atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ulong, _atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ulong_nv, _atomic_dec_32_nv) +#endif +STRONG_ALIAS(_atomic_dec_uint, _atomic_dec_32) +STRONG_ALIAS(_atomic_dec_uint_nv, _atomic_dec_32_nv) + +ATOMIC_OP_ALIAS(atomic_dec_ptr, _atomic_dec_ptr) +ATOMIC_OP_ALIAS(atomic_dec_ptr_nv, _atomic_dec_ptr_nv) +ATOMIC_OP_ALIAS(atomic_dec_uint, _atomic_dec_uint) +ATOMIC_OP_ALIAS(atomic_dec_uint_nv, _atomic_dec_uint_nv) +ATOMIC_OP_ALIAS(atomic_dec_ulong, _atomic_dec_ulong) +ATOMIC_OP_ALIAS(atomic_dec_ulong_nv, _atomic_dec_ulong_nv) diff --git a/common/lib/libc/arch/mips/atomic/atomic_inc.S b/common/lib/libc/arch/mips/atomic/atomic_inc.S new file mode 100644 index 000000000..5ab8e4934 --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_inc.S @@ -0,0 +1,111 @@ +/* $NetBSD: atomic_inc.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + +RCSID("$NetBSD: atomic_inc.S,v 1.2 2009/12/14 00:39:00 matt Exp $") + + .text + .set noat + .set noreorder + .set nomacro + +LEAF(_atomic_inc_32) +1: INT_LL t0, 0(a0) + nop + INT_ADDU t0, 1 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_32, _atomic_inc_32) + +LEAF(_atomic_inc_32_nv) +1: INT_LL v0, 0(a0) + nop + INT_ADDU v0, 1 + move t0, v0 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_32_nv, _atomic_inc_32_nv) + +#if !defined(__mips_o32) +LEAF(_atomic_inc_64) +1: REG_LL t0, 0(a0) + nop + REG_ADDU t0, 1 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_inc_64) +ATOMIC_OP_ALIAS(atomic_inc_64, _atomic_inc_64) + +LEAF(_atomic_inc_64_nv) +1: REG_LL v0, 0(a0) + nop + REG_ADDU v0, 1 + move t0, v0 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_inc_64_nv) +ATOMIC_OP_ALIAS(atomic_inc_64_nv, _atomic_inc_64_nv) +#endif + +#ifdef _LP64 +STRONG_ALIAS(_atomic_inc_ptr, _atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ptr_nv, _atomic_inc_64_nv) +STRONG_ALIAS(_atomic_inc_ulong, _atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ulong_nv, _atomic_inc_64_nv) +#else +STRONG_ALIAS(_atomic_inc_ptr, _atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ptr_nv, _atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ulong, _atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ulong_nv, _atomic_inc_32_nv) +#endif +STRONG_ALIAS(_atomic_inc_uint, _atomic_inc_32) +STRONG_ALIAS(_atomic_inc_uint_nv, _atomic_inc_32_nv) + +ATOMIC_OP_ALIAS(atomic_inc_ptr, _atomic_inc_ptr) +ATOMIC_OP_ALIAS(atomic_inc_ptr_nv, _atomic_inc_ptr_nv) +ATOMIC_OP_ALIAS(atomic_inc_uint, _atomic_inc_uint) +ATOMIC_OP_ALIAS(atomic_inc_uint_nv, _atomic_inc_uint_nv) +ATOMIC_OP_ALIAS(atomic_inc_ulong, _atomic_inc_ulong) +ATOMIC_OP_ALIAS(atomic_inc_ulong_nv, _atomic_inc_ulong_nv) diff --git a/common/lib/libc/arch/mips/atomic/atomic_op_asm.h b/common/lib/libc/arch/mips/atomic/atomic_op_asm.h new file mode 100644 index 000000000..34bdd3bea --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_op_asm.h @@ -0,0 +1,47 @@ +/* $NetBSD: atomic_op_asm.h,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#endif /* _KERNEL */ + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/mips/atomic/atomic_or.S b/common/lib/libc/arch/mips/atomic/atomic_or.S new file mode 100644 index 000000000..bf25d8945 --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_or.S @@ -0,0 +1,103 @@ +/* $NetBSD: atomic_or.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + + .text + .set noat + .set noreorder + .set nomacro + +LEAF(_atomic_or_32) +1: INT_LL t0, 0(a0) + nop + or t0, a1 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_32, _atomic_or_32) + +LEAF(_atomic_or_32_nv) +1: INT_LL v0, 0(a0) + nop + or v0, a1 + move t0, v0 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_32_nv, _atomic_or_32_nv) + +#if !defined(__mips_o32) +LEAF(_atomic_or_64) +1: REG_LL t0, 0(a0) + nop + or t0, a1 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_or_64) +ATOMIC_OP_ALIAS(atomic_or_64, _atomic_or_64) + +LEAF(_atomic_or_64_nv) +1: REG_LL v0, 0(a0) + nop + or v0, a1 + move t0, v0 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop + j ra + nop +END(_atomic_or_64_nv) +ATOMIC_OP_ALIAS(atomic_or_64_nv, _atomic_or_64_nv) +#endif + +#ifdef _LP64 +STRONG_ALIAS(_atomic_or_ulong, _atomic_or_64) +STRONG_ALIAS(_atomic_or_ulong_nv, _atomic_or_64_nv) +#else +STRONG_ALIAS(_atomic_or_ulong, _atomic_or_32) +STRONG_ALIAS(_atomic_or_ulong_nv, _atomic_or_32_nv) +#endif +STRONG_ALIAS(_atomic_or_uint, _atomic_or_32) +STRONG_ALIAS(_atomic_or_uint_nv, _atomic_or_32_nv) + +ATOMIC_OP_ALIAS(atomic_or_uint, _atomic_or_uint) +ATOMIC_OP_ALIAS(atomic_or_uint_nv, _atomic_or_uint_nv) +ATOMIC_OP_ALIAS(atomic_or_ulong, _atomic_or_ulong) +ATOMIC_OP_ALIAS(atomic_or_ulong_nv, _atomic_or_ulong_nv) diff --git a/common/lib/libc/arch/mips/atomic/atomic_swap.S b/common/lib/libc/arch/mips/atomic/atomic_swap.S new file mode 100644 index 000000000..b5f94778d --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/atomic_swap.S @@ -0,0 +1,79 @@ +/* $NetBSD: atomic_swap.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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 "atomic_op_asm.h" + +RCSID("$NetBSD: atomic_swap.S,v 1.2 2009/12/14 00:39:00 matt Exp $") + + .text + .set noat + .set noreorder + .set nomacro + +LEAF(_atomic_swap_32) +1: INT_LL v0, 0(a0) + nop + move t0, a1 + INT_SC t0, 0(a0) + beq t0, zero, 1b + nop +2: + j ra + nop +END(_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_32, _atomic_swap_32) + +#if !defined(__mips_o32) +LEAF(_atomic_swap_64) +1: REG_LL v0, 0(a0) + nop + move t0, a1 + REG_SC t0, 0(a0) + beq t0, zero, 1b + nop +2: + j ra + nop +END(_atomic_swap_64) +ATOMIC_OP_ALIAS(atomic_swap_64, _atomic_swap_64) +#endif + +#ifdef _LP64 +STRONG_ALIAS(_atomic_swap_ptr, _atomic_swap_64) +STRONG_ALIAS(_atomic_swap_ulong, _atomic_swap_64) +#else +STRONG_ALIAS(_atomic_swap_ptr, _atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ulong, _atomic_swap_32) +#endif +STRONG_ALIAS(_atomic_swap_uint, _atomic_swap_32) + +ATOMIC_OP_ALIAS(atomic_swap_ptr, _atomic_swap_ptr) +ATOMIC_OP_ALIAS(atomic_swap_uint, _atomic_swap_uint) +ATOMIC_OP_ALIAS(atomic_swap_ulong, _atomic_swap_ulong) diff --git a/common/lib/libc/arch/mips/atomic/membar_ops.S b/common/lib/libc/arch/mips/atomic/membar_ops.S new file mode 100644 index 000000000..4d016246d --- /dev/null +++ b/common/lib/libc/arch/mips/atomic/membar_ops.S @@ -0,0 +1,77 @@ +/* $NetBSD: membar_ops.S,v 1.4 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe, and by Andrew Doran. + * + * 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 "atomic_op_asm.h" + +#if defined(_KERNEL) + +#ifdef _KERNEL_OPT +#include "opt_cputype.h" +#include "opt_lockdebug.h" +#include "opt_multiprocessor.h" +#endif + + +#include + +#if (MIPS_HAS_LLSC != 0 && defined(MULTIPROCESSOR)) || !defined(__mips_o32) +#define SYNC sync +#endif +#elif !defined(__mips_o32) +#define SYNC sync +#endif + + .text + +LEAF(_membar_sync) + j ra +#ifdef SYNC + SYNC +#else + nop +#endif +END(_membar_sync) + +#ifdef _KERNEL +STRONG_ALIAS(mb_read, _membar_sync) +STRONG_ALIAS(mb_write, _membar_sync) +STRONG_ALIAS(mb_memory, _membar_sync) +#endif + +ATOMIC_OP_ALIAS(membar_sync,_membar_sync) +ATOMIC_OP_ALIAS(membar_enter,_membar_sync) +STRONG_ALIAS(_membar_enter,_membar_sync) +ATOMIC_OP_ALIAS(membar_exit,_membar_sync) +STRONG_ALIAS(_membar_exit,_membar_sync) +ATOMIC_OP_ALIAS(membar_producer,_membar_sync) +STRONG_ALIAS(_membar_producer,_membar_sync) +ATOMIC_OP_ALIAS(membar_consumer,_membar_sync) +STRONG_ALIAS(_membar_consumer,_membar_sync) diff --git a/common/lib/libc/arch/mips/gen/byte_swap_2.S b/common/lib/libc/arch/mips/gen/byte_swap_2.S new file mode 100644 index 000000000..52c6a97ef --- /dev/null +++ b/common/lib/libc/arch/mips/gen/byte_swap_2.S @@ -0,0 +1,83 @@ +/* $NetBSD: byte_swap_2.S,v 1.3 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)htons.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: byte_swap_2.S,v 1.3 2009/12/14 00:39:00 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#undef _LOCORE +#define _LOCORE /* XXX not really, just assembly-code source */ +#include + +#if defined(_KERNEL) || defined(_STANDALONE) +#define BSWAP16_NAME bswap16 +#else +#define BSWAP16_NAME __bswap16 +#endif + +NLEAF(BSWAP16_NAME) +#if BYTE_ORDER == LITTLE_ENDIAN +ALEAF(htons) +ALEAF(ntohs) +#endif +#if (__mips == 32 || __mips == 64) && __mips_isa_rev == 2 + /* + * If we are on MIPS32r2 or MIPS64r2, use the new instructions + */ + wsbh a0, a0 # word swap bytes within halfwords + and v0, a0, 0xffff # bound it to 16bits + j ra +#else + srl v0, a0, 8 + and v0, v0, 0xff + sll v1, a0, 8 + and v1, v1, 0xff00 + or v0, v0, v1 + j ra +#endif +END(BSWAP16_NAME) + +#if BYTE_ORDER == BIG_ENDIAN +NLEAF(htons) +ALEAF(ntohs) + move v0, a0 + j ra +END(htons) +#endif diff --git a/common/lib/libc/arch/mips/gen/byte_swap_4.S b/common/lib/libc/arch/mips/gen/byte_swap_4.S new file mode 100644 index 000000000..aa13e25cb --- /dev/null +++ b/common/lib/libc/arch/mips/gen/byte_swap_4.S @@ -0,0 +1,87 @@ +/* $NetBSD: byte_swap_4.S,v 1.3 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)htonl.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: byte_swap_4.S,v 1.3 2009/12/14 00:39:00 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#undef _LOCORE +#define _LOCORE /* XXX not really, just assembly-code source */ +#include + +#if defined(_KERNEL) || defined(_STANDALONE) +#define BSWAP32_NAME bswap32 +#else +#define BSWAP32_NAME __bswap32 +#endif + +NLEAF(BSWAP32_NAME) # a0 = 0x11223344, return 0x44332211 +#if BYTE_ORDER == LITTLE_ENDIAN +ALEAF(htonl) # a0 = 0x11223344, return 0x44332211 +ALEAF(ntohl) +#endif +#if (__mips == 32 || __mips == 64) && __mips_isa_rev == 2 + /* + * If we are on MIPS32R2 or MIPS64R2 it's much easier + */ + wsbh a0, a0 # word swap bytes within halfwords + rotr v0, a0, 16 # rotate word 16bits + j ra +#else + srl v1, a0, 24 # v1 = 0x00000011 + sll v0, a0, 24 # v0 = 0x44000000 + or v0, v0, v1 + and v1, a0, 0xff00 + sll v1, v1, 8 # v1 = 0x00330000 + or v0, v0, v1 + srl v1, a0, 8 + and v1, v1, 0xff00 # v1 = 0x00002200 + or v0, v0, v1 + j ra +#endif +END(BSWAP32_NAME) + +#if BYTE_ORDER == BIG_ENDIAN +NLEAF(htonl) # a0 = 0x11223344, return 0x44332211 +ALEAF(ntohl) + move v0, a0 + j ra +END(htonl) +#endif diff --git a/common/lib/libc/arch/mips/gen/byte_swap_8.S b/common/lib/libc/arch/mips/gen/byte_swap_8.S new file mode 100644 index 000000000..4b3cb859b --- /dev/null +++ b/common/lib/libc/arch/mips/gen/byte_swap_8.S @@ -0,0 +1,118 @@ +/* $NetBSD: byte_swap_8.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: byte_swap_8.S,v 1.2 2009/12/14 00:39:00 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +#undef _LOCORE +#define _LOCORE /* XXX not really, just assembly-code source */ +#include + +NLEAF(bswap64) # a0 = 0xffeeddccbbaa9988 return 0x8899aabbccddeeff +#if (__mips == 32 || __mips == 64) && __mips_isa_rev == 2 +#if !defined(__mips_o32) + /* + * If we are on MIPS32r2 or MIPS64r2 use the new instructions. + */ + dsbh v0, a0 # dwords swap bytes within halfwords + dshd v0, v0 # dwords swap halwords within dwords + j ra +#else /* defined(__mips_o32) */ + /* + * If we are on MIPS32r2 or MIPS64r2 use the new instructions. + * (except we must use the 32bit versions) + */ + wsbh v1, a0 # word swap bytes within halfwords + wsbh v0, a1 # word swap bytes within halfwords + rotr v1, v1, 16 # rotate word 16bits and swap word + rotr v0, v0, 16 # rotate word 16bits and swap word + j ra +#endif /* defined(__mips_o32) */ +#elif !defined(__mips_o32) + # a0 = 0xffeeddccbbaa9988 + li t0, 0xffff # t0 = 0x000000000000ffff + dsll t1, t0, 32 # t1 = 0x0000ffff00000000 + or t0, t1 # t0 = 0x0000ffff0000ffff + dsll t2, t0, 8 # t2 = 0x00ffff0000ffff00 + xor t2, t0 # t2 = 0x00ff00ff00ff00ff + /* + * We could swap by halfword, but that would be one instruction longer. + */ + dsrl ta0, a0, 32 # ta0 = 0x00000000ffeeddcc + dsll ta1, a0, 32 # ta1 = 0xbbaa998800000000 + or a1, ta0, ta1 # a1 = 0xbbaa9988ffeeddcc + # words swapped + and ta0, a1, t0 # ta0 = 0x000099880000ddcc + dsrl ta1, a1, 16 # ta1 = 0x0000bbaa9988ffee + and ta1, t0 # ta1 = 0x0000bbaa0000ffee + dsll a2, ta0, 16 # a2 = 0x99880000ddcc0000 + or a2, ta1 # a2 = 0x9988bbaaddccffee + # halfwords swapped + and ta0, a2, t2 # ta0 = 0x008800aa00cc00ee + dsrl ta1, a2, 8 # ta1 = 0x009988bbaaddccff + and ta1, t2 # ta1 = 0x009900bb00dd00ff + dsll v0, ta0, 8 # v0 = 0x8800aa00cc00ee00 + or v0, ta1 # v0 = 0x8899aabbccddeeff + # bytes swapped + j ra +#else /* defined(__mips_o32) */ + /* + * 32bit ABI. + */ + # a0 = 0xccddeeff + # a1 = 0x8899aabb + srl t0, a0, 24 # t0 = 0x000000cc + srl t1, a1, 24 # t1 = 0x00000088 + sll ta0, a0, 24 # ta0 = 0xff000000 + sll ta1, a1, 24 # ta1 = 0xbb000000 + or ta0, ta0, t0 # ta0 = 0xff0000cc + or ta1, ta1, t1 # ta1 = 0xbb000088 + and t0, a0, 0xff00 # t0 = 0x0000ee00 + and t1, a1, 0xff00 # t1 = 0x0000aa00 + sll t0, t0, 8 # t0 = 0x00ee0000 + sll t1, t1, 8 # t1 = 0x00aa0000 + or ta0, ta0, t0 # ta0 = 0xffee00cc + or ta1, ta1, t1 # ta1 = 0xbbaa0088 + srl t0, a0, 8 # t0 = 0x00ccddee + srl t1, a1, 8 # t1 = 0x008899aa + and t0, t0, 0xff00 # t0 = 0x0000dd00 + and t1, t1, 0xff00 # t1 = 0x00009900 + or v1, ta0, t0 # v1 = 0xffeeddcc + or v0, ta1, t1 # v0 = 0xbbaa9988 + j ra +#endif /* defined(__mips_o32) */ +END(bswap64) diff --git a/common/lib/libc/arch/mips/string/bcopy.S b/common/lib/libc/arch/mips/string/bcopy.S new file mode 100644 index 000000000..05f6777e0 --- /dev/null +++ b/common/lib/libc/arch/mips/string/bcopy.S @@ -0,0 +1,294 @@ +/* $NetBSD: bcopy.S,v 1.3 2009/12/14 00:39:00 matt Exp $ */ + +/* + * Mach Operating System + * Copyright (c) 1993 Carnegie Mellon University + * All Rights Reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR + * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie Mellon + * the rights to redistribute these changes. + */ + +/* + * File: mips_bcopy.s + * Author: Chris Maeda + * Date: June 1993 + * + * Fast copy routine. Derived from aligned_block_copy. + */ + + +#include +#ifndef _LOCORE +#define _LOCORE /* XXX not really, just assembly-code source */ +#endif +#include + + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)mips_bcopy.s 2.2 CMU 18/06/93") +#else + RCSID("$NetBSD: bcopy.S,v 1.3 2009/12/14 00:39:00 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * bcopy(caddr_t src, caddr_t dst, unsigned int len) + * + * a0 src address + * a1 dst address + * a2 length + */ + +#if defined(MEMCOPY) || defined(MEMMOVE) +#ifdef MEMCOPY +#define FUNCTION memcpy +#else +#define FUNCTION memmove +#endif +#define SRCREG a1 +#define DSTREG a0 +#else +#define FUNCTION bcopy +#define SRCREG a0 +#define DSTREG a1 +#endif + +#define SIZEREG a2 + +LEAF(FUNCTION) + .set noat + .set noreorder + +#if defined(MEMCOPY) || defined(MEMMOVE) + /* set up return value, while we still can */ + move v0,DSTREG +#endif + /* + * Make sure we can copy forwards. + */ + sltu t0,SRCREG,DSTREG # t0 == SRCREG < DSTREG + bne t0,zero,6f # copy backwards + + /* + * There are four alignment cases (with frequency) + * (Based on measurements taken with a DECstation 5000/200 + * inside a Mach kernel.) + * + * aligned -> aligned (mostly) + * unaligned -> aligned (sometimes) + * aligned,unaligned -> unaligned (almost never) + * + * Note that we could add another case that checks if + * the destination and source are unaligned but the + * copy is alignable. eg if src and dest are both + * on a halfword boundary. + */ + andi t1,DSTREG,(SZREG-1) # get last bits of dest + bne t1,zero,3f # dest unaligned + andi t0,SRCREG,(SZREG-1) # get last bits of src + bne t0,zero,5f + + /* + * Forward aligned->aligned copy, 8 words at a time. + */ +98: + li AT,-(SZREG*8) + and t0,SIZEREG,AT # count truncated to multiples + PTR_ADDU a3,SRCREG,t0 # run fast loop up to this addr + sltu AT,SRCREG,a3 # any work to do? + beq AT,zero,2f + PTR_SUBU SIZEREG,t0 + + /* + * loop body + */ +1: # cp + REG_L t3,(0*SZREG)(SRCREG) + REG_L v1,(1*SZREG)(SRCREG) + REG_L t0,(2*SZREG)(SRCREG) + REG_L t1,(3*SZREG)(SRCREG) + PTR_ADDU SRCREG,SZREG*8 + REG_S t3,(0*SZREG)(DSTREG) + REG_S v1,(1*SZREG)(DSTREG) + REG_S t0,(2*SZREG)(DSTREG) + REG_S t1,(3*SZREG)(DSTREG) + REG_L t1,(-1*SZREG)(SRCREG) + REG_L t0,(-2*SZREG)(SRCREG) + REG_L v1,(-3*SZREG)(SRCREG) + REG_L t3,(-4*SZREG)(SRCREG) + PTR_ADDU DSTREG,SZREG*8 + REG_S t1,(-1*SZREG)(DSTREG) + REG_S t0,(-2*SZREG)(DSTREG) + REG_S v1,(-3*SZREG)(DSTREG) + bne SRCREG,a3,1b + REG_S t3,(-4*SZREG)(DSTREG) + + /* + * Copy a word at a time, no loop unrolling. + */ +2: # wordcopy + andi t2,SIZEREG,(SZREG-1) # get byte count / SZREG + PTR_SUBU t2,SIZEREG,t2 # t2 = words to copy * SZREG + beq t2,zero,3f + PTR_ADDU t0,SRCREG,t2 # stop at t0 + PTR_SUBU SIZEREG,SIZEREG,t2 +1: + REG_L t3,0(SRCREG) + PTR_ADDU SRCREG,SZREG + REG_S t3,0(DSTREG) + bne SRCREG,t0,1b + PTR_ADDU DSTREG,SZREG + +3: # bytecopy + beq SIZEREG,zero,4f # nothing left to do? + nop +1: + lb t3,0(SRCREG) + PTR_ADDU SRCREG,1 + sb t3,0(DSTREG) + PTR_SUBU SIZEREG,1 + bgtz SIZEREG,1b + PTR_ADDU DSTREG,1 + +4: # copydone + j ra + nop + + /* + * Copy from unaligned source to aligned dest. + */ +5: # destaligned + andi t0,SIZEREG,(SZREG-1) # t0 = bytecount mod SZREG + PTR_SUBU a3,SIZEREG,t0 # number of words to transfer + beq a3,zero,3b + nop + move SIZEREG,t0 # this many to do after we are done + PTR_ADDU a3,SRCREG,a3 # stop point + +1: + REG_LHI t3,0(SRCREG) + REG_LLO t3,SZREG-1(SRCREG) + PTR_ADDI SRCREG,SZREG + REG_S t3,0(DSTREG) + bne SRCREG,a3,1b + PTR_ADDI DSTREG,SZREG + + b 3b + nop + +6: # backcopy -- based on above + PTR_ADDU SRCREG,SIZEREG + PTR_ADDU DSTREG,SIZEREG + andi t1,DSTREG,SZREG-1 # get last 3 bits of dest + bne t1,zero,3f + andi t0,SRCREG,SZREG-1 # get last 3 bits of src + bne t0,zero,5f + + /* + * Forward aligned->aligned copy, 8*4 bytes at a time. + */ + li AT,(-8*SZREG) + and t0,SIZEREG,AT # count truncated to multiple of 32 + beq t0,zero,2f # any work to do? + PTR_SUBU SIZEREG,t0 + PTR_SUBU a3,SRCREG,t0 + + /* + * loop body + */ +1: # cp + REG_L t3,(-4*SZREG)(SRCREG) + REG_L v1,(-3*SZREG)(SRCREG) + REG_L t0,(-2*SZREG)(SRCREG) + REG_L t1,(-1*SZREG)(SRCREG) + PTR_SUBU SRCREG,8*SZREG + REG_S t3,(-4*SZREG)(DSTREG) + REG_S v1,(-3*SZREG)(DSTREG) + REG_S t0,(-2*SZREG)(DSTREG) + REG_S t1,(-1*SZREG)(DSTREG) + REG_L t1,(3*SZREG)(SRCREG) + REG_L t0,(2*SZREG)(SRCREG) + REG_L v1,(1*SZREG)(SRCREG) + REG_L t3,(0*SZREG)(SRCREG) + PTR_SUBU DSTREG,8*SZREG + REG_S t1,(3*SZREG)(DSTREG) + REG_S t0,(2*SZREG)(DSTREG) + REG_S v1,(1*SZREG)(DSTREG) + bne SRCREG,a3,1b + REG_S t3,(0*SZREG)(DSTREG) + + /* + * Copy a word at a time, no loop unrolling. + */ +2: # wordcopy + andi t2,SIZEREG,SZREG-1 # get byte count / 4 + PTR_SUBU t2,SIZEREG,t2 # t2 = number of words to copy + beq t2,zero,3f + PTR_SUBU t0,SRCREG,t2 # stop at t0 + PTR_SUBU SIZEREG,SIZEREG,t2 +1: + REG_L t3,-SZREG(SRCREG) + PTR_SUBU SRCREG,SZREG + REG_S t3,-SZREG(DSTREG) + bne SRCREG,t0,1b + PTR_SUBU DSTREG,SZREG + +3: # bytecopy + beq SIZEREG,zero,4f # nothing left to do? + nop +1: + lb t3,-1(SRCREG) + PTR_SUBU SRCREG,1 + sb t3,-1(DSTREG) + PTR_SUBU SIZEREG,1 + bgtz SIZEREG,1b + PTR_SUBU DSTREG,1 + +4: # copydone + j ra + nop + + /* + * Copy from unaligned source to aligned dest. + */ +5: # destaligned + andi t0,SIZEREG,SZREG-1 # t0 = bytecount mod 4 + PTR_SUBU a3,SIZEREG,t0 # number of words to transfer + beq a3,zero,3b + nop + move SIZEREG,t0 # this many to do after we are done + PTR_SUBU a3,SRCREG,a3 # stop point + +1: + REG_LHI t3,-SZREG(SRCREG) + REG_LLO t3,-1(SRCREG) + PTR_SUBU SRCREG,SZREG + REG_S t3,-SZREG(DSTREG) + bne SRCREG,a3,1b + PTR_SUBU DSTREG,SZREG + + b 3b + nop + + .set reorder + .set at + END(FUNCTION) diff --git a/common/lib/libc/arch/mips/string/ffs.S b/common/lib/libc/arch/mips/string/ffs.S new file mode 100644 index 000000000..62f2d0aa6 --- /dev/null +++ b/common/lib/libc/arch/mips/string/ffs.S @@ -0,0 +1,54 @@ +/* $NetBSD: ffs.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + /* RCSID("from: @(#)ffs.s 8.1 (Berkeley) 6/4/93") */ + RCSID("$NetBSD: ffs.S,v 1.2 2009/12/14 00:39:00 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* bit = ffs(value) */ + +LEAF(ffs) + move v0, zero + beq a0, zero, done +1: + and v1, a0, 1 # bit set? + addu v0, v0, 1 + srl a0, a0, 1 + beq v1, zero, 1b # no, continue +done: + j ra +END(ffs) diff --git a/common/lib/libc/arch/mips/string/memcpy.S b/common/lib/libc/arch/mips/string/memcpy.S new file mode 100644 index 000000000..564d86c86 --- /dev/null +++ b/common/lib/libc/arch/mips/string/memcpy.S @@ -0,0 +1,5 @@ +/* $NetBSD: memcpy.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + + +#define MEMCOPY +#include "bcopy.S" diff --git a/common/lib/libc/arch/mips/string/memmove.S b/common/lib/libc/arch/mips/string/memmove.S new file mode 100644 index 000000000..fa1922b24 --- /dev/null +++ b/common/lib/libc/arch/mips/string/memmove.S @@ -0,0 +1,5 @@ +/* $NetBSD: memmove.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + + +#define MEMMOVE +#include "bcopy.S" diff --git a/common/lib/libc/arch/mips/string/strchr.S b/common/lib/libc/arch/mips/string/strchr.S new file mode 100644 index 000000000..2e49a4c38 --- /dev/null +++ b/common/lib/libc/arch/mips/string/strchr.S @@ -0,0 +1,59 @@ +/* $NetBSD: strchr.S,v 1.3 2011/01/02 02:58:52 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + ASMSTR("from: @(#)index.s 8.1 (Berkeley) 6/4/93") + ASMSTR("$NetBSD: strchr.S,v 1.3 2011/01/02 02:58:52 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +#ifdef __ABICALLS__ + .abicalls +#endif + +LEAF(strchr) +XLEAF(index) +1: + lbu a2, 0(a0) # get a byte + PTR_ADDU a0, 1 + beq a2, a1, fnd + bne a2, zero, 1b +notfnd: + move v0, zero + j ra +fnd: + PTR_SUBU v0, a0, 1 + j ra +END(strchr) diff --git a/common/lib/libc/arch/mips/string/strcmp.S b/common/lib/libc/arch/mips/string/strcmp.S new file mode 100644 index 000000000..6d576922f --- /dev/null +++ b/common/lib/libc/arch/mips/string/strcmp.S @@ -0,0 +1,63 @@ +/* $NetBSD: strcmp.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + /* RCSID("from: @(#)strcmp.s 8.1 (Berkeley) 6/4/93") */ + RCSID("$NetBSD: strcmp.S,v 1.2 2009/12/14 00:39:00 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * NOTE: this version assumes unsigned chars in order to be "8 bit clean". + */ +LEAF(strcmp) +1: + lbu t0, 0(a0) # get two bytes and compare them + lbu t1, 0(a1) + beq t0, zero, LessOrEq # end of first string? + bne t0, t1, NotEq + lbu t0, 1(a0) # unroll loop + lbu t1, 1(a1) + PTR_ADD a0, a0, 2 + beq t0, zero, LessOrEq # end of first string? + PTR_ADD a1, a1, 2 + beq t0, t1, 1b +NotEq: + subu v0, t0, t1 + j ra +LessOrEq: + subu v0, zero, t1 + j ra +END(strcmp) diff --git a/common/lib/libc/arch/mips/string/strlen.S b/common/lib/libc/arch/mips/string/strlen.S new file mode 100644 index 000000000..842dcf93f --- /dev/null +++ b/common/lib/libc/arch/mips/string/strlen.S @@ -0,0 +1,50 @@ +/* $NetBSD: strlen.S,v 1.2 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + /* RCSID("from: @(#)strlen.s 8.1 (Berkeley) 6/4/93") */ + RCSID("$NetBSD: strlen.S,v 1.2 2009/12/14 00:39:00 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +LEAF(strlen) + PTR_ADDU v1, a0, 1 +1: + lb v0, 0(a0) # get byte from string + PTR_ADDU a0, a0, 1 # increment pointer + bne v0, zero, 1b # continue if not end + PTR_SUBU v0, a0, v1 # compute length - 1 for '\0' char + j ra +END(strlen) diff --git a/common/lib/libc/arch/mips/string/strrchr.S b/common/lib/libc/arch/mips/string/strrchr.S new file mode 100644 index 000000000..8b90edc64 --- /dev/null +++ b/common/lib/libc/arch/mips/string/strrchr.S @@ -0,0 +1,57 @@ +/* $NetBSD: strrchr.S,v 1.2 2011/01/02 02:58:52 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + ASMSTR("from: @(#)rindex.s 8.1 (Berkeley) 6/4/93") + ASMSTR("$NetBSD: strrchr.S,v 1.2 2011/01/02 02:58:52 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +#ifdef __ABICALLS__ + .abicalls +#endif + +LEAF(strrchr) +XLEAF(rindex) + move v0, zero # default if not found +1: + lbu a3, 0(a0) # get a byte + PTR_ADDU a0, a0, 1 + bne a3, a1, 2f + PTR_SUBU v0, a0, 1 # save address of last match +2: + bne a3, zero, 1b # continue if not end + j ra +END(strrchr) diff --git a/common/lib/libc/arch/powerpc/atomic/Makefile.inc b/common/lib/libc/arch/powerpc/atomic/Makefile.inc new file mode 100644 index 000000000..bc058908e --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/Makefile.inc @@ -0,0 +1,15 @@ +# $NetBSD: Makefile.inc,v 1.6 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add.S atomic_and.S atomic_cas.S atomic_dec.S \ + atomic_inc.S atomic_or.S atomic_swap.S membar_ops.S + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_cas.c + +.endif diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_add.S b/common/lib/libc/arch/powerpc/atomic/atomic_add.S new file mode 100644 index 000000000..fa69943b3 --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_add.S @@ -0,0 +1,70 @@ +/* $NetBSD: atomic_add.S,v 1.7 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: atomic_add.S,v 1.7 2011/01/15 07:31:11 matt Exp $") + + .text +ENTRY(_atomic_add_32) +1: lwarx %r10,0,%r3 + add %r10,%r10,%r4 + stwcx. %r10,0,%r3 + bne- 1b + blr +END(_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_32,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_add_long,_atomic_add_32) +STRONG_ALIAS(_atomic_add_long,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_ptr,_atomic_add_32) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32) +#endif + +ENTRY(_atomic_add_32_nv) +1: lwarx %r10,0,%r3 + add %r10,%r10,%r4 + stwcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +END(_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_32_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_int_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_add_long_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv) +ATOMIC_OP_ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv) +#endif diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_and.S b/common/lib/libc/arch/powerpc/atomic/atomic_and.S new file mode 100644 index 000000000..97ba1eed2 --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_and.S @@ -0,0 +1,66 @@ +/* $NetBSD: atomic_and.S,v 1.6 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: atomic_and.S,v 1.6 2011/01/15 07:31:11 matt Exp $") + + .text +ENTRY(_atomic_and_32) +1: lwarx %r10,0,%r3 + and %r10,%r10,%r4 + stwcx. %r10,0,%r3 + bne- 1b + blr +END(_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_32,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_and_ulong,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32) +#endif + +ENTRY(_atomic_and_32_nv) +1: lwarx %r10,0,%r3 + and %r10,%r10,%r4 + stwcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +END(_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_32_nv,_atomic_and_32_nv) +ATOMIC_OP_ALIAS(atomic_and_uint_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv) +#endif diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_cas.S b/common/lib/libc/arch/powerpc/atomic/atomic_cas.S new file mode 100644 index 000000000..2ecaffd1c --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_cas.S @@ -0,0 +1,66 @@ +/* $NetBSD: atomic_cas.S,v 1.7 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: atomic_cas.S,v 1.7 2011/01/15 07:31:11 matt Exp $") + + .text +ENTRY(_atomic_cas_32) +1: lwarx %r10,0,%r3 + cmpw %r10,%r4 + bne- 2f + stwcx. %r5,0,%r3 + bne- 1b +2: mr %r3,%r10 + blr +END(_atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_32,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_cas_ulong,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ptr,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32) +#endif + +ATOMIC_OP_ALIAS(atomic_cas_32_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_32_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_cas_ulong_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ptr_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_32) +#endif diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_dec.S b/common/lib/libc/arch/powerpc/atomic/atomic_dec.S new file mode 100644 index 000000000..9ee07cf66 --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_dec.S @@ -0,0 +1,70 @@ +/* $NetBSD: atomic_dec.S,v 1.6 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: atomic_dec.S,v 1.6 2011/01/15 07:31:11 matt Exp $") + .text + +ENTRY(_atomic_dec_32) +1: lwarx %r10,0,%r3 + addi %r10,%r10,-1 + stwcx. %r10,0,%r3 + bne- 1b + blr +END(_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_32,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_dec_ulong,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_ptr,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32) +#endif + +ENTRY(_atomic_dec_32_nv) +1: lwarx %r10,0,%r3 + addi %r10,%r10,-1 + stwcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +END(_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv) +ATOMIC_OP_ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv) +#endif diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_inc.S b/common/lib/libc/arch/powerpc/atomic/atomic_inc.S new file mode 100644 index 000000000..b46694772 --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_inc.S @@ -0,0 +1,70 @@ +/* $NetBSD: atomic_inc.S,v 1.6 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: atomic_inc.S,v 1.6 2011/01/15 07:31:11 matt Exp $") + + .text +ENTRY(_atomic_inc_32) +1: lwarx %r10,0,%r3 + addi %r10,%r10,1 + stwcx. %r10,0,%r3 + bne- 1b + blr +END(_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_32,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_inc_ulong,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_ptr,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32) +#endif + +ENTRY(_atomic_inc_32_nv) +1: lwarx %r10,0,%r3 + addi %r10,%r10,1 + stwcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +END(_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv) +ATOMIC_OP_ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv) +#endif diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_op_asm.h b/common/lib/libc/arch/powerpc/atomic/atomic_op_asm.h new file mode 100644 index 000000000..456fb7f47 --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_op_asm.h @@ -0,0 +1,47 @@ +/* $NetBSD: atomic_op_asm.h,v 1.5 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#endif /* _KERNEL */ + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_or.S b/common/lib/libc/arch/powerpc/atomic/atomic_or.S new file mode 100644 index 000000000..5c634f9a5 --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_or.S @@ -0,0 +1,66 @@ +/* $NetBSD: atomic_or.S,v 1.6 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: atomic_or.S,v 1.6 2011/01/15 07:31:11 matt Exp $") + + .text +ENTRY(_atomic_or_32) +1: lwarx %r10,0,%r3 + or %r10,%r10,%r4 + stwcx. %r10,0,%r3 + bne- 1b + blr +END(_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_32,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_or_ulong,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32) +#endif + +ENTRY(_atomic_or_32_nv) +1: lwarx %r10,0,%r3 + or %r10,%r10,%r4 + stwcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +END(_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_32_nv,_atomic_or_32_nv) +ATOMIC_OP_ALIAS(atomic_or_uint_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv) +#endif diff --git a/common/lib/libc/arch/powerpc/atomic/atomic_swap.S b/common/lib/libc/arch/powerpc/atomic/atomic_swap.S new file mode 100644 index 000000000..db96dc46f --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/atomic_swap.S @@ -0,0 +1,52 @@ +/* $NetBSD: atomic_swap.S,v 1.6 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: atomic_swap.S,v 1.6 2011/01/15 07:31:11 matt Exp $") + + .text +ENTRY(_atomic_swap_32) +1: lwarx %r10,0,%r3 + stwcx. %r4,0,%r3 + bne- 1b + mr %r3,%r10 + blr +END(_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_32,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) +#if !defined(_LP64) +ATOMIC_OP_ALIAS(atomic_swap_ulong,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_ptr,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32) +#endif diff --git a/common/lib/libc/arch/powerpc/atomic/membar_ops.S b/common/lib/libc/arch/powerpc/atomic/membar_ops.S new file mode 100644 index 000000000..dd103cfc9 --- /dev/null +++ b/common/lib/libc/arch/powerpc/atomic/membar_ops.S @@ -0,0 +1,56 @@ +/* $NetBSD: membar_ops.S,v 1.4 2011/01/15 07:31:11 matt Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe, and by Andrew Doran. + * + * 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 "atomic_op_asm.h" + +__RCSID("$NetBSD: membar_ops.S,v 1.4 2011/01/15 07:31:11 matt Exp $") + + .text +/* These assume Total Store Order (TSO) */ + +ENTRY(_membar_consumer) + isync + blr +END(_membar_consumer) + +ENTRY(_membar_producer) + sync + blr +END(_membar_producer) + +ATOMIC_OP_ALIAS(membar_producer,_membar_producer) +ATOMIC_OP_ALIAS(membar_consumer,_membar_consumer) +ATOMIC_OP_ALIAS(membar_enter,_membar_consumer) +STRONG_ALIAS(_membar_enter,_membar_consumer) +ATOMIC_OP_ALIAS(membar_exit,_membar_producer) +STRONG_ALIAS(_membar_exit,_membar_producer) +ATOMIC_OP_ALIAS(membar_sync,_membar_producer) +STRONG_ALIAS(_membar_sync,_membar_producer) diff --git a/common/lib/libc/arch/powerpc/string/bcopy.S b/common/lib/libc/arch/powerpc/string/bcopy.S new file mode 100644 index 000000000..35582a8fa --- /dev/null +++ b/common/lib/libc/arch/powerpc/string/bcopy.S @@ -0,0 +1,4 @@ +/* $NetBSD: bcopy.S,v 1.1 2008/02/21 17:35:47 garbled Exp $ */ + +#define _BCOPY +#include "memmove.S" diff --git a/common/lib/libc/arch/powerpc/string/ffs.S b/common/lib/libc/arch/powerpc/string/ffs.S new file mode 100644 index 000000000..9a8c31213 --- /dev/null +++ b/common/lib/libc/arch/powerpc/string/ffs.S @@ -0,0 +1,45 @@ +/* $NetBSD: ffs.S,v 1.5 2011/01/15 07:31:12 matt Exp $ */ + +/*- + * Copyright (C) 2001 Martin J. Laubach + * 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 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. + */ +/*----------------------------------------------------------------------*/ + +#include + +__RCSID("$NetBSD: ffs.S,v 1.5 2011/01/15 07:31:12 matt Exp $") + + .align 4 +ENTRY(ffs) + neg %r4, %r3 + and %r3, %r4, %r3 + cntlzw %r3, %r3 + li %r0, 32 + subf %r3, %r3, %r0 + blr +END(ffs) + +/*----------------------------------------------------------------------*/ diff --git a/common/lib/libc/arch/powerpc/string/memcmp.S b/common/lib/libc/arch/powerpc/string/memcmp.S new file mode 100644 index 000000000..2e5f6d6a0 --- /dev/null +++ b/common/lib/libc/arch/powerpc/string/memcmp.S @@ -0,0 +1,270 @@ +/* $NetBSD: memcmp.S,v 1.3 2011/01/15 07:31:12 matt Exp $ */ + +/* stropt/memcmp.S, pl_string_common, pl_linux 10/11/04 11:45:35 + * ========================================================================== + * Optimized memcmp implementation for IBM PowerPC 405/440. + * + * Copyright (c) 2003, IBM Corporation + * 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. + * * Neither the name of IBM 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 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 OWNER 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. + * + * ========================================================================== + * + * Function: Compare two character strings (up to n characters) + * + * int memcmp(const char *s1, const char *s2, int n) + * + * Input: r3 - buffer 1 address + * r4 - buffer 2 address + * r5 - maximum characters to compare + * Output: r3 <0 (less), 0 (equal), >0 (greater) + * + * ========================================================================== + */ + +#include + + .text + .align 4 +/* LINTSTUB: Func: void *memcmp(const void *, const void *, size_t) */ +ENTRY(memcmp) + + /* + * Check count passed in R5. If zero, return 0; otherwise continue. + */ + cmpwi %r5,0 + beq- ret_0; + + /* + * Most of the time the difference is found in the first + * several bytes. The following code minimizes the number + * of load operations for short compares. + */ + + mr %r11, %r3 /* Save buffer 1 */ + +again: + + not %r10, %r4 /* buffer 2: bytes to page bdy */ + rlwinm. %r10, %r10,29,23,31 /* buffer 2: dwords to page bdy */ + beq- bytebybyte /* If < 8 bytes to the page bdy */ + /* do byte by byte */ + lwz %r8, 0(%r4) /* load 1st buffer 2 word */ + + not %r12, %r11 /* buffer 1: bytes to page bdy */ + rlwinm. %r12, %r12,29,23,31 /* buffer 1: dwords to page bdy */ + beq- bytebybyte /* If < 8 bytes to the page bdy */ + /* do byte by byte */ + lwz %r6, 0(%r11) /* load 1st buffer 1 word */ + + cmpwi %r5, 4 /* If remaining count <= 4 */ + ble+ first4 /* handle specially. DWG */ + + cmplw %r8, %r6 /* compare buffer 2 and buffer 1*/ + bne+ all_done /* different => we're done */ + + lwzu %r9, 4(%r4) /* load 2nd buffer 2 word */ + lwzu %r7, 4(%r11) /* load 2nd buffer 1 word */ + + cmpwi %r5, 8 /* If remaining count <= 8 */ + ble+ last4 /* handle specially. DWG */ + + cmplw %r9, %r7 /* compare buffer 2 and buffer 1*/ + bne+ all_done /* different => we're done */ + + addi %r5, %r5, -8 /* Update character counter DWG */ + addi %r10, %r4, 0x0004 /* DWG*/ + not %r10, %r10 /* buffer 2: bytes to page bdy DWG */ + rlwinm. %r10, %r10,29,23,31 /* buffer 2: dwords to page bdy DWG */ + addi %r12, %r11, 0x0004 /* DWG */ + not %r12, %r12 /* buffer 1: bytes to page bdy DWG */ + rlwinm. %r12, %r12,29,23,31 /* buffer 1: dwords to page bdy DWG */ + + /* The following section prior to loop: figures out whether */ + /* the buffer 1 or buffer 2 is closer to the page boundary. */ + /* The main loop count is then set up to reflect the number of */ + /* double words of the buffer that is closest */ + + cmpw %r10, %r12 /* Find closest */ + blt lt + + mr %r10, %r12 + +lt: + + srwi %r12, %r5, 3 /* Double check the total count */ + cmpw %r10, %r12 /* limitation */ + blt lt2 + + mr %r10, %r12 /* DWG */ +lt2: /* DWG */ + cmpwi %r10, 0 /* DWG */ + bne lt3 /* DWG */ + addi %r4, %r4, 0x0004 /* DWG */ + addi %r11,%r11,0x0004 /* DWG */ + b again /* DWG */ +lt3: /* DWG */ + mtctr %r10 /* dword count for loop */ + lwzu %r6, 4(%r11) /* pre-load buffer 1 word */ + + b in /* To the loop */ + +loop: /* main loop */ + + cmplw %r8, %r6 /* Compare first buffer 2 word */ + bne- all_done /* with first buffer 1 word */ + /* If different, we're done */ + cmplw %r9, %r7 /* Compare second buffer 2 word */ + /* with second buffer 1 word */ + lwzu %r6, 4(%r11) /* pre-load buffer 1 word */ + + bne- all_done /* If different, we're done */ + +in: + + lwzu %r7, 4(%r11) /* pre-load buffer 1 word */ + lwzu %r8, 4(%r4) /* pre-load buffer 2 word */ + lwzu %r9, 4(%r4) /* pre-load buffer 2 word */ + + bdnz+ loop /* Do more DW's if cnt > 0 */ + + /*mfctr %r12*/ /*DWG*/ /* number of dwords left */ + /*subf %r10, %r12, %r10*/ /*DWG*//* number of dwords compared */ + slwi %r10, %r10, 3 + subf %r5, %r10, %r5 /* adjust byte counter */ + /*bne+ partial*/ /*DWG*/ /* If less than 8 bytes, handle */ + /* specially */ + /*cmpwi %r5, 8*/ /* Removed. DWG */ + /*blt partial*/ /* Removed. DWG */ + + /*addic %r5, %r5, -8*/ /*DWG*/ /* Subtract two words from count*/ + + cmplw %r8, %r6 /* compare last dword */ + addi %r4, %r4, 4 + bne- all_done + + cmplw %r9, %r7 + addi %r11, %r11, 4 + bne- all_done + +bytebybyte: + + /* We've gotten close to a page boundary: do a byte-byte-byte + * compare for the following 8 bytes, and then go back to + * the full-word compare loop. + */ + + li %r3, 8 /* loop count */ + cmpw %r3, %r5 /* take min(8, counter) */ + ble f2 + + mr. %r3, %r5 + + beqlr + +f2: + + mtctr %r3 + subf %r5, %r3, %r5 /* adjust counter */ + +bbb: + + lbz %r6, 0(%r11) /* byte copy loop */ + + addi %r11, %r11, 1 + + lbz %r8, 0(%r4) + + addi %r4, %r4, 1 + + cmplw %r8, %r6 + + bdnzt+ eq, bbb + + bne all_done + + cmpwi %r5, 0 + bgt again /* handle the rest */ + + xor %r3,%r3,%r3 + + blr + +#if 0 /* Removed code section. DWG */ +partial: + + mr. %r3, %r5 + + beqlr /* If count -> 0, we're done */ + +f1: + + subfic %r3, %r3, 4 /* zero/end in first word? */ + cmpwi %r3, 0 + blt last4 +#endif /* DWG */ + +first4: + subfic %r3, %r5, 4 /* If count <= 4, handle */ + rlwinm %r3, %r3, 3, 0, 31 /* count *= 8 */ + srw %r6, %r6, %r3 /* align 1st buffer 1 word */ + srw %r8, %r8, %r3 /* align 1st buffer 2 word */ + + cmplw %r8, %r6 /* get result */ + bne all_done + xor %r3,%r3,%r3 + blr + +last4: + subfic %r10, %r5, 8 /*DWG*/ + rlwinm %r10, %r10, 3, 0, 31 /* count *= 8 */ + srw %r7, %r7, %r10 /* align 2nd buffer 1 word */ + srw %r9, %r9, %r10 /* align 2nd buffer 2 word */ + + cmplw %r9, %r7 /* get result */ + bne all_done +ret_0: + xor %r3,%r3,%r3 /* Equal result */ + blr + +all_done: + + blt finish_lt + + addi %r3,0,-1 /* Less than result */ + + blr + +finish_lt: + + addi %r3,0,1 /* Greater than result */ + + blr +END(memcmp) diff --git a/common/lib/libc/arch/powerpc/string/memcpy.S b/common/lib/libc/arch/powerpc/string/memcpy.S new file mode 100644 index 000000000..b987961e9 --- /dev/null +++ b/common/lib/libc/arch/powerpc/string/memcpy.S @@ -0,0 +1,117 @@ +/* $NetBSD: memcpy.S,v 1.3 2011/01/15 07:31:12 matt Exp $ */ + +/* stropt/memcpy_440.S, pl_string_common, pl_linux 10/11/04 11:45:36 + * ========================================================================== + * Optimized memcpy implementation for IBM PowerPC 440. + * + * Copyright (c) 2003, IBM Corporation + * 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. + * * Neither the name of IBM 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 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 OWNER 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. + * + * ========================================================================== + * + * Function: Copy n bytes of the source to the destination. Behavior is + * undefined for objects that overlap. + * + * + * void *memcpy(void * dest, const void * src, int n) + * + * Input: r3 - destination address + * r4 - source address + * r5 - byte count + * Output: r3 - destination address + * + * ========================================================================== + */ + +#include + + .text + .align 4 +/* LINTSTUB: Func: void *memcpy(void *, const void *, size_t) */ +ENTRY(memcpy) + /* + * Check count passed in R5. If zero, return; otherwise continue. + */ + cmpwi %r5,0 + beqlr- + + mr %r8, %r3 /* Copy dst (return value) */ + + addi %r4, %r4, -4 /* Prepare for main loop's auto */ + addi %r8, %r8, -4 /* update */ + + srwi. %r9,%r5,2 /* Word count -> r9 */ + beq- last1 /* Partial copy if <4 bytes */ + + mtctr %r9 /* Word cnt in CTR for loop */ + lwzu %r7, 4(%r4) /* Preload for main loop */ + + b g1 + +g0: /* Main loop */ + + lwzu %r7, 4(%r4) /* Load a new word */ + stwu %r6, 4(%r8) /* Store previous word */ + +g1: + + bdz- last /* Dec ctr and exit loop if no */ + /* more words */ + lwzu %r6, 4(%r4) /* Load another word */ + stwu %r7, 4(%r8) /* Store previous word */ + bdnz+ g0 /* Dec ctr and continue loop if */ + /* more words */ + + mr %r7, %r6 + +last: + + stwu %r7, 4(%r8) /* Store last word */ + +last1: /* Byte-by-byte copy */ + + clrlwi. %r5,%r5,30 + beqlr + + mtctr %r5 + + lbzu %r6, 4(%r4) /* 1st byte: update by word */ + stbu %r6, 4(%r8) + bdzlr- + +last2: + + lbzu %r6, 1(%r4) /* Handle the rest */ + stbu %r6, 1(%r8) + bdnz+ last2 + + blr +END(memcpy) diff --git a/common/lib/libc/arch/powerpc/string/memmove.S b/common/lib/libc/arch/powerpc/string/memmove.S new file mode 100644 index 000000000..cd676ca92 --- /dev/null +++ b/common/lib/libc/arch/powerpc/string/memmove.S @@ -0,0 +1,182 @@ +/* $NetBSD: memmove.S,v 1.3 2011/01/15 07:31:12 matt Exp $ */ + +/* stropt/memmove.S, pl_string_common, pl_linux 10/11/04 11:45:37 + * ========================================================================== + * Optimized memmove implementation for IBM PowerPC 405/440. + * + * Copyright (c) 2003, IBM Corporation + * 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. + * * Neither the name of IBM 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 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 OWNER 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. + * + * ========================================================================== + * + * Function: Move memory area (handles overlapping regions) + * + * void *memmove(void * dest, const void * src, int n) + * + * Input: r3 - destination address + * r4 - source address + * r5 - byte count + * Output: r3 - destination address + * + * ========================================================================== + */ + +#include + + .text + .align 4 +#ifdef _BCOPY +/* bcopy = memcpy/memmove with arguments reversed. */ +/* LINTSTUB: Func: void bcopy(void *, void *, size_t) */ +ENTRY(bcopy) + mr %r6, %r3 /* swap src/dst */ + mr %r3, %r4 + mr %r4, %r6 +#else +/* LINTSTUB: Func: void *memmove(void *, const void *, size_t) */ +ENTRY(memmove) +#endif + + mr %r8, %r3 /* Save dst (return value) */ + + cmpw %r4, %r8 /* Branch to reverse if */ + blt reverse /* src < dest. Don't want to */ + /* overwrite end of src with */ + /* start of dest */ + + addi %r4, %r4, -4 /* Back up src and dst pointers */ + addi %r8, %r8, -4 /* due to auto-update of 'load' */ + + srwi. %r9,%r5,2 /* How many words in total cnt */ + beq- last1 /* Handle byte by byte if < 4 */ + /* bytes total */ + mtctr %r9 /* Count of words for loop */ + lwzu %r7, 4(%r4) /* Preload first word */ + + b g1 + +g0: /* Main loop */ + + lwzu %r7, 4(%r4) /* Load a new word */ + stwu %r6, 4(%r8) /* Store previous word */ + +g1: + + bdz- last /* Dec cnt, and branch if just */ + /* one word to store */ + lwzu %r6, 4(%r4) /* Load another word */ + stwu %r7, 4(%r8) /* Store previous word */ + bdnz+ g0 /* Dec cnt, and loop again if */ + /* more words */ + mr %r7, %r6 /* If word count -> 0, then... */ + +last: + + stwu %r7, 4(%r8) /* ... store last word */ + +last1: /* Byte-by-byte copy */ + + clrlwi. %r5,%r5,30 /* If count -> 0, then ... */ + beqlr /* we're done */ + + mtctr %r5 /* else load count for loop */ + + lbzu %r6, 4(%r4) /* 1st byte: update addr by 4 */ + stbu %r6, 4(%r8) /* since we pre-adjusted by 4 */ + bdzlr- /* in anticipation of main loop */ + +last2: + + lbzu %r6, 1(%r4) /* But handle the rest by */ + stbu %r6, 1(%r8) /* updating addr by 1 */ + bdnz+ last2 + + blr + + /* We're here since src < dest. Don't want to overwrite end of */ + /* src with start of dest */ + +reverse: + + add %r4, %r4, %r5 /* Work from end to beginning */ + add %r8, %r8, %r5 /* so add count to string ptrs */ + srwi. %r9,%r5,2 /* Words in total count */ + beq- rlast1 /* Handle byte by byte if < 4 */ + /* bytes total */ + + mtctr %r9 /* Count of words for loop */ + + lwzu %r7, -4(%r4) /* Preload first word */ + b rg1 + +rg0: /* Main loop */ + + lwzu %r7, -4(%r4) /* Load a new word */ + stwu %r6, -4(%r8) /* Store previous word */ + +rg1: + + bdz- rlast /* Dec cnt, and branch if just */ + /* one word to store */ + + lwzu %r6, -4(%r4) /* Load another word */ + stwu %r7, -4(%r8) /* Store previous word */ + + bdnz+ rg0 /* Dec cnt, and loop again if */ + /* more words */ + + mr %r7, %r6 /* If word count -> 0, then... */ + +rlast: + + stwu %r7, -4(%r8) /* ... store last word */ + +rlast1: /* Byte-by-byte copy */ + + clrlwi. %r5,%r5,30 /* If count -> 0, then... */ + beqlr /* ... we're done */ + + mtctr %r5 /* else load count for loop */ + +rlast2: + + lbzu %r6, -1(%r4) /* Handle the rest, byte by */ + stbu %r6, -1(%r8) /* byte */ + + bdnz+ rlast2 /* Dec ctr, and branch if more */ + /* bytes left */ + blr + +#ifdef _BCOPY +END(bcopy) +#else +END(memmove) +#endif diff --git a/common/lib/libc/arch/powerpc/string/strlen.S b/common/lib/libc/arch/powerpc/string/strlen.S new file mode 100644 index 000000000..e822e89bc --- /dev/null +++ b/common/lib/libc/arch/powerpc/string/strlen.S @@ -0,0 +1,118 @@ +/* $NetBSD: strlen.S,v 1.6 2011/01/15 07:31:12 matt Exp $ */ + +/*- + * Copyright (C) 2001 Martin J. Laubach + * 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 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. + */ +/*----------------------------------------------------------------------*/ + +#include + +__RCSID("$NetBSD: strlen.S,v 1.6 2011/01/15 07:31:12 matt Exp $"); + +/*----------------------------------------------------------------------*/ +/* The algorithm here uses the following techniques: + + 1) Given a word 'x', we can test to see if it contains any 0 bytes + by subtracting 0x01010101, and seeing if any of the high bits of each + byte changed from 0 to 1. This works because the least significant + 0 byte must have had no incoming carry (otherwise it's not the least + significant), so it is 0x00 - 0x01 == 0xff. For all other + byte values, either they have the high bit set initially, or when + 1 is subtracted you get a value in the range 0x00-0x7f, none of which + have their high bit set. The expression here is + (x + 0xfefefeff) & ~(x | 0x7f7f7f7f), which gives 0x00000000 when + there were no 0x00 bytes in the word. + + 2) Given a word 'x', we can test to see _which_ byte was zero by + calculating ~(((x & 0x7f7f7f7f) + 0x7f7f7f7f) | x | 0x7f7f7f7f). + This produces 0x80 in each byte that was zero, and 0x00 in all + the other bytes. The '| 0x7f7f7f7f' clears the low 7 bits in each + byte, and the '| x' part ensures that bytes with the high bit set + produce 0x00. The addition will carry into the high bit of each byte + iff that byte had one of its low 7 bits set. We can then just see + which was the most significant bit set and divide by 8 to find how + many to add to the index. + This is from the book 'The PowerPC Compiler Writer's Guide', + by Steve Hoxey, Faraydon Karim, Bill Hay and Hank Warren. +*/ +/*----------------------------------------------------------------------*/ + + .text + .align 4 + +ENTRY(strlen) + + /* Setup constants */ + lis %r10, 0x7f7f + lis %r9, 0xfefe + ori %r10, %r10, 0x7f7f + ori %r9, %r9, 0xfeff + + /* Mask out leading bytes on non aligned strings */ + rlwinm. %r8, %r3, 3, 27, 28 /* leading bits to mask */ +#ifdef _LP64 + clrrdi %r5, %r3, 2 /* clear low 2 addr bits */ +#else + clrrwi %r5, %r3, 2 /* clear low 2 addr bits */ +#endif + li %r0, -1 + beq+ 3f /* skip alignment if already */ + /* aligned */ + + srw %r0, %r0, %r8 /* make 0000...1111 mask */ + + lwz %r7, 0(%r5) + nor %r0, %r0, %r0 /* invert mask */ + or %r7, %r7, %r0 /* make leading bytes != 0 */ + b 2f + +3: subi %r5, %r5, 4 + +1: lwzu %r7, 4(%r5) /* fetch data word */ + +2: nor %r0, %r7, %r10 /* do step 1 */ + add %r6, %r7, %r9 + and. %r0, %r0, %r6 + + beq+ 1b /* no NUL bytes here */ + + and %r8, %r7, %r10 /* ok, a NUL is somewhere */ + or %r7, %r7, %r10 /* do step 2 to find out */ + add %r0, %r8, %r10 /* where */ + nor %r8, %r7, %r0 + + cntlzw %r0, %r8 /* offset from this word */ + srwi %r4, %r0, 3 + + add %r4, %r5, %r4 /* r4 contains end pointer */ + /* NOTE: Keep it so this function returns the end pointer + in r4, so we can it use from other str* calls (strcat + comes to mind */ + + subf %r3, %r3, %r4 + blr +END(strlen) +/*----------------------------------------------------------------------*/ diff --git a/common/lib/libc/arch/powerpc64/atomic/Makefile.inc b/common/lib/libc/arch/powerpc64/atomic/Makefile.inc new file mode 100644 index 000000000..bc058908e --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/Makefile.inc @@ -0,0 +1,15 @@ +# $NetBSD: Makefile.inc,v 1.6 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add.S atomic_and.S atomic_cas.S atomic_dec.S \ + atomic_inc.S atomic_or.S atomic_swap.S membar_ops.S + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_cas.c + +.endif diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_add.S b/common/lib/libc/arch/powerpc64/atomic/atomic_add.S new file mode 100644 index 000000000..c8345e2e9 --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_add.S @@ -0,0 +1,61 @@ +/* $NetBSD: atomic_add.S,v 1.5 2009/03/08 12:08:19 he Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#include "../../powerpc/atomic/atomic_add.S" + + .text + +ENTRY(_atomic_add_64) +1: ldarx %r10,0,%r3 + add %r10,%r10,%r4 + stdcx. %r10,0,%r3 + bne- 1b + blr +ATOMIC_OP_ALIAS(atomic_add_64,_atomic_add_64) +ATOMIC_OP_ALIAS(atomic_add_long,_atomic_add_64) +STRONG_ALIAS(_atomic_add_long,_atomic_add_64) +ATOMIC_OP_ALIAS(atomic_add_ptr,_atomic_add_64) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_64) + +ENTRY(_atomic_add_64_nv) +1: ldarx %r10,0,%r3 + add %r10,%r10,%r4 + stdcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +ATOMIC_OP_ALIAS(atomic_add_64_nv,_atomic_add_64_nv) +ATOMIC_OP_ALIAS(atomic_add_long_nv,_atomic_add_64_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_64_nv) +ATOMIC_OP_ALIAS(atomic_add_ptr_nv,_atomic_add_64_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_64_nv) diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_and.S b/common/lib/libc/arch/powerpc64/atomic/atomic_and.S new file mode 100644 index 000000000..ad2158266 --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_and.S @@ -0,0 +1,57 @@ +/* $NetBSD: atomic_and.S,v 1.5 2009/03/08 12:08:19 he Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#include "../../powerpc/atomic/atomic_and.S" + + .text + +ENTRY(_atomic_and_64) +1: ldarx %r10,0,%r3 + and %r10,%r10,%r4 + stdcx. %r10,0,%r3 + bne- 1b + blr +ATOMIC_OP_ALIAS(atomic_and_64,_atomic_and_64) +ATOMIC_OP_ALIAS(atomic_and_ulong,_atomic_and_64) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_64) + +ENTRY(_atomic_and_64_nv) +1: ldarx %r10,0,%r3 + and %r10,%r10,%r4 + stdcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +ATOMIC_OP_ALIAS(atomic_and_64_nv,_atomic_and_64_nv) +ATOMIC_OP_ALIAS(atomic_and_ulong_nv,_atomic_and_64_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_64_nv) diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_cas.S b/common/lib/libc/arch/powerpc64/atomic/atomic_cas.S new file mode 100644 index 000000000..fbebd0374 --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_cas.S @@ -0,0 +1,58 @@ +/* $NetBSD: atomic_cas.S,v 1.6 2009/03/08 12:08:19 he Exp $ */ + +/*- + * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#include "../../powerpc/atomic/atomic_cas.S" + + .text + +ENTRY(_atomic_cas_64) +1: ldarx %r10,0,%r3 + cmpd %r10,%r4 + bne- 2f + stdcx. %r5,0,%r3 + bne- 1b +2: mr %r3,%r10 + blr + +ATOMIC_OP_ALIAS(atomic_cas_64,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ulong,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ptr,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_64) + +ATOMIC_OP_ALIAS(atomic_cas_64_ni,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_64_ni,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ulong_ni,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_64) +ATOMIC_OP_ALIAS(atomic_cas_ptr_ni,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_64) diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_dec.S b/common/lib/libc/arch/powerpc64/atomic/atomic_dec.S new file mode 100644 index 000000000..3c6d0466a --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_dec.S @@ -0,0 +1,61 @@ +/* $NetBSD: atomic_dec.S,v 1.5 2009/03/08 12:08:19 he Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#include "../../powerpc/atomic/atomic_dec.S" + + .text + +ENTRY(_atomic_dec_64) +1: ldarx %r10,0,%r3 + addi %r10,%r10,-1 + stdcx. %r10,0,%r3 + bne- 1b + blr +ATOMIC_OP_ALIAS(atomic_dec_64,_atomic_dec_64) +ATOMIC_OP_ALIAS(atomic_dec_ulong,_atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_64) +ATOMIC_OP_ALIAS(atomic_dec_ptr,_atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_64) + +ENTRY(_atomic_dec_64_nv) +1: ldarx %r10,0,%r3 + addi %r10,%r10,-1 + stdcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +ATOMIC_OP_ALIAS(atomic_dec_64_nv,_atomic_dec_64_nv) +ATOMIC_OP_ALIAS(atomic_dec_ulong_nv,_atomic_dec_64_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_64_nv) +ATOMIC_OP_ALIAS(atomic_dec_ptr_nv,_atomic_dec_64_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_64_nv) diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_inc.S b/common/lib/libc/arch/powerpc64/atomic/atomic_inc.S new file mode 100644 index 000000000..962c0f6ba --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_inc.S @@ -0,0 +1,61 @@ +/* $NetBSD: atomic_inc.S,v 1.5 2009/03/08 12:08:19 he Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#include "../../powerpc/atomic/atomic_inc.S" + + .text + +ENTRY(_atomic_inc_64) +1: ldarx %r10,0,%r3 + addi %r10,%r10,1 + stdcx. %r10,0,%r3 + bne- 1b + blr +ATOMIC_OP_ALIAS(atomic_inc_64,_atomic_inc_64) +ATOMIC_OP_ALIAS(atomic_inc_ulong,_atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_64) +ATOMIC_OP_ALIAS(atomic_inc_ptr,_atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_64) + +ENTRY(_atomic_inc_64_nv) +1: ldarx %r10,0,%r3 + addi %r10,%r10,1 + stdcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +ATOMIC_OP_ALIAS(atomic_inc_64_nv,_atomic_inc_64_nv) +ATOMIC_OP_ALIAS(atomic_inc_ulong_nv,_atomic_inc_64_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_64_nv) +ATOMIC_OP_ALIAS(atomic_inc_ptr_nv,_atomic_inc_64_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_64_nv) diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_op_asm.h b/common/lib/libc/arch/powerpc64/atomic/atomic_op_asm.h new file mode 100644 index 000000000..34a15b652 --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_op_asm.h @@ -0,0 +1,48 @@ +/* $NetBSD: atomic_op_asm.h,v 1.4 2010/03/22 02:22:32 mrg Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#define _NOREGNAMES +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#endif /* _KERNEL */ + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_or.S b/common/lib/libc/arch/powerpc64/atomic/atomic_or.S new file mode 100644 index 000000000..eee40279d --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_or.S @@ -0,0 +1,57 @@ +/* $NetBSD: atomic_or.S,v 1.5 2009/03/08 12:08:19 he Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#include "../../powerpc/atomic/atomic_or.S" + + .text + +ENTRY(_atomic_or_64) +1: ldarx %r10,0,%r3 + or %r10,%r10,%r4 + stdcx. %r10,0,%r3 + bne- 1b + blr +ATOMIC_OP_ALIAS(atomic_or_64,_atomic_or_64) +ATOMIC_OP_ALIAS(atomic_or_ulong,_atomic_or_64) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_64) + +ENTRY(_atomic_or_64_nv) +1: ldarx %r10,0,%r3 + or %r10,%r10,%r4 + stdcx. %r10,0,%r3 + bne- 1b + mr %r3,%r10 + blr +ATOMIC_OP_ALIAS(atomic_or_64_nv,_atomic_or_64_nv) +ATOMIC_OP_ALIAS(atomic_or_ulong_nv,_atomic_or_64_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_64_nv) diff --git a/common/lib/libc/arch/powerpc64/atomic/atomic_swap.S b/common/lib/libc/arch/powerpc64/atomic/atomic_swap.S new file mode 100644 index 000000000..bba9800d9 --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/atomic_swap.S @@ -0,0 +1,48 @@ +/* $NetBSD: atomic_swap.S,v 1.5 2009/03/08 12:08:19 he Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#include "../../powerpc/atomic/atomic_swap.S" + + .text + +ENTRY(_atomic_swap_64) +1: ldarx %r10,0,%r3 + stdcx. %r4,0,%r3 + bne- 1b + mr %r3,%r10 + blr +ATOMIC_OP_ALIAS(atomic_swap_64,_atomic_swap_64) +ATOMIC_OP_ALIAS(atomic_swap_ulong,_atomic_swap_64) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_64) +ATOMIC_OP_ALIAS(atomic_swap_ptr,_atomic_swap_64) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_64) diff --git a/common/lib/libc/arch/powerpc64/atomic/membar_ops.S b/common/lib/libc/arch/powerpc64/atomic/membar_ops.S new file mode 100644 index 000000000..680520751 --- /dev/null +++ b/common/lib/libc/arch/powerpc64/atomic/membar_ops.S @@ -0,0 +1,3 @@ +/* $NetBSD: membar_ops.S,v 1.1 2007/11/28 21:47:02 ad Exp $ */ + +#include "../../powerpc/atomic/membar_ops.S" diff --git a/common/lib/libc/arch/powerpc64/string/ffs.S b/common/lib/libc/arch/powerpc64/string/ffs.S new file mode 100644 index 000000000..4cce87c52 --- /dev/null +++ b/common/lib/libc/arch/powerpc64/string/ffs.S @@ -0,0 +1,3 @@ +/* $NetBSD: ffs.S,v 1.1 2006/07/07 07:37:44 ross Exp $ */ + +#include "../../powerpc/string/ffs.S" diff --git a/common/lib/libc/arch/powerpc64/string/strlen.S b/common/lib/libc/arch/powerpc64/string/strlen.S new file mode 100644 index 000000000..871907b5c --- /dev/null +++ b/common/lib/libc/arch/powerpc64/string/strlen.S @@ -0,0 +1,5 @@ +/* $NetBSD: strlen.S,v 1.1 2006/07/07 07:37:44 ross Exp $ */ + +/* XXX could be optimized for ppc64 */ + +#include "../../powerpc/string/strlen.S" diff --git a/common/lib/libc/arch/sh3/atomic/Makefile.inc b/common/lib/libc/arch/sh3/atomic/Makefile.inc new file mode 100644 index 000000000..1bafd20d6 --- /dev/null +++ b/common/lib/libc/arch/sh3/atomic/Makefile.inc @@ -0,0 +1,17 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c atomic_and_32_cas.c \ + atomic_and_32_nv_cas.c atomic_dec_32_cas.c atomic_dec_32_nv_cas.c \ + atomic_inc_32_cas.c atomic_inc_32_nv_cas.c atomic_or_32_cas.c \ + atomic_or_32_nv_cas.c atomic_swap_32_cas.c membar_ops_nop.c + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_testset.c + +.endif diff --git a/common/lib/libc/arch/sh3/string/ffs.S b/common/lib/libc/arch/sh3/string/ffs.S new file mode 100644 index 000000000..f7189bea7 --- /dev/null +++ b/common/lib/libc/arch/sh3/string/ffs.S @@ -0,0 +1,96 @@ +/* $NetBSD: ffs.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by ITOH Yasufumi. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: ffs.S,v 1.2 2008/04/28 20:22:52 martin Exp $") +#endif + +/* + * ffs - find first bit set + * + * This code makes use of ``test 8bit'' and ``shift 8bit'' instructions. + * The remaining 8bit is tested in every 2bit. + */ + +ENTRY(ffs) + mov r4,r0 ! using r0 specific instructions + tst #0xff,r0 + bf/s L8bit + mov #0+1,r1 ! ret = 1..8 + + tst r0,r0 ! ffs(0) is 0 + bt Lzero ! testing here to accelerate ret=1..8 cases + + shlr8 r0 + tst #0xff,r0 + bf/s L8bit + mov #8+1,r1 ! ret = 9..16 + + shlr8 r0 + tst #0xff,r0 + bf/s L8bit + mov #16+1,r1 ! ret = 17..24 + + shlr8 r0 + mov #24+1,r1 ! ret = 25..32 + +L8bit: + tst #0x0f,r0 + bt 4f + + tst #0x03,r0 + bt 2f + tst #0x01,r0 ! not bit 0 -> T + mov #0,r0 + rts + addc r1,r0 ! 0 + r1 + T -> r0 + +2: tst #0x04,r0 + mov #2,r0 + rts + addc r1,r0 + +4: tst #0x30,r0 + bt 6f + tst #0x10,r0 + mov #4,r0 + rts + addc r1,r0 + +6: tst #0x40,r0 + mov #6,r0 + rts + addc r1,r0 + +Lzero: rts + nop diff --git a/common/lib/libc/arch/sh3/string/memcpy.S b/common/lib/libc/arch/sh3/string/memcpy.S new file mode 100644 index 000000000..aca97dd34 --- /dev/null +++ b/common/lib/libc/arch/sh3/string/memcpy.S @@ -0,0 +1,271 @@ +/* $NetBSD: memcpy.S,v 1.2 2006/04/22 23:53:47 uwe Exp $ */ + +/* + * Copyright (c) 2000 SHIMIZU Ryo + * 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 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. + */ + +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: memcpy.S,v 1.2 2006/04/22 23:53:47 uwe Exp $") +#endif + +#if !defined(MEMCOPY) && !defined(MEMMOVE) && !defined(BCOPY) +#define MEMCOPY +#endif + +#if defined(MEMCOPY) || defined(MEMMOVE) +#define REG_DST0 r3 +#define REG_SRC r5 +#define REG_DST r4 +#else +#define REG_SRC r4 +#define REG_DST r5 +#endif + +#define REG_LEN r6 + +#if defined(MEMCOPY) +ENTRY(memcpy) +#elif defined(MEMMOVE) +ENTRY(memmove) +#elif defined(BCOPY) +ENTRY(bcopy) +#endif +#ifdef REG_DST0 + mov REG_DST,REG_DST0 +#endif + cmp/eq REG_DST,REG_SRC /* if ( src == dst ) return; */ + bt/s bcopy_return + cmp/hi REG_DST,REG_SRC + bf/s bcopy_overlap + + mov REG_SRC,r0 + xor REG_DST,r0 + and #3,r0 + mov r0,r1 + tst r0,r0 /* (src ^ dst) & 3 */ + bf/s word_align + +longword_align: + tst REG_LEN,REG_LEN /* if ( len==0 ) return; */ + bt/s bcopy_return + + + mov REG_SRC,r0 + tst #1,r0 /* if ( src & 1 ) */ + bt 1f + mov.b @REG_SRC+,r0 /* *dst++ = *src++; */ + add #-1,REG_LEN + mov.b r0,@REG_DST + add #1,REG_DST +1: + + + mov #1,r0 + cmp/hi r0,REG_LEN /* if ( (len > 1) && */ + bf/s 1f + mov REG_SRC,r0 + tst #2,r0 /* (src & 2) { */ + bt 1f + mov.w @REG_SRC+,r0 /* *((unsigned short*)dst)++ = *((unsigned short*)src)++; */ + add #-2,REG_LEN /* len -= 2; */ + mov.w r0,@REG_DST + add #2,REG_DST /* } */ +1: + + + mov #3,r1 + cmp/hi r1,REG_LEN /* while ( len > 3 ) { */ + bf/s no_align_delay + tst REG_LEN,REG_LEN +2: + mov.l @REG_SRC+,r0 /* *((unsigned long*)dst)++ = *((unsigned long*)src)++; */ + add #-4,REG_LEN /* len -= 4; */ + mov.l r0,@REG_DST + cmp/hi r1,REG_LEN + bt/s 2b + add #4,REG_DST /* } */ + + bra no_align_delay + tst REG_LEN,REG_LEN + + +word_align: + mov r1,r0 + tst #1,r0 + bf/s no_align_delay + tst REG_LEN,REG_LEN /* if ( len == 0 ) return; */ + bt bcopy_return + + + mov REG_SRC,r0 /* if ( src & 1 ) */ + tst #1,r0 + bt 1f + mov.b @REG_SRC+,r0 /* *dst++ = *src++; */ + add #-1,REG_LEN + mov.b r0,@REG_DST + add #1,REG_DST +1: + + + mov #1,r1 + cmp/hi r1,REG_LEN /* while ( len > 1 ) { */ + bf/s no_align_delay + tst REG_LEN,REG_LEN +2: + mov.w @REG_SRC+,r0 /* *((unsigned short*)dst)++ = *((unsigned short*)src)++; */ + add #-2,REG_LEN /* len -= 2; */ + mov.w r0,@REG_DST + cmp/hi r1,REG_LEN + bt/s 2b + add #2,REG_DST /* } */ + + +no_align: + tst REG_LEN,REG_LEN /* while ( len!= ) { */ +no_align_delay: + bt bcopy_return +1: + mov.b @REG_SRC+,r0 /* *dst++ = *src++; */ + add #-1,REG_LEN /* len--; */ + mov.b r0,@REG_DST + tst REG_LEN,REG_LEN + bf/s 1b + add #1,REG_DST /* } */ +bcopy_return: + rts +#ifdef REG_DST0 + mov REG_DST0,r0 +#else + nop +#endif + + +bcopy_overlap: + add REG_LEN,REG_SRC + add REG_LEN,REG_DST + + mov REG_SRC,r0 + xor REG_DST,r0 + and #3,r0 + mov r0,r1 + tst r0,r0 /* (src ^ dst) & 3 */ + bf/s ov_word_align + +ov_longword_align: + tst REG_LEN,REG_LEN /* if ( len==0 ) return; */ + bt/s bcopy_return + + + mov REG_SRC,r0 + tst #1,r0 /* if ( src & 1 ) */ + bt 1f + add #-1,REG_SRC /* *--dst = *--src; */ + mov.b @REG_SRC,r0 + mov.b r0,@-REG_DST + add #-1,REG_LEN +1: + + + mov #1,r0 + cmp/hi r0,REG_LEN /* if ( (len > 1) && */ + bf/s 1f + mov REG_SRC,r0 + tst #2,r0 /* (src & 2) { */ + bt 1f + add #-2,REG_SRC /* *--((unsigned short*)dst) = *--((unsigned short*)src); */ + mov.w @REG_SRC,r0 + add #-2,REG_LEN /* len -= 2; */ + mov.w r0,@-REG_DST /* } */ +1: + + + mov #3,r1 + cmp/hi r1,REG_LEN /* while ( len > 3 ) { */ + bf/s ov_no_align_delay + tst REG_LEN,REG_LEN +2: + add #-4,REG_SRC + mov.l @REG_SRC,r0 /* *((unsigned long*)dst)++ = *((unsigned long*)src)++; */ + add #-4,REG_LEN /* len -= 4; */ + cmp/hi r1,REG_LEN + bt/s 2b + mov.l r0,@-REG_DST /* } */ + + bra ov_no_align_delay + tst REG_LEN,REG_LEN + + +ov_word_align: + mov r1,r0 + tst #1,r0 + bf/s ov_no_align_delay + tst REG_LEN,REG_LEN /* if ( len == 0 ) return; */ + bt bcopy_return + + + mov REG_SRC,r0 /* if ( src & 1 ) */ + tst #1,r0 + bt 1f + add #-1,REG_SRC + mov.b @REG_SRC,r0 /* *--dst = *--src; */ + add #-1,REG_LEN + mov.b r0,@-REG_DST +1: + + + mov #1,r1 + cmp/hi r1,REG_LEN /* while ( len > 1 ) { */ + bf/s ov_no_align_delay + tst REG_LEN,REG_LEN +2: + add #-2,REG_SRC + mov.w @REG_SRC,r0 /* *--((unsigned short*)dst) = *--((unsigned short*)src); */ + add #-2,REG_LEN /* len -= 2; */ + cmp/hi r1,REG_LEN + bt/s 2b + mov.w r0,@-REG_DST /* } */ + + +ov_no_align: + tst REG_LEN,REG_LEN /* while ( len!= ) { */ +ov_no_align_delay: + bt 9f +1: + add #-1,REG_SRC + mov.b @REG_SRC,r0 /* *--dst = *--src; */ + add #-1,REG_LEN /* len--; */ + tst REG_LEN,REG_LEN + bf/s 1b + mov.b r0,@-REG_DST /* } */ +9: + rts +#ifdef REG_DST0 + mov REG_DST0,r0 +#else + nop +#endif diff --git a/common/lib/libc/arch/sh3/string/memmove.S b/common/lib/libc/arch/sh3/string/memmove.S new file mode 100644 index 000000000..0ccd67bbe --- /dev/null +++ b/common/lib/libc/arch/sh3/string/memmove.S @@ -0,0 +1,4 @@ +/* $NetBSD: memmove.S,v 1.2 2006/04/22 23:53:47 uwe Exp $ */ + +#define MEMMOVE +#include "memcpy.S" diff --git a/common/lib/libc/arch/sh3/string/memset.S b/common/lib/libc/arch/sh3/string/memset.S new file mode 100644 index 000000000..c0ab17156 --- /dev/null +++ b/common/lib/libc/arch/sh3/string/memset.S @@ -0,0 +1,298 @@ +/* $NetBSD: memset.S,v 1.2 2008/02/16 17:37:13 apb Exp $ */ + +/*- + * Copyright (c) 2002 SHIMIZU Ryo. 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 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. + */ + +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: memset.S,v 1.2 2008/02/16 17:37:13 apb Exp $") +#endif + +#define REG_PTR r0 +#define REG_TMP1 r1 + +#ifdef BZERO +# define REG_C r2 +# define REG_DST r4 +# define REG_LEN r5 +#else +# define REG_DST0 r3 +# define REG_DST r4 +# define REG_C r5 +# define REG_LEN r6 +#endif + +#ifdef BZERO +ENTRY(bzero) +#else +ENTRY(memset) + mov REG_DST,REG_DST0 /* for return value */ +#endif + /* small amount to fill ? */ + mov #28,REG_TMP1 + cmp/hs REG_TMP1,REG_LEN /* if (len >= 28) goto large; */ + bt/s large + mov #12,REG_TMP1 /* if (len >= 12) goto small; */ + cmp/hs REG_TMP1,REG_LEN + bt/s small +#ifdef BZERO + mov #0,REG_C +#endif + /* very little fill (0 ~ 11 bytes) */ + tst REG_LEN,REG_LEN + add REG_DST,REG_LEN + bt/s done + add #1,REG_DST + + /* unroll 4 loops */ + cmp/eq REG_DST,REG_LEN +1: mov.b REG_C,@-REG_LEN + bt/s done + cmp/eq REG_DST,REG_LEN + mov.b REG_C,@-REG_LEN + bt/s done + cmp/eq REG_DST,REG_LEN + mov.b REG_C,@-REG_LEN + bt/s done + cmp/eq REG_DST,REG_LEN + mov.b REG_C,@-REG_LEN + bf/s 1b + cmp/eq REG_DST,REG_LEN +done: +#ifdef BZERO + rts + nop +#else + rts + mov REG_DST0,r0 +#endif + + +small: + mov REG_DST,r0 + tst #1,r0 + bt/s small_aligned + mov REG_DST,REG_TMP1 + shll REG_LEN + mova 1f,r0 /* 1f must be 4bytes aligned! */ + add #16,REG_TMP1 /* REG_TMP1 = dst+16; */ + sub REG_LEN,r0 + jmp @r0 + mov REG_C,r0 + + .align 2 + mov.b r0,@(15,REG_TMP1) + mov.b r0,@(14,REG_TMP1) + mov.b r0,@(13,REG_TMP1) + mov.b r0,@(12,REG_TMP1) + mov.b r0,@(11,REG_TMP1) + mov.b r0,@(10,REG_TMP1) + mov.b r0,@(9,REG_TMP1) + mov.b r0,@(8,REG_TMP1) + mov.b r0,@(7,REG_TMP1) + mov.b r0,@(6,REG_TMP1) + mov.b r0,@(5,REG_TMP1) + mov.b r0,@(4,REG_TMP1) + mov.b r0,@(3,REG_TMP1) + mov.b r0,@(2,REG_TMP1) + mov.b r0,@(1,REG_TMP1) + mov.b r0,@REG_TMP1 + mov.b r0,@(15,REG_DST) + mov.b r0,@(14,REG_DST) + mov.b r0,@(13,REG_DST) + mov.b r0,@(12,REG_DST) + mov.b r0,@(11,REG_DST) + mov.b r0,@(10,REG_DST) + mov.b r0,@(9,REG_DST) + mov.b r0,@(8,REG_DST) + mov.b r0,@(7,REG_DST) + mov.b r0,@(6,REG_DST) + mov.b r0,@(5,REG_DST) + mov.b r0,@(4,REG_DST) + mov.b r0,@(3,REG_DST) + mov.b r0,@(2,REG_DST) + mov.b r0,@(1,REG_DST) +#ifdef BZERO + rts +1: mov.b r0,@REG_DST +#else + mov.b r0,@REG_DST +1: rts + mov REG_DST0,r0 +#endif + + +/* 2 bytes aligned small fill */ +small_aligned: +#ifndef BZERO + extu.b REG_C,REG_TMP1 /* REG_C = ??????xx, REG_TMP1 = ????00xx */ + shll8 REG_C /* REG_C = ????xx00, REG_TMP1 = ????00xx */ + or REG_TMP1,REG_C /* REG_C = ????xxxx */ +#endif + + mov REG_LEN,r0 + tst #1,r0 /* len is aligned? */ + bt/s 1f + add #-1,r0 + mov.b REG_C,@(r0,REG_DST) /* fill last a byte */ + mov r0,REG_LEN +1: + + mova 1f,r0 /* 1f must be 4bytes aligned! */ + sub REG_LEN,r0 + jmp @r0 + mov REG_C,r0 + + .align 2 + mov.w r0,@(30,REG_DST) + mov.w r0,@(28,REG_DST) + mov.w r0,@(26,REG_DST) + mov.w r0,@(24,REG_DST) + mov.w r0,@(22,REG_DST) + mov.w r0,@(20,REG_DST) + mov.w r0,@(18,REG_DST) + mov.w r0,@(16,REG_DST) + mov.w r0,@(14,REG_DST) + mov.w r0,@(12,REG_DST) + mov.w r0,@(10,REG_DST) + mov.w r0,@(8,REG_DST) + mov.w r0,@(6,REG_DST) + mov.w r0,@(4,REG_DST) + mov.w r0,@(2,REG_DST) +#ifdef BZERO + rts +1: mov.w r0,@REG_DST +#else + mov.w r0,@REG_DST +1: rts + mov REG_DST0,r0 +#endif + + + + .align 2 +large: +#ifdef BZERO + mov #0,REG_C +#else + extu.b REG_C,REG_TMP1 /* REG_C = ??????xx, REG_TMP1 = ????00xx */ + shll8 REG_C /* REG_C = ????xx00, REG_TMP1 = ????00xx */ + or REG_C,REG_TMP1 /* REG_C = ????xx00, REG_TMP1 = ????xxxx */ + swap.w REG_TMP1,REG_C /* REG_C = xxxx????, REG_TMP1 = ????xxxx */ + xtrct REG_TMP1,REG_C /* REG_C = xxxxxxxx */ +#endif + + mov #3,REG_TMP1 + tst REG_TMP1,REG_DST + mov REG_DST,REG_PTR + bf/s unaligned_dst + add REG_LEN,REG_PTR /* REG_PTR = dst + len; */ + tst REG_TMP1,REG_LEN + bf/s unaligned_len + +aligned: + /* fill 32*n bytes */ + mov #32,REG_TMP1 + cmp/hi REG_LEN,REG_TMP1 + bt 9f + .align 2 +1: sub REG_TMP1,REG_PTR + mov.l REG_C,@REG_PTR + sub REG_TMP1,REG_LEN + mov.l REG_C,@(4,REG_PTR) + cmp/hi REG_LEN,REG_TMP1 + mov.l REG_C,@(8,REG_PTR) + mov.l REG_C,@(12,REG_PTR) + mov.l REG_C,@(16,REG_PTR) + mov.l REG_C,@(20,REG_PTR) + mov.l REG_C,@(24,REG_PTR) + bf/s 1b + mov.l REG_C,@(28,REG_PTR) +9: + + /* fill left 4*n bytes */ + cmp/eq REG_DST,REG_PTR + bt 9f + add #4,REG_DST + cmp/eq REG_DST,REG_PTR +1: mov.l REG_C,@-REG_PTR + bt/s 9f + cmp/eq REG_DST,REG_PTR + mov.l REG_C,@-REG_PTR + bt/s 9f + cmp/eq REG_DST,REG_PTR + mov.l REG_C,@-REG_PTR + bt/s 9f + cmp/eq REG_DST,REG_PTR + mov.l REG_C,@-REG_PTR + bf/s 1b + cmp/eq REG_DST,REG_PTR +9: +#ifdef BZERO + rts + nop +#else + rts + mov REG_DST0,r0 +#endif + + +unaligned_dst: + mov #1,REG_TMP1 + tst REG_TMP1,REG_DST /* if (dst & 1) { */ + add #1,REG_TMP1 + bt/s 2f + tst REG_TMP1,REG_DST + mov.b REG_C,@REG_DST /* *dst++ = c; */ + add #1,REG_DST + tst REG_TMP1,REG_DST +2: /* } */ + /* if (dst & 2) { */ + bt 4f + mov.w REG_C,@REG_DST /* *(uint16_t*)dst++ = c; */ + add #2,REG_DST +4: /* } */ + + + tst #3,REG_PTR /* if (ptr & 3) { */ + bt/s 4f /* */ +unaligned_len: + tst #1,REG_PTR /* if (ptr & 1) { */ + bt/s 2f + tst #2,REG_PTR + mov.b REG_C,@-REG_PTR /* --ptr = c; */ +2: /* } */ + /* if (ptr & 2) { */ + bt 4f + mov.w REG_C,@-REG_PTR /* *--(uint16_t*)ptr = c; */ +4: /* } */ + /* } */ + + mov REG_PTR,REG_LEN + bra aligned + sub REG_DST,REG_LEN + diff --git a/common/lib/libc/arch/sparc/atomic/Makefile.inc b/common/lib/libc/arch/sparc/atomic/Makefile.inc new file mode 100644 index 000000000..dd8c33b8a --- /dev/null +++ b/common/lib/libc/arch/sparc/atomic/Makefile.inc @@ -0,0 +1,37 @@ +# $NetBSD: Makefile.inc,v 1.14 2009/03/13 19:16:46 abs Exp $ + +.include + +# This is called with MACHINE=sparc64 && MACHINE_ARCH=sparc when building +# 32 bit kernels for ultrasparc hardware +.if ${MACHINE} == "sparc64" + +SPARC64DIR= ${.PARSEDIR}/../../sparc64/atomic +.PATH.c: ${SPARC64DIR} +.PATH.S: ${SPARC64DIR} + +.include "${SPARC64DIR}/Makefile.inc" + +.else + +. if defined(LIB) + +. if (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c atomic_and_32_cas.c \ + atomic_and_32_nv_cas.c atomic_dec_32_cas.c atomic_dec_32_nv_cas.c \ + atomic_inc_32_cas.c atomic_inc_32_nv_cas.c atomic_or_32_cas.c \ + atomic_or_32_nv_cas.c atomic_swap_32_cas.c membar_ops.S +. endif + +. if (${LIB} == "kern" || ${LIB} == "rump") +SRCS+= atomic_cas.S +. endif + +. if (${LIB} == "c" || ${LIB} == "pthread") +SRCS+= atomic_init_testset.c +. endif + +. endif + +.endif diff --git a/common/lib/libc/arch/sparc/atomic/atomic_cas.S b/common/lib/libc/arch/sparc/atomic/atomic_cas.S new file mode 100644 index 000000000..ab0f29308 --- /dev/null +++ b/common/lib/libc/arch/sparc/atomic/atomic_cas.S @@ -0,0 +1,144 @@ +/* $NetBSD: atomic_cas.S,v 1.10 2009/03/13 16:40:22 nakayama Exp $ */ + +/*- + * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Doran and Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + +#if defined(_HARDKERNEL) + +#include + +#include "opt_multiprocessor.h" + +#define DISABLE_INTERRUPTS \ + rd %psr, %o4 /* disable interrupts */;\ + or %o4, PSR_PIL, %o5 ;\ + wr %o5, 0, %psr ;\ + nop ;\ + nop ;\ + nop + +#define RESTORE_INTERRUPTS \ + wr %o4, 0, %psr /* enable interrupts */ ;\ + nop ;\ + nop ;\ + nop + +#else /* _HARDKERNEL */ + +#define MULTIPROCESSOR 1 +#define DISABLE_INTERRUPTS /* nothing */ +#define RESTORE_INTERRUPTS /* nothing */ + +#endif /* _HARDKERNEL */ + +#if defined(MULTIPROCESSOR) + .section .bss + .align 1024 +OTYPE(_C_LABEL(_atomic_cas_locktab)) +_C_LABEL(_atomic_cas_locktab): + .space 1024 + +#define ACQUIRE_INTERLOCK \ + DISABLE_INTERRUPTS ;\ + srl %o0, 3, %o5 /* get lock address */ ;\ + and %o5, 1023, %o5 ;\ + sethi %hi(_C_LABEL(_atomic_cas_locktab)), %o3 ;\ + add %o5, %o3, %o5 ;\ + ;\ + /* %o5 has interlock address */ ;\ + ;\ +1: ldstub [%o5], %o3 /* acquire lock */ ;\ + tst %o3 ;\ + bz,a 2f ;\ + nop ;\ + nop ;\ + nop ;\ + b,a 1b /* spin */ ;\ + nop ;\ + /* We now hold the interlock */ ;\ +2: + +#define RELEASE_INTERLOCK \ + stb %g0, [%o5] /* release interlock */ ;\ + RESTORE_INTERRUPTS + +#else /* ! MULTIPROCESSOR */ + +#define ACQUIRE_INTERLOCK DISABLE_INTERRUPTS + +#define RELEASE_INTERLOCK RESTORE_INTERRUPTS + +#endif /* MULTIPROCESSOR */ + + .text + +/* + * The v7 and v8 SPARC doesn't have compare-and-swap, so we block interrupts + * and use an interlock. + * + * XXX On single CPU systems, this should use a restartable sequence: + * XXX there we don't need the overhead of interlocking. + * + * XXX NOTE! The interlock trick only works if EVERYTHING writes to + * XXX the memory cell through this code path! + */ +ENTRY(_atomic_cas_32) + ACQUIRE_INTERLOCK + ! %o4 has saved PSR value + ! %o5 has interlock address + + ld [%o0], %o3 ! get old value + cmp %o1, %o3 ! old == new? + beq,a 3f ! yes, do the store + st %o2, [%o0] ! (in the delay slot) + +3: RELEASE_INTERLOCK + + retl + mov %o3, %o0 ! return old value + +ATOMIC_OP_ALIAS(atomic_cas_32,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ulong,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ptr,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_32_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_32_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ulong_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_ptr_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_32) diff --git a/common/lib/libc/arch/sparc/atomic/atomic_op_asm.h b/common/lib/libc/arch/sparc/atomic/atomic_op_asm.h new file mode 100644 index 000000000..f7472d6a0 --- /dev/null +++ b/common/lib/libc/arch/sparc/atomic/atomic_op_asm.h @@ -0,0 +1,65 @@ +/* $NetBSD: atomic_op_asm.h,v 1.6 2011/01/17 18:11:09 joerg Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#ifdef __arch64__ +#define ATOMIC_OP_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _64) +#else +#define ATOMIC_OP_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _32) +#endif + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#ifdef __arch64__ +#define ATOMIC_OP_ALIAS_SIZE(a,s) WEAK_ALIAS(a,s ## _64) +#else +#define ATOMIC_OP_ALIAS_SIZE(a,s) WEAK_ALIAS(a,s ## _32) +#endif + +#endif /* _KERNEL */ + +#ifdef __arch64__ +#define STRONG_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _64) +#else +#define STRONG_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _32) +#endif + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/sparc/atomic/membar_ops.S b/common/lib/libc/arch/sparc/atomic/membar_ops.S new file mode 100644 index 000000000..f3317c83a --- /dev/null +++ b/common/lib/libc/arch/sparc/atomic/membar_ops.S @@ -0,0 +1,55 @@ +/* $NetBSD: membar_ops.S,v 1.4 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe, and by Andrew Doran. + * + * 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 "atomic_op_asm.h" + + .text + +/* These assume Total Store Order (TSO) */ + +ENTRY(_membar_producer) + retl + nop + +ENTRY(_membar_consumer) + add %sp, -112, %sp + st %g0, [%sp+100] + retl + sub %sp, -112, %sp + +ATOMIC_OP_ALIAS(membar_producer,_membar_producer) +ATOMIC_OP_ALIAS(membar_consumer,_membar_consumer) +ATOMIC_OP_ALIAS(membar_enter,_membar_consumer) +STRONG_ALIAS(_membar_enter,_membar_consumer) +ATOMIC_OP_ALIAS(membar_exit,_membar_consumer) +STRONG_ALIAS(_membar_exit,_membar_consumer) +ATOMIC_OP_ALIAS(membar_sync,_membar_consumer) +STRONG_ALIAS(_membar_sync,_membar_consumer) diff --git a/common/lib/libc/arch/sparc/gen/mul.S b/common/lib/libc/arch/sparc/gen/mul.S new file mode 100644 index 000000000..a10a9e6c6 --- /dev/null +++ b/common/lib/libc/arch/sparc/gen/mul.S @@ -0,0 +1,156 @@ +/* $NetBSD: mul.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: mul.s,v 1.5 92/06/25 13:24:03 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)mul.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: mul.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Signed multiply, from Appendix E of the Sparc Version 8 + * Architecture Manual. + * + * Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the upper 32 bits of + * the 64-bit product). + * + * This code optimizes short (less than 13-bit) multiplies. + */ + +FUNC(.mul) + mov %o0, %y ! multiplier -> Y + andncc %o0, 0xfff, %g0 ! test bits 12..31 + be Lmul_shortway ! if zero, can do it the short way + andcc %g0, %g0, %o4 ! zero the partial product and clear N and V + + /* + * Long multiply. 32 steps, followed by a final shift step. + */ + mulscc %o4, %o1, %o4 ! 1 + mulscc %o4, %o1, %o4 ! 2 + mulscc %o4, %o1, %o4 ! 3 + mulscc %o4, %o1, %o4 ! 4 + mulscc %o4, %o1, %o4 ! 5 + mulscc %o4, %o1, %o4 ! 6 + mulscc %o4, %o1, %o4 ! 7 + mulscc %o4, %o1, %o4 ! 8 + mulscc %o4, %o1, %o4 ! 9 + mulscc %o4, %o1, %o4 ! 10 + mulscc %o4, %o1, %o4 ! 11 + mulscc %o4, %o1, %o4 ! 12 + mulscc %o4, %o1, %o4 ! 13 + mulscc %o4, %o1, %o4 ! 14 + mulscc %o4, %o1, %o4 ! 15 + mulscc %o4, %o1, %o4 ! 16 + mulscc %o4, %o1, %o4 ! 17 + mulscc %o4, %o1, %o4 ! 18 + mulscc %o4, %o1, %o4 ! 19 + mulscc %o4, %o1, %o4 ! 20 + mulscc %o4, %o1, %o4 ! 21 + mulscc %o4, %o1, %o4 ! 22 + mulscc %o4, %o1, %o4 ! 23 + mulscc %o4, %o1, %o4 ! 24 + mulscc %o4, %o1, %o4 ! 25 + mulscc %o4, %o1, %o4 ! 26 + mulscc %o4, %o1, %o4 ! 27 + mulscc %o4, %o1, %o4 ! 28 + mulscc %o4, %o1, %o4 ! 29 + mulscc %o4, %o1, %o4 ! 30 + mulscc %o4, %o1, %o4 ! 31 + mulscc %o4, %o1, %o4 ! 32 + mulscc %o4, %g0, %o4 ! final shift + + ! If %o0 was negative, the result is + ! (%o0 * %o1) + (%o1 << 32)) + ! We fix that here. + + tst %o0 + bge 1f + rd %y, %o0 + + ! %o0 was indeed negative; fix upper 32 bits of result by subtracting + ! %o1 (i.e., return %o4 - %o1 in %o1). + retl + sub %o4, %o1, %o1 + +1: + retl + mov %o4, %o1 + +Lmul_shortway: + /* + * Short multiply. 12 steps, followed by a final shift step. + * The resulting bits are off by 12 and (32-12) = 20 bit positions, + * but there is no problem with %o0 being negative (unlike above). + */ + mulscc %o4, %o1, %o4 ! 1 + mulscc %o4, %o1, %o4 ! 2 + mulscc %o4, %o1, %o4 ! 3 + mulscc %o4, %o1, %o4 ! 4 + mulscc %o4, %o1, %o4 ! 5 + mulscc %o4, %o1, %o4 ! 6 + mulscc %o4, %o1, %o4 ! 7 + mulscc %o4, %o1, %o4 ! 8 + mulscc %o4, %o1, %o4 ! 9 + mulscc %o4, %o1, %o4 ! 10 + mulscc %o4, %o1, %o4 ! 11 + mulscc %o4, %o1, %o4 ! 12 + mulscc %o4, %g0, %o4 ! final shift + + /* + * %o4 has 20 of the bits that should be in the low part of the + * result; %y has the bottom 12 (as %y's top 12). That is: + * + * %o4 %y + * +----------------+----------------+ + * | -12- | -20- | -12- | -20- | + * +------(---------+------)---------+ + * --hi-- ----low-part---- + * + * The upper 12 bits of %o4 should be sign-extended to form the + * high part of the product (i.e., highpart = %o4 >> 20). + */ + + rd %y, %o5 + sll %o4, 12, %o0 ! shift middle bits left 12 + srl %o5, 20, %o5 ! shift low bits right 20, zero fill at left + or %o5, %o0, %o0 ! construct low part of result + retl + sra %o4, 20, %o1 ! ... and extract high part of result diff --git a/common/lib/libc/arch/sparc/gen/saveregs.S b/common/lib/libc/arch/sparc/gen/saveregs.S new file mode 100644 index 000000000..9b497ac5d --- /dev/null +++ b/common/lib/libc/arch/sparc/gen/saveregs.S @@ -0,0 +1,61 @@ +/* $NetBSD: saveregs.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: saveregs.s,v 1.1 91/07/06 17:22:33 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: saveregs.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * Save register arguments in caller's `arg dump' area, so that + * stdarg functions work. + * + * This really should be done with a pointer to the arg dump area; + * our caller should allocate that area, not our caller's caller. + * But then, they did not let me invent the calling sequence.... + * + * We assume the caller has executed a `save' instruction. + */ + +ENTRY(__builtin_saveregs) + st %i0, [%fp + 0x44] ! fr->fr_argd[0] + st %i1, [%fp + 0x48] ! fr->fr_argd[1] + st %i2, [%fp + 0x4c] ! fr->fr_argd[2] + st %i3, [%fp + 0x50] ! fr->fr_argd[3] + st %i4, [%fp + 0x54] ! fr->fr_argd[4] + retl + st %i5, [%fp + 0x58] ! fr->fr_argd[5] diff --git a/common/lib/libc/arch/sparc/gen/umul.S b/common/lib/libc/arch/sparc/gen/umul.S new file mode 100644 index 000000000..1622237a5 --- /dev/null +++ b/common/lib/libc/arch/sparc/gen/umul.S @@ -0,0 +1,189 @@ +/* $NetBSD: umul.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: umul.s,v 1.4 92/06/25 13:24:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)umul.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: umul.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Unsigned multiply. Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the + * upper 32 bits of the 64-bit product). + * + * This code optimizes short (less than 13-bit) multiplies. Short + * multiplies require 25 instruction cycles, and long ones require + * 45 instruction cycles. + * + * On return, overflow has occurred (%o1 is not zero) if and only if + * the Z condition code is clear, allowing, e.g., the following: + * + * call .umul + * nop + * bnz overflow (or tnz) + */ + +FUNC(.umul) + or %o0, %o1, %o4 + mov %o0, %y ! multiplier -> Y + andncc %o4, 0xfff, %g0 ! test bits 12..31 of *both* args + be Lmul_shortway ! if zero, can do it the short way + andcc %g0, %g0, %o4 ! zero the partial product and clear N and V + + /* + * Long multiply. 32 steps, followed by a final shift step. + */ + mulscc %o4, %o1, %o4 ! 1 + mulscc %o4, %o1, %o4 ! 2 + mulscc %o4, %o1, %o4 ! 3 + mulscc %o4, %o1, %o4 ! 4 + mulscc %o4, %o1, %o4 ! 5 + mulscc %o4, %o1, %o4 ! 6 + mulscc %o4, %o1, %o4 ! 7 + mulscc %o4, %o1, %o4 ! 8 + mulscc %o4, %o1, %o4 ! 9 + mulscc %o4, %o1, %o4 ! 10 + mulscc %o4, %o1, %o4 ! 11 + mulscc %o4, %o1, %o4 ! 12 + mulscc %o4, %o1, %o4 ! 13 + mulscc %o4, %o1, %o4 ! 14 + mulscc %o4, %o1, %o4 ! 15 + mulscc %o4, %o1, %o4 ! 16 + mulscc %o4, %o1, %o4 ! 17 + mulscc %o4, %o1, %o4 ! 18 + mulscc %o4, %o1, %o4 ! 19 + mulscc %o4, %o1, %o4 ! 20 + mulscc %o4, %o1, %o4 ! 21 + mulscc %o4, %o1, %o4 ! 22 + mulscc %o4, %o1, %o4 ! 23 + mulscc %o4, %o1, %o4 ! 24 + mulscc %o4, %o1, %o4 ! 25 + mulscc %o4, %o1, %o4 ! 26 + mulscc %o4, %o1, %o4 ! 27 + mulscc %o4, %o1, %o4 ! 28 + mulscc %o4, %o1, %o4 ! 29 + mulscc %o4, %o1, %o4 ! 30 + mulscc %o4, %o1, %o4 ! 31 + mulscc %o4, %o1, %o4 ! 32 + mulscc %o4, %g0, %o4 ! final shift + + + /* + * Normally, with the shift-and-add approach, if both numbers are + * positive you get the correct result. WIth 32-bit two's-complement + * numbers, -x is represented as + * + * x 32 + * ( 2 - ------ ) mod 2 * 2 + * 32 + * 2 + * + * (the `mod 2' subtracts 1 from 1.bbbb). To avoid lots of 2^32s, + * we can treat this as if the radix point were just to the left + * of the sign bit (multiply by 2^32), and get + * + * -x = (2 - x) mod 2 + * + * Then, ignoring the `mod 2's for convenience: + * + * x * y = xy + * -x * y = 2y - xy + * x * -y = 2x - xy + * -x * -y = 4 - 2x - 2y + xy + * + * For signed multiplies, we subtract (x << 32) from the partial + * product to fix this problem for negative multipliers (see mul.s). + * Because of the way the shift into the partial product is calculated + * (N xor V), this term is automatically removed for the multiplicand, + * so we don't have to adjust. + * + * But for unsigned multiplies, the high order bit wasn't a sign bit, + * and the correction is wrong. So for unsigned multiplies where the + * high order bit is one, we end up with xy - (y << 32). To fix it + * we add y << 32. + */ + tst %o1 + bl,a 1f ! if %o1 < 0 (high order bit = 1), + add %o4, %o0, %o4 ! %o4 += %o0 (add y to upper half) +1: rd %y, %o0 ! get lower half of product + retl + addcc %o4, %g0, %o1 ! put upper half in place and set Z for %o1==0 + +Lmul_shortway: + /* + * Short multiply. 12 steps, followed by a final shift step. + * The resulting bits are off by 12 and (32-12) = 20 bit positions, + * but there is no problem with %o0 being negative (unlike above), + * and overflow is impossible (the answer is at most 24 bits long). + */ + mulscc %o4, %o1, %o4 ! 1 + mulscc %o4, %o1, %o4 ! 2 + mulscc %o4, %o1, %o4 ! 3 + mulscc %o4, %o1, %o4 ! 4 + mulscc %o4, %o1, %o4 ! 5 + mulscc %o4, %o1, %o4 ! 6 + mulscc %o4, %o1, %o4 ! 7 + mulscc %o4, %o1, %o4 ! 8 + mulscc %o4, %o1, %o4 ! 9 + mulscc %o4, %o1, %o4 ! 10 + mulscc %o4, %o1, %o4 ! 11 + mulscc %o4, %o1, %o4 ! 12 + mulscc %o4, %g0, %o4 ! final shift + + /* + * %o4 has 20 of the bits that should be in the result; %y has + * the bottom 12 (as %y's top 12). That is: + * + * %o4 %y + * +----------------+----------------+ + * | -12- | -20- | -12- | -20- | + * +------(---------+------)---------+ + * -----result----- + * + * The 12 bits of %o4 left of the `result' area are all zero; + * in fact, all top 20 bits of %o4 are zero. + */ + + rd %y, %o5 + sll %o4, 12, %o0 ! shift middle bits left 12 + srl %o5, 20, %o5 ! shift low bits right 20 + or %o5, %o0, %o0 + retl + addcc %g0, %g0, %o1 ! %o1 = zero, and set Z diff --git a/common/lib/libc/arch/sparc/net/htonl.S b/common/lib/libc/arch/sparc/net/htonl.S new file mode 100644 index 000000000..17e1986e5 --- /dev/null +++ b/common/lib/libc/arch/sparc/net/htonl.S @@ -0,0 +1,52 @@ +/* $NetBSD: htonl.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: htonl.s,v 1.1 92/06/25 12:47:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)htonl.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: htonl.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* netorder = htonl(hostorder) */ + + +ENTRY(htonl) + retl + nop diff --git a/common/lib/libc/arch/sparc/net/htons.S b/common/lib/libc/arch/sparc/net/htons.S new file mode 100644 index 000000000..14daafa73 --- /dev/null +++ b/common/lib/libc/arch/sparc/net/htons.S @@ -0,0 +1,51 @@ +/* $NetBSD: htons.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: htons.s,v 1.1 92/06/25 12:47:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)htons.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: htons.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* netorder = htons(hostorder) */ +ENTRY(htons) + sethi %hi(0xffff0000), %o1 + retl + andn %o0, %o1, %o0 diff --git a/common/lib/libc/arch/sparc/net/ntohl.S b/common/lib/libc/arch/sparc/net/ntohl.S new file mode 100644 index 000000000..4fc522af3 --- /dev/null +++ b/common/lib/libc/arch/sparc/net/ntohl.S @@ -0,0 +1,49 @@ +/* $NetBSD: ntohl.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ntohl.s,v 1.1 92/06/25 12:47:06 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ntohl.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ntohl.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* hostorder = ntohl(netorder) */ +ENTRY(ntohl) + retl + nop diff --git a/common/lib/libc/arch/sparc/net/ntohs.S b/common/lib/libc/arch/sparc/net/ntohs.S new file mode 100644 index 000000000..8ab75118a --- /dev/null +++ b/common/lib/libc/arch/sparc/net/ntohs.S @@ -0,0 +1,52 @@ +/* $NetBSD: ntohs.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ntohs.s,v 1.1 92/06/25 12:47:07 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ntohs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ntohs.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* hostorder = ntohs(netorder) */ + +ENTRY(ntohs) + sethi %hi(0xffff0000), %o1 + retl + andn %o0, %o1, %o0 diff --git a/common/lib/libc/arch/sparc/string/bzero.S b/common/lib/libc/arch/sparc/string/bzero.S new file mode 100644 index 000000000..857bf2031 --- /dev/null +++ b/common/lib/libc/arch/sparc/string/bzero.S @@ -0,0 +1,179 @@ +/* $NetBSD: bzero.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: bzero.s,v 1.1 92/06/25 12:52:46 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)bzero.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: bzero.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * We should unroll the loop, but at the moment this would + * gain nothing since the `std' instructions are what limits us. + */ + +#ifdef MEMSET +/* + * void * + * memset(void *addr, int pattern, size_t len) + */ +ENTRY(memset) + ! %o0 = addr, %o1 = pattern, %o2 = len + /* + * Expand the byte pattern to fill 64 bits in an even-aligned + * register pair; shuffle arguments to match those of bzero. + */ + and %o1, 0xff, %o3 + mov %o2, %o1 ! shuffle argument + sll %o3, 8, %o2 + or %o2, %o3, %o2 + mov %o0, %g1 ! save original pointer + sll %o2, 16, %o3 + or %o2, %o3, %o2 + ! Optimize a common case: addr and len are both multiples of 8. + or %o0, %o1, %o5 + btst 7, %o5 ! ((addr | len) & 7) != 0? + bnz 1f ! if so, cannot optimize + mov %o2, %o3 ! in any case, complete pat expansion +#else +/* + * void + * bzero(void *addr, size_t len) + */ +ENTRY(bzero) + ! %o0 = addr, %o1 = len + + clr %o2 + ! Optimize a common case: addr and len are both multiples of 8. + or %o0, %o1, %o5 + btst 7, %o5 ! ((addr | len) & 7) != 0? + bnz 1f ! if so, cannot optimize + clr %o3 ! in any case, we want o3=0 +#endif + + /* `Good' operands, can just store doubles. */ +0: + deccc 8, %o1 ! while ((len -= 8) >= 0) + bge,a 0b + std %o2, [%o0 + %o1] ! *(quad *)(addr + len) = 0; + retl + nop + + /* + * Either the address is unaligned, or the count is not a + * multiple of 8, or both. We will have to align the address + * in order to use anything `better' than stb. + */ +1: + cmp %o1, 15 ! len >= 15? + bge,a Lstd ! yes, use std + btst 1, %o0 ! (but first check alignment) + + ! not enough to bother: do byte-at-a-time loop. +2: + deccc %o1 ! while (--len >= 0) + bge,a 2b + stb %o2, [%o0 + %o1] ! addr[len] = 0; + retl + nop + +Lstd: + /* + * There are at least 15 bytes to zero. + * We may have to zero some initial stuff to align + * the address. + */ + bz,a 1f ! if (addr & 1) { + btst 2, %o0 + stb %o2, [%o0] ! *addr = 0; + inc %o0 ! addr++; + dec %o1 ! len--; + btst 2, %o0 ! } +1: + bz,a 1f ! if (addr & 2) { + btst 4, %o0 + sth %o2, [%o0] ! *(short *)addr = 0; + inc 2, %o0 ! addr += 2; + dec 2, %o1 ! len -= 2; + btst 4, %o0 ! } +1: + bz 1f ! if (addr & 4) { + dec 8, %o1 + st %o2, [%o0] ! *(int *)addr = 0; + inc 4, %o0 ! addr += 4; + dec 4, %o1 ! len -= 4; + ! } + /* + * Address is double word aligned; len is 8 less than + * the number of bytes remaining (i.e., len is 0 if + * the remaining count is 8, 1 if it is 9, etc.). + */ +1: + std %o2, [%o0] ! do { +2: ! *(quad *)addr = 0; + inc 8, %o0 ! addr += 8; + deccc 8, %o1 ! } while ((len -= 8) >= 0); + bge,a 2b + std %o2, [%o0] + + /* + * Len is in [-8..-1] where -8 => done, -7 => 1 byte to zero, + * -6 => two bytes, etc. Mop up this remainder, if any. + */ + btst 4, %o1 + bz 1f ! if (len & 4) { + btst 2, %o1 + st %o2, [%o0] ! *(int *)addr = 0; + inc 4, %o0 ! addr += 4; +1: + bz 1f ! if (len & 2) { + btst 1, %o1 + sth %o2, [%o0] ! *(short *)addr = 0; + inc 2, %o0 ! addr += 2; +1: + bnz,a 1f ! if (len & 1) + stb %o2, [%o0] ! *addr = 0; +1: + retl +#ifdef MEMSET + mov %g1, %o0 ! restore original pointer +#else + nop +#endif diff --git a/common/lib/libc/arch/sparc/string/ffs.S b/common/lib/libc/arch/sparc/string/ffs.S new file mode 100644 index 000000000..104e36263 --- /dev/null +++ b/common/lib/libc/arch/sparc/string/ffs.S @@ -0,0 +1,113 @@ +/* $NetBSD: ffs.S,v 1.2 2009/12/19 19:09:48 pooka Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ffs.s,v 1.3 92/07/07 00:23:57 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ffs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ffs.S,v 1.2 2009/12/19 19:09:48 pooka Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * ffs returns the number of the rightmost bit set in its argument, + * i.e., the lowest value such that (x & (ffs(x) - 1)) is nonzero. + * If no bits are set, ffs returns 0. + * + * We use a table lookup on each byte. + * + * In each section below, %o1 is the current byte (0, 1, 2, or 3). + * The last byte is handled specially: for the first three, + * if that byte is nonzero, we return the table value + * (plus 0, 8, or 16 for the byte number), but for the last + * one, we just return the table value plus 24. This means + * that ffstab[0] must be -24 so that ffs(0) will return 0. + */ +ENTRY(ffs) +#ifdef PIC + PICCY_SET(ffstab, %o2, %o3) +#else + set ffstab, %o2 +#endif + andcc %o0, 0xff, %o1 ! get low byte + be,a 1f ! try again if 0 + srl %o0, 8, %o0 ! delay slot, get ready for next byte + + retl ! return ffstab[%o1] + ldsb [%o2 + %o1], %o0 + +1: + andcc %o0, 0xff, %o1 ! byte 1 like byte 0... + be,a 2f + srl %o0, 8, %o0 ! (use delay to prepare for byte 2) + + ldsb [%o2 + %o1], %o0 + retl ! return ffstab[%o1] + 8 + add %o0, 8, %o0 + +2: + andcc %o0, 0xff, %o1 + be,a 3f + srl %o0, 8, %o0 ! (prepare for byte 3) + + ldsb [%o2 + %o1], %o0 + retl ! return ffstab[%o1] + 16 + add %o0, 16, %o0 + +3: ! just return ffstab[%o0] + 24 + ldsb [%o2 + %o0], %o0 + retl + add %o0, 24, %o0 + +ffstab: + .byte -24,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 00-0f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 10-1f */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 20-2f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 30-3f */ + .byte 7,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 40-4f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 50-5f */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 60-6f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 70-7f */ + .byte 8,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 80-8f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 10-9f */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* a0-af */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* b0-bf */ + .byte 7,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* c0-cf */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* d0-df */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* e0-ef */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* f0-ff */ diff --git a/common/lib/libc/arch/sparc/string/memset.S b/common/lib/libc/arch/sparc/string/memset.S new file mode 100644 index 000000000..9bbf1eb79 --- /dev/null +++ b/common/lib/libc/arch/sparc/string/memset.S @@ -0,0 +1,4 @@ +/* $NetBSD: memset.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +#define MEMSET +#include "bzero.S" diff --git a/common/lib/libc/arch/sparc/string/strlen.S b/common/lib/libc/arch/sparc/string/strlen.S new file mode 100644 index 000000000..0bb811e65 --- /dev/null +++ b/common/lib/libc/arch/sparc/string/strlen.S @@ -0,0 +1,55 @@ +/* $NetBSD: strlen.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: strlen.s,v 1.1 92/06/25 12:52:47 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)strlen.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: strlen.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(strlen) + add %o0, 1, %o1 ! save starting point + 1 +1: + ldsb [%o0], %o2 ! fetch byte + tst %o2 ! null? + bne 1b ! no, keep going + inc %o0 ! always increment pointer + retl + sub %o0, %o1, %o0 ! return length (ptr - (origptr+1)) diff --git a/common/lib/libc/arch/sparc64/atomic/Makefile.inc b/common/lib/libc/arch/sparc64/atomic/Makefile.inc new file mode 100644 index 000000000..bc058908e --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/Makefile.inc @@ -0,0 +1,15 @@ +# $NetBSD: Makefile.inc,v 1.6 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add.S atomic_and.S atomic_cas.S atomic_dec.S \ + atomic_inc.S atomic_or.S atomic_swap.S membar_ops.S + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_cas.c + +.endif diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_add.S b/common/lib/libc/arch/sparc64/atomic/atomic_add.S new file mode 100644 index 000000000..f9df71745 --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_add.S @@ -0,0 +1,76 @@ +/* $NetBSD: atomic_add.S,v 1.6 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_add_32) + ld [%o0], %o2 +1: add %o1, %o2, %o3 + cas [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %icc, 1b + mov %o3, %o2 + retl + add %o1, %o2, %o0 +ATOMIC_OP_ALIAS(atomic_add_32,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) + +ATOMIC_OP_ALIAS(atomic_add_32_nv,_atomic_add_32) +STRONG_ALIAS(_atomic_add_32_nv,_atomic_add_32) +ATOMIC_OP_ALIAS(atomic_add_int_nv,_atomic_add_32) +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32) + +ENTRY(_atomic_add_64) + ldx [%o0], %o2 +1: add %o1, %o2, %o3 + casx [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %xcc, 1b + mov %o3, %o2 + retl + add %o1, %o2, %o0 +ATOMIC_OP_ALIAS(atomic_add_64,_atomic_add_64) + +ATOMIC_OP_ALIAS(atomic_add_64_nv,_atomic_add_64) +STRONG_ALIAS(_atomic_add_64_nv,_atomic_add_64) + +ATOMIC_OP_ALIAS_SIZE(atomic_add_long,_atomic_add) +STRONG_ALIAS_SIZE(_atomic_add_long,_atomic_add) +ATOMIC_OP_ALIAS_SIZE(atomic_add_ptr,_atomic_add) +STRONG_ALIAS_SIZE(_atomic_add_ptr,_atomic_add) + +ATOMIC_OP_ALIAS_SIZE(atomic_add_long_nv,_atomic_add) +STRONG_ALIAS_SIZE(_atomic_add_long_nv,_atomic_add) +ATOMIC_OP_ALIAS_SIZE(atomic_add_ptr_nv,_atomic_add) +STRONG_ALIAS_SIZE(_atomic_add_ptr_nv,_atomic_add) diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_and.S b/common/lib/libc/arch/sparc64/atomic/atomic_and.S new file mode 100644 index 000000000..d60b3b577 --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_and.S @@ -0,0 +1,72 @@ +/* $NetBSD: atomic_and.S,v 1.6 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_and_32) + ld [%o0], %o2 +1: and %o1, %o2, %o3 + cas [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %icc, 1b + mov %o3, %o2 + retl + and %o1, %o2, %o0 +ATOMIC_OP_ALIAS(atomic_and_32,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) + +ATOMIC_OP_ALIAS(atomic_and_32_nv,_atomic_and_32) +STRONG_ALIAS(_atomic_and_32_nv,_atomic_and_32) +ATOMIC_OP_ALIAS(atomic_and_uint_nv,_atomic_and_32) +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32) + +ENTRY(_atomic_and_64) + ldx [%o0], %o2 +1: and %o1, %o2, %o3 + casx [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %xcc, 1b + mov %o3, %o2 + retl + and %o1, %o2, %o0 +ATOMIC_OP_ALIAS(atomic_and_64,_atomic_and_64) + +ATOMIC_OP_ALIAS(atomic_and_64_nv,_atomic_and_64) +STRONG_ALIAS(_atomic_and_64_nv,_atomic_and_64) + +ATOMIC_OP_ALIAS_SIZE(atomic_and_ulong,_atomic_and) +STRONG_ALIAS_SIZE(_atomic_and_ulong,_atomic_and) + +ATOMIC_OP_ALIAS_SIZE(atomic_and_ulong_nv,_atomic_and) +STRONG_ALIAS_SIZE(_atomic_and_ulong_nv,_atomic_and) diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_cas.S b/common/lib/libc/arch/sparc64/atomic/atomic_cas.S new file mode 100644 index 000000000..03668b54f --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_cas.S @@ -0,0 +1,66 @@ +/* $NetBSD: atomic_cas.S,v 1.6 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_cas_32) + cas [%o0], %o1, %o2 + retl + mov %o2, %o0 + +ATOMIC_OP_ALIAS(atomic_cas_32,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) + +ATOMIC_OP_ALIAS(atomic_cas_32_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_32_ni,_atomic_cas_32) +ATOMIC_OP_ALIAS(atomic_cas_uint_ni,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32) + +ENTRY(_atomic_cas_64) + casx [%o0], %o1, %o2 + retl + mov %o2, %o0 + +ATOMIC_OP_ALIAS(atomic_cas_64,_atomic_cas_64) +ATOMIC_OP_ALIAS_SIZE(atomic_cas_ulong,_atomic_cas) +STRONG_ALIAS_SIZE(_atomic_cas_ulong,_atomic_cas) +ATOMIC_OP_ALIAS_SIZE(atomic_cas_ptr,_atomic_cas) +STRONG_ALIAS_SIZE(_atomic_cas_ptr,_atomic_cas) + +ATOMIC_OP_ALIAS(atomic_cas_64_ni,_atomic_cas_64) +STRONG_ALIAS_SIZE(_atomic_cas_64_ni,_atomic_cas) +ATOMIC_OP_ALIAS_SIZE(atomic_cas_ulong_ni,_atomic_cas) +STRONG_ALIAS_SIZE(_atomic_cas_ulong_ni,_atomic_cas) +ATOMIC_OP_ALIAS_SIZE(atomic_cas_ptr_ni,_atomic_cas) +STRONG_ALIAS_SIZE(_atomic_cas_ptr_ni,_atomic_cas) diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_dec.S b/common/lib/libc/arch/sparc64/atomic/atomic_dec.S new file mode 100644 index 000000000..79dbac8c6 --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_dec.S @@ -0,0 +1,76 @@ +/* $NetBSD: atomic_dec.S,v 1.7 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_dec_32) + ld [%o0], %o2 +1: add %o2, -1, %o3 + cas [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %icc, 1b + mov %o3, %o2 + retl + add %o2, -1, %o0 +ATOMIC_OP_ALIAS(atomic_dec_32,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) + +ATOMIC_OP_ALIAS(atomic_dec_32_nv,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_32_nv,_atomic_dec_32) +ATOMIC_OP_ALIAS(atomic_dec_uint_nv,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32) + +ENTRY(_atomic_dec_64) + ldx [%o0], %o2 +1: add %o2, -1, %o3 + casx [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %xcc, 1b + mov %o3, %o2 + retl + add %o2, -1, %o0 +ATOMIC_OP_ALIAS(atomic_dec_64,_atomic_dec_64) + +ATOMIC_OP_ALIAS(atomic_dec_64_nv,_atomic_dec_64) +STRONG_ALIAS(_atomic_dec_64_nv,_atomic_dec_64) + +ATOMIC_OP_ALIAS_SIZE(atomic_dec_ulong,_atomic_dec) +STRONG_ALIAS_SIZE(_atomic_dec_ulong,_atomic_dec) +ATOMIC_OP_ALIAS_SIZE(atomic_dec_ptr,_atomic_dec) +STRONG_ALIAS_SIZE(_atomic_dec_ptr,_atomic_dec) + +ATOMIC_OP_ALIAS_SIZE(atomic_dec_ulong_nv,_atomic_dec) +STRONG_ALIAS_SIZE(_atomic_dec_ulong_nv,_atomic_dec) +ATOMIC_OP_ALIAS_SIZE(atomic_dec_ptr_nv,_atomic_dec) +STRONG_ALIAS_SIZE(_atomic_dec_ptr_nv,_atomic_dec) diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_inc.S b/common/lib/libc/arch/sparc64/atomic/atomic_inc.S new file mode 100644 index 000000000..7c0128686 --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_inc.S @@ -0,0 +1,76 @@ +/* $NetBSD: atomic_inc.S,v 1.6 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_inc_32) + ld [%o0], %o2 +1: add %o2, 1, %o3 + cas [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %icc, 1b + mov %o3, %o2 + retl + add %o2, 1, %o0 +ATOMIC_OP_ALIAS(atomic_inc_32,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) + +ATOMIC_OP_ALIAS(atomic_inc_32_nv,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_32_nv,_atomic_inc_32) +ATOMIC_OP_ALIAS(atomic_inc_uint_nv,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32) + +ENTRY(_atomic_inc_64) + ldx [%o0], %o2 +1: add %o2, 1, %o3 + casx [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %xcc, 1b + mov %o3, %o2 + retl + add %o2, 1, %o0 +ATOMIC_OP_ALIAS(atomic_inc_64,_atomic_inc_64) + +ATOMIC_OP_ALIAS(atomic_inc_64_nv,_atomic_inc_64) +STRONG_ALIAS(_atomic_inc_64_nv,_atomic_inc_64) + +ATOMIC_OP_ALIAS_SIZE(atomic_inc_ulong,_atomic_inc) +STRONG_ALIAS_SIZE(_atomic_inc_ulong,_atomic_inc) +ATOMIC_OP_ALIAS_SIZE(atomic_inc_ptr,_atomic_inc) +STRONG_ALIAS_SIZE(_atomic_inc_ptr,_atomic_inc) + +ATOMIC_OP_ALIAS_SIZE(atomic_inc_ulong_nv,_atomic_inc) +STRONG_ALIAS_SIZE(_atomic_inc_ulong_nv,_atomic_inc) +ATOMIC_OP_ALIAS_SIZE(atomic_inc_ptr_nv,_atomic_inc) +STRONG_ALIAS_SIZE(_atomic_inc_ptr_nv,_atomic_inc) diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_op_asm.h b/common/lib/libc/arch/sparc64/atomic/atomic_op_asm.h new file mode 100644 index 000000000..68c611261 --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_op_asm.h @@ -0,0 +1,65 @@ +/* $NetBSD: atomic_op_asm.h,v 1.6 2011/01/17 18:11:10 joerg Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_ASM_H_ +#define _ATOMIC_OP_ASM_H_ + +#include + +#if defined(_KERNEL) + +#define ATOMIC_OP_ALIAS(a,s) STRONG_ALIAS(a,s) + +#ifdef __arch64__ +#define ATOMIC_OP_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _64) +#else +#define ATOMIC_OP_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _32) +#endif + +#else /* _KERNEL */ + +#define ATOMIC_OP_ALIAS(a,s) WEAK_ALIAS(a,s) + +#ifdef __arch64__ +#define ATOMIC_OP_ALIAS_SIZE(a,s) WEAK_ALIAS(a,s ## _64) +#else +#define ATOMIC_OP_ALIAS_SIZE(a,s) WEAK_ALIAS(a,s ## _32) +#endif + +#endif /* _KERNEL */ + +#ifdef __arch64__ +#define STRONG_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _64) +#else +#define STRONG_ALIAS_SIZE(a,s) STRONG_ALIAS(a,s ## _32) +#endif + +#endif /* _ATOMIC_OP_ASM_H_ */ diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_or.S b/common/lib/libc/arch/sparc64/atomic/atomic_or.S new file mode 100644 index 000000000..edb82d393 --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_or.S @@ -0,0 +1,72 @@ +/* $NetBSD: atomic_or.S,v 1.7 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_or_32) + ld [%o0], %o2 +1: or %o1, %o2, %o3 + cas [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %icc, 1b + mov %o3, %o2 + retl + or %o1, %o2, %o0 +ATOMIC_OP_ALIAS(atomic_or_32,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) + +ATOMIC_OP_ALIAS(atomic_or_32_nv,_atomic_or_32) +STRONG_ALIAS(_atomic_or_32_nv,_atomic_or_32) +ATOMIC_OP_ALIAS(atomic_or_uint_nv,_atomic_or_32) +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32) + +ENTRY(_atomic_or_64) + ldx [%o0], %o2 +1: or %o1, %o2, %o3 + casx [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %xcc, 1b + mov %o3, %o2 + retl + or %o1, %o2, %o0 +ATOMIC_OP_ALIAS(atomic_or_64,_atomic_or_64) + +ATOMIC_OP_ALIAS(atomic_or_64_nv,_atomic_or_64) +STRONG_ALIAS(_atomic_or_64_nv,_atomic_or_64) + +ATOMIC_OP_ALIAS_SIZE(atomic_or_ulong,_atomic_or) +STRONG_ALIAS_SIZE(_atomic_or_ulong,_atomic_or) + +ATOMIC_OP_ALIAS_SIZE(atomic_or_ulong_nv,_atomic_or) +STRONG_ALIAS_SIZE(_atomic_or_ulong_nv,_atomic_or) diff --git a/common/lib/libc/arch/sparc64/atomic/atomic_swap.S b/common/lib/libc/arch/sparc64/atomic/atomic_swap.S new file mode 100644 index 000000000..03f3288bb --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/atomic_swap.S @@ -0,0 +1,63 @@ +/* $NetBSD: atomic_swap.S,v 1.6 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_asm.h" + + .text + +ENTRY(_atomic_swap_32) + ld [%o0], %o2 +1: mov %o1, %o3 + cas [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %icc, 1b + mov %o3, %o2 + retl + mov %o3, %o0 +ATOMIC_OP_ALIAS(atomic_swap_32,_atomic_swap_32) +ATOMIC_OP_ALIAS(atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) + +ENTRY(_atomic_swap_64) + ldx [%o0], %o2 +1: mov %o1, %o3 + casx [%o0], %o2, %o3 + cmp %o2, %o3 + bne,a,pn %xcc, 1b + mov %o3, %o2 + retl + mov %o3, %o0 +ATOMIC_OP_ALIAS(atomic_swap_64,_atomic_swap_64) + +ATOMIC_OP_ALIAS_SIZE(atomic_swap_ulong,_atomic_swap) +STRONG_ALIAS_SIZE(_atomic_swap_ulong,_atomic_swap) +ATOMIC_OP_ALIAS_SIZE(atomic_swap_ptr,_atomic_swap) +STRONG_ALIAS_SIZE(_atomic_swap_ptr,_atomic_swap) diff --git a/common/lib/libc/arch/sparc64/atomic/membar_ops.S b/common/lib/libc/arch/sparc64/atomic/membar_ops.S new file mode 100644 index 000000000..a3f5cb59f --- /dev/null +++ b/common/lib/libc/arch/sparc64/atomic/membar_ops.S @@ -0,0 +1,54 @@ +/* $NetBSD: membar_ops.S,v 1.5 2008/05/25 15:56:12 chs Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe, and by Andrew Doran. + * + * 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 "atomic_op_asm.h" + + .text + +/* These assume Total Store Order (TSO) */ + +ENTRY(_membar_producer) + retl + nop + +ENTRY(_membar_consumer) + membar #LoadLoad + retl + nop + +ATOMIC_OP_ALIAS(membar_producer,_membar_producer) +ATOMIC_OP_ALIAS(membar_consumer,_membar_consumer) +ATOMIC_OP_ALIAS(membar_enter,_membar_consumer) +STRONG_ALIAS(_membar_enter,_membar_consumer) +ATOMIC_OP_ALIAS(membar_exit,_membar_consumer) +STRONG_ALIAS(_membar_exit,_membar_consumer) +ATOMIC_OP_ALIAS(membar_sync,_membar_consumer) +STRONG_ALIAS(_membar_sync,_membar_consumer) diff --git a/common/lib/libc/arch/sparc64/gen/mul.S b/common/lib/libc/arch/sparc64/gen/mul.S new file mode 100644 index 000000000..b4f9f3449 --- /dev/null +++ b/common/lib/libc/arch/sparc64/gen/mul.S @@ -0,0 +1,64 @@ +/* $NetBSD: mul.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: mul.s,v 1.5 92/06/25 13:24:03 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)mul.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: mul.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Signed multiply. + * + * Returns %o0 * %o1 in %o0 + * + * According to the manual the smaller parameter should be in + * rs1, so we'll move it there. + */ + +FUNC(.mul) + cmp %o0, %o1 + bge 1f + nop + retl + mulx %o0, %o1, %o0 +1: + retl + mulx %o1, %o0, %o0 diff --git a/common/lib/libc/arch/sparc64/gen/saveregs.S b/common/lib/libc/arch/sparc64/gen/saveregs.S new file mode 100644 index 000000000..0cd77ba2b --- /dev/null +++ b/common/lib/libc/arch/sparc64/gen/saveregs.S @@ -0,0 +1,62 @@ +/* $NetBSD: saveregs.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: saveregs.s,v 1.1 91/07/06 17:22:33 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: saveregs.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * Save register arguments in caller's `arg dump' area, so that + * stdarg functions work. + * + * This really should be done with a pointer to the arg dump area; + * our caller should allocate that area, not our caller's caller. + * But then, they did not let me invent the calling sequence.... + * + * We assume the caller has executed a `save' instruction. + */ +#define REGOFF (BIAS - CC64FSZ) + +ENTRY(__builtin_saveregs) + stx %i0, [%fp + REGOFF + 0x00] ! fr->fr_argd[0] + stx %i1, [%fp + REGOFF + 0x08] ! fr->fr_argd[1] + stx %i2, [%fp + REGOFF + 0x10] ! fr->fr_argd[2] + stx %i3, [%fp + REGOFF + 0x18] ! fr->fr_argd[3] + stx %i4, [%fp + REGOFF + 0x20] ! fr->fr_argd[4] + retl + stx %i5, [%fp + REGOFF + 0x28] ! fr->fr_argd[5] diff --git a/common/lib/libc/arch/sparc64/gen/umul.S b/common/lib/libc/arch/sparc64/gen/umul.S new file mode 100644 index 000000000..d97d01bd2 --- /dev/null +++ b/common/lib/libc/arch/sparc64/gen/umul.S @@ -0,0 +1,62 @@ +/* $NetBSD: umul.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: umul.s,v 1.4 92/06/25 13:24:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)umul.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: umul.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Unsigned multiply. Returns %o0 * %o1 in %o0 + * + * According to the manual the smaller parameter should be in + * rs1, so we'll move it there. + */ + +FUNC(.umul) + cmp %o0, %o1 + bge 1f + nop + retl + mulx %o0, %o1, %o0 +1: + retl + mulx %o1, %o0, %o0 diff --git a/common/lib/libc/arch/sparc64/net/htonl.S b/common/lib/libc/arch/sparc64/net/htonl.S new file mode 100644 index 000000000..15f0e4e53 --- /dev/null +++ b/common/lib/libc/arch/sparc64/net/htonl.S @@ -0,0 +1,52 @@ +/* $NetBSD: htonl.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: htonl.s,v 1.1 92/06/25 12:47:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)htonl.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: htonl.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* netorder = htonl(hostorder) */ + + +ENTRY(htonl) + retl + srl %o0, 0, %o0 /* zero extend -- or do we sign extend? */ diff --git a/common/lib/libc/arch/sparc64/net/htons.S b/common/lib/libc/arch/sparc64/net/htons.S new file mode 100644 index 000000000..2d06027fc --- /dev/null +++ b/common/lib/libc/arch/sparc64/net/htons.S @@ -0,0 +1,52 @@ +/* $NetBSD: htons.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: htons.s,v 1.1 92/06/25 12:47:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)htons.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: htons.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* netorder = htons(hostorder) */ +ENTRY(htons) + sethi %hi(0xffff0000), %o1 + signx %o1, %o1 + retl + andn %o0, %o1, %o0 diff --git a/common/lib/libc/arch/sparc64/net/ntohl.S b/common/lib/libc/arch/sparc64/net/ntohl.S new file mode 100644 index 000000000..cb5bde8b5 --- /dev/null +++ b/common/lib/libc/arch/sparc64/net/ntohl.S @@ -0,0 +1,49 @@ +/* $NetBSD: ntohl.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ntohl.s,v 1.1 92/06/25 12:47:06 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ntohl.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ntohl.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* hostorder = ntohl(netorder) */ +ENTRY(ntohl) + retl + signx %o0, %o0 diff --git a/common/lib/libc/arch/sparc64/net/ntohs.S b/common/lib/libc/arch/sparc64/net/ntohs.S new file mode 100644 index 000000000..f377d9798 --- /dev/null +++ b/common/lib/libc/arch/sparc64/net/ntohs.S @@ -0,0 +1,54 @@ +/* $NetBSD: ntohs.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ntohs.s,v 1.1 92/06/25 12:47:07 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ntohs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ntohs.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* hostorder = ntohs(netorder) */ + +ENTRY(ntohs) + sethi %hi(0xffff0000), %o1 + signx %o1, %o1 + retl + andn %o0, %o1, %o0 + diff --git a/common/lib/libc/arch/sparc64/string/ffs.S b/common/lib/libc/arch/sparc64/string/ffs.S new file mode 100644 index 000000000..93e620ae2 --- /dev/null +++ b/common/lib/libc/arch/sparc64/string/ffs.S @@ -0,0 +1,135 @@ +/* $NetBSD: ffs.S,v 1.2 2009/12/19 19:09:48 pooka Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ffs.s,v 1.3 92/07/07 00:23:57 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ffs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ffs.S,v 1.2 2009/12/19 19:09:48 pooka Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#if 0 + /* + * We have a popcount instruction -- use it. + * only uses %o0, %o1, %o2 + * + * Here's the pseudo-code from the v9 spec: + * + * int ffs(unsigned zz) { + * return popc( zz ^ ( ~ (-zz))); + * } + * + * XXXX sptifires and blackbirds don't implement popc, + * so we won't use this nice clean code 8^(. + */ +ENTRY(ffs) + neg %o0, %o1 ! %o1 = -zz + xnor %o0, %o1, %o2 ! %o2 = zz ^ ~ -zz + popc %o2, %o1 + movrz %o0, %g0, %o1 ! result of ffs(0) should be zero + retl + mov %o1, %o0 +#endif +/* + * ffs returns the number of the rightmost bit set in its argument, + * i.e., the lowest value such that (x & (ffs(x) - 1)) is nonzero. + * If no bits are set, ffs returns 0. + * + * We use a table lookup on each byte. + * + * In each section below, %o1 is the current byte (0, 1, 2, or 3). + * The last byte is handled specially: for the first three, + * if that byte is nonzero, we return the table value + * (plus 0, 8, or 16 for the byte number), but for the last + * one, we just return the table value plus 24. This means + * that ffstab[0] must be -24 so that ffs(0) will return 0. + */ +ENTRY(ffs) +#ifdef PIC + PICCY_SET(ffstab, %o2, %o3) +#else + set ffstab, %o2 +#endif + andcc %o0, 0xff, %o1 ! get low byte + be,a 1f ! try again if 0 + srl %o0, 8, %o0 ! delay slot, get ready for next byte + + retl ! return ffstab[%o1] + ldsb [%o2 + %o1], %o0 + +1: + andcc %o0, 0xff, %o1 ! byte 1 like byte 0... + be,a 2f + srl %o0, 8, %o0 ! (use delay to prepare for byte 2) + + ldsb [%o2 + %o1], %o0 + retl ! return ffstab[%o1] + 8 + add %o0, 8, %o0 + +2: + andcc %o0, 0xff, %o1 + be,a 3f + srl %o0, 8, %o0 ! (prepare for byte 3) + + ldsb [%o2 + %o1], %o0 + retl ! return ffstab[%o1] + 16 + add %o0, 16, %o0 + +3: ! just return ffstab[%o0] + 24 + ldsb [%o2 + %o0], %o0 + retl + add %o0, 24, %o0 + +ffstab: + .byte -24,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 00-0f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 10-1f */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 20-2f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 30-3f */ + .byte 7,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 40-4f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 50-5f */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 60-6f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 70-7f */ + .byte 8,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 80-8f */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* 10-9f */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* a0-af */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* b0-bf */ + .byte 7,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* c0-cf */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* d0-df */ + .byte 6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* e0-ef */ + .byte 5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1 /* f0-ff */ diff --git a/common/lib/libc/arch/sparc64/string/strlen.S b/common/lib/libc/arch/sparc64/string/strlen.S new file mode 100644 index 000000000..82c52beb5 --- /dev/null +++ b/common/lib/libc/arch/sparc64/string/strlen.S @@ -0,0 +1,165 @@ +/* $NetBSD: strlen.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */ + +/* + * Copyright 2002 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Eduardo Horvath for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: strlen.S,v 1.1 2005/12/20 19:28:50 christos Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* The algorithm here uses the following techniques: + * + * 1) Given a word 'x', we can test to see if it contains any 0 bytes + * by subtracting 0x01010101, and seeing if any of the high bits of each + * byte changed from 0 to 1. This works because the least significant + * 0 byte must have had no incoming carry (otherwise it's not the least + * significant), so it is 0x00 - 0x01 == 0xff. For all other + * byte values, either they have the high bit set initially, or when + * 1 is subtracted you get a value in the range 0x00-0x7f, none of which + * have their high bit set. The expression here is + * (x + 0xfefefeff) & ~(x | 0x7f7f7f7f), which gives 0x00000000 when + * there were no 0x00 bytes in the word. + * + * 2) Now just hunt for the first byte that's 0x00 in 'x'. + * + * This is from the book 'The PowerPC Compiler Writer's Guide', + * by Steve Hoxey, Faraydon Karim, Bill Hay and Hank Warren. + */ + +ENTRY(strlen) + /* + * Calculate address for and load the first xword. + */ + andn %o0, 0x7, %o1 + ldx [%o1], %g1 + + /* + * Now prepare some constants while the data arrives... + */ + sethi %hi(0xfefefefe), %o3 + sethi %hi(0x7f7f7f7f), %o2 + + or %o3, %lo(0xfefefefe), %o3 + or %o2, %lo(0x7f7f7f7f), %o2 + + sllx %o3, 32, %o5 + andcc %o0, 0x7, %g5 ! Hoisted from below to fill a slot + + sllx %o2, 32, %o4 + or %o3, %o5, %o3 + + sll %g5, 3, %g5 ! Convert to bytes. hoisted + or %o2, %o4, %o2 + + inc %o3 + neg %g5 ! hoisted + + /* + * Mask off the leading bits: + * + * if (ptr & 0x7) + * mask = -1 << (64 - ((ptr & 0x7) << 3)); + */ + +! andcc %o0, 0x7, %g5 ! Hoisted above + bz,pt %icc, 0f + + +! sll %g5, 3, %g5 ! Convert to bytes. Also hoisted + +! neg %g5 ! Hoisted + + add %g5, 64, %g5 + mov -1, %o4 + + sllx %o4, %g5, %o4 + + or %o4, %g1, %g1 ! Make leading bytes != 0 + +0: + or %g1, %o2, %o5 ! Do step 1 -- use or/andn instead of nor/and + add %g1, %o3, %g5 + + inc 8, %o1 ! Point to next word + andncc %g5, %o5, %g0 + bz,a,pt %xcc, 0b + ldx [%o1], %g1 + + mov -1, %o4 + dec 8, %o1 + + sllx %o4, 64-8, %o5 + + btst %g1, %o5 ! Check high byte + bz %xcc,0f + srlx %o5, 8, %o5 + + inc %o1 + btst %g1, %o5 ! Check 2nd byte + bz %xcc,0f + srlx %o5, 8, %o5 + + inc %o1 + btst %g1, %o5 ! Check 3rd byte + bz %xcc,0f + srlx %o5, 8, %o5 + + inc %o1 + btst %g1, %o5 ! Check 4th byte + bz %xcc,0f + srlx %o5, 8, %o5 + + inc %o1 + btst %g1, %o5 ! Check 5th byte + bz %xcc,0f + srlx %o5, 8, %o5 + + inc %o1 + btst %g1, %o5 ! Check 6th byte + bz %xcc,0f + srlx %o5, 8, %o5 + + inc %o1 + btst %g1, %o5 ! Check 7th byte + bz %xcc,0f + nop + + inc %o1 +0: + retl + sub %o1, %o0, %o0 ! return length (ptr - (origptr+1)) diff --git a/common/lib/libc/arch/vax/atomic/Makefile.inc b/common/lib/libc/arch/vax/atomic/Makefile.inc new file mode 100644 index 000000000..1bafd20d6 --- /dev/null +++ b/common/lib/libc/arch/vax/atomic/Makefile.inc @@ -0,0 +1,17 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic_add_32_cas.c atomic_add_32_nv_cas.c atomic_and_32_cas.c \ + atomic_and_32_nv_cas.c atomic_dec_32_cas.c atomic_dec_32_nv_cas.c \ + atomic_inc_32_cas.c atomic_inc_32_nv_cas.c atomic_or_32_cas.c \ + atomic_or_32_nv_cas.c atomic_swap_32_cas.c membar_ops_nop.c + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_testset.c + +.endif diff --git a/common/lib/libc/arch/vax/gen/bswap16.S b/common/lib/libc/arch/vax/gen/bswap16.S new file mode 100644 index 000000000..dc1c5932c --- /dev/null +++ b/common/lib/libc/arch/vax/gen/bswap16.S @@ -0,0 +1,44 @@ +/* $NetBSD: bswap16.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas. + * + * 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 + +#if defined(_KERNEL) || defined(_STANDALONE) +ENTRY(_C_LABEL(bswap16), 0) +#else +ALTENTRY(_C_LABEL(ntohs)) +ALTENTRY(_C_LABEL(htons)) +ENTRY(_C_LABEL(__bswap16), 0) +#endif + movl 4(%ap), %r1 + extzv $8, $8, %r1, %r0 + insv %r1, $8, $8, %r0 + ret diff --git a/common/lib/libc/arch/vax/gen/bswap32.S b/common/lib/libc/arch/vax/gen/bswap32.S new file mode 100644 index 000000000..91377ee9c --- /dev/null +++ b/common/lib/libc/arch/vax/gen/bswap32.S @@ -0,0 +1,48 @@ +/* $NetBSD: bswap32.S,v 1.2 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas. + * + * 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 + +#if defined(_KERNEL) || defined(_STANDALONE) +ENTRY(_C_LABEL(bswap32), 0) +#else +#if BYTE_ORDER == LITTLE_ENDIAN +ALTENTRY(_C_LABEL(ntohl)) +ALTENTRY(_C_LABEL(htonl)) +#endif +ENTRY(_C_LABEL(__bswap32), 0) +#endif + rotl $-8, 4(%ap), %r0 + insv %r0, $16, $8, %r0 + rotl $8, 4(%ap), %r1 + movb %r1, %r0 + ret + diff --git a/common/lib/libc/arch/vax/gen/bswap64.S b/common/lib/libc/arch/vax/gen/bswap64.S new file mode 100644 index 000000000..b95bb7441 --- /dev/null +++ b/common/lib/libc/arch/vax/gen/bswap64.S @@ -0,0 +1,19 @@ +/* Written by Anders Magnusson. Public Domain */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "$NetBSD: bswap64.S,v 1.2 2005/12/24 08:33:32 matt Exp $" +#endif /* LIBC_SCCS and not lint */ + +#include + +ENTRY(bswap64, 0) + movq 4(%ap),%r3 + rotl $-8,%r3,%r1 + insv %r1,$16,$8,%r1 + rotl $8,%r3,%r2 + movb %r2,%r1 + rotl $-8,%r4,%r0 + insv %r0,$16,$8,%r0 + rotl $8,%r4,%r2 + movb %r2,%r0 + ret diff --git a/common/lib/libc/arch/vax/gen/udiv.S b/common/lib/libc/arch/vax/gen/udiv.S new file mode 100644 index 000000000..e9819c6ce --- /dev/null +++ b/common/lib/libc/arch/vax/gen/udiv.S @@ -0,0 +1,100 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Donn Seeley at UUNET Technologies, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)udiv.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: udiv.S,v 1.2 2005/12/24 08:33:32 matt Exp $" +#endif /* LIBC_SCCS and not lint */ + +/* + * Unsigned division, PCC flavor. + * udiv() takes an ordinary dividend/divisor pair; + * audiv() takes a pointer to a dividend and an ordinary divisor. + */ + +#include + +#define DIVIDEND 4(%ap) +#define DIVISOR 8(%ap) + +#ifdef __ELF__ +ASENTRY(__udiv,0) +#else +ASENTRY(udiv,0) +#endif + movl DIVISOR,%r2 + jlss Leasy # big divisor: settle by comparison + movl DIVIDEND,%r0 + jlss Lhard # big dividend: extended division + divl2 %r2,%r0 # small divisor and dividend: signed division + ret +Lhard: + clrl %r1 + ediv %r2,%r0,%r0,%r1 + ret +Leasy: + cmpl DIVIDEND,%r2 + jgequ Lone # if dividend is as big or bigger, return 1 + clrl %r0 # else return 0 + ret +Lone: + movl $1,%r0 + ret + +#ifdef __ELF__ +ASENTRY(__audiv,0) +#else +ASENTRY(audiv,0) +#endif + movl DIVIDEND,%r3 + movl DIVISOR,%r2 + jlss La_easy # big divisor: settle by comparison + movl (%r3),%r0 + jlss La_hard # big dividend: extended division + divl2 %r2,%r0 # small divisor and dividend: signed division + movl %r0,(%r3) # leave the value of the assignment in %r0 + ret +La_hard: + clrl %r1 + ediv %r2,%r0,%r0,%r1 + movl %r0,(%r3) + ret +La_easy: + cmpl (%r3),%r2 + jgequ La_one # if dividend is as big or bigger, return 1 + clrl %r0 # else return 0 + clrl (%r3) + ret +La_one: + movl $1,%r0 + movl %r0,(%r3) + ret diff --git a/common/lib/libc/arch/vax/gen/urem.S b/common/lib/libc/arch/vax/gen/urem.S new file mode 100644 index 000000000..ffae00cf1 --- /dev/null +++ b/common/lib/libc/arch/vax/gen/urem.S @@ -0,0 +1,100 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Donn Seeley at UUNET Technologies, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)urem.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: urem.S,v 1.2 2005/12/24 08:33:32 matt Exp $" +#endif /* LIBC_SCCS and not lint */ + +#include + +/* + * Unsigned modulus, PCC flavor. + * urem() takes an ordinary dividend/divisor pair; + * aurem() takes a pointer to a dividend and an ordinary divisor. + */ + +#define DIVIDEND 4(%ap) +#define DIVISOR 8(%ap) + +#ifdef __ELF__ +ASENTRY(__urem,0) +#else +ASENTRY(urem,0) +#endif + movl DIVISOR,%r2 + jlss Leasy # big divisor: settle by comparison + movl DIVIDEND,%r0 + jlss Lhard # big dividend: need extended division + divl3 %r2,%r0,%r1 # small divisor and dividend: signed modulus + mull2 %r2,%r1 + subl2 %r1,%r0 + ret +Lhard: + clrl %r1 + ediv %r2,%r0,%r1,%r0 + ret +Leasy: + subl3 %r2,DIVIDEND,%r0 + jcc Ldifference # if divisor goes in once, return difference + movl DIVIDEND,%r0 # if divisor is bigger, return dividend +Ldifference: + ret + +#ifdef __ELF__ +ASENTRY(__aurem,0) +#else +ASENTRY(aurem,0) +#endif + movl DIVIDEND,%r3 + movl DIVISOR,%r2 + jlss La_easy # big divisor: settle by comparison + movl (%r3),%r0 + jlss La_hard # big dividend: need extended division + divl3 %r2,%r0,%r1 # small divisor and dividend: signed modulus + mull2 %r2,%r1 + subl2 %r1,%r0 + movl %r0,(%r3) # leave the value of the assignment in %r0 + ret +La_hard: + clrl %r1 + ediv %r2,%r0,%r1,%r0 + movl %r0,(%r3) + ret +La_easy: + subl3 %r2,(%r3),%r0 + jcs La_dividend # if divisor is bigger, leave dividend alone + movl %r0,(%r3) # if divisor goes in once, store difference + ret +La_dividend: + movl (%r3),%r0 + ret diff --git a/common/lib/libc/arch/vax/string/memcpy.S b/common/lib/libc/arch/vax/string/memcpy.S new file mode 100644 index 000000000..843451c0d --- /dev/null +++ b/common/lib/libc/arch/vax/string/memcpy.S @@ -0,0 +1,90 @@ +/* $NetBSD: memcpy.S,v 1.2 2005/12/24 08:33:32 matt 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)memcpy.s 8.1 (Berkeley) 6/4/93" +#endif /* LIBC_SCCS and not lint */ + +/* + * void *memcpy(dst, src, size) + * returns dst + * + * This optimises the usual case (count < 65536) at the expense + * of some extra memory references and branches when count >= 65536. + */ + +#include + +ENTRY(memcpy, 0) + movzwl $65535,%r0 /* %r0 = 64K (needed below) */ + movq 8(%ap),%r1 /* %r1 = src, %r2 = length */ + movl 4(%ap),%r3 /* %r3 = dst */ + cmpl %r1,%r3 + bgtru 1f /* normal forward case */ + beql 2f /* equal, nothing to do */ + addl2 %r2,%r1 /* overlaps iff srcdst */ + cmpl %r1,%r3 + bgtru 4f /* overlapping, must move backwards */ + subl2 %r2,%r1 + +1: /* move forward */ + cmpl %r2,%r0 + bgtru 3f /* stupid movc3 limitation */ + movc3 %r2,(%r1),(%r3) /* move it all */ +2: + movl 4(%ap),%r0 /* return original dst */ + ret +3: + subl2 %r0,12(%ap) /* adjust length by 64K */ + movc3 %r0,(%r1),(%r3) /* move 64K */ + movl 12(%ap),%r2 + decw %r0 /* from 0 to 65535 */ + brb 1b /* retry */ + +4: /* move backward */ + addl2 %r2,%r3 +5: + cmpl %r2,%r0 + bgtru 6f /* stupid movc3 limitation */ + subl2 %r2,%r1 + subl2 %r2,%r3 + movc3 %r2,(%r1),(%r3) /* move it all */ + movl 4(%ap),%r0 /* return original dst */ + ret +6: + subl2 %r0,12(%ap) /* adjust length by 64K */ + subl2 %r0,%r1 + subl2 %r0,%r3 + movc3 %r0,(%r1),(%r3) /* move 64K */ + movl 12(%ap),%r2 + decw %r0 + subl2 %r0,%r1 + subl2 %r0,%r3 + brb 5b diff --git a/common/lib/libc/arch/vax/string/memmove.S b/common/lib/libc/arch/vax/string/memmove.S new file mode 100644 index 000000000..9e6516ae2 --- /dev/null +++ b/common/lib/libc/arch/vax/string/memmove.S @@ -0,0 +1,90 @@ +/* $NetBSD: memmove.S,v 1.2 2005/12/24 08:33:32 matt 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)memmove.s 8.1 (Berkeley) 6/4/93" +#endif /* LIBC_SCCS and not lint */ + +/* + * void *memmove(dst, src, size) + * returns dst + * + * This optimises the usual case (count < 65536) at the expense + * of some extra memory references and branches when count >= 65536. + */ + +#include + +ENTRY(memmove, 0) + movzwl $65535,%r0 /* %r0 = 64K (needed below) */ + movq 8(%ap),%r1 /* %r1 = src, %r2 = length */ + movl 4(%ap),%r3 /* %r3 = dst */ + cmpl %r1,%r3 + bgtru 1f /* normal forward case */ + beql 2f /* equal, nothing to do */ + addl2 %r2,%r1 /* overlaps iff srcdst */ + cmpl %r1,%r3 + bgtru 4f /* overlapping, must move backwards */ + subl2 %r2,%r1 + +1: /* move forward */ + cmpl %r2,%r0 + bgtru 3f /* stupid movc3 limitation */ + movc3 %r2,(%r1),(%r3) /* move it all */ +2: + movl 4(%ap),%r0 /* return original dst */ + ret +3: + subl2 %r0,12(%ap) /* adjust length by 64K */ + movc3 %r0,(%r1),(%r3) /* move 64K */ + movl 12(%ap),%r2 + decw %r0 /* from 0 to 65535 */ + brb 1b /* retry */ + +4: /* move backward */ + addl2 %r2,%r3 +5: + cmpl %r2,%r0 + bgtru 6f /* stupid movc3 limitation */ + subl2 %r2,%r1 + subl2 %r2,%r3 + movc3 %r2,(%r1),(%r3) /* move it all */ + movl 4(%ap),%r0 /* return original dst */ + ret +6: + subl2 %r0,12(%ap) /* adjust length by 64K */ + subl2 %r0,%r1 + subl2 %r0,%r3 + movc3 %r0,(%r1),(%r3) /* move 64K */ + movl 12(%ap),%r2 + decw %r0 + subl2 %r0,%r1 + subl2 %r0,%r3 + brb 5b diff --git a/common/lib/libc/arch/vax/string/memset.S b/common/lib/libc/arch/vax/string/memset.S new file mode 100644 index 000000000..670957772 --- /dev/null +++ b/common/lib/libc/arch/vax/string/memset.S @@ -0,0 +1,52 @@ +/* $NetBSD: memset.S,v 1.1 2010/03/12 09:12:34 uwe 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)memset.s 8.1 (Berkeley) 6/4/93" +#endif /* LIBC_SCCS and not lint */ + +/* void *memset(base, c, length) */ + +#include + +ENTRY(memset, 0) + movl 4(%ap),%r3 +1: + movzwl $65535,%r0 + movq 8(%ap),%r1 + cmpl %r2,%r0 + jgtru 2f + movc5 $0,(%r3),%r1,%r2,(%r3) + movl %r1,%r0 + ret +2: + subl2 %r0,12(%ap) + movc5 $0,(%r3),%r1,%r0,(%r3) + jbr 1b diff --git a/common/lib/libc/arch/x86_64/atomic/Makefile.inc b/common/lib/libc/arch/x86_64/atomic/Makefile.inc new file mode 100644 index 000000000..a590a2a8a --- /dev/null +++ b/common/lib/libc/arch/x86_64/atomic/Makefile.inc @@ -0,0 +1,14 @@ +# $NetBSD: Makefile.inc,v 1.6 2009/01/04 17:54:29 pooka Exp $ + +.if defined(LIB) && (${LIB} == "kern" || ${LIB} == "c" || ${LIB} == "pthread" \ + || ${LIB} == "rump") + +SRCS+= atomic.S + +.endif + +.if defined(LIB) && (${LIB} == "c" || ${LIB} == "pthread") + +SRCS+= atomic_init_cas.c + +.endif diff --git a/common/lib/libc/arch/x86_64/atomic/atomic.S b/common/lib/libc/arch/x86_64/atomic/atomic.S new file mode 100644 index 000000000..797f0ee85 --- /dev/null +++ b/common/lib/libc/arch/x86_64/atomic/atomic.S @@ -0,0 +1,404 @@ +/* $NetBSD: atomic.S,v 1.16 2011/01/12 23:12:10 joerg Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe, and by Andrew Doran. + * + * 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 + +#ifdef _KERNEL +#define ALIAS(f, t) STRONG_ALIAS(f,t) +#else +#define ALIAS(f, t) WEAK_ALIAS(f,t) +#endif + +#ifdef _HARDKERNEL +#define LOCK(n) .Lpatch ## n: lock +#define ENDLABEL(a) _ALIGN_TEXT; LABEL(a) +#else +#define LOCK(n) lock +#define ENDLABEL(a) /* nothing */ +#endif + + .text + +/* 32-bit */ + +ENTRY(_atomic_add_32) + LOCK(1) + addl %esi, (%rdi) + ret + +ENTRY(_atomic_add_32_nv) + movl %esi, %eax + LOCK(2) + xaddl %eax, (%rdi) + addl %esi, %eax + ret + +ENTRY(_atomic_and_32) + LOCK(3) + andl %esi, (%rdi) + ret + +ENTRY(_atomic_and_32_nv) + movl (%rdi), %eax +1: + movl %eax, %ecx + andl %esi, %ecx + LOCK(4) + cmpxchgl %ecx, (%rdi) + jnz 1b + movl %ecx, %eax + ret + +ENTRY(_atomic_dec_32) + LOCK(5) + decl (%rdi) + ret + +ENTRY(_atomic_dec_32_nv) + movl $-1, %eax + LOCK(6) + xaddl %eax, (%rdi) + decl %eax + ret + +ENTRY(_atomic_inc_32) + LOCK(7) + incl (%rdi) + ret + +ENTRY(_atomic_inc_32_nv) + movl $1, %eax + LOCK(8) + xaddl %eax, (%rdi) + incl %eax + ret + +ENTRY(_atomic_or_32) + LOCK(9) + orl %esi, (%rdi) + ret + +ENTRY(_atomic_or_32_nv) + movl (%rdi), %eax +1: + movl %eax, %ecx + orl %esi, %ecx + LOCK(10) + cmpxchgl %ecx, (%rdi) + jnz 1b + movl %ecx, %eax + ret + +ENTRY(_atomic_swap_32) + movl %esi, %eax + xchgl %eax, (%rdi) + ret + +ENTRY(_atomic_cas_32) + movl %esi, %eax + LOCK(12) + cmpxchgl %edx, (%rdi) + /* %eax now contains the old value */ + ret + +ENTRY(_atomic_cas_32_ni) + movl %esi, %eax + cmpxchgl %edx, (%rdi) + /* %eax now contains the old value */ + ret + +/* 64-bit */ + +ENTRY(_atomic_add_64) + LOCK(13) + addq %rsi, (%rdi) + ret + +ENTRY(_atomic_add_64_nv) + movq %rsi, %rax + LOCK(14) + xaddq %rax, (%rdi) + addq %rsi, %rax + ret + +ENTRY(_atomic_and_64) + LOCK(15) + andq %rsi, (%rdi) + ret + +ENTRY(_atomic_and_64_nv) + movq (%rdi), %rax +1: + movq %rax, %rcx + andq %rsi, %rcx + LOCK(16) + cmpxchgq %rcx, (%rdi) + jnz 1b + movq %rcx, %rax + ret + +ENTRY(_atomic_dec_64) + LOCK(17) + decq (%rdi) + ret + +ENTRY(_atomic_dec_64_nv) + movq $-1, %rax + LOCK(18) + xaddq %rax, (%rdi) + decq %rax + ret + +ENTRY(_atomic_inc_64) + LOCK(19) + incq (%rdi) + ret + +ENTRY(_atomic_inc_64_nv) + movq $1, %rax + LOCK(20) + xaddq %rax, (%rdi) + incq %rax + ret + +ENTRY(_atomic_or_64) + LOCK(21) + orq %rsi, (%rdi) + ret + +ENTRY(_atomic_or_64_nv) + movq (%rdi), %rax +1: + movq %rax, %rcx + orq %rsi, %rcx + LOCK(22) + cmpxchgq %rcx, (%rdi) + jnz 1b + movq %rcx, %rax + ret + +ENTRY(_atomic_swap_64) + movq %rsi, %rax + xchgq %rax, (%rdi) + ret + +ENTRY(_atomic_cas_64) + movq %rsi, %rax + LOCK(24) + cmpxchgq %rdx, (%rdi) + /* %eax now contains the old value */ + ret + +ENTRY(_atomic_cas_64_ni) + movq %rsi, %rax + cmpxchgq %rdx, (%rdi) + /* %eax now contains the old value */ + ret + +/* memory barriers */ + +ENTRY(_membar_consumer) + LOCK(25) + addq $0, -8(%rsp) + ret +ENDLABEL(membar_consumer_end) + +ENTRY(_membar_producer) + /* A store is enough */ + movq $0, -8(%rsp) + ret +ENDLABEL(membar_producer_end) + +ENTRY(_membar_sync) + LOCK(26) + addq $0, -8(%rsp) + ret +ENDLABEL(membar_sync_end) + +#ifdef _HARDKERNEL +ENTRY(sse2_lfence) + lfence + ret +ENDLABEL(sse2_lfence_end) + +ENTRY(sse2_mfence) + mfence + ret +ENDLABEL(sse2_mfence_end) + +atomic_lockpatch: + .globl atomic_lockpatch + .quad .Lpatch1, .Lpatch2, .Lpatch3, .Lpatch4, .Lpatch5 + .quad .Lpatch6, .Lpatch7, .Lpatch8, .Lpatch9, .Lpatch10 + .quad .Lpatch12, .Lpatch13, .Lpatch14, .Lpatch15 + .quad .Lpatch16, .Lpatch17, .Lpatch18, .Lpatch19, .Lpatch20 + .quad .Lpatch21, .Lpatch22, .Lpatch24, .Lpatch25 + .quad .Lpatch26, 0 +#endif /* _HARDKERNEL */ + +ALIAS(atomic_add_32,_atomic_add_32) +ALIAS(atomic_add_64,_atomic_add_64) +ALIAS(atomic_add_int,_atomic_add_32) +ALIAS(atomic_add_long,_atomic_add_64) +ALIAS(atomic_add_ptr,_atomic_add_64) + +ALIAS(atomic_add_32_nv,_atomic_add_32_nv) +ALIAS(atomic_add_64_nv,_atomic_add_64_nv) +ALIAS(atomic_add_int_nv,_atomic_add_32_nv) +ALIAS(atomic_add_long_nv,_atomic_add_64_nv) +ALIAS(atomic_add_ptr_nv,_atomic_add_64_nv) + +ALIAS(atomic_and_32,_atomic_and_32) +ALIAS(atomic_and_64,_atomic_and_64) +ALIAS(atomic_and_uint,_atomic_and_32) +ALIAS(atomic_and_ulong,_atomic_and_64) +ALIAS(atomic_and_ptr,_atomic_and_64) + +ALIAS(atomic_and_32_nv,_atomic_and_32_nv) +ALIAS(atomic_and_64_nv,_atomic_and_64_nv) +ALIAS(atomic_and_uint_nv,_atomic_and_32_nv) +ALIAS(atomic_and_ulong_nv,_atomic_and_64_nv) +ALIAS(atomic_and_ptr_nv,_atomic_and_64_nv) + +ALIAS(atomic_dec_32,_atomic_dec_32) +ALIAS(atomic_dec_64,_atomic_dec_64) +ALIAS(atomic_dec_uint,_atomic_dec_32) +ALIAS(atomic_dec_ulong,_atomic_dec_64) +ALIAS(atomic_dec_ptr,_atomic_dec_64) + +ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv) +ALIAS(atomic_dec_64_nv,_atomic_dec_64_nv) +ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv) +ALIAS(atomic_dec_ulong_nv,_atomic_dec_64_nv) +ALIAS(atomic_dec_ptr_nv,_atomic_dec_64_nv) + +ALIAS(atomic_inc_32,_atomic_inc_32) +ALIAS(atomic_inc_64,_atomic_inc_64) +ALIAS(atomic_inc_uint,_atomic_inc_32) +ALIAS(atomic_inc_ulong,_atomic_inc_64) +ALIAS(atomic_inc_ptr,_atomic_inc_64) + +ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv) +ALIAS(atomic_inc_64_nv,_atomic_inc_64_nv) +ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv) +ALIAS(atomic_inc_ulong_nv,_atomic_inc_64_nv) +ALIAS(atomic_inc_ptr_nv,_atomic_inc_64_nv) + +ALIAS(atomic_or_32,_atomic_or_32) +ALIAS(atomic_or_uint,_atomic_or_32) +ALIAS(atomic_or_ulong,_atomic_or_64) +ALIAS(atomic_or_ptr,_atomic_or_64) + +ALIAS(atomic_or_32_nv,_atomic_or_32_nv) +ALIAS(atomic_or_64_nv,_atomic_or_64_nv) +ALIAS(atomic_or_uint_nv,_atomic_or_32_nv) +ALIAS(atomic_or_ulong_nv,_atomic_or_64_nv) +ALIAS(atomic_or_ptr_nv,_atomic_or_64_nv) + +ALIAS(atomic_swap_32,_atomic_swap_32) +ALIAS(atomic_swap_64,_atomic_swap_64) +ALIAS(atomic_swap_uint,_atomic_swap_32) +ALIAS(atomic_swap_ulong,_atomic_swap_64) +ALIAS(atomic_swap_ptr,_atomic_swap_64) + +ALIAS(atomic_cas_32,_atomic_cas_32) +ALIAS(atomic_cas_64,_atomic_cas_64) +ALIAS(atomic_cas_uint,_atomic_cas_32) +ALIAS(atomic_cas_ulong,_atomic_cas_64) +ALIAS(atomic_cas_ptr,_atomic_cas_64) + +ALIAS(atomic_cas_32_ni,_atomic_cas_32_ni) +ALIAS(atomic_cas_64_ni,_atomic_cas_64_ni) +ALIAS(atomic_cas_uint_ni,_atomic_cas_32_ni) +ALIAS(atomic_cas_ulong_ni,_atomic_cas_64_ni) +ALIAS(atomic_cas_ptr_ni,_atomic_cas_64_ni) + +ALIAS(membar_consumer,_membar_consumer) +ALIAS(membar_producer,_membar_producer) +ALIAS(membar_enter,_membar_consumer) +ALIAS(membar_exit,_membar_producer) +ALIAS(membar_sync,_membar_sync) + +STRONG_ALIAS(_atomic_add_int,_atomic_add_32) +STRONG_ALIAS(_atomic_add_long,_atomic_add_64) +STRONG_ALIAS(_atomic_add_ptr,_atomic_add_64) + +STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv) +STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_64_nv) +STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_64_nv) + +STRONG_ALIAS(_atomic_and_uint,_atomic_and_32) +STRONG_ALIAS(_atomic_and_ulong,_atomic_and_64) +STRONG_ALIAS(_atomic_and_ptr,_atomic_and_64) + +STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv) +STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_64_nv) +STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_64_nv) + +STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32) +STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_64) +STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_64) + +STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv) +STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_64_nv) +STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_64_nv) + +STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32) +STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_64) +STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_64) + +STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv) +STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_64_nv) +STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_64_nv) + +STRONG_ALIAS(_atomic_or_uint,_atomic_or_32) +STRONG_ALIAS(_atomic_or_ulong,_atomic_or_64) +STRONG_ALIAS(_atomic_or_ptr,_atomic_or_64) + +STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv) +STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_64_nv) +STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_64_nv) + +STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32) +STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_64) +STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_64) + +STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32) +STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_64) +STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_64) + +STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32_ni) +STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_64_ni) +STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_64_ni) + +STRONG_ALIAS(_membar_enter,_membar_consumer) +STRONG_ALIAS(_membar_exit,_membar_producer) diff --git a/common/lib/libc/arch/x86_64/gen/byte_swap_2.S b/common/lib/libc/arch/x86_64/gen/byte_swap_2.S new file mode 100644 index 000000000..288aaf6f9 --- /dev/null +++ b/common/lib/libc/arch/x86_64/gen/byte_swap_2.S @@ -0,0 +1,19 @@ +/* $NetBSD: byte_swap_2.S,v 1.2 2006/02/04 21:53:31 uwe Exp $ */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: byte_swap_2.S,v 1.2 2006/02/04 21:53:31 uwe Exp $") +#endif + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap16)) +#else +_ENTRY(_C_LABEL(__bswap16)) +#endif +_ENTRY(_C_LABEL(ntohs)) +_ENTRY(_C_LABEL(htons)) +_PROF_PROLOGUE + movl %edi,%eax + xchgb %ah,%al + ret diff --git a/common/lib/libc/arch/x86_64/gen/byte_swap_4.S b/common/lib/libc/arch/x86_64/gen/byte_swap_4.S new file mode 100644 index 000000000..ee8006e22 --- /dev/null +++ b/common/lib/libc/arch/x86_64/gen/byte_swap_4.S @@ -0,0 +1,18 @@ +/* $NetBSD: byte_swap_4.S,v 1.2 2006/02/04 21:53:31 uwe Exp $ */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: byte_swap_4.S,v 1.2 2006/02/04 21:53:31 uwe Exp $") +#endif + +#if defined(_KERNEL) || defined(_STANDALONE) +_ENTRY(_C_LABEL(bswap32)) +#else +_ENTRY(_C_LABEL(__bswap32)) +#endif +_ENTRY(_C_LABEL(ntohl)) +_ENTRY(_C_LABEL(htonl)) +_PROF_PROLOGUE + movl %edi,%eax + bswapl %eax + ret diff --git a/common/lib/libc/arch/x86_64/gen/byte_swap_8.S b/common/lib/libc/arch/x86_64/gen/byte_swap_8.S new file mode 100644 index 000000000..1ac0df266 --- /dev/null +++ b/common/lib/libc/arch/x86_64/gen/byte_swap_8.S @@ -0,0 +1,17 @@ +/* $NetBSD: byte_swap_8.S,v 1.1 2010/01/14 02:09:46 joerg Exp $ */ + +/* + * Written by Frank van der Linden (fvdl@wasabisystems.com) + * Public Domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: byte_swap_8.S,v 1.1 2010/01/14 02:09:46 joerg Exp $") +#endif + +ENTRY(bswap64) + bswapq %rdi + movq %rdi,%rax + ret diff --git a/common/lib/libc/arch/x86_64/string/bcmp.S b/common/lib/libc/arch/x86_64/string/bcmp.S new file mode 100644 index 000000000..03671a8e8 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/bcmp.S @@ -0,0 +1,23 @@ +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: bcmp.S,v 1.2 2007/11/12 18:41:59 ad Exp $") +#endif + +ENTRY(bcmp) + xorl %eax,%eax /* clear return value */ + + movq %rdx,%rcx /* compare by words */ + shrq $3,%rcx + repe + cmpsq + jne L1 + + movq %rdx,%rcx /* compare remainder by bytes */ + andq $7,%rcx + repe + cmpsb + je L2 + +L1: incl %eax +L2: ret diff --git a/common/lib/libc/arch/x86_64/string/bcopy.S b/common/lib/libc/arch/x86_64/string/bcopy.S new file mode 100644 index 000000000..8b0c4f3d1 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/bcopy.S @@ -0,0 +1,160 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from locore.s. + * + * 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 + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: bcopy.S,v 1.4 2009/11/22 17:25:47 dsl Exp $") +#endif + + /* + * (ov)bcopy (src,dst,cnt) + * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800 + * + * Hacked about by dsl@netbsd.org + */ + +#ifdef MEMCOPY +ENTRY(memcpy) +#define NO_OVERLAP +#else +#ifdef MEMMOVE +ENTRY(memmove) +#else +ENTRY(bcopy) +#endif +#endif + movq %rdx,%rcx +#if defined(MEMCOPY) || defined(MEMMOVE) + movq %rdi,%rax /* must return destination address */ + mov %rdi,%r11 /* for misaligned check */ +#else + mov %rsi,%r11 /* for misaligned check */ + xchgq %rdi,%rsi /* bcopy() has arg order reversed */ +#endif + +#if !defined(NO_OVERLAP) + movq %rdi,%r8 + subq %rsi,%r8 +#endif + + shrq $3,%rcx /* count for copy by words */ + jz 8f /* j if less than 8 bytes */ + + lea -8(%rdi,%rdx),%r9 /* target address of last 8 */ + mov -8(%rsi,%rdx),%r10 /* get last word */ +#if !defined(NO_OVERLAP) + cmpq %rdx,%r8 /* overlapping? */ + jb 10f +#endif + +/* + * Non-overlaping, copy forwards. + * Newer Intel cpus (Nehalem) will do 16byte read/write transfers + * if %ecx is more than 76. + * AMD might do something similar some day. + */ + and $7,%r11 /* destination misaligned ? */ + jnz 2f + rep + movsq + mov %r10,(%r9) /* write last word */ + ret + +/* + * Destination misaligned + * AMD say it is better to align the destination (not the source). + * This will also re-align copies if the source and dest are both + * misaligned by the same amount) + * (I think Nehalem will use its accelerated copy if the source + * and destination have the same alignment.) + */ +2: + lea -9(%r11,%rdx),%rcx /* post re-alignment count */ + neg %r11 /* now -1 .. -7 */ + mov (%rsi),%rdx /* get first word */ + mov %rdi,%r8 /* target for first word */ + lea 8(%rsi,%r11),%rsi + lea 8(%rdi,%r11),%rdi + shr $3,%rcx + rep + movsq + mov %rdx,(%r8) /* write first word */ + mov %r10,(%r9) /* write last word */ + ret + +#if !defined(NO_OVERLAP) +/* Must copy backwards. + * Reverse copy is probably easy to code faster than 'rep movds' + * since that requires (IIRC) an extra clock every 3 iterations (AMD). + * However I don't suppose anything cares that much! + * The big cost is the std/cld pair - reputedly 50+ cycles on Netburst P4. + * The copy is aligned with the buffer start (more likely to + * be a multiple of 8 than the end). + */ +10: + lea -8(%rsi,%rcx,8),%rsi + lea -8(%rdi,%rcx,8),%rdi + std + rep + movsq + cld + mov %r10,(%r9) /* write last bytes */ + ret +#endif + +/* Less than 8 bytes to copy, copy by bytes */ +/* Intel Nehalem optimise 'rep movsb' for <= 7 bytes (9-15 clocks). + * For longer transfers it is 50+ ! + */ +8: mov %rdx,%rcx + +#if !defined(NO_OVERLAP) + cmpq %rdx,%r8 /* overlapping? */ + jb 81f +#endif + + /* nope, copy forwards. */ + rep + movsb + ret + +#if !defined(NO_OVERLAP) +/* Must copy backwards */ +81: + lea -1(%rsi,%rcx),%rsi + lea -1(%rdi,%rcx),%rdi + std + rep + movsb + cld + ret +#endif diff --git a/common/lib/libc/arch/x86_64/string/bzero.S b/common/lib/libc/arch/x86_64/string/bzero.S new file mode 100644 index 000000000..27c6e4a8a --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/bzero.S @@ -0,0 +1,8 @@ +/* + * Code for bzero() is in memset.S + * + * This file is left in place so that libc.a doesn't get a duplicate + * symbol error on update builds. + * + * If you are reading this in 2010, delete the file! + */ diff --git a/common/lib/libc/arch/x86_64/string/ffs.S b/common/lib/libc/arch/x86_64/string/ffs.S new file mode 100644 index 000000000..77934ce5c --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/ffs.S @@ -0,0 +1,20 @@ +/* + * Written by J.T. Conklin . + * Public domain. + * Adapted for NetBSD/x86_64 by Frank van der Linden + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: ffs.S,v 1.4 2009/07/20 15:21:00 christos Exp $") +#endif + +ENTRY(ffs) + bsfl %edi,%eax + jz 1f /* ZF is set if all bits are 0 */ + incl %eax /* bits numbered from 1, not 0 */ + ret + +1: xorl %eax,%eax /* clear result */ + ret diff --git a/common/lib/libc/arch/x86_64/string/memchr.S b/common/lib/libc/arch/x86_64/string/memchr.S new file mode 100644 index 000000000..5531a51b9 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/memchr.S @@ -0,0 +1,108 @@ +/* $NetBSD: memchr.S,v 1.5 2009/08/01 20:47:02 dsl Exp $ */ + +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Laight. + * + * 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 + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: memchr.S,v 1.5 2009/08/01 20:47:02 dsl Exp $") +#endif + +/* + * The instruction sequences used try to avoid data dependencies + * between adjacent instructions (to allow parallel execution). + * The 'imul' for %r9 could be put into the delay following the + * memory read (ie inside the loop) at no obvious cost - except + * that the loop is currently exactly 32 bytes - 2 fetch blocks!. + * + * I don't think aligning any of the other branch targets is useful. + */ + +ENTRY(memchr) + movabsq $0x0101010101010101,%r8 + lea (%rdi,%rdx),%r10 /* limit of buffer to scan */ + movzbq %sil,%rsi /* mask high bits! */ + + /* 'directpath' imuls can execute 3 at a time ... (amd) */ + imul %r8,%rsi /* search byte replicated in word */ + imul $0x80,%r8,%r9 /* 0x8080808080808080 */ + test $7,%dil + jnz 20f /* jump if misaligned */ + jmp 1f /* jump to avoid 4 nops (13 bytes) in gap */ + + _ALIGN_TEXT /* entire loop now in 32 aligned bytes */ +1: + cmpq %r10,%rdi /* end of buffer ? */ + jae 30f /* jump if so */ + + movq (%rdi),%rax /* value to check */ + addq $8,%rdi + xorq %rsi,%rax /* now looking for zeros */ +2: + mov %rax,%rcx + subq %r8,%rax /* x - 0x01 */ + not %rcx + andq %r9,%rax /* (x - 0x01) & 0x80 */ + andq %rcx,%rax /* ((x - 0x01) & 0x80) & ~x */ + je 1b /* jump if not found */ + +/* Found byte in word, get its address */ + bsf %rax,%rax + shr $3,%eax + lea -8(%rax,%rdi),%rax + cmpq %r10,%rax /* need to check not beyond buffer */ + jae 30f + rep + ret /* amd - no ret after jmp */ + +/* Input misaligned, read aligned and make low bytes invalid */ +20: + mov %dil,%cl /* misalignment amount 1..7 (+high bits )*/ + and $~7,%dil /* %rdi now start of word */ + test %rdx,%rdx /* zero length, don't read */ + jz 30f + + neg %cl /* 7..1 (+high bits) */ + mov (%rdi),%rax /* word containing first byte */ + addq $8,%rdi + and $7,%cl /* 7..1 */ + + mov %r8,%r11 /* any value with bits in each byte */ + shl $3,%cl /* 56..8 */ + xorq %rsi,%rax /* now looking for zeros */ + + /* Set low bytes non-zero */ + shr %cl,%r11 /* non-zero in unwanted bytes */ + or %r11,%rax /* low bytes now set */ + jmp 2b + +/* Not found */ +30: xorq %rax,%rax + ret diff --git a/common/lib/libc/arch/x86_64/string/memcmp.S b/common/lib/libc/arch/x86_64/string/memcmp.S new file mode 100644 index 000000000..86928e5df --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/memcmp.S @@ -0,0 +1,39 @@ +/* + * Written by J.T. Conklin . + * Public domain. + * Adapted for NetBSD/x86_64 by Frank van der Linden + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: memcmp.S,v 1.2 2007/11/12 18:41:59 ad Exp $") +#endif + +ENTRY(memcmp) + movq %rdx,%rcx /* compare by longs */ + shrq $3,%rcx + repe + cmpsq + jne L5 /* do we match so far? */ + + movq %rdx,%rcx /* compare remainder by bytes */ + andq $7,%rcx + repe + cmpsb + jne L6 /* do we match? */ + + xorl %eax,%eax /* we match, return zero */ + ret + +L5: movl $8,%ecx /* We know that one of the next */ + subq %rcx,%rdi /* eight pairs of bytes do not */ + subq %rcx,%rsi /* match. */ + repe + cmpsb +L6: xorl %eax,%eax /* Perform unsigned comparison */ + movb -1(%rdi),%al + xorl %edx,%edx + movb -1(%rsi),%dl + subl %edx,%eax + ret diff --git a/common/lib/libc/arch/x86_64/string/memcpy.S b/common/lib/libc/arch/x86_64/string/memcpy.S new file mode 100644 index 000000000..a53243b5f --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/memcpy.S @@ -0,0 +1,4 @@ +/* $NetBSD: memcpy.S,v 1.1 2005/12/20 19:28:51 christos Exp $ */ + +#define MEMCOPY +#include "bcopy.S" diff --git a/common/lib/libc/arch/x86_64/string/memmove.S b/common/lib/libc/arch/x86_64/string/memmove.S new file mode 100644 index 000000000..f4b7b0825 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/memmove.S @@ -0,0 +1,4 @@ +/* $NetBSD: memmove.S,v 1.1 2005/12/20 19:28:51 christos Exp $ */ + +#define MEMMOVE +#include "bcopy.S" diff --git a/common/lib/libc/arch/x86_64/string/memset.S b/common/lib/libc/arch/x86_64/string/memset.S new file mode 100644 index 000000000..8891e7ebf --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/memset.S @@ -0,0 +1,91 @@ +/* $NetBSD: memset.S,v 1.3 2009/08/01 20:35:45 dsl Exp $ */ + +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Laight. + * + * 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 + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: memset.S,v 1.3 2009/08/01 20:35:45 dsl Exp $") +#endif + +#ifndef _KERNEL +/* bzero, %rdi is buffer, %rsi length */ + +ENTRY(bzero) + mov %rsi,%rdx /* length */ + xor %eax,%eax /* value to write */ + jmp 1f +#endif + +/* memset, %rdi is buffer, %rsi char to fill, %rdx length */ + +ENTRY(memset) + movzbq %sil,%rax /* byte value to fill */ + mov %rdx,%rsi /* copy of length */ + mov $0x0101010101010101,%r9 + imul %r9,%rax /* fill value in all bytes */ + +1: + mov %rdi,%r9 /* Need to return buffer address */ + or %edi,%edx /* address | length */ + mov %rsi,%rcx + cmp $7,%rsi + jbe 10f /* jump if short fill */ + test $7,%dl /* check for misaligned fill */ + jnz 20f /* jump if misaligned */ + +/* Target aligned and length multiple of 8 */ +2: + shr $3,%rcx + rep stosq + mov %r9,%rax + ret + +/* + * Short transfer, any faffing here will generate mispredicted branches. + * So we keep it simple. + */ +10: rep stosb + mov %r9,%rax + ret + +/* + * Buffer or length misaligned. + * Write pattern to first and last word of buffer, then fill middle. + * (This writes to some bytes more than once - possibly three times!.) + */ +20: + mov %rax,(%rdi) + movzbq %dil,%rdx /* low address for alignment */ + mov %rax,-8(%rcx,%rdi) + and $7,%dl /* offset in word */ + sub %rdx,%rcx /* adjust length ... */ + add %rdx,%rdi /* ... and target */ + jmp 2b diff --git a/common/lib/libc/arch/x86_64/string/strcat.S b/common/lib/libc/arch/x86_64/string/strcat.S new file mode 100644 index 000000000..9dd99a874 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/strcat.S @@ -0,0 +1,164 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strcat.S,v 1.1 2005/12/20 19:28:51 christos Exp $") +#endif + +ENTRY(strcat) + movq %rdi,%rax + movabsq $0x0101010101010101,%r8 + movabsq $0x8080808080808080,%r9 + + /* + * Align destination to word boundary. + * Consider unrolling loop? + */ +.Lscan: +.Lscan_align: + testb $7,%dil + je .Lscan_aligned + cmpb $0,(%rdi) + je .Lcopy + incq %rdi + jmp .Lscan_align + + _ALIGN_TEXT +.Lscan_aligned: +.Lscan_loop: + movq (%rdi),%rdx + addq $8,%rdi + subq %r8,%rdx + testq %r9,%rdx + je .Lscan_loop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word equal 0. + */ + + cmpb $0,-8(%rdi) /* 1st byte == 0? */ + jne 1f + subq $8,%rdi + jmp .Lcopy + +1: cmpb $0,-7(%rdi) /* 2nd byte == 0? */ + jne 1f + subq $7,%rdi + jmp .Lcopy + +1: cmpb $0,-6(%rdi) /* 3rd byte == 0? */ + jne 1f + subq $6,%rdi + jmp .Lcopy + +1: cmpb $0,-5(%rdi) /* 4th byte == 0? */ + jne 1f + subq $5,%rdi + jmp .Lcopy + +1: cmpb $0,-4(%rdi) /* 5th byte == 0? */ + jne 1f + subq $4,%rdi + jmp .Lcopy + +1: cmpb $0,-3(%rdi) /* 6th byte == 0? */ + jne 1f + subq $3,%rdi + jmp .Lcopy + +1: cmpb $0,-2(%rdi) /* 7th byte == 0? */ + jne 1f + subq $2,%rdi + jmp .Lcopy + +1: cmpb $0,-1(%rdi) /* 8th byte == 0? */ + jne .Lscan_loop + subq $1,%rdi + + /* + * Align source to a word boundary. + * Consider unrolling loop? + */ +.Lcopy: +.Lcopy_align: + testb $7,%sil + je .Lcopy_aligned + movb (%rsi),%dl + incq %rsi + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl + jne .Lcopy_align + ret + + _ALIGN_TEXT +.Lcopy_loop: + movq %rdx,(%rdi) + addq $8,%rdi +.Lcopy_aligned: + movq (%rsi),%rdx + movq %rdx,%rcx + addq $8,%rsi + subq %r8,%rcx + testq %r9,%rcx + je .Lcopy_loop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word equal 0. + */ + + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 1st byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 2nd byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 3rd byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 4th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 5th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 6th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 7th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 8th byte == 0? */ + jne .Lcopy_aligned + +.Ldone: + ret diff --git a/common/lib/libc/arch/x86_64/string/strchr.S b/common/lib/libc/arch/x86_64/string/strchr.S new file mode 100644 index 000000000..a223774c0 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/strchr.S @@ -0,0 +1,153 @@ +/* $NetBSD: strchr.S,v 1.6 2009/07/20 15:21:00 christos Exp $ */ + +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Laight. + * + * 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. + */ + +/* See comments in strlen.S about checking words for byte values */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strchr.S,v 1.6 2009/07/20 15:21:00 christos Exp $") +#endif + +/* + * On entry %rdi is the buffer and the low byte of %rsi (%sil) the + * character to search for. + * + * Registers %rdx, %rcx, %r8-%r11 and %rax are also usable + */ + +/* Uncomment below to get regression test to run this version but + * have everything else use the trivial one below. */ +/* #define TEST_STRCHR */ + +#ifdef TEST_STRCHR +ENTRY(test_strchr) +#else +ENTRY(strchr) +#endif + movabsq $0x0101010101010101,%r8 + + movzbq %sil,%rdx /* value to search for (c) */ + /* These imul are 'directpath' on athlons, so are fast */ + imul $0x80,%r8,%r9 /* 0x8080808080808080 */ + imul %r8,%rdx /* (c) copied to all bytes */ + test $7,%dil + jnz 20f /* jump if misaligned */ + + _ALIGN_TEXT /* one byte nop */ +1: + movq (%rdi),%rax /* bytes to check (x) */ +2: + addq $8,%rdi + mov %rax,%r10 + mov %rax,%r11 /* for 'char' check */ + not %r10 /* invert of data (~x) */ + + xorq %rdx,%r11 /* convert 'char' test to one for NUL */ + subq %r8,%rax /* x - 0x10 */ + movq %r10,%rsi /* ~x */ + subq %r8,%r11 /* (x ^ c) - 0x10 */ +/* + * Here we could check ((x - 0x10) | ((x ^ c) - 0x10)) & 0x80 + * and short-circuit the case where no top bits are set, and + * we continue the loop. + * However it needs 3 more clocks that are difficult to interleave + * in the existing dependency chain ... + */ + andq %r9,%rax /* (x - 0x10) & 0x80 */ + xorq %rdx,%rsi /* c ^ ~x == ~(c ^ x) */ + andq %r9,%r11 /* ((x ^ c) - 0x10) & 0x80 */ + andq %r10,%rax /* (x - 0x10) & 0x80 & ~x */ + jne 10f /* jump if string ends */ + andq %rsi,%r11 /* ((x ^ c) - 0x10) & 0x80 & ~(x ^ c) */ + je 1b /* jump if no match */ + + /* Found char, since LE can use bit scan */ + bsf %r11,%r11 /* 7, 15, 23 ... 63 */ +8: shr $3,%r11 /* 0, 1, 2 .. 7 */ + lea -8(%r11,%rdi),%rax + ret + +/* End of string, check whether char is before NUL */ + _ALIGN_TEXT /* adds three byte nop */ +10: + bsf %rax,%rax /* count to NUL */ + andq %rsi,%r11 /* check for char in last 8 bytes */ + je 11f + bsf %r11,%r11 /* NUL and char - see which was first */ + cmp %r11,%rax + jae 8b /* return 'found' if same - searching for NUL */ +11: xor %eax,%eax /* char not found */ + ret + +/* Source misaligned: read aligned word and make low bytes invalid */ +/* I (dsl) think a _ALIGN_TEXT here will slow things down! */ +20: + xor %rcx,%rcx + sub %dil,%cl /* Convert low address values 1..7 ... */ + sbb %rsi,%rsi /* carry was set, so %rsi now ~0u! */ + and $7,%cl /* ... to 7..1 */ + and $~7,%dil /* move address to start of word */ + shl $3,%cl /* now 56, 48 ... 16, 8 */ + movq (%rdi),%rax /* aligned word containing first data */ + xor %rdx,%rsi /* invert of search pattern (~c) */ + je 22f /* searching for 0xff */ +21: shr %cl,%rsi /* ~c in low bytes */ + or %rsi,%rax /* set some bits making low bytes invalid */ + jmp 2b + +/* We are searching for 0xff, so can't use ~pattern for invalid value */ +22: + mov %r8,%r10 /* 0x01 pattern */ + lea (%r8,%r8),%rsi /* 0x02 - bits gets set (above) */ + not %r10 /* now 0xfe */ + sar %cl,%r10 /* top bytes 0xff */ + and %r10,%rax /* clear lsb from unwanted low bytes */ + jmp 21b + +#ifdef TEST_STRCHR +/* Trivial version for bug-fixing above */ +ENTRY(strchr) + movq %rsi,%rdx + movq %rdi,%rsi +1: + lodsb + cmp %al,%dl + je 2f + test %al,%al + jne 1b + xor %eax,%eax + ret +2: lea -1(%rsi),%rax + ret +#endif + +STRONG_ALIAS(index,strchr) diff --git a/common/lib/libc/arch/x86_64/string/strcmp.S b/common/lib/libc/arch/x86_64/string/strcmp.S new file mode 100644 index 000000000..ce05ff4b5 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/strcmp.S @@ -0,0 +1,72 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strcmp.S,v 1.1 2005/12/20 19:28:51 christos Exp $") +#endif + +ENTRY(strcmp) + /* + * Align s1 to word boundary. + * Consider unrolling loop? + */ +.Ls1align: + testb $7,%dil + je .Ls1aligned + movb (%rdi),%al + incq %rdi + movb (%rsi),%dl + incq %rsi + testb %al,%al + je .Ldone + cmpb %al,%dl + je .Ls1align + jmp .Ldone + + /* + * Check whether s2 is aligned to a word boundary. If it is, we + * can compare by words. Otherwise we have to compare by bytes. + */ +.Ls1aligned: + testb $7,%sil + jne .Lbyte_loop + + movabsq $0x0101010101010101,%r8 + subq $8,%rdi + movabsq $0x8080808080808080,%r9 + subq $8,%rsi + + _ALIGN_TEXT +.Lword_loop: + movq 8(%rdi),%rax + addq $8,%rdi + movq 8(%rsi),%rdx + addq $8,%rsi + cmpq %rax,%rdx + jne .Lbyte_loop + subq %r8,%rdx + notq %rax + andq %rax,%rdx + testq %r9,%rdx + je .Lword_loop + + _ALIGN_TEXT +.Lbyte_loop: + movb (%rdi),%al + incq %rdi + movb (%rsi),%dl + incq %rsi + testb %al,%al + je .Ldone + cmpb %al,%dl + je .Lbyte_loop + +.Ldone: + movzbq %al,%rax + movzbq %dl,%rdx + subq %rdx,%rax + ret diff --git a/common/lib/libc/arch/x86_64/string/strcpy.S b/common/lib/libc/arch/x86_64/string/strcpy.S new file mode 100644 index 000000000..02cb40879 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/strcpy.S @@ -0,0 +1,111 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strcpy.S,v 1.1 2005/12/20 19:28:51 christos Exp $") +#endif + +/* + * This strcpy implementation copies a byte at a time until the + * source pointer is aligned to a word boundary, it then copies by + * words until it finds a word containing a zero byte, and finally + * copies by bytes until the end of the string is reached. + * + * While this may result in unaligned stores if the source and + * destination pointers are unaligned with respect to each other, + * it is still faster than either byte copies or the overhead of + * an implementation suitable for machines with strict alignment + * requirements. + */ + +ENTRY(strcpy) + movq %rdi,%rax + movabsq $0x0101010101010101,%r8 + movabsq $0x8080808080808080,%r9 + + /* + * Align source to a word boundary. + * Consider unrolling loop? + */ + _ALIGN_TEXT +.Lalign: + testb $7,%sil + je .Lword_aligned + movb (%rsi),%dl + incq %rsi + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl + jne .Lalign + ret + + _ALIGN_TEXT +.Lloop: + movq %rdx,(%rdi) + addq $8,%rdi +.Lword_aligned: + movq (%rsi),%rdx + movq %rdx,%rcx + addq $8,%rsi + subq %r8,%rcx + testq %r9,%rcx + je .Lloop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word equal 0. + */ + + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 1st byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 2nd byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 3rd byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 4th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 5th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 6th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 7th byte == 0? */ + je .Ldone + + shrq $8,%rdx + movb %dl,(%rdi) + incq %rdi + testb %dl,%dl /* 8th byte == 0? */ + jne .Lword_aligned + +.Ldone: + ret diff --git a/common/lib/libc/arch/x86_64/string/strlen.S b/common/lib/libc/arch/x86_64/string/strlen.S new file mode 100644 index 000000000..01391d943 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/strlen.S @@ -0,0 +1,176 @@ +/* $NetBSD: strlen.S,v 1.5 2009/07/12 21:24:21 dsl Exp $ */ + +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Laight. + * + * 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. + */ + +/* + * Inspired by a version written by J.T. Conklin + * (Only the long comment really remains his work!) + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strlen.S,v 1.5 2009/07/12 21:24:21 dsl Exp $") +#endif + +/* + * There are many well known branch-free sequences which are used + * for determining whether a zero-byte is contained within a word. + * These sequences are generally much more efficent than loading + * and comparing each byte individually. + * + * The expression [1,2]: + * + * (1) ~(((x & 0x7f....7f) + 0x7f....7f) | (x | 0x7f....7f)) + * + * evaluates to a non-zero value if any of the bytes in the + * original word is zero. + * + * It also has the useful property that bytes in the result word + * that correspond to non-zero bytes in the original word have + * the value 0x00, while bytes corresponding to zero bytes have + * the value 0x80. This allows calculation of the first (and + * last) occurrence of a zero byte within the word (useful for C's + * str* primitives) by counting the number of leading (or + * trailing) zeros and dividing the result by 8. On machines + * without (or with slow) clz() / ctz() instructions, testing + * each byte in the result word for zero is necessary. + * + * This typically takes 4 instructions (5 on machines without + * "not-or") not including those needed to load the constant. + * + * + * The expression: + * + * (2) ((x - 0x01....01) & 0x80....80 & ~x) + * + * evaluates to a non-zero value if any of the bytes in the + * original word is zero. + * + * On little endian machines, the first byte in the result word + * that corresponds to a zero byte in the original byte is 0x80, + * so clz() can be used as above. On big endian machines, and + * little endian machines without (or with a slow) clz() insn, + * testing each byte in the original for zero is necessary. + * + * This typically takes 3 instructions (4 on machines without + * "and with complement") not including those needed to load + * constants. + * + * + * The expression: + * + * (3) ((x - 0x01....01) & 0x80....80) + * + * always evaluates to a non-zero value if any of the bytes in + * the original word is zero or has the top bit set. + * For strings that are likely to only contain 7-bit ascii these + * false positives will be rare. + * + * To account for possible false positives, each byte of the + * original word must be checked when the expression evaluates to + * a non-zero value. However, because it is simpler than those + * presented above, code that uses it will be faster as long as + * the rate of false positives is low. + * + * This is likely, because the the false positive can only occur + * if the most siginificant bit of a byte within the word is set. + * The expression will never fail for typical 7-bit ASCII strings. + * + * This typically takes 2 instructions not including those needed + * to load constants. + * + * + * [1] Henry S. Warren Jr., "Hacker's Delight", Addison-Westley 2003 + * + * [2] International Business Machines, "The PowerPC Compiler Writer's + * Guide", Warthman Associates, 1996 + */ + +#ifdef TEST_STRLEN +ENTRY(test_strlen) +#else +ENTRY(strlen) +#endif + movabsq $0x0101010101010101,%r8 + + test $7,%dil + movq %rdi,%rax /* Buffer, %rdi unchanged */ + movabsq $0x8080808080808080,%r9 + jnz 10f /* Jump if misaligned */ + + _ALIGN_TEXT +1: + movq (%rax),%rdx /* get bytes to check */ +2: + addq $8,%rax + mov %rdx,%rcx /* save for later check */ + subq %r8,%rdx /* alg (3) above first */ + not %rcx /* Invert of data */ + andq %r9,%rdx + je 1b /* jump if all 0x01-0x80 */ + + /* Do check from alg (2) above - loops for 0x81..0xff bytes */ + andq %rcx,%rdx + je 1b + + /* Since we are LE, use bit scan for first 0x80 byte */ + sub %rdi,%rax /* length to next word */ + bsf %rdx,%rdx /* 7, 15, 23 ... 63 */ + shr $3,%rdx /* 0, 1, 2 ... 7 */ + lea -8(%rax,%rdx),%rax + ret + +/* Misaligned, read aligned word and make low bytes non-zero */ + _ALIGN_TEXT +10: + mov %al,%cl + mov $1,%rsi + and $7,%cl /* offset into word 1..7 */ + and $~7,%al /* start of word with buffer */ + shl $3,%cl /* bit count 8, 16 .. 56 */ + movq (%rax),%rdx /* first data in high bytes */ + shl %cl,%rsi + dec %rsi + or %rsi,%rdx /* low bytes now non-zero */ + jmp 2b + +#ifdef TEST_STRLEN +/* trivial implementation when testing above! */ +ENTRY(strlen) + mov %rdi,%rax +1: + cmpb $0,(%rax) + jz 2f + inc %rax + jmp 1b +2: sub %rdi,%rax + ret +#endif diff --git a/common/lib/libc/arch/x86_64/string/strrchr.S b/common/lib/libc/arch/x86_64/string/strrchr.S new file mode 100644 index 000000000..9f3ebb108 --- /dev/null +++ b/common/lib/libc/arch/x86_64/string/strrchr.S @@ -0,0 +1,124 @@ +/* + * Written by J.T. Conklin + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strrchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $") +#endif + +ENTRY(strrchr) + movzbq %sil,%rcx + + /* zero return value */ + xorq %rax,%rax + + /* + * Align to word boundary. + * Consider unrolling loop? + */ +.Lalign: + testb $7,%dil + je .Lword_aligned + movb (%rdi),%dl + cmpb %cl,%dl + cmoveq %rdi,%rax + incq %rdi + testb %dl,%dl + jne .Lalign + jmp .Ldone + +.Lword_aligned: + /* copy char to all bytes in word */ + movb %cl,%ch + movq %rcx,%rdx + salq $16,%rcx + orq %rdx,%rcx + movq %rcx,%rdx + salq $32,%rcx + orq %rdx,%rcx + + movabsq $0x0101010101010101,%r8 + movabsq $0x8080808080808080,%r9 + + /* Check whether any byte in the word is equal to ch or 0. */ + _ALIGN_TEXT +.Lloop: + movq (%rdi),%rdx + addq $8,%rdi + movq %rdx,%rsi + subq %r8,%rdx + xorq %rcx,%rsi + subq %r8,%rsi + orq %rsi,%rdx + testq %r9,%rdx + je .Lloop + + /* + * In rare cases, the above loop may exit prematurely. We must + * return to the loop if none of the bytes in the word match + * ch or are equal to 0. + */ + + movb -8(%rdi),%dl + cmpb %cl,%dl /* 1st byte == ch? */ + jne 1f + leaq -8(%rdi),%rax +1: testb %dl,%dl /* 1st byte == 0? */ + je .Ldone + + movb -7(%rdi),%dl + cmpb %cl,%dl /* 2nd byte == ch? */ + jne 1f + leaq -7(%rdi),%rax +1: testb %dl,%dl /* 2nd byte == 0? */ + je .Ldone + + movb -6(%rdi),%dl + cmpb %cl,%dl /* 3rd byte == ch? */ + jne 1f + leaq -6(%rdi),%rax +1: testb %dl,%dl /* 3rd byte == 0? */ + je .Ldone + + movb -5(%rdi),%dl + cmpb %cl,%dl /* 4th byte == ch? */ + jne 1f + leaq -5(%rdi),%rax +1: testb %dl,%dl /* 4th byte == 0? */ + je .Ldone + + movb -4(%rdi),%dl + cmpb %cl,%dl /* 5th byte == ch? */ + jne 1f + leaq -4(%rdi),%rax +1: testb %dl,%dl /* 5th byte == 0? */ + je .Ldone + + movb -3(%rdi),%dl + cmpb %cl,%dl /* 6th byte == ch? */ + jne 1f + leaq -3(%rdi),%rax +1: testb %dl,%dl /* 6th byte == 0? */ + je .Ldone + + movb -2(%rdi),%dl + cmpb %cl,%dl /* 7th byte == ch? */ + jne 1f + leaq -2(%rdi),%rax +1: testb %dl,%dl /* 7th byte == 0? */ + je .Ldone + + movb -1(%rdi),%dl + cmpb %cl,%dl /* 8th byte == ch? */ + jne 1f + leaq -1(%rdi),%rax +1: testb %dl,%dl /* 8th byte == 0? */ + jne .Lloop + +.Ldone: + ret + +STRONG_ALIAS(rindex,strrchr) diff --git a/common/lib/libc/atomic/atomic_add_32_cas.c b/common/lib/libc/atomic/atomic_add_32_cas.c new file mode 100644 index 000000000..e94682f24 --- /dev/null +++ b/common/lib/libc/atomic/atomic_add_32_cas.c @@ -0,0 +1,62 @@ +/* $NetBSD: atomic_add_32_cas.c,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +atomic_add_32(volatile uint32_t *addr, int32_t val) +{ + uint32_t old, new; + + do { + old = *addr; + new = old + val; + } while (atomic_cas_32(addr, old, new) != old); +} + +#undef atomic_add_32 +atomic_op_alias(atomic_add_32,_atomic_add_32) + +#undef atomic_add_int +atomic_op_alias(atomic_add_int,_atomic_add_32) +__strong_alias(_atomic_add_int,_atomic_add_32) + +#if !defined(_LP64) +#undef atomic_add_long +atomic_op_alias(atomic_add_long,_atomic_add_32) +__strong_alias(_atomic_add_long,_atomic_add_32) + +#undef atomic_add_ptr +atomic_op_alias(atomic_add_ptr,_atomic_add_32) +__strong_alias(_atomic_add_ptr,_atomic_add_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_add_32_nv_cas.c b/common/lib/libc/atomic/atomic_add_32_nv_cas.c new file mode 100644 index 000000000..1416d971c --- /dev/null +++ b/common/lib/libc/atomic/atomic_add_32_nv_cas.c @@ -0,0 +1,64 @@ +/* $NetBSD: atomic_add_32_nv_cas.c,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_add_32_nv(volatile uint32_t *addr, int32_t val) +{ + uint32_t old, new; + + do { + old = *addr; + new = old + val; + } while (atomic_cas_32(addr, old, new) != old); + + return (new); +} + +#undef atomic_add_32_nv +atomic_op_alias(atomic_add_32_nv,_atomic_add_32_nv) + +#undef atomic_add_int_nv +atomic_op_alias(atomic_add_int_nv,_atomic_add_32_nv) +__strong_alias(_atomic_add_int_nv,_atomic_add_32_nv) + +#if !defined(_LP64) +#undef atomic_add_long_nv +atomic_op_alias(atomic_add_long_nv,_atomic_add_32_nv) +__strong_alias(_atomic_add_long_nv,_atomic_add_32_nv) + +#undef atomic_add_ptr_nv +atomic_op_alias(atomic_add_ptr_nv,_atomic_add_32_nv) +__strong_alias(_atomic_add_ptr_nv,_atomic_add_32_nv) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_add_64_cas.c b/common/lib/libc/atomic/atomic_add_64_cas.c new file mode 100644 index 000000000..b99a800d0 --- /dev/null +++ b/common/lib/libc/atomic/atomic_add_64_cas.c @@ -0,0 +1,62 @@ +/* $NetBSD: atomic_add_64_cas.c,v 1.5 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +void +atomic_add_64(volatile uint64_t *addr, int64_t val) +{ + uint64_t old, new; + + do { + old = *addr; + new = old + val; + } while (atomic_cas_64(addr, old, new) != old); +} + +#undef atomic_add_64 +atomic_op_alias(atomic_add_64,_atomic_add_64) + +#if defined(_LP64) +#undef atomic_add_long +atomic_op_alias(atomic_add_long,_atomic_add_64) +__strong_alias(_atomic_add_long,_atomic_add_64) + +#undef atomic_add_ptr +atomic_op_alias(atomic_add_ptr,_atomic_add_64) +__strong_alias(_atomic_add_ptr,_atomic_add_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_add_64_nv_cas.c b/common/lib/libc/atomic/atomic_add_64_nv_cas.c new file mode 100644 index 000000000..9dde5e9c9 --- /dev/null +++ b/common/lib/libc/atomic/atomic_add_64_nv_cas.c @@ -0,0 +1,64 @@ +/* $NetBSD: atomic_add_64_nv_cas.c,v 1.5 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_add_64_nv(volatile uint64_t *addr, int64_t val) +{ + uint64_t old, new; + + do { + old = *addr; + new = old + val; + } while (atomic_cas_64(addr, old, new) != old); + + return (new); +} + +#undef atomic_add_64_nv +atomic_op_alias(atomic_add_64_nv,_atomic_add_64_nv) + +#if defined(_LP64) +#undef atomic_add_long_nv +atomic_op_alias(atomic_add_long_nv,_atomic_add_64_nv) +__strong_alias(_atomic_add_long_nv,_atomic_add_64_nv) + +#undef atomic_add_ptr_nv +atomic_op_alias(atomic_add_ptr_nv,_atomic_add_64_nv) +__strong_alias(_atomic_add_ptr_nv,_atomic_add_64_nv) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_and_32_cas.c b/common/lib/libc/atomic/atomic_and_32_cas.c new file mode 100644 index 000000000..a95fe6832 --- /dev/null +++ b/common/lib/libc/atomic/atomic_and_32_cas.c @@ -0,0 +1,56 @@ +/* $NetBSD: atomic_and_32_cas.c,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +atomic_and_32(volatile uint32_t *addr, uint32_t val) +{ + uint32_t old, new; + + do { + old = *addr; + new = old & val; + } while (atomic_cas_32(addr, old, new) != old); +} + +#undef atomic_and_32 +atomic_op_alias(atomic_and_32,_atomic_and_32) +#undef atomic_and_uint +atomic_op_alias(atomic_and_uint,_atomic_and_32) +__strong_alias(_atomic_and_uint,_atomic_and_32) +#if !defined(_LP64) +#undef atomic_and_ulong +atomic_op_alias(atomic_and_ulong,_atomic_and_32) +__strong_alias(_atomic_and_ulong,_atomic_and_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_and_32_nv_cas.c b/common/lib/libc/atomic/atomic_and_32_nv_cas.c new file mode 100644 index 000000000..9a391c9e2 --- /dev/null +++ b/common/lib/libc/atomic/atomic_and_32_nv_cas.c @@ -0,0 +1,58 @@ +/* $NetBSD: atomic_and_32_nv_cas.c,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_and_32_nv(volatile uint32_t *addr, uint32_t val) +{ + uint32_t old, new; + + do { + old = *addr; + new = old & val; + } while (atomic_cas_32(addr, old, new) != old); + + return (new); +} + +#undef atomic_and_32_nv +atomic_op_alias(atomic_and_32_nv,_atomic_and_32_nv) +#undef atomic_and_uint_nv +atomic_op_alias(atomic_and_uint_nv,_atomic_and_32_nv) +__strong_alias(_atomic_and_uint_nv,_atomic_and_32_nv) +#if !defined(_LP64) +#undef atomic_and_ulong_nv +atomic_op_alias(atomic_and_ulong_nv,_atomic_and_32_nv) +__strong_alias(_atomic_and_ulong_nv,_atomic_and_32_nv) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_and_64_cas.c b/common/lib/libc/atomic/atomic_and_64_cas.c new file mode 100644 index 000000000..7c3c52237 --- /dev/null +++ b/common/lib/libc/atomic/atomic_and_64_cas.c @@ -0,0 +1,57 @@ +/* $NetBSD: atomic_and_64_cas.c,v 1.5 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +void +atomic_and_64(volatile uint64_t *addr, uint64_t val) +{ + uint64_t old, new; + + do { + old = *addr; + new = old & val; + } while (atomic_cas_64(addr, old, new) != old); +} + +#undef atomic_and_64 +atomic_op_alias(atomic_and_64,_atomic_and_64) +#if defined(_LP64) +#undef atomic_and_ulong +atomic_op_alias(atomic_and_ulong,_atomic_and_64) +__strong_alias(_atomic_and_ulong,_atomic_and_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_and_64_nv_cas.c b/common/lib/libc/atomic/atomic_and_64_nv_cas.c new file mode 100644 index 000000000..4c9019d1a --- /dev/null +++ b/common/lib/libc/atomic/atomic_and_64_nv_cas.c @@ -0,0 +1,59 @@ +/* $NetBSD: atomic_and_64_nv_cas.c,v 1.5 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_and_64_nv(volatile uint64_t *addr, uint64_t val) +{ + uint64_t old, new; + + do { + old = *addr; + new = old & val; + } while (atomic_cas_64(addr, old, new) != old); + + return (new); +} + +#undef atomic_and_64_nv +atomic_op_alias(atomic_and_64_nv,_atomic_and_64_nv) +#if defined(_LP64) +#undef atomic_and_ulong_nv +atomic_op_alias(atomic_and_ulong_nv,_atomic_and_64_nv) +__strong_alias(_atomic_and_ulong_nv,_atomic_and_64_nv) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_dec_32_add.c b/common/lib/libc/atomic/atomic_dec_32_add.c new file mode 100644 index 000000000..46285d672 --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_32_add.c @@ -0,0 +1,55 @@ +/* $NetBSD: atomic_dec_32_add.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +atomic_dec_32(volatile uint32_t *addr) +{ + + atomic_add_32(addr, -1); +} + +#undef atomic_dec_32 +atomic_op_alias(atomic_dec_32,_atomic_dec_32) +#undef atomic_dec_uint +atomic_op_alias(atomic_dec_uint,_atomic_dec_32) +__strong_alias(_atomic_dec_uint,_atomic_dec_32) +#if !defined(_LP64) +#undef atomic_dec_ulong +atomic_op_alias(atomic_dec_ulong,_atomic_dec_32) +__strong_alias(_atomic_dec_ulong,_atomic_dec_32) +#undef atomic_dec_ptr +atomic_op_alias(atomic_dec_ptr,_atomic_dec_32) +__strong_alias(_atomic_dec_ptr,_atomic_dec_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_dec_32_cas.c b/common/lib/libc/atomic/atomic_dec_32_cas.c new file mode 100644 index 000000000..0f977f391 --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_32_cas.c @@ -0,0 +1,62 @@ +/* $NetBSD: atomic_dec_32_cas.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +atomic_dec_32(volatile uint32_t *addr) +{ + uint32_t old, new; + + do { + old = *addr; + new = old - 1; + } while (atomic_cas_32(addr, old, new) != old); +} + +#undef atomic_dec_32 +atomic_op_alias(atomic_dec_32,_atomic_dec_32) + +#undef atomic_dec_uint +atomic_op_alias(atomic_dec_uint,_atomic_dec_32) +__strong_alias(_atomic_dec_uint,_atomic_dec_32) + +#if !defined(_LP64) +#undef atomic_dec_ulong +atomic_op_alias(atomic_dec_ulong,_atomic_dec_32) +__strong_alias(_atomic_dec_ulong,_atomic_dec_32) + +#undef atomic_dec_ptr +atomic_op_alias(atomic_dec_ptr,_atomic_dec_32) +__strong_alias(_atomic_dec_ptr,_atomic_dec_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_dec_32_nv_add.c b/common/lib/libc/atomic/atomic_dec_32_nv_add.c new file mode 100644 index 000000000..4af68c50c --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_32_nv_add.c @@ -0,0 +1,55 @@ +/* $NetBSD: atomic_dec_32_nv_add.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_dec_32_nv(volatile uint32_t *addr) +{ + + return (atomic_add_32_nv(addr, -1)); +} + +#undef atomic_dec_32_nv +atomic_op_alias(atomic_dec_32_nv,_atomic_dec_32_nv) +#undef atomic_dec_uint_nv +atomic_op_alias(atomic_dec_uint_nv,_atomic_dec_32_nv) +__strong_alias(_atomic_dec_uint_nv,_atomic_dec_32_nv) +#if !defined(_LP64) +#undef atomic_dec_ulong_nv +atomic_op_alias(atomic_dec_ulong_nv,_atomic_dec_32_nv) +__strong_alias(_atomic_dec_ulong_nv,_atomic_dec_32_nv) +#undef atomic_dec_ptr_nv +atomic_op_alias(atomic_dec_ptr_nv,_atomic_dec_32_nv) +__strong_alias(_atomic_dec_ptr_nv,_atomic_dec_32_nv) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_dec_32_nv_cas.c b/common/lib/libc/atomic/atomic_dec_32_nv_cas.c new file mode 100644 index 000000000..9f7d6ae65 --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_32_nv_cas.c @@ -0,0 +1,64 @@ +/* $NetBSD: atomic_dec_32_nv_cas.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_dec_32_nv(volatile uint32_t *addr) +{ + uint32_t old, new; + + do { + old = *addr; + new = old - 1; + } while (atomic_cas_32(addr, old, new) != old); + + return (new); +} + +#undef atomic_dec_32_nv +atomic_op_alias(atomic_dec_32_nv,_atomic_dec_32_nv) + +#undef atomic_dec_uint_nv +atomic_op_alias(atomic_dec_uint_nv,_atomic_dec_32_nv) +__strong_alias(_atomic_dec_uint_nv,_atomic_dec_32_nv) + +#if !defined(_LP64) +#undef atomic_dec_ulong_nv +atomic_op_alias(atomic_dec_ulong_nv,_atomic_dec_32_nv) +__strong_alias(_atomic_dec_ulong_nv,_atomic_dec_32_nv) + +#undef atomic_dec_ptr_nv +atomic_op_alias(atomic_dec_ptr_nv,_atomic_dec_32_nv) +__strong_alias(_atomic_dec_ptr_nv,_atomic_dec_32_nv) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_dec_64_add.c b/common/lib/libc/atomic/atomic_dec_64_add.c new file mode 100644 index 000000000..dca0ee344 --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_64_add.c @@ -0,0 +1,56 @@ +/* $NetBSD: atomic_dec_64_add.c,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +void +atomic_dec_64(volatile uint64_t *addr) +{ + + atomic_add_64(addr, -1); +} + +#undef atomic_dec_64 +atomic_op_alias(atomic_dec_64,_atomic_dec_64) +#if defined(_LP64) +#undef atomic_dec_ulong +atomic_op_alias(atomic_dec_ulong,_atomic_dec_64) +__strong_alias(_atomic_dec_ulong,_atomic_dec_64) +#undef atomic_dec_ptr +atomic_op_alias(atomic_dec_ptr,_atomic_dec_64) +__strong_alias(_atomic_dec_ptr,_atomic_dec_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_dec_64_cas.c b/common/lib/libc/atomic/atomic_dec_64_cas.c new file mode 100644 index 000000000..0c20aceb0 --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_64_cas.c @@ -0,0 +1,62 @@ +/* $NetBSD: atomic_dec_64_cas.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +void +atomic_dec_64(volatile uint64_t *addr) +{ + uint64_t old, new; + + do { + old = *addr; + new = old - 1; + } while (atomic_cas_64(addr, old, new) != old); +} + +#undef atomic_dec_64 +atomic_op_alias(atomic_dec_64,_atomic_dec_64) + +#if defined(_LP64) +#undef atomic_dec_ulong +atomic_op_alias(atomic_dec_ulong,_atomic_dec_64) +__strong_alias(_atomic_dec_ulong,_atomic_dec_64) + +#undef atomic_dec_ptr +atomic_op_alias(atomic_dec_ptr,_atomic_dec_64) +__strong_alias(_atomic_dec_ptr,_atomic_dec_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_dec_64_nv_add.c b/common/lib/libc/atomic/atomic_dec_64_nv_add.c new file mode 100644 index 000000000..02a90b0a3 --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_64_nv_add.c @@ -0,0 +1,56 @@ +/* $NetBSD: atomic_dec_64_nv_add.c,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_dec_64_nv(volatile uint64_t *addr) +{ + + return (atomic_add_64_nv(addr, -1)); +} + +#undef atomic_dec_64_nv +atomic_op_alias(atomic_dec_64_nv,_atomic_dec_64_nv) +#if defined(_LP64) +#undef atomic_dec_ulong_nv +atomic_op_alias(atomic_dec_ulong_nv,_atomic_dec_64_nv) +__strong_alias(_atomic_dec_ulong_nv,_atomic_dec_64_nv) +#undef atomic_dec_ptr_nv +atomic_op_alias(atomic_dec_ptr_nv,_atomic_dec_64_nv) +__strong_alias(_atomic_dec_ptr_nv,_atomic_dec_64_nv) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_dec_64_nv_cas.c b/common/lib/libc/atomic/atomic_dec_64_nv_cas.c new file mode 100644 index 000000000..22f5c9044 --- /dev/null +++ b/common/lib/libc/atomic/atomic_dec_64_nv_cas.c @@ -0,0 +1,64 @@ +/* $NetBSD: atomic_dec_64_nv_cas.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_dec_64_nv(volatile uint64_t *addr) +{ + uint64_t old, new; + + do { + old = *addr; + new = old - 1; + } while (atomic_cas_64(addr, old, new) != old); + + return (new); +} + +#undef atomic_dec_64_nv +atomic_op_alias(atomic_dec_64_nv,_atomic_dec_64_nv) + +#if defined(_LP64) +#undef atomic_dec_ulong_nv +atomic_op_alias(atomic_dec_ulong_nv,_atomic_dec_64_nv) +__strong_alias(_atomic_dec_ulong_nv,_atomic_dec_64_nv) + +#undef atomic_dec_ptr_nv +atomic_op_alias(atomic_dec_ptr_nv,_atomic_dec_64_nv) +__strong_alias(_atomic_dec_ptr_nv,_atomic_dec_64_nv) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_inc_32_add.c b/common/lib/libc/atomic/atomic_inc_32_add.c new file mode 100644 index 000000000..06aaa46b0 --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_32_add.c @@ -0,0 +1,55 @@ +/* $NetBSD: atomic_inc_32_add.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +atomic_inc_32(volatile uint32_t *addr) +{ + + atomic_add_32(addr, 1); +} + +#undef atomic_inc_32 +atomic_op_alias(atomic_inc_32,_atomic_inc_32) +#undef atomic_inc_uint +atomic_op_alias(atomic_inc_uint,_atomic_inc_32) +__strong_alias(_atomic_inc_uint,_atomic_inc_32) +#if !defined(_LP64) +#undef atomic_inc_ulong +atomic_op_alias(atomic_inc_ulong,_atomic_inc_32) +__strong_alias(_atomic_inc_ulong,_atomic_inc_32) +#undef atomic_inc_ptr +atomic_op_alias(atomic_inc_ptr,_atomic_inc_32) +__strong_alias(_atomic_inc_ptr,_atomic_inc_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_inc_32_cas.c b/common/lib/libc/atomic/atomic_inc_32_cas.c new file mode 100644 index 000000000..2f8f9151f --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_32_cas.c @@ -0,0 +1,62 @@ +/* $NetBSD: atomic_inc_32_cas.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +atomic_inc_32(volatile uint32_t *addr) +{ + uint32_t old, new; + + do { + old = *addr; + new = old + 1; + } while (atomic_cas_32(addr, old, new) != old); +} + +#undef atomic_inc_32 +atomic_op_alias(atomic_inc_32,_atomic_inc_32) + +#undef atomic_inc_uint +atomic_op_alias(atomic_inc_uint,_atomic_inc_32) +__strong_alias(_atomic_inc_uint,_atomic_inc_32) + +#if !defined(_LP64) +#undef atomic_inc_ulong +atomic_op_alias(atomic_inc_ulong,_atomic_inc_32) +__strong_alias(_atomic_inc_ulong,_atomic_inc_32) + +#undef atomic_inc_ptr +atomic_op_alias(atomic_inc_ptr,_atomic_inc_32) +__strong_alias(_atomic_inc_ptr,_atomic_inc_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_inc_32_nv_add.c b/common/lib/libc/atomic/atomic_inc_32_nv_add.c new file mode 100644 index 000000000..6fc0900df --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_32_nv_add.c @@ -0,0 +1,55 @@ +/* $NetBSD: atomic_inc_32_nv_add.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_inc_32_nv(volatile uint32_t *addr) +{ + + return (atomic_add_32_nv(addr, 1)); +} + +#undef atomic_inc_32_nv +atomic_op_alias(atomic_inc_32_nv,_atomic_inc_32_nv) +#undef atomic_inc_uint_nv +atomic_op_alias(atomic_inc_uint_nv,_atomic_inc_32_nv) +__strong_alias(_atomic_inc_uint_nv,_atomic_inc_32_nv) +#if !defined(_LP64) +#undef atomic_inc_ulong_nv +atomic_op_alias(atomic_inc_ulong_nv,_atomic_inc_32_nv) +__strong_alias(_atomic_inc_ulong_nv,_atomic_inc_32_nv) +#undef atomic_inc_ptr_nv +atomic_op_alias(atomic_inc_ptr_nv,_atomic_inc_32_nv) +__strong_alias(_atomic_inc_ptr_nv,_atomic_inc_32_nv) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_inc_32_nv_cas.c b/common/lib/libc/atomic/atomic_inc_32_nv_cas.c new file mode 100644 index 000000000..45fdffbf3 --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_32_nv_cas.c @@ -0,0 +1,64 @@ +/* $NetBSD: atomic_inc_32_nv_cas.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_inc_32_nv(volatile uint32_t *addr) +{ + uint32_t old, new; + + do { + old = *addr; + new = old + 1; + } while (atomic_cas_32(addr, old, new) != old); + + return (new); +} + +#undef atomic_inc_32_nv +atomic_op_alias(atomic_inc_32_nv,_atomic_inc_32_nv) + +#undef atomic_inc_uint_nv +atomic_op_alias(atomic_inc_uint_nv,_atomic_inc_32_nv) +__strong_alias(_atomic_inc_uint_nv,_atomic_inc_32_nv) + +#if !defined(_LP64) +#undef atomic_inc_ulong_nv +atomic_op_alias(atomic_inc_ulong_nv,_atomic_inc_32_nv) +__strong_alias(_atomic_inc_ulong_nv,_atomic_inc_32_nv) + +#undef atomic_inc_ptr_nv +atomic_op_alias(atomic_inc_ptr_nv,_atomic_inc_32_nv) +__strong_alias(_atomic_inc_ptr_nv,_atomic_inc_32_nv) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_inc_64_add.c b/common/lib/libc/atomic/atomic_inc_64_add.c new file mode 100644 index 000000000..626eb1cec --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_64_add.c @@ -0,0 +1,56 @@ +/* $NetBSD: atomic_inc_64_add.c,v 1.4 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +void +atomic_inc_64(volatile uint64_t *addr) +{ + + atomic_add_64(addr, 1); +} + +#undef atomic_inc_64 +atomic_op_alias(atomic_inc_64,_atomic_inc_64) +#if defined(_LP64) +#undef atomic_inc_ulong +atomic_op_alias(atomic_inc_ulong,_atomic_inc_64) +__strong_alias(_atomic_inc_ulong,_atomic_inc_64) +#undef atomic_inc_ptr +atomic_op_alias(atomic_inc_ptr,_atomic_inc_64) +__strong_alias(_atomic_inc_ptr,_atomic_inc_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_inc_64_cas.c b/common/lib/libc/atomic/atomic_inc_64_cas.c new file mode 100644 index 000000000..ded46ca56 --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_64_cas.c @@ -0,0 +1,62 @@ +/* $NetBSD: atomic_inc_64_cas.c,v 1.3 2008/04/28 20:22:52 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +void +atomic_inc_64(volatile uint64_t *addr) +{ + uint64_t old, new; + + do { + old = *addr; + new = old + 1; + } while (atomic_cas_64(addr, old, new) != old); +} + +#undef atomic_inc_64 +atomic_op_alias(atomic_inc_64,_atomic_inc_64) + +#if defined(_LP64) +#undef atomic_inc_ulong +atomic_op_alias(atomic_inc_ulong,_atomic_inc_64) +__strong_alias(_atomic_inc_ulong,_atomic_inc_64) + +#undef atomic_inc_ptr +atomic_op_alias(atomic_inc_ptr,_atomic_inc_64) +__strong_alias(_atomic_inc_ptr,_atomic_inc_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_inc_64_nv_add.c b/common/lib/libc/atomic/atomic_inc_64_nv_add.c new file mode 100644 index 000000000..71bbc9ace --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_64_nv_add.c @@ -0,0 +1,56 @@ +/* $NetBSD: atomic_inc_64_nv_add.c,v 1.4 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_inc_64_nv(volatile uint64_t *addr) +{ + + return (atomic_add_64_nv(addr, 1)); +} + +#undef atomic_inc_64_nv +atomic_op_alias(atomic_inc_64_nv,_atomic_inc_64_nv) +#if defined(_LP64) +#undef atomic_inc_ulong_nv +atomic_op_alias(atomic_inc_ulong_nv,_atomic_inc_64_nv) +__strong_alias(_atomic_inc_ulong_nv,_atomic_inc_64_nv) +#undef atomic_inc_ptr_nv +atomic_op_alias(atomic_inc_ptr_nv,_atomic_inc_64_nv) +__strong_alias(_atomic_inc_ptr_nv,_atomic_inc_64_nv) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_inc_64_nv_cas.c b/common/lib/libc/atomic/atomic_inc_64_nv_cas.c new file mode 100644 index 000000000..f835df598 --- /dev/null +++ b/common/lib/libc/atomic/atomic_inc_64_nv_cas.c @@ -0,0 +1,64 @@ +/* $NetBSD: atomic_inc_64_nv_cas.c,v 1.4 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_inc_64_nv(volatile uint64_t *addr) +{ + uint64_t old, new; + + do { + old = *addr; + new = old + 1; + } while (atomic_cas_64(addr, old, new) != old); + + return (new); +} + +#undef atomic_inc_64_nv +atomic_op_alias(atomic_inc_64_nv,_atomic_inc_64_nv) + +#if defined(_LP64) +#undef atomic_add_ulong_nv +atomic_op_alias(atomic_inc_ulong_nv,_atomic_inc_64_nv) +__strong_alias(_atomic_inc_ulong_nv,_atomic_inc_64_nv) + +#undef atomic_inc_ptr_nv +atomic_op_alias(atomic_inc_ptr_nv,_atomic_inc_64_nv) +__strong_alias(_atomic_inc_ptr_nv,_atomic_inc_64_nv) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_init_cas.c b/common/lib/libc/atomic/atomic_init_cas.c new file mode 100644 index 000000000..51a83d011 --- /dev/null +++ b/common/lib/libc/atomic/atomic_init_cas.c @@ -0,0 +1,44 @@ +/* $NetBSD: atomic_init_cas.c,v 1.3 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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. + */ + +/* + * libc glue for atomic operations where the hardware _does_ provide + * compare-and-swap. + */ + +#include +__RCSID("$NetBSD: atomic_init_cas.c,v 1.3 2008/04/28 20:22:53 martin Exp $"); + +void __libc_atomic_init(void) __attribute__ ((visibility("hidden"))); + +void +__libc_atomic_init(void) +{ + + /* nothing to do */ +} diff --git a/common/lib/libc/atomic/atomic_init_testset.c b/common/lib/libc/atomic/atomic_init_testset.c new file mode 100644 index 000000000..9769d62b5 --- /dev/null +++ b/common/lib/libc/atomic/atomic_init_testset.c @@ -0,0 +1,154 @@ +/* $NetBSD: atomic_init_testset.c,v 1.7 2009/12/14 00:39:00 matt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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. + */ + +/* + * libc glue for atomic operations where the hardware does not provide + * compare-and-swap. It's assumed that this will only be used on 32-bit + * platforms. + * + * This should be compiled with '-fno-reorder-blocks -fomit-frame-pointer' + * if using gcc. + */ + +#include +__RCSID("$NetBSD: atomic_init_testset.c,v 1.7 2009/12/14 00:39:00 matt Exp $"); + +#include "atomic_op_namespace.h" + +#include +#include +#include +#include +#include + +#include + +#define I2 __SIMPLELOCK_UNLOCKED, __SIMPLELOCK_UNLOCKED, +#define I16 I2 I2 I2 I2 I2 I2 I2 I2 +#define I128 I16 I16 I16 I16 I16 I16 I16 I16 + +static __cpu_simple_lock_t atomic_locks[128] = { I128 }; + +#ifdef __HAVE_ASM_ATOMIC_CAS_UP +extern uint32_t _atomic_cas_up(volatile uint32_t *, uint32_t, uint32_t); +#else +static uint32_t _atomic_cas_up(volatile uint32_t *, uint32_t, uint32_t); +#endif + +static uint32_t (*_atomic_cas_fn)(volatile uint32_t *, uint32_t, uint32_t) = + _atomic_cas_up; + +void __libc_atomic_init(void) __attribute__ ((visibility("hidden"))); + +RAS_DECL(_atomic_cas); + +#ifndef __HAVE_ASM_ATOMIC_CAS_UP +static uint32_t +_atomic_cas_up(volatile uint32_t *ptr, uint32_t old, uint32_t new) +{ + uint32_t ret; + + RAS_START(_atomic_cas); + ret = *ptr; + if (__predict_false(ret != old)) { + return ret; + } + *ptr = new; + RAS_END(_atomic_cas); + + return ret; +} +#endif + +static uint32_t +_atomic_cas_mp(volatile uint32_t *ptr, uint32_t old, uint32_t new) +{ + __cpu_simple_lock_t *lock; + uint32_t ret; + + lock = &atomic_locks[((uintptr_t)ptr >> 3) & 127]; + __cpu_simple_lock(lock); + ret = *ptr; + if (__predict_true(ret == old)) { + *ptr = new; + } + __cpu_simple_unlock(lock); + + return ret; +} + +uint32_t +_atomic_cas_32(volatile uint32_t *ptr, uint32_t old, uint32_t new) +{ + + return (*_atomic_cas_fn)(ptr, old, new); +} + +void +__libc_atomic_init(void) +{ + int ncpu, mib[2]; + size_t len; + + _atomic_cas_fn = _atomic_cas_mp; + + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + len = sizeof(ncpu); + if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) + return; + if (ncpu > 1) + return; + if (rasctl(RAS_ADDR(_atomic_cas), RAS_SIZE(_atomic_cas), + RAS_INSTALL) == 0) { + _atomic_cas_fn = _atomic_cas_up; + return; + } +} + +#undef atomic_cas_32 +#undef atomic_cas_uint +#undef atomic_cas_ulong +#undef atomic_cas_ptr + +atomic_op_alias(atomic_cas_32,_atomic_cas_32) +atomic_op_alias(atomic_cas_uint,_atomic_cas_32) +__strong_alias(_atomic_cas_uint,_atomic_cas_32) +atomic_op_alias(atomic_cas_ulong,_atomic_cas_32) +__strong_alias(_atomic_cas_ulong,_atomic_cas_32) +atomic_op_alias(atomic_cas_ptr,_atomic_cas_32) +__strong_alias(_atomic_cas_ptr,_atomic_cas_32) + +atomic_op_alias(atomic_cas_32_ni,_atomic_cas_32) +__strong_alias(_atomic_cas_32_ni,_atomic_cas_32) +atomic_op_alias(atomic_cas_uint_ni,_atomic_cas_32) +__strong_alias(_atomic_cas_uint_ni,_atomic_cas_32) +atomic_op_alias(atomic_cas_ulong_ni,_atomic_cas_32) +__strong_alias(_atomic_cas_ulong_ni,_atomic_cas_32) +atomic_op_alias(atomic_cas_ptr_ni,_atomic_cas_32) +__strong_alias(_atomic_cas_ptr_ni,_atomic_cas_32) diff --git a/common/lib/libc/atomic/atomic_op_namespace.h b/common/lib/libc/atomic/atomic_op_namespace.h new file mode 100644 index 000000000..5bf410e29 --- /dev/null +++ b/common/lib/libc/atomic/atomic_op_namespace.h @@ -0,0 +1,125 @@ +/* $NetBSD: atomic_op_namespace.h,v 1.4 2008/06/23 10:33:52 ad Exp $ */ + +/*- + * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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. + */ + +#ifndef _ATOMIC_OP_NAMESPACE_H_ +#define _ATOMIC_OP_NAMESPACE_H_ + +#include + +#if !defined(__lint__) + +#define atomic_add_32 _atomic_add_32 +#define atomic_add_int _atomic_add_int +#define atomic_add_long _atomic_add_long +#define atomic_add_ptr _atomic_add_ptr +#define atomic_add_64 _atomic_add_64 + +#define atomic_add_32_nv _atomic_add_32_nv +#define atomic_add_int_nv _atomic_add_int_nv +#define atomic_add_long_nv _atomic_add_long_nv +#define atomic_add_ptr_nv _atomic_add_ptr_nv +#define atomic_add_64_nv _atomic_add_64_nv + +#define atomic_and_32 _atomic_and_32 +#define atomic_and_uint _atomic_and_uint +#define atomic_and_ulong _atomic_and_ulong +#define atomic_and_64 _atomic_and_64 + +#define atomic_and_32_nv _atomic_and_32_nv +#define atomic_and_uint_nv _atomic_and_uint_nv +#define atomic_and_ulong_nv _atomic_and_ulong_nv +#define atomic_and_64_nv _atomic_and_64_nv + +#define atomic_or_32 _atomic_or_32 +#define atomic_or_uint _atomic_or_uint +#define atomic_or_ulong _atomic_or_ulong +#define atomic_or_64 _atomic_or_64 + +#define atomic_or_32_nv _atomic_or_32_nv +#define atomic_or_uint_nv _atomic_or_uint_nv +#define atomic_or_ulong_nv _atomic_or_ulong_nv +#define atomic_or_64_nv _atomic_or_64_nv + +#define atomic_cas_32 _atomic_cas_32 +#define atomic_cas_uint _atomic_cas_uint +#define atomic_cas_ulong _atomic_cas_ulong +#define atomic_cas_ptr _atomic_cas_ptr +#define atomic_cas_64 _atomic_cas_64 + +#define atomic_cas_32_ni _atomic_cas_32_ni +#define atomic_cas_uint_ni _atomic_cas_uint_ni +#define atomic_cas_ulong_ni _atomic_cas_ulong_ni +#define atomic_cas_ptr_ni _atomic_cas_ptr_ni +#define atomic_cas_64_ni _atomic_cas_64_ni + +#define atomic_swap_32 _atomic_swap_32 +#define atomic_swap_uint _atomic_swap_uint +#define atomic_swap_ulong _atomic_swap_ulong +#define atomic_swap_ptr _atomic_swap_ptr +#define atomic_swap_64 _atomic_swap_64 + +#define atomic_dec_32 _atomic_dec_32 +#define atomic_dec_uint _atomic_dec_uint +#define atomic_dec_ulong _atomic_dec_ulong +#define atomic_dec_ptr _atomic_dec_ptr +#define atomic_dec_64 _atomic_dec_64 + +#define atomic_dec_32_nv _atomic_dec_32_nv +#define atomic_dec_uint_nv _atomic_dec_uint_nv +#define atomic_dec_ptr_nv _atomic_dec_ptr_nv +#define atomic_dec_64_nv _atomic_dec_64_nv + +#define atomic_inc_32 _atomic_inc_32 +#define atomic_inc_uint _atomic_inc_uint +#define atomic_inc_ulong _atomic_inc_ulong +#define atomic_inc_ptr _atomic_inc_ptr +#define atomic_inc_64 _atomic_inc_64 + +#define atomic_inc_32_nv _atomic_inc_32_nv +#define atomic_inc_uint_nv _atomic_inc_uint_nv +#define atomic_inc_ptr_nv _atomic_inc_ptr_nv +#define atomic_inc_64_nv _atomic_inc_64_nv + +#define membar_enter _membar_enter +#define membar_exit _membar_exit +#define membar_producer _membar_producer +#define membar_consumer _membar_consumer +#define membar_sync _membar_sync + +#endif /* __lint__ */ + +#if defined(_KERNEL) +#define atomic_op_alias(a,s) __strong_alias(a,s) +#else +#define atomic_op_alias(a,s) __weak_alias(a,s) +#endif /* _KERNEL */ + +#endif /* _ATOMIC_OP_NAMESPACE_H_ */ diff --git a/common/lib/libc/atomic/atomic_or_32_cas.c b/common/lib/libc/atomic/atomic_or_32_cas.c new file mode 100644 index 000000000..6ce5768ab --- /dev/null +++ b/common/lib/libc/atomic/atomic_or_32_cas.c @@ -0,0 +1,56 @@ +/* $NetBSD: atomic_or_32_cas.c,v 1.4 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +atomic_or_32(volatile uint32_t *addr, uint32_t val) +{ + uint32_t old, new; + + do { + old = *addr; + new = old | val; + } while (atomic_cas_32(addr, old, new) != old); +} + +#undef atomic_or_32 +atomic_op_alias(atomic_or_32,_atomic_or_32) +#undef atomic_or_uint +atomic_op_alias(atomic_or_uint,_atomic_or_32) +__strong_alias(_atomic_or_uint,_atomic_or_32) +#if !defined(_LP64) +#undef atomic_or_ulong +atomic_op_alias(atomic_or_ulong,_atomic_or_32) +__strong_alias(_atomic_or_ulong,_atomic_or_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_or_32_nv_cas.c b/common/lib/libc/atomic/atomic_or_32_nv_cas.c new file mode 100644 index 000000000..a812f866d --- /dev/null +++ b/common/lib/libc/atomic/atomic_or_32_nv_cas.c @@ -0,0 +1,58 @@ +/* $NetBSD: atomic_or_32_nv_cas.c,v 1.4 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_or_32_nv(volatile uint32_t *addr, uint32_t val) +{ + uint32_t old, new; + + do { + old = *addr; + new = old | val; + } while (atomic_cas_32(addr, old, new) != old); + + return (new); +} + +#undef atomic_or_32_nv +atomic_op_alias(atomic_or_32_nv,_atomic_or_32_nv) +#undef atomic_or_uint_nv +atomic_op_alias(atomic_or_uint_nv,_atomic_or_32_nv) +__strong_alias(_atomic_or_uint_nv,_atomic_or_32_nv) +#if !defined(_LP64) +#undef atomic_or_ulong_nv +atomic_op_alias(atomic_or_ulong_nv,_atomic_or_32_nv) +__strong_alias(_atomic_or_ulong_nv,_atomic_or_32_nv) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_or_64_cas.c b/common/lib/libc/atomic/atomic_or_64_cas.c new file mode 100644 index 000000000..fc409b420 --- /dev/null +++ b/common/lib/libc/atomic/atomic_or_64_cas.c @@ -0,0 +1,57 @@ +/* $NetBSD: atomic_or_64_cas.c,v 1.5 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +void +atomic_or_64(volatile uint64_t *addr, uint64_t val) +{ + uint64_t old, new; + + do { + old = *addr; + new = old | val; + } while (atomic_cas_64(addr, old, new) != old); +} + +#undef atomic_or_64 +atomic_op_alias(atomic_or_64,_atomic_or_64) +#if defined(_LP64) +#undef atomic_or_ulong +atomic_op_alias(atomic_or_ulong,_atomic_or_64) +__strong_alias(_atomic_or_ulong,_atomic_or_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_or_64_nv_cas.c b/common/lib/libc/atomic/atomic_or_64_nv_cas.c new file mode 100644 index 000000000..bbca59e8e --- /dev/null +++ b/common/lib/libc/atomic/atomic_or_64_nv_cas.c @@ -0,0 +1,59 @@ +/* $NetBSD: atomic_or_64_nv_cas.c,v 1.5 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_or_64_nv(volatile uint64_t *addr, uint64_t val) +{ + uint64_t old, new; + + do { + old = *addr; + new = old | val; + } while (atomic_cas_64(addr, old, new) != old); + + return (new); +} + +#undef atomic_or_64_nv +atomic_op_alias(atomic_or_64_nv,_atomic_or_64_nv) +#if defined(_LP64) +#undef atomic_or_ulong_nv +atomic_op_alias(atomic_or_ulong_nv,_atomic_or_64_nv) +__strong_alias(_atomic_or_ulong_nv,_atomic_or_64_nv) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/atomic_swap_32_cas.c b/common/lib/libc/atomic/atomic_swap_32_cas.c new file mode 100644 index 000000000..6b8b6dcda --- /dev/null +++ b/common/lib/libc/atomic/atomic_swap_32_cas.c @@ -0,0 +1,60 @@ +/* $NetBSD: atomic_swap_32_cas.c,v 1.4 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +uint32_t +atomic_swap_32(volatile uint32_t *addr, uint32_t new) +{ + uint32_t old; + + do { + old = *addr; + } while (atomic_cas_32(addr, old, new) != old); + + return (old); +} + +#undef atomic_swap_32 +atomic_op_alias(atomic_swap_32,_atomic_swap_32) +#undef atomic_swap_uint +atomic_op_alias(atomic_swap_uint,_atomic_swap_32) +__strong_alias(_atomic_swap_uint,_atomic_swap_32) +#if !defined(_LP64) +#undef atomic_swap_ulong +atomic_op_alias(atomic_swap_ulong,_atomic_swap_32) +__strong_alias(_atomic_swap_ulong,_atomic_swap_32) +#undef atomic_swap_ptr +atomic_op_alias(atomic_swap_ptr,_atomic_swap_32) +__strong_alias(_atomic_swap_ptr,_atomic_swap_32) +#endif /* _LP64 */ diff --git a/common/lib/libc/atomic/atomic_swap_64_cas.c b/common/lib/libc/atomic/atomic_swap_64_cas.c new file mode 100644 index 000000000..8cf4db8c8 --- /dev/null +++ b/common/lib/libc/atomic/atomic_swap_64_cas.c @@ -0,0 +1,61 @@ +/* $NetBSD: atomic_swap_64_cas.c,v 1.5 2008/04/28 20:22:53 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +#ifdef __HAVE_ATOMIC64_OPS + +uint64_t +atomic_swap_64(volatile uint64_t *addr, uint64_t new) +{ + uint64_t old; + + do { + old = *addr; + } while (atomic_cas_64(addr, old, new) != old); + + return (old); +} + +#undef atomic_swap_64 +atomic_op_alias(atomic_swap_64,_atomic_swap_64) +#if defined(_LP64) +#undef atomic_swap_ulong +atomic_op_alias(atomic_swap_ulong,_atomic_swap_64) +__strong_alias(_atomic_swap_ulong,_atomic_swap_64) +#undef atomic_swap_ptr +atomic_op_alias(atomic_swap_ptr,_atomic_swap_64) +__strong_alias(_atomic_swap_ptr,_atomic_swap_64) +#endif /* _LP64 */ + +#endif diff --git a/common/lib/libc/atomic/membar_ops_nop.c b/common/lib/libc/atomic/membar_ops_nop.c new file mode 100644 index 000000000..f6791d963 --- /dev/null +++ b/common/lib/libc/atomic/membar_ops_nop.c @@ -0,0 +1,56 @@ +/* $NetBSD: membar_ops_nop.c,v 1.5 2009/08/21 14:31:02 skrll Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "atomic_op_namespace.h" + +#include + +void +membar_sync(void) +{ + + /* nothing */ +} + +#undef membar_enter +atomic_op_alias(membar_enter,_membar_sync) +__strong_alias(_membar_enter,_membar_sync) +#undef membar_exit +atomic_op_alias(membar_exit,_membar_sync) +__strong_alias(_membar_exit,_membar_sync) +#undef membar_producer +atomic_op_alias(membar_producer,_membar_sync) +__strong_alias(_membar_producer,_membar_sync) +#undef membar_consumer +atomic_op_alias(membar_consumer,_membar_sync) +__strong_alias(_membar_consumer,_membar_sync) +#undef membar_sync +atomic_op_alias(membar_sync,_membar_sync) diff --git a/common/lib/libc/gen/bswap16.c b/common/lib/libc/gen/bswap16.c new file mode 100644 index 000000000..54496fee1 --- /dev/null +++ b/common/lib/libc/gen/bswap16.c @@ -0,0 +1,23 @@ +/* $NetBSD: bswap16.c,v 1.2 2008/02/16 17:37:13 apb Exp $ */ + +/* + * Written by Manuel Bouyer . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: bswap16.c,v 1.2 2008/02/16 17:37:13 apb Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#undef bswap16 + +uint16_t +bswap16(x) + uint16_t x; +{ + return ((x << 8) & 0xff00) | ((x >> 8) & 0x00ff); +} diff --git a/common/lib/libc/gen/bswap32.c b/common/lib/libc/gen/bswap32.c new file mode 100644 index 000000000..ded986636 --- /dev/null +++ b/common/lib/libc/gen/bswap32.c @@ -0,0 +1,26 @@ +/* $NetBSD: bswap32.c,v 1.2 2008/02/16 17:37:13 apb Exp $ */ + +/* + * Written by Manuel Bouyer . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: bswap32.c,v 1.2 2008/02/16 17:37:13 apb Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#undef bswap32 + +uint32_t +bswap32(x) + uint32_t x; +{ + return ((x << 24) & 0xff000000 ) | + ((x << 8) & 0x00ff0000 ) | + ((x >> 8) & 0x0000ff00 ) | + ((x >> 24) & 0x000000ff ); +} diff --git a/common/lib/libc/gen/bswap64.c b/common/lib/libc/gen/bswap64.c new file mode 100644 index 000000000..b2decf94d --- /dev/null +++ b/common/lib/libc/gen/bswap64.c @@ -0,0 +1,44 @@ +/* $NetBSD: bswap64.c,v 1.3 2009/03/16 05:59:21 cegger Exp $ */ + +/* + * Written by Manuel Bouyer . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: bswap64.c,v 1.3 2009/03/16 05:59:21 cegger Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#undef bswap64 + +uint64_t +bswap64(uint64_t x) +{ +#ifdef _LP64 + /* + * Assume we have wide enough registers to do it without touching + * memory. + */ + return ( (x << 56) & 0xff00000000000000UL ) | + ( (x << 40) & 0x00ff000000000000UL ) | + ( (x << 24) & 0x0000ff0000000000UL ) | + ( (x << 8) & 0x000000ff00000000UL ) | + ( (x >> 8) & 0x00000000ff000000UL ) | + ( (x >> 24) & 0x0000000000ff0000UL ) | + ( (x >> 40) & 0x000000000000ff00UL ) | + ( (x >> 56) & 0x00000000000000ffUL ); +#else + /* + * Split the operation in two 32bit steps. + */ + uint32_t tl, th; + + th = bswap32((uint32_t)(x & 0x00000000ffffffffULL)); + tl = bswap32((uint32_t)((x >> 32) & 0x00000000ffffffffULL)); + return ((uint64_t)th << 32) | tl; +#endif +} diff --git a/common/lib/libc/gen/ptree.c b/common/lib/libc/gen/ptree.c new file mode 100644 index 000000000..1a2b9403c --- /dev/null +++ b/common/lib/libc/gen/ptree.c @@ -0,0 +1,1202 @@ +/* $NetBSD: ptree.c,v 1.5 2009/06/07 03:12:40 yamt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas . + * + * 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. + */ + +#define _PT_PRIVATE + +#if defined(PTCHECK) && !defined(PTDEBUG) +#define PTDEBUG +#endif + +#if defined(_KERNEL) || defined(_STANDALONE) +#include +#include +#include +#include +__KERNEL_RCSID(0, "$NetBSD: ptree.c,v 1.5 2009/06/07 03:12:40 yamt Exp $"); +#else +#include +#include +#include +#include +#include +#ifdef PTDEBUG +#include +#define KASSERT(e) assert(e) +#else +#define KASSERT(e) do { } while (/*CONSTCOND*/ 0) +#endif +__RCSID("$NetBSD: ptree.c,v 1.5 2009/06/07 03:12:40 yamt Exp $"); +#endif /* _KERNEL || _STANDALONE */ + +#ifdef _LIBC +#include "namespace.h" +#endif + +#ifdef PTTEST +#include "ptree.h" +#else +#include +#endif + +/* + * This is an implementation of a radix / PATRICIA tree. As in a traditional + * patricia tree, all the data is at the leaves of the tree. An N-value + * tree would have N leaves, N-1 branching nodes, and a root pointer. Each + * branching node would have left(0) and right(1) pointers that either point + * to another branching node or a leaf node. The root pointer would also + * point to either the first branching node or a leaf node. Leaf nodes + * have no need for pointers. + * + * However, allocation for these branching nodes is problematic since the + * allocation could fail. This would cause insertions to fail for reasons + * beyond the users control. So to prevent this, in this implementation + * each node has two identities: its leaf identity and its branch identity. + * Each is separate from the other. Every branch is tagged as to whether + * it points to a leaf or a branch. This is not an attribute of the object + * but of the pointer to the object. The low bit of the pointer is used as + * the tag to determine whether it points to a leaf or branch identity, with + * branch identities having the low bit set. + * + * A node's branch identity has one rule: when traversing the tree from the + * root to the node's leaf identity, one of the branches traversed will be via + * the node's branch identity. Of course, that has an exception: since to + * store N leaves, you need N-1 branches. That one node whose branch identity + * isn't used is stored as "oddman"-out in the root. + * + * Branching nodes also has a bit offset and a bit length which determines + * which branch slot is used. The bit length can be zero resulting in a + * one-way branch. This is happens in two special cases: the root and + * interior mask nodes. + * + * To support longest match first lookups, when a mask node (one that only + * match the first N bits) has children who first N bits match the mask nodes, + * that mask node is converted from being a leaf node to being a one-way + * branch-node. The mask becomes fixed in position in the tree. The mask + * will always be the longest mask match for its descendants (unless they + * traverse an even longer match). + */ + +#define NODETOITEM(pt, ptn) \ + ((void *)((uintptr_t)(ptn) - (pt)->pt_node_offset)) +#define NODETOKEY(pt, ptn) \ + ((void *)((uintptr_t)(ptn) - (pt)->pt_node_offset + pt->pt_key_offset)) +#define ITEMTONODE(pt, ptn) \ + ((pt_node_t *)((uintptr_t)(ptn) + (pt)->pt_node_offset)) + +bool ptree_check(const pt_tree_t *); +#if PTCHECK > 1 +#define PTREE_CHECK(pt) ptree_check(pt) +#else +#define PTREE_CHECK(pt) do { } while (/*CONSTCOND*/ 0) +#endif + +static inline bool +ptree_matchnode(const pt_tree_t *pt, const pt_node_t *target, + const pt_node_t *ptn, pt_bitoff_t max_bitoff, + pt_bitoff_t *bitoff_p, pt_slot_t *slots_p) +{ + return (*pt->pt_ops->ptto_matchnode)(NODETOKEY(pt, target), + (ptn != NULL ? NODETOKEY(pt, ptn) : NULL), max_bitoff, + bitoff_p, slots_p); +} + +static inline pt_slot_t +ptree_testnode(const pt_tree_t *pt, const pt_node_t *target, + const pt_node_t *ptn) +{ + const pt_bitlen_t bitlen = PTN_BRANCH_BITLEN(ptn); + if (bitlen == 0) + return PT_SLOT_ROOT; + return (*pt->pt_ops->ptto_testnode)(NODETOKEY(pt, target), + PTN_BRANCH_BITOFF(ptn), + bitlen); +} + +static inline bool +ptree_matchkey(const pt_tree_t *pt, const void *key, + const pt_node_t *ptn, pt_bitoff_t bitoff, pt_bitlen_t bitlen) +{ + return (*pt->pt_ops->ptto_matchkey)(key, NODETOKEY(pt, ptn), + bitoff, bitlen); +} + +static inline pt_slot_t +ptree_testkey(const pt_tree_t *pt, const void *key, const pt_node_t *ptn) +{ + return (*pt->pt_ops->ptto_testkey)(key, + PTN_BRANCH_BITOFF(ptn), + PTN_BRANCH_BITLEN(ptn)); +} + +static inline void +ptree_set_position(uintptr_t node, pt_slot_t position) +{ + if (PT_LEAF_P(node)) + PTN_SET_LEAF_POSITION(PT_NODE(node), position); + else + PTN_SET_BRANCH_POSITION(PT_NODE(node), position); +} + +void +ptree_init(pt_tree_t *pt, const pt_tree_ops_t *ops, size_t node_offset, + size_t key_offset) +{ + memset(pt, 0, sizeof(*pt)); + pt->pt_node_offset = node_offset; + pt->pt_key_offset = key_offset; + pt->pt_ops = ops; +} + +typedef struct { + uintptr_t *id_insertp; + pt_node_t *id_parent; + uintptr_t id_node; + pt_slot_t id_parent_slot; + pt_bitoff_t id_bitoff; + pt_slot_t id_slot; +} pt_insertdata_t; + +typedef bool (*pt_insertfunc_t)(pt_tree_t *, pt_node_t *, pt_insertdata_t *); + +/* + * Move a branch identify from src to dst. The leaves don't care since + * nothing for them has changed. + */ +/*ARGSUSED*/ +static uintptr_t +ptree_move_branch(pt_tree_t * const pt, pt_node_t * const dst, + const pt_node_t * const src) +{ + KASSERT(PTN_BRANCH_BITLEN(src) == 1); + /* set branch bitlen and bitoff in one step. */ + dst->ptn_branchdata = src->ptn_branchdata; + PTN_SET_BRANCH_POSITION(dst, PTN_BRANCH_POSITION(src)); + PTN_COPY_BRANCH_SLOTS(dst, src); + return PTN_BRANCH(dst); +} + +#ifndef PTNOMASK +static inline uintptr_t * +ptree_find_branch(pt_tree_t * const pt, uintptr_t branch_node) +{ + pt_node_t * const branch = PT_NODE(branch_node); + pt_node_t *parent; + + for (parent = &pt->pt_rootnode;;) { + uintptr_t *nodep = + &PTN_BRANCH_SLOT(parent, ptree_testnode(pt, branch, parent)); + if (*nodep == branch_node) + return nodep; + if (PT_LEAF_P(*nodep)) + return NULL; + parent = PT_NODE(*nodep); + } +} + +static bool +ptree_insert_leaf_after_mask(pt_tree_t * const pt, pt_node_t * const target, + pt_insertdata_t * const id) +{ + const uintptr_t target_node = PTN_LEAF(target); + const uintptr_t mask_node = id->id_node; + pt_node_t * const mask = PT_NODE(mask_node); + const pt_bitlen_t mask_len = PTN_MASK_BITLEN(mask); + + KASSERT(PT_LEAF_P(mask_node)); + KASSERT(PTN_LEAF_POSITION(mask) == id->id_parent_slot); + KASSERT(mask_len <= id->id_bitoff); + KASSERT(PTN_ISMASK_P(mask)); + KASSERT(!PTN_ISMASK_P(target) || mask_len < PTN_MASK_BITLEN(target)); + + if (mask_node == PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode)) { + KASSERT(id->id_parent != mask); + /* + * Nice, mask was an oddman. So just set the oddman to target. + */ + PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = target_node; + } else { + /* + * We need to find out who's pointing to mask's branch + * identity. We know that between root and the leaf identity, + * we must traverse the node's branch identity. + */ + uintptr_t * const mask_nodep = ptree_find_branch(pt, PTN_BRANCH(mask)); + KASSERT(mask_nodep != NULL); + KASSERT(*mask_nodep == PTN_BRANCH(mask)); + KASSERT(PTN_BRANCH_BITLEN(mask) == 1); + + /* + * Alas, mask was used as a branch. Since the mask is becoming + * a one-way branch, we need make target take over mask's + * branching responsibilities. Only then can we change it. + */ + *mask_nodep = ptree_move_branch(pt, target, mask); + + /* + * However, it's possible that mask's parent is itself. If + * that's true, update the insert point to use target since it + * has taken over mask's branching duties. + */ + if (id->id_parent == mask) + id->id_insertp = &PTN_BRANCH_SLOT(target, + id->id_parent_slot); + } + + PTN_SET_BRANCH_BITLEN(mask, 0); + PTN_SET_BRANCH_BITOFF(mask, mask_len); + + PTN_BRANCH_ROOT_SLOT(mask) = target_node; + PTN_BRANCH_ODDMAN_SLOT(mask) = PT_NULL; + PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT); + PTN_SET_BRANCH_POSITION(mask, id->id_parent_slot); + + /* + * Now that everything is done, to make target visible we need to + * change mask from a leaf to a branch. + */ + *id->id_insertp = PTN_BRANCH(mask); + PTREE_CHECK(pt); + return true; +} + +/*ARGSUSED*/ +static bool +ptree_insert_mask_before_node(pt_tree_t * const pt, pt_node_t * const target, + pt_insertdata_t * const id) +{ + const uintptr_t node = id->id_node; + pt_node_t * const ptn = PT_NODE(node); + const pt_slot_t mask_len = PTN_MASK_BITLEN(target); + const pt_bitlen_t node_mask_len = PTN_MASK_BITLEN(ptn); + + KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(ptn)); + KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(ptn)); + KASSERT(PTN_ISMASK_P(target)); + + /* + * If the node we are placing ourself in front is a mask with the + * same mask length as us, return failure. + */ + if (PTN_ISMASK_P(ptn) && node_mask_len == mask_len) + return false; + + PTN_SET_BRANCH_BITLEN(target, 0); + PTN_SET_BRANCH_BITOFF(target, mask_len); + + PTN_BRANCH_SLOT(target, PT_SLOT_ROOT) = node; + *id->id_insertp = PTN_BRANCH(target); + + PTN_SET_BRANCH_POSITION(target, id->id_parent_slot); + ptree_set_position(node, PT_SLOT_ROOT); + + PTREE_CHECK(pt); + return true; +} +#endif /* !PTNOMASK */ + +/*ARGSUSED*/ +static bool +ptree_insert_branch_at_node(pt_tree_t * const pt, pt_node_t * const target, + pt_insertdata_t * const id) +{ + const uintptr_t target_node = PTN_LEAF(target); + const uintptr_t node = id->id_node; + const pt_slot_t other_slot = id->id_slot ^ PT_SLOT_OTHER; + + KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(PT_NODE(node))); + KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(PT_NODE(node))); + KASSERT((node == pt->pt_root) == (id->id_parent == &pt->pt_rootnode)); +#ifndef PTNOMASK + KASSERT(!PTN_ISMASK_P(target) || id->id_bitoff <= PTN_MASK_BITLEN(target)); +#endif + KASSERT(node == pt->pt_root || PTN_BRANCH_BITOFF(id->id_parent) + PTN_BRANCH_BITLEN(id->id_parent) <= id->id_bitoff); + + PTN_SET_BRANCH_BITOFF(target, id->id_bitoff); + PTN_SET_BRANCH_BITLEN(target, 1); + + PTN_BRANCH_SLOT(target, id->id_slot) = target_node; + PTN_BRANCH_SLOT(target, other_slot) = node; + *id->id_insertp = PTN_BRANCH(target); + + PTN_SET_LEAF_POSITION(target, id->id_slot); + ptree_set_position(node, other_slot); + + PTN_SET_BRANCH_POSITION(target, id->id_parent_slot); + PTREE_CHECK(pt); + return true; +} + +static bool +ptree_insert_leaf(pt_tree_t * const pt, pt_node_t * const target, + pt_insertdata_t * const id) +{ + const uintptr_t leaf_node = id->id_node; + pt_node_t * const leaf = PT_NODE(leaf_node); +#ifdef PTNOMASK + const bool inserting_mask = false; + const bool at_mask = false; +#else + const bool inserting_mask = PTN_ISMASK_P(target); + const bool at_mask = PTN_ISMASK_P(leaf); + const pt_bitlen_t leaf_masklen = PTN_MASK_BITLEN(leaf); + const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target); +#endif + pt_insertfunc_t insertfunc = ptree_insert_branch_at_node; + bool matched; + + /* + * In all likelyhood we are going simply going to insert a branch + * where this leaf is which will point to the old and new leaves. + */ + KASSERT(PT_LEAF_P(leaf_node)); + KASSERT(PTN_LEAF_POSITION(leaf) == id->id_parent_slot); + matched = ptree_matchnode(pt, target, leaf, UINT_MAX, + &id->id_bitoff, &id->id_slot); + if (__predict_false(!inserting_mask)) { + /* + * We aren't inserting a mask nor is the leaf a mask, which + * means we are trying to insert a duplicate leaf. Can't do + * that. + */ + if (!at_mask && matched) + return false; + +#ifndef PTNOMASK + /* + * We are at a mask and the leaf we are about to insert + * is at or beyond the mask, we need to convert the mask + * from a leaf to a one-way branch interior mask. + */ + if (at_mask && id->id_bitoff >= leaf_masklen) + insertfunc = ptree_insert_leaf_after_mask; +#endif /* PTNOMASK */ + } +#ifndef PTNOMASK + else { + /* + * We are inserting a mask. + */ + if (matched) { + /* + * If the leaf isn't a mask, we obviously have to + * insert the new mask before non-mask leaf. If the + * leaf is a mask, and the new node has a LEQ mask + * length it too needs to inserted before leaf (*). + * + * In other cases, we place the new mask as leaf after + * leaf mask. Which mask comes first will be a one-way + * branch interior mask node which has the other mask + * node as a child. + * + * (*) ptree_insert_mask_before_node can detect a + * duplicate mask and return failure if needed. + */ + if (!at_mask || target_masklen <= leaf_masklen) + insertfunc = ptree_insert_mask_before_node; + else + insertfunc = ptree_insert_leaf_after_mask; + } else if (at_mask && id->id_bitoff >= leaf_masklen) { + /* + * If the new mask has a bit offset GEQ than the leaf's + * mask length, convert the left to a one-way branch + * interior mask and make that point to the new [leaf] + * mask. + */ + insertfunc = ptree_insert_leaf_after_mask; + } else { + /* + * The new mask has a bit offset less than the leaf's + * mask length or if the leaf isn't a mask at all, the + * new mask deserves to be its own leaf so we use the + * default insertfunc to do that. + */ + } + } +#endif /* PTNOMASK */ + + return (*insertfunc)(pt, target, id); +} + +static bool +ptree_insert_node_common(pt_tree_t *pt, void *item) +{ + pt_node_t * const target = ITEMTONODE(pt, item); +#ifndef PTNOMASK + const bool inserting_mask = PTN_ISMASK_P(target); + const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target); +#endif + pt_insertfunc_t insertfunc; + pt_insertdata_t id; + + /* + * We need a leaf so we can match against. Until we get a leaf + * we having nothing to test against. + */ + if (__predict_false(PT_NULL_P(pt->pt_root))) { + PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PTN_LEAF(target); + PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(target); + PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT); + PTREE_CHECK(pt); + return true; + } + + id.id_bitoff = 0; + id.id_parent = &pt->pt_rootnode; + id.id_parent_slot = PT_SLOT_ROOT; + id.id_insertp = &PTN_BRANCH_ROOT_SLOT(id.id_parent); + for (;;) { + pt_bitoff_t branch_bitoff; + pt_node_t * const ptn = PT_NODE(*id.id_insertp); + id.id_node = *id.id_insertp; + + /* + * If we hit a leaf, try to insert target at leaf. We could + * have inlined ptree_insert_leaf here but that would have + * made this routine much harder to understand. Trust the + * compiler to optimize this properly. + */ + if (PT_LEAF_P(id.id_node)) { + KASSERT(PTN_LEAF_POSITION(ptn) == id.id_parent_slot); + insertfunc = ptree_insert_leaf; + break; + } + + /* + * If we aren't a leaf, we must be a branch. Make sure we are + * in the slot we think we are. + */ + KASSERT(PT_BRANCH_P(id.id_node)); + KASSERT(PTN_BRANCH_POSITION(ptn) == id.id_parent_slot); + + /* + * Where is this branch? + */ + branch_bitoff = PTN_BRANCH_BITOFF(ptn); + +#ifndef PTNOMASK + /* + * If this is a one-way mask node, its offset must equal + * its mask's bitlen. + */ + KASSERT(!(PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) || PTN_MASK_BITLEN(ptn) == branch_bitoff); + + /* + * If we are inserting a mask, and we know that at this point + * all bits before the current bit offset match both the target + * and the branch. If the target's mask length is LEQ than + * this branch's bit offset, then this is where the mask needs + * to added to the tree. + */ + if (__predict_false(inserting_mask) + && (PTN_ISROOT_P(pt, id.id_parent) + || id.id_bitoff < target_masklen) + && target_masklen <= branch_bitoff) { + /* + * We don't know about the bits (if any) between + * id.id_bitoff and the target's mask length match + * both the target and the branch. If the target's + * mask length is greater than the current bit offset + * make sure the untested bits match both the target + * and the branch. + */ + if (target_masklen == id.id_bitoff + || ptree_matchnode(pt, target, ptn, target_masklen, + &id.id_bitoff, &id.id_slot)) { + /* + * The bits matched, so insert the mask as a + * one-way branch. + */ + insertfunc = ptree_insert_mask_before_node; + break; + } else if (id.id_bitoff < branch_bitoff) { + /* + * They didn't match, so create a normal branch + * because this mask needs to a be a new leaf. + */ + insertfunc = ptree_insert_branch_at_node; + break; + } + } +#endif /* PTNOMASK */ + + /* + * If we are skipping some bits, verify they match the node. + * If they don't match, it means we have a leaf to insert. + * Note that if we are advancing bit by bit, we'll skip + * doing matchnode and walk the tree bit by bit via testnode. + */ + if (id.id_bitoff < branch_bitoff + && !ptree_matchnode(pt, target, ptn, branch_bitoff, + &id.id_bitoff, &id.id_slot)) { + KASSERT(id.id_bitoff < branch_bitoff); + insertfunc = ptree_insert_branch_at_node; + break; + } + + /* + * At this point, all bits before branch_bitoff are known + * to match the target. + */ + KASSERT(id.id_bitoff >= branch_bitoff); + + /* + * Decend the tree one level. + */ + id.id_parent = ptn; + id.id_parent_slot = ptree_testnode(pt, target, id.id_parent); + id.id_bitoff += PTN_BRANCH_BITLEN(id.id_parent); + id.id_insertp = &PTN_BRANCH_SLOT(id.id_parent, id.id_parent_slot); + } + + /* + * Do the actual insertion. + */ + return (*insertfunc)(pt, target, &id); +} + +bool +ptree_insert_node(pt_tree_t *pt, void *item) +{ + pt_node_t * const target = ITEMTONODE(pt, item); + + memset(target, 0, sizeof(*target)); + return ptree_insert_node_common(pt, target); +} + +#ifndef PTNOMASK +bool +ptree_insert_mask_node(pt_tree_t *pt, void *item, pt_bitlen_t mask_len) +{ + pt_node_t * const target = ITEMTONODE(pt, item); + pt_bitoff_t bitoff = mask_len; + pt_slot_t slot; + + memset(target, 0, sizeof(*target)); + KASSERT(mask_len == 0 || (~PT__MASK(PTN_MASK_BITLEN) & mask_len) == 0); + /* + * Only the first bits can be non-zero. + * All other bits must be 0. + */ + if (!ptree_matchnode(pt, target, NULL, UINT_MAX, &bitoff, &slot)) + return false; + PTN_SET_MASK_BITLEN(target, mask_len); + PTN_MARK_MASK(target); + return ptree_insert_node_common(pt, target); +} +#endif /* !PTNOMASH */ + +void * +ptree_find_filtered_node(pt_tree_t *pt, void *key, pt_filter_t filter, + void *filter_arg) +{ +#ifndef PTNOMASK + pt_node_t *mask = NULL; +#endif + bool at_mask = false; + pt_node_t *ptn, *parent; + pt_bitoff_t bitoff; + pt_slot_t parent_slot; + + if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode))) + return NULL; + + bitoff = 0; + parent = &pt->pt_rootnode; + parent_slot = PT_SLOT_ROOT; + for (;;) { + const uintptr_t node = PTN_BRANCH_SLOT(parent, parent_slot); + const pt_slot_t branch_bitoff = PTN_BRANCH_BITOFF(PT_NODE(node)); + ptn = PT_NODE(node); + + if (PT_LEAF_P(node)) { +#ifndef PTNOMASK + at_mask = PTN_ISMASK_P(ptn); +#endif + break; + } + + if (bitoff < branch_bitoff) { + if (!ptree_matchkey(pt, key, ptn, bitoff, branch_bitoff - bitoff)) { +#ifndef PTNOMASK + if (mask != NULL) + return NODETOITEM(pt, mask); +#endif + return NULL; + } + bitoff = branch_bitoff; + } + +#ifndef PTNOMASK + if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0 + && (!filter + || (*filter)(filter_arg, NODETOITEM(pt, ptn), + PT_FILTER_MASK))) + mask = ptn; +#endif + + parent = ptn; + parent_slot = ptree_testkey(pt, key, parent); + bitoff += PTN_BRANCH_BITLEN(parent); + } + + KASSERT(PTN_ISROOT_P(pt, parent) || PTN_BRANCH_BITOFF(parent) + PTN_BRANCH_BITLEN(parent) == bitoff); + if (!filter || (*filter)(filter_arg, NODETOITEM(pt, ptn), at_mask ? PT_FILTER_MASK : 0)) { +#ifndef PTNOMASK + if (PTN_ISMASK_P(ptn)) { + const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn); + if (bitoff == PTN_MASK_BITLEN(ptn)) + return NODETOITEM(pt, ptn); + if (ptree_matchkey(pt, key, ptn, bitoff, mask_len - bitoff)) + return NODETOITEM(pt, ptn); + } else +#endif /* !PTNOMASK */ + if (ptree_matchkey(pt, key, ptn, bitoff, UINT_MAX)) + return NODETOITEM(pt, ptn); + } + +#ifndef PTNOMASK + /* + * By virtue of how the mask was placed in the tree, + * all nodes descended from it will match it. But the bits + * before the mask still need to be checked and since the + * mask was a branch, that was done implicitly. + */ + if (mask != NULL) { + KASSERT(ptree_matchkey(pt, key, mask, 0, PTN_MASK_BITLEN(mask))); + return NODETOITEM(pt, mask); + } +#endif /* !PTNOMASK */ + + /* + * Nothing matched. + */ + return NULL; +} + +void * +ptree_iterate(pt_tree_t *pt, const void *item, pt_direction_t direction) +{ + const pt_node_t * const target = ITEMTONODE(pt, item); + uintptr_t node, next_node; + + if (direction != PT_ASCENDING && direction != PT_DESCENDING) + return NULL; + + node = PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode); + if (PT_NULL_P(node)) + return NULL; + + if (item == NULL) { + pt_node_t * const ptn = PT_NODE(node); + if (direction == PT_ASCENDING + && PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) + return NODETOITEM(pt, ptn); + next_node = node; + } else { +#ifndef PTNOMASK + uintptr_t mask_node = PT_NULL; +#endif /* !PTNOMASK */ + next_node = PT_NULL; + while (!PT_LEAF_P(node)) { + pt_node_t * const ptn = PT_NODE(node); + pt_slot_t slot; +#ifndef PTNOMASK + if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) { + if (ptn == target) + break; + if (direction == PT_DESCENDING) { + mask_node = node; + next_node = PT_NULL; + } + } +#endif /* !PTNOMASK */ + slot = ptree_testnode(pt, target, ptn); + node = PTN_BRANCH_SLOT(ptn, slot); + if (direction == PT_ASCENDING) { + if (slot != (pt_slot_t)((1 << PTN_BRANCH_BITLEN(ptn)) - 1)) + next_node = PTN_BRANCH_SLOT(ptn, slot + 1); + } else { + if (slot > 0) { +#ifndef PTNOMASK + mask_node = PT_NULL; +#endif /* !PTNOMASK */ + next_node = PTN_BRANCH_SLOT(ptn, slot - 1); + } + } + } + if (PT_NODE(node) != target) + return NULL; +#ifndef PTNOMASK + if (PT_BRANCH_P(node)) { + pt_node_t *ptn = PT_NODE(node); + KASSERT(PTN_ISMASK_P(PT_NODE(node)) && PTN_BRANCH_BITLEN(PT_NODE(node)) == 0); + if (direction == PT_ASCENDING) { + next_node = PTN_BRANCH_ROOT_SLOT(ptn); + ptn = PT_NODE(next_node); + } + } + /* + * When descending, if we countered a mask node then that's + * we want to return. + */ + if (direction == PT_DESCENDING && !PT_NULL_P(mask_node)) { + KASSERT(PT_NULL_P(next_node)); + return NODETOITEM(pt, PT_NODE(mask_node)); + } +#endif /* !PTNOMASK */ + } + + node = next_node; + if (PT_NULL_P(node)) + return NULL; + + while (!PT_LEAF_P(node)) { + pt_node_t * const ptn = PT_NODE(node); + pt_slot_t slot; + if (direction == PT_ASCENDING) { +#ifndef PTNOMASK + if (PT_BRANCH_P(node) + && PTN_ISMASK_P(ptn) + && PTN_BRANCH_BITLEN(ptn) == 0) + return NODETOITEM(pt, ptn); +#endif /* !PTNOMASK */ + slot = PT_SLOT_LEFT; + } else { + slot = (1 << PTN_BRANCH_BITLEN(ptn)) - 1; + } + node = PTN_BRANCH_SLOT(ptn, slot); + } + return NODETOITEM(pt, PT_NODE(node)); +} + +void +ptree_remove_node(pt_tree_t *pt, void *item) +{ + pt_node_t * const target = ITEMTONODE(pt, item); + const pt_slot_t leaf_slot = PTN_LEAF_POSITION(target); + const pt_slot_t branch_slot = PTN_BRANCH_POSITION(target); + pt_node_t *ptn, *parent; + uintptr_t node; + uintptr_t *removep; + uintptr_t *nodep; + pt_bitoff_t bitoff; + pt_slot_t parent_slot; +#ifndef PTNOMASK + bool at_mask; +#endif + + if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode))) { + KASSERT(!PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode))); + return; + } + + bitoff = 0; + removep = NULL; + nodep = NULL; + parent = &pt->pt_rootnode; + parent_slot = PT_SLOT_ROOT; + for (;;) { + node = PTN_BRANCH_SLOT(parent, parent_slot); + ptn = PT_NODE(node); +#ifndef PTNOMASK + at_mask = PTN_ISMASK_P(ptn); +#endif + + if (PT_LEAF_P(node)) + break; + + /* + * If we are at the target, then we are looking at its branch + * identity. We need to remember who's pointing at it so we + * stop them from doing that. + */ + if (__predict_false(ptn == target)) { + KASSERT(nodep == NULL); +#ifndef PTNOMASK + /* + * Interior mask nodes are trivial to get rid of. + */ + if (at_mask && PTN_BRANCH_BITLEN(ptn) == 0) { + PTN_BRANCH_SLOT(parent, parent_slot) = + PTN_BRANCH_ROOT_SLOT(ptn); + KASSERT(PT_NULL_P(PTN_BRANCH_ODDMAN_SLOT(ptn))); + PTREE_CHECK(pt); + return; + } +#endif /* !PTNOMASK */ + nodep = &PTN_BRANCH_SLOT(parent, parent_slot); + KASSERT(*nodep == PTN_BRANCH(target)); + } + /* + * We need also need to know who's pointing at our parent. + * After we remove ourselves from our parent, he'll only + * have one child and that's unacceptable. So we replace + * the pointer to the parent with our abadoned sibling. + */ + removep = &PTN_BRANCH_SLOT(parent, parent_slot); + + /* + * Descend into the tree. + */ + parent = ptn; + parent_slot = ptree_testnode(pt, target, parent); + bitoff += PTN_BRANCH_BITLEN(parent); + } + + /* + * We better have found that the leaf we are looking for is target. + */ + if (target != ptn) { + KASSERT(target == ptn); + return; + } + + /* + * If we didn't encounter target as branch, then target must be the + * oddman-out. + */ + if (nodep == NULL) { + KASSERT(PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) == PTN_LEAF(target)); + KASSERT(nodep == NULL); + nodep = &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode); + } + + KASSERT((removep == NULL) == (parent == &pt->pt_rootnode)); + + /* + * We have to special remove the last leaf from the root since + * the only time the tree can a PT_NULL node is when it's empty. + */ + if (__predict_false(PTN_ISROOT_P(pt, parent))) { + KASSERT(removep == NULL); + KASSERT(parent == &pt->pt_rootnode); + KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode)); + KASSERT(*nodep == PTN_LEAF(target)); + PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PT_NULL; + PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PT_NULL; + return; + } + + KASSERT((parent == target) == (removep == nodep)); + if (PTN_BRANCH(parent) == PTN_BRANCH_SLOT(target, PTN_BRANCH_POSITION(parent))) { + /* + * The pointer to the parent actually lives in the target's + * branch identity. We can't just move the target's branch + * identity since that would result in the parent pointing + * to its own branch identity and that's fobidden. + */ + const pt_slot_t slot = PTN_BRANCH_POSITION(parent); + const pt_slot_t other_slot = slot ^ PT_SLOT_OTHER; + const pt_bitlen_t parent_bitlen = PTN_BRANCH_BITLEN(parent); + + KASSERT(PTN_BRANCH_BITOFF(target) < PTN_BRANCH_BITOFF(parent)); + + /* + * This gets so confusing. The target's branch identity + * points to the branch identity of the parent of the target's + * leaf identity: + * + * TB = { X, PB = { TL, Y } } + * or TB = { X, PB = { TL } } + * + * So we can't move the target's branch identity to the parent + * because that would corrupt the tree. + */ + if (__predict_true(parent_bitlen > 0)) { + /* + * The parent is a two-way branch. We have to have + * do to this chang in two steps to keep internally + * consistent. First step is to copy our sibling from + * our parent to where we are pointing to parent's + * branch identiy. This remove all references to his + * branch identity from the tree. We then simply make + * the parent assume the target's branching duties. + * + * TB = { X, PB = { Y, TL } } --> PB = { X, Y }. + * TB = { X, PB = { TL, Y } } --> PB = { X, Y }. + * TB = { PB = { Y, TL }, X } --> PB = { Y, X }. + * TB = { PB = { TL, Y }, X } --> PB = { Y, X }. + */ + PTN_BRANCH_SLOT(target, slot) = + PTN_BRANCH_SLOT(parent, parent_slot ^ PT_SLOT_OTHER); + *nodep = ptree_move_branch(pt, parent, target); + PTREE_CHECK(pt); + return; + } else { + /* + * If parent was a one-way branch, it must have been + * mask which pointed to a single leaf which we are + * removing. This means we have to convert the + * parent back to a leaf node. So in the same + * position that target pointed to parent, we place + * leaf pointer to parent. In the other position, + * we just put the other node from target. + * + * TB = { X, PB = { TL } } --> PB = { X, PL } + */ + KASSERT(PTN_ISMASK_P(parent)); + KASSERT(slot == ptree_testnode(pt, parent, target)); + PTN_BRANCH_SLOT(parent, slot) = PTN_LEAF(parent); + PTN_BRANCH_SLOT(parent, other_slot) = + PTN_BRANCH_SLOT(target, other_slot); + PTN_SET_LEAF_POSITION(parent,slot); + PTN_SET_BRANCH_BITLEN(parent, 1); + } + PTN_SET_BRANCH_BITOFF(parent, PTN_BRANCH_BITOFF(target)); + PTN_SET_BRANCH_POSITION(parent, PTN_BRANCH_POSITION(target)); + + *nodep = PTN_BRANCH(parent); + PTREE_CHECK(pt); + return; + } + +#ifndef PTNOMASK + if (__predict_false(PTN_BRANCH_BITLEN(parent) == 0)) { + /* + * Parent was a one-way branch which is changing back to a leaf. + * Since parent is no longer a one-way branch, it can take over + * target's branching duties. + * + * GB = { PB = { TL } } --> GB = { PL } + * TB = { X, Y } --> PB = { X, Y } + */ + KASSERT(PTN_ISMASK_P(parent)); + KASSERT(parent != target); + *removep = PTN_LEAF(parent); + } else +#endif /* !PTNOMASK */ + { + /* + * Now we are the normal removal case. Since after the + * target's leaf identity is removed from the its parent, + * that parent will only have one decendent. So we can + * just as easily replace the node that has the parent's + * branch identity with the surviving node. This freeing + * parent from its branching duties which means it can + * take over target's branching duties. + * + * GB = { PB = { X, TL } } --> GB = { X } + * TB = { V, W } --> PB = { V, W } + */ + const pt_slot_t other_slot = parent_slot ^ PT_SLOT_OTHER; + uintptr_t other_node = PTN_BRANCH_SLOT(parent, other_slot); + const pt_slot_t target_slot = (parent == target ? branch_slot : leaf_slot); + + *removep = other_node; + + ptree_set_position(other_node, target_slot); + + /* + * If target's branch identity contained its leaf identity, we + * have nothing left to do. We've already moved 'X' so there + * is no longer anything in the target's branch identiy that + * has to be preserved. + */ + if (parent == target) { + /* + * GB = { TB = { X, TL } } --> GB = { X } + * TB = { X, TL } --> don't care + */ + PTREE_CHECK(pt); + return; + } + } + + /* + * If target wasn't used as a branch, then it must have been the + * oddman-out of the tree (the one node that doesn't have a branch + * identity). This makes parent the new oddman-out. + */ + if (*nodep == PTN_LEAF(target)) { + KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode)); + PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(parent); + PTREE_CHECK(pt); + return; + } + + /* + * Finally move the target's branching duties to the parent. + */ + KASSERT(PTN_BRANCH_BITOFF(parent) > PTN_BRANCH_BITOFF(target)); + *nodep = ptree_move_branch(pt, parent, target); + PTREE_CHECK(pt); +} + +#ifdef PTCHECK +static const pt_node_t * +ptree_check_find_node2(const pt_tree_t *pt, const pt_node_t *parent, + uintptr_t target) +{ + const pt_bitlen_t slots = 1 << PTN_BRANCH_BITLEN(parent); + pt_slot_t slot; + + for (slot = 0; slot < slots; slot++) { + const uintptr_t node = PTN_BRANCH_SLOT(parent, slot); + if (PTN_BRANCH_SLOT(parent, slot) == node) + return parent; + } + for (slot = 0; slot < slots; slot++) { + const uintptr_t node = PTN_BRANCH_SLOT(parent, slot); + const pt_node_t *branch; + if (!PT_BRANCH_P(node)) + continue; + branch = ptree_check_find_node2(pt, PT_NODE(node), target); + if (branch != NULL) + return branch; + } + + return NULL; +} + +static bool +ptree_check_leaf(const pt_tree_t *pt, const pt_node_t *parent, + const pt_node_t *ptn) +{ + const pt_bitoff_t leaf_position = PTN_LEAF_POSITION(ptn); + const pt_bitlen_t bitlen = PTN_BRANCH_BITLEN(ptn); + const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn); + const uintptr_t leaf_node = PTN_LEAF(ptn); + const bool is_parent_root = (parent == &pt->pt_rootnode); + const bool is_mask = PTN_ISMASK_P(ptn); + bool ok = true; + + if (is_parent_root) { + ok = ok && PTN_BRANCH_ODDMAN_SLOT(parent) == leaf_node; + KASSERT(ok); + return ok; + } + + if (is_mask && PTN_ISMASK_P(parent) && PTN_BRANCH_BITLEN(parent) == 0) { + ok = ok && PTN_MASK_BITLEN(parent) < mask_len; + KASSERT(ok); + ok = ok && PTN_BRANCH_BITOFF(parent) < mask_len; + KASSERT(ok); + } + ok = ok && PTN_BRANCH_SLOT(parent, leaf_position) == leaf_node; + KASSERT(ok); + ok = ok && leaf_position == ptree_testnode(pt, ptn, parent); + KASSERT(ok); + if (PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) != leaf_node) { + ok = ok && bitlen > 0; + KASSERT(ok); + ok = ok && ptn == ptree_check_find_node2(pt, ptn, PTN_LEAF(ptn)); + KASSERT(ok); + } + return ok; +} + +static bool +ptree_check_branch(const pt_tree_t *pt, const pt_node_t *parent, + const pt_node_t *ptn) +{ + const bool is_parent_root = (parent == &pt->pt_rootnode); + const pt_slot_t branch_slot = PTN_BRANCH_POSITION(ptn); + const pt_bitoff_t bitoff = PTN_BRANCH_BITOFF(ptn); + const pt_bitoff_t bitlen = PTN_BRANCH_BITLEN(ptn); + const pt_bitoff_t parent_bitoff = PTN_BRANCH_BITOFF(parent); + const pt_bitoff_t parent_bitlen = PTN_BRANCH_BITLEN(parent); + const bool is_parent_mask = PTN_ISMASK_P(parent) && parent_bitlen == 0; + const bool is_mask = PTN_ISMASK_P(ptn) && bitlen == 0; + const pt_bitoff_t parent_mask_len = PTN_MASK_BITLEN(parent); + const pt_bitoff_t mask_len = PTN_MASK_BITLEN(ptn); + const pt_bitlen_t slots = 1 << bitlen; + pt_slot_t slot; + bool ok = true; + + ok = ok && PTN_BRANCH_SLOT(parent, branch_slot) == PTN_BRANCH(ptn); + KASSERT(ok); + ok = ok && branch_slot == ptree_testnode(pt, ptn, parent); + KASSERT(ok); + + if (is_mask) { + ok = ok && bitoff == mask_len; + KASSERT(ok); + if (is_parent_mask) { + ok = ok && parent_mask_len < mask_len; + KASSERT(ok); + ok = ok && parent_bitoff < bitoff; + KASSERT(ok); + } + } else { + if (is_parent_mask) { + ok = ok && parent_bitoff <= bitoff; + } else if (!is_parent_root) { + ok = ok && parent_bitoff < bitoff; + } + KASSERT(ok); + } + + for (slot = 0; slot < slots; slot++) { + const uintptr_t node = PTN_BRANCH_SLOT(ptn, slot); + pt_bitoff_t tmp_bitoff = 0; + pt_slot_t tmp_slot; + ok = ok && node != PTN_BRANCH(ptn); + KASSERT(ok); + if (bitlen > 0) { + ok = ok && ptree_matchnode(pt, PT_NODE(node), ptn, bitoff, &tmp_bitoff, &tmp_slot); + KASSERT(ok); + tmp_slot = ptree_testnode(pt, PT_NODE(node), ptn); + ok = ok && slot == tmp_slot; + KASSERT(ok); + } + if (PT_LEAF_P(node)) + ok = ok && ptree_check_leaf(pt, ptn, PT_NODE(node)); + else + ok = ok && ptree_check_branch(pt, ptn, PT_NODE(node)); + } + + return ok; +} +#endif /* PTCHECK */ + +/*ARGSUSED*/ +bool +ptree_check(const pt_tree_t *pt) +{ + bool ok = true; +#ifdef PTCHECK + const pt_node_t * const parent = &pt->pt_rootnode; + const uintptr_t node = pt->pt_root; + const pt_node_t * const ptn = PT_NODE(node); + + ok = ok && PTN_BRANCH_BITOFF(parent) == 0; + ok = ok && !PTN_ISMASK_P(parent); + + if (PT_NULL_P(node)) + return ok; + + if (PT_LEAF_P(node)) + ok = ok && ptree_check_leaf(pt, parent, ptn); + else + ok = ok && ptree_check_branch(pt, parent, ptn); +#endif + return ok; +} diff --git a/common/lib/libc/gen/rb.c b/common/lib/libc/gen/rb.c new file mode 100644 index 000000000..a277c28f2 --- /dev/null +++ b/common/lib/libc/gen/rb.c @@ -0,0 +1,1335 @@ +/* $NetBSD: rb.c,v 1.9 2010/11/17 13:19:32 tron Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas . + * + * 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. + */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#include +#include +#ifdef RBDEBUG +#define KASSERT(s) assert(s) +#else +#define KASSERT(s) do { } while (/*CONSTCOND*/ 0) +#endif +#else +#include +#endif + +#ifdef _LIBC +__weak_alias(rb_tree_init, _rb_tree_init) +__weak_alias(rb_tree_find_node, _rb_tree_find_node) +__weak_alias(rb_tree_find_node_geq, _rb_tree_find_node_geq) +__weak_alias(rb_tree_find_node_leq, _rb_tree_find_node_leq) +__weak_alias(rb_tree_insert_node, _rb_tree_insert_node) +__weak_alias(rb_tree_remove_node, _rb_tree_remove_node) +__weak_alias(rb_tree_iterate, _rb_tree_iterate) +#ifdef RBDEBUG +__weak_alias(rb_tree_check, _rb_tree_check) +__weak_alias(rb_tree_depths, _rb_tree_depths) +#endif + +#include "namespace.h" +#endif + +#ifdef RBTEST +#include "rbtree.h" +#else +#include +#endif + +static void rb_tree_insert_rebalance(struct rb_tree *, struct rb_node *); +static void rb_tree_removal_rebalance(struct rb_tree *, struct rb_node *, + unsigned int); +#ifdef RBDEBUG +static const struct rb_node *rb_tree_iterate_const(const struct rb_tree *, + const struct rb_node *, const unsigned int); +static bool rb_tree_check_node(const struct rb_tree *, const struct rb_node *, + const struct rb_node *, bool); +#else +#define rb_tree_check_node(a, b, c, d) true +#endif + +#define RB_NODETOITEM(rbto, rbn) \ + ((void *)((uintptr_t)(rbn) - (rbto)->rbto_node_offset)) +#define RB_ITEMTONODE(rbto, rbn) \ + ((rb_node_t *)((uintptr_t)(rbn) + (rbto)->rbto_node_offset)) + +#define RB_SENTINEL_NODE NULL + +void +rb_tree_init(struct rb_tree *rbt, const rb_tree_ops_t *ops) +{ + + rbt->rbt_ops = ops; + *((const struct rb_node **)&rbt->rbt_root) = RB_SENTINEL_NODE; + RB_TAILQ_INIT(&rbt->rbt_nodes); +#ifndef RBSMALL + rbt->rbt_minmax[RB_DIR_LEFT] = rbt->rbt_root; /* minimum node */ + rbt->rbt_minmax[RB_DIR_RIGHT] = rbt->rbt_root; /* maximum node */ +#endif +#ifdef RBSTATS + rbt->rbt_count = 0; + rbt->rbt_insertions = 0; + rbt->rbt_removals = 0; + rbt->rbt_insertion_rebalance_calls = 0; + rbt->rbt_insertion_rebalance_passes = 0; + rbt->rbt_removal_rebalance_calls = 0; + rbt->rbt_removal_rebalance_passes = 0; +#endif +} + +void * +rb_tree_find_node(struct rb_tree *rbt, const void *key) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_key_fn compare_key = rbto->rbto_compare_key; + struct rb_node *parent = rbt->rbt_root; + + while (!RB_SENTINEL_P(parent)) { + void *pobj = RB_NODETOITEM(rbto, parent); + const signed int diff = (*compare_key)(rbto->rbto_context, + pobj, key); + if (diff == 0) + return pobj; + parent = parent->rb_nodes[diff < 0]; + } + + return NULL; +} + +void * +rb_tree_find_node_geq(struct rb_tree *rbt, const void *key) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_key_fn compare_key = rbto->rbto_compare_key; + struct rb_node *parent = rbt->rbt_root, *last = NULL; + + while (!RB_SENTINEL_P(parent)) { + void *pobj = RB_NODETOITEM(rbto, parent); + const signed int diff = (*compare_key)(rbto->rbto_context, + pobj, key); + if (diff == 0) + return pobj; + if (diff > 0) + last = parent; + parent = parent->rb_nodes[diff < 0]; + } + + return RB_NODETOITEM(rbto, last); +} + +void * +rb_tree_find_node_leq(struct rb_tree *rbt, const void *key) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_key_fn compare_key = rbto->rbto_compare_key; + struct rb_node *parent = rbt->rbt_root, *last = NULL; + + while (!RB_SENTINEL_P(parent)) { + void *pobj = RB_NODETOITEM(rbto, parent); + const signed int diff = (*compare_key)(rbto->rbto_context, + pobj, key); + if (diff == 0) + return pobj; + if (diff < 0) + last = parent; + parent = parent->rb_nodes[diff < 0]; + } + + return RB_NODETOITEM(rbto, last); +} + +void * +rb_tree_insert_node(struct rb_tree *rbt, void *object) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes; + struct rb_node *parent, *tmp, *self = RB_ITEMTONODE(rbto, object); + unsigned int position; + bool rebalance; + + RBSTAT_INC(rbt->rbt_insertions); + + tmp = rbt->rbt_root; + /* + * This is a hack. Because rbt->rbt_root is just a struct rb_node *, + * just like rb_node->rb_nodes[RB_DIR_LEFT], we can use this fact to + * avoid a lot of tests for root and know that even at root, + * updating RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will + * update rbt->rbt_root. + */ + parent = (struct rb_node *)(void *)&rbt->rbt_root; + position = RB_DIR_LEFT; + + /* + * Find out where to place this new leaf. + */ + while (!RB_SENTINEL_P(tmp)) { + void *tobj = RB_NODETOITEM(rbto, tmp); + const signed int diff = (*compare_nodes)(rbto->rbto_context, + tobj, object); + if (__predict_false(diff == 0)) { + /* + * Node already exists; return it. + */ + return tobj; + } + parent = tmp; + position = (diff < 0); + tmp = parent->rb_nodes[position]; + } + +#ifdef RBDEBUG + { + struct rb_node *prev = NULL, *next = NULL; + + if (position == RB_DIR_RIGHT) + prev = parent; + else if (tmp != rbt->rbt_root) + next = parent; + + /* + * Verify our sequential position + */ + KASSERT(prev == NULL || !RB_SENTINEL_P(prev)); + KASSERT(next == NULL || !RB_SENTINEL_P(next)); + if (prev != NULL && next == NULL) + next = TAILQ_NEXT(prev, rb_link); + if (prev == NULL && next != NULL) + prev = TAILQ_PREV(next, rb_node_qh, rb_link); + KASSERT(prev == NULL || !RB_SENTINEL_P(prev)); + KASSERT(next == NULL || !RB_SENTINEL_P(next)); + KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0); + KASSERT(next == NULL || (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, self), RB_NODETOITEM(rbto, next)) < 0); + } +#endif + + /* + * Initialize the node and insert as a leaf into the tree. + */ + RB_SET_FATHER(self, parent); + RB_SET_POSITION(self, position); + if (__predict_false(parent == (struct rb_node *)(void *)&rbt->rbt_root)) { + RB_MARK_BLACK(self); /* root is always black */ +#ifndef RBSMALL + rbt->rbt_minmax[RB_DIR_LEFT] = self; + rbt->rbt_minmax[RB_DIR_RIGHT] = self; +#endif + rebalance = false; + } else { + KASSERT(position == RB_DIR_LEFT || position == RB_DIR_RIGHT); +#ifndef RBSMALL + /* + * Keep track of the minimum and maximum nodes. If our + * parent is a minmax node and we on their min/max side, + * we must be the new min/max node. + */ + if (parent == rbt->rbt_minmax[position]) + rbt->rbt_minmax[position] = self; +#endif /* !RBSMALL */ + /* + * All new nodes are colored red. We only need to rebalance + * if our parent is also red. + */ + RB_MARK_RED(self); + rebalance = RB_RED_P(parent); + } + KASSERT(RB_SENTINEL_P(parent->rb_nodes[position])); + self->rb_left = parent->rb_nodes[position]; + self->rb_right = parent->rb_nodes[position]; + parent->rb_nodes[position] = self; + KASSERT(RB_CHILDLESS_P(self)); + + /* + * Insert the new node into a sorted list for easy sequential access + */ + RBSTAT_INC(rbt->rbt_count); +#ifdef RBDEBUG + if (RB_ROOT_P(rbt, self)) { + RB_TAILQ_INSERT_HEAD(&rbt->rbt_nodes, self, rb_link); + } else if (position == RB_DIR_LEFT) { + KASSERT((*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, self), + RB_NODETOITEM(rbto, RB_FATHER(self))) < 0); + RB_TAILQ_INSERT_BEFORE(RB_FATHER(self), self, rb_link); + } else { + KASSERT((*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, RB_FATHER(self)), + RB_NODETOITEM(rbto, self)) < 0); + RB_TAILQ_INSERT_AFTER(&rbt->rbt_nodes, RB_FATHER(self), + self, rb_link); + } +#endif + KASSERT(rb_tree_check_node(rbt, self, NULL, !rebalance)); + + /* + * Rebalance tree after insertion + */ + if (rebalance) { + rb_tree_insert_rebalance(rbt, self); + KASSERT(rb_tree_check_node(rbt, self, NULL, true)); + } + + /* Succesfully inserted, return our node pointer. */ + return object; +} + +/* + * Swap the location and colors of 'self' and its child @ which. The child + * can not be a sentinel node. This is our rotation function. However, + * since it preserves coloring, it great simplifies both insertion and + * removal since rotation almost always involves the exchanging of colors + * as a separate step. + */ +/*ARGSUSED*/ +static void +rb_tree_reparent_nodes(struct rb_tree *rbt, struct rb_node *old_father, + const unsigned int which) +{ + const unsigned int other = which ^ RB_DIR_OTHER; + struct rb_node * const grandpa = RB_FATHER(old_father); + struct rb_node * const old_child = old_father->rb_nodes[which]; + struct rb_node * const new_father = old_child; + struct rb_node * const new_child = old_father; + + KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT); + + KASSERT(!RB_SENTINEL_P(old_child)); + KASSERT(RB_FATHER(old_child) == old_father); + + KASSERT(rb_tree_check_node(rbt, old_father, NULL, false)); + KASSERT(rb_tree_check_node(rbt, old_child, NULL, false)); + KASSERT(RB_ROOT_P(rbt, old_father) || + rb_tree_check_node(rbt, grandpa, NULL, false)); + + /* + * Exchange descendant linkages. + */ + grandpa->rb_nodes[RB_POSITION(old_father)] = new_father; + new_child->rb_nodes[which] = old_child->rb_nodes[other]; + new_father->rb_nodes[other] = new_child; + + /* + * Update ancestor linkages + */ + RB_SET_FATHER(new_father, grandpa); + RB_SET_FATHER(new_child, new_father); + + /* + * Exchange properties between new_father and new_child. The only + * change is that new_child's position is now on the other side. + */ +#if 0 + { + struct rb_node tmp; + tmp.rb_info = 0; + RB_COPY_PROPERTIES(&tmp, old_child); + RB_COPY_PROPERTIES(new_father, old_father); + RB_COPY_PROPERTIES(new_child, &tmp); + } +#else + RB_SWAP_PROPERTIES(new_father, new_child); +#endif + RB_SET_POSITION(new_child, other); + + /* + * Make sure to reparent the new child to ourself. + */ + if (!RB_SENTINEL_P(new_child->rb_nodes[which])) { + RB_SET_FATHER(new_child->rb_nodes[which], new_child); + RB_SET_POSITION(new_child->rb_nodes[which], which); + } + + KASSERT(rb_tree_check_node(rbt, new_father, NULL, false)); + KASSERT(rb_tree_check_node(rbt, new_child, NULL, false)); + KASSERT(RB_ROOT_P(rbt, new_father) || + rb_tree_check_node(rbt, grandpa, NULL, false)); +} + +static void +rb_tree_insert_rebalance(struct rb_tree *rbt, struct rb_node *self) +{ + struct rb_node * father = RB_FATHER(self); + struct rb_node * grandpa = RB_FATHER(father); + struct rb_node * uncle; + unsigned int which; + unsigned int other; + + KASSERT(!RB_ROOT_P(rbt, self)); + KASSERT(RB_RED_P(self)); + KASSERT(RB_RED_P(father)); + RBSTAT_INC(rbt->rbt_insertion_rebalance_calls); + + for (;;) { + KASSERT(!RB_SENTINEL_P(self)); + + KASSERT(RB_RED_P(self)); + KASSERT(RB_RED_P(father)); + /* + * We are red and our parent is red, therefore we must have a + * grandfather and he must be black. + */ + grandpa = RB_FATHER(father); + KASSERT(RB_BLACK_P(grandpa)); + KASSERT(RB_DIR_RIGHT == 1 && RB_DIR_LEFT == 0); + which = (father == grandpa->rb_right); + other = which ^ RB_DIR_OTHER; + uncle = grandpa->rb_nodes[other]; + + if (RB_BLACK_P(uncle)) + break; + + RBSTAT_INC(rbt->rbt_insertion_rebalance_passes); + /* + * Case 1: our uncle is red + * Simply invert the colors of our parent and + * uncle and make our grandparent red. And + * then solve the problem up at his level. + */ + RB_MARK_BLACK(uncle); + RB_MARK_BLACK(father); + if (__predict_false(RB_ROOT_P(rbt, grandpa))) { + /* + * If our grandpa is root, don't bother + * setting him to red, just return. + */ + KASSERT(RB_BLACK_P(grandpa)); + return; + } + RB_MARK_RED(grandpa); + self = grandpa; + father = RB_FATHER(self); + KASSERT(RB_RED_P(self)); + if (RB_BLACK_P(father)) { + /* + * If our greatgrandpa is black, we're done. + */ + KASSERT(RB_BLACK_P(rbt->rbt_root)); + return; + } + } + + KASSERT(!RB_ROOT_P(rbt, self)); + KASSERT(RB_RED_P(self)); + KASSERT(RB_RED_P(father)); + KASSERT(RB_BLACK_P(uncle)); + KASSERT(RB_BLACK_P(grandpa)); + /* + * Case 2&3: our uncle is black. + */ + if (self == father->rb_nodes[other]) { + /* + * Case 2: we are on the same side as our uncle + * Swap ourselves with our parent so this case + * becomes case 3. Basically our parent becomes our + * child. + */ + rb_tree_reparent_nodes(rbt, father, other); + KASSERT(RB_FATHER(father) == self); + KASSERT(self->rb_nodes[which] == father); + KASSERT(RB_FATHER(self) == grandpa); + self = father; + father = RB_FATHER(self); + } + KASSERT(RB_RED_P(self) && RB_RED_P(father)); + KASSERT(grandpa->rb_nodes[which] == father); + /* + * Case 3: we are opposite a child of a black uncle. + * Swap our parent and grandparent. Since our grandfather + * is black, our father will become black and our new sibling + * (former grandparent) will become red. + */ + rb_tree_reparent_nodes(rbt, grandpa, which); + KASSERT(RB_FATHER(self) == father); + KASSERT(RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER] == grandpa); + KASSERT(RB_RED_P(self)); + KASSERT(RB_BLACK_P(father)); + KASSERT(RB_RED_P(grandpa)); + + /* + * Final step: Set the root to black. + */ + RB_MARK_BLACK(rbt->rbt_root); +} + +static void +rb_tree_prune_node(struct rb_tree *rbt, struct rb_node *self, bool rebalance) +{ + const unsigned int which = RB_POSITION(self); + struct rb_node *father = RB_FATHER(self); +#ifndef RBSMALL + const bool was_root = RB_ROOT_P(rbt, self); +#endif + + KASSERT(rebalance || (RB_ROOT_P(rbt, self) || RB_RED_P(self))); + KASSERT(!rebalance || RB_BLACK_P(self)); + KASSERT(RB_CHILDLESS_P(self)); + KASSERT(rb_tree_check_node(rbt, self, NULL, false)); + + /* + * Since we are childless, we know that self->rb_left is pointing + * to the sentinel node. + */ + father->rb_nodes[which] = self->rb_left; + + /* + * Remove ourselves from the node list, decrement the count, + * and update min/max. + */ + RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); + RBSTAT_DEC(rbt->rbt_count); +#ifndef RBSMALL + if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self)) { + rbt->rbt_minmax[RB_POSITION(self)] = father; + /* + * When removing the root, rbt->rbt_minmax[RB_DIR_LEFT] is + * updated automatically, but we also need to update + * rbt->rbt_minmax[RB_DIR_RIGHT]; + */ + if (__predict_false(was_root)) { + rbt->rbt_minmax[RB_DIR_RIGHT] = father; + } + } + RB_SET_FATHER(self, NULL); +#endif + + /* + * Rebalance if requested. + */ + if (rebalance) + rb_tree_removal_rebalance(rbt, father, which); + KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true)); +} + +/* + * When deleting an interior node + */ +static void +rb_tree_swap_prune_and_rebalance(struct rb_tree *rbt, struct rb_node *self, + struct rb_node *standin) +{ + const unsigned int standin_which = RB_POSITION(standin); + unsigned int standin_other = standin_which ^ RB_DIR_OTHER; + struct rb_node *standin_son; + struct rb_node *standin_father = RB_FATHER(standin); + bool rebalance = RB_BLACK_P(standin); + + if (standin_father == self) { + /* + * As a child of self, any childen would be opposite of + * our parent. + */ + KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other])); + standin_son = standin->rb_nodes[standin_which]; + } else { + /* + * Since we aren't a child of self, any childen would be + * on the same side as our parent. + */ + KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_which])); + standin_son = standin->rb_nodes[standin_other]; + } + + /* + * the node we are removing must have two children. + */ + KASSERT(RB_TWOCHILDREN_P(self)); + /* + * If standin has a child, it must be red. + */ + KASSERT(RB_SENTINEL_P(standin_son) || RB_RED_P(standin_son)); + + /* + * Verify things are sane. + */ + KASSERT(rb_tree_check_node(rbt, self, NULL, false)); + KASSERT(rb_tree_check_node(rbt, standin, NULL, false)); + + if (__predict_false(RB_RED_P(standin_son))) { + /* + * We know we have a red child so if we flip it to black + * we don't have to rebalance. + */ + KASSERT(rb_tree_check_node(rbt, standin_son, NULL, true)); + RB_MARK_BLACK(standin_son); + rebalance = false; + + if (standin_father == self) { + KASSERT(RB_POSITION(standin_son) == standin_which); + } else { + KASSERT(RB_POSITION(standin_son) == standin_other); + /* + * Change the son's parentage to point to his grandpa. + */ + RB_SET_FATHER(standin_son, standin_father); + RB_SET_POSITION(standin_son, standin_which); + } + } + + if (standin_father == self) { + /* + * If we are about to delete the standin's father, then when + * we call rebalance, we need to use ourselves as our father. + * Otherwise remember our original father. Also, sincef we are + * our standin's father we only need to reparent the standin's + * brother. + * + * | R --> S | + * | Q S --> Q T | + * | t --> | + */ + KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other])); + KASSERT(!RB_SENTINEL_P(self->rb_nodes[standin_other])); + KASSERT(self->rb_nodes[standin_which] == standin); + /* + * Have our son/standin adopt his brother as his new son. + */ + standin_father = standin; + } else { + /* + * | R --> S . | + * | / \ | T --> / \ | / | + * | ..... | S --> ..... | T | + * + * Sever standin's connection to his father. + */ + standin_father->rb_nodes[standin_which] = standin_son; + /* + * Adopt the far son. + */ + standin->rb_nodes[standin_other] = self->rb_nodes[standin_other]; + RB_SET_FATHER(standin->rb_nodes[standin_other], standin); + KASSERT(RB_POSITION(self->rb_nodes[standin_other]) == standin_other); + /* + * Use standin_other because we need to preserve standin_which + * for the removal_rebalance. + */ + standin_other = standin_which; + } + + /* + * Move the only remaining son to our standin. If our standin is our + * son, this will be the only son needed to be moved. + */ + KASSERT(standin->rb_nodes[standin_other] != self->rb_nodes[standin_other]); + standin->rb_nodes[standin_other] = self->rb_nodes[standin_other]; + RB_SET_FATHER(standin->rb_nodes[standin_other], standin); + + /* + * Now copy the result of self to standin and then replace + * self with standin in the tree. + */ + RB_COPY_PROPERTIES(standin, self); + RB_SET_FATHER(standin, RB_FATHER(self)); + RB_FATHER(standin)->rb_nodes[RB_POSITION(standin)] = standin; + + /* + * Remove ourselves from the node list, decrement the count, + * and update min/max. + */ + RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); + RBSTAT_DEC(rbt->rbt_count); +#ifndef RBSMALL + if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self)) + rbt->rbt_minmax[RB_POSITION(self)] = RB_FATHER(self); + RB_SET_FATHER(self, NULL); +#endif + + KASSERT(rb_tree_check_node(rbt, standin, NULL, false)); + KASSERT(RB_FATHER_SENTINEL_P(standin) + || rb_tree_check_node(rbt, standin_father, NULL, false)); + KASSERT(RB_LEFT_SENTINEL_P(standin) + || rb_tree_check_node(rbt, standin->rb_left, NULL, false)); + KASSERT(RB_RIGHT_SENTINEL_P(standin) + || rb_tree_check_node(rbt, standin->rb_right, NULL, false)); + + if (!rebalance) + return; + + rb_tree_removal_rebalance(rbt, standin_father, standin_which); + KASSERT(rb_tree_check_node(rbt, standin, NULL, true)); +} + +/* + * We could do this by doing + * rb_tree_node_swap(rbt, self, which); + * rb_tree_prune_node(rbt, self, false); + * + * But it's more efficient to just evalate and recolor the child. + */ +static void +rb_tree_prune_blackred_branch(struct rb_tree *rbt, struct rb_node *self, + unsigned int which) +{ + struct rb_node *father = RB_FATHER(self); + struct rb_node *son = self->rb_nodes[which]; +#ifndef RBSMALL + const bool was_root = RB_ROOT_P(rbt, self); +#endif + + KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT); + KASSERT(RB_BLACK_P(self) && RB_RED_P(son)); + KASSERT(!RB_TWOCHILDREN_P(son)); + KASSERT(RB_CHILDLESS_P(son)); + KASSERT(rb_tree_check_node(rbt, self, NULL, false)); + KASSERT(rb_tree_check_node(rbt, son, NULL, false)); + + /* + * Remove ourselves from the tree and give our former child our + * properties (position, color, root). + */ + RB_COPY_PROPERTIES(son, self); + father->rb_nodes[RB_POSITION(son)] = son; + RB_SET_FATHER(son, father); + + /* + * Remove ourselves from the node list, decrement the count, + * and update minmax. + */ + RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); + RBSTAT_DEC(rbt->rbt_count); +#ifndef RBSMALL + if (__predict_false(was_root)) { + KASSERT(rbt->rbt_minmax[which] == son); + rbt->rbt_minmax[which ^ RB_DIR_OTHER] = son; + } else if (rbt->rbt_minmax[RB_POSITION(self)] == self) { + rbt->rbt_minmax[RB_POSITION(self)] = son; + } + RB_SET_FATHER(self, NULL); +#endif + + KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true)); + KASSERT(rb_tree_check_node(rbt, son, NULL, true)); +} + +void +rb_tree_remove_node(struct rb_tree *rbt, void *object) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + struct rb_node *standin, *self = RB_ITEMTONODE(rbto, object); + unsigned int which; + + KASSERT(!RB_SENTINEL_P(self)); + RBSTAT_INC(rbt->rbt_removals); + + /* + * In the following diagrams, we (the node to be removed) are S. Red + * nodes are lowercase. T could be either red or black. + * + * Remember the major axiom of the red-black tree: the number of + * black nodes from the root to each leaf is constant across all + * leaves, only the number of red nodes varies. + * + * Thus removing a red leaf doesn't require any other changes to a + * red-black tree. So if we must remove a node, attempt to rearrange + * the tree so we can remove a red node. + * + * The simpliest case is a childless red node or a childless root node: + * + * | T --> T | or | R --> * | + * | s --> * | + */ + if (RB_CHILDLESS_P(self)) { + const bool rebalance = RB_BLACK_P(self) && !RB_ROOT_P(rbt, self); + rb_tree_prune_node(rbt, self, rebalance); + return; + } + KASSERT(!RB_CHILDLESS_P(self)); + if (!RB_TWOCHILDREN_P(self)) { + /* + * The next simpliest case is the node we are deleting is + * black and has one red child. + * + * | T --> T --> T | + * | S --> R --> R | + * | r --> s --> * | + */ + which = RB_LEFT_SENTINEL_P(self) ? RB_DIR_RIGHT : RB_DIR_LEFT; + KASSERT(RB_BLACK_P(self)); + KASSERT(RB_RED_P(self->rb_nodes[which])); + KASSERT(RB_CHILDLESS_P(self->rb_nodes[which])); + rb_tree_prune_blackred_branch(rbt, self, which); + return; + } + KASSERT(RB_TWOCHILDREN_P(self)); + + /* + * We invert these because we prefer to remove from the inside of + * the tree. + */ + which = RB_POSITION(self) ^ RB_DIR_OTHER; + + /* + * Let's find the node closes to us opposite of our parent + * Now swap it with ourself, "prune" it, and rebalance, if needed. + */ + standin = RB_ITEMTONODE(rbto, rb_tree_iterate(rbt, object, which)); + rb_tree_swap_prune_and_rebalance(rbt, self, standin); +} + +static void +rb_tree_removal_rebalance(struct rb_tree *rbt, struct rb_node *parent, + unsigned int which) +{ + KASSERT(!RB_SENTINEL_P(parent)); + KASSERT(RB_SENTINEL_P(parent->rb_nodes[which])); + KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT); + RBSTAT_INC(rbt->rbt_removal_rebalance_calls); + + while (RB_BLACK_P(parent->rb_nodes[which])) { + unsigned int other = which ^ RB_DIR_OTHER; + struct rb_node *brother = parent->rb_nodes[other]; + + RBSTAT_INC(rbt->rbt_removal_rebalance_passes); + + KASSERT(!RB_SENTINEL_P(brother)); + /* + * For cases 1, 2a, and 2b, our brother's children must + * be black and our father must be black + */ + if (RB_BLACK_P(parent) + && RB_BLACK_P(brother->rb_left) + && RB_BLACK_P(brother->rb_right)) { + if (RB_RED_P(brother)) { + /* + * Case 1: Our brother is red, swap its + * position (and colors) with our parent. + * This should now be case 2b (unless C or E + * has a red child which is case 3; thus no + * explicit branch to case 2b). + * + * B -> D + * A d -> b E + * C E -> A C + */ + KASSERT(RB_BLACK_P(parent)); + rb_tree_reparent_nodes(rbt, parent, other); + brother = parent->rb_nodes[other]; + KASSERT(!RB_SENTINEL_P(brother)); + KASSERT(RB_RED_P(parent)); + KASSERT(RB_BLACK_P(brother)); + KASSERT(rb_tree_check_node(rbt, brother, NULL, false)); + KASSERT(rb_tree_check_node(rbt, parent, NULL, false)); + } else { + /* + * Both our parent and brother are black. + * Change our brother to red, advance up rank + * and go through the loop again. + * + * B -> *B + * *A D -> A d + * C E -> C E + */ + RB_MARK_RED(brother); + KASSERT(RB_BLACK_P(brother->rb_left)); + KASSERT(RB_BLACK_P(brother->rb_right)); + if (RB_ROOT_P(rbt, parent)) + return; /* root == parent == black */ + KASSERT(rb_tree_check_node(rbt, brother, NULL, false)); + KASSERT(rb_tree_check_node(rbt, parent, NULL, false)); + which = RB_POSITION(parent); + parent = RB_FATHER(parent); + continue; + } + } + /* + * Avoid an else here so that case 2a above can hit either + * case 2b, 3, or 4. + */ + if (RB_RED_P(parent) + && RB_BLACK_P(brother) + && RB_BLACK_P(brother->rb_left) + && RB_BLACK_P(brother->rb_right)) { + KASSERT(RB_RED_P(parent)); + KASSERT(RB_BLACK_P(brother)); + KASSERT(RB_BLACK_P(brother->rb_left)); + KASSERT(RB_BLACK_P(brother->rb_right)); + /* + * We are black, our father is red, our brother and + * both nephews are black. Simply invert/exchange the + * colors of our father and brother (to black and red + * respectively). + * + * | f --> F | + * | * B --> * b | + * | N N --> N N | + */ + RB_MARK_BLACK(parent); + RB_MARK_RED(brother); + KASSERT(rb_tree_check_node(rbt, brother, NULL, true)); + break; /* We're done! */ + } else { + /* + * Our brother must be black and have at least one + * red child (it may have two). + */ + KASSERT(RB_BLACK_P(brother)); + KASSERT(RB_RED_P(brother->rb_nodes[which]) || + RB_RED_P(brother->rb_nodes[other])); + if (RB_BLACK_P(brother->rb_nodes[other])) { + /* + * Case 3: our brother is black, our near + * nephew is red, and our far nephew is black. + * Swap our brother with our near nephew. + * This result in a tree that matches case 4. + * (Our father could be red or black). + * + * | F --> F | + * | x B --> x B | + * | n --> n | + */ + KASSERT(RB_RED_P(brother->rb_nodes[which])); + rb_tree_reparent_nodes(rbt, brother, which); + KASSERT(RB_FATHER(brother) == parent->rb_nodes[other]); + brother = parent->rb_nodes[other]; + KASSERT(RB_RED_P(brother->rb_nodes[other])); + } + /* + * Case 4: our brother is black and our far nephew + * is red. Swap our father and brother locations and + * change our far nephew to black. (these can be + * done in either order so we change the color first). + * The result is a valid red-black tree and is a + * terminal case. (again we don't care about the + * father's color) + * + * If the father is red, we will get a red-black-black + * tree: + * | f -> f --> b | + * | B -> B --> F N | + * | n -> N --> | + * + * If the father is black, we will get an all black + * tree: + * | F -> F --> B | + * | B -> B --> F N | + * | n -> N --> | + * + * If we had two red nephews, then after the swap, + * our former father would have a red grandson. + */ + KASSERT(RB_BLACK_P(brother)); + KASSERT(RB_RED_P(brother->rb_nodes[other])); + RB_MARK_BLACK(brother->rb_nodes[other]); + rb_tree_reparent_nodes(rbt, parent, other); + break; /* We're done! */ + } + } + KASSERT(rb_tree_check_node(rbt, parent, NULL, true)); +} + +void * +rb_tree_iterate(struct rb_tree *rbt, void *object, const unsigned int direction) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + const unsigned int other = direction ^ RB_DIR_OTHER; + struct rb_node *self; + + KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT); + + if (object == NULL) { +#ifndef RBSMALL + if (RB_SENTINEL_P(rbt->rbt_root)) + return NULL; + return RB_NODETOITEM(rbto, rbt->rbt_minmax[direction]); +#else + self = rbt->rbt_root; + if (RB_SENTINEL_P(self)) + return NULL; + while (!RB_SENTINEL_P(self->rb_nodes[direction])) + self = self->rb_nodes[direction]; + return RB_NODETOITEM(rbto, self); +#endif /* !RBSMALL */ + } + self = RB_ITEMTONODE(rbto, object); + KASSERT(!RB_SENTINEL_P(self)); + /* + * We can't go any further in this direction. We proceed up in the + * opposite direction until our parent is in direction we want to go. + */ + if (RB_SENTINEL_P(self->rb_nodes[direction])) { + while (!RB_ROOT_P(rbt, self)) { + if (other == RB_POSITION(self)) + return RB_NODETOITEM(rbto, RB_FATHER(self)); + self = RB_FATHER(self); + } + return NULL; + } + + /* + * Advance down one in current direction and go down as far as possible + * in the opposite direction. + */ + self = self->rb_nodes[direction]; + KASSERT(!RB_SENTINEL_P(self)); + while (!RB_SENTINEL_P(self->rb_nodes[other])) + self = self->rb_nodes[other]; + return RB_NODETOITEM(rbto, self); +} + +#ifdef RBDEBUG +static const struct rb_node * +rb_tree_iterate_const(const struct rb_tree *rbt, const struct rb_node *self, + const unsigned int direction) +{ + const unsigned int other = direction ^ RB_DIR_OTHER; + KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT); + + if (self == NULL) { +#ifndef RBSMALL + if (RB_SENTINEL_P(rbt->rbt_root)) + return NULL; + return rbt->rbt_minmax[direction]; +#else + self = rbt->rbt_root; + if (RB_SENTINEL_P(self)) + return NULL; + while (!RB_SENTINEL_P(self->rb_nodes[direction])) + self = self->rb_nodes[direction]; + return self; +#endif /* !RBSMALL */ + } + KASSERT(!RB_SENTINEL_P(self)); + /* + * We can't go any further in this direction. We proceed up in the + * opposite direction until our parent is in direction we want to go. + */ + if (RB_SENTINEL_P(self->rb_nodes[direction])) { + while (!RB_ROOT_P(rbt, self)) { + if (other == RB_POSITION(self)) + return RB_FATHER(self); + self = RB_FATHER(self); + } + return NULL; + } + + /* + * Advance down one in current direction and go down as far as possible + * in the opposite direction. + */ + self = self->rb_nodes[direction]; + KASSERT(!RB_SENTINEL_P(self)); + while (!RB_SENTINEL_P(self->rb_nodes[other])) + self = self->rb_nodes[other]; + return self; +} + +static unsigned int +rb_tree_count_black(const struct rb_node *self) +{ + unsigned int left, right; + + if (RB_SENTINEL_P(self)) + return 0; + + left = rb_tree_count_black(self->rb_left); + right = rb_tree_count_black(self->rb_right); + + KASSERT(left == right); + + return left + RB_BLACK_P(self); +} + +static bool +rb_tree_check_node(const struct rb_tree *rbt, const struct rb_node *self, + const struct rb_node *prev, bool red_check) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes; + + KASSERT(!RB_SENTINEL_P(self)); + KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0); + + /* + * Verify our relationship to our parent. + */ + if (RB_ROOT_P(rbt, self)) { + KASSERT(self == rbt->rbt_root); + KASSERT(RB_POSITION(self) == RB_DIR_LEFT); + KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self); + KASSERT(RB_FATHER(self) == (const struct rb_node *) &rbt->rbt_root); + } else { + int diff = (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, self), + RB_NODETOITEM(rbto, RB_FATHER(self))); + + KASSERT(self != rbt->rbt_root); + KASSERT(!RB_FATHER_SENTINEL_P(self)); + if (RB_POSITION(self) == RB_DIR_LEFT) { + KASSERT(diff < 0); + KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self); + } else { + KASSERT(diff > 0); + KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_RIGHT] == self); + } + } + + /* + * Verify our position in the linked list against the tree itself. + */ + { + const struct rb_node *prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT); + const struct rb_node *next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT); + KASSERT(prev0 == TAILQ_PREV(self, rb_node_qh, rb_link)); + KASSERT(next0 == TAILQ_NEXT(self, rb_link)); +#ifndef RBSMALL + KASSERT(prev0 != NULL || self == rbt->rbt_minmax[RB_DIR_LEFT]); + KASSERT(next0 != NULL || self == rbt->rbt_minmax[RB_DIR_RIGHT]); +#endif + } + + /* + * The root must be black. + * There can never be two adjacent red nodes. + */ + if (red_check) { + KASSERT(!RB_ROOT_P(rbt, self) || RB_BLACK_P(self)); + (void) rb_tree_count_black(self); + if (RB_RED_P(self)) { + const struct rb_node *brother; + KASSERT(!RB_ROOT_P(rbt, self)); + brother = RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER]; + KASSERT(RB_BLACK_P(RB_FATHER(self))); + /* + * I'm red and have no children, then I must either + * have no brother or my brother also be red and + * also have no children. (black count == 0) + */ + KASSERT(!RB_CHILDLESS_P(self) + || RB_SENTINEL_P(brother) + || RB_RED_P(brother) + || RB_CHILDLESS_P(brother)); + /* + * If I'm not childless, I must have two children + * and they must be both be black. + */ + KASSERT(RB_CHILDLESS_P(self) + || (RB_TWOCHILDREN_P(self) + && RB_BLACK_P(self->rb_left) + && RB_BLACK_P(self->rb_right))); + /* + * If I'm not childless, thus I have black children, + * then my brother must either be black or have two + * black children. + */ + KASSERT(RB_CHILDLESS_P(self) + || RB_BLACK_P(brother) + || (RB_TWOCHILDREN_P(brother) + && RB_BLACK_P(brother->rb_left) + && RB_BLACK_P(brother->rb_right))); + } else { + /* + * If I'm black and have one child, that child must + * be red and childless. + */ + KASSERT(RB_CHILDLESS_P(self) + || RB_TWOCHILDREN_P(self) + || (!RB_LEFT_SENTINEL_P(self) + && RB_RIGHT_SENTINEL_P(self) + && RB_RED_P(self->rb_left) + && RB_CHILDLESS_P(self->rb_left)) + || (!RB_RIGHT_SENTINEL_P(self) + && RB_LEFT_SENTINEL_P(self) + && RB_RED_P(self->rb_right) + && RB_CHILDLESS_P(self->rb_right))); + + /* + * If I'm a childless black node and my parent is + * black, my 2nd closet relative away from my parent + * is either red or has a red parent or red children. + */ + if (!RB_ROOT_P(rbt, self) + && RB_CHILDLESS_P(self) + && RB_BLACK_P(RB_FATHER(self))) { + const unsigned int which = RB_POSITION(self); + const unsigned int other = which ^ RB_DIR_OTHER; + const struct rb_node *relative0, *relative; + + relative0 = rb_tree_iterate_const(rbt, + self, other); + KASSERT(relative0 != NULL); + relative = rb_tree_iterate_const(rbt, + relative0, other); + KASSERT(relative != NULL); + KASSERT(RB_SENTINEL_P(relative->rb_nodes[which])); +#if 0 + KASSERT(RB_RED_P(relative) + || RB_RED_P(relative->rb_left) + || RB_RED_P(relative->rb_right) + || RB_RED_P(RB_FATHER(relative))); +#endif + } + } + /* + * A grandparent's children must be real nodes and not + * sentinels. First check out grandparent. + */ + KASSERT(RB_ROOT_P(rbt, self) + || RB_ROOT_P(rbt, RB_FATHER(self)) + || RB_TWOCHILDREN_P(RB_FATHER(RB_FATHER(self)))); + /* + * If we are have grandchildren on our left, then + * we must have a child on our right. + */ + KASSERT(RB_LEFT_SENTINEL_P(self) + || RB_CHILDLESS_P(self->rb_left) + || !RB_RIGHT_SENTINEL_P(self)); + /* + * If we are have grandchildren on our right, then + * we must have a child on our left. + */ + KASSERT(RB_RIGHT_SENTINEL_P(self) + || RB_CHILDLESS_P(self->rb_right) + || !RB_LEFT_SENTINEL_P(self)); + + /* + * If we have a child on the left and it doesn't have two + * children make sure we don't have great-great-grandchildren on + * the right. + */ + KASSERT(RB_TWOCHILDREN_P(self->rb_left) + || RB_CHILDLESS_P(self->rb_right) + || RB_CHILDLESS_P(self->rb_right->rb_left) + || RB_CHILDLESS_P(self->rb_right->rb_left->rb_left) + || RB_CHILDLESS_P(self->rb_right->rb_left->rb_right) + || RB_CHILDLESS_P(self->rb_right->rb_right) + || RB_CHILDLESS_P(self->rb_right->rb_right->rb_left) + || RB_CHILDLESS_P(self->rb_right->rb_right->rb_right)); + + /* + * If we have a child on the right and it doesn't have two + * children make sure we don't have great-great-grandchildren on + * the left. + */ + KASSERT(RB_TWOCHILDREN_P(self->rb_right) + || RB_CHILDLESS_P(self->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_left->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_left->rb_right) + || RB_CHILDLESS_P(self->rb_left->rb_right) + || RB_CHILDLESS_P(self->rb_left->rb_right->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_right->rb_right)); + + /* + * If we are fully interior node, then our predecessors and + * successors must have no children in our direction. + */ + if (RB_TWOCHILDREN_P(self)) { + const struct rb_node *prev0; + const struct rb_node *next0; + + prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT); + KASSERT(prev0 != NULL); + KASSERT(RB_RIGHT_SENTINEL_P(prev0)); + + next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT); + KASSERT(next0 != NULL); + KASSERT(RB_LEFT_SENTINEL_P(next0)); + } + } + + return true; +} + +void +rb_tree_check(const struct rb_tree *rbt, bool red_check) +{ + const struct rb_node *self; + const struct rb_node *prev; +#ifdef RBSTATS + unsigned int count = 0; +#endif + + KASSERT(rbt->rbt_root != NULL); + KASSERT(RB_LEFT_P(rbt->rbt_root)); + +#if defined(RBSTATS) && !defined(RBSMALL) + KASSERT(rbt->rbt_count > 1 + || rbt->rbt_minmax[RB_DIR_LEFT] == rbt->rbt_minmax[RB_DIR_RIGHT]); +#endif + + prev = NULL; + TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) { + rb_tree_check_node(rbt, self, prev, false); +#ifdef RBSTATS + count++; +#endif + } +#ifdef RBSTATS + KASSERT(rbt->rbt_count == count); +#endif + if (red_check) { + KASSERT(RB_BLACK_P(rbt->rbt_root)); + KASSERT(RB_SENTINEL_P(rbt->rbt_root) + || rb_tree_count_black(rbt->rbt_root)); + + /* + * The root must be black. + * There can never be two adjacent red nodes. + */ + TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) { + rb_tree_check_node(rbt, self, NULL, true); + } + } +} +#endif /* RBDEBUG */ + +#ifdef RBSTATS +static void +rb_tree_mark_depth(const struct rb_tree *rbt, const struct rb_node *self, + size_t *depths, size_t depth) +{ + if (RB_SENTINEL_P(self)) + return; + + if (RB_TWOCHILDREN_P(self)) { + rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1); + rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1); + return; + } + depths[depth]++; + if (!RB_LEFT_SENTINEL_P(self)) { + rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1); + } + if (!RB_RIGHT_SENTINEL_P(self)) { + rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1); + } +} + +void +rb_tree_depths(const struct rb_tree *rbt, size_t *depths) +{ + rb_tree_mark_depth(rbt, rbt->rbt_root, depths, 1); +} +#endif /* RBSTATS */ diff --git a/common/lib/libc/gen/rpst.c b/common/lib/libc/gen/rpst.c new file mode 100644 index 000000000..753e9923f --- /dev/null +++ b/common/lib/libc/gen/rpst.c @@ -0,0 +1,712 @@ +/* $NetBSD: rpst.c,v 1.9 2009/05/26 22:39:15 yamt Exp $ */ + +/*- + * Copyright (c)2009 YAMAMOTO Takashi, + * 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 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. + */ + +/* + * radix priority search tree + * + * described in: + * SIAM J. COMPUT. + * Vol. 14, No. 2, May 1985 + * PRIORITY SEARCH TREES + * EDWARD M. McCREIGHT + * + * ideas from linux: + * - grow tree height on-demand. + * - allow duplicated X values. in that case, we act as a heap. + */ + +#include + +#if defined(_KERNEL) +__KERNEL_RCSID(0, "$NetBSD: rpst.c,v 1.9 2009/05/26 22:39:15 yamt Exp $"); +#include +#else /* defined(_KERNEL) */ +__RCSID("$NetBSD: rpst.c,v 1.9 2009/05/26 22:39:15 yamt Exp $"); +#include +#include +#include +#if 1 +#define KASSERT assert +#else +#define KASSERT(a) +#endif +#endif /* defined(_KERNEL) */ + +#include + +/* + * rpst_init_tree: initialize a tree. + */ + +void +rpst_init_tree(struct rpst_tree *t) +{ + + t->t_root = NULL; + t->t_height = 0; +} + +/* + * rpst_height2max: calculate the maximum index which can be handled by + * a tree with the given height. + * + * 0 ... 0x0000000000000001 + * 1 ... 0x0000000000000003 + * 2 ... 0x0000000000000007 + * 3 ... 0x000000000000000f + * + * 31 ... 0x00000000ffffffff + * + * 63 ... 0xffffffffffffffff + */ + +static uint64_t +rpst_height2max(unsigned int height) +{ + + KASSERT(height < 64); + if (height == 63) { + return UINT64_MAX; + } + return (UINT64_C(1) << (height + 1)) - 1; +} + +/* + * rpst_level2mask: calculate the mask for the given level in the tree. + * + * the mask used to index root's children is level 0. + */ + +static uint64_t +rpst_level2mask(const struct rpst_tree *t, unsigned int level) +{ + uint64_t mask; + + if (t->t_height < level) { + mask = 0; + } else { + mask = UINT64_C(1) << (t->t_height - level); + } + return mask; +} + +/* + * rpst_startmask: calculate the mask for the start of a search. + * (ie. the mask for the top-most bit) + */ + +static uint64_t +rpst_startmask(const struct rpst_tree *t) +{ + const uint64_t mask = rpst_level2mask(t, 0); + + KASSERT((mask | (mask - 1)) == rpst_height2max(t->t_height)); + return mask; +} + +/* + * rpst_update_parents: update n_parent of children + */ + +static inline void +rpst_update_parents(struct rpst_node *n) +{ + int i; + + for (i = 0; i < 2; i++) { + if (n->n_children[i] != NULL) { + n->n_children[i]->n_parent = n; + } + } +} + +/* + * rpst_enlarge_tree: enlarge tree so that 'idx' can be stored + */ + +static void +rpst_enlarge_tree(struct rpst_tree *t, uint64_t idx) +{ + + while (idx > rpst_height2max(t->t_height)) { + struct rpst_node *n = t->t_root; + + if (n != NULL) { + rpst_remove_node(t, n); + memset(&n->n_children, 0, sizeof(n->n_children)); + n->n_children[0] = t->t_root; + t->t_root->n_parent = n; + t->t_root = n; + n->n_parent = NULL; + } + t->t_height++; + } +} + +/* + * rpst_insert_node1: a helper for rpst_insert_node. + */ + +static struct rpst_node * +rpst_insert_node1(struct rpst_node **where, struct rpst_node *n, uint64_t mask) +{ + struct rpst_node *parent; + struct rpst_node *cur; + unsigned int idx; + + KASSERT((n->n_x & ((-mask) << 1)) == 0); + parent = NULL; +next: + cur = *where; + if (cur == NULL) { + n->n_parent = parent; + memset(&n->n_children, 0, sizeof(n->n_children)); + *where = n; + return NULL; + } + KASSERT(cur->n_parent == parent); + if (n->n_y == cur->n_y && n->n_x == cur->n_x) { + return cur; + } + if (n->n_y < cur->n_y) { + /* + * swap cur and n. + * note that n is not in tree. + */ + memcpy(n->n_children, cur->n_children, sizeof(n->n_children)); + n->n_parent = cur->n_parent; + rpst_update_parents(n); + *where = n; + n = cur; + cur = *where; + } + KASSERT(*where == cur); + idx = (n->n_x & mask) != 0; + where = &cur->n_children[idx]; + parent = cur; + KASSERT((*where) == NULL || ((((*where)->n_x & mask) != 0) == idx)); + KASSERT((*where) == NULL || (*where)->n_y >= cur->n_y); + mask >>= 1; + goto next; +} + +/* + * rpst_insert_node: insert a node into the tree. + * + * => return NULL on success. + * => if a duplicated node (a node with the same X,Y pair as ours) is found, + * return the node. in that case, the tree is intact. + */ + +struct rpst_node * +rpst_insert_node(struct rpst_tree *t, struct rpst_node *n) +{ + + rpst_enlarge_tree(t, n->n_x); + return rpst_insert_node1(&t->t_root, n, rpst_startmask(t)); +} + +/* + * rpst_find_pptr: find a pointer to the given node. + * + * also, return the parent node via parentp. (NULL for the root node.) + */ + +static inline struct rpst_node ** +rpst_find_pptr(struct rpst_tree *t, struct rpst_node *n, + struct rpst_node **parentp) +{ + struct rpst_node * const parent = n->n_parent; + unsigned int i; + + *parentp = parent; + if (parent == NULL) { + return &t->t_root; + } + for (i = 0; i < 2 - 1; i++) { + if (parent->n_children[i] == n) { + break; + } + } + KASSERT(parent->n_children[i] == n); + return &parent->n_children[i]; +} + +/* + * rpst_remove_node_at: remove a node at *where. + */ + +static void +rpst_remove_node_at(struct rpst_node *parent, struct rpst_node **where, + struct rpst_node *cur) +{ + struct rpst_node *tmp[2]; + struct rpst_node *selected; + unsigned int selected_idx = 0; /* XXX gcc */ + unsigned int i; + + KASSERT(cur != NULL); + KASSERT(parent == cur->n_parent); +next: + selected = NULL; + for (i = 0; i < 2; i++) { + struct rpst_node *c; + + c = cur->n_children[i]; + KASSERT(c == NULL || c->n_parent == cur); + if (selected == NULL || (c != NULL && c->n_y < selected->n_y)) { + selected = c; + selected_idx = i; + } + } + /* + * now we have: + * + * parent + * \ <- where + * cur + * / \ + * A selected + * / \ + * B C + */ + *where = selected; + if (selected == NULL) { + return; + } + /* + * swap selected->n_children and cur->n_children. + */ + memcpy(tmp, selected->n_children, sizeof(tmp)); + memcpy(selected->n_children, cur->n_children, sizeof(tmp)); + memcpy(cur->n_children, tmp, sizeof(tmp)); + rpst_update_parents(cur); + rpst_update_parents(selected); + selected->n_parent = parent; + /* + * parent + * \ <- where + * selected + * / \ + * A selected + * + * cur + * / \ + * B C + */ + where = &selected->n_children[selected_idx]; + /* + * parent + * \ + * selected + * / \ <- where + * A selected (*) + * + * cur (**) + * / \ + * B C + * + * (*) this 'selected' will be overwritten in the next iteration. + * (**) cur->n_parent is bogus. + */ + parent = selected; + goto next; +} + +/* + * rpst_remove_node: remove a node from the tree. + */ + +void +rpst_remove_node(struct rpst_tree *t, struct rpst_node *n) +{ + struct rpst_node *parent; + struct rpst_node **where; + + where = rpst_find_pptr(t, n, &parent); + rpst_remove_node_at(parent, where, n); +} + +static bool __unused +rpst_iterator_match_p(const struct rpst_node *n, const struct rpst_iterator *it) +{ + + if (n->n_y > it->it_max_y) { + return false; + } + if (n->n_x < it->it_min_x) { + return false; + } + if (n->n_x > it->it_max_x) { + return false; + } + return true; +} + +struct rpst_node * +rpst_iterate_first(struct rpst_tree *t, uint64_t max_y, uint64_t min_x, + uint64_t max_x, struct rpst_iterator *it) +{ + struct rpst_node *n; + + KASSERT(min_x <= max_x); + n = t->t_root; + if (n == NULL || n->n_y > max_y) { + return NULL; + } + if (rpst_height2max(t->t_height) < min_x) { + return NULL; + } + it->it_tree = t; + it->it_cur = n; + it->it_idx = (min_x & rpst_startmask(t)) != 0; + it->it_level = 0; + it->it_max_y = max_y; + it->it_min_x = min_x; + it->it_max_x = max_x; + return rpst_iterate_next(it); +} + +static inline unsigned int +rpst_node_on_edge_p(const struct rpst_node *n, uint64_t val, uint64_t mask) +{ + + return ((n->n_x ^ val) & ((-mask) << 1)) == 0; +} + +static inline uint64_t +rpst_maxidx(const struct rpst_node *n, uint64_t max_x, uint64_t mask) +{ + + if (rpst_node_on_edge_p(n, max_x, mask)) { + return (max_x & mask) != 0; + } else { + return 1; + } +} + +static inline uint64_t +rpst_minidx(const struct rpst_node *n, uint64_t min_x, uint64_t mask) +{ + + if (rpst_node_on_edge_p(n, min_x, mask)) { + return (min_x & mask) != 0; + } else { + return 0; + } +} + +struct rpst_node * +rpst_iterate_next(struct rpst_iterator *it) +{ + struct rpst_tree *t; + struct rpst_node *n; + struct rpst_node *next; + const uint64_t max_y = it->it_max_y; + const uint64_t min_x = it->it_min_x; + const uint64_t max_x = it->it_max_x; + unsigned int idx; + unsigned int maxidx; + unsigned int level; + uint64_t mask; + + t = it->it_tree; + n = it->it_cur; + idx = it->it_idx; + level = it->it_level; + mask = rpst_level2mask(t, level); + maxidx = rpst_maxidx(n, max_x, mask); + KASSERT(n == t->t_root || rpst_iterator_match_p(n, it)); +next: + KASSERT(mask == rpst_level2mask(t, level)); + KASSERT(idx >= rpst_minidx(n, min_x, mask)); + KASSERT(maxidx == rpst_maxidx(n, max_x, mask)); + KASSERT(idx <= maxidx + 2); + KASSERT(n != NULL); +#if 0 + printf("%s: cur=%p, idx=%u maxidx=%u level=%u mask=%" PRIx64 "\n", + __func__, (void *)n, idx, maxidx, level, mask); +#endif + if (idx == maxidx + 1) { /* visit the current node */ + idx++; + if (min_x <= n->n_x && n->n_x <= max_x) { + it->it_cur = n; + it->it_idx = idx; + it->it_level = level; + KASSERT(rpst_iterator_match_p(n, it)); + return n; /* report */ + } + goto next; + } else if (idx == maxidx + 2) { /* back to the parent */ + struct rpst_node **where; + + where = rpst_find_pptr(t, n, &next); + if (next == NULL) { + KASSERT(level == 0); + KASSERT(t->t_root == n); + KASSERT(&t->t_root == where); + return NULL; /* done */ + } + KASSERT(level > 0); + level--; + n = next; + mask = rpst_level2mask(t, level); + maxidx = rpst_maxidx(n, max_x, mask); + idx = where - n->n_children + 1; + KASSERT(idx < 2 + 1); + goto next; + } + /* go to a child */ + KASSERT(idx < 2); + next = n->n_children[idx]; + if (next == NULL || next->n_y > max_y) { + idx++; + goto next; + } + KASSERT(next->n_parent == n); + KASSERT(next->n_y >= n->n_y); + level++; + mask >>= 1; + n = next; + idx = rpst_minidx(n, min_x, mask); + maxidx = rpst_maxidx(n, max_x, mask); +#if 0 + printf("%s: visit %p idx=%u level=%u mask=%llx\n", + __func__, n, idx, level, mask); +#endif + goto next; +} + +#if defined(UNITTEST) +#include + +#include +#include +#include + +static void +rpst_dump_node(const struct rpst_node *n, unsigned int depth) +{ + unsigned int i; + + for (i = 0; i < depth; i++) { + printf(" "); + } + printf("[%u]", depth); + if (n == NULL) { + printf("NULL\n"); + return; + } + printf("%p x=%" PRIx64 "(%" PRIu64 ") y=%" PRIx64 "(%" PRIu64 ")\n", + (const void *)n, n->n_x, n->n_x, n->n_y, n->n_y); + for (i = 0; i < 2; i++) { + rpst_dump_node(n->n_children[i], depth + 1); + } +} + +static void +rpst_dump_tree(const struct rpst_tree *t) +{ + + printf("pst %p height=%u\n", (const void *)t, t->t_height); + rpst_dump_node(t->t_root, 0); +} + +struct testnode { + struct rpst_node n; + struct testnode *next; + bool failed; + bool found; +}; + +struct rpst_tree t; +struct testnode *h = NULL; + +static uintmax_t +tvdiff(const struct timeval *tv1, const struct timeval *tv2) +{ + + return (uintmax_t)tv1->tv_sec * 1000000 + tv1->tv_usec - + tv2->tv_sec * 1000000 - tv2->tv_usec; +} + +static unsigned int +query(uint64_t max_y, uint64_t min_x, uint64_t max_x) +{ + struct testnode *n; + struct rpst_node *rn; + struct rpst_iterator it; + struct timeval start; + struct timeval end; + unsigned int done; + + printf("quering max_y=%" PRIu64 " min_x=%" PRIu64 " max_x=%" PRIu64 + "\n", + max_y, min_x, max_x); + done = 0; + gettimeofday(&start, NULL); + for (rn = rpst_iterate_first(&t, max_y, min_x, max_x, &it); + rn != NULL; + rn = rpst_iterate_next(&it)) { + done++; +#if 0 + printf("found %p x=%" PRIu64 " y=%" PRIu64 "\n", + (void *)rn, rn->n_x, rn->n_y); +#endif + n = (void *)rn; + assert(!n->found); + n->found = true; + } + gettimeofday(&end, NULL); + printf("%u nodes found in %ju usecs\n", done, + tvdiff(&end, &start)); + + gettimeofday(&start, NULL); + for (n = h; n != NULL; n = n->next) { + assert(n->failed || + n->found == rpst_iterator_match_p(&n->n, &it)); + n->found = false; + } + gettimeofday(&end, NULL); + printf("(linear search took %ju usecs)\n", tvdiff(&end, &start)); + return done; +} + +int +main(int argc, char *argv[]) +{ + struct testnode *n; + unsigned int i; + struct rpst_iterator it; + struct timeval start; + struct timeval end; + uint64_t min_y = UINT64_MAX; + uint64_t max_y = 0; + uint64_t min_x = UINT64_MAX; + uint64_t max_x = 0; + uint64_t w; + unsigned int done; + unsigned int fail; + unsigned int num = 500000; + + rpst_init_tree(&t); + rpst_dump_tree(&t); + assert(NULL == rpst_iterate_first(&t, UINT64_MAX, 0, UINT64_MAX, &it)); + + for (i = 0; i < num; i++) { + n = malloc(sizeof(*n)); + if (i > 499000) { + n->n.n_x = 10; + n->n.n_y = random(); + } else if (i > 400000) { + n->n.n_x = i; + n->n.n_y = random(); + } else { + n->n.n_x = random(); + n->n.n_y = random(); + } + if (n->n.n_y < min_y) { + min_y = n->n.n_y; + } + if (n->n.n_y > max_y) { + max_y = n->n.n_y; + } + if (n->n.n_x < min_x) { + min_x = n->n.n_x; + } + if (n->n.n_x > max_x) { + max_x = n->n.n_x; + } + n->found = false; + n->failed = false; + n->next = h; + h = n; + } + + done = 0; + fail = 0; + gettimeofday(&start, NULL); + for (n = h; n != NULL; n = n->next) { + struct rpst_node *o; +#if 0 + printf("insert %p x=%" PRIu64 " y=%" PRIu64 "\n", + n, n->n.n_x, n->n.n_y); +#endif + o = rpst_insert_node(&t, &n->n); + if (o == NULL) { + done++; + } else { + n->failed = true; + fail++; + } + } + gettimeofday(&end, NULL); + printf("%u nodes inserted and %u insertion failed in %ju usecs\n", + done, fail, + tvdiff(&end, &start)); + + assert(min_y == 0 || 0 == query(min_y - 1, 0, UINT64_MAX)); + assert(max_x == UINT64_MAX || + 0 == query(UINT64_MAX, max_x + 1, UINT64_MAX)); + assert(min_x == 0 || 0 == query(UINT64_MAX, 0, min_x - 1)); + + done = query(max_y, min_x, max_x); + assert(done == num - fail); + + done = query(UINT64_MAX, 0, UINT64_MAX); + assert(done == num - fail); + + w = max_x - min_x; + query(max_y / 2, min_x, max_x); + query(max_y, min_x + w / 2, max_x); + query(max_y / 2, min_x + w / 2, max_x); + query(max_y / 2, min_x, max_x - w / 2); + query(max_y / 2, min_x + w / 3, max_x - w / 3); + query(max_y - 1, min_x + 1, max_x - 1); + query(UINT64_MAX, 10, 10); + + done = 0; + gettimeofday(&start, NULL); + for (n = h; n != NULL; n = n->next) { + if (n->failed) { + continue; + } +#if 0 + printf("remove %p x=%" PRIu64 " y=%" PRIu64 "\n", + n, n->n.n_x, n->n.n_y); +#endif + rpst_remove_node(&t, &n->n); + done++; + } + gettimeofday(&end, NULL); + printf("%u nodes removed in %ju usecs\n", done, + tvdiff(&end, &start)); + + rpst_dump_tree(&t); +} +#endif /* defined(UNITTEST) */ diff --git a/common/lib/libc/gmon/mcount.c b/common/lib/libc/gmon/mcount.c new file mode 100644 index 000000000..0a0c71688 --- /dev/null +++ b/common/lib/libc/gmon/mcount.c @@ -0,0 +1,270 @@ +/* $NetBSD: mcount.c,v 1.8 2009/01/05 18:06:57 pooka Exp $ */ + +/* + * Copyright (c) 2003, 2004 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Nathan J. Williams for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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. + */ + +/*- + * Copyright (c) 1983, 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. + */ + +/* If building a standalone libkern, don't include mcount. */ +#if (!defined(_KERNEL) || defined(GPROF)) && !defined(_STANDALONE) + +#ifdef _KERNEL_OPT +#include "opt_multiprocessor.h" +#endif + +#include +#if !defined(lint) && !defined(_KERNEL) && defined(LIBC_SCCS) +#if 0 +static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: mcount.c,v 1.8 2009/01/05 18:06:57 pooka Exp $"); +#endif +#endif + +#include +#include + +#ifndef _KERNEL +#include "reentrant.h" +#endif + +#ifdef _REENTRANT +extern thread_key_t _gmonkey; +extern struct gmonparam _gmondummy; +struct gmonparam *_m_gmon_alloc(void); +#endif + +_MCOUNT_DECL __P((u_long, u_long)) +#ifdef _KERNEL + __attribute__((__no_instrument_function__)) +#endif + __used; + +/* XXX: make these interfaces */ +#ifdef _RUMPKERNEL +#undef MCOUNT_ENTER +#define MCOUNT_ENTER +#undef MCOUNT_EXIT +#define MCOUNT_EXIT +#undef MCOUNT +#define MCOUNT +#endif + +/* + * mcount is called on entry to each function compiled with the profiling + * switch set. _mcount(), which is declared in a machine-dependent way + * with _MCOUNT_DECL, does the actual work and is either inlined into a + * C routine or called by an assembly stub. In any case, this magic is + * taken care of by the MCOUNT definition in . + * + * _mcount updates data structures that represent traversals of the + * program's call graph edges. frompc and selfpc are the return + * address and function address that represents the given call graph edge. + * + * Note: the original BSD code used the same variable (frompcindex) for + * both frompcindex and frompc. Any reasonable, modern compiler will + * perform this optimization. + */ +_MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */ + u_long frompc, selfpc; +{ + u_short *frompcindex; + struct tostruct *top, *prevtop; + struct gmonparam *p; + long toindex; +#if defined(_KERNEL) && !defined(_RUMPKERNEL) + int s; +#endif + +#if defined(_REENTRANT) && !defined(_KERNEL) + if (__isthreaded) { + p = thr_getspecific(_gmonkey); + if (p == NULL) { + /* Prevent recursive calls while allocating */ + thr_setspecific(_gmonkey, &_gmondummy); + p = _m_gmon_alloc(); + } + } else +#endif + p = &_gmonparam; + /* + * check that we are profiling + * and that we aren't recursively invoked. + */ + if (p->state != GMON_PROF_ON) + return; +#ifdef _KERNEL + MCOUNT_ENTER; +#endif + p->state = GMON_PROF_BUSY; + /* + * check that frompcindex is a reasonable pc value. + * for example: signal catchers get called from the stack, + * not from text space. too bad. + */ + frompc -= p->lowpc; + if (frompc > p->textsize) + goto done; + +#if (HASHFRACTION & (HASHFRACTION - 1)) == 0 + if (p->hashfraction == HASHFRACTION) + frompcindex = + &p->froms[ + (size_t)(frompc / (HASHFRACTION * sizeof(*p->froms)))]; + else +#endif + frompcindex = + &p->froms[ + (size_t)(frompc / (p->hashfraction * sizeof(*p->froms)))]; + toindex = *frompcindex; + if (toindex == 0) { + /* + * first time traversing this arc + */ + toindex = ++p->tos[0].link; + if (toindex >= p->tolimit) + /* halt further profiling */ + goto overflow; + + *frompcindex = (u_short)toindex; + top = &p->tos[(size_t)toindex]; + top->selfpc = selfpc; + top->count = 1; + top->link = 0; + goto done; + } + top = &p->tos[(size_t)toindex]; + if (top->selfpc == selfpc) { + /* + * arc at front of chain; usual case. + */ + top->count++; + goto done; + } + /* + * have to go looking down chain for it. + * top points to what we are looking at, + * prevtop points to previous top. + * we know it is not at the head of the chain. + */ + for (; /* goto done */; ) { + if (top->link == 0) { + /* + * top is end of the chain and none of the chain + * had top->selfpc == selfpc. + * so we allocate a new tostruct + * and link it to the head of the chain. + */ + toindex = ++p->tos[0].link; + if (toindex >= p->tolimit) + goto overflow; + + top = &p->tos[(size_t)toindex]; + top->selfpc = selfpc; + top->count = 1; + top->link = *frompcindex; + *frompcindex = (u_short)toindex; + goto done; + } + /* + * otherwise, check the next arc on the chain. + */ + prevtop = top; + top = &p->tos[top->link]; + if (top->selfpc == selfpc) { + /* + * there it is. + * increment its count + * move it to the head of the chain. + */ + top->count++; + toindex = prevtop->link; + prevtop->link = top->link; + top->link = *frompcindex; + *frompcindex = (u_short)toindex; + goto done; + } + + } +done: + p->state = GMON_PROF_ON; +#ifdef _KERNEL + MCOUNT_EXIT; +#endif + return; +overflow: + p->state = GMON_PROF_ERROR; +#ifdef _KERNEL + MCOUNT_EXIT; +#endif + return; +} + +#ifdef MCOUNT +/* + * Actual definition of mcount function. Defined in , + * which is included by . + */ +MCOUNT +#endif + +#endif /* (!_KERNEL || GPROF) && !_STANDALONE */ diff --git a/common/lib/libc/hash/rmd160/rmd160.c b/common/lib/libc/hash/rmd160/rmd160.c new file mode 100644 index 000000000..ea107e3ca --- /dev/null +++ b/common/lib/libc/hash/rmd160/rmd160.c @@ -0,0 +1,393 @@ +/* $NetBSD: rmd160.c,v 1.5 2009/08/21 09:40:51 skrll Exp $ */ +/* $KAME: rmd160.c,v 1.2 2003/07/25 09:37:55 itojun Exp $ */ +/* $OpenBSD: rmd160.c,v 1.3 2001/09/26 21:40:13 markus Exp $ */ +/* + * Copyright (c) 2001 Markus Friedl. 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 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. + */ +/* + * Preneel, Bosselaers, Dobbertin, "The Cryptographic Hash Function RIPEMD-160", + * RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997, + * ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf + */ + +#include + +#if defined(_KERNEL) || defined(_STANDALONE) +__KERNEL_RCSID(0, "$NetBSD: rmd160.c,v 1.5 2009/08/21 09:40:51 skrll Exp $"); + +#include + +#else + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: rmd160.c,v 1.5 2009/08/21 09:40:51 skrll Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#endif + +#include +#include +#include + + +#define PUT_64BIT_LE(cp, value) do { \ + (cp)[7] = (u_char)((value) >> 56); \ + (cp)[6] = (u_char)((value) >> 48); \ + (cp)[5] = (u_char)((value) >> 40); \ + (cp)[4] = (u_char)((value) >> 32); \ + (cp)[3] = (u_char)((value) >> 24); \ + (cp)[2] = (u_char)((value) >> 16); \ + (cp)[1] = (u_char)((value) >> 8); \ + (cp)[0] = (u_char)((value)); } while (/*CONSTCOND*/0) + +#define PUT_32BIT_LE(cp, value) do { \ + (cp)[3] = (value) >> 24; \ + (cp)[2] = (value) >> 16; \ + (cp)[1] = (value) >> 8; \ + (cp)[0] = (value); } while (/*CONSTCOND*/0) + +#define H0 0x67452301U +#define H1 0xEFCDAB89U +#define H2 0x98BADCFEU +#define H3 0x10325476U +#define H4 0xC3D2E1F0U + +#define K0 0x00000000U +#define K1 0x5A827999U +#define K2 0x6ED9EBA1U +#define K3 0x8F1BBCDCU +#define K4 0xA953FD4EU + +#define KK0 0x50A28BE6U +#define KK1 0x5C4DD124U +#define KK2 0x6D703EF3U +#define KK3 0x7A6D76E9U +#define KK4 0x00000000U + +/* rotate x left n bits. */ +#define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n)))) + +#define F0(x, y, z) ((x) ^ (y) ^ (z)) +#define F1(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define F2(x, y, z) (((x) | (~y)) ^ (z)) +#define F3(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define F4(x, y, z) ((x) ^ ((y) | (~z))) + +#define R(a, b, c, d, e, Fj, Kj, sj, rj) \ + do { \ + a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \ + c = ROL(10, c); \ + } while(/*CONSTCOND*/0) + +#define X(i) x[i] + +static const u_char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#if defined(__weak_alias) +__weak_alias(RMD160Init,_RMD160Init) +__weak_alias(RMD160Update,_RMD160Update) +__weak_alias(RMD160Final,_RMD160Final) +__weak_alias(RMD160Transform,_RMD160Transform) +#endif +#endif + +void +RMD160Init(RMD160_CTX *ctx) +{ + ctx->count = 0; + ctx->state[0] = H0; + ctx->state[1] = H1; + ctx->state[2] = H2; + ctx->state[3] = H3; + ctx->state[4] = H4; +} + +void +RMD160Update(RMD160_CTX *ctx, const u_char *input, uint32_t len) +{ + uint32_t have, off, need; + + have = (uint32_t)((ctx->count/8) % 64); + need = 64 - have; + ctx->count += 8 * len; + off = 0; + + if (len >= need) { + if (have) { + memcpy(ctx->buffer + have, input, (size_t)need); + RMD160Transform(ctx->state, ctx->buffer); + off = need; + have = 0; + } + /* now the buffer is empty */ + while (off + 64 <= len) { + RMD160Transform(ctx->state, input+off); + off += 64; + } + } + if (off < len) + memcpy(ctx->buffer + have, input+off, (size_t)len-off); +} + +void +RMD160Final(u_char digest[20], RMD160_CTX *ctx) +{ + int i; + u_char size[8]; + uint32_t padlen; + + PUT_64BIT_LE(size, ctx->count); + + /* + * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes + * for the size + */ + padlen = (uint32_t)(64 - ((ctx->count/8) % 64)); + if (padlen < 1 + 8) + padlen += 64; + RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ + RMD160Update(ctx, size, 8); + + if (digest != NULL) + for (i = 0; i < 5; i++) + PUT_32BIT_LE(digest + i*4, ctx->state[i]); + + memset(ctx, 0, sizeof (*ctx)); +} + +void +RMD160Transform(uint32_t state[5], const u_char block[64]) +{ + uint32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16]; + +#if BYTE_ORDER == LITTLE_ENDIAN + memcpy(x, block, (size_t)64); +#else + int i; + + for (i = 0; i < 16; i++) + x[i] = le32dec(block+i*4); +#endif + + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + + /* Round 1 */ + R(a, b, c, d, e, F0, K0, 11, 0); + R(e, a, b, c, d, F0, K0, 14, 1); + R(d, e, a, b, c, F0, K0, 15, 2); + R(c, d, e, a, b, F0, K0, 12, 3); + R(b, c, d, e, a, F0, K0, 5, 4); + R(a, b, c, d, e, F0, K0, 8, 5); + R(e, a, b, c, d, F0, K0, 7, 6); + R(d, e, a, b, c, F0, K0, 9, 7); + R(c, d, e, a, b, F0, K0, 11, 8); + R(b, c, d, e, a, F0, K0, 13, 9); + R(a, b, c, d, e, F0, K0, 14, 10); + R(e, a, b, c, d, F0, K0, 15, 11); + R(d, e, a, b, c, F0, K0, 6, 12); + R(c, d, e, a, b, F0, K0, 7, 13); + R(b, c, d, e, a, F0, K0, 9, 14); + R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */ + /* Round 2 */ + R(e, a, b, c, d, F1, K1, 7, 7); + R(d, e, a, b, c, F1, K1, 6, 4); + R(c, d, e, a, b, F1, K1, 8, 13); + R(b, c, d, e, a, F1, K1, 13, 1); + R(a, b, c, d, e, F1, K1, 11, 10); + R(e, a, b, c, d, F1, K1, 9, 6); + R(d, e, a, b, c, F1, K1, 7, 15); + R(c, d, e, a, b, F1, K1, 15, 3); + R(b, c, d, e, a, F1, K1, 7, 12); + R(a, b, c, d, e, F1, K1, 12, 0); + R(e, a, b, c, d, F1, K1, 15, 9); + R(d, e, a, b, c, F1, K1, 9, 5); + R(c, d, e, a, b, F1, K1, 11, 2); + R(b, c, d, e, a, F1, K1, 7, 14); + R(a, b, c, d, e, F1, K1, 13, 11); + R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */ + /* Round 3 */ + R(d, e, a, b, c, F2, K2, 11, 3); + R(c, d, e, a, b, F2, K2, 13, 10); + R(b, c, d, e, a, F2, K2, 6, 14); + R(a, b, c, d, e, F2, K2, 7, 4); + R(e, a, b, c, d, F2, K2, 14, 9); + R(d, e, a, b, c, F2, K2, 9, 15); + R(c, d, e, a, b, F2, K2, 13, 8); + R(b, c, d, e, a, F2, K2, 15, 1); + R(a, b, c, d, e, F2, K2, 14, 2); + R(e, a, b, c, d, F2, K2, 8, 7); + R(d, e, a, b, c, F2, K2, 13, 0); + R(c, d, e, a, b, F2, K2, 6, 6); + R(b, c, d, e, a, F2, K2, 5, 13); + R(a, b, c, d, e, F2, K2, 12, 11); + R(e, a, b, c, d, F2, K2, 7, 5); + R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */ + /* Round 4 */ + R(c, d, e, a, b, F3, K3, 11, 1); + R(b, c, d, e, a, F3, K3, 12, 9); + R(a, b, c, d, e, F3, K3, 14, 11); + R(e, a, b, c, d, F3, K3, 15, 10); + R(d, e, a, b, c, F3, K3, 14, 0); + R(c, d, e, a, b, F3, K3, 15, 8); + R(b, c, d, e, a, F3, K3, 9, 12); + R(a, b, c, d, e, F3, K3, 8, 4); + R(e, a, b, c, d, F3, K3, 9, 13); + R(d, e, a, b, c, F3, K3, 14, 3); + R(c, d, e, a, b, F3, K3, 5, 7); + R(b, c, d, e, a, F3, K3, 6, 15); + R(a, b, c, d, e, F3, K3, 8, 14); + R(e, a, b, c, d, F3, K3, 6, 5); + R(d, e, a, b, c, F3, K3, 5, 6); + R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */ + /* Round 5 */ + R(b, c, d, e, a, F4, K4, 9, 4); + R(a, b, c, d, e, F4, K4, 15, 0); + R(e, a, b, c, d, F4, K4, 5, 5); + R(d, e, a, b, c, F4, K4, 11, 9); + R(c, d, e, a, b, F4, K4, 6, 7); + R(b, c, d, e, a, F4, K4, 8, 12); + R(a, b, c, d, e, F4, K4, 13, 2); + R(e, a, b, c, d, F4, K4, 12, 10); + R(d, e, a, b, c, F4, K4, 5, 14); + R(c, d, e, a, b, F4, K4, 12, 1); + R(b, c, d, e, a, F4, K4, 13, 3); + R(a, b, c, d, e, F4, K4, 14, 8); + R(e, a, b, c, d, F4, K4, 11, 11); + R(d, e, a, b, c, F4, K4, 8, 6); + R(c, d, e, a, b, F4, K4, 5, 15); + R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */ + + aa = a ; bb = b; cc = c; dd = d; ee = e; + + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + + /* Parallel round 1 */ + R(a, b, c, d, e, F4, KK0, 8, 5); + R(e, a, b, c, d, F4, KK0, 9, 14); + R(d, e, a, b, c, F4, KK0, 9, 7); + R(c, d, e, a, b, F4, KK0, 11, 0); + R(b, c, d, e, a, F4, KK0, 13, 9); + R(a, b, c, d, e, F4, KK0, 15, 2); + R(e, a, b, c, d, F4, KK0, 15, 11); + R(d, e, a, b, c, F4, KK0, 5, 4); + R(c, d, e, a, b, F4, KK0, 7, 13); + R(b, c, d, e, a, F4, KK0, 7, 6); + R(a, b, c, d, e, F4, KK0, 8, 15); + R(e, a, b, c, d, F4, KK0, 11, 8); + R(d, e, a, b, c, F4, KK0, 14, 1); + R(c, d, e, a, b, F4, KK0, 14, 10); + R(b, c, d, e, a, F4, KK0, 12, 3); + R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */ + /* Parallel round 2 */ + R(e, a, b, c, d, F3, KK1, 9, 6); + R(d, e, a, b, c, F3, KK1, 13, 11); + R(c, d, e, a, b, F3, KK1, 15, 3); + R(b, c, d, e, a, F3, KK1, 7, 7); + R(a, b, c, d, e, F3, KK1, 12, 0); + R(e, a, b, c, d, F3, KK1, 8, 13); + R(d, e, a, b, c, F3, KK1, 9, 5); + R(c, d, e, a, b, F3, KK1, 11, 10); + R(b, c, d, e, a, F3, KK1, 7, 14); + R(a, b, c, d, e, F3, KK1, 7, 15); + R(e, a, b, c, d, F3, KK1, 12, 8); + R(d, e, a, b, c, F3, KK1, 7, 12); + R(c, d, e, a, b, F3, KK1, 6, 4); + R(b, c, d, e, a, F3, KK1, 15, 9); + R(a, b, c, d, e, F3, KK1, 13, 1); + R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */ + /* Parallel round 3 */ + R(d, e, a, b, c, F2, KK2, 9, 15); + R(c, d, e, a, b, F2, KK2, 7, 5); + R(b, c, d, e, a, F2, KK2, 15, 1); + R(a, b, c, d, e, F2, KK2, 11, 3); + R(e, a, b, c, d, F2, KK2, 8, 7); + R(d, e, a, b, c, F2, KK2, 6, 14); + R(c, d, e, a, b, F2, KK2, 6, 6); + R(b, c, d, e, a, F2, KK2, 14, 9); + R(a, b, c, d, e, F2, KK2, 12, 11); + R(e, a, b, c, d, F2, KK2, 13, 8); + R(d, e, a, b, c, F2, KK2, 5, 12); + R(c, d, e, a, b, F2, KK2, 14, 2); + R(b, c, d, e, a, F2, KK2, 13, 10); + R(a, b, c, d, e, F2, KK2, 13, 0); + R(e, a, b, c, d, F2, KK2, 7, 4); + R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */ + /* Parallel round 4 */ + R(c, d, e, a, b, F1, KK3, 15, 8); + R(b, c, d, e, a, F1, KK3, 5, 6); + R(a, b, c, d, e, F1, KK3, 8, 4); + R(e, a, b, c, d, F1, KK3, 11, 1); + R(d, e, a, b, c, F1, KK3, 14, 3); + R(c, d, e, a, b, F1, KK3, 14, 11); + R(b, c, d, e, a, F1, KK3, 6, 15); + R(a, b, c, d, e, F1, KK3, 14, 0); + R(e, a, b, c, d, F1, KK3, 6, 5); + R(d, e, a, b, c, F1, KK3, 9, 12); + R(c, d, e, a, b, F1, KK3, 12, 2); + R(b, c, d, e, a, F1, KK3, 9, 13); + R(a, b, c, d, e, F1, KK3, 12, 9); + R(e, a, b, c, d, F1, KK3, 5, 7); + R(d, e, a, b, c, F1, KK3, 15, 10); + R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */ + /* Parallel round 5 */ + R(b, c, d, e, a, F0, KK4, 8, 12); + R(a, b, c, d, e, F0, KK4, 5, 15); + R(e, a, b, c, d, F0, KK4, 12, 10); + R(d, e, a, b, c, F0, KK4, 9, 4); + R(c, d, e, a, b, F0, KK4, 12, 1); + R(b, c, d, e, a, F0, KK4, 5, 5); + R(a, b, c, d, e, F0, KK4, 14, 8); + R(e, a, b, c, d, F0, KK4, 6, 7); + R(d, e, a, b, c, F0, KK4, 8, 6); + R(c, d, e, a, b, F0, KK4, 13, 2); + R(b, c, d, e, a, F0, KK4, 6, 13); + R(a, b, c, d, e, F0, KK4, 5, 14); + R(e, a, b, c, d, F0, KK4, 15, 0); + R(d, e, a, b, c, F0, KK4, 13, 3); + R(c, d, e, a, b, F0, KK4, 11, 9); + R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */ + + t = state[1] + cc + d; + state[1] = state[2] + dd + e; + state[2] = state[3] + ee + a; + state[3] = state[4] + aa + b; + state[4] = state[0] + bb + c; + state[0] = t; +} diff --git a/common/lib/libc/hash/sha1/sha1.c b/common/lib/libc/hash/sha1/sha1.c new file mode 100644 index 000000000..cbd60fc79 --- /dev/null +++ b/common/lib/libc/hash/sha1/sha1.c @@ -0,0 +1,289 @@ +/* $NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $ */ +/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ + +/* + * SHA-1 in C + * By Steve Reid + * 100% Public Domain + * + * Test Vectors (from FIPS PUB 180-1) + * "abc" + * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D + * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 + * A million repetitions of "a" + * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F + */ + +#define SHA1HANDSOFF /* Copies data before messing with it. */ + +#include + +#if defined(_KERNEL) || defined(_STANDALONE) +__KERNEL_RCSID(0, "$NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $"); + +#include + +#else + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#endif + +#include +#include + + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#if !HAVE_SHA1_H + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +/* + * blk0() and blk() perform the initial expand. + * I got the idea of expanding during the round function from SSLeay + */ +#if BYTE_ORDER == LITTLE_ENDIAN +# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ + |(rol(block->l[i],8)&0x00FF00FF)) +#else +# define blk0(i) block->l[i] +#endif +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ + ^block->l[(i+2)&15]^block->l[i&15],1)) + +/* + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 + */ +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); + + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#if defined(__weak_alias) +__weak_alias(SHA1Transform,_SHA1Transform) +__weak_alias(SHA1Init,_SHA1Init) +__weak_alias(SHA1Update,_SHA1Update) +__weak_alias(SHA1Final,_SHA1Final) +#endif +#endif + +typedef union { + uint8_t c[64]; + uint32_t l[16]; +} CHAR64LONG16; + +/* old sparc64 gcc could not compile this */ +#undef SPARC64_GCC_WORKAROUND +#if defined(__sparc64__) && defined(__GNUC__) && __GNUC__ < 3 +#define SPARC64_GCC_WORKAROUND +#endif + +#ifdef SPARC64_GCC_WORKAROUND +void do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); +void do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); +void do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); +void do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); + +#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i) +#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i) +#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i) +#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i) +#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i) + +void +do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) +{ + nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3); + nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); + nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11); + nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15); + nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19); +} + +void +do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) +{ + nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23); + nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); + nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31); + nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35); + nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39); +} + +void +do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) +{ + nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43); + nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); + nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51); + nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55); + nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59); +} + +void +do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) +{ + nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63); + nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); + nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); + nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75); + nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); +} +#endif + +/* + * Hash a single 512-bit block. This is the core of the algorithm. + */ +void SHA1Transform(uint32_t state[5], const uint8_t buffer[64]) +{ + uint32_t a, b, c, d, e; + CHAR64LONG16 *block; + +#ifdef SHA1HANDSOFF + CHAR64LONG16 workspace; +#endif + + _DIAGASSERT(buffer != 0); + _DIAGASSERT(state != 0); + +#ifdef SHA1HANDSOFF + block = &workspace; + (void)memcpy(block, buffer, 64); +#else + block = (CHAR64LONG16 *)(void *)buffer; +#endif + + /* Copy context->state[] to working vars */ + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + +#ifdef SPARC64_GCC_WORKAROUND + do_R01(&a, &b, &c, &d, &e, block); + do_R2(&a, &b, &c, &d, &e, block); + do_R3(&a, &b, &c, &d, &e, block); + do_R4(&a, &b, &c, &d, &e, block); +#else + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); +#endif + + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + + /* Wipe variables */ + a = b = c = d = e = 0; +} + + +/* + * SHA1Init - Initialize new context + */ +void SHA1Init(SHA1_CTX *context) +{ + + _DIAGASSERT(context != 0); + + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + + +/* + * Run your data through this. + */ +void SHA1Update(SHA1_CTX *context, const uint8_t *data, unsigned int len) +{ + unsigned int i, j; + + _DIAGASSERT(context != 0); + _DIAGASSERT(data != 0); + + j = context->count[0]; + if ((context->count[0] += len << 3) < j) + context->count[1] += (len>>29)+1; + j = (j >> 3) & 63; + if ((j + len) > 63) { + (void)memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) + SHA1Transform(context->state, &data[i]); + j = 0; + } else { + i = 0; + } + (void)memcpy(&context->buffer[j], &data[i], len - i); +} + + +/* + * Add padding and return the message digest. + */ +void SHA1Final(uint8_t digest[20], SHA1_CTX *context) +{ + unsigned int i; + uint8_t finalcount[8]; + + _DIAGASSERT(digest != 0); + _DIAGASSERT(context != 0); + + for (i = 0; i < 8; i++) { + finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + SHA1Update(context, (const uint8_t *)"\200", 1); + while ((context->count[0] & 504) != 448) + SHA1Update(context, (const uint8_t *)"\0", 1); + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + + if (digest) { + for (i = 0; i < 20; i++) + digest[i] = (uint8_t) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } +} + +#endif /* HAVE_SHA1_H */ diff --git a/common/lib/libc/hash/sha2/sha2.c b/common/lib/libc/hash/sha2/sha2.c new file mode 100644 index 000000000..55fd47df0 --- /dev/null +++ b/common/lib/libc/hash/sha2/sha2.c @@ -0,0 +1,1007 @@ +/* $NetBSD: sha2.c,v 1.21 2010/01/24 21:11:18 joerg Exp $ */ +/* $KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $ */ + +/* + * sha2.c + * + * Version 1.0.0beta1 + * + * Written by Aaron D. Gifford + * + * Copyright 2000 Aaron D. Gifford. 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 copyright holder nor the names of contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(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 AUTHOR(S) OR CONTRIBUTOR(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. + * + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include + +#if defined(_KERNEL) || defined(_STANDALONE) +__KERNEL_RCSID(0, "$NetBSD: sha2.c,v 1.21 2010/01/24 21:11:18 joerg Exp $"); + +#include /* XXX: to pull for vax memset(9) */ +#include + +#else + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha2.c,v 1.21 2010/01/24 21:11:18 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#endif + +#include +#include + +#if HAVE_NBTOOL_CONFIG_H +# if HAVE_SYS_ENDIAN_H +# include +# else +# undef htobe32 +# undef htobe64 +# undef be32toh +# undef be64toh + +static uint32_t +htobe32(uint32_t x) +{ + uint8_t p[4]; + memcpy(p, &x, 4); + + return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); +} + +static uint64_t +htobe64(uint64_t x) +{ + uint8_t p[8]; + uint32_t u, v; + memcpy(p, &x, 8); + + u = ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); + v = ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); + + return ((((uint64_t)u) << 32) | v); +} + +static uint32_t +be32toh(uint32_t x) +{ + return htobe32(x); +} + +static uint64_t +be64toh(uint64_t x) +{ + return htobe64(x); +} +# endif +#endif + +/*** SHA-256/384/512 Various Length Definitions ***********************/ +/* NOTE: Most of these are in sha2.h */ +#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) +#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16) +#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) + +/* + * Macro for incrementally adding the unsigned 64-bit integer n to the + * unsigned 128-bit integer (represented using a two-element array of + * 64-bit words): + */ +#define ADDINC128(w,n) { \ + (w)[0] += (uint64_t)(n); \ + if ((w)[0] < (n)) { \ + (w)[1]++; \ + } \ +} + +/*** THE SIX LOGICAL FUNCTIONS ****************************************/ +/* + * Bit shifting and rotation (used by the six SHA-XYZ logical functions: + * + * NOTE: The naming of R and S appears backwards here (R is a SHIFT and + * S is a ROTATION) because the SHA-256/384/512 description document + * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this + * same "backwards" definition. + */ +/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ +#define R(b,x) ((x) >> (b)) +/* 32-bit Rotate-right (used in SHA-256): */ +#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) +/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ +#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) + +/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ +#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) +#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) + +/* Four of six logical functions used in SHA-256: */ +#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) +#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) +#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x))) +#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) + +/* Four of six logical functions used in SHA-384 and SHA-512: */ +#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) +#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) +#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x))) +#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x))) + +/*** INTERNAL FUNCTION PROTOTYPES *************************************/ +/* NOTE: These should not be accessed directly from outside this + * library -- they are intended for private internal visibility/use + * only. + */ +static void SHA512_Last(SHA512_CTX *); +void SHA224_Transform(SHA224_CTX *, const uint32_t*); +void SHA256_Transform(SHA256_CTX *, const uint32_t*); +void SHA384_Transform(SHA384_CTX *, const uint64_t*); +void SHA512_Transform(SHA512_CTX *, const uint64_t*); + + +/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ +/* Hash constant words K for SHA-256: */ +static const uint32_t K256[64] = { + 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, + 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, + 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, + 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, + 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, + 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, + 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, + 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, + 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, + 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, + 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, + 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, + 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, + 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, + 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, + 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL +}; + +/* Initial hash value H for SHA-224: */ +static const uint32_t sha224_initial_hash_value[8] = { + 0xc1059ed8UL, + 0x367cd507UL, + 0x3070dd17UL, + 0xf70e5939UL, + 0xffc00b31UL, + 0x68581511UL, + 0x64f98fa7UL, + 0xbefa4fa4UL +}; + +/* Initial hash value H for SHA-256: */ +static const uint32_t sha256_initial_hash_value[8] = { + 0x6a09e667UL, + 0xbb67ae85UL, + 0x3c6ef372UL, + 0xa54ff53aUL, + 0x510e527fUL, + 0x9b05688cUL, + 0x1f83d9abUL, + 0x5be0cd19UL +}; + +/* Hash constant words K for SHA-384 and SHA-512: */ +static const uint64_t K512[80] = { + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL +}; + +/* Initial hash value H for SHA-384 */ +static const uint64_t sha384_initial_hash_value[8] = { + 0xcbbb9d5dc1059ed8ULL, + 0x629a292a367cd507ULL, + 0x9159015a3070dd17ULL, + 0x152fecd8f70e5939ULL, + 0x67332667ffc00b31ULL, + 0x8eb44a8768581511ULL, + 0xdb0c2e0d64f98fa7ULL, + 0x47b5481dbefa4fa4ULL +}; + +/* Initial hash value H for SHA-512 */ +static const uint64_t sha512_initial_hash_value[8] = { + 0x6a09e667f3bcc908ULL, + 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, + 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, + 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, + 0x5be0cd19137e2179ULL +}; + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#if defined(__weak_alias) +__weak_alias(SHA224_Init,_SHA224_Init) +__weak_alias(SHA224_Update,_SHA224_Update) +__weak_alias(SHA224_Final,_SHA224_Final) +__weak_alias(SHA224_Transform,_SHA224_Transform) + +__weak_alias(SHA256_Init,_SHA256_Init) +__weak_alias(SHA256_Update,_SHA256_Update) +__weak_alias(SHA256_Final,_SHA256_Final) +__weak_alias(SHA256_Transform,_SHA256_Transform) + +__weak_alias(SHA384_Init,_SHA384_Init) +__weak_alias(SHA384_Update,_SHA384_Update) +__weak_alias(SHA384_Final,_SHA384_Final) +__weak_alias(SHA384_Transform,_SHA384_Transform) + +__weak_alias(SHA512_Init,_SHA512_Init) +__weak_alias(SHA512_Update,_SHA512_Update) +__weak_alias(SHA512_Final,_SHA512_Final) +__weak_alias(SHA512_Transform,_SHA512_Transform) +#endif +#endif + +/*** SHA-256: *********************************************************/ +int +SHA256_Init(SHA256_CTX *context) +{ + if (context == NULL) + return 1; + + memcpy(context->state, sha256_initial_hash_value, + (size_t)(SHA256_DIGEST_LENGTH)); + memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH)); + context->bitcount = 0; + + return 1; +} + +#ifdef SHA2_UNROLL_TRANSFORM + +/* Unrolled SHA-256 round macros: */ + +#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \ + W256[j] = be32toh(*data); \ + ++data; \ + T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \ + K256[j] + W256[j]; \ + (d) += T1; \ + (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ + j++ + +#define ROUND256(a,b,c,d,e,f,g,h) \ + s0 = W256[(j+1)&0x0f]; \ + s0 = sigma0_256(s0); \ + s1 = W256[(j+14)&0x0f]; \ + s1 = sigma1_256(s1); \ + T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \ + (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \ + (d) += T1; \ + (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ + j++ + +void +SHA256_Transform(SHA256_CTX *context, const uint32_t *data) +{ + uint32_t a, b, c, d, e, f, g, h, s0, s1; + uint32_t T1, *W256; + int j; + + W256 = (uint32_t *)context->buffer; + + /* Initialize registers with the prev. intermediate value */ + a = context->state[0]; + b = context->state[1]; + c = context->state[2]; + d = context->state[3]; + e = context->state[4]; + f = context->state[5]; + g = context->state[6]; + h = context->state[7]; + + j = 0; + do { + /* Rounds 0 to 15 (unrolled): */ + ROUND256_0_TO_15(a,b,c,d,e,f,g,h); + ROUND256_0_TO_15(h,a,b,c,d,e,f,g); + ROUND256_0_TO_15(g,h,a,b,c,d,e,f); + ROUND256_0_TO_15(f,g,h,a,b,c,d,e); + ROUND256_0_TO_15(e,f,g,h,a,b,c,d); + ROUND256_0_TO_15(d,e,f,g,h,a,b,c); + ROUND256_0_TO_15(c,d,e,f,g,h,a,b); + ROUND256_0_TO_15(b,c,d,e,f,g,h,a); + } while (j < 16); + + /* Now for the remaining rounds to 64: */ + do { + ROUND256(a,b,c,d,e,f,g,h); + ROUND256(h,a,b,c,d,e,f,g); + ROUND256(g,h,a,b,c,d,e,f); + ROUND256(f,g,h,a,b,c,d,e); + ROUND256(e,f,g,h,a,b,c,d); + ROUND256(d,e,f,g,h,a,b,c); + ROUND256(c,d,e,f,g,h,a,b); + ROUND256(b,c,d,e,f,g,h,a); + } while (j < 64); + + /* Compute the current intermediate hash value */ + context->state[0] += a; + context->state[1] += b; + context->state[2] += c; + context->state[3] += d; + context->state[4] += e; + context->state[5] += f; + context->state[6] += g; + context->state[7] += h; + + /* Clean up */ + a = b = c = d = e = f = g = h = T1 = 0; +} + +#else /* SHA2_UNROLL_TRANSFORM */ + +void +SHA256_Transform(SHA256_CTX *context, const uint32_t *data) +{ + uint32_t a, b, c, d, e, f, g, h, s0, s1; + uint32_t T1, T2, *W256; + int j; + + W256 = (uint32_t *)(void *)context->buffer; + + /* Initialize registers with the prev. intermediate value */ + a = context->state[0]; + b = context->state[1]; + c = context->state[2]; + d = context->state[3]; + e = context->state[4]; + f = context->state[5]; + g = context->state[6]; + h = context->state[7]; + + j = 0; + do { + W256[j] = be32toh(*data); + ++data; + /* Apply the SHA-256 compression function to update a..h */ + T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j]; + T2 = Sigma0_256(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + + j++; + } while (j < 16); + + do { + /* Part of the message block expansion: */ + s0 = W256[(j+1)&0x0f]; + s0 = sigma0_256(s0); + s1 = W256[(j+14)&0x0f]; + s1 = sigma1_256(s1); + + /* Apply the SHA-256 compression function to update a..h */ + T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + + (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); + T2 = Sigma0_256(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + + j++; + } while (j < 64); + + /* Compute the current intermediate hash value */ + context->state[0] += a; + context->state[1] += b; + context->state[2] += c; + context->state[3] += d; + context->state[4] += e; + context->state[5] += f; + context->state[6] += g; + context->state[7] += h; + + /* Clean up */ + a = b = c = d = e = f = g = h = T1 = T2 = 0; +} + +#endif /* SHA2_UNROLL_TRANSFORM */ + +int +SHA256_Update(SHA256_CTX *context, const uint8_t *data, size_t len) +{ + unsigned int freespace, usedspace; + + if (len == 0) { + /* Calling with no data is valid - we do nothing */ + return 1; + } + + usedspace = (unsigned int)((context->bitcount >> 3) % + SHA256_BLOCK_LENGTH); + if (usedspace > 0) { + /* Calculate how much free space is available in the buffer */ + freespace = SHA256_BLOCK_LENGTH - usedspace; + + if (len >= freespace) { + /* Fill the buffer completely and process it */ + memcpy(&context->buffer[usedspace], data, + (size_t)(freespace)); + context->bitcount += freespace << 3; + len -= freespace; + data += freespace; + SHA256_Transform(context, + (uint32_t *)(void *)context->buffer); + } else { + /* The buffer is not yet full */ + memcpy(&context->buffer[usedspace], data, len); + context->bitcount += len << 3; + /* Clean up: */ + usedspace = freespace = 0; + return 1; + } + } + /* + * Process as many complete blocks as possible. + * + * Check alignment of the data pointer. If it is 32bit aligned, + * SHA256_Transform can be called directly on the data stream, + * otherwise enforce the alignment by copy into the buffer. + */ + if ((uintptr_t)data % 4 == 0) { + while (len >= SHA256_BLOCK_LENGTH) { + SHA256_Transform(context, + (const uint32_t *)(const void *)data); + context->bitcount += SHA256_BLOCK_LENGTH << 3; + len -= SHA256_BLOCK_LENGTH; + data += SHA256_BLOCK_LENGTH; + } + } else { + while (len >= SHA256_BLOCK_LENGTH) { + memcpy(context->buffer, data, SHA256_BLOCK_LENGTH); + SHA256_Transform(context, + (const uint32_t *)(const void *)context->buffer); + context->bitcount += SHA256_BLOCK_LENGTH << 3; + len -= SHA256_BLOCK_LENGTH; + data += SHA256_BLOCK_LENGTH; + } + } + if (len > 0) { + /* There's left-overs, so save 'em */ + memcpy(context->buffer, data, len); + context->bitcount += len << 3; + } + /* Clean up: */ + usedspace = freespace = 0; + + return 1; +} + +static int +SHA224_256_Final(uint8_t digest[], SHA256_CTX *context, size_t len) +{ + unsigned int usedspace; + size_t i; + + /* If no digest buffer is passed, we don't bother doing this: */ + if (digest != NULL) { + usedspace = (unsigned int)((context->bitcount >> 3) % + SHA256_BLOCK_LENGTH); + context->bitcount = htobe64(context->bitcount); + if (usedspace > 0) { + /* Begin padding with a 1 bit: */ + context->buffer[usedspace++] = 0x80; + + if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) { + /* Set-up for the last transform: */ + memset(&context->buffer[usedspace], 0, + (size_t)(SHA256_SHORT_BLOCK_LENGTH - + usedspace)); + } else { + if (usedspace < SHA256_BLOCK_LENGTH) { + memset(&context->buffer[usedspace], 0, + (size_t)(SHA256_BLOCK_LENGTH - + usedspace)); + } + /* Do second-to-last transform: */ + SHA256_Transform(context, + (uint32_t *)(void *)context->buffer); + + /* And set-up for the last transform: */ + memset(context->buffer, 0, + (size_t)(SHA256_SHORT_BLOCK_LENGTH)); + } + } else { + /* Set-up for the last transform: */ + memset(context->buffer, 0, + (size_t)(SHA256_SHORT_BLOCK_LENGTH)); + + /* Begin padding with a 1 bit: */ + *context->buffer = 0x80; + } + /* Set the bit count: */ + memcpy(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], + &context->bitcount, sizeof(context->bitcount)); + + /* Final transform: */ + SHA256_Transform(context, (uint32_t *)(void *)context->buffer); + + for (i = 0; i < len / 4; i++) + be32enc(digest + 4 * i, context->state[i]); + } + + /* Clean up state data: */ + memset(context, 0, sizeof(*context)); + usedspace = 0; + + return 1; +} + +int +SHA256_Final(uint8_t digest[], SHA256_CTX *context) +{ + return SHA224_256_Final(digest, context, SHA256_DIGEST_LENGTH); +} + +/*** SHA-224: *********************************************************/ +int +SHA224_Init(SHA224_CTX *context) +{ + if (context == NULL) + return 1; + + /* The state and buffer size are driven by SHA256, not by SHA224. */ + memcpy(context->state, sha224_initial_hash_value, + (size_t)(SHA256_DIGEST_LENGTH)); + memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH)); + context->bitcount = 0; + + return 1; +} + +int +SHA224_Update(SHA224_CTX *context, const uint8_t *data, size_t len) +{ + return SHA256_Update((SHA256_CTX *)context, data, len); +} + +void +SHA224_Transform(SHA224_CTX *context, const uint32_t *data) +{ + SHA256_Transform((SHA256_CTX *)context, data); +} + +int +SHA224_Final(uint8_t digest[], SHA224_CTX *context) +{ + return SHA224_256_Final(digest, (SHA256_CTX *)context, + SHA224_DIGEST_LENGTH); +} + +/*** SHA-512: *********************************************************/ +int +SHA512_Init(SHA512_CTX *context) +{ + if (context == NULL) + return 1; + + memcpy(context->state, sha512_initial_hash_value, + (size_t)(SHA512_DIGEST_LENGTH)); + memset(context->buffer, 0, (size_t)(SHA512_BLOCK_LENGTH)); + context->bitcount[0] = context->bitcount[1] = 0; + + return 1; +} + +#ifdef SHA2_UNROLL_TRANSFORM + +/* Unrolled SHA-512 round macros: */ +#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \ + W512[j] = be64toh(*data); \ + ++data; \ + T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \ + K512[j] + W512[j]; \ + (d) += T1, \ + (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \ + j++ + +#define ROUND512(a,b,c,d,e,f,g,h) \ + s0 = W512[(j+1)&0x0f]; \ + s0 = sigma0_512(s0); \ + s1 = W512[(j+14)&0x0f]; \ + s1 = sigma1_512(s1); \ + T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \ + (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \ + (d) += T1; \ + (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ + j++ + +void +SHA512_Transform(SHA512_CTX *context, const uint64_t *data) +{ + uint64_t a, b, c, d, e, f, g, h, s0, s1; + uint64_t T1, *W512 = (uint64_t *)context->buffer; + int j; + + /* Initialize registers with the prev. intermediate value */ + a = context->state[0]; + b = context->state[1]; + c = context->state[2]; + d = context->state[3]; + e = context->state[4]; + f = context->state[5]; + g = context->state[6]; + h = context->state[7]; + + j = 0; + do { + ROUND512_0_TO_15(a,b,c,d,e,f,g,h); + ROUND512_0_TO_15(h,a,b,c,d,e,f,g); + ROUND512_0_TO_15(g,h,a,b,c,d,e,f); + ROUND512_0_TO_15(f,g,h,a,b,c,d,e); + ROUND512_0_TO_15(e,f,g,h,a,b,c,d); + ROUND512_0_TO_15(d,e,f,g,h,a,b,c); + ROUND512_0_TO_15(c,d,e,f,g,h,a,b); + ROUND512_0_TO_15(b,c,d,e,f,g,h,a); + } while (j < 16); + + /* Now for the remaining rounds up to 79: */ + do { + ROUND512(a,b,c,d,e,f,g,h); + ROUND512(h,a,b,c,d,e,f,g); + ROUND512(g,h,a,b,c,d,e,f); + ROUND512(f,g,h,a,b,c,d,e); + ROUND512(e,f,g,h,a,b,c,d); + ROUND512(d,e,f,g,h,a,b,c); + ROUND512(c,d,e,f,g,h,a,b); + ROUND512(b,c,d,e,f,g,h,a); + } while (j < 80); + + /* Compute the current intermediate hash value */ + context->state[0] += a; + context->state[1] += b; + context->state[2] += c; + context->state[3] += d; + context->state[4] += e; + context->state[5] += f; + context->state[6] += g; + context->state[7] += h; + + /* Clean up */ + a = b = c = d = e = f = g = h = T1 = 0; +} + +#else /* SHA2_UNROLL_TRANSFORM */ + +void +SHA512_Transform(SHA512_CTX *context, const uint64_t *data) +{ + uint64_t a, b, c, d, e, f, g, h, s0, s1; + uint64_t T1, T2, *W512 = (void *)context->buffer; + int j; + + /* Initialize registers with the prev. intermediate value */ + a = context->state[0]; + b = context->state[1]; + c = context->state[2]; + d = context->state[3]; + e = context->state[4]; + f = context->state[5]; + g = context->state[6]; + h = context->state[7]; + + j = 0; + do { + W512[j] = be64toh(*data); + ++data; + /* Apply the SHA-512 compression function to update a..h */ + T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j]; + T2 = Sigma0_512(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + + j++; + } while (j < 16); + + do { + /* Part of the message block expansion: */ + s0 = W512[(j+1)&0x0f]; + s0 = sigma0_512(s0); + s1 = W512[(j+14)&0x0f]; + s1 = sigma1_512(s1); + + /* Apply the SHA-512 compression function to update a..h */ + T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + + (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); + T2 = Sigma0_512(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + + j++; + } while (j < 80); + + /* Compute the current intermediate hash value */ + context->state[0] += a; + context->state[1] += b; + context->state[2] += c; + context->state[3] += d; + context->state[4] += e; + context->state[5] += f; + context->state[6] += g; + context->state[7] += h; + + /* Clean up */ + a = b = c = d = e = f = g = h = T1 = T2 = 0; +} + +#endif /* SHA2_UNROLL_TRANSFORM */ + +int +SHA512_Update(SHA512_CTX *context, const uint8_t *data, size_t len) +{ + unsigned int freespace, usedspace; + + if (len == 0) { + /* Calling with no data is valid - we do nothing */ + return 1; + } + + usedspace = (unsigned int)((context->bitcount[0] >> 3) % + SHA512_BLOCK_LENGTH); + if (usedspace > 0) { + /* Calculate how much free space is available in the buffer */ + freespace = SHA512_BLOCK_LENGTH - usedspace; + + if (len >= freespace) { + /* Fill the buffer completely and process it */ + memcpy(&context->buffer[usedspace], data, + (size_t)(freespace)); + ADDINC128(context->bitcount, freespace << 3); + len -= freespace; + data += freespace; + SHA512_Transform(context, + (uint64_t *)(void *)context->buffer); + } else { + /* The buffer is not yet full */ + memcpy(&context->buffer[usedspace], data, len); + ADDINC128(context->bitcount, len << 3); + /* Clean up: */ + usedspace = freespace = 0; + return 1; + } + } + /* + * Process as many complete blocks as possible. + * + * Check alignment of the data pointer. If it is 64bit aligned, + * SHA512_Transform can be called directly on the data stream, + * otherwise enforce the alignment by copy into the buffer. + */ + if ((uintptr_t)data % 8 == 0) { + while (len >= SHA512_BLOCK_LENGTH) { + SHA512_Transform(context, + (const uint64_t*)(const void *)data); + ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); + len -= SHA512_BLOCK_LENGTH; + data += SHA512_BLOCK_LENGTH; + } + } else { + while (len >= SHA512_BLOCK_LENGTH) { + memcpy(context->buffer, data, SHA512_BLOCK_LENGTH); + SHA512_Transform(context, + (const void *)context->buffer); + ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); + len -= SHA512_BLOCK_LENGTH; + data += SHA512_BLOCK_LENGTH; + } + } + if (len > 0) { + /* There's left-overs, so save 'em */ + memcpy(context->buffer, data, len); + ADDINC128(context->bitcount, len << 3); + } + /* Clean up: */ + usedspace = freespace = 0; + + return 1; +} + +static void +SHA512_Last(SHA512_CTX *context) +{ + unsigned int usedspace; + + usedspace = (unsigned int)((context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH); + context->bitcount[0] = htobe64(context->bitcount[0]); + context->bitcount[1] = htobe64(context->bitcount[1]); + if (usedspace > 0) { + /* Begin padding with a 1 bit: */ + context->buffer[usedspace++] = 0x80; + + if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) { + /* Set-up for the last transform: */ + memset(&context->buffer[usedspace], 0, + (size_t)(SHA512_SHORT_BLOCK_LENGTH - usedspace)); + } else { + if (usedspace < SHA512_BLOCK_LENGTH) { + memset(&context->buffer[usedspace], 0, + (size_t)(SHA512_BLOCK_LENGTH - usedspace)); + } + /* Do second-to-last transform: */ + SHA512_Transform(context, + (uint64_t *)(void *)context->buffer); + + /* And set-up for the last transform: */ + memset(context->buffer, 0, + (size_t)(SHA512_BLOCK_LENGTH - 2)); + } + } else { + /* Prepare for final transform: */ + memset(context->buffer, 0, (size_t)(SHA512_SHORT_BLOCK_LENGTH)); + + /* Begin padding with a 1 bit: */ + *context->buffer = 0x80; + } + /* Store the length of input data (in bits): */ + memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], + &context->bitcount[1], sizeof(context->bitcount[1])); + memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8], + &context->bitcount[0], sizeof(context->bitcount[0])); + + /* Final transform: */ + SHA512_Transform(context, (uint64_t *)(void *)context->buffer); +} + +int +SHA512_Final(uint8_t digest[], SHA512_CTX *context) +{ + size_t i; + + /* If no digest buffer is passed, we don't bother doing this: */ + if (digest != NULL) { + SHA512_Last(context); + + /* Save the hash data for output: */ + for (i = 0; i < 8; ++i) + be64enc(digest + 8 * i, context->state[i]); + } + + /* Zero out state data */ + memset(context, 0, sizeof(*context)); + + return 1; +} + +/*** SHA-384: *********************************************************/ +int +SHA384_Init(SHA384_CTX *context) +{ + if (context == NULL) + return 1; + + memcpy(context->state, sha384_initial_hash_value, + (size_t)(SHA512_DIGEST_LENGTH)); + memset(context->buffer, 0, (size_t)(SHA384_BLOCK_LENGTH)); + context->bitcount[0] = context->bitcount[1] = 0; + + return 1; +} + +int +SHA384_Update(SHA384_CTX *context, const uint8_t *data, size_t len) +{ + return SHA512_Update((SHA512_CTX *)context, data, len); +} + +void +SHA384_Transform(SHA512_CTX *context, const uint64_t *data) +{ + SHA512_Transform((SHA512_CTX *)context, data); +} + +int +SHA384_Final(uint8_t digest[], SHA384_CTX *context) +{ + size_t i; + + /* If no digest buffer is passed, we don't bother doing this: */ + if (digest != NULL) { + SHA512_Last((SHA512_CTX *)context); + + /* Save the hash data for output: */ + for (i = 0; i < 6; ++i) + be64enc(digest + 8 * i, context->state[i]); + } + + /* Zero out state data */ + memset(context, 0, sizeof(*context)); + + return 1; +} diff --git a/common/lib/libc/inet/inet_addr.c b/common/lib/libc/inet/inet_addr.c new file mode 100644 index 000000000..551ba1716 --- /dev/null +++ b/common/lib/libc/inet/inet_addr.c @@ -0,0 +1,223 @@ +/* $NetBSD: inet_addr.c,v 1.2 2008/02/16 17:37:13 apb Exp $ */ + +/* + * Copyright (c) 1983, 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. 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. + */ + +/* + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * 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, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION 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. + */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Portions Copyright (c) 1996-1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 !defined(_KERNEL) && !defined(_STANDALONE) +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; +static const char rcsid[] = "Id: inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp"; +#else +__RCSID("$NetBSD: inet_addr.c,v 1.2 2008/02/16 17:37:13 apb Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include "namespace.h" +#include +#include + +#include +#include + +#include + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_aton,_inet_aton) +#endif +#else +#include +#include +#endif + +/* + * Ascii internet address interpretation routine. + * The value returned is in network order. + */ +uint32_t +inet_addr(const char *cp) { + struct in_addr val; + + if (inet_aton(cp, &val)) + return (val.s_addr); + return (INADDR_NONE); +} + +/* + * Check whether "cp" is a valid ascii representation + * of an Internet address and convert to a binary address. + * Returns 1 if the address is valid, 0 if not. + * This replaces inet_addr, the return value from which + * cannot distinguish between failure and a local broadcast address. + */ +int +inet_aton(const char *cp, struct in_addr *addr) { + uint32_t val; + int base, n; + char c; + uint8_t parts[4]; + uint8_t *pp = parts; + int digit; + + c = *cp; + for (;;) { + /* + * Collect number up to ``.''. + * Values are specified as for C: + * 0x=hex, 0=octal, isdigit=decimal. + */ + if (!isdigit((unsigned char)c)) + return (0); + val = 0; base = 10; digit = 0; + if (c == '0') { + c = *++cp; + if (c == 'x' || c == 'X') + base = 16, c = *++cp; + else { + base = 8; + digit = 1 ; + } + } + for (;;) { + if (isascii(c) && isdigit((unsigned char)c)) { + if (base == 8 && (c == '8' || c == '9')) + return (0); + val = (val * base) + (c - '0'); + c = *++cp; + digit = 1; + } else if (base == 16 && isascii(c) && + isxdigit((unsigned char)c)) { + val = (val << 4) | + (c + 10 - (islower((unsigned char)c) ? 'a' : 'A')); + c = *++cp; + digit = 1; + } else + break; + } + if (c == '.') { + /* + * Internet format: + * a.b.c.d + * a.b.c (with c treated as 16 bits) + * a.b (with b treated as 24 bits) + */ + if (pp >= parts + 3 || val > 0xffU) + return (0); + *pp++ = val; + c = *++cp; + } else + break; + } + /* + * Check for trailing characters. + */ + if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c))) + return (0); + /* + * Did we get a valid digit? + */ + if (!digit) + return (0); + /* + * Concoct the address according to + * the number of parts specified. + */ + n = pp - parts + 1; + switch (n) { + case 1: /* a -- 32 bits */ + break; + + case 2: /* a.b -- 8.24 bits */ + if (val > 0xffffffU) + return (0); + val |= parts[0] << 24; + break; + + case 3: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffffU) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xffU) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + if (addr != NULL) + addr->s_addr = htonl(val); + return (1); +} diff --git a/common/lib/libc/md/md4c.c b/common/lib/libc/md/md4c.c new file mode 100644 index 000000000..ee7a69726 --- /dev/null +++ b/common/lib/libc/md/md4c.c @@ -0,0 +1,324 @@ +/* $NetBSD: md4c.c,v 1.4 2009/03/16 05:59:21 cegger Exp $ */ + +/* + * This file is derived from the RSA Data Security, Inc. MD4 Message-Digest + * Algorithm and has been modified by Jason R. Thorpe + * for portability and formatting. + */ + +/* + * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. + * + * License to copy and use this software is granted provided that it + * is identified as the "RSA Data Security, Inc. MD4 Message-Digest + * Algorithm" in all material mentioning or referencing this software + * or this function. + * + * License is also granted to make and use derivative works provided + * that such works are identified as "derived from the RSA Data + * Security, Inc. MD4 Message-Digest Algorithm" in all material + * mentioning or referencing the derived work. + * + * RSA Data Security, Inc. makes no representations concerning either + * the merchantability of this software or the suitability of this + * software for any particular purpose. It is provided "as is" + * without express or implied warranty of any kind. + * + * These notices must be retained in any copies of any part of this + * documentation and/or software. + */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: md4c.c,v 1.4 2009/03/16 05:59:21 cegger Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#include +#include +#include + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#else + +#include +#include +#include + +#endif /* !_KERNEL && !_STANDALONE */ + +#if !HAVE_MD4_H + +typedef unsigned char *POINTER; +typedef uint16_t UINT2; +typedef uint32_t UINT4; + +/* + * Constants for MD4Transform routine. + */ +#define S11 3 +#define S12 7 +#define S13 11 +#define S14 19 +#define S21 3 +#define S22 5 +#define S23 9 +#define S24 13 +#define S31 3 +#define S32 9 +#define S33 11 +#define S34 15 + +static void MD4Transform __P((UINT4 [4], const unsigned char [64])); + +static void Encode __P((unsigned char *, UINT4 *, unsigned int)); +static void Decode __P((UINT4 *, const unsigned char *, unsigned int)); + +static const unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* + * F, G and H are basic MD4 functions. + */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) + +/* + * ROTATE_LEFT rotates x left n bits. + */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* + * FF, GG and HH are transformations for rounds 1, 2 and 3. + * Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s) { \ + (a) += F ((b), (c), (d)) + (x); \ + (a) = ROTATE_LEFT ((a), (s)); \ +} + +#define GG(a, b, c, d, x, s) { \ + (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \ + (a) = ROTATE_LEFT ((a), (s)); \ +} + +#define HH(a, b, c, d, x, s) { \ + (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \ + (a) = ROTATE_LEFT ((a), (s)); \ +} + +#if !defined(_KERNEL) && !defined(_STANDALONE) && defined(__weak_alias) +__weak_alias(MD4Init,_MD4Init) +__weak_alias(MD4Update,_MD4Update) +__weak_alias(MD4Final,_MD4Final) +__weak_alias(MD4Transform,_MD4Transform) +#endif + +/* + * MD4 initialization. Begins an MD4 operation, writing a new context. + */ +void +MD4Init(MD4_CTX *context) /* context */ +{ + + _DIAGASSERT(context != 0); + + context->count[0] = context->count[1] = 0; + + /* Load magic initialization constants. */ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* + * MD4 block update operation. Continues an MD4 message-digest + * operation, processing another message block, and updating the + * context. + */ +void +MD4Update (MD4_CTX *context, /* context */ + const unsigned char *input, /* input block */ + unsigned int inputLen) /* length of input block */ +{ + unsigned int i, idx, partLen; + + _DIAGASSERT(context != 0); + _DIAGASSERT(input != 0); + + /* Compute number of bytes mod 64 */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - idx; + + /* Transform as many times as possible. */ + if (inputLen >= partLen) { + memcpy(&context->buffer[idx], input, partLen); + MD4Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD4Transform(context->state, &input[i]); + + idx = 0; + } else + i = 0; + + /* Buffer remaining input */ + memcpy(&context->buffer[idx], &input[i], inputLen - i); +} + +/* + * MD4 finalization. Ends an MD4 message-digest operation, writing the + * message digest and zeroing the context. + */ +void +MD4Final (unsigned char digest[16], /* message digest */ + MD4_CTX *context) /* context */ +{ + unsigned char bits[8]; + unsigned int idx, padLen; + + _DIAGASSERT(digest != 0); + _DIAGASSERT(context != 0); + + /* Save number of bits */ + Encode(bits, context->count, 8); + + /* Pad out to 56 mod 64. */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); + MD4Update(context, PADDING, padLen); + + /* Append length (before padding) */ + MD4Update(context, bits, 8); + + /* Store state in digest */ + Encode(digest, context->state, 16); + + /* Zeroize sensitive information. */ + memset(context, 0, sizeof(*context)); +} + +/* + * MD4 basic transformation. Transforms state based on block. + */ +static void +MD4Transform (UINT4 state[4], const unsigned char block[64]) +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode(x, block, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11); /* 1 */ + FF (d, a, b, c, x[ 1], S12); /* 2 */ + FF (c, d, a, b, x[ 2], S13); /* 3 */ + FF (b, c, d, a, x[ 3], S14); /* 4 */ + FF (a, b, c, d, x[ 4], S11); /* 5 */ + FF (d, a, b, c, x[ 5], S12); /* 6 */ + FF (c, d, a, b, x[ 6], S13); /* 7 */ + FF (b, c, d, a, x[ 7], S14); /* 8 */ + FF (a, b, c, d, x[ 8], S11); /* 9 */ + FF (d, a, b, c, x[ 9], S12); /* 10 */ + FF (c, d, a, b, x[10], S13); /* 11 */ + FF (b, c, d, a, x[11], S14); /* 12 */ + FF (a, b, c, d, x[12], S11); /* 13 */ + FF (d, a, b, c, x[13], S12); /* 14 */ + FF (c, d, a, b, x[14], S13); /* 15 */ + FF (b, c, d, a, x[15], S14); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 0], S21); /* 17 */ + GG (d, a, b, c, x[ 4], S22); /* 18 */ + GG (c, d, a, b, x[ 8], S23); /* 19 */ + GG (b, c, d, a, x[12], S24); /* 20 */ + GG (a, b, c, d, x[ 1], S21); /* 21 */ + GG (d, a, b, c, x[ 5], S22); /* 22 */ + GG (c, d, a, b, x[ 9], S23); /* 23 */ + GG (b, c, d, a, x[13], S24); /* 24 */ + GG (a, b, c, d, x[ 2], S21); /* 25 */ + GG (d, a, b, c, x[ 6], S22); /* 26 */ + GG (c, d, a, b, x[10], S23); /* 27 */ + GG (b, c, d, a, x[14], S24); /* 28 */ + GG (a, b, c, d, x[ 3], S21); /* 29 */ + GG (d, a, b, c, x[ 7], S22); /* 30 */ + GG (c, d, a, b, x[11], S23); /* 31 */ + GG (b, c, d, a, x[15], S24); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 0], S31); /* 33 */ + HH (d, a, b, c, x[ 8], S32); /* 34 */ + HH (c, d, a, b, x[ 4], S33); /* 35 */ + HH (b, c, d, a, x[12], S34); /* 36 */ + HH (a, b, c, d, x[ 2], S31); /* 37 */ + HH (d, a, b, c, x[10], S32); /* 38 */ + HH (c, d, a, b, x[ 6], S33); /* 39 */ + HH (b, c, d, a, x[14], S34); /* 40 */ + HH (a, b, c, d, x[ 1], S31); /* 41 */ + HH (d, a, b, c, x[ 9], S32); /* 42 */ + HH (c, d, a, b, x[ 5], S33); /* 43 */ + HH (b, c, d, a, x[13], S34); /* 44 */ + HH (a, b, c, d, x[ 3], S31); /* 45 */ + HH (d, a, b, c, x[11], S32); /* 46 */ + HH (c, d, a, b, x[ 7], S33); /* 47 */ + HH (b, c, d, a, x[15], S34); /* 48 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. */ + memset(x, 0, sizeof (x)); +} + +/* + * Encodes input (UINT4) into output (unsigned char). Assumes len is + * a multiple of 4. + */ +static void +Encode(unsigned char *output, UINT4 *input, unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + +/* + * Decodes input (unsigned char) into output (UINT4). Assumes len is + * a multiple of 4. + */ +static void +Decode(UINT4 *output, const unsigned char *input, unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); +} + +#endif /* HAVE_MD4_H */ diff --git a/common/lib/libc/md/md5c.c b/common/lib/libc/md/md5c.c new file mode 100644 index 000000000..e15548dd7 --- /dev/null +++ b/common/lib/libc/md/md5c.c @@ -0,0 +1,357 @@ +/* $NetBSD: md5c.c,v 1.4 2009/03/16 05:59:21 cegger Exp $ */ + +/* + * This file is derived from the RSA Data Security, Inc. MD5 Message-Digest + * Algorithm and has been modified by Jason R. Thorpe + * for portability and formatting. + */ + +/* + * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + * rights reserved. + * + * License to copy and use this software is granted provided that it + * is identified as the "RSA Data Security, Inc. MD5 Message-Digest + * Algorithm" in all material mentioning or referencing this software + * or this function. + * + * License is also granted to make and use derivative works provided + * that such works are identified as "derived from the RSA Data + * Security, Inc. MD5 Message-Digest Algorithm" in all material + * mentioning or referencing the derived work. + * + * RSA Data Security, Inc. makes no representations concerning either + * the merchantability of this software or the suitability of this + * software for any particular purpose. It is provided "as is" + * without express or implied warranty of any kind. + * + * These notices must be retained in any copies of any part of this + * documentation and/or software. + */ + +#if defined(_KERNEL) || defined(_STANDALONE) +#include +#include +#include +#else +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: md5c.c,v 1.4 2009/03/16 05:59:21 cegger Exp $"); +#endif /* LIBC_SCCS and not lint */ +#include "namespace.h" +#include +#include +#include +#include +#endif /* _KERNEL || _STANDALONE */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#if !HAVE_MD5_H + +#define ZEROIZE(d, l) memset((d), 0, (l)) + +typedef unsigned char *POINTER; +typedef uint16_t UINT2; +typedef uint32_t UINT4; + +/* + * Constants for MD5Transform routine. + */ +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + +#if !defined(_KERNEL) && !defined(_STANDALONE) && defined(__weak_alias) +__weak_alias(MD5Init,_MD5Init) +__weak_alias(MD5Update,_MD5Update) +__weak_alias(MD5Final,_MD5Final) +__weak_alias(MD5Transform,_MD5Transform) +#endif + +static void MD5Transform __P((UINT4 [4], const unsigned char [64])); + +static void Encode __P((unsigned char *, UINT4 *, unsigned int)); +static void Decode __P((UINT4 *, const unsigned char *, unsigned int)); + +/* + * Encodes input (UINT4) into output (unsigned char). Assumes len is + * a multiple of 4. + */ +static void +Encode (unsigned char *output, + UINT4 *input, + unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + +/* + * Decodes input (unsigned char) into output (UINT4). Assumes len is + * a multiple of 4. + */ +static void +Decode (UINT4 *output, + const unsigned char *input, + unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); +} + +static const unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* + * F, G, H and I are basic MD5 functions. + */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + +/* + * ROTATE_LEFT rotates x left n bits. + */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* + * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. + * Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +/* + * MD5 initialization. Begins an MD5 operation, writing a new context. + */ +void +MD5Init(MD5_CTX *context) +{ + + _DIAGASSERT(context != 0); + + context->count[0] = context->count[1] = 0; + + /* Load magic initialization constants. */ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* + * MD5 block update operation. Continues an MD5 message-digest + * operation, processing another message block, and updating the + * context. + */ +void +MD5Update(MD5_CTX *context, + const unsigned char *input, /* input block */ + unsigned int inputLen) /* length of input block */ +{ + unsigned int i, idx, partLen; + + _DIAGASSERT(context != 0); + _DIAGASSERT(input != 0); + + /* Compute number of bytes mod 64 */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - idx; + + /* Transform as many times as possible. */ + if (inputLen >= partLen) { + memcpy((POINTER)&context->buffer[idx], input, partLen); + MD5Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform(context->state, &input[i]); + + idx = 0; + } else + i = 0; + + /* Buffer remaining input */ + memcpy(&context->buffer[idx], &input[i], inputLen - i); +} + +/* + * MD5 finalization. Ends an MD5 message-digest operation, writing the + * message digest and zeroing the context. + */ +void +MD5Final(unsigned char digest[16], /* message digest */ + MD5_CTX *context) /* context */ +{ + unsigned char bits[8]; + unsigned int idx, padLen; + + _DIAGASSERT(digest != 0); + _DIAGASSERT(context != 0); + + /* Save number of bits */ + Encode(bits, context->count, 8); + + /* Pad out to 56 mod 64. */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); + MD5Update (context, PADDING, padLen); + + /* Append length (before padding) */ + MD5Update(context, bits, 8); + + /* Store state in digest */ + Encode(digest, context->state, 16); + + /* Zeroize sensitive information. */ + ZEROIZE((POINTER)(void *)context, sizeof(*context)); +} + +/* + * MD5 basic transformation. Transforms state based on block. + */ +static void +MD5Transform(UINT4 state[4], const unsigned char block[64]) +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode(x, block, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. */ + ZEROIZE((POINTER)(void *)x, sizeof (x)); +} + +#endif /* HAVE_MD5_H */ diff --git a/common/lib/libc/net/__cmsg_alignbytes.c b/common/lib/libc/net/__cmsg_alignbytes.c new file mode 100644 index 000000000..726ff566e --- /dev/null +++ b/common/lib/libc/net/__cmsg_alignbytes.c @@ -0,0 +1,74 @@ +/* $NetBSD: __cmsg_alignbytes.c,v 1.3 2009/03/16 05:59:21 cegger Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jun-ichiro Hagino. + * + * 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. + */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __cmsg_alignbytes.c,v 1.3 2009/03/16 05:59:21 cegger Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#else +#include +#include +#include +#endif + +int +__cmsg_alignbytes(void) +{ + static int alignbytes = -1; +#ifdef HW_ALIGNBYTES + int mib[2]; + size_t len; + int ret; +#endif + + if (alignbytes > 0) + return alignbytes; + +#ifdef HW_ALIGNBYTES + mib[0] = CTL_HW; + mib[1] = HW_ALIGNBYTES; + len = sizeof(alignbytes); + ret = sysctl(mib, (u_int) (sizeof(mib) / sizeof(mib[0])), + (void *)&alignbytes, &len, NULL, (size_t)0); + if (ret >= 0 && alignbytes >= 0) + return alignbytes; +#endif + /* last resort */ + alignbytes = ALIGNBYTES; + return alignbytes; +} diff --git a/common/lib/libc/net/htonl.c b/common/lib/libc/net/htonl.c new file mode 100644 index 000000000..7edc4f8bb --- /dev/null +++ b/common/lib/libc/net/htonl.c @@ -0,0 +1,27 @@ +/* $NetBSD: htonl.c,v 1.1 2005/12/20 19:28:51 christos Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: htonl.c,v 1.1 2005/12/20 19:28:51 christos Exp $"); +#endif + +#include + +#undef htonl + +uint32_t +htonl(x) + uint32_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *)&x; + return (uint32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); +#else + return x; +#endif +} diff --git a/common/lib/libc/net/htons.c b/common/lib/libc/net/htons.c new file mode 100644 index 000000000..830db0f1b --- /dev/null +++ b/common/lib/libc/net/htons.c @@ -0,0 +1,27 @@ +/* $NetBSD: htons.c,v 1.1 2005/12/20 19:28:51 christos Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: htons.c,v 1.1 2005/12/20 19:28:51 christos Exp $"); +#endif + +#include + +#undef htons + +uint16_t +htons(x) + uint16_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *) &x; + return (uint16_t)(s[0] << 8 | s[1]); +#else + return x; +#endif +} diff --git a/common/lib/libc/net/ntohl.c b/common/lib/libc/net/ntohl.c new file mode 100644 index 000000000..dc0ca9a8b --- /dev/null +++ b/common/lib/libc/net/ntohl.c @@ -0,0 +1,27 @@ +/* $NetBSD: ntohl.c,v 1.1 2005/12/20 19:28:51 christos Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ntohl.c,v 1.1 2005/12/20 19:28:51 christos Exp $"); +#endif + +#include + +#undef ntohl + +uint32_t +ntohl(x) + uint32_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *)&x; + return (uint32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); +#else + return x; +#endif +} diff --git a/common/lib/libc/net/ntohs.c b/common/lib/libc/net/ntohs.c new file mode 100644 index 000000000..eb4e84147 --- /dev/null +++ b/common/lib/libc/net/ntohs.c @@ -0,0 +1,27 @@ +/* $NetBSD: ntohs.c,v 1.1 2005/12/20 19:28:51 christos Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ntohs.c,v 1.1 2005/12/20 19:28:51 christos Exp $"); +#endif + +#include + +#undef ntohs + +uint16_t +ntohs(x) + uint16_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *) &x; + return (uint16_t)(s[0] << 8 | s[1]); +#else + return x; +#endif +} diff --git a/common/lib/libc/quad/adddi3.c b/common/lib/libc/quad/adddi3.c new file mode 100644 index 000000000..81eaede17 --- /dev/null +++ b/common/lib/libc/quad/adddi3.c @@ -0,0 +1,62 @@ +/* $NetBSD: adddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)adddi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: adddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Add two quads. This is trivial since a one-bit carry from a single + * u_int addition x+y occurs if and only if the sum x+y is less than + * either x or y (the choice to compare with x or y is arbitrary). + */ +quad_t +__adddi3(quad_t a, quad_t b) +{ + union uu aa, bb, sum; + + aa.q = a; + bb.q = b; + sum.ul[L] = aa.ul[L] + bb.ul[L]; + sum.ul[H] = aa.ul[H] + bb.ul[H] + (sum.ul[L] < bb.ul[L]); + return (sum.q); +} diff --git a/common/lib/libc/quad/anddi3.c b/common/lib/libc/quad/anddi3.c new file mode 100644 index 000000000..4a5605afb --- /dev/null +++ b/common/lib/libc/quad/anddi3.c @@ -0,0 +1,60 @@ +/* $NetBSD: anddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)anddi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: anddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return a & b, in quad. + */ +quad_t +__anddi3(quad_t a, quad_t b) +{ + union uu aa, bb; + + aa.q = a; + bb.q = b; + aa.ul[0] &= bb.ul[0]; + aa.ul[1] &= bb.ul[1]; + return (aa.q); +} diff --git a/common/lib/libc/quad/ashldi3.c b/common/lib/libc/quad/ashldi3.c new file mode 100644 index 000000000..d242614e9 --- /dev/null +++ b/common/lib/libc/quad/ashldi3.c @@ -0,0 +1,68 @@ +/* $NetBSD: ashldi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ashldi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: ashldi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Shift a (signed) quad value left (arithmetic shift left). + * This is the same as logical shift left! + */ +quad_t +__ashldi3(quad_t a, qshift_t shift) +{ + union uu aa; + + if (shift == 0) + return(a); + aa.q = a; + if (shift >= INT_BITS) { + aa.ul[H] = aa.ul[L] << (shift - INT_BITS); + aa.ul[L] = 0; + } else { + aa.ul[H] = (aa.ul[H] << shift) | + (aa.ul[L] >> (INT_BITS - shift)); + aa.ul[L] <<= shift; + } + return (aa.q); +} diff --git a/common/lib/libc/quad/ashrdi3.c b/common/lib/libc/quad/ashrdi3.c new file mode 100644 index 000000000..c064b57d5 --- /dev/null +++ b/common/lib/libc/quad/ashrdi3.c @@ -0,0 +1,80 @@ +/* $NetBSD: ashrdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ashrdi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: ashrdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Shift a (signed) quad value right (arithmetic shift right). + */ +quad_t +__ashrdi3(quad_t a, qshift_t shift) +{ + union uu aa; + + if (shift == 0) + return(a); + aa.q = a; + if (shift >= INT_BITS) { + int s; + + /* + * Smear bits rightward using the machine's right-shift + * method, whether that is sign extension or zero fill, + * to get the `sign word' s. Note that shifting by + * INT_BITS is undefined, so we shift (INT_BITS-1), + * then 1 more, to get our answer. + */ + /* LINTED inherits machine dependency */ + s = (aa.sl[H] >> (INT_BITS - 1)) >> 1; + /* LINTED inherits machine dependency*/ + aa.ul[L] = aa.sl[H] >> (shift - INT_BITS); + aa.ul[H] = s; + } else { + aa.ul[L] = (aa.ul[L] >> shift) | + (aa.ul[H] << (INT_BITS - shift)); + /* LINTED inherits machine dependency */ + aa.sl[H] >>= shift; + } + return (aa.q); +} diff --git a/common/lib/libc/quad/cmpdi2.c b/common/lib/libc/quad/cmpdi2.c new file mode 100644 index 000000000..e1b3c4268 --- /dev/null +++ b/common/lib/libc/quad/cmpdi2.c @@ -0,0 +1,61 @@ +/* $NetBSD: cmpdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)cmpdi2.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: cmpdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return 0, 1, or 2 as a <, =, > b respectively. + * Both a and b are considered signed---which means only the high word is + * signed. + */ +int +__cmpdi2(quad_t a, quad_t b) +{ + union uu aa, bb; + + aa.q = a; + bb.q = b; + return (aa.sl[H] < bb.sl[H] ? 0 : aa.sl[H] > bb.sl[H] ? 2 : + aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1); +} diff --git a/common/lib/libc/quad/divdi3.c b/common/lib/libc/quad/divdi3.c new file mode 100644 index 000000000..b241ed1d4 --- /dev/null +++ b/common/lib/libc/quad/divdi3.c @@ -0,0 +1,69 @@ +/* $NetBSD: divdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)divdi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: divdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Divide two signed quads. + * ??? if -1/2 should produce -1 on this machine, this code is wrong + */ +quad_t +__divdi3(quad_t a, quad_t b) +{ + u_quad_t ua, ub, uq; + int neg = 0; + + ua = a; + ub = b; + + if (a < 0) + ua = -ua, neg ^= 1; + if (b < 0) + ub = -ub, neg ^= 1; + + uq = __qdivrem(ua, ub, (u_quad_t *)0); + if (neg) + uq = - uq; + return uq; +} diff --git a/common/lib/libc/quad/iordi3.c b/common/lib/libc/quad/iordi3.c new file mode 100644 index 000000000..300bdca21 --- /dev/null +++ b/common/lib/libc/quad/iordi3.c @@ -0,0 +1,60 @@ +/* $NetBSD: iordi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)iordi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: iordi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return a | b, in quad. + */ +quad_t +__iordi3(quad_t a, quad_t b) +{ + union uu aa, bb; + + aa.q = a; + bb.q = b; + aa.ul[0] |= bb.ul[0]; + aa.ul[1] |= bb.ul[1]; + return (aa.q); +} diff --git a/common/lib/libc/quad/lshldi3.c b/common/lib/libc/quad/lshldi3.c new file mode 100644 index 000000000..652552488 --- /dev/null +++ b/common/lib/libc/quad/lshldi3.c @@ -0,0 +1,68 @@ +/* $NetBSD: lshldi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)lshldi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: lshldi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Shift an (unsigned) quad value left (logical shift left). + * This is the same as arithmetic shift left! + */ +quad_t +__lshldi3(quad_t a, qshift_t shift) +{ + union uu aa; + + if (shift == 0) + return(a); + aa.q = a; + if (shift >= INT_BITS) { + aa.ul[H] = aa.ul[L] << (shift - INT_BITS); + aa.ul[L] = 0; + } else { + aa.ul[H] = (aa.ul[H] << shift) | + (aa.ul[L] >> (INT_BITS - shift)); + aa.ul[L] <<= shift; + } + return (aa.q); +} diff --git a/common/lib/libc/quad/lshrdi3.c b/common/lib/libc/quad/lshrdi3.c new file mode 100644 index 000000000..80a4b6d76 --- /dev/null +++ b/common/lib/libc/quad/lshrdi3.c @@ -0,0 +1,67 @@ +/* $NetBSD: lshrdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)lshrdi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: lshrdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Shift an (unsigned) quad value right (logical shift right). + */ +quad_t +__lshrdi3(quad_t a, qshift_t shift) +{ + union uu aa; + + if (shift == 0) + return(a); + aa.q = a; + if (shift >= INT_BITS) { + aa.ul[L] = aa.ul[H] >> (shift - INT_BITS); + aa.ul[H] = 0; + } else { + aa.ul[L] = (aa.ul[L] >> shift) | + (aa.ul[H] << (INT_BITS - shift)); + aa.ul[H] >>= shift; + } + return (aa.q); +} diff --git a/common/lib/libc/quad/moddi3.c b/common/lib/libc/quad/moddi3.c new file mode 100644 index 000000000..74333b90e --- /dev/null +++ b/common/lib/libc/quad/moddi3.c @@ -0,0 +1,69 @@ +/* $NetBSD: moddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)moddi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: moddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return remainder after dividing two signed quads. + * + * XXX we assume a % b < 0 iff a < 0, but this is actually machine-dependent. + */ +quad_t +__moddi3(quad_t a, quad_t b) +{ + u_quad_t ua, ub, ur; + int neg = 0; + + ua = a; + ub = b; + + if (a < 0) + ua = -ua, neg ^= 1; + if (b < 0) + ub = -ub; + (void)__qdivrem(ua, ub, &ur); + if (neg) + ur = -ur; + return (ur); +} diff --git a/common/lib/libc/quad/muldi3.c b/common/lib/libc/quad/muldi3.c new file mode 100644 index 000000000..bf3b5bdef --- /dev/null +++ b/common/lib/libc/quad/muldi3.c @@ -0,0 +1,248 @@ +/* $NetBSD: muldi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)muldi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: muldi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Multiply two quads. + * + * Our algorithm is based on the following. Split incoming quad values + * u and v (where u,v >= 0) into + * + * u = 2^n u1 * u0 (n = number of bits in `u_int', usu. 32) + * + * and + * + * v = 2^n v1 * v0 + * + * Then + * + * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0 + * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0 + * + * Now add 2^n u1 v1 to the first term and subtract it from the middle, + * and add 2^n u0 v0 to the last term and subtract it from the middle. + * This gives: + * + * uv = (2^2n + 2^n) (u1 v1) + + * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) + + * (2^n + 1) (u0 v0) + * + * Factoring the middle a bit gives us: + * + * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high] + * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid] + * (2^n + 1) (u0 v0) [u0v0 = low] + * + * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done + * in just half the precision of the original. (Note that either or both + * of (u1 - u0) or (v0 - v1) may be negative.) + * + * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278. + * + * Since C does not give us a `int * int = quad' operator, we split + * our input quads into two ints, then split the two ints into two + * shorts. We can then calculate `short * short = int' in native + * arithmetic. + * + * Our product should, strictly speaking, be a `long quad', with 128 + * bits, but we are going to discard the upper 64. In other words, + * we are not interested in uv, but rather in (uv mod 2^2n). This + * makes some of the terms above vanish, and we get: + * + * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low) + * + * or + * + * (2^n)(high + mid + low) + low + * + * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor + * of 2^n in either one will also vanish. Only `low' need be computed + * mod 2^2n, and only because of the final term above. + */ +static quad_t __lmulq(u_int, u_int); + +quad_t +__muldi3(quad_t a, quad_t b) +{ + union uu u, v, low, prod; + u_int high, mid, udiff, vdiff; + int negall, negmid; +#define u1 u.ul[H] +#define u0 u.ul[L] +#define v1 v.ul[H] +#define v0 v.ul[L] + + /* + * Get u and v such that u, v >= 0. When this is finished, + * u1, u0, v1, and v0 will be directly accessible through the + * int fields. + */ + if (a >= 0) + u.q = a, negall = 0; + else + u.q = -a, negall = 1; + if (b >= 0) + v.q = b; + else + v.q = -b, negall ^= 1; + + if (u1 == 0 && v1 == 0) { + /* + * An (I hope) important optimization occurs when u1 and v1 + * are both 0. This should be common since most numbers + * are small. Here the product is just u0*v0. + */ + prod.q = __lmulq(u0, v0); + } else { + /* + * Compute the three intermediate products, remembering + * whether the middle term is negative. We can discard + * any upper bits in high and mid, so we can use native + * u_int * u_int => u_int arithmetic. + */ + low.q = __lmulq(u0, v0); + + if (u1 >= u0) + negmid = 0, udiff = u1 - u0; + else + negmid = 1, udiff = u0 - u1; + if (v0 >= v1) + vdiff = v0 - v1; + else + vdiff = v1 - v0, negmid ^= 1; + mid = udiff * vdiff; + + high = u1 * v1; + + /* + * Assemble the final product. + */ + prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] + + low.ul[H]; + prod.ul[L] = low.ul[L]; + } + return (negall ? -prod.q : prod.q); +#undef u1 +#undef u0 +#undef v1 +#undef v0 +} + +/* + * Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half + * the number of bits in an int (whatever that is---the code below + * does not care as long as quad.h does its part of the bargain---but + * typically N==16). + * + * We use the same algorithm from Knuth, but this time the modulo refinement + * does not apply. On the other hand, since N is half the size of an int, + * we can get away with native multiplication---none of our input terms + * exceeds (UINT_MAX >> 1). + * + * Note that, for u_int l, the quad-precision result + * + * l << N + * + * splits into high and low ints as HHALF(l) and LHUP(l) respectively. + */ +static quad_t +__lmulq(u_int u, u_int v) +{ + u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low; + u_int prodh, prodl, was; + union uu prod; + int neg; + + u1 = HHALF(u); + u0 = LHALF(u); + v1 = HHALF(v); + v0 = LHALF(v); + + low = u0 * v0; + + /* This is the same small-number optimization as before. */ + if (u1 == 0 && v1 == 0) + return (low); + + if (u1 >= u0) + udiff = u1 - u0, neg = 0; + else + udiff = u0 - u1, neg = 1; + if (v0 >= v1) + vdiff = v0 - v1; + else + vdiff = v1 - v0, neg ^= 1; + mid = udiff * vdiff; + + high = u1 * v1; + + /* prod = (high << 2N) + (high << N); */ + prodh = high + HHALF(high); + prodl = LHUP(high); + + /* if (neg) prod -= mid << N; else prod += mid << N; */ + if (neg) { + was = prodl; + prodl -= LHUP(mid); + prodh -= HHALF(mid) + (prodl > was); + } else { + was = prodl; + prodl += LHUP(mid); + prodh += HHALF(mid) + (prodl < was); + } + + /* prod += low << N */ + was = prodl; + prodl += LHUP(low); + prodh += HHALF(low) + (prodl < was); + /* ... + low; */ + if ((prodl += low) < low) + prodh++; + + /* return 4N-bit product */ + prod.ul[H] = prodh; + prod.ul[L] = prodl; + return (prod.q); +} diff --git a/common/lib/libc/quad/negdi2.c b/common/lib/libc/quad/negdi2.c new file mode 100644 index 000000000..000889a9f --- /dev/null +++ b/common/lib/libc/quad/negdi2.c @@ -0,0 +1,59 @@ +/* $NetBSD: negdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)negdi2.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: negdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return -a (or, equivalently, 0 - a), in quad. See subdi3.c. + */ +quad_t +__negdi2(quad_t a) +{ + union uu aa, res; + + aa.q = a; + res.ul[L] = -aa.ul[L]; + res.ul[H] = -aa.ul[H] - (res.ul[L] > 0); + return (res.q); +} diff --git a/common/lib/libc/quad/notdi2.c b/common/lib/libc/quad/notdi2.c new file mode 100644 index 000000000..1706b6d46 --- /dev/null +++ b/common/lib/libc/quad/notdi2.c @@ -0,0 +1,60 @@ +/* $NetBSD: notdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)notdi2.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: notdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return ~a. For some reason gcc calls this `one's complement' rather + * than `not'. + */ +quad_t +__one_cmpldi2(quad_t a) +{ + union uu aa; + + aa.q = a; + aa.ul[0] = ~aa.ul[0]; + aa.ul[1] = ~aa.ul[1]; + return (aa.q); +} diff --git a/common/lib/libc/quad/qdivrem.c b/common/lib/libc/quad/qdivrem.c new file mode 100644 index 000000000..84b77812a --- /dev/null +++ b/common/lib/libc/quad/qdivrem.c @@ -0,0 +1,284 @@ +/* $NetBSD: qdivrem.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)qdivrem.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: qdivrem.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed), + * section 4.3.1, pp. 257--259. + */ + +#include "quad.h" + +#define B ((int)1 << HALF_BITS) /* digit base */ + +/* Combine two `digits' to make a single two-digit number. */ +#define COMBINE(a, b) (((u_int)(a) << HALF_BITS) | (b)) + +/* select a type for digits in base B: use unsigned short if they fit */ +#if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff +typedef unsigned short digit; +#else +typedef u_int digit; +#endif + +static void shl __P((digit *p, int len, int sh)); + +/* + * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v. + * + * We do this in base 2-sup-HALF_BITS, so that all intermediate products + * fit within u_int. As a consequence, the maximum length dividend and + * divisor are 4 `digits' in this base (they are shorter if they have + * leading zeros). + */ +u_quad_t +__qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq) +{ + union uu tmp; + digit *u, *v, *q; + digit v1, v2; + u_int qhat, rhat, t; + int m, n, d, j, i; + digit uspace[5], vspace[5], qspace[5]; + + /* + * Take care of special cases: divide by zero, and u < v. + */ + if (vq == 0) { + /* divide by zero. */ + static volatile const unsigned int zero = 0; + + tmp.ul[H] = tmp.ul[L] = 1 / zero; + if (arq) + *arq = uq; + return (tmp.q); + } + if (uq < vq) { + if (arq) + *arq = uq; + return (0); + } + u = &uspace[0]; + v = &vspace[0]; + q = &qspace[0]; + + /* + * Break dividend and divisor into digits in base B, then + * count leading zeros to determine m and n. When done, we + * will have: + * u = (u[1]u[2]...u[m+n]) sub B + * v = (v[1]v[2]...v[n]) sub B + * v[1] != 0 + * 1 < n <= 4 (if n = 1, we use a different division algorithm) + * m >= 0 (otherwise u < v, which we already checked) + * m + n = 4 + * and thus + * m = 4 - n <= 2 + */ + tmp.uq = uq; + u[0] = 0; + u[1] = (digit)HHALF(tmp.ul[H]); + u[2] = (digit)LHALF(tmp.ul[H]); + u[3] = (digit)HHALF(tmp.ul[L]); + u[4] = (digit)LHALF(tmp.ul[L]); + tmp.uq = vq; + v[1] = (digit)HHALF(tmp.ul[H]); + v[2] = (digit)LHALF(tmp.ul[H]); + v[3] = (digit)HHALF(tmp.ul[L]); + v[4] = (digit)LHALF(tmp.ul[L]); + for (n = 4; v[1] == 0; v++) { + if (--n == 1) { + u_int rbj; /* r*B+u[j] (not root boy jim) */ + digit q1, q2, q3, q4; + + /* + * Change of plan, per exercise 16. + * r = 0; + * for j = 1..4: + * q[j] = floor((r*B + u[j]) / v), + * r = (r*B + u[j]) % v; + * We unroll this completely here. + */ + t = v[2]; /* nonzero, by definition */ + q1 = (digit)(u[1] / t); + rbj = COMBINE(u[1] % t, u[2]); + q2 = (digit)(rbj / t); + rbj = COMBINE(rbj % t, u[3]); + q3 = (digit)(rbj / t); + rbj = COMBINE(rbj % t, u[4]); + q4 = (digit)(rbj / t); + if (arq) + *arq = rbj % t; + tmp.ul[H] = COMBINE(q1, q2); + tmp.ul[L] = COMBINE(q3, q4); + return (tmp.q); + } + } + + /* + * By adjusting q once we determine m, we can guarantee that + * there is a complete four-digit quotient at &qspace[1] when + * we finally stop. + */ + for (m = 4 - n; u[1] == 0; u++) + m--; + for (i = 4 - m; --i >= 0;) + q[i] = 0; + q += 4 - m; + + /* + * Here we run Program D, translated from MIX to C and acquiring + * a few minor changes. + * + * D1: choose multiplier 1 << d to ensure v[1] >= B/2. + */ + d = 0; + for (t = v[1]; t < B / 2; t <<= 1) + d++; + if (d > 0) { + shl(&u[0], m + n, d); /* u <<= d */ + shl(&v[1], n - 1, d); /* v <<= d */ + } + /* + * D2: j = 0. + */ + j = 0; + v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ + v2 = v[2]; /* for D3 */ + do { + digit uj0, uj1, uj2; + + /* + * D3: Calculate qhat (\^q, in TeX notation). + * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and + * let rhat = (u[j]*B + u[j+1]) mod v[1]. + * While rhat < B and v[2]*qhat > rhat*B+u[j+2], + * decrement qhat and increase rhat correspondingly. + * Note that if rhat >= B, v[2]*qhat < rhat*B. + */ + uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ + uj1 = u[j + 1]; /* for D3 only */ + uj2 = u[j + 2]; /* for D3 only */ + if (uj0 == v1) { + qhat = B; + rhat = uj1; + goto qhat_too_big; + } else { + u_int nn = COMBINE(uj0, uj1); + qhat = nn / v1; + rhat = nn % v1; + } + while (v2 * qhat > COMBINE(rhat, uj2)) { + qhat_too_big: + qhat--; + if ((rhat += v1) >= B) + break; + } + /* + * D4: Multiply and subtract. + * The variable `t' holds any borrows across the loop. + * We split this up so that we do not require v[0] = 0, + * and to eliminate a final special case. + */ + for (t = 0, i = n; i > 0; i--) { + t = u[i + j] - v[i] * qhat - t; + u[i + j] = (digit)LHALF(t); + t = (B - HHALF(t)) & (B - 1); + } + t = u[j] - t; + u[j] = (digit)LHALF(t); + /* + * D5: test remainder. + * There is a borrow if and only if HHALF(t) is nonzero; + * in that (rare) case, qhat was too large (by exactly 1). + * Fix it by adding v[1..n] to u[j..j+n]. + */ + if (HHALF(t)) { + qhat--; + for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ + t += u[i + j] + v[i]; + u[i + j] = (digit)LHALF(t); + t = HHALF(t); + } + u[j] = (digit)LHALF(u[j] + t); + } + q[j] = (digit)qhat; + } while (++j <= m); /* D7: loop on j. */ + + /* + * If caller wants the remainder, we have to calculate it as + * u[m..m+n] >> d (this is at most n digits and thus fits in + * u[m+1..m+n], but we may need more source digits). + */ + if (arq) { + if (d) { + for (i = m + n; i > m; --i) + u[i] = (digit)(((u_int)u[i] >> d) | + LHALF((u_int)u[i - 1] << (HALF_BITS - d))); + u[i] = 0; + } + tmp.ul[H] = COMBINE(uspace[1], uspace[2]); + tmp.ul[L] = COMBINE(uspace[3], uspace[4]); + *arq = tmp.q; + } + + tmp.ul[H] = COMBINE(qspace[1], qspace[2]); + tmp.ul[L] = COMBINE(qspace[3], qspace[4]); + return (tmp.q); +} + +/* + * Shift p[0]..p[len] left `sh' bits, ignoring any bits that + * `fall out' the left (there never will be any such anyway). + * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. + */ +static void +shl(digit *p, int len, int sh) +{ + int i; + + for (i = 0; i < len; i++) + p[i] = (digit)(LHALF((u_int)p[i] << sh) | + ((u_int)p[i + 1] >> (HALF_BITS - sh))); + p[i] = (digit)(LHALF((u_int)p[i] << sh)); +} diff --git a/common/lib/libc/quad/quad.h b/common/lib/libc/quad/quad.h new file mode 100644 index 000000000..c5096405a --- /dev/null +++ b/common/lib/libc/quad/quad.h @@ -0,0 +1,139 @@ +/* $NetBSD: quad.h,v 1.1 2005/12/20 20:29:40 christos Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * @(#)quad.h 8.1 (Berkeley) 6/4/93 + */ + +/* + * Quad arithmetic. + * + * This library makes the following assumptions: + * + * - The type long long (aka quad_t) exists. + * + * - A quad variable is exactly twice as long as `int'. + * + * - The machine's arithmetic is two's complement. + * + * This library can provide 128-bit arithmetic on a machine with 128-bit + * quads and 64-bit ints, for instance, or 96-bit arithmetic on machines + * with 48-bit ints. + */ + +#include +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#else +#include +#endif + +/* + * Depending on the desired operation, we view a `long long' (aka quad_t) in + * one or more of the following formats. + */ +union uu { + quad_t q; /* as a (signed) quad */ + u_quad_t uq; /* as an unsigned quad */ + int sl[2]; /* as two signed ints */ + u_int ul[2]; /* as two unsigned ints */ +}; + +/* + * Define high and low parts of a quad_t. + */ +#define H _QUAD_HIGHWORD +#define L _QUAD_LOWWORD + +/* + * Total number of bits in a quad_t and in the pieces that make it up. + * These are used for shifting, and also below for halfword extraction + * and assembly. + */ +#define QUAD_BITS (sizeof(quad_t) * CHAR_BIT) +#define INT_BITS (sizeof(int) * CHAR_BIT) +#define HALF_BITS (sizeof(int) * CHAR_BIT / 2) + +/* + * Extract high and low shortwords from longword, and move low shortword of + * longword to upper half of long, i.e., produce the upper longword of + * ((quad_t)(x) << (number_of_bits_in_int/2)). (`x' must actually be u_int.) + * + * These are used in the multiply code, to split a longword into upper + * and lower halves, and to reassemble a product as a quad_t, shifted left + * (sizeof(int)*CHAR_BIT/2). + */ +#define HHALF(x) ((u_int)(x) >> HALF_BITS) +#define LHALF(x) ((u_int)(x) & (((int)1 << HALF_BITS) - 1)) +#define LHUP(x) ((u_int)(x) << HALF_BITS) + +/* + * XXX + * Compensate for gcc 1 vs gcc 2. Gcc 1 defines ?sh?di3's second argument + * as u_quad_t, while gcc 2 correctly uses int. Unfortunately, we still use + * both compilers. + */ +#if __GNUC_PREREQ__(2, 0) || defined(lint) +typedef unsigned int qshift_t; +#else +typedef u_quad_t qshift_t; +#endif + +__BEGIN_DECLS +quad_t __adddi3 __P((quad_t, quad_t)); +quad_t __anddi3 __P((quad_t, quad_t)); +quad_t __ashldi3 __P((quad_t, qshift_t)); +quad_t __ashrdi3 __P((quad_t, qshift_t)); +int __cmpdi2 __P((quad_t, quad_t)); +quad_t __divdi3 __P((quad_t, quad_t)); +quad_t __fixdfdi __P((double)); +quad_t __fixsfdi __P((float)); +u_quad_t __fixunsdfdi __P((double)); +u_quad_t __fixunssfdi __P((float)); +double __floatdidf __P((quad_t)); +float __floatdisf __P((quad_t)); +double __floatunsdidf __P((u_quad_t)); +quad_t __iordi3 __P((quad_t, quad_t)); +quad_t __lshldi3 __P((quad_t, qshift_t)); +quad_t __lshrdi3 __P((quad_t, qshift_t)); +quad_t __moddi3 __P((quad_t, quad_t)); +quad_t __muldi3 __P((quad_t, quad_t)); +quad_t __negdi2 __P((quad_t)); +quad_t __one_cmpldi2 __P((quad_t)); +u_quad_t __qdivrem __P((u_quad_t, u_quad_t, u_quad_t *)); +quad_t __subdi3 __P((quad_t, quad_t)); +int __ucmpdi2 __P((u_quad_t, u_quad_t)); +u_quad_t __udivdi3 __P((u_quad_t, u_quad_t )); +u_quad_t __umoddi3 __P((u_quad_t, u_quad_t )); +quad_t __xordi3 __P((quad_t, quad_t)); +__END_DECLS diff --git a/common/lib/libc/quad/subdi3.c b/common/lib/libc/quad/subdi3.c new file mode 100644 index 000000000..b84ef05f9 --- /dev/null +++ b/common/lib/libc/quad/subdi3.c @@ -0,0 +1,61 @@ +/* $NetBSD: subdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)subdi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: subdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Subtract two quad values. This is trivial since a one-bit carry + * from a single u_int difference x-y occurs if and only if (x-y) > x. + */ +quad_t +__subdi3(quad_t a, quad_t b) +{ + union uu aa, bb, diff; + + aa.q = a; + bb.q = b; + diff.ul[L] = aa.ul[L] - bb.ul[L]; + diff.ul[H] = aa.ul[H] - bb.ul[H] - (diff.ul[L] > aa.ul[L]); + return (diff.q); +} diff --git a/common/lib/libc/quad/ucmpdi2.c b/common/lib/libc/quad/ucmpdi2.c new file mode 100644 index 000000000..04e713314 --- /dev/null +++ b/common/lib/libc/quad/ucmpdi2.c @@ -0,0 +1,60 @@ +/* $NetBSD: ucmpdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ucmpdi2.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: ucmpdi2.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return 0, 1, or 2 as a <, =, > b respectively. + * Neither a nor b are considered signed. + */ +int +__ucmpdi2(u_quad_t a, u_quad_t b) +{ + union uu aa, bb; + + aa.uq = a; + bb.uq = b; + return (aa.ul[H] < bb.ul[H] ? 0 : aa.ul[H] > bb.ul[H] ? 2 : + aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1); +} diff --git a/common/lib/libc/quad/udivdi3.c b/common/lib/libc/quad/udivdi3.c new file mode 100644 index 000000000..72a95c746 --- /dev/null +++ b/common/lib/libc/quad/udivdi3.c @@ -0,0 +1,55 @@ +/* $NetBSD: udivdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)udivdi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: udivdi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Divide two unsigned quads. + */ +u_quad_t +__udivdi3(u_quad_t a, u_quad_t b) +{ + + return (__qdivrem(a, b, (u_quad_t *)0)); +} diff --git a/common/lib/libc/quad/umoddi3.c b/common/lib/libc/quad/umoddi3.c new file mode 100644 index 000000000..2fef61e5f --- /dev/null +++ b/common/lib/libc/quad/umoddi3.c @@ -0,0 +1,57 @@ +/* $NetBSD: umoddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)umoddi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: umoddi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return remainder after dividing two unsigned quads. + */ +u_quad_t +__umoddi3(u_quad_t a, u_quad_t b) +{ + u_quad_t r; + + (void)__qdivrem(a, b, &r); + return (r); +} diff --git a/common/lib/libc/quad/xordi3.c b/common/lib/libc/quad/xordi3.c new file mode 100644 index 000000000..1a1fa4f27 --- /dev/null +++ b/common/lib/libc/quad/xordi3.c @@ -0,0 +1,60 @@ +/* $NetBSD: xordi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)xordi3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: xordi3.c,v 1.2 2009/03/15 22:31:12 cegger Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Return a ^ b, in quad. + */ +quad_t +__xordi3(quad_t a, quad_t b) +{ + union uu aa, bb; + + aa.q = a; + bb.q = b; + aa.ul[0] ^= bb.ul[0]; + aa.ul[1] ^= bb.ul[1]; + return (aa.q); +} diff --git a/common/lib/libc/stdlib/_strtol.h b/common/lib/libc/stdlib/_strtol.h new file mode 100644 index 000000000..8f7ec4924 --- /dev/null +++ b/common/lib/libc/stdlib/_strtol.h @@ -0,0 +1,169 @@ +/* $NetBSD: _strtol.h,v 1.2 2009/05/20 22:03:29 christos 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. + * + * Original version ID: + * NetBSD: src/lib/libc/locale/_wcstol.h,v 1.2 2003/08/07 16:43:03 agc Exp + */ + +/* + * function template for strtol, strtoll and strtoimax. + * + * parameters: + * _FUNCNAME : function name + * __INT : return type + * __INT_MIN : lower limit of the return type + * __INT_MAX : upper limit of the return type + */ + +__INT +_FUNCNAME(const char *nptr, char **endptr, int base) +{ + const char *s; + __INT acc, cutoff; + unsigned char c; + int i, neg, any, cutlim; + + _DIAGASSERT(nptr != NULL); + /* endptr may be NULL */ + + /* check base value */ + if (base && (base < 2 || base > 36)) { +#if !defined(_KERNEL) && !defined(_STANDALONE) + errno = EINVAL; + if (endptr != NULL) + /* LINTED interface specification */ + *endptr = __UNCONST(nptr); + return 0; +#else + panic("%s: invalid base %d", __func__, base); +#endif + } + + /* + * 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 = *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 ? __INT_MIN : __INT_MAX); + cutlim = (int)(cutoff % base); + cutoff /= base; + if (neg) { + if (cutlim > 0) { + cutlim -= base; + cutoff += 1; + } + cutlim = -cutlim; + } + for (acc = 0, any = 0;; c = *s++) { + if (isdigit(c)) + i = c - '0'; + else if (isalpha(c)) + i = c - (isupper(c) ? 'A' - 10 : 'a' - 10); + else + break; + if (i >= base) + break; + if (any < 0) + continue; + if (neg) { + if (acc < cutoff || (acc == cutoff && i > cutlim)) { + acc = __INT_MIN; +#if !defined(_KERNEL) && !defined(_STANDALONE) + any = -1; + errno = ERANGE; +#else + any = 0; + break; +#endif + } else { + any = 1; + acc *= base; + acc -= i; + } + } else { + if (acc > cutoff || (acc == cutoff && i > cutlim)) { + acc = __INT_MAX; +#if !defined(_KERNEL) && !defined(_STANDALONE) + any = -1; + errno = ERANGE; +#else + any = 0; + break; +#endif + } else { + any = 1; + acc *= base; + acc += i; + } + } + } + if (endptr != NULL) + /* LINTED interface specification */ + *endptr = __UNCONST(any ? s - 1 : nptr); + return(acc); +} diff --git a/common/lib/libc/stdlib/_strtoul.h b/common/lib/libc/stdlib/_strtoul.h new file mode 100644 index 000000000..a9519f955 --- /dev/null +++ b/common/lib/libc/stdlib/_strtoul.h @@ -0,0 +1,128 @@ +/* $NetBSD: _strtoul.h,v 1.1 2008/08/20 12:42:26 joerg 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. + * + * Original version ID: + * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp + */ + +/* + * function template for strtoul, strtoull and strtoumax. + * + * parameters: + * _FUNCNAME : function name + * __UINT : return type + * __UINT_MAX : upper limit of the return type + */ + +__UINT +_FUNCNAME(const char *nptr, char **endptr, int base) +{ + const char *s; + __UINT acc, cutoff; + unsigned char c; + int i, neg, any, cutlim; + + _DIAGASSERT(nptr != NULL); + /* endptr may be NULL */ + + /* check base value */ + if (base && (base < 2 || base > 36)) { +#if !defined(_KERNEL) && !defined(_STANDALONE) + errno = EINVAL; + return(0); +#else + panic("%s: invalid base %d", __func__, base); +#endif + } + + /* + * 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 = *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); + + /* + * See strtol for comments as to the logic used. + */ + cutoff = __UINT_MAX / (__UINT)base; + cutlim = (int)(__UINT_MAX % (__UINT)base); + for (acc = 0, any = 0;; c = *s++) { + if (isdigit(c)) + i = c - '0'; + else if (isalpha(c)) + i = c - (isupper(c) ? 'A' - 10 : 'a' - 10); + else + break; + if (i >= base) + break; + if (any < 0) + continue; + if (acc > cutoff || (acc == cutoff && i > cutlim)) { + acc = __UINT_MAX; +#if !defined(_KERNEL) && !defined(_STANDALONE) + any = -1; + errno = ERANGE; +#else + any = 0; + break; +#endif + } else { + any = 1; + acc *= (__UINT)base; + acc += i; + } + } + if (neg && any > 0) + acc = -acc; + if (endptr != NULL) + /* LINTED interface specification */ + *endptr = __UNCONST(any ? s - 1 : nptr); + return(acc); +} diff --git a/common/lib/libc/stdlib/heapsort.c b/common/lib/libc/stdlib/heapsort.c new file mode 100644 index 000000000..d0c74c59b --- /dev/null +++ b/common/lib/libc/stdlib/heapsort.c @@ -0,0 +1,229 @@ +/* $NetBSD: heapsort.c,v 1.3 2008/11/17 10:21:30 jnemeth Exp $ */ + +/*- + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +/* + * XXX Undefine the renames of these functions so that we don't + * XXX rename the versions found in the host's headers by mistake! + */ +#undef heapsort +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "from: @(#)heapsort.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: heapsort.c,v 1.3 2008/11/17 10:21:30 jnemeth Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if defined(_KERNEL) || defined(_STANDALONE) +#include + +#include +#else /* _KERNEL || _STANDALONE */ +#include "namespace.h" +#include + +#include +#include +#include + +#if HAVE_NBTOOL_CONFIG_H +/* XXX Now, re-apply the renaming that we undid above. */ +#define heapsort __nbcompat_heapsort +#endif + +#ifdef __weak_alias +__weak_alias(heapsort,_heapsort) +#endif +#endif /* _KERNEL || _STANDALONE */ + +/* + * 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. + */ +#if defined(_KERNEL) || defined(_STANDALONE) +int +kheapsort(void *vbase, size_t nmemb, size_t size, + int (*compar)(const void *, const void *), void *k) +#else +int +heapsort(void *vbase, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)) +#endif +{ + size_t cnt, i, j, l; + char tmp, *tmp1, *tmp2; + char *base, *p, *t; +#if !defined(_KERNEL) && !defined(_STANDALONE) + char *k; +#endif + + _DIAGASSERT(vbase != NULL); + _DIAGASSERT(compar != NULL); + + if (nmemb <= 1) + return (0); + + if (!size) { +#if !defined(_KERNEL) && !defined(_STANDALONE) + errno = EINVAL; +#endif + return (-1); + } + +#if !defined(_KERNEL) && !defined(_STANDALONE) + if ((k = malloc(size)) == NULL) + return (-1); +#endif + + /* + * 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); + } +#if !defined(_KERNEL) && !defined(_STANDALONE) + free(k); +#endif + return (0); +} diff --git a/common/lib/libc/stdlib/random.c b/common/lib/libc/stdlib/random.c new file mode 100644 index 000000000..85a50dad8 --- /dev/null +++ b/common/lib/libc/stdlib/random.c @@ -0,0 +1,530 @@ +/* $NetBSD: random.c,v 1.3 2005/12/21 14:23:58 christos Exp $ */ + +/* + * Copyright (c) 1983, 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. + */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95"; +#else +__RCSID("$NetBSD: random.c,v 1.3 2005/12/21 14:23:58 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include "reentrant.h" + +#ifdef __weak_alias +__weak_alias(initstate,_initstate) +__weak_alias(random,_random) +__weak_alias(setstate,_setstate) +__weak_alias(srandom,_srandom) +#endif + + +#ifdef _REENTRANT +static mutex_t random_mutex = MUTEX_INITIALIZER; +#endif +#else +#include +#define mutex_lock(a) (void)0 +#define mutex_unlock(a) (void)0 +#endif + +#ifndef SMALL_RANDOM +static void srandom_unlocked(unsigned int); +static long random_unlocked(void); + +#define USE_BETTER_RANDOM + +/* + * 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 ints; 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 ints 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. + * + * Modified 28 December 1994 by Jacob S. Rosenberg. + * The following changes have been made: + * All references to the type u_int have been changed to unsigned long. + * All references to type int have been changed to type long. Other + * cleanups have been made as well. A warning for both initstate and + * setstate has been inserted to the effect that on Sparc platforms + * the 'arg_state' variable must be forced to begin on word boundaries. + * This can be easily done by casting a long integer array to char *. + * The overall logic has been left STRICTLY alone. This software was + * tested on both a VAX and Sun SpacsStation with exactly the same + * results. The new version and the original give IDENTICAL results. + * The new version is somewhat faster than the original. As the + * documentation says: "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." For a buffer of + * 128 bytes, this new version runs about 19 percent faster and for a 16 + * byte buffer it is about 5 percent faster. + * + * Modified 07 January 2002 by Jason R. Thorpe. + * The following changes have been made: + * All the references to "long" have been changed back to "int". This + * fixes memory corruption problems on LP64 platforms. + */ + +/* + * 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 const int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; +static const 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. + */ + +/* LINTED */ +static int randtbl[DEG_3 + 1] = { + TYPE_3, +#ifdef USE_BETTER_RANDOM + 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, +#else + 0x9a319039, 0x32d9c024, 0x9b663182, + 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, + 0xf103bc02, 0x48f340fb, 0x7449e56b, + 0xbeb1dbb0, 0xab5c5918, 0x946554fd, + 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, + 0x2d436b86, 0xda672e2a, 0x1588ca88, + 0xe369735d, 0x904f35f7, 0xd7158fd6, + 0x6fa6f051, 0x616e6b96, 0xac94efdc, + 0x36413f93, 0xc622c298, 0xf5a42ab8, + 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, + 0x27fb47b9, +#endif /* USE_BETTER_RANDOM */ +}; + +/* + * 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 int *fptr = &randtbl[SEP_3 + 1]; +static int *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 int *state = &randtbl[1]; +static int rand_type = TYPE_3; +static int rand_deg = DEG_3; +static int rand_sep = SEP_3; +static int *end_ptr = &randtbl[DEG_3 + 1]; + +/* + * 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_unlocked(unsigned int x) +{ + int i; + + if (rand_type == TYPE_0) + state[0] = x; + else { + state[0] = x; + for (i = 1; i < rand_deg; i++) { +#ifdef USE_BETTER_RANDOM + int x1, hi, lo, t; + + /* + * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1). + * From "Random number generators: good ones are hard + * to find", Park and Miller, Communications of the ACM, + * vol. 31, no. 10, + * October 1988, p. 1195. + */ + x1 = state[i - 1]; + hi = x1 / 127773; + lo = x1 % 127773; + t = 16807 * lo - 2836 * hi; + if (t <= 0) + t += 0x7fffffff; + state[i] = t; +#else + state[i] = 1103515245 * state[i - 1] + 12345; +#endif /* USE_BETTER_RANDOM */ + } + fptr = &state[rand_sep]; + rptr = &state[0]; + for (i = 0; i < 10 * rand_deg; i++) + (void)random_unlocked(); + } +} + +void +srandom(unsigned long x) +{ + + mutex_lock(&random_mutex); + srandom_unlocked((unsigned int) x); + mutex_unlock(&random_mutex); +} + +/* + * 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. + * + * Note: The Sparc platform requires that arg_state begin on an int + * word boundary; otherwise a bus error will occur. Even so, lint will + * complain about mis-alignment, but you should disregard these messages. + */ +char * +initstate( + unsigned long seed, /* seed for R.N.G. */ + char *arg_state, /* pointer to state array */ + size_t n) /* # bytes of state info */ +{ + void *ostate = (void *)(&state[-1]); + int *int_arg_state; + + _DIAGASSERT(arg_state != NULL); + + int_arg_state = (int *)(void *)arg_state; + + mutex_lock(&random_mutex); + if (rand_type == TYPE_0) + state[-1] = rand_type; + else + state[-1] = MAX_TYPES * (int)(rptr - state) + rand_type; + if (n < BREAK_0) { + mutex_unlock(&random_mutex); + return (NULL); + } else 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 = (int *) (int_arg_state + 1); /* first location */ + end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ + srandom_unlocked((unsigned int) seed); + if (rand_type == TYPE_0) + int_arg_state[0] = rand_type; + else + int_arg_state[0] = MAX_TYPES * (int)(rptr - state) + rand_type; + mutex_unlock(&random_mutex); + return((char *)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. + * + * Note: The Sparc platform requires that arg_state begin on a long + * word boundary; otherwise a bus error will occur. Even so, lint will + * complain about mis-alignment, but you should disregard these messages. + */ +char * +setstate(char *arg_state) /* pointer to state array */ +{ + int *new_state; + int type; + int rear; + void *ostate = (void *)(&state[-1]); + + _DIAGASSERT(arg_state != NULL); + + new_state = (int *)(void *)arg_state; + type = (int)(new_state[0] % MAX_TYPES); + rear = (int)(new_state[0] / MAX_TYPES); + + mutex_lock(&random_mutex); + if (rand_type == TYPE_0) + state[-1] = rand_type; + else + state[-1] = MAX_TYPES * (int)(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: + mutex_unlock(&random_mutex); + return (NULL); + } + state = (int *) (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 */ + mutex_unlock(&random_mutex); + return((char *)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_unlocked(void) +{ + int i; + int *f, *r; + + if (rand_type == TYPE_0) { + i = state[0]; + state[0] = i = (i * 1103515245 + 12345) & 0x7fffffff; + } else { + /* + * Use local variables rather than static variables for speed. + */ + f = fptr; r = rptr; + *f += *r; + /* chucking least random bit */ + i = ((unsigned int)*f >> 1) & 0x7fffffff; + if (++f >= end_ptr) { + f = state; + ++r; + } + else if (++r >= end_ptr) { + r = state; + } + + fptr = f; rptr = r; + } + return(i); +} + +long +random(void) +{ + long r; + + mutex_lock(&random_mutex); + r = random_unlocked(); + mutex_unlock(&random_mutex); + return (r); +} +#else +long +random(void) +{ + static u_long randseed = 1; + long x, hi, lo, t; + + /* + * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1). + * From "Random number generators: good ones are hard to find", + * Park and Miller, Communications of the ACM, vol. 31, no. 10, + * October 1988, p. 1195. + */ + x = randseed; + hi = x / 127773; + lo = x % 127773; + t = 16807 * lo - 2836 * hi; + if (t <= 0) + t += 0x7fffffff; + randseed = t; + return (t); +} +#endif /* SMALL_RANDOM */ diff --git a/common/lib/libc/stdlib/strtoll.c b/common/lib/libc/stdlib/strtoll.c new file mode 100644 index 000000000..184f92c90 --- /dev/null +++ b/common/lib/libc/stdlib/strtoll.c @@ -0,0 +1,66 @@ +/* $NetBSD: strtoll.c,v 1.6 2008/08/22 03:00:02 matt Exp $ */ + +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: strtoll.c,v 1.6 2008/08/22 03:00:02 matt Exp $"); + +#ifdef _LIBC +#include "namespace.h" +#endif + +#if defined(_KERNEL) +#include +#include +#elif defined(_STANDALONE) +#include +#include +#include +#else +#include +#include +#include +#include +#include +#include +#endif + +#define _FUNCNAME strtoll +#define __INT long long +#define __INT_MIN LLONG_MIN +#define __INT_MAX LLONG_MAX + +#include "_strtol.h" + +#ifdef _LIBC +__weak_alias(strtoll, _strtoll) +#endif diff --git a/common/lib/libc/stdlib/strtoul.c b/common/lib/libc/stdlib/strtoul.c new file mode 100644 index 000000000..764b09189 --- /dev/null +++ b/common/lib/libc/stdlib/strtoul.c @@ -0,0 +1,53 @@ +/* $NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster Exp $ */ + +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * 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 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 +__RCSID("$NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster Exp $"); + +#if defined(_KERNEL) +#include +#include +#elif defined(_STANDALONE) +#include +#include +#include +#else +#include +#include +#include +#include +#include +#include +#endif + +#define _FUNCNAME strtoul +#define __UINT unsigned long int +#define __UINT_MAX ULONG_MAX + +#include "_strtoul.h" diff --git a/common/lib/libc/stdlib/strtoull.c b/common/lib/libc/stdlib/strtoull.c new file mode 100644 index 000000000..72efd4ae9 --- /dev/null +++ b/common/lib/libc/stdlib/strtoull.c @@ -0,0 +1,65 @@ +/* $NetBSD: strtoull.c,v 1.5 2008/09/10 18:08:58 joerg Exp $ */ + +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: strtoull.c,v 1.5 2008/09/10 18:08:58 joerg Exp $"); + +#ifdef _LIBC +#include "namespace.h" +#endif + +#if defined(_KERNEL) +#include +#include +#elif defined(_STANDALONE) +#include +#include +#include +#else +#include +#include +#include +#include +#include +#include +#endif + +#define _FUNCNAME strtoull +#define __UINT unsigned long long int +#define __UINT_MAX ULLONG_MAX + +#include "_strtoul.h" + +#ifdef _LIBC +__weak_alias(strtoull, _strtoull) +#endif diff --git a/common/lib/libc/stdlib/strtoumax.c b/common/lib/libc/stdlib/strtoumax.c new file mode 100644 index 000000000..d8356e0b1 --- /dev/null +++ b/common/lib/libc/stdlib/strtoumax.c @@ -0,0 +1,66 @@ +/* $NetBSD: strtoumax.c,v 1.5 2008/09/10 18:08:58 joerg Exp $ */ + +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: strtoumax.c,v 1.5 2008/09/10 18:08:58 joerg Exp $"); + +#ifdef _LIBC +#include "namespace.h" +#endif + +#if defined(_KERNEL) +#include +#include +#elif defined(_STANDALONE) +#include +#include +#include +#else +#include +#include +#include +#include +#include +#include +#include +#endif + +#define _FUNCNAME strtoumax +#define __UINT uintmax_t +#define __UINT_MAX UINTMAX_MAX + +#include "_strtoul.h" + +#ifdef _LIBC +__weak_alias(strtoumax, _strtoumax) +#endif diff --git a/common/lib/libc/string/bcmp.c b/common/lib/libc/string/bcmp.c new file mode 100644 index 000000000..724658d9d --- /dev/null +++ b/common/lib/libc/string/bcmp.c @@ -0,0 +1,70 @@ +/* $NetBSD: bcmp.c,v 1.6 2009/04/25 12:20:48 skrll 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)bcmp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: bcmp.c,v 1.6 2009/04/25 12:20:48 skrll Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + + +#if defined(_KERNEL) || defined(_STANDALONE) +#include +#if defined(_STANDALONE) +#include +#endif +#else +#include +#include +#endif + +/* + * bcmp -- vax cmpc3 instruction + */ +int +bcmp(const void *b1, const void *b2, size_t length) +{ + const char *p1 = b1, *p2 = b2; + + _DIAGASSERT(b1 != 0); + _DIAGASSERT(b2 != 0); + + if (length == 0) + return(0); + do + if (*p1++ != *p2++) + break; + while (--length); + return(length); +} diff --git a/common/lib/libc/string/bcopy.c b/common/lib/libc/string/bcopy.c new file mode 100644 index 000000000..6d379ffba --- /dev/null +++ b/common/lib/libc/string/bcopy.c @@ -0,0 +1,210 @@ +/* $NetBSD: bcopy.c,v 1.9 2009/03/18 12:25:06 tsutsui 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 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)bcopy.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: bcopy.c,v 1.9 2009/03/18 12:25:06 tsutsui Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#if !defined(MEMCOPY) && defined(_STANDALONE) +#include +#endif +#endif + +#ifdef _FORTIFY_SOURCE +#undef bcopy +#undef memcpy +#undef memmove +#endif + +#ifndef __OPTIMIZE_SIZE__ +/* + * sizeof(word) MUST BE A POWER OF TWO + * SO THAT wmask BELOW IS ALL ONES + */ +typedef long word; /* "word" used for optimal copy speed */ + +#define wsize sizeof(word) +#define wmask (wsize - 1) + +/* + * Copy a block of memory, handling overlap. + * This is the routine that actually implements + * (the portable versions of) bcopy, memcpy, and memmove. + */ +#if defined(MEMCOPY) +void * +memcpy(void *dst0, const void *src0, size_t length) +#elif defined(MEMMOVE) +void * +memmove(void *dst0, const void *src0, size_t length) +#else +void +bcopy(const void *src0, void *dst0, size_t length) +#endif +{ + char *dst = dst0; + const char *src = src0; + size_t t; + unsigned long u; + +#if !defined(_KERNEL) + _DIAGASSERT(dst0 != 0); + _DIAGASSERT(src0 != 0); +#endif + + if (length == 0 || dst == src) /* nothing to do */ + goto done; + + /* + * Macros: loop-t-times; and loop-t-times, t>0 + */ +#define TLOOP(s) if (t) TLOOP1(s) +#define TLOOP1(s) do { s; } while (--t) + + if ((unsigned long)dst < (unsigned long)src) { + /* + * Copy forward. + */ + u = (unsigned long)src; /* only need low bits */ + if ((u | (unsigned long)dst) & wmask) { + /* + * Try to align operands. This cannot be done + * unless the low bits match. + */ + if ((u ^ (unsigned long)dst) & wmask || length < wsize) + t = length; + else + t = wsize - (size_t)(u & wmask); + length -= t; + TLOOP1(*dst++ = *src++); + } + /* + * Copy whole words, then mop up any trailing bytes. + */ + t = length / wsize; + TLOOP(*(word *)(void *)dst = *(const word *)(const void *)src; src += wsize; dst += wsize); + t = length & wmask; + TLOOP(*dst++ = *src++); + } else { + /* + * Copy backwards. Otherwise essentially the same. + * Alignment works as before, except that it takes + * (t&wmask) bytes to align, not wsize-(t&wmask). + */ + src += length; + dst += length; + _DIAGASSERT((unsigned long)dst >= (unsigned long)dst0); + _DIAGASSERT((unsigned long)src >= (unsigned long)src0); + u = (unsigned long)src; + if ((u | (unsigned long)dst) & wmask) { + if ((u ^ (unsigned long)dst) & wmask || length <= wsize) + t = length; + else + t = (size_t)(u & wmask); + length -= t; + TLOOP1(*--dst = *--src); + } + t = length / wsize; + TLOOP(src -= wsize; dst -= wsize; *(word *)(void *)dst = *(const word *)(const void *)src); + t = length & wmask; + TLOOP(*--dst = *--src); + } +done: +#if defined(MEMCOPY) || defined(MEMMOVE) + return (dst0); +#else + return; +#endif +} +#else /* __OPTIMIZE_SIZE__ */ +#if defined(MEMCOPY) +/* + * This is designed to be small, not fast. + */ +void * +memcpy(void *s1, const void *s2, size_t n) +{ + const char *f = s2; + char *t = s1; + + while (n-- > 0) + *t++ = *f++; + return s1; +} +#elif defined(MEMMOVE) +/* + * This is designed to be small, not fast. + */ +void * +memmove(void *s1, const void *s2, size_t n) +{ + const char *f = s2; + char *t = s1; + + if (f < t) { + f += n; + t += n; + while (n-- > 0) + *--t = *--f; + } else { + while (n-- > 0) + *t++ = *f++; + } + return s1; +} +#else +/* + * This is designed to be small, not fast. + */ +void +bcopy(const void *s2, void *s1, size_t n) +{ + const char *f = s2; + char *t = s1; + + while (n-- > 0) + *t++ = *f++; +} +#endif +#endif /* __OPTIMIZE_SIZE__ */ diff --git a/common/lib/libc/string/bzero.c b/common/lib/libc/string/bzero.c new file mode 100644 index 000000000..8db58bb9f --- /dev/null +++ b/common/lib/libc/string/bzero.c @@ -0,0 +1,4 @@ +/* $NetBSD: bzero.c,v 1.1 2005/12/20 19:28:52 christos Exp $ */ + +#define BZERO +#include "memset.c" diff --git a/common/lib/libc/string/bzero2.c b/common/lib/libc/string/bzero2.c new file mode 100644 index 000000000..0522034e7 --- /dev/null +++ b/common/lib/libc/string/bzero2.c @@ -0,0 +1,36 @@ +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas . + * + * 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. + */ + +#define BZERO + +#include "memset2.c" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: bzero2.c,v 1.2 2009/12/14 00:39:01 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ diff --git a/common/lib/libc/string/ffs.c b/common/lib/libc/string/ffs.c new file mode 100644 index 000000000..0217f5e0f --- /dev/null +++ b/common/lib/libc/string/ffs.c @@ -0,0 +1,60 @@ +/* $NetBSD: ffs.c,v 1.3 2007/11/02 21:05:06 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ffs.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: ffs.c,v 1.3 2007/11/02 21:05:06 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#else +#include +#endif + +/* #undef ffs() - might be defined as macro to __builtin_ffs() */ +#undef ffs + +/* + * ffs -- vax ffs instruction + */ +int +ffs(int mask) +{ + return ffs32((uint32_t)mask); +} diff --git a/common/lib/libc/string/memchr.c b/common/lib/libc/string/memchr.c new file mode 100644 index 000000000..93d11cd59 --- /dev/null +++ b/common/lib/libc/string/memchr.c @@ -0,0 +1,66 @@ +/* $NetBSD: memchr.c,v 1.3 2008/01/08 21:57:06 martin 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 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)memchr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: memchr.c,v 1.3 2008/01/08 21:57:06 martin Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +void * +memchr(const void *s, int c, size_t n) +{ + _DIAGASSERT(s != NULL); + + if (n != 0) { + const unsigned char *p = s; + const unsigned char cmp = c; + + do { + if (*p++ == cmp) + return __UNCONST(p - 1); + } while (--n != 0); + } + return NULL; +} diff --git a/common/lib/libc/string/memcmp.c b/common/lib/libc/string/memcmp.c new file mode 100644 index 000000000..12880486b --- /dev/null +++ b/common/lib/libc/string/memcmp.c @@ -0,0 +1,69 @@ +/* $NetBSD: memcmp.c,v 1.2 2007/06/04 18:19:27 christos 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 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)memcmp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: memcmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +/* + * Compare memory regions. + */ +int +memcmp(const void *s1, const void *s2, size_t n) +{ + _DIAGASSERT(s1 != 0); + _DIAGASSERT(s2 != 0); + + if (n != 0) { + const unsigned char *p1 = s1, *p2 = s2; + + do { + if (*p1++ != *p2++) + return (*--p1 - *--p2); + } while (--n != 0); + } + return (0); +} diff --git a/common/lib/libc/string/memcpy.c b/common/lib/libc/string/memcpy.c new file mode 100644 index 000000000..38792bb2f --- /dev/null +++ b/common/lib/libc/string/memcpy.c @@ -0,0 +1,4 @@ +/* $NetBSD: memcpy.c,v 1.1 2005/12/20 19:28:52 christos Exp $ */ + +#define MEMCOPY +#include "bcopy.c" diff --git a/common/lib/libc/string/memmove.c b/common/lib/libc/string/memmove.c new file mode 100644 index 000000000..a3af3b7a4 --- /dev/null +++ b/common/lib/libc/string/memmove.c @@ -0,0 +1,4 @@ +/* $NetBSD: memmove.c,v 1.1 2005/12/20 19:28:52 christos Exp $ */ + +#define MEMMOVE +#include "bcopy.c" diff --git a/common/lib/libc/string/memset.c b/common/lib/libc/string/memset.c new file mode 100644 index 000000000..ed2ccb416 --- /dev/null +++ b/common/lib/libc/string/memset.c @@ -0,0 +1,167 @@ +/* $NetBSD: memset.c,v 1.8 2009/03/18 12:25:06 tsutsui 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 + * Mike Hibler and 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)memset.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: memset.c,v 1.8 2009/03/18 12:25:06 tsutsui Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#include +#else +#include +#if defined(BZERO) && defined(_STANDALONE) +#include +#endif +#include +#endif + +#define wsize sizeof(u_int) +#define wmask (wsize - 1) + +#ifdef _FORTIFY_SOURCE +#undef bzero +#undef memset +#endif + +#ifndef __OPTIMIZE_SIZE__ +#ifdef BZERO +#define RETURN return +#define VAL 0 +#define WIDEVAL 0 + +void +bzero(void *dst0, size_t length) +#else +#define RETURN return (dst0) +#define VAL c0 +#define WIDEVAL c + +void * +memset(void *dst0, int c0, size_t length) +#endif +{ + size_t t; +#ifndef BZERO + u_int c; +#endif + u_char *dst; + + _DIAGASSERT(dst0 != 0); + + dst = dst0; + /* + * If not enough words, just fill bytes. A length >= 2 words + * guarantees that at least one of them is `complete' after + * any necessary alignment. For instance: + * + * |-----------|-----------|-----------| + * |00|01|02|03|04|05|06|07|08|09|0A|00| + * ^---------------------^ + * dst dst+length-1 + * + * but we use a minimum of 3 here since the overhead of the code + * to do word writes is substantial. + */ + if (length < 3 * wsize) { + while (length != 0) { + *dst++ = VAL; + --length; + } + RETURN; + } + +#ifndef BZERO + if ((c = (u_char)c0) != 0) { /* Fill the word. */ + c = (c << 8) | c; /* u_int is 16 bits. */ +#if UINT_MAX > 0xffff + c = (c << 16) | c; /* u_int is 32 bits. */ +#endif +#if UINT_MAX > 0xffffffff + c = (c << 32) | c; /* u_int is 64 bits. */ +#endif + } +#endif + /* Align destination by filling in bytes. */ + if ((t = (size_t)((u_long)dst & wmask)) != 0) { + t = wsize - t; + length -= t; + do { + *dst++ = VAL; + } while (--t != 0); + } + + /* Fill words. Length was >= 2*words so we know t >= 1 here. */ + t = length / wsize; + do { + *(u_int *)(void *)dst = WIDEVAL; + dst += wsize; + } while (--t != 0); + + /* Mop up trailing bytes, if any. */ + t = length & wmask; + if (t != 0) + do { + *dst++ = VAL; + } while (--t != 0); + RETURN; +} +#else /* __OPTIMIZE_SIZE__ */ +#ifdef BZERO +void +bzero(void *dstv, size_t length) +{ + u_char *dst = dstv; + while (length-- > 0) + *dst++ = 0; +} +#else +void * +memset(void *dstv, int c, size_t length) +{ + u_char *dst = dstv; + while (length-- > 0) + *dst++ = c; + return dstv; +} +#endif /* BZERO */ +#endif /* __OPTIMIZE_SIZE__ */ diff --git a/common/lib/libc/string/memset2.c b/common/lib/libc/string/memset2.c new file mode 100644 index 000000000..7009f4805 --- /dev/null +++ b/common/lib/libc/string/memset2.c @@ -0,0 +1,277 @@ +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas . + * + * 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 + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#include +#include +#else +#include +#include +#endif + +#include +#include + +#ifdef TEST +#include +#define _DIAGASSERT(a) assert(a) +#endif + +#ifdef _FORTIFY_SOURCE +#undef bzero +#undef memset +#endif + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: memset2.c,v 1.2 2009/12/14 00:39:01 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +/* + * Assume uregister_t is the widest non-synthetic unsigned type. + */ +typedef uregister_t memword_t; + +#ifdef BZERO +static inline +#define memset memset0 +#endif + +#ifdef TEST +static +#define memset test_memset +#endif + +#ifdef CTASSERT +CTASSERT((~(memword_t)0U >> 1) != ~(memword_t)0U); +#endif + +void * +memset(void *addr, int c, size_t len) +{ + memword_t *dstp = addr; + memword_t *edstp; + memword_t fill; +#ifndef __OPTIMIZE_SIZE__ + memword_t keep_mask = 0; +#endif + size_t fill_count; + + _DIAGASSERT(addr != 0); + + if (__predict_false(len == 0)) + return addr; + + /* + * Pad out the fill byte (v) across a memword_t. + * The conditional at the end prevents GCC from complaing about + * shift count >= width of type + */ + fill = c; + fill |= fill << 8; + fill |= fill << 16; + fill |= fill << (sizeof(c) < sizeof(fill) ? 32 : 0); + + /* + * Get the number of unaligned bytes to fill in the first word. + */ + fill_count = -(uintptr_t)addr & (sizeof(memword_t) - 1); + + if (__predict_false(fill_count != 0)) { +#ifndef __OPTIMIZE_SIZE__ + /* + * We want to clear trailing bytes in the word. + * On big/little endian, these are the least/most significant, + * bits respectively. So as we shift, the keep_mask will only + * have bits set for the bytes we won't be filling. + */ +#if BYTE_ORDER == BIG_ENDIAN + keep_mask = ~(memword_t)0U << (fill_count * 8); +#endif +#if BYTE_ORDER == LITTLE_ENDIAN + keep_mask = ~(memword_t)0U >> (fill_count * 8); +#endif + /* + * Make sure dstp is aligned to a memword_t boundary. + */ + dstp = (memword_t *)((uintptr_t)addr & -sizeof(memword_t)); + if (len >= fill_count) { + /* + * If we can fill the rest of this word, then we mask + * off the bytes we are filling and then fill in those + * bytes with the new fill value. + */ + *dstp = (*dstp & keep_mask) | (fill & ~keep_mask); + len -= fill_count; + if (__predict_false(len == 0)) + return addr; + /* + * Since we were able to fill the rest of this word, + * we will advance to the next word and thus have no + * bytes to preserve. + * + * If we don't have enough to fill the rest of this + * word, we will fall through the following loop + * (since there are no full words to fill). Then we + * use the keep_mask above to preserve the leading + * bytes of word. + */ + dstp++; + keep_mask = 0; + } else { + len += (uintptr_t)addr & (sizeof(memword_t) - 1); + } +#else /* __OPTIMIZE_SIZE__ */ + uint8_t *dp, *ep; + if (len < fill_count) + fill_count = len; + for (dp = (uint8_t *)dstp, ep = dp + fill_count; + dp != ep; dp++) + *dp = fill; + if ((len -= fill_count) == 0) + return addr; + dstp = (memword_t *)ep; +#endif /* __OPTIMIZE_SIZE__ */ + } + + /* + * Simply fill memory one word at time (for as many full words we have + * to write). + */ + for (edstp = dstp + len / sizeof(memword_t); dstp != edstp; dstp++) + *dstp = fill; + + /* + * We didn't subtract out the full words we just filled since we know + * by the time we get here we will have less than a words worth to + * write. So we can concern ourselves with only the subword len bits. + */ + len &= sizeof(memword_t)-1; + if (len > 0) { +#ifndef __OPTIMIZE_SIZE__ + /* + * We want to clear leading bytes in the word. + * On big/little endian, these are the most/least significant + * bits, respectively, But as we want the mask of the bytes to + * keep, we have to complement the mask. So after we shift, + * the keep_mask will only have bits set for the bytes we won't + * be filling. + * + * But the keep_mask could already have bytes to preserve + * if the amount to fill was less than the amount of traiing + * space in the first word. + */ +#if BYTE_ORDER == BIG_ENDIAN + keep_mask |= ~(memword_t)0U >> (len * 8); +#endif +#if BYTE_ORDER == LITTLE_ENDIAN + keep_mask |= ~(memword_t)0U << (len * 8); +#endif + /* + * Now we mask off the bytes we are filling and then fill in + * those bytes with the new fill value. + */ + *dstp = (*dstp & keep_mask) | (fill & ~keep_mask); +#else /* __OPTIMIZE_SIZE__ */ + uint8_t *dp, *ep; + for (dp = (uint8_t *)dstp, ep = dp + len; + dp != ep; dp++) + *dp = fill; +#endif /* __OPTIMIZE_SIZE__ */ + } + + /* + * Return the initial addr + */ + return addr; +} + +#ifdef BZERO +/* + * For bzero, simply inline memset and let the compiler optimize things away. + */ +void +bzero(void *addr, size_t len) +{ + memset(addr, 0, len); +} +#endif + +#ifdef TEST +#include +#include + +#undef memset + +static union { + uint8_t bytes[sizeof(memword_t) * 4]; + memword_t words[4]; +} testmem; + +int +main(int argc, char **argv) +{ + size_t start; + size_t len; + bool failed = false; + + for (start = 1; start < sizeof(testmem) - 1; start++) { + for (len = 1; start + len < sizeof(testmem) - 1; len++) { + bool ok = true; + size_t i; + uint8_t check_value; + memset(testmem.bytes, 0xff, sizeof(testmem)); + test_memset(testmem.bytes + start, 0x00, len); + for (i = 0; i < sizeof(testmem); i++) { + if (i == 0 || i == start + len) + check_value = 0xff; + else if (i == start) + check_value = 0x00; + if (testmem.bytes[i] != check_value) { + if (ok) + printf("pass @ %zu .. %zu failed", + start, start + len - 1); + ok = false; + printf(" [%zu]=0x%02x(!0x%02x)", + i, testmem.bytes[i], check_value); + } + } + if (!ok) { + printf("\n"); + failed = 1; + } + } + } + + return failed ? 1 : 0; +} +#endif /* TEST */ diff --git a/common/lib/libc/string/popcount32.c b/common/lib/libc/string/popcount32.c new file mode 100644 index 000000000..bc5058d54 --- /dev/null +++ b/common/lib/libc/string/popcount32.c @@ -0,0 +1,77 @@ +/* $NetBSD: popcount32.c,v 1.3 2009/07/21 19:56:55 drochner Exp $ */ +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Joerg Sonnenberger. + * + * 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 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 +__RCSID("$NetBSD: popcount32.c,v 1.3 2009/07/21 19:56:55 drochner Exp $"); + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#include +#endif + +/* + * This a hybrid algorithm for bit counting between parallel counting and + * using multiplication. The idea is to sum up the bits in each Byte, so + * that the final accumulation can be done with a single multiplication. + * If the platform has a slow multiplication instruction, it can be replaced + * by the commented out version below. + */ + +unsigned int +popcount32(uint32_t v) +{ + unsigned int c; + + v = v - ((v >> 1) & 0x55555555U); + v = (v & 0x33333333U) + ((v >> 2) & 0x33333333U); + v = (v + (v >> 4)) & 0x0f0f0f0fU; + c = (v * 0x01010101U) >> 24; + /* + * v = (v >> 16) + v; + * v = (v >> 8) + v; + * c = v & 255; + */ + + return c; +} + +#if UINT_MAX == 0xffffffffU +__strong_alias(popcount, popcount32) +#endif + +#if ULONG_MAX == 0xffffffffU +__strong_alias(popcountl, popcount32) +#endif diff --git a/common/lib/libc/string/popcount64.c b/common/lib/libc/string/popcount64.c new file mode 100644 index 000000000..51a1c19be --- /dev/null +++ b/common/lib/libc/string/popcount64.c @@ -0,0 +1,82 @@ +/* $NetBSD: popcount64.c,v 1.5 2009/08/05 15:04:15 joerg Exp $ */ +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Joerg Sonnenberger. + * + * 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 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 +__RCSID("$NetBSD: popcount64.c,v 1.5 2009/08/05 15:04:15 joerg Exp $"); + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#include +#endif + +/* + * If uint64_t is larger than size_t, the follow assumes that + * splitting into 32bit halfes is faster. + * + * The native pocount64 version is based on the same ideas as popcount32(3), + * see popcount32.c for comments. + */ + +#if SIZE_MAX < 0xffffffffffffffffULL +unsigned int +popcount64(uint64_t v) +{ + return popcount32((uint32_t)(v >> 32)) + + popcount32((uint32_t)(v & 0xffffffffULL)); +} +#else +unsigned int +popcount64(uint64_t v) +{ + unsigned int c; + + v = v - ((v >> 1) & 0x5555555555555555ULL); + v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL); + v = ((v + (v >> 4)) & 0x0f0f0f0f0f0f0f0fULL) * 0x0101010101010101ULL; + c = v >> 56; + + return c; +} +#endif + +#if ULONG_MAX == 0xffffffffffffffffULL +__strong_alias(popcountl, popcount64) +#endif + +#if ULLONG_MAX == 0xffffffffffffffffULL +__strong_alias(popcountll, popcount64) +#endif + diff --git a/common/lib/libc/string/strcasecmp.c b/common/lib/libc/string/strcasecmp.c new file mode 100644 index 000000000..ad4e78ee7 --- /dev/null +++ b/common/lib/libc/string/strcasecmp.c @@ -0,0 +1,68 @@ +/* $NetBSD: strcasecmp.c,v 1.2 2007/06/04 18:19:27 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strcasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include "namespace.h" +#include +#include +#include +#ifdef __weak_alias +__weak_alias(strcasecmp,_strcasecmp) +__weak_alias(strncasecmp,_strncasecmp) +#endif +#else +#include +#include +#endif + +int +strcasecmp(const char *s1, const char *s2) +{ + const unsigned char *us1 = (const unsigned char *)s1, + *us2 = (const unsigned char *)s2; + + _DIAGASSERT(s1 != NULL); + _DIAGASSERT(s2 != NULL); + + while (tolower(*us1) == tolower(*us2++)) + if (*us1++ == '\0') + return (0); + return (tolower(*us1) - tolower(*--us2)); +} diff --git a/common/lib/libc/string/strcat.c b/common/lib/libc/string/strcat.c new file mode 100644 index 000000000..0d83d4562 --- /dev/null +++ b/common/lib/libc/string/strcat.c @@ -0,0 +1,65 @@ +/* $NetBSD: strcat.c,v 1.2 2007/06/04 18:19:27 christos Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strcat.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strcat.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +#ifdef _FORTIFY_SOURCE +#undef strcat +#endif + +char * +strcat(char *s, const char *append) +{ + char *t = s; + + _DIAGASSERT(t != NULL); + _DIAGASSERT(append != NULL); + + for (; *t; ++t) + ; + while ((*t++ = *append++) != '\0') + ; + return (s); +} diff --git a/common/lib/libc/string/strchr.c b/common/lib/libc/string/strchr.c new file mode 100644 index 000000000..01c8946c4 --- /dev/null +++ b/common/lib/libc/string/strchr.c @@ -0,0 +1,65 @@ +/* $NetBSD: strchr.c,v 1.4 2009/07/17 19:37:57 dsl 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)index.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strchr.c,v 1.4 2009/07/17 19:37:57 dsl Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include "namespace.h" +#include +#include +#else +#include +#endif + +__strong_alias(index, strchr) +char * +strchr(const char *p, int ch) +{ + const char cmp = ch; + _DIAGASSERT(p != NULL); + + for (;; ++p) { + if (*p == cmp) { + /* LINTED const cast-away */ + return(__UNCONST(p)); + } + if (!*p) + return((char *)NULL); + } + /* NOTREACHED */ +} diff --git a/common/lib/libc/string/strcmp.c b/common/lib/libc/string/strcmp.c new file mode 100644 index 000000000..e17aab2db --- /dev/null +++ b/common/lib/libc/string/strcmp.c @@ -0,0 +1,65 @@ +/* $NetBSD: strcmp.c,v 1.2 2007/06/04 18:19:27 christos 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 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strcmp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strcmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +/* + * Compare strings. + */ +int +strcmp(const char *s1, const char *s2) +{ + + _DIAGASSERT(s1 != NULL); + _DIAGASSERT(s2 != NULL); + + while (*s1 == *s2++) + if (*s1++ == 0) + return (0); + return (*(const unsigned char *)s1 - *(const unsigned char *)--s2); +} diff --git a/common/lib/libc/string/strcpy.c b/common/lib/libc/string/strcpy.c new file mode 100644 index 000000000..f7dfb58fd --- /dev/null +++ b/common/lib/libc/string/strcpy.c @@ -0,0 +1,62 @@ +/* $NetBSD: strcpy.c,v 1.2 2007/06/04 18:19:27 christos Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strcpy.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strcpy.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +#ifdef _FORTIFY_SOURCE +#undef strcpy +#endif + +char * +strcpy(char *to, const char *from) +{ + char *save = to; + + _DIAGASSERT(to != NULL); + _DIAGASSERT(from != NULL); + + for (; (*to = *from) != '\0'; ++from, ++to); + return(save); +} diff --git a/common/lib/libc/string/strlcat.c b/common/lib/libc/string/strlcat.c new file mode 100644 index 000000000..d4a8d7eba --- /dev/null +++ b/common/lib/libc/string/strlcat.c @@ -0,0 +1,85 @@ +/* $NetBSD: strlcat.c,v 1.3 2007/06/04 18:19:27 christos Exp $ */ +/* $OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $ */ + +/* + * Copyright (c) 1998 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 TODD C. MILLER DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER 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 !defined(_KERNEL) && !defined(_STANDALONE) +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: strlcat.c,v 1.3 2007/06/04 18:19:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifdef _LIBC +#include "namespace.h" +#endif +#include +#include +#include + +#ifdef _LIBC +# ifdef __weak_alias +__weak_alias(strlcat, _strlcat) +# endif +#endif + +#else +#include +#endif /* !_KERNEL && !_STANDALONE */ + +#if !HAVE_STRLCAT +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + + _DIAGASSERT(dst != NULL); + _DIAGASSERT(src != NULL); + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} +#endif diff --git a/common/lib/libc/string/strlcpy.c b/common/lib/libc/string/strlcpy.c new file mode 100644 index 000000000..fbb5f4dd2 --- /dev/null +++ b/common/lib/libc/string/strlcpy.c @@ -0,0 +1,81 @@ +/* $NetBSD: strlcpy.c,v 1.3 2007/06/04 18:19:27 christos Exp $ */ +/* $OpenBSD: strlcpy.c,v 1.7 2003/04/12 21:56:39 millert Exp $ */ + +/* + * Copyright (c) 1998 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 TODD C. MILLER DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER 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 !defined(_KERNEL) && !defined(_STANDALONE) +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: strlcpy.c,v 1.3 2007/06/04 18:19:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifdef _LIBC +#include "namespace.h" +#endif +#include +#include +#include + +#ifdef _LIBC +# ifdef __weak_alias +__weak_alias(strlcpy, _strlcpy) +# endif +#endif +#else +#include +#endif /* !_KERNEL && !_STANDALONE */ + + +#if !HAVE_STRLCPY +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + _DIAGASSERT(dst != NULL); + _DIAGASSERT(src != NULL); + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} +#endif diff --git a/common/lib/libc/string/strlen.c b/common/lib/libc/string/strlen.c new file mode 100644 index 000000000..84195a3fa --- /dev/null +++ b/common/lib/libc/string/strlen.c @@ -0,0 +1,58 @@ +/* $NetBSD: strlen.c,v 1.2 2007/06/04 18:19:27 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strlen.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strlen.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +size_t +strlen(const char *str) +{ + const char *s; + + _DIAGASSERT(str != NULL); + + for (s = str; *s; ++s) + continue; + return(s - str); +} diff --git a/common/lib/libc/string/strncasecmp.c b/common/lib/libc/string/strncasecmp.c new file mode 100644 index 000000000..e58a5daf1 --- /dev/null +++ b/common/lib/libc/string/strncasecmp.c @@ -0,0 +1,74 @@ +/* $NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include "namespace.h" +#include +#include +#include +#ifdef __weak_alias +__weak_alias(strcasecmp,_strcasecmp) +__weak_alias(strncasecmp,_strncasecmp) +#endif +#else +#include +#include +#endif + +int +strncasecmp(const char *s1, const char *s2, size_t n) +{ + + _DIAGASSERT(s1 != NULL); + _DIAGASSERT(s2 != NULL); + + if (n != 0) { + const unsigned char *us1 = (const unsigned char *)s1, + *us2 = (const unsigned char *)s2; + + do { + if (tolower(*us1) != tolower(*us2++)) + return (tolower(*us1) - tolower(*--us2)); + if (*us1++ == '\0') + break; + } while (--n != 0); + } + return (0); +} diff --git a/common/lib/libc/string/strncmp.c b/common/lib/libc/string/strncmp.c new file mode 100644 index 000000000..105e9149c --- /dev/null +++ b/common/lib/libc/string/strncmp.c @@ -0,0 +1,65 @@ +/* $NetBSD: strncmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strncmp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strncmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +int +strncmp(const char *s1, const char *s2, size_t n) +{ + + _DIAGASSERT(s1 != NULL); + _DIAGASSERT(s2 != NULL); + + if (n == 0) + return (0); + do { + if (*s1 != *s2++) + return (*(const unsigned char *)s1 - + *(const unsigned char *)--s2); + if (*s1++ == 0) + break; + } while (--n != 0); + return (0); +} diff --git a/common/lib/libc/string/strncpy.c b/common/lib/libc/string/strncpy.c new file mode 100644 index 000000000..afed6c039 --- /dev/null +++ b/common/lib/libc/string/strncpy.c @@ -0,0 +1,80 @@ +/* $NetBSD: strncpy.c,v 1.3 2007/06/04 18:19:28 christos 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 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strncpy.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strncpy.c,v 1.3 2007/06/04 18:19:28 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +#ifdef _FORTIFY_SOURCE +#undef strncpy +#endif + +/* + * Copy src to dst, truncating or null-padding to always copy n bytes. + * Return dst. + */ +char * +strncpy(char *dst, const char *src, size_t n) +{ + + _DIAGASSERT(dst != NULL); + _DIAGASSERT(src != NULL); + + if (n != 0) { + char *d = dst; + const char *s = src; + + do { + if ((*d++ = *s++) == 0) { + /* NUL pad the remaining n-1 bytes */ + while (--n != 0) + *d++ = 0; + break; + } + } while (--n != 0); + } + return (dst); +} diff --git a/common/lib/libc/string/strrchr.c b/common/lib/libc/string/strrchr.c new file mode 100644 index 000000000..c3ddb6452 --- /dev/null +++ b/common/lib/libc/string/strrchr.c @@ -0,0 +1,66 @@ +/* $NetBSD: strrchr.c,v 1.5 2009/07/17 19:37:57 dsl Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)rindex.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strrchr.c,v 1.5 2009/07/17 19:37:57 dsl Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#endif + +__strong_alias(rindex, strrchr) +char * +strrchr(const char *p, int ch) +{ + char *save; + const char c = ch; + + _DIAGASSERT(p != NULL); + + for (save = NULL;; ++p) { + if (*p == c) { + /* LINTED const cast-away */ + save = __UNCONST(p); + } + if (!*p) + return(save); + } + /* NOTREACHED */ +} diff --git a/common/lib/libc/string/strsep.c b/common/lib/libc/string/strsep.c new file mode 100644 index 000000000..7972b32e6 --- /dev/null +++ b/common/lib/libc/string/strsep.c @@ -0,0 +1,97 @@ +/* $NetBSD: strsep.c,v 1.3 2007/06/04 18:19:28 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strsep.c,v 1.3 2007/06/04 18:19:28 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(strsep,_strsep) +#endif + +#else +#include +#include +#endif /* !_KERNEL && !_STANDALONE */ + +#if !HAVE_STRSEP +/* + * Get next token from string *stringp, where tokens are possibly-empty + * strings separated by characters from delim. + * + * Writes NULs into the string at *stringp to end tokens. + * delim need not remain constant from call to call. + * On return, *stringp points past the last NUL written (if there might + * be further tokens), or is NULL (if there are definitely no more tokens). + * + * If *stringp is NULL, strsep returns NULL. + */ +char * +strsep(char **stringp, const char *delim) +{ + char *s; + const char *spanp; + int c, sc; + char *tok; + + _DIAGASSERT(stringp != NULL); + _DIAGASSERT(delim != NULL); + + if ((s = *stringp) == NULL) + return (NULL); + for (tok = s;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *stringp = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ +} +#endif diff --git a/common/lib/libc/string/strstr.c b/common/lib/libc/string/strstr.c new file mode 100644 index 000000000..c2fa47d3e --- /dev/null +++ b/common/lib/libc/string/strstr.c @@ -0,0 +1,75 @@ +/* $NetBSD: strstr.c,v 1.2 2007/06/04 18:19:28 christos 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 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strstr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strstr.c,v 1.2 2007/06/04 18:19:28 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#else +#include +#include +#endif + +/* + * Find the first occurrence of find in s. + */ +char * +strstr(const char *s, const char *find) +{ + char c, sc; + size_t len; + + _DIAGASSERT(s != NULL); + _DIAGASSERT(find != NULL); + + if ((c = *find++) != 0) { + len = strlen(find); + do { + do { + if ((sc = *s++) == 0) + return (NULL); + } while (sc != c); + } while (strncmp(s, find, len) != 0); + s--; + } + return __UNCONST(s); +} diff --git a/common/lib/libc/sys/cpuset.c b/common/lib/libc/sys/cpuset.c new file mode 100644 index 000000000..66b332050 --- /dev/null +++ b/common/lib/libc/sys/cpuset.c @@ -0,0 +1,310 @@ +/* $NetBSD: cpuset.c,v 1.16 2010/09/21 02:03:29 rmind Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + */ + +#ifndef _STANDALONE +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: cpuset.c,v 1.16 2010/09/21 02:03:29 rmind Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#ifdef _KERNEL +#include +#include +#include +#else +#include +#include +#include +#include +#endif + +#define CPUSET_SHIFT 5 +#define CPUSET_MASK 31 +#define CPUSET_NENTRIES(nc) ((nc) > 32 ? ((nc) >> CPUSET_SHIFT) : 1) +#ifndef __lint__ +#define CPUSET_SIZE(n) (sizeof( \ + struct { \ + uint32_t bits[0]; \ + }) + sizeof(uint32_t) * (n)) +#else +#define CPUSET_SIZE(n) 0 +#endif + +struct _cpuset { + uint32_t bits[0]; +}; + +#ifdef _KERNEL +struct _kcpuset { + unsigned int nused; + struct _kcpuset *next; + uint32_t bits[0]; +}; +#define KCPUSET_SIZE(n) (sizeof( \ + struct { \ + unsigned int nused; \ + struct _kcpuset *next; \ + uint32_t bits[0]; \ + }) + sizeof(uint32_t) * (n)) +#endif + +static size_t cpuset_size = 0; +static size_t cpuset_nentries = 0; + +#ifndef _KERNEL +size_t +/*ARGSUSED*/ +_cpuset_size(const cpuset_t *c) +{ + + return cpuset_size; +} + +void +_cpuset_zero(cpuset_t *c) +{ + + memset(c->bits, 0, cpuset_nentries * sizeof(c->bits[0])); +} + +int +_cpuset_isset(cpuid_t i, const cpuset_t *c) +{ + const unsigned long j = i >> CPUSET_SHIFT; + + if (j >= cpuset_nentries) { + errno = EINVAL; + return -1; + } + return ((1 << (i & CPUSET_MASK)) & c->bits[j]) != 0; +} + +int +_cpuset_set(cpuid_t i, cpuset_t *c) +{ + const unsigned long j = i >> CPUSET_SHIFT; + + if (j >= cpuset_nentries) { + errno = EINVAL; + return -1; + } + c->bits[j] |= 1 << (i & CPUSET_MASK); + return 0; +} + +int +_cpuset_clr(cpuid_t i, cpuset_t *c) +{ + const unsigned long j = i >> CPUSET_SHIFT; + + if (j >= cpuset_nentries) { + errno = EINVAL; + return -1; + } + c->bits[j] &= ~(1 << (i & CPUSET_MASK)); + return 0; +} + +cpuset_t * +_cpuset_create(void) +{ + + if (cpuset_size == 0) { + static int mib[2] = { CTL_HW, HW_NCPU }; + size_t len; + u_int nc; + + len = sizeof(nc); + if (sysctl(mib, __arraycount(mib), &nc, &len, NULL, 0) == -1) + return NULL; + + cpuset_nentries = CPUSET_NENTRIES(nc); + cpuset_size = CPUSET_SIZE(cpuset_nentries); + } + return calloc(1, cpuset_size); +} + +void +_cpuset_destroy(cpuset_t *c) +{ + + free(c); +} + +#else + +kcpuset_t * +kcpuset_create(void) +{ + kcpuset_t *c; + + if (cpuset_size == 0) { + cpuset_nentries = CPUSET_NENTRIES(MAXCPUS); + cpuset_size = KCPUSET_SIZE(cpuset_nentries); + } + c = kmem_zalloc(cpuset_size, KM_SLEEP); + c->next = NULL; + c->nused = 1; + return c; +} + +void +kcpuset_destroy(kcpuset_t *c) +{ + kcpuset_t *nc; + + while (c) { + KASSERT(c->nused == 0); + nc = c->next; + kmem_free(c, cpuset_size); + c = nc; + } +} + +void +kcpuset_copy(kcpuset_t *d, const kcpuset_t *s) +{ + + KASSERT(d->nused == 1); + memcpy(d->bits, s->bits, cpuset_nentries * sizeof(s->bits[0])); +} + +void +kcpuset_use(kcpuset_t *c) +{ + + atomic_inc_uint(&c->nused); +} + +void +kcpuset_unuse(kcpuset_t *c, kcpuset_t **lst) +{ + + if (atomic_dec_uint_nv(&c->nused) != 0) + return; + KASSERT(c->nused == 0); + KASSERT(c->next == NULL); + if (lst == NULL) { + kcpuset_destroy(c); + return; + } + c->next = *lst; + *lst = c; +} + +int +kcpuset_copyin(const cpuset_t *u, kcpuset_t *k, size_t len) +{ + + KASSERT(k->nused > 0); + KASSERT(k->next == NULL); + if (len != CPUSET_SIZE(cpuset_nentries)) + return EINVAL; + return copyin(u->bits, k->bits, cpuset_nentries * sizeof(k->bits[0])); +} + +int +kcpuset_copyout(const kcpuset_t *k, cpuset_t *u, size_t len) +{ + + KASSERT(k->nused > 0); + KASSERT(k->next == NULL); + if (len != CPUSET_SIZE(cpuset_nentries)) + return EINVAL; + return copyout(k->bits, u->bits, cpuset_nentries * sizeof(u->bits[0])); +} + +void +kcpuset_zero(kcpuset_t *c) +{ + + KASSERT(c->nused > 0); + KASSERT(c->next == NULL); + memset(c->bits, 0, cpuset_nentries * sizeof(c->bits[0])); +} + +void +kcpuset_fill(kcpuset_t *c) +{ + + KASSERT(c->nused > 0); + KASSERT(c->next == NULL); + memset(c->bits, ~0, cpuset_nentries * sizeof(c->bits[0])); +} + +void +kcpuset_set(cpuid_t i, kcpuset_t *c) +{ + const unsigned long j = i >> CPUSET_SHIFT; + + KASSERT(c->next == NULL); + KASSERT(j < cpuset_nentries); + c->bits[j] |= 1 << (i & CPUSET_MASK); +} + +int +kcpuset_isset(cpuid_t i, const kcpuset_t *c) +{ + const unsigned long j = i >> CPUSET_SHIFT; + + KASSERT(c != NULL); + KASSERT(c->nused > 0); + KASSERT(c->next == NULL); + KASSERT(j < cpuset_nentries); + return ((1 << (i & CPUSET_MASK)) & c->bits[j]) != 0; +} + +bool +kcpuset_iszero(const kcpuset_t *c) +{ + unsigned long j; + + for (j = 0; j < cpuset_nentries; j++) + if (c->bits[j] != 0) + return false; + return true; +} + +bool +kcpuset_match(const kcpuset_t *c1, const kcpuset_t *c2) +{ + unsigned long j; + + for (j = 0; j < cpuset_nentries; j++) + if ((c1->bits[j] & c2->bits[j]) != c2->bits[j]) + return false; + return true; +} + +#endif +#endif diff --git a/lib/nbsd_libc/Makefile b/lib/nbsd_libc/Makefile new file mode 100644 index 000000000..2bce0bba5 --- /dev/null +++ b/lib/nbsd_libc/Makefile @@ -0,0 +1,149 @@ +# $NetBSD: Makefile,v 1.143 2010/09/04 12:17:58 ahoka Exp $ +# @(#)Makefile 8.2 (Berkeley) 2/3/94 +# +# All library objects contain sccsid strings by default; they may be +# excluded as a space-saving measure. To produce a library that does +# not contain these strings, delete -DLIBC_SCCS and -DSYSLIBC_SCCS +# from CPPFLAGS below. To remove these strings from just the system call +# stubs, remove just -DSYSLIBC_SCCS from CPPFLAGS. +# +# The NLS (message catalog) functions are always in libc. To choose that +# strerror(), perror(), strsignal(), psignal(), etc. actually call the NLS +# functions, put -DNLS on the CPPFLAGS line below. +# +# The YP functions are always in libc. To choose that getpwent() and friends +# actually call the YP functions, put -DYP on the CPPFLAGS line below. +# +# The Hesiod functions are always in libc. To choose that getpwent() and friends +# actually call the Hesiod functions, put -DHESIOD on the CPPFLAGS line below. + +.include "Makefile.inc" + +LIB= c +CPPFLAGS+= -I${.CURDIR}/include -I${.CURDIR} + +LIBCDIR= ${.CURDIR} + +.if exists (${ARCHDIR}/Makefile.inc) +.PATH: ${ARCHDIR} +.include "${ARCHDIR}/Makefile.inc" +.endif + +.if exists (${ARCHDIR}/genassym.cf) +DPSRCS+= assym.h +CLEANFILES+= assym.h assym.h.tmp + +assym.h: ${ARCHDIR}/genassym.cf + ${_MKTARGET_CREATE} + ${TOOL_GENASSYM} -- ${CC} ${CFLAGS} \ + ${CPPFLAGS} ${CPPFLAGS.assym.h} ${PROF} \ + < ${ARCHDIR}/genassym.cf > assym.h.tmp && \ + mv -f assym.h.tmp assym.h +.endif + +# The following controls how to build compatibility code for old NetBSD +# binaries. If BUILDCOLD is yes, then we build a separate library; otherwise +# we include the code in libc. +BUILDCOLD?= no +.if "${BUILDCOLD}" == "yes" +SUBDIR=compat +.include +.else +COMPATDIR=${.CURDIR}/compat +.include "${.CURDIR}/compat/Makefile.inc" +.endif + +.include "${.CURDIR}/../../common/lib/libc/Makefile.inc" +.include "${.CURDIR}/atomic/Makefile.inc" +.include "${.CURDIR}/cdb/Makefile.inc" +.include "${.CURDIR}/db/Makefile.inc" +.if (${CITRUS} == "yes") +.include "${.CURDIR}/citrus/Makefile.inc" +.endif +.include "${.CURDIR}/compat-43/Makefile.inc" +.include "${.CURDIR}/dlfcn/Makefile.inc" +.include "${.CURDIR}/gdtoa/Makefile.inc" +.include "${.CURDIR}/gen/Makefile.inc" +.include "${.CURDIR}/gmon/Makefile.inc" +.include "${.CURDIR}/hash/Makefile.inc" +.include "${.CURDIR}/iconv/Makefile.inc" +.include "${.CURDIR}/inet/Makefile.inc" +.include "${.CURDIR}/isc/Makefile.inc" +.include "${.CURDIR}/locale/Makefile.inc" +.include "${.CURDIR}/md/Makefile.inc" +.include "${.CURDIR}/misc/Makefile.inc" +.include "${.CURDIR}/net/Makefile.inc" +.include "${.CURDIR}/nameser/Makefile.inc" +.include "${.CURDIR}/nls/Makefile.inc" +.if (${MACHINE_ARCH} != "alpha") && (${MACHINE_ARCH} != "sparc64") +.include "${.CURDIR}/quad/Makefile.inc" +.endif +.if (${USE_LIBTRE} == "yes") +.include "${NETBSDSRCDIR}/external/bsd/tre/Makefile.inc" +.else +.include "${.CURDIR}/regex/Makefile.inc" +.endif +.include "${.CURDIR}/resolv/Makefile.inc" +.include "${.CURDIR}/rpc/Makefile.inc" +.include "${.CURDIR}/ssp/Makefile.inc" +.include "${.CURDIR}/stdio/Makefile.inc" +.include "${.CURDIR}/stdlib/Makefile.inc" +.include "${.CURDIR}/string/Makefile.inc" +.include "${.CURDIR}/termios/Makefile.inc" +.include "${.CURDIR}/thread-stub/Makefile.inc" +.include "${.CURDIR}/time/Makefile.inc" +.include "${.CURDIR}/sys/Makefile.inc" +.include "${.CURDIR}/uuid/Makefile.inc" +.if (${MKYP} != "no") +.include "${.CURDIR}/yp/Makefile.inc" +.endif + +# Remove from SRCS the .c files for any .S files added by the MD makefiles, +# also remove from SRCS the .c files for the .S and .c files in NO_SRCS. +# Add the .c file for .S files (in both variables) to LSRCS so that the +# 'normal' .c file for assembly files is used for the lint librray. +# +# Usage: +# Add .S files to NO_SRSC when another .S file provides the entry points. +# Add .c files to NO_SRSC when another .c file provides the entry points. +# (lint is run on all .c files in SRCS) + +.for check_file in ${SRCS:M*.S} ${NO_SRCS} +unwanted_file := ${SRCS:M${check_file:.S=.c}} +.if "${unwanted_file}" != "" +SRCS := ${SRCS:N${unwanted_file}} +.if "${unwanted_file}" != "${check_file}" +LSRCS := ${LSRCS} ${unwanted_file} +.endif +.endif +.endfor + +NLS= C.msg Pig.msg ca.msg cs.msg de.msg es.msg fi.msg fr.msg nl.msg \ + no.msg pl.msg sk.msg sv.msg + +realall: tags +tags: ${.OBJDIR}/tags +${.OBJDIR}/tags: ${SRCS} + ${_MKTARGET_CREATE} + -${TOOL_CTAGS} -w ${.ALLSRC:M*.c} + -egrep "^ENTRY(.*)|^FUNC(.*)|^SYSCALL(.*)" /dev/null ${.ALLSRC:M*.S} | \ + sed "s;\([^:]*\):\([^(]*\)(\([^, )]*\)\(.*\);\3 \1 /^\2(\3\4$$/;" \ + >> ${.TARGET}; sort -o ${.TARGET} ${.TARGET} + +FILES= ${.OBJDIR}/tags +FILESNAME= libc.tags +FILESDIR= /var/db + + +# workaround for I18N stuffs: build singlebyte setlocale() for libc.a, +# multibyte for libc.so. the quirk should be removed when we support +# dlopen() from within statically linked binaries. +.if (${CITRUS} == "yes") +CSHLIBFLAGS+= -D_I18N_DYNAMIC +.endif + +.include + +# force the dynamic linker to initialize libc first +SHLIB_SHFLAGS+= -Wl,-z,initfirst + diff --git a/lib/nbsd_libc/Makefile.inc b/lib/nbsd_libc/Makefile.inc new file mode 100644 index 000000000..6837e02c6 --- /dev/null +++ b/lib/nbsd_libc/Makefile.inc @@ -0,0 +1,64 @@ +# $NetBSD: Makefile.inc,v 1.6 2010/05/30 08:28:53 tnozaki Exp $ +# @(#)Makefile 8.2 (Berkeley) 2/3/94 +# +# All library objects contain sccsid strings by default; they may be +# excluded as a space-saving measure. To produce a library that does +# not contain these strings, delete -DLIBC_SCCS and -DSYSLIBC_SCCS +# from CPPFLAGS below. To remove these strings from just the system call +# stubs, remove just -DSYSLIBC_SCCS from CPPFLAGS. +# +# The NLS (message catalog) functions are always in libc. To choose that +# strerror(), perror(), strsignal(), psignal(), etc. actually call the NLS +# functions, put -DNLS on the CPPFLAGS line below. +# +# The YP functions are always in libc. To choose that getpwent() and friends +# actually call the YP functions, put -DYP on the CPPFLAGS line below. +# +# The Hesiod functions are always in libc. To choose that getpwent() and friends +# actually call the Hesiod functions, put -DHESIOD on the CPPFLAGS line below. + +USE_FORT?= yes + +USE_SHLIBDIR= yes + +CITRUS?= yes + +.include + +WARNS=4 +CPPFLAGS+= -D_LIBC -DLIBC_SCCS -DSYSLIBC_SCCS -D_REENTRANT + +.if (${USE_HESIOD} != "no") +CPPFLAGS+= -DHESIOD +.endif + +.if (${USE_INET6} != "no") +CPPFLAGS+= -DINET6 +.endif + +CPPFLAGS+= -DNLS + +.if (${USE_YP} != "no") +CPPFLAGS+= -DYP +.endif + +.if ${MACHINE_ARCH} == "i386" +# Set lint to exit on warnings +LINTFLAGS+= -w +.endif +# ignore 'empty translation unit' warnings. +LINTFLAGS+= -X 272 + +.include "libcincludes.mk" + +ARCHDIR= ${.CURDIR}/arch/${ARCHSUBDIR} +AFLAGS+= -I${ARCHDIR} +.if defined(DESTDIR) +AFLAGS+= -nostdinc -isystem ${DESTDIR}/usr/include +.endif +CLEANFILES+= tags + +# Don't try to lint the C library against itself when creating llib-lc.ln +LLIBS= + +INCSDIR= /usr/include diff --git a/lib/nbsd_libc/arch/alpha/Makefile.inc b/lib/nbsd_libc/arch/alpha/Makefile.inc new file mode 100644 index 000000000..a89768201 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/Makefile.inc @@ -0,0 +1,44 @@ +# $NetBSD: Makefile.inc,v 1.20 2008/10/26 07:43:07 mrg Exp $ + +SRCS+= __longjmp14.c +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +# `source' files built from m4 source +SRCS+= __divqu.S __divq.S __divlu.S __divl.S +SRCS+= __remqu.S __remq.S __remlu.S __reml.S +CLEANFILES+= __divqu.S __divq.S __divlu.S __divl.S +CLEANFILES+= __remqu.S __remq.S __remlu.S __reml.S + +__divqu.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__divqu -DOP=div -DS=false -DWORDSIZE=64 \ + ${.ALLSRC} > ${.TARGET} + +__divq.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__divq -DOP=div -DS=true -DWORDSIZE=64 \ + ${.ALLSRC} > ${.TARGET} + +__divlu.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__divlu -DOP=div -DS=false -DWORDSIZE=32 \ + ${.ALLSRC} > ${.TARGET} + +__divl.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__divl -DOP=div -DS=true -DWORDSIZE=32 \ + ${.ALLSRC} > ${.TARGET} + +__remqu.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__remqu -DOP=rem -DS=false -DWORDSIZE=64 \ + ${.ALLSRC} > ${.TARGET} + +__remq.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__remq -DOP=rem -DS=true -DWORDSIZE=64 \ + ${.ALLSRC} > ${.TARGET} + +__remlu.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__remlu -DOP=rem -DS=false -DWORDSIZE=32 \ + ${.ALLSRC} > ${.TARGET} + +__reml.S: ${LIBCDIR}/arch/alpha/gen/divrem.m4 + ${TOOL_M4} -DNAME=__reml -DOP=rem -DS=true -DWORDSIZE=32 \ + ${.ALLSRC} > ${.TARGET} + +CPPFLAGS+= -I. diff --git a/lib/nbsd_libc/arch/alpha/SYS.h b/lib/nbsd_libc/arch/alpha/SYS.h new file mode 100644 index 000000000..2cd48391a --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/SYS.h @@ -0,0 +1,76 @@ +/* $NetBSD: SYS.h,v 1.8 2002/01/14 00:55:55 thorpej Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include +#include + + +#define CALLSYS_ERROR(name) \ + CALLSYS_NOERROR(name); \ + br gp, LLABEL(name,0); \ +LLABEL(name,0): \ + LDGP(gp); \ + beq a3, LLABEL(name,1); \ + jmp zero, __cerror; \ +LLABEL(name,1): + + +#define SYSCALL(name) \ +LEAF(name,0); /* XXX # of args? */ \ + CALLSYS_ERROR(name) + +#define SYSCALL_NOERROR(name) \ +LEAF(name,0); /* XXX # of args? */ \ + CALLSYS_NOERROR(name) + +#define PSEUDO(label,name) \ +LEAF(label,0); /* XXX # of args? */ \ + CALLSYS_ERROR(name); \ + RET; \ +END(label); + +#define PSEUDO_NOERROR(label,name) \ +LEAF(label,0); /* XXX # of args? */ \ + CALLSYS_NOERROR(name); \ + RET; \ +END(label); + +#define RSYSCALL(name) \ + SYSCALL(name); \ + RET; \ +END(name) + +#define RSYSCALL_NOERROR(name) \ + SYSCALL_NOERROR(name); \ + RET; \ +END(name) + +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) diff --git a/lib/nbsd_libc/arch/alpha/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/alpha/gdtoa/Makefile.inc new file mode 100644 index 000000000..1d786e010 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/alpha/gdtoa/arith.h b/lib/nbsd_libc/arch/alpha/gdtoa/arith.h new file mode 100644 index 000000000..650a936eb --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gdtoa/arith.h @@ -0,0 +1,6 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_LITTLE_ENDIAN +#ifndef _IEEE_FP +#define Sudden_Underflow +#endif diff --git a/lib/nbsd_libc/arch/alpha/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/alpha/gdtoa/gd_qnan.h new file mode 100644 index 000000000..36cb4afa4 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gdtoa/gd_qnan.h @@ -0,0 +1,5 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff80000 diff --git a/lib/nbsd_libc/arch/alpha/gen/Makefile.inc b/lib/nbsd_libc/arch/alpha/gen/Makefile.inc new file mode 100644 index 000000000..c3f59d946 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/Makefile.inc @@ -0,0 +1,28 @@ +# $NetBSD: Makefile.inc,v 1.22 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= fabs.S + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_dbl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +SRCS+= flt_rounds.c fpgetmask.c fpgetround.c fpgetsticky.c fpsetmask.c \ + fpsetround.c fpsetsticky.c +SRCS+= __setjmp14.S +SRCS+= __sigsetjmp14.S +SRCS+= byte_swap_2.S byte_swap_4.S bswap64.c +SRCS+= _resumecontext.S + +LSRCS.alpha.gen= Lint_bswap16.c Lint_bswap32.c Lint_swapcontext.c +LSRCS+= ${LSRCS.alpha.gen} +DPSRCS+= ${LSRCS.alpha.gen} +CLEANFILES+= ${LSRCS.alpha.gen} + +SRCS+= makecontext.c resumecontext.c swapcontext.S _lwp.c + diff --git a/lib/nbsd_libc/arch/alpha/gen/__longjmp14.c b/lib/nbsd_libc/arch/alpha/gen/__longjmp14.c new file mode 100644 index 000000000..af70b735e --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/__longjmp14.c @@ -0,0 +1,99 @@ +/* $NetBSD: __longjmp14.c,v 1.5 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christian Limpach and Matt Thomas. + * + * 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 "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include + +void +__longjmp14(jmp_buf env, int val) +{ + struct sigcontext *sc = (void *)env; + ucontext_t uc; + + /* Ensure non-zero SP */ + if (sc->sc_sp == 0 || sc->sc_regs[R_ZERO] != 0xacedbade) + goto err; + + /* Ensure non-zero return value */ + if (val == 0) + val = -1; + + /* Set _UC_SIGMASK and _UC_CPU */ + uc.uc_flags = _UC_SIGMASK | _UC_CPU; + + /* Clear uc_link */ + uc.uc_link = 0; + + /* Save return value in context */ + uc.uc_mcontext.__gregs[_REG_V0] = val; + + /* Copy saved registers */ + uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[R_S0]; + uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[R_S1]; + uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[R_S2]; + uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[R_S3]; + uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[R_S4]; + uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[R_S5]; + uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[R_S6]; + uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[R_RA]; + uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp; + uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc; + uc.uc_mcontext.__gregs[_REG_PS] = + (sc->sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR; + + /* Copy FP state */ + if (sc->sc_ownedfp) { + memcpy(&uc.uc_mcontext.__fpregs.__fp_fr, + &sc->sc_fpregs, 31 * sizeof(unsigned long)); + uc.uc_mcontext.__fpregs.__fp_fpcr = sc->sc_fpcr; + /* XXX sc_fp_control */ + uc.uc_flags |= _UC_FPU; + } + + /* Copy signal mask */ + uc.uc_sigmask = sc->sc_mask; + + setcontext(&uc); + err: + longjmperror(); + abort(); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/alpha/gen/__setjmp14.S b/lib/nbsd_libc/arch/alpha/gen/__setjmp14.S new file mode 100644 index 000000000..4e59a7b64 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/__setjmp14.S @@ -0,0 +1,118 @@ +/* $NetBSD: __setjmp14.S,v 1.4 2004/03/21 18:04:27 matt Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include +#include "assym.h" + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and the previous signal state. + */ + + .set noreorder + +LEAF(__setjmp14, 1) + LDGP(pv) + stq ra, SC_PC(a0) /* sc_pc = return address */ + stq s0, (SC_REGS+_REG_S0)(a0) /* saved bits of sc_regs */ + stq s1, (SC_REGS+_REG_S1)(a0) + stq s2, (SC_REGS+_REG_S2)(a0) + stq s3, (SC_REGS+_REG_S3)(a0) + stq s4, (SC_REGS+_REG_S4)(a0) + stq s5, (SC_REGS+_REG_S5)(a0) + stq s6, (SC_REGS+_REG_S6)(a0) + stq ra, (SC_REGS+_REG_RA)(a0) + stq sp, (SC_REGS+_REG_SP)(a0) + + /* + * get signal information + */ + mov a0, s0 /* squirrel away ptr to sc */ + + /* see what's blocked */ + mov zero, a0 /* how (insignificant) */ + mov zero, a1 /* set (NULL) */ + lda a2, SC_MASK(s0) /* point to mask in sc */ + CALL(__sigprocmask14) + + lda sp, -24(sp) /* sizeof struct sigaltstack */ + mov zero, a0 + mov sp, a1 + CALL(__sigaltstack14) + ldl t0, 16(sp) /* offset of ss_flags */ + lda sp, 24(sp) /* sizeof struct sigaltstack */ + ldq ra, (SC_REGS+_REG_RA)(s0) /* restore return address */ + blt v0, botch /* check for error */ + and t0, 0x1, t0 /* get SA_ONSTACK flag */ + stq t0, SC_ONSTACK(s0) /* and save it in sc_onstack */ + /* + * Restore old s0 and a0, and continue saving registers + */ + mov s0, a0 + ldq s0, (SC_REGS+_REG_S0)(a0) + + ldiq t0, 0xacedbade /* sigcontext magic number */ + stq t0, (SC_REGS+_REG_UNIQUE)(a0) /* magic in sc_regs[31] */ + /* Too bad we can't check if we actually used FP */ + ldiq t0, 1 + stq t0, SC_OWNEDFP(a0) /* say we've used FP. */ + stt fs0, (2*8 + SC_FPREGS)(a0) /* saved bits of sc_fpregs */ + stt fs1, (3*8 + SC_FPREGS)(a0) + stt fs2, (4*8 + SC_FPREGS)(a0) + stt fs3, (5*8 + SC_FPREGS)(a0) + stt fs4, (6*8 + SC_FPREGS)(a0) + stt fs5, (7*8 + SC_FPREGS)(a0) + stt fs6, (8*8 + SC_FPREGS)(a0) + stt fs7, (9*8 + SC_FPREGS)(a0) + mf_fpcr ft0 /* get FP control reg */ + stt ft0, SC_FPCR(a0) /* and store it in sc_fpcr */ + stq zero, SC_FP_CONTROL(a0) /* FP software control XXX */ + stq zero, (SC_RESERVED + 0*8)(a0) /* sc_reserved[0] */ + stq zero, (SC_RESERVED + 1*8)(a0) /* sc_reserved[1] */ + stq zero, (SC_XXX + 0*8)(a0) /* sc_xxx[0] */ + stq zero, (SC_XXX + 1*8)(a0) /* sc_xxx[1] */ + stq zero, (SC_XXX + 2*8)(a0) /* sc_xxx[2] */ + stq zero, (SC_XXX + 3*8)(a0) /* sc_xxx[3] */ + stq zero, (SC_XXX + 4*8)(a0) /* sc_xxx[4] */ + stq zero, (SC_XXX + 5*8)(a0) /* sc_xxx[5] */ + stq zero, (SC_XXX + 6*8)(a0) /* sc_xxx[6] */ + stq zero, (SC_XXX + 7*8)(a0) /* sc_xxx[7] */ + + mov zero, v0 /* return zero */ + RET +botch: + CALL(abort) + RET /* "can't" get here... */ +END(__setjmp14) diff --git a/lib/nbsd_libc/arch/alpha/gen/__sigsetjmp14.S b/lib/nbsd_libc/arch/alpha/gen/__sigsetjmp14.S new file mode 100644 index 000000000..4460f7e99 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/__sigsetjmp14.S @@ -0,0 +1,62 @@ +/* $NetBSD: __sigsetjmp14.S,v 1.2 2000/06/14 06:49:01 cgd Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a, mask) + * by restoring registers from the stack. + * If `mask' is non-zero, the previous signal + * state will be restored. + */ + + .set noreorder + +LEAF(__sigsetjmp14, 2) + LDGP(pv) + stq a1, (83 * 8)(a0) /* save the mask */ + bne a1, Lsavesig /* if !zero, save signals */ + jmp zero, _setjmp /* else don't. */ +Lsavesig: + jmp zero, __setjmp14 +END(__sigsetjmp14) + +LEAF(__siglongjmp14, 2) + LDGP(pv) + ldq t0, (83 * 8)(a0) /* get the mask */ + bne t0, Lrestoresig /* if !zero, restore signals */ + jmp zero, _longjmp +Lrestoresig: + jmp zero, __longjmp14 +END(__siglongjmp14) diff --git a/lib/nbsd_libc/arch/alpha/gen/_lwp.c b/lib/nbsd_libc/arch/alpha/gen/_lwp.c new file mode 100644 index 000000000..21213682b --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/_lwp.c @@ -0,0 +1,63 @@ +/* $NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nathan J. Williams. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:55 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + __greg_t *gr; + + getcontext(u); + gr = u->uc_mcontext.__gregs; + + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + gr[_REG_PC] = (unsigned long)start; + gr[_REG_T12] = (unsigned long) start; + gr[_REG_RA] = (unsigned long) _lwp_exit; + gr[_REG_A0] = (unsigned long) arg; + gr[_REG_SP] = ((unsigned long) (stack_base + stack_size)) & ~0x7; + gr[_REG_S6] = 0; +} diff --git a/lib/nbsd_libc/arch/alpha/gen/_resumecontext.S b/lib/nbsd_libc/arch/alpha/gen/_resumecontext.S new file mode 100644 index 000000000..8babf1596 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/_resumecontext.S @@ -0,0 +1,35 @@ +/* $NetBSD: _resumecontext.S,v 1.3 2008/04/22 04:47:44 dogcow Exp $ */ + +/* + * Copyright (c) 2004 + * Matthias Drochner. 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 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 + +LEAF_NOPROFILE(__resumecontext, 0) + LDGP(ra) + CALL(_resumecontext) +END(__resumecontext) + diff --git a/lib/nbsd_libc/arch/alpha/gen/divrem.m4 b/lib/nbsd_libc/arch/alpha/gen/divrem.m4 new file mode 100644 index 000000000..d8a1d2bd0 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/divrem.m4 @@ -0,0 +1,197 @@ +/* $NetBSD: divrem.m4,v 1.9 2002/01/21 23:40:41 ross Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +/* + * Division and remainder. + * + * The use of m4 is modeled after the sparc code, but the algorithm is + * simple binary long division. + * + * Note that the loops could probably benefit from unrolling. + */ + +/* + * M4 Parameters + * NAME name of function to generate + * OP OP=div: t10 / t11 -> t12; OP=rem: t10 % t11 -> t12 + * S S=true: signed; S=false: unsigned + * WORDSIZE total number of bits + */ + +define(A, `t10') +define(B, `t11') +define(RESULT, `t12') + +define(BIT, `t0') +define(I, `t1') +define(CC, `t2') +define(T_0, `t3') +ifelse(S, `true', `define(NEG, `t4')') + +#include + +LEAF(NAME, 0) /* XXX */ + lda sp, -64(sp) + stq BIT, 0(sp) + stq I, 8(sp) + stq CC, 16(sp) + stq T_0, 24(sp) +ifelse(S, `true', +` stq NEG, 32(sp)') + stq A, 40(sp) + stq B, 48(sp) + mov zero, RESULT /* Initialize result to zero */ + +ifelse(S, `true', +` + /* Compute sign of result. If either is negative, this is easy. */ + or A, B, NEG /* not the sign, but... */ + srl NEG, WORDSIZE - 1, NEG /* rather, or of high bits */ + blbc NEG, Ldoit /* neither negative? do it! */ + +ifelse(OP, `div', +` xor A, B, NEG /* THIS is the sign! */ +', ` mov A, NEG /* sign follows A. */ +') + srl NEG, WORDSIZE - 1, NEG /* make negation the low bit. */ + + srl A, WORDSIZE - 1, I /* is A negative? */ + blbc I, LnegB /* no. */ + /* A is negative; flip it. */ +ifelse(WORDSIZE, `32', ` + /* top 32 bits may be random junk */ + zap A, 0xf0, A +') + subq zero, A, A + srl B, WORDSIZE - 1, I /* is B negative? */ + blbc I, Ldoit /* no. */ +LnegB: + /* B is definitely negative, no matter how we got here. */ +ifelse(WORDSIZE, `32', ` + /* top 32 bits may be random junk */ + zap B, 0xf0, B +') + subq zero, B, B +Ldoit: +') +ifelse(WORDSIZE, `32', ` + /* + * Clear the top 32 bits of each operand, as they may + * sign extension (if negated above), or random junk. + */ + zap A, 0xf0, A + zap B, 0xf0, B +') + + /* kill the special cases. */ + beq B, Ldotrap /* division by zero! */ + + cmpult A, B, CC /* A < B? */ + /* RESULT is already zero, from above. A is untouched. */ + bne CC, Lret_result + + cmpeq A, B, CC /* A == B? */ + cmovne CC, 1, RESULT + cmovne CC, zero, A + bne CC, Lret_result + + /* + * Find out how many bits of zeros are at the beginning of the divisor. + */ +LBbits: + ldiq T_0, 1 /* I = 0; BIT = 1< + +LEAF(fabs, 1) + cpys fzero, fa0, fv0 + RET +END(fabs) diff --git a/lib/nbsd_libc/arch/alpha/gen/flt_rounds.c b/lib/nbsd_libc/arch/alpha/gen/flt_rounds.c new file mode 100644 index 000000000..ef06c6c36 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/flt_rounds.c @@ -0,0 +1,64 @@ +/* $NetBSD: flt_rounds.c,v 1.8 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Copyright (c) 1995 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.8 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const int map[] = { + 0, /* round to zero */ + 3, /* round to negative infinity */ + 1, /* round to nearest */ + 2 /* round to positive infinity */ +}; + +int +__flt_rounds() +{ + double fpcrval; + u_int64_t old; + + __asm("trapb"); + __asm("mf_fpcr %0" : "=f" (fpcrval)); + __asm("trapb"); + old = *(u_int64_t *)(void *)&fpcrval; + + return map[(old >> 58) & 0x3]; +} diff --git a/lib/nbsd_libc/arch/alpha/gen/fpgetmask.c b/lib/nbsd_libc/arch/alpha/gen/fpgetmask.c new file mode 100644 index 000000000..b0fd56bd3 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/fpgetmask.c @@ -0,0 +1,52 @@ +/* $NetBSD: fpgetmask.c,v 1.5 2005/06/12 05:21:25 lukem Exp $ */ + +/* + * Copyright (c) 1999 Ross Harvey + * 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 by Ross Harvey + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetmask.c,v 1.5 2005/06/12 05:21:25 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetmask,_fpgetmask) +#endif + +fp_except +fpgetmask __P((void)) +{ + return sysarch(ALPHA_FPGETMASK, 0L); +} diff --git a/lib/nbsd_libc/arch/alpha/gen/fpgetround.c b/lib/nbsd_libc/arch/alpha/gen/fpgetround.c new file mode 100644 index 000000000..1bee9b690 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/fpgetround.c @@ -0,0 +1,61 @@ +/* $NetBSD: fpgetround.c,v 1.9 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Copyright (c) 1995 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.9 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetround,_fpgetround) +#endif + +fp_rnd +fpgetround() +{ + double fpcrval; + u_int64_t old; + + __asm("mf_fpcr %0" : "=f" (fpcrval)); + old = *(u_int64_t *)(void *)&fpcrval; + + return ((old >> 58) & 0x3); +} diff --git a/lib/nbsd_libc/arch/alpha/gen/fpgetsticky.c b/lib/nbsd_libc/arch/alpha/gen/fpgetsticky.c new file mode 100644 index 000000000..55d7e660a --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/fpgetsticky.c @@ -0,0 +1,54 @@ +/* $NetBSD: fpgetsticky.c,v 1.9 2008/05/10 15:31:04 martin Exp $ */ + +/*- + * Copyright (c) 2001 Ross Harvey + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetsticky.c,v 1.9 2008/05/10 15:31:04 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetsticky,_fpgetsticky) +#endif + +fp_except +fpgetsticky(void) +{ + return sysarch(ALPHA_FPGETSTICKY, 0L); +} diff --git a/lib/nbsd_libc/arch/alpha/gen/fpsetmask.c b/lib/nbsd_libc/arch/alpha/gen/fpsetmask.c new file mode 100644 index 000000000..55d6dc119 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/fpsetmask.c @@ -0,0 +1,56 @@ +/* $NetBSD: fpsetmask.c,v 1.4 2005/06/12 05:21:25 lukem Exp $ */ + +/* + * Copyright (c) 1995 Ross Harvey + * 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 by Ross Harvey + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetmask.c,v 1.4 2005/06/12 05:21:25 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetmask,_fpsetmask) +#endif + +fp_except +fpsetmask(mask) + fp_except mask; +{ + struct alpha_fp_except_args a; + + a.mask = mask; + return sysarch(ALPHA_FPSETMASK, &a); +} diff --git a/lib/nbsd_libc/arch/alpha/gen/fpsetround.c b/lib/nbsd_libc/arch/alpha/gen/fpsetround.c new file mode 100644 index 000000000..b3cb5a5a3 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/fpsetround.c @@ -0,0 +1,70 @@ +/* $NetBSD: fpsetround.c,v 1.10 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Copyright (c) 1995 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.10 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetround,_fpsetround) +#endif + +fp_rnd +fpsetround(rnd_dir) + fp_rnd rnd_dir; +{ + double fpcrval; + u_int64_t old, new; + + __asm("excb"); + __asm("mf_fpcr %0" : "=f" (fpcrval)); + old = *(u_int64_t *)(void *)&fpcrval; + + new = old & ~(long)0x0c00000000000000; + new = (long)rnd_dir << 58; + *(u_int64_t *)(void *)&fpcrval = new; + + __asm("mt_fpcr %0" : : "f" (fpcrval)); + __asm("excb"); + + return ((old >> 58) & 0x3); +} diff --git a/lib/nbsd_libc/arch/alpha/gen/fpsetsticky.c b/lib/nbsd_libc/arch/alpha/gen/fpsetsticky.c new file mode 100644 index 000000000..830683474 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/fpsetsticky.c @@ -0,0 +1,56 @@ +/* $NetBSD: fpsetsticky.c,v 1.4 2005/06/12 05:21:25 lukem Exp $ */ + +/* + * Copyright (c) 1999 Ross Harvey + * 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 by Ross Harvey + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetsticky.c,v 1.4 2005/06/12 05:21:25 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetsticky,_fpsetsticky) +#endif + +fp_except +fpsetsticky(sticky) + fp_except sticky; +{ + struct alpha_fp_except_args a; + + a.mask = sticky; + return sysarch(ALPHA_FPSETSTICKY, &a); +} diff --git a/lib/nbsd_libc/arch/alpha/gen/makecontext.c b/lib/nbsd_libc/arch/alpha/gen/makecontext.c new file mode 100644 index 000000000..3554eb3a7 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/makecontext.c @@ -0,0 +1,74 @@ +/* $NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:55 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + int i; + unsigned long *sp; + va_list ap; + + void __resumecontext(void); + + /* Compute and align stack pointer. */ + sp = (unsigned long *) + (((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) & ~0x7); + /* Allocate necessary stack space for arguments exceeding a0-5. */ + if (argc > 6) + sp -= (argc - 6); + gr[_REG_SP] = (__greg_t)sp; + /* Arrange for return via the trampoline code. */ + gr[_REG_RA] = (__greg_t)__resumecontext; + gr[_REG_PC] = (__greg_t)func; + gr[_REG_PV] = (__greg_t)func; + + va_start(ap, argc); + /* Pass up to six arguments in a0-5. */ + for (i = 0; i < argc && i < 6; i++) + gr[_REG_A0 + i] = va_arg(ap, unsigned long); + /* Pass any additional arguments on the stack. */ + for (argc -= i; argc > 0; argc--) + *sp++ = va_arg(ap, unsigned long); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/alpha/gen/nanf.c b/lib/nbsd_libc/arch/alpha/gen/nanf.c new file mode 100644 index 000000000..7a5b3a2f0 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.5 2009/02/22 01:34:01 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.5 2009/02/22 01:34:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0, 0, 0xc0, 0x7f } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/alpha/gen/resumecontext.c b/lib/nbsd_libc/arch/alpha/gen/resumecontext.c new file mode 100644 index 000000000..cd1795b59 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/resumecontext.c @@ -0,0 +1,60 @@ +/* $NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:55 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (!uct.uc_link) { + exit(0); + /* NOTREACHED */ + } + + (void)setcontext(uct.uc_link); + /* NOTREACHED */ + + /* something is wrong, pull the brake */ + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/alpha/gen/swapcontext.S b/lib/nbsd_libc/arch/alpha/gen/swapcontext.S new file mode 100644 index 000000000..2a91164c1 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gen/swapcontext.S @@ -0,0 +1,55 @@ +/* $NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +NESTED(swapcontext, 2, 24, ra, IM_RA|IM_A0|IM_A1, 0) + LDGP(pv) + lda sp, -24(sp) + stq ra, (24- 8)(sp) /* must save ra, oucp, ucp */ + stq a0, (24-16)(sp) + stq a1, (24-24)(sp) + CALL(_getcontext) /* getcontext(oucp) */ + ldq t0, (24-16)(sp) + bne v0, Lerr + ldq t1, (24- 8)(sp) + stq t1, (56 + 26 * 8)(t0) /* Adjust saved RA */ + stq t1, (56 + 32 * 8)(t0) /* Adjust saved PC */ + lda t1, 24(sp) + stq t1, (56 + 30 * 8)(t0) /* Adjust saved SP */ + + ldq a0, (24-24)(sp) + CALL(setcontext) /* setcontext(ucp) */ +Lerr: + ldq ra, (24- 8)(sp) + lda sp, 24(sp) + RET +END(swapcontext) diff --git a/lib/nbsd_libc/arch/alpha/genassym.cf b/lib/nbsd_libc/arch/alpha/genassym.cf new file mode 100644 index 000000000..64d4ed09a --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/genassym.cf @@ -0,0 +1,84 @@ +# $NetBSD: genassym.cf,v 1.2 2008/04/28 20:22:55 martin Exp $ + +# +# Copyright (c) 2001 The NetBSD Foundation, Inc. +# All rights reserved. +# +# This code is derived from software contributed to The NetBSD Foundation +# by Matt Thomas . +# +# 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 + +define _REG_V0 8*_REG_V0 /* 0 */ +define _REG_T0 8*_REG_T0 /* 1 */ +define _REG_T1 8*_REG_T1 /* 2 */ +define _REG_T2 8*_REG_T2 /* 3 */ +define _REG_T3 8*_REG_T3 /* 4 */ +define _REG_T4 8*_REG_T4 /* 5 */ +define _REG_T5 8*_REG_T5 /* 6 */ +define _REG_T6 8*_REG_T6 /* 7 */ +define _REG_T7 8*_REG_T7 /* 8 */ +define _REG_S0 8*_REG_S0 /* 9 */ +define _REG_S1 8*_REG_S1 /* 10 */ +define _REG_S2 8*_REG_S2 /* 11 */ +define _REG_S3 8*_REG_S3 /* 12 */ +define _REG_S4 8*_REG_S4 /* 13 */ +define _REG_S5 8*_REG_S5 /* 14 */ +define _REG_S6 8*_REG_S6 /* 15 */ +define _REG_A0 8*_REG_A0 /* 16 */ +define _REG_A1 8*_REG_A1 /* 17 */ +define _REG_A2 8*_REG_A2 /* 18 */ +define _REG_A3 8*_REG_A3 /* 19 */ +define _REG_A4 8*_REG_A4 /* 20 */ +define _REG_A5 8*_REG_A5 /* 21 */ +define _REG_T8 8*_REG_T8 /* 22 */ +define _REG_T9 8*_REG_T9 /* 23 */ +define _REG_T10 8*_REG_T10 /* 24 */ +define _REG_T11 8*_REG_T11 /* 25 */ +define _REG_RA 8*_REG_RA /* 26 */ +define _REG_T12 8*_REG_T12 /* 27 */ +define _REG_PV 8*_REG_PV /* 27 */ +define _REG_AT 8*_REG_AT /* 28 */ +define _REG_GP 8*_REG_GP /* 29 */ +define _REG_SP 8*_REG_SP /* 30 */ +define _REG_UNIQUE 8*_REG_UNIQUE /* 31 */ +define _REG_PC 8*_REG_PC /* 32 */ +define _REG_PS 8*_REG_PS /* 33 */ + +define SC_ONSTACK offsetof(struct sigcontext, sc_onstack) +define SC_SP offsetof(struct sigcontext, sc_sp) +define SC_PC offsetof(struct sigcontext, sc_pc) +define SC_PS offsetof(struct sigcontext, sc_ps) +define SC_REGS offsetof(struct sigcontext, sc_regs) +define SC_OWNEDFP offsetof(struct sigcontext, sc_regs) +define SC_FPREGS offsetof(struct sigcontext, sc_fpregs) +define SC_FPCR offsetof(struct sigcontext, sc_fpcr) +define SC_FP_CONTROL offsetof(struct sigcontext, sc_fp_control) +define SC_MASK offsetof(struct sigcontext, sc_mask) +define SC_RESERVED offsetof(struct sigcontext, sc_reserved) +define SC_XXX offsetof(struct sigcontext, sc_xxx) diff --git a/lib/nbsd_libc/arch/alpha/gmon/Makefile.inc b/lib/nbsd_libc/arch/alpha/gmon/Makefile.inc new file mode 100644 index 000000000..35d0bfd2a --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/gmon/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2000/09/05 16:28:31 thorpej Exp $ + +SRCS+= _mcount.S diff --git a/lib/nbsd_libc/arch/alpha/net/Makefile.inc b/lib/nbsd_libc/arch/alpha/net/Makefile.inc new file mode 100644 index 000000000..9f1a4381e --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/net/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/12/06 07:12:17 uebayasi Exp $ + +# objects built from assembler sources (need lint stubs) +# hton* and nto* functions provided by ../gen/byte_swap_*.S +SRCS+= + +LSRCS.alpha.net= Lint_htonl.c Lint_htons.c Lint_ntohl.c Lint_ntohs.c +LSRCS+= ${LSRCS.alpha.net} +DPSRCS+= ${LSRCS.alpha.net} +CLEANFILES+= ${LSRCS.alpha.net} diff --git a/lib/nbsd_libc/arch/alpha/stdlib/Makefile.inc b/lib/nbsd_libc/arch/alpha/stdlib/Makefile.inc new file mode 100644 index 000000000..0dbbca485 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/31 20:39:59 dsl Exp $ diff --git a/lib/nbsd_libc/arch/alpha/string/Makefile.inc b/lib/nbsd_libc/arch/alpha/string/Makefile.inc new file mode 100644 index 000000000..d7c479478 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/string/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.6 2009/07/30 20:57:15 dsl Exp $ + +SRCS+= bcopy.S bzero.S ffs.S + +SRCS+= memcpy.S memmove.S diff --git a/lib/nbsd_libc/arch/alpha/sys/__clone.S b/lib/nbsd_libc/arch/alpha/sys/__clone.S new file mode 100644 index 000000000..824d1c967 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/__clone.S @@ -0,0 +1,91 @@ +/* $NetBSD: __clone.S,v 1.3 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +LEAF(__clone, 4) + br pv, 1f +1: LDGP(pv) + + /* + * Sanity checks: func and stack may not be NULL. + */ + beq a0, 9f + beq a1, 9f + + /* + * We need to be able to get at the func and arg arguments + * in the child. Luckily, we have a convenient place to + * do this; the child's stack. + */ + subq a1, 16, a1 + stq a0, 0(a1) + stq a3, 8(a1) + + /* + * The system call expects (flags, stack). + */ + mov a2, a0 + CALLSYS_ERROR(__clone) + + beq a4, 8f /* a4 (rv[1]) == 0, parent, child pid in v0 */ + + /* + * Fetch the function and argument from the new stack, and + * pop it back. + */ + ldq pv, 0(sp) + ldq a0, 8(sp) + addq sp, 16, sp + + /* Call the clone's entry point. */ + CALL((pv)) + + /* Pass return value to _exit(). */ + mov v0, a0 + CALL(_exit) + + /* NOTREACHED */ + +8: RET + +9: ldiq v0, EINVAL + jmp zero, __cerror + END(__clone) diff --git a/lib/nbsd_libc/arch/alpha/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/alpha/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..77d089609 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/__sigaction14_sigtramp.c @@ -0,0 +1,74 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.7 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + extern const int __sigtramp_sigcontext_1[]; + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specified or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/alpha/sys/__sigtramp2.S b/lib/nbsd_libc/arch/alpha/sys/__sigtramp2.S new file mode 100644 index 000000000..4c94fb2fd --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/__sigtramp2.S @@ -0,0 +1,47 @@ +/* $NetBSD: __sigtramp2.S,v 1.2 2006/11/08 21:16:40 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +/* + * The Alpha signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, the stack looks like: + * + * ucontext structure [128] == sp + sizeof(siginfo_t)] + * sp-> siginfo structure [0] + */ +NESTED_NOPROFILE(__sigtramp_siginfo_2,0,0,ra,0,0) + ldgp gp,0(ra) + lda a0,(128)(sp) /* get pointer to ucontext */ + CALLSYS_NOERROR(setcontext) /* and call setcontext() with it */ + ldiq a0,-1 /* if that failed, set an exit code */ + CALLSYS_NOERROR(exit) /* and call exit() */ +END(__sigtramp_siginfo_2) diff --git a/lib/nbsd_libc/arch/alpha/sys/__syscall.S b/lib/nbsd_libc/arch/alpha/sys/__syscall.S new file mode 100644 index 000000000..ca2b60017 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/__syscall.S @@ -0,0 +1,32 @@ +/* $NetBSD: __syscall.S,v 1.1 2000/12/13 07:49:17 mycroft Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/alpha/sys/__vfork14.S b/lib/nbsd_libc/arch/alpha/sys/__vfork14.S new file mode 100644 index 000000000..2b4022c3c --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/__vfork14.S @@ -0,0 +1,35 @@ +/* $NetBSD: __vfork14.S,v 1.2 2000/06/14 06:49:03 cgd Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +SYSCALL(__vfork14) + cmovne a4, zero, v0 /* a4 (rv[1]) != 0, child */ + RET +END(__vfork14) diff --git a/lib/nbsd_libc/arch/alpha/sys/brk.S b/lib/nbsd_libc/arch/alpha/sys/brk.S new file mode 100644 index 000000000..032640f65 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/brk.S @@ -0,0 +1,54 @@ +/* $NetBSD: brk.S,v 1.9 2001/05/07 17:19:17 kleink Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + + .globl _end +IMPORT(__curbrk, 8) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data +EXPORT(__minbrk) + .quad _end + + .text +LEAF(_brk, 1) + br pv, L1 /* XXX profiling */ +L1: LDGP(pv) + ldq v0, __minbrk + cmpult a0, v0, t0 + cmovne t0, v0, a0 + CALLSYS_ERROR(break) + stq a0, __curbrk + mov zero, v0 + RET +END(_brk) diff --git a/lib/nbsd_libc/arch/alpha/sys/cerror.S b/lib/nbsd_libc/arch/alpha/sys/cerror.S new file mode 100644 index 000000000..18788635b --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/cerror.S @@ -0,0 +1,65 @@ +/* $NetBSD: cerror.S,v 1.6 2000/06/14 06:49:03 cgd Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +#ifdef _REENTRANT +#define FRAME_SIZE 16 +#define FRAME_RA_OFFSET 0 +#define FRAME_V0_OFFSET 8 +#endif + +#ifdef _REENTRANT +NESTED(__cerror, 0, FRAME_SIZE, ra, IM_RA|IM_V0, 0) +#else +LEAF(__cerror, 0) +#endif + br t0, L1 +L1: LDGP(t0) + +#ifdef _REENTRANT + lda sp, -FRAME_SIZE(sp) + stq ra, FRAME_RA_OFFSET(sp) + stq v0, FRAME_V0_OFFSET(sp) + + CALL(__errno) + + ldq t0, FRAME_V0_OFFSET(sp) + stl t0, 0(v0) +#else + stl v0, errno +#endif + + ldiq v0, -1 +#ifdef _REENTRANT + ldq ra, FRAME_RA_OFFSET(sp) + lda sp, FRAME_SIZE(sp) +#endif + RET +END(__cerror) diff --git a/lib/nbsd_libc/arch/alpha/sys/exect.S b/lib/nbsd_libc/arch/alpha/sys/exect.S new file mode 100644 index 000000000..57db0fa0f --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/exect.S @@ -0,0 +1,35 @@ +/* $NetBSD: exect.S,v 1.3 2000/06/14 06:49:03 cgd Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +LEAF(exect, 3) + CALLSYS_ERROR(execve) + RET +END(exect) diff --git a/lib/nbsd_libc/arch/alpha/sys/fork.S b/lib/nbsd_libc/arch/alpha/sys/fork.S new file mode 100644 index 000000000..2b93d7bea --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/fork.S @@ -0,0 +1,36 @@ +/* $NetBSD: fork.S,v 1.5 2003/02/13 02:50:48 nathanw Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +LEAF(__fork, 0) + CALLSYS_ERROR(fork) + cmovne a4, zero, v0 /* a4 (rv[1]) != 0, child */ + RET +END(__fork) diff --git a/lib/nbsd_libc/arch/alpha/sys/getcontext.S b/lib/nbsd_libc/arch/alpha/sys/getcontext.S new file mode 100644 index 000000000..b97395745 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/getcontext.S @@ -0,0 +1,43 @@ +/* $NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +LEAF(_getcontext, 1) + CALLSYS_ERROR(getcontext) + stq ra, (56 + 32 * 8)(a0) /* Adjust saved PC */ + stq zero, (56 + 0 * 8)(a0) /* Arrange for a return value of 0 */ + RET +END(_getcontext) diff --git a/lib/nbsd_libc/arch/alpha/sys/pipe.S b/lib/nbsd_libc/arch/alpha/sys/pipe.S new file mode 100644 index 000000000..cddc812c5 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/pipe.S @@ -0,0 +1,42 @@ +/* $NetBSD: pipe.S,v 1.4 2001/05/07 17:19:18 kleink Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +LEAF(_pipe, 0) + CALLSYS_ERROR(pipe) + stl v0, 0(a0) + stl a4, 4(a0) + mov zero, v0 + RET +END(_pipe) diff --git a/lib/nbsd_libc/arch/alpha/sys/ptrace.S b/lib/nbsd_libc/arch/alpha/sys/ptrace.S new file mode 100644 index 000000000..46eff5b7f --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/ptrace.S @@ -0,0 +1,37 @@ +/* $NetBSD: ptrace.S,v 1.5 2000/06/14 06:49:03 cgd Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +LEAF(ptrace, 4) + LDGP(pv) + stl zero, errno + CALLSYS_ERROR(ptrace) + RET +END(ptrace) diff --git a/lib/nbsd_libc/arch/alpha/sys/sbrk.S b/lib/nbsd_libc/arch/alpha/sys/sbrk.S new file mode 100644 index 000000000..afd80c4a2 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/sbrk.S @@ -0,0 +1,52 @@ +/* $NetBSD: sbrk.S,v 1.9 2001/05/07 17:19:18 kleink Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + + .globl _end + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +EXPORT(__curbrk) + .quad _end + + .text +LEAF(_sbrk, 1) + br pv, L1 /* XXX profiling */ +L1: LDGP(pv) + ldq a1, __curbrk + addq a0, a1, a0 + CALLSYS_ERROR(break) + stq a0, __curbrk + mov a1, v0 + RET +END(_sbrk) diff --git a/lib/nbsd_libc/arch/alpha/sys/shmat.S b/lib/nbsd_libc/arch/alpha/sys/shmat.S new file mode 100644 index 000000000..90e318516 --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:51 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/alpha/sys/syscall.S b/lib/nbsd_libc/arch/alpha/sys/syscall.S new file mode 100644 index 000000000..b97ef3fbf --- /dev/null +++ b/lib/nbsd_libc/arch/alpha/sys/syscall.S @@ -0,0 +1,32 @@ +/* $NetBSD: syscall.S,v 1.2 2000/06/14 06:49:03 cgd Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/arm/Makefile.inc b/lib/nbsd_libc/arch/arm/Makefile.inc new file mode 100644 index 000000000..7f0313bec --- /dev/null +++ b/lib/nbsd_libc/arch/arm/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.9 2010/07/06 05:59:52 mrg Exp $ + +.include + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +CPPFLAGS += -DSOFTFLOAT + +SOFTFLOAT_BITS=32 +.include diff --git a/lib/nbsd_libc/arch/arm/SYS.h b/lib/nbsd_libc/arch/arm/SYS.h new file mode 100644 index 000000000..d4f2e5d7c --- /dev/null +++ b/lib/nbsd_libc/arch/arm/SYS.h @@ -0,0 +1,94 @@ +/* $NetBSD: SYS.h,v 1.10 2011/01/14 06:12:16 matt Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)SYS.h 5.5 (Berkeley) 5/7/91 + */ + +#include +#include +#include + +#ifdef __STDC__ +#define SYSTRAP(x) swi SWI_OS_NETBSD | SYS_ ## x +#else +#define SYSTRAP(x) swi SWI_OS_NETBSD | SYS_/**/x +#endif + +#ifdef __ELF__ +#define CERROR _C_LABEL(__cerror) +#define CURBRK _C_LABEL(__curbrk) +#else +#define CERROR _ASM_LABEL(cerror) +#define CURBRK _ASM_LABEL(curbrk) +#endif + +#define _SYSCALL_NOERROR(x,y) \ + ENTRY(x); \ + SYSTRAP(y) + +#define _SYSCALL(x, y) \ + _SYSCALL_NOERROR(x,y); \ + bcs CERROR + +#define SYSCALL_NOERROR(x) \ + _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) \ + _SYSCALL(x,x) + + +#define PSEUDO_NOERROR(x,y) \ + _SYSCALL_NOERROR(x,y); \ + RET + +#define PSEUDO(x,y) \ + _SYSCALL(x,y); \ + RET + + +#define RSYSCALL_NOERROR(x) \ + PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) \ + PSEUDO(x,x) + +#ifdef WEAK_ALIAS +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) +#else +#define WSYSCALL(weak,strong) \ + PSEUDO(weak,weak) +#endif + + .globl CERROR diff --git a/lib/nbsd_libc/arch/arm/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/arm/gdtoa/Makefile.inc new file mode 100644 index 000000000..1d786e010 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/arm/gdtoa/arith.h b/lib/nbsd_libc/arch/arm/gdtoa/arith.h new file mode 100644 index 000000000..40d3ed1fa --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gdtoa/arith.h @@ -0,0 +1,9 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#include + +#if BYTE_ORDER == BIG_ENDIAN +#define IEEE_BIG_ENDIAN +#else +#define IEEE_LITTLE_ENDIAN +#endif diff --git a/lib/nbsd_libc/arch/arm/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/arm/gdtoa/gd_qnan.h new file mode 100644 index 000000000..9554f4991 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gdtoa/gd_qnan.h @@ -0,0 +1,12 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#include + +#define f_QNAN 0x7fc00000 +#if BYTE_ORDER == BIG_ENDIAN +#define d_QNAN0 0x7ff80000 +#define d_QNAN1 0x0 +#else +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff80000 +#endif diff --git a/lib/nbsd_libc/arch/arm/gen/Makefile.inc b/lib/nbsd_libc/arch/arm/gen/Makefile.inc new file mode 100644 index 000000000..d9aa365d2 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/Makefile.inc @@ -0,0 +1,27 @@ +# $NetBSD: Makefile.inc,v 1.16 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= alloca.S byte_swap_2.S byte_swap_4.S bswap64.c divsi3.S \ + fabs.c flt_rounds.c + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_dbl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +SRCS+= setjmp.S +SRCS+= _setjmp.S +SRCS+= sigsetjmp.S + +SRCS+= makecontext.c resumecontext.c swapcontext.S + +SRCS+= _lwp.c + +LSRCS.arm.gen= Lint_bswap16.c Lint_bswap32.c Lint_swapcontext.c +LSRCS+= ${LSRCS.arm.gen} +DPSRCS+= ${LSRCS.arm.gen} +CLEANFILES+= ${LSRCS.arm.gen} diff --git a/lib/nbsd_libc/arch/arm/gen/_lwp.c b/lib/nbsd_libc/arch/arm/gen/_lwp.c new file mode 100644 index 000000000..fcc78bd2c --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/_lwp.c @@ -0,0 +1,71 @@ +/* $NetBSD: _lwp.c,v 1.4 2005/06/12 05:21:25 lukem Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Jason R. Thorpe for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.4 2005/06/12 05:21:25 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + sp = (void **) (stack_base + stack_size); + + /* + * Note: We make sure the stack is 8-byte aligned, here. + */ + + u->uc_mcontext.__gregs[_REG_R0] = (__greg_t) arg; + u->uc_mcontext.__gregs[_REG_SP] = ((__greg_t) sp) & ~7; + u->uc_mcontext.__gregs[_REG_LR] = (__greg_t) _lwp_exit; + u->uc_mcontext.__gregs[_REG_PC] = (__greg_t) start; +} diff --git a/lib/nbsd_libc/arch/arm/gen/_setjmp.S b/lib/nbsd_libc/arch/arm/gen/_setjmp.S new file mode 100644 index 000000000..559d9b38f --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/_setjmp.S @@ -0,0 +1,105 @@ +/* $NetBSD: _setjmp.S,v 1.6 2004/08/21 11:20:10 rearnsha Exp $ */ + +/* + * Copyright (c) 1997 Mark Brinicombe + * 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 by Mark Brinicombe + * 4. 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 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 + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from the last call to + * _setjmp(a) + * by restoring registers from the stack. + * The previous signal state is NOT restored. + * + * Note: r0 is the return value + * r1-r3 are scratch registers in functions + */ + +ENTRY(_setjmp) + ldr r1, .L_setjmp_magic + str r1, [r0], #4 +#ifdef SOFTFLOAT + add r0, r0, #52 +#else + /* Store fp registers */ + sfm f4, 4, [r0], #48 + /* Store fpsr */ + rfs r1 + str r1, [r0], #0x0004 +#endif /* SOFTFLOAT */ + /* Store integer registers */ + stmia r0, {r4-r14} + + mov r0, #0x00000000 + RET + +.L_setjmp_magic: + .word _JB_MAGIC__SETJMP + +ENTRY(_longjmp) + ldr r2, .L_setjmp_magic + ldr r3, [r0], #4 + teq r2, r3 + bne botch + +#ifdef SOFTFLOAT + add r0, r0, #52 +#else + /* Restore fp registers */ + lfm f4, 4, [r0], #48 + /* Restore fpsr */ + ldr r4, [r0], #0x0004 + wfs r4 +#endif /* SOFTFLOAT */ + /* Restore integer registers */ + ldmia r0, {r4-r14} + + /* Validate sp and r14 */ + teq sp, #0 + teqne r14, #0 + beq botch + + /* Set return value */ + mov r0, r1 + teq r0, #0x00000000 + moveq r0, #0x00000001 + RET + + /* validation failed, die die die. */ +botch: + bl PIC_SYM(_C_LABEL(longjmperror), PLT) + bl PIC_SYM(_C_LABEL(abort), PLT) + b . - 8 /* Cannot get here */ diff --git a/lib/nbsd_libc/arch/arm/gen/alloca.S b/lib/nbsd_libc/arch/arm/gen/alloca.S new file mode 100644 index 000000000..694c2a9c7 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/alloca.S @@ -0,0 +1,44 @@ +/* $NetBSD: alloca.S,v 1.4 2004/08/21 11:20:10 rearnsha Exp $ */ + +/* + * Copyright (c) 1995 Mark Brinicombe + * 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 by Mark Brinicombe + * 4. 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 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. + */ + +/* like alloc, but automatic automatic free in return */ + +#include + +ENTRY(alloca) + add r0, r0, #0x00000007 /* round up to next 8 byte alignment */ + bic r0, r0, #0x00000007 + sub sp, sp, r0 /* Adjust the stack pointer */ + mov r0, sp /* r0 = base of new space */ + RET diff --git a/lib/nbsd_libc/arch/arm/gen/fabs.c b/lib/nbsd_libc/arch/arm/gen/fabs.c new file mode 100644 index 000000000..64cbeba37 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/fabs.c @@ -0,0 +1,46 @@ +/* $NetBSD: fabs.c,v 1.3 2006/05/16 05:29:16 mrg Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * + * 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 by Mark Brinicombe + * 4. 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 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. + */ + +/* + * fabs(x) returns the absolute value of x. + */ + +double fabs(double x); + +double +fabs(double x) +{ + if (x < 0) + x = -x; + return(x); +} diff --git a/lib/nbsd_libc/arch/arm/gen/flt_rounds.c b/lib/nbsd_libc/arch/arm/gen/flt_rounds.c new file mode 100644 index 000000000..a341e6e3c --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/flt_rounds.c @@ -0,0 +1,81 @@ +/* $NetBSD: flt_rounds.c,v 1.3 2006/02/25 00:58:35 wiz Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.3 2006/02/25 00:58:35 wiz Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 2, /* round to positive infinity */ + 3, /* round to negative infinity */ + 0 /* round to zero */ +}; + +/* + * Return the current FP rounding mode + * + * Returns: + * 0 - round to zero + * 1 - round to nearest + * 2 - round to postive infinity + * 3 - round to negative infinity + * + * ok all we need to do is get the current FP rounding mode + * index our map table and return the appropriate value. + * + * HOWEVER: + * The ARM FPA codes the rounding mode into the actual FP instructions + * so there is no such thing as a global rounding mode. + * The default is round to nearest if rounding is not explicitly specified. + * FP instructions generated by GCC will not explicitly specify a rounding + * mode. + * + * So the best we can do it to return the rounding mode FP instructions + * use if rounding is not specified which is round to nearest. + * + * This could change in the future with new floating point emulators or + * soft float FP libraries. + */ + +int __flt_rounds(void); + +int +__flt_rounds() +{ + return(map[fpgetround()]); +} diff --git a/lib/nbsd_libc/arch/arm/gen/makecontext.c b/lib/nbsd_libc/arch/arm/gen/makecontext.c new file mode 100644 index 000000000..cf9bfeaf1 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/makecontext.c @@ -0,0 +1,73 @@ +/* $NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:55 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + int i; + unsigned int *sp; + va_list ap; + + /* Compute and align stack pointer. */ + sp = (unsigned int *) + (((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) & ~7); + /* Allocate necessary stack space for arguments exceeding r0-3. */ + if (argc > 4) + sp -= argc - 4; + gr[_REG_SP] = (__greg_t)sp; + /* Wipe out frame pointer. */ + gr[_REG_FP] = 0; + /* Arrange for return via the trampoline code. */ + gr[_REG_LR] = (__greg_t)_resumecontext; + gr[_REG_PC] = (__greg_t)func; + + va_start(ap, argc); + /* Pass up to four arguments in r0-3. */ + for (i = 0; i < argc && i < 4; i++) + gr[_REG_R0 + i] = va_arg(ap, int); + /* Pass any additional arguments on the stack. */ + for (argc -= i; argc > 0; argc--) + *sp++ = va_arg(ap, int); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/arm/gen/nanf.c b/lib/nbsd_libc/arch/arm/gen/nanf.c new file mode 100644 index 000000000..0be7edddb --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/nanf.c @@ -0,0 +1,19 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:01 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = +#if BYTE_ORDER == BIG_ENDIAN + { { 0x7f, 0xc0, 0, 0 } }; +#else + { { 0, 0, 0xc0, 0x7f } }; +#endif + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/arm/gen/resumecontext.c b/lib/nbsd_libc/arch/arm/gen/resumecontext.c new file mode 100644 index 000000000..7cb6a420d --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:55 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/arm/gen/setjmp.S b/lib/nbsd_libc/arch/arm/gen/setjmp.S new file mode 100644 index 000000000..b3f5a6204 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/setjmp.S @@ -0,0 +1,121 @@ +/* $NetBSD: setjmp.S,v 1.8 2005/10/16 17:26:24 christos Exp $ */ + +/* + * Copyright (c) 1997 Mark Brinicombe + * 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 by Mark Brinicombe + * 4. 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 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 + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__setjmp14) + /* Get the signal mask. */ + stmfd sp!, {r0-r2, r14} + add r2, r0, #(_JB_SIGMASK * 4) + mov r1, #0x00000000 + mov r0, #0x00000000 + bl PIC_SYM(_C_LABEL(__sigprocmask14), PLT) + ldmfd sp!, {r0-r2, r14} + + ldr r1, .Lsetjmp_magic + str r1, [r0], #4 + +#ifdef SOFTFLOAT + add r0, r0, #52 +#else + /* Store fp registers */ + sfm f4, 4, [r0], #48 + /* Store fpsr */ + rfs r1 + str r1, [r0], #0x0004 +#endif /*SOFTFLOAT*/ + /* Store integer registers */ + stmia r0, {r4-r14} + mov r0, #0x00000000 + RET + +.Lsetjmp_magic: + .word _JB_MAGIC_SETJMP + + +ENTRY(__longjmp14) + ldr r2, .Lsetjmp_magic + ldr r3, [r0] + teq r2, r3 + bne .Lbotch + + /* Restore the signal mask. */ + stmfd sp!, {r0-r2, r14} + mov r2, #0x00000000 + add r1, r0, #(_JB_SIGMASK * 4) + mov r0, #3 /* SIG_SETMASK */ + bl PIC_SYM(_C_LABEL(__sigprocmask14), PLT) + ldmfd sp!, {r0-r2, r14} + + add r0, r0, #4 +#ifdef SOFTFLOAT + add r0, r0, #52 +#else + /* Restore fp registers */ + lfm f4, 4, [r0], #48 + /* Restore FPSR */ + ldr r4, [r0], #0x0004 + wfs r4 +#endif /* SOFTFLOAT */ + /* Restore integer registers */ + ldmia r0, {r4-r14} + + /* Validate sp and r14 */ + teq sp, #0 + teqne r14, #0 + beq .Lbotch + + /* Set return value */ + + mov r0, r1 + teq r0, #0x00000000 + moveq r0, #0x00000001 + RET + + /* validation failed, die die die. */ +.Lbotch: + bl PIC_SYM(_C_LABEL(longjmperror), PLT) + bl PIC_SYM(_C_LABEL(abort), PLT) + b . - 8 /* Cannot get here */ diff --git a/lib/nbsd_libc/arch/arm/gen/sigsetjmp.S b/lib/nbsd_libc/arch/arm/gen/sigsetjmp.S new file mode 100644 index 000000000..58afe0b10 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/sigsetjmp.S @@ -0,0 +1,61 @@ +/* $NetBSD: sigsetjmp.S,v 1.5 2005/10/16 17:26:24 christos Exp $ */ + +/* + * Copyright (c) 1997 Mark Brinicombe + * 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 by Mark Brinicombe + * 4. 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 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 + +/* + * C library -- sigsetjmp, siglongjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a, m) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__sigsetjmp14) + teq r1, #0 + beq PIC_SYM(_C_LABEL(_setjmp), PLT) + b PIC_SYM(_C_LABEL(__setjmp14), PLT) + +.L_setjmp_magic: + .word _JB_MAGIC__SETJMP + +ENTRY(__siglongjmp14) + ldr r2, .L_setjmp_magic + ldr r3, [r0] + teq r2, r3 + beq PIC_SYM(_C_LABEL(_longjmp), PLT) + b PIC_SYM(_C_LABEL(__longjmp14), PLT) diff --git a/lib/nbsd_libc/arch/arm/gen/swapcontext.S b/lib/nbsd_libc/arch/arm/gen/swapcontext.S new file mode 100644 index 000000000..a193733d6 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/gen/swapcontext.S @@ -0,0 +1,50 @@ +/* $NetBSD: swapcontext.S,v 1.5 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.5 2008/04/28 20:22:55 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + +ENTRY(swapcontext) + stmfd sp!, {r0-r1, lr} /* Must save oucp, ucp, lr. */ + bl PIC_SYM(_C_LABEL(_getcontext), PLT) /* getcontext(oucp) */ + cmp r0, #0 + ldmfd sp!, {r0-r1, lr} + RETc(ne) + str lr, [r0, #(36 + 15*4)] /* Adjust saved PC. */ +#ifdef SOFTFLOAT + /* Ahem. */ +#endif + mov r0, r1 + bl PIC_SYM(_C_LABEL(setcontext), PLT) + RET diff --git a/lib/nbsd_libc/arch/arm/hardfloat/fpgetmask.S b/lib/nbsd_libc/arch/arm/hardfloat/fpgetmask.S new file mode 100644 index 000000000..706460863 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/hardfloat/fpgetmask.S @@ -0,0 +1,51 @@ +/* $NetBSD: fpgetmask.S,v 1.4 2004/08/21 11:21:00 rearnsha Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD project. + * 4. 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 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 + +/* + * Return the current FP exception enable mask + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetmask, _fpgetmask) +ENTRY(_fpgetmask) +#else +ENTRY(fpgetmask) +#endif + rfs r0 + mov r0, r0, lsr #16 + and r0, r0, #0x1f + RET diff --git a/lib/nbsd_libc/arch/arm/hardfloat/fpgetround.c b/lib/nbsd_libc/arch/arm/hardfloat/fpgetround.c new file mode 100644 index 000000000..e333bcd65 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/hardfloat/fpgetround.c @@ -0,0 +1,72 @@ +/* $NetBSD: fpgetround.c,v 1.4 2006/02/25 02:28:55 wiz Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.4 2006/02/25 02:28:55 wiz Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetround,_fpgetround) +#endif + +/* + * Return the current FP rounding mode + * + * ok all we need to do is return the current FP rounding mode + * + * HOWEVER: + * The ARM FPA codes the rounding mode into the actual FP instructions + * so there is no such thing as a global rounding mode. + * The default is round to nearest if rounding is not explicitly specified. + * FP instructions generated by GCC will not explicitly specify a rounding + * mode. + * + * So the best we can do it to return the rounding mode FP instructions + * use if rounding is not specified which is round to nearest. + * + * This could change in the future with new floating point emulators or + * soft float FP libraries. + */ + +fp_rnd +fpgetround() +{ + + return(FP_RN); +} diff --git a/lib/nbsd_libc/arch/arm/hardfloat/fpgetsticky.S b/lib/nbsd_libc/arch/arm/hardfloat/fpgetsticky.S new file mode 100644 index 000000000..f7ee5805b --- /dev/null +++ b/lib/nbsd_libc/arch/arm/hardfloat/fpgetsticky.S @@ -0,0 +1,50 @@ +/* $NetBSD: fpgetsticky.S,v 1.4 2004/08/21 11:21:00 rearnsha Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD project. + * 4. 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 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 + +/* + * Return the current FP cumulative flags + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetsticky, _fpgetsticky) +ENTRY(_fpgetsticky) +#else +ENTRY(fpgetsticky) +#endif + rfs r0 + and r0, r0, #0x1f + RET diff --git a/lib/nbsd_libc/arch/arm/hardfloat/fpsetmask.S b/lib/nbsd_libc/arch/arm/hardfloat/fpsetmask.S new file mode 100644 index 000000000..1c956cf61 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/hardfloat/fpsetmask.S @@ -0,0 +1,54 @@ +/* $NetBSD: fpsetmask.S,v 1.4 2004/08/21 11:21:00 rearnsha Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD project. + * 4. 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 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 + +/* + * Set the current FP exception enable mask + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetmask, _fpsetmask) +ENTRY(_fpsetmask) +#else +ENTRY(fpsetmask) +#endif + rfs r1 + bic r1, r1, #0x001f0000 + and r0, r0, #0x0000001f + orr r0, r1, r0, lsl #16 + wfs r0 + mov r0, r1, lsr #16 /* Return old mask */ + RET diff --git a/lib/nbsd_libc/arch/arm/hardfloat/fpsetround.c b/lib/nbsd_libc/arch/arm/hardfloat/fpsetround.c new file mode 100644 index 000000000..ffb0a1d3b --- /dev/null +++ b/lib/nbsd_libc/arch/arm/hardfloat/fpsetround.c @@ -0,0 +1,70 @@ +/* $NetBSD: fpsetround.c,v 1.6 2006/02/25 02:28:55 wiz Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.6 2006/02/25 02:28:55 wiz Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetround,_fpsetround) +#endif + +/* + * Return the current FP rounding mode + * + * ok all we need to do is return the current FP rounding mode + * + * HOWEVER: + * The ARM FPA codes the rounding mode into the actual FP instructions + * so there is no such thing as a global rounding mode. + * The default is round to nearest if rounding is not explicitly specified. + * FP instructions generated by GCC will not explicitly specify a rounding + * mode. + * + * All we can do is abort if they ask for anything else. + */ + +fp_rnd +fpsetround(fp_rnd rnd_dir) +{ + + if (rnd_dir != FP_RN) + abort(); + return (FP_RN); +} diff --git a/lib/nbsd_libc/arch/arm/hardfloat/fpsetsticky.S b/lib/nbsd_libc/arch/arm/hardfloat/fpsetsticky.S new file mode 100644 index 000000000..dd81e897f --- /dev/null +++ b/lib/nbsd_libc/arch/arm/hardfloat/fpsetsticky.S @@ -0,0 +1,54 @@ +/* $NetBSD: fpsetsticky.S,v 1.4 2004/08/21 11:21:00 rearnsha Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD project. + * 4. 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 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 + +/* + * Set the current FP cumulative flags + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetsticky, _fpsetsticky) +ENTRY(_fpsetsticky) +#else +ENTRY(fpsetsticky) +#endif + rfs r1 + bic r1, r1, #0x1f + and r0, r0, #0x1f + orr r0, r1, r0 + wfs r0 + mov r0, r1 /* Return old mask */ + RET diff --git a/lib/nbsd_libc/arch/arm/net/Makefile.inc b/lib/nbsd_libc/arch/arm/net/Makefile.inc new file mode 100644 index 000000000..10fabd39b --- /dev/null +++ b/lib/nbsd_libc/arch/arm/net/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2000/12/29 20:13:53 bjh21 Exp $ + +# hton* and nto* functions provided by ../gen/byte_swap_*.S +SRCS+= diff --git a/lib/nbsd_libc/arch/arm/softfloat/arm-gcc.h b/lib/nbsd_libc/arch/arm/softfloat/arm-gcc.h new file mode 100644 index 000000000..c92086a79 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/softfloat/arm-gcc.h @@ -0,0 +1,100 @@ +/* $NetBSD: arm-gcc.h,v 1.3 2005/12/24 21:11:16 perry Exp $ */ + +/* +------------------------------------------------------------------------------- +One of the macros `BIGENDIAN' or `LITTLEENDIAN' must be defined. +------------------------------------------------------------------------------- +*/ +#ifdef __ARMEB__ +#define BIGENDIAN +#else +#define LITTLEENDIAN +#endif + +/* +------------------------------------------------------------------------------- +The macro `BITS64' can be defined to indicate that 64-bit integer types are +supported by the compiler. +------------------------------------------------------------------------------- +*/ +#define BITS64 + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines the most convenient type that holds +integers of at least as many bits as specified. For example, `uint8' should +be the most convenient type that can hold unsigned integers of as many as +8 bits. The `flag' type must be able to hold either a 0 or 1. For most +implementations of C, `flag', `uint8', and `int8' should all be `typedef'ed +to the same as `int'. +------------------------------------------------------------------------------- +*/ +typedef int flag; +typedef int uint8; +typedef int int8; +typedef int uint16; +typedef int int16; +typedef unsigned int uint32; +typedef signed int int32; +#ifdef BITS64 +typedef unsigned long long int uint64; +typedef signed long long int int64; +#endif + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines a type that holds integers +of _exactly_ the number of bits specified. For instance, for most +implementation of C, `bits16' and `sbits16' should be `typedef'ed to +`unsigned short int' and `signed short int' (or `short int'), respectively. +------------------------------------------------------------------------------- +*/ +typedef unsigned char bits8; +typedef signed char sbits8; +typedef unsigned short int bits16; +typedef signed short int sbits16; +typedef unsigned int bits32; +typedef signed int sbits32; +#ifdef BITS64 +typedef unsigned long long int bits64; +typedef signed long long int sbits64; +#endif + +#ifdef BITS64 +/* +------------------------------------------------------------------------------- +The `LIT64' macro takes as its argument a textual integer literal and +if necessary ``marks'' the literal as having a 64-bit integer type. +For example, the GNU C Compiler (`gcc') requires that 64-bit literals be +appended with the letters `LL' standing for `long long', which is `gcc's +name for the 64-bit integer type. Some compilers may allow `LIT64' to be +defined as the identity macro: `#define LIT64( a ) a'. +------------------------------------------------------------------------------- +*/ +#define LIT64( a ) a##LL +#endif + +/* +------------------------------------------------------------------------------- +The macro `INLINE' can be used before functions that should be inlined. If +a compiler does not support explicit inlining, this macro should be defined +to be `static'. +------------------------------------------------------------------------------- +*/ +#define INLINE static inline + +/* +------------------------------------------------------------------------------- +The ARM FPA is odd in that it stores doubles high-order word first, no matter +what the endianness of the CPU. VFP is sane. +------------------------------------------------------------------------------- +*/ +#if defined(SOFTFLOAT_FOR_GCC) +#if defined(__VFP_FP__) || defined(__ARMEB__) +#define FLOAT64_DEMANGLE(a) (a) +#define FLOAT64_MANGLE(a) (a) +#else +#define FLOAT64_DEMANGLE(a) (((a) << 32) | ((a) >> 32)) +#define FLOAT64_MANGLE(a) FLOAT64_DEMANGLE(a) +#endif +#endif diff --git a/lib/nbsd_libc/arch/arm/softfloat/milieu.h b/lib/nbsd_libc/arch/arm/softfloat/milieu.h new file mode 100644 index 000000000..a3f829c42 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/softfloat/milieu.h @@ -0,0 +1,48 @@ +/* $NetBSD: milieu.h,v 1.1 2000/12/29 20:13:54 bjh21 Exp $ */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +Include common integer types and flags. +------------------------------------------------------------------------------- +*/ +#include "arm-gcc.h" + +/* +------------------------------------------------------------------------------- +Symbolic Boolean literals. +------------------------------------------------------------------------------- +*/ +enum { + FALSE = 0, + TRUE = 1 +}; diff --git a/lib/nbsd_libc/arch/arm/softfloat/softfloat.h b/lib/nbsd_libc/arch/arm/softfloat/softfloat.h new file mode 100644 index 000000000..f424abec8 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/softfloat/softfloat.h @@ -0,0 +1,314 @@ +/* $NetBSD: softfloat.h,v 1.7 2006/05/16 20:55:51 mrg Exp $ */ + +/* This is a derivative work. */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +The macro `FLOATX80' must be defined to enable the extended double-precision +floating-point format `floatx80'. If this macro is not defined, the +`floatx80' type will not be defined, and none of the functions that either +input or output the `floatx80' type will be defined. The same applies to +the `FLOAT128' macro and the quadruple-precision format `float128'. +------------------------------------------------------------------------------- +*/ +/* #define FLOATX80 */ +/* #define FLOAT128 */ + +#include + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point types. +------------------------------------------------------------------------------- +*/ +typedef unsigned int float32; +typedef unsigned long long float64; +#ifdef FLOATX80 +typedef struct { + unsigned short high; + unsigned long long low; +} floatx80; +#endif +#ifdef FLOAT128 +typedef struct { + unsigned long long high, low; +} float128; +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point underflow tininess-detection mode. +------------------------------------------------------------------------------- +*/ +#ifndef SOFTFLOAT_FOR_GCC +extern int float_detect_tininess; +#endif +enum { + float_tininess_after_rounding = 0, + float_tininess_before_rounding = 1 +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point rounding mode. +------------------------------------------------------------------------------- +*/ +extern fp_rnd float_rounding_mode; +enum { + float_round_nearest_even = FP_RN, + float_round_to_zero = FP_RZ, + float_round_down = FP_RM, + float_round_up = FP_RP +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point exception flags. +------------------------------------------------------------------------------- +*/ +extern fp_except float_exception_flags; +extern fp_except float_exception_mask; +enum { + float_flag_inexact = FP_X_IMP, + float_flag_underflow = FP_X_UFL, + float_flag_overflow = FP_X_OFL, + float_flag_divbyzero = FP_X_DZ, + float_flag_invalid = FP_X_INV +}; + +/* +------------------------------------------------------------------------------- +Routine to raise any or all of the software IEC/IEEE floating-point +exception flags. +------------------------------------------------------------------------------- +*/ +void float_raise( fp_except ); + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE integer-to-floating-point conversion routines. +------------------------------------------------------------------------------- +*/ +float32 int32_to_float32( int ); +float64 int32_to_float64( int ); +#ifdef FLOATX80 +floatx80 int32_to_floatx80( int ); +#endif +#ifdef FLOAT128 +float128 int32_to_float128( int ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __floatdi?f is in libgcc2.c */ +float32 int64_to_float32( long long ); +float64 int64_to_float64( long long ); +#ifdef FLOATX80 +floatx80 int64_to_floatx80( long long ); +#endif +#ifdef FLOAT128 +float128 int64_to_float128( long long ); +#endif +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float32_to_int32( float32 ); +int float32_to_int32_round_to_zero( float32 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned int float32_to_uint32_round_to_zero( float32 ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ +long long float32_to_int64( float32 ); +long long float32_to_int64_round_to_zero( float32 ); +#endif +float64 float32_to_float64( float32 ); +#ifdef FLOATX80 +floatx80 float32_to_floatx80( float32 ); +#endif +#ifdef FLOAT128 +float128 float32_to_float128( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision operations. +------------------------------------------------------------------------------- +*/ +float32 float32_round_to_int( float32 ); +float32 float32_add( float32, float32 ); +float32 float32_sub( float32, float32 ); +float32 float32_mul( float32, float32 ); +float32 float32_div( float32, float32 ); +float32 float32_rem( float32, float32 ); +float32 float32_sqrt( float32 ); +int float32_eq( float32, float32 ); +int float32_le( float32, float32 ); +int float32_lt( float32, float32 ); +int float32_eq_signaling( float32, float32 ); +int float32_le_quiet( float32, float32 ); +int float32_lt_quiet( float32, float32 ); +#ifndef SOFTFLOAT_FOR_GCC +int float32_is_signaling_nan( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float64_to_int32( float64 ); +int float64_to_int32_round_to_zero( float64 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned int float64_to_uint32_round_to_zero( float64 ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ +long long float64_to_int64( float64 ); +long long float64_to_int64_round_to_zero( float64 ); +#endif +float32 float64_to_float32( float64 ); +#ifdef FLOATX80 +floatx80 float64_to_floatx80( float64 ); +#endif +#ifdef FLOAT128 +float128 float64_to_float128( float64 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision operations. +------------------------------------------------------------------------------- +*/ +float64 float64_round_to_int( float64 ); +float64 float64_add( float64, float64 ); +float64 float64_sub( float64, float64 ); +float64 float64_mul( float64, float64 ); +float64 float64_div( float64, float64 ); +float64 float64_rem( float64, float64 ); +float64 float64_sqrt( float64 ); +int float64_eq( float64, float64 ); +int float64_le( float64, float64 ); +int float64_lt( float64, float64 ); +int float64_eq_signaling( float64, float64 ); +int float64_le_quiet( float64, float64 ); +int float64_lt_quiet( float64, float64 ); +#ifndef SOFTFLOAT_FOR_GCC +int float64_is_signaling_nan( float64 ); +#endif + +#ifdef FLOATX80 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int floatx80_to_int32( floatx80 ); +int floatx80_to_int32_round_to_zero( floatx80 ); +long long floatx80_to_int64( floatx80 ); +long long floatx80_to_int64_round_to_zero( floatx80 ); +float32 floatx80_to_float32( floatx80 ); +float64 floatx80_to_float64( floatx80 ); +#ifdef FLOAT128 +float128 floatx80_to_float128( floatx80 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision rounding precision. Valid +values are 32, 64, and 80. +------------------------------------------------------------------------------- +*/ +extern int floatx80_rounding_precision; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision operations. +------------------------------------------------------------------------------- +*/ +floatx80 floatx80_round_to_int( floatx80 ); +floatx80 floatx80_add( floatx80, floatx80 ); +floatx80 floatx80_sub( floatx80, floatx80 ); +floatx80 floatx80_mul( floatx80, floatx80 ); +floatx80 floatx80_div( floatx80, floatx80 ); +floatx80 floatx80_rem( floatx80, floatx80 ); +floatx80 floatx80_sqrt( floatx80 ); +int floatx80_eq( floatx80, floatx80 ); +int floatx80_le( floatx80, floatx80 ); +int floatx80_lt( floatx80, floatx80 ); +int floatx80_eq_signaling( floatx80, floatx80 ); +int floatx80_le_quiet( floatx80, floatx80 ); +int floatx80_lt_quiet( floatx80, floatx80 ); +int floatx80_is_signaling_nan( floatx80 ); + +#endif + +#ifdef FLOAT128 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float128_to_int32( float128 ); +int float128_to_int32_round_to_zero( float128 ); +long long float128_to_int64( float128 ); +long long float128_to_int64_round_to_zero( float128 ); +float32 float128_to_float32( float128 ); +float64 float128_to_float64( float128 ); +#ifdef FLOATX80 +floatx80 float128_to_floatx80( float128 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision operations. +------------------------------------------------------------------------------- +*/ +float128 float128_round_to_int( float128 ); +float128 float128_add( float128, float128 ); +float128 float128_sub( float128, float128 ); +float128 float128_mul( float128, float128 ); +float128 float128_div( float128, float128 ); +float128 float128_rem( float128, float128 ); +float128 float128_sqrt( float128 ); +int float128_eq( float128, float128 ); +int float128_le( float128, float128 ); +int float128_lt( float128, float128 ); +int float128_eq_signaling( float128, float128 ); +int float128_le_quiet( float128, float128 ); +int float128_lt_quiet( float128, float128 ); +int float128_is_signaling_nan( float128 ); + +#endif + diff --git a/lib/nbsd_libc/arch/arm/stdlib/Makefile.inc b/lib/nbsd_libc/arch/arm/stdlib/Makefile.inc new file mode 100644 index 000000000..d1dda0262 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/07/31 20:39:59 dsl Exp $ diff --git a/lib/nbsd_libc/arch/arm/string/Makefile.inc b/lib/nbsd_libc/arch/arm/string/Makefile.inc new file mode 100644 index 000000000..ca9054435 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/string/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.8 2009/07/30 20:57:15 dsl Exp $ + +SRCS+= memcpy.S bcopy.S memmove.S memset.S bzero.S ffs.S strcmp.S +SRCS+= strncmp.S memcmp.S + diff --git a/lib/nbsd_libc/arch/arm/string/bcopy.S b/lib/nbsd_libc/arch/arm/string/bcopy.S new file mode 100644 index 000000000..71fb90905 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/string/bcopy.S @@ -0,0 +1,4 @@ +/* $NetBSD: bcopy.S,v 1.3 2003/10/14 07:51:45 scw Exp $ */ + +#define _BCOPY +#include "memmove.S" diff --git a/lib/nbsd_libc/arch/arm/string/bzero.S b/lib/nbsd_libc/arch/arm/string/bzero.S new file mode 100644 index 000000000..1c9d39085 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/string/bzero.S @@ -0,0 +1,35 @@ +/* $NetBSD: bzero.S,v 1.4 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Neil A. Carson and Mark Brinicombe + * + * 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 + +#define _BZERO +#include "memset.S" diff --git a/lib/nbsd_libc/arch/arm/sys/__clone.S b/lib/nbsd_libc/arch/arm/sys/__clone.S new file mode 100644 index 000000000..f5b329a17 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/__clone.S @@ -0,0 +1,93 @@ +/* $NetBSD: __clone.S,v 1.6 2006/04/24 19:00:30 snj Exp $ */ + +/* + * Copyright (c) 2001 Christopher Gilbert + * All rights reserved. + * + * 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 company nor 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 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + + /* test stack and function are not null */ + teq r0, #0x00 + teqne r1, #0x00 + beq .Leinval + + /* place the func and its arg onto the child's stack */ + stmfd r1!, {r0, r3} + + /* syscall expects (flags, stack) */ + mov r0, r2 + + SYSTRAP(__clone) + bcs PIC_SYM(CERROR, PLT) + + /* + * r1 and r0 are the same as from fork: + * r1 == 0 in parent process, r1 == 1 in child process. + * r0 == pid of child in parent, r0 == pid of parent in child. + */ + teq r1, #0x00 + + /* if this is the parent then just return the pid */ + RETc(eq) + /* + * This is the child + * load the function and arg off the stack + */ + ldmfd sp!, {r1, r2} + + /* setup return address */ + add lr, pc, #.Lreturnhere - . - 8 + + /* place arg in r0 */ + mov r0, r2 + + /* call the function */ +#ifdef _ARM_ARCH_4T + bx r1 +#else + mov pc, r1 +#endif + +.Lreturnhere: + /* call _exit with the returned value */ + b PIC_SYM(_C_LABEL(_exit), PLT) + + /* NOTREACHED */ + + /* error handler if func or stack is NULL */ +.Leinval: + mov R0, #EINVAL + b PIC_SYM(CERROR, PLT) diff --git a/lib/nbsd_libc/arch/arm/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/arm/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..615ebe816 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/__sigaction14_sigtramp.c @@ -0,0 +1,79 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.7 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.7 2008/04/28 20:22:55 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + extern const int __sigtramp_sigcontext_1[]; + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specificed or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/arm/sys/__sigtramp2.S b/lib/nbsd_libc/arch/arm/sys/__sigtramp2.S new file mode 100755 index 000000000..4bc72f412 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/__sigtramp2.S @@ -0,0 +1,49 @@ +/* $NetBSD: __sigtramp2.S,v 1.2 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +/* + * The ARM signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, the stack looks like: + * + * ucontext structure + * sp-> siginfo structure + * and r5 points to the ucontext + */ +ENTRY_NP(__sigtramp_siginfo_2) + mov r0, r5 /* set the arg */ + SYSTRAP(setcontext) /* and call setcontext */ + + /* If that failed, exit with the error code. */ + SYSTRAP(exit) diff --git a/lib/nbsd_libc/arch/arm/sys/__syscall.S b/lib/nbsd_libc/arch/arm/sys/__syscall.S new file mode 100644 index 000000000..563aaad57 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/__syscall.S @@ -0,0 +1,36 @@ +/* $NetBSD: __syscall.S,v 1.4 2003/08/07 16:42:03 agc 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/arm/sys/__vfork14.S b/lib/nbsd_libc/arch/arm/sys/__vfork14.S new file mode 100644 index 000000000..f2710a99c --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/__vfork14.S @@ -0,0 +1,53 @@ +/* $NetBSD: __vfork14.S,v 1.7 2004/08/21 11:30:17 rearnsha 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +/* + * pid = vfork(); + * + * On return from the SWI: + * r1 == 0 in parent process, r1 == 1 in child process. + * r0 == pid of child in parent, r0 == pid of parent in child. + */ + .text + .align 0 + +ENTRY(__vfork14) + mov r2, r14 + SYSTRAP(__vfork14) + bcs PIC_SYM(CERROR, PLT) + mov r14, r2 + sub r1, r1, #1 /* r1 == 0xffffffff if parent, 0 if child */ + and r0, r0, r1 /* r0 == 0 if child, else unchanged */ + RET diff --git a/lib/nbsd_libc/arch/arm/sys/brk.S b/lib/nbsd_libc/arch/arm/sys/brk.S new file mode 100644 index 000000000..b5cbbd440 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/brk.S @@ -0,0 +1,98 @@ +/* $NetBSD: brk.S,v 1.8 2004/08/21 11:30:17 rearnsha 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. + * + * from: @(#)brk.s 5.2 (Berkeley) 12/17/90 + */ + +#include "SYS.h" + + .globl _C_LABEL(end) + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data + .align 0 + .globl _C_LABEL(__minbrk) + .type _C_LABEL(__minbrk),#object +_C_LABEL(__minbrk): + .word _C_LABEL(end) + +/* + * Change the data segment size + */ +ENTRY(_brk) +#ifdef PIC + /* Setup the GOT */ + ldr r3, .Lgot + add r3, pc, r3 +.L1: + ldr r1, .Lminbrk + ldr r1, [r3, r1] +#else + ldr r1, .Lminbrk +#endif + /* Get the minimum allowable brk address */ + ldr r1, [r1] + + /* + * Valid the address specified and set to the minimum + * if the address is below minbrk. + */ + cmp r0, r1 + movcc r0, r1 + mov r2, r0 + SYSTRAP(break) + bcs PIC_SYM(CERROR, PLT) + +#ifdef PIC + ldr r1, .Lcurbrk + ldr r1, [r3, r1] +#else + ldr r1, .Lcurbrk +#endif + /* Store the new address in curbrk */ + str r2, [r1] + + /* Return 0 for success */ + mov r0, #0x00000000 + RET + + .align 2 +#ifdef PIC +.Lgot: + .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) + (. - (.L1+4)) +#endif +.Lminbrk: + .word PIC_SYM(_C_LABEL(__minbrk), GOT) +.Lcurbrk: + .word PIC_SYM(CURBRK, GOT) diff --git a/lib/nbsd_libc/arch/arm/sys/cerror.S b/lib/nbsd_libc/arch/arm/sys/cerror.S new file mode 100644 index 000000000..f05cd69ee --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/cerror.S @@ -0,0 +1,75 @@ +/* $NetBSD: cerror.S,v 1.7 2011/01/14 06:12:17 matt 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. + * + * from: @(#)cerror.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +#ifdef PIC + .hidden CERROR +#endif + +ASENTRY(CERROR) +#ifdef _REENTRANT + stmfd sp!, {r4, lr} + mov r4, r0 + bl PIC_SYM(_C_LABEL(__errno), PLT) + str r4, [r0] + mvn r0, #0x00000000 + mvn r1, #0x00000000 + ldmfd sp!, {r4, pc} +#else +#ifdef PIC + /* Setup the GOT */ + ldr r3, .Lgot + add r3, pc, r3 +.L1: + ldr r1, .Lerrno + ldr r1, [r3, r1] +#else + ldr r1, .Lerrno +#endif /* PIC */ + str r0, [r1] + mvn r0, #0x00000000 + mvn r1, #0x00000000 + RET + +#ifdef PIC + .align 0 +.Lgot: + .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) + (. - (.L1+4)) +#endif /* PIC */ + + .globl _C_LABEL(errno) + +.Lerrno: + .word PIC_SYM(_C_LABEL(errno), GOT) +#endif /* _REENTRANT */ diff --git a/lib/nbsd_libc/arch/arm/sys/exect.S b/lib/nbsd_libc/arch/arm/sys/exect.S new file mode 100644 index 000000000..07d9c5d86 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/exect.S @@ -0,0 +1,36 @@ +/* $NetBSD: exect.S,v 1.4 2003/08/07 16:42:04 agc 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. + * + * from: @(#)exect.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +PSEUDO(exect,execve) diff --git a/lib/nbsd_libc/arch/arm/sys/fork.S b/lib/nbsd_libc/arch/arm/sys/fork.S new file mode 100644 index 000000000..41dac4946 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/fork.S @@ -0,0 +1,47 @@ +/* $NetBSD: fork.S,v 1.6 2004/08/21 11:30:17 rearnsha 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. + * + * from: @(#)fork.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +/* + * pid = fork(); + * + * On return from the SWI: + * r1 == 0 in parent process, r1 == 1 in child process. + * r0 == pid of child in parent, r0 == pid of parent in child. + */ + +_SYSCALL(__fork,fork) + sub r1, r1, #1 /* r1 == 0xffffffff if parent, 0 if child */ + and r0, r0, r1 /* r0 == 0 if child, else unchanged */ + RET diff --git a/lib/nbsd_libc/arch/arm/sys/getcontext.S b/lib/nbsd_libc/arch/arm/sys/getcontext.S new file mode 100644 index 000000000..79ff1e6d5 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/getcontext.S @@ -0,0 +1,54 @@ +/* $NetBSD: getcontext.S,v 1.6 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.6 2008/04/28 20:22:55 martin Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +ENTRY(_getcontext) + mov r2, r0 /* Must save argument pointer. */ + SYSTRAP (getcontext) + bcs PIC_SYM(CERROR, PLT) + str lr, [r2, #(36 + 15*4)] /* Adjust PC. */ +#ifdef SOFTFLOAT + /* Ahem. */ + /* Judging from the softfloat code we should at least save + the exception codes and rounding mode here. */ +#endif + mov r0, #0 + str r0, [r2, #(36 + 0*4)] /* Return 0. */ + RET diff --git a/lib/nbsd_libc/arch/arm/sys/pipe.S b/lib/nbsd_libc/arch/arm/sys/pipe.S new file mode 100644 index 000000000..8dc1492d1 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/pipe.S @@ -0,0 +1,47 @@ +/* $NetBSD: pipe.S,v 1.6 2004/08/21 11:30:17 rearnsha 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. + * + * from: @(#)pipe.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +ENTRY(_pipe) + mov r2, r0 + SYSTRAP(pipe) + bcs PIC_SYM(CERROR, PLT) + str r0, [r2, #0x0000] + str r1, [r2, #0x0004] + mov r0, #0x00000000 + RET diff --git a/lib/nbsd_libc/arch/arm/sys/ptrace.S b/lib/nbsd_libc/arch/arm/sys/ptrace.S new file mode 100644 index 000000000..1a91c0d76 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/ptrace.S @@ -0,0 +1,75 @@ +/* $NetBSD: ptrace.S,v 1.8 2004/08/21 11:30:17 rearnsha 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. + * + * from: @(#)ptrace.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +ENTRY(ptrace) +#ifdef _REENTRANT + stmfd sp!, {r0-r3, lr} + sub sp, sp, #4 /* align stack */ + bl PIC_SYM(_C_LABEL(__errno), PLT) + add sp, sp, #4 /* unalign stack */ + mov r1, #0x00000000 + str r1, [r0] + ldmfd sp!, {r0-r3, lr} +#else + stmfd sp!, {r0, r1} +#ifdef PIC + /* Setup the GOT */ + ldr r0, .Lgot + add r0, pc, r0 +.L1: + ldr r1, .Lerrno + ldr r1, [r0, r1] +#else + ldr r1, .Lerrno +#endif /* PIC */ + mov r0, #0x00000000 + str r0, [r1] + ldmfd sp!, {r0, r1} +#endif /* _REENTRANT */ + + SYSTRAP(ptrace) + bcs PIC_SYM(CERROR, PLT) + RET + +#ifndef _REENTRANT +#ifdef PIC + .align 0 +.Lgot: + .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) + (. - (.L1+4)) +#endif /* PIC */ + +.Lerrno: + .word PIC_SYM(_C_LABEL(errno), GOT) +#endif /* !_REENTRANT */ diff --git a/lib/nbsd_libc/arch/arm/sys/sbrk.S b/lib/nbsd_libc/arch/arm/sys/sbrk.S new file mode 100644 index 000000000..6ad161997 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/sbrk.S @@ -0,0 +1,86 @@ +/* $NetBSD: sbrk.S,v 1.8 2004/08/21 11:30:17 rearnsha 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. + * + * from: @(#)sbrk.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + + .globl _C_LABEL(end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data + .align 0 + .globl CURBRK + .type CURBRK,#object +CURBRK: + .word _C_LABEL(end) + +/* + * Change the data segment size + */ +ENTRY(_sbrk) +#ifdef PIC + /* Setup the GOT */ + ldr r3, .Lgot + add r3, pc, r3 +.L1: + ldr r2, .Lcurbrk + ldr r2, [r3, r2] +#else + ldr r2, .Lcurbrk +#endif + /* Get the current brk address */ + ldr r1, [r2] + + /* Calculate new value */ + mov r3, r0 + add r0, r0, r1 + SYSTRAP(break) + bcs PIC_SYM(CERROR, PLT) + + /* Store new curbrk value */ + ldr r0, [r2] + add r1, r0, r3 + str r1, [r2] + + /* Return old curbrk value */ + RET + + .align 0 +#ifdef PIC +.Lgot: + .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) + (. - (.L1+4)) +#endif +.Lcurbrk: + .word PIC_SYM(CURBRK, GOT) diff --git a/lib/nbsd_libc/arch/arm/sys/shmat.S b/lib/nbsd_libc/arch/arm/sys/shmat.S new file mode 100644 index 000000000..13bdd0d87 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/12/29 20:14:04 bjh21 Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/arm/sys/syscall.S b/lib/nbsd_libc/arch/arm/sys/syscall.S new file mode 100644 index 000000000..43c826145 --- /dev/null +++ b/lib/nbsd_libc/arch/arm/sys/syscall.S @@ -0,0 +1,36 @@ +/* $NetBSD: syscall.S,v 1.4 2003/08/07 16:42:05 agc 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/hppa/DEFS.h b/lib/nbsd_libc/arch/hppa/DEFS.h new file mode 100644 index 000000000..6e0125943 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/DEFS.h @@ -0,0 +1,3 @@ +/* $NetBSD: DEFS.h,v 1.1 2002/06/06 20:31:20 fredette Exp $ */ + +#include diff --git a/lib/nbsd_libc/arch/hppa/Makefile.inc b/lib/nbsd_libc/arch/hppa/Makefile.inc new file mode 100644 index 000000000..432cdb2b4 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.9 2009/10/21 19:30:46 skrll Exp $ + +# $OpenBSD: Makefile.inc,v 1.3 1999/12/25 06:02:10 mickey Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +SRCS+= bcopy.c diff --git a/lib/nbsd_libc/arch/hppa/SYS.h b/lib/nbsd_libc/arch/hppa/SYS.h new file mode 100644 index 000000000..5e5084429 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/SYS.h @@ -0,0 +1,77 @@ +/* $NetBSD: SYS.h,v 1.7 2009/11/03 05:07:25 snj Exp $ */ + +/* $OpenBSD: SYS.h,v 1.9 2001/09/20 20:52:09 millert Exp $ */ + +/* + * Copyright (c) 1998-2002 Michael Shalayeff + * 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 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 MIND + * 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 +#undef _LOCORE +#define _LOCORE +#include + +#define SYSENTRY(x) LEAF_ENTRY(x) +#define SYSEXIT(x) EXIT(x) + +#define SYSCALL(x) !\ + stw %rp, HPPA_FRAME_ERP(%sr0,%sp) !\ + ldil L%SYSCALLGATE, %r1 !\ + ble 4(%sr7, %r1) !\ + ldi __CONCAT(SYS_,x), %t1 !\ + .import __cerror, code !\ + comb,<> %r0, %t1, __cerror !\ + ldw HPPA_FRAME_ERP(%sr0,%sp), %rp + +#define PSEUDO(x,y) !\ +SYSENTRY(x) !\ + SYSCALL(y) !\ + bv %r0(%rp) !\ + nop !\ +SYSEXIT(x) + +#define PSEUDO_NOERROR(x,y) !\ +SYSENTRY(x) !\ + stw %rp, HPPA_FRAME_ERP(%sr0,%sp) !\ + ldil L%SYSCALLGATE, %r1 !\ + ble 4(%sr7, %r1) !\ + ldi __CONCAT(SYS_,y), %t1 !\ + ldw HPPA_FRAME_ERP(%sr0,%sp), %rp !\ + bv %r0(%rp) !\ + nop !\ +SYSEXIT(x) + +#define RSYSCALL(x) PSEUDO(x,x) +#define RSYSCALL_NOERROR(x) PSEUDO_NOERROR(x,x) + +#ifdef WEAK_ALIAS +#define WSYSCALL(weak,strong) !\ + WEAK_ALIAS(weak,strong) !\ + PSEUDO(strong,weak) +#else +#define WSYSCALL(weak,strong) !\ + PSEUDO(weak,weak) +#endif diff --git a/lib/nbsd_libc/arch/hppa/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/hppa/gdtoa/Makefile.inc new file mode 100644 index 000000000..1d786e010 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/hppa/gdtoa/arith.h b/lib/nbsd_libc/arch/hppa/gdtoa/arith.h new file mode 100644 index 000000000..4674f629c --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_BIG_ENDIAN diff --git a/lib/nbsd_libc/arch/hppa/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/hppa/gdtoa/gd_qnan.h new file mode 100644 index 000000000..b9e91e70f --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gdtoa/gd_qnan.h @@ -0,0 +1,11 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fa00000 +#define d_QNAN0 0x7ff40000 +#define d_QNAN1 0x0 +#ifdef _LP64 +#define ld_QNAN0 0x7fff4000 +#define ld_QNAN1 0x0 +#define ld_QNAN2 0x0 +#define ld_QNAN3 0x0 +#endif diff --git a/lib/nbsd_libc/arch/hppa/gen/Makefile.inc b/lib/nbsd_libc/arch/hppa/gen/Makefile.inc new file mode 100644 index 000000000..24ab0e5cc --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/Makefile.inc @@ -0,0 +1,20 @@ +# $NetBSD: Makefile.inc,v 1.15 2006/06/28 14:46:32 drochner Exp $ + +SRCS+= bswap16.c bswap32.c bswap64.c +SRCS+= fabs.c +SRCS+= __sigsetjmp14.S __setjmp14.S _setjmp.S __longjmp14.c + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_dbl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +SRCS+= makecontext.c resumecontext.c _resumecontext.S swapcontext.S _lwp.c + +SRCS+= flt_rounds.c fpgetmask.c fpgetround.c fpgetsticky.c fpsetmask.c \ + fpsetround.c fpsetsticky.c diff --git a/lib/nbsd_libc/arch/hppa/gen/__longjmp14.c b/lib/nbsd_libc/arch/hppa/gen/__longjmp14.c new file mode 100644 index 000000000..bb14fd627 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/__longjmp14.c @@ -0,0 +1,118 @@ +/* $NetBSD: __longjmp14.c,v 1.4 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christian Limpach. + * + * 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 "namespace.h" +#include +#include +#include +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include + +#include +#include + +ssize_t _sys_write(int, void *, size_t); + +void +__longjmp14(jmp_buf env, int val) +{ + ucontext_t uc; + struct sigcontext *sc = (void *)env; + register_t *regs = (void *)(sc + 1); + register register_t dp __asm("r27"); + + /* Ensure non-zero SP */ + if (sc->sc_sp == 0) + goto err; + + /* Make return value non-zero */ + if (val == 0) + val = 1; + + /* Copy callee-saved regs */ + regs -= 3; + uc.uc_mcontext.__gregs[3] = regs[3]; + uc.uc_mcontext.__gregs[4] = regs[4]; + uc.uc_mcontext.__gregs[5] = regs[5]; + uc.uc_mcontext.__gregs[6] = regs[6]; + uc.uc_mcontext.__gregs[7] = regs[7]; + uc.uc_mcontext.__gregs[8] = regs[8]; + uc.uc_mcontext.__gregs[9] = regs[9]; + uc.uc_mcontext.__gregs[10] = regs[10]; + uc.uc_mcontext.__gregs[11] = regs[11]; + uc.uc_mcontext.__gregs[12] = regs[12]; + uc.uc_mcontext.__gregs[13] = regs[13]; + uc.uc_mcontext.__gregs[14] = regs[14]; + uc.uc_mcontext.__gregs[15] = regs[15]; + uc.uc_mcontext.__gregs[16] = regs[16]; + uc.uc_mcontext.__gregs[17] = regs[17]; + uc.uc_mcontext.__gregs[18] = regs[18]; + + /* Preserve the current value of DP */ + uc.uc_mcontext.__gregs[27] = dp; + + /* Set the desired return value. */ + uc.uc_mcontext.__gregs[_REG_RET0] = val; + + /* + * Set _UC_{SET,CLR}STACK according to SS_ONSTACK. + * + * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK, + * since libpthread may want to interpose on signal handling. + */ + uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK); + + sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL); + + /* Clear uc_link */ + uc.uc_link = 0; + + /* Copy signal mask */ + uc.uc_sigmask = sc->sc_mask; + + /* Copy special regs */ + uc.uc_mcontext.__gregs[_REG_PSW] = sc->sc_ps; + uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp; + uc.uc_mcontext.__gregs[_REG_PCSQH] = sc->sc_pcsqh; + uc.uc_mcontext.__gregs[_REG_PCOQH] = sc->sc_pcoqh; + uc.uc_mcontext.__gregs[_REG_PCSQT] = sc->sc_pcsqt; + uc.uc_mcontext.__gregs[_REG_PCOQT] = sc->sc_pcoqt; + + setcontext(&uc); + err: + longjmperror(); + abort(); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/hppa/gen/__setjmp14.S b/lib/nbsd_libc/arch/hppa/gen/__setjmp14.S new file mode 100644 index 000000000..4777b21f8 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/__setjmp14.S @@ -0,0 +1,131 @@ +/* $NetBSD: __setjmp14.S,v 1.5 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matthew Fredette. + * + * 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 +/* XXX fredette - this is awful */ +#define _LOCORE +#include +#undef _LOCORE +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __setjmp14.S,v 1.5 2008/04/28 20:22:55 martin Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +ENTRY(__setjmp14,0) + /* Finish our stack frame. */ + stw %rp, HPPA_FRAME_CRP(%sp) + stw %arg0, HPPA_FRAME_ARG(0)(%sp) + + /* A sigcontext is at the beginning of our jmp_buf. */ + stw %r0, 4(%arg0) ; unused word (old style signal mask) + stw %sp, 8(%arg0) ; sc.sc_sp = %sp + stw %r0, 16(%arg0) ; sc.sc_ap = NULL + mfsp %sr0, %r1 + stw %r1, 20(%arg0) ; sc.sc_pcsqh = %sr0 + stw %rp, 24(%arg0) ; sc.sc_pcoqh = %rp + stw %r1, 28(%arg0) ; sc.sc_pcsqh = %sr0 + ldo 4(%rp), %r1 + stw %r1, 32(%arg0) ; sc.sc_pcoqt = %rp + 4 +#define PSW_MBS (PSW_C|PSW_Q|PSW_P|PSW_D|PSW_I) + ldil L%PSW_MBS, %r1 + ldo R%PSW_MBS(%r1), %r1 + stw %r1, 36(%arg0) ; set sc.sc_ps + + /* We store all callee-saved registers after the sigcontext. */ + ldo 56(%arg0), %r1 + stwm %r3, 4(%r1) + stwm %r4, 4(%r1) + stwm %r5, 4(%r1) + stwm %r6, 4(%r1) + stwm %r7, 4(%r1) + stwm %r8, 4(%r1) + stwm %r9, 4(%r1) + stwm %r10, 4(%r1) + stwm %r11, 4(%r1) + stwm %r12, 4(%r1) + stwm %r13, 4(%r1) + stwm %r14, 4(%r1) + stwm %r15, 4(%r1) + stwm %r16, 4(%r1) + stwm %r17, 4(%r1) + stwm %r18, 4(%r1) + + /* + * Start the stack frame for the calls we will make. + * The minimum frame is 48 bytes, but the stack must + * always be 64-byte aligned; we use the top 4 bytes + * to save our caller's %r3, and the following 12 bytes + * as the stack_t for our sigaltstack call. + * XXX - this assumes that sizeof(stack_t) <= 12 + */ + copy %r3, %r1 + copy %sp, %r3 + stwm %r1, HPPA_FRAME_SIZE(%sp) + + /* Get signal stack info. */ + ldo 4(%r3), %arg1 ; set up oss + bl __sigaltstack14, %rp + copy %r0, %arg0 ; set up ss + + /* Recover our jmp_buf and extract SS_ONSTACK */ + ldw HPPA_FRAME_ARG(0)(%r3), %arg0 + ldw 12(%r3), %r1 ; get ss_flags + ldi 1, %r20 ; SS_ONSTACK + and %r1, %r20, %r1 + stw %r1, 0(%arg0) ; sc.sc_onstack + + /* Get the signal mask. */ + ldo 40(%arg0), %arg2 ; oset = &sc.sc_mask + copy %r0, %arg1 ; set = NULL + bl __sigprocmask14, %rp + copy %r0, %arg0 ; action = 0 + + /* Return 0. */ + ldo HPPA_FRAME_SIZE(%r3),%sp + ldwm -HPPA_FRAME_SIZE(%sp),%r3 + ldw HPPA_FRAME_CRP(%sp), %rp + bv %r0(%rp) + copy %r0, %ret0 +EXIT(__setjmp14) + + .end diff --git a/lib/nbsd_libc/arch/hppa/gen/__sigsetjmp14.S b/lib/nbsd_libc/arch/hppa/gen/__sigsetjmp14.S new file mode 100644 index 000000000..6e1f154e3 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/__sigsetjmp14.S @@ -0,0 +1,67 @@ +/* $NetBSD: __sigsetjmp14.S,v 1.2 2008/04/28 20:22:55 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matthew Fredette. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __sigsetjmp14.S,v 1.2 2008/04/28 20:22:55 martin Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a,m) + * by restoring registers from the stack, + * The previous signal state is restored if 'm' was non-zero. + */ + +/* grab _JBLEN */ +#include + +ENTRY(__sigsetjmp14,0) + stw %arg1, (_JBLEN * 4)(%arg0) ; save mask at end of area + add,<> %r0, %arg0, %r0 + b,n _setjmp + b,n __setjmp14 +EXIT(__sigsetjmp14) + + +ENTRY(__siglongjmp14,0) + ldw (_JBLEN * 4)(%arg0), %r1 + add,<> %r0, %r1, %r0 + b,n _longjmp + b,n __longjmp14 +EXIT(__siglongjmp14) + + .end diff --git a/lib/nbsd_libc/arch/hppa/gen/_lwp.c b/lib/nbsd_libc/arch/hppa/gen/_lwp.c new file mode 100644 index 000000000..4e8cc16b0 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/_lwp.c @@ -0,0 +1,72 @@ +/* $NetBSD: _lwp.c,v 1.4 2010/01/07 12:31:10 skrll Exp $ */ + +/* + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Wayne Knowles + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.4 2010/01/07 12:31:10 skrll Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + caddr_t sp; + __greg_t *gr; + __greg_t *gp; + __greg_t fp; + + getcontext(u); + gr = u->uc_mcontext.__gregs; + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + sp = stack_base + HPPA_FRAME_SIZE; + + fp = (__greg_t)start; + if (fp & 2) { + gp = (__greg_t *)(fp & ~3); + fp = gp[0]; + gr[_REG_R19] = gp[1]; + } + gr[_REG_PCOQH] = fp | HPPA_PC_PRIV_USER; + gr[_REG_PCOQT] = (fp + 4) | HPPA_PC_PRIV_USER; + gr[_REG_RP] = (__greg_t) _lwp_exit; + gr[_REG_ARG0] = (__greg_t) arg; + gr[_REG_SP] = (__greg_t) sp; +} diff --git a/lib/nbsd_libc/arch/hppa/gen/_resumecontext.S b/lib/nbsd_libc/arch/hppa/gen/_resumecontext.S new file mode 100644 index 000000000..5bf827ad9 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/_resumecontext.S @@ -0,0 +1,54 @@ +/* $NetBSD: _resumecontext.S,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Wayne Knowles. + * + * 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 "SYS.h" + +#include +#include + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: _resumecontext.S,v 1.3 2008/04/28 20:22:56 martin Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + + .import _resumecontext, code + +LEAF_ENTRY_NOPROFILE(__resumecontext) +#ifdef PIC + ldw HPPA_FRAME_EDP(%sp), %r19 + addil LT%_C_LABEL(_resumecontext), %r19 + ldw RT%_C_LABEL(_resumecontext)(%r1), %r1 +#else + ldil L%_C_LABEL(_resumecontext), %r1 + ldo R%_C_LABEL(_resumecontext)(%r1), %r1 +#endif + bv,n %r0(%r1) +EXIT(__resumecontext) + .end diff --git a/lib/nbsd_libc/arch/hppa/gen/_setjmp.S b/lib/nbsd_libc/arch/hppa/gen/_setjmp.S new file mode 100644 index 000000000..9d7c74cb4 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/_setjmp.S @@ -0,0 +1,128 @@ +/* $NetBSD: _setjmp.S,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matthew Fredette. + * + * 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 +/* XXX fredette - this is awful */ +#define _LOCORE +#include +#undef _LOCORE + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: _setjmp.S,v 1.4 2008/04/28 20:22:56 martin Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +ENTRY(_setjmp,0) + /* A sigcontext is at the beginning of our jmp_buf. */ + stw %r0, 0(%arg0) ; no onstack + stw %r0, 4(%arg0) ; unused word (old style signal mask) + stw %sp, 8(%arg0) ; sc.sc_sp = %sp + stw %r0, 16(%arg0) ; sc.sc_ap = NULL + mfsp %sr0, %r1 + stw %r1, 20(%arg0) ; sc.sc_pcsqh = %sr0 + stw %rp, 24(%arg0) ; sc.sc_pcoqh = %rp + stw %r1, 28(%arg0) ; sc.sc_pcsqh = %sr0 + ldo 4(%rp), %r1 + stw %r1, 32(%arg0) ; sc.sc_pcoqt = %rp + 4 + stw %r0, 36(%arg0) ; set sc.sc_ps + + /* We store all callee-saved registers after the sigcontext. */ + ldo 56(%arg0), %r1 + stwm %r3, 4(%r1) + stwm %r4, 4(%r1) + stwm %r5, 4(%r1) + stwm %r6, 4(%r1) + stwm %r7, 4(%r1) + stwm %r8, 4(%r1) + stwm %r9, 4(%r1) + stwm %r10, 4(%r1) + stwm %r11, 4(%r1) + stwm %r12, 4(%r1) + stwm %r13, 4(%r1) + stwm %r14, 4(%r1) + stwm %r15, 4(%r1) + stwm %r16, 4(%r1) + stwm %r17, 4(%r1) + stwm %r18, 4(%r1) + + /* Return 0. */ + bv %r0(%rp) + copy %r0, %ret0 +EXIT(_setjmp) + +ENTRY(_longjmp,0) + ldw 8(%arg0), %r1 ; ensure non-zero SP + combt,=,n %r0, %r1, botch ; oops! + add,<> %r0, %arg1, %ret0 ; ensure return value non-zero + ldi 1, %ret0 + + /* restore callee-saved registers */ + ldo 56(%arg0), %r1 + ldwm 4(%r1), %r3 + ldwm 4(%r1), %r4 + ldwm 4(%r1), %r5 + ldwm 4(%r1), %r6 + ldwm 4(%r1), %r7 + ldwm 4(%r1), %r8 + ldwm 4(%r1), %r9 + ldwm 4(%r1), %r10 + ldwm 4(%r1), %r11 + ldwm 4(%r1), %r12 + ldwm 4(%r1), %r13 + ldwm 4(%r1), %r14 + ldwm 4(%r1), %r15 + ldwm 4(%r1), %r16 + ldwm 4(%r1), %r17 + ldwm 4(%r1), %r18 + + /* restore the rest */ + ldw 8(%arg0), %sp + ldw 24(%arg0), %rp + bv %r0(%rp) + nop +botch: + bl longjmperror, %rp + nop + bl abort, %rp + nop +EXIT(_longjmp) + + .end diff --git a/lib/nbsd_libc/arch/hppa/gen/fabs.c b/lib/nbsd_libc/arch/hppa/gen/fabs.c new file mode 100644 index 000000000..f08f504c2 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/fabs.c @@ -0,0 +1,17 @@ +/* $NetBSD: fabs.c,v 1.4 2005/12/24 21:42:32 perry Exp $ */ + +/* $OpenBSD: fabs.c,v 1.3 2002/10/21 18:41:05 mickey Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain + */ + +#include + +double +fabs(double val) +{ + + __asm volatile("fabs,dbl %0,%0" : "+f" (val)); + return (val); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/flt_rounds.c b/lib/nbsd_libc/arch/hppa/gen/flt_rounds.c new file mode 100644 index 000000000..e043d2b77 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/flt_rounds.c @@ -0,0 +1,31 @@ +/* $NetBSD: flt_rounds.c,v 1.4 2005/12/24 21:42:32 perry Exp $ */ + +/* $OpenBSD: flt_rounds.c,v 1.3 2002/10/21 18:41:05 mickey Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.4 2005/12/24 21:42:32 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 0, /* round to zero */ + 2, /* round to positive infinity */ + 3 /* round to negative infinity */ +}; + +int +__flt_rounds(void) +{ + uint64_t fpsr; + + __asm volatile("fstd %%fr0,0(%1)" : "=m" (fpsr) : "r" (&fpsr)); + return map[(fpsr >> 41) & 0x03]; +} diff --git a/lib/nbsd_libc/arch/hppa/gen/fpgetmask.c b/lib/nbsd_libc/arch/hppa/gen/fpgetmask.c new file mode 100644 index 000000000..4ef451c65 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/fpgetmask.c @@ -0,0 +1,24 @@ +/* $NetBSD: fpgetmask.c,v 1.4 2005/12/24 21:42:32 perry Exp $ */ + +/* $OpenBSD: fpgetmask.c,v 1.3 2002/10/21 18:41:05 mickey Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetmask.c,v 1.4 2005/12/24 21:42:32 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +fp_except +fpgetmask(void) +{ + uint64_t fpsr; + + __asm volatile("fstd %%fr0,0(%1)" : "=m" (fpsr) : "r" (&fpsr)); + return ((fpsr >> 32) & 0x1f); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/fpgetround.c b/lib/nbsd_libc/arch/hppa/gen/fpgetround.c new file mode 100644 index 000000000..1629b6498 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/fpgetround.c @@ -0,0 +1,24 @@ +/* $NetBSD: fpgetround.c,v 1.4 2005/12/24 21:42:32 perry Exp $ */ + +/* $OpenBSD: fpgetround.c,v 1.3 2002/10/21 18:41:05 mickey Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.4 2005/12/24 21:42:32 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +fp_rnd +fpgetround(void) +{ + uint64_t fpsr; + + __asm volatile("fstd %%fr0,0(%1)" : "=m" (fpsr) : "r" (&fpsr)); + return ((fpsr >> 41) & 0x3); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/fpgetsticky.c b/lib/nbsd_libc/arch/hppa/gen/fpgetsticky.c new file mode 100644 index 000000000..29ea34aa2 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/fpgetsticky.c @@ -0,0 +1,24 @@ +/* $NetBSD: fpgetsticky.c,v 1.4 2005/12/24 21:42:32 perry Exp $ */ + +/* $OpenBSD: fpgetsticky.c,v 1.3 2002/10/21 18:41:05 mickey Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetsticky.c,v 1.4 2005/12/24 21:42:32 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +fp_except +fpgetsticky(void) +{ + uint64_t fpsr; + + __asm volatile("fstd %%fr0,0(%1)" : "=m" (fpsr) : "r" (&fpsr)); + return ((fpsr >> 59) & 0x1f); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/fpsetmask.c b/lib/nbsd_libc/arch/hppa/gen/fpsetmask.c new file mode 100644 index 000000000..1633f61e4 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/fpsetmask.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpsetmask.c,v 1.5 2009/01/25 16:44:41 skrll Exp $ */ + +/* $OpenBSD: fpsetmask.c,v 1.4 2004/01/05 06:06:16 otto Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetmask.c,v 1.5 2009/01/25 16:44:41 skrll Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +fp_except +fpsetmask(fp_except mask) +{ + uint64_t fpsr; + fp_except old; + + __asm volatile("fstd %%fr0,0(%1)" : "=m"(fpsr) : "r"(&fpsr) : "memory"); + old = (fpsr >> 32) & 0x1f; + fpsr = (fpsr & 0xffffffe000000000LL) | ((uint64_t)(mask & 0x1f) << 32); + __asm volatile("fldd 0(%0),%%fr0" : : "r"(&fpsr) : "memory"); + return (old); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/fpsetround.c b/lib/nbsd_libc/arch/hppa/gen/fpsetround.c new file mode 100644 index 000000000..d9474625c --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/fpsetround.c @@ -0,0 +1,29 @@ +/* $NetBSD: fpsetround.c,v 1.5 2009/01/25 16:44:41 skrll Exp $ */ + +/* $OpenBSD: fpsetround.c,v 1.3 2002/10/21 18:41:05 mickey Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.5 2009/01/25 16:44:41 skrll Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +fp_rnd +fpsetround(fp_rnd rnd_dir) +{ + uint64_t fpsr; + fp_rnd old; + + __asm volatile("fstd %%fr0,0(%1)" : "=m" (fpsr) : "r" (&fpsr) : "memory"); + old = (fpsr >> 41) & 0x03; + fpsr = (fpsr & 0xfffff9ff00000000LL) | + ((uint64_t)(rnd_dir & 0x03) << 41); + __asm volatile("fldd 0(%0),%%fr0" : : "r" (&fpsr) : "memory"); + return (old); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/fpsetsticky.c b/lib/nbsd_libc/arch/hppa/gen/fpsetsticky.c new file mode 100644 index 000000000..472160900 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/fpsetsticky.c @@ -0,0 +1,29 @@ +/* $NetBSD: fpsetsticky.c,v 1.5 2009/01/25 16:44:41 skrll Exp $ */ + +/* $OpenBSD: fpsetsticky.c,v 1.4 2004/01/05 06:06:16 otto Exp $ */ + +/* + * Written by Miodrag Vallat. Public domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetsticky.c,v 1.5 2009/01/25 16:44:41 skrll Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +fp_except +fpsetsticky(fp_except mask) +{ + uint64_t fpsr; + fp_except old; + + __asm volatile("fstd %%fr0,0(%1)" : "=m" (fpsr) : "r" (&fpsr) : "memory"); + old = (fpsr >> 59) & 0x1f; + fpsr = (fpsr & 0x07ffffff00000000LL) | ((uint64_t)(mask & 0x1f) << 59); + __asm volatile("fldd 0(%0),%%fr0" : : "r" (&fpsr) : "memory"); + + return (old); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/makecontext.c b/lib/nbsd_libc/arch/hppa/gen/makecontext.c new file mode 100644 index 000000000..c7cb50d7a --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/makecontext.c @@ -0,0 +1,99 @@ +/* $NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +#include +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + __greg_t *gp, rp, fp; + register __greg_t dp __asm("r27"); + uintptr_t *sp; + int i; + va_list ap; + + void __resumecontext(void); + + /* LINTED uintptr_t is safe */ + sp = (uintptr_t *)ucp->uc_stack.ss_sp; + /* LINTED uintptr_t is safe */ + sp += 16; /* standard frame */ + sp += (argc >= 4 ? argc : 4); /* Make room for >=4 arguments. */ + sp = (uintptr_t *) + ((uintptr_t)(sp + 16) & ~0x3f); /* Align on 64-byte boundary. */ + + /* Save away the registers that we'll need. */ + gr[_REG_SP] = (__greg_t)sp; + rp = (__greg_t)__resumecontext; + if (rp & 2) { + gp = (__greg_t *)(rp & ~3); + rp = gp[0]; + sp[-8] = gp[1]; + } + gr[_REG_RP] = rp; + fp = (__greg_t)func; + if (fp & 2) { + gp = (__greg_t *)(fp & ~3); + fp = gp[0]; + gr[_REG_R19] = gp[1]; + } + gr[_REG_PCOQH] = fp | HPPA_PC_PRIV_USER; + gr[_REG_PCOQT] = (fp + 4) | HPPA_PC_PRIV_USER; + gr[_REG_DP] = dp; + + /* Construct argument list. */ + va_start(ap, argc); + /* Up to the first four arguments are passed in %arg0-3. */ + for (i = 0; i < argc && i < 4; i++) { + /* LINTED uintptr_t is safe */ + gr[_REG_ARG0 - i] = va_arg(ap, uintptr_t); + } + /* Pass remaining arguments on the stack below the %arg0-3 gap. */ + for (; i < argc; i++) { + /* LINTED uintptr_t is safe */ + sp[-9 - i] = va_arg(ap, uintptr_t); + } + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/hppa/gen/nanf.c b/lib/nbsd_libc/arch/hppa/gen/nanf.c new file mode 100644 index 000000000..b95319bf9 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.2 2009/02/22 01:34:01 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.2 2009/02/22 01:34:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0x7f, 0xa0, 0, 0 } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/hppa/gen/resumecontext.c b/lib/nbsd_libc/arch/hppa/gen/resumecontext.c new file mode 100644 index 000000000..03443a32d --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.3 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/hppa/gen/swapcontext.S b/lib/nbsd_libc/arch/hppa/gen/swapcontext.S new file mode 100644 index 000000000..7e5601c1e --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/gen/swapcontext.S @@ -0,0 +1,53 @@ +/* $NetBSD: swapcontext.S,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" +#include + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.4 2008/04/28 20:22:56 martin Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + +LEAF_ENTRY(swapcontext) + stw %rp, HPPA_FRAME_CRP(%sp) + stw %arg0, HPPA_FRAME_ARG(0)(%sp) + stw %arg1, HPPA_FRAME_ARG(1)(%sp) + SYSCALL(getcontext) + + ldw HPPA_FRAME_ARG(0)(%sp), %arg1 + ldw HPPA_FRAME_ARG(1)(%sp), %arg0 + stw %r0, (_OFFSETOF_UC_GREGS + _REG_RET0 * SZREG)(%arg1) + ldo 4(%rp), %r1 + stw %r1, (_OFFSETOF_UC_GREGS + _REG_PCOQT * SZREG)(%arg1) + stw %rp, (_OFFSETOF_UC_GREGS + _REG_PCOQH * SZREG)(%arg1) + SYSCALL(setcontext) +EXIT(swapcontext) + .end diff --git a/lib/nbsd_libc/arch/hppa/net/Makefile.inc b/lib/nbsd_libc/arch/hppa/net/Makefile.inc new file mode 100644 index 000000000..e3e9f6774 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/net/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2002/06/06 20:31:21 fredette Exp $ + +SRCS += ntohl.c ntohs.c htons.c htonl.c + diff --git a/lib/nbsd_libc/arch/hppa/stdlib/Makefile.inc b/lib/nbsd_libc/arch/hppa/stdlib/Makefile.inc new file mode 100644 index 000000000..d1dda0262 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/07/31 20:39:59 dsl Exp $ diff --git a/lib/nbsd_libc/arch/hppa/string/Makefile.inc b/lib/nbsd_libc/arch/hppa/string/Makefile.inc new file mode 100644 index 000000000..f00669afd --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/string/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.3 2009/07/30 20:57:16 dsl Exp $ + +# $OpenBSD: Makefile.inc,v 1.4 1999/09/14 00:46:18 mickey Exp $ + +SRCS+= bcmp.S bzero.S ffs.S + +# NetBSD doesn't currently let archs supply a strlcpy.S. +# plus, the one that fredette made is untested. +#SRCS+= strlcpy.S + diff --git a/lib/nbsd_libc/arch/hppa/string/bcmp.S b/lib/nbsd_libc/arch/hppa/string/bcmp.S new file mode 100644 index 000000000..6e2263fe8 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/string/bcmp.S @@ -0,0 +1,84 @@ +/* $NetBSD: bcmp.S,v 1.2 2003/10/06 05:30:21 matt Exp $ */ + +/* $OpenBSD: bcmp.S,v 1.3 2001/06/04 23:14:02 mickey Exp $ */ + +/* + * Copyright 1996 1995 by Open Software Foundation, Inc. + * All Rights Reserved + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby granted, + * provided that the above copyright notice appears in all copies and + * that both the copyright notice and this permission notice appear in + * supporting documentation. + * + * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, + * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* + * (c) Copyright 1988 HEWLETT-PACKARD COMPANY + * + * To anyone who acknowledges that this file is provided "AS IS" + * without any express or implied warranty: + * permission to use, copy, modify, and distribute this file + * for any purpose is hereby granted without fee, provided that + * the above copyright notice and this notice appears in all + * copies, and that the name of Hewlett-Packard Company not be + * used in advertising or publicity pertaining to distribution + * of the software without specific, written prior permission. + * Hewlett-Packard Company makes no representations about the + * suitability of this software for any purpose. + */ +/* + * Copyright (c) 1990,1991,1994 The University of Utah and + * the Computer Systems Laboratory (CSL). All rights reserved. + * + * THE UNIVERSITY OF UTAH AND CSL PROVIDE THIS SOFTWARE IN ITS "AS IS" + * CONDITION, AND DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES + * WHATSOEVER RESULTING FROM ITS USE. + * + * CSL requests users of this software to return to csl-dist@cs.utah.edu any + * improvements that they make and grant CSL redistribution rights. + * + * Utah Hdr: movc.s 1.26 94/12/14 + */ + +#include + +#ifdef SYSLIBC_SCCS + .text + .asciz "$OpenBSD: bcmp.S,v 1.3 2001/06/04 23:14:02 mickey Exp $" + .align 4 +#endif + +/* + * int + * bcmp(src, dst, count) + * vaddr_t src; + * vaddr_t dst; + * size_t count; + */ +LEAF_ENTRY(bcmp) + comb,>= %r0,%arg2,$bcmp_exit + copy %t1,%t2 + +$bcmp_loop: + ldbs,ma 1(%arg0),%t1 + ldbs,ma 1(%arg1),%t2 + comb,<>,n %t1,%t2,$bcmp_exit + addib,<> -1,%arg2,$bcmp_loop + nop + +$bcmp_exit: + bv 0(%r2) + sub %t1,%t2,%ret0 + +EXIT(bcmp) + diff --git a/lib/nbsd_libc/arch/hppa/string/bzero.S b/lib/nbsd_libc/arch/hppa/string/bzero.S new file mode 100644 index 000000000..c14de6205 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/string/bzero.S @@ -0,0 +1,119 @@ +/* $NetBSD: bzero.S,v 1.2 2003/10/06 05:30:21 matt Exp $ */ + +/* $OpenBSD: bzero.S,v 1.3 2001/06/04 23:14:02 mickey Exp $ */ + +/* + * (c) Copyright 1988 HEWLETT-PACKARD COMPANY + * + * To anyone who acknowledges that this file is provided "AS IS" + * without any express or implied warranty: + * permission to use, copy, modify, and distribute this file + * for any purpose is hereby granted without fee, provided that + * the above copyright notice and this notice appears in all + * copies, and that the name of Hewlett-Packard Company not be + * used in advertising or publicity pertaining to distribution + * of the software without specific, written prior permission. + * Hewlett-Packard Company makes no representations about the + * suitability of this software for any purpose. + */ +/* + * Copyright (c) 1990,1994 The University of Utah and + * the Computer Systems Laboratory (CSL). All rights reserved. + * + * THE UNIVERSITY OF UTAH AND CSL PROVIDE THIS SOFTWARE IN ITS "AS IS" + * CONDITION, AND DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES + * WHATSOEVER RESULTING FROM ITS USE. + * + * CSL requests users of this software to return to csl-dist@cs.utah.edu any + * improvements that they make and grant CSL redistribution rights. + * + * Utah $Hdr: bzero.s 1.9 94/12/14$ + * Author: Bob Wheeler, University of Utah CSL + */ + +#include + +#ifdef SYSLIBC_SCCS + .text + .asciz "$OpenBSD: bzero.S,v 1.3 2001/06/04 23:14:02 mickey Exp $" + .align 4 +#endif + +/* + * void + * bzero(dst, count) + * void *dst; + * size_t count; + */ +LEAF_ENTRY(bzero) + comb,>=,n %r0,%arg1,$bzero_exit + + /* + * If we need to clear less than a word do it a byte at a time + */ + + comib,>>,n 4,%arg1,$bzero_bytes + + /* + * Since we are only clearing memory the alignment restrictions + * are simplified. Figure out how many "extra" bytes we need to + * store with stbys. + */ + + extru %arg0,31,2,%t1 + add %arg1,%t1,%arg1 + + /* + * We will zero the destination in blocks of 16 bytes as long as we + * can and then we'll go to the 4 byte moves. + */ + + comib,>>= 15, %arg1, $bzero_word + addi -16, %arg1, %arg1 + +$bzero_loop_16: + stbys,b,m %r0,4(%arg0) + stwm %r0,4(%arg0) + stwm %r0,4(%arg0) + stwm %r0,4(%arg0) + comib,<< 15, %arg1, $bzero_loop_16 + addi -16, %arg1, %arg1 + + /* + * see if there is anything left that needs to be zeroed in a word + * move. Since the count was decremented by 16, add 12 to test if + * there are any full word moves left to do. + */ + +$bzero_word: + addib,<,n 12,%arg1,$bzero_cleanup + +$bzero_loop_4: + addib,>= -4,%arg1,$bzero_loop_4 + stbys,b,m %r0,4(%arg0) + + /* + * zero the last bytes that may be unaligned on a word boundary + */ + +$bzero_cleanup: + addib,=,n 4,%arg1,$bzero_exit + add %arg0,%arg1,%arg0 + b $bzero_exit + stbys,e %r0,0(%arg0) + b,n $bzero_exit + + + /* + * zero by bytes + */ + +$bzero_bytes: + addib,> -1,%arg1,$bzero_bytes + stbs,ma %r0,1(%arg0) + +$bzero_exit: + bv,n %r0(%rp) +EXIT(bzero) + + .end diff --git a/lib/nbsd_libc/arch/hppa/string/ffs.S b/lib/nbsd_libc/arch/hppa/string/ffs.S new file mode 100644 index 000000000..a4754a721 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/string/ffs.S @@ -0,0 +1,63 @@ +/* $NetBSD: ffs.S,v 1.3 2004/07/18 20:30:04 chs Exp $ */ + +/* $OpenBSD: ffs.S,v 1.3 2001/06/04 23:14:02 mickey Exp $ */ + +/* + * Copyright (c) 1990, 1991, 1992, 1994, The University of Utah and + * the Computer Systems Laboratory at the University of Utah (CSL). + * All rights reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS + * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF + * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * CSL requests users of this software to return to csl-dist@cs.utah.edu any + * improvements that they make and grant CSL redistribution rights. + * + */ + +#include + +#ifdef SYSLIBC_SCCS + .text + .asciz "$OpenBSD: ffs.S,v 1.3 2001/06/04 23:14:02 mickey Exp $" + .align 4 +#endif + +/* + * ffs(bitmask) + * + * Return the position of the "most significant" bit in `bitmask'. + * Since this is similar to the VAX ffs instruction, bits in a word + * are numbered as "32, 31, ... 1", 0 is returned if no bits are set. + */ + +LEAF_ENTRY(ffs) + comb,= %arg0,%r0,ffsdone ; If arg0 is 0 + or %r0,%r0,%ret0 ; return 0 + ldi 32,%ret0 ; Set return to high bit + extru,= %arg0,31,16,%r0 ; If low 16 bits are non-zero + addi,tr -16,%ret0,%ret0 ; subtract 16 from bitpos + shd %r0,%arg0,16,%arg0 ; else shift right 16 bits + extru,= %arg0,31,8,%r0 ; If low 8 bits are non-zero + addi,tr -8,%ret0,%ret0 ; subtract 8 from bitpos + shd %r0,%arg0,8,%arg0 ; else shift right 8 bits + extru,= %arg0,31,4,%r0 ; If low 4 bits are non-zero + addi,tr -4,%ret0,%ret0 ; subtract 4 from bitpos + shd %r0,%arg0,4,%arg0 ; else shift right 4 bits + extru,= %arg0,31,2,%r0 ; If low 2 bits are non-zero + addi,tr -2,%ret0,%ret0 ; subtract 2 from bitpos + shd %r0,%arg0,2,%arg0 ; else shift right 2 bits + extru,= %arg0,31,1,%r0 ; If low bit is non-zero + addi -1,%ret0,%ret0 ; subtract 1 from bitpos +ffsdone: + bv,n %r0(%rp) +EXIT(ffs) + + .end diff --git a/lib/nbsd_libc/arch/hppa/string/strlcpy.S b/lib/nbsd_libc/arch/hppa/string/strlcpy.S new file mode 100644 index 000000000..ec199b13a --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/string/strlcpy.S @@ -0,0 +1,84 @@ +/* $NetBSD: strlcpy.S,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/* + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matthew Fredette. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: strlcpy.S,v 1.3 2008/04/28 20:22:56 martin Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * size_t strlcpy(char *dst, const char *src, size_t size); + */ +LEAF_ENTRY(_strlcpy) + .weak strlcpy + strlcpy = _strlcpy + + /* Always load the first byte of the source string. */ + ldbs,ma 1(%arg1), %r1 + + /* + * If our dst buffer is zero bytes, branch immediately + * to the code that counts the (remaining) length + * of the src string. + */ + comb,= %r0, %arg2, $strlcpy_dst_done + copy %arg1, %ret0 + + /* + * If our dst buffer is one byte, branch immediately + * to the code that NUL-terminates the dst buffer and + * counts the (remaining) length of the src string. + */ + addib,=,n -1, %arg2, $strlcpy_dst_full + + /* + * Loop copying bytes. At the top of this loop, + * %arg2 is always the number of bytes we can still + * store, and %r1 holds the next byte to store. + */ +$strlcpy_loop: + comb,= %r0, %r1, $strlcpy_exit + stbs,ma %r1, 1(%arg0) + addib,<> -1, %arg2, $strlcpy_loop + ldbs,ma 1(%arg1), %r1 + +$strlcpy_dst_full: + stbs,ma %r0, 1(%arg0) + +$strlcpy_dst_done: + comb,<>,n %r0, %r1, $strlcpy_dst_done + ldbs,ma 1(%arg1), %r1 + +$strlcpy_exit: + bv %r0(%rp) + sub %arg1, %ret0, %ret0 +EXIT(strlcpy) diff --git a/lib/nbsd_libc/arch/hppa/sys/__clone.S b/lib/nbsd_libc/arch/hppa/sys/__clone.S new file mode 100644 index 000000000..b3ee7a2ea --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/__clone.S @@ -0,0 +1,87 @@ +/* $NetBSD: __clone.S,v 1.6 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS + WEAK_ALIAS(clone, __clone) +#endif + + .import __cerror, code + +/* + * int clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone, 0) + + /* + * Sanity checks: func and stack may not be NULL. + */ + ldi EINVAL, %t1 + comb,= %r0, %arg0, __cerror + nop + comb,= %r0, %arg1, __cerror + nop + + /* + * Put the func and arg arguments into a frame in the child's stack. + */ + ldo (HPPA_FRAME_SIZE * 2)(%arg1), %arg1 + stw %arg0, HPPA_FRAME_ARG(0)(%arg1) + stw %arg3, HPPA_FRAME_ARG(1)(%arg1) + + /* + * The system call expects (flags, stack). + */ + copy %arg2, %arg0 + SYSCALL(__clone) + comb,<>,n %r0, %ret1, 9f + bv,n %r0(%rp) + +9: /* + * Child: Reload the function and argument from the new stack. + */ + ldw HPPA_FRAME_ARG(0)(%sp), %r22 + ldw HPPA_FRAME_ARG(1)(%sp), %arg0 + + /* Call the clone's entry point. */ + stw %r19, HPPA_FRAME_ARG(2)(%sp) + bl $$dyncall, %r31 + copy %r31, %rp + ldw HPPA_FRAME_ARG(2)(%sp), %r19 + + /* Pass the return value to _exit(). */ + copy %ret0, %arg0 + PIC_CALL(_exit) + + /* NOTREACHED */ +EXIT(__clone) diff --git a/lib/nbsd_libc/arch/hppa/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/hppa/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..dbfb30046 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/__sigaction14_sigtramp.c @@ -0,0 +1,53 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.4 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern int __sigtramp_siginfo_2[]; + + return (__sigaction_sigtramp(sig, act, oact, + __sigtramp_siginfo_2, 2)); +} diff --git a/lib/nbsd_libc/arch/hppa/sys/__sigtramp2.S b/lib/nbsd_libc/arch/hppa/sys/__sigtramp2.S new file mode 100644 index 000000000..7c047ba8b --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/__sigtramp2.S @@ -0,0 +1,133 @@ +/* $NetBSD: __sigtramp2.S,v 1.4 2009/11/03 05:07:25 snj Exp $ */ + +/* + * Copyright (c) 1998-2001 Michael Shalayeff + * 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 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 OR HIS RELATIVES 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 MIND, 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. + * + * Portitions of this file are derived from other sources, see + * the copyrights and acknowledgements below. + */ +/* + * Copyright (c) 1990,1991,1992,1994 The University of Utah and + * the Computer Systems Laboratory (CSL). All rights reserved. + * + * THE UNIVERSITY OF UTAH AND CSL PROVIDE THIS SOFTWARE IN ITS "AS IS" + * CONDITION, AND DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES + * WHATSOEVER RESULTING FROM ITS USE. + * + * CSL requests users of this software to return to csl-dist@cs.utah.edu any + * improvements that they make and grant CSL redistribution rights. + * + * Utah $Hdr: locore.s 1.62 94/12/15$ + */ +/* + * (c) Copyright 1988 HEWLETT-PACKARD COMPANY + * + * To anyone who acknowledges that this file is provided "AS IS" + * without any express or implied warranty: + * permission to use, copy, modify, and distribute this file + * for any purpose is hereby granted without fee, provided that + * the above copyright notice and this notice appears in all + * copies, and that the name of Hewlett-Packard Company not be + * used in advertising or publicity pertaining to distribution + * of the software without specific, written prior permission. + * Hewlett-Packard Company makes no representations about the + * suitability of this software for any purpose. + */ + +#include "SYS.h" + +/* + * The hppa signal trampoline is required to call the handler + * possibily via a PLABEL. + * + * On entry, stack looks like: + * + * sp-> [x] + * r3,arg2-> ucontext structure + * arg1-> siginfo structure [0] + * + * where + * x = HPPA_FRAME_ROUND(sizeof(sigframe_siginfo)) + * + * and + * struct sigframe_siginfo { + * siginfo_t sf_si; + * ucontext_t sf_uc; + * }; + */ + +ENTRY_NOPROFILE(__sigtramp_siginfo_2, 0) + .call + + /* + * Our sendsig() places the address of the signal handler in %arg3. + * It may actually be a PLABEL. + */ + bb,>=,n %arg3, 30, L$sigcode_bounce ; branch if not a PLABEL + depi 0, 31, 2, %arg3 ; zero L bit in PLABEL pointer + ldw 4(%arg3), %r19 ; load shared library linkage + ldw 0(%arg3), %arg3 ; load real catcher address + +L$sigcode_bounce: + + /* + * This blr puts the address of the following nop in rp. + * It also schedules the nop for execution, which is why + * that instruction has to be a nop, or, rather, not any + * instruction only meant to execute once the signal handler + * returns. + */ + blr %r0, %rp + + /* + * This bv schedules the instruction pointed to by arg3 + * for execution. So, arg3 is the address of the signal + * handler. + */ + bv,n %r0(%arg3) + nop + + /* + * The signal handler has returned. Since r3 is on the list + * of callee-saved registers, it's whatever the sendsig + * code wanted it set to. Since we copy it into arg0, + * it looks like sendsig leaves r3 holding the desired + * single argument to sys_setcontext, i.e., the ucontext_t *. + */ + + /* Make a SYS_setcontext system call. */ + copy %r3, %arg0 + ldil L%SYSCALLGATE, %r1 + .call + ble 4(%sr7, %r1) + ldi SYS_setcontext, %t1 + + /* Make a SYS_exit system call. */ + copy %ret0, %arg0 + ldil L%SYSCALLGATE, %r1 + .call + ble 4(%sr7, %r1) + ldi SYS_exit, %t1 +EXIT(__sigtramp_siginfo_2) diff --git a/lib/nbsd_libc/arch/hppa/sys/__syscall.S b/lib/nbsd_libc/arch/hppa/sys/__syscall.S new file mode 100644 index 000000000..bdef76fdd --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/__syscall.S @@ -0,0 +1,4 @@ +/* $NetBSD: __syscall.S,v 1.1 2002/06/06 20:31:23 fredette Exp $ */ + +#include "SYS.h" +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/hppa/sys/__vfork14.S b/lib/nbsd_libc/arch/hppa/sys/__vfork14.S new file mode 100644 index 000000000..770ccf96a --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/__vfork14.S @@ -0,0 +1,66 @@ +/* $NetBSD: __vfork14.S,v 1.5 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + +ENTRY(__vfork14, 0) + /* + * NB: __vfork14 is a tricky syscall. We can't save + * any values on the stack, because the stack will be + * trashed by the child, leaving garbage for when the + * parent returns. + * + * So we must save values in registers, specifically, + * registers that the kernel is willing to preserve + * across the syscall. Normally, this would be callee- + * saved registers, with the kernel being the callee, + * but the problem is that we, too, are a callee, and + * would have to save that very same register somewhere, + * because *our* caller is counting on us to do so. + * + * What we need is a normally caller-saved register, + * that the kernel is willing to save for us. The + * syscall entry code in locore.S has been modified + * to do just this for the t4 register. + */ + .import __cerror, code + + copy %rp, %t4 + ldil L%SYSCALLGATE, %r1 + ble 4(%sr7, %r1) + ldi SYS___vfork14, %t1 + comb,<> %r0, %t1, __cerror + copy %t4, %rp + addi -1, %ret1, %ret1 + bv %r0(%rp) + and %ret0, %ret1, %ret0 +EXIT(__vfork14) diff --git a/lib/nbsd_libc/arch/hppa/sys/brk.S b/lib/nbsd_libc/arch/hppa/sys/brk.S new file mode 100644 index 000000000..61ce62527 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/brk.S @@ -0,0 +1,78 @@ +/* $NetBSD: brk.S,v 1.4 2009/11/03 05:07:25 snj Exp $ */ + +/* $OpenBSD: brk.S,v 1.7 2001/06/04 23:14:04 mickey Exp $ */ + +/* + * Copyright (c) 1999 Michael Shalayeff + * 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 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 OR HIS RELATIVES 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 MIND, 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: brk.S,v 1.4 2009/11/03 05:07:25 snj Exp $") +#endif /* LIBC_SCCS and not lint */ + + .import curbrk, data + .import end, data + +#ifdef WEAK_ALIAS + WEAK_ALIAS(brk, _brk) +#endif + + .data + .export __minbrk, data +__minbrk: + .long end + +ENTRY(_brk,0) +#ifdef PIC + addil LT%__minbrk, %r19 + ldw RT%__minbrk(%r1), %r1 + ldw 0(%r1), %t2 +#else /* !PIC */ + ldil L%__minbrk, %t1 + ldw R%__minbrk(%t1), %t2 +#endif /* !PIC */ + sub,<< %t2, %arg0, %r0 + copy %t2, %arg0 + stw %arg0, HPPA_FRAME_ARG(0)(%sp) + + SYSCALL(break) + + ldw HPPA_FRAME_ARG(0)(%sp), %arg0 +#ifdef PIC + addil LT%curbrk, %r19 + ldw RT%curbrk(%r1), %r1 + bv %r0(%rp) + stw %arg0, 0(%r1) +#else /* !PIC */ + ldil L%curbrk, %t1 + bv %r0(%rp) + stw %arg0, R%curbrk(%t1) +#endif /* !PIC */ +EXIT(brk) + + .end + diff --git a/lib/nbsd_libc/arch/hppa/sys/cerror.S b/lib/nbsd_libc/arch/hppa/sys/cerror.S new file mode 100644 index 000000000..f55b042cb --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/cerror.S @@ -0,0 +1,62 @@ +/* $NetBSD: cerror.S,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nick Hudson + * + * 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 "SYS.h" + +#ifdef _REENTRANT +ENTRY(__cerror,HPPA_FRAME_SIZE) +#else +LEAF_ENTRY(__cerror) +#endif +#ifdef _REENTRANT + stw %rp, HPPA_FRAME_CRP(%sp) + stw %t1, HPPA_FRAME_ARG(0)(%sp) + ldo HPPA_FRAME_SIZE(%sp),%sp + bl __errno, %rp + nop + ldo -HPPA_FRAME_SIZE(%sp), %sp + ldw HPPA_FRAME_ARG(0)(%sp), %t1 + ldw HPPA_FRAME_CRP(%sp), %rp + stw %t1, 0(%ret0) +#else +#ifdef PIC + addil LT%errno, %r19 + ldw RT%errno(%r1), %r1 + stw %t1, 0(%r1) +#else + ldil L%errno, %r1 + stw %t1, R%errno(%r1) +#endif +#endif + ldi -1, %ret0 + bv %r0(%rp) + ldi -1, %ret1 +EXIT(__cerror) diff --git a/lib/nbsd_libc/arch/hppa/sys/exect.S b/lib/nbsd_libc/arch/hppa/sys/exect.S new file mode 100644 index 000000000..5bb0ce1e5 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/exect.S @@ -0,0 +1,40 @@ +/* $NetBSD: exect.S,v 1.2 2009/11/03 05:07:25 snj Exp $ */ + +/* $OpenBSD: exect.S,v 1.5 2001/03/29 01:43:52 mickey Exp $ */ + +/* + * Copyright (c) 1999 Michael Shalayeff + * 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 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 OR HIS RELATIVES 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 MIND, 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: exect.S,v 1.2 2009/11/03 05:07:25 snj Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* we do no tracing yet */ +PSEUDO(exect,execve) + + .end diff --git a/lib/nbsd_libc/arch/hppa/sys/fork.S b/lib/nbsd_libc/arch/hppa/sys/fork.S new file mode 100644 index 000000000..0ee7ddf54 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/fork.S @@ -0,0 +1,44 @@ +/* $NetBSD: fork.S,v 1.4 2009/11/03 05:07:25 snj Exp $ */ + +/* $OpenBSD: fork.S,v 1.7 2001/06/04 23:14:04 mickey Exp $ */ + +/* + * Copyright (c) 1999 Michael Shalayeff + * 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 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 OR HIS RELATIVES 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 MIND, 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: fork.S,v 1.4 2009/11/03 05:07:25 snj Exp $") +#endif /* LIBC_SCCS and not lint */ + +ENTRY(__fork,0) + SYSCALL(fork) + addi -1, %ret1, %ret1 + bv %r0(%rp) + and %ret0, %ret1, %ret0 +EXIT(__fork) + + .end diff --git a/lib/nbsd_libc/arch/hppa/sys/getcontext.S b/lib/nbsd_libc/arch/hppa/sys/getcontext.S new file mode 100644 index 000000000..e1a6aa692 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/getcontext.S @@ -0,0 +1,50 @@ +/* $NetBSD: getcontext.S,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +#define _OFFSETOF_UC_GREGS (10 * 4) +#define SZREG 4 + +ENTRY(_getcontext, 0) + SYSCALL(getcontext) + stw %rp, (_OFFSETOF_UC_GREGS + _REG_PCOQH * SZREG)(%arg0) + ldo 4(%rp), %r1 + stw %r1, (_OFFSETOF_UC_GREGS + _REG_PCOQT * SZREG)(%arg0) + bv %r0(%rp) + stw %r0, (_OFFSETOF_UC_GREGS + _REG_RET0 * SZREG)(%arg0) +EXIT(_getcontext) diff --git a/lib/nbsd_libc/arch/hppa/sys/pipe.S b/lib/nbsd_libc/arch/hppa/sys/pipe.S new file mode 100644 index 000000000..00970ae79 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/pipe.S @@ -0,0 +1,58 @@ +/* $NetBSD: pipe.S,v 1.3 2003/10/06 05:30:21 matt Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)pipe.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: pipe.S,v 1.3 2003/10/06 05:30:21 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef WEAK_ALIAS + WEAK_ALIAS(pipe, _pipe) +#endif + +ENTRY(_pipe,0) + stw %arg0, HPPA_FRAME_ARG(0)(%sp) + SYSCALL(pipe) + ldw HPPA_FRAME_ARG(0)(%sp), %arg0 + stw %ret0, 0(%arg0) + stw %ret1, 4(%arg0) + bv %r0(%rp) + copy %r0, %ret0 +EXIT(_pipe) diff --git a/lib/nbsd_libc/arch/hppa/sys/ptrace.S b/lib/nbsd_libc/arch/hppa/sys/ptrace.S new file mode 100644 index 000000000..623525993 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/ptrace.S @@ -0,0 +1,59 @@ +/* $NetBSD: ptrace.S,v 1.6 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + + .import __cerror, code + +/* + * int ptrace(int request, pid_t pid, void * addr, int data); + */ + +ENTRY(ptrace, HPPA_FRAME_SIZE) + stw %rp, HPPA_FRAME_CRP(%sp) + stw %arg0, HPPA_FRAME_ARG(0)(%sp) + stw %arg1, HPPA_FRAME_ARG(1)(%sp) + stw %arg2, HPPA_FRAME_ARG(2)(%sp) + stw %arg3, HPPA_FRAME_ARG(3)(%sp) + ldo HPPA_FRAME_SIZE(%sp),%sp + bl __cerror, %rp + copy %r0, %t1 + ldo -HPPA_FRAME_SIZE(%sp), %sp + ldw HPPA_FRAME_ARG(0)(%sp), %arg0 + ldw HPPA_FRAME_ARG(1)(%sp), %arg1 + ldw HPPA_FRAME_ARG(2)(%sp), %arg2 + ldw HPPA_FRAME_ARG(3)(%sp), %arg3 + ldw HPPA_FRAME_CRP(%sp), %rp + + SYSCALL(ptrace) + bv,n %r0(%rp) +EXIT(ptrace) diff --git a/lib/nbsd_libc/arch/hppa/sys/sbrk.S b/lib/nbsd_libc/arch/hppa/sys/sbrk.S new file mode 100644 index 000000000..963d2519a --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/sbrk.S @@ -0,0 +1,76 @@ +/* $NetBSD: sbrk.S,v 1.4 2009/11/03 05:07:25 snj Exp $ */ + +/* $OpenBSD: sbrk.S,v 1.7 2001/06/04 23:14:04 mickey Exp $ */ + +/* + * Copyright (c) 1999 Michael Shalayeff + * 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 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 OR HIS RELATIVES 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 MIND, 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: sbrk.S,v 1.4 2009/11/03 05:07:25 snj Exp $") +#endif /* LIBC_SCCS and not lint */ + + .import end, data + +#ifdef WEAK_ALIAS + WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data + .export curbrk, data +curbrk: + .long end + +ENTRY(_sbrk,0) +#ifdef PIC + addil LT%curbrk, %r19 + ldw RT%curbrk(%r1), %r1 + ldw 0(%r1), %t2 +#else /* !PIC */ + ldil L%curbrk, %t1 + ldw R%curbrk(%t1), %t2 +#endif /* !PIC */ + add %arg0, %t2, %arg0 + stw %arg0, HPPA_FRAME_ARG(0)(%sp) + + SYSCALL(break) + +#ifdef PIC + addil LT%curbrk, %r19 + ldw RT%curbrk(%r1), %r1 +#else /* !PIC */ + ldil L%curbrk, %t1 + ldo R%curbrk(%t1), %r1 +#endif /* !PIC */ + ldw 0(%r1), %ret0 + ldw HPPA_FRAME_ARG(0)(%sp), %arg0 + bv %r0(%rp) + stw %arg0, 0(%r1) + +EXIT(sbrk) + + .end diff --git a/lib/nbsd_libc/arch/hppa/sys/shmat.S b/lib/nbsd_libc/arch/hppa/sys/shmat.S new file mode 100644 index 000000000..0eaf832c3 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.3 2004/07/18 20:48:04 chs Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/hppa/sys/syscall.S b/lib/nbsd_libc/arch/hppa/sys/syscall.S new file mode 100644 index 000000000..fbf8ee412 --- /dev/null +++ b/lib/nbsd_libc/arch/hppa/sys/syscall.S @@ -0,0 +1,39 @@ +/* $NetBSD: syscall.S,v 1.2 2009/11/03 05:07:25 snj Exp $ */ + +/* $OpenBSD: syscall.S,v 1.4 2001/03/29 01:43:53 mickey Exp $ */ + +/* + * Copyright (c) 1999 Michael Shalayeff + * 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 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 OR HIS RELATIVES 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 MIND, 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: syscall.S,v 1.2 2009/11/03 05:07:25 snj Exp $") +#endif /* LIBC_SCCS and not lint */ + +RSYSCALL(syscall) + + .end diff --git a/lib/nbsd_libc/arch/i386/Makefile.inc b/lib/nbsd_libc/arch/i386/Makefile.inc new file mode 100644 index 000000000..6b78a3bbf --- /dev/null +++ b/lib/nbsd_libc/arch/i386/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.20 2006/06/17 18:04:23 uwe Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S diff --git a/lib/nbsd_libc/arch/i386/SYS.h b/lib/nbsd_libc/arch/i386/SYS.h new file mode 100644 index 000000000..11e06ab52 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/SYS.h @@ -0,0 +1,152 @@ +/* $NetBSD: SYS.h,v 1.23 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Doran. + * + * 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. + */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)SYS.h 5.5 (Berkeley) 5/7/91 + */ + +#include +#include + +#ifdef __STDC__ +#define _SYSNAM(x) $(SYS_ ## x) +#else +#define _SYSNAM(x) $(SYS_/**/x) +#endif + +#define OSYSTRAP(x) \ + movl _SYSNAM(x),%eax ;\ + int $0x80 + +#ifdef I686_LIBC +#define SYSTRAP(x) \ + pushl %ebx ;\ + movl _SYSNAM(x),%eax ;\ + movl $123f,%edx ;\ + movl %esp,%ecx ;\ + sysenter ;\ +123: movl %ebx,%edx ;\ + popl %ebx +#else /* I686_LIBC */ +#define SYSTRAP(x) OSYSTRAP(x) +#endif /* I686_LIBC */ + +#ifdef __ELF__ +#define CERROR _C_LABEL(__cerror) +#define CURBRK _C_LABEL(__curbrk) +#else +#define CERROR _ASM_LABEL(cerror) +#define CURBRK _ASM_LABEL(curbrk) +#endif + +#define _SYSCALL_NOERROR(x,y) \ + ENTRY(x); \ + SYSTRAP(y) + +#ifdef PIC +#define _SYSCALL_ERR \ + PIC_PROLOGUE; \ + mov PIC_GOT(CERROR), %ecx; \ + PIC_EPILOGUE; \ + jmp *%ecx +#else +#define _SYSCALL_ERR \ + jmp CERROR +#endif + +#define _SYSCALL(x,y) \ + .text; _ALIGN_TEXT; \ + 2: _SYSCALL_ERR; \ + _SYSCALL_NOERROR(x,y); \ + jc 2b + +#define SYSCALL_NOERROR(x) \ + _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) \ + _SYSCALL(x,x) + +#define PSEUDO_NOERROR(x,y) \ + _SYSCALL_NOERROR(x,y); \ + ret + +#define PSEUDO(x,y) \ + _SYSCALL_NOERROR(x,y); \ + jc 2f; \ + ret; \ + 2: _SYSCALL_ERR + +#define RSYSCALL_NOERROR(x) \ + PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) \ + PSEUDO(x,x) + +#ifdef WEAK_ALIAS +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) +#else +#define WSYSCALL(weak,strong) \ + PSEUDO(weak,weak) +#endif + + .globl CERROR diff --git a/lib/nbsd_libc/arch/i386/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/i386/gdtoa/Makefile.inc new file mode 100644 index 000000000..ce2f4854d --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gdtoa/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c strtold_px.c +SRCS+= strtopx.c diff --git a/lib/nbsd_libc/arch/i386/gdtoa/arith.h b/lib/nbsd_libc/arch/i386/gdtoa/arith.h new file mode 100644 index 000000000..1513f1fd2 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_LITTLE_ENDIAN diff --git a/lib/nbsd_libc/arch/i386/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/i386/gdtoa/gd_qnan.h new file mode 100644 index 000000000..b316779ef --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gdtoa/gd_qnan.h @@ -0,0 +1,11 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff80000 +#define ldus_QNAN0 0x0 +#define ldus_QNAN1 0x0 +#define ldus_QNAN2 0x0 +#define ldus_QNAN3 0x4000 +#define ldus_QNAN4 0x7fff +/* 2 bytes of tail padding follow, per i386 ABI */ diff --git a/lib/nbsd_libc/arch/i386/gen/Makefile.inc b/lib/nbsd_libc/arch/i386/gen/Makefile.inc new file mode 100644 index 000000000..4c845e75e --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/Makefile.inc @@ -0,0 +1,38 @@ +# $NetBSD: Makefile.inc,v 1.26 2009/12/06 07:12:17 uebayasi Exp $ + +# objects built from assembler sources (need lint stubs) +SRCS+= alloca.S byte_swap_2.S byte_swap_4.S fabs.S \ + flt_rounds.S fpgetmask.S fpgetround.S fpgetsticky.S \ + fpsetmask.S fpsetround.S fpsetsticky.S + +SRCS+= setjmp.S _setjmp.S sigsetjmp.S + +SRCS+= resumecontext.S swapcontext.S + +# objects built from C sources +SRCS+= bswap64.c _lwp.c makecontext.c + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c infinityl.c +SRCS+= fpclassifyl.c isfinitel.c isinfl.c isnanl.c signbitl.c + +# "internal" objects (don't provide part of the user-visible API) +SRCS+= divsi3.S fixdfsi.S fixunsdfsi.S udivsi3.S + +LSRCS.i386.gen=\ + Lint__setjmp.c Lint_alloca.c Lint_bswap16.c Lint_bswap32.c \ + Lint_fabs.c \ + Lint___setjmp14.c Lint___sigsetjmp14.c Lint_flt_rounds.c \ + Lint_fpgetmask.c Lint_fpgetround.c Lint_fpgetsticky.c \ + Lint_fpsetmask.c Lint_fpsetround.c Lint_fpsetsticky.c \ + Lint_resumecontext.c Lint_swapcontext.c Lint__setjmp.c +LSRCS+= ${LSRCS.i386.gen} +DPSRCS+= ${LSRCS.i386.gen} +CLEANFILES+= ${LSRCS.i386.gen} diff --git a/lib/nbsd_libc/arch/i386/gen/_lwp.c b/lib/nbsd_libc/arch/i386/gen/_lwp.c new file mode 100644 index 000000000..0837709bd --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/_lwp.c @@ -0,0 +1,70 @@ +/* $NetBSD: _lwp.c,v 1.6 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nathan J. Williams. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.6 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + /* LINTED uintptr_t is safe */ + u->uc_mcontext.__gregs[_REG_EIP] = (uintptr_t)start; + + /* Align to a word */ + /* LINTED uintptr_t is safe */ + sp = (void **) ((uintptr_t)(stack_base + stack_size) & ~0x3); + + *--sp = arg; + *--sp = (void *) _lwp_exit; + + /* LINTED uintptr_t is safe */ + u->uc_mcontext.__gregs[_REG_UESP] = (uintptr_t) sp; + + /* LINTED private is currently unused */ +} diff --git a/lib/nbsd_libc/arch/i386/gen/_setjmp.S b/lib/nbsd_libc/arch/i386/gen/_setjmp.S new file mode 100644 index 000000000..1fb2366bc --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/_setjmp.S @@ -0,0 +1,77 @@ +/* $NetBSD: _setjmp.S,v 1.8 2005/10/05 20:18:12 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)_setjmp.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: _setjmp.S,v 1.8 2005/10/05 20:18:12 christos Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from the last call to + * _setjmp(a) + * by restoring registers from the stack. + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + movl 4(%esp),%eax + movl 0(%esp),%edx + movl %edx, 0(%eax) /* rta */ + movl %ebx, 4(%eax) + movl %esp, 8(%eax) + movl %ebp,12(%eax) + movl %esi,16(%eax) + movl %edi,20(%eax) + xorl %eax,%eax + ret + +ENTRY(_longjmp) + movl 4(%esp),%edx + movl 8(%esp),%eax + movl 0(%edx),%ecx + movl 4(%edx),%ebx + movl 8(%edx),%esp + movl 12(%edx),%ebp + movl 16(%edx),%esi + movl 20(%edx),%edi + testl %eax,%eax + jnz 1f + incl %eax +1: movl %ecx,0(%esp) + ret diff --git a/lib/nbsd_libc/arch/i386/gen/alloca.S b/lib/nbsd_libc/arch/i386/gen/alloca.S new file mode 100644 index 000000000..1ceb4577d --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/alloca.S @@ -0,0 +1,56 @@ +/* $NetBSD: alloca.S,v 1.8 2003/08/07 16:42:06 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)alloca.s 5.2 (Berkeley) 5/14/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: alloca.S,v 1.8 2003/08/07 16:42:06 agc Exp $") +#endif + +/* like alloc, but automatic free in return */ + +ENTRY(alloca) + popl %edx /* pop return addr */ + popl %eax /* pop amount to allocate */ + movl %esp,%ecx + addl $3,%eax /* round up to next word */ + andl $-4,%eax + subl %eax,%esp + movl %esp,%eax /* base of newly allocated space */ + pushl 8(%ecx) /* copy possible saved registers */ + pushl 4(%ecx) + pushl 0(%ecx) + pushl %eax /* dummy to pop at callsite */ + jmp *%edx /* "return" */ diff --git a/lib/nbsd_libc/arch/i386/gen/divsi3.S b/lib/nbsd_libc/arch/i386/gen/divsi3.S new file mode 100644 index 000000000..61d6b3436 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/divsi3.S @@ -0,0 +1,46 @@ +/* $NetBSD: divsi3.S,v 1.6 2003/08/07 16:42:07 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)divsi3.s 5.1 (Berkeley) 5/15/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: divsi3.S,v 1.6 2003/08/07 16:42:07 agc Exp $") +#endif + +ENTRY(__divsi3) + movl 4(%esp),%eax + cltd + idivl 8(%esp) + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fabs.S b/lib/nbsd_libc/arch/i386/gen/fabs.S new file mode 100644 index 000000000..76c3f3e9e --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fabs.S @@ -0,0 +1,45 @@ +/* $NetBSD: fabs.S,v 1.5 2003/08/07 16:42:07 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)fabs.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: fabs.S,v 1.5 2003/08/07 16:42:07 agc Exp $") +#endif + +ENTRY(fabs) + fldl 4(%esp) + fabs + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fixdfsi.S b/lib/nbsd_libc/arch/i386/gen/fixdfsi.S new file mode 100644 index 000000000..6bcf500f0 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fixdfsi.S @@ -0,0 +1,46 @@ +/* $NetBSD: fixdfsi.S,v 1.6 2003/08/07 16:42:07 agc Exp $ */ + +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)fixdfsi.s 5.4 (Berkeley) 4/12/91 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: fixdfsi.S,v 1.6 2003/08/07 16:42:07 agc Exp $") +#endif + +ENTRY(__fixdfsi) + fldl 4(%esp) + fistpl 4(%esp) + movl 4(%esp),%eax + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fixunsdfsi.S b/lib/nbsd_libc/arch/i386/gen/fixunsdfsi.S new file mode 100644 index 000000000..e9c2e67a8 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fixunsdfsi.S @@ -0,0 +1,75 @@ +/* $NetBSD: fixunsdfsi.S,v 1.11 2003/08/07 16:42:07 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)fixunsdfsi.s 5.1 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: fixunsdfsi.S,v 1.11 2003/08/07 16:42:07 agc Exp $") +#endif + +ENTRY(__fixunsdfsi) + fldl 4(%esp) /* argument double to accum stack */ + frndint /* create integer */ +#ifdef PIC + PIC_PROLOGUE + leal PIC_GOTOFF(fbiggestsigned),%eax + PIC_EPILOGUE + fcoml (%eax) +#else + fcoml fbiggestsigned /* bigger than biggest signed? */ +#endif + fstsw %ax + sahf + jnb 2f + + fistpl 4(%esp) + movl 4(%esp),%eax + ret + +2: +#ifdef PIC + PIC_PROLOGUE + leal PIC_GOTOFF(fbiggestsigned),%eax + PIC_EPILOGUE + fsubl (%eax) +#else + fsubl fbiggestsigned /* reduce for proper conversion */ +#endif + fistpl 4(%esp) /* convert */ + movl 4(%esp),%eax + orl $0x80000000,%eax /* restore bias */ + ret + +fbiggestsigned: .double 0r2147483648.0 diff --git a/lib/nbsd_libc/arch/i386/gen/flt_rounds.S b/lib/nbsd_libc/arch/i386/gen/flt_rounds.S new file mode 100644 index 000000000..4b2132769 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/flt_rounds.S @@ -0,0 +1,33 @@ +/* $NetBSD: flt_rounds.S,v 1.6 1999/08/23 08:45:09 kleink Exp $ */ + +/* + * Written by J.T. Conklin, Apr 4, 1995 + * Public domain. + */ + +#include + + .text + _ALIGN_TEXT +_map: + .byte 1 /* round to nearest */ + .byte 3 /* round to negative infinity */ + .byte 2 /* round to positive infinity */ + .byte 0 /* round to zero */ + +ENTRY(__flt_rounds) + subl $4,%esp + fnstcw (%esp) + movl (%esp),%eax + shrl $10,%eax + andl $3,%eax +#ifdef PIC + PIC_PROLOGUE + leal PIC_GOTOFF(_map),%ecx + PIC_EPILOGUE + movb (%ecx,%eax,1),%al +#else + movb _map(,%eax,1),%al +#endif + addl $4,%esp + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fpclassifyl.c b/lib/nbsd_libc/arch/i386/gen/fpclassifyl.c new file mode 100644 index 000000000..185ee0c5b --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fpclassifyl.c @@ -0,0 +1,70 @@ +/* $NetBSD: fpclassifyl.c,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyl.c,v 1.3 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__fpclassifyl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + _DIAGASSERT(u.extu_ext.ext_exp == 0 || + (u.extu_ext.ext_frach & 0x80000000)); + + if (u.extu_ext.ext_exp == 0) { + if ((u.extu_ext.ext_frach & 0x7fffffff) == 0 && + u.extu_ext.ext_fracl == 0) + return FP_ZERO; + else + return FP_SUBNORMAL; + } else if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) { + if ((u.extu_ext.ext_frach & 0x7fffffff) == 0 && + u.extu_ext.ext_fracl == 0) + return FP_INFINITE; + else + return FP_NAN; + } + + return FP_NORMAL; +} diff --git a/lib/nbsd_libc/arch/i386/gen/fpgetmask.S b/lib/nbsd_libc/arch/i386/gen/fpgetmask.S new file mode 100644 index 000000000..e3e295312 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fpgetmask.S @@ -0,0 +1,22 @@ +/* $NetBSD: fpgetmask.S,v 1.4 2002/01/13 21:45:42 thorpej Exp $ */ + +/* + * Written by J.T. Conklin, Apr 4, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetmask, _fpgetmask) +ENTRY(_fpgetmask) +#else +ENTRY(fpgetmask) +#endif + subl $4,%esp + fnstcw (%esp) + movl (%esp),%eax + notl %eax + andl $63,%eax + addl $4,%esp + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fpgetround.S b/lib/nbsd_libc/arch/i386/gen/fpgetround.S new file mode 100644 index 000000000..093c11559 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fpgetround.S @@ -0,0 +1,22 @@ +/* $NetBSD: fpgetround.S,v 1.5 2002/01/13 21:45:43 thorpej Exp $ */ + +/* + * Written by J.T. Conklin, Apr 4, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetround, _fpgetround) +ENTRY(_fpgetround) +#else +ENTRY(fpgetround) +#endif + subl $4,%esp + fnstcw (%esp) + movl (%esp),%eax + rorl $10,%eax + andl $3,%eax + addl $4,%esp + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fpgetsticky.S b/lib/nbsd_libc/arch/i386/gen/fpgetsticky.S new file mode 100644 index 000000000..5f54f94a5 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fpgetsticky.S @@ -0,0 +1,21 @@ +/* $NetBSD: fpgetsticky.S,v 1.6 2002/01/13 21:45:43 thorpej Exp $ */ + +/* + * Written by J.T. Conklin, Apr 4, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetsticky, _fpgetsticky) +ENTRY(_fpgetsticky) +#else +ENTRY(fpgetsticky) +#endif + subl $4,%esp + fnstsw (%esp) + movl (%esp),%eax + andl $63,%eax + addl $4,%esp + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fpsetmask.S b/lib/nbsd_libc/arch/i386/gen/fpsetmask.S new file mode 100644 index 000000000..7e010cc4e --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fpsetmask.S @@ -0,0 +1,33 @@ +/* $NetBSD: fpsetmask.S,v 1.4 2002/01/13 21:45:43 thorpej Exp $ */ + +/* + * Written by Charles M. Hannum, Apr 9, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetmask, _fpsetmask) +ENTRY(_fpsetmask) +#else +ENTRY(fpsetmask) +#endif + subl $4,%esp + + fnstcw (%esp) + movl (%esp),%eax + movl %eax,%edx + + notl %eax + andl $63,%eax + + addl %eax,%edx + movl 8(%esp),%ecx + andl $63,%ecx + subl %ecx,%edx + movl %edx,(%esp) + fldcw (%esp) + + addl $4,%esp + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fpsetround.S b/lib/nbsd_libc/arch/i386/gen/fpsetround.S new file mode 100644 index 000000000..6e00f87b5 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fpsetround.S @@ -0,0 +1,34 @@ +/* $NetBSD: fpsetround.S,v 1.3 2002/01/13 21:45:43 thorpej Exp $ */ + +/* + * Written by Charles M. Hannum, Apr 9, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetround, _fpsetround) +ENTRY(_fpsetround) +#else +ENTRY(fpsetround) +#endif + subl $4,%esp + + fnstcw (%esp) + movl (%esp),%eax + + rorl $10,%eax + movl %eax,%edx + andl $3,%eax + + subl %eax,%edx + movl 8(%esp),%ecx + andl $3,%ecx + orl %ecx,%edx + roll $10,%edx + movl %edx,(%esp) + fldcw (%esp) + + addl $4,%esp + ret diff --git a/lib/nbsd_libc/arch/i386/gen/fpsetsticky.S b/lib/nbsd_libc/arch/i386/gen/fpsetsticky.S new file mode 100644 index 000000000..76389d315 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/fpsetsticky.S @@ -0,0 +1,32 @@ +/* $NetBSD: fpsetsticky.S,v 1.6 2002/01/13 21:45:43 thorpej Exp $ */ + +/* + * Written by Charles M. Hannum, Apr 9, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetsticky, _fpsetsticky) +ENTRY(_fpsetsticky) +#else +ENTRY(fpsetsticky) +#endif + subl $28,%esp + + fnstenv (%esp) + movl 4(%esp),%eax + movl %eax,%edx + + andl $63,%eax + + subl %eax,%edx + movl 32(%esp),%ecx + andl $63,%ecx + addl %ecx,%edx + movl %edx,4(%esp) + fldenv (%esp) + + addl $28,%esp + ret diff --git a/lib/nbsd_libc/arch/i386/gen/infinityl.c b/lib/nbsd_libc/arch/i386/gen/infinityl.c new file mode 100644 index 000000000..3e7f543a2 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/infinityl.c @@ -0,0 +1,16 @@ +/* $NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:26 lukem Exp $ */ + +/* + * IEEE-compatible infinityl.c for little-endian 80-bit format -- public domain. + * Note that the representation includes 16 bits of tail padding per i386 ABI. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:26 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include + +const union __long_double_u __infinityl = + { { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f, 0, 0 } }; diff --git a/lib/nbsd_libc/arch/i386/gen/isfinitel.c b/lib/nbsd_libc/arch/i386/gen/isfinitel.c new file mode 100644 index 000000000..8b0c8a7d2 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/isfinitel.c @@ -0,0 +1,59 @@ +/* $NetBSD: isfinitel.c,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinitel.c,v 1.3 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__isfinitel(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + _DIAGASSERT(u.extu_ext.ext_exp == 0 || + (u.extu_ext.ext_frach & 0x80000000)); + + if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/arch/i386/gen/isinfl.c b/lib/nbsd_libc/arch/i386/gen/isinfl.c new file mode 100644 index 000000000..b1675e921 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/isinfl.c @@ -0,0 +1,63 @@ +/* $NetBSD: isinfl.c,v 1.6 2007/02/02 23:14:09 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinfl.c,v 1.6 2007/02/02 23:14:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.3 isinf - test for infinity + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__isinfl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + u.extu_ext.ext_frach == 0x80000000 && u.extu_ext.ext_fracl == 0); +} diff --git a/lib/nbsd_libc/arch/i386/gen/isnanl.c b/lib/nbsd_libc/arch/i386/gen/isnanl.c new file mode 100644 index 000000000..a85fdc6c0 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/isnanl.c @@ -0,0 +1,64 @@ +/* $NetBSD: isnanl.c,v 1.6 2007/02/02 23:14:09 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnanl.c,v 1.6 2007/02/02 23:14:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.4 isnan - test for a NaN + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__isnanl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + (u.extu_ext.ext_frach & 0x80000000) != 0 && + (u.extu_ext.ext_frach != 0x80000000 || u.extu_ext.ext_fracl != 0)); +} diff --git a/lib/nbsd_libc/arch/i386/gen/makecontext.c b/lib/nbsd_libc/arch/i386/gen/makecontext.c new file mode 100644 index 000000000..11e8ec711 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/makecontext.c @@ -0,0 +1,93 @@ +/* $NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:56 martin 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#if __STDC__ +#include +#else +#include +#endif + + +void +#if __STDC__ +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +#else +makecontext(ucp, func, argc, va_alist) + ucontext_t *ucp; + void (*func)(); + int argc; + va_dcl +#endif +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + unsigned int *sp; + va_list ap; + + /* LINTED __greg_t is safe */ + gr[_REG_EIP] = (__greg_t)func; + + /* LINTED uintptr_t is safe */ + sp = (unsigned int *)((uintptr_t)ucp->uc_stack.ss_sp + + ucp->uc_stack.ss_size); + /* Align on word boundary. */ + /* LINTED uintptr_t is safe */ + sp = (unsigned int *)((uintptr_t)sp & ~0x3); + sp -= argc + 1; /* Make room for ret and args. */ + /* LINTED __greg_t is safe */ + gr[_REG_UESP] = (__greg_t)sp; + gr[_REG_EBP] = (__greg_t)0; /* Wipe out frame pointer. */ + + /* Put return address on top of stack. */ + /* LINTED uintptr_t is safe */ + *sp++ = (uintptr_t)_resumecontext; + + /* Construct argument list. */ +#if __STDC__ + va_start(ap, argc); +#else + va_start(ap); +#endif + while (argc-- > 0) { + /* LINTED uintptr_t is safe */ + *sp++ = va_arg(ap, uintptr_t); + } + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/i386/gen/nanf.c b/lib/nbsd_libc/arch/i386/gen/nanf.c new file mode 100644 index 000000000..ac1fde339 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:01 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0, 0, 0xc0, 0x7f } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/i386/gen/resumecontext.S b/lib/nbsd_libc/arch/i386/gen/resumecontext.S new file mode 100644 index 000000000..629a09f5a --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/resumecontext.S @@ -0,0 +1,83 @@ +/* $NetBSD: resumecontext.S,v 1.6 2008/04/28 20:22:56 martin 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. + */ + +#include +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: resumecontext.S,v 1.6 2008/04/28 20:22:56 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + +/* + * This assembly-language implementation differs from the (obvious) + * C-language implementation only in not clobbering the previous + * function's return address (us), which is the point of the exercise. + */ + +NENTRY(_resumecontext) /* profiling prologue would clobber TOS */ + leal -(4 + 776)(%esp),%ecx /* retaddr + sizeof (ucontext_t) */ + andl $~(0xf),%ecx /* align on _UC_UCONTEXT_ALIGN */ + movl %ecx,%esp + PIC_PROLOGUE + pushl %ecx +#ifdef PIC + call PIC_PLT(_C_LABEL(_getcontext)) +#else + call _C_LABEL(_getcontext) +#endif + addl $4,%esp + PIC_EPILOGUE + movl 4(%esp),%ecx /* uc_link */ + PIC_PROLOGUE + pushl %ecx /* argument for exit() or setcontext() */ + testl %ecx,%ecx /* link end? */ + jnz 9f /* watch out for local label use in PIC_* */ + + /* normal exit */ +#ifdef PIC + call PIC_PLT(_C_LABEL(exit)) +#else + call _C_LABEL(exit) +#endif + /* NOTREACHED */ + +9: +#ifdef PIC + call PIC_PLT(_C_LABEL(setcontext)) +#else + call _C_LABEL(setcontext) +#endif + /* NOTREACHED */ + + /* something is wrong, pull the brake */ + pushl $-1 + pushl %eax /* unused return address */ + SYSTRAP(exit) diff --git a/lib/nbsd_libc/arch/i386/gen/setjmp.S b/lib/nbsd_libc/arch/i386/gen/setjmp.S new file mode 100644 index 000000000..4d2a5b5ea --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/setjmp.S @@ -0,0 +1,109 @@ +/* $NetBSD: setjmp.S,v 1.14 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: setjmp.S,v 1.14 2005/09/13 01:44:08 christos Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__setjmp14) + movl 4(%esp),%ecx + movl 0(%esp),%edx + movl %edx, 0(%ecx) + movl %ebx, 4(%ecx) + movl %esp, 8(%ecx) + movl %ebp,12(%ecx) + movl %esi,16(%ecx) + movl %edi,20(%ecx) + + /* Get the signal mask. */ + leal 24(%ecx),%edx + + PIC_PROLOGUE + pushl %edx + pushl $0 + pushl $0 +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif + addl $12,%esp + PIC_EPILOGUE + + xorl %eax,%eax + ret + +ENTRY(__longjmp14) + /* Restore the signal mask. */ + movl 4(%esp),%ecx + leal 24(%ecx),%edx + + PIC_PROLOGUE + pushl $0 + pushl %edx + pushl $3 /* SIG_SETMASK */ +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif + addl $12,%esp + PIC_EPILOGUE + + movl 4(%esp),%edx + movl 8(%esp),%eax + movl 0(%edx),%ecx + movl 4(%edx),%ebx + movl 8(%edx),%esp + movl 12(%edx),%ebp + movl 16(%edx),%esi + movl 20(%edx),%edi + testl %eax,%eax + jnz 1f + incl %eax +1: movl %ecx,0(%esp) + ret diff --git a/lib/nbsd_libc/arch/i386/gen/signbitl.c b/lib/nbsd_libc/arch/i386/gen/signbitl.c new file mode 100644 index 000000000..8ea9a1f4e --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/signbitl.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitl.c,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: signbitl.c,v 1.2 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__signbitl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_sign == 1); +} diff --git a/lib/nbsd_libc/arch/i386/gen/sigsetjmp.S b/lib/nbsd_libc/arch/i386/gen/sigsetjmp.S new file mode 100644 index 000000000..63fbb0d99 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/sigsetjmp.S @@ -0,0 +1,109 @@ +/* $NetBSD: sigsetjmp.S,v 1.16 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90" + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: sigsetjmp.S,v 1.16 2005/09/13 01:44:08 christos Exp $") +#endif + +ENTRY(__sigsetjmp14) + movl 4(%esp),%ecx + movl 0(%esp),%edx + movl %edx, 0(%ecx) + movl %ebx, 4(%ecx) + movl %esp, 8(%ecx) + movl %ebp,12(%ecx) + movl %esi,16(%ecx) + movl %edi,20(%ecx) + + /* Check if we should save the signal mask, and remember it. */ + movl 8(%esp),%eax + movl %eax,40(%ecx) + testl %eax,%eax + jz 2f /* no, skip */ + + /* Get the signal mask. */ + leal 24(%ecx),%edx + + PIC_PROLOGUE + pushl %edx + pushl $0 + pushl $0 +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif + addl $12,%esp + PIC_EPILOGUE + +2: xorl %eax,%eax + ret + +ENTRY(__siglongjmp14) + /* Check to see if we need to restore the signal mask. */ + movl 4(%esp),%ecx + cmpl $0,40(%ecx) + jz 2f /* no, skip */ + + /* Restore the signal mask. */ + leal 24(%ecx),%edx + + PIC_PROLOGUE + pushl $0 + pushl %edx + pushl $3 /* SIG_SETMASK */ +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif + addl $12,%esp + PIC_EPILOGUE + +2: movl 4(%esp),%edx + movl 8(%esp),%eax + movl 0(%edx),%ecx + movl 4(%edx),%ebx + movl 8(%edx),%esp + movl 12(%edx),%ebp + movl 16(%edx),%esi + movl 20(%edx),%edi + testl %eax,%eax + jnz 3f + incl %eax +3: movl %ecx,0(%esp) + ret diff --git a/lib/nbsd_libc/arch/i386/gen/swapcontext.S b/lib/nbsd_libc/arch/i386/gen/swapcontext.S new file mode 100644 index 000000000..a140c991e --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/swapcontext.S @@ -0,0 +1,70 @@ +/* $NetBSD: swapcontext.S,v 1.6 2008/04/28 20:22:56 martin 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.6 2008/04/28 20:22:56 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + +ENTRY(swapcontext) + movl 4(%esp),%ecx + PIC_PROLOGUE + pushl %ecx /* push oucp */ +#ifdef PIC + call PIC_PLT(_C_LABEL(_getcontext)) +#else + call _C_LABEL(_getcontext) +#endif + addl $4,%esp /* pop oucp */ + PIC_EPILOGUE + testl %eax,%eax + jnz 2f + movl 4(%esp),%ecx + movl 0(%esp),%edx + movl %edx,(36 + 14 * 4)(%ecx) /* Adjust saved %eip ... */ +#ifdef PIC + movl %ebx,(36 + 8 * 4)(%ecx) /* ... clobbered PIC reg ... */ +#endif + leal 4(%esp),%edx + movl %edx,(36 + 17 * 4)(%ecx) /* ... and %esp. */ + /* Note: oucp->%eax has been cleared by getcontext(). */ + movl 8(%esp),%ecx + PIC_PROLOGUE + pushl %ecx /* push ucp */ +#ifdef PIC + call PIC_PLT(_C_LABEL(setcontext)) +#else + call _C_LABEL(setcontext) +#endif + addl $4,%esp /* pop ucp */ + PIC_EPILOGUE + /* No need to error-check: setcontext() must have failed. */ +2: ret diff --git a/lib/nbsd_libc/arch/i386/gen/udivsi3.S b/lib/nbsd_libc/arch/i386/gen/udivsi3.S new file mode 100644 index 000000000..0cb51af5c --- /dev/null +++ b/lib/nbsd_libc/arch/i386/gen/udivsi3.S @@ -0,0 +1,46 @@ +/* $NetBSD: udivsi3.S,v 1.6 2003/08/07 16:42:07 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)udivsi3.s 5.1 (Berkeley) 5/15/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: udivsi3.S,v 1.6 2003/08/07 16:42:07 agc Exp $") +#endif + +ENTRY(__udivsi3) + movl 4(%esp),%eax + xorl %edx,%edx + divl 8(%esp) + ret diff --git a/lib/nbsd_libc/arch/i386/net/Makefile.inc b/lib/nbsd_libc/arch/i386/net/Makefile.inc new file mode 100644 index 000000000..0185ca659 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/net/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/12/06 07:12:17 uebayasi Exp $ + +# objects built from assembler sources (need lint stubs) +# hton* and nto* functions provided by ../gen/byte_swap_*.S +SRCS+= + +LSRCS.i386.net= Lint_htonl.c Lint_htons.c Lint_ntohl.c Lint_ntohs.c +LSRCS+= ${LSRCS.i386.net} +DPSRCS+= ${LSRCS.i386.net} +CLEANFILES+= ${LSRCS.i386.net} diff --git a/lib/nbsd_libc/arch/i386/stdlib/Makefile.inc b/lib/nbsd_libc/arch/i386/stdlib/Makefile.inc new file mode 100644 index 000000000..189f574a9 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/stdlib/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.9 2009/08/11 17:30:43 dsl Exp $ + +# objects built from assembler sources +SRCS+= abs.S div.S labs.S ldiv.S llabs.S +NO_SRCS+= imaxabs.S diff --git a/lib/nbsd_libc/arch/i386/stdlib/abs.S b/lib/nbsd_libc/arch/i386/stdlib/abs.S new file mode 100644 index 000000000..74e96252b --- /dev/null +++ b/lib/nbsd_libc/arch/i386/stdlib/abs.S @@ -0,0 +1,47 @@ +/* $NetBSD: abs.S,v 1.7 2003/08/07 16:42:07 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)abs.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: abs.S,v 1.7 2003/08/07 16:42:07 agc Exp $") +#endif + +ENTRY(abs) + movl 4(%esp),%eax + testl %eax,%eax + jns 1f + negl %eax +1: ret diff --git a/lib/nbsd_libc/arch/i386/stdlib/div.S b/lib/nbsd_libc/arch/i386/stdlib/div.S new file mode 100644 index 000000000..66f335188 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/stdlib/div.S @@ -0,0 +1,23 @@ +/* $NetBSD: div.S,v 1.8 2003/07/26 19:24:33 salo Exp $ */ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) +RCSID("$NetBSD: div.S,v 1.8 2003/07/26 19:24:33 salo Exp $") +#endif + +ENTRY(div) + pushl %ebx + movl 8(%esp),%ebx + movl 12(%esp),%eax + movl 16(%esp),%ecx + cdq + idiv %ecx + movl %eax,(%ebx) + movl %edx,4(%ebx) + popl %ebx + ret $4 diff --git a/lib/nbsd_libc/arch/i386/stdlib/labs.S b/lib/nbsd_libc/arch/i386/stdlib/labs.S new file mode 100644 index 000000000..3b3414dfd --- /dev/null +++ b/lib/nbsd_libc/arch/i386/stdlib/labs.S @@ -0,0 +1,47 @@ +/* $NetBSD: labs.S,v 1.7 2003/08/07 16:42:07 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)abs.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: labs.S,v 1.7 2003/08/07 16:42:07 agc Exp $") +#endif + +ENTRY(labs) + movl 4(%esp),%eax + testl %eax,%eax + jns 1f + negl %eax +1: ret diff --git a/lib/nbsd_libc/arch/i386/stdlib/ldiv.S b/lib/nbsd_libc/arch/i386/stdlib/ldiv.S new file mode 100644 index 000000000..d9acd25f2 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/stdlib/ldiv.S @@ -0,0 +1,23 @@ +/* $NetBSD: ldiv.S,v 1.8 2003/07/26 19:24:33 salo Exp $ */ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) +RCSID("$NetBSD: ldiv.S,v 1.8 2003/07/26 19:24:33 salo Exp $") +#endif + +ENTRY(ldiv) + pushl %ebx + movl 8(%esp),%ebx + movl 12(%esp),%eax + movl 16(%esp),%ecx + cdq + idiv %ecx + movl %eax,(%ebx) + movl %edx,4(%ebx) + popl %ebx + ret $4 diff --git a/lib/nbsd_libc/arch/i386/stdlib/llabs.S b/lib/nbsd_libc/arch/i386/stdlib/llabs.S new file mode 100644 index 000000000..bc740ff4b --- /dev/null +++ b/lib/nbsd_libc/arch/i386/stdlib/llabs.S @@ -0,0 +1,60 @@ +/* $NetBSD: llabs.S,v 1.5 2008/08/04 21:29:28 matt Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)abs.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: llabs.S,v 1.5 2008/08/04 21:29:28 matt Exp $") +#endif + +#ifdef WEAK_ALIAS +WEAK_ALIAS(llabs, _llabs) +WEAK_ALIAS(imaxabs, _llabs) +#endif + +#ifdef WEAK_ALIAS +ENTRY(_llabs) +#else +STRONG_ALIAS(imaxabs, llabs) +ENTRY(llabs) +#endif + movl 8(%esp),%edx + movl 4(%esp),%eax + testl %edx,%edx + jns 1f + negl %eax + adcl $0,%edx + negl %edx +1: ret diff --git a/lib/nbsd_libc/arch/i386/string/Makefile.inc b/lib/nbsd_libc/arch/i386/string/Makefile.inc new file mode 100644 index 000000000..c1611747a --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/30 20:57:16 dsl Exp $ + +# objects built from assembler sources +SRCS+= bcmp.S bcopy.S bzero.S ffs.S memchr.S memcmp.S \ + memcpy.S memmove.S memset.S strcat.S strchr.S \ + strcmp.S strcpy.S strlen.S strncmp.S strrchr.S swab.S diff --git a/lib/nbsd_libc/arch/i386/string/bcmp.S b/lib/nbsd_libc/arch/i386/string/bcmp.S new file mode 100644 index 000000000..3ea341e0c --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/bcmp.S @@ -0,0 +1,34 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: bcmp.S,v 1.9 2007/11/12 18:41:59 ad Exp $") +#endif + +ENTRY(bcmp) + pushl %edi + pushl %esi + movl 12(%esp),%edi + movl 16(%esp),%esi + xorl %eax,%eax /* clear return value */ + + movl 20(%esp),%ecx /* compare by words */ + shrl $2,%ecx + repe + cmpsl + jne L1 + + movl 20(%esp),%ecx /* compare remainder by bytes */ + andl $3,%ecx + repe + cmpsb + je L2 + +L1: incl %eax +L2: popl %esi + popl %edi + ret diff --git a/lib/nbsd_libc/arch/i386/string/bcopy.S b/lib/nbsd_libc/arch/i386/string/bcopy.S new file mode 100644 index 000000000..35fb1df2d --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/bcopy.S @@ -0,0 +1,4 @@ +/* $NetBSD: bcopy.S,v 1.15 2005/02/09 18:15:16 christos Exp $ */ + +#define BCOPY +#include "memcpy.S" diff --git a/lib/nbsd_libc/arch/i386/string/bzero.S b/lib/nbsd_libc/arch/i386/string/bzero.S new file mode 100644 index 000000000..f83744741 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/bzero.S @@ -0,0 +1,4 @@ +/* $NetBSD: bzero.S,v 1.12 2005/02/09 18:15:16 christos Exp $ */ + +#define BZERO +#include "memset.S" diff --git a/lib/nbsd_libc/arch/i386/string/index.S b/lib/nbsd_libc/arch/i386/string/index.S new file mode 100644 index 000000000..bda26bbe1 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/index.S @@ -0,0 +1,4 @@ +/* $NetBSD: index.S,v 1.14 2005/02/09 18:15:16 christos Exp $ */ + +#define INDEX +#include "strchr.S" diff --git a/lib/nbsd_libc/arch/i386/string/rindex.S b/lib/nbsd_libc/arch/i386/string/rindex.S new file mode 100644 index 000000000..f39cc2c47 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/rindex.S @@ -0,0 +1,4 @@ +/* $NetBSD: rindex.S,v 1.14 2005/02/09 18:15:16 christos Exp $ */ + +#define RINDEX +#include "strrchr.S" diff --git a/lib/nbsd_libc/arch/i386/string/strncmp.S b/lib/nbsd_libc/arch/i386/string/strncmp.S new file mode 100644 index 000000000..45b1ce21f --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/strncmp.S @@ -0,0 +1,116 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strncmp.S,v 1.14 2005/08/03 22:59:50 rpaulo Exp $") +#endif + +/* + * NOTE: I've unrolled the loop eight times: large enough to make a + * significant difference, and small enough not to totally trash the + * cache. + */ + +ENTRY(strncmp) + pushl %ebx + movl 8(%esp),%eax + movl 12(%esp),%ecx + movl 16(%esp),%edx + testl %edx,%edx + jmp L2 /* Jump into the loop! */ + + _ALIGN_TEXT,0x90 +L1: incl %eax + incl %ecx + decl %edx +L2: jz L4 /* strings are equal */ + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + je L1 + + _ALIGN_TEXT,0x90 +L3: movzbl (%eax),%eax /* unsigned comparison */ + movzbl (%ecx),%ecx + subl %ecx,%eax + popl %ebx + ret + _ALIGN_TEXT,0x90 +L4: xorl %eax,%eax + popl %ebx + ret diff --git a/lib/nbsd_libc/arch/i386/string/swab.S b/lib/nbsd_libc/arch/i386/string/swab.S new file mode 100644 index 000000000..65f790333 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/string/swab.S @@ -0,0 +1,68 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: swab.S,v 1.13 2007/11/12 18:42:00 ad Exp $") +#endif + +/* + * On the i486, this code is negligibly faster than the code generated + * by gcc at about half the size. If my i386 databook is correct, it + * should be considerably faster than the gcc code on a i386. + */ + +ENTRY(swab) + pushl %esi + pushl %edi + movl 12(%esp),%esi + movl 16(%esp),%edi + movl 20(%esp),%ecx + + shrl $1,%ecx + testl $7,%ecx # copy first group of 1 to 7 words + jz L2 # while swapping alternate bytes. + _ALIGN_TEXT,0x90 +L1: lodsw + rorw $8,%ax + stosw + decl %ecx + testl $7,%ecx + jnz L1 + +L2: shrl $3,%ecx # copy remainder 8 words at a time + jz L4 # while swapping alternate bytes. + _ALIGN_TEXT,0x90 +L3: lodsw + rorw $8,%ax + stosw + lodsw + rorw $8,%ax + stosw + lodsw + rorw $8,%ax + stosw + lodsw + rorw $8,%ax + stosw + lodsw + rorw $8,%ax + stosw + lodsw + rorw $8,%ax + stosw + lodsw + rorw $8,%ax + stosw + lodsw + rorw $8,%ax + stosw + decl %ecx + jnz L3 + +L4: popl %edi + popl %esi + ret diff --git a/lib/nbsd_libc/arch/i386/sys/__clone.S b/lib/nbsd_libc/arch/i386/sys/__clone.S new file mode 100644 index 000000000..ef27f464d --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/__clone.S @@ -0,0 +1,110 @@ +/* $NetBSD: __clone.S,v 1.3 2001/07/18 12:24:53 fvdl Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + .text + +/* + * int clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + pushl %ebp + + /* + * Sanity checks: func and stack may not be NULL. + */ + movl 8(%esp), %ebp + cmpl $0,%ebp /* function */ + je 3f + movl 12(%esp),%eax /* stack */ + cmpl $0,%eax + je 3f + + /* + * Set up the stack for the clone. + */ + movl 20(%esp),%ecx + movl %ecx,-4(%eax) /* argument */ + leal -4(%eax),%eax /* sp points to arg */ + + pushl %eax /* stack */ + pushl 20(%esp) /* flags */ + pushl $0 /* dummy return address */ + + SYSTRAP(__clone) + jc 4f + cmpl $0,%eax + jne 2f /* we're the parent */ + call *%ebp /* this is the clone, call the function */ + +#ifdef PIC + PIC_PROLOGUE + pushl %eax /* clone does _exit(func(arg)); */ + call PIC_PLT(_C_LABEL(_exit)) + addl $4,%esp + PIC_EPILOGUE +#else + pushl %eax + call _C_LABEL(_exit) + addl $4,%esp +#endif + +2: + addl $12,%esp + popl %ebp + ret +3: + movl $EINVAL,%eax + jmp 5f +4: + addl $12,%esp +5: + popl %ebp +#ifdef PIC + PIC_PROLOGUE + movl PIC_GOT(CERROR),%edx + PIC_EPILOGUE + jmp *%edx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/i386/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/i386/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..dab72d14b --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/__sigaction14_sigtramp.c @@ -0,0 +1,79 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.12 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.12 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + extern const int __sigtramp_sigcontext_1[]; + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specified or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/i386/sys/__sigtramp2.S b/lib/nbsd_libc/arch/i386/sys/__sigtramp2.S new file mode 100644 index 000000000..c530bcd4f --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/__sigtramp2.S @@ -0,0 +1,52 @@ +/* $NetBSD: __sigtramp2.S,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 "SYS.h" + +/* + * The i386 signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * ucontext structure [12+sizeof(siginfo_t)] + * siginfo structure [12] + * pointer to ucontext structure [8] + * pointer to siginfo structure [4] + * sp-> signal number [0] + */ +NENTRY(__sigtramp_siginfo_2) + leal 12+128(%esp),%eax /* get address of ucontext */ + movl %eax,4(%esp) /* put it in the argument slot */ + /* fake return address already there */ + SYSTRAP(setcontext) /* do setcontext */ + movl $-1,4(%esp) /* if we return here, something is wrong */ + SYSTRAP(exit) /* exit */ diff --git a/lib/nbsd_libc/arch/i386/sys/__syscall.S b/lib/nbsd_libc/arch/i386/sys/__syscall.S new file mode 100644 index 000000000..cb5db5139 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/__syscall.S @@ -0,0 +1,62 @@ +/* $NetBSD: __syscall.S,v 1.3 2003/08/07 16:42:08 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __syscall.S,v 1.3 2003/08/07 16:42:08 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(__syscall) + pop %ecx /* rta */ + pop %eax /* syscall number */ + pop %edx /* junk */ + push %ecx + int $0x80 + push %ecx /* Keep stack frame consistant */ + push %ecx /* Keep stack frame consistant */ + jc err + ret +err: +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/i386/sys/__vfork14.S b/lib/nbsd_libc/arch/i386/sys/__vfork14.S new file mode 100644 index 000000000..2dc085d8e --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/__vfork14.S @@ -0,0 +1,67 @@ +/* $NetBSD: __vfork14.S,v 1.8 2007/03/09 14:30:48 ad Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __vfork14.S,v 1.8 2007/03/09 14:30:48 ad Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * pid = vfork(); + * + * %edx == 0 in parent process, %edx == 1 in child process. + * %eax == pid of child in parent, %eax == pid of parent in child. + * + */ +ENTRY(__vfork14) + popl %ecx /* my rta into %ecx */ + OSYSTRAP(__vfork14) /* don't clobber %ecx */ + jc err + decl %edx + andl %edx,%eax + jmp *%ecx +err: + pushl %ecx +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/i386/sys/brk.S b/lib/nbsd_libc/arch/i386/sys/brk.S new file mode 100644 index 000000000..73e363cdf --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/brk.S @@ -0,0 +1,95 @@ +/* $NetBSD: brk.S,v 1.21 2007/03/09 14:30:56 ad Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)brk.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: brk.S,v 1.21 2007/03/09 14:30:56 ad Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _end + .globl _C_LABEL(__minbrk) + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data +_C_LABEL(__minbrk): + .long _end + .text + +ENTRY(_brk) +#ifdef PIC + movl 4(%esp),%ecx + PIC_PROLOGUE + movl PIC_GOT(_C_LABEL(__minbrk)),%edx + PIC_EPILOGUE + cmpl %ecx,(%edx) + jb 1f + movl (%edx),%ecx + movl %ecx,4(%esp) +1: + OSYSTRAP(break) /* don't clobber %ecx */ + jc err + PIC_PROLOGUE + movl PIC_GOT(CURBRK),%edx # set up GOT addressing + PIC_EPILOGUE + movl %ecx,(%edx) + xorl %eax,%eax + ret +err: + PIC_PROLOGUE + mov PIC_GOT(CERROR),%edx + PIC_EPILOGUE + jmp *%edx +#else + movl 4(%esp),%ecx + cmpl %ecx,_C_LABEL(__minbrk) + jb 1f + movl _C_LABEL(__minbrk),%ecx + movl %ecx,4(%esp) +1: + OSYSTRAP(break) /* don't clobber %ecx */ + jc err + movl %ecx,CURBRK + xorl %eax,%eax + ret +err: + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/i386/sys/cerror.S b/lib/nbsd_libc/arch/i386/sys/cerror.S new file mode 100644 index 000000000..3a0c7223d --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/cerror.S @@ -0,0 +1,58 @@ +/* $NetBSD: cerror.S,v 1.14 2003/08/07 16:42:08 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)cerror.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: cerror.S,v 1.14 2003/08/07 16:42:08 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _C_LABEL(__errno) + +_ENTRY(CERROR) + pushl %eax +#ifdef PIC + PIC_PROLOGUE + call PIC_PLT(_C_LABEL(__errno)) + PIC_EPILOGUE +#else + call _C_LABEL(__errno) +#endif /* PIC */ + popl (%eax) + movl $-1,%eax + movl $-1,%edx + ret diff --git a/lib/nbsd_libc/arch/i386/sys/exect.S b/lib/nbsd_libc/arch/i386/sys/exect.S new file mode 100644 index 000000000..dacc45052 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/exect.S @@ -0,0 +1,57 @@ +/* $NetBSD: exect.S,v 1.13 2003/08/07 16:42:08 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)exect.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: exect.S,v 1.13 2003/08/07 16:42:08 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" +#include + +ENTRY(exect) + pushfl + orb $(PSL_T>>8),1(%esp) + popfl + SYSTRAP(execve) +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/i386/sys/fork.S b/lib/nbsd_libc/arch/i386/sys/fork.S new file mode 100644 index 000000000..9b96f9eef --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/fork.S @@ -0,0 +1,47 @@ +/* $NetBSD: fork.S,v 1.9 2003/08/07 16:42:09 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)fork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: fork.S,v 1.9 2003/08/07 16:42:09 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +_SYSCALL(__fork,fork) + decl %edx /* from 1 to 0 in child, 0 to -1 in parent */ + andl %edx,%eax + ret /* pid = fork(); */ diff --git a/lib/nbsd_libc/arch/i386/sys/getcontext.S b/lib/nbsd_libc/arch/i386/sys/getcontext.S new file mode 100644 index 000000000..dc000270c --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/getcontext.S @@ -0,0 +1,50 @@ +/* $NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:56 martin 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. + */ + +#include "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:56 martin Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +_SYSCALL(_getcontext,getcontext) + movl 4(%esp),%ecx + movl 0(%esp),%edx + movl %edx,(36 + 14 * 4)(%ecx) /* Adjust saved %eip ... */ + leal 4(%esp),%edx + movl %edx,(36 + 17 * 4)(%ecx) /* ... and %esp. */ + movl $0,(36 + 11 * 4)(%ecx) /* Arrange for return of 0. */ + xorl %eax,%eax + ret diff --git a/lib/nbsd_libc/arch/i386/sys/pipe.S b/lib/nbsd_libc/arch/i386/sys/pipe.S new file mode 100644 index 000000000..2b4d3212e --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/pipe.S @@ -0,0 +1,53 @@ +/* $NetBSD: pipe.S,v 1.6 2003/08/07 16:42:09 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)pipe.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: pipe.S,v 1.6 2003/08/07 16:42:09 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +_SYSCALL(_pipe,pipe) + movl 4(%esp),%ecx + movl %eax,(%ecx) + movl %edx,4(%ecx) + xorl %eax,%eax + ret diff --git a/lib/nbsd_libc/arch/i386/sys/ptrace.S b/lib/nbsd_libc/arch/i386/sys/ptrace.S new file mode 100644 index 000000000..1fd7acf0f --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/ptrace.S @@ -0,0 +1,67 @@ +/* $NetBSD: ptrace.S,v 1.16 2003/08/07 16:42:09 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)ptrace.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: ptrace.S,v 1.16 2003/08/07 16:42:09 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _C_LABEL(__errno) + +ENTRY(ptrace) +#ifdef PIC + PIC_PROLOGUE + call PIC_PLT(_C_LABEL(__errno)) + PIC_EPILOGUE +#else + call _C_LABEL(__errno) +#endif /* PIC */ + xorl %edx,%edx + movl %edx,(%eax) + SYSTRAP(ptrace) + jc err + ret +err: +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/i386/sys/sbrk.S b/lib/nbsd_libc/arch/i386/sys/sbrk.S new file mode 100644 index 000000000..359e50057 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/sbrk.S @@ -0,0 +1,94 @@ +/* $NetBSD: sbrk.S,v 1.19 2008/07/02 20:07:43 rmind Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sbrk.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: sbrk.S,v 1.19 2008/07/02 20:07:43 rmind Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _end + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +CURBRK: .long _end + .text + +ENTRY(_sbrk) +#ifdef PIC + movl 4(%esp),%ecx + PIC_PROLOGUE + movl PIC_GOT(CURBRK),%edx + PIC_EPILOGUE + movl (%edx),%eax + test %ecx,%ecx + jz out + addl %eax,%ecx + movl %ecx,4(%esp) + OSYSTRAP(break) /* don't clobber %ecx */ + jc err + PIC_PROLOGUE + movl PIC_GOT(CURBRK),%edx + PIC_EPILOGUE + movl (%edx),%eax + movl %ecx,(%edx) +out: + ret +err: + PIC_PROLOGUE + mov PIC_GOT(CERROR),%edx + PIC_EPILOGUE + jmp *%edx +#else + movl 4(%esp),%ecx + movl CURBRK,%eax + test %ecx,%ecx + jz out + addl %eax,4(%esp) + OSYSTRAP(break) /* don't clobber %ecx */ + jc err + movl CURBRK,%eax + addl %ecx,CURBRK +out: + ret +err: + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/i386/sys/shmat.S b/lib/nbsd_libc/arch/i386/sys/shmat.S new file mode 100644 index 000000000..0e7842254 --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:52 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/i386/sys/syscall.S b/lib/nbsd_libc/arch/i386/sys/syscall.S new file mode 100644 index 000000000..3fe3f72ab --- /dev/null +++ b/lib/nbsd_libc/arch/i386/sys/syscall.S @@ -0,0 +1,60 @@ +/* $NetBSD: syscall.S,v 1.11 2003/08/07 16:42:09 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: syscall.S,v 1.11 2003/08/07 16:42:09 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(syscall) + pop %ecx /* rta */ + pop %eax /* syscall number */ + push %ecx + int $0x80 + push %ecx /* Keep stack frame consistant */ + jc err + ret +err: +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/ia64/Makefile.inc b/lib/nbsd_libc/arch/ia64/Makefile.inc new file mode 100644 index 000000000..6c135b17b --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/09/23 17:39:34 cherry Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S diff --git a/lib/nbsd_libc/arch/ia64/SYS.h b/lib/nbsd_libc/arch/ia64/SYS.h new file mode 100644 index 000000000..8ee7fb443 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/SYS.h @@ -0,0 +1,74 @@ +/* $NetBSD: SYS.h,v 1.1 2006/09/10 21:22:33 cherry Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +#include +#include + +#define RET { br.ret.sptk.few rp;; } + +#define CALLSYS_ERROR(name) \ + CALLSYS_NOERROR(name) \ +{ cmp.ne p6,p0=r0,r10; \ +(p6) br.cond.sptk.few __cerror ;; } + +#define SYSCALL(name) \ +ENTRY(name,0); /* XXX # of args? */ \ + CALLSYS_ERROR(name) + +#define SYSCALL_NOERROR(name) \ +ENTRY(name,0); /* XXX # of args? */ \ + CALLSYS_NOERROR(name) + +#define PSEUDO(label,name) \ +ENTRY(label,0); /* XXX # of args? */ \ + CALLSYS_ERROR(name); \ + RET; \ +END(label); + +#define PSEUDO_NOERROR(label,name) \ +ENTRY(label,0); /* XXX # of args? */ \ + CALLSYS_NOERROR(name); \ + RET; \ +END(label); + +#define RSYSCALL(name) \ + SYSCALL(name); \ + RET; \ +END(name) + +#define RSYSCALL_NOERROR(name) \ + SYSCALL_NOERROR(name); \ + RET; \ +END(name) + +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) diff --git a/lib/nbsd_libc/arch/ia64/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/ia64/gdtoa/Makefile.inc new file mode 100644 index 000000000..43505eaa3 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/09/22 12:09:24 cherry Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/ia64/gdtoa/arith.h b/lib/nbsd_libc/arch/ia64/gdtoa/arith.h new file mode 100644 index 000000000..4fb91e5d4 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gdtoa/arith.h @@ -0,0 +1,6 @@ +/* $NetBSD: arith.h,v 1.1 2006/09/10 21:22:33 cherry Exp $ */ + +#define IEEE_LITTLE_ENDIAN +#ifndef _IEEE_FP +#define Sudden_Underflow +#endif diff --git a/lib/nbsd_libc/arch/ia64/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/ia64/gdtoa/gd_qnan.h new file mode 100644 index 000000000..92b073466 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gdtoa/gd_qnan.h @@ -0,0 +1,5 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/09/10 21:22:33 cherry Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff80000 diff --git a/lib/nbsd_libc/arch/ia64/gen/Makefile.inc b/lib/nbsd_libc/arch/ia64/gen/Makefile.inc new file mode 100644 index 000000000..03edc59a5 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gen/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.4 2006/09/23 17:39:34 cherry Exp $ + +SRCS+= bswap16.c bswap32.c bswap64.c + +SRCS+= flt_rounds.c fpgetround.c fpsetround.c fpgetmask.c fpsetmask.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c diff --git a/lib/nbsd_libc/arch/ia64/gen/flt_rounds.c b/lib/nbsd_libc/arch/ia64/gen/flt_rounds.c new file mode 100644 index 000000000..a48a42376 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gen/flt_rounds.c @@ -0,0 +1,25 @@ +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +/* __FBSDID("$FreeBSD: src/lib/libc/ia64/gen/flt_rounds.c,v 1.1 2004/07/19 08:17:24 das Exp $"); */ + +#include + +static const int map[] = { + 1, /* round to nearest */ + 3, /* round to zero */ + 2, /* round to negative infinity */ + 0 /* round to positive infinity */ +}; + +int +__flt_rounds(void) +{ + int x; + + __asm("mov %0=ar.fpsr" : "=r" (x)); + return (map[(x >> 10) & 0x03]); +} diff --git a/lib/nbsd_libc/arch/ia64/gen/fpgetmask.c b/lib/nbsd_libc/arch/ia64/gen/fpgetmask.c new file mode 100644 index 000000000..6bf750cbd --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gen/fpgetmask.c @@ -0,0 +1,41 @@ +/*- + * Copyright (c) 2001 Doug Rabson + * 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 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 +/* __FBSDID("$FreeBSD: src/lib/libc/ia64/gen/fpgetmask.c,v 1.4 2003/10/22 09:00:07 marcel Exp $"); */ + +#include +#include + +/* XXX: Move to sysarchs() */ +fp_except +fpgetmask(void) +{ + uint64_t fpsr; + + __asm __volatile("mov %0=ar.fpsr" : "=r" (fpsr)); + return (~fpsr & 0x3d); +} diff --git a/lib/nbsd_libc/arch/ia64/gen/fpgetround.c b/lib/nbsd_libc/arch/ia64/gen/fpgetround.c new file mode 100644 index 000000000..407edce74 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gen/fpgetround.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2003 Marcel Moolenaar + * 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 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. + * + * $FreeBSD: src/lib/libc/ia64/gen/fpgetround.c,v 1.1 2003/01/11 07:24:54 marcel Exp $ + */ + +#include +#include + +fp_rnd +fpgetround(void) +{ + uint64_t fpsr; + + __asm __volatile("mov %0=ar.fpsr" : "=r"(fpsr)); + return ((fp_rnd)((fpsr >> 10) & 3)); +} diff --git a/lib/nbsd_libc/arch/ia64/gen/fpsetmask.c b/lib/nbsd_libc/arch/ia64/gen/fpsetmask.c new file mode 100644 index 000000000..ea87938c1 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gen/fpsetmask.c @@ -0,0 +1,44 @@ +/*- + * Copyright (c) 2001 Doug Rabson + * 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 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 +/* __FBSDID("$FreeBSD: src/lib/libc/ia64/gen/fpsetmask.c,v 1.4 2003/10/22 09:00:07 marcel Exp $"); */ + +#include +#include + +fp_except +fpsetmask(fp_except mask) +{ + int64_t fpsr; + int64_t oldmask; + + __asm __volatile("mov %0=ar.fpsr" : "=r" (fpsr)); + oldmask = ~fpsr & 0x3d; + fpsr = (fpsr & ~0x3d) | (~mask & 0x3d); + __asm __volatile("mov ar.fpsr=%0" :: "r" (fpsr)); + return (oldmask); +} diff --git a/lib/nbsd_libc/arch/ia64/gen/fpsetround.c b/lib/nbsd_libc/arch/ia64/gen/fpsetround.c new file mode 100644 index 000000000..695540692 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/gen/fpsetround.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2003 Marcel Moolenaar + * 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 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. + * + * $FreeBSD: src/lib/libc/ia64/gen/fpsetround.c,v 1.1 2003/01/11 07:24:54 marcel Exp $ + */ + +#include +#include + +fp_rnd +fpsetround(fp_rnd rnd) +{ + uint64_t fpsr; + fp_rnd prev; + + __asm __volatile("mov %0=ar.fpsr" : "=r"(fpsr)); + prev = (fp_rnd)((fpsr >> 10) & 3); + fpsr = (fpsr & ~0xC00ULL) | ((unsigned int)rnd << 10); + __asm __volatile("mov ar.fpsr=%0" :: "r"(fpsr)); + return (prev); +} diff --git a/lib/nbsd_libc/arch/ia64/net/Makefile.inc b/lib/nbsd_libc/arch/ia64/net/Makefile.inc new file mode 100644 index 000000000..fa364cfe5 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/net/Makefile.inc @@ -0,0 +1,2 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/09/10 21:22:33 cherry Exp $ +# XXX: Stub diff --git a/lib/nbsd_libc/arch/ia64/stdlib/Makefile.inc b/lib/nbsd_libc/arch/ia64/stdlib/Makefile.inc new file mode 100644 index 000000000..d1dda0262 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/07/31 20:39:59 dsl Exp $ diff --git a/lib/nbsd_libc/arch/ia64/string/Makefile.inc b/lib/nbsd_libc/arch/ia64/string/Makefile.inc new file mode 100644 index 000000000..eebf40a43 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/string/Makefile.inc @@ -0,0 +1,2 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/07/30 20:57:16 dsl Exp $ + diff --git a/lib/nbsd_libc/arch/ia64/sys/__clone.S b/lib/nbsd_libc/arch/ia64/sys/__clone.S new file mode 100644 index 000000000..a62702e14 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/__clone.S @@ -0,0 +1,3 @@ +/* $NetBSD: __clone.S,v 1.1 2006/09/10 21:22:34 cherry Exp $ */ + +/* XXX: Stub */ \ No newline at end of file diff --git a/lib/nbsd_libc/arch/ia64/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/ia64/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..77451d093 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/__sigaction14_sigtramp.c @@ -0,0 +1,74 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + extern const int __sigtramp_sigcontext_1[]; + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specified or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/ia64/sys/__sigtramp2.S b/lib/nbsd_libc/arch/ia64/sys/__sigtramp2.S new file mode 100644 index 000000000..a305ed4ff --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/__sigtramp2.S @@ -0,0 +1,12 @@ +/* $NetBSD: __sigtramp2.S,v 1.1 2006/09/23 17:39:34 cherry Exp $ */ + +#include "SYS.h" + +/* XXX: Fix this when we look at signal handling */ + +ENTRY(__sigtramp_siginfo_2, 2) + /* XXX: get pointer to ucontext */ + /* setcontext() ie. CALLSYS_NOERROR(setcontext) */ + /* get error code */ + CALLSYS_NOERROR(exit) /* exit */ +END(__sigtramp_siginfo_2) \ No newline at end of file diff --git a/lib/nbsd_libc/arch/ia64/sys/__syscall.S b/lib/nbsd_libc/arch/ia64/sys/__syscall.S new file mode 100644 index 000000000..e3312dbea --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/__syscall.S @@ -0,0 +1,7 @@ +/* $NetBSD: __syscall.S,v 1.2 2006/09/22 12:09:25 cherry Exp $ */ + +#include "SYS.h" + + +RSYSCALL(__syscall) + diff --git a/lib/nbsd_libc/arch/ia64/sys/__vfork14.S b/lib/nbsd_libc/arch/ia64/sys/__vfork14.S new file mode 100644 index 000000000..324396f02 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/__vfork14.S @@ -0,0 +1,8 @@ +/* $NetBSD: __vfork14.S,v 1.2 2006/09/22 17:59:47 cherry Exp $ */ + +#include "SYS.h" + +SYSCALL(__vfork14) + /* XXX: watchout for return values for child/parent */ + RET +END(__vfork14) diff --git a/lib/nbsd_libc/arch/ia64/sys/brk.S b/lib/nbsd_libc/arch/ia64/sys/brk.S new file mode 100644 index 000000000..8be7e907f --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/brk.S @@ -0,0 +1,57 @@ +/* $NetBSD: brk.S,v 1.2 2006/09/23 17:39:34 cherry Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include +/* __FBSDID("$FreeBSD: src/lib/libc/ia64/sys/brk.S,v 1.4 2003/05/16 21:26:39 marcel Exp $"); */ + +#include "SYS.h" + + .globl _end +IMPORT(__curbrk, 8) + + .data +EXPORT(__minbrk) + .quad _end + + .text +ENTRY(_brk, 1) + add r14=@ltoff(__minbrk),gp ;; + ld8 r14=[r14] ;; + ld8 r14=[r14] ;; + cmp.ltu p6,p0=r32,r14 ;; +(p6) mov r32=r14 ;; + st8 [sp]=r32 + CALLSYS_ERROR(break) + ld8 r15=[sp] + add r14=@ltoff(__curbrk),gp ;; + ld8 r14=[r14] ;; + st8 [r14]=r15 + mov ret0=0 + br.ret.sptk.few rp +END(_brk) diff --git a/lib/nbsd_libc/arch/ia64/sys/cerror.S b/lib/nbsd_libc/arch/ia64/sys/cerror.S new file mode 100644 index 000000000..769824104 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/cerror.S @@ -0,0 +1,48 @@ +/* $NetBSD: cerror.S,v 1.2 2006/09/22 17:59:47 cherry Exp $ */ + +/*- + * Copyright (c) 2000 Doug Rabson + * 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 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 +/* __FBSDID("$FreeBSD: src/lib/libc/ia64/sys/cerror.S,v 1.2 2003/03/03 01:09:46 obrien Exp $"); */ + + +ENTRY(__cerror, 0) + alloc loc0=ar.pfs,0,3,1,0 + ;; + mov loc1=rp + mov loc2=ret0 + mov out0=ret0 + ;; + br.call.sptk.few rp=__errno + st4 [ret0]=loc2 + ;; + mov ret0=-1 + mov ar.pfs=loc0 + mov rp=loc1 + ;; + br.ret.sptk.few rp +END(__cerror) diff --git a/lib/nbsd_libc/arch/ia64/sys/exect.S b/lib/nbsd_libc/arch/ia64/sys/exect.S new file mode 100644 index 000000000..5a78ec0eb --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/exect.S @@ -0,0 +1,3 @@ +/* $NetBSD: exect.S,v 1.1 2006/09/10 21:22:34 cherry Exp $ */ + +/* XXX: Stub */ \ No newline at end of file diff --git a/lib/nbsd_libc/arch/ia64/sys/fork.S b/lib/nbsd_libc/arch/ia64/sys/fork.S new file mode 100644 index 000000000..c0df388ee --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/fork.S @@ -0,0 +1,9 @@ +/* $NetBSD: fork.S,v 1.2 2006/09/23 17:39:34 cherry Exp $ */ + +#include "SYS.h" + +ENTRY(__fork, 0) + CALLSYS_ERROR(fork) + /* XXX: get return value for child */ + RET +END(__fork) \ No newline at end of file diff --git a/lib/nbsd_libc/arch/ia64/sys/getcontext.S b/lib/nbsd_libc/arch/ia64/sys/getcontext.S new file mode 100644 index 000000000..ea491ab90 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/getcontext.S @@ -0,0 +1,3 @@ +/* $NetBSD: getcontext.S,v 1.1 2006/09/10 21:22:34 cherry Exp $ */ + +/* XXX: Stub */ \ No newline at end of file diff --git a/lib/nbsd_libc/arch/ia64/sys/pipe.S b/lib/nbsd_libc/arch/ia64/sys/pipe.S new file mode 100644 index 000000000..e545e5a50 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/pipe.S @@ -0,0 +1,50 @@ +/* $NetBSD: pipe.S,v 1.2 2006/09/23 17:39:34 cherry Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include +/* __FBSDID("$FreeBSD: src/lib/libc/ia64/sys/pipe.S,v 1.4 2003/05/16 21:26:39 marcel Exp $"); */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif +/* XXX: Relook param passing */ + +ENTRY(_pipe, 1) + st8 [sp]=r32 + CALLSYS_ERROR(pipe) + ld8 r14=[sp] + ;; + st4 [r14]=ret0,4 + ;; + st4 [r14]=ret1 + mov ret0=0 + br.ret.sptk.few rp +END(_pipe) diff --git a/lib/nbsd_libc/arch/ia64/sys/ptrace.S b/lib/nbsd_libc/arch/ia64/sys/ptrace.S new file mode 100644 index 000000000..bea78b23a --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/ptrace.S @@ -0,0 +1,3 @@ +/* $NetBSD: ptrace.S,v 1.1 2006/09/10 21:22:34 cherry Exp $ */ + +/* XXX: Stub */ \ No newline at end of file diff --git a/lib/nbsd_libc/arch/ia64/sys/sbrk.S b/lib/nbsd_libc/arch/ia64/sys/sbrk.S new file mode 100644 index 000000000..2340e2fa4 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/sbrk.S @@ -0,0 +1,63 @@ +/* $NetBSD: sbrk.S,v 1.2 2006/09/23 17:39:34 cherry Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include +/* __FBSDID("$FreeBSD: src/lib/libc/ia64/sys/sbrk.S,v 1.5 2003/05/16 21:26:39 marcel Exp $"); */ + +#include "SYS.h" + + .globl _end + + .data +EXPORT(__curbrk) + .quad _end + + .text +ENTRY(_sbrk, 1) + add r14 = @ltoff(__curbrk), gp + ;; + ld8 r14 = [r14] + cmp.eq p6, p0 = r32, r0 + ;; + ld8 ret0 = [r14] +(p6) br.ret.sptk.few rp + ;; + add r32 = ret0, r32 + ;; + st8 [sp] = r32 + CALLSYS_ERROR(break) + ld8 r15 = [sp] + add r14 = @ltoff(__curbrk), gp + ;; + ld8 r14 = [r14] + ;; + ld8 ret0 = [r14] + st8 [r14] = r15 + br.ret.sptk.few rp +END(_sbrk) diff --git a/lib/nbsd_libc/arch/ia64/sys/shmat.S b/lib/nbsd_libc/arch/ia64/sys/shmat.S new file mode 100644 index 000000000..cacc70093 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/shmat.S @@ -0,0 +1,3 @@ +/* $NetBSD: shmat.S,v 1.1 2006/09/10 21:22:34 cherry Exp $ */ + +/* XXX: Stub */ \ No newline at end of file diff --git a/lib/nbsd_libc/arch/ia64/sys/syscall.S b/lib/nbsd_libc/arch/ia64/sys/syscall.S new file mode 100644 index 000000000..21359c595 --- /dev/null +++ b/lib/nbsd_libc/arch/ia64/sys/syscall.S @@ -0,0 +1,7 @@ +/* $NetBSD: syscall.S,v 1.2 2006/09/22 12:09:25 cherry Exp $ */ + +#include "SYS.h" + + +RSYSCALL(syscall) + diff --git a/lib/nbsd_libc/arch/m68k/DEFS.h b/lib/nbsd_libc/arch/m68k/DEFS.h new file mode 100644 index 000000000..1abc2d6b0 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/DEFS.h @@ -0,0 +1,3 @@ +/* $NetBSD: DEFS.h,v 1.3 1995/12/13 22:21:23 thorpej Exp $ */ + +#include diff --git a/lib/nbsd_libc/arch/m68k/Makefile.inc b/lib/nbsd_libc/arch/m68k/Makefile.inc new file mode 100644 index 000000000..abe01f49a --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.13 2008/02/09 02:41:06 mrg Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S __mmap.S + +ASM+= _lwp_getprivate.S mremap.S + +.if ${MKSOFTFLOAT} != "no" +CPPFLAGS+= -DSOFTLOFLOAT_NEED_FIXUNS -DSOFTFLOAT -DSOFTFLOATM68K_FOR_GCC +. include +.endif diff --git a/lib/nbsd_libc/arch/m68k/SYS.h b/lib/nbsd_libc/arch/m68k/SYS.h new file mode 100644 index 000000000..cf7099854 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/SYS.h @@ -0,0 +1,97 @@ +/* $NetBSD: SYS.h,v 1.16 2003/08/07 16:42:09 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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. + * + * from: @(#)SYS.h 5.5 (Berkeley) 5/7/91 + */ + +#include +#include + +#ifdef __STDC__ +#define IMMEDIATE # +#define SYSTRAP(x) movl IMMEDIATE SYS_ ## x ## ,%d0; trap IMMEDIATE 0 +#else +#define SYSTRAP(x) movl #SYS_/**/x,%d0; trap #0 +#endif + +#ifdef __ELF__ +#define CERROR _C_LABEL(__cerror) +#define CURBRK _C_LABEL(__curbrk) +#else +#define CERROR _ASM_LABEL(cerror) +#define CURBRK _ASM_LABEL(curbrk) +#endif + +#define _SYSCALL_NOERROR(x,y) \ + ENTRY(x); \ + SYSTRAP(y) + +#define _SYSCALL(x,y) \ + .even; \ + err: jra PIC_PLT(CERROR); \ + _SYSCALL_NOERROR(x,y); \ + jcs err + +#define SYSCALL_NOERROR(x) \ + _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) \ + _SYSCALL(x,x) + +#define PSEUDO_NOERROR(x,y) \ + _SYSCALL_NOERROR(x,y); \ + rts + +#define PSEUDO(x,y) \ + _SYSCALL(x,y); \ + rts + +#define RSYSCALL_NOERROR(x) \ + PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) \ + PSEUDO(x,x) + +#ifdef WEAK_ALIAS +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) +#else +#define WSYSCALL(weak,strong) \ + PSEUDO(weak,weak) +#endif + +#define ASMSTR .asciz + + .globl CERROR diff --git a/lib/nbsd_libc/arch/m68k/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/m68k/gdtoa/Makefile.inc new file mode 100644 index 000000000..66da1e59d --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gdtoa/Makefile.inc @@ -0,0 +1,8 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/03/21 22:50:13 he Exp $ + +SRCS+= strtof.c + +.if ${MACHINE_ARCH} != "m68000" +SRCS+= strtold_pxL.c +SRCS+= strtopxL.c +.endif diff --git a/lib/nbsd_libc/arch/m68k/gdtoa/arith.h b/lib/nbsd_libc/arch/m68k/gdtoa/arith.h new file mode 100644 index 000000000..4674f629c --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_BIG_ENDIAN diff --git a/lib/nbsd_libc/arch/m68k/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/m68k/gdtoa/gd_qnan.h new file mode 100644 index 000000000..eb22e0b82 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gdtoa/gd_qnan.h @@ -0,0 +1,10 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x7ff80000 +#define d_QNAN1 0x0 +#ifndef __mc68010__ +#define ld_QNAN0 0x7fff0000 +#define ld_QNAN1 0x40000000 +#define ld_QNAN2 0x0 +#endif diff --git a/lib/nbsd_libc/arch/m68k/gen/Makefile.inc b/lib/nbsd_libc/arch/m68k/gen/Makefile.inc new file mode 100644 index 000000000..f1ed0466f --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/Makefile.inc @@ -0,0 +1,72 @@ +# $NetBSD: Makefile.inc,v 1.30 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= alloca.S fabs.S + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +# 68000-based machines use a double-extended `long double' type +# for which the generic ieee754 versions can be used +.if ${MACHINE_ARCH} == "m68000" +SRCS+= infinityl_dbl_ieee754.c +.else +SRCS+= infinityl.c +SRCS+= fpclassifyl.c isfinitel.c isinfl.c isnanl.c signbitl.c +.endif + +SRCS+= ashlsi3.S ashrsi3.S \ + lshlsi3.S lshrsi3.S \ + negdf2.S negsf2.S +SRCS+= bswap16.S bswap32.S bswap64.S +SRCS+= _lwp.c + +# 68000-based machines build with a libgcc that includes +# much of the (soft)float and integer support that would +# otherwise be compiled here. +.if (${MACHINE_ARCH} == "m68000" || ${MKSOFTFLOAT} == "yes") +SRCS+= modf_ieee754.c # generic ieee754 version +SRCS+= flt_rounds_softfloat.S +.if ${MKSOFTFLOAT} != "yes" +SRCS+= fpfake.c +.endif +.else +SRCS+= modf.S +SRCS+= flt_rounds.S fpgetmask.S fpgetround.S fpgetsticky.S fpsetmask.S \ + fpsetround.S fpsetsticky.S +SRCS+= adddf3.S addsf3.S cmpdf2.S cmpsf2.S divdf3.S \ + divsf3.S divsi3.S extendsfdf2.S fixdfsi.S fixunsdfsi.S \ + floatsidf.S modsi3.S muldf3.S mulsf3.S mulsi3.S \ + subdf3.S subsf3.S truncdfsf2.S udivsi3.S \ + umodsi3.S umulsi3.S +.endif + +SRCS+= setjmp.S longjmp.c +SRCS+= _setjmp.S +SRCS+= sigsetjmp.S + +SRCS+= makecontext.c resumecontext.S swapcontext.S + +.ifdef M68040 +SRCS+= ldexp_ieee754.c # generic ieee754 version +.elifdef MKSOFTFLOAT +SRCS+= ldexp_ieee754.c # generic ieee754 version +.elifdef M68060 +SRCS+= ldexp_ieee754.c # generic ieee754 version +.elif ${MACHINE_ARCH} == "m68000" +SRCS+= ldexp_ieee754.c # generic ieee754 version +.else +SRCS+= ldexp_881.c +.endif + +LSRCS.m68k.gen= Lint_bswap16.c Lint_bswap32.c Lint_bswap64.c \ + Lint_resumecontext.c Lint_swapcontext.c +LSRCS+= ${LSRCS.m68k.gen} +DPSRCS+= ${LSRCS.m68k.gen} +CLEANFILES+= ${LSRCS.m68k.gen} diff --git a/lib/nbsd_libc/arch/m68k/gen/_lwp.c b/lib/nbsd_libc/arch/m68k/gen/_lwp.c new file mode 100644 index 000000000..6d6e09331 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/_lwp.c @@ -0,0 +1,63 @@ +/* $NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nathan J. Williams and Steve C. Woodford. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + u->uc_mcontext.__gregs[_REG_PC] = (int)start; + + sp = (void **) (stack_base + stack_size); + + *--sp = arg; + *--sp = (void *) _lwp_exit; + + u->uc_mcontext.__gregs[_REG_A7] = (int) sp; +} diff --git a/lib/nbsd_libc/arch/m68k/gen/_setjmp.S b/lib/nbsd_libc/arch/m68k/gen/_setjmp.S new file mode 100644 index 000000000..4efad3841 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/_setjmp.S @@ -0,0 +1,88 @@ +/* $NetBSD: _setjmp.S,v 1.7 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)_setjmp.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: _setjmp.S,v 1.7 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * _setjmp(a) + * by restoring registers from the stack, + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + movl %sp@(4),%a0 /* save area pointer */ + clrl %a0@+ /* no old onstack */ + clrl %a0@+ /* no old sigmask */ + movl %sp,%a0@+ /* save old SP */ + movl %a6,%a0@+ /* save old FP */ + clrl %a0@+ /* no old AP */ + movl %sp@,%a0@+ /* save old PC */ + clrl %a0@+ /* clear PS */ + moveml #0x3CFC,%a0@ /* save other non-scratch regs */ + clrl %d0 /* return zero */ + rts + +ENTRY(_longjmp) + movl %sp@(4),%a0 /* save area pointer */ + addql #8,%a0 /* skip onstack/sigmask */ + tstl %a0@ /* ensure non-zero SP */ + jeq botch /* oops! */ + movl %sp@(8),%d0 /* grab return value */ + jne ok /* non-zero ok */ + moveq #1,%d0 /* else make non-zero */ +ok: + movl %a0@+,%sp /* restore SP */ + movl %a0@+,%a6 /* restore FP */ + addql #4,%a0 /* skip AP */ + movl %a0@+,%sp@ /* restore PC */ + moveml %a0@(4),#0x3CFC /* restore non-scratch regs */ + rts + +botch: + jbsr PIC_PLT(_C_LABEL(longjmperror)) + stop #0 diff --git a/lib/nbsd_libc/arch/m68k/gen/adddf3.S b/lib/nbsd_libc/arch/m68k/gen/adddf3.S new file mode 100644 index 000000000..da3aa182e --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/adddf3.S @@ -0,0 +1,55 @@ +/* $NetBSD: adddf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)adddf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: adddf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* double + double */ +ENTRY(__adddf3) + fmoved %sp@(4),%fp0 + faddd %sp@(12),%fp0 +#ifndef __SVR4_ABI__ + fmoved %fp0,%sp@- + movel %sp@+,%d0 + movel %sp@+,%d1 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/addsf3.S b/lib/nbsd_libc/arch/m68k/gen/addsf3.S new file mode 100644 index 000000000..3c243db61 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/addsf3.S @@ -0,0 +1,53 @@ +/* $NetBSD: addsf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)addsf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: addsf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* single + single */ +ENTRY(__addsf3) + fmoves %sp@(4),%fp0 + fadds %sp@(8),%fp0 +#ifndef __SVR4_ABI__ + fmoves %fp0,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/alloca.S b/lib/nbsd_libc/arch/m68k/gen/alloca.S new file mode 100644 index 000000000..1c305c5d5 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/alloca.S @@ -0,0 +1,59 @@ +/* $NetBSD: alloca.S,v 1.6 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)alloca.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: alloca.S,v 1.6 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* like alloc, but automatic free in return */ + +ENTRY(alloca) + movl %sp@,%a1 /* save return addr */ + movl %sp,%d0 /* get current SP value */ + subl %sp@(4),%d0 /* allocate requested space */ + andb #~3,%d0 /* longword align for efficiency */ + addql #8,%d0 /* reuse space of call frame */ + movl %d0,%sp /* set new SP value */ + lea %sp@(-4),%sp /* account for argument pop in caller */ +#ifdef __SVR4_ABI__ + moveal %d0,%a0 +#endif + jmp %a1@ /* funny return */ diff --git a/lib/nbsd_libc/arch/m68k/gen/ashlsi3.S b/lib/nbsd_libc/arch/m68k/gen/ashlsi3.S new file mode 100644 index 000000000..dc56771a5 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/ashlsi3.S @@ -0,0 +1,51 @@ +/* $NetBSD: ashlsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ashlsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: ashlsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* int << int */ +ENTRY(__ashlsi3) + movel %sp@(8),%d1 + movel %sp@(4),%d0 + asll %d1,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/ashrsi3.S b/lib/nbsd_libc/arch/m68k/gen/ashrsi3.S new file mode 100644 index 000000000..9ea73ce7f --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/ashrsi3.S @@ -0,0 +1,51 @@ +/* $NetBSD: ashrsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ashrsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: ashrsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* int >> int */ +ENTRY(__ashrsi3) + movel %sp@(8),%d1 + movel %sp@(4),%d0 + asrl %d1,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/cmpdf2.S b/lib/nbsd_libc/arch/m68k/gen/cmpdf2.S new file mode 100644 index 000000000..3322c11a4 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/cmpdf2.S @@ -0,0 +1,58 @@ +/* $NetBSD: cmpdf2.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)cmpdf2.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: cmpdf2.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* double > double: 1 */ +/* double < double: -1 */ +/* double == double: 0 */ +ENTRY(__cmpdf2) + fmoved %sp@(4),%fp0 + fcmpd %sp@(12),%fp0 + fbgt Lagtb + fslt %d0 + extbl %d0 + rts +Lagtb: + moveq #1,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/cmpsf2.S b/lib/nbsd_libc/arch/m68k/gen/cmpsf2.S new file mode 100644 index 000000000..d42dd84ff --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/cmpsf2.S @@ -0,0 +1,58 @@ +/* $NetBSD: cmpsf2.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)cmpsf2.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: cmpsf2.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* single > single: 1 */ +/* single < single: -1 */ +/* single == single: 0 */ +ENTRY(__cmpsf2) + fmoves %sp@(4),%fp0 + fcmps %sp@(8),%fp0 + fbgt Lagtb + fslt %d0 + extbl %d0 + rts +Lagtb: + moveq #1,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/divdf3.S b/lib/nbsd_libc/arch/m68k/gen/divdf3.S new file mode 100644 index 000000000..3611290db --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/divdf3.S @@ -0,0 +1,55 @@ +/* $NetBSD: divdf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)divdf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: divdf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* double / double */ +ENTRY(__divdf3) + fmoved %sp@(4),%fp0 + fdivd %sp@(12),%fp0 +#ifndef __SVR4_ABI__ + fmoved %fp0,%sp@- + movel %sp@+,%d0 + movel %sp@+,%d1 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/divsf3.S b/lib/nbsd_libc/arch/m68k/gen/divsf3.S new file mode 100644 index 000000000..219f2233a --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/divsf3.S @@ -0,0 +1,53 @@ +/* $NetBSD: divsf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)divsf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: divsf3.S,v 1.6 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* single / single */ +ENTRY(__divsf3) + fmoves %sp@(4),%fp0 + fdivs %sp@(8),%fp0 +#ifndef __SVR4_ABI__ + fmoves %fp0,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/extendsfdf2.S b/lib/nbsd_libc/arch/m68k/gen/extendsfdf2.S new file mode 100644 index 000000000..2ba61787b --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/extendsfdf2.S @@ -0,0 +1,54 @@ +/* $NetBSD: extendsfdf2.S,v 1.6 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)extendsfdf2.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: extendsfdf2.S,v 1.6 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* (double) float */ +ENTRY(__extendsfdf2) + fmoves %sp@(4),%fp0 +#ifndef __SVR4_ABI__ + fmoved %fp0,%sp@- + movel %sp@+,%d0 + movel %sp@+,%d1 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fabs.S b/lib/nbsd_libc/arch/m68k/gen/fabs.S new file mode 100644 index 000000000..b88bcdc17 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fabs.S @@ -0,0 +1,45 @@ +/* $NetBSD: fabs.S,v 1.8 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by + * + * 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 + +RCSID("$NetBSD: fabs.S,v 1.8 2008/04/28 20:22:56 martin Exp $") + +ENTRY(fabs) +#if defined(__SVR4_ABI__) && defined(__HAVE_68881__) + bclr #31,%sp@(4) + fmoved %sp@(4),%fp0 +#else + movl %sp@(4),%d0 + movl %sp@(8),%d1 + bclr #31,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fixdfsi.S b/lib/nbsd_libc/arch/m68k/gen/fixdfsi.S new file mode 100644 index 000000000..6ef5b56b0 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fixdfsi.S @@ -0,0 +1,50 @@ +/* $NetBSD: fixdfsi.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)fixdfsi.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: fixdfsi.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* (int) double */ +ENTRY(__fixdfsi) + fintrzd %sp@(4),%fp0 + fmovel %fp0,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fixunsdfsi.S b/lib/nbsd_libc/arch/m68k/gen/fixunsdfsi.S new file mode 100644 index 000000000..57669d6c2 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fixunsdfsi.S @@ -0,0 +1,57 @@ +/* $NetBSD: fixunsdfsi.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)fixunsdfsi.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: fixunsdfsi.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* (unsigned) double */ +ENTRY(__fixunsdfsi) + fintrzd %sp@(4),%fp0 + fcmpd #0r2147483648.0,%fp0 + fbge Lwaybig + fmovel %fp0,%d0 + rts +Lwaybig: + fsubd #0r2147483648.0,%fp0 + fmovel %fp0,%d0 + bset #31,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/floatsidf.S b/lib/nbsd_libc/arch/m68k/gen/floatsidf.S new file mode 100644 index 000000000..d4fe49a0a --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/floatsidf.S @@ -0,0 +1,55 @@ +/* $NetBSD: floatsidf.S,v 1.6 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)floatsidf.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: floatsidf.S,v 1.6 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* (double) int */ +ENTRY(__floatsidf) + fmovel %sp@(4),%fp0 +#ifndef __SVR4_ABI__ + fmoved %fp0,%sp@- + movel %sp@+,%d0 + movel %sp@+,%d1 +#endif + rts + diff --git a/lib/nbsd_libc/arch/m68k/gen/flt_rounds.S b/lib/nbsd_libc/arch/m68k/gen/flt_rounds.S new file mode 100644 index 000000000..051e710ad --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/flt_rounds.S @@ -0,0 +1,25 @@ +/* $NetBSD: flt_rounds.S,v 1.9 2000/12/04 12:02:08 is Exp $ */ + +/* + * Written by J.T. Conklin, Apr 6, 1995 + * Public domain. + */ + +#include + + .text + .even +#if 0 +/* XXX This is, effectively, an exclusive-or with 1 operation. */ +_map: + .byte 1 /* round to nearest */ + .byte 0 /* round to zero */ + .byte 3 /* round to negative infinity */ + .byte 2 /* round to positive infinity */ +#endif + +ENTRY(__flt_rounds) + fmovel %fpcr,%d0 + bfextu %d0{#26:#2},%d0 + eorib #1,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/flt_rounds_softfloat.S b/lib/nbsd_libc/arch/m68k/gen/flt_rounds_softfloat.S new file mode 100644 index 000000000..f271b24a7 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/flt_rounds_softfloat.S @@ -0,0 +1,32 @@ +/* $NetBSD: flt_rounds_softfloat.S,v 1.2 2004/09/26 21:13:27 jmmv Exp $ */ + +/* + * Written by J.T. Conklin, Apr 6, 1995 + * Public domain. + + * Broken by Bruce O'Neel Aug 4 2003 + gcc no longer seems to export fpCCR so this fails. + for now just return 0. + */ + +#include + + .text + .even + + /* NB: this is tied to the gcc-2.95 lb1sf68.asm: */ +_map: + .byte 1 /* round to nearest */ + .byte 0 /* round to zero */ + .byte 2 /* round to positive infinity */ + .byte 3 /* round to negative infinity */ + +ENTRY(__flt_rounds) + /* lea _C_LABEL(_fpCCR),%a0 | check the rounding mode */ + /* movew %a0@(6),%d0 | rounding mode in d0 */ + /* lea _map,%a0 */ + moveb #0,%d0 + rts + + + diff --git a/lib/nbsd_libc/arch/m68k/gen/fpclassifyl.c b/lib/nbsd_libc/arch/m68k/gen/fpclassifyl.c new file mode 100644 index 000000000..5caeebf56 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpclassifyl.c @@ -0,0 +1,70 @@ +/* $NetBSD: fpclassifyl.c,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyl.c,v 1.3 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * IEEE 754 compatible 80-bit extended-precision Motorola 68k version + */ +int +__fpclassifyl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + _DIAGASSERT(u.extu_ext.ext_zero == 0); + + if (u.extu_ext.ext_exp == 0 && + (u.extu_ext.ext_frach & 0x80000000) == 0) { + if ((u.extu_ext.ext_frach & 0x7fffffff) == 0 && + u.extu_ext.ext_fracl == 0) + return FP_ZERO; + else + return FP_SUBNORMAL; + } else if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) { + if ((u.extu_ext.ext_frach & 0x7fffffff) == 0 && + u.extu_ext.ext_fracl == 0) + return FP_INFINITE; + else + return FP_NAN; + } + + return FP_NORMAL; +} diff --git a/lib/nbsd_libc/arch/m68k/gen/fpfake.c b/lib/nbsd_libc/arch/m68k/gen/fpfake.c new file mode 100644 index 000000000..b2835d4ad --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpfake.c @@ -0,0 +1,37 @@ +#include + +fp_except +fpgetmask(void) +{ + return 0; +} + +fp_rnd +fpgetround(void) +{ + return 0; +} + +fp_except +fpgetsticky(void) +{ + return 0; +} + +fp_except +fpsetmask(fp_except mask) +{ + return 0; +} + +fp_rnd +fpsetround(fp_rnd rnd_dir) +{ + return 0; +} + +fp_except +fpsetsticky(fp_except sticky) +{ + return 0; +} diff --git a/lib/nbsd_libc/arch/m68k/gen/fpgetmask.S b/lib/nbsd_libc/arch/m68k/gen/fpgetmask.S new file mode 100644 index 000000000..3410585fc --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpgetmask.S @@ -0,0 +1,18 @@ +/* $NetBSD: fpgetmask.S,v 1.7 2002/01/13 21:45:44 thorpej Exp $ */ + +/* + * Written by Charles M. Hannum, Apr 9, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetmask, _fpgetmask) +ENTRY(_fpgetmask) +#else +ENTRY(fpgetmask) +#endif + fmovel %fpcr,%d0 + bfextu %d0{#18:#5},%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fpgetround.S b/lib/nbsd_libc/arch/m68k/gen/fpgetround.S new file mode 100644 index 000000000..29c372932 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpgetround.S @@ -0,0 +1,18 @@ +/* $NetBSD: fpgetround.S,v 1.8 2002/01/13 21:45:44 thorpej Exp $ */ + +/* + * Written by J.T. Conklin, Apr 6, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetround, _fpgetround) +ENTRY(_fpgetround) +#else +ENTRY(fpgetround) +#endif + fmovel %fpcr,%d0 + bfextu %d0{#26:#2},%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fpgetsticky.S b/lib/nbsd_libc/arch/m68k/gen/fpgetsticky.S new file mode 100644 index 000000000..9e482eb8a --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpgetsticky.S @@ -0,0 +1,18 @@ +/* $NetBSD: fpgetsticky.S,v 1.7 2002/01/13 21:45:44 thorpej Exp $ */ + +/* + * Written by Charles M. Hannum, Apr 9, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetsticky, _fpgetsticky) +ENTRY(_fpgetsticky) +#else +ENTRY(fpgetsticky) +#endif + fmovel %fpsr,%d0 + bfextu %d0{#24:#5},%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fpsetmask.S b/lib/nbsd_libc/arch/m68k/gen/fpsetmask.S new file mode 100644 index 000000000..65a5572d4 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpsetmask.S @@ -0,0 +1,23 @@ +/* $NetBSD: fpsetmask.S,v 1.7 2002/01/13 21:45:44 thorpej Exp $ */ + +/* + * Written by Charles M. Hannum, Apr 9, 1995 + * Public Domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetmask, _fpsetmask) +ENTRY(_fpsetmask) +#else +ENTRY(fpsetmask) +#endif + movel %d2,%sp@- + fmovel %fpcr,%d1 + movel %sp@(8),%d2 + bfextu %d1{#18:#5},%d0 + bfins %d2,%d1{#18:#5} + movel %sp@+,%d2 + fmovel %d1,%fpcr + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fpsetround.S b/lib/nbsd_libc/arch/m68k/gen/fpsetround.S new file mode 100644 index 000000000..b02e66975 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpsetround.S @@ -0,0 +1,23 @@ +/* $NetBSD: fpsetround.S,v 1.8 2002/01/13 21:45:44 thorpej Exp $ */ + +/* + * Written by J.T. Conklin, Apr 6, 1995 + * Public Domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetround, _fpsetround) +ENTRY(_fpsetround) +#else +ENTRY(fpsetround) +#endif + movel %d2,%sp@- + fmovel %fpcr,%d1 + movel %sp@(8),%d2 + bfextu %d1{#26,#2},%d0 + bfins %d2,%d1{#26:#2} + movel %sp@+,%d2 + fmovel %d1,%fpcr + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/fpsetsticky.S b/lib/nbsd_libc/arch/m68k/gen/fpsetsticky.S new file mode 100644 index 000000000..f0d7db2ec --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/fpsetsticky.S @@ -0,0 +1,23 @@ +/* $NetBSD: fpsetsticky.S,v 1.7 2002/01/13 21:45:45 thorpej Exp $ */ + +/* + * Written by Charles M. Hannum, Apr 9, 1995 + * Public domain. + */ + +#include + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetsticky, _fpsetsticky) +ENTRY(_fpsetsticky) +#else +ENTRY(fpsetsticky) +#endif + movel %d2,%sp@- + fmovel %fpsr,%d1 + movel %sp@(8),%d2 + bfextu %d1{#24:#5},%d0 + bfins %d2,%d1{#24:#5} + movel %sp@+,%d2 + fmovel %d1,%fpsr + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/infinityl.c b/lib/nbsd_libc/arch/m68k/gen/infinityl.c new file mode 100644 index 000000000..775388c04 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/infinityl.c @@ -0,0 +1,17 @@ +/* $NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:26 lukem Exp $ */ + +/* + * IEEE-compatible infinityl.c for Motorola 68k 80-bit format -- public domain. + * Note that the representation includes 16 bits of padding between exponent + * and mantissa. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:26 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include + +const union __long_double_u __infinityl = + { { 0x7f, 0xff, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0 } }; diff --git a/lib/nbsd_libc/arch/m68k/gen/isfinitel.c b/lib/nbsd_libc/arch/m68k/gen/isfinitel.c new file mode 100644 index 000000000..8b1fd9781 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/isfinitel.c @@ -0,0 +1,58 @@ +/* $NetBSD: isfinitel.c,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinitel.c,v 1.2 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * IEEE 754 compatible 80-bit extended-precision Motorola 68k version + */ +int +__isfinitel(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + _DIAGASSERT(u.extu_ext.ext_zero == 0); + + if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/arch/m68k/gen/isinfl.c b/lib/nbsd_libc/arch/m68k/gen/isinfl.c new file mode 100644 index 000000000..b67676f22 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/isinfl.c @@ -0,0 +1,64 @@ +/* $NetBSD: isinfl.c,v 1.6 2009/01/30 07:00:45 mhitch Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinfl.c,v 1.6 2009/01/30 07:00:45 mhitch Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.3 isinf - test for infinity + * IEEE 754 compatible 80-bit extended-precision Motorola 68k version + */ +int +__isinfl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + /* Note: the explicit integer bit is "don't care". */ + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + ((u.extu_ext.ext_frach & 0x7fffffff) == 0 && u.extu_ext.ext_fracl == 0)); +} diff --git a/lib/nbsd_libc/arch/m68k/gen/isnanl.c b/lib/nbsd_libc/arch/m68k/gen/isnanl.c new file mode 100644 index 000000000..d046f7d33 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/isnanl.c @@ -0,0 +1,64 @@ +/* $NetBSD: isnanl.c,v 1.5 2004/03/04 23:42:38 kleink Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnanl.c,v 1.5 2004/03/04 23:42:38 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.4 isnan - test for a NaN + * IEEE 754 compatible 80-bit extended-precision Motorola 68k version + */ +int +__isnanl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + /* Note: the explicit integer bit is "don't care". */ + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + (u.extu_ext.ext_frach != 0 || u.extu_ext.ext_fracl != 0)); +} diff --git a/lib/nbsd_libc/arch/m68k/gen/ldexp_881.c b/lib/nbsd_libc/arch/m68k/gen/ldexp_881.c new file mode 100644 index 000000000..ab4a0a4e1 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/ldexp_881.c @@ -0,0 +1,55 @@ +/* $NetBSD: ldexp_881.c,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles M. Hannum. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ldexp_881.c,v 1.3 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +/* + * ldexp(value, exp): return value * (2 ** exp). + */ +double +ldexp(value, exp2) + double value; + int exp2; +{ + double temp; + + __asm ("fscalel %2,%1" + : "=f" (temp) + : "0" (value), "g" (exp2)); + return (temp); +} diff --git a/lib/nbsd_libc/arch/m68k/gen/longjmp.c b/lib/nbsd_libc/arch/m68k/gen/longjmp.c new file mode 100644 index 000000000..e8d5d0387 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/longjmp.c @@ -0,0 +1,96 @@ +/* $NetBSD: longjmp.c,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christian Limpach. + * + * 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 "namespace.h" +#include +#include +#include +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include + +typedef struct { + __greg_t __data[6]; + __greg_t __addr[4]; +} __jmp_buf_regs_t; + +void +__longjmp14(jmp_buf env, int val) +{ + struct sigcontext *sc = (void *)env; + __jmp_buf_regs_t *r = (void *)&sc[1]; + ucontext_t uc; + + /* Ensure non-zero SP */ + if (sc->sc_sp == 0) + goto err; + + /* Make return value non-zero */ + if (val == 0) + val = 1; + + /* Save return value in context */ + uc.uc_mcontext.__gregs[_REG_D0] = val; + + /* + * Set _UC_SIGMASK, _UC_CPU and _UC_M68K_UC_USER + * Set _UC_{SET,CLR}STACK according to SS_ONSTACK + */ + uc.uc_flags = _UC_SIGMASK | _UC_CPU | _UC_M68K_UC_USER | + (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK); + + /* Clear uc_link */ + uc.uc_link = 0; + + /* Copy signal mask */ + uc.uc_sigmask = sc->sc_mask; + + /* Copy SP/PC/PS/FP */ + uc.uc_mcontext.__gregs[_REG_A7] = sc->sc_sp; + uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc; + uc.uc_mcontext.__gregs[_REG_PS] = sc->sc_ps; + uc.uc_mcontext.__gregs[_REG_A6] = sc->sc_fp; + + /* Copy remaining non-scratch regs. */ + memcpy(&uc.uc_mcontext.__gregs[_REG_D2], + &r->__data, sizeof(r->__data)); + memcpy(&uc.uc_mcontext.__gregs[_REG_A2], + &r->__addr, sizeof(r->__addr)); + + setcontext(&uc); + err: + longjmperror(); + abort(); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/m68k/gen/lshlsi3.S b/lib/nbsd_libc/arch/m68k/gen/lshlsi3.S new file mode 100644 index 000000000..2d2e5921e --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/lshlsi3.S @@ -0,0 +1,51 @@ +/* $NetBSD: lshlsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)lshlsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: lshlsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* unsigned << unsigned */ +ENTRY(__lshlsi3) + movel %sp@(8),%d1 + movel %sp@(4),%d0 + lsll %d1,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/lshrsi3.S b/lib/nbsd_libc/arch/m68k/gen/lshrsi3.S new file mode 100644 index 000000000..0f1303956 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/lshrsi3.S @@ -0,0 +1,51 @@ +/* $NetBSD: lshrsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)lshrsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: lshrsi3.S,v 1.5 2003/08/07 16:42:10 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* unsigned >> unsigned */ +ENTRY(__lshrsi3) + movel %sp@(8),%d1 + movel %sp@(4),%d0 + lsrl %d1,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/makecontext.c b/lib/nbsd_libc/arch/m68k/gen/makecontext.c new file mode 100644 index 000000000..173904ea6 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/makecontext.c @@ -0,0 +1,65 @@ +/* $NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + mcontext_t *mcp = &ucp->uc_mcontext; + int *sp; + va_list ap; + + mcp->__gregs[_REG_PC] = (__greg_t)func; + + sp = (int *)((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + sp = (int *)((uintptr_t)sp & ~0x3); /* Align on word boundary. */ + sp -= (argc + 1); /* Make room for retaddr and args. */ + mcp->__gregs[_REG_A7] = (__greg_t)sp; + mcp->__gregs[_REG_A6] = 0; /* Wipe out frame pointer. */ + + *sp++ = (int)_resumecontext; + + va_start(ap, argc); + while (argc-- > 0) + *sp++ = va_arg(ap, int); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/m68k/gen/modf.S b/lib/nbsd_libc/arch/m68k/gen/modf.S new file mode 100644 index 000000000..57f236932 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/modf.S @@ -0,0 +1,61 @@ +/* $NetBSD: modf.S,v 1.6 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)modf.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: modf.S,v 1.6 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * double modf(val, iptr) + * returns: xxx and n (in *iptr) where val == n.xxx + */ +ENTRY(modf) + fmoved %sp@(4),%fp0 + movel %sp@(12),%a0 + fintrzx %fp0,%fp1 + fmoved %fp1,%a0@ + fsubx %fp1,%fp0 +#ifndef __SVR4_ABI__ + fmoved %fp0,%sp@- + movel %sp@+,%d0 + movel %sp@+,%d1 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/muldf3.S b/lib/nbsd_libc/arch/m68k/gen/muldf3.S new file mode 100644 index 000000000..e637ebfce --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/muldf3.S @@ -0,0 +1,55 @@ +/* $NetBSD: muldf3.S,v 1.7 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)muldf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: muldf3.S,v 1.7 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* double * double */ +ENTRY(__muldf3) + fmoved %sp@(4),%fp0 + fmuld %sp@(12),%fp0 +#ifndef __SVR4_ABI__ + fmoved %fp0,%sp@- + movel %sp@+,%d0 + movel %sp@+,%d1 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/mulsf3.S b/lib/nbsd_libc/arch/m68k/gen/mulsf3.S new file mode 100644 index 000000000..61bf7a3c8 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/mulsf3.S @@ -0,0 +1,53 @@ +/* $NetBSD: mulsf3.S,v 1.6 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)mulsf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: mulsf3.S,v 1.6 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* single * single */ +ENTRY(__mulsf3) + fmoves %sp@(4),%fp0 + fmuls %sp@(8),%fp0 +#ifndef __SVR4_ABI__ + fmoves %fp0,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/nanf.c b/lib/nbsd_libc/arch/m68k/gen/nanf.c new file mode 100644 index 000000000..8516c1b23 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:01 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0x7f, 0xc0, 0, 0 } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/m68k/gen/negdf2.S b/lib/nbsd_libc/arch/m68k/gen/negdf2.S new file mode 100644 index 000000000..805412a78 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/negdf2.S @@ -0,0 +1,55 @@ +/* $NetBSD: negdf2.S,v 1.7 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)negdf2.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: negdf2.S,v 1.7 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* -double */ +ENTRY(__negdf2) +#if defined(__SVR4_ABI__) && defined(__HAVE_68881__) + fnegd %sp@(4),%fp0 +#else + movel %sp@(4),%d0 + movel %sp@(8),%d1 + bchg #31,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/negsf2.S b/lib/nbsd_libc/arch/m68k/gen/negsf2.S new file mode 100644 index 000000000..3ff73c492 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/negsf2.S @@ -0,0 +1,54 @@ +/* $NetBSD: negsf2.S,v 1.7 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID"from: @(#)negsf2.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: negsf2.S,v 1.7 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* -single */ +ENTRY(__negsf2) +#if defined(__SVR4_ABI__) && defined(__HAVE_68881__) + fnegs %sp@(4),%fp0 +#else + movel %sp@(4),%d0 + bchg #31,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/resumecontext.S b/lib/nbsd_libc/arch/m68k/gen/resumecontext.S new file mode 100644 index 000000000..078d55f58 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/resumecontext.S @@ -0,0 +1,59 @@ +/* $NetBSD: resumecontext.S,v 1.5 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: resumecontext.S,v 1.5 2008/04/28 20:22:56 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + +/* + * This assembly-language implementation differs from the (obvious) + * C-language implementation only in not clobbering the previous + * function's return address (us), which is the point of the exercise. + */ + +ENTRY(_resumecontext) + subl #(4 + 1024),%sp /* retaddr preservation + ucontext_t */ + lea %sp@,%a0 + movl %a0,%sp@- + jbsr PIC_PLT(_C_LABEL(_getcontext)) + movl %sp@(4 + 4),%sp@ /* uc_link */ + tstl %sp@ /* check for NULL */ + jne 1f + jbsr PIC_PLT(_C_LABEL(exit)) /* cleanly exit(0) */ + jmp 2f +1: jbsr PIC_PLT(_C_LABEL(setcontext)) + /* NOTREACHED */ + /* But just in case... */ +2: movl #-1,%sp@ + jbsr PIC_PLT(_C_LABEL(_exit)) + /* NOTREACHED */ diff --git a/lib/nbsd_libc/arch/m68k/gen/setjmp.S b/lib/nbsd_libc/arch/m68k/gen/setjmp.S new file mode 100644 index 000000000..945eb7efd --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/setjmp.S @@ -0,0 +1,93 @@ +/* $NetBSD: setjmp.S,v 1.12 2006/03/09 16:20:27 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)setjmp.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: setjmp.S,v 1.12 2006/03/09 16:20:27 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +ENTRY(__setjmp14) + /* Get signal stack info. Note overlay of ss_sp and ss_size! */ + lea %sp@(-12),%sp /* sizeof(stack_t) */ + clrl %sp@ /* ss = NULL */ + movl %sp,%sp@(4) /* oss = stack_t on stack */ + jbsr PIC_PLT(_C_LABEL(__sigaltstack14)) + + movl %sp@(8),%d0 /* ss_flags */ + andl #1,%d0 /* extract SS_ONSTACK */ + lea %sp@(12),%sp /* pop stack_t */ + + /* Get pointer to jmp_buf; a sigcontext is at the beginning. */ + movl %sp@(4),%a0 + movl %d0,%a0@ /* store onstack */ + clrl %a0@(4) /* unused word (old style signal mask) */ + + /* Get the signal mask. */ + pea %a0@(28) /* oset = &sc.sc_mask */ + movl #0,%sp@- /* set = NULL */ + movl #0,%sp@- /* action = 0 */ + jbsr PIC_PLT(_C_LABEL(__sigprocmask14)) + addl #12,%sp + + movl %sp@(4),%a0 /* get jmp_buf pointer again */ + lea %sp@(4),%a1 /* adjust SP since we won't rts */ + movl %a1,%a0@(8) /* save SP */ + movl %a6,%a0@(12) /* save FP */ + clrl %a0@(16) /* no AP */ + movl %sp@,%a0@(20) /* save return PC */ + clrl %a0@(24) /* clear PS */ + + /* Save remaining non-scratch regs after signal mask. */ + moveml #0x3CFC,%a0@(44) + + clrl %d0 /* return 0 */ + rts + diff --git a/lib/nbsd_libc/arch/m68k/gen/signbitl.c b/lib/nbsd_libc/arch/m68k/gen/signbitl.c new file mode 100644 index 000000000..157471634 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/signbitl.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitl.c,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: signbitl.c,v 1.2 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * IEEE 754 compatible 80-bit extended-precision Motorola 68k version + */ +int +__signbitl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_sign == 1); +} diff --git a/lib/nbsd_libc/arch/m68k/gen/sigsetjmp.S b/lib/nbsd_libc/arch/m68k/gen/sigsetjmp.S new file mode 100644 index 000000000..d64865ffb --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/sigsetjmp.S @@ -0,0 +1,77 @@ +/* $NetBSD: sigsetjmp.S,v 1.8 2006/03/09 16:20:27 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)_setjmp.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: sigsetjmp.S,v 1.8 2006/03/09 16:20:27 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a,m) + * by restoring registers from the stack, + * The previous signal state is restored if 'm' was non-zero. + */ + +/* grab _JBLEN */ +#include + +ENTRY(__sigsetjmp14) + movl %sp@(8),%d1 /* grab the mask */ + movl %sp@(4),%a0 /* grab the area pointer */ + movl %d1,%a0@(_JBLEN * 4) /* save at end of area */ + tstl %d1 + bne dosig + jra PIC_PLT(_C_LABEL(_setjmp)) +dosig: + jra PIC_PLT(_C_LABEL(__setjmp14)) + + +ENTRY(__siglongjmp14) + movl %sp@(4),%a0 /* save area pointer */ + tstl %a0@(_JBLEN * 4) /* check mask... */ + bne didsig + jra PIC_PLT(_C_LABEL(_longjmp)) +didsig: + jra PIC_PLT(_C_LABEL(__longjmp14)) diff --git a/lib/nbsd_libc/arch/m68k/gen/subdf3.S b/lib/nbsd_libc/arch/m68k/gen/subdf3.S new file mode 100644 index 000000000..beba95510 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/subdf3.S @@ -0,0 +1,55 @@ +/* $NetBSD: subdf3.S,v 1.6 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)subdf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: subdf3.S,v 1.6 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* double - double */ +ENTRY(__subdf3) + fmoved %sp@(4),%fp0 + fsubd %sp@(12),%fp0 +#ifndef __SVR4_ABI__ + fmoved %fp0,%sp@- + movel %sp@+,%d0 + movel %sp@+,%d1 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/subsf3.S b/lib/nbsd_libc/arch/m68k/gen/subsf3.S new file mode 100644 index 000000000..ab3a852a4 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/subsf3.S @@ -0,0 +1,53 @@ +/* $NetBSD: subsf3.S,v 1.6 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)subsf3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: subsf3.S,v 1.6 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* single - single */ +ENTRY(__subsf3) + fmoves %sp@(4),%fp0 + fsubs %sp@(8),%fp0 +#ifndef __SVR4_ABI__ + fmoves %fp0,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/swapcontext.S b/lib/nbsd_libc/arch/m68k/gen/swapcontext.S new file mode 100644 index 000000000..5cb0aa2c3 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/swapcontext.S @@ -0,0 +1,49 @@ +/* $NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:56 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + +ENTRY(swapcontext) + movl %sp@(4),%sp@- | push oucp on stack + jbsr PIC_PLT(_C_LABEL(_getcontext)) | getcontext(oucp) + tstl %d0 | OK? + jne L1 + movl %sp@,%a0 + addql #8,%a0@(96) | adjust saved stack pointer (again) + movl %sp@(4),%a0@(100) | adjust saved program counter (again) + movl %sp@(12),%sp@ | push ucp on stack + jbsr PIC_PLT(_C_LABEL(setcontext)) | setcontext(ucp) +L1: addql #4,%sp | pop ucp + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/truncdfsf2.S b/lib/nbsd_libc/arch/m68k/gen/truncdfsf2.S new file mode 100644 index 000000000..7a3e1739f --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/truncdfsf2.S @@ -0,0 +1,52 @@ +/* $NetBSD: truncdfsf2.S,v 1.6 2003/08/07 16:42:11 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)truncdfsf2.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: truncdfsf2.S,v 1.6 2003/08/07 16:42:11 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* (float) double */ +ENTRY(__truncdfsf2) + fmoved %sp@(4),%fp0 +#ifndef __SVR4_ABI__ + fmoves %fp0,%d0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/gen/umulsi3.S b/lib/nbsd_libc/arch/m68k/gen/umulsi3.S new file mode 100644 index 000000000..c6d6fb22d --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/gen/umulsi3.S @@ -0,0 +1,50 @@ +/* $NetBSD: umulsi3.S,v 1.5 2003/08/07 16:42:12 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)umulsi3.s 5.1 (Berkeley) 6/7/90") +#else + RCSID("$NetBSD: umulsi3.S,v 1.5 2003/08/07 16:42:12 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* unsigned * unsigned */ +ENTRY(__umulsi3) + movel %sp@(4),%d0 + mulul %sp@(8),%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/net/Makefile.inc b/lib/nbsd_libc/arch/m68k/net/Makefile.inc new file mode 100644 index 000000000..dc2ddf344 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/net/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 1995/02/25 14:58:55 cgd Exp $ + +SRCS+= htonl.S htons.S ntohl.S ntohs.S diff --git a/lib/nbsd_libc/arch/m68k/quad/ashldi3.S b/lib/nbsd_libc/arch/m68k/quad/ashldi3.S new file mode 100644 index 000000000..22e527d05 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/quad/ashldi3.S @@ -0,0 +1,60 @@ +/* $NetBSD: ashldi3.S,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 + +| d0 msw +| d1 lsw +| d2 shift +| d3 offset (32 - shift) + +ENTRY(__ashldi3) + link %a6,#0 + moveml %d2-%d4,%sp@- + movel %a6@(8),%d0 + movel %a6@(12),%d1 + movel %a6@(16),%d2 + moveq #32,%d3 + subl %d2,%d3 + jgt L2 + negl %d3 + movel %d1,%d0 + asll %d3,%d0 + clrl %d1 + jra L3 +L2: asll %d2,%d0 + movel %d1,%d4 + lsrl %d3,%d4 + orl %d4,%d0 + asll %d2,%d1 +L3: moveml %a6@(-12),%d2-%d4 + unlk %a6 + rts diff --git a/lib/nbsd_libc/arch/m68k/quad/ashrdi3.S b/lib/nbsd_libc/arch/m68k/quad/ashrdi3.S new file mode 100644 index 000000000..5289ec977 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/quad/ashrdi3.S @@ -0,0 +1,61 @@ +/* $NetBSD: ashrdi3.S,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 + +| d0 msw +| d1 lsw +| d2 shift +| d3 offset (32 - shift) + +ENTRY(__ashrdi3) + link %a6,#0 + moveml %d2-%d4,%sp@- + movel %a6@(8),%d0 + movel %a6@(12),%d1 + movel %a6@(16),%d2 + moveq #32,%d3 + subl %d2,%d3 + jgt L2 + negl %d3 + movel %d0,%d1 + asrl %d3,%d1 + smi %d0 + extbl %d0 + jra L3 +L2: lsrl %d2,%d1 + movel %d0,%d4 + asll %d3,%d4 + orl %d4,%d1 + asrl %d2,%d0 +L3: moveml %a6@(-12),%d2-%d4 + unlk %a6 + rts diff --git a/lib/nbsd_libc/arch/m68k/quad/lshrdi3.S b/lib/nbsd_libc/arch/m68k/quad/lshrdi3.S new file mode 100644 index 000000000..cf3794d50 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/quad/lshrdi3.S @@ -0,0 +1,60 @@ +/* $NetBSD: lshrdi3.S,v 1.5 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 + +| d0 msw +| d1 lsw +| d2 shift +| d3 offset (32 - shift) + +ENTRY(__lshrdi3) + link %a6,#0 + moveml %d2-%d4,%sp@- + movel %a6@(8),%d0 + movel %a6@(12),%d1 + movel %a6@(16),%d2 + moveq #32,%d3 + subl %d2,%d3 + jgt L2 + negl %d3 + movel %d0,%d1 + lsrl %d3,%d1 + clrl %d0 + jra L3 +L2: lsrl %d2,%d1 + movel %d0,%d4 + asll %d3,%d4 + orl %d4,%d1 + lsrl %d2,%d0 +L3: moveml %a6@(-12),%d2-%d4 + unlk %a6 + rts diff --git a/lib/nbsd_libc/arch/m68k/softfloat/m68k-gcc.h b/lib/nbsd_libc/arch/m68k/softfloat/m68k-gcc.h new file mode 100644 index 000000000..07bd45b37 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/softfloat/m68k-gcc.h @@ -0,0 +1,89 @@ +/* $NetBSD: m68k-gcc.h,v 1.2 2005/12/24 21:11:16 perry Exp $ */ + +/* +------------------------------------------------------------------------------- +One of the macros `BIGENDIAN' or `LITTLEENDIAN' must be defined. +------------------------------------------------------------------------------- +*/ +#include +#if _BYTE_ORDER == _BIG_ENDIAN +#define BIGENDIAN +#endif +#if _BYTE_ORDER == _LITTLE_ENDIAN +#define LITTLEENDIAN +#endif + +/* +------------------------------------------------------------------------------- +The macro `BITS64' can be defined to indicate that 64-bit integer types are +supported by the compiler. +------------------------------------------------------------------------------- +*/ +#define BITS64 + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines the most convenient type that holds +integers of at least as many bits as specified. For example, `uint8' should +be the most convenient type that can hold unsigned integers of as many as +8 bits. The `flag' type must be able to hold either a 0 or 1. For most +implementations of C, `flag', `uint8', and `int8' should all be `typedef'ed +to the same as `int'. +------------------------------------------------------------------------------- +*/ +typedef int flag; +typedef int uint8; +typedef int int8; +typedef int uint16; +typedef int int16; +typedef unsigned int uint32; +typedef signed int int32; +#ifdef BITS64 +typedef unsigned long long int uint64; +typedef signed long long int int64; +#endif + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines a type that holds integers +of _exactly_ the number of bits specified. For instance, for most +implementation of C, `bits16' and `sbits16' should be `typedef'ed to +`unsigned short int' and `signed short int' (or `short int'), respectively. +------------------------------------------------------------------------------- +*/ +typedef unsigned char bits8; +typedef signed char sbits8; +typedef unsigned short int bits16; +typedef signed short int sbits16; +typedef unsigned int bits32; +typedef signed int sbits32; +#ifdef BITS64 +typedef unsigned long long int bits64; +typedef signed long long int sbits64; +#endif + +#ifdef BITS64 +/* +------------------------------------------------------------------------------- +The `LIT64' macro takes as its argument a textual integer literal and +if necessary ``marks'' the literal as having a 64-bit integer type. +For example, the GNU C Compiler (`gcc') requires that 64-bit literals be +appended with the letters `LL' standing for `long long', which is `gcc's +name for the 64-bit integer type. Some compilers may allow `LIT64' to be +defined as the identity macro: `#define LIT64( a ) a'. +------------------------------------------------------------------------------- +*/ +#define LIT64( a ) a##LL +#endif + +/* +------------------------------------------------------------------------------- +The macro `INLINE' can be used before functions that should be inlined. If +a compiler does not support explicit inlining, this macro should be defined +to be `static'. +------------------------------------------------------------------------------- +*/ +#define INLINE static inline + +#define FLOAT64_DEMANGLE(a) (a) +#define FLOAT64_MANGLE(a) (a) diff --git a/lib/nbsd_libc/arch/m68k/softfloat/milieu.h b/lib/nbsd_libc/arch/m68k/softfloat/milieu.h new file mode 100644 index 000000000..462472b23 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/softfloat/milieu.h @@ -0,0 +1,48 @@ +/* $NetBSD: milieu.h,v 1.1 2004/09/26 21:13:27 jmmv Exp $ */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +Include common integer types and flags. +------------------------------------------------------------------------------- +*/ +#include "m68k-gcc.h" + +/* +------------------------------------------------------------------------------- +Symbolic Boolean literals. +------------------------------------------------------------------------------- +*/ +enum { + FALSE = 0, + TRUE = 1 +}; diff --git a/lib/nbsd_libc/arch/m68k/softfloat/softfloat.h b/lib/nbsd_libc/arch/m68k/softfloat/softfloat.h new file mode 100644 index 000000000..1c4c1533d --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/softfloat/softfloat.h @@ -0,0 +1,302 @@ +/* $NetBSD: softfloat.h,v 1.2 2006/05/16 20:55:51 mrg Exp $ */ + +/* This is a derivative work. */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +The macro `FLOATX80' must be defined to enable the extended double-precision +floating-point format `floatx80'. If this macro is not defined, the +`floatx80' type will not be defined, and none of the functions that either +input or output the `floatx80' type will be defined. The same applies to +the `FLOAT128' macro and the quadruple-precision format `float128'. +------------------------------------------------------------------------------- +*/ +#define FLOATX80 +/* #define FLOAT128 */ + +#include + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point types. +------------------------------------------------------------------------------- +*/ +typedef unsigned int float32; +typedef unsigned long long float64; +#ifdef FLOATX80 +typedef struct { + unsigned short high; + unsigned long long low; +} floatx80; +#endif +#ifdef FLOAT128 +typedef struct { + unsigned long long high, low; +} float128; +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point underflow tininess-detection mode. +------------------------------------------------------------------------------- +*/ +#ifndef SOFTFLOAT_FOR_GCC +extern int8 float_detect_tininess; +#endif +enum { + float_tininess_after_rounding = 0, + float_tininess_before_rounding = 1 +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point rounding mode. +------------------------------------------------------------------------------- +*/ +extern fp_rnd float_rounding_mode; +enum { + float_round_nearest_even = FP_RN, + float_round_to_zero = FP_RZ, + float_round_down = FP_RM, + float_round_up = FP_RP +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point exception flags. +------------------------------------------------------------------------------- +*/ +extern fp_except float_exception_flags; +extern fp_except float_exception_mask; +enum { + float_flag_inexact = FP_X_IMP, + float_flag_underflow = FP_X_UFL, + float_flag_overflow = FP_X_OFL, + float_flag_divbyzero = FP_X_DZ, + float_flag_invalid = FP_X_INV +}; + +/* +------------------------------------------------------------------------------- +Routine to raise any or all of the software IEC/IEEE floating-point +exception flags. +------------------------------------------------------------------------------- +*/ +void float_raise( fp_except ); + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE integer-to-floating-point conversion routines. +------------------------------------------------------------------------------- +*/ +float32 int32_to_float32( int ); +float64 int32_to_float64( int ); +#ifdef FLOATX80 +floatx80 int32_to_floatx80( int ); +#endif +#ifdef FLOAT128 +float128 int32_to_float128( int ); +#endif +float32 int64_to_float32( long long ); +float64 int64_to_float64( long long ); +#ifdef FLOATX80 +floatx80 int64_to_floatx80( long long ); +#endif +#ifdef FLOAT128 +float128 int64_to_float128( long long ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float32_to_int32( float32 ); +int float32_to_int32_round_to_zero( float32 ); +unsigned int float32_to_uint32_round_to_zero( float32 ); +long long float32_to_int64( float32 ); +long long float32_to_int64_round_to_zero( float32 ); +float64 float32_to_float64( float32 ); +#ifdef FLOATX80 +floatx80 float32_to_floatx80( float32 ); +#endif +#ifdef FLOAT128 +float128 float32_to_float128( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision operations. +------------------------------------------------------------------------------- +*/ +float32 float32_round_to_int( float32 ); +float32 float32_add( float32, float32 ); +float32 float32_sub( float32, float32 ); +float32 float32_mul( float32, float32 ); +float32 float32_div( float32, float32 ); +float32 float32_rem( float32, float32 ); +float32 float32_sqrt( float32 ); +flag float32_eq( float32, float32 ); +flag float32_le( float32, float32 ); +flag float32_lt( float32, float32 ); +flag float32_eq_signaling( float32, float32 ); +flag float32_le_quiet( float32, float32 ); +flag float32_lt_quiet( float32, float32 ); +#ifndef SOFTFLOAT_FOR_GCC +flag float32_is_signaling_nan( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float64_to_int32( float64 ); +int float64_to_int32_round_to_zero( float64 ); +unsigned int float64_to_uint32_round_to_zero( float64 ); +long long float64_to_int64( float64 ); +long long float64_to_int64_round_to_zero( float64 ); +float32 float64_to_float32( float64 ); +#ifdef FLOATX80 +floatx80 float64_to_floatx80( float64 ); +#endif +#ifdef FLOAT128 +float128 float64_to_float128( float64 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision operations. +------------------------------------------------------------------------------- +*/ +float64 float64_round_to_int( float64 ); +float64 float64_add( float64, float64 ); +float64 float64_sub( float64, float64 ); +float64 float64_mul( float64, float64 ); +float64 float64_div( float64, float64 ); +float64 float64_rem( float64, float64 ); +float64 float64_sqrt( float64 ); +flag float64_eq( float64, float64 ); +flag float64_le( float64, float64 ); +flag float64_lt( float64, float64 ); +flag float64_eq_signaling( float64, float64 ); +flag float64_le_quiet( float64, float64 ); +flag float64_lt_quiet( float64, float64 ); +flag float64_is_signaling_nan( float64 ); + +#ifdef FLOATX80 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int floatx80_to_int32( floatx80 ); +int floatx80_to_int32_round_to_zero( floatx80 ); +long long floatx80_to_int64( floatx80 ); +long long floatx80_to_int64_round_to_zero( floatx80 ); +float32 floatx80_to_float32( floatx80 ); +float64 floatx80_to_float64( floatx80 ); +#ifdef FLOAT128 +float128 floatx80_to_float128( floatx80 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision rounding precision. Valid +values are 32, 64, and 80. +------------------------------------------------------------------------------- +*/ +extern int floatx80_rounding_precision; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision operations. +------------------------------------------------------------------------------- +*/ +floatx80 floatx80_round_to_int( floatx80 ); +floatx80 floatx80_add( floatx80, floatx80 ); +floatx80 floatx80_sub( floatx80, floatx80 ); +floatx80 floatx80_mul( floatx80, floatx80 ); +floatx80 floatx80_div( floatx80, floatx80 ); +floatx80 floatx80_rem( floatx80, floatx80 ); +floatx80 floatx80_sqrt( floatx80 ); +flag floatx80_eq( floatx80, floatx80 ); +flag floatx80_le( floatx80, floatx80 ); +flag floatx80_lt( floatx80, floatx80 ); +flag floatx80_eq_signaling( floatx80, floatx80 ); +flag floatx80_le_quiet( floatx80, floatx80 ); +flag floatx80_lt_quiet( floatx80, floatx80 ); +flag floatx80_is_signaling_nan( floatx80 ); +flag floatx80_is_nan( floatx80 ); + +#endif + +#ifdef FLOAT128 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float128_to_int32( float128 ); +int float128_to_int32_round_to_zero( float128 ); +long long float128_to_int64( float128 ); +long long float128_to_int64_round_to_zero( float128 ); +float32 float128_to_float32( float128 ); +float64 float128_to_float64( float128 ); +#ifdef FLOATX80 +floatx80 float128_to_floatx80( float128 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision operations. +------------------------------------------------------------------------------- +*/ +float128 float128_round_to_int( float128 ); +float128 float128_add( float128, float128 ); +float128 float128_sub( float128, float128 ); +float128 float128_mul( float128, float128 ); +float128 float128_div( float128, float128 ); +float128 float128_rem( float128, float128 ); +float128 float128_sqrt( float128 ); +flag float128_eq( float128, float128 ); +flag float128_le( float128, float128 ); +flag float128_lt( float128, float128 ); +flag float128_eq_signaling( float128, float128 ); +flag float128_le_quiet( float128, float128 ); +flag float128_lt_quiet( float128, float128 ); +flag float128_is_signaling_nan( float128 ); + +#endif diff --git a/lib/nbsd_libc/arch/m68k/stdlib/Makefile.inc b/lib/nbsd_libc/arch/m68k/stdlib/Makefile.inc new file mode 100644 index 000000000..4945ed9a0 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/stdlib/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.7 2009/08/11 17:30:43 dsl Exp $ + +SRCS+= abs.S llabs.S +NO_SRCS+= labs.S imaxabs.S diff --git a/lib/nbsd_libc/arch/m68k/stdlib/abs.S b/lib/nbsd_libc/arch/m68k/stdlib/abs.S new file mode 100644 index 000000000..261136df9 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/stdlib/abs.S @@ -0,0 +1,58 @@ +/* $NetBSD: abs.S,v 1.6 2003/08/07 16:42:12 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)abs.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: abs.S,v 1.6 2003/08/07 16:42:12 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* labs - long int absolute value */ + +ENTRY_NOPROFILE(labs) +/* fall through to: */ + +/* abs - int absolute value */ + +ENTRY(abs) + movl %sp@(4),%d0 + jge L1 + negl %d0 +L1: + rts diff --git a/lib/nbsd_libc/arch/m68k/stdlib/llabs.S b/lib/nbsd_libc/arch/m68k/stdlib/llabs.S new file mode 100644 index 000000000..b565dedcb --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/stdlib/llabs.S @@ -0,0 +1,63 @@ +/* $NetBSD: llabs.S,v 1.4 2008/08/04 21:29:28 matt Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)abs.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: llabs.S,v 1.4 2008/08/04 21:29:28 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(llabs, _llabs) +WEAK_ALIAS(imaxabs, _llabs) +#endif +/* llabs - long long int absolute value */ + +#ifdef WEAK_ALIAS +ENTRY(_llabs) +#else +ENTRY(llabs) +#endif + movl %sp@(8),%d1 + movl %sp@(4),%d0 + jge L1 + negl %d1 + negxl %d0 +L1: + rts diff --git a/lib/nbsd_libc/arch/m68k/string/Makefile.inc b/lib/nbsd_libc/arch/m68k/string/Makefile.inc new file mode 100644 index 000000000..239dce29d --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/string/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/30 20:57:16 dsl Exp $ + +SRCS+= bcmp.S bcopy.S bzero.S ffs.S memcmp.S memset.S \ + strcat.S strcmp.S strcpy.S strlen.S \ + strncmp.S strncpy.S \ + swab.S +SRCS+= memcpy.S memccpy.S memmove.S strchr.S strrchr.S diff --git a/lib/nbsd_libc/arch/m68k/string/memccpy.S b/lib/nbsd_libc/arch/m68k/string/memccpy.S new file mode 100644 index 000000000..4a88a9c8e --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/string/memccpy.S @@ -0,0 +1,82 @@ +/* $NetBSD: memccpy.S,v 1.1 2001/02/05 00:17:22 scottr Exp $ */ + +/* + * Copyright (C) 1999 Scott Reynolds. 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 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. + */ + +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: memccpy.S,v 1.1 2001/02/05 00:17:22 scottr Exp $") +#endif /* LIBC_SCCS and not lint */ + +ENTRY(memccpy) + movl %sp@(16),%d0 | count + jeq Lmcbail | nothing to do + + movl %sp@(4),%a0 | a0 = toaddr + subql #1,%d0 | prepare count for DBcc loop + movl %sp@(8),%a1 | a1 = fromaddr + movl %sp@(12),%d1 | d1 = terminator + jeq Lmcloop | handle ASCII NUL specially + + movl %d2,%sp@- | save scratch register +Lmcnzloop: + movb %a1@+,%d2 | move a byte + movb %d2,%a0@+ + cmpb %d2,%d1 | found the terminator? + dbeq %d0,Lmcnzloop | if not, keep transferring, + jeq Lmcnzdone | otherwise done + clrw %d0 | check to see if more to do + subql #1,%d0 + jcc Lmcnzloop | yes, keep going... + + movl %sp@+,%d2 | restore scratch register + movql #0,%d0 | no terminator found, return NULL +#ifdef __SVR4_ABI__ + movl %d0,%a0 | XXX maybe use lea to avoid stall? +#endif + rts + +Lmcloop: + movb %a1@+,%a0@+ | move a byte; was it NUL? + dbeq %d0,Lmcloop | if not, keep transferring, + jeq Lmcdone | otherwise done + clrw %d0 | check to see if more to do + subql #1,%d0 + jcc Lmcloop | yes, keep going... + | Note: %d0 is now -1! + movql #0,%d0 | no terminator found, return NULL +Lmcbail: +#ifdef __SVR4_ABI__ + movl %d0,%a0 | XXX maybe use lea to avoid stall? +#endif + rts + +Lmcnzdone: + movl %sp@+,%d2 | restore scratch register +Lmcdone: + movl %a0,%d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/string/swab.S b/lib/nbsd_libc/arch/m68k/string/swab.S new file mode 100644 index 000000000..91fea3fee --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/string/swab.S @@ -0,0 +1,20 @@ +/* $NetBSD: swab.S,v 1.7 1999/10/25 23:48:16 thorpej Exp $ */ + +#include + +ENTRY(swab) + movl %sp@(4),%a0 | source + movl %sp@(8),%a1 | destination + movl %sp@(12),%d0 | count + lsrl #1,%d0 | count is in bytes; we need words + jeq swdone + +swloop: + movw %a0@+,%d1 + rorw #8,%d1 + movw %d1,%a1@+ + subql #1,%d0 + jne swloop + +swdone: + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/__clone.S b/lib/nbsd_libc/arch/m68k/sys/__clone.S new file mode 100644 index 000000000..d51788bb7 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/__clone.S @@ -0,0 +1,65 @@ +/* $NetBSD: __clone.S,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Steve C. Woodford. + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + movl %sp@(4),%d0 /* NULL function pointer? */ + jeq 2f /* Yup, bomb out */ + movl %d0,%a1 + movl %sp@(8),%d0 /* NULL stack? */ + jeq 2f /* Yup, bomb out */ + movl %d0,%a0 + movl %sp@(16),%a0@- /* Push clone's `arg' on its new stack */ + lea %a0@(-12),%a0 /* Fake syscall args for the clone */ + movl %a0,%sp@- /* Syscall arg: stack */ + movl %sp@(16),%sp@- /* Syscall arg: flags */ + clrl %sp@- /* Fake return address */ + SYSTRAP(__clone) /* Note: `fn' in a1@ is preserved */ + lea %sp@(12),%sp /* Zap syscall args */ + jcs 3f /* Punt if syscall failed */ + tstl %d0 + jne 1f /* We're the parent, just return. */ + jsr %a1@ /* We're the clone, call the function */ + movl %d0,%sp@- /* If clone returns, invoke _exit(3) */ + jbsr PIC_PLT(_C_LABEL(_exit)) + /* NOTREACHED */ +1: rts +2: movl #EINVAL,%d0 +3: jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/arch/m68k/sys/__mmap.S b/lib/nbsd_libc/arch/m68k/sys/__mmap.S new file mode 100644 index 000000000..0780a99a7 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/__mmap.S @@ -0,0 +1,35 @@ +/* $NetBSD: __mmap.S,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * 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 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 "SYS.h" + +_SYSCALL(__mmap,mmap) +#ifdef __SVR4_ABI__ + movl %d0,%a0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/m68k/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..256a99084 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/__sigaction14_sigtramp.c @@ -0,0 +1,82 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.9 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.9 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) +#ifdef __LIBC12_SOURCE__ +extern const int __sigtramp_sigcontext_1[] + __weak_reference(__sigtramp_sigcontext_1); +#endif + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specified or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/m68k/sys/__sigtramp2.S b/lib/nbsd_libc/arch/m68k/sys/__sigtramp2.S new file mode 100755 index 000000000..3885afec8 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/__sigtramp2.S @@ -0,0 +1,50 @@ +/* $NetBSD: __sigtramp2.S,v 1.2 2008/05/02 18:34:17 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * 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 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 "SYS.h" + +/* + * The m68k signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * ucontext structure [12+sizeof(siginfo_t)] + * siginfo structure [12] + * pointer to ucontext structure [8] + * pointer to siginfo structure [4] + * sp-> signal number [0] + */ +ENTRY_NOPROFILE(__sigtramp_siginfo_2) + movl %sp@(8),%a0 /* get pointer to ucontext */ + movl %a0,%sp@(4) /* put it in the argument slot */ + /* fake return address already there */ + movl #SYS_setcontext,%d0 + trap #0 + movl %d0,%sp@(4) /* error code */ + SYSTRAP(exit) /* exit */ diff --git a/lib/nbsd_libc/arch/m68k/sys/__syscall.S b/lib/nbsd_libc/arch/m68k/sys/__syscall.S new file mode 100644 index 000000000..8c4174e3e --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/__syscall.S @@ -0,0 +1,4 @@ +/* $NetBSD: __syscall.S,v 1.1 2000/12/13 21:46:23 scw Exp $ */ + +#include "SYS.h" +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/m68k/sys/__vfork14.S b/lib/nbsd_libc/arch/m68k/sys/__vfork14.S new file mode 100644 index 000000000..ed27d4115 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/__vfork14.S @@ -0,0 +1,107 @@ +/* $NetBSD: __vfork14.S,v 1.9 2003/08/07 16:42:14 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)Ovfork.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: __vfork14.S,v 1.9 2003/08/07 16:42:14 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * @(#)vfork.s 4.1 (Berkeley) 12/21/80 + * C library -- vfork + */ + +/* + * pid = vfork(); + * + * d1 == 0 in parent process, d1 == 1 in child process. + * d0 == pid of child in parent, d0 == pid of parent in child. + * + * trickery here, due to keith sklower, uses ret to clear the stack, + * and then returns with a jump indirect, since only one person can return + * with a ret off this stack... we do the ret before we vfork! + */ + +ENTRY(__vfork14) + movl %sp@+,%a1 + movl #SYS___vfork14,%d0 + trap #0 + jcs err + subql #1,%d1 /* from 1 to 0 in child, 0 to -1 in parent */ + andl %d1,%d0 + jmp %a1@ +err: +#ifdef _REENTRANT + .globl _C_LABEL(__errno) + movl %a1,%sp@- + movl %d0,%sp@- +#if defined(PIC) && !defined(__ELF__) + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a1 + lea %pc@(0,a1:l),%a1 + movl %a1@(_C_LABEL(__errno):l),%a1 + jsr %a1@ +#else + jbsr PIC_PLT(_C_LABEL(__errno)) +#endif /* PIC */ +#ifdef __SVR4_ABI__ + movl %sp@+,%a0@ +#else + movl %d0,%a1 + movl %sp@+,%a1@ +#endif + movl %sp@+,%a1 +#else + .globl _C_LABEL(errno) +#ifdef PIC +#ifdef __ELF__ + lea %pc@(_GLOBAL_OFFSET_TABLE_@GOTPC),%a0 + movl %a0@(_C_LABEL(errno)@GOT:w),%a0 +#else + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a0 + lea %pc@(0,a0:l),%a0 + movl %a0@(_C_LABEL(errno):w),%a0 +#endif + movl %d0,%a0@ +#else + movl %d0,_C_LABEL(errno) +#endif /* PIC */ +#endif /* _REENTRANT */ + moveq #-1,%d0 + jmp %a1@ diff --git a/lib/nbsd_libc/arch/m68k/sys/_lwp_getprivate.S b/lib/nbsd_libc/arch/m68k/sys/_lwp_getprivate.S new file mode 100644 index 000000000..df907b51c --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/_lwp_getprivate.S @@ -0,0 +1,35 @@ +/* $NetBSD: _lwp_getprivate.S,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * 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 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 "SYS.h" + +SYSCALL(_lwp_getprivate) +#ifdef __SVR4_ABI__ + movl %d0,%a0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/brk.S b/lib/nbsd_libc/arch/m68k/sys/brk.S new file mode 100644 index 000000000..be026f8b5 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/brk.S @@ -0,0 +1,94 @@ +/* $NetBSD: brk.S,v 1.15 2003/12/26 11:21:48 martin Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)brk.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: brk.S,v 1.15 2003/12/26 11:21:48 martin Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + + .globl _end + .globl _C_LABEL(__minbrk) + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data +_C_LABEL(__minbrk): + .long _end + + .text + +ENTRY(_brk) +#ifdef PIC +#ifdef __ELF__ + lea %pc@(_GLOBAL_OFFSET_TABLE_@GOTPC),%a1 + movl %a1@(_C_LABEL(__minbrk)@GOT:w),%a0 +#else + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a1 + lea %pc@(0,a1:l),%a1 + movl %a1@(_C_LABEL(__minbrk):w),%a0 +#endif + movl %a0@,%d0 +#else + movl _C_LABEL(__minbrk),%d0 +#endif + cmpl %sp@(4),%d0 + jls ok + movl %d0,%sp@(4) +ok: + movl #SYS_break,%d0 + trap #0 + jcs err +#ifdef PIC +#ifdef __ELF__ + movl %a1@(CURBRK@GOT:w),%a0 +#else + movl %a1@(CURBRK:w),%a0 +#endif + movl %sp@(4),%a0@ +#else + movl %sp@(4),CURBRK +#endif + clrl %d0 + rts +err: + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/arch/m68k/sys/cerror.S b/lib/nbsd_libc/arch/m68k/sys/cerror.S new file mode 100644 index 000000000..40d2be204 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/cerror.S @@ -0,0 +1,86 @@ +/* $NetBSD: cerror.S,v 1.15 2009/10/03 22:28:33 phx Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)cerror.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: cerror.S,v 1.15 2009/10/03 22:28:33 phx Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef _REENTRANT + .globl _C_LABEL(__errno) +#else + .globl _C_LABEL(errno) +#endif +_ENTRY(CERROR) +#ifdef _REENTRANT + movl %d0,%sp@- +#if defined(PIC) && !defined(__ELF__) + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a0 + lea %pc@(0,%a0:l),%a0 + movl %a0@(_C_LABEL(__errno):l),%a0 + jsr %a0@ +#else + jbsr PIC_PLT(_C_LABEL(__errno)) +#endif +#ifndef __SVR4_ABI__ + movl %d0,%a0 +#endif + movl %sp@+,%a0@ +#else +#ifdef PIC +#ifdef __ELF__ + lea %pc@(_GLOBAL_OFFSET_TABLE_@GOTPC),%a0 + movl %a0@(_C_LABEL(errno)@GOT:w),%a0 +#else + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a0 + lea %pc@(0,a0:l),%a0 + movl %a0@(_C_LABEL(errno):w),%a0 +#endif + movl %d0,%a0@ +#else + movl %d0,_C_LABEL(errno) +#endif +#endif /* _REENTRANT */ + movl #-1,%d0 + movl #-1,%d1 +#ifdef __SVR4_ABI__ + movl %d0,%a0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/exect.S b/lib/nbsd_libc/arch/m68k/sys/exect.S new file mode 100644 index 000000000..5be1a0a17 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/exect.S @@ -0,0 +1,50 @@ +/* $NetBSD: exect.S,v 1.7 2003/08/07 16:42:14 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" +#include + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)exect.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: exect.S,v 1.7 2003/08/07 16:42:14 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(exect) + movl #SYS_execve,%d0 + trap #0 + jra PIC_PLT(CERROR) /* exect(file, argv, env) */ diff --git a/lib/nbsd_libc/arch/m68k/sys/fork.S b/lib/nbsd_libc/arch/m68k/sys/fork.S new file mode 100644 index 000000000..84a957a10 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/fork.S @@ -0,0 +1,49 @@ +/* $NetBSD: fork.S,v 1.7 2003/08/07 16:42:14 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)fork.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: fork.S,v 1.7 2003/08/07 16:42:14 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +_SYSCALL(__fork,fork) + subql #1,%d1 /* from 1 to 0 in child, 0 to -1 in parent */ + andl %d1,%d0 + rts /* pid = fork() */ diff --git a/lib/nbsd_libc/arch/m68k/sys/getcontext.S b/lib/nbsd_libc/arch/m68k/sys/getcontext.S new file mode 100644 index 000000000..d29104a50 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/getcontext.S @@ -0,0 +1,47 @@ +/* $NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:56 martin Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +_SYSCALL(_getcontext,getcontext) + movl %sp@(4),%a0 + addql #4,%a0@(96) | adjust saved stack pointer + movl %sp@,%a0@(100) | adjust saved program counter + clrl %a0@(36) | arrange for return value of 0 + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/mremap.S b/lib/nbsd_libc/arch/m68k/sys/mremap.S new file mode 100644 index 000000000..624be032f --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/mremap.S @@ -0,0 +1,35 @@ +/* $NetBSD: mremap.S,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * 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 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 "SYS.h" + +SYSCALL(mremap) +#ifdef __SVR4_ABI__ + movl %d0,%a0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/pipe.S b/lib/nbsd_libc/arch/m68k/sys/pipe.S new file mode 100644 index 000000000..860252bd1 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/pipe.S @@ -0,0 +1,55 @@ +/* $NetBSD: pipe.S,v 1.6 2003/08/07 16:42:14 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)pipe.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: pipe.S,v 1.6 2003/08/07 16:42:14 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +_SYSCALL(_pipe,pipe) + movl %sp@(4),%a0 + movl %d0,%a0@+ + movl %d1,%a0@ + clrl %d0 + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/ptrace.S b/lib/nbsd_libc/arch/m68k/sys/ptrace.S new file mode 100644 index 000000000..58c27d337 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/ptrace.S @@ -0,0 +1,86 @@ +/* $NetBSD: ptrace.S,v 1.11 2003/08/07 16:42:14 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ptrace.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: ptrace.S,v 1.11 2003/08/07 16:42:14 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef _REENTRANT + .globl _C_LABEL(__errno) +#else + .globl _C_LABEL(errno) +#endif + +ENTRY(ptrace) +#ifdef _REENTRANT +#if defined(PIC) && !defined(__ELF__) + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a0 + lea %pc@(0,%a0:l),%a0 + movl %a0@(_C_LABEL(__errno):l),%a0 + jsr %a0@ +#else + jbsr PIC_PLT(_C_LABEL(__errno)) +#endif /* PIC */ +#ifndef __SVR4_ABI__ + movl %d0,%a0 +#endif + clrl %a0@ +#else +#ifdef PIC +#ifdef __ELF__ + lea %pc@(_GLOBAL_OFFSET_TABLE_@GOTPC),%a0 + movl %a0@(_C_LABEL(errno)@GOT:w),%a0 +#else + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a0 + lea %pc@(0,%a0:l),%a0 + movl %a0@(_C_LABEL(errno):w),%a0 +#endif + clrl %a0@ +#else + clrl _C_LABEL(errno) +#endif /* PIC */ +#endif /* _REENTRANT */ + movl #SYS_ptrace,%d0 + trap #0 + jcs err + rts +err: + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/arch/m68k/sys/sbrk.S b/lib/nbsd_libc/arch/m68k/sys/sbrk.S new file mode 100644 index 000000000..2ea3333cf --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/sbrk.S @@ -0,0 +1,88 @@ +/* $NetBSD: sbrk.S,v 1.14 2003/08/07 16:42:14 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sbrk.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: sbrk.S,v 1.14 2003/08/07 16:42:14 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + + .globl _end + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +CURBRK: .long _end + + .text + +ENTRY(_sbrk) +#ifdef PIC +#ifdef __ELF__ + lea %pc@(_GLOBAL_OFFSET_TABLE_@GOTPC),%a1 + movl %a1@(CURBRK@GOT:w),%a1 +#else + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a1 + lea %pc@(0,%a1:l),%a1 + movl %a1@(CURBRK:w),%a1 +#endif + movl %a1@,%d0 +#else + movl CURBRK,%d0 +#endif + addl %d0,%sp@(4) + movl #SYS_break,%d0 + trap #0 + jcs err +#ifdef PIC + movl %a1@,%d0 + movl %sp@(4),%a1@ +#else + movl CURBRK,%d0 + movl %sp@(4),CURBRK +#endif +#ifdef __SVR4_ABI__ + movl %d0,%a0 +#endif + rts +err: + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/arch/m68k/sys/shmat.S b/lib/nbsd_libc/arch/m68k/sys/shmat.S new file mode 100644 index 000000000..3a65ab4b3 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/shmat.S @@ -0,0 +1,35 @@ +/* $NetBSD: shmat.S,v 1.2 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * 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 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 "SYS.h" + +SYSCALL(shmat) +#ifdef __SVR4_ABI__ + movl %d0,%a0 +#endif + rts diff --git a/lib/nbsd_libc/arch/m68k/sys/syscall.S b/lib/nbsd_libc/arch/m68k/sys/syscall.S new file mode 100644 index 000000000..a06ae29b2 --- /dev/null +++ b/lib/nbsd_libc/arch/m68k/sys/syscall.S @@ -0,0 +1,52 @@ +/* $NetBSD: syscall.S,v 1.7 2003/08/07 16:42:14 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)syscall.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: syscall.S,v 1.7 2003/08/07 16:42:14 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +ENTRY(syscall) + clrl %d0 + trap #0 + jcs err + rts +err: + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/arch/mips/Makefile.inc b/lib/nbsd_libc/arch/mips/Makefile.inc new file mode 100644 index 000000000..032d3d9e4 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/Makefile.inc @@ -0,0 +1,11 @@ +# $NetBSD: Makefile.inc,v 1.9 2009/12/14 01:07:41 matt Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +CPPFLAGS+= -I. +CPPFLAGS.assym.h+=-D__LIBC12_SOURCE__ + +.if ${MKSOFTFLOAT} != "no" +.include +CPPFLAGS+= -DSOFTFLOAT_NEED_FIXUNS +.endif diff --git a/lib/nbsd_libc/arch/mips/SYS.h b/lib/nbsd_libc/arch/mips/SYS.h new file mode 100644 index 000000000..2bafcc08b --- /dev/null +++ b/lib/nbsd_libc/arch/mips/SYS.h @@ -0,0 +1,141 @@ +/* $NetBSD: SYS.h,v 1.19 2009/12/14 01:07:41 matt Exp $ */ + +/*- + * Copyright (c) 1996 Jonathan Stone + * 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 by Jonathan Stone for + * the NetBSD Project. + * 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. + */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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. + * + * from: @(#)SYS.h 8.1 (Berkeley) 6/4/93 + */ + +#include +#include + + +/* + * If compiling for shared libs, Emit sysV ABI PIC segment pseudo-ops. + * + * i) Emit .abicalls before .LEAF entrypoint, and .cpload/.cprestore after. + * ii) Do interprocedure jumps indirectly via t9, with the side-effect of + * preserving the callee's entry address in t9. + */ +#ifdef __ABICALLS__ + .abicalls +# if defined(__mips_o32) || defined(__mips_o64) +# define PIC_PROLOGUE(x) SETUP_GP +# define PIC_TAILCALL(l) PTR_LA t9, _C_LABEL(l); jr t9 +# define PIC_RETURN() j ra +# else +# define PIC_PROLOGUE(x) SETUP_GP64(t3, x) +# define PIC_TAILCALL(l) PTR_LA t9, _C_LABEL(l); RESTORE_GP64; jr t9 +# define PIC_RETURN() RESTORE_GP64; j ra +# endif +#else +# define PIC_PROLOGUE(x) +# define PIC_TAILCALL(l) j _C_LABEL(l) +# define PIC_RETURN() +#endif /* __ABICALLS__ */ + + +#ifdef __STDC__ +# define SYSTRAP(x) li v0,SYS_ ## x; syscall; +#else +# define SYSTRAP(x) li v0,SYS_/**/x; syscall; +#endif + + +/* + * Do a syscall that cannot fail (sync, get{p,u,g,eu,eg)id) + */ +#define RSYSCALL_NOERROR(x) \ + PSEUDO_NOERROR(x,x) + +/* + * Do a normal syscall. + */ +#define RSYSCALL(x) \ + PSEUDO(x,x) + +/* + * Do a syscall that has an internal name and a weak external alias. + */ +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) + +/* + * Do a renamed or pseudo syscall (e.g., _exit()), where the entrypoint + * and syscall name are not the same. + */ +#define PSEUDO_NOERROR(x,y) \ +LEAF(x); \ + SYSTRAP(y); \ + j ra; \ + END(x) + +#define PSEUDO(x,y) \ +LEAF(x); \ + PIC_PROLOGUE(x); \ + SYSTRAP(y); \ + bne a3,zero,err; \ + PIC_RETURN(); \ +err: \ + PIC_TAILCALL(__cerror); \ +END(x) diff --git a/lib/nbsd_libc/arch/mips/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/mips/gdtoa/Makefile.inc new file mode 100644 index 000000000..51535fa03 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gdtoa/Makefile.inc @@ -0,0 +1,9 @@ +# $NetBSD: Makefile.inc,v 1.4 2011/01/20 18:41:39 matt Exp $ + +SRCS+= strtof.c +.if (${MACHINE_ARCH} == "mips64eb" || ${MACHINE_ARCH} == "mips64el") +.if !defined(CPUFLAGS) || empty(CPUFLAGS:M-mabi=32) +SRCS+= strtold_pQ.c +SRCS+= strtopQ.c +.endif +.endif diff --git a/lib/nbsd_libc/arch/mips/gdtoa/arith.h b/lib/nbsd_libc/arch/mips/gdtoa/arith.h new file mode 100644 index 000000000..40d3ed1fa --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gdtoa/arith.h @@ -0,0 +1,9 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#include + +#if BYTE_ORDER == BIG_ENDIAN +#define IEEE_BIG_ENDIAN +#else +#define IEEE_LITTLE_ENDIAN +#endif diff --git a/lib/nbsd_libc/arch/mips/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/mips/gdtoa/gd_qnan.h new file mode 100644 index 000000000..7fcafc099 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gdtoa/gd_qnan.h @@ -0,0 +1,20 @@ +/* $NetBSD: gd_qnan.h,v 1.2 2011/01/17 23:53:03 matt Exp $ */ + +#include + +#define f_QNAN 0x7fa00000 +#if BYTE_ORDER == BIG_ENDIAN +#define d_QNAN0 0x7ff40000 +#define d_QNAN1 0x0 +#define ld_QNAN0 0x7fff8000 +#define ld_QNAN1 0x0 +#define ld_QNAN2 0x0 +#define ld_QNAN3 0x0 +#else +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff40000 +#define ld_QNAN0 0x0 +#define ld_QNAN1 0x0 +#define ld_QNAN2 0x0 +#define ld_QNAN3 0x7fff8000 +#endif diff --git a/lib/nbsd_libc/arch/mips/gen/Makefile.inc b/lib/nbsd_libc/arch/mips/gen/Makefile.inc new file mode 100644 index 000000000..08d2e3783 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/Makefile.inc @@ -0,0 +1,38 @@ +# $NetBSD: Makefile.inc,v 1.32 2011/01/17 23:53:03 matt Exp $ + +.if ${MKSOFTFLOAT} == "no" +SRCS+= fabs.S ldexp.S modf.S +.else +SRCS+= fabs_ieee754.c ldexp_ieee754.c modf_ieee754.c +.endif + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c fpclassifyl_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c isinfl_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c isnanl_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c isfinitel_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c signbitl_ieee754.c + +SRCS+= nanf.c +SRCS+= flt_rounds.c + +.if ${MKSOFTFLOAT} == "no" +SRCS+= fpgetmask.c fpgetround.c fpgetsticky.c fpsetmask.c \ + fpsetround.c fpsetsticky.c +.endif + +SRCS+= setjmp.S longjmp.c +SRCS+= _setjmp.S +SRCS+= sigsetjmp.S +SRCS+= byte_swap_2.S byte_swap_4.S byte_swap_8.S + +SRCS+= makecontext.c resumecontext.c _resumecontext.S swapcontext.S _lwp.c + +# mips abi builtin extensions (used by GCC for lexical-closure trampoline) +SRCS+= cacheflush.c + +LSRCS.mips.gen= Lint__setjmp.c Lint_bswap16.c Lint_bswap32.c Lint_swapcontext.c +LSRCS+= ${LSRCS.mips.gen} +DPSRCS+= ${LSRCS.mips.gen} +CLEANFILES+= ${LSRCS.mips.gen} diff --git a/lib/nbsd_libc/arch/mips/gen/_lwp.c b/lib/nbsd_libc/arch/mips/gen/_lwp.c new file mode 100644 index 000000000..408240229 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/_lwp.c @@ -0,0 +1,64 @@ +/* $NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:56 martin Exp $ */ + +/* + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Wayne Knowles + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#define CALLFRAME_SIZ 24 + +void _lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + caddr_t sp; + __greg_t *gr; + + getcontext(u); + gr = u->uc_mcontext.__gregs; + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + sp = stack_base + stack_size - CALLFRAME_SIZ; + + gr[_REG_EPC] = (unsigned long) start; + gr[_REG_T9] = (unsigned long) start; /* required for .abicalls */ + gr[_REG_RA] = (unsigned long) _lwp_exit; + gr[_REG_A0] = (unsigned long) arg; + gr[_REG_SP] = (unsigned long) sp; +} diff --git a/lib/nbsd_libc/arch/mips/gen/_resumecontext.S b/lib/nbsd_libc/arch/mips/gen/_resumecontext.S new file mode 100644 index 000000000..2631070f9 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/_resumecontext.S @@ -0,0 +1,62 @@ +/* $NetBSD: _resumecontext.S,v 1.6 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Wayne Knowles. + * + * 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 "SYS.h" +#include "assym.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: _resumecontext.S,v 1.6 2009/12/14 01:07:42 matt Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + + .set reorder + .hidden _C_LABEL(__resumecontext) + +LEAF_NOPROFILE(__resumecontext) + /* + * We get here not by a call through $t9 but thru $ra after the + * function passed to makecontext returns. + */ + PTR_SUBU sp, sp, UCONTEXT_SIZE # get space for ucontext + move a0, sp # arg0 for getcontext + PTR_S zero, _OFFSETOF_UC_LINK(a0) # make sure uc_link is 0 + SYSTRAP(getcontext) # get context + PTR_L a0, _OFFSETOF_UC_LINK(a0) # linked context? + beq a0, zero, 1f # nope, exit process + nop + SYSTRAP(setcontext) # yes, become it. + /* NOTREACHED (in theory) */ + li a0, -1 # failure, +1: + SYSTRAP(exit) # all hope is lost. + /* NOTREACHED */ +END(__resumecontext) diff --git a/lib/nbsd_libc/arch/mips/gen/_setjmp.S b/lib/nbsd_libc/arch/mips/gen/_setjmp.S new file mode 100644 index 000000000..3d2f91fdf --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/_setjmp.S @@ -0,0 +1,188 @@ +/* $NetBSD: _setjmp.S,v 1.22 2010/09/03 17:22:51 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 /* XXX */ + +#include "assym.h" + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)_setjmp.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: _setjmp.S,v 1.22 2010/09/03 17:22:51 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * _setjmp(a) + * by restoring registers from the stack, + * The previous signal state is NOT restored. + */ + + .set noreorder + +LEAF(_setjmp) + REG_PROLOGUE + REG_LI v0, 0xACEDBADE # sigcontext magic number + REG_S ra, _OFFSETOF_SC_PC(a0) # sc_pc = return address + REG_S v0, _OFFSETOF_SC_REGS(a0) # saved in sc_regs[0] + REG_S s0, _OFFSETOF_SC_REGS_S0(a0) + REG_S s1, _OFFSETOF_SC_REGS_S1(a0) + REG_S s2, _OFFSETOF_SC_REGS_S2(a0) + REG_S s3, _OFFSETOF_SC_REGS_S3(a0) + REG_S s4, _OFFSETOF_SC_REGS_S4(a0) + REG_S s5, _OFFSETOF_SC_REGS_S5(a0) + REG_S s6, _OFFSETOF_SC_REGS_S6(a0) + REG_S s7, _OFFSETOF_SC_REGS_S7(a0) + REG_S sp, _OFFSETOF_SC_REGS_SP(a0) + REG_S s8, _OFFSETOF_SC_REGS_S8(a0) +#if defined(__mips_n32) || defined(__mips_n64) + REG_S gp, _OFFSETOF_SC_REGS_GP(a0) # newabi gp is callee-saved +#endif + /* + * In N32, FP registers F20, F22, F24, F26, F28, F30 are callee-saved. + * In N64, FP registers F23 .. F31 are callee-saved. + * In O32, FP registers F20 .. F23 are callee-saved. + */ +#ifndef SOFTFLOAT_FOR_GCC + cfc1 v0, $31 # too bad cant check if FP used + INT_S v0, _OFFSETOF_SC_FPREGS_FCSR(a0) +#if defined(__mips_n64) || defined(__mips_n32) + FP_S $f30, _OFFSETOF_SC_FPREGS_F30(a0) + FP_S $f28, _OFFSETOF_SC_FPREGS_F28(a0) + FP_S $f26, _OFFSETOF_SC_FPREGS_F26(a0) + FP_S $f24, _OFFSETOF_SC_FPREGS_F24(a0) +#endif +#if defined(__mips_n32) || defined(__mips_o32) || defined(__mips_o64) + FP_S $f22, _OFFSETOF_SC_FPREGS_F22(a0) + FP_S $f20, _OFFSETOF_SC_FPREGS_F20(a0) +#endif +#if defined(__mips_o32) || defined(__mips_o64) + FP_S $f21, _OFFSETOF_SC_FPREGS_F21(a0) + FP_S $f23, _OFFSETOF_SC_FPREGS_F23(a0) +#endif +#if defined(__mips_n64) + FP_S $f25, _OFFSETOF_SC_FPREGS_F25(a0) + FP_S $f27, _OFFSETOF_SC_FPREGS_F27(a0) + FP_S $f29, _OFFSETOF_SC_FPREGS_F29(a0) + FP_S $f31, _OFFSETOF_SC_FPREGS_F31(a0) +#endif +#endif /* SOFTFLOAT_FOR_GCC */ + REG_EPILOGUE + + j ra + move v0, zero +END(_setjmp) + +LEAF(_longjmp) + PIC_PROLOGUE(_longjmp) + PTR_SUBU sp, sp, CALLFRAME_SIZ + SAVE_GP(CALLFRAME_GP) + + REG_PROLOGUE + REG_L v0, _OFFSETOF_SC_REGS(a0) # get magic number + REG_L ra, _OFFSETOF_SC_PC(a0) + REG_LI t0, 0xACEDBADE + bne v0, t0, botch # jump if error + PTR_ADDU sp, sp, CALLFRAME_SIZ # does not matter, sanity + REG_S a1, _OFFSETOF_SC_REGS_V0(a0) # save return value + REG_L s0, _OFFSETOF_SC_REGS_S0(a0) + REG_L s1, _OFFSETOF_SC_REGS_S1(a0) + REG_L s2, _OFFSETOF_SC_REGS_S2(a0) + REG_L s3, _OFFSETOF_SC_REGS_S3(a0) + REG_L s4, _OFFSETOF_SC_REGS_S4(a0) + REG_L s5, _OFFSETOF_SC_REGS_S5(a0) + REG_L s6, _OFFSETOF_SC_REGS_S6(a0) + REG_L s7, _OFFSETOF_SC_REGS_S7(a0) +#if defined(__mips_n32) || defined(__mips_n64) + REG_L gp, _OFFSETOF_SC_REGS_GP(a0) +#endif + # get fpu status + REG_L sp, _OFFSETOF_SC_REGS_SP(a0) + REG_L s8, _OFFSETOF_SC_REGS_S8(a0) +#ifndef SOFTFLOAT_FOR_GCC + INT_L v0, _OFFSETOF_SC_FPREGS_FCSR(a0) + ctc1 v0, $31 + /* + * In N32, FP registers F20, F22, F24, F26, F28, F30 are callee-saved. + * In N64, FP registers F23 .. F31 are callee-saved. + * In O32, FP registers F20 .. F23 are callee-saved. + */ +#if defined(__mips_n64) || defined(__mips_n32) + FP_L $f30, _OFFSETOF_SC_FPREGS_F30(a0) + FP_L $f28, _OFFSETOF_SC_FPREGS_F28(a0) + FP_L $f26, _OFFSETOF_SC_FPREGS_F26(a0) + FP_L $f24, _OFFSETOF_SC_FPREGS_F24(a0) +#endif +#if defined(__mips_n32) || defined(__mips_o32) || defined(__mips_o64) + FP_L $f22, _OFFSETOF_SC_FPREGS_F22(a0) + FP_L $f20, _OFFSETOF_SC_FPREGS_F20(a0) +#endif +#if defined(__mips_o32) || defined(__mips_o64) + FP_L $f21, _OFFSETOF_SC_FPREGS_F21(a0) + FP_L $f23, _OFFSETOF_SC_FPREGS_F23(a0) +#endif +#if defined(__mips_n64) + FP_L $f25, _OFFSETOF_SC_FPREGS_F25(a0) + FP_L $f27, _OFFSETOF_SC_FPREGS_F27(a0) + FP_L $f29, _OFFSETOF_SC_FPREGS_F29(a0) + FP_L $f31, _OFFSETOF_SC_FPREGS_F31(a0) +#endif +#endif /* SOFTFLOAT_FOR_GCC */ + + REG_EPILOGUE + move v0, a1 # get return value in 1st arg + j ra + nop + +botch: + /* + * We know we aren't returning so we don't care about restoring + * our caller's GP. + */ + jal _C_LABEL(longjmperror) + + PIC_TAILCALL(abort) +END(_longjmp) diff --git a/lib/nbsd_libc/arch/mips/gen/cacheflush.c b/lib/nbsd_libc/arch/mips/gen/cacheflush.c new file mode 100644 index 000000000..45a9aefbe --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/cacheflush.c @@ -0,0 +1,47 @@ +/* $NetBSD: cacheflush.c,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jonathan Stone. + * + * 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 + +int +_cacheflush(addr, nbytes, whichcache) + void * addr; + int nbytes, whichcache; +{ + struct mips_cacheflush_args cfa; + + cfa.va = (vaddr_t) addr; + cfa.nbytes = nbytes; + cfa.whichcache = whichcache; + return (sysarch(MIPS_CACHEFLUSH, (void *)&cfa)); +} diff --git a/lib/nbsd_libc/arch/mips/gen/fabs.S b/lib/nbsd_libc/arch/mips/gen/fabs.S new file mode 100644 index 000000000..812b70e54 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/fabs.S @@ -0,0 +1,57 @@ +/* $NetBSD: fabs.S,v 1.8 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)fabs.s 8.1 (Berkeley) 2/16/94") +#else + RCSID("$NetBSD: fabs.S,v 1.8 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + + + .set noreorder + +/* + * fabs(x) + * double x; + * + * Return absolute value of x. + */ +LEAF(fabs) + j ra + abs.d $f0, $f12 # compute absolute value of x +END(fabs) diff --git a/lib/nbsd_libc/arch/mips/gen/flt_rounds.c b/lib/nbsd_libc/arch/mips/gen/flt_rounds.c new file mode 100644 index 000000000..ab8a5cdcc --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/flt_rounds.c @@ -0,0 +1,34 @@ +/* $NetBSD: flt_rounds.c,v 1.6 2010/09/03 17:22:51 matt Exp $ */ + +/* + * Written by J.T. Conklin, Apr 11, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.6 2010/09/03 17:22:51 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 0, /* round to zero */ + 2, /* round to positive infinity */ + 3 /* round to negative infinity */ +}; + +int +__flt_rounds() +{ +#ifdef SOFTFLOAT_FOR_GCC + return map[fpgetround()]; +#else + int x; + + __asm("cfc1\t%0,$31" : "=r" (x)); + return map[x & 0x03]; +#endif +} diff --git a/lib/nbsd_libc/arch/mips/gen/fpgetmask.c b/lib/nbsd_libc/arch/mips/gen/fpgetmask.c new file mode 100644 index 000000000..1e62810da --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/fpgetmask.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 11, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetmask,_fpgetmask) +#endif + +fp_except +fpgetmask() +{ + int x; + + __asm("cfc1 %0,$31" : "=r" (x)); + return (x >> 7) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/mips/gen/fpgetround.c b/lib/nbsd_libc/arch/mips/gen/fpgetround.c new file mode 100644 index 000000000..e05bdc82f --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/fpgetround.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 11, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetround,_fpgetround) +#endif + +fp_rnd +fpgetround() +{ + int x; + + __asm("cfc1 %0,$31" : "=r" (x)); + return x & 0x03; +} diff --git a/lib/nbsd_libc/arch/mips/gen/fpgetsticky.c b/lib/nbsd_libc/arch/mips/gen/fpgetsticky.c new file mode 100644 index 000000000..d72d234a6 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/fpgetsticky.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 11, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetsticky,_fpgetsticky) +#endif + +fp_except +fpgetsticky() +{ + int x; + + __asm("cfc1 %0,$31" : "=r" (x)); + return (x >> 2) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/mips/gen/fpsetmask.c b/lib/nbsd_libc/arch/mips/gen/fpsetmask.c new file mode 100644 index 000000000..f3710abc7 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/fpsetmask.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 11, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetmask,_fpsetmask) +#endif + +fp_except +fpsetmask(mask) + fp_except mask; +{ + fp_except old; + fp_except new; + + __asm("cfc1 %0,$31" : "=r" (old)); + + new = old; + new &= ~(0x1f << 7); + new |= ((mask & 0x1f) << 7); + + __asm("ctc1 %0,$31" : : "r" (new)); + + return (old >> 7) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/mips/gen/fpsetround.c b/lib/nbsd_libc/arch/mips/gen/fpsetround.c new file mode 100644 index 000000000..7741b8401 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/fpsetround.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 11, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetround,_fpsetround) +#endif + +fp_rnd +fpsetround(rnd_dir) + fp_rnd rnd_dir; +{ + fp_rnd old; + fp_rnd new; + + __asm("cfc1 %0,$31" : "=r" (old)); + + new = old; + new &= ~0x03; + new |= (rnd_dir & 0x03); + + __asm("ctc1 %0,$31" : : "r" (new)); + + return old & 0x03; +} diff --git a/lib/nbsd_libc/arch/mips/gen/fpsetsticky.c b/lib/nbsd_libc/arch/mips/gen/fpsetsticky.c new file mode 100644 index 000000000..c86f59511 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/fpsetsticky.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 11, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetsticky,_fpsetsticky) +#endif + +fp_except +fpsetsticky(sticky) + fp_except sticky; +{ + fp_except old; + fp_except new; + + __asm("cfc1 %0,$31" : "=r" (old)); + + new = old; + new &= ~(0x1f << 2); + new |= ((sticky & 0x1f) << 2); + + __asm("ctc1 %0,$31" : : "r" (new)); + + return (old >> 2) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/mips/gen/ldexp.S b/lib/nbsd_libc/arch/mips/gen/ldexp.S new file mode 100644 index 000000000..2c8fca3cd --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/ldexp.S @@ -0,0 +1,217 @@ +/* $NetBSD: ldexp.S,v 1.9 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ldexp.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: ldexp.S,v 1.9 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#define DEXP_INF 0x7ff +#define DEXP_BIAS 1023 +#define DEXP_MIN -1022 +#define DEXP_MAX 1023 +#define DFRAC_BITS 52 +#define DIMPL_ONE 0x00100000 +#define DLEAD_ZEROS 31 - 20 +#define STICKYBIT 1 +#define GUARDBIT 0x80000000 +#define DSIGNAL_NAN 0x00040000 +#define DQUIET_NAN0 0x0007ffff +#define DQUIET_NAN1 0xffffffff + +/* + * double ldexp(x, N) + * double x; int N; + * + * Return x * (2**N), for integer values N. + */ +LEAF(ldexp) + mfc1 v1, $f13 # get MSW of x + mfc1 t3, $f12 # get LSW of x + sll t1, v1, 1 # get x exponent + srl t1, t1, 32 - 11 + beq t1, DEXP_INF, 9f # is it a NAN or infinity? + beq t1, zero, 1f # zero or denormalized number? + addu t1, t1, a2 # scale exponent + sll v0, a2, 20 # position N for addition + bge t1, DEXP_INF, 8f # overflow? + addu v0, v0, v1 # multiply by (2**N) + ble t1, zero, 4f # underflow? + mtc1 v0, $f1 # save MSW of result + mtc1 t3, $f0 # save LSW of result + j ra +1: + sll t2, v1, 32 - 20 # get x fraction + srl t2, t2, 32 - 20 + srl t0, v1, 31 # get x sign + bne t2, zero, 1f + beq t3, zero, 9f # result is zero +1: +/* + * Find out how many leading zero bits are in t2,t3 and put in t9. + */ + move v0, t2 + move t9, zero + bne t2, zero, 1f + move v0, t3 + addu t9, 32 +1: + srl ta0, v0, 16 + bne ta0, zero, 1f + addu t9, 16 + sll v0, 16 +1: + srl ta0, v0, 24 + bne ta0, zero, 1f + addu t9, 8 + sll v0, 8 +1: + srl ta0, v0, 28 + bne ta0, zero, 1f + addu t9, 4 + sll v0, 4 +1: + srl ta0, v0, 30 + bne ta0, zero, 1f + addu t9, 2 + sll v0, 2 +1: + srl ta0, v0, 31 + bne ta0, zero, 1f + addu t9, 1 +/* + * Now shift t2,t3 the correct number of bits. + */ +1: + subu t9, t9, DLEAD_ZEROS # dont count normal leading zeros + li t1, DEXP_MIN + DEXP_BIAS + subu t1, t1, t9 # adjust exponent + addu t1, t1, a2 # scale exponent + li v0, 32 + blt t9, v0, 1f + subu t9, t9, v0 # shift fraction left >= 32 bits + sll t2, t3, t9 + move t3, zero + b 2f +1: + subu v0, v0, t9 # shift fraction left < 32 bits + sll t2, t2, t9 + srl ta0, t3, v0 + or t2, t2, ta0 + sll t3, t3, t9 +2: + bge t1, DEXP_INF, 8f # overflow? + ble t1, zero, 4f # underflow? + sll t2, t2, 32 - 20 # clear implied one bit + srl t2, t2, 32 - 20 +3: + sll t1, t1, 31 - 11 # reposition exponent + sll t0, t0, 31 # reposition sign + or t0, t0, t1 # put result back together + or t0, t0, t2 + mtc1 t0, $f1 # save MSW of result + mtc1 t3, $f0 # save LSW of result + j ra +4: + li v0, 0x80000000 + ble t1, -52, 7f # is result too small for denorm? + sll t2, v1, 31 - 20 # clear exponent, extract fraction + or t2, t2, v0 # set implied one bit + blt t1, -30, 2f # will all bits in t3 be shifted out? + srl t2, t2, 31 - 20 # shift fraction back to normal position + subu t1, t1, 1 + sll ta0, t2, t1 # shift right t2,t3 based on exponent + srl t8, t3, t1 # save bits shifted out + negu t1 + srl t3, t3, t1 + or t3, t3, ta0 + srl t2, t2, t1 + bge t8, zero, 1f # does result need to be rounded? + addu t3, t3, 1 # round result + sltu ta0, t3, 1 + sll t8, t8, 1 + addu t2, t2, ta0 + bne t8, zero, 1f # round result to nearest + and t3, t3, ~1 +1: + mtc1 t3, $f0 # save denormalized result (LSW) + mtc1 t2, $f1 # save denormalized result (MSW) + bge v1, zero, 1f # should result be negative? + neg.d $f0, $f0 # negate result +1: + j ra +2: + mtc1 zero, $f1 # exponent and upper fraction + addu t1, t1, 20 # compute amount to shift right by + sll t8, t2, t1 # save bits shifted out + negu t1 + srl t3, t2, t1 + bge t8, zero, 1f # does result need to be rounded? + addu t3, t3, 1 # round result + sltu ta0, t3, 1 + sll t8, t8, 1 + mtc1 ta0, $f1 # exponent and upper fraction + bne t8, zero, 1f # round result to nearest + and t3, t3, ~1 +1: + mtc1 t3, $f0 + bge v1, zero, 1f # is result negative? + neg.d $f0, $f0 # negate result +1: + j ra +7: + mtc1 zero, $f0 # result is zero + mtc1 zero, $f1 + beq t0, zero, 1f # is result positive? + neg.d $f0, $f0 # negate result +1: + j ra +8: + li t1, 0x7ff00000 # result is infinity (MSW) + mtc1 t1, $f1 + mtc1 zero, $f0 # result is infinity (LSW) + bge v1, zero, 1f # should result be negative infinity? + neg.d $f0, $f0 # result is negative infinity +1: + add.d $f0, $f0 # cause overflow faults if enabled + j ra +9: + mov.d $f0, $f12 # yes, result is just x + j ra +END(ldexp) diff --git a/lib/nbsd_libc/arch/mips/gen/longjmp.c b/lib/nbsd_libc/arch/mips/gen/longjmp.c new file mode 100644 index 000000000..8a7072137 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/longjmp.c @@ -0,0 +1,108 @@ +/* $NetBSD: longjmp.c,v 1.4 2010/09/03 17:22:51 matt Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christian Limpach and Matt Thomas. + * + * 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 "namespace.h" +#include +#include +#include +#include +#include + +#include + +#define __LIBC12_SOURCE__ +#include +#include + +void +__longjmp14(jmp_buf env, int val) +{ + struct sigcontext *sc = (void *)env; + ucontext_t uc; + + /* Ensure non-zero SP and sigcontext magic number is present */ + if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != (mips_reg_t)0xACEDBADE) + goto err; + + /* Ensure non-zero return value */ + if (val == 0) + val = 1; + + /* + * Set _UC_{SET,CLR}STACK according to SS_ONSTACK. + * + * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK, + * since libpthread may want to interpose on signal handling. + */ + uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK); + + sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL); + + /* Clear uc_link */ + uc.uc_link = 0; + + /* Save return value in context */ + uc.uc_mcontext.__gregs[_REG_V0] = val; + + /* Copy saved registers */ + uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0]; + uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1]; + uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2]; + uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3]; + uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4]; + uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5]; + uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6]; + uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7]; + uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8]; +#if defined(__mips_n32) || defined(__mips_n64) + uc.uc_mcontext.__gregs[_REG_GP] = sc->sc_regs[_R_GP]; +#endif + uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP]; + uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA]; + uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc; + + /* Copy FP state */ + if (sc->sc_fpused) { + /* FP saved regs are $f20 .. $f31 */ + memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20], + &sc->sc_fpregs[20], 32 - 20); + uc.uc_mcontext.__fpregs.__fp_csr = + sc->sc_fpregs[_R_FSR - _FPBASE]; + /* XXX sc_fp_control */ + uc.uc_flags |= _UC_FPU; + } + + setcontext(&uc); + err: + longjmperror(); + abort(); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/mips/gen/makecontext.c b/lib/nbsd_libc/arch/mips/gen/makecontext.c new file mode 100644 index 000000000..7aa42984f --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/makecontext.c @@ -0,0 +1,95 @@ +/* $NetBSD: makecontext.c,v 1.5 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.5 2009/12/14 01:07:42 matt Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + __greg_t *sp; + int i; + va_list ap; + + void __resumecontext(void); + + /* LINTED uintptr_t is safe */ + sp = (__greg_t *) + ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + /* LINTED uintptr_t is safe */ +#if defined(__mips_o32) || defined(__mips_o64) + sp -= (argc >= 4 ? argc : 4); /* Make room for >=4 arguments. */ + sp = (__greg_t *) + ((uintptr_t)sp & ~0x7); /* Align on double-word boundary. */ +#elif defined(__mips_n32) || defined(__mips_n64) + sp -= (argc > 8 ? argc - 8 : 0); /* Make room for > 8 arguments. */ + sp = (__greg_t *) + ((uintptr_t)sp & ~0xf); /* Align on quad-word boundary. */ +#endif + + gr[_REG_SP] = (intptr_t)sp; + gr[_REG_RA] = (intptr_t)__resumecontext; + gr[_REG_T9] = (intptr_t)func; /* required for .abicalls */ + gr[_REG_EPC] = (intptr_t)func; + + /* Construct argument list. */ + va_start(ap, argc); +#if defined(__mips_o32) || defined(__mips_o64) + /* Up to the first four arguments are passed in $a0-3. */ + for (i = 0; i < argc && i < 4; i++) + /* LINTED __greg_t is safe */ + gr[_REG_A0 + i] = va_arg(ap, __greg_t); + /* Pass remaining arguments on the stack above the $a0-3 gap. */ + sp += i; +#endif +#if defined(__mips_n32) || defined(__mips_n64) + /* Up to the first 8 arguments are passed in $a0-7. */ + for (i = 0; i < argc && i < 8; i++) + /* LINTED __greg_t is safe */ + gr[_REG_A0 + i] = va_arg(ap, __greg_t); + /* Pass remaining arguments on the stack above the $a0-3 gap. */ +#endif + /* Pass remaining arguments on the stack above the $a0-3 gap. */ + for (; i < argc; i++) + /* LINTED uintptr_t is safe */ + *sp++ = va_arg(ap, __greg_t); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/mips/gen/modf.S b/lib/nbsd_libc/arch/mips/gen/modf.S new file mode 100644 index 000000000..7e523af27 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/modf.S @@ -0,0 +1,75 @@ +/* $NetBSD: modf.S,v 1.11 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1995 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)modf.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: modf.S,v 1.11 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * double modf(val, iptr) + * double val, *iptr; + * returns: xxx and n (in *iptr) where val == n.xxx + */ +LEAF(modf) + cfc1 t0, $31 # get the control register + li.d $f2, 4503599627370496e0 # f2 <- 2^52 + + or t1, t0, 0x3 # set rounding mode to round to zero + xor t1, t1, 0x2 # (i.e., 01) + ctc1 t1, $31 + + mov.d $f0, $f12 # f0 <- f12 + abs.d $f4, $f12 # f4 <- |f12| + c.olt.d $f4, $f2 # f4 ? < f2 + bc1f 1f # leave f0 alone if Nan, infinity + # or >=2^52 + c.eq.d $f12,$f4 # was f12 positive ? + add.d $f4,$f2,$f4 # round off to integer + bc1f 2f # No -> will have to negate result + sub.d $f0,$f4,$f2 # Remove fudge factor + j 1f # integer fraction got +2: + sub.d $f0,$f2,$f4 # Remove fudge factor and negate +1: + ctc1 t0, $31 # restore old rounding mode + s.d $f0, 0(a2) # save the integer part + sub.d $f0, $f12, $f0 # subtract val - integer part + j ra +END(modf) diff --git a/lib/nbsd_libc/arch/mips/gen/nanf.c b/lib/nbsd_libc/arch/mips/gen/nanf.c new file mode 100644 index 000000000..6fbda2885 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/nanf.c @@ -0,0 +1,19 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = +#if BYTE_ORDER == BIG_ENDIAN + { { 0x7f, 0xa0, 0, 0 } }; +#else + { { 0, 0, 0xa0, 0x7f } }; +#endif + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/mips/gen/resumecontext.c b/lib/nbsd_libc/arch/mips/gen/resumecontext.c new file mode 100644 index 000000000..08362ec2e --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:56 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/mips/gen/setjmp.S b/lib/nbsd_libc/arch/mips/gen/setjmp.S new file mode 100644 index 000000000..c75325bc6 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/setjmp.S @@ -0,0 +1,148 @@ +/* $NetBSD: setjmp.S,v 1.19 2010/09/03 17:22:51 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "assym.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)setjmp.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: setjmp.S,v 1.19 2010/09/03 17:22:51 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +#define SETJMP_FRAME_SIZE (CALLFRAME_SIZ + STACK_T_SIZE) + +NON_LEAF(__setjmp14, SETJMP_FRAME_SIZE, ra) + .mask 0x80010000, (CALLFRAME_RA - CALLFRAME_SIZ) + SETUP_GP + PTR_SUBU sp, sp, SETJMP_FRAME_SIZE # allocate stack frame + SAVE_GP(CALLFRAME_GP) + SETUP_GP64(CALLFRAME_GP, __setjmp14) + + REG_S ra, CALLFRAME_RA(sp) # save RA + REG_S s0, CALLFRAME_S0(sp) # save S0 + move s0, a0 # save sigcontext + + /* Get the signal mask. */ + PTR_ADDU a2, a0, _OFFSETOF_SC_MASK # &sc.sc_mask + move a1, zero + jal _C_LABEL(__sigprocmask14) # get current signal mask + + /* Get the signal stack. */ + move a0, zero + PTR_ADDU a1, sp, CALLFRAME_SIZ # pointer to stack_t + jal _C_LABEL(__sigaltstack14) + + move a0, s0 # restore jmpbuf + INT_L v1, CALLFRAME_SIZ+_OFFSETOF_STACK_T_FLAGS(sp) + # get old ss_onstack + and v1, v1, SS_ONSTACK # extract onstack flag + INT_S v1, _OFFSETOF_SC_ONSTACK(a0) # save it in sc_onstack + + REG_L s0, CALLFRAME_S0(sp) # restore S0 + REG_L ra, CALLFRAME_RA(sp) # restore RA + blt v0, zero, botch # check for sigaltstack() error + nop + /* + * We know we won't need this routine's GP anymore. + */ + RESTORE_GP64 + PTR_ADDU sp, sp, SETJMP_FRAME_SIZE # pop stack frame + + + REG_PROLOGUE + REG_S ra, _OFFSETOF_SC_PC(a0) # sc_pc = return address + REG_LI v0, 0xACEDBADE # sigcontext magic number + REG_S v0, _OFFSETOF_SC_REGS(a0) # saved in sc_regs[0] + REG_S s0, _OFFSETOF_SC_REGS_S0(a0) + REG_S s1, _OFFSETOF_SC_REGS_S1(a0) + REG_S s2, _OFFSETOF_SC_REGS_S2(a0) + REG_S s3, _OFFSETOF_SC_REGS_S3(a0) + REG_S s4, _OFFSETOF_SC_REGS_S4(a0) + REG_S s5, _OFFSETOF_SC_REGS_S5(a0) + REG_S s6, _OFFSETOF_SC_REGS_S6(a0) + REG_S s7, _OFFSETOF_SC_REGS_S7(a0) + REG_S gp, _OFFSETOF_SC_REGS_GP(a0) + REG_S sp, _OFFSETOF_SC_REGS_SP(a0) + REG_S s8, _OFFSETOF_SC_REGS_S8(a0) +#ifdef SOFTFLOAT_FOR_GCC + INT_S zero, _OFFSETOF_SC_FPUSED(a0) # sc_fpused = 0 +#else + li v0, 1 # be nice if we could tell + INT_S v0, _OFFSETOF_SC_FPUSED(a0) # sc_fpused = 1 + cfc1 v0, $31 + INT_S v0, _OFFSETOF_SC_FPREGS_FCSR(a0) +#if defined(__mips_o32) || defined(__mips_o64) || defined(__mips_n32) + FP_S $f20, _OFFSETOF_SC_FPREGS_F20(a0) + FP_S $f22, _OFFSETOF_SC_FPREGS_F22(a0) +#endif +#if defined(__mips_o32) || defined(__mips_o64) + FP_S $f21, _OFFSETOF_SC_FPREGS_F21(a0) + FP_S $f23, _OFFSETOF_SC_FPREGS_F23(a0) +#endif +#if defined(__mips_n32) || defined(__mips_n64) + FP_S $f24, _OFFSETOF_SC_FPREGS_F24(a0) + FP_S $f26, _OFFSETOF_SC_FPREGS_F26(a0) + FP_S $f28, _OFFSETOF_SC_FPREGS_F28(a0) + FP_S $f30, _OFFSETOF_SC_FPREGS_F30(a0) +#endif +#if defined(__mips_n64) + FP_S $f25, _OFFSETOF_SC_FPREGS_F25(a0) + FP_S $f27, _OFFSETOF_SC_FPREGS_F27(a0) + FP_S $f29, _OFFSETOF_SC_FPREGS_F29(a0) + FP_S $f31, _OFFSETOF_SC_FPREGS_F31(a0) +#endif +#endif /* SOFTFLOAT_FOR_GCC */ + REG_EPILOGUE + move v0, zero + j ra + +botch: + jal _C_LABEL(abort) +END(__setjmp14) diff --git a/lib/nbsd_libc/arch/mips/gen/sigsetjmp.S b/lib/nbsd_libc/arch/mips/gen/sigsetjmp.S new file mode 100644 index 000000000..5cbe82d80 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/sigsetjmp.S @@ -0,0 +1,79 @@ +/* $NetBSD: sigsetjmp.S,v 1.9 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1995, + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Havard Eidnes. + * + * 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 "assym.h" +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)setjmp.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: sigsetjmp.S,v 1.9 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a, savemask) + * by restoring registers from the stack, + * and dependent on savemask restores the + * signal mask. + */ + +LEAF(__sigsetjmp14) + PIC_PROLOGUE(__sigsetjmp14) + REG_S a1, _JBLEN*SZREG(a0) # save "savemask" + bne a1, 0x0, 1f # do saving of signal mask? + PIC_TAILCALL(_setjmp) + +1: PIC_TAILCALL(__setjmp14) +END(__sigsetjmp14) + +LEAF(__siglongjmp14) + PIC_PROLOGUE(__siglongjmp14) + REG_L t0, _JBLEN*SZREG(a0) # get "savemask" + bne t0, 0x0, 1f # restore signal mask? + PIC_TAILCALL(_longjmp) + +1: PIC_TAILCALL(__longjmp14) +END(__siglongjmp14) diff --git a/lib/nbsd_libc/arch/mips/gen/swapcontext.S b/lib/nbsd_libc/arch/mips/gen/swapcontext.S new file mode 100644 index 000000000..9f19534e7 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/gen/swapcontext.S @@ -0,0 +1,67 @@ +/* $NetBSD: swapcontext.S,v 1.4 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" +#include "assym.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.4 2009/12/14 01:07:42 matt Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + + .set reorder + +NESTED(swapcontext, CALLFRAME_SIZ, ra) + .mask 0x80000030, (CALLFRAME_RA - CALLFRAME_SIZ) + PTR_SUBU sp, sp, CALLFRAME_SIZ + PTR_S ra, CALLFRAME_RA(sp) # save ra + PTR_S a0, 0(sp) # stash away oucp + PTR_S a1, SZREG(sp) # stash away ucp + SYSTRAP(getcontext) + PTR_L ra, CALLFRAME_RA(sp) + bne a3, zero, 1f + + PTR_L v1, 0(sp) # load oucp again for adjustment + PTR_L a0, SZREG(sp) # load ucp + PTR_ADDIU v0, sp, CALLFRAME_SIZ + REG_PROLOGUE + REG_S zero, _OFFSETOF_UC_GREGS_V0(v1) + REG_S ra, _OFFSETOF_UC_GREGS_EPC(v1) + REG_S v0, _OFFSETOF_UC_GREGS_SP(v1) + REG_EPILOGUE + + SYSTRAP(setcontext) + /* NOTREACHED */ +1: + PTR_ADDU sp, sp, CALLFRAME_SIZ + SETUP_GPX(t0); + SETUP_GP64(t3,swapcontext); + PIC_TAILCALL(__cerror) +END(swapcontext) diff --git a/lib/nbsd_libc/arch/mips/genassym.cf b/lib/nbsd_libc/arch/mips/genassym.cf new file mode 100644 index 000000000..512d3f953 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/genassym.cf @@ -0,0 +1,86 @@ +# $NetBSD: genassym.cf,v 1.2 2009/12/14 01:07:41 matt Exp $ + +# +# Copyright (c) 2001 The NetBSD Foundation, Inc. +# All rights reserved. +# +# This code is derived from software contributed to The NetBSD Foundation +# by Matt Thomas . +# +# 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 +include + +define _OFFSETOF_UC_GREGS offsetof(ucontext_t, uc_mcontext.__gregs[0]) +define _OFFSETOF_UC_GREGS_V0 offsetof(ucontext_t, uc_mcontext.__gregs[_REG_V0]) +define _OFFSETOF_UC_GREGS_SP offsetof(ucontext_t, uc_mcontext.__gregs[_REG_SP]) +define _OFFSETOF_UC_GREGS_EPC offsetof(ucontext_t, uc_mcontext.__gregs[_REG_EPC]) +define _OFFSETOF_UC_LINK offsetof(ucontext_t, uc_link) +define UCONTEXT_SIZE sizeof(ucontext_t) + +define _OFFSETOF_SC_REGS offsetof(struct sigcontext, sc_regs[0]) +define _OFFSETOF_SC_REGS_V0 offsetof(struct sigcontext, sc_regs[_R_V0]) +define _OFFSETOF_SC_REGS_S0 offsetof(struct sigcontext, sc_regs[_R_S0]) +define _OFFSETOF_SC_REGS_S1 offsetof(struct sigcontext, sc_regs[_R_S1]) +define _OFFSETOF_SC_REGS_S2 offsetof(struct sigcontext, sc_regs[_R_S2]) +define _OFFSETOF_SC_REGS_S3 offsetof(struct sigcontext, sc_regs[_R_S3]) +define _OFFSETOF_SC_REGS_S4 offsetof(struct sigcontext, sc_regs[_R_S4]) +define _OFFSETOF_SC_REGS_S5 offsetof(struct sigcontext, sc_regs[_R_S5]) +define _OFFSETOF_SC_REGS_S6 offsetof(struct sigcontext, sc_regs[_R_S6]) +define _OFFSETOF_SC_REGS_S7 offsetof(struct sigcontext, sc_regs[_R_S7]) +define _OFFSETOF_SC_REGS_S8 offsetof(struct sigcontext, sc_regs[_R_S8]) +define _OFFSETOF_SC_REGS_SP offsetof(struct sigcontext, sc_regs[_R_SP]) +define _OFFSETOF_SC_REGS_GP offsetof(struct sigcontext, sc_regs[_R_GP]) +define _OFFSETOF_SC_FPREGS offsetof(struct sigcontext, sc_fpregs[0]) +define _OFFSETOF_SC_FPREGS_F20 offsetof(struct sigcontext, sc_fpregs[20]) +define _OFFSETOF_SC_FPREGS_F21 offsetof(struct sigcontext, sc_fpregs[21]) +define _OFFSETOF_SC_FPREGS_F22 offsetof(struct sigcontext, sc_fpregs[22]) +define _OFFSETOF_SC_FPREGS_F23 offsetof(struct sigcontext, sc_fpregs[23]) +define _OFFSETOF_SC_FPREGS_F24 offsetof(struct sigcontext, sc_fpregs[24]) +define _OFFSETOF_SC_FPREGS_F25 offsetof(struct sigcontext, sc_fpregs[25]) +define _OFFSETOF_SC_FPREGS_F26 offsetof(struct sigcontext, sc_fpregs[26]) +define _OFFSETOF_SC_FPREGS_F27 offsetof(struct sigcontext, sc_fpregs[27]) +define _OFFSETOF_SC_FPREGS_F28 offsetof(struct sigcontext, sc_fpregs[28]) +define _OFFSETOF_SC_FPREGS_F29 offsetof(struct sigcontext, sc_fpregs[29]) +define _OFFSETOF_SC_FPREGS_F30 offsetof(struct sigcontext, sc_fpregs[30]) +define _OFFSETOF_SC_FPREGS_F31 offsetof(struct sigcontext, sc_fpregs[31]) +define _OFFSETOF_SC_FPREGS_FCSR offsetof(struct sigcontext, sc_fpregs[32]) +define _OFFSETOF_SC_PC offsetof(struct sigcontext, sc_pc) +define _OFFSETOF_SC_MASK offsetof(struct sigcontext, sc_mask) +define _OFFSETOF_SC_MASK13 offsetof(struct sigcontext, __sc_mask13) +define _OFFSETOF_SC_ONSTACK offsetof(struct sigcontext, sc_onstack) +define _OFFSETOF_SC_FPUSED offsetof(struct sigcontext, sc_fpused) + +define SIGINFO_SIZE sizeof(siginfo_t) + +define STACK_T_SIZE ((sizeof(stack_t) + 2 * sizeof(register_t) - 1) & -(2 * sizeof(register_t))) +define _OFFSETOF_STACK_T_SP offsetof(stack_t, ss_sp) +define _OFFSETOF_STACK_T_SIZE offsetof(stack_t, ss_size) +define _OFFSETOF_STACK_T_FLAGS offsetof(stack_t, ss_flags) + +define SS_ONSTACK SS_ONSTACK diff --git a/lib/nbsd_libc/arch/mips/net/Makefile.inc b/lib/nbsd_libc/arch/mips/net/Makefile.inc new file mode 100644 index 000000000..a76082951 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/net/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/12/06 07:12:17 uebayasi Exp $ + +# objects built from assembler sources (need lint stubs) +# hton* and nto* functions provided by ../gen/byte_swap_*.S +SRCS+= + +LSRCS.mips.net= Lint_htonl.c Lint_htons.c Lint_ntohl.c Lint_ntohs.c +LSRCS+= ${LSRCS.mips.net} +DPSRCS+= ${LSRCS.mips.net} +CLEANFILES+= ${LSRCS.mips.net} diff --git a/lib/nbsd_libc/arch/mips/softfloat/milieu.h b/lib/nbsd_libc/arch/mips/softfloat/milieu.h new file mode 100644 index 000000000..e66eb2efc --- /dev/null +++ b/lib/nbsd_libc/arch/mips/softfloat/milieu.h @@ -0,0 +1,48 @@ +/* $NetBSD: milieu.h,v 1.2 2009/12/14 01:07:42 matt Exp $ */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +Include common integer types and flags. +------------------------------------------------------------------------------- +*/ +#include "mips-gcc.h" + +/* +------------------------------------------------------------------------------- +Symbolic Boolean literals. +------------------------------------------------------------------------------- +*/ +enum { + FALSE = 0, + TRUE = 1 +}; diff --git a/lib/nbsd_libc/arch/mips/softfloat/mips-gcc.h b/lib/nbsd_libc/arch/mips/softfloat/mips-gcc.h new file mode 100644 index 000000000..6ed3c22eb --- /dev/null +++ b/lib/nbsd_libc/arch/mips/softfloat/mips-gcc.h @@ -0,0 +1,95 @@ +/* $NetBSD: mips-gcc.h,v 1.2 2009/12/14 01:07:42 matt Exp $ */ + +/* +------------------------------------------------------------------------------- +One of the macros `BIGENDIAN' or `LITTLEENDIAN' must be defined. +------------------------------------------------------------------------------- +*/ +#ifdef __MIPSEB__ +#define BIGENDIAN +#else +#define LITTLEENDIAN +#endif + +/* +------------------------------------------------------------------------------- +The macro `BITS64' can be defined to indicate that 64-bit integer types are +supported by the compiler. +------------------------------------------------------------------------------- +*/ +#define BITS64 + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines the most convenient type that holds +integers of at least as many bits as specified. For example, `uint8' should +be the most convenient type that can hold unsigned integers of as many as +8 bits. The `flag' type must be able to hold either a 0 or 1. For most +implementations of C, `flag', `uint8', and `int8' should all be `typedef'ed +to the same as `int'. +------------------------------------------------------------------------------- +*/ +typedef int flag; +typedef int uint8; +typedef int int8; +typedef int uint16; +typedef int int16; +typedef unsigned int uint32; +typedef signed int int32; +#ifdef BITS64 +typedef unsigned long long int uint64; +typedef signed long long int int64; +#endif + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines a type that holds integers +of _exactly_ the number of bits specified. For instance, for most +implementation of C, `bits16' and `sbits16' should be `typedef'ed to +`unsigned short int' and `signed short int' (or `short int'), respectively. +------------------------------------------------------------------------------- +*/ +typedef unsigned char bits8; +typedef signed char sbits8; +typedef unsigned short int bits16; +typedef signed short int sbits16; +typedef unsigned int bits32; +typedef signed int sbits32; +#ifdef BITS64 +typedef unsigned long long int bits64; +typedef signed long long int sbits64; +#endif + +#ifdef BITS64 +/* +------------------------------------------------------------------------------- +The `LIT64' macro takes as its argument a textual integer literal and +if necessary ``marks'' the literal as having a 64-bit integer type. +For example, the GNU C Compiler (`gcc') requires that 64-bit literals be +appended with the letters `LL' standing for `long long', which is `gcc's +name for the 64-bit integer type. Some compilers may allow `LIT64' to be +defined as the identity macro: `#define LIT64( a ) a'. +------------------------------------------------------------------------------- +*/ +#define LIT64( a ) a##LL +#endif + +/* +------------------------------------------------------------------------------- +The macro `INLINE' can be used before functions that should be inlined. If +a compiler does not support explicit inlining, this macro should be defined +to be `static'. +------------------------------------------------------------------------------- +*/ +#define INLINE static inline + +/* +------------------------------------------------------------------------------- +The ARM FPA is odd in that it stores doubles high-order word first, no matter +what the endianness of the CPU. VFP is sane. +------------------------------------------------------------------------------- +*/ +#if defined(SOFTFLOAT_FOR_GCC) +#define FLOAT64_DEMANGLE(a) (a) +#define FLOAT64_MANGLE(a) (a) +#endif diff --git a/lib/nbsd_libc/arch/mips/softfloat/softfloat.h b/lib/nbsd_libc/arch/mips/softfloat/softfloat.h new file mode 100644 index 000000000..ab2ae0569 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/softfloat/softfloat.h @@ -0,0 +1,323 @@ +/* $NetBSD: softfloat.h,v 1.3 2011/01/17 23:53:04 matt Exp $ */ + +/* This is a derivative work. */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +The macro `FLOATX80' must be defined to enable the extended double-precision +floating-point format `floatx80'. If this macro is not defined, the +`floatx80' type will not be defined, and none of the functions that either +input or output the `floatx80' type will be defined. The same applies to +the `FLOAT128' macro and the quadruple-precision format `float128'. +------------------------------------------------------------------------------- +*/ +/* #define FLOATX80 */ +#if defined(__mips_n32) || defined(__mips_n64) +#define FLOAT128 +#endif + +#include + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point types. +------------------------------------------------------------------------------- +*/ +typedef unsigned int float32; +typedef unsigned long long float64; +#ifdef FLOATX80 +typedef struct { + unsigned short high; + unsigned long long low; +} floatx80; +#endif +#ifdef FLOAT128 +typedef struct { + unsigned long long high, low; +} float128; +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point underflow tininess-detection mode. +------------------------------------------------------------------------------- +*/ +#ifndef SOFTFLOAT_FOR_GCC +extern int float_detect_tininess; +#endif +enum { + float_tininess_after_rounding = 0, + float_tininess_before_rounding = 1 +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point rounding mode. +------------------------------------------------------------------------------- +*/ +extern fp_rnd float_rounding_mode; +enum { + float_round_nearest_even = FP_RN, + float_round_to_zero = FP_RZ, + float_round_down = FP_RM, + float_round_up = FP_RP +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point exception flags. +------------------------------------------------------------------------------- +*/ +extern fp_except float_exception_flags; +extern fp_except float_exception_mask; +enum { + float_flag_inexact = FP_X_IMP, + float_flag_underflow = FP_X_UFL, + float_flag_overflow = FP_X_OFL, + float_flag_divbyzero = FP_X_DZ, + float_flag_invalid = FP_X_INV +}; + +/* +------------------------------------------------------------------------------- +Routine to raise any or all of the software IEC/IEEE floating-point +exception flags. +------------------------------------------------------------------------------- +*/ +void float_raise( fp_except ); + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE integer-to-floating-point conversion routines. +------------------------------------------------------------------------------- +*/ +float32 int32_to_float32( int ); +float64 int32_to_float64( int ); +#ifdef FLOATX80 +floatx80 int32_to_floatx80( int ); +#endif +#ifdef FLOAT128 +float128 int32_to_float128( int ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __floatdi?f is in libgcc2.c */ +float32 int64_to_float32( long long ); +float64 int64_to_float64( long long ); +#ifdef FLOATX80 +floatx80 int64_to_floatx80( long long ); +#endif +#endif +#ifdef FLOAT128 +float128 int64_to_float128( long long ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float32_to_int32( float32 ); +int float32_to_int32_round_to_zero( float32 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned int float32_to_uint32_round_to_zero( float32 ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ +long long float32_to_int64( float32 ); +long long float32_to_int64_round_to_zero( float32 ); +#endif +float64 float32_to_float64( float32 ); +#ifdef FLOATX80 +floatx80 float32_to_floatx80( float32 ); +#endif +#ifdef FLOAT128 +float128 float32_to_float128( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision operations. +------------------------------------------------------------------------------- +*/ +float32 float32_round_to_int( float32 ); +float32 float32_add( float32, float32 ); +float32 float32_sub( float32, float32 ); +float32 float32_mul( float32, float32 ); +float32 float32_div( float32, float32 ); +float32 float32_rem( float32, float32 ); +float32 float32_sqrt( float32 ); +int float32_eq( float32, float32 ); +int float32_le( float32, float32 ); +int float32_lt( float32, float32 ); +int float32_eq_signaling( float32, float32 ); +int float32_le_quiet( float32, float32 ); +int float32_lt_quiet( float32, float32 ); +#ifndef SOFTFLOAT_FOR_GCC +int float32_is_signaling_nan( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float64_to_int32( float64 ); +int float64_to_int32_round_to_zero( float64 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned int float64_to_uint32_round_to_zero( float64 ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ +long long float64_to_int64( float64 ); +long long float64_to_int64_round_to_zero( float64 ); +#endif +float32 float64_to_float32( float64 ); +#ifdef FLOATX80 +floatx80 float64_to_floatx80( float64 ); +#endif +#ifdef FLOAT128 +float128 float64_to_float128( float64 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision operations. +------------------------------------------------------------------------------- +*/ +float64 float64_round_to_int( float64 ); +float64 float64_add( float64, float64 ); +float64 float64_sub( float64, float64 ); +float64 float64_mul( float64, float64 ); +float64 float64_div( float64, float64 ); +float64 float64_rem( float64, float64 ); +float64 float64_sqrt( float64 ); +int float64_eq( float64, float64 ); +int float64_le( float64, float64 ); +int float64_lt( float64, float64 ); +int float64_eq_signaling( float64, float64 ); +int float64_le_quiet( float64, float64 ); +int float64_lt_quiet( float64, float64 ); +#ifndef SOFTFLOAT_FOR_GCC +int float64_is_signaling_nan( float64 ); +#endif + +#ifdef FLOATX80 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int floatx80_to_int32( floatx80 ); +int floatx80_to_int32_round_to_zero( floatx80 ); +long long floatx80_to_int64( floatx80 ); +long long floatx80_to_int64_round_to_zero( floatx80 ); +float32 floatx80_to_float32( floatx80 ); +float64 floatx80_to_float64( floatx80 ); +#ifdef FLOAT128 +float128 floatx80_to_float128( floatx80 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision rounding precision. Valid +values are 32, 64, and 80. +------------------------------------------------------------------------------- +*/ +extern int floatx80_rounding_precision; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision operations. +------------------------------------------------------------------------------- +*/ +floatx80 floatx80_round_to_int( floatx80 ); +floatx80 floatx80_add( floatx80, floatx80 ); +floatx80 floatx80_sub( floatx80, floatx80 ); +floatx80 floatx80_mul( floatx80, floatx80 ); +floatx80 floatx80_div( floatx80, floatx80 ); +floatx80 floatx80_rem( floatx80, floatx80 ); +floatx80 floatx80_sqrt( floatx80 ); +int floatx80_eq( floatx80, floatx80 ); +int floatx80_le( floatx80, floatx80 ); +int floatx80_lt( floatx80, floatx80 ); +int floatx80_eq_signaling( floatx80, floatx80 ); +int floatx80_le_quiet( floatx80, floatx80 ); +int floatx80_lt_quiet( floatx80, floatx80 ); +int floatx80_is_signaling_nan( floatx80 ); + +#endif + +#ifdef FLOAT128 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float128_to_int32( float128 ); +int float128_to_int32_round_to_zero( float128 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned int float128_to_uint32_round_to_zero( float64 ); +#endif +long long float128_to_int64( float128 ); +long long float128_to_int64_round_to_zero( float128 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned long long float128_to_uint64_round_to_zero( float128 ); +#endif +float32 float128_to_float32( float128 ); +float64 float128_to_float64( float128 ); +#ifdef FLOATX80 +floatx80 float128_to_floatx80( float128 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision operations. +------------------------------------------------------------------------------- +*/ +float128 float128_round_to_int( float128 ); +float128 float128_add( float128, float128 ); +float128 float128_sub( float128, float128 ); +float128 float128_mul( float128, float128 ); +float128 float128_div( float128, float128 ); +float128 float128_rem( float128, float128 ); +float128 float128_sqrt( float128 ); +int float128_eq( float128, float128 ); +int float128_le( float128, float128 ); +int float128_lt( float128, float128 ); +int float128_eq_signaling( float128, float128 ); +int float128_le_quiet( float128, float128 ); +int float128_lt_quiet( float128, float128 ); +int float128_is_signaling_nan( float128 ); +flag float128_is_nan( float128 ); + +#endif + diff --git a/lib/nbsd_libc/arch/mips/stdlib/Makefile.inc b/lib/nbsd_libc/arch/mips/stdlib/Makefile.inc new file mode 100644 index 000000000..2f91b992a --- /dev/null +++ b/lib/nbsd_libc/arch/mips/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/31 20:40:00 dsl Exp $ diff --git a/lib/nbsd_libc/arch/mips/string/Makefile.inc b/lib/nbsd_libc/arch/mips/string/Makefile.inc new file mode 100644 index 000000000..0213864d4 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/string/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/12/14 01:07:42 matt Exp $ + +SRCS+= bcmp.S bcopy.S bzero.S \ + ffs.S \ + memcpy.S memmove.S \ + strchr.S strrchr.S \ + strcmp.S strlen.S diff --git a/lib/nbsd_libc/arch/mips/string/bcmp.S b/lib/nbsd_libc/arch/mips/string/bcmp.S new file mode 100644 index 000000000..c90fa2c36 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/string/bcmp.S @@ -0,0 +1,124 @@ +/* $NetBSD: bcmp.S,v 1.9 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 +#define _LOCORE /* XXX not really, just assembly-code source */ +#include /* LWLO/LWHI, SWLO/SWHI */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)bcmp.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: bcmp.S,v 1.9 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* bcmp(s1, s2, n) */ + + +LEAF(bcmp) + .set noreorder + blt a2, 16, small # is it worth any trouble? + xor v0, a0, a1 # compare low two bits of addresses + and v0, v0, 3 + PTR_SUBU a3, zero, a1 # compute # bytes to word align address + bne v0, zero, unaligned # not possible to align addresses + and a3, a3, 3 + + beq a3, zero, 1f + PTR_SUBU a2, a2, a3 # subtract from remaining count + move v0, v1 # init v0,v1 so unmodified bytes match + LWHI v0, 0(a0) # read 1, 2, or 3 bytes + LWHI v1, 0(a1) + PTR_ADDU a1, a1, a3 + bne v0, v1, nomatch + PTR_ADDU a0, a0, a3 +1: + and a3, a2, ~3 # compute number of whole words left + PTR_SUBU a2, a2, a3 # which has to be >= (16-3) & ~3 + PTR_ADDU a3, a3, a0 # compute ending address +2: + lw v0, 0(a0) # compare words + lw v1, 0(a1) + PTR_ADDU a0, a0, 4 + bne v0, v1, nomatch + PTR_ADDU a1, a1, 4 + bne a0, a3, 2b + nop + b small # finish remainder + nop +unaligned: + beq a3, zero, 2f + PTR_SUBU a2, a2, a3 # subtract from remaining count + PTR_ADDU a3, a3, a0 # compute ending address +1: + lbu v0, 0(a0) # compare bytes until a1 word aligned + lbu v1, 0(a1) + PTR_ADDU a0, a0, 1 + bne v0, v1, nomatch + PTR_ADDU a1, a1, 1 + bne a0, a3, 1b + nop +2: + and a3, a2, ~3 # compute number of whole words left + PTR_SUBU a2, a2, a3 # which has to be >= (16-3) & ~3 + PTR_ADDU a3, a3, a0 # compute ending address +3: + LWHI v0, 0(a0) # compare words a0 unaligned, a1 aligned + LWLO v0, 3(a0) + lw v1, 0(a1) + PTR_ADDU a0, a0, 4 + bne v0, v1, nomatch + PTR_ADDU a1, a1, 4 + bne a0, a3, 3b + nop +small: + ble a2, zero, match + PTR_ADDU a3, a2, a0 # compute ending address +1: + lbu v0, 0(a0) + lbu v1, 0(a1) + PTR_ADDU a0, a0, 1 + bne v0, v1, nomatch + PTR_ADDU a1, a1, 1 + bne a0, a3, 1b + nop +match: + j ra + move v0, zero +nomatch: + j ra + li v0, 1 + .set reorder +END(bcmp) diff --git a/lib/nbsd_libc/arch/mips/string/bzero.S b/lib/nbsd_libc/arch/mips/string/bzero.S new file mode 100644 index 000000000..f25c02055 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/string/bzero.S @@ -0,0 +1,97 @@ +/* $NetBSD: bzero.S,v 1.10 2009/12/14 02:53:52 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)bzero.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: bzero.S,v 1.10 2009/12/14 02:53:52 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + + +#define _LOCORE /* XXX not really, just assembly-code source */ +#include + +/* bzero(s1, n) */ + +LEAF(bzero) + .set noreorder + blt a1, 3*SZREG, smallclr # small amount to clear? + PTR_SUBU a3, zero, a0 # compute # bytes to word align address + and a3, a3, SZREG-1 + beq a3, zero, 1f # skip if word aligned +#if SZREG == 4 + PTR_SUBU a1, a1, a3 # subtract from remaining count + SWHI zero, 0(a0) # clear 1, 2, or 3 bytes to align + PTR_ADDU a0, a0, a3 +#endif +#if SZREG == 8 + PTR_SUBU a1, a1, a3 # subtract from remaining count + PTR_ADDU a0, a0, a3 # align dst to next word + sll a3, a3, 3 # bits to bytes + li a2, -1 # make a mask +#if _BYTE_ORDER == _BIG_ENDIAN + REG_SRLV a2, a2, a3 # we want to keep the MSB bytes +#endif +#if _BYTE_ORDER == _LITTLE_ENDIAN + REG_SLLV a2, a2, a3 # we want to keep the LSB bytes +#endif + nor a2, zero, a2 # complement the mask + REG_L v0, -SZREG(a0) # load the word to partially clear + and v0, v0, a2 # clear the bytes + REG_S v0, -SZREG(a0) # store it back +#endif +1: + and v0, a1, SZREG-1 # compute number of words left + PTR_SUBU a3, a1, v0 + move a1, v0 + PTR_ADDU a3, a3, a0 # compute ending address +2: + PTR_ADDU a0, a0, SZREG # clear words + bne a0, a3, 2b # unrolling loop doesnt help + REG_S zero, -SZREG(a0) # since we are limited by memory speed +smallclr: + ble a1, zero, 2f + PTR_ADDU a3, a1, a0 # compute ending address +1: + PTR_ADDU a0, a0, 1 # clear bytes + bne a0, a3, 1b + sb zero, -1(a0) +2: + j ra + nop +END(bzero) diff --git a/lib/nbsd_libc/arch/mips/sys/__clone.S b/lib/nbsd_libc/arch/mips/sys/__clone.S new file mode 100644 index 000000000..de7ef4792 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/__clone.S @@ -0,0 +1,103 @@ +/* $NetBSD: __clone.S,v 1.8 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __clone.S,v 1.8 2009/12/14 01:07:42 matt Exp $"); +#endif /* SYSLIBC_SCCS and not lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +LEAF(__clone) + PIC_PROLOGUE(__clone) + + /* + * Sanity checks: func and stack may not be NULL. + */ + li v0, EINVAL + beq a0, zero, 8f + beq a1, zero, 8f + + /* + * We need to be able to get at the func and arg arguments + * in the child. Luckily, we have a convenient place to + * do this: the child's stack. + */ + PTR_SUBU a1, a1, CALLFRAME_SIZ + REG_S a0, 0(a1) + REG_S a3, SZREG(a1) + + /* + * The system call expects (flags, stack). + */ + move a0, a2 + SYSTRAP(__clone) + bne a3, zero, 8f + + bne v1, zero, 9f # v1 (rv[1]) == 0, parent, child pid in v0 + + PIC_RETURN() # parent return + +8: PIC_TAILCALL(__cerror) + /* NOTREACHED */ + +9: /* + * Child: Fetch the function and argument from the new stack and create + a frame so that the child can be safely called. + * + * There are already register slots on the stack from above. + * They already include the o32 argument save area. The + * highest is unused. a1 should equal sp now. + */ + + REG_L t9, 0(sp) + REG_L a0, SZREG(sp) + + REG_S zero, CALLFRAME_RA(sp) /* make sure stack frame ends */ + + /* Call the clone's entry point. */ + SAVE_GP(CALLFRAME_GP) + jal t9 + + /* Pass the return value to _exit. */ + move a0, v0 + PIC_TAILCALL(_exit) + + /* NOTREACHED */ +END(__clone) diff --git a/lib/nbsd_libc/arch/mips/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/mips/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..929edac97 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/__sigaction14_sigtramp.c @@ -0,0 +1,79 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.7 2008/04/28 20:22:56 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.7 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + extern const int __sigtramp_sigcontext_1[]; + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specified or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/mips/sys/__sigtramp2.S b/lib/nbsd_libc/arch/mips/sys/__sigtramp2.S new file mode 100644 index 000000000..63b0661de --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/__sigtramp2.S @@ -0,0 +1,54 @@ +/* $NetBSD: __sigtramp2.S,v 1.3 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" +#include "assym.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __sigtramp2.S,v 1.3 2009/12/14 01:07:42 matt Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + + +/* + * The MIPS signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * sp -> siginfo_t structure + * sp + SIGINFO_SIZE -> ucontext_t structure + */ +LEAF_NOPROFILE(__sigtramp_siginfo_2) + PTR_ADDU a0, sp, SIGINFO_SIZE /* address of ucontext */ + SYSTRAP(setcontext) /* and do setcontext */ + move a0, v0 /* exit with errno */ + SYSTRAP(exit) /* if sigreturn fails */ +END(__sigtramp_siginfo_2) diff --git a/lib/nbsd_libc/arch/mips/sys/__syscall.S b/lib/nbsd_libc/arch/mips/sys/__syscall.S new file mode 100644 index 000000000..653a95421 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/__syscall.S @@ -0,0 +1,5 @@ +/* $NetBSD: __syscall.S,v 1.1 2000/12/13 23:18:40 simonb Exp $ */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/mips/sys/__vfork14.S b/lib/nbsd_libc/arch/mips/sys/__vfork14.S new file mode 100644 index 000000000..dcb309fd9 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/__vfork14.S @@ -0,0 +1,63 @@ +/* $NetBSD: __vfork14.S,v 1.6 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)Ovfork.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: __vfork14.S,v 1.6 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * pid = vfork(); + * + * v1 == 0 in parent process, v1 == 1 in child process. + * v0 == pid of child in parent, v0 == pid of parent in child. + */ + +LEAF(__vfork14) + PIC_PROLOGUE(__vfork14) + li v0, SYS___vfork14 # system call number for vfork + syscall + beq a3, zero, 1f # jump if no errors + PIC_TAILCALL(__cerror) +1: + beq v1, zero, 2f # parent process ? + move v0, zero # return zero in child +2: + PIC_RETURN() +END(__vfork14) diff --git a/lib/nbsd_libc/arch/mips/sys/brk.S b/lib/nbsd_libc/arch/mips/sys/brk.S new file mode 100644 index 000000000..f0571d075 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/brk.S @@ -0,0 +1,74 @@ +/* $NetBSD: brk.S,v 1.18 2010/12/12 09:00:37 tsutsui Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)brk.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: brk.S,v 1.18 2010/12/12 09:00:37 tsutsui Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + + .globl _C_LABEL(__minbrk) + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data +_C_LABEL(__minbrk): + PTR_WORD _C_LABEL(_end) + .text + +LEAF(_brk) + PIC_PROLOGUE(_brk) + PTR_LA t0, _C_LABEL(__minbrk) + PTR_L v0, 0(t0) + bgeu a0, v0, 1f + move a0, v0 # dont allow break < minbrk +1: + li v0, SYS_break + syscall + bne a3, zero, 2f + PTR_LA t0, _C_LABEL(__curbrk) + PTR_S a0, 0(t0) + move v0, zero + PIC_RETURN() +2: + PIC_TAILCALL(__cerror) +END(_brk) diff --git a/lib/nbsd_libc/arch/mips/sys/cerror.S b/lib/nbsd_libc/arch/mips/sys/cerror.S new file mode 100644 index 000000000..059c3d313 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/cerror.S @@ -0,0 +1,81 @@ +/* $NetBSD: cerror.S,v 1.16 2011/01/17 23:34:44 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)cerror.s 8.1 (Berkeley) 6/16/93") +#else + RCSID("$NetBSD: cerror.S,v 1.16 2011/01/17 23:34:44 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef PIC + .hidden __cerror +#endif + +#ifdef _REENTRANT + .globl _C_LABEL(__errno) +NESTED_NOPROFILE(__cerror, CALLFRAME_SIZ, ra) + .mask 0x80000000, (CALLFRAME_RA - CALLFRAME_SIZ) + PIC_PROLOGUE(__cerror) + PTR_SUBU sp, CALLFRAME_SIZ + PTR_S ra, CALLFRAME_RA(sp) + INT_S v0, CALLFRAME_S0(sp) # save errno value + + SAVE_GP(CALLFRAME_GP) + + PTR_LA t9, _C_LABEL(__errno) # locate address of errno + RESTORE_GP64 # restore GP (from t3) + jalr t9 # __errno() + + INT_L t0, CALLFRAME_S0(sp) + PTR_L ra, CALLFRAME_RA(sp) + INT_S t0, 0(v0) # update errno value + PTR_ADDU sp, CALLFRAME_SIZ + li v0, -1 + li v1, -1 + j ra # GP has been restored. +END(__cerror) +#else + .globl _C_LABEL(errno) +LEAF_NOPROFILE(__cerror) + PIC_PROLOGUE(__cerror) + INT_S v0, _C_LABEL(errno) + li v0, -1 + li v1, -1 + PIC_RETURN() +END(__cerror) +#endif /* _REENTRANT */ diff --git a/lib/nbsd_libc/arch/mips/sys/exect.S b/lib/nbsd_libc/arch/mips/sys/exect.S new file mode 100644 index 000000000..4f69e12ec --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/exect.S @@ -0,0 +1,52 @@ +/* $NetBSD: exect.S,v 1.10 2009/12/14 01:07:42 matt Exp $ */ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)exect.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: exect.S,v 1.10 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +LEAF(exect) + PIC_PROLOGUE(exect) + li v0, SYS_execve + syscall + bne a3, zero, 1f + PIC_RETURN() +1: + PIC_TAILCALL(__cerror) +END(exect) diff --git a/lib/nbsd_libc/arch/mips/sys/fork.S b/lib/nbsd_libc/arch/mips/sys/fork.S new file mode 100644 index 000000000..1a532fc70 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/fork.S @@ -0,0 +1,56 @@ +/* $NetBSD: fork.S,v 1.12 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)fork.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: fork.S,v 1.12 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +LEAF(__fork) + PIC_PROLOGUE(__fork) + + SYSTRAP(fork) # pid = fork() + bne a3, zero, 2f + beq v1, zero, 1f # v1 == 0 in parent, 1 in child + move v0, zero +1: + PIC_RETURN() +2: + PIC_TAILCALL(__cerror) +END(__fork) diff --git a/lib/nbsd_libc/arch/mips/sys/getcontext.S b/lib/nbsd_libc/arch/mips/sys/getcontext.S new file mode 100644 index 000000000..34743c69a --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/getcontext.S @@ -0,0 +1,56 @@ +/* $NetBSD: getcontext.S,v 1.4 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" +#include "assym.h" +#include + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.4 2009/12/14 01:07:42 matt Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + .set reorder + +LEAF(_getcontext) + PIC_PROLOGUE(_getcontext) + SYSTRAP(getcontext) + bne a3, zero, 1f + REG_PROLOGUE + REG_S zero, (_OFFSETOF_UC_GREGS + _REG_V0 * SZREG)(a0) + REG_S ra, (_OFFSETOF_UC_GREGS + _REG_EPC * SZREG)(a0) + REG_EPILOGUE + PIC_RETURN() +1: + PIC_TAILCALL(__cerror) +END(_getcontext) diff --git a/lib/nbsd_libc/arch/mips/sys/pipe.S b/lib/nbsd_libc/arch/mips/sys/pipe.S new file mode 100644 index 000000000..d4047e241 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/pipe.S @@ -0,0 +1,61 @@ +/* $NetBSD: pipe.S,v 1.12 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)pipe.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: pipe.S,v 1.12 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +LEAF(_pipe) + PIC_PROLOGUE(_pipe); + + SYSTRAP(pipe) # pipe(fildes) int fildes[2]; + + bne a3, zero, 1f + INT_S v0, 0(a0) # store the two file descriptors + INT_S v1, 4(a0) + move v0, zero + PIC_RETURN() +1: + PIC_TAILCALL(__cerror) +END(_pipe) diff --git a/lib/nbsd_libc/arch/mips/sys/ptrace.S b/lib/nbsd_libc/arch/mips/sys/ptrace.S new file mode 100644 index 000000000..a1817e3d6 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/ptrace.S @@ -0,0 +1,54 @@ +/* $NetBSD: ptrace.S,v 1.10 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)ptrace.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: ptrace.S,v 1.10 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +LEAF(ptrace) + PIC_PROLOGUE(ptrace) + INT_S zero, _C_LABEL(errno) /* BOGUS */ + + SYSTRAP(ptrace) + bne a3, zero, 1f + PIC_RETURN() +1: + PIC_TAILCALL(__cerror) +END(ptrace) diff --git a/lib/nbsd_libc/arch/mips/sys/sbrk.S b/lib/nbsd_libc/arch/mips/sys/sbrk.S new file mode 100644 index 000000000..f8b9b522d --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/sbrk.S @@ -0,0 +1,70 @@ +/* $NetBSD: sbrk.S,v 1.18 2010/12/12 09:03:06 tsutsui Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sbrk.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: sbrk.S,v 1.18 2010/12/12 09:03:06 tsutsui Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +_C_LABEL(__curbrk): + PTR_WORD _C_LABEL(_end) + .text + +LEAF(_sbrk) + PIC_PROLOGUE(_sbrk) + PTR_LA t0, _C_LABEL(__curbrk) + PTR_L v1, 0(t0) + PTR_ADDU a0, a0, v1 # compute current break + SYSTRAP(break) + bne a3, zero, 1f + move v0, v1 # return old val of curbrk from above + PTR_LA t0, _C_LABEL(__curbrk) + PTR_S a0, 0(t0) # save current val of curbrk from above + PIC_RETURN() +1: + PIC_TAILCALL(__cerror) +END(_sbrk) diff --git a/lib/nbsd_libc/arch/mips/sys/shmat.S b/lib/nbsd_libc/arch/mips/sys/shmat.S new file mode 100644 index 000000000..0e7842254 --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:52 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/mips/sys/syscall.S b/lib/nbsd_libc/arch/mips/sys/syscall.S new file mode 100644 index 000000000..9d90f0baa --- /dev/null +++ b/lib/nbsd_libc/arch/mips/sys/syscall.S @@ -0,0 +1,45 @@ +/* $NetBSD: syscall.S,v 1.6 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)syscall.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: syscall.S,v 1.6 2009/12/14 01:07:42 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/powerpc/Makefile.inc b/lib/nbsd_libc/arch/powerpc/Makefile.inc new file mode 100644 index 000000000..519090484 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/Makefile.inc @@ -0,0 +1,9 @@ +# $NetBSD: Makefile.inc,v 1.13 2011/01/17 08:23:53 matt Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +CPPFLAGS+= -I. + +.if defined(MKSOFTFLOAT) && (${MKSOFTFLOAT} != "no") +.include +.endif diff --git a/lib/nbsd_libc/arch/powerpc/SYS.h b/lib/nbsd_libc/arch/powerpc/SYS.h new file mode 100644 index 000000000..e6d1ac442 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/SYS.h @@ -0,0 +1,43 @@ +/* $NetBSD: SYS.h,v 1.12 2011/01/15 07:31:12 matt Exp $ */ + +#include +#include + +#ifdef __STDC__ +#define _DOSYSCALL(x) li %r0,(SYS_ ## x) ;\ + sc +#else +#define _DOSYSCALL(x) li %r0,(SYS_/**/x) ;\ + sc +#endif /* __STDC__ */ + +#define _SYSCALL_NOERROR(x,y) .text ;\ + .align 2 ;\ + ENTRY(x) ;\ + _DOSYSCALL(y) + +#define _SYSCALL(x,y) .text ;\ + .align 2 ;\ + 2: b _C_LABEL(__cerror) ;\ + _SYSCALL_NOERROR(x,y) ;\ + bso 2b + +#define SYSCALL_NOERROR(x) _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) _SYSCALL(x,x) + +#define PSEUDO_NOERROR(x,y) _SYSCALL_NOERROR(x,y) ;\ + blr ;\ + END(x) + +#define PSEUDO(x,y) _SYSCALL_NOERROR(x,y) ;\ + bnslr ;\ + b _C_LABEL(__cerror) ;\ + END(x) + +#define RSYSCALL_NOERROR(x) PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) PSEUDO(x,x) + +#define WSYSCALL(weak,strong) WEAK_ALIAS(weak,strong) ;\ + PSEUDO(strong,weak) diff --git a/lib/nbsd_libc/arch/powerpc/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/powerpc/gdtoa/Makefile.inc new file mode 100644 index 000000000..1d786e010 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/powerpc/gdtoa/arith.h b/lib/nbsd_libc/arch/powerpc/gdtoa/arith.h new file mode 100644 index 000000000..4674f629c --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_BIG_ENDIAN diff --git a/lib/nbsd_libc/arch/powerpc/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/powerpc/gdtoa/gd_qnan.h new file mode 100644 index 000000000..75f636b54 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gdtoa/gd_qnan.h @@ -0,0 +1,5 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x7ff80000 +#define d_QNAN1 0x0 diff --git a/lib/nbsd_libc/arch/powerpc/gen/Makefile.inc b/lib/nbsd_libc/arch/powerpc/gen/Makefile.inc new file mode 100644 index 000000000..a0d17995e --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/Makefile.inc @@ -0,0 +1,27 @@ +# $NetBSD: Makefile.inc,v 1.24 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= __setjmp14.S __sigsetjmp14.S _setjmp.S +SRCS+= bswap16.c bswap32.c bswap64.c +SRCS+= fabs.c flt_rounds.c +SRCS+= syncicache.c +SRCS+= _lwp.c makecontext.c resumecontext.c swapcontext.S + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_dbl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +.if (${MKSOFTFLOAT} == "no") +SRCS+= fpgetround.c fpsetround.c fpgetmask.c fpsetmask.c +SRCS+= fpgetsticky.c fpsetsticky.c +.endif + +LSRCS.powerpc.gen=Lint_swapcontext.c +LSRCS+= ${LSRCS.powerpc.gen} +DPSRCS+= ${LSRCS.powerpc.gen} +CLEANFILES+= ${LSRCS.powerpc.gen} diff --git a/lib/nbsd_libc/arch/powerpc/gen/__setjmp14.S b/lib/nbsd_libc/arch/powerpc/gen/__setjmp14.S new file mode 100644 index 000000000..f8cf9556d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/__setjmp14.S @@ -0,0 +1,50 @@ +/* $NetBSD: __setjmp14.S,v 1.5 2011/01/15 07:31:12 matt Exp $ */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: __setjmp14.S,v 1.5 2011/01/15 07:31:12 matt Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * longjmp(a,v) + * will generate a "return(v?v:1)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__setjmp14) + mr %r6,%r3 + li %r3,SIG_BLOCK + li %r4,0 + addi %r5,%r6,4*(1+24) # &sigmask + _DOSYSCALL(__sigprocmask14) # assume no error XXX + mflr %r11 + mfcr %r12 + mr %r10,%r1 + mr %r9,%r2 + stmw %r8,4(%r6) # save r8-r31 + li %r3,0 + blr +END(__setjmp14) + +ENTRY(__longjmp14) + lmw %r8,4(%r3) # load r8-r31 + mr %r6,%r4 + mtlr %r11 + mtcr %r12 + mr %r2,%r9 + mr %r1,%r10 + addi %r4,%r3,4*(1+24) # &sigmask + li %r3,SIG_SETMASK + li %r5,0 + _DOSYSCALL(__sigprocmask14) # assume no error XXX + or. %r3,%r6,%r6 + bnelr + li %r3,1 + blr +END(__longjmp14) diff --git a/lib/nbsd_libc/arch/powerpc/gen/__sigsetjmp14.S b/lib/nbsd_libc/arch/powerpc/gen/__sigsetjmp14.S new file mode 100644 index 000000000..e026982c1 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/__sigsetjmp14.S @@ -0,0 +1,46 @@ +/* $NetBSD: __sigsetjmp14.S,v 1.5 2011/01/15 07:31:12 matt Exp $ */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: __sigsetjmp14.S,v 1.5 2011/01/15 07:31:12 matt Exp $") +#endif + +ENTRY(__sigsetjmp14) + mr %r6,%r3 + or. %r7,%r4,%r4 + beq 1f + li %r3,SIG_BLOCK + li %r4,0 + addi %r5,%r6,4*(1+24) # &sigmask + _DOSYSCALL(__sigprocmask14) # assume no error XXX +1: + mflr %r11 + mfcr %r12 + mr %r10,%r1 + mr %r9,%r2 + stmw %r7,0(%r6) # save r7-r31 + li %r3,0 + blr +END(__sigsetjmp14) + +ENTRY(__siglongjmp14) + lmw %r7,0(%r3) # load r7-r31 + mr %r6,%r4 + mtlr %r11 + mtcr %r12 + mr %r2,%r9 + mr %r1,%r10 + or. %r7,%r7,%r7 + beq 1f + addi %r4,%r3,4*(1+24) # &sigmask + li %r3,SIG_SETMASK + li %r5,0 + _DOSYSCALL(__sigprocmask14) # assume no error XXX +1: + or. %r3,%r6,%r6 + bnelr + li %r3,1 + blr +END(__siglongjmp14) diff --git a/lib/nbsd_libc/arch/powerpc/gen/_lwp.c b/lib/nbsd_libc/arch/powerpc/gen/_lwp.c new file mode 100644 index 000000000..4a5597037 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/_lwp.c @@ -0,0 +1,67 @@ +/* $NetBSD: _lwp.c,v 1.4 2005/06/12 05:21:26 lukem Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Allen Briggs for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.4 2005/06/12 05:21:26 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), void *arg, + void *private, caddr_t stack_base, size_t stack_size) +{ + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + sp = (void **) (stack_base + stack_size); + + u->uc_mcontext.__gregs[3] = (int) arg; /* arg1 */ + u->uc_mcontext.__gregs[1] = ((int) sp) - 12; /* stack */ + u->uc_mcontext.__gregs[33] = (int) _lwp_exit; /* LR */ + u->uc_mcontext.__gregs[34] = (int) start; /* PC */ +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/_setjmp.S b/lib/nbsd_libc/arch/powerpc/gen/_setjmp.S new file mode 100644 index 000000000..44f2a5733 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/_setjmp.S @@ -0,0 +1,39 @@ +/* $NetBSD: _setjmp.S,v 1.3 2011/01/15 07:31:12 matt Exp $ */ + +#include + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: _setjmp.S,v 1.3 2011/01/15 07:31:12 matt Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v?v:1)" from the last call to + * _setjmp(a) + * by restoring registers from the stack. + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + mflr %r11 /* save return address */ + mfcr %r12 /* save condition register */ + mr %r10,%r1 /* save stack pointer */ + mr %r9,%r2 /* save GPR2 (not needed) */ + stmw %r9,8(%r3) /* save r9..r31 */ + li %r3,0 /* indicate success */ + blr /* return */ +END(_setjmp) + +ENTRY(_longjmp) + lmw %r9,8(%r3) /* save r9..r31 */ + mtlr %r11 /* restore LR */ + mtcr %r12 /* restore CR */ + mr %r2,%r9 /* restore GPR2 (not needed) */ + mr %r1,%r10 /* restore stack */ + or. %r3,%r4,%r4 /* get return value */ + bnelr /* return if not 0 */ + li %r3,1 /* what's the point? */ + blr /* return */ +END(_longjmp) diff --git a/lib/nbsd_libc/arch/powerpc/gen/fabs.c b/lib/nbsd_libc/arch/powerpc/gen/fabs.c new file mode 100644 index 000000000..25d28db8b --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/fabs.c @@ -0,0 +1,15 @@ +/* $NetBSD: fabs.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +#include + +double +fabs(double x) +{ +#ifdef _SOFT_FLOAT + if (x < 0) + x = -x; +#else + __asm volatile("fabs %0,%1" : "=f"(x) : "f"(x)); +#endif + return (x); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/flt_rounds.c b/lib/nbsd_libc/arch/powerpc/gen/flt_rounds.c new file mode 100644 index 000000000..d0b14f40d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/flt_rounds.c @@ -0,0 +1,62 @@ +/* $NetBSD: flt_rounds.c,v 1.10 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.10 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 0, /* round to zero */ + 2, /* round to positive infinity */ + 3 /* round to negative infinity */ +}; + +int +__flt_rounds(void) +{ +#ifdef _SOFT_FLOAT + return map[fpgetround()]; +#else + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return map[((uint32_t)fpscr & FPSCR_RN)]; +#endif +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/fpgetmask.c b/lib/nbsd_libc/arch/powerpc/gen/fpgetmask.c new file mode 100644 index 000000000..278411b49 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/fpgetmask.c @@ -0,0 +1,57 @@ +/* $NetBSD: fpgetmask.c,v 1.9 2008/04/28 20:22:56 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetmask.c,v 1.9 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetmask,_fpgetmask) +#endif + +#define MASKBITS (FPSCR_VE|FPSCR_OE|FPSCR_UE|FPSCR_ZE|FPSCR_XE) +#define MASKSHFT 3 + +fp_except +fpgetmask(void) +{ + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return (((uint32_t)fpscr & MASKBITS) >> MASKSHFT); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/fpgetround.c b/lib/nbsd_libc/arch/powerpc/gen/fpgetround.c new file mode 100644 index 000000000..65c19eb80 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/fpgetround.c @@ -0,0 +1,57 @@ +/* $NetBSD: fpgetround.c,v 1.9 2008/04/28 20:22:56 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.9 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetround,_fpgetround) +#endif + +#define ROUNDBITS FPSCR_RN +#define ROUNDSHFT 0 + +fp_rnd +fpgetround(void) +{ + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return (((uint32_t)fpscr & ROUNDBITS) >> ROUNDSHFT); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/fpgetsticky.c b/lib/nbsd_libc/arch/powerpc/gen/fpgetsticky.c new file mode 100644 index 000000000..7fba344c9 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/fpgetsticky.c @@ -0,0 +1,57 @@ +/* $NetBSD: fpgetsticky.c,v 1.9 2008/04/28 20:22:56 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetsticky.c,v 1.9 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetsticky,_fpgetsticky) +#endif + +#define STICKYBITS (FPSCR_XX|FPSCR_ZX|FPSCR_UX|FPSCR_OX|FPSCR_VX) +#define STICKYSHFT 25 + +fp_except +fpgetsticky(void) +{ + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return (((uint32_t)fpscr & STICKYBITS) >> STICKYSHFT); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/fpsetmask.c b/lib/nbsd_libc/arch/powerpc/gen/fpsetmask.c new file mode 100644 index 000000000..8e6f07939 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/fpsetmask.c @@ -0,0 +1,62 @@ +/* $NetBSD: fpsetmask.c,v 1.9 2008/04/28 20:22:56 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetmask.c,v 1.9 2008/04/28 20:22:56 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetmask,_fpsetmask) +#endif + +#define MASKBITS (FPSCR_XE|FPSCR_ZE|FPSCR_UE|FPSCR_OE|FPSCR_VE) +#define MASKSHFT 3 + +fp_except +fpsetmask(fp_except mask) +{ + uint64_t fpscr; + fp_except old; + + __asm volatile("mffs %0" : "=f"(fpscr)); + old = ((uint32_t)fpscr & MASKBITS) >> MASKSHFT; + fpscr &= ~MASKBITS; + fpscr |= ((uint32_t)mask << MASKSHFT) & MASKBITS; + __asm volatile("mtfsf 0xff,%0" :: "f"(fpscr)); + return (old); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/fpsetround.c b/lib/nbsd_libc/arch/powerpc/gen/fpsetround.c new file mode 100644 index 000000000..d45124104 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/fpsetround.c @@ -0,0 +1,61 @@ +/* $NetBSD: fpsetround.c,v 1.9 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.9 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetround,_fpsetround) +#endif + +#define ROUNDBITS FPSCR_RN + +fp_rnd +fpsetround(fp_rnd rnd_dir) +{ + uint64_t fpscr; + fp_rnd old; + + __asm volatile("mffs %0" : "=f"(fpscr)); + old = (uint32_t)fpscr & ROUNDBITS; + fpscr &= ~ROUNDBITS; + fpscr |= rnd_dir & ROUNDBITS; + __asm volatile("mtfsf 0xff,%0" :: "f"(fpscr)); + return (old); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/fpsetsticky.c b/lib/nbsd_libc/arch/powerpc/gen/fpsetsticky.c new file mode 100644 index 000000000..2ce3f7d45 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/fpsetsticky.c @@ -0,0 +1,84 @@ +/* $NetBSD: fpsetsticky.c,v 1.10 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetsticky.c,v 1.10 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#define STICKYBITS (FPSCR_XX|FPSCR_ZX|FPSCR_UX|FPSCR_OX|FPSCR_VX) +#define INVBITS (FPSCR_VXCVI|FPSCR_VXSQRT|FPSCR_VXSOFT|FPSCR_VXVC|\ + FPSCR_VXIMZ|FPSCR_VXZDZ|FPSCR_VXIDI|FPSCR_VXISI|\ + FPSCR_VXSNAN) +#define STICKYSHFT 25 + +#ifdef __weak_alias +__weak_alias(fpsetsticky,_fpsetsticky) +#endif + +fp_except +fpsetsticky(fp_except mask) +{ + uint64_t fpscr; + fp_except old; + + __asm volatile("mffs %0" : "=f"(fpscr)); + old = ((uint32_t)fpscr & STICKYBITS) >> STICKYSHFT; + /* + * FPSCR_VX (aka FP_X_INV) is not a sticky bit but a summary of the + * all the FPSCR_VX* sticky bits. So when FP_X_INV is cleared then + * clear all of those bits, likewise when it's set, set them all. + */ + if ((mask & FP_X_INV) == 0) + fpscr &= ~INVBITS; + else + fpscr |= INVBITS; + fpscr &= ~STICKYBITS; + fpscr |= ((uint32_t)mask << STICKYSHFT) & STICKYBITS; + /* + * Make FPSCR_FX reflect the presence of a set sticky bit (or not). + */ + if (fpscr & (STICKYBITS|INVBITS)) + fpscr |= FPSCR_FX; + else + fpscr &= ~FPSCR_FX; + /* + * Write back the fpscr. + */ + __asm volatile("mtfsf 0xff,%0" :: "f"(fpscr)); + return (old); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/makecontext.c b/lib/nbsd_libc/arch/powerpc/gen/makecontext.c new file mode 100644 index 000000000..3d8f763a0 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/makecontext.c @@ -0,0 +1,84 @@ +/* $NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#if __STDC__ +#include +#else +#include +#endif + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + int *sp; + int i; + va_list ap; + + /* LINTED uintptr_t is safe */ + sp = (int *) + ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + /* LINTED uintptr_t is safe */ + sp -= 2 + (argc > 8 ? argc - 8: 0); /* Make room for call frame. */ + sp = (int *) + ((uintptr_t)sp & ~0xf); /* Align on quad-word boundary. */ + + /* + * Start executing at -- when completes, return to + * <_resumecontext>. + */ + gr[_REG_R1] = (__greg_t)sp; + gr[_REG_LR] = (__greg_t)_resumecontext; + gr[_REG_PC] = (__greg_t)func; + + /* Wipe out stack frame backchain pointer. */ + *sp = 0; + + /* Construct argument list. */ + va_start(ap, argc); + /* Up to the first eight arguments are passed in r3-10. */ + for (i = 0; i < argc && i < 8; i++) + gr[_REG_R3 + i] = va_arg(ap, int); + /* Pass remaining arguments on the stack above the backchain/lr gap. */ + for (sp += 2; i < argc; i++) + *sp++ = va_arg(ap, int); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/nanf.c b/lib/nbsd_libc/arch/powerpc/gen/nanf.c new file mode 100644 index 000000000..1688d0eae --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0x7f, 0xc0, 0, 0 } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/powerpc/gen/resumecontext.c b/lib/nbsd_libc/arch/powerpc/gen/resumecontext.c new file mode 100644 index 000000000..1a7c4c55f --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/powerpc/gen/swapcontext.S b/lib/nbsd_libc/arch/powerpc/gen/swapcontext.S new file mode 100644 index 000000000..d5db5d1ba --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/swapcontext.S @@ -0,0 +1,70 @@ +/* $NetBSD: swapcontext.S,v 1.6 2011/01/16 02:43:10 matt Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" +#include "assym.h" + +__RCSID("$NetBSD: swapcontext.S,v 1.6 2011/01/16 02:43:10 matt Exp $") + +#define XCALLFRAMELEN (((2+3)*SZREG + CALLFRAMELEN - 1) & -CALLFRAMELEN) +#define XCALLFRAME_R30 (XCALLFRAMELEN-1*SZREG) +#define XCALLFRAME_UCP (XCALLFRAMELEN-2*SZREG) +#define XCALLFRAME_OUCP (XCALLFRAMELEN-3*SZREG) + +ENTRY(swapcontext) + stwu %r1,-XCALLFRAMELEN(%r1) # set up new stack frame + mflr %r0 + stw %r0,XCALLFRAMELEN+CALLFRAME_LR(%r1) # save link register + stw %r3,XCALLFRAME_OUCP(%r1) # must save oucp + stw %r4,XCALLFRAME_UCP(%r1) # must save ucp +#ifdef PIC + stw %r30,XCALLFRAME_R30(%r1) # must save r30 + PIC_TOCSETUP(swapcontext,%r30) # setup toc pointer +#endif + bl PIC_PLT(_C_LABEL(_getcontext)) # getcontext(oucp) + cmpwi %r3,0 + bne 1f + lwz %r11,XCALLFRAME_OUCP(%r1) # load oucp for adjustment + lwz %r0,XCALLFRAMELEN+CALLFRAME_LR(%r1) + stw %r0,UC_GREGS_PC(%r11) # pc <- lr + addi %r0,%r1,XCALLFRAMELEN + stw %r0,UC_GREGS_R1(%r11) # adjust sp + lwz %r3,XCALLFRAME_UCP(%r1) # load ucp + bl PIC_PLT(_C_LABEL(setcontext)) # setcontext(ucp) +1: + lwz %r0,XCALLFRAMELEN+CALLFRAME_LR(%r1) +#ifdef PIC + lwz %r30,XCALLFRAME_R30(%r1) +#endif + mtlr %r0 + addi %r1,%r1,XCALLFRAMELEN + blr +END(swapcontext) diff --git a/lib/nbsd_libc/arch/powerpc/gen/syncicache.c b/lib/nbsd_libc/arch/powerpc/gen/syncicache.c new file mode 100644 index 000000000..2c16406a1 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/gen/syncicache.c @@ -0,0 +1,140 @@ +/* $NetBSD: syncicache.c,v 1.15 2008/03/18 20:11:43 he Exp $ */ + +/* + * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank. + * Copyright (C) 1995-1997, 1999 TooLs GmbH. + * 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 by TooLs GmbH. + * 4. The name of TooLs GmbH may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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 +#if defined(_KERNEL) +#include +#include +#include +#endif +#if !defined(_STANDALONE) +#include +#endif + +#include + + +#if defined(_STANDALONE) +#ifndef CACHELINESIZE +#error "Must know the size of a cache line" +#endif +static struct cache_info _cache_info = { + CACHELINESIZE, + CACHELINESIZE, + CACHELINESIZE, + CACHELINESIZE +}; +#define CACHEINFO _cache_info +#elif defined(_KERNEL) +#define CACHEINFO (curcpu()->ci_ci) +#else +#include + +size_t __getcachelinesize (void); + +static int _cachelinesize = 0; + +static struct cache_info _cache_info; +#define CACHEINFO _cache_info + +size_t +__getcachelinesize(void) +{ + static int cachemib[] = { CTL_MACHDEP, CPU_CACHELINE }; + static int cacheinfomib[] = { CTL_MACHDEP, CPU_CACHEINFO }; + size_t clen = sizeof(_cache_info); + + if (_cachelinesize) + return _cachelinesize; + + if (sysctl(cacheinfomib, sizeof(cacheinfomib) / sizeof(cacheinfomib[0]), + &_cache_info, &clen, NULL, 0) == 0) { + _cachelinesize = _cache_info.dcache_line_size; + return _cachelinesize; + } + + /* Try older deprecated sysctl */ + clen = sizeof(_cachelinesize); + if (sysctl(cachemib, sizeof(cachemib) / sizeof(cachemib[0]), + &_cachelinesize, &clen, NULL, 0) < 0 + || !_cachelinesize) + abort(); + + _cache_info.dcache_size = _cachelinesize; + _cache_info.dcache_line_size = _cachelinesize; + _cache_info.icache_size = _cachelinesize; + _cache_info.icache_line_size = _cachelinesize; + + /* If there is no cache, indicate we have issued the sysctl. */ + if (!_cachelinesize) + _cachelinesize = 1; + + return _cachelinesize; +} +#endif + +void +__syncicache(void *from, size_t len) +{ + size_t l, off; + size_t linesz; + char *p; + +#if !defined(_KERNEL) && !defined(_STANDALONE) + if (!_cachelinesize) + __getcachelinesize(); +#endif + + if (CACHEINFO.dcache_size > 0) { + linesz = CACHEINFO.dcache_line_size; + off = (uintptr_t)from & (linesz - 1); + l = (len + off + linesz - 1) & ~(linesz - 1); + p = (char *)from - off; + do { + __asm volatile ("dcbst 0,%0" :: "r"(p)); + p += linesz; + } while ((l -= linesz) != 0); + } + __asm volatile ("sync"); + + if (CACHEINFO.icache_size > 0 ) { + linesz = CACHEINFO.icache_line_size; + off = (uintptr_t)from & (linesz - 1); + l = (len + off + linesz - 1) & ~(linesz - 1); + p = (char *)from - off; + do { + __asm volatile ("icbi 0,%0" :: "r"(p)); + p += linesz; + } while ((l -= linesz) != 0); + } + __asm volatile ("sync; isync"); +} diff --git a/lib/nbsd_libc/arch/powerpc/genassym.cf b/lib/nbsd_libc/arch/powerpc/genassym.cf new file mode 100644 index 000000000..5fb22774b --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/genassym.cf @@ -0,0 +1,48 @@ +# $NetBSD: genassym.cf,v 1.3 2011/01/18 01:23:24 matt Exp $ + +# +# Copyright (c) 2001 The NetBSD Foundation, Inc. +# All rights reserved. +# +# This code is derived from software contributed to The NetBSD Foundation +# by Matt Thomas . +# +# 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 + +define CALLFRAMELEN sizeof(struct callframe) +define CALLFRAME_LR offsetof(struct callframe, cf_lr) +define CALLFRAME_R30 offsetof(struct callframe, cf_r30) +define CALLFRAME_R31 offsetof(struct callframe, cf_r31) + +define UC_GREGS_R1 offsetof(ucontext_t, uc_mcontext.__gregs[_REG_R1]) +define UC_GREGS_R3 offsetof(ucontext_t, uc_mcontext.__gregs[_REG_R3]) +define UC_GREGS_PC offsetof(ucontext_t, uc_mcontext.__gregs[_REG_PC]) + +define SIG_BLOCK SIG_BLOCK +define SIG_SETMASK SIG_SETMASK diff --git a/lib/nbsd_libc/arch/powerpc/net/Makefile.inc b/lib/nbsd_libc/arch/powerpc/net/Makefile.inc new file mode 100644 index 000000000..80be36301 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/net/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.2 2003/05/12 09:32:10 kleink Exp $ + +SRCS+= htonl.c htons.c ntohl.c ntohs.c diff --git a/lib/nbsd_libc/arch/powerpc/softfloat/milieu.h b/lib/nbsd_libc/arch/powerpc/softfloat/milieu.h new file mode 100644 index 000000000..52c2bc3cf --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/softfloat/milieu.h @@ -0,0 +1,48 @@ +/* $NetBSD: milieu.h,v 1.1 2001/05/25 11:38:57 simonb Exp $ */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +Include common integer types and flags. +------------------------------------------------------------------------------- +*/ +#include "powerpc-gcc.h" + +/* +------------------------------------------------------------------------------- +Symbolic Boolean literals. +------------------------------------------------------------------------------- +*/ +enum { + FALSE = 0, + TRUE = 1 +}; diff --git a/lib/nbsd_libc/arch/powerpc/softfloat/powerpc-gcc.h b/lib/nbsd_libc/arch/powerpc/softfloat/powerpc-gcc.h new file mode 100644 index 000000000..9a1784211 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/softfloat/powerpc-gcc.h @@ -0,0 +1,89 @@ +/* $NetBSD: powerpc-gcc.h,v 1.2 2005/12/24 21:11:16 perry Exp $ */ + +/* +------------------------------------------------------------------------------- +One of the macros `BIGENDIAN' or `LITTLEENDIAN' must be defined. +------------------------------------------------------------------------------- +*/ +#include +#if _BYTE_ORDER == _BIG_ENDIAN +#define BIGENDIAN +#endif +#if _BYTE_ORDER == _LITTLE_ENDIAN +#define LITTLEENDIAN +#endif + +/* +------------------------------------------------------------------------------- +The macro `BITS64' can be defined to indicate that 64-bit integer types are +supported by the compiler. +------------------------------------------------------------------------------- +*/ +#define BITS64 + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines the most convenient type that holds +integers of at least as many bits as specified. For example, `uint8' should +be the most convenient type that can hold unsigned integers of as many as +8 bits. The `flag' type must be able to hold either a 0 or 1. For most +implementations of C, `flag', `uint8', and `int8' should all be `typedef'ed +to the same as `int'. +------------------------------------------------------------------------------- +*/ +typedef int flag; +typedef int uint8; +typedef int int8; +typedef int uint16; +typedef int int16; +typedef unsigned int uint32; +typedef signed int int32; +#ifdef BITS64 +typedef unsigned long long int uint64; +typedef signed long long int int64; +#endif + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines a type that holds integers +of _exactly_ the number of bits specified. For instance, for most +implementation of C, `bits16' and `sbits16' should be `typedef'ed to +`unsigned short int' and `signed short int' (or `short int'), respectively. +------------------------------------------------------------------------------- +*/ +typedef unsigned char bits8; +typedef signed char sbits8; +typedef unsigned short int bits16; +typedef signed short int sbits16; +typedef unsigned int bits32; +typedef signed int sbits32; +#ifdef BITS64 +typedef unsigned long long int bits64; +typedef signed long long int sbits64; +#endif + +#ifdef BITS64 +/* +------------------------------------------------------------------------------- +The `LIT64' macro takes as its argument a textual integer literal and +if necessary ``marks'' the literal as having a 64-bit integer type. +For example, the GNU C Compiler (`gcc') requires that 64-bit literals be +appended with the letters `LL' standing for `long long', which is `gcc's +name for the 64-bit integer type. Some compilers may allow `LIT64' to be +defined as the identity macro: `#define LIT64( a ) a'. +------------------------------------------------------------------------------- +*/ +#define LIT64( a ) a##LL +#endif + +/* +------------------------------------------------------------------------------- +The macro `INLINE' can be used before functions that should be inlined. If +a compiler does not support explicit inlining, this macro should be defined +to be `static'. +------------------------------------------------------------------------------- +*/ +#define INLINE static inline + +#define FLOAT64_DEMANGLE(a) (a) +#define FLOAT64_MANGLE(a) (a) diff --git a/lib/nbsd_libc/arch/powerpc/softfloat/softfloat.h b/lib/nbsd_libc/arch/powerpc/softfloat/softfloat.h new file mode 100644 index 000000000..d9c095b2c --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/softfloat/softfloat.h @@ -0,0 +1,303 @@ +/* $NetBSD: softfloat.h,v 1.5 2006/05/16 20:55:51 mrg Exp $ */ + +/* This is a derivative work. */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +The macro `FLOATX80' must be defined to enable the extended double-precision +floating-point format `floatx80'. If this macro is not defined, the +`floatx80' type will not be defined, and none of the functions that either +input or output the `floatx80' type will be defined. The same applies to +the `FLOAT128' macro and the quadruple-precision format `float128'. +------------------------------------------------------------------------------- +*/ +/* #define FLOATX80 */ +/* #define FLOAT128 */ + +#include + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point types. +------------------------------------------------------------------------------- +*/ +typedef unsigned int float32; +typedef unsigned long long float64; +#ifdef FLOATX80 +typedef struct { + unsigned short high; + unsigned long long low; +} floatx80; +#endif +#ifdef FLOAT128 +typedef struct { + unsigned long long high, low; +} float128; +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point underflow tininess-detection mode. +------------------------------------------------------------------------------- +*/ +#ifndef SOFTFLOAT_FOR_GCC +extern int8 float_detect_tininess; +#endif +enum { + float_tininess_after_rounding = 0, + float_tininess_before_rounding = 1 +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point rounding mode. +------------------------------------------------------------------------------- +*/ +extern fp_rnd float_rounding_mode; +enum { + float_round_nearest_even = FP_RN, + float_round_to_zero = FP_RZ, + float_round_down = FP_RM, + float_round_up = FP_RP +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point exception flags. +------------------------------------------------------------------------------- +*/ +extern fp_except float_exception_flags; +extern fp_except float_exception_mask; +enum { + float_flag_inexact = FP_X_IMP, + float_flag_underflow = FP_X_UFL, + float_flag_overflow = FP_X_OFL, + float_flag_divbyzero = FP_X_DZ, + float_flag_invalid = FP_X_INV +}; + +/* +------------------------------------------------------------------------------- +Routine to raise any or all of the software IEC/IEEE floating-point +exception flags. +------------------------------------------------------------------------------- +*/ +void float_raise( fp_except ); + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE integer-to-floating-point conversion routines. +------------------------------------------------------------------------------- +*/ +float32 int32_to_float32( int ); +float64 int32_to_float64( int ); +#ifdef FLOATX80 +floatx80 int32_to_floatx80( int ); +#endif +#ifdef FLOAT128 +float128 int32_to_float128( int ); +#endif +float32 int64_to_float32( long long ); +float64 int64_to_float64( long long ); +#ifdef FLOATX80 +floatx80 int64_to_floatx80( long long ); +#endif +#ifdef FLOAT128 +float128 int64_to_float128( long long ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float32_to_int32( float32 ); +int float32_to_int32_round_to_zero( float32 ); +unsigned int float32_to_uint32_round_to_zero( float32 ); +long long float32_to_int64( float32 ); +long long float32_to_int64_round_to_zero( float32 ); +float64 float32_to_float64( float32 ); +#ifdef FLOATX80 +floatx80 float32_to_floatx80( float32 ); +#endif +#ifdef FLOAT128 +float128 float32_to_float128( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision operations. +------------------------------------------------------------------------------- +*/ +float32 float32_round_to_int( float32 ); +float32 float32_add( float32, float32 ); +float32 float32_sub( float32, float32 ); +float32 float32_mul( float32, float32 ); +float32 float32_div( float32, float32 ); +float32 float32_rem( float32, float32 ); +float32 float32_sqrt( float32 ); +flag float32_eq( float32, float32 ); +flag float32_le( float32, float32 ); +flag float32_lt( float32, float32 ); +flag float32_eq_signaling( float32, float32 ); +flag float32_le_quiet( float32, float32 ); +flag float32_lt_quiet( float32, float32 ); +#ifndef SOFTFLOAT_FOR_GCC +flag float32_is_signaling_nan( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float64_to_int32( float64 ); +int float64_to_int32_round_to_zero( float64 ); +unsigned int float64_to_uint32_round_to_zero( float64 ); +long long float64_to_int64( float64 ); +long long float64_to_int64_round_to_zero( float64 ); +float32 float64_to_float32( float64 ); +#ifdef FLOATX80 +floatx80 float64_to_floatx80( float64 ); +#endif +#ifdef FLOAT128 +float128 float64_to_float128( float64 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision operations. +------------------------------------------------------------------------------- +*/ +float64 float64_round_to_int( float64 ); +float64 float64_add( float64, float64 ); +float64 float64_sub( float64, float64 ); +float64 float64_mul( float64, float64 ); +float64 float64_div( float64, float64 ); +float64 float64_rem( float64, float64 ); +float64 float64_sqrt( float64 ); +flag float64_eq( float64, float64 ); +flag float64_le( float64, float64 ); +flag float64_lt( float64, float64 ); +flag float64_eq_signaling( float64, float64 ); +flag float64_le_quiet( float64, float64 ); +flag float64_lt_quiet( float64, float64 ); +#ifndef SOFTFLOAT_FOR_GCC +flag float64_is_signaling_nan( float64 ); +#endif + +#ifdef FLOATX80 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int floatx80_to_int32( floatx80 ); +int floatx80_to_int32_round_to_zero( floatx80 ); +long long floatx80_to_int64( floatx80 ); +long long floatx80_to_int64_round_to_zero( floatx80 ); +float32 floatx80_to_float32( floatx80 ); +float64 floatx80_to_float64( floatx80 ); +#ifdef FLOAT128 +float128 floatx80_to_float128( floatx80 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision rounding precision. Valid +values are 32, 64, and 80. +------------------------------------------------------------------------------- +*/ +extern int floatx80_rounding_precision; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision operations. +------------------------------------------------------------------------------- +*/ +floatx80 floatx80_round_to_int( floatx80 ); +floatx80 floatx80_add( floatx80, floatx80 ); +floatx80 floatx80_sub( floatx80, floatx80 ); +floatx80 floatx80_mul( floatx80, floatx80 ); +floatx80 floatx80_div( floatx80, floatx80 ); +floatx80 floatx80_rem( floatx80, floatx80 ); +floatx80 floatx80_sqrt( floatx80 ); +flag floatx80_eq( floatx80, floatx80 ); +flag floatx80_le( floatx80, floatx80 ); +flag floatx80_lt( floatx80, floatx80 ); +flag floatx80_eq_signaling( floatx80, floatx80 ); +flag floatx80_le_quiet( floatx80, floatx80 ); +flag floatx80_lt_quiet( floatx80, floatx80 ); +flag floatx80_is_signaling_nan( floatx80 ); + +#endif + +#ifdef FLOAT128 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float128_to_int32( float128 ); +int float128_to_int32_round_to_zero( float128 ); +long long float128_to_int64( float128 ); +long long float128_to_int64_round_to_zero( float128 ); +float32 float128_to_float32( float128 ); +float64 float128_to_float64( float128 ); +#ifdef FLOATX80 +floatx80 float128_to_floatx80( float128 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision operations. +------------------------------------------------------------------------------- +*/ +float128 float128_round_to_int( float128 ); +float128 float128_add( float128, float128 ); +float128 float128_sub( float128, float128 ); +float128 float128_mul( float128, float128 ); +float128 float128_div( float128, float128 ); +float128 float128_rem( float128, float128 ); +float128 float128_sqrt( float128 ); +flag float128_eq( float128, float128 ); +flag float128_le( float128, float128 ); +flag float128_lt( float128, float128 ); +flag float128_eq_signaling( float128, float128 ); +flag float128_le_quiet( float128, float128 ); +flag float128_lt_quiet( float128, float128 ); +flag float128_is_signaling_nan( float128 ); + +#endif diff --git a/lib/nbsd_libc/arch/powerpc/stdlib/Makefile.inc b/lib/nbsd_libc/arch/powerpc/stdlib/Makefile.inc new file mode 100644 index 000000000..2f91b992a --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/31 20:40:00 dsl Exp $ diff --git a/lib/nbsd_libc/arch/powerpc/string/Makefile.inc b/lib/nbsd_libc/arch/powerpc/string/Makefile.inc new file mode 100644 index 000000000..5814c9a6b --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/string/Makefile.inc @@ -0,0 +1,24 @@ +# $NetBSD: Makefile.inc,v 1.13 2010/03/14 00:26:10 mrg Exp $ + +SRCS+= bzero.S ffs.S strlen.S +NO_SRCS+= memset.S + +# disable the asm versions of these because they break the explora. +# the special rules here are to override the suffix rules which seem +# to prefer .S files over .c +.if ${MACHINE} == "evbppc" +bcopy.o: bcopy.c +bcopy.po: bcopy.c +bcopy.pico: bcopy.c +memcpy.o: memcpy.c +memcpy.po: memcpy.c +memcpy.pico: memcpy.c +memcmp.o: memcmp.c +memcmp.po: memcmp.c +memcmp.pico: memcmp.c +memmove.o: memmove.c +memmove.po: memmove.c +memmove.pico: memmove.c +.else +SRCS+= memcmp.S bcopy.S memcpy.S memmove.S +.endif diff --git a/lib/nbsd_libc/arch/powerpc/string/bzero.S b/lib/nbsd_libc/arch/powerpc/string/bzero.S new file mode 100644 index 000000000..3e441e1bd --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/string/bzero.S @@ -0,0 +1,378 @@ +/* $NetBSD: bzero.S,v 1.10 2011/01/19 02:47:01 matt Exp $ */ + +/*- + * Copyright (C) 2001 Martin J. Laubach + * 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 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. + */ +/*----------------------------------------------------------------------*/ + +#include + + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: bzero.S,v 1.10 2011/01/19 02:47:01 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + +#ifdef _KERNEL +#include +#endif + +#define USE_STSWX 0 /* don't. slower than trivial copy loop */ + +/*----------------------------------------------------------------------*/ +/* + void bzero(void *b %r3, size_t len %r4); + void * memset(void *b %r3, int c %r4, size_t len %r5); +*/ +/*----------------------------------------------------------------------*/ + +#define r_dst %r3 +#define r_len %r4 +#define r_val %r0 + + .text + .align 4 +ENTRY(bzero) + li r_val, 0 /* Value to stuff in */ + b cb_memset +END(bzero) + +ENTRY(memset) + cmplwi cr1, %r5, 0 + mr. %r0, %r4 + mr %r8, %r3 + beqlr- cr1 /* Nothing to do */ + + rlwimi %r0, %r4, 8, 16, 23 /* word extend fill value */ + rlwimi %r0, %r0, 16, 0, 15 + mr %r4, %r5 + bne- simple_fill /* =! 0, use trivial fill */ +cb_memset: + +/*----------------------------------------------------------------------*/ +#ifndef _KERNEL + /* First find out cache line size */ + mflr %r9 +#ifdef PIC + PIC_GOTSETUP(%r10) + mtlr %r9 + lwz %r5,cache_info@got(%r10) +#else + lis %r5,cache_info@h + ori %r5,%r5,cache_info@l +#endif + lwz %r6, 4(%r5) + cmpwi %r6, -1 + bne+ cb_cacheline_known + +/*----------------------------------------------------------------------*/ +#define CTL_MACHDEP 7 +#define CPU_CACHELINE 1 +#define CPU_CACHEINFO 5 + +#define STKFRAME_SZ 64 +#define MIB 8 +#define OLDPLEN 16 +#define R3_SAVE 20 +#define R4_SAVE 24 +#define R0_SAVE 28 +#define R8_SAVE 32 +#define R31_SAVE 36 +#ifdef PIC +#define R30_SAVE 40 +#endif + + stw %r9, 4(%r1) + stwu %r1, -STKFRAME_SZ(%r1) + + stw %r31, R31_SAVE(%r1) + mr %r31, %r5 /* cache info */ + +#ifdef PIC + stw %r30, R30_SAVE(%r1) + PIC_TOCSETUP(cb_memset,%r30) +#endif + + stw %r8, R8_SAVE(%r1) + stw %r3, R3_SAVE(%r1) + stw %r4, R4_SAVE(%r1) + stw %r0, R0_SAVE(%r1) + + li %r0, CTL_MACHDEP /* Construct MIB */ + stw %r0, MIB(%r1) + li %r0, CPU_CACHEINFO + stw %r0, MIB+4(%r1) + + li %r0, 4*4 /* Oldlenp := 4*4 */ + stw %r0, OLDPLEN(%r1) + + addi %r3, %r1, MIB + li %r4, 2 /* namelen */ + /* %r5 already contains &cache_info */ + addi %r6, %r1, OLDPLEN + li %r7, 0 + li %r8, 0 + bl PIC_PLT(_C_LABEL(sysctl)) + + cmpwi %r3, 0 /* Check result */ + beq 1f + + /* Failure, try older sysctl */ + + li %r0, CTL_MACHDEP /* Construct MIB */ + stw %r0, MIB(%r1) + li %r0, CPU_CACHELINE + stw %r0, MIB+4(%r1) + + li %r0, 4 /* Oldlenp := 4 */ + stw %r0, OLDPLEN(%r1) + + addi %r3, %r1, MIB + li %r4, 2 /* namelen */ + addi %r5, %r31, 4 + addi %r6, %r1, OLDPLEN + li %r7, 0 + li %r8, 0 + bl PIC_PLT(_C_LABEL(sysctl)) +1: + lwz %r3, R3_SAVE(%r1) + lwz %r4, R4_SAVE(%r1) + lwz %r8, R8_SAVE(%r1) + lwz %r0, R0_SAVE(%r1) + lwz %r9, 4(%r31) + lwz %r31, R31_SAVE(%r1) +#ifdef PIC + lwz %r30, R30_SAVE(%r1) +#endif + addi %r1, %r1, STKFRAME_SZ + lwz %r0, 4(%r1) + mtlr %r0 + + cntlzw %r6, %r9 /* compute shift value */ + li %r5, 31 + subf %r5, %r6, %r5 + +#ifdef PIC + mflr %r9 + PIC_GOTSETUP(%r10) + mtlr %r9 + lwz %r6, cache_sh@got(%r10) + stw %r5, 0(%r6) +#else + lis %r6, cache_sh@ha + stw %r5, cache_sh@l(%r6) +#endif +/*----------------------------------------------------------------------*/ +/* Okay, we know the cache line size (%r9) and shift value (%r10) */ +cb_cacheline_known: +#ifdef PIC + lwz %r5, cache_info@got(%r10) + lwz %r9, 4(%r5) + lwz %r5, cache_sh@got(%r10) + lwz %r10, 0(%r5) +#else + lis %r9, cache_info+4@ha + lwz %r9, cache_info+4@l(%r9) + lis %r10, cache_sh@ha + lwz %r10, cache_sh@l(%r10) +#endif + +#else /* _KERNEL */ +#ifdef MULTIPROCESSOR + mfsprg %r10, 0 /* Get cpu_info pointer */ +#else + lis %r10, cpu_info_store@ha + addi %r10, %r10, cpu_info_store@l +#endif + lwz %r9, CPU_CI+4(%r10) /* Load D$ line size */ + cntlzw %r10, %r9 /* Calculate shift.. */ + li %r6, 31 + subf %r10, %r10, %r6 +#endif /* _KERNEL */ + /* Back in memory filling business */ + + cmplwi cr1, r_len, 0 /* Nothing to do? */ + add %r5, %r9, %r9 + cmplw r_len, %r5 /* <= 2*CL bytes to move? */ + beqlr- cr1 /* then do nothing */ + + blt+ simple_fill /* a trivial fill routine */ + + /* Word align the block, fill bytewise until dst even*/ + + andi. %r5, r_dst, 0x03 + li %r6, 4 + beq+ cb_aligned_w /* already aligned to word? */ + + subf %r5, %r5, %r6 /* bytes to fill to align4 */ +#if USE_STSWX + mtxer %r5 + stswx %r0, 0, r_dst + add r_dst, %r5, r_dst +#else + mtctr %r5 + + subi r_dst, r_dst, 1 +1: stbu r_val, 1(r_dst) /* Fill bytewise */ + bdnz 1b + + addi r_dst, r_dst, 1 +#endif + subf r_len, %r5, r_len + +cb_aligned_w: /* Cache block align, fill wordwise until dst aligned */ + + /* I know I have something to do since we had > 2*CL initially */ + /* so no need to check for r_len = 0 */ + + subi %r6, %r9, 1 /* CL mask */ + and. %r5, r_dst, %r6 + srwi %r5, %r5, 2 + srwi %r6, %r9, 2 + beq cb_aligned_cb /* already on CL boundary? */ + + subf %r5, %r5, %r6 /* words to fill to alignment */ + mtctr %r5 + slwi %r5, %r5, 2 + subf r_len, %r5, r_len + + subi r_dst, r_dst, 4 +1: stwu r_val, 4(r_dst) /* Fill wordwise */ + bdnz 1b + addi r_dst, r_dst, 4 + +cb_aligned_cb: /* no need to check r_len, see above */ + + srw. %r5, r_len, %r10 /* Number of cache blocks */ + mtctr %r5 + beq cblocks_done + + slw %r5, %r5, %r10 + subf r_len, %r5, r_len + +1: dcbz 0, r_dst /* Clear blockwise */ + add r_dst, r_dst, %r9 + bdnz 1b + +cblocks_done: /* still CL aligned, but less than CL bytes left */ + cmplwi cr1, r_len, 0 + cmplwi r_len, 8 + beq- cr1, sf_return + + blt- sf_bytewise /* <8 remaining? */ + b sf_aligned_w + +/*----------------------------------------------------------------------*/ +wbzero: li r_val, 0 + + cmplwi r_len, 0 + beqlr- /* Nothing to do */ + +simple_fill: +#if USE_STSWX + cmplwi cr1, r_len, 12 /* < 12 bytes to move? */ +#else + cmplwi cr1, r_len, 8 /* < 8 bytes to move? */ +#endif + andi. %r5, r_dst, 0x03 /* bytes to fill to align4 */ + blt cr1, sf_bytewise /* trivial byte mover */ + + li %r6, 4 + subf %r5, %r5, %r6 + beq+ sf_aligned_w /* dest is word aligned */ + +#if USE_STSWX + mtxer %r5 + stswx %r0, 0, r_dst + add r_dst, %r5, r_dst +#else + mtctr %r5 /* nope, then fill bytewise */ + subi r_dst, r_dst, 1 /* until it is */ +1: stbu r_val, 1(r_dst) + bdnz 1b + + addi r_dst, r_dst, 1 +#endif + subf r_len, %r5, r_len + +sf_aligned_w: /* no need to check r_len since it were >= 8 bytes initially */ +#if USE_STSWX + mr %r6, %r0 + mr %r7, %r0 + + srwi %r5, r_len, 3 + mtctr %r5 + + slwi %r5, %r5, 3 /* adjust len */ + subf. r_len, %r5, r_len + +1: stswi %r6, r_dst, 8 + addi r_dst, r_dst, 8 + bdnz 1b +#else + srwi %r5, r_len, 2 /* words to fill */ + mtctr %r5 + + slwi %r5, %r5, 2 + subf. r_len, %r5, r_len /* adjust len for fill */ + + subi r_dst, r_dst, 4 +1: stwu r_val, 4(r_dst) + bdnz 1b + addi r_dst, r_dst, 4 +#endif + +sf_word_done: bne- sf_bytewise + +sf_return: mr %r3, %r8 /* restore orig ptr */ + blr /* for memset functionality */ + +sf_bytewise: +#if USE_STSWX + mr %r5, %r0 + mr %r6, %r0 + mr %r7, %r0 + + mtxer r_len + stswx %r5, 0, r_dst +#else + mtctr r_len + + subi r_dst, r_dst, 1 +1: stbu r_val, 1(r_dst) + bdnz 1b +#endif + mr %r3, %r8 /* restore orig ptr */ + blr /* for memset functionality */ +END(memset) + +/*----------------------------------------------------------------------*/ +#ifndef _KERNEL + .data +cache_info: .long -1, -1, -1, -1 +cache_sh: .long 0 + +#endif +/*----------------------------------------------------------------------*/ diff --git a/lib/nbsd_libc/arch/powerpc/sys/__clone.S b/lib/nbsd_libc/arch/powerpc/sys/__clone.S new file mode 100644 index 000000000..3ede8dcb4 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/__clone.S @@ -0,0 +1,73 @@ +/* $NetBSD: __clone.S,v 1.4 2011/01/16 02:43:10 matt Exp $ */ + +/*- + * Copyright (c) 2001 Tsubai Masanari. 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 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. + */ + +#include +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __clone.S,v 1.4 2011/01/16 02:43:10 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + /* + * Sanity checks: func and stack may not be NULL. + */ + cmpwi %r3,0 + beq 1f + cmpwi %r4,0 + beq 1f + + mr %r7,%r3 /* Save fn in r7. */ + mr %r3,%r5 + _DOSYSCALL(__clone) /* (flags, stack) */ + bso 2f /* error... */ + + cmpwi %r3,0 + bnelr /* We're the parent, just return. */ + + mtlr %r7 /* fn */ + mr %r3,%r6 /* arg */ + blrl /* Call the clone's entry point. */ + +#ifdef PIC + PIC_TOCSETUP(__clone, %r30) /* exit won't return so blow away r30 */ +#endif + bl PIC_PLT(_C_LABEL(_exit)) + +1: + li %r3,EINVAL +2: + b _C_LABEL(__cerror) +END(__clone) diff --git a/lib/nbsd_libc/arch/powerpc/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/powerpc/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..71f97406a --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/__sigaction14_sigtramp.c @@ -0,0 +1,82 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.9 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.9 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) +#ifdef __LIBC12_SOURCE__ +extern const int __sigtramp_sigcontext_1[] + __weak_reference(__sigtramp_sigcontext_1); +#endif + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specificed or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/powerpc/sys/__sigtramp2.S b/lib/nbsd_libc/arch/powerpc/sys/__sigtramp2.S new file mode 100644 index 000000000..132b347c5 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/__sigtramp2.S @@ -0,0 +1,53 @@ +/* $NetBSD: __sigtramp2.S,v 1.3 2011/01/15 07:31:12 matt Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigtramp2.S,v 1.3 2011/01/15 07:31:12 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + +/* + * On entry to user space, stack and registers look like: + * + * r3 signal number + * r4 pointer to siginfo structure + * r5 pointer to ucontext structure + * r30 pointer to ucontext structure + * srr0 address of handler + * lr address of this trampoline + * + */ +ENTRY_NOPROFILE(__sigtramp_siginfo_2) + mr %r3,%r30 /* restore ucontext pointer */ + _DOSYSCALL(setcontext) /* restore machine state */ + _DOSYSCALL(exit) /* or exit with errno if failed */ +END(__sigtramp_siginfo_2) diff --git a/lib/nbsd_libc/arch/powerpc/sys/__syscall.S b/lib/nbsd_libc/arch/powerpc/sys/__syscall.S new file mode 100644 index 000000000..d5ac44741 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/__syscall.S @@ -0,0 +1,5 @@ +/* $NetBSD: __syscall.S,v 1.1 2000/12/13 07:50:48 matt Exp $ */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/powerpc/sys/__vfork14.S b/lib/nbsd_libc/arch/powerpc/sys/__vfork14.S new file mode 100644 index 000000000..c2894d053 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/__vfork14.S @@ -0,0 +1,16 @@ +/* $NetBSD: __vfork14.S,v 1.4 2011/01/15 07:31:12 matt Exp $ */ + +/* + * pid = vfork(); + * + * r4 == 0 in parent process, 1 in child process. + * r3 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +SYSCALL(__vfork14) + addi %r4,%r4,-1 # from 1 to 0 in child, 0 to -1 in parent + and %r3,%r3,%r4 # return 0 in child, pid in parent + blr +END(__vfork14) diff --git a/lib/nbsd_libc/arch/powerpc/sys/brk.S b/lib/nbsd_libc/arch/powerpc/sys/brk.S new file mode 100644 index 000000000..fff9dd0a6 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/brk.S @@ -0,0 +1,50 @@ +/* $NetBSD: brk.S,v 1.12 2011/01/16 02:43:10 matt Exp $ */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: brk.S,v 1.12 2011/01/16 02:43:10 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(__minbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data +_C_LABEL(__minbrk): + .long _C_LABEL(_end) # XXX not used yet + + .text +ENTRY(_brk) +#if defined(PIC) + mflr %r10 + PIC_GOTSETUP(%r9) + mtlr %r10 + lwz %r5,_C_LABEL(_end)@got(%r9) +#else + lis %r5,_C_LABEL(_end)@ha # r5 = &_end + addi %r5,%r5,_C_LABEL(_end)@l +#endif + cmplw %r5,%r3 # if (&_end <= r3) + bgt 0f + mr %r5,%r3 # r5 = r3 +0: + mr %r3,%r5 # new break value + _DOSYSCALL(break) # assume, that r5 is kept + bso 1f +#ifdef PIC + lwz %r6,_C_LABEL(__curbrk)@got(%r9) + stw %r5,0(%r6) +#else + lis %r6,_C_LABEL(__curbrk)@ha # record new break + stw %r5,_C_LABEL(__curbrk)@l(%r6) +#endif + blr # return 0 + +1: + b _C_LABEL(__cerror) +END(_brk) diff --git a/lib/nbsd_libc/arch/powerpc/sys/cerror.S b/lib/nbsd_libc/arch/powerpc/sys/cerror.S new file mode 100644 index 000000000..9e86f75ed --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/cerror.S @@ -0,0 +1,52 @@ +/* $NetBSD: cerror.S,v 1.8 2011/01/16 02:43:10 matt Exp $ */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: cerror.S,v 1.8 2011/01/16 02:43:10 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + +#ifndef _REENTRANT + .globl _C_LABEL(errno) +#endif +#ifdef PIC + .hidden _C_LABEL(__cerror) +#endif + +ENTRY(__cerror) +#ifdef _REENTRANT + mflr %r0 + stwu %r1,-CALLFRAMELEN(%r1) # allocate new stack frame + stw %r0,CALLFRAMELEN+CALLFRAME_LR(%r1) +#ifdef PIC + stw %r30,CALLFRAME_R30(%r1) + PIC_TOCSETUP(__cerror, %r30) +#endif + stw %r31,CALLFRAME_R31(%r1) + mr %r31,%r3 # stash away in callee-saved register + bl PIC_PLT(_C_LABEL(__errno)) + stw %r31,0(%r3) + lwz %r0,CALLFRAMELEN+CALLFRAME_LR(%r1) + lwz %r31,CALLFRAME_R31(%r1) +#ifdef PIC + lwz %r30,CALLFRAME_R30(%r1) +#endif + mtlr %r0 + addi %r1,%r1,CALLFRAMELEN +#else +#ifdef PIC + mflr %r10 + PIC_GOTSETUP(%r4) + lwz %r4,_C_LABEL(errno)@got(%r4) + stw %r3,0(%r4) + mtlr %r10 +#else + lis %r4,_C_LABEL(errno)@ha + stw %r3,_C_LABEL(errno)@l(%r4) +#endif /* PIC */ +#endif /* _REENTRANT */ + li %r3,-1 + li %r4,-1 + blr +END(__cerror) diff --git a/lib/nbsd_libc/arch/powerpc/sys/exect.S b/lib/nbsd_libc/arch/powerpc/sys/exect.S new file mode 100644 index 000000000..362cbf6ea --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/exect.S @@ -0,0 +1,5 @@ +/* $NetBSD: exect.S,v 1.3 1998/05/25 15:28:03 ws Exp $ */ + +#include "SYS.h" + +PSEUDO(exect,execve) diff --git a/lib/nbsd_libc/arch/powerpc/sys/fork.S b/lib/nbsd_libc/arch/powerpc/sys/fork.S new file mode 100644 index 000000000..c442b8241 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/fork.S @@ -0,0 +1,16 @@ +/* $NetBSD: fork.S,v 1.6 2011/01/15 07:31:13 matt Exp $ */ + +/* + * pid = fork(); + * + * r4 == 0 in parent process, 1 in child process. + * r3 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +_SYSCALL(__fork,fork) + addi %r4,%r4,-1 # from 1 to 0 in child, 0 to -1 in parent + and %r3,%r3,%r4 # return 0 in child, pid in parent + blr +END(__fork) diff --git a/lib/nbsd_libc/arch/powerpc/sys/getcontext.S b/lib/nbsd_libc/arch/powerpc/sys/getcontext.S new file mode 100644 index 000000000..9ebd65b59 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/getcontext.S @@ -0,0 +1,53 @@ +/* $NetBSD: getcontext.S,v 1.5 2011/01/15 07:31:13 matt Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getcontext.S,v 1.5 2011/01/15 07:31:13 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +ENTRY(_getcontext) + mr %r5,%r3 # must save pointer + _DOSYSCALL(getcontext) + bso 1f + mflr %r4 + stw %r4,UC_GREGS_PC(%r5) # saved pc <- lr + stw %r3,UC_GREGS_R3(%r5) # arrange for return value 0 + blr +1: + b _C_LABEL(__cerror) +END(_getcontext) diff --git a/lib/nbsd_libc/arch/powerpc/sys/pipe.S b/lib/nbsd_libc/arch/powerpc/sys/pipe.S new file mode 100644 index 000000000..7bce8c70e --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/pipe.S @@ -0,0 +1,23 @@ +/* $NetBSD: pipe.S,v 1.8 2011/01/15 07:31:13 matt Exp $ */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: pipe.S,v 1.8 2011/01/15 07:31:13 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +ENTRY(_pipe) + mr %r5,%r3 # save pointer + _DOSYSCALL(pipe) # assume, that r5 is kept + bso 1f + stw %r3,0(%r5) # success, store fds + stw %r4,4(%r5) + li %r3,0 + blr # and return 0 +1: + b _C_LABEL(__cerror) +END(_pipe) diff --git a/lib/nbsd_libc/arch/powerpc/sys/ptrace.S b/lib/nbsd_libc/arch/powerpc/sys/ptrace.S new file mode 100644 index 000000000..3b9571d37 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/ptrace.S @@ -0,0 +1,61 @@ +/* $NetBSD: ptrace.S,v 1.6 2011/01/16 02:43:10 matt Exp $ */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ptrace.S,v 1.6 2011/01/16 02:43:10 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + +#define XCALLFRAMELEN ((((2+5)*SZREG) + CALLFRAMELEN - 1) & -CALLFRAMELEN) +#define XCALLFRAME_DATA (XCALLFRAMELEN - 1*SZREG) +#define XCALLFRAME_ADDR (XCALLFRAMELEN - 2*SZREG) +#define XCALLFRAME_PID (XCALLFRAMELEN - 3*SZREG) +#define XCALLFRAME_REQUEST (XCALLFRAMELEN - 4*SZREG) +#define XCALLFRAME_R30 (XCALLFRAMELEN - 5*SZREG) + +ENTRY(ptrace) +#ifdef _REENTRANT + mflr %r0 + stw %r0,CALLFRAME_LR(%r1) + stwu %r1,-XCALLFRAMELEN(%r1) + stw %r3,XCALLFRAME_REQUEST(%r1) + stw %r4,XCALLFRAME_PID(%r1) + stw %r5,XCALLFRAME_ADDR(%r1) + stw %r6,XCALLFRAME_DATA(%r1) +#ifdef PIC + stw %r30,XCALLFRAME_R30(%r1) + PIC_TOCSETUP(ptrace, %r30) +#endif + + bl PIC_PLT(_C_LABEL(__errno)) + li %r7,0 + stw %r7,0(%r3) + +#ifdef PIC + lwz %r30,XCALLFRAME_R30(%r1) +#endif + lwz %r6,XCALLFRAME_DATA(%r1) + lwz %r4,XCALLFRAME_PID(%r1) + lwz %r5,XCALLFRAME_ADDR(%r1) + lwz %r3,XCALLFRAME_REQUEST(%r1) + addi %r1,%r1,XCALLFRAMELEN + lwz %r0,CALLFRAME_LR(%r1) + mtlr %r0 +#else +#ifdef PIC + mflr %r0 + PIC_GOTSETUP(%r7) + lwz %r7,_C_LABEL(errno)@got(%r7) + li %r8,0 + stw %r8,0(%r7) + mtlr 0 +#else + lis %r7,_C_LABEL(errno)@ha + li %r8,0 + stw %r8,_C_LABEL(errno)@l(%r7) +#endif /* PIC */ +#endif /* _REENTRANT */ + _DOSYSCALL(ptrace) + bnslr + b _C_LABEL(__cerror) diff --git a/lib/nbsd_libc/arch/powerpc/sys/sbrk.S b/lib/nbsd_libc/arch/powerpc/sys/sbrk.S new file mode 100644 index 000000000..ad2e25714 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/sbrk.S @@ -0,0 +1,41 @@ +/* $NetBSD: sbrk.S,v 1.11 2011/01/16 02:43:10 matt Exp $ */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sbrk.S,v 1.11 2011/01/16 02:43:10 matt Exp $") +#endif /* LIBC_SCCS && !lint */ + + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +_C_LABEL(__curbrk): + .long _C_LABEL(_end) + .text + +ENTRY(_sbrk) +#ifdef PIC + mflr %r10 + PIC_GOTSETUP(%r5) + mtlr %r10 + lwz %r5,_C_LABEL(__curbrk)@got(%r5) + lwz %r6,0(%r5) +#else + lis %r5,_C_LABEL(__curbrk)@ha + lwzu %r6,_C_LABEL(__curbrk)@l(%r5) # r6 = old break, r5 = &curbrk +#endif + add %r3,%r3,%r6 + mr %r7,%r3 # r7 = new break + _DOSYSCALL(break) # break(new_break) + bso 1f + mr %r3,%r6 # set return value + stw %r7,0(%r5) # record new break + blr +1: + b _C_LABEL(__cerror) +END(_sbrk) diff --git a/lib/nbsd_libc/arch/powerpc/sys/shmat.S b/lib/nbsd_libc/arch/powerpc/sys/shmat.S new file mode 100644 index 000000000..b72046bf8 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:53 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/powerpc/sys/syscall.S b/lib/nbsd_libc/arch/powerpc/sys/syscall.S new file mode 100644 index 000000000..ffc225900 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc/sys/syscall.S @@ -0,0 +1,5 @@ +/* $NetBSD: syscall.S,v 1.2 1998/05/25 15:28:04 ws Exp $ */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/powerpc64/Makefile.inc b/lib/nbsd_libc/arch/powerpc64/Makefile.inc new file mode 100644 index 000000000..4cf8f6337 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/07/07 06:56:34 ross Exp $ + +KMINCLUDES= +KMSRCS= + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S diff --git a/lib/nbsd_libc/arch/powerpc64/SYS.h b/lib/nbsd_libc/arch/powerpc64/SYS.h new file mode 100644 index 000000000..7b7c8c15b --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/SYS.h @@ -0,0 +1,43 @@ +/* $NetBSD: SYS.h,v 1.2 2010/03/22 02:17:23 mrg Exp $ */ + +#include +#include + +#ifdef __STDC__ +#define _DOSYSCALL(x) li %r0,(SYS_ ## x) ;\ + sc +#else +#define _DOSYSCALL(x) li %r0,(SYS_/**/x) ;\ + sc +#endif /* __STDC__ */ + +#define _SYSCALL_NOERROR(x,y) .text ;\ + .align 2 ;\ + ENTRY(x) ;\ + _DOSYSCALL(y) + +#define _SYSCALL(x,y) .text ;\ + .align 2 ;\ + 2: b PIC_PLT(_C_LABEL(__cerror));\ + nop ;\ + _SYSCALL_NOERROR(x,y) ;\ + bso 2b + +#define SYSCALL_NOERROR(x) _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) _SYSCALL(x,x) + +#define PSEUDO_NOERROR(x,y) _SYSCALL_NOERROR(x,y) ;\ + blr + +#define PSEUDO(x,y) _SYSCALL_NOERROR(x,y) ;\ + bnslr ;\ + b PIC_PLT(_C_LABEL(__cerror));\ + nop + +#define RSYSCALL_NOERROR(x) PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) PSEUDO(x,x) + +#define WSYSCALL(weak,strong) WEAK_ALIAS(weak,strong) ;\ + PSEUDO(strong,weak) diff --git a/lib/nbsd_libc/arch/powerpc64/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/powerpc64/gdtoa/Makefile.inc new file mode 100644 index 000000000..98cc03a7d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/07/01 16:37:20 ross Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/powerpc64/gdtoa/arith.h b/lib/nbsd_libc/arch/powerpc64/gdtoa/arith.h new file mode 100644 index 000000000..6ea59e97f --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#define IEEE_BIG_ENDIAN diff --git a/lib/nbsd_libc/arch/powerpc64/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/powerpc64/gdtoa/gd_qnan.h new file mode 100644 index 000000000..a37738ff2 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gdtoa/gd_qnan.h @@ -0,0 +1,5 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x7ff80000 +#define d_QNAN1 0x0 diff --git a/lib/nbsd_libc/arch/powerpc64/gen/Makefile.inc b/lib/nbsd_libc/arch/powerpc64/gen/Makefile.inc new file mode 100644 index 000000000..36713d375 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/Makefile.inc @@ -0,0 +1,27 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= __setjmp14.S __sigsetjmp14.S _setjmp.S +SRCS+= bswap16.c bswap32.c bswap64.c +SRCS+= fabs.c flt_rounds.c +SRCS+= syncicache.c +SRCS+= _lwp.c makecontext.c resumecontext.c swapcontext.S + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_dbl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +.if (${MKSOFTFLOAT} == "no") +SRCS+= fpgetround.c fpsetround.c fpgetmask.c fpsetmask.c +SRCS+= fpgetsticky.c fpsetsticky.c +.endif + +LSRCS.powerpc.gen=Lint_swapcontext.c +LSRCS+= ${LSRCS.powerpc.gen} +DPSRCS+= ${LSRCS.powerpc.gen} +CLEANFILES+= ${LSRCS.powerpc.gen} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/__setjmp14.S b/lib/nbsd_libc/arch/powerpc64/gen/__setjmp14.S new file mode 100644 index 000000000..f4b6af187 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/__setjmp14.S @@ -0,0 +1,101 @@ +/* $NetBSD: __setjmp14.S,v 1.2 2009/03/08 18:57:12 he Exp $ */ + +#include + +#include + +#if defined(LIBC_SCCS) + .text + .asciz "$NetBSD: __setjmp14.S,v 1.2 2009/03/08 18:57:12 he Exp $" +#endif + +/* + * C library -- _setjmp, _longjmp + * + * longjmp(a,v) + * will generate a "return(v?v:1)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__setjmp14) + mr %r6,%r3 + li %r3,1 # SIG_BLOCK + li %r4,0 + addi %r5,%r6,100 # &sigmask + li %r0,SYS___sigprocmask14 + sc # assume no error XXX + mflr %r11 + mfcr %r12 + mr %r10,%r1 + mr %r9,%r2 + + std %r8,8(%r6) # save r8-r31 + std %r9,16(%r6) + std %r10,24(%r6) + std %r11,32(%r6) + std %r12,40(%r6) + std %r13,48(%r6) + std %r14,56(%r6) + std %r15,64(%r6) + std %r16,72(%r6) + std %r17,80(%r6) + std %r18,88(%r6) + std %r19,96(%r6) + std %r20,104(%r6) + std %r21,112(%r6) + std %r22,120(%r6) + std %r23,128(%r6) + std %r24,136(%r6) + std %r25,144(%r6) + std %r26,152(%r6) + std %r27,160(%r6) + std %r28,168(%r6) + std %r29,176(%r6) + std %r30,184(%r6) + std %r31,192(%r6) + + li %r3,0 + blr + +ENTRY(__longjmp14) + ld %r8,8(%r6) # load r8-r31 + ld %r9,16(%r6) + ld %r10,24(%r6) + ld %r11,32(%r6) + ld %r12,40(%r6) + ld %r13,48(%r6) + ld %r14,56(%r6) + ld %r15,64(%r6) + ld %r16,72(%r6) + ld %r17,80(%r6) + ld %r18,88(%r6) + ld %r19,96(%r6) + ld %r20,104(%r6) + ld %r21,112(%r6) + ld %r22,120(%r6) + ld %r23,128(%r6) + ld %r24,136(%r6) + ld %r25,144(%r6) + ld %r26,152(%r6) + ld %r27,160(%r6) + ld %r28,168(%r6) + ld %r29,176(%r6) + ld %r30,184(%r6) + ld %r31,192(%r6) + + mr %r6,%r4 + mtlr %r11 + mtcr %r12 + mr %r2,%r9 + mr %r1,%r10 + addi %r4,%r3,100 # &sigmask + li %r3,3 # SIG_SETMASK + li %r5,0 + li %r0,SYS___sigprocmask14 + sc # assume no error XXX + or. %r3,%r6,%r6 + bnelr + li %r3,1 + blr diff --git a/lib/nbsd_libc/arch/powerpc64/gen/__sigsetjmp14.S b/lib/nbsd_libc/arch/powerpc64/gen/__sigsetjmp14.S new file mode 100644 index 000000000..cb6d9743c --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/__sigsetjmp14.S @@ -0,0 +1,55 @@ +/* $NetBSD: __sigsetjmp14.S,v 1.2 2010/03/22 01:39:11 mrg Exp $ */ + +#include +#include + +#if defined(LIBC_SCCS) + .text + .asciz "$NetBSD: __sigsetjmp14.S,v 1.2 2010/03/22 01:39:11 mrg Exp $" +#endif + +ENTRY(__sigsetjmp14) + mr %r6,%r3 + or. %r7,%r4,%r4 + beq 1f + li %r3,1 # SIG_BLOCK + li %r4,0 + addi %r5,%r6,100 # &sigmask + li %r0,SYS___sigprocmask14 + sc # assume no error XXX +1: + mflr %r11 + mfcr %r12 + mr %r10,%r1 + mr %r9,%r2 + i = 0 + .rept 32-7 + std 7+i,i*8(%r6) # save r7-r31 + i = i + 1 + .endr + li %r3,0 + blr + +ENTRY(__siglongjmp14) + i = 0 + .rept 32-7 + ld 7+i,i*8(%r3) # load r7-r31 + i = i + 1 + .endr + mr %r6,%r4 + mtlr %r11 + mtcr %r12 + mr %r2,%r9 + mr %r1,%r10 + or. %r7,%r7,%r7 + beq 1f + addi %r4,%r3,100 # &sigmask + li %r3,3 # SIG_SETMASK + li %r5,0 + li %r0,SYS___sigprocmask14 + sc # assume no error XXX +1: + or. %r3,%r6,%r6 + bnelr + li %r3,1 + blr diff --git a/lib/nbsd_libc/arch/powerpc64/gen/_lwp.c b/lib/nbsd_libc/arch/powerpc64/gen/_lwp.c new file mode 100644 index 000000000..406f743f3 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/_lwp.c @@ -0,0 +1,67 @@ +/* $NetBSD: _lwp.c,v 1.2 2006/07/05 18:05:45 ross Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Allen Briggs for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.2 2006/07/05 18:05:45 ross Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), void *arg, + void *private, caddr_t stack_base, size_t stack_size) +{ + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + sp = (void **) (stack_base + stack_size); + + u->uc_mcontext.__gregs[3] = (__greg_t) arg; /* arg1 */ + u->uc_mcontext.__gregs[1] = ((__greg_t) sp) - 112; /* stack */ + u->uc_mcontext.__gregs[33] = (__greg_t) _lwp_exit; /* LR */ + u->uc_mcontext.__gregs[34] = (__greg_t) start; /* PC */ +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/_setjmp.S b/lib/nbsd_libc/arch/powerpc64/gen/_setjmp.S new file mode 100644 index 000000000..3d8da049a --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/_setjmp.S @@ -0,0 +1,46 @@ +/* $NetBSD: _setjmp.S,v 1.2 2006/07/05 18:06:47 ross Exp $ */ + +#include + +#if defined(LIBC_SCCS) + .text + .asciz "$NetBSD: _setjmp.S,v 1.2 2006/07/05 18:06:47 ross Exp $" +#endif + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v?v:1)" from the last call to + * _setjmp(a) + * by restoring registers from the stack. + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + mflr %r11 /* save return address */ + mfcr %r12 /* save condition register */ + mr %r10,%r1 /* save stack pointer */ + mr %r9,%r2 /* save GPR2 (not needed) */ + i = 0 + .rept 32-9 + std 9+i,i*8+16(%r3) + i = i + 1 + .endr + li %r3,0 /* indicate success */ + blr /* return */ + +ENTRY(_longjmp) + i = 0 + .rept 32-9 + ld 9+i,i*8+16(%r3) + i = i + 1 + .endr + mtlr %r11 /* restore LR */ + mtcr %r12 /* restore CR */ + mr %r2,%r9 /* restore GPR2 (not needed) */ + mr %r1,%r10 /* restore stack */ + or. %r3,%r4,%r4 /* get return value */ + bnelr /* return if not 0 */ + li %r3,1 /* what's the point? */ + blr /* return */ diff --git a/lib/nbsd_libc/arch/powerpc64/gen/fabs.c b/lib/nbsd_libc/arch/powerpc64/gen/fabs.c new file mode 100644 index 000000000..4cc21a901 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/fabs.c @@ -0,0 +1,15 @@ +/* $NetBSD: fabs.c,v 1.2 2006/07/05 18:07:22 ross Exp $ */ + +#include + +double +fabs(double x) +{ +#ifdef _SOFT_FLOAT + if (x < 0) + x = -x; +#else + __asm volatile("fabs %0,%1" : "=f"(x) : "f"(x)); +#endif + return (x); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/flt_rounds.c b/lib/nbsd_libc/arch/powerpc64/gen/flt_rounds.c new file mode 100644 index 000000000..13834cb2d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/flt_rounds.c @@ -0,0 +1,62 @@ +/* $NetBSD: flt_rounds.c,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.1 2006/07/01 16:37:20 ross Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 0, /* round to zero */ + 2, /* round to positive infinity */ + 3 /* round to negative infinity */ +}; + +int +__flt_rounds(void) +{ +#ifdef _SOFT_FLOAT + return map[fpgetround()]; +#else + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return map[((uint32_t)fpscr & FPSCR_RN)]; +#endif +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/fpgetmask.c b/lib/nbsd_libc/arch/powerpc64/gen/fpgetmask.c new file mode 100644 index 000000000..5e44e336c --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/fpgetmask.c @@ -0,0 +1,57 @@ +/* $NetBSD: fpgetmask.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetmask.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetmask,_fpgetmask) +#endif + +#define MASKBITS (FPSCR_VE|FPSCR_OE|FPSCR_UE|FPSCR_ZE|FPSCR_XE) +#define MASKSHFT 3 + +fp_except +fpgetmask(void) +{ + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return (((uint32_t)fpscr & MASKBITS) >> MASKSHFT); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/fpgetround.c b/lib/nbsd_libc/arch/powerpc64/gen/fpgetround.c new file mode 100644 index 000000000..8cc346ee7 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/fpgetround.c @@ -0,0 +1,57 @@ +/* $NetBSD: fpgetround.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetround,_fpgetround) +#endif + +#define ROUNDBITS FPSCR_RN +#define ROUNDSHFT 0 + +fp_rnd +fpgetround(void) +{ + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return (((uint32_t)fpscr & ROUNDBITS) >> ROUNDSHFT); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/fpgetsticky.c b/lib/nbsd_libc/arch/powerpc64/gen/fpgetsticky.c new file mode 100644 index 000000000..f46aa8387 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/fpgetsticky.c @@ -0,0 +1,57 @@ +/* $NetBSD: fpgetsticky.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetsticky.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpgetsticky,_fpgetsticky) +#endif + +#define STICKYBITS (FPSCR_XX|FPSCR_ZX|FPSCR_UX|FPSCR_OX|FPSCR_VX) +#define STICKYSHFT 25 + +fp_except +fpgetsticky(void) +{ + uint64_t fpscr; + + __asm volatile("mffs %0" : "=f"(fpscr)); + return (((uint32_t)fpscr & STICKYBITS) >> STICKYSHFT); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/fpsetmask.c b/lib/nbsd_libc/arch/powerpc64/gen/fpsetmask.c new file mode 100644 index 000000000..0ede2542b --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/fpsetmask.c @@ -0,0 +1,62 @@ +/* $NetBSD: fpsetmask.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetmask.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetmask,_fpsetmask) +#endif + +#define MASKBITS (FPSCR_XE|FPSCR_ZE|FPSCR_UE|FPSCR_OE|FPSCR_VE) +#define MASKSHFT 3 + +fp_except +fpsetmask(fp_except mask) +{ + uint64_t fpscr; + fp_except old; + + __asm volatile("mffs %0" : "=f"(fpscr)); + old = ((uint32_t)fpscr & MASKBITS) >> MASKSHFT; + fpscr &= ~MASKBITS; + fpscr |= ((uint32_t)mask << MASKSHFT) & MASKBITS; + __asm volatile("mtfsf 0xff,%0" :: "f"(fpscr)); + return (old); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/fpsetround.c b/lib/nbsd_libc/arch/powerpc64/gen/fpsetround.c new file mode 100644 index 000000000..90fe8297a --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/fpsetround.c @@ -0,0 +1,61 @@ +/* $NetBSD: fpsetround.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fpsetround,_fpsetround) +#endif + +#define ROUNDBITS FPSCR_RN + +fp_rnd +fpsetround(fp_rnd rnd_dir) +{ + uint64_t fpscr; + fp_rnd old; + + __asm volatile("mffs %0" : "=f"(fpscr)); + old = (uint32_t)fpscr & ROUNDBITS; + fpscr &= ~ROUNDBITS; + fpscr |= rnd_dir & ROUNDBITS; + __asm volatile("mtfsf 0xff,%0" :: "f"(fpscr)); + return (old); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/fpsetsticky.c b/lib/nbsd_libc/arch/powerpc64/gen/fpsetsticky.c new file mode 100644 index 000000000..30e1bd855 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/fpsetsticky.c @@ -0,0 +1,84 @@ +/* $NetBSD: fpsetsticky.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/* + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dan Winship. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetsticky.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#define STICKYBITS (FPSCR_XX|FPSCR_ZX|FPSCR_UX|FPSCR_OX|FPSCR_VX) +#define INVBITS (FPSCR_VXCVI|FPSCR_VXSQRT|FPSCR_VXSOFT|FPSCR_VXVC|\ + FPSCR_VXIMZ|FPSCR_VXZDZ|FPSCR_VXIDI|FPSCR_VXISI|\ + FPSCR_VXSNAN) +#define STICKYSHFT 25 + +#ifdef __weak_alias +__weak_alias(fpsetsticky,_fpsetsticky) +#endif + +fp_except +fpsetsticky(fp_except mask) +{ + uint64_t fpscr; + fp_except old; + + __asm volatile("mffs %0" : "=f"(fpscr)); + old = ((uint32_t)fpscr & STICKYBITS) >> STICKYSHFT; + /* + * FPSCR_VX (aka FP_X_INV) is not a sticky bit but a summary of the + * all the FPSCR_VX* sticky bits. So when FP_X_INV is cleared then + * clear all of those bits, likewise when it's set, set them all. + */ + if ((mask & FP_X_INV) == 0) + fpscr &= ~INVBITS; + else + fpscr |= INVBITS; + fpscr &= ~STICKYBITS; + fpscr |= ((uint32_t)mask << STICKYSHFT) & STICKYBITS; + /* + * Make FPSCR_FX reflect the presence of a set sticky bit (or not). + */ + if (fpscr & (STICKYBITS|INVBITS)) + fpscr |= FPSCR_FX; + else + fpscr &= ~FPSCR_FX; + /* + * Write back the fpscr. + */ + __asm volatile("mtfsf 0xff,%0" :: "f"(fpscr)); + return (old); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/makecontext.c b/lib/nbsd_libc/arch/powerpc64/gen/makecontext.c new file mode 100644 index 000000000..fbfe1ecdd --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/makecontext.c @@ -0,0 +1,82 @@ +/* $NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#if __STDC__ +#include +#else +#include +#endif + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + __greg_t sp, *spp; + int i; + va_list ap; + + sp = (__greg_t) ((uintptr_t)ucp->uc_stack.ss_sp + + ucp->uc_stack.ss_size); + sp -= 2 + (argc > 8 ? argc - 8: 0); /* Make room for call frame. */ + sp = (__greg_t) ((uintptr_t)sp & ~0xf); + + /* + * Start executing at -- when completes, return to + * <_resumecontext>. + */ + gr[_REG_R1] = sp; + gr[_REG_LR] = (__greg_t)_resumecontext; + gr[_REG_PC] = (__greg_t)func; // XXX -- this is the descriptor address! + + /* Wipe out stack frame backchain pointer. */ + spp = (__greg_t *)sp; + *spp = 0; + + /* Construct argument list. */ + va_start(ap, argc); + /* Up to the first eight arguments are passed in r3-10. */ + for (i = 0; i < argc && i < 8; i++) + gr[_REG_R3 + i] = va_arg(ap, long); + /* Pass remaining arguments on the stack above the backchain/lr gap. */ + for (spp += 2; i < argc; i++) + *spp++ = va_arg(ap, long); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/nanf.c b/lib/nbsd_libc/arch/powerpc64/gen/nanf.c new file mode 100644 index 000000000..5007de780 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.2 2009/02/22 01:34:02 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.2 2009/02/22 01:34:02 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0x7f, 0xc0, 0, 0 } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/powerpc64/gen/resumecontext.c b/lib/nbsd_libc/arch/powerpc64/gen/resumecontext.c new file mode 100644 index 000000000..7baafea1e --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/powerpc64/gen/swapcontext.S b/lib/nbsd_libc/arch/powerpc64/gen/swapcontext.S new file mode 100644 index 000000000..a3d7b9c77 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/swapcontext.S @@ -0,0 +1,62 @@ +/* $NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + .text + .asciz "$NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $" +#endif /* LIBC_SCCS && !lint */ + +ENTRY(swapcontext) + mc_off = 56 # offset of uc_mcontext + stdu %r1,-SF_SZ(%r1) # set up new stack frame + mflr %r0 + std %r0,[SF_SZ+SF_LR](%r1) # save link register + std %r3,SF_PARAM(%r1) # must save oucp (was 8) + std %r4,SF_PARAM+8(%r1) # must save ucp (was 12) + bl ._getcontext # getcontext(oucp) + nop + cmpdi %r3,0 + bne 1f + ld %r11,SF_PARAM(%r1) # load oucp for adjustment + ld %r0,SF_SZ+SF_LR(%r1) # get LR back + std %r0,mc_off+34*8(%r11) # pc <- lr + la %r0,16(%r1) + std %r0,mc_off+1*8(%r11) # adjust sp + ld %r3,SF_PARAM+8(%r1) # load ucp + bl .setcontext # setcontext(ucp) + nop +1: + ld %r0,SF_SZ+SF_LR(%r1) + mtlr %r0 + la %r1,SF_SZ(%r1) + blr diff --git a/lib/nbsd_libc/arch/powerpc64/gen/syncicache.c b/lib/nbsd_libc/arch/powerpc64/gen/syncicache.c new file mode 100644 index 000000000..2e73ae724 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/gen/syncicache.c @@ -0,0 +1,138 @@ +/* $NetBSD: syncicache.c,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +/* + * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank. + * Copyright (C) 1995-1997, 1999 TooLs GmbH. + * 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 by TooLs GmbH. + * 4. The name of TooLs GmbH may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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 +#if defined(_KERNEL) || defined(_STANDALONE) +#include +#include +#include +#endif +#include + +#include + + +#if defined(_STANDALONE) +#ifndef CACHELINESIZE +#error "Must know the size of a cache line" +#endif +static struct cache_info _cache_info = { + CACHELINESIZE, + CACHELINESIZE, + CACHELINESIZE, + CACHELINESIZE +}; +#define CACHEINFO _cache_info +#elif defined(_KERNEL) +#define CACHEINFO (curcpu()->ci_ci) +#else +#include + +size_t __getcachelinesize (void); + +static int _cachelinesize = 0; + +static struct cache_info _cache_info; +#define CACHEINFO _cache_info + +size_t +__getcachelinesize(void) +{ + static int cachemib[] = { CTL_MACHDEP, CPU_CACHELINE }; + static int cacheinfomib[] = { CTL_MACHDEP, CPU_CACHEINFO }; + size_t clen = sizeof(_cache_info); + + if (_cachelinesize) + return _cachelinesize; + + if (sysctl(cacheinfomib, sizeof(cacheinfomib) / sizeof(cacheinfomib[0]), + &_cache_info, &clen, NULL, 0) == 0) { + _cachelinesize = _cache_info.dcache_line_size; + return _cachelinesize; + } + + /* Try older deprecated sysctl */ + clen = sizeof(_cachelinesize); + if (sysctl(cachemib, sizeof(cachemib) / sizeof(cachemib[0]), + &_cachelinesize, &clen, NULL, 0) < 0 + || !_cachelinesize) + abort(); + + _cache_info.dcache_size = _cachelinesize; + _cache_info.dcache_line_size = _cachelinesize; + _cache_info.icache_size = _cachelinesize; + _cache_info.icache_line_size = _cachelinesize; + + /* If there is no cache, indicate we have issued the sysctl. */ + if (!_cachelinesize) + _cachelinesize = 1; + + return _cachelinesize; +} +#endif + +void +__syncicache(void *from, size_t len) +{ + size_t l, off; + size_t linesz; + char *p; + +#if !defined(_KERNEL) && !defined(_STANDALONE) + if (!_cachelinesize) + __getcachelinesize(); +#endif + + if (CACHEINFO.dcache_size > 0) { + linesz = CACHEINFO.dcache_line_size; + off = (uintptr_t)from & (linesz - 1); + l = (len + off + linesz - 1) & ~(linesz - 1); + p = (char *)from - off; + do { + __asm volatile ("dcbst 0,%0" :: "r"(p)); + p += linesz; + } while ((l -= linesz) != 0); + } + __asm volatile ("sync"); + + if (CACHEINFO.icache_size > 0 ) { + linesz = CACHEINFO.icache_line_size; + off = (uintptr_t)from & (linesz - 1); + l = (len + off + linesz - 1) & ~(linesz - 1); + p = (char *)from - off; + do { + __asm volatile ("icbi 0,%0" :: "r"(p)); + p += linesz; + } while ((l -= linesz) != 0); + } + __asm volatile ("sync; isync"); +} diff --git a/lib/nbsd_libc/arch/powerpc64/net/Makefile.inc b/lib/nbsd_libc/arch/powerpc64/net/Makefile.inc new file mode 100644 index 000000000..74baf8322 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/net/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/07/01 16:37:20 ross Exp $ + +SRCS+= htonl.c htons.c ntohl.c ntohs.c diff --git a/lib/nbsd_libc/arch/powerpc64/stdlib/Makefile.inc b/lib/nbsd_libc/arch/powerpc64/stdlib/Makefile.inc new file mode 100644 index 000000000..f12f78d35 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.3 2009/07/31 20:40:00 dsl Exp $ diff --git a/lib/nbsd_libc/arch/powerpc64/string/Makefile.inc b/lib/nbsd_libc/arch/powerpc64/string/Makefile.inc new file mode 100644 index 000000000..b724ef5f9 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/string/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.6 2009/08/11 17:30:43 dsl Exp $ + +SRCS+= bzero.S ffs.S strlen.S +NO_SRCS+= memset.S diff --git a/lib/nbsd_libc/arch/powerpc64/string/bzero.S b/lib/nbsd_libc/arch/powerpc64/string/bzero.S new file mode 100644 index 000000000..096725fdf --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/string/bzero.S @@ -0,0 +1,100 @@ +/* $NetBSD: bzero.S,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Ross Harvey. + * + * 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 + + .file "bzero.c" +ENTRY(bzero) + mr 5, 4 # translate to memcpy signature + li 4, 0 # add missing memcpy param +ENTRY(memset) + cmpldi 7, 5, 7 # only optimize if len >= 8 + mr 9, 3 + ble 7, 2f + rldicl. 0, 3, 0, 61 # only optimize if aligned + beq 0, 4f +2: + cmpdi 7, 5, 0 + beqlr 7 +3: + mtctr 5 + rldicl 0, 4, 0, 56 + .p2align 4,, 15 +2: + stb 0, 0(9) # byte-at-a-time loop + addi 9, 9, 1 + bdnz 2b + blr # done +4: + rldicl 4, 4, 0, 56 # construct parallel store + cmpldi 7, 5, 31 + mr 10, 3 + sldi 0, 4, 8 + or 0, 0, 4 + sldi 9, 0, 16 + or 0, 0, 9 + sldi 11, 0, 32 + or 0, 0, 11 + ble 7, 5f + addi 9, 5, -32 + srdi 9, 9, 5 + addi 9, 9, 1 + mtctr 9 + .p2align 4,, 15 +2: + std 0, 0(10) # 8-way + unrolled store loop + std 0, 8(10) + addi 5, 5, -32 # schedule count decrement + std 0, 16(10) + std 0, 24(10) + addi 10, 10, 32 + bdnz 2b + cmpldi 7, 5, 7 + ble 7, 8f +5: + addi 9, 5, -8 + srdi 9, 9, 3 + addi 9, 9, 1 + mtctr 9 + .p2align 4,, 15 +14: + std 0, 0(10) # 8-way not-unrolled loop + addi 5, 5, -8 + addi 10, 10, 8 + bdnz 14b +8: + cmpdi 7, 5, 0 + mr 9, 10 + bne 7, 3b + blr + .long 0 + .byte 0, 0, 0, 0, 0, 0, 0, 0 + .size .memset, .-.memset diff --git a/lib/nbsd_libc/arch/powerpc64/sys/Makefile.inc b/lib/nbsd_libc/arch/powerpc64/sys/Makefile.inc new file mode 100644 index 000000000..7d5f09434 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/07/07 06:40:45 ross Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S diff --git a/lib/nbsd_libc/arch/powerpc64/sys/__clone.S b/lib/nbsd_libc/arch/powerpc64/sys/__clone.S new file mode 100644 index 000000000..46e980691 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/__clone.S @@ -0,0 +1,66 @@ +/* $NetBSD: __clone.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +/*- + * Copyright (c) 2001 Tsubai Masanari. 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 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. + */ + +#include +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + /* + * Sanity checks: func and stack may not be NULL. + */ + cmpdi %r3,0 + beq 1f + cmpdi %r4,0 + beq 1f + + mr %r7,%r3 /* Save fn in r7. */ + mr %r3,%r5 + li %r0,SYS___clone /* (flags, stack) */ + sc + bso 2f /* error... */ + + cmpdi %r3,0 + bnelr /* We're the parent, just return. */ + + mtlr %r7 /* fn */ + mr %r3,%r6 /* arg */ + blrl /* Call the clone's entry point. */ + + bl PIC_PLT(_C_LABEL(_exit)) + +1: + li %r3,EINVAL +2: + b PIC_PLT(_C_LABEL(__cerror)) diff --git a/lib/nbsd_libc/arch/powerpc64/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/powerpc64/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..092c2bc27 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/__sigaction14_sigtramp.c @@ -0,0 +1,82 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) +#ifdef __LIBC12_SOURCE__ +extern const int __sigtramp_sigcontext_1[] + __weak_reference(__sigtramp_sigcontext_1); +#endif + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specificed or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/powerpc64/sys/__sigtramp2.S b/lib/nbsd_libc/arch/powerpc64/sys/__sigtramp2.S new file mode 100644 index 000000000..e43c4d45d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/__sigtramp2.S @@ -0,0 +1,48 @@ +/* $NetBSD: __sigtramp2.S,v 1.3 2011/01/17 08:23:54 matt Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +/* + * On entry to user space, stack and registers look like: + * + * r3 signal number + * r4 pointer to siginfo structure + * r5 pointer to ucontext structure + * r30 pointer to ucontext structure + * srr0 address of handler + * lr address of this trampoline + * + */ +ENTRY_NOPROFILE(__sigtramp_siginfo_2) + mr %r3,%r30 /* restore ucontext pointer */ + _DOSYSCALL(setcontext) /* restore machine state */ + _DOSYSCALL(exit) /* or exit with errno if failed */ diff --git a/lib/nbsd_libc/arch/powerpc64/sys/__syscall.S b/lib/nbsd_libc/arch/powerpc64/sys/__syscall.S new file mode 100644 index 000000000..076066d7d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/__syscall.S @@ -0,0 +1,5 @@ +/* $NetBSD: __syscall.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/powerpc64/sys/__vfork14.S b/lib/nbsd_libc/arch/powerpc64/sys/__vfork14.S new file mode 100644 index 000000000..1b1469fd1 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/__vfork14.S @@ -0,0 +1,15 @@ +/* $NetBSD: __vfork14.S,v 1.1 2006/07/06 17:04:22 ross Exp $ */ + +/* + * pid = vfork(); + * + * r4 == 0 in parent process, 1 in child process. + * r3 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +SYSCALL(__vfork14) + addi %r4,%r4,-1 # from 1 to 0 in child, 0 to -1 in parent + and %r3,%r3,%r4 # return 0 in child, pid in parent + blr diff --git a/lib/nbsd_libc/arch/powerpc64/sys/brk.S b/lib/nbsd_libc/arch/powerpc64/sys/brk.S new file mode 100644 index 000000000..6eb574d1b --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/brk.S @@ -0,0 +1,40 @@ +/* $NetBSD: brk.S,v 1.3 2006/07/07 06:50:06 ross Exp $ */ + +#include "SYS.h" + + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(__minbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .macro LdAddr r,sym + .pushsection ".toc","aw" +100: .tc \sym[TC],\sym + .popsection + ld \r,100b@toc(2) + .endm + + .data +_C_LABEL(__minbrk): + .long _C_LABEL(_end) + .text + +ENTRY(_brk) + LdAddr %r5,_end + cmpld %r5,%r3 # if (&_end <= r3) + bgt 0f + mr %r5,%r3 # r5 = r3 +0: + mr %r3,%r5 # new break value + li %r0,SYS_break + sc # assume, that r5 is kept + bso 1f + LdAddr %r6,__curbrk + std %r5,0(%r6) + blr # return 0 + +1: + b .__cerror diff --git a/lib/nbsd_libc/arch/powerpc64/sys/cerror.S b/lib/nbsd_libc/arch/powerpc64/sys/cerror.S new file mode 100644 index 000000000..e360b691d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/cerror.S @@ -0,0 +1,38 @@ +/* $NetBSD: cerror.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#include +#include "SYS.h" + +#ifndef _REENTRANT + .globl _C_LABEL(errno) +#endif + +ENTRY(__cerror) +#ifdef _REENTRANT + mflr %r0 + stwu %r1,-16(%r1) # allocate new stack frame + stw %r0,20(%r1) + stw %r31,8(%r1) + mr %r31,%r3 # stash away in callee-saved register + bl PIC_PLT(_C_LABEL(__errno)) + stw %r31,0(%r3) + lwz %r0,20(%r1) + lwz %r31,8(%r1) + mtlr %r0 + la %r1,16(%r1) +#else +#ifdef PIC + mflr %r10 + bl _GLOBAL_OFFSET_TABLE_@local-4 + mflr %r4 + lwz %r4,_C_LABEL(errno)@got(%r4) + stw %r3,0(%r4) + mtlr %r10 +#else + lis %r4,_C_LABEL(errno)@ha + stw %r3,_C_LABEL(errno)@l(%r4) +#endif /* PIC */ +#endif /* _REENTRANT */ + li %r3,-1 + li %r4,-1 + blr diff --git a/lib/nbsd_libc/arch/powerpc64/sys/exect.S b/lib/nbsd_libc/arch/powerpc64/sys/exect.S new file mode 100644 index 000000000..197230950 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/exect.S @@ -0,0 +1,5 @@ +/* $NetBSD: exect.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#include "SYS.h" + +PSEUDO(exect,execve) diff --git a/lib/nbsd_libc/arch/powerpc64/sys/fork.S b/lib/nbsd_libc/arch/powerpc64/sys/fork.S new file mode 100644 index 000000000..0e1b6584f --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/fork.S @@ -0,0 +1,15 @@ +/* $NetBSD: fork.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +/* + * pid = fork(); + * + * r4 == 0 in parent process, 1 in child process. + * r3 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +_SYSCALL(__fork,fork) + addi %r4,%r4,-1 # from 1 to 0 in child, 0 to -1 in parent + and %r3,%r3,%r4 # return 0 in child, pid in parent + blr diff --git a/lib/nbsd_libc/arch/powerpc64/sys/getcontext.S b/lib/nbsd_libc/arch/powerpc64/sys/getcontext.S new file mode 100644 index 000000000..a718dc529 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/getcontext.S @@ -0,0 +1,53 @@ +/* $NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + .text + .asciz "$NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $" +#endif /* SYSLIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +ENTRY(_getcontext) + mr %r5,%r3 # must save pointer + li %r0,SYS_getcontext + sc + bso 1f + mflr %r4 + stw %r4,(48 + 34 * 4)(%r5) # saved pc <- lr + stw %r3,(48 + 3 * 4)(%r5) # arrange for return value 0 + blr +1: + b .__cerror diff --git a/lib/nbsd_libc/arch/powerpc64/sys/pipe.S b/lib/nbsd_libc/arch/powerpc64/sys/pipe.S new file mode 100644 index 000000000..224453a68 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/pipe.S @@ -0,0 +1,19 @@ +/* $NetBSD: pipe.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +ENTRY(_pipe) + mr %r5,%r3 # save pointer + li %r0,SYS_pipe + sc # assume, that r5 is kept + bso 1f + stw %r3,0(%r5) # success, store fds + stw %r4,4(%r5) + li %r3,0 + blr # and return 0 +1: + b PIC_PLT(_C_LABEL(__cerror)) diff --git a/lib/nbsd_libc/arch/powerpc64/sys/ptrace.S b/lib/nbsd_libc/arch/powerpc64/sys/ptrace.S new file mode 100644 index 000000000..161535dfc --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/ptrace.S @@ -0,0 +1,46 @@ +/* $NetBSD: ptrace.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#include "SYS.h" + +ENTRY(ptrace) +#ifdef _REENTRANT + mflr %r0 + stwu %r1,-32(%r1) + stw %r0,36(%r1) + stw %r3,8(%r1) + stw %r4,12(%r1) + stw %r5,16(%r1) + stw %r6,20(%r1) + + bl PIC_PLT(_C_LABEL(__errno)) + li %r7,0 + stw %r7,0(%r3) + + lwz %r3,8(%r1) + lwz %r4,12(%r1) + lwz %r5,16(%r1) + lwz %r0,36(%r1) + lwz %r6,20(%r1) + mtlr %r0 + la %r1,32(%r1) +#else +#ifdef PIC + mflr %r0 + bl _GLOBAL_OFFSET_TABLE_@local-4 + mflr %r7 + lwz %r7,_C_LABEL(errno)@got(%r7) + li %r8,0 + stw %r8,0(%r7) + mtlr 0 +#else + lis %r7,_C_LABEL(errno)@ha + li %r8,0 + stw %r8,_C_LABEL(errno)@l(%r7) +#endif /* PIC */ +#endif /* _REENTRANT */ + li %r0,SYS_ptrace + sc + bso 1f + blr +1: + b PIC_PLT(_C_LABEL(__cerror)) diff --git a/lib/nbsd_libc/arch/powerpc64/sys/sbrk.S b/lib/nbsd_libc/arch/powerpc64/sys/sbrk.S new file mode 100644 index 000000000..75d9b2ee7 --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/sbrk.S @@ -0,0 +1,37 @@ +/* $NetBSD: sbrk.S,v 1.3 2006/07/07 06:50:06 ross Exp $ */ + +#include "SYS.h" + + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + + .data +_C_LABEL(__curbrk): + .long _C_LABEL(_end) + .text + + .macro LdAddr r,sym + .pushsection ".toc","aw" +100: .tc \sym[TC],\sym + .popsection + ld \r,100b@toc(2) + .endm + +ENTRY(_sbrk) + ldAddr %r6,__curbrk + add %r3,%r3,%r6 + mr %r7,%r3 # r7 = new break + li %r0,SYS_break + sc # break(new_break) + bso 1f + mr %r3,%r6 # set return value + LdAddr %r6,__curbrk + std %r7,0(%r6) + blr +1: + b PIC_PLT(_C_LABEL(__cerror)) diff --git a/lib/nbsd_libc/arch/powerpc64/sys/shmat.S b/lib/nbsd_libc/arch/powerpc64/sys/shmat.S new file mode 100644 index 000000000..92f9beccc --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/powerpc64/sys/syscall.S b/lib/nbsd_libc/arch/powerpc64/sys/syscall.S new file mode 100644 index 000000000..f30fb7c1d --- /dev/null +++ b/lib/nbsd_libc/arch/powerpc64/sys/syscall.S @@ -0,0 +1,5 @@ +/* $NetBSD: syscall.S,v 1.1 2006/07/01 16:37:20 ross Exp $ */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/sh3/Makefile.inc b/lib/nbsd_libc/arch/sh3/Makefile.inc new file mode 100644 index 000000000..e97ee9792 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.6 2006/06/17 18:04:23 uwe Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +CPPFLAGS+= -DSOFTFLOAT # -DSOFTFLOAT_NEED_FIXUNS +.include diff --git a/lib/nbsd_libc/arch/sh3/SYS.h b/lib/nbsd_libc/arch/sh3/SYS.h new file mode 100644 index 000000000..91e856cc1 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/SYS.h @@ -0,0 +1,126 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)SYS.h 5.5 (Berkeley) 5/7/91 + * $NetBSD: SYS.h,v 1.9 2006/01/06 06:19:20 uwe Exp $ + */ + +#include +#include + +#ifdef __STDC__ +#define SYSTRAP(x) \ + mov.l 903f, r0; \ + .long 0xc380; /* trapa #0x80 */ \ + bra 904f; \ + nop; \ + .align 2; \ + 903: .long (SYS_ ## x); \ + 904: +#else +#define SYSTRAP(x) \ + mov.l 903f, r0; \ + trapa #0x80; \ + bra 904f; \ + nop; \ + .align 2; \ + 903: .long (SYS_/**/x); \ + 904: +#endif + +#define _SYSCALL_NOERROR(x,y) \ + ENTRY(x); \ + SYSTRAP(y) + +#ifdef PIC + +#define JUMP_CERROR \ + mov r0, r4; \ + mov.l 912f, r1; \ + mova 912f, r0; \ + mov.l 913f, r2; \ + add r1, r0; \ + mov.l @(r0, r2), r3; \ + jmp @r3; \ + nop; \ + .align 2; \ + 912: .long _GLOBAL_OFFSET_TABLE_; \ + 913: .long PIC_GOT(cerror) + +#else /* !PIC */ + +#define JUMP_CERROR \ + mov.l 912f, r3; \ + jmp @r3; \ + mov r0, r4; \ + .align 2; \ + 912: .long cerror + +#endif /* !PIC */ + +#define _SYSCALL(x,y) \ + .text; \ + 911: JUMP_CERROR; \ + _SYSCALL_NOERROR(x,y); \ + bf 911b + +#define SYSCALL_NOERROR(x) \ + _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) \ + _SYSCALL(x,x) + +#define PSEUDO_NOERROR(x,y) \ + _SYSCALL_NOERROR(x,y); \ + rts; \ + nop + +#define PSEUDO(x,y) \ + _SYSCALL(x,y); \ + rts; \ + nop + +#define RSYSCALL_NOERROR(x) \ + PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) \ + PSEUDO(x,x) + +#ifdef WEAK_ALIAS +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) +#else +#define WSYSCALL(weak,strong) \ + PSEUDO(weak,weak) +#endif + + .globl cerror diff --git a/lib/nbsd_libc/arch/sh3/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/sh3/gdtoa/Makefile.inc new file mode 100644 index 000000000..1d786e010 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/sh3/gdtoa/arith.h b/lib/nbsd_libc/arch/sh3/gdtoa/arith.h new file mode 100644 index 000000000..40d3ed1fa --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gdtoa/arith.h @@ -0,0 +1,9 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#include + +#if BYTE_ORDER == BIG_ENDIAN +#define IEEE_BIG_ENDIAN +#else +#define IEEE_LITTLE_ENDIAN +#endif diff --git a/lib/nbsd_libc/arch/sh3/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/sh3/gdtoa/gd_qnan.h new file mode 100644 index 000000000..a7b54a203 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gdtoa/gd_qnan.h @@ -0,0 +1,12 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#include + +#define f_QNAN 0x7fa00000 +#if BYTE_ORDER == BIG_ENDIAN +#define d_QNAN0 0x7ff40000 +#define d_QNAN1 0x0 +#else +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff40000 +#endif diff --git a/lib/nbsd_libc/arch/sh3/gen/Makefile.inc b/lib/nbsd_libc/arch/sh3/gen/Makefile.inc new file mode 100644 index 000000000..651ada0b6 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/Makefile.inc @@ -0,0 +1,26 @@ +# $NetBSD: Makefile.inc,v 1.25 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= setjmp.S _setjmp.S sigsetjmp.S + +SRCS+= makecontext.c resumecontext.c swapcontext.S + +SRCS+= _lwp.c + +SRCS+= fabs.c flt_rounds.c bswap16.c bswap32.c bswap64.c + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_dbl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +LSRCS.sh3.gen= Lint__setjmp.c Lint___setjmp14.c Lint___sigsetjmp14.c \ + Lint_swapcontext.c + +LSRCS+= ${LSRCS.sh3.gen} +DPSRCS+= ${LSRCS.sh3.gen} +CLEANFILES+= ${LSRCS.sh3.gen} diff --git a/lib/nbsd_libc/arch/sh3/gen/_lwp.c b/lib/nbsd_libc/arch/sh3/gen/_lwp.c new file mode 100644 index 000000000..84d16472c --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/_lwp.c @@ -0,0 +1,67 @@ +/* $NetBSD: _lwp.c,v 1.4 2005/06/12 05:21:26 lukem Exp $ */ + +/* + * Copyright (c) 2002 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Jason R. Thorpe for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.4 2005/06/12 05:21:26 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + sp = (void **) (stack_base + stack_size); + + u->uc_mcontext.__gregs[_REG_R4] = (__greg_t) arg; + u->uc_mcontext.__gregs[_REG_SP] = ((__greg_t) sp) & ~3; + u->uc_mcontext.__gregs[_REG_PR] = (__greg_t) _lwp_exit; + u->uc_mcontext.__gregs[_REG_PC] = (__greg_t) start; +} diff --git a/lib/nbsd_libc/arch/sh3/gen/_setjmp.S b/lib/nbsd_libc/arch/sh3/gen/_setjmp.S new file mode 100644 index 000000000..81a022dc1 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/_setjmp.S @@ -0,0 +1,89 @@ +/* $NetBSD: _setjmp.S,v 1.7 2006/01/05 02:04:41 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)_setjmp.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: _setjmp.S,v 1.7 2006/01/05 02:04:41 uwe Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from the last call to + * _setjmp(a) + * by restoring registers from the stack. + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + add #((_JB_HAS_MASK + 1) * 4), r4 + mov #0, r0 + mov.l r0, @-r4 /* no saved signal mask */ + mov.l r15, @-r4 + mov.l r14, @-r4 + mov.l r13, @-r4 + mov.l r12, @-r4 + mov.l r11, @-r4 + mov.l r10, @-r4 + mov.l r9, @-r4 + mov.l r8, @-r4 + sts.l pr, @-r4 + rts + xor r0, r0 + SET_ENTRY_SIZE(_setjmp) + +ENTRY(_longjmp) + lds.l @r4+, pr + mov.l @r4+, r8 + mov.l @r4+, r9 + mov.l @r4+, r10 + mov.l @r4+, r11 + mov.l @r4+, r12 + mov.l @r4+, r13 + mov.l @r4+, r14 + mov.l @r4+, r15 + + mov r5, r0 + tst r0, r0 + bf .L0 + add #1, r0 +.L0: + rts + nop + SET_ENTRY_SIZE(_longjmp) diff --git a/lib/nbsd_libc/arch/sh3/gen/fabs.c b/lib/nbsd_libc/arch/sh3/gen/fabs.c new file mode 100644 index 000000000..f0738d445 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/fabs.c @@ -0,0 +1,46 @@ +/* $NetBSD: fabs.c,v 1.3 2006/05/21 02:51:15 mrg Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * + * 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 by Mark Brinicombe + * 4. 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 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. + */ + +/* + * fabs(x) returns the absolute value of x. + */ + +double fabs(double x); + +double +fabs(double x) +{ + if (x < 0) + x = -x; + return(x); +} diff --git a/lib/nbsd_libc/arch/sh3/gen/flt_rounds.c b/lib/nbsd_libc/arch/sh3/gen/flt_rounds.c new file mode 100644 index 000000000..936d8f4c4 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/flt_rounds.c @@ -0,0 +1,81 @@ +/* $NetBSD: flt_rounds.c,v 1.4 2007/01/17 23:24:22 hubertf Exp $ */ + +/* + * Copyright (c) 1996 Mark Brinicombe + * 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 by Mark Brinicombe + * for the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.4 2007/01/17 23:24:22 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 2, /* round to positive infinity */ + 3, /* round to negative infinity */ + 0 /* round to zero */ +}; + +/* + * Return the current FP rounding mode + * + * Returns: + * 0 - round to zero + * 1 - round to nearest + * 2 - round to postive infinity + * 3 - round to negative infinity + * + * ok all we need to do is get the current FP rounding mode + * index our map table and return the appropriate value. + * + * HOWEVER: + * The ARM FPA codes the rounding mode into the actual FP instructions + * so there is no such thing as a global rounding mode. + * The default is round to nearest if rounding is not explicitly specified. + * FP instructions generated by GCC will not explicitly specify a rounding + * mode. + * + * So the best we can do it to return the rounding mode FP instructions + * use if rounding is not specified which is round to nearest. + * + * This could change in the future with new floating point emulators or + * soft float FP libraries. + */ + +extern int __flt_rounds __P((void)); + +int +__flt_rounds() +{ + return(map[fpgetround()]); +} diff --git a/lib/nbsd_libc/arch/sh3/gen/makecontext.c b/lib/nbsd_libc/arch/sh3/gen/makecontext.c new file mode 100644 index 000000000..1c55e4500 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/makecontext.c @@ -0,0 +1,71 @@ +/* $NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + int i; + unsigned int *sp; + va_list ap; + + /* Compute and align stack pointer. */ + sp = (unsigned int *) + (((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) & ~3); + /* Allocate necessary stack space for arguments exceeding r4-7. */ + if (argc > 4) + sp -= argc - 4; + gr[_REG_SP] = (__greg_t)sp; + /* Arrange for return via the trampoline code. */ + gr[_REG_PR] = (__greg_t)_resumecontext; + gr[_REG_PC] = (__greg_t)func; + + va_start(ap, argc); + /* Pass up to four arguments in r4-7. */ + for (i = 0; i < argc && i < 4; i++) + gr[_REG_R4 - i] = va_arg(ap, int); /* sic! see mcontext.h */ + /* Pass any additional arguments on the stack. */ + for (argc -= i; argc > 0; argc--) + *sp++ = va_arg(ap, int); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/sh3/gen/nanf.c b/lib/nbsd_libc/arch/sh3/gen/nanf.c new file mode 100644 index 000000000..6fbda2885 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/nanf.c @@ -0,0 +1,19 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = +#if BYTE_ORDER == BIG_ENDIAN + { { 0x7f, 0xa0, 0, 0 } }; +#else + { { 0, 0, 0xa0, 0x7f } }; +#endif + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/sh3/gen/resumecontext.c b/lib/nbsd_libc/arch/sh3/gen/resumecontext.c new file mode 100644 index 000000000..1a7c4c55f --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/sh3/gen/setjmp.S b/lib/nbsd_libc/arch/sh3/gen/setjmp.S new file mode 100644 index 000000000..5ad5da2d5 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/setjmp.S @@ -0,0 +1,130 @@ +/* $NetBSD: setjmp.S,v 1.10 2006/01/05 19:21:37 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: setjmp.S,v 1.10 2006/01/05 19:21:37 uwe Exp $") +#endif + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__setjmp14) + PIC_PROLOGUE(.L_got_1) + sts.l pr, @-sp + mov.l r4, @-sp + + mov.l .L___sigprocmask14_1, r0 + mov r4, r6 + mov #1, r4 /* how = SIG_BLOCK */ + mov #0, r5 /* new = NULL */ +1: CALL r0 + add #(_JB_SIGMASK * 4), r6 /* old = &sigmask */ + + mov.l @sp+, r4 + lds.l @sp+, pr + PIC_EPILOGUE + + /* identical to _setjmp except that _JB_HAS_MASK is non-zero */ + add #((_JB_HAS_MASK + 1) * 4), r4 + mov #1, r0 + mov.l r0, @-r4 /* has signal mask */ + mov.l r15, @-r4 + mov.l r14, @-r4 + mov.l r13, @-r4 + mov.l r12, @-r4 + mov.l r11, @-r4 + mov.l r10, @-r4 + mov.l r9, @-r4 + mov.l r8, @-r4 + sts.l pr, @-r4 + rts + xor r0, r0 + + .align 2 +.L_got_1: PIC_GOT_DATUM +.L___sigprocmask14_1: CALL_DATUM(_C_LABEL(__sigprocmask14), 1b) + SET_ENTRY_SIZE(__setjmp14) + + +ENTRY(__longjmp14) + /* we won't return here, so we don't need to save pr and r12 */ + PIC_PROLOGUE_NOSAVE(.L_got_2) + mov.l r5, @-sp + mov.l r4, @-sp + + mov.l .L___sigprocmask14_2, r0 + mov r4, r5 + mov #3, r4 /* how = SIG_SETMASK */ + add #(_JB_SIGMASK * 4), r5 /* new = &sigmask */ +1: CALL r0 + mov #0, r6 /* old = NULL */ + + mov.l @sp+, r4 + mov.l @sp+, r5 + + /* identical to _longjmp */ + lds.l @r4+, pr + mov.l @r4+, r8 + mov.l @r4+, r9 + mov.l @r4+, r10 + mov.l @r4+, r11 + mov.l @r4+, r12 + mov.l @r4+, r13 + mov.l @r4+, r14 + mov.l @r4+, r15 + + mov r5, r0 + tst r0, r0 /* make sure return value is non-zero */ + bf .L0 + add #1, r0 +.L0: + rts + nop + + .align 2 +.L_got_2: PIC_GOT_DATUM +.L___sigprocmask14_2: CALL_DATUM(_C_LABEL(__sigprocmask14), 1b) + SET_ENTRY_SIZE(__longjmp14) diff --git a/lib/nbsd_libc/arch/sh3/gen/sigsetjmp.S b/lib/nbsd_libc/arch/sh3/gen/sigsetjmp.S new file mode 100644 index 000000000..2c7d7b15f --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/sigsetjmp.S @@ -0,0 +1,130 @@ +/* $NetBSD: sigsetjmp.S,v 1.9 2006/01/05 19:21:37 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: sigsetjmp.S,v 1.9 2006/01/05 19:21:37 uwe Exp $") +#endif + +ENTRY(__sigsetjmp14) + tst r5, r5 + bt 2f /* if (savemask == 0) */ + + /* identical to longjmp except that _JB_HAS_MASK is in the argument */ + PIC_PROLOGUE(.L_got_1) + sts.l pr, @-sp + mov.l r4, @-sp + mov.l r5, @-sp + + mov.l .L___sigprocmask14_1, r0 + mov r4, r6 + mov #1, r4 /* how = SIG_BLOCK */ + mov #0, r5 /* new = NULL */ +1: CALL r0 + add #(_JB_SIGMASK * 4), r6 /* old = &sigmask */ + + mov.l @sp+, r5 + mov.l @sp+, r4 + lds.l @sp+, pr + PIC_EPILOGUE + +2: /* identical to _setjmp except that _JB_HAS_MASK is in the argument */ + add #((_JB_HAS_MASK + 1) * 4), r4 + mov.l r5, @-r4 /* has signal mask? */ + mov.l r15, @-r4 + mov.l r14, @-r4 + mov.l r13, @-r4 + mov.l r12, @-r4 + mov.l r11, @-r4 + mov.l r10, @-r4 + mov.l r9, @-r4 + mov.l r8, @-r4 + sts.l pr, @-r4 + rts + xor r0, r0 + + .align 2 +.L_got_1: PIC_GOT_DATUM +.L___sigprocmask14_1: CALL_DATUM(_C_LABEL(__sigprocmask14), 1b) + SET_ENTRY_SIZE(__sigsetjmp14) + + +ENTRY(__siglongjmp14) + mov.l @(_JB_HAS_MASK * 4, r4), r0 + tst r0, r0 + bt 2f /* if no mask */ + + /* identical to longjmp */ + /* we won't return here, so we don't need to save pr and r12 */ + PIC_PROLOGUE_NOSAVE(.L_got_2) + mov.l r5, @-sp + mov.l r4, @-sp + + mov.l .L___sigprocmask14_2, r0 + mov r4, r5 + mov #3, r4 /* how = SIG_SETMASK */ + add #(_JB_SIGMASK * 4), r5 /* new = &sigmask */ +1: CALL r0 + mov #0, r6 /* old = NULL */ + + mov.l @sp+, r4 + mov.l @sp+, r5 + +2: /* identical to _longjmp */ + lds.l @r4+, pr + mov.l @r4+, r8 + mov.l @r4+, r9 + mov.l @r4+, r10 + mov.l @r4+, r11 + mov.l @r4+, r12 + mov.l @r4+, r13 + mov.l @r4+, r14 + mov.l @r4+, r15 + + mov r5, r0 + tst r0, r0 /* make sure return value is non-zero */ + bf .L0 + add #1, r0 +.L0: + rts + nop + + .align 2 +.L_got_2: PIC_GOT_DATUM +.L___sigprocmask14_2: CALL_DATUM(_C_LABEL(__sigprocmask14), 1b) + SET_ENTRY_SIZE(__siglongjmp14) diff --git a/lib/nbsd_libc/arch/sh3/gen/swapcontext.S b/lib/nbsd_libc/arch/sh3/gen/swapcontext.S new file mode 100644 index 000000000..841a842e2 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/gen/swapcontext.S @@ -0,0 +1,75 @@ +/* $NetBSD: swapcontext.S,v 1.9 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.9 2008/04/28 20:22:57 martin Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +/* + * int + * swapcontext(ucontext_t * restrict oucp, ucontext_t * restrict ucp); + */ +ENTRY(swapcontext) + PIC_PROLOGUE(.L_got) + mov.l r5, @-sp + sts.l pr, @-sp + + mov.l .L__getcontext, r0 +1: CALL r0 /* _getcontext(oucp) */ + mov.l r4, @-sp + mov.l @sp+, r1 + tst r0, r0 + bf 3f /* return error from getcontext */ + + /* Note: getcontext does _UC_MACHINE_INTRV(oucp) = 0 for us */ + mov.l @sp, r0 + mov.l r0, @(36 + 1 * 4, r1) /* _UC_MACHINE_SET_PC(oucp, pr) */ + mov.l .L_setcontext, r2 +2: CALL r2 /* setcontext(ucp) */ + mov.l @(4, sp), r4 + /* if we get here, return error from setcontext */ +3: + lds.l @sp+, pr +#ifdef PIC + add #4, sp + rts + PIC_EPILOGUE +#else + rts + add #4, sp +#endif + + .align 2 +.L_got: PIC_GOT_DATUM +.L__getcontext: CALL_DATUM(_C_LABEL(_getcontext), 1b) +.L_setcontext: CALL_DATUM(_C_LABEL(setcontext), 2b) + SET_ENTRY_SIZE(swapcontext) diff --git a/lib/nbsd_libc/arch/sh3/net/Makefile.inc b/lib/nbsd_libc/arch/sh3/net/Makefile.inc new file mode 100644 index 000000000..02c67cff1 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/net/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2000/01/05 14:07:36 msaitoh Exp $ + +SRCS+= htonl.c htons.c ntohl.c ntohs.c diff --git a/lib/nbsd_libc/arch/sh3/net/htonl.c b/lib/nbsd_libc/arch/sh3/net/htonl.c new file mode 100644 index 000000000..67356285f --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/net/htonl.c @@ -0,0 +1,56 @@ +/* $NetBSD: htonl.c,v 1.3 2005/12/24 23:10:08 perry Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)htonl.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#include +#if defined(LIBC_SCCS) + __RCSID("$NetBSD: htonl.c,v 1.3 2005/12/24 23:10:08 perry Exp $"); +#endif + +/* hostorder = htonl(netorder) */ +#if BYTE_ORDER == LITTLE_ENDIAN +u_int32_t +htonl(x) + u_int32_t x; +{ + u_int32_t y = 0; + + __asm("swap.b %1, %0" : "=r" (y) : "r" (x)); + __asm("swap.w %1, %0" : "=r" (y) : "r" (y)); + __asm("swap.b %1, %0" : "=r" (y) : "r" (y)); + return y; +} +#endif diff --git a/lib/nbsd_libc/arch/sh3/net/htons.c b/lib/nbsd_libc/arch/sh3/net/htons.c new file mode 100644 index 000000000..544f9fcff --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/net/htons.c @@ -0,0 +1,52 @@ +/* $NetBSD: htons.c,v 1.3 2005/12/24 23:10:08 perry Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)htonl.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#include +#if defined(LIBC_SCCS) + __RCSID("$NetBSD: htons.c,v 1.3 2005/12/24 23:10:08 perry Exp $"); +#endif + +/* netorder = htons(hostorder) */ +#if BYTE_ORDER == LITTLE_ENDIAN +u_int16_t +htons(x) + u_int16_t x; +{ + __asm("swap.b %1, %0" : "=r" (x) : "r" (x)); + return x; +} +#endif diff --git a/lib/nbsd_libc/arch/sh3/net/ntohl.c b/lib/nbsd_libc/arch/sh3/net/ntohl.c new file mode 100644 index 000000000..364656b59 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/net/ntohl.c @@ -0,0 +1,56 @@ +/* $NetBSD: ntohl.c,v 1.3 2005/12/24 23:10:08 perry Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)ntohl.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#include +#if defined(LIBC_SCCS) + __RCSID("$NetBSD: ntohl.c,v 1.3 2005/12/24 23:10:08 perry Exp $"); +#endif + +/* netorder = ntohl(hostorder) */ +#if BYTE_ORDER == LITTLE_ENDIAN +u_int32_t +ntohl(x) + u_int32_t x; +{ + u_int32_t y = 0; + + __asm("swap.b %1, %0" : "=r" (y) : "r" (x)); + __asm("swap.w %1, %0" : "=r" (y) : "r" (y)); + __asm("swap.b %1, %0" : "=r" (y) : "r" (y)); + return y; +} +#endif diff --git a/lib/nbsd_libc/arch/sh3/net/ntohs.c b/lib/nbsd_libc/arch/sh3/net/ntohs.c new file mode 100644 index 000000000..174a64d8c --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/net/ntohs.c @@ -0,0 +1,52 @@ +/* $NetBSD: ntohs.c,v 1.3 2005/12/24 23:10:08 perry Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)ntohl.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#include +#if defined(LIBC_SCCS) + __RCSID("$NetBSD: ntohs.c,v 1.3 2005/12/24 23:10:08 perry Exp $"); +#endif + +/* hostorder = ntohs(netorder) */ +#if BYTE_ORDER == LITTLE_ENDIAN +u_int16_t +ntohs(x) + u_int16_t x; +{ + __asm("swap.b %1, %0" : "=r" (x) : "r" (x)); + return x; +} +#endif diff --git a/lib/nbsd_libc/arch/sh3/softfloat/milieu.h b/lib/nbsd_libc/arch/sh3/softfloat/milieu.h new file mode 100644 index 000000000..64429ca18 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/softfloat/milieu.h @@ -0,0 +1,48 @@ +/* $NetBSD: milieu.h,v 1.1 2000/06/06 17:28:42 tsubai Exp $ */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +Include common integer types and flags. +------------------------------------------------------------------------------- +*/ +#include "sh3-gcc.h" + +/* +------------------------------------------------------------------------------- +Symbolic Boolean literals. +------------------------------------------------------------------------------- +*/ +enum { + FALSE = 0, + TRUE = 1 +}; diff --git a/lib/nbsd_libc/arch/sh3/softfloat/sh3-gcc.h b/lib/nbsd_libc/arch/sh3/softfloat/sh3-gcc.h new file mode 100644 index 000000000..15c168aa9 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/softfloat/sh3-gcc.h @@ -0,0 +1,89 @@ +/* $NetBSD: sh3-gcc.h,v 1.2 2005/12/24 21:11:16 perry Exp $ */ + +/* +------------------------------------------------------------------------------- +One of the macros `BIGENDIAN' or `LITTLEENDIAN' must be defined. +------------------------------------------------------------------------------- +*/ +#include +#if _BYTE_ORDER == _BIG_ENDIAN +#define BIGENDIAN +#endif +#if _BYTE_ORDER == _LITTLE_ENDIAN +#define LITTLEENDIAN +#endif + +/* +------------------------------------------------------------------------------- +The macro `BITS64' can be defined to indicate that 64-bit integer types are +supported by the compiler. +------------------------------------------------------------------------------- +*/ +#define BITS64 + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines the most convenient type that holds +integers of at least as many bits as specified. For example, `uint8' should +be the most convenient type that can hold unsigned integers of as many as +8 bits. The `flag' type must be able to hold either a 0 or 1. For most +implementations of C, `flag', `uint8', and `int8' should all be `typedef'ed +to the same as `int'. +------------------------------------------------------------------------------- +*/ +typedef int flag; +typedef unsigned char uint8; +typedef signed char int8; +typedef int uint16; +typedef int int16; +typedef unsigned int uint32; +typedef signed int int32; +#ifdef BITS64 +typedef unsigned long long int uint64; +typedef signed long long int int64; +#endif + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines a type that holds integers +of _exactly_ the number of bits specified. For instance, for most +implementation of C, `bits16' and `sbits16' should be `typedef'ed to +`unsigned short int' and `signed short int' (or `short int'), respectively. +------------------------------------------------------------------------------- +*/ +typedef unsigned char bits8; +typedef signed char sbits8; +typedef unsigned short int bits16; +typedef signed short int sbits16; +typedef unsigned int bits32; +typedef signed int sbits32; +#ifdef BITS64 +typedef unsigned long long int bits64; +typedef signed long long int sbits64; +#endif + +#ifdef BITS64 +/* +------------------------------------------------------------------------------- +The `LIT64' macro takes as its argument a textual integer literal and +if necessary ``marks'' the literal as having a 64-bit integer type. +For example, the GNU C Compiler (`gcc') requires that 64-bit literals be +appended with the letters `LL' standing for `long long', which is `gcc's +name for the 64-bit integer type. Some compilers may allow `LIT64' to be +defined as the identity macro: `#define LIT64( a ) a'. +------------------------------------------------------------------------------- +*/ +#define LIT64(a) a##LL +#endif + +/* +------------------------------------------------------------------------------- +The macro `INLINE' can be used before functions that should be inlined. If +a compiler does not support explicit inlining, this macro should be defined +to be `static'. +------------------------------------------------------------------------------- +*/ +#define INLINE static inline + +#define FLOAT64_DEMANGLE(a) (a) +#define FLOAT64_MANGLE(a) (a) diff --git a/lib/nbsd_libc/arch/sh3/softfloat/softfloat.h b/lib/nbsd_libc/arch/sh3/softfloat/softfloat.h new file mode 100644 index 000000000..a1f9b1ab8 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/softfloat/softfloat.h @@ -0,0 +1,314 @@ +/* $NetBSD: softfloat.h,v 1.9 2006/05/16 20:55:51 mrg Exp $ */ + +/* This is a derivative work. */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +The macro `FLOATX80' must be defined to enable the extended double-precision +floating-point format `floatx80'. If this macro is not defined, the +`floatx80' type will not be defined, and none of the functions that either +input or output the `floatx80' type will be defined. The same applies to +the `FLOAT128' macro and the quadruple-precision format `float128'. +------------------------------------------------------------------------------- +*/ +/* #define FLOATX80 */ +/* #define FLOAT128 */ + +#include + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point types. +------------------------------------------------------------------------------- +*/ +typedef unsigned int float32; +typedef unsigned long long float64; +#ifdef FLOATX80 +typedef struct { + unsigned short high; + unsigned long long low; +} floatx80; +#endif +#ifdef FLOAT128 +typedef struct { + unsigned long long high, low; +} float128; +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point underflow tininess-detection mode. +------------------------------------------------------------------------------- +*/ +#ifndef SOFTFLOAT_FOR_GCC +extern int8 float_detect_tininess; +#endif +enum { + float_tininess_after_rounding = 0, + float_tininess_before_rounding = 1 +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point rounding mode. +------------------------------------------------------------------------------- +*/ +extern fp_rnd float_rounding_mode; +enum { + float_round_nearest_even = FP_RN, + float_round_to_zero = FP_RZ, + float_round_down = FP_RM, + float_round_up = FP_RP +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point exception flags. +------------------------------------------------------------------------------- +*/ +extern fp_except float_exception_flags; +extern fp_except float_exception_mask; +enum { + float_flag_inexact = FP_X_IMP, + float_flag_underflow = FP_X_UFL, + float_flag_overflow = FP_X_OFL, + float_flag_divbyzero = FP_X_DZ, + float_flag_invalid = FP_X_INV +}; + +/* +------------------------------------------------------------------------------- +Routine to raise any or all of the software IEC/IEEE floating-point +exception flags. +------------------------------------------------------------------------------- +*/ +void float_raise( fp_except ); + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE integer-to-floating-point conversion routines. +------------------------------------------------------------------------------- +*/ +float32 int32_to_float32( int ); +float64 int32_to_float64( int ); +#ifdef FLOATX80 +floatx80 int32_to_floatx80( int ); +#endif +#ifdef FLOAT128 +float128 int32_to_float128( int ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __floatdi?f is in libgcc2.c */ +float32 int64_to_float32( long long ); +float64 int64_to_float64( long long ); +#ifdef FLOATX80 +floatx80 int64_to_floatx80( long long ); +#endif +#ifdef FLOAT128 +float128 int64_to_float128( long long ); +#endif +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float32_to_int32( float32 ); +int float32_to_int32_round_to_zero( float32 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned int float32_to_uint32_round_to_zero( float32 ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ +long long float32_to_int64( float32 ); +long long float32_to_int64_round_to_zero( float32 ); +#endif +float64 float32_to_float64( float32 ); +#ifdef FLOATX80 +floatx80 float32_to_floatx80( float32 ); +#endif +#ifdef FLOAT128 +float128 float32_to_float128( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision operations. +------------------------------------------------------------------------------- +*/ +float32 float32_round_to_int( float32 ); +float32 float32_add( float32, float32 ); +float32 float32_sub( float32, float32 ); +float32 float32_mul( float32, float32 ); +float32 float32_div( float32, float32 ); +float32 float32_rem( float32, float32 ); +float32 float32_sqrt( float32 ); +flag float32_eq( float32, float32 ); +flag float32_le( float32, float32 ); +flag float32_lt( float32, float32 ); +flag float32_eq_signaling( float32, float32 ); +flag float32_le_quiet( float32, float32 ); +flag float32_lt_quiet( float32, float32 ); +#ifndef SOFTFLOAT_FOR_GCC +flag float32_is_signaling_nan( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float64_to_int32( float64 ); +int float64_to_int32_round_to_zero( float64 ); +#if defined(SOFTFLOAT_FOR_GCC) && defined(SOFTFLOAT_NEED_FIXUNS) +unsigned int float64_to_uint32_round_to_zero( float64 ); +#endif +#ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ +long long float64_to_int64( float64 ); +long long float64_to_int64_round_to_zero( float64 ); +#endif +float32 float64_to_float32( float64 ); +#ifdef FLOATX80 +floatx80 float64_to_floatx80( float64 ); +#endif +#ifdef FLOAT128 +float128 float64_to_float128( float64 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision operations. +------------------------------------------------------------------------------- +*/ +float64 float64_round_to_int( float64 ); +float64 float64_add( float64, float64 ); +float64 float64_sub( float64, float64 ); +float64 float64_mul( float64, float64 ); +float64 float64_div( float64, float64 ); +float64 float64_rem( float64, float64 ); +float64 float64_sqrt( float64 ); +flag float64_eq( float64, float64 ); +flag float64_le( float64, float64 ); +flag float64_lt( float64, float64 ); +flag float64_eq_signaling( float64, float64 ); +flag float64_le_quiet( float64, float64 ); +flag float64_lt_quiet( float64, float64 ); +#ifndef SOFTFLOAT_FOR_GCC +flag float64_is_signaling_nan( float64 ); +#endif + +#ifdef FLOATX80 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int floatx80_to_int32( floatx80 ); +int floatx80_to_int32_round_to_zero( floatx80 ); +long long floatx80_to_int64( floatx80 ); +long long floatx80_to_int64_round_to_zero( floatx80 ); +float32 floatx80_to_float32( floatx80 ); +float64 floatx80_to_float64( floatx80 ); +#ifdef FLOAT128 +float128 floatx80_to_float128( floatx80 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision rounding precision. Valid +values are 32, 64, and 80. +------------------------------------------------------------------------------- +*/ +extern int floatx80_rounding_precision; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision operations. +------------------------------------------------------------------------------- +*/ +floatx80 floatx80_round_to_int( floatx80 ); +floatx80 floatx80_add( floatx80, floatx80 ); +floatx80 floatx80_sub( floatx80, floatx80 ); +floatx80 floatx80_mul( floatx80, floatx80 ); +floatx80 floatx80_div( floatx80, floatx80 ); +floatx80 floatx80_rem( floatx80, floatx80 ); +floatx80 floatx80_sqrt( floatx80 ); +flag floatx80_eq( floatx80, floatx80 ); +flag floatx80_le( floatx80, floatx80 ); +flag floatx80_lt( floatx80, floatx80 ); +flag floatx80_eq_signaling( floatx80, floatx80 ); +flag floatx80_le_quiet( floatx80, floatx80 ); +flag floatx80_lt_quiet( floatx80, floatx80 ); +flag floatx80_is_signaling_nan( floatx80 ); + +#endif + +#ifdef FLOAT128 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float128_to_int32( float128 ); +int float128_to_int32_round_to_zero( float128 ); +long long float128_to_int64( float128 ); +long long float128_to_int64_round_to_zero( float128 ); +float32 float128_to_float32( float128 ); +float64 float128_to_float64( float128 ); +#ifdef FLOATX80 +floatx80 float128_to_floatx80( float128 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision operations. +------------------------------------------------------------------------------- +*/ +float128 float128_round_to_int( float128 ); +float128 float128_add( float128, float128 ); +float128 float128_sub( float128, float128 ); +float128 float128_mul( float128, float128 ); +float128 float128_div( float128, float128 ); +float128 float128_rem( float128, float128 ); +float128 float128_sqrt( float128 ); +flag float128_eq( float128, float128 ); +flag float128_le( float128, float128 ); +flag float128_lt( float128, float128 ); +flag float128_eq_signaling( float128, float128 ); +flag float128_le_quiet( float128, float128 ); +flag float128_lt_quiet( float128, float128 ); +flag float128_is_signaling_nan( float128 ); + +#endif + diff --git a/lib/nbsd_libc/arch/sh3/stdlib/Makefile.inc b/lib/nbsd_libc/arch/sh3/stdlib/Makefile.inc new file mode 100644 index 000000000..2f91b992a --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/31 20:40:00 dsl Exp $ diff --git a/lib/nbsd_libc/arch/sh3/string/Makefile.inc b/lib/nbsd_libc/arch/sh3/string/Makefile.inc new file mode 100644 index 000000000..2aced0da6 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/string/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/30 20:57:16 dsl Exp $ + +SRCS+= bcopy.S bzero.S ffs.S memset.S +SRCS+= memcpy.S memmove.S diff --git a/lib/nbsd_libc/arch/sh3/string/bcopy.S b/lib/nbsd_libc/arch/sh3/string/bcopy.S new file mode 100644 index 000000000..b32725ad4 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/string/bcopy.S @@ -0,0 +1,4 @@ +/* $NetBSD: bcopy.S,v 1.3 2006/04/23 00:08:18 uwe Exp $ */ + +#define BCOPY +#include "memcpy.S" diff --git a/lib/nbsd_libc/arch/sh3/string/bzero.S b/lib/nbsd_libc/arch/sh3/string/bzero.S new file mode 100644 index 000000000..2d5ceca08 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/string/bzero.S @@ -0,0 +1,5 @@ +/* $NetBSD: bzero.S,v 1.1 2002/11/20 14:23:54 itohy Exp $ */ + +#define BZERO +#include "memset.S" + diff --git a/lib/nbsd_libc/arch/sh3/sys/__clone.S b/lib/nbsd_libc/arch/sh3/sys/__clone.S new file mode 100644 index 000000000..7e8b80871 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/__clone.S @@ -0,0 +1,88 @@ +/* $NetBSD: __clone.S,v 1.6 2006/01/06 03:58:31 uwe Exp $ */ + +/*- + * Copyright (c) 2001 Tsubai Masanari. 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 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. + */ + +#include +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone,__clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + /* + * Sanity checks: func and stack may not be NULL. + */ + tst r4, r4 + bt inval + tst r5, r5 + bt inval + + mov r4, r2 /* save entry point for later */ + + mov r6, r4 /* r4 = flags, r5 = stack (already) */ + mov.l .L_SYS___clone, r0 + trapa #0x80 + bf err + + tst r0, r0 + bf 3f /* we're the parent, just return */ + + jsr @r2 /* clone does _exit((*fn)(arg)); */ + mov r7, r4 + + mov.l .L__exit, r1 +#ifdef PIC + mov r0, r4 + mov.l .L_got, r12 /* PIC_PROLOGUE_NOSAVE(.L_got) */ + mova .L_got, r0 /* expanded to fill the delay slot */ +1: JUMP r1 + add r0, r12 +#else + JUMP r1 + mov r0, r4 +#endif + /* NOTREACHED */ + +inval: + mov #EINVAL, r0 +err: + JUMP_CERROR + /* NOTREACHED */ + +3: rts + nop + + .align 2 +.L_SYS___clone: .long SYS___clone + +.L_got: PIC_GOT_DATUM +.L__exit: CALL_DATUM(_C_LABEL(_exit), 1b) + SET_ENTRY_SIZE(__clone) diff --git a/lib/nbsd_libc/arch/sh3/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/sh3/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..73b8fd837 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/__sigaction14_sigtramp.c @@ -0,0 +1,76 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.8 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.8 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) +#ifdef __LIBC12_SOURCE__ +extern const int __sigtramp_sigcontext_1[] + __weak_reference(__sigtramp_sigcontext_1); +#endif + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern int __sigtramp_siginfo_2[]; + +#ifdef __LIBC12_SOURCE__ + /* + * We select the compatibility SIGCONTEXT trampoline if SA_SIGINFO + * is not set in the sigaction. + */ + if (act && (act->sa_flags & SA_SIGINFO) == 0) { + extern int __sigtramp_sigcontext_1[]; + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + /* + * EINVAL might indicate that trampoline version 1 is + * not supported by the kernel; fall back on native + * SIGINFO trampoline. + */ + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/sh3/sys/__sigtramp2.S b/lib/nbsd_libc/arch/sh3/sys/__sigtramp2.S new file mode 100644 index 000000000..930a9996d --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/__sigtramp2.S @@ -0,0 +1,54 @@ +/* $NetBSD: __sigtramp2.S,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +/* + * The SH signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * siginfo structure + * sp-> ucontext structure + * + * NB: This order is different from what other ports use (siginfo at + * the top of the stack), because we want to avoid wasting two + * instructions to skip to the ucontext. Not that this order really + * matters, but I think this inconsistency deserves an explanation. + */ +NENTRY(__sigtramp_siginfo_2) + mov r15, r4 /* get pointer to ucontext */ + SYSTRAP(setcontext) /* and call setcontext() */ + mov r0, r4 /* exit with errno */ + SYSTRAP(exit) /* if sigreturn fails */ + + SET_ENTRY_SIZE(__sigtramp_siginfo_2) diff --git a/lib/nbsd_libc/arch/sh3/sys/__syscall.S b/lib/nbsd_libc/arch/sh3/sys/__syscall.S new file mode 100644 index 000000000..0da939426 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/__syscall.S @@ -0,0 +1,44 @@ +/* $NetBSD: __syscall.S,v 1.6 2006/01/06 01:57:38 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __syscall.S,v 1.6 2006/01/06 01:57:38 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/sh3/sys/__vfork14.S b/lib/nbsd_libc/arch/sh3/sys/__vfork14.S new file mode 100644 index 000000000..0d2a29610 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/__vfork14.S @@ -0,0 +1,67 @@ +/* $NetBSD: __vfork14.S,v 1.6 2006/01/06 07:07:34 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __vfork14.S,v 1.6 2006/01/06 07:07:34 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * pid = vfork(); + * + * r1 == 0 in parent process, r1 == 1 in child process. + * r0 == pid of child in parent, r0 == pid of parent in child. + * + */ +ENTRY(__vfork14) + mov.l .L_SYS___vfork14, r0 + trapa #0x80 + bf err + + add #0xff, r1 /* from 1 to 0 in child, 0 to -1 in parent */ + rts + and r1, r0 /* 0 in child, child pid in parent */ + +err: + JUMP_CERROR + + .align 2 +.L_SYS___vfork14: + .long SYS___vfork14 + + SET_ENTRY_SIZE(__vfork14) diff --git a/lib/nbsd_libc/arch/sh3/sys/brk.S b/lib/nbsd_libc/arch/sh3/sys/brk.S new file mode 100644 index 000000000..b6f26c148 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/brk.S @@ -0,0 +1,98 @@ +/* $NetBSD: brk.S,v 1.10 2006/01/06 03:58:31 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)brk.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: brk.S,v 1.10 2006/01/06 03:58:31 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk,_brk) +#endif + + .data + .globl _C_LABEL(__minbrk) + .align 2 +_C_LABEL(__minbrk): + .long _end + + .text + +ENTRY(_brk) +#ifdef PIC + mova L_GOT, r0 + mov.l L_GOT, r2 + add r0, r2 + mov.l Lminbrk, r0 + mov.l @(r0, r2), r0 + mov.l @r0, r0 +#else + mov.l Lminbrk, r0 + mov.l @r0, r0 +#endif + cmp/hs r4, r0 + bf 1f + mov r0, r4 +1: + mov.l LSYS_break, r0 + trapa #0x80 + bf err +#ifdef PIC + mov.l Lcurbrk, r0 + mov.l @(r0, r2), r1 + xor r0, r0 +#else + xor r0, r0 + mov.l Lcurbrk, r1 +#endif + rts + mov.l r4, @r1 +err: + JUMP_CERROR + + .align 2 +LSYS_break: .long SYS_break +#ifdef PIC +L_GOT: .long _GLOBAL_OFFSET_TABLE_ +Lminbrk: .long _C_LABEL(__minbrk)@GOT +Lcurbrk: .long curbrk@GOT +#else +Lminbrk: .long _C_LABEL(__minbrk) +Lcurbrk: .long curbrk +#endif + SET_ENTRY_SIZE(_brk) diff --git a/lib/nbsd_libc/arch/sh3/sys/cerror.S b/lib/nbsd_libc/arch/sh3/sys/cerror.S new file mode 100644 index 000000000..9d8c55254 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/cerror.S @@ -0,0 +1,99 @@ +/* $NetBSD: cerror.S,v 1.10 2006/01/06 05:14:39 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)cerror.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: cerror.S,v 1.10 2006/01/06 05:14:39 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef _REENTRANT + + + .globl _C_LABEL(__errno) + + .align 2 +cerror: + mov.l .L___errno, r1 + PIC_PROLOGUE(.L_got) + sts.l pr, @-sp +1: CALL r1 + mov.l r4, @-sp ! save error code + mov.l @sp+, r4 + lds.l @sp+, pr + PIC_EPILOGUE + mov.l r4, @r0 + mov #-1, r1 + rts + mov #-1, r0 + + .align 2 +.L_got: PIC_GOT_DATUM +.L___errno: CALL_DATUM(_C_LABEL(__errno), 1b) + SET_ASENTRY_SIZE(cerror) + +#else /* !_REENTRANT */ + + + .globl _C_LABEL(errno) + + .align 2 +cerror: +#ifdef PIC + mova L_GOT, r0 + mov.l L_GOT, r1 + add r0, r1 + mov.l L_errno, r0 + mov.l @(r0, r1), r1 + mov.l r4, @r1 +#else + mov.l L_errno, r1 + mov.l r4, @r1 +#endif + mov #-1, r0 + rts + mov #-1, r1 + + .align 2 +#ifdef PIC +L_GOT: .long _GLOBAL_OFFSET_TABLE_ +L_errno: .long _C_LABEL(errno)@GOT +#else +L_errno: .long _C_LABEL(errno) +#endif + +#endif /* !_REENTRANT */ diff --git a/lib/nbsd_libc/arch/sh3/sys/exect.S b/lib/nbsd_libc/arch/sh3/sys/exect.S new file mode 100644 index 000000000..a4f2a9e96 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/exect.S @@ -0,0 +1,44 @@ +/* $NetBSD: exect.S,v 1.5 2006/01/06 01:53:30 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)exect.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: exect.S,v 1.5 2006/01/06 01:53:30 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +PSEUDO(exect,execve) diff --git a/lib/nbsd_libc/arch/sh3/sys/fork.S b/lib/nbsd_libc/arch/sh3/sys/fork.S new file mode 100644 index 000000000..5448955a5 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/fork.S @@ -0,0 +1,63 @@ +/* $NetBSD: fork.S,v 1.10 2006/01/06 05:11:29 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)fork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: fork.S,v 1.10 2006/01/06 05:11:29 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * pid = fork(); + * + * r1 == 0 in parent process, r1 == 1 in child process. + * r0 == pid of child in parent, r0 == pid of parent in child. + * + */ +ENTRY(__fork) + mov #SYS_fork, r0 + trapa #0x80 + bf err + + add #0xff, r1 /* from 1 to 0 in child, 0 to -1 in parent */ + rts + and r1, r0 /* 0 in child, child pid in parent */ + +err: + JUMP_CERROR + + SET_ENTRY_SIZE(__fork) diff --git a/lib/nbsd_libc/arch/sh3/sys/getcontext.S b/lib/nbsd_libc/arch/sh3/sys/getcontext.S new file mode 100644 index 000000000..91ca4a9bc --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/getcontext.S @@ -0,0 +1,48 @@ +/* $NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext,_getcontext) +#endif + +_SYSCALL(_getcontext,getcontext) + sts pr, r2 + mov.l r2, @(36 + 1 * 4, r4) /* saved pc <- pr */ + add #36 + 20 * 4, r4 + rts + mov.l r0, @r4 /* arrange for return value 0 */ diff --git a/lib/nbsd_libc/arch/sh3/sys/pipe.S b/lib/nbsd_libc/arch/sh3/sys/pipe.S new file mode 100644 index 000000000..11c6b953b --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/pipe.S @@ -0,0 +1,52 @@ +/* $NetBSD: pipe.S,v 1.7 2006/01/06 05:23:46 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)pipe.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: pipe.S,v 1.7 2006/01/06 05:23:46 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe,_pipe) +#endif + +_SYSCALL(_pipe,pipe) + mov.l r0, @r4 + mov.l r1, @(4, r4) + rts + mov #0, r0 diff --git a/lib/nbsd_libc/arch/sh3/sys/ptrace.S b/lib/nbsd_libc/arch/sh3/sys/ptrace.S new file mode 100644 index 000000000..be78d6cb3 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/ptrace.S @@ -0,0 +1,111 @@ +/* $NetBSD: ptrace.S,v 1.8 2006/01/06 20:38:01 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)ptrace.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: ptrace.S,v 1.8 2006/01/06 20:38:01 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * int + * ptrace(int request, pid_t pid, caddr_t addr, int data); + */ +ENTRY(ptrace) +#ifdef _REENTRANT + + mov.l .L___errno, r1 + PIC_PROLOGUE(.L_GOT) + sts.l pr, @-sp + mov.l r7, @-sp + mov.l r6, @-sp + mov.l r5, @-sp +1: CALL r1 + mov.l r4, @-sp + mov.l @sp+, r4 + mov.l @sp+, r5 + mov.l @sp+, r6 + mov.l @sp+, r7 + lds.l @sp+, pr + PIC_EPILOGUE + +#else /* ! _REENTRANT */ + +#ifdef PIC + mova .L_GOT, r0 + mov.l .L_GOT, r1 + add r0, r1 + mov.l .L_errno, r0 + mov.l @(r0, r1), r0 +#else + mov.l .L_errno, r0 +#endif + +#endif /* ! _REENTRANT */ + + mov #0, r1 + mov.l r1, @r0 /* errno = 0; */ + + mov #SYS_ptrace, r0 + trapa #0x80 + bf err + rts + nop +err: + JUMP_CERROR + + .align 2 +.L_GOT: + PIC_GOT_DATUM + +#ifdef _REENTRANT + .global _C_LABEL(__errno) +.L___errno: + CALL_DATUM(_C_LABEL(__errno), 1b) + +#else /* ! _REENTRANT */ + .global _C_LABEL(errno) +.L_errno: +#ifdef PIC + .long PIC_GOT(_C_LABEL(errno)) +#else + .long _C_LABEL(errno) +#endif + +#endif /* ! _REENTRANT */ + + SET_ENTRY_SIZE(ptrace) diff --git a/lib/nbsd_libc/arch/sh3/sys/sbrk.S b/lib/nbsd_libc/arch/sh3/sys/sbrk.S new file mode 100644 index 000000000..75351c786 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/sbrk.S @@ -0,0 +1,92 @@ +/* $NetBSD: sbrk.S,v 1.9 2006/01/06 03:58:31 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sbrk.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: sbrk.S,v 1.9 2006/01/06 03:58:31 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _end + .globl curbrk + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk,_sbrk) +#endif + + .data +curbrk: .long _end + .text + +ENTRY(_sbrk) + mov r4, r2 +#ifdef PIC + mova L_GOT, r0 + mov.l L_GOT, r3 + add r0, r3 + mov.l Lcurbrk, r0 + mov.l @(r0, r3), r0 +#else + mov.l Lcurbrk, r0 +#endif + mov.l @r0, r0 + add r0, r4 + mov.l LSYS_break, r0 + trapa #0x80 + bf err +#ifdef PIC + mov.l Lcurbrk, r0 + mov.l @(r0, r3), r1 +#else + mov.l Lcurbrk, r1 +#endif + mov.l @r1, r0 + add r0, r2 + rts + mov.l r2, @r1 +err: + JUMP_CERROR + + .align 2 +LSYS_break: .long SYS_break +#ifdef PIC +L_GOT: .long _GLOBAL_OFFSET_TABLE_ +Lcurbrk: .long curbrk@GOT +#else +Lcurbrk: .long curbrk +#endif + SET_ENTRY_SIZE(_sbrk) diff --git a/lib/nbsd_libc/arch/sh3/sys/shmat.S b/lib/nbsd_libc/arch/sh3/sys/shmat.S new file mode 100644 index 000000000..7f57fad5f --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:54 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/sh3/sys/syscall.S b/lib/nbsd_libc/arch/sh3/sys/syscall.S new file mode 100644 index 000000000..979ef83a8 --- /dev/null +++ b/lib/nbsd_libc/arch/sh3/sys/syscall.S @@ -0,0 +1,44 @@ +/* $NetBSD: syscall.S,v 1.6 2006/01/06 01:27:23 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: syscall.S,v 1.6 2006/01/06 01:27:23 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/sparc/Makefile.inc b/lib/nbsd_libc/arch/sparc/Makefile.inc new file mode 100644 index 000000000..cc99fbeb2 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/Makefile.inc @@ -0,0 +1,32 @@ +# $NetBSD: Makefile.inc,v 1.13 2008/10/26 07:43:07 mrg Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +# `source' files built from m4 source +# the name `div.o' is taken for the ANSI C `div' function, hence sdiv here +SRCS+= rem.S sdiv.S udiv.S urem.S +CLEANFILES+=rem.S sdiv.S udiv.S urem.S + +sdiv.S: ${LIBCDIR}/arch/sparc/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.div')define(OP,\`div')define(S,\`true')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} + +udiv.S: ${LIBCDIR}/arch/sparc/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.udiv')define(OP,\`div')define(S,\`false')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} + +rem.S: ${LIBCDIR}/arch/sparc/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.rem')define(OP,\`rem')define(S,\`true')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} + +urem.S: ${LIBCDIR}/arch/sparc/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.urem')define(OP,\`rem')define(S,\`false')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} diff --git a/lib/nbsd_libc/arch/sparc/SYS.h b/lib/nbsd_libc/arch/sparc/SYS.h new file mode 100644 index 000000000..5f8f3a469 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/SYS.h @@ -0,0 +1,151 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * @(#)SYS.h 8.1 (Berkeley) 6/4/93 + * + * from: Header: SYS.h,v 1.2 92/07/03 18:57:00 torek Exp + * $NetBSD: SYS.h,v 1.17 2007/10/08 13:06:00 uwe Exp $ + */ + +#include +#include +#include + +#ifdef __STDC__ +#define _CAT(x,y) x##y +#else +#define _CAT(x,y) x/**/y +#endif + +#ifdef __ELF__ +#define CERROR _C_LABEL(__cerror) +#define CURBRK _C_LABEL(__curbrk) +#else +#define CERROR _ASM_LABEL(cerror) +#define CURBRK _ASM_LABEL(curbrk) +#endif + +/* + * ERROR branches to cerror. + */ +#ifdef PIC +#define CALL(name) \ + PIC_PROLOGUE(%g1, %g2); \ + set name, %g2; \ + ld [%g1 + %g2], %g2; \ + jmp %g2; \ + nop +#else +#define CALL(name) \ + set name, %g1; \ + jmp %g1; \ + nop +#endif +#define ERROR() CALL(CERROR) + +/* + * SYSCALL is used when further action must be taken before returning. + * Note that it adds a `nop' over what we could do, if we only knew + * what came at label 1.... + */ +#define _SYSCALL(x,y) \ +ENTRY(x); \ + mov _CAT(SYS_,y), %g1; \ + t ST_SYSCALL; \ + bcc 1f; \ + nop; \ + ERROR(); \ +1: /* next insn */ + +#define SYSCALL(x) \ + _SYSCALL(x,x) + +/* + * RSYSCALL is used when the system call should just return. Here we + * use the SYSCALL_G2RFLAG to put the `success' return address in %g2 + * and avoid a branch. + * + * PSEUDO(x,y) is like RSYSCALL(y), except that the name is x. + */ +#define _RSYSCALL(x,y) \ +ENTRY(x); \ + mov (_CAT(SYS_,y)) | SYSCALL_G2RFLAG, %g1; \ + add %o7, 8, %g2; \ + t ST_SYSCALL; \ + ERROR() + +#define RSYSCALL(x) _RSYSCALL(x,x) +#define PSEUDO(x,y) _RSYSCALL(x,y) + +/* + * WSYSCALL(weak,strong) is like RSYSCALL(weak), + * except that weak is a weak internal alias for the strong symbol. + */ +#ifdef WEAK_ALIAS +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) +#else +#define WSYSCALL(weak,strong) \ + RSYSCALL(weak) +#endif + +/* + * SYSCALL_NOERROR is like SYSCALL, except it's used for syscalls that + * never fail. + * + * XXX - This should be optimized. + */ +#define SYSCALL_NOERROR(x) \ +ENTRY(x); \ + mov _CAT(SYS_,x), %g1; \ + t ST_SYSCALL + +/* + * RSYSCALL_NOERROR is like RSYSCALL, except it's used for syscalls + * that never fail. + * + * PSEUDO_NOERROR(x,y) is like RSYSCALL_NOERROR(y), except that the + * name is x. + * + * XXX - This should be optimized. + */ +#define _RSYSCALL_NOERROR(x,y) \ +ENTRY(x); \ + mov (_CAT(SYS_,y)) | SYSCALL_G2RFLAG, %g1; \ + add %o7, 8, %g2; \ + t ST_SYSCALL + +#define RSYSCALL_NOERROR(x) _RSYSCALL_NOERROR(x,x) +#define PSEUDO_NOERROR(x,y) _RSYSCALL_NOERROR(x,y) + + .globl CERROR diff --git a/lib/nbsd_libc/arch/sparc/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/sparc/gdtoa/Makefile.inc new file mode 100644 index 000000000..1d786e010 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c diff --git a/lib/nbsd_libc/arch/sparc/gdtoa/arith.h b/lib/nbsd_libc/arch/sparc/gdtoa/arith.h new file mode 100644 index 000000000..4674f629c --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_BIG_ENDIAN diff --git a/lib/nbsd_libc/arch/sparc/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/sparc/gdtoa/gd_qnan.h new file mode 100644 index 000000000..75f636b54 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gdtoa/gd_qnan.h @@ -0,0 +1,5 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x7ff80000 +#define d_QNAN1 0x0 diff --git a/lib/nbsd_libc/arch/sparc/gen/Makefile.inc b/lib/nbsd_libc/arch/sparc/gen/Makefile.inc new file mode 100644 index 000000000..dae1231b8 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/Makefile.inc @@ -0,0 +1,29 @@ +# $NetBSD: Makefile.inc,v 1.22 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= fabs.S modf.S +SRCS+= flt_rounds.c fpgetmask.c fpgetround.c fpgetsticky.c fpsetmask.c \ + fpsetround.c fpsetsticky.c + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c infinityl_dbl_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c + +SRCS+= setjmp.S longjmp.c +SRCS+= _setjmp.S +SRCS+= sigsetjmp.S + +SRCS+= makecontext.c resumecontext.c _lwp.c swapcontext.S + +SRCS+= fixunsdfsi.S mul.S umul.S saveregs.S +SRCS+= bswap16.c bswap32.c bswap64.c + +LSRCS.sparc.gen= Lint_swapcontext.c +LSRCS+= ${LSRCS.sparc.gen} +DPSRCS+= ${LSRCS.sparc.gen} +CLEANFILES+= ${LSRCS.sparc.gen} diff --git a/lib/nbsd_libc/arch/sparc/gen/_lwp.c b/lib/nbsd_libc/arch/sparc/gen/_lwp.c new file mode 100644 index 000000000..a8a91e8d6 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/_lwp.c @@ -0,0 +1,73 @@ +/* $NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), void *arg, + void *private, caddr_t stack_base, size_t stack_size) +{ + __greg_t *gr; + unsigned long *sp; + + getcontext(u); + gr = u->uc_mcontext.__gregs; + + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + + sp = (ulong *)(stack_base + stack_size); + sp = (ulong *)((ulong)sp & ~0x07); + + /* Make room for the fake caller stack frame (CCFSZ, only in words) */ + sp -= 8 + 8 + 1 + 6 + 1; + + /* Entering (*start)(arg), return is to _lwp_exit */ + gr[_REG_PC] = (ulong) start; + gr[_REG_nPC] = (ulong) start + 4; + gr[_REG_O0] = (ulong)arg; + gr[_REG_O6] = (ulong)sp; + gr[_REG_O7] = (ulong)_lwp_exit - 8; + + /* XXX: uwe: why do we need this? */ + /* create loopback in the window save area on the stack? */ + sp[8+6] = (ulong)sp; /* %i6 */ + sp[8+7] = (ulong)_lwp_exit - 8; /* %i7 */ +} diff --git a/lib/nbsd_libc/arch/sparc/gen/_setjmp.S b/lib/nbsd_libc/arch/sparc/gen/_setjmp.S new file mode 100644 index 000000000..3c593f40e --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/_setjmp.S @@ -0,0 +1,72 @@ +/* $NetBSD: _setjmp.S,v 1.8 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Paul Kranenburg. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +RCSID("$NetBSD: _setjmp.S,v 1.8 2008/04/28 20:22:57 martin Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v?v:1)" from + * the last call to + * _setjmp(a) + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + st %sp, [%o0+0] /* store caller's stack pointer */ + st %o7, [%o0+4] /* and the return pc */ + retl + clr %o0 ! return 0 + +ENTRY(_longjmp) + sub %sp, 64, %sp ! set up a local stack frame + tst %o1 ! compute v ? v : 1 + be,a 0f + mov 1, %o1 +0: + t ST_FLUSHWIN ! flush register windows out to the stack + + /* + * We restore the saved stack pointer to %fp, then issue + * a `restore' instruction which will reload the register + * window from the stack. + */ + ld [%o0+4], %o7 /* restore return pc */ + ld [%o0+0], %fp /* and stack pointer */ + + retl ! success, return %o1 + restore %o1, 0, %o0 diff --git a/lib/nbsd_libc/arch/sparc/gen/divrem.m4 b/lib/nbsd_libc/arch/sparc/gen/divrem.m4 new file mode 100644 index 000000000..601fd9c9a --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/divrem.m4 @@ -0,0 +1,272 @@ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: divrem.m4,v 1.4 92/06/25 13:23:57 torek Exp + * $NetBSD: divrem.m4,v 1.5 2003/08/07 16:42:23 agc Exp $ + */ + +/* + * Division and remainder, from Appendix E of the Sparc Version 8 + * Architecture Manual, with fixes from Gordon Irlam. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)divrem.m4 8.1 (Berkeley) 6/4/93" +#endif /* LIBC_SCCS and not lint */ + +/* + * Input: dividend and divisor in %o0 and %o1 respectively. + * + * m4 parameters: + * NAME name of function to generate + * OP OP=div => %o0 / %o1; OP=rem => %o0 % %o1 + * S S=true => signed; S=false => unsigned + * + * Algorithm parameters: + * N how many bits per iteration we try to get (4) + * WORDSIZE total number of bits (32) + * + * Derived constants: + * TWOSUPN 2^N, for label generation (m4 exponentiation currently broken) + * TOPBITS number of bits in the top `decade' of a number + * + * Important variables: + * Q the partial quotient under development (initially 0) + * R the remainder so far, initially the dividend + * ITER number of main division loop iterations required; + * equal to ceil(log2(quotient) / N). Note that this + * is the log base (2^N) of the quotient. + * V the current comparand, initially divisor*2^(ITER*N-1) + * + * Cost: + * Current estimate for non-large dividend is + * ceil(log2(quotient) / N) * (10 + 7N/2) + C + * A large dividend is one greater than 2^(31-TOPBITS) and takes a + * different path, as the upper bits of the quotient must be developed + * one bit at a time. + */ + +define(N, `4') +define(TWOSUPN, `16') +define(WORDSIZE, `32') +define(TOPBITS, eval(WORDSIZE - N*((WORDSIZE-1)/N))) + +define(dividend, `%o0') +define(divisor, `%o1') +define(Q, `%o2') +define(R, `%o3') +define(ITER, `%o4') +define(V, `%o5') + +/* m4 reminder: ifelse(a,b,c,d) => if a is b, then c, else d */ +define(T, `%g1') +define(SC, `%g7') +ifelse(S, `true', `define(SIGN, `%g6')') + +/* + * This is the recursive definition for developing quotient digits. + * + * Parameters: + * $1 the current depth, 1 <= $1 <= N + * $2 the current accumulation of quotient bits + * N max depth + * + * We add a new bit to $2 and either recurse or insert the bits in + * the quotient. R, Q, and V are inputs and outputs as defined above; + * the condition codes are expected to reflect the input R, and are + * modified to reflect the output R. + */ +define(DEVELOP_QUOTIENT_BITS, +` ! depth $1, accumulated bits $2 + bl L.$1.eval(TWOSUPN+$2) + srl V,1,V + ! remainder is positive + subcc R,V,R + ifelse($1, N, + ` b 9f + add Q, ($2*2+1), Q + ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2+1)')') +L.$1.eval(TWOSUPN+$2): + ! remainder is negative + addcc R,V,R + ifelse($1, N, + ` b 9f + add Q, ($2*2-1), Q + ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2-1)')') + ifelse($1, 1, `9:')') + +#include +#include + +FUNC(NAME) +ifelse(S, `true', +` ! compute sign of result; if neither is negative, no problem + orcc divisor, dividend, %g0 ! either negative? + bge 2f ! no, go do the divide + ifelse(OP, `div', + `xor divisor, dividend, SIGN', + `mov dividend, SIGN') ! compute sign in any case + tst divisor + bge 1f + tst dividend + ! divisor is definitely negative; dividend might also be negative + bge 2f ! if dividend not negative... + neg divisor ! in any case, make divisor nonneg +1: ! dividend is negative, divisor is nonnegative + neg dividend ! make dividend nonnegative +2: +') + ! Ready to divide. Compute size of quotient; scale comparand. + orcc divisor, %g0, V + bnz 1f + mov dividend, R + + ! Divide by zero trap. If it returns, return 0 (about as + ! wrong as possible, but that is what SunOS does...). + t ST_DIV0 + retl + clr %o0 + +1: + cmp R, V ! if divisor exceeds dividend, done + blu Lgot_result ! (and algorithm fails otherwise) + clr Q + sethi %hi(1 << (WORDSIZE - TOPBITS - 1)), T + cmp R, T + blu Lnot_really_big + clr ITER + + ! `Here the dividend is >= 2^(31-N) or so. We must be careful here, + ! as our usual N-at-a-shot divide step will cause overflow and havoc. + ! The number of bits in the result here is N*ITER+SC, where SC <= N. + ! Compute ITER in an unorthodox manner: know we need to shift V into + ! the top decade: so do not even bother to compare to R.' + 1: + cmp V, T + bgeu 3f + mov 1, SC + sll V, N, V + b 1b + inc ITER + + ! Now compute SC. + 2: addcc V, V, V + bcc Lnot_too_big + inc SC + + ! We get here if the divisor overflowed while shifting. + ! This means that R has the high-order bit set. + ! Restore V and subtract from R. + sll T, TOPBITS, T ! high order bit + srl V, 1, V ! rest of V + add V, T, V + b Ldo_single_div + dec SC + + Lnot_too_big: + 3: cmp V, R + blu 2b + nop + be Ldo_single_div + nop + /* NB: these are commented out in the V8-Sparc manual as well */ + /* (I do not understand this) */ + ! V > R: went too far: back up 1 step + ! srl V, 1, V + ! dec SC + ! do single-bit divide steps + ! + ! We have to be careful here. We know that R >= V, so we can do the + ! first divide step without thinking. BUT, the others are conditional, + ! and are only done if R >= 0. Because both R and V may have the high- + ! order bit set in the first step, just falling into the regular + ! division loop will mess up the first time around. + ! So we unroll slightly... + Ldo_single_div: + deccc SC + bl Lend_regular_divide + nop + sub R, V, R + mov 1, Q + b Lend_single_divloop + nop + Lsingle_divloop: + sll Q, 1, Q + bl 1f + srl V, 1, V + ! R >= 0 + sub R, V, R + b 2f + inc Q + 1: ! R < 0 + add R, V, R + dec Q + 2: + Lend_single_divloop: + deccc SC + bge Lsingle_divloop + tst R + b,a Lend_regular_divide + +Lnot_really_big: +1: + sll V, N, V + cmp V, R + bleu 1b + inccc ITER + be Lgot_result + dec ITER + + tst R ! set up for initial iteration +Ldivloop: + sll Q, N, Q + DEVELOP_QUOTIENT_BITS(1, 0) +Lend_regular_divide: + deccc ITER + bge Ldivloop + tst R + bl,a Lgot_result + ! non-restoring fixup here (one instruction only!) +ifelse(OP, `div', +` dec Q +', ` add R, divisor, R +') + +Lgot_result: +ifelse(S, `true', +` ! check to see if answer should be < 0 + tst SIGN + bl,a 1f + ifelse(OP, `div', `neg Q', `neg R') +1:') + retl + ifelse(OP, `div', `mov Q, %o0', `mov R, %o0') diff --git a/lib/nbsd_libc/arch/sparc/gen/fabs.S b/lib/nbsd_libc/arch/sparc/gen/fabs.S new file mode 100644 index 000000000..ef7dafa4b --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fabs.S @@ -0,0 +1,54 @@ +/* $NetBSD: fabs.S,v 1.4 2003/08/07 16:42:23 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: fabs.s,v 1.4 91/10/07 23:59:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)fabs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: fabs.S,v 1.4 2003/08/07 16:42:23 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* fabs - floating absolute value */ + + +ENTRY(fabs) + std %o0, [%sp + 32] ! return value => %f0:f1 + ldd [%sp + 32], %f0 ! (via kernel %o0/%o1 slot) + retl + fabss %f0, %f0 ! return absolute value diff --git a/lib/nbsd_libc/arch/sparc/gen/fixunsdfsi.S b/lib/nbsd_libc/arch/sparc/gen/fixunsdfsi.S new file mode 100644 index 000000000..504917fb7 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fixunsdfsi.S @@ -0,0 +1,93 @@ +/* $NetBSD: fixunsdfsi.S,v 1.5 2003/08/07 16:42:23 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: fixunsdfsi.s,v 1.3 91/10/08 00:03:15 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)fixunsdfsi.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: fixunsdfsi.S,v 1.5 2003/08/07 16:42:23 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Convert double to unsigned integer (for gcc). + * + * I have made the output for NaN agree with the Sun compiler, not + * that it really matters, by using `fbul,a'. + */ + + + .align 8 +Lbig: + .word 0x41e00000 ! .double 0r2147483648.0e+00 + .word 0 ! (who me, not trust the assembler?) + +ENTRY(__fixunsdfsi) + sub %sp, 8, %sp + std %o0, [%sp + 64] ! get argument into fpu reg + ldd [%sp + 64], %f0 +#ifdef PIC + PICCY_SET(Lbig, %g1, %o0) + ldd [%g1], %f2 +#else + sethi %hi(Lbig), %g1 + ldd [%g1 + %lo(Lbig)], %f2 +#endif + fcmped %f0, %f2 ! d < 2^31, or NaN, or -Inf? + nop ! (fpop2 delay) + fbul,a 1f ! if so, use fdtoi to convert to int + fdtoi %f0, %f0 ! (this includes negatives!) + + ! d does not fit in an int, so subtract 2^31, convert, + ! and add 2^31 again (sigh). Just hope the intermediate + ! fits (if not, the result is undefined anyway). + + fsubd %f0, %f2, %f0 ! d -= 2^31 + fdtoi %f0, %f0 ! convert to int + st %f0, [%sp + 64] ! move into return reg + ld [%sp + 64], %o0 + sethi %hi(0x80000000), %o1 + add %o0, %o1, %o0 ! add 2^31 + retl + add %sp, 8, %sp + +1: + st %f0, [%sp + 64] ! return result + ld [%sp + 64], %o0 + retl + add %sp, 8, %sp diff --git a/lib/nbsd_libc/arch/sparc/gen/flt_rounds.c b/lib/nbsd_libc/arch/sparc/gen/flt_rounds.c new file mode 100644 index 000000000..7f674836e --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/flt_rounds.c @@ -0,0 +1,30 @@ +/* $NetBSD: flt_rounds.c,v 1.6 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.6 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 0, /* round to zero */ + 2, /* round to positive infinity */ + 3 /* round to negative infinity */ +}; + +int +__flt_rounds() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return map[(x >> 30) & 0x03]; +} diff --git a/lib/nbsd_libc/arch/sparc/gen/fpgetmask.c b/lib/nbsd_libc/arch/sparc/gen/fpgetmask.c new file mode 100644 index 000000000..0639cb887 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fpgetmask.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetmask,_fpgetmask) +#endif + +fp_except +fpgetmask() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return (x >> 23) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc/gen/fpgetround.c b/lib/nbsd_libc/arch/sparc/gen/fpgetround.c new file mode 100644 index 000000000..2dffecfec --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fpgetround.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetround,_fpgetround) +#endif + +fp_rnd +fpgetround() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return (x >> 30) & 0x03; +} diff --git a/lib/nbsd_libc/arch/sparc/gen/fpgetsticky.c b/lib/nbsd_libc/arch/sparc/gen/fpgetsticky.c new file mode 100644 index 000000000..ebff93ff4 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fpgetsticky.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetsticky,_fpgetsticky) +#endif + +fp_except +fpgetsticky() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return (x >> 5) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc/gen/fpsetmask.c b/lib/nbsd_libc/arch/sparc/gen/fpsetmask.c new file mode 100644 index 000000000..99ca472c1 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fpsetmask.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetmask.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetmask,_fpsetmask) +#endif + +fp_except +fpsetmask(mask) + fp_except mask; +{ + fp_except old; + fp_except new; + + __asm("st %%fsr,%0" : "=m" (*&old)); + + new = old; + new &= ~(0x1f << 23); + new |= ((mask & 0x1f) << 23); + + __asm("ld %0,%%fsr" : : "m" (*&new)); + + return (old >> 23) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc/gen/fpsetround.c b/lib/nbsd_libc/arch/sparc/gen/fpsetround.c new file mode 100644 index 000000000..122feb266 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fpsetround.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetround,_fpsetround) +#endif + +fp_rnd +fpsetround(rnd_dir) + fp_rnd rnd_dir; +{ + fp_rnd old; + fp_rnd new; + + __asm("st %%fsr,%0" : "=m" (*&old)); + + new = old; + new &= ~(0x03 << 30); + new |= ((rnd_dir & 0x03) << 30); + + __asm("ld %0,%%fsr" : : "m" (*&new)); + + return (old >> 30) & 0x03; +} diff --git a/lib/nbsd_libc/arch/sparc/gen/fpsetsticky.c b/lib/nbsd_libc/arch/sparc/gen/fpsetsticky.c new file mode 100644 index 000000000..d0706902f --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/fpsetsticky.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetsticky.c,v 1.5 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetsticky,_fpsetsticky) +#endif + +fp_except +fpsetsticky(sticky) + fp_except sticky; +{ + fp_except old; + fp_except new; + + __asm("st %%fsr,%0" : "=m" (*&old)); + + new = old; + new &= ~(0x1f << 5); + new |= ((sticky & 0x1f) << 5); + + __asm("ld %0,%%fsr" : : "m" (*&new)); + + return (old >> 5) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc/gen/longjmp.c b/lib/nbsd_libc/arch/sparc/gen/longjmp.c new file mode 100644 index 000000000..825e02378 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/longjmp.c @@ -0,0 +1,86 @@ +/* $NetBSD: longjmp.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christian Limpach. + * + * 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 "namespace.h" +#include +#include +#include +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include + +/* + * Use setcontext to reload the stack pointer, program counter , and + * set the return value in %o0. The %i and %l registers will be reloaded + * from the place to which %sp points. + */ +void +__longjmp14(jmp_buf env, int val) +{ + struct sigcontext *sc = (void *)env; + ucontext_t uc; + + /* Ensure non-zero SP */ + if (sc->sc_sp == 0) + goto err; + + /* Initialise the fields we're going to use */ + uc.uc_link = 0; + + /* + * Set _UC_{SET,CLR}STACK according to SS_ONSTACK. + * + * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK, + * since libpthread may want to interpose on signal handling. + */ + uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK); + + sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL); + + /* Extract PSR, PC, NPC and SP from jmp_buf */ + uc.uc_mcontext.__gregs[_REG_PSR] = sc->sc_psr; + uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc; + uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_npc; + uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp; + + /* Set the return value; make sure it's non-zero */ + uc.uc_mcontext.__gregs[_REG_O0] = (val != 0 ? val : 1); + + setcontext(&uc); + +err: + longjmperror(); + abort(); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/sparc/gen/makecontext.c b/lib/nbsd_libc/arch/sparc/gen/makecontext.c new file mode 100644 index 000000000..1742bfb27 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/makecontext.c @@ -0,0 +1,75 @@ +/* $NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + int i; + unsigned long *sp; + va_list ap; + + sp = (unsigned long *) + ((unsigned long)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + /* Make room for: rwindow, struct return pointer, argd, argx */ + sp -= 8 + 8 + 1 + 6 + 1; /* CCFSZ, only in words */ + /* CCFSZ provides space for up to 7 arguments, add more if necessary */ + if (argc > 7) + sp -= argc - 7; + /* Align on double-word boundary. */ + sp = (unsigned long *)((unsigned long)sp & ~0x7); + + gr[_REG_O6] = (__greg_t)sp; + gr[_REG_PC] = (__greg_t)func; + gr[_REG_nPC] = (__greg_t)func + 4; + gr[_REG_O7] = (__greg_t)_resumecontext - 8; + + va_start(ap, argc); + /* Pass up to 6 arguments in %o0..%o5. */ + for (i = 0; i < argc && i < 6; i++) + gr[_REG_O0 + i] = va_arg(ap, unsigned long); + /* Pass any additional arguments on the stack. */ + for (/* i = 6 */; i < argc; i++) + sp[8 + 8 + 1 + 6 + (i - 6)] = va_arg(ap, unsigned long); + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/sparc/gen/modf.S b/lib/nbsd_libc/arch/sparc/gen/modf.S new file mode 100644 index 000000000..f7cce29fe --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/modf.S @@ -0,0 +1,197 @@ +/* $NetBSD: modf.S,v 1.6 2003/08/07 16:42:23 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: modf.s,v 1.3 92/06/20 00:00:54 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)modf.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: modf.S,v 1.6 2003/08/07 16:42:23 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#include + +/* + * double modf(double val, double *iptr) + * + * Returns the fractional part of `val', storing the integer part of + * `val' in *iptr. Both *iptr and the return value have the same sign + * as `val'. + * + * Method: + * + * We use the fpu's normalization hardware to compute the integer portion + * of the double precision argument. Sun IEEE double precision numbers + * have 52 bits of mantissa, 11 bits of exponent, and one bit of sign, + * with the sign occupying bit 31 of word 0, and the exponent bits 30:20 + * of word 0. Thus, values >= 2^52 are by definition integers. + * + * If we take a value that is in the range [+0..2^52) and add 2^52, all + * of the fractional bits fall out and all of the integer bits are summed + * with 2^52. If we then subtract 2^52, we get those integer bits back. + * This must be done with rounding set to `towards 0' or `towards -inf'. + * `Toward -inf' fails when the value is 0 (we get -0 back).... + * + * Note that this method will work anywhere, but is machine dependent in + * various aspects. + * + * Stack usage: + * 4@[%fp - 4] saved %fsr + * 4@[%fp - 8] new %fsr with rounding set to `towards 0' + * 8@[%fp - 16] space for moving between %i and %f registers + * Register usage: + * %i0%i1 double val; + * %l0 scratch + * %l1 sign bit (0x80000000) + * %i2 double *iptr; + * %f2:f3 `magic number' 2^52, in fpu registers + * %f4:f5 double v, in fpu registers + */ + + .align 8 +Lmagic: + .word 0x43300000 ! sign = 0, exponent = 52 + 1023, mantissa = 0 + .word 0 ! (i.e., .double 0r4503599627370496e+00) + +L0: + .word 0 ! 0.0 + .word 0 + +ENTRY(modf) + save %sp, -64-16, %sp + + /* + * First, compute v = abs(val) by clearing sign bit, + * and then set up the fpu registers. This would be + * much easier if we could do alu operations on fpu registers! + */ + sethi %hi(0x80000000), %l1 ! sign bit + andn %i0, %l1, %l0 + st %l0, [%fp - 16] +#ifdef PIC + PICCY_SET(Lmagic, %l0, %o7) + ldd [%l0], %f2 +#else + sethi %hi(Lmagic), %l0 + ldd [%l0 + %lo(Lmagic)], %f2 +#endif + st %i1, [%fp - 12] + ldd [%fp - 16], %f4 ! %f4:f5 = v + + /* + * Is %f4:f5 >= %f2:f3 ? If so, it is all integer bits. + * It is probably less, though. + */ + fcmped %f4, %f2 + nop ! fpop2 delay + fbuge Lbig ! if >= (or unordered), go out + nop + + /* + * v < 2^52, so add 2^52, then subtract 2^52, but do it all + * with rounding set towards zero. We leave any enabled + * traps enabled, but change the rounding mode. This might + * not be so good. Oh well.... + */ + st %fsr, [%fp - 4] ! %l5 = current FSR mode + set FSR_RD, %l3 ! %l3 = rounding direction mask + ld [%fp - 4], %l5 + set FSR_RD_RZ << FSR_RD_SHIFT, %l4 + andn %l5, %l3, %l6 + or %l6, %l4, %l6 ! round towards zero, please + and %l5, %l3, %l5 ! save original rounding mode + st %l6, [%fp - 8] + ld [%fp - 8], %fsr + + faddd %f4, %f2, %f4 ! %f4:f5 += 2^52 + fsubd %f4, %f2, %f4 ! %f4:f5 -= 2^52 + + /* + * Restore %fsr, but leave exceptions accrued. + */ + st %fsr, [%fp - 4] + ld [%fp - 4], %l6 + andn %l6, %l3, %l6 ! %l6 = %fsr & ~FSR_RD; + or %l5, %l6, %l5 ! %l5 |= %l6; + st %l5, [%fp - 4] + ld [%fp - 4], %fsr ! restore %fsr, leaving accrued stuff + + /* + * Now insert the original sign in %f4:f5. + * This is a lot of work, so it is conditional here. + */ + btst %l1, %i0 + be 1f + nop + st %f4, [%fp - 16] + ld [%fp - 16], %g1 + or %l1, %g1, %g1 + st %g1, [%fp - 16] + ld [%fp - 16], %f4 +1: + + /* + * The value in %f4:f5 is now the integer portion of the original + * argument. We need to store this in *ival (%i2), subtract it + * from the original value argument (%i0:i1), and return the result. + */ + std %f4, [%i2] ! *ival = %f4:f5; + std %i0, [%fp - 16] + ldd [%fp - 16], %f0 ! %f0:f1 = val; + fsubd %f0, %f4, %f0 ! %f0:f1 -= %f4:f5; + ret + restore + +Lbig: + /* + * We get here if the original comparison of %f4:f5 (v) to + * %f2:f3 (2^52) came out `greater or unordered'. In this + * case the integer part is the original value, and the + * fractional part is 0. + */ +#ifdef PIC + PICCY_SET(L0, %l0, %o7) + std %f0, [%i2] ! *ival = val; + ldd [%l0], %f0 ! return 0.0; +#else + sethi %hi(L0), %l0 + std %f0, [%i2] ! *ival = val; + ldd [%l0 + %lo(L0)], %f0 ! return 0.0; +#endif + ret + restore diff --git a/lib/nbsd_libc/arch/sparc/gen/nanf.c b/lib/nbsd_libc/arch/sparc/gen/nanf.c new file mode 100644 index 000000000..1688d0eae --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0x7f, 0xc0, 0, 0 } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/sparc/gen/resumecontext.c b/lib/nbsd_libc/arch/sparc/gen/resumecontext.c new file mode 100644 index 000000000..1a7c4c55f --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/sparc/gen/setjmp.S b/lib/nbsd_libc/arch/sparc/gen/setjmp.S new file mode 100644 index 000000000..993b92761 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/setjmp.S @@ -0,0 +1,86 @@ +/* $NetBSD: setjmp.S,v 1.11 2007/10/08 13:06:00 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: setjmp.s,v 1.2 92/06/25 03:18:43 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)setjmp.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: setjmp.S,v 1.11 2007/10/08 13:06:00 uwe Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +#include "SYS.h" + +ENTRY(__setjmp14) + save %sp, -CCFSZ, %sp + /* + * local vars on stack: struct sigstack foo @ offset 0x48 + * Note: sc_g1 is not used; sc_o0 is set in longjmp() + */ + mov 1, %o0 /* SIG_BLOCK */ + add %i0, 32, %o2 /* build sigcontext sc.sc_mask */ + call _C_LABEL(__sigprocmask14) + clr %o1 /* sigprocmask(SIG_BLOCK, NULL, &sc.sc_mask) */ + + add %sp, 0x48, %o1 /* (foo being in arg dump area) */ + call _C_LABEL(__sigaltstack14) + clr %o0 /* sigstack(NULL, &foo) */ + + ld [%sp + 0x48 + 8], %o0 /* foo.ss_flags */ + and %o0, 1, %o1 /* onstack = foo.ss_flags & 1; */ + st %o1, [%i0 + 0] /* sc.sc_onstack = current onstack; */ + st %fp, [%i0 + 8] /* sc.sc_sp = (caller's) sp */ + add %i7, 8, %o0 + st %o0, [%i0 + 12] /* sc.sc_pc = return_pc */ + add %i7, 12, %o0 + st %o0, [%i0 + 16] /* sc.sc_npc = return_pc + 4 */ + st %g0, [%i0 + 20] /* sc.sc_psr = (clean psr) */ + ret /* return 0 */ + restore %g0, %g0, %o0 + diff --git a/lib/nbsd_libc/arch/sparc/gen/sigsetjmp.S b/lib/nbsd_libc/arch/sparc/gen/sigsetjmp.S new file mode 100644 index 000000000..44958e617 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/sigsetjmp.S @@ -0,0 +1,93 @@ +/* $NetBSD: sigsetjmp.S,v 1.6 2007/10/08 13:06:00 uwe Exp $ */ +/* + * Copyright (c) 1995 Paul Kranenburg + * 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 by Paul Kranenburg. + * 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. + * + */ + +#include "SYS.h" + +#ifdef PIC + +ENTRY(__sigsetjmp14) + PIC_PROLOGUE(%g1, %g2) ! %g1 = _GLOBAL_OFFSET_TABLE + tst %o1 + bnz 1f + st %o1, [%o0 + 56] ! jmpbuf[JBLEN] = savemask + + !! if (savemask == 0) goto _setjmp + set _C_LABEL(_setjmp), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + + !! if (savemask != 0) goto setjmp +1: set _C_LABEL(__setjmp14), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + + +ENTRY(__siglongjmp14) + PIC_PROLOGUE(%g1, %g2) ! %g1 = _GLOBAL_OFFSET_TABLE + ld [%o0 + 56], %g2 ! restoremask = jmpbuf[JBLEN] + tst %g2 + bnz 1f + nop + + !! if (restoremask == 0) goto _longjmp + set _C_LABEL(_longjmp), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + + !! if (restoremask != 0) goto longjmp +1: set _C_LABEL(__longjmp14), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + +#else /* !PIC */ + +ENTRY(__sigsetjmp14) + cmp %o1,0 + be _C_LABEL(_setjmp) + st %o1,[%o0+56] ! jmpbuf[JBLEN] + nop + ba,a _C_LABEL(__setjmp14) + unimp 0 + +ENTRY(__siglongjmp14) + ld [%o0+56],%g1 + cmp %g1,0 + be _C_LABEL(_longjmp) + nop + ba,a _C_LABEL(__longjmp14) + unimp 0 + +#endif /* !PIC */ diff --git a/lib/nbsd_libc/arch/sparc/gen/swapcontext.S b/lib/nbsd_libc/arch/sparc/gen/swapcontext.S new file mode 100644 index 000000000..8b88d296a --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/gen/swapcontext.S @@ -0,0 +1,56 @@ +/* $NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + +ENTRY(swapcontext) + mov %o0, %o2 ! must save pointer + mov %o1, %o3 ! must save pointer + mov SYS_getcontext, %g1 + t ST_SYSCALL + bcc,a 1f + st %g0, [%o2 + 40 + 11 * 4] ! gr[_REG_O0] = 0 + ERROR() +1: + add %o7, 12, %o1 + st %o1, [%o2 + 40 + 2 * 4] ! gr[_REG_nPC] = retaddr + 4 + add %o7, 8, %o1 + st %o1, [%o2 + 40 + 1 * 4] ! gr[_REG_PC] = retaddr + + mov %o3, %o0 + mov %o1, %g2 ! optimize `return' + mov SYS_setcontext|SYSCALL_G2RFLAG, %g1 + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/arch/sparc/net/Makefile.inc b/lib/nbsd_libc/arch/sparc/net/Makefile.inc new file mode 100644 index 000000000..0279aaa3b --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/net/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 1995/02/25 14:59:11 cgd Exp $ + +SRCS+= htonl.S htons.S ntohl.S ntohs.S diff --git a/lib/nbsd_libc/arch/sparc/stdlib/Makefile.inc b/lib/nbsd_libc/arch/sparc/stdlib/Makefile.inc new file mode 100644 index 000000000..102817f9c --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/stdlib/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/08/11 17:30:43 dsl Exp $ + +SRCS+= llabs.S +NO_SRCS+= imaxabs.S diff --git a/lib/nbsd_libc/arch/sparc/stdlib/llabs.S b/lib/nbsd_libc/arch/sparc/stdlib/llabs.S new file mode 100644 index 000000000..ab81609cc --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/stdlib/llabs.S @@ -0,0 +1,67 @@ +/* $NetBSD: llabs.S,v 1.4 2008/08/04 21:29:28 matt Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: abs.s,v 1.1 91/07/06 18:01:57 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "from: @(#)abs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: llabs.S,v 1.4 2008/08/04 21:29:28 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(llabs, _llabs) +WEAK_ALIAS(imaxabs, _llabs) +#endif + +/* llabs - long long int absolute value */ +/* imaxabs - intmax_t absolute value */ + + +#ifdef WEAK_ALIAS +ENTRY(_llabs) +#else +ENTRY(llabs) +#endif + tst %o0 + bge 1f + nop + subcc %g0, %o1, %o1 + subx %g0, %o0, %o0 +1: retl + nop diff --git a/lib/nbsd_libc/arch/sparc/string/Makefile.inc b/lib/nbsd_libc/arch/sparc/string/Makefile.inc new file mode 100644 index 000000000..ed9c5d329 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/string/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/07/30 20:57:17 dsl Exp $ + +SRCS+= bzero.S ffs.S memset.S strlen.S diff --git a/lib/nbsd_libc/arch/sparc/sys/__clone.S b/lib/nbsd_libc/arch/sparc/sys/__clone.S new file mode 100644 index 000000000..c420ded64 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/__clone.S @@ -0,0 +1,93 @@ +/* $NetBSD: __clone.S,v 1.6 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + save %sp, -CCFSZ, %sp + + /* + * Sanity checks: func and stack may not be NULL. + */ + tst %i0 + be 8f ! func == NULL, bail + orcc %i1, %g0, %o1 ! setup stack arg for syscall, test + be 8f ! stack == NULL, bail + mov %i2, %o0 ! setup flags arg for syscall + + /* + * Allocate "caller's" frame in the child stack as ABI + * requires. + * + * We pass the function and the argument to the child by + * stashing them at the bottom of the frame. There they are + * safe from window spill would we need to take one as it's + * below the window save area. + */ + sub %o1, CCFSZ, %o1 ! make space on the child's stack + st %i0, [%o1+CCFSZ-8] ! save func + st %i3, [%o1+CCFSZ-4] ! save arg + + /* + * Args are now set up for system call as (flags, stack). + */ + mov SYS___clone, %g1 + t ST_SYSCALL + bcs 9f + tst %o1 ! %o1 (rv[1]) == 0 in parent + +1: bz 2f ! yes, parent + ld [%sp+CCFSZ-8], %l0 ! grab the function... + call %l0 ! Call the clone's entry point. + ld [%sp+CCFSZ-4], %o0 ! ...and the argument from the stack + + CALL(_C_LABEL(_exit)) + /* NOTREACHED */ + +2: ret + restore %g0, %o0, %o0 + +8: restore %g0, EINVAL, %o0 + ERROR() + /* NOTREACHED */ + +9: restore %g0, %o0, %o0 + ERROR() + /* NOTREACHED */ diff --git a/lib/nbsd_libc/arch/sparc/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/sparc/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..263296403 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/__sigaction14_sigtramp.c @@ -0,0 +1,72 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.7 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.7 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern void __sigtramp_siginfo_2(void); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the compatibility SIGCONTEXT trampoline if SA_SIGINFO + * is not set in the sigaction. + */ + if (act && (act->sa_flags & SA_SIGINFO) == 0) { + extern void __sigtramp_sigcontext_1(void); + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + /* + * EINVAL might indicate that trampoline version 1 is + * not supported by the kernel; fall back on native + * SIGINFO trampoline. + */ + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/sparc/sys/__sigtramp2.S b/lib/nbsd_libc/arch/sparc/sys/__sigtramp2.S new file mode 100644 index 000000000..3cb90dad3 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/__sigtramp2.S @@ -0,0 +1,52 @@ +/* $NetBSD: __sigtramp2.S,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Paul Kranenburg. + * + * 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 +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __sigtramp2.S,v 1.2 2008/04/28 20:22:57 martin Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * When this code is run, the stack looks like: + * [%sp] struct frame + * [%sp + 96] siginfo_t + * [%sp + 96 + 128] ucontext_t + * . + * . + */ +ENTRY_NOPROFILE(__sigtramp_siginfo_2) + add %sp, 96 + 128, %o0 /* get pointer to ucontext */ + mov SYS_setcontext, %g1 + t ST_SYSCALL /* call setcontext */ + mov SYS_exit, %g1 /* exit with errno */ + t ST_SYSCALL /* if sigreturn fails */ diff --git a/lib/nbsd_libc/arch/sparc/sys/__syscall.S b/lib/nbsd_libc/arch/sparc/sys/__syscall.S new file mode 100644 index 000000000..1e00e4205 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/__syscall.S @@ -0,0 +1,39 @@ +/* $NetBSD: __syscall.S,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Paul Kranenburg. + * + * 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 +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __syscall.S,v 1.2 2008/04/28 20:22:57 martin Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/sparc/sys/__vfork14.S b/lib/nbsd_libc/arch/sparc/sys/__vfork14.S new file mode 100644 index 000000000..74cc932ce --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/__vfork14.S @@ -0,0 +1,59 @@ +/* $NetBSD: __vfork14.S,v 1.2 2003/08/07 16:42:25 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: Ovfork.s,v 1.1 91/07/06 13:05:56 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)Ovfork.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: __vfork14.S,v 1.2 2003/08/07 16:42:25 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +/* + * pid = vfork(); + * + * %o1 == 0 in parent process, 1 in child process. + * %o0 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +SYSCALL(__vfork14) + dec %o1 ! from 1 to 0 in child, 0 to -1 in parent + retl + and %o0, %o1, %o0 ! return 0 in child, pid in parent diff --git a/lib/nbsd_libc/arch/sparc/sys/brk.S b/lib/nbsd_libc/arch/sparc/sys/brk.S new file mode 100644 index 000000000..20f996937 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/brk.S @@ -0,0 +1,98 @@ +/* $NetBSD: brk.S,v 1.15 2008/11/06 13:42:55 nakayama Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: brk.s,v 1.3 92/06/25 12:56:05 mccanne Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)brk.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: brk.S,v 1.15 2008/11/06 13:42:55 nakayama Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl CURBRK + .globl _C_LABEL(__minbrk) + .globl _end + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data +_C_LABEL(__minbrk): + .long _end ! lower brk limit; also for gmon code + .text + +ENTRY(_brk) +#ifdef PIC + PIC_PROLOGUE(%o5,%o4) + set _C_LABEL(__minbrk), %o4 + ld [%o5 + %o4], %o4 + ld [%o4], %o1 ! %o1 = minbrk + cmp %o1, %o0 ! if (minbrk > %o0) + bgu,a 0f + mov %o1, %o0 ! %o0 = minbrk +0: + mov %o0, %o2 ! save argument to syscall + mov SYS_break, %g1 + t ST_SYSCALL + set CURBRK, %o3 + bcc,a 1f + ld [%o5 + %o3], %o4 + ERROR() +1: + retl ! success, return 0 & record new break + st %o2, [%o4] +#else + sethi %hi(_C_LABEL(__minbrk)), %o1 ! %o1 = minbrk + ld [%o1 + %lo(_C_LABEL(__minbrk))], %o1 + cmp %o1, %o0 ! if (minbrk > %o0) + bgu,a 0f + mov %o1, %o0 ! %o0 = minbrk +0: + mov %o0, %o2 ! save argument to syscall + mov SYS_break, %g1 + t ST_SYSCALL + bcc,a 1f + sethi %hi(CURBRK), %g1 + ERROR() +1: + retl ! success, return 0 & record new break + st %o2, [%g1 + %lo(CURBRK)] +#endif diff --git a/lib/nbsd_libc/arch/sparc/sys/cerror.S b/lib/nbsd_libc/arch/sparc/sys/cerror.S new file mode 100644 index 000000000..ee02e04c7 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/cerror.S @@ -0,0 +1,76 @@ +/* $NetBSD: cerror.S,v 1.10 2007/10/08 13:06:00 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: cerror.s,v 1.3 92/07/02 04:17:59 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)cerror.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: cerror.S,v 1.10 2007/10/08 13:06:00 uwe Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef _REENTRANT +FUNC(CERROR) + save %sp, -CCFSZ, %sp + call _C_LABEL(__errno) + nop + st %i0, [%o0] + mov -1, %i0 + ret + restore %g0, -1, %o1 +#else + .globl _C_LABEL(errno) +#ifdef PIC +FUNC(CERROR) + set _C_LABEL(errno), %g2 + ld [%g1 + %g2], %g1 + st %o0, [%g1] + mov -1, %o0 + retl + mov -1, %o1 +#else +FUNC(CERROR) + sethi %hi(_C_LABEL(errno)), %g1 + st %o0, [%g1 + %lo(_C_LABEL(errno))] + mov -1, %o0 + retl + mov -1, %o1 +#endif +#endif /* _REENTRANT */ diff --git a/lib/nbsd_libc/arch/sparc/sys/exect.S b/lib/nbsd_libc/arch/sparc/sys/exect.S new file mode 100644 index 000000000..208cef90b --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/exect.S @@ -0,0 +1,52 @@ +/* $NetBSD: exect.S,v 1.3 2003/08/07 16:42:25 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: exect.s,v 1.1 91/07/06 13:05:57 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)exect.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: exect.S,v 1.3 2003/08/07 16:42:25 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(exect) + mov SYS_execve, %g1 /* execve(file, argv, env) */ + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/arch/sparc/sys/fork.S b/lib/nbsd_libc/arch/sparc/sys/fork.S new file mode 100644 index 000000000..1ffb1901b --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/fork.S @@ -0,0 +1,52 @@ +/* $NetBSD: fork.S,v 1.5 2003/08/07 16:42:25 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: fork.s,v 1.1 91/07/06 13:05:58 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)fork.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: fork.S,v 1.5 2003/08/07 16:42:25 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +_SYSCALL(__fork,fork) + dec %o1 ! from 1 to 0 in child, 0 to -1 in parent + retl + and %o0, %o1, %o0 ! return 0 in child, pid in parent diff --git a/lib/nbsd_libc/arch/sparc/sys/getcontext.S b/lib/nbsd_libc/arch/sparc/sys/getcontext.S new file mode 100644 index 000000000..d5788bb01 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/getcontext.S @@ -0,0 +1,54 @@ +/* $NetBSD: getcontext.S,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.2 2008/04/28 20:22:57 martin Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +ENTRY(_getcontext) + mov %o0, %o2 ! must save pointer + mov SYS_getcontext, %g1 + t ST_SYSCALL + bcc,a 1f + clr [%o2 + 40 + 11 * 4] ! gr[_REG_O0] = 0 + ERROR() +1: + add %o7, 8, %o1 + st %o1, [%o2 + 40 + 1 * 4] ! gr[_REG_PC] = retaddr + add %o7, 12, %o1 + retl + st %o1, [%o2 + 40 + 2 * 4] ! gr[_REG_nPC] = retaddr + 4 diff --git a/lib/nbsd_libc/arch/sparc/sys/pipe.S b/lib/nbsd_libc/arch/sparc/sys/pipe.S new file mode 100644 index 000000000..457c713f3 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/pipe.S @@ -0,0 +1,63 @@ +/* $NetBSD: pipe.S,v 1.4 2003/08/07 16:42:25 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: pipe.s,v 1.1 91/07/06 13:05:58 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)pipe.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: pipe.S,v 1.4 2003/08/07 16:42:25 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +ENTRY(_pipe) + mov %o0, %o2 ! save pointer + mov SYS_pipe, %g1 + t ST_SYSCALL ! pipe() + bcc,a 1f + st %o0, [%o2] ! success, store fds + ERROR() +1: + st %o1, [%o2 + 4] + retl ! and return 0 + clr %o0 diff --git a/lib/nbsd_libc/arch/sparc/sys/ptrace.S b/lib/nbsd_libc/arch/sparc/sys/ptrace.S new file mode 100644 index 000000000..e5507bbe5 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/ptrace.S @@ -0,0 +1,74 @@ +/* $NetBSD: ptrace.S,v 1.8 2007/10/08 13:06:00 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ptrace.s,v 1.2 91/12/20 01:59:00 leres Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ptrace.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ptrace.S,v 1.8 2007/10/08 13:06:00 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(ptrace) +#ifdef _REENTRANT + save %sp, -CCFSZ, %sp + call _C_LABEL(__errno) + nop + st %g0, [%o0] + restore +#else +#ifdef PIC + PIC_PROLOGUE(%g1,%g2) + set _C_LABEL(errno), %g2 + ld [%g1 + %g2], %g1 + st %g0, [%g1] +#else + sethi %hi(_C_LABEL(errno)), %g1 + st %g0, [%g1 + %lo(_C_LABEL(errno))] +#endif +#endif /* !_REENTRANT */ + mov SYS_ptrace, %g1 + t ST_SYSCALL + bcc 1f + nop + ERROR() +1: + retl + nop diff --git a/lib/nbsd_libc/arch/sparc/sys/sbrk.S b/lib/nbsd_libc/arch/sparc/sys/sbrk.S new file mode 100644 index 000000000..5eebf90d3 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/sbrk.S @@ -0,0 +1,89 @@ +/* $NetBSD: sbrk.S,v 1.10 2007/10/08 13:06:00 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sbrk.s,v 1.3 92/07/02 00:56:49 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sbrk.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: sbrk.S,v 1.10 2007/10/08 13:06:00 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _end + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +CURBRK: .long _end + .text + +ENTRY(_sbrk) +#ifdef PIC + PIC_PROLOGUE(%o5,%o4) + set CURBRK, %o2 + ld [%o5 + %o2], %o2 + ld [%o2], %o3 ! %o3 = old break + add %o3, %o0, %o4 ! %o4 = new break + mov %o4, %o0 ! copy for syscall + mov SYS_break, %g1 + t ST_SYSCALL ! break(new_break) + bcc,a 1f ! if success, + mov %o3, %o0 ! set return value + ERROR() +1: + retl ! and update curbrk + st %o4, [%o2] +#else + sethi %hi(CURBRK), %o2 + ld [%o2 + %lo(CURBRK)], %o3 ! %o3 = old break + add %o3, %o0, %o4 ! %o4 = new break + mov %o4, %o0 ! copy for syscall + mov SYS_break, %g1 + t ST_SYSCALL ! break(new_break) + bcc,a 1f ! if success, + mov %o3, %o0 ! set return value + ERROR() +1: + retl ! and update curbrk + st %o4, [%o2 + %lo(CURBRK)] +#endif diff --git a/lib/nbsd_libc/arch/sparc/sys/shmat.S b/lib/nbsd_libc/arch/sparc/sys/shmat.S new file mode 100644 index 000000000..7f57fad5f --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:54 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/sparc/sys/syscall.S b/lib/nbsd_libc/arch/sparc/sys/syscall.S new file mode 100644 index 000000000..b824c4312 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc/sys/syscall.S @@ -0,0 +1,49 @@ +/* $NetBSD: syscall.S,v 1.4 2003/08/07 16:42:26 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: syscall.s,v 1.1 91/07/06 13:06:02 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)syscall.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: syscall.S,v 1.4 2003/08/07 16:42:26 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/sparc64/Makefile.inc b/lib/nbsd_libc/arch/sparc64/Makefile.inc new file mode 100644 index 000000000..a3a651be4 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/Makefile.inc @@ -0,0 +1,50 @@ +# $NetBSD: Makefile.inc,v 1.13 2009/12/06 07:05:50 uebayasi Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S + +# Some assembler files use v9a extensions. +AFLAGS+= -Wa,-Av9a + +# `source' files built from m4 source +# the name `div.o' is taken for the ANSI C `div' function, hence sdiv here +SRCS+= rem.S sdiv.S udiv.S urem.S +CLEANFILES+=rem.S sdiv.S udiv.S urem.S + +sdiv.S: ${LIBCDIR}/arch/sparc64/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.div')define(OP,\`div')define(S,\`true')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} + +udiv.S: ${LIBCDIR}/arch/sparc64/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.udiv')define(OP,\`div')define(S,\`false')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} + +rem.S: ${LIBCDIR}/arch/sparc64/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.rem')define(OP,\`rem')define(S,\`true')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} + +urem.S: ${LIBCDIR}/arch/sparc64/gen/divrem.m4 + @echo 'building ${.TARGET} from ${.ALLSRC}' + @(echo "define(NAME,\`.urem')define(OP,\`rem')define(S,\`false')"; \ + cat ${.ALLSRC}) | ${TOOL_M4} > ${.TARGET} + @chmod 444 ${.TARGET} + +.PATH: ${ARCHDIR}/softfloat +SRCS+= qp.c +CPPFLAGS+= -DSOFTFLOATSPARC64_FOR_GCC +.if ${MKSOFTFLOAT} != "no" +.include +.else +# +# Can't directly use softfloat.c due to .PATH issues with the other source +# in softfloat. Just provide a wrapper which pulls it in via a #include +SRCS.softfloat= softfloat-wrapper.c +SRCS+= ${SRCS.softfloat} +CPPFLAGS+= -I${ARCHDIR}/softfloat -I${.CURDIR}/softfloat \ + -I${.CURDIR}/softfloat/bits64 +.endif diff --git a/lib/nbsd_libc/arch/sparc64/SYS.h b/lib/nbsd_libc/arch/sparc64/SYS.h new file mode 100644 index 000000000..ede6a3e96 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/SYS.h @@ -0,0 +1,135 @@ +/* $NetBSD: SYS.h,v 1.13 2003/08/07 16:42:26 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * @(#)SYS.h 8.1 (Berkeley) 6/4/93 + * + * from: Header: SYS.h,v 1.2 92/07/03 18:57:00 torek Exp + */ + +#include +#include +#include + +#ifdef __STDC__ +#define _CAT(x,y) x##y +#else +#define _CAT(x,y) x/**/y +#endif + +/* + * ERROR branches to cerror. This is done with a macro so that I can + * change it to be position independent later, if need be. + */ +#ifdef PIC +#ifdef BIGPIC +#define JUMP(name) \ + PIC_PROLOGUE(%g1,%g5); \ + sethi %hi(_C_LABEL(name)),%g5; \ + or %g5,%lo(_C_LABEL(name)),%g5; \ + ldx [%g1+%g5],%g5; \ + jmp %g5; \ + nop +#else +#define JUMP(name) \ + PIC_PROLOGUE(%g1,%g5); \ + ldx [%g1+_C_LABEL(name)],%g5; jmp %g5; nop +#endif +#else +#define JUMP(name) set _C_LABEL(name),%g1; jmp %g1; nop +#endif +#define ERROR() JUMP(__cerror) +/* + * SYSCALL is used when further action must be taken before returning. + * Note that it adds a `nop' over what we could do, if we only knew what + * came at label 1.... + */ +#define _SYSCALL(x,y) \ + ENTRY(x); mov _CAT(SYS_,y),%g1; t ST_SYSCALL; bcc 1f; nop; ERROR(); 1: + +#define SYSCALL(x) \ + _SYSCALL(x,x) + +/* + * RSYSCALL is used when the system call should just return. Here + * we use the SYSCALL_G7RFLAG to put the `success' return address in %g7 + * and avoid a branch. + */ +#define RSYSCALL(x) \ + ENTRY(x); mov (_CAT(SYS_,x))|SYSCALL_G7RFLAG,%g1; add %o7,8,%g7; \ + t ST_SYSCALL; ERROR() + +/* + * PSEUDO(x,y) is like RSYSCALL(y) except that the name is x. + */ +#define PSEUDO(x,y) \ + ENTRY(x); mov (_CAT(SYS_,y))|SYSCALL_G7RFLAG,%g1; add %o7,8,%g7; \ + t ST_SYSCALL; ERROR() + +/* + * WSYSCALL(weak,strong) is like RSYSCALL(weak), except that weak is + * a weak internal alias for the strong symbol. + */ +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) + +/* + * SYSCALL_NOERROR is like SYSCALL, except it's used for syscalls + * that never fail. + * + * XXX - This should be optimized. + */ +#define SYSCALL_NOERROR(x) \ + ENTRY(x); mov _CAT(SYS_,x),%g1; t ST_SYSCALL + +/* + * RSYSCALL_NOERROR is like RSYSCALL, except it's used for syscalls + * that never fail. + * + * XXX - This should be optimized. + */ +#define RSYSCALL_NOERROR(x) \ + ENTRY(x); mov (_CAT(SYS_,x))|SYSCALL_G7RFLAG,%g1; add %o7,8,%g7; \ + t ST_SYSCALL + +/* + * PSEUDO_NOERROR(x,y) is like RSYSCALL_NOERROR(y) except that the name is x. + */ +#define PSEUDO_NOERROR(x,y) \ + ENTRY(x); mov (_CAT(SYS_,y))|SYSCALL_G7RFLAG,%g1; add %o7,8,%g7; \ + t ST_SYSCALL + + .register %g7,#scratch + + .globl _C_LABEL(__cerror) diff --git a/lib/nbsd_libc/arch/sparc64/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/sparc64/gdtoa/Makefile.inc new file mode 100644 index 000000000..c13f9a737 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gdtoa/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c strtold_pQ.c +SRCS+= strtopQ.c diff --git a/lib/nbsd_libc/arch/sparc64/gdtoa/arith.h b/lib/nbsd_libc/arch/sparc64/gdtoa/arith.h new file mode 100644 index 000000000..4674f629c --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_BIG_ENDIAN diff --git a/lib/nbsd_libc/arch/sparc64/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/sparc64/gdtoa/gd_qnan.h new file mode 100644 index 000000000..860e1b064 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gdtoa/gd_qnan.h @@ -0,0 +1,9 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x7ff80000 +#define d_QNAN1 0x0 +#define ld_QNAN0 0x7fff8000 +#define ld_QNAN1 0x0 +#define ld_QNAN2 0x0 +#define ld_QNAN3 0x0 diff --git a/lib/nbsd_libc/arch/sparc64/gen/Makefile.inc b/lib/nbsd_libc/arch/sparc64/gen/Makefile.inc new file mode 100644 index 000000000..3753cba45 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/Makefile.inc @@ -0,0 +1,30 @@ +# $NetBSD: Makefile.inc,v 1.19 2009/12/06 07:12:17 uebayasi Exp $ + +SRCS+= fabs.S modf.S +SRCS+= flt_rounds.c fpgetmask.c fpgetround.c fpgetsticky.c fpsetmask.c \ + fpsetround.c fpsetsticky.c + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c infinityl.c +SRCS+= fpclassifyl.c isfinitel.c isinfl.c isnanl.c signbitl.c + +SRCS+= bswap16.c bswap32.c bswap64.c +SRCS+= setjmp.S +SRCS+= _setjmp.S +SRCS+= sigsetjmp.S longjmp.c + +SRCS+= makecontext.c resumecontext.c _lwp.c swapcontext.S + +SRCS+= fixunsdfsi.S mul.S umul.S saveregs.S + +LSRCS.sparc64.gen=Lint_swapcontext.c +LSRCS+= ${LSRCS.sparc64.gen} +DPSRCS+= ${LSRCS.sparc64.gen} +CLEANFILES+= ${LSRCS.sparc64.gen} diff --git a/lib/nbsd_libc/arch/sparc64/gen/_lwp.c b/lib/nbsd_libc/arch/sparc64/gen/_lwp.c new file mode 100644 index 000000000..b217d28c4 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/_lwp.c @@ -0,0 +1,73 @@ +/* $NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.5 2008/04/28 20:22:57 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, + caddr_t stack_base, size_t stack_size) +{ + __greg_t *gr; + unsigned long *sp; + + getcontext(u); + gr = u->uc_mcontext.__gregs; + + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + sp = (ulong *)(stack_base + stack_size); + sp = (ulong *)((ulong)sp & ~0x0f); + + sp -= 8 + 8 + 6; + + sp[8] = (ulong)arg; + sp[14] = (ulong)sp - 2047; + sp[15] = (ulong)_lwp_exit - 8; + + sp = (ulong *)((caddr_t)sp - 2047); + + gr[_REG_PC] = (ulong) start; + gr[_REG_nPC] = (ulong) start + 4; + + gr[_REG_O0] = (ulong) arg; + gr[_REG_O6] = (ulong) sp; + gr[_REG_O7] = (ulong)_lwp_exit - 8; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/_setjmp.S b/lib/nbsd_libc/arch/sparc64/gen/_setjmp.S new file mode 100644 index 000000000..4990bb51f --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/_setjmp.S @@ -0,0 +1,76 @@ +/* $NetBSD: _setjmp.S,v 1.6 2004/12/07 08:20:07 martin Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: _setjmp.s,v 1.1 91/07/06 16:45:53 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)_setjmp.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: _setjmp.S,v 1.6 2004/12/07 08:20:07 martin Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v?v:1)" from + * the last call to + * _setjmp(a) + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + stx %sp, [%o0+0] /* store caller's stack pointer */ + stx %o7, [%o0+8] /* and return pc */ + retl + clr %o0 ! return 0 + +ENTRY(_longjmp) + save %sp, -CC64FSZ, %sp + flushw + /* + * We restore the saved stack pointer to %fp, then issue + * a `restore' instruction which will reload the register + * window from the stack. + */ + ldx [%i0+8], %i7 + ldx [%i0+0], %fp + mov 1, %i0 + movrnz %i1, %i1, %i0 ! compute v ? v : 1 + ret + restore diff --git a/lib/nbsd_libc/arch/sparc64/gen/divrem.m4 b/lib/nbsd_libc/arch/sparc64/gen/divrem.m4 new file mode 100644 index 000000000..108918e72 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/divrem.m4 @@ -0,0 +1,273 @@ +/* $NetBSD: divrem.m4,v 1.4 2003/08/07 16:42:27 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: divrem.m4,v 1.4 92/06/25 13:23:57 torek Exp + */ + +#include +#include + +/* + * Division and remainder, from Appendix E of the Sparc Version 8 + * Architecture Manual, with fixes from Gordon Irlam. + */ + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: divrem.m4,v 1.4 2003/08/07 16:42:27 agc Exp $") +#endif + +/* + * Input: dividend and divisor in %o0 and %o1 respectively. + * + * m4 parameters: + * NAME name of function to generate + * OP OP=div => %o0 / %o1; OP=rem => %o0 % %o1 + * S S=true => signed; S=false => unsigned + * + * Algorithm parameters: + * N how many bits per iteration we try to get (4) + * WORDSIZE total number of bits (32) + * + * Derived constants: + * TWOSUPN 2^N, for label generation (m4 exponentiation currently broken) + * TOPBITS number of bits in the top `decade' of a number + * + * Important variables: + * Q the partial quotient under development (initially 0) + * R the remainder so far, initially the dividend + * ITER number of main division loop iterations required; + * equal to ceil(log2(quotient) / N). Note that this + * is the log base (2^N) of the quotient. + * V the current comparand, initially divisor*2^(ITER*N-1) + * + * Cost: + * Current estimate for non-large dividend is + * ceil(log2(quotient) / N) * (10 + 7N/2) + C + * A large dividend is one greater than 2^(31-TOPBITS) and takes a + * different path, as the upper bits of the quotient must be developed + * one bit at a time. + */ + +define(N, `4') +define(TWOSUPN, `16') +define(WORDSIZE, `32') +define(TOPBITS, eval(WORDSIZE - N*((WORDSIZE-1)/N))) + +define(dividend, `%o0') +define(divisor, `%o1') +define(Q, `%o2') +define(R, `%o3') +define(ITER, `%o4') +define(V, `%o5') + +/* m4 reminder: ifelse(a,b,c,d) => if a is b, then c, else d */ +define(T, `%g1') +define(SC, `%g5') +ifelse(S, `true', `define(SIGN, `%g6')') + +/* + * This is the recursive definition for developing quotient digits. + * + * Parameters: + * $1 the current depth, 1 <= $1 <= N + * $2 the current accumulation of quotient bits + * N max depth + * + * We add a new bit to $2 and either recurse or insert the bits in + * the quotient. R, Q, and V are inputs and outputs as defined above; + * the condition codes are expected to reflect the input R, and are + * modified to reflect the output R. + */ +define(DEVELOP_QUOTIENT_BITS, +` ! depth $1, accumulated bits $2 + bl L.$1.eval(TWOSUPN+$2) + srl V,1,V + ! remainder is positive + subcc R,V,R + ifelse($1, N, + ` b 9f + add Q, ($2*2+1), Q + ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2+1)')') +L.$1.eval(TWOSUPN+$2): + ! remainder is negative + addcc R,V,R + ifelse($1, N, + ` b 9f + add Q, ($2*2-1), Q + ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2-1)')') + ifelse($1, 1, `9:')') + +FUNC(NAME) +ifelse(S, `true', +` ! compute sign of result; if neither is negative, no problem + orcc divisor, dividend, %g0 ! either negative? + bge 2f ! no, go do the divide + ifelse(OP, `div', + `xor divisor, dividend, SIGN', + `mov dividend, SIGN') ! compute sign in any case + tst divisor + bge 1f + tst dividend + ! divisor is definitely negative; dividend might also be negative + bge 2f ! if dividend not negative... + neg divisor ! in any case, make divisor nonneg +1: ! dividend is negative, divisor is nonnegative + neg dividend ! make dividend nonnegative +2: +') + ! Ready to divide. Compute size of quotient; scale comparand. + orcc divisor, %g0, V + bnz 1f + mov dividend, R + + ! Divide by zero trap. If it returns, return 0 (about as + ! wrong as possible, but that is what SunOS does...). + t ST_DIV0 + retl + clr %o0 + +1: + cmp R, V ! if divisor exceeds dividend, done + blu Lgot_result ! (and algorithm fails otherwise) + clr Q + sethi %hi(1 << (WORDSIZE - TOPBITS - 1)), T + cmp R, T + blu Lnot_really_big + clr ITER + + ! `Here the dividend is >= 2^(31-N) or so. We must be careful here, + ! as our usual N-at-a-shot divide step will cause overflow and havoc. + ! The number of bits in the result here is N*ITER+SC, where SC <= N. + ! Compute ITER in an unorthodox manner: know we need to shift V into + ! the top decade: so do not even bother to compare to R.' + 1: + cmp V, T + bgeu 3f + mov 1, SC + sll V, N, V + b 1b + inc ITER + + ! Now compute SC. + 2: addcc V, V, V + bcc Lnot_too_big + inc SC + + ! We get here if the divisor overflowed while shifting. + ! This means that R has the high-order bit set. + ! Restore V and subtract from R. + sll T, TOPBITS, T ! high order bit + srl V, 1, V ! rest of V + add V, T, V + b Ldo_single_div + dec SC + + Lnot_too_big: + 3: cmp V, R + blu 2b + nop + be Ldo_single_div + nop + /* NB: these are commented out in the V8-Sparc manual as well */ + /* (I do not understand this) */ + ! V > R: went too far: back up 1 step + ! srl V, 1, V + ! dec SC + ! do single-bit divide steps + ! + ! We have to be careful here. We know that R >= V, so we can do the + ! first divide step without thinking. BUT, the others are conditional, + ! and are only done if R >= 0. Because both R and V may have the high- + ! order bit set in the first step, just falling into the regular + ! division loop will mess up the first time around. + ! So we unroll slightly... + Ldo_single_div: + deccc SC + bl Lend_regular_divide + nop + sub R, V, R + mov 1, Q + b Lend_single_divloop + nop + Lsingle_divloop: + sll Q, 1, Q + bl 1f + srl V, 1, V + ! R >= 0 + sub R, V, R + b 2f + inc Q + 1: ! R < 0 + add R, V, R + dec Q + 2: + Lend_single_divloop: + deccc SC + bge Lsingle_divloop + tst R + b,a Lend_regular_divide + +Lnot_really_big: +1: + sll V, N, V + cmp V, R + bleu 1b + inccc ITER + be Lgot_result + dec ITER + + tst R ! set up for initial iteration +Ldivloop: + sll Q, N, Q + DEVELOP_QUOTIENT_BITS(1, 0) +Lend_regular_divide: + deccc ITER + bge Ldivloop + tst R + bl,a Lgot_result + ! non-restoring fixup here (one instruction only!) +ifelse(OP, `div', +` dec Q +', ` add R, divisor, R +') + +Lgot_result: +ifelse(S, `true', +` ! check to see if answer should be < 0 + tst SIGN + bl,a 1f + ifelse(OP, `div', `neg Q', `neg R') +1:') + retl + ifelse(OP, `div', `mov Q, %o0', `mov R, %o0') diff --git a/lib/nbsd_libc/arch/sparc64/gen/fabs.S b/lib/nbsd_libc/arch/sparc64/gen/fabs.S new file mode 100644 index 000000000..bb7ac9fce --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fabs.S @@ -0,0 +1,52 @@ +/* $NetBSD: fabs.S,v 1.5 2003/08/07 16:42:27 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: fabs.s,v 1.4 91/10/07 23:59:05 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)fabs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: fabs.S,v 1.5 2003/08/07 16:42:27 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* fabs - floating absolute value */ + +ENTRY(fabs) + retl + fabss %f0, %f0 ! return absolute value + diff --git a/lib/nbsd_libc/arch/sparc64/gen/fixunsdfsi.S b/lib/nbsd_libc/arch/sparc64/gen/fixunsdfsi.S new file mode 100644 index 000000000..bc67fd001 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fixunsdfsi.S @@ -0,0 +1,100 @@ +/* $NetBSD: fixunsdfsi.S,v 1.4 2003/08/07 16:42:27 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: fixunsdfsi.s,v 1.3 91/10/08 00:03:15 torek Exp + */ + +#include +#define _LOCORE +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)fixunsdfsi.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: fixunsdfsi.S,v 1.4 2003/08/07 16:42:27 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Convert double to unsigned integer (for gcc). + * + * I have made the output for NaN agree with the Sun compiler, not + * that it really matters, by using `fbul,a'. + */ + + + .align 8 +Lbig: + .word 0x43e00000 ! .double 2^63 + .word 0 ! (who me, not trust the assembler?) + +/* + * Same as above but to unsigned long + */ +ENTRY(__dtoul) + sub %sp, 16, %sp + std %f2, [%sp + 64 + BIAS + 8] +#ifdef PIC + PICCY_SET(Lbig, %g1, %o0) + ldd [%g1], %f2 +#else + sethi %hi(Lbig), %g1 + ldd [%g1 + %lo(Lbig)], %f2 +#endif + fcmped %f0, %f2 ! d < 2^63, or NaN, or -Inf? + nop ! (fpop2 delay) + fbul,a 1f ! if so, use fdtoi to convert to int + fdtox %f0, %f0 ! (this includes negatives!) + + ! d does not fit in an int, so subtract 2^63, convert, + ! and add 2^63 again (sigh). Just hope the intermediate + ! fits (if not, the result is undefined anyway). + + fsubd %f0, %f2, %f0 ! d -= 2^63 + fdtox %f0, %f0 ! convert to int + std %f0, [%sp + 64 + BIAS] ! move into return reg + ldx [%sp + 64 + BIAS], %o0 + sethi %hi(0x80000000), %o1 + sllx %o1, 32, %o1 + add %o0, %o1, %o0 ! add 2^63 + ldd [%sp + 64 + BIAS + 8], %f2 + retl + add %sp, 16, %sp + +1: + std %f0, [%sp + 64 + BIAS] ! return result + ldx [%sp + 64 + BIAS], %o0 + ldd [%sp + 64 + BIAS + 8], %f2 + retl + add %sp, 16, %sp diff --git a/lib/nbsd_libc/arch/sparc64/gen/flt_rounds.c b/lib/nbsd_libc/arch/sparc64/gen/flt_rounds.c new file mode 100644 index 000000000..5e70b742b --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/flt_rounds.c @@ -0,0 +1,30 @@ +/* $NetBSD: flt_rounds.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: flt_rounds.c,v 1.4 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const int map[] = { + 1, /* round to nearest */ + 0, /* round to zero */ + 2, /* round to positive infinity */ + 3 /* round to negative infinity */ +}; + +int +__flt_rounds() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return map[(x >> 30) & 0x03]; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/fpclassifyl.c b/lib/nbsd_libc/arch/sparc64/gen/fpclassifyl.c new file mode 100644 index 000000000..613798a71 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fpclassifyl.c @@ -0,0 +1,66 @@ +/* $NetBSD: fpclassifyl.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyl.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__fpclassifyl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + if (u.extu_ext.ext_exp == 0) { + if (u.extu_ext.ext_frach == 0 && u.extu_ext.ext_frachm == 0 && + u.extu_ext.ext_fraclm == 0 && u.extu_ext.ext_fracl == 0) + return FP_ZERO; + else + return FP_SUBNORMAL; + } else if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) { + if (u.extu_ext.ext_frach == 0 && u.extu_ext.ext_frachm == 0 && + u.extu_ext.ext_fraclm == 0 && u.extu_ext.ext_fracl == 0) + return FP_INFINITE; + else + return FP_NAN; + } + + return FP_NORMAL; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/fpgetmask.c b/lib/nbsd_libc/arch/sparc64/gen/fpgetmask.c new file mode 100644 index 000000000..b0cadda9c --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fpgetmask.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetmask.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetmask.c,v 1.4 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetmask,_fpgetmask) +#endif + +fp_except +fpgetmask() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return (x >> 23) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/fpgetround.c b/lib/nbsd_libc/arch/sparc64/gen/fpgetround.c new file mode 100644 index 000000000..c44991092 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fpgetround.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetround.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetround.c,v 1.4 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetround,_fpgetround) +#endif + +fp_rnd +fpgetround() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return (x >> 30) & 0x03; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/fpgetsticky.c b/lib/nbsd_libc/arch/sparc64/gen/fpgetsticky.c new file mode 100644 index 000000000..bd0484dd4 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fpgetsticky.c @@ -0,0 +1,28 @@ +/* $NetBSD: fpgetsticky.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpgetsticky.c,v 1.4 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpgetsticky,_fpgetsticky) +#endif + +fp_except +fpgetsticky() +{ + int x; + + __asm("st %%fsr,%0" : "=m" (*&x)); + return (x >> 5) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/fpsetmask.c b/lib/nbsd_libc/arch/sparc64/gen/fpsetmask.c new file mode 100644 index 000000000..65e7a7945 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fpsetmask.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetmask.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetmask.c,v 1.4 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetmask,_fpsetmask) +#endif + +fp_except +fpsetmask(mask) + fp_except mask; +{ + fp_except old; + fp_except new; + + __asm("st %%fsr,%0" : "=m" (*&old)); + + new = old; + new &= ~(0x1f << 23); + new |= ((mask & 0x1f) << 23); + + __asm("ld %0,%%fsr" : : "m" (*&new)); + + return (old >> 23) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/fpsetround.c b/lib/nbsd_libc/arch/sparc64/gen/fpsetround.c new file mode 100644 index 000000000..9e21dc7e3 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fpsetround.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetround.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetround.c,v 1.4 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetround,_fpsetround) +#endif + +fp_rnd +fpsetround(rnd_dir) + fp_rnd rnd_dir; +{ + fp_rnd old; + fp_rnd new; + + __asm("st %%fsr,%0" : "=m" (*&old)); + + new = old; + new &= ~(0x03 << 30); + new |= ((rnd_dir & 0x03) << 30); + + __asm("ld %0,%%fsr" : : "m" (*&new)); + + return (old >> 30) & 0x03; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/fpsetsticky.c b/lib/nbsd_libc/arch/sparc64/gen/fpsetsticky.c new file mode 100644 index 000000000..10e98ed78 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/fpsetsticky.c @@ -0,0 +1,37 @@ +/* $NetBSD: fpsetsticky.c,v 1.4 2005/12/24 23:10:08 perry Exp $ */ + +/* + * Written by J.T. Conklin, Apr 10, 1995 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpsetsticky.c,v 1.4 2005/12/24 23:10:08 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(fpsetsticky,_fpsetsticky) +#endif + +fp_except +fpsetsticky(sticky) + fp_except sticky; +{ + fp_except old; + fp_except new; + + __asm("st %%fsr,%0" : "=m" (*&old)); + + new = old; + new &= ~(0x1f << 5); + new |= ((sticky & 0x1f) << 5); + + __asm("ld %0,%%fsr" : : "m" (*&new)); + + return (old >> 5) & 0x1f; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/infinityl.c b/lib/nbsd_libc/arch/sparc64/gen/infinityl.c new file mode 100644 index 000000000..ebd8f2daf --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/infinityl.c @@ -0,0 +1,15 @@ +/* $NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * IEEE-compatible infinityl.c for big-endian 128-bit format -- public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include + +const union __long_double_u __infinityl = + { { 0x7f, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; diff --git a/lib/nbsd_libc/arch/sparc64/gen/isfinitel.c b/lib/nbsd_libc/arch/sparc64/gen/isfinitel.c new file mode 100644 index 000000000..b56951235 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/isfinitel.c @@ -0,0 +1,55 @@ +/* $NetBSD: isfinitel.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinitel.c,v 1.2 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__isfinitel(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/isinfl.c b/lib/nbsd_libc/arch/sparc64/gen/isinfl.c new file mode 100644 index 000000000..8d7acc6dd --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/isinfl.c @@ -0,0 +1,64 @@ +/* $NetBSD: isinfl.c,v 1.4 2004/03/04 23:42:39 kleink Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinfl.c,v 1.4 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.3 isinf - test for infinity + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__isinfl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + (u.extu_ext.ext_frach == 0 && u.extu_ext.ext_frachm == 0 && + u.extu_ext.ext_fraclm == 0 && u.extu_ext.ext_fracl == 0)); +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/isnanl.c b/lib/nbsd_libc/arch/sparc64/gen/isnanl.c new file mode 100644 index 000000000..2b67f0866 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/isnanl.c @@ -0,0 +1,64 @@ +/* $NetBSD: isnanl.c,v 1.4 2004/03/04 23:42:39 kleink Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnanl.c,v 1.4 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.4 isnan - test for a NaN + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__isnanl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + (u.extu_ext.ext_frach != 0 || u.extu_ext.ext_frachm != 0 || + u.extu_ext.ext_fraclm != 0 || u.extu_ext.ext_fracl != 0)); +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/longjmp.c b/lib/nbsd_libc/arch/sparc64/gen/longjmp.c new file mode 100644 index 000000000..54f54ac20 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/longjmp.c @@ -0,0 +1,98 @@ +/* $NetBSD: longjmp.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christian Limpach. + * + * 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 "namespace.h" +#include +#include +#include +#include +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include + +typedef struct { + __greg_t __glob[5]; +} __jmp_buf_regs_t; + +void +__longjmp14(jmp_buf env, int val) +{ + struct sigcontext *sc = (void *)env; + __jmp_buf_regs_t *r = (void *)&sc[1]; + ucontext_t uc; + + /* Ensure non-zero SP */ + if (sc->sc_sp == 0) + goto err; + + /* + * Set _UC_CPU. No FPU data saved, so we can't restore + * that. Set _UC_{SET,CLR}STACK according to SS_ONSTACK + */ + memset(&uc, 0, sizeof(uc)); + uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK); + + /* + * Set the signal mask - this is a weak symbol, so don't use + * _UC_SIGMASK in the mcontext, libpthread might override sigprocmask. + */ + sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL); + + /* Fill other registers */ + uc.uc_mcontext.__gregs[_REG_CCR] = sc->sc_tstate; + uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc; + uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_npc; + uc.uc_mcontext.__gregs[_REG_G1] = sc->sc_g1; + uc.uc_mcontext.__gregs[_REG_G2] = sc->sc_o0; + uc.uc_mcontext.__gregs[_REG_G3] = r->__glob[0]; + uc.uc_mcontext.__gregs[_REG_G4] = r->__glob[1]; + uc.uc_mcontext.__gregs[_REG_G5] = r->__glob[2]; + uc.uc_mcontext.__gregs[_REG_G6] = r->__glob[3]; + uc.uc_mcontext.__gregs[_REG_G7] = r->__glob[4]; + uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp; + + /* Make return value non-zero */ + if (val == 0) + val = 1; + + /* Save return value in context */ + uc.uc_mcontext.__gregs[_REG_O0] = val; + + setcontext(&uc); + + err: + longjmperror(); + abort(); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/makecontext.c b/lib/nbsd_libc/arch/sparc64/gen/makecontext.c new file mode 100644 index 000000000..64fa3c622 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/makecontext.c @@ -0,0 +1,81 @@ +/* $NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + int i; + ulong *sp; + va_list ap; + + sp = (ulong *)((caddr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + /* Align on quad-word boundary. */ + sp = (ulong *)((ulong)sp & ~0xf); + + /* Make room for: argx, argd, struct return pointer(?), rwindow. */ + sp -= (argc > 6 ? argc - 6 : 0) + 6 + 8 + 8; + + va_start(ap, argc); + + /* Pass up to 6 arguments in %o0-5. */ + for (i = 0; i < argc && i < 6; i++) + gr[_REG_O0 + i] = sp[i + 16] = va_arg(ap, unsigned long); + + /* Pass any additional arguments on the stack. */ + for (; i < argc; i++) + sp[i + 16] = va_arg(ap, unsigned long); + va_end(ap); + + sp[14] = (ulong)sp - 2047; + sp[15] = (ulong)_resumecontext - 8; + + sp = (ulong *)((caddr_t)sp - 2047); + + gr[_REG_O6] = (__greg_t)sp; + gr[_REG_PC] = (__greg_t)func; + gr[_REG_nPC] = (__greg_t)func + 4; + gr[_REG_O7] = (__greg_t)_resumecontext - 8; + +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/modf.S b/lib/nbsd_libc/arch/sparc64/gen/modf.S new file mode 100644 index 000000000..624a2f3a2 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/modf.S @@ -0,0 +1,189 @@ +/* $NetBSD: modf.S,v 1.4 2003/08/07 16:42:27 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: modf.s,v 1.3 92/06/20 00:00:54 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)modf.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: modf.S,v 1.4 2003/08/07 16:42:27 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#include + +/* + * double modf(double val, double *iptr) + * + * Returns the fractional part of `val', storing the integer part of + * `val' in *iptr. Both *iptr and the return value have the same sign + * as `val'. + * + * Method: + * + * We use the fpu's normalization hardware to compute the integer portion + * of the double precision argument. Sun IEEE double precision numbers + * have 52 bits of mantissa, 11 bits of exponent, and one bit of sign, + * with the sign occupying bit 31 of word 0, and the exponent bits 30:20 + * of word 0. Thus, values >= 2^52 are by definition integers. + * + * If we take a value that is in the range [+0..2^52) and add 2^52, all + * of the fractional bits fall out and all of the integer bits are summed + * with 2^52. If we then subtract 2^52, we get those integer bits back. + * This must be done with rounding set to `towards 0' or `towards -inf'. + * `Toward -inf' fails when the value is 0 (we get -0 back).... + * + * Note that this method will work anywhere, but is machine dependent in + * various aspects. + * + * Stack usage: + * 4@[%fp + BIAS - 4] saved %fsr + * 4@[%fp + BIAS - 8] new %fsr with rounding set to `towards 0' + * 8@[%fp + BIAS - 16] space for moving between %i and %f registers + * Register usage: + * %f0:f1 double val; + * %l0 scratch + * %l1 sign bit (0x80000000) + * %i1 double *iptr; + * %f2:f3 `magic number' 2^52, in fpu registers + * %f4:f5 double v, in fpu registers + * %f6:f7 double temp. + */ + + .align 8 +Lmagic: + .word 0x43300000 ! sign = 0, exponent = 52 + 1023, mantissa = 0 + .word 0 ! (i.e., .double 0r4503599627370496e+00) + +L0: + .word 0 ! 0.0 + .word 0 + +ENTRY(modf) + save %sp, -CC64FSZ-16, %sp + + /* + * First, compute v = abs(val) + */ + fabsd %f0, %f4 ! %f4:f5 = v + fcmped %fcc1, %f0, %f4 ! %fcc1 = (val == abs(val)) +#ifdef PIC + PICCY_SET(Lmagic, %l0, %o7) + ldd [%l0], %f2 +#else + sethi %hi(Lmagic), %l0 + ldd [%l0 + %lo(Lmagic)], %f2 +#endif + + /* + * Is %f4:f5 >= %f2:f3 ? If so, it is all integer bits. + * It is probably less, though. + */ + fcmped %f4, %f2 + fbuge Lbig ! if >= (or unordered), go out + nop + + /* + * v < 2^52, so add 2^52, then subtract 2^52, but do it all + * with rounding set towards zero. We leave any enabled + * traps enabled, but change the rounding mode. This might + * not be so good. Oh well.... + */ + st %fsr, [%fp + BIAS - 4] ! %l5 = current FSR mode + set FSR_RD, %l3 ! %l3 = rounding direction mask + ld [%fp + BIAS - 4], %l5 + set FSR_RD_RZ << FSR_RD_SHIFT, %l4 + andn %l5, %l3, %l6 + or %l6, %l4, %l6 ! round towards zero, please + and %l5, %l3, %l5 ! save original rounding mode + st %l6, [%fp + BIAS - 8] + ld [%fp + BIAS - 8], %fsr + + faddd %f4, %f2, %f4 ! %f4:f5 += 2^52 + fsubd %f4, %f2, %f4 ! %f4:f5 -= 2^52 + + /* + * Restore %fsr, but leave exceptions accrued. + */ + st %fsr, [%fp + BIAS - 4] + ld [%fp + BIAS - 4], %l6 + andn %l6, %l3, %l6 ! %l6 = %fsr & ~FSR_RD; + or %l5, %l6, %l5 ! %l5 |= %l6; + st %l5, [%fp + BIAS - 4] + ld [%fp + BIAS - 4], %fsr ! restore %fsr, leaving accrued stuff + + /* + * Now insert the original sign in %f4:f5. + * %fcc1 should still have the reults of (val == abs(val)) + * from above, so we use a conditional move on %fcc1 to: + * + * %f4 = (val == abs(val)) ? %f4 : -%f4 + * + */ + fnegd %f4, %f6 + fmovdnz %fcc1, %f6, %f4 +1: + + /* + * The value in %f4:f5 is now the integer portion of the original + * argument. We need to store this in *ival (%i1), subtract it + * from the original value argument (%d0), and return the result. + */ + std %f4, [%i1] ! *ival = %f4:f5; + fsubd %f0, %f4, %f0 ! %f0:f1 -= %f4:f5; + ret + restore + +Lbig: + /* + * We get here if the original comparison of %f4:f5 (v) to + * %f2:f3 (2^52) came out `greater or unordered'. In this + * case the integer part is the original value, and the + * fractional part is 0. + */ +#ifdef PIC + PICCY_SET(L0, %l0, %o7) + std %f0, [%i1] ! *ival = val; + ldd [%l0], %f0 ! return 0.0; +#else + sethi %hi(L0), %l0 + std %f0, [%i1] ! *ival = val; + ldd [%l0 + %lo(L0)], %f0 ! return 0.0; +#endif + ret + restore + diff --git a/lib/nbsd_libc/arch/sparc64/gen/nanf.c b/lib/nbsd_libc/arch/sparc64/gen/nanf.c new file mode 100644 index 000000000..1688d0eae --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0x7f, 0xc0, 0, 0 } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/sparc64/gen/resumecontext.c b/lib/nbsd_libc/arch/sparc64/gen/resumecontext.c new file mode 100644 index 000000000..1a7c4c55f --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/setjmp.S b/lib/nbsd_libc/arch/sparc64/gen/setjmp.S new file mode 100644 index 000000000..e12e04a38 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/setjmp.S @@ -0,0 +1,136 @@ +/* $NetBSD: setjmp.S,v 1.7 2005/10/16 17:15:38 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: setjmp.s,v 1.2 92/06/25 03:18:43 torek Exp + */ + +#define _LOCORE +#include +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)setjmp.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: setjmp.S,v 1.7 2005/10/16 17:15:38 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp + * + * __longjmp14(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +#include "SYS.h" + + .register %g2,#ignore + .register %g3,#ignore + .register %g6,#ignore + +ENTRY(__setjmp14) + save %sp, -CC64FSZ, %sp + + /* save globals into locals */ + mov %g1, %l1 + mov %g2, %l2 + mov %g3, %l3 + mov %g4, %l4 + mov %g5, %l5 + mov %g6, %l6 + mov %g7, %l7 + +#ifdef PIC + PIC_PROLOGUE(%i1, %o4) +#endif + + /* now get sigmask and onstack flag */ + add %i0, 0x38, %o2 /* build sigcontext in [%o2]->sc.sc_mask */ + mov 1, %o0 /* SIG_BLOCK */ + /* sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *)a) */ +#ifdef PIC +#ifdef BIGPIC + set _C_LABEL(__sigprocmask14), %o4 + ldx [%i1 + %o4], %g2 +#else + ldx [%i1 + _C_LABEL(__sigprocmask14)], %g2 +#endif + call %g2 +#else + call _C_LABEL(__sigprocmask14) +#endif + clr %o1 + + clr %o0 /* sigstack(NULL, &foo) */ +#ifdef PIC +#ifdef BIGPIC + set _C_LABEL(__sigaltstack14), %o4 + ldx [%i1 + %o4], %g2 +#else + ldx [%i1 + _C_LABEL(__sigaltstack14)], %g2 +#endif + call %g2 +#else + call _C_LABEL(__sigaltstack14) +#endif + add %i0, 8, %o1 /* (foo being part of the sigcontext we're overwriting) */ + + lduw [%i0 + 8 + 0x10], %o0 /* foo.ss_flags */ + and %o0, 1, %o1 /* onstack = foo.ss_flags & SS_ONSTACK; */ + st %o1, [%i0 + 0x00] /* sc.sc_onstack = current onstack; */ + + /* store essential state */ + stx %fp, [%i0 + 0x08] /* sc.sc_sp = sp */ + add %i7, 8, %o0 + stx %o0, [%i0 + 0x10] /* sc.sc_pc = return_pc */ + add %i7, 12, %o0 + stx %o0, [%i0 + 0x18] /* sc.sc_npc = return_pc + 4 */ + stx %g0, [%i0 + 0x20] /* sc.sc_tstate = (clean ccr) */ + stx %l1, [%i0 + 0x28] /* sc.sc_g1 */ + stx %l2, [%i0 + 0x30] /* sc.sc_o0, set in longjmp, use as %g2 */ + /* 0x38 */ /* sc.sc_mask was already saved above */ + + /* save additional registers needed to fill a complete mcontext */ + stx %l3, [%i0 + 0x40] + stx %l4, [%i0 + 0x48] + stx %l5, [%i0 + 0x50] + stx %l6, [%i0 + 0x58] + stx %l7, [%i0 + 0x60] + + ret /* return 0 */ + restore %g0, 0, %o0 diff --git a/lib/nbsd_libc/arch/sparc64/gen/signbitl.c b/lib/nbsd_libc/arch/sparc64/gen/signbitl.c new file mode 100644 index 000000000..0328c9fde --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/signbitl.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitl.c,v 1.2 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__signbitl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_sign == 1); +} diff --git a/lib/nbsd_libc/arch/sparc64/gen/sigsetjmp.S b/lib/nbsd_libc/arch/sparc64/gen/sigsetjmp.S new file mode 100644 index 000000000..fedbc7004 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/sigsetjmp.S @@ -0,0 +1,108 @@ +/* $NetBSD: sigsetjmp.S,v 1.8 2005/10/16 17:15:38 christos Exp $ */ +/* + * Copyright (c) 1995 Paul Kranenburg + * 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 by Paul Kranenburg. + * 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. + * + */ + +#include "SYS.h" + +#ifdef PIC +#ifdef BIGPIC + +ENTRY(__sigsetjmp14) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + st %o1,[%o0+0x68] ! jmpbuf[JBLEN] + sethi %hi(_C_LABEL(__setjmp14)), %o2 + sethi %hi(_C_LABEL(_setjmp)), %o3 + or %o2, %lo(_C_LABEL(__setjmp14)), %o2 + or %o3, %lo(_C_LABEL(_setjmp)), %o3 + brz %o1, 1f + ldx [%g1+%o3], %o3 ! if (%o1 == 0) goto _setjmp; + ldx [%g1+%o2], %o3 ! else goto __setjmp; +1: + jmp %o3 + nop + +ENTRY(__siglongjmp14) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + ld [%o0+0x68], %o2 ! jmpbuf[JBLEN] + sethi %hi(_C_LABEL(__longjmp14)), %o3 + sethi %hi(_C_LABEL(_longjmp)), %o4 + or %o3, %lo(_C_LABEL(__longjmp14)), %o3 + or %o4, %lo(_C_LABEL(_longjmp)), %o4 + brz,a %o2, 1f + ldx [%g1+%o4], %o4 ! if (%o2 == 0) goto _longjmp; + ldx [%g1+%o3], %o4 ! else goto __longjmp; +1: + jmp %o4 + nop + unimp 0 +#else + +ENTRY(__sigsetjmp14) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + cmp %o1,0 + st %o1,[%o0+0x68] ! jmpbuf[JBLEN] + bne,a 1f + ldx [%g1+_C_LABEL(__setjmp14)], %g1 ! if (%o1 != 0) goto __setjmp; + ldx [%g1+_C_LABEL(_setjmp)], %g1 ! else goto _setjmp; +1: + jmp %g1 + nop + +ENTRY(__siglongjmp14) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + ld [%o0+0x68],%o2 ! jmpbuf[JBLEN] + cmp %o2,0 + bne,a 1f + ldx [%g1+_C_LABEL(__longjmp14)], %g1 ! if (%o2 != 0) goto __longjmp; + ldx [%g1+_C_LABEL(_longjmp)], %g1 ! else goto _longjmp; +1: + jmp %g1 + nop + unimp 0 +#endif /* BIGPIC */ +#else /* PIC */ + +ENTRY(__sigsetjmp14) + cmp %o1,0 + be _C_LABEL(_setjmp) + st %o1,[%o0+0x68] ! jmpbuf[JBLEN] + ba,a _C_LABEL(__setjmp14) + unimp 0 + +ENTRY(__siglongjmp14) + ld [%o0+0x68],%g1 + cmp %g1,0 + be _C_LABEL(_longjmp) + nop + ba,a _C_LABEL(__longjmp14) + unimp 0 + +#endif /* PIC */ diff --git a/lib/nbsd_libc/arch/sparc64/gen/swapcontext.S b/lib/nbsd_libc/arch/sparc64/gen/swapcontext.S new file mode 100644 index 000000000..972d5a389 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/gen/swapcontext.S @@ -0,0 +1,58 @@ +/* $NetBSD: swapcontext.S,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.4 2008/04/28 20:22:57 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + + .register %g2,#scratch + +ENTRY(swapcontext) + mov %o0, %o2 /* must save pointer */ + mov %o1, %o3 /* must save pointer */ + mov SYS_getcontext, %g1 + t ST_SYSCALL + bcc,a 1f + stx %g0, [%o2 + 64 + 11 * 8] /* gr[_REG_O0] = 0 */ + ERROR() +1: + add %o7, 12, %o1 + stx %o1, [%o2 + 64 + 2 * 8] /* gr[_REG_nPC] = retaddr + 4 */ + add %o7, 8, %o1 + stx %o1, [%o2 + 64 + 1 * 8] /* gr[_REG_PC] = retaddr */ + + mov %o3, %o0 + mov %o1, %g2 /* optimize `return' */ + mov SYS_setcontext|SYSCALL_G2RFLAG, %g1 + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/arch/sparc64/net/Makefile.inc b/lib/nbsd_libc/arch/sparc64/net/Makefile.inc new file mode 100644 index 000000000..f51553517 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/net/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 1998/09/11 04:56:30 eeh Exp $ + +SRCS+= htonl.S htons.S ntohl.S ntohs.S diff --git a/lib/nbsd_libc/arch/sparc64/softfloat/milieu.h b/lib/nbsd_libc/arch/sparc64/softfloat/milieu.h new file mode 100644 index 000000000..ef43336cd --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/softfloat/milieu.h @@ -0,0 +1,48 @@ +/* $NetBSD: milieu.h,v 1.1 2002/02/05 07:53:05 jmc Exp $ */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +Include common integer types and flags. +------------------------------------------------------------------------------- +*/ +#include "sparc64-gcc.h" + +/* +------------------------------------------------------------------------------- +Symbolic Boolean literals. +------------------------------------------------------------------------------- +*/ +enum { + FALSE = 0, + TRUE = 1 +}; diff --git a/lib/nbsd_libc/arch/sparc64/softfloat/qp.c b/lib/nbsd_libc/arch/sparc64/softfloat/qp.c new file mode 100644 index 000000000..40bf84563 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/softfloat/qp.c @@ -0,0 +1,285 @@ +/* $NetBSD: qp.c,v 1.7 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc. + * 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 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 "milieu.h" +#include "softfloat.h" + + +void _Qp_add(float128 *c, float128 *a, float128 *b); +int _Qp_cmp(float128 *a, float128 *b); +int _Qp_cmpe(float128 *a, float128 *b); +void _Qp_div(float128 *c, float128 *a, float128 *b); +void _Qp_dtoq(float128 *c, double a); +int _Qp_feq(float128 *a, float128 *b); +int _Qp_fge(float128 *a, float128 *b); +int _Qp_fgt(float128 *a, float128 *b); +int _Qp_fle(float128 *a, float128 *b); +int _Qp_flt(float128 *a, float128 *b); +int _Qp_fne(float128 *a, float128 *b); +void _Qp_itoq(float128 *c, int a); +void _Qp_mul(float128 *c, float128 *a, float128 *b); +void _Qp_neg(float128 *c, float128 *a); +double _Qp_qtod(float128 *a); +int _Qp_qtoi(float128 *a); +float _Qp_qtos(float128 *a); +unsigned int _Qp_qtoui(float128 *a); +unsigned long _Qp_qtoux(float128 *a); +long _Qp_qtox(float128 *a); +void _Qp_sqrt(float128 *c, float128 *a); +void _Qp_stoq(float128 *c, float a); +void _Qp_sub(float128 *c, float128 *a, float128 *b); +void _Qp_uitoq(float128 *c, unsigned int a); +void _Qp_uxtoq(float128 *c, unsigned long a); +void _Qp_xtoq(float128 *c, long a); + + +void +_Qp_add(float128 *c, float128 *a, float128 *b) +{ + *c = float128_add(*a, *b); +} + + +int +_Qp_cmp(float128 *a, float128 *b) +{ + + if (float128_eq(*a, *b)) + return 0; + + if (float128_le(*a, *b)) + return 1; + + return 2; +} + + +/* + * XXX + */ +int +_Qp_cmpe(float128 *a, float128 *b) +{ + return _Qp_cmp(a, b); +} + + +void +_Qp_div(float128 *c, float128 *a, float128 *b) +{ + *c = float128_div(*a, *b); +} + + +void +_Qp_dtoq(float128 *c, double a) +{ + float64 _b; + + memcpy (&_b, &a, sizeof(float64)); + *c = float64_to_float128(_b); +} + + +int +_Qp_feq(float128 *a, float128 *b) +{ + return float128_eq(*a, *b); +} + + +int +_Qp_fge(float128 *a, float128 *b) +{ + return float128_le(*b, *a); +} + + +int +_Qp_fgt(float128 *a, float128 *b) +{ + return float128_lt(*b, *a); +} + + +int +_Qp_fle(float128 *a, float128 *b) +{ + return float128_le(*a, *b); +} + + +int +_Qp_flt(float128 *a, float128 *b) +{ + return float128_lt(*a, *b); +} + + +int +_Qp_fne(float128 *a, float128 *b) +{ + return !float128_eq(*a, *b); +} + + +void +_Qp_itoq(float128 *c, int a) +{ + *c = int32_to_float128(a); +} + + +void +_Qp_mul(float128 *c, float128 *a, float128 *b) +{ + *c = float128_mul(*a, *b); +} + + +/* + * XXX need corresponding softfloat function + */ +static float128 __zero = {0x4034000000000000, 0x00000000}; + +void +_Qp_neg(float128 *c, float128 *a) +{ + *c = float128_sub(__zero, *a); +} + + +double +_Qp_qtod(float128 *a) +{ + float64 _c; + double c; + + _c = float128_to_float64(*a); + + memcpy(&c, &_c, sizeof(double)); + + return c; +} + + +int +_Qp_qtoi(float128 *a) +{ + return float128_to_int32(*a); +} + + +float + _Qp_qtos(float128 *a) +{ + float c; + float32 _c; + + _c = float128_to_float32(*a); + + memcpy(&c, &_c, sizeof(_c)); + + return c; +} + + +unsigned int +_Qp_qtoui(float128 *a) +{ + return (unsigned int)float128_to_int64(*a); +} + + +unsigned long +_Qp_qtoux(float128 *a) +{ + return (unsigned long)float128_to_uint64_round_to_zero(*a); +} + + +long +_Qp_qtox(float128 *a) +{ + return (long)float128_to_int64_round_to_zero(*a); +} + + +void +_Qp_sqrt(float128 *c, float128 *a) +{ + *c = float128_sqrt(*a); +} + + +void +_Qp_stoq(float128 *c, float a) +{ + float32 _a; + + memcpy(&_a, &a, sizeof(a)); + + *c = float32_to_float128(_a); +} + + +void +_Qp_sub(float128 *c, float128 *a, float128 *b) +{ + *c = float128_sub(*a, *b); +} + + +void +_Qp_uitoq(float128 *c, unsigned int a) +{ + *c = int64_to_float128(a); +} + + +void +_Qp_uxtoq(float128 *c, unsigned long a) +{ + + if (a & 0x8000000000000000ULL) { + a = (a >> 1) | (a & 1); + *c = int64_to_float128(a); + *c = float128_add(*c, *c); + } else + *c = int64_to_float128(a); +} + + +void +_Qp_xtoq(float128 *c, long a) +{ + *c = int64_to_float128(a); +} diff --git a/lib/nbsd_libc/arch/sparc64/softfloat/softfloat-qp.h b/lib/nbsd_libc/arch/sparc64/softfloat/softfloat-qp.h new file mode 100644 index 000000000..1fda8802c --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/softfloat/softfloat-qp.h @@ -0,0 +1,88 @@ +/* + * Get everything SOFTFLOAT_FOR_GCC normally would rename out of the users + * namespace. Much of this isn't used but to avoid dissecting softloat.c + * all of it is pulled in even for just the _Qp* case + */ + +#if defined(SOFTFLOATSPARC64_FOR_GCC) && !defined(SOFTFLOAT_FOR_GCC) +#define float_exception_flags _softfloat_float_exception_flags +#define float_rounding_mode _softfloat_float_rounding_mode +#define float_raise _softfloat_float_raise + +#define float32_eq _softfloat_float32_eq +#define float32_le _softfloat_float32_le +#define float32_lt _softfloat_float32_lt +#define float64_eq _softfloat_float64_eq +#define float64_le _softfloat_float64_le +#define float64_lt _softfloat_float64_lt + +#define float32_add _softfloat_float32_add +#define float64_add _softfloat_float64_add +#define float32_sub _softfloat_float32_sub +#define float64_sub _softfloat_float64_sub +#define float32_mul _softfloat_float32_mul +#define float64_mul _softfloat_float64_mul +#define float32_div _softfloat_float32_div +#define float64_div _softfloat_float64_div +#define int32_to_float32 _softfloat_int32_to_float32 +#define int32_to_float64 _softfloat_int32_to_float64 +#define int64_to_float32 _softfloat_int64_to_float32 +#define int64_to_float64 _softfloat_int64_to_float64 +#define float32_to_int32_round_to_zero _softfloat_float32_to_int32_round_to_zero +#define float64_to_int32_round_to_zero _softfloat_float64_to_int32_round_to_zero +#define float32_to_int64_round_to_zero _softfloat_float32_to_int64_round_to_zero +#define float64_to_int64_round_to_zero _softfloat_float64_to_int64_round_to_zero +#define float32_to_uint32_round_to_zero _softfloat_float32_to_uint32_round_to_zero +#define float64_to_uint32_round_to_zero _softfloat_float64_to_uint32_round_to_zero +#define float32_to_float64 _softfloat_float32_to_float64 +#define float64_to_float32 _softfloat_float64_to_float32 +#define float32_is_signaling_nan _softfloat_float32_is_signaling_nan +#define float64_is_signaling_nan _softfloat_float64_is_signaling_nan + +#endif /* SOFTFLOATSPARC64_FOR_GCC and !SOFTFLOAT_FOR_GCC */ + +/* + * The following will always end up in the namespace if FLOAT128 is + * defined and SOFTFLOAT_FOR_GCC isn't. So rename them out of the user's + * namespace. + */ + +#ifdef SOFTFLOATSPARC64_FOR_GCC +#define float128_add _softfloat_float128_add +#define float128_div _softfloat_float128_div +#define float128_eq _softfloat_float128_eq +#define float128_eq_signaling _softfloat_float128_eq_signaling +#define float128_is_nan _softfloat_float128_is_nan +#define float128_is_signaling_nan _softfloat_float128_is_signaling_nan +#define float128_le _softfloat_float128_le +#define float128_le_quiet _softfloat_float128_le_quiet +#define float128_lt _softfloat_float128_lt +#define float128_lt_quiet _softfloat_float128_lt_quiet +#define float128_mul _softfloat_float128_mul +#define float128_rem _softfloat_float128_rem +#define float128_round_to_int _softfloat_float128_round_to_int +#define float128_sqrt _softfloat_float128_sqrt +#define float128_sub _softfloat_float128_sub +#define float128_to_float32 _softfloat_float128_to_float32 +#define float128_to_float64 _softfloat_float128_to_float64 +#define float128_to_int32 _softfloat_float128_to_int32 +#define float128_to_int32_round_to_zero _softfloat_float128_to_int32_round_to_zero +#define float128_to_int64 _softfloat_float128_to_int64 +#define float128_to_int64_round_to_zero _softfloat_float128_to_int64_round_to_zero +#define float128_to_uint64_round_to_zero _softfloat_float128_to_uint64_round_to_zero +#define float32_to_float128 _softfloat_float32_to_float128 +#define float64_to_float128 _softfloat_float64_to_float128 +#define int32_to_float128 _softfloat_int32_to_float128 +#define int64_to_float128 _softfloat_int64_to_float128 + +/* + * If this isn't defined go ahead and set it now since this is now past + * anywhere define's are happening and this will conditionally compile out + * a lot of extraneous code in softfloat.c + */ + +#ifndef SOFTFLOAT_FOR_GCC +#define SOFTFLOAT_FOR_GCC +#endif + +#endif /* SOFTFLOATSPARC64_FOR_GCC */ diff --git a/lib/nbsd_libc/arch/sparc64/softfloat/softfloat-wrapper.c b/lib/nbsd_libc/arch/sparc64/softfloat/softfloat-wrapper.c new file mode 100644 index 000000000..ac5582aff --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/softfloat/softfloat-wrapper.c @@ -0,0 +1 @@ +#include diff --git a/lib/nbsd_libc/arch/sparc64/softfloat/softfloat.h b/lib/nbsd_libc/arch/sparc64/softfloat/softfloat.h new file mode 100644 index 000000000..069c0e5b6 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/softfloat/softfloat.h @@ -0,0 +1,303 @@ +/* $NetBSD: softfloat.h,v 1.6 2007/11/08 15:50:23 martin Exp $ */ + +/* This is a derivative work. */ + +/* +=============================================================================== + +This C header file is part of the SoftFloat IEC/IEEE Floating-point +Arithmetic Package, Release 2a. + +Written by John R. Hauser. This work was made possible in part by the +International Computer Science Institute, located at Suite 600, 1947 Center +Street, Berkeley, California 94704. Funding was partially provided by the +National Science Foundation under grant MIP-9311980. The original version +of this code was written as part of a project to build a fixed-point vector +processor in collaboration with the University of California at Berkeley, +overseen by Profs. Nelson Morgan and John Wawrzynek. More information +is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ +arithmetic/SoftFloat.html'. + +THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort +has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT +TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO +PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY +AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. + +Derivative works are acceptable, even for commercial purposes, so long as +(1) they include prominent notice that the work is derivative, and (2) they +include prominent notice akin to these four paragraphs for those parts of +this code that are retained. + +=============================================================================== +*/ + +/* +------------------------------------------------------------------------------- +The macro `FLOATX80' must be defined to enable the extended double-precision +floating-point format `floatx80'. If this macro is not defined, the +`floatx80' type will not be defined, and none of the functions that either +input or output the `floatx80' type will be defined. The same applies to +the `FLOAT128' macro and the quadruple-precision format `float128'. +------------------------------------------------------------------------------- +*/ +/* #define FLOATX80 */ +#define FLOAT128 + +#include "softfloat-qp.h" + +#include + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point types. +------------------------------------------------------------------------------- +*/ +typedef unsigned int float32; +typedef unsigned long long float64; +#ifdef FLOATX80 +typedef struct { + unsigned short high; + unsigned long long low; +} floatx80; +#endif +#ifdef FLOAT128 +typedef struct { + unsigned long long high, low; +} float128; +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point underflow tininess-detection mode. +------------------------------------------------------------------------------- +*/ +#ifndef SOFTFLOAT_FOR_GCC +extern int8 float_detect_tininess; +#endif +enum { + float_tininess_after_rounding = 0, + float_tininess_before_rounding = 1 +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point rounding mode. +------------------------------------------------------------------------------- +*/ +extern fp_rnd float_rounding_mode; +enum { + float_round_nearest_even = FP_RN, + float_round_to_zero = FP_RZ, + float_round_down = FP_RM, + float_round_up = FP_RP +}; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE floating-point exception flags. +------------------------------------------------------------------------------- +*/ +extern fp_except float_exception_flags; +extern fp_except float_exception_mask; +enum { + float_flag_inexact = FP_X_IMP, + float_flag_underflow = FP_X_UFL, + float_flag_overflow = FP_X_OFL, + float_flag_divbyzero = FP_X_DZ, + float_flag_invalid = FP_X_INV +}; + +/* +------------------------------------------------------------------------------- +Routine to raise any or all of the software IEC/IEEE floating-point +exception flags. +------------------------------------------------------------------------------- +*/ +void float_raise( fp_except ); + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE integer-to-floating-point conversion routines. +------------------------------------------------------------------------------- +*/ +float32 int32_to_float32( int ); +float64 int32_to_float64( int ); +#ifdef FLOATX80 +floatx80 int32_to_floatx80( int ); +#endif +#ifdef FLOAT128 +float128 int32_to_float128( int ); +#endif +float32 int64_to_float32( long long ); +float64 int64_to_float64( long long ); +#ifdef FLOATX80 +floatx80 int64_to_floatx80( long long ); +#endif +#ifdef FLOAT128 +float128 int64_to_float128( long long ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float32_to_int32( float32 ); +int float32_to_int32_round_to_zero( float32 ); +unsigned int float32_to_uint32_round_to_zero( float32 ); +long long float32_to_int64( float32 ); +long long float32_to_int64_round_to_zero( float32 ); +float64 float32_to_float64( float32 ); +#ifdef FLOATX80 +floatx80 float32_to_floatx80( float32 ); +#endif +#ifdef FLOAT128 +float128 float32_to_float128( float32 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE single-precision operations. +------------------------------------------------------------------------------- +*/ +float32 float32_round_to_int( float32 ); +float32 float32_add( float32, float32 ); +float32 float32_sub( float32, float32 ); +float32 float32_mul( float32, float32 ); +float32 float32_div( float32, float32 ); +float32 float32_rem( float32, float32 ); +float32 float32_sqrt( float32 ); +flag float32_eq( float32, float32 ); +flag float32_le( float32, float32 ); +flag float32_lt( float32, float32 ); +flag float32_eq_signaling( float32, float32 ); +flag float32_le_quiet( float32, float32 ); +flag float32_lt_quiet( float32, float32 ); +flag float32_is_signaling_nan( float32 ); + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float64_to_int32( float64 ); +int float64_to_int32_round_to_zero( float64 ); +unsigned int float64_to_uint32_round_to_zero( float64 ); +long long float64_to_int64( float64 ); +long long float64_to_int64_round_to_zero( float64 ); +float32 float64_to_float32( float64 ); +#ifdef FLOATX80 +floatx80 float64_to_floatx80( float64 ); +#endif +#ifdef FLOAT128 +float128 float64_to_float128( float64 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE double-precision operations. +------------------------------------------------------------------------------- +*/ +float64 float64_round_to_int( float64 ); +float64 float64_add( float64, float64 ); +float64 float64_sub( float64, float64 ); +float64 float64_mul( float64, float64 ); +float64 float64_div( float64, float64 ); +float64 float64_rem( float64, float64 ); +float64 float64_sqrt( float64 ); +flag float64_eq( float64, float64 ); +flag float64_le( float64, float64 ); +flag float64_lt( float64, float64 ); +flag float64_eq_signaling( float64, float64 ); +flag float64_le_quiet( float64, float64 ); +flag float64_lt_quiet( float64, float64 ); +flag float64_is_signaling_nan( float64 ); + +#ifdef FLOATX80 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int floatx80_to_int32( floatx80 ); +int floatx80_to_int32_round_to_zero( floatx80 ); +long long floatx80_to_int64( floatx80 ); +long long floatx80_to_int64_round_to_zero( floatx80 ); +float32 floatx80_to_float32( floatx80 ); +float64 floatx80_to_float64( floatx80 ); +#ifdef FLOAT128 +float128 floatx80_to_float128( floatx80 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision rounding precision. Valid +values are 32, 64, and 80. +------------------------------------------------------------------------------- +*/ +extern int floatx80_rounding_precision; + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE extended double-precision operations. +------------------------------------------------------------------------------- +*/ +floatx80 floatx80_round_to_int( floatx80 ); +floatx80 floatx80_add( floatx80, floatx80 ); +floatx80 floatx80_sub( floatx80, floatx80 ); +floatx80 floatx80_mul( floatx80, floatx80 ); +floatx80 floatx80_div( floatx80, floatx80 ); +floatx80 floatx80_rem( floatx80, floatx80 ); +floatx80 floatx80_sqrt( floatx80 ); +flag floatx80_eq( floatx80, floatx80 ); +flag floatx80_le( floatx80, floatx80 ); +flag floatx80_lt( floatx80, floatx80 ); +flag floatx80_eq_signaling( floatx80, floatx80 ); +flag floatx80_le_quiet( floatx80, floatx80 ); +flag floatx80_lt_quiet( floatx80, floatx80 ); +flag floatx80_is_signaling_nan( floatx80 ); + +#endif + +#ifdef FLOAT128 + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision conversion routines. +------------------------------------------------------------------------------- +*/ +int float128_to_int32( float128 ); +int float128_to_int32_round_to_zero( float128 ); +long long float128_to_int64( float128 ); +long long float128_to_int64_round_to_zero( float128 ); +unsigned long long float128_to_uint64_round_to_zero( float128 ); +float32 float128_to_float32( float128 ); +float64 float128_to_float64( float128 ); +#ifdef FLOATX80 +floatx80 float128_to_floatx80( float128 ); +#endif + +/* +------------------------------------------------------------------------------- +Software IEC/IEEE quadruple-precision operations. +------------------------------------------------------------------------------- +*/ +float128 float128_round_to_int( float128 ); +float128 float128_add( float128, float128 ); +float128 float128_sub( float128, float128 ); +float128 float128_mul( float128, float128 ); +float128 float128_div( float128, float128 ); +float128 float128_rem( float128, float128 ); +float128 float128_sqrt( float128 ); +flag float128_eq( float128, float128 ); +flag float128_le( float128, float128 ); +flag float128_lt( float128, float128 ); +flag float128_eq_signaling( float128, float128 ); +flag float128_le_quiet( float128, float128 ); +flag float128_lt_quiet( float128, float128 ); +flag float128_is_signaling_nan( float128 ); +flag float128_is_nan( float128 ); + +#endif diff --git a/lib/nbsd_libc/arch/sparc64/softfloat/sparc64-gcc.h b/lib/nbsd_libc/arch/sparc64/softfloat/sparc64-gcc.h new file mode 100644 index 000000000..778e9d0e3 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/softfloat/sparc64-gcc.h @@ -0,0 +1,89 @@ +/* $NetBSD: sparc64-gcc.h,v 1.2 2005/12/24 21:11:16 perry Exp $ */ + +/* +------------------------------------------------------------------------------- +One of the macros `BIGENDIAN' or `LITTLEENDIAN' must be defined. +------------------------------------------------------------------------------- +*/ +#include +#if _BYTE_ORDER == _BIG_ENDIAN +#define BIGENDIAN +#endif +#if _BYTE_ORDER == _LITTLE_ENDIAN +#define LITTLEENDIAN +#endif + +/* +------------------------------------------------------------------------------- +The macro `BITS64' can be defined to indicate that 64-bit integer types are +supported by the compiler. +------------------------------------------------------------------------------- +*/ +#define BITS64 + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines the most convenient type that holds +integers of at least as many bits as specified. For example, `uint8' should +be the most convenient type that can hold unsigned integers of as many as +8 bits. The `flag' type must be able to hold either a 0 or 1. For most +implementations of C, `flag', `uint8', and `int8' should all be `typedef'ed +to the same as `int'. +------------------------------------------------------------------------------- +*/ +typedef int flag; +typedef int uint8; +typedef int int8; +typedef int uint16; +typedef int int16; +typedef unsigned int uint32; +typedef signed int int32; +#ifdef BITS64 +typedef unsigned long long int uint64; +typedef signed long long int int64; +#endif + +/* +------------------------------------------------------------------------------- +Each of the following `typedef's defines a type that holds integers +of _exactly_ the number of bits specified. For instance, for most +implementation of C, `bits16' and `sbits16' should be `typedef'ed to +`unsigned short int' and `signed short int' (or `short int'), respectively. +------------------------------------------------------------------------------- +*/ +typedef unsigned char bits8; +typedef signed char sbits8; +typedef unsigned short int bits16; +typedef signed short int sbits16; +typedef unsigned int bits32; +typedef signed int sbits32; +#ifdef BITS64 +typedef unsigned long long int bits64; +typedef signed long long int sbits64; +#endif + +#ifdef BITS64 +/* +------------------------------------------------------------------------------- +The `LIT64' macro takes as its argument a textual integer literal and +if necessary ``marks'' the literal as having a 64-bit integer type. +For example, the GNU C Compiler (`gcc') requires that 64-bit literals be +appended with the letters `LL' standing for `long long', which is `gcc's +name for the 64-bit integer type. Some compilers may allow `LIT64' to be +defined as the identity macro: `#define LIT64( a ) a'. +------------------------------------------------------------------------------- +*/ +#define LIT64( a ) a##LL +#endif + +/* +------------------------------------------------------------------------------- +The macro `INLINE' can be used before functions that should be inlined. If +a compiler does not support explicit inlining, this macro should be defined +to be `static'. +------------------------------------------------------------------------------- +*/ +#define INLINE static inline + +#define FLOAT64_DEMANGLE(a) (a) +#define FLOAT64_MANGLE(a) (a) diff --git a/lib/nbsd_libc/arch/sparc64/stdlib/Makefile.inc b/lib/nbsd_libc/arch/sparc64/stdlib/Makefile.inc new file mode 100644 index 000000000..2f91b992a --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/stdlib/Makefile.inc @@ -0,0 +1 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/31 20:40:00 dsl Exp $ diff --git a/lib/nbsd_libc/arch/sparc64/stdlib/abs.S b/lib/nbsd_libc/arch/sparc64/stdlib/abs.S new file mode 100644 index 000000000..379b26e95 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/stdlib/abs.S @@ -0,0 +1,53 @@ +/* $NetBSD: abs.S,v 1.2 2003/08/07 16:42:28 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: abs.s,v 1.1 91/07/06 18:01:57 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)abs.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: abs.S,v 1.2 2003/08/07 16:42:28 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* abs - int absolute value */ + + +ENTRY(abs) + neg %o0, %o1 + retl + movrlz %o0, %o1, %o0 diff --git a/lib/nbsd_libc/arch/sparc64/string/Makefile.inc b/lib/nbsd_libc/arch/sparc64/string/Makefile.inc new file mode 100644 index 000000000..3d197b8bb --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/string/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.8 2009/08/11 17:30:43 dsl Exp $ + +SRCS+= ffs.S memcpy.S memset.S strlen.S +NO_SRCS+= bcopy.S bzero.S diff --git a/lib/nbsd_libc/arch/sparc64/string/memcpy.S b/lib/nbsd_libc/arch/sparc64/string/memcpy.S new file mode 100644 index 000000000..7d677f4d3 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/string/memcpy.S @@ -0,0 +1,1958 @@ +/* $NetBSD: memcpy.S,v 1.2 2001/08/01 05:52:12 eeh Exp $ */ + +/* + * Copyright (c) 2001 Eduardo E. Horvath + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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 +#ifndef _LOCORE +#define _LOCORE +#endif +#include +#include +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: memcpy.S,v 1.2 2001/08/01 05:52:12 eeh Exp $") +#endif /* LIBC_SCCS and not lint */ + +#define EMPTY nop +#define NOTREACHED ta 1 + +#define BCOPY_SMALL 16 +#define BLOCK_SIZE 64 + +#if 0 +#define ASI_STORE ASI_BLK_COMMIT_P +#else +#define ASI_STORE ASI_BLK_P +#endif + +#if 1 +/* + * kernel bcopy/memcpy + * Assumes regions do not overlap; has no useful return value. + * + * Must not use %g7 (see copyin/copyout above). + */ +ENTRY(memcpy) /* dest, src, size */ + /* + * Swap args for bcopy. Gcc generates calls to memcpy for + * structure assignments. + */ + mov %o0, %o3 + mov %o1, %o0 + mov %o3, %o1 +#endif +ENTRY(bcopy) /* src, dest, size */ +#ifdef DEBUG + set pmapdebug, %o4 + ld [%o4], %o4 + btst 0x80, %o4 ! PDB_COPY + bz,pt %icc, 3f + nop + save %sp, -CC64FSZ, %sp + mov %i0, %o1 + set 2f, %o0 + mov %i1, %o2 + call printf + mov %i2, %o3 +! ta 1; nop + restore + .data +2: .asciz "bcopy(%p->%p,%x)\n" + _ALIGN + .text +3: +#endif + /* + * Check for overlaps and punt. + * + * If src <= dest <= src+len we have a problem. + */ + + sub %o1, %o0, %o3 + + cmp %o3, %o2 + blu,pn %xcc, Lovbcopy + cmp %o2, BCOPY_SMALL +Lbcopy_start: + bge,pt %xcc, 2f ! if >= this many, go be fancy. + cmp %o2, 256 + + mov %o1, %o5 ! Save memcpy return value + /* + * Not much to copy, just do it a byte at a time. + */ + deccc %o2 ! while (--len >= 0) + bl 1f + EMPTY +0: + inc %o0 + ldsb [%o0 - 1], %o4 ! (++dst)[-1] = *src++; + stb %o4, [%o1] + deccc %o2 + bge 0b + inc %o1 +1: + retl + mov %o5, %o0 + NOTREACHED + + /* + * Overlapping bcopies -- punt. + */ +Lovbcopy: + + /* + * Since src comes before dst, and the regions might overlap, + * we have to do the copy starting at the end and working backwards. + * + * We could optimize this, but it almost never happens. + */ + mov %o1, %o5 ! Retval + add %o2, %o0, %o0 ! src += len + add %o2, %o1, %o1 ! dst += len + + deccc %o2 + bl,pn %xcc, 1f + dec %o0 +0: + dec %o1 + ldsb [%o0], %o4 + dec %o0 + + deccc %o2 + bge,pt %xcc, 0b + stb %o4, [%o1] +1: + retl + mov %o5, %o0 + + /* + * Plenty of data to copy, so try to do it optimally. + */ +2: +#if 1 + ! If it is big enough, use VIS instructions + bge Lbcopy_block + nop +#endif +Lbcopy_fancy: + + !! + !! First align the output to a 8-byte entity + !! + + save %sp, -CC64FSZ, %sp + + mov %i0, %o0 + mov %i1, %o1 + + mov %i2, %o2 + btst 1, %o1 + + bz,pt %icc, 4f + btst 2, %o1 + ldub [%o0], %o4 ! Load 1st byte + + deccc 1, %o2 + ble,pn %xcc, Lbcopy_finish ! XXXX + inc 1, %o0 + + stb %o4, [%o1] ! Store 1st byte + inc 1, %o1 ! Update address + btst 2, %o1 +4: + bz,pt %icc, 4f + + btst 1, %o0 + bz,a 1f + lduh [%o0], %o4 ! Load short + + ldub [%o0], %o4 ! Load bytes + + ldub [%o0+1], %o3 + sllx %o4, 8, %o4 + or %o3, %o4, %o4 + +1: + deccc 2, %o2 + ble,pn %xcc, Lbcopy_finish ! XXXX + inc 2, %o0 + sth %o4, [%o1] ! Store 1st short + + inc 2, %o1 +4: + btst 4, %o1 + bz,pt %xcc, 4f + + btst 3, %o0 + bz,a,pt %xcc, 1f + lduw [%o0], %o4 ! Load word -1 + + btst 1, %o0 + bz,a,pt %icc, 2f + lduh [%o0], %o4 + + ldub [%o0], %o4 + + lduh [%o0+1], %o3 + sllx %o4, 16, %o4 + or %o4, %o3, %o4 + + ldub [%o0+3], %o3 + sllx %o4, 8, %o4 + ba,pt %icc, 1f + or %o4, %o3, %o4 + +2: + lduh [%o0+2], %o3 + sllx %o4, 16, %o4 + or %o4, %o3, %o4 + +1: + deccc 4, %o2 + ble,pn %xcc, Lbcopy_finish ! XXXX + inc 4, %o0 + + st %o4, [%o1] ! Store word + inc 4, %o1 +4: + !! + !! We are now 32-bit aligned in the dest. + !! +Lbcopy__common: + + and %o0, 7, %o4 ! Shift amount + andn %o0, 7, %o0 ! Source addr + + brz,pt %o4, Lbcopy_noshift8 ! No shift version... + + sllx %o4, 3, %o4 ! In bits + mov 8<<3, %o3 + + ldx [%o0], %l0 ! Load word -1 + sub %o3, %o4, %o3 ! Reverse shift + deccc 16*8, %o2 ! Have enough room? + + sllx %l0, %o4, %l0 + bl,pn %xcc, 2f + and %o3, 0x38, %o3 +Lbcopy_unrolled8: + + /* + * This is about as close to optimal as you can get, since + * the shifts require EU0 and cannot be paired, and you have + * 3 dependent operations on the data. + */ + +! ldx [%o0+0*8], %l0 ! Already done +! sllx %l0, %o4, %l0 ! Already done + ldx [%o0+1*8], %l1 + ldx [%o0+2*8], %l2 + ldx [%o0+3*8], %l3 + ldx [%o0+4*8], %l4 + ldx [%o0+5*8], %l5 + ldx [%o0+6*8], %l6 +#if 1 + ba,pt %icc, 1f + ldx [%o0+7*8], %l7 + .align 8 +1: + srlx %l1, %o3, %g1 + inc 8*8, %o0 + + sllx %l1, %o4, %l1 + or %g1, %l0, %o5 + ldx [%o0+0*8], %l0 + + stx %o5, [%o1+0*8] + srlx %l2, %o3, %g1 + + sllx %l2, %o4, %l2 + or %g1, %l1, %o5 + ldx [%o0+1*8], %l1 + + stx %o5, [%o1+1*8] + srlx %l3, %o3, %g1 + + sllx %l3, %o4, %l3 + or %g1, %l2, %o5 + ldx [%o0+2*8], %l2 + + stx %o5, [%o1+2*8] + srlx %l4, %o3, %g1 + + sllx %l4, %o4, %l4 + or %g1, %l3, %o5 + ldx [%o0+3*8], %l3 + + stx %o5, [%o1+3*8] + srlx %l5, %o3, %g1 + + sllx %l5, %o4, %l5 + or %g1, %l4, %o5 + ldx [%o0+4*8], %l4 + + stx %o5, [%o1+4*8] + srlx %l6, %o3, %g1 + + sllx %l6, %o4, %l6 + or %g1, %l5, %o5 + ldx [%o0+5*8], %l5 + + stx %o5, [%o1+5*8] + srlx %l7, %o3, %g1 + + sllx %l7, %o4, %l7 + or %g1, %l6, %o5 + ldx [%o0+6*8], %l6 + + stx %o5, [%o1+6*8] + srlx %l0, %o3, %g1 + deccc 8*8, %o2 ! Have enough room? + + sllx %l0, %o4, %l0 ! Next loop + or %g1, %l7, %o5 + ldx [%o0+7*8], %l7 + + stx %o5, [%o1+7*8] + bge,pt %xcc, 1b + inc 8*8, %o1 + +Lbcopy_unrolled8_cleanup: + !! + !! Finished 8 byte block, unload the regs. + !! + srlx %l1, %o3, %g1 + inc 7*8, %o0 + + sllx %l1, %o4, %l1 + or %g1, %l0, %o5 + + stx %o5, [%o1+0*8] + srlx %l2, %o3, %g1 + + sllx %l2, %o4, %l2 + or %g1, %l1, %o5 + + stx %o5, [%o1+1*8] + srlx %l3, %o3, %g1 + + sllx %l3, %o4, %l3 + or %g1, %l2, %o5 + + stx %o5, [%o1+2*8] + srlx %l4, %o3, %g1 + + sllx %l4, %o4, %l4 + or %g1, %l3, %o5 + + stx %o5, [%o1+3*8] + srlx %l5, %o3, %g1 + + sllx %l5, %o4, %l5 + or %g1, %l4, %o5 + + stx %o5, [%o1+4*8] + srlx %l6, %o3, %g1 + + sllx %l6, %o4, %l6 + or %g1, %l5, %o5 + + stx %o5, [%o1+5*8] + srlx %l7, %o3, %g1 + + sllx %l7, %o4, %l7 + or %g1, %l6, %o5 + + stx %o5, [%o1+6*8] + inc 7*8, %o1 + + mov %l7, %l0 ! Save our unused data + dec 7*8, %o2 +#else + /* + * This version also handles aligned copies at almost the + * same speed. It should take the same number of cycles + * as the previous version, but is slightly slower, probably + * due to i$ issues. + */ + ldx [%o0+7*8], %l7 + ba,pt %icc, 1f + clr %g1 + .align 32 +1: + srlx %l1, %o3, %g1 + bz,pn %xcc, 3f + inc 8*8, %o0 + + sllx %l1, %o4, %l1 + or %g1, %l0, %o5 + ba,pt %icc, 4f + ldx [%o0+0*8], %l0 + + nop +3: + mov %l0, %o5 + ldx [%o0+0*8], %l0 + +4: + bz,pn %icc, 3f + stx %o5, [%o1+0*8] + srlx %l2, %o3, %g1 + + sllx %l2, %o4, %l2 +3: + or %g1, %l1, %o5 + ldx [%o0+1*8], %l1 + + bz,pn %icc, 3f + stx %o5, [%o1+1*8] + srlx %l3, %o3, %g1 + + sllx %l3, %o4, %l3 +3: + or %g1, %l2, %o5 + ldx [%o0+2*8], %l2 + + bz,pn %icc, 3f + stx %o5, [%o1+2*8] + srlx %l4, %o3, %g1 + + sllx %l4, %o4, %l4 +3: + or %g1, %l3, %o5 + ldx [%o0+3*8], %l3 + + bz,pn %icc, 3f + stx %o5, [%o1+3*8] + srlx %l5, %o3, %g1 + + sllx %l5, %o4, %l5 +3: + or %g1, %l4, %o5 + ldx [%o0+4*8], %l4 + + bz,pn %icc, 3f + stx %o5, [%o1+4*8] + srlx %l6, %o3, %g1 + + sllx %l6, %o4, %l6 +3: + or %g1, %l5, %o5 + ldx [%o0+5*8], %l5 + + bz,pn %icc, 3f + stx %o5, [%o1+5*8] + srlx %l7, %o3, %g1 + + sllx %l7, %o4, %l7 +3: + or %g1, %l6, %o5 + ldx [%o0+6*8], %l6 + + bz,pn %icc, 3f + stx %o5, [%o1+6*8] + srlx %l0, %o3, %g1 + + sllx %l0, %o4, %l0 ! Next loop +3: + or %g1, %l7, %o5 + ldx [%o0+7*8], %l7 + deccc 8*8, %o2 ! Have enough room? + + stx %o5, [%o1+7*8] + inc 8*8, %o1 + bge,pt %xcc, 1b + tst %o4 + + + !! + !! Now unload all those regs + !! +Lbcopy_unrolled8_cleanup: + srlx %l1, %o3, %g1 + bz,pn %xcc, 3f + inc 7*8, %o0 ! Point at the last load + + sllx %l1, %o4, %l1 + ba,pt %icc, 4f + or %g1, %l0, %o5 + +3: + mov %l0, %o5 + +4: + bz,pn %icc, 3f + stx %o5, [%o1+0*8] + srlx %l2, %o3, %g1 + + sllx %l2, %o4, %l2 +3: + or %g1, %l1, %o5 + + bz,pn %icc, 3f + stx %o5, [%o1+1*8] + srlx %l3, %o3, %g1 + + sllx %l3, %o4, %l3 +3: + or %g1, %l2, %o5 + + bz,pn %icc, 3f + stx %o5, [%o1+2*8] + srlx %l4, %o3, %g1 + + sllx %l4, %o4, %l4 +3: + or %g1, %l3, %o5 + + bz,pn %icc, 3f + stx %o5, [%o1+3*8] + srlx %l5, %o3, %g1 + + sllx %l5, %o4, %l5 +3: + or %g1, %l4, %o5 + + bz,pn %icc, 3f + stx %o5, [%o1+4*8] + srlx %l6, %o3, %g1 + + sllx %l6, %o4, %l6 +3: + or %g1, %l5, %o5 + + bz,pn %icc, 3f + stx %o5, [%o1+5*8] + srlx %l7, %o3, %g1 + + sllx %l7, %o4, %l7 +3: + or %g1, %l6, %o5 + mov %l7, %l0 ! Shuffle to %l0 + + stx %o5, [%o1+6*8] + or %g1, %l7, %o5 + dec 7*8, %o2 + + inc 7*8, %o1 ! Point at last store +#endif +2: + inccc 16*8, %o2 + bz,pn %icc, Lbcopy_complete + + !! Unrolled 8 times +Lbcopy_aligned8: +! ldx [%o0], %l0 ! Already done +! sllx %l0, %o4, %l0 ! Shift high word + + deccc 8, %o2 ! Pre-decrement + bl,pn %xcc, Lbcopy_finish +1: + ldx [%o0+8], %l1 ! Load word 0 + inc 8, %o0 + + srlx %l1, %o3, %o5 + or %o5, %l0, %o5 ! Combine + + stx %o5, [%o1] ! Store result + inc 8, %o1 + + deccc 8, %o2 + bge,pn %xcc, 1b + sllx %l1, %o4, %l0 + + btst 7, %o2 ! Done? + bz,pt %xcc, Lbcopy_complete + + !! + !! Loadup the last dregs into %l0 and shift it into place + !! + srlx %o3, 3, %o5 ! # bytes in %l0 + dec 8, %o5 ! - 8 + !! n-8 - (by - 8) -> n - by + subcc %o2, %o5, %g0 ! # bytes we need + ble,pt %icc, Lbcopy_finish + nop + ldx [%o0+8], %l1 ! Need another word + srlx %l1, %o3, %l1 + ba,pt %icc, Lbcopy_finish + or %l0, %l1, %l0 ! All loaded up. + +Lbcopy_noshift8: + deccc 8*8, %o2 ! Have enough room? + bl,pn %xcc, 2f + nop + ba,pt %icc, 1f + nop + .align 32 +1: + ldx [%o0+0*8], %l0 + ldx [%o0+1*8], %l1 + ldx [%o0+2*8], %l2 + ldx [%o0+3*8], %l3 + stx %l0, [%o1+0*8] + stx %l1, [%o1+1*8] + stx %l2, [%o1+2*8] + stx %l3, [%o1+3*8] + + + ldx [%o0+4*8], %l4 + ldx [%o0+5*8], %l5 + ldx [%o0+6*8], %l6 + ldx [%o0+7*8], %l7 + inc 8*8, %o0 + stx %l4, [%o1+4*8] + stx %l5, [%o1+5*8] + deccc 8*8, %o2 + stx %l6, [%o1+6*8] + stx %l7, [%o1+7*8] + stx %l2, [%o1+2*8] + bge,pt %xcc, 1b + inc 8*8, %o1 +2: + inc 8*8, %o2 +1: + deccc 8, %o2 + bl,pn %icc, 1f ! < 0 --> sub word + nop + ldx [%o0], %o5 + inc 8, %o0 + stx %o5, [%o1] + bg,pt %icc, 1b ! Exactly 0 --> done + inc 8, %o1 +1: + btst 7, %o2 ! Done? + bz,pt %xcc, Lbcopy_complete + clr %o4 + ldx [%o0], %l0 +Lbcopy_finish: + + brz,pn %o2, 2f ! 100% complete? + cmp %o2, 8 ! Exactly 8 bytes? + bz,a,pn %xcc, 2f + stx %l0, [%o1] + + btst 4, %o2 ! Word store? + bz %xcc, 1f + srlx %l0, 32, %o5 ! Shift high word down + stw %o5, [%o1] + inc 4, %o1 + mov %l0, %o5 ! Operate on the low bits +1: + btst 2, %o2 + mov %o5, %l0 + bz 1f + srlx %l0, 16, %o5 + + sth %o5, [%o1] ! Store short + inc 2, %o1 + mov %l0, %o5 ! Operate on low bytes +1: + mov %o5, %l0 + btst 1, %o2 ! Byte aligned? + bz 2f + srlx %l0, 8, %o5 + + stb %o5, [%o1] ! Store last byte + inc 1, %o1 ! Update address +2: +Lbcopy_complete: +#if 0 + !! + !! verify copy success. + !! + + mov %i0, %o2 + mov %i1, %o4 + mov %i2, %l4 +0: + ldub [%o2], %o1 + inc %o2 + ldub [%o4], %o3 + inc %o4 + cmp %o3, %o1 + bnz 1f + dec %l4 + brnz %l4, 0b + nop + ba 2f + nop + +1: + set 0f, %o0 + call printf + sub %i2, %l4, %o5 + set 1f, %o0 + mov %i0, %o1 + mov %i1, %o2 + call printf + mov %i2, %o3 + ta 1 + .data +0: .asciz "bcopy failed: %x@%p != %x@%p byte %d\n" +1: .asciz "bcopy(%p, %p, %lx)\n" + .align 8 + .text +2: +#endif + ret + restore %i1, %g0, %o0 + +#if 1 + +/* + * Block copy. Useful for >256 byte copies. + * + * Benchmarking has shown this always seems to be slower than + * the integer version, so this is disabled. Maybe someone will + * figure out why sometime. + */ + +Lbcopy_block: +#ifdef _KERNEL +/* + * Kernel: + * + * Here we use VIS instructions to do a block clear of a page. + * But before we can do that we need to save and enable the FPU. + * The last owner of the FPU registers is fpproc, and + * fpproc->p_md.md_fpstate is the current fpstate. If that's not + * null, call savefpstate() with it to store our current fp state. + * + * Next, allocate an aligned fpstate on the stack. We will properly + * nest calls on a particular stack so this should not be a problem. + * + * Now we grab either curproc (or if we're on the interrupt stack + * proc0). We stash its existing fpstate in a local register and + * put our new fpstate in curproc->p_md.md_fpstate. We point + * fpproc at curproc (or proc0) and enable the FPU. + * + * If we are ever preempted, our FPU state will be saved in our + * fpstate. Then, when we're resumed and we take an FPDISABLED + * trap, the trap handler will be able to fish our FPU state out + * of curproc (or proc0). + * + * On exiting this routine we undo the damage: restore the original + * pointer to curproc->p_md.md_fpstate, clear our fpproc, and disable + * the MMU. + * + * + * Register usage, Kernel only (after save): + * + * %i0 src + * %i1 dest + * %i2 size + * + * %l0 XXXX DEBUG old fpstate + * %l1 fpproc (hi bits only) + * %l2 orig fpproc + * %l3 orig fpstate + * %l5 curproc + * %l6 old fpstate + * + * Register ussage, Kernel and user: + * + * %g1 src (retval for memcpy) + * + * %o0 src + * %o1 dest + * %o2 end dest + * %o5 last safe fetchable address + */ + +#if 1 + ENABLE_FPU(0) +#else + save %sp, -(CC64FSZ+FS_SIZE+BLOCK_SIZE), %sp ! Allocate an fpstate + sethi %hi(FPPROC), %l1 + LDPTR [%l1 + %lo(FPPROC)], %l2 ! Load fpproc + add %sp, (CC64FSZ+STKB+BLOCK_SIZE-1), %l0 ! Calculate pointer to fpstate + brz,pt %l2, 1f ! fpproc == NULL? + andn %l0, BLOCK_ALIGN, %l0 ! And make it block aligned + LDPTR [%l2 + P_FPSTATE], %l3 + brz,pn %l3, 1f ! Make sure we have an fpstate + mov %l3, %o0 + call _C_LABEL(savefpstate) ! Save the old fpstate + set EINTSTACK-STKB, %l4 ! Are we on intr stack? + cmp %sp, %l4 + bgu,pt %xcc, 1f + set INTSTACK-STKB, %l4 + cmp %sp, %l4 + blu %xcc, 1f +0: + sethi %hi(_C_LABEL(proc0)), %l4 ! Yes, use proc0 + ba,pt %xcc, 2f ! XXXX needs to change to CPUs idle proc + or %l4, %lo(_C_LABEL(proc0)), %l5 +1: + sethi %hi(CURPROC), %l4 ! Use curproc + LDPTR [%l4 + %lo(CURPROC)], %l5 + brz,pn %l5, 0b ! If curproc is NULL need to use proc0 + nop +2: + LDPTR [%l5 + P_FPSTATE], %l6 ! Save old fpstate + STPTR %l0, [%l5 + P_FPSTATE] ! Insert new fpstate + STPTR %l5, [%l1 + %lo(FPPROC)] ! Set new fpproc + wr %g0, FPRS_FEF, %fprs ! Enable FPU +#endif + mov %i0, %o0 ! Src addr. + mov %i1, %o1 ! Store our dest ptr here. + mov %i2, %o2 ! Len counter +#endif + + !! + !! First align the output to a 64-bit entity + !! + + mov %o1, %g1 ! memcpy retval + add %o0, %o2, %o5 ! End of source block + + andn %o0, 7, %o3 ! Start of block + dec %o5 + fzero %f0 + + andn %o5, BLOCK_ALIGN, %o5 ! Last safe addr. + ldd [%o3], %f2 ! Load 1st word + + dec 8, %o3 ! Move %o3 1 word back + btst 1, %o1 + bz 4f + + mov -7, %o4 ! Lowest src addr possible + alignaddr %o0, %o4, %o4 ! Base addr for load. + + cmp %o3, %o4 + be,pt %xcc, 1f ! Already loaded? + mov %o4, %o3 + fmovd %f2, %f0 ! No. Shift + ldd [%o3+8], %f2 ! And load +1: + + faligndata %f0, %f2, %f4 ! Isolate 1st byte + + stda %f4, [%o1] ASI_FL8_P ! Store 1st byte + inc 1, %o1 ! Update address + inc 1, %o0 + dec 1, %o2 +4: + btst 2, %o1 + bz 4f + + mov -6, %o4 ! Calculate src - 6 + alignaddr %o0, %o4, %o4 ! calculate shift mask and dest. + + cmp %o3, %o4 ! Addresses same? + be,pt %xcc, 1f + mov %o4, %o3 + fmovd %f2, %f0 ! Shuffle data + ldd [%o3+8], %f2 ! Load word 0 +1: + faligndata %f0, %f2, %f4 ! Move 1st short low part of f8 + + stda %f4, [%o1] ASI_FL16_P ! Store 1st short + dec 2, %o2 + inc 2, %o1 + inc 2, %o0 +4: + brz,pn %o2, Lbcopy_blockfinish ! XXXX + + btst 4, %o1 + bz 4f + + mov -4, %o4 + alignaddr %o0, %o4, %o4 ! calculate shift mask and dest. + + cmp %o3, %o4 ! Addresses same? + beq,pt %xcc, 1f + mov %o4, %o3 + fmovd %f2, %f0 ! Shuffle data + ldd [%o3+8], %f2 ! Load word 0 +1: + faligndata %f0, %f2, %f4 ! Move 1st short low part of f8 + + st %f5, [%o1] ! Store word + dec 4, %o2 + inc 4, %o1 + inc 4, %o0 +4: + brz,pn %o2, Lbcopy_blockfinish ! XXXX + !! + !! We are now 32-bit aligned in the dest. + !! +Lbcopy_block_common: + + mov -0, %o4 + alignaddr %o0, %o4, %o4 ! base - shift + + cmp %o3, %o4 ! Addresses same? + beq,pt %xcc, 1f + mov %o4, %o3 + fmovd %f2, %f0 ! Shuffle data + ldd [%o3+8], %f2 ! Load word 0 +1: + add %o3, 8, %o0 ! now use %o0 for src + + !! + !! Continue until our dest is block aligned + !! +Lbcopy_block_aligned8: +1: + brz %o2, Lbcopy_blockfinish + btst BLOCK_ALIGN, %o1 ! Block aligned? + bz 1f + + faligndata %f0, %f2, %f4 ! Generate result + deccc 8, %o2 + ble,pn %icc, Lbcopy_blockfinish ! Should never happen + fmovd %f4, %f48 + + std %f4, [%o1] ! Store result + inc 8, %o1 + + fmovd %f2, %f0 + inc 8, %o0 + ba,pt %xcc, 1b ! Not yet. + ldd [%o0], %f2 ! Load next part +Lbcopy_block_aligned64: +1: + +/* + * 64-byte aligned -- ready for block operations. + * + * Here we have the destination block aligned, but the + * source pointer may not be. Sub-word alignment will + * be handled by faligndata instructions. But the source + * can still be potentially aligned to 8 different words + * in our 64-bit block, so we have 8 different copy routines. + * + * Once we figure out our source alignment, we branch + * to the appropriate copy routine, which sets up the + * alignment for faligndata and loads (sets) the values + * into the source registers and does the copy loop. + * + * When were down to less than 1 block to store, we + * exit the copy loop and execute cleanup code. + * + * Block loads and stores are not properly interlocked. + * Stores save one reg/cycle, so you can start overwriting + * registers the cycle after the store is issued. + * + * Block loads require a block load to a different register + * block or a membar #Sync before accessing the loaded + * data. + * + * Since the faligndata instructions may be offset as far + * as 7 registers into a block (if you are shifting source + * 7 -> dest 0), you need 3 source register blocks for full + * performance: one you are copying, one you are loading, + * and one for interlocking. Otherwise, we would need to + * sprinkle the code with membar #Sync and lose the advantage + * of running faligndata in parallel with block stores. This + * means we are fetching a full 128 bytes ahead of the stores. + * We need to make sure the prefetch does not inadvertently + * cross a page boundary and fault on data that we will never + * store. + * + */ +#if 1 + and %o0, BLOCK_ALIGN, %o3 + srax %o3, 3, %o3 ! Isolate the offset + + brz %o3, L100 ! 0->0 + btst 4, %o3 + bnz %xcc, 4f + btst 2, %o3 + bnz %xcc, 2f + btst 1, %o3 + ba,pt %xcc, L101 ! 0->1 + nop /* XXX spitfire bug */ +2: + bz %xcc, L102 ! 0->2 + nop + ba,pt %xcc, L103 ! 0->3 + nop /* XXX spitfire bug */ +4: + bnz %xcc, 2f + btst 1, %o3 + bz %xcc, L104 ! 0->4 + nop + ba,pt %xcc, L105 ! 0->5 + nop /* XXX spitfire bug */ +2: + bz %xcc, L106 ! 0->6 + nop + ba,pt %xcc, L107 ! 0->7 + nop /* XXX spitfire bug */ +#else + + !! + !! Isolate the word offset, which just happens to be + !! the slot in our jump table. + !! + !! This is 6 insns, most of which cannot be paired, + !! which is about the same as the above version. + !! + rd %pc, %o4 +1: + and %o0, 0x31, %o3 + add %o3, (Lbcopy_block_jmp - 1b), %o3 + jmpl %o4 + %o3, %g0 + nop + + !! + !! Jump table + !! + +Lbcopy_block_jmp: + ba,a,pt %xcc, L100 + nop + ba,a,pt %xcc, L101 + nop + ba,a,pt %xcc, L102 + nop + ba,a,pt %xcc, L103 + nop + ba,a,pt %xcc, L104 + nop + ba,a,pt %xcc, L105 + nop + ba,a,pt %xcc, L106 + nop + ba,a,pt %xcc, L107 + nop +#endif + + !! + !! Source is block aligned. + !! + !! Just load a block and go. + !! +L100: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L100" + .align 8 +2: +#endif + fmovd %f0 , %f62 + ldda [%o0] ASI_BLK_P, %f0 + inc BLOCK_SIZE, %o0 + cmp %o0, %o5 + bleu,a,pn %icc, 3f + ldda [%o0] ASI_BLK_P, %f16 + ba,pt %icc, 3f + membar #Sync + + .align 32 ! ICache align. +3: + faligndata %f62, %f0, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f0, %f2, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f2, %f4, %f36 + cmp %o0, %o5 + faligndata %f4, %f6, %f38 + faligndata %f6, %f8, %f40 + faligndata %f8, %f10, %f42 + faligndata %f10, %f12, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f12, %f14, %f46 + + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + stda %f32, [%o1] ASI_STORE + faligndata %f14, %f16, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f16, %f18, %f34 + inc BLOCK_SIZE, %o1 + faligndata %f18, %f20, %f36 + dec BLOCK_SIZE, %o2 + faligndata %f20, %f22, %f38 + cmp %o0, %o5 + faligndata %f22, %f24, %f40 + faligndata %f24, %f26, %f42 + faligndata %f26, %f28, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f28, %f30, %f46 + + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + stda %f32, [%o1] ASI_STORE + faligndata %f30, %f48, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f48, %f50, %f34 + inc BLOCK_SIZE, %o1 + faligndata %f50, %f52, %f36 + dec BLOCK_SIZE, %o2 + faligndata %f52, %f54, %f38 + cmp %o0, %o5 + faligndata %f54, %f56, %f40 + faligndata %f56, %f58, %f42 + faligndata %f58, %f60, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f60, %f62, %f46 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 ! Increment is at top + membar #Sync +2: + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + + !! + !! Source at BLOCK_ALIGN+8 + !! + !! We need to load almost 1 complete block by hand. + !! +L101: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L101" + .align 8 +2: +#endif +! fmovd %f0, %f0 ! Hoist fmovd + ldd [%o0], %f2 + inc 8, %o0 + ldd [%o0], %f4 + inc 8, %o0 + ldd [%o0], %f6 + inc 8, %o0 + ldd [%o0], %f8 + inc 8, %o0 + ldd [%o0], %f10 + inc 8, %o0 + ldd [%o0], %f12 + inc 8, %o0 + ldd [%o0], %f14 + inc 8, %o0 + + cmp %o0, %o5 + bleu,a,pn %icc, 3f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +3: + faligndata %f0, %f2, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f2, %f4, %f34 + cmp %o0, %o5 + faligndata %f4, %f6, %f36 + dec BLOCK_SIZE, %o2 + faligndata %f6, %f8, %f38 + faligndata %f8, %f10, %f40 + faligndata %f10, %f12, %f42 + faligndata %f12, %f14, %f44 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + brlez,pn %o2, Lbcopy_blockdone + faligndata %f14, %f16, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f16, %f18, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f18, %f20, %f34 + inc BLOCK_SIZE, %o1 + faligndata %f20, %f22, %f36 + cmp %o0, %o5 + faligndata %f22, %f24, %f38 + dec BLOCK_SIZE, %o2 + faligndata %f24, %f26, %f40 + faligndata %f26, %f28, %f42 + faligndata %f28, %f30, %f44 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + brlez,pn %o2, Lbcopy_blockdone + faligndata %f30, %f48, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f48, %f50, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f50, %f52, %f34 + inc BLOCK_SIZE, %o1 + faligndata %f52, %f54, %f36 + cmp %o0, %o5 + faligndata %f54, %f56, %f38 + dec BLOCK_SIZE, %o2 + faligndata %f56, %f58, %f40 + faligndata %f58, %f60, %f42 + faligndata %f60, %f62, %f44 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + brlez,pn %o2, Lbcopy_blockdone + faligndata %f62, %f0, %f46 + + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + + !! + !! Source at BLOCK_ALIGN+16 + !! + !! We need to load 6 doubles by hand. + !! +L102: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L102" + .align 8 +2: +#endif + ldd [%o0], %f4 + inc 8, %o0 + fmovd %f0, %f2 ! Hoist fmovd + ldd [%o0], %f6 + inc 8, %o0 + + ldd [%o0], %f8 + inc 8, %o0 + ldd [%o0], %f10 + inc 8, %o0 + ldd [%o0], %f12 + inc 8, %o0 + ldd [%o0], %f14 + inc 8, %o0 + + cmp %o0, %o5 + bleu,a,pn %icc, 3f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +3: + faligndata %f2, %f4, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f4, %f6, %f34 + cmp %o0, %o5 + faligndata %f6, %f8, %f36 + dec BLOCK_SIZE, %o2 + faligndata %f8, %f10, %f38 + faligndata %f10, %f12, %f40 + faligndata %f12, %f14, %f42 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + faligndata %f14, %f16, %f44 + + brlez,pn %o2, Lbcopy_blockdone + faligndata %f16, %f18, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f18, %f20, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f20, %f22, %f34 + inc BLOCK_SIZE, %o1 + faligndata %f22, %f24, %f36 + cmp %o0, %o5 + faligndata %f24, %f26, %f38 + dec BLOCK_SIZE, %o2 + faligndata %f26, %f28, %f40 + faligndata %f28, %f30, %f42 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + faligndata %f30, %f48, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f48, %f50, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f50, %f52, %f32 + inc BLOCK_SIZE, %o0 + faligndata %f52, %f54, %f34 + inc BLOCK_SIZE, %o1 + faligndata %f54, %f56, %f36 + cmp %o0, %o5 + faligndata %f56, %f58, %f38 + dec BLOCK_SIZE, %o2 + faligndata %f58, %f60, %f40 + faligndata %f60, %f62, %f42 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + faligndata %f62, %f0, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f0, %f2, %f46 + + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + + !! + !! Source at BLOCK_ALIGN+24 + !! + !! We need to load 5 doubles by hand. + !! +L103: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L103" + .align 8 +2: +#endif + fmovd %f0, %f4 + ldd [%o0], %f6 + inc 8, %o0 + ldd [%o0], %f8 + inc 8, %o0 + ldd [%o0], %f10 + inc 8, %o0 + ldd [%o0], %f12 + inc 8, %o0 + ldd [%o0], %f14 + inc 8, %o0 + + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + inc BLOCK_SIZE, %o0 +3: + faligndata %f4, %f6, %f32 + cmp %o0, %o5 + faligndata %f6, %f8, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f8, %f10, %f36 + faligndata %f10, %f12, %f38 + faligndata %f12, %f14, %f40 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + faligndata %f14, %f16, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f16, %f18, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f18, %f20, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f20, %f22, %f32 + cmp %o0, %o5 + faligndata %f22, %f24, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f24, %f26, %f36 + inc BLOCK_SIZE, %o1 + faligndata %f26, %f28, %f38 + faligndata %f28, %f30, %f40 + ble,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + faligndata %f30, %f48, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f48, %f50, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f50, %f52, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f52, %f54, %f32 + cmp %o0, %o5 + faligndata %f54, %f56, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f56, %f58, %f36 + faligndata %f58, %f60, %f38 + inc BLOCK_SIZE, %o1 + faligndata %f60, %f62, %f40 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + faligndata %f62, %f0, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f0, %f2, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f2, %f4, %f46 + + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + + !! + !! Source at BLOCK_ALIGN+32 + !! + !! We need to load 4 doubles by hand. + !! +L104: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L104" + .align 8 +2: +#endif + fmovd %f0, %f6 + ldd [%o0], %f8 + inc 8, %o0 + ldd [%o0], %f10 + inc 8, %o0 + ldd [%o0], %f12 + inc 8, %o0 + ldd [%o0], %f14 + inc 8, %o0 + + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + inc BLOCK_SIZE, %o0 +3: + faligndata %f6, %f8, %f32 + cmp %o0, %o5 + faligndata %f8, %f10, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f10, %f12, %f36 + faligndata %f12, %f14, %f38 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + faligndata %f14, %f16, %f40 + faligndata %f16, %f18, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f18, %f20, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f20, %f22, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f22, %f24, %f32 + cmp %o0, %o5 + faligndata %f24, %f26, %f34 + faligndata %f26, %f28, %f36 + inc BLOCK_SIZE, %o1 + faligndata %f28, %f30, %f38 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + faligndata %f30, %f48, %f40 + dec BLOCK_SIZE, %o2 + faligndata %f48, %f50, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f50, %f52, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f52, %f54, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f54, %f56, %f32 + cmp %o0, %o5 + faligndata %f56, %f58, %f34 + faligndata %f58, %f60, %f36 + inc BLOCK_SIZE, %o1 + faligndata %f60, %f62, %f38 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + faligndata %f62, %f0, %f40 + dec BLOCK_SIZE, %o2 + faligndata %f0, %f2, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f2, %f4, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f4, %f6, %f46 + + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + + !! + !! Source at BLOCK_ALIGN+40 + !! + !! We need to load 3 doubles by hand. + !! +L105: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L105" + .align 8 +2: +#endif + fmovd %f0, %f8 + ldd [%o0], %f10 + inc 8, %o0 + ldd [%o0], %f12 + inc 8, %o0 + ldd [%o0], %f14 + inc 8, %o0 + + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + inc BLOCK_SIZE, %o0 +3: + faligndata %f8, %f10, %f32 + cmp %o0, %o5 + faligndata %f10, %f12, %f34 + faligndata %f12, %f14, %f36 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + faligndata %f14, %f16, %f38 + dec BLOCK_SIZE, %o2 + faligndata %f16, %f18, %f40 + inc BLOCK_SIZE, %o0 + faligndata %f18, %f20, %f42 + faligndata %f20, %f22, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f22, %f24, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f24, %f26, %f32 + cmp %o0, %o5 + faligndata %f26, %f28, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f28, %f30, %f36 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + faligndata %f30, %f48, %f38 + inc BLOCK_SIZE, %o1 + faligndata %f48, %f50, %f40 + inc BLOCK_SIZE, %o0 + faligndata %f50, %f52, %f42 + faligndata %f52, %f54, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f54, %f56, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f56, %f58, %f32 + cmp %o0, %o5 + faligndata %f58, %f60, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f60, %f62, %f36 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + faligndata %f62, %f0, %f38 + inc BLOCK_SIZE, %o1 + faligndata %f0, %f2, %f40 + inc BLOCK_SIZE, %o0 + faligndata %f2, %f4, %f42 + faligndata %f4, %f6, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f6, %f8, %f46 + + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + + + !! + !! Source at BLOCK_ALIGN+48 + !! + !! We need to load 2 doubles by hand. + !! +L106: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L106" + .align 8 +2: +#endif + fmovd %f0, %f10 + ldd [%o0], %f12 + inc 8, %o0 + ldd [%o0], %f14 + inc 8, %o0 + + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + inc BLOCK_SIZE, %o0 +3: + faligndata %f10, %f12, %f32 + cmp %o0, %o5 + faligndata %f12, %f14, %f34 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + faligndata %f14, %f16, %f36 + dec BLOCK_SIZE, %o2 + faligndata %f16, %f18, %f38 + inc BLOCK_SIZE, %o0 + faligndata %f18, %f20, %f40 + faligndata %f20, %f22, %f42 + faligndata %f22, %f24, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f24, %f26, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f26, %f28, %f32 + cmp %o0, %o5 + faligndata %f28, %f30, %f34 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + faligndata %f30, %f48, %f36 + dec BLOCK_SIZE, %o2 + faligndata %f48, %f50, %f38 + inc BLOCK_SIZE, %o1 + faligndata %f50, %f52, %f40 + faligndata %f52, %f54, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f54, %f56, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f56, %f58, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f58, %f60, %f32 + cmp %o0, %o5 + faligndata %f60, %f62, %f34 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + faligndata %f62, %f0, %f36 + dec BLOCK_SIZE, %o2 + faligndata %f0, %f2, %f38 + inc BLOCK_SIZE, %o1 + faligndata %f2, %f4, %f40 + faligndata %f4, %f6, %f42 + inc BLOCK_SIZE, %o0 + faligndata %f6, %f8, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f8, %f10, %f46 + + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + + + !! + !! Source at BLOCK_ALIGN+56 + !! + !! We need to load 1 double by hand. + !! +L107: +#ifdef RETURN_NAME + sethi %hi(1f), %g1 + ba,pt %icc, 2f + or %g1, %lo(1f), %g1 +1: + .asciz "L107" + .align 8 +2: +#endif + fmovd %f0, %f12 + ldd [%o0], %f14 + inc 8, %o0 + + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + inc BLOCK_SIZE, %o0 +3: + faligndata %f12, %f14, %f32 + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f48 + membar #Sync +2: + faligndata %f14, %f16, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f16, %f18, %f36 + inc BLOCK_SIZE, %o0 + faligndata %f18, %f20, %f38 + faligndata %f20, %f22, %f40 + faligndata %f22, %f24, %f42 + faligndata %f24, %f26, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f26, %f28, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f28, %f30, %f32 + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f0 + membar #Sync +2: + faligndata %f30, %f48, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f48, %f50, %f36 + inc BLOCK_SIZE, %o1 + faligndata %f50, %f52, %f38 + faligndata %f52, %f54, %f40 + inc BLOCK_SIZE, %o0 + faligndata %f54, %f56, %f42 + faligndata %f56, %f58, %f44 + brlez,pn %o2, Lbcopy_blockdone + faligndata %f58, %f60, %f46 + + stda %f32, [%o1] ASI_STORE + + faligndata %f60, %f62, %f32 + cmp %o0, %o5 + bleu,a,pn %icc, 2f + ldda [%o0] ASI_BLK_P, %f16 + membar #Sync +2: + faligndata %f62, %f0, %f34 + dec BLOCK_SIZE, %o2 + faligndata %f0, %f2, %f36 + inc BLOCK_SIZE, %o1 + faligndata %f2, %f4, %f38 + faligndata %f4, %f6, %f40 + inc BLOCK_SIZE, %o0 + faligndata %f6, %f8, %f42 + faligndata %f8, %f10, %f44 + + brlez,pn %o2, Lbcopy_blockdone + faligndata %f10, %f12, %f46 + + stda %f32, [%o1] ASI_STORE + ba 3b + inc BLOCK_SIZE, %o1 + +Lbcopy_blockdone: + inc BLOCK_SIZE, %o2 ! Fixup our overcommit + membar #Sync ! Finish any pending loads +#define FINISH_REG(f) \ + deccc 8, %o2; \ + bl,a Lbcopy_blockfinish; \ + fmovd f, %f48; \ + std f, [%o1]; \ + inc 8, %o1 + + FINISH_REG(%f32) + FINISH_REG(%f34) + FINISH_REG(%f36) + FINISH_REG(%f38) + FINISH_REG(%f40) + FINISH_REG(%f42) + FINISH_REG(%f44) + FINISH_REG(%f46) + FINISH_REG(%f48) +#undef FINISH_REG + !! + !! The low 3 bits have the sub-word bits needed to be + !! stored [because (x-8)&0x7 == x]. + !! +Lbcopy_blockfinish: + brz,pn %o2, 2f ! 100% complete? + fmovd %f48, %f4 + cmp %o2, 8 ! Exactly 8 bytes? + bz,a,pn %xcc, 2f + std %f4, [%o1] + + btst 4, %o2 ! Word store? + bz %xcc, 1f + nop + st %f4, [%o1] + inc 4, %o1 +1: + btst 2, %o2 + fzero %f0 + bz 1f + + mov -6, %o4 + alignaddr %o1, %o4, %g0 + + faligndata %f0, %f4, %f8 + + stda %f8, [%o1] ASI_FL16_P ! Store short + inc 2, %o1 +1: + btst 1, %o2 ! Byte aligned? + bz 2f + + mov -7, %o0 ! Calculate dest - 7 + alignaddr %o1, %o0, %g0 ! Calculate shift mask and dest. + + faligndata %f0, %f4, %f8 ! Move 1st byte to low part of f8 + + stda %f8, [%o1] ASI_FL8_P ! Store 1st byte + inc 1, %o1 ! Update address +2: + membar #Sync +#if 0 + !! + !! verify copy success. + !! + + mov %i0, %o2 + mov %i1, %o4 + mov %i2, %l4 +0: + ldub [%o2], %o1 + inc %o2 + ldub [%o4], %o3 + inc %o4 + cmp %o3, %o1 + bnz 1f + dec %l4 + brnz %l4, 0b + nop + ba 2f + nop + +1: + set block_disable, %o0 + stx %o0, [%o0] + + set 0f, %o0 + call prom_printf + sub %i2, %l4, %o5 + set 1f, %o0 + mov %i0, %o1 + mov %i1, %o2 + call prom_printf + mov %i2, %o3 + ta 1 + .data + _ALIGN +block_disable: .xword 0 +0: .asciz "bcopy failed: %x@%p != %x@%p byte %d\r\n" +1: .asciz "bcopy(%p, %p, %lx)\r\n" + _ALIGN + .text +2: +#endif +#ifdef _KERNEL + + set 1f, %o0 + mov %i0, %o1 + mov %i1, %o2 + call printf + mov %i2, %o3 + + .data + _ALIGN +1: .asciz "block exit (%p, %p, %d)\n" + _ALIGN + .text +/* + * Weve saved our possible fpstate, now disable the fpu + * and continue with life. + */ +#if 1 + RESTORE_FPU +#else +#ifdef DEBUG + LDPTR [%l1 + %lo(FPPROC)], %l7 + cmp %l7, %l5 +! tnz 1 ! fpproc has changed! + LDPTR [%l5 + P_FPSTATE], %l7 + cmp %l7, %l0 + tnz 1 ! fpstate has changed! +#endif + andcc %l2, %l3, %g0 ! If (fpproc && fpstate) + STPTR %l2, [%l1 + %lo(FPPROC)] ! Restore old fproc + bz,pt %xcc, 1f ! Skip if no fpstate + STPTR %l6, [%l5 + P_FPSTATE] ! Restore old fpstate + + call _C_LABEL(loadfpstate) ! Re-load orig fpstate + mov %l3, %o0 +1: +#endif + set 1f, %o0 + mov %i0, %o1 + mov %i1, %o2 + call printf + mov %i2, %o3 + + .data + _ALIGN +1: .asciz "block done (%p, %p, %d)\n" + _ALIGN + .text + + + ret + restore %g1, 0, %o0 ! Return DEST for memcpy +#endif + retl + mov %g1, %o0 +#endif + + diff --git a/lib/nbsd_libc/arch/sparc64/string/memset.S b/lib/nbsd_libc/arch/sparc64/string/memset.S new file mode 100644 index 000000000..ca10dc702 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/string/memset.S @@ -0,0 +1,196 @@ +/* $NetBSD: memset.S,v 1.4 2001/08/02 01:17:28 eeh Exp $ */ + +/* + * Copyright (c) 2001, Eduardo E. Horvath + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * from: Header: bzero.s,v 1.1 92/06/25 12:52:46 torek Exp + */ + +#include +#ifndef _LOCORE +#define _LOCORE +#endif +#include +#include +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: memset.S,v 1.4 2001/08/02 01:17:28 eeh Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * bzero(addr, len) + * + * We want to use VIS instructions if we're clearing out more than + * 256 bytes, but to do that we need to properly save and restore the + * FP registers. Unfortunately the code to do that in the kernel needs + * to keep track of the current owner of the FPU, hence the different + * code. + * + * XXXXX To produce more efficient code, we do not allow lengths + * greater than 0x80000000000000000, which are negative numbers. + * This should not really be an issue since the VA hole should + * cause any such ranges to fail anyway. + */ +ENTRY(bzero) + ! %o0 = addr, %o1 = len + mov %o1, %o2 + clr %o1 ! Initialize our pattern +/* + * memset(addr, c, len) + * + */ +ENTRY(memset) + ! %o0 = addr, %o1 = pattern, %o2 = len + mov %o0, %o4 ! Save original pointer + +Lbzero_internal: + btst 7, %o0 ! Word aligned? + bz,pn %xcc, 0f + nop + inc %o0 + deccc %o2 ! Store up to 7 bytes + bge,a,pt %xcc, Lbzero_internal + stb %o1, [%o0 - 1] + + retl ! Duplicate Lbzero_done + mov %o4, %o0 +0: + /* + * Duplicate the pattern so it fills 64-bits. + */ + andcc %o1, 0x0ff, %o1 ! No need to extend zero + bz,pt %icc, 1f + sllx %o1, 8, %o3 ! sigh. all dependent insns. + or %o1, %o3, %o1 + sllx %o1, 16, %o3 + or %o1, %o3, %o1 + sllx %o1, 32, %o3 + or %o1, %o3, %o1 +1: +#if 1 + !! Now we are 64-bit aligned + cmp %o2, 256 ! Use block clear if len > 256 + bge,pt %xcc, Lbzero_block ! use block store insns +#endif + deccc 8, %o2 +Lbzero_longs: + bl,pn %xcc, Lbzero_cleanup ! Less than 8 bytes left + nop +3: + inc 8, %o0 + deccc 8, %o2 + bge,pt %xcc, 3b + stx %o1, [%o0 - 8] ! Do 1 longword at a time + + /* + * Len is in [-8..-1] where -8 => done, -7 => 1 byte to zero, + * -6 => two bytes, etc. Mop up this remainder, if any. + */ +Lbzero_cleanup: + btst 4, %o2 + bz,pt %xcc, 5f ! if (len & 4) { + nop + stw %o1, [%o0] ! *(int *)addr = 0; + inc 4, %o0 ! addr += 4; +5: + btst 2, %o2 + bz,pt %xcc, 7f ! if (len & 2) { + nop + sth %o1, [%o0] ! *(short *)addr = 0; + inc 2, %o0 ! addr += 2; +7: + btst 1, %o2 + bnz,a %icc, Lbzero_done ! if (len & 1) + stb %o1, [%o0] ! *addr = 0; +Lbzero_done: + retl + mov %o4, %o0 ! Restore ponter for memset (ugh) + +#if 1 +Lbzero_block: +/* + * Userland: + * + * Floating point registers are volatile. What luck. + * + * See locore.s for the kernel version. + * + */ +! wr %g0, FPRS_FEF, %fprs ! Enable FPU + + !! We are now 8-byte aligned. We need to become 64-byte aligned. + btst 63, %o0 + bz,pt %xcc, 2f + nop +1: + stx %o1, [%o0] + inc 8, %o0 + btst 63, %o0 + bnz,pt %xcc, 1b + dec 8, %o2 + +2: + brz %o1, 3f ! Skip the memory op + fzero %f0 ! for bzero + + stx %o1, [%o0] ! Flush this puppy to RAM + membar #StoreLoad + ldd [%o0], %f0 +3: + fmovd %f0, %f2 ! Duplicate the pattern + fmovd %f0, %f4 + fmovd %f0, %f6 + fmovd %f0, %f8 + fmovd %f0, %f10 + fmovd %f0, %f12 + fmovd %f0, %f14 + + !! Remember: we were 8 bytes too far + dec 56, %o2 ! Go one iteration too far +5: + stda %f0, [%o0] ASI_BLK_P ! Store 64 bytes + deccc 64, %o2 + bg,pn %xcc, 5b + inc 64, %o0 + + membar #Sync +/* + * Now we're done we need to load the FPU state from where + * we put it. + */ + ba,pt %xcc, Lbzero_longs ! Finish up the remainder + inccc 56, %o2 ! Restore the count +#endif diff --git a/lib/nbsd_libc/arch/sparc64/sys/__clone.S b/lib/nbsd_libc/arch/sparc64/sys/__clone.S new file mode 100644 index 000000000..3d36d7ded --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/__clone.S @@ -0,0 +1,91 @@ +/* $NetBSD: __clone.S,v 1.7 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + save %sp, -CC64FSZ, %sp + + /* + * Sanity checks: func and stack may not be NULL. + */ + brz,pn %i0,8f ! func == NULL, bail + orcc %i1, %g0, %o1 ! setup stack arg for syscall, test + bz,pn %xcc, 8f ! stack == NULL, bail + mov %i2, %o0 ! setup flags arg for syscall + + /* + * Allocate "caller's" frame in the child stack as ABI + * requires. Subtract BIAS - it will be 64-bit code. + * + * We pass the function and the argument to the child by + * stashing them at the bottom of the frame. There they are + * safe from window spill would we need to take one as it's + * below the window save area. + */ + sub %o1, CC64FSZ+BIAS, %o1 ! make space + stx %i0, [%o1+CC64FSZ-16+BIAS] ! save func + stx %i3, [%o1+CC64FSZ-8+BIAS] ! save arg + + /* + * Args are now set up for system call as (flags, stack). + */ + mov SYS___clone, %g1 + t ST_SYSCALL + bcs,pn %xcc, 9f + tst %o1 ! %o1 (rv[1]) == 0 in parent + bz %xcc, 2f ! yes, parent + ldx [%sp+CC64FSZ-16+BIAS], %l0 ! grab the function... + call %l0 ! Call the clone's entry point. + ldx [%sp+CC64FSZ-8+BIAS], %o0 ! ...and the argument + + JUMP(_exit) + /* NOTREACHED */ + +2: ret + restore %g0, %o0, %o0 + +8: restore %g0, EINVAL, %o0 + ERROR() + /* NOTREACHED */ + +9: restore %o0, %g0, %o0 + ERROR() + /* NOTREACHED */ diff --git a/lib/nbsd_libc/arch/sparc64/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/sparc64/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..62833a674 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/__sigaction14_sigtramp.c @@ -0,0 +1,72 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.9 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.9 2009/01/11 02:46:24 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern void __sigtramp_siginfo_2(void); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the compatibility SIGCONTEXT trampoline if SA_SIGINFO + * is not set in the sigaction. + */ + if (act && (act->sa_flags & SA_SIGINFO) == 0) { + extern void __sigtramp_sigcontext_1(void); + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_1, 1); + /* + * EINVAL might indicate that trampoline version 1 is + * not supported by the kernel; fall back on native + * SIGINFO trampoline. + */ + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/sparc64/sys/__sigtramp2.S b/lib/nbsd_libc/arch/sparc64/sys/__sigtramp2.S new file mode 100644 index 000000000..9f9357194 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/__sigtramp2.S @@ -0,0 +1,55 @@ +/* $NetBSD: __sigtramp2.S,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Paul Kranenburg. + * + * 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 +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __sigtramp2.S,v 1.3 2008/04/28 20:22:57 martin Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#define _LOCORE +#include /* BIAS, CC64FSZ */ + +/* + * When this code is run, the stack looks like: + * [%sp + BIAS] struct frame + * [%sp + BIAS + CC64FSZ] siginfo_t + * [%sp + BIAS + CC64FSZ + 128] ucontext_t + * . + * . + */ +ENTRY_NOPROFILE(__sigtramp_siginfo_2) + add %sp, BIAS + CC64FSZ + 128, %o0 /* get pointer to ucontext */ + mov SYS_setcontext, %g1 + t ST_SYSCALL /* call setcontext */ + mov SYS_exit, %g1 /* exit with errno */ + t ST_SYSCALL /* if sigreturn fails */ diff --git a/lib/nbsd_libc/arch/sparc64/sys/__syscall.S b/lib/nbsd_libc/arch/sparc64/sys/__syscall.S new file mode 100644 index 000000000..5d55ec75e --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/__syscall.S @@ -0,0 +1,6 @@ +/* $NetBSD: __syscall.S,v 1.1 2000/12/13 20:25:04 martin Exp $ */ + +#include +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/sparc64/sys/__vfork14.S b/lib/nbsd_libc/arch/sparc64/sys/__vfork14.S new file mode 100644 index 000000000..1ffd4d619 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/__vfork14.S @@ -0,0 +1,59 @@ +/* $NetBSD: __vfork14.S,v 1.3 2003/08/07 16:42:29 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: Ovfork.s,v 1.1 91/07/06 13:05:56 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)Ovfork.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: __vfork14.S,v 1.3 2003/08/07 16:42:29 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +/* + * pid = vfork(); + * + * %o1 == 0 in parent process, 1 in child process. + * %o0 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +SYSCALL(__vfork14) + dec %o1 /* from 1 to 0 in child, 0 to -1 in parent */ + retl + and %o0, %o1, %o0 /* return 0 in child, pid in parent */ diff --git a/lib/nbsd_libc/arch/sparc64/sys/brk.S b/lib/nbsd_libc/arch/sparc64/sys/brk.S new file mode 100644 index 000000000..7813b5ba0 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/brk.S @@ -0,0 +1,112 @@ +/* $NetBSD: brk.S,v 1.12 2003/12/26 11:21:48 martin Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: brk.s,v 1.3 92/06/25 12:56:05 mccanne Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)brk.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: brk.S,v 1.12 2003/12/26 11:21:48 martin Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(__minbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data + .align 8 +_C_LABEL(__minbrk): + .xword _C_LABEL(_end) /* lower brk limit; also for gmon code */ + .text + +ENTRY(_brk) +#ifdef PIC +#ifdef BIGPIC + PIC_PROLOGUE(%o5,%o4) + set _C_LABEL(__minbrk), %o4 + ldx [%o5 + %o4], %o4 + ldx [%o4], %o1 /* %o1 = minbrk */ + cmp %o1, %o0 /* if (minbrk > %o0) */ + movgu %xcc, %o1, %o0 /* %o0 = minbrk */ + mov %o0, %o2 /* save argument to syscall */ + mov SYS_break, %g1 + t ST_SYSCALL + set _C_LABEL(__curbrk), %o3 + bcc,a,pt %icc, 1f + ldx [%o5 + %o3], %o4 + ERROR() +1: + retl /* success, return 0 & record new break */ + stx %o2, [%o4] +#else + PIC_PROLOGUE(%o5,%o4) + ldx [%o5 + _C_LABEL(__minbrk)], %o4 + ldx [%o4], %o1 /* %o1 = minbrk */ + cmp %o1, %o0 /* if (minbrk > %o0) */ + movgu %xcc, %o1, %o0 /* %o0 = minbrk */ + mov %o0, %o2 /* save argument to syscall */ + mov SYS_break, %g1 + t ST_SYSCALL + bcc,a,pt %icc, 1f + ldx [%o5 + _C_LABEL(__curbrk)], %o4 + ERROR() +1: + retl /* success, return 0 & record new break */ + stx %o2, [%o4] +#endif +#else + sethi %hi(_C_LABEL(__minbrk)), %o1 /* %o1 = minbrk */ + ldx [%o1 + %lo(_C_LABEL(__minbrk))], %o1 + cmp %o1, %o0 /* if (minbrk > %o0) */ + movgu %xcc, %o1, %o0 /* %o0 = minbrk */ + mov %o0, %o2 /* save argument to syscall */ + mov SYS_break, %g1 + t ST_SYSCALL + bcc,a,pt %icc,1f + sethi %hi(_C_LABEL(__curbrk)), %g1 + ERROR() +1: + retl /* success, return 0 & record new break */ + stx %o2, [%g1 + %lo(_C_LABEL(__curbrk))] +#endif diff --git a/lib/nbsd_libc/arch/sparc64/sys/cerror.S b/lib/nbsd_libc/arch/sparc64/sys/cerror.S new file mode 100644 index 000000000..3cd2acf01 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/cerror.S @@ -0,0 +1,87 @@ +/* $NetBSD: cerror.S,v 1.6 2003/08/07 16:42:29 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: cerror.s,v 1.3 92/07/02 04:17:59 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)cerror.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: cerror.S,v 1.6 2003/08/07 16:42:29 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef _REENTRANT +FUNC(__cerror) + save %sp, -CC64FSZ, %sp + call _C_LABEL(__errno) + nop + st %i0, [%o0] + mov -1, %i0 + ret + restore %g0, -1, %o1 +#else + .globl _C_LABEL(errno) +#ifdef PIC +#ifdef BIGPIC +FUNC(__cerror) + PIC_PROLOGUE(%g1, %o3) + set _C_LABEL(errno),%o1 + ldx [%g1 + %o1], %g1 + st %o0, [%g1] + mov -1, %o0 + retl + mov -1, %o1 +#else +FUNC(__cerror) + PIC_PROLOGUE(%g1, %o3) + ldx [%g1 + _C_LABEL(errno)], %g1 + st %o0, [%g1] + mov -1, %o0 + retl + mov -1, %o1 +#endif +#else +FUNC(__cerror) + sethi %hi(_C_LABEL(errno)), %g1 + st %o0, [%g1 + %lo(_C_LABEL(errno))] + mov -1, %o0 + retl + mov -1, %o1 +#endif +#endif /* _REENTRANT */ diff --git a/lib/nbsd_libc/arch/sparc64/sys/exect.S b/lib/nbsd_libc/arch/sparc64/sys/exect.S new file mode 100644 index 000000000..5c886989c --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/exect.S @@ -0,0 +1,52 @@ +/* $NetBSD: exect.S,v 1.2 2003/08/07 16:42:29 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: exect.s,v 1.1 91/07/06 13:05:57 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)exect.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: exect.S,v 1.2 2003/08/07 16:42:29 agc Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(exect) + mov SYS_execve, %g1 /* execve(file, argv, env) */ + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/arch/sparc64/sys/fork.S b/lib/nbsd_libc/arch/sparc64/sys/fork.S new file mode 100644 index 000000000..e83affa83 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/fork.S @@ -0,0 +1,52 @@ +/* $NetBSD: fork.S,v 1.5 2003/08/07 16:42:30 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: fork.s,v 1.1 91/07/06 13:05:58 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)fork.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: fork.S,v 1.5 2003/08/07 16:42:30 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +_SYSCALL(__fork,fork) + dec %o1 /* from 1 to 0 in child, 0 to -1 in parent */ + retl + and %o0, %o1, %o0 /* return 0 in child, pid in parent */ diff --git a/lib/nbsd_libc/arch/sparc64/sys/getcontext.S b/lib/nbsd_libc/arch/sparc64/sys/getcontext.S new file mode 100644 index 000000000..29f0c840a --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/getcontext.S @@ -0,0 +1,54 @@ +/* $NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $") +#endif /* SYSLIBC_SCCS && !lint */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +ENTRY(_getcontext) + mov %o0, %o2 /* must save pointer */ + mov SYS_getcontext, %g1 + t ST_SYSCALL + bcc,a 1f + stx %g0, [%o2 + 64 + 11 * 8] /* gr[_REG_O0] = 0 */ + ERROR() +1: + add %o7, 8, %o1 + stx %o1, [%o2 + 64 + 1 * 8] /* gr[_REG_PC] = retaddr */ + add %o7, 12, %o1 + retl + stx %o1, [%o2 + 64 + 2 * 8] /* gr[_REG_nPC] = retaddr + 4 */ diff --git a/lib/nbsd_libc/arch/sparc64/sys/pipe.S b/lib/nbsd_libc/arch/sparc64/sys/pipe.S new file mode 100644 index 000000000..c0c6ecfc9 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/pipe.S @@ -0,0 +1,63 @@ +/* $NetBSD: pipe.S,v 1.4 2003/08/07 16:42:30 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: pipe.s,v 1.1 91/07/06 13:05:58 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)pipe.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: pipe.S,v 1.4 2003/08/07 16:42:30 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +ENTRY(_pipe) + mov %o0, %o2 /* save pointer */ + mov SYS_pipe, %g1 + t ST_SYSCALL /* pipe() */ + bcc,a 1f + st %o0, [%o2] /* success, store fds */ + ERROR() +1: + st %o1, [%o2 + 4] + retl /* and return 0 */ + clr %o0 diff --git a/lib/nbsd_libc/arch/sparc64/sys/ptrace.S b/lib/nbsd_libc/arch/sparc64/sys/ptrace.S new file mode 100644 index 000000000..ecf2c9cb3 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/ptrace.S @@ -0,0 +1,62 @@ +/* $NetBSD: ptrace.S,v 1.5 2003/08/07 16:42:30 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: ptrace.s,v 1.2 91/12/20 01:59:00 leres Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)ptrace.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: ptrace.S,v 1.5 2003/08/07 16:42:30 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(ptrace) + save %sp, -CC64FSZ, %sp + call _C_LABEL(__errno) + nop + st %g0, [%o0] + restore + mov SYS_ptrace, %g1 + t ST_SYSCALL + bcc 1f + nop + ERROR() +1: + retl + nop diff --git a/lib/nbsd_libc/arch/sparc64/sys/sbrk.S b/lib/nbsd_libc/arch/sparc64/sys/sbrk.S new file mode 100644 index 000000000..e420e5eff --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/sbrk.S @@ -0,0 +1,107 @@ +/* $NetBSD: sbrk.S,v 1.9 2003/08/07 16:42:30 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sbrk.s,v 1.3 92/07/02 00:56:49 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sbrk.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: sbrk.S,v 1.9 2003/08/07 16:42:30 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _C_LABEL(__curbrk) + .globl _C_LABEL(_end) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data + .align 8 +_C_LABEL(__curbrk): + .xword _C_LABEL(_end) + .text + +ENTRY(_sbrk) +#ifdef PIC +#ifdef BIGPIC + PIC_PROLOGUE(%o5,%o4) + set _C_LABEL(__curbrk), %o3 + ldx [%o5 + %o3], %o2 + ldx [%o2], %o3 /* %o3 = old break */ + add %o3, %o0, %o4 /* %o4 = new break */ + mov %o4, %o0 /* copy for syscall */ + mov SYS_break, %g1 + t ST_SYSCALL /* break(new_break) */ + bcc,a 1f /* if success, */ + mov %o3, %o0 /* set return value */ + ERROR() +1: + retl /* and update curbrk */ + stx %o4, [%o2] +#else + PIC_PROLOGUE(%o5,%o4) + ldx [%o5 + _C_LABEL(__curbrk)], %o2 + ldx [%o2], %o3 /* %o3 = old break */ + add %o3, %o0, %o4 /* %o4 = new break */ + mov %o4, %o0 /* copy for syscall */ + mov SYS_break, %g1 + t ST_SYSCALL /* break(new_break) */ + bcc,a 1f /* if success, */ + mov %o3, %o0 /* set return value */ + ERROR() +1: + retl /* and update curbrk */ + stx %o4, [%o2] +#endif +#else + sethi %hi(_C_LABEL(__curbrk)), %o2 + ldx [%o2 + %lo(_C_LABEL(__curbrk))], %o3 /* %o3 = old break */ + add %o3, %o0, %o4 /* %o4 = new break */ + mov %o4, %o0 /* copy for syscall */ + mov SYS_break, %g1 + t ST_SYSCALL /* break(new_break) */ + bcc,a 1f /* if success, */ + mov %o3, %o0 /* set return value */ + ERROR() +1: + retl /* and update curbrk */ + stx %o4, [%o2 + %lo(_C_LABEL(__curbrk))] +#endif diff --git a/lib/nbsd_libc/arch/sparc64/sys/shmat.S b/lib/nbsd_libc/arch/sparc64/sys/shmat.S new file mode 100644 index 000000000..216dbd807 --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:55 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/sparc64/sys/syscall.S b/lib/nbsd_libc/arch/sparc64/sys/syscall.S new file mode 100644 index 000000000..f49bf2dbf --- /dev/null +++ b/lib/nbsd_libc/arch/sparc64/sys/syscall.S @@ -0,0 +1,49 @@ +/* $NetBSD: syscall.S,v 1.2 2003/08/07 16:42:30 agc Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: syscall.s,v 1.1 91/07/06 13:06:02 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)syscall.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: syscall.S,v 1.2 2003/08/07 16:42:30 agc Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/arch/vax/DEFS.h b/lib/nbsd_libc/arch/vax/DEFS.h new file mode 100644 index 000000000..9e2619748 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/DEFS.h @@ -0,0 +1,4 @@ +/* $NetBSD: DEFS.h,v 1.3 1996/01/06 18:34:59 ragge Exp $ */ + +#include + diff --git a/lib/nbsd_libc/arch/vax/Makefile.inc b/lib/nbsd_libc/arch/vax/Makefile.inc new file mode 100644 index 000000000..592be933b --- /dev/null +++ b/lib/nbsd_libc/arch/vax/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.9 2010/07/06 05:59:52 mrg Exp $ + +SRCS+= __longjmp14.c +SRCS+= __sigaction14_sigtramp.c __sigtramp3.S + +CPPFLAGS+= -I. +CPPFLAGS.assym.h+=-D__LIBC12_SOURCE__ diff --git a/lib/nbsd_libc/arch/vax/SYS.h b/lib/nbsd_libc/arch/vax/SYS.h new file mode 100644 index 000000000..7bc45135e --- /dev/null +++ b/lib/nbsd_libc/arch/vax/SYS.h @@ -0,0 +1,90 @@ +/* $NetBSD: SYS.h,v 1.10 2003/08/07 16:42:30 agc Exp $ */ +/* + * Copyright (c) 1983, 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. + * + * @(#)SYS.h 8.1 (Berkeley) 6/4/93 + */ + +#include +#include + +#ifdef __STDC__ +#define SYSTRAP(x) chmk $ SYS_ ## x +#else +#define SYSTRAP(x) chmk $ SYS_/**/x +#endif + +#define _SYSCALL_NOERROR(x,y) \ + ENTRY(x,0); \ + SYSTRAP(y) + +#define _SYSCALL(x,y) \ + err: nop; nop; jmp CERROR+2; \ + _SYSCALL_NOERROR(x,y); \ + jcs err+2 + +#define SYSCALL_NOERROR(x) \ + _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) \ + _SYSCALL(x,x) + +#define PSEUDO_NOERROR(x,y) \ + _SYSCALL_NOERROR(x,y); \ + ret + +#define PSEUDO(x,y) \ + _SYSCALL(x,y); \ + ret + +#define RSYSCALL_NOERROR(x) \ + PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) \ + PSEUDO(x,x) + +#ifdef WEAK_ALIAS +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) +#else +#define WSYSCALL(weak,strong) \ + PSEUDO(weak,weak) +#endif + +#define ASMSTR .asciz + +#ifdef __ELF__ +#define CERROR _C_LABEL(__cerror) +#define CURBRK _C_LABEL(__curbrk) +#else +#define CERROR _ASM_LABEL(cerror) +#define CURBRK _ASM_LABEL(curbrk) +#endif + + .globl CERROR diff --git a/lib/nbsd_libc/arch/vax/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/vax/gdtoa/Makefile.inc new file mode 100644 index 000000000..bae804d5d --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gdtoa/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof_vaxf.c diff --git a/lib/nbsd_libc/arch/vax/gdtoa/arith.h b/lib/nbsd_libc/arch/vax/gdtoa/arith.h new file mode 100644 index 000000000..66e57a0b8 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gdtoa/arith.h @@ -0,0 +1,4 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define VAX +#define NO_HEX_FP /* XXX */ diff --git a/lib/nbsd_libc/arch/vax/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/vax/gdtoa/gd_qnan.h new file mode 100644 index 000000000..86a5f8de9 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gdtoa/gd_qnan.h @@ -0,0 +1,3 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +/* No NaN in VAX FP. */ diff --git a/lib/nbsd_libc/arch/vax/gen/Makefile.inc b/lib/nbsd_libc/arch/vax/gen/Makefile.inc new file mode 100644 index 000000000..224cb0ff2 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/Makefile.inc @@ -0,0 +1,22 @@ +# $NetBSD: Makefile.inc,v 1.17 2010/07/06 05:59:52 mrg Exp $ + +SRCS+= byte_swap_2.S byte_swap_4.S bswap64.S \ + fabs.S frexp.c \ + fpclassifyf.c fpclassifyd.c \ + infinityf.c infinity.c infinityl.c \ + isfinitef.c isfinited.c \ + ldexp.S \ + makecontext.c modf.S \ + resumecontext.c \ + signbitf.c signbitd.c \ + swapcontext.S \ + udiv.S urem.S \ + __setjmp14.S __sigsetjmp14.S _lwp.c _setjmp.S + +# ABI compatibility for libc.so.12 +SRCS+= isinf.c isnan.c + +LSRCS.vax.gen= Lint_bswap16.c Lint_bswap32.c Lint_bswap64.c +LSRCS+= ${LSRCS.vax.gen} +DPSRCS+= ${LSRCS.vax.gen} +CLEANFILES+= ${LSRCS.vax.gen} diff --git a/lib/nbsd_libc/arch/vax/gen/__longjmp14.c b/lib/nbsd_libc/arch/vax/gen/__longjmp14.c new file mode 100644 index 000000000..c5d1aafbe --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/__longjmp14.c @@ -0,0 +1,94 @@ +/* $NetBSD: __longjmp14.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christian Limpach and Matt Thomas. + * + * 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 "namespace.h" +#include +#include +#include +#include +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include + +struct _jmp_buf { + struct sigcontext jb_sc; + register_t jb_regs[6]; +}; + +void +__longjmp14(jmp_buf env, int val) +{ + struct _jmp_buf *jb = (void *)env; + ucontext_t uc; + + /* Ensure non-zero SP */ + if (jb->jb_sc.sc_sp == 0) + goto err; + + /* Ensure non-zero return value */ + if (val == 0) + val = -1; + + /* Set _UC_SIGMASK and _UC_CPU */ + uc.uc_flags = _UC_SIGMASK | _UC_CPU; + + /* Clear uc_link */ + uc.uc_link = 0; + + /* Save return value in context */ + uc.uc_mcontext.__gregs[_REG_R0] = val; + + /* Copy saved registers */ + uc.uc_mcontext.__gregs[_REG_AP] = jb->jb_sc.sc_ap; + uc.uc_mcontext.__gregs[_REG_SP] = jb->jb_sc.sc_sp; + uc.uc_mcontext.__gregs[_REG_FP] = jb->jb_sc.sc_fp; + uc.uc_mcontext.__gregs[_REG_PC] = jb->jb_sc.sc_pc; + uc.uc_mcontext.__gregs[_REG_PSL] = jb->jb_sc.sc_ps; + + uc.uc_mcontext.__gregs[_REG_R6] = jb->jb_regs[0]; + uc.uc_mcontext.__gregs[_REG_R7] = jb->jb_regs[1]; + uc.uc_mcontext.__gregs[_REG_R8] = jb->jb_regs[2]; + uc.uc_mcontext.__gregs[_REG_R9] = jb->jb_regs[3]; + uc.uc_mcontext.__gregs[_REG_R10] = jb->jb_regs[4]; + uc.uc_mcontext.__gregs[_REG_R11] = jb->jb_regs[5]; + + /* Copy signal mask */ + uc.uc_sigmask = jb->jb_sc.sc_mask; + + setcontext(&uc); + err: + longjmperror(); + abort(); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/vax/gen/__setjmp14.S b/lib/nbsd_libc/arch/vax/gen/__setjmp14.S new file mode 100644 index 000000000..69726b169 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/__setjmp14.S @@ -0,0 +1,115 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)setjmp.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: __setjmp14.S,v 1.10 2005/05/03 04:37:33 matt Exp $" +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +#include "DEFS.h" +#include "assym.h" + +ENTRY(__setjmp14, R6) + movl 4(%ap),%r2 # construct sigcontext + subl2 $12,%sp # space for current struct sigstack + pushl %sp # get current values + pushl $0 # no new values + calls $4,_C_LABEL(__sigaltstack14) # pop args plus signal stack value + movl 4(%ap),%r2 # construct sigcontext + movl (%sp)+,(%r2) # save onsigstack status of caller + pushal 28(%r2) + pushl $0 + pushl $0 + calls $3,_C_LABEL(__sigprocmask14) # get signal mask + addl3 $8,4(%ap),%r2 # point to sp in signal context + clrl %r0 # assume no stack arguments + bbc $13,6(%fp),1f # handle callg + addl3 $1,(%ap),%r0 # get argument count +1 if calls +1: moval 24(%fp)[%r0],(%r2)+ # save sp of caller + movl 12(%fp),(%r2)+ # save frame pointer of caller + movl 8(%fp),(%r2)+ # save argument pointer of caller + movl 16(%fp),(%r2)+ # save pc of caller + movpsl (%r2) # save current psl + movw 4(%fp),(%r2) # save psw of caller +#ifdef __ELF__ + addl3 $SC_LEN,4(%ap),%r2 # point to past signal context + movq %r6,(%r2)+ # save r6/r7 + movq %r8,(%r2)+ # save r8/r9 + movq %r10,(%r2)+ # save r10/r11 +#endif + clrl %r0 + ret + +#ifndef __ELF__ +ENTRY(__longjmp14, 0) + movl 8(%ap),%r0 # return(v) + movl 4(%ap),%r1 # fetch buffer + tstl 12(%r1) # is fp non-null? + beql botch + +#ifdef __ELF__ + moval $SC_LEN(%r1),%r2 # get ptr to saved registers + movq (%r2)+,%r6 # restore r6/r7 + movq (%r2)+,%r8 # restore r8/r9 + movq (%r2)+,%r10 # restore r10/r11 +#else +loop: + cmpl 12(%r1),%fp # are we there yet? + beql done + blssu botch + moval 20(%fp),%r2 + blbc 6(%fp),1f # was %r0 saved? + movl %r0,(%r2)+ +1: + bbc $1,6(%fp),2f # was %r1 saved? + movl %r1,(%r2) +2: + movab loop,16(%fp) + ret # pop another frame + +done: +#endif /* !__ELF__ */ + pushl %r1 # pointer to sigcontext + calls $1,_C_LABEL(__sigreturn14) # restore previous context + # we should never return +botch: + calls $0,_C_LABEL(longjmperror) + halt +#endif diff --git a/lib/nbsd_libc/arch/vax/gen/__sigsetjmp14.S b/lib/nbsd_libc/arch/vax/gen/__sigsetjmp14.S new file mode 100644 index 000000000..b5ada4642 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/__sigsetjmp14.S @@ -0,0 +1,64 @@ +/* + * Copyright (c) 1983, 1993, 1995 + * 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "$NetBSD: __sigsetjmp14.S,v 1.4 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a, savemask) + * by restoring registers from the stack, + * and dependent on savemask restores the + * signal mask. + */ + +#include "DEFS.h" +#include + +ENTRY(__sigsetjmp14, R6) + movl 4(%ap),%r0 # get env pointer + movl 8(%ap),(_JBLEN*4)(%r0) # save "savemask" + tstl 8(%ap) # do saving of signal mask? + beql L1 + jmp _C_LABEL(__setjmp14)+2 # yep, do full setjmp +L1: jmp _C_LABEL(_setjmp)+2 # nope, skip to _setjmp + +ENTRY(__siglongjmp14, 0) + movl 4(%ap),%r0 # get env pointer + tstl (_JBLEN*4)(%r0) # test if "savemask" was set + beql L2 + jmp _C_LABEL(__longjmp14)+2 # yep, do full longjmp +L2: jmp _C_LABEL(_longjmp)+2 # nope, skip to _longjmp + + diff --git a/lib/nbsd_libc/arch/vax/gen/_lwp.c b/lib/nbsd_libc/arch/vax/gen/_lwp.c new file mode 100644 index 000000000..d48b6cd0c --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/_lwp.c @@ -0,0 +1,79 @@ +/* $NetBSD: _lwp.c,v 1.1 2009/06/03 01:02:28 christos Exp $ */ + +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.1 2009/06/03 01:02:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + __greg_t *gr = u->uc_mcontext.__gregs; + int *sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + /* Align to a word */ + /* LINTED uintptr_t is safe */ + sp = (int *)((uintptr_t)(stack_base + stack_size) & ~0x3); + + /* + * Allocate necessary stack space for arguments including arg count + * and call frame + */ + sp -= 1 + 1 + 5; + + sp[0] = 0; /* condition handler is null */ + sp[1] = 0x20000000; /* make this a CALLS frame */ + sp[2] = 0; /* saved argument pointer */ + sp[3] = 0; /* saved frame pointer */ + sp[4] = (intptr_t)_lwp_exit + 2;/* return via _lwp_exit */ + sp[5] = 1; /* argc */ + sp[6] = (intptr_t)arg; /* argv */ + + gr[_REG_AP] = (__greg_t)(sp + 5); + gr[_REG_SP] = (__greg_t)sp; + gr[_REG_FP] = (__greg_t)sp; + gr[_REG_PC] = (__greg_t)start + 2; +} diff --git a/lib/nbsd_libc/arch/vax/gen/_setjmp.S b/lib/nbsd_libc/arch/vax/gen/_setjmp.S new file mode 100644 index 000000000..4cd0fca4a --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/_setjmp.S @@ -0,0 +1,121 @@ +/* + * Copyright (c) 1980, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)_setjmp.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: _setjmp.S,v 1.9 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * _setjmp(a) + * by restoring registers from the stack, + * The previous signal state is NOT restored. + * + * Even though we don't use sigreturn14, we still store things in a sigcontext + * in order to be consistent. + */ + +#include "DEFS.h" + +ENTRY(_setjmp, R6) + movl 4(%ap),%r0 + movl 12(%fp),12(%r0) # save frame pointer of caller + movl 16(%fp),20(%r0) # save pc of caller +#ifdef __ELF__ + movl 8(%fp),16(%r0) # save ap of caller + clrl %r1 # clear arg count + bbc $13,6(%fp),1f # was this a callg? + addl3 $1,(%ap),%r1 # get real arg count+1 for calls +1: moval 24(%fp)[%r1],8(%r0) # save sp of caller + movpsl 24(%r0) # save current psl + movw 4(%fp),24(%r0) # save psw of caller + movq %r6,44(%r0) # save r6/r7 + movq %r8,52(%r0) # save r8/r9 + movq %r10,60(%r0) # save r10/r11 +#endif + clrl %r0 + ret + +ENTRY(_longjmp, 0) + movl 8(%ap),%r0 # return(v) + movl 4(%ap),%r1 # fetch buffer + tstl 12(%r1) # is fp null + beql botch +#ifdef __ELF__ + movq 44(%r1),%r6 # restore r6/r7 + movq 52(%r1),%r8 # restore r8/r9 + movq 60(%r1),%r10 # restore r10/r11 + movl 16(%r1),%ap # restore ap + movl 8(%r1),%sp # restore sp + movl 12(%r1),%fp # restore fp + movq 20(%r1),-(%sp) # save pc/psl to new stack + rei # and go back to saved pc/psl +#else +loop: + bitw $1,6(%fp) # %r0 saved? + beql 1f + movl %r0,20(%fp) + bitw $2,6(%fp) # was %r1 saved? + beql 2f + movl %r1,24(%fp) + brb 2f +1: + bitw $2,6(%fp) # was %r1 saved? + beql 2f + movl %r1,20(%fp) +2: + cmpl 12(%r1),12(%fp) + beql done + blssu botch + movab loop,16(%fp) + ret # pop another frame + +done: + cmpb *16(%fp),$2 # returning to an "rei"? + bneq 1f + movab 3f,16(%fp) # do return w/ psl-pc pop + brw 2f +1: + movab 4f,16(%fp) # do standard return +2: + ret # unwind stack before signals enabled +3: + addl2 $8,%sp # compensate for PSL-PC push +4: + jmp *20(%r1) # done, return.... +#endif /* !__ELF__ */ + +botch: + calls $0,_C_LABEL(longjmperror) + halt diff --git a/lib/nbsd_libc/arch/vax/gen/alloca.S b/lib/nbsd_libc/arch/vax/gen/alloca.S new file mode 100644 index 000000000..d70bf2465 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/alloca.S @@ -0,0 +1,46 @@ +/*- + * Copyright (c) 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)alloca.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: alloca.S,v 1.3 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +#include "DEFS.h" + +ENTRY(alloca, 0) + movl 4(%ap),%r0 # get allocation size + movl 16(%fp),%r2 # save return address before we smash it + movab here,16(%fp) + ret +here: + subl2 %r0,%sp # create stack space + bicl2 $3,%sp # align to longword boundary + movl %sp,%r0 + jmp (%r2) diff --git a/lib/nbsd_libc/arch/vax/gen/byte_swap_2.S b/lib/nbsd_libc/arch/vax/gen/byte_swap_2.S new file mode 100644 index 000000000..6a94404fd --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/byte_swap_2.S @@ -0,0 +1,43 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)htons.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: byte_swap_2.S,v 1.3 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +#include "DEFS.h" + +ALTENTRY(ntohs) +ALTENTRY(htons) +ENTRY(__bswap16, 0) + rotl $8,4(%ap),%r0 + movb 5(%ap),%r0 + movzwl %r0,%r0 + ret diff --git a/lib/nbsd_libc/arch/vax/gen/byte_swap_4.S b/lib/nbsd_libc/arch/vax/gen/byte_swap_4.S new file mode 100644 index 000000000..13cb0519c --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/byte_swap_4.S @@ -0,0 +1,43 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)htonl.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: byte_swap_4.S,v 1.4 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +#include "DEFS.h" + +ALTENTRY(ntohl) +ALTENTRY(htonl) +ENTRY(__bswap32, 0) + rotl $-8,4(%ap),%r0 + insv %r0,$16,$8,%r0 + movb 7(%ap),%r0 + ret diff --git a/lib/nbsd_libc/arch/vax/gen/fabs.S b/lib/nbsd_libc/arch/vax/gen/fabs.S new file mode 100644 index 000000000..a31bb9b75 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/fabs.S @@ -0,0 +1,44 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)fabs.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: fabs.S,v 1.3 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +/* fabs - floating absolute value */ + +#include "DEFS.h" + +ENTRY(fabs, 0) + movd 4(%ap),%r0 + bgeq 1f + mnegd %r0,%r0 +1: + ret diff --git a/lib/nbsd_libc/arch/vax/gen/fpclassifyd.c b/lib/nbsd_libc/arch/vax/gen/fpclassifyd.c new file mode 100644 index 000000000..d7814de6f --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/fpclassifyd.c @@ -0,0 +1,69 @@ +/* $NetBSD: fpclassifyd.c,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2004 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyd.c,v 1.3 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * VAX D_floating version + * + * Implementation notes: + * Reserved operand -> FP_ROP + * True zero -> FP_ZERO + * Dirty zero -> FP_DIRTY_ZERO + * Finite -> FP_NORMAL + */ +int +__fpclassifyd(double x) +{ + union vax_dfloating_u u; + + u.dfltu_d = x; + + if (u.dfltu_dflt.dflt_exp == 0) { + if (u.dfltu_dflt.dflt_sign != 0) + return FP_ROP; + else if (u.dfltu_dflt.dflt_frach != 0 || + u.dfltu_dflt.dflt_fracm != 0 || + u.dfltu_dflt.dflt_fracl != 0) + return FP_DIRTYZERO; + else + return FP_ZERO; + } else { + return FP_NORMAL; + } +} diff --git a/lib/nbsd_libc/arch/vax/gen/fpclassifyf.c b/lib/nbsd_libc/arch/vax/gen/fpclassifyf.c new file mode 100644 index 000000000..20145ade3 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/fpclassifyf.c @@ -0,0 +1,68 @@ +/* $NetBSD: fpclassifyf.c,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2004 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyf.c,v 1.3 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * VAX F_floating version + * + * Implementation notes: + * Reserved operand -> FP_ROP + * True zero -> FP_ZERO + * Dirty zero -> FP_DIRTY_ZERO + * Finite -> FP_NORMAL + */ +int +__fpclassifyf(float x) +{ + union vax_ffloating_u u; + + u.ffltu_f = x; + + if (u.ffltu_fflt.fflt_exp == 0) { + if (u.ffltu_fflt.fflt_sign != 0) + return FP_ROP; + else if (u.ffltu_fflt.fflt_frach != 0 || + u.ffltu_fflt.fflt_fracl != 0) + return FP_DIRTYZERO; + else + return FP_ZERO; + } else { + return FP_NORMAL; + } +} diff --git a/lib/nbsd_libc/arch/vax/gen/frexp.c b/lib/nbsd_libc/arch/vax/gen/frexp.c new file mode 100644 index 000000000..66db2499b --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/frexp.c @@ -0,0 +1,69 @@ +/* $NetBSD: frexp.c,v 1.4 2003/08/07 16:42:31 agc Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)frexp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: frexp.c,v 1.4 2003/08/07 16:42:31 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +double +frexp(value, eptr) + double value; + int *eptr; +{ + union { + double v; + struct { + u_int u_mant1 : 7; + u_int u_exp : 8; + u_int u_sign : 1; + u_int u_mant2 : 16; + u_int u_mant3 : 32; + } s; + } u; + + if (value) { + u.v = value; + *eptr = u.s.u_exp - 128; + u.s.u_exp = 128; + return (u.v); + } else { + *eptr = 0; + return (0.0); + } +} diff --git a/lib/nbsd_libc/arch/vax/gen/infinity.c b/lib/nbsd_libc/arch/vax/gen/infinity.c new file mode 100644 index 000000000..fb0d41fab --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/infinity.c @@ -0,0 +1,17 @@ +/* $NetBSD: infinity.c,v 1.8 2002/02/19 21:50:01 thorpej Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinity.c,v 1.8 2002/02/19 21:50:01 thorpej Exp $"); +#endif /* LIBC_SCCS and not lint */ +/* + * XXX - This is not correct, but what can we do about it??? + */ + +/* infinity.c */ + +#include + +/* The highest D float on a vax. */ +const union __double_u __infinity = + { { 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; diff --git a/lib/nbsd_libc/arch/vax/gen/infinityf.c b/lib/nbsd_libc/arch/vax/gen/infinityf.c new file mode 100644 index 000000000..49a2704e4 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/infinityf.c @@ -0,0 +1,11 @@ +/* $NetBSD: infinityf.c,v 1.1 2003/10/25 22:31:20 kleink Exp $ */ + +/* + * infinityf.c - max. value representable in VAX F_floating -- public domain. + * This is _not_ infinity. + */ + +#include + +const union __float_u __infinityf = + { { 0xff, 0x7f, 0xff, 0xff } }; diff --git a/lib/nbsd_libc/arch/vax/gen/infinityl.c b/lib/nbsd_libc/arch/vax/gen/infinityl.c new file mode 100644 index 000000000..8c613b94a --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/infinityl.c @@ -0,0 +1,16 @@ +/* $NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * infinityl.c - max. value representable in VAX D_floating -- public domain. + * This is _not_ infinity. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include + +const union __long_double_u __infinityl = + { { 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; diff --git a/lib/nbsd_libc/arch/vax/gen/isfinited.c b/lib/nbsd_libc/arch/vax/gen/isfinited.c new file mode 100644 index 000000000..778cf81ca --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/isfinited.c @@ -0,0 +1,59 @@ +/* $NetBSD: isfinited.c,v 1.5 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinited.c,v 1.5 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * VAX D_floating version + */ +int +__isfinited(double x) +{ + union vax_dfloating_u u; + + u.dfltu_d = x; + + if (u.dfltu_dflt.dflt_exp == 0 && + ((u.dfltu_dflt.dflt_frach != 0 || + u.dfltu_dflt.dflt_fracm != 0 || + u.dfltu_dflt.dflt_fracl != 0) || + (u.dfltu_dflt.dflt_sign != 0))) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/arch/vax/gen/isfinitef.c b/lib/nbsd_libc/arch/vax/gen/isfinitef.c new file mode 100644 index 000000000..3bdfc2227 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/isfinitef.c @@ -0,0 +1,58 @@ +/* $NetBSD: isfinitef.c,v 1.4 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinitef.c,v 1.4 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * VAX F_floating version + */ +int +__isfinitef(float x) +{ + union vax_ffloating_u u; + + u.ffltu_f = x; + + if (u.ffltu_fflt.fflt_exp == 0 && + ((u.ffltu_fflt.fflt_frach != 0 || + u.ffltu_fflt.fflt_fracl != 0) || + (u.ffltu_fflt.fflt_sign != 0))) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/arch/vax/gen/isinf.c b/lib/nbsd_libc/arch/vax/gen/isinf.c new file mode 100644 index 000000000..8478a4cb0 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/isinf.c @@ -0,0 +1,51 @@ +/* $NetBSD: isinf.c,v 1.8 2004/03/04 23:42:39 kleink Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinf.c,v 1.8 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include + +#undef isinf +int isinf __P((double)); + +/* ARGSUSED */ +int +isinf(double d) +{ + return (0); +} diff --git a/lib/nbsd_libc/arch/vax/gen/isnan.c b/lib/nbsd_libc/arch/vax/gen/isnan.c new file mode 100644 index 000000000..d46615ef3 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/isnan.c @@ -0,0 +1,52 @@ +/* $NetBSD: isnan.c,v 1.3 2004/03/04 23:42:39 kleink Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnan.c,v 1.3 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#undef isnan +int isnan __P((double)); + +/* ARGSUSED */ +int +isnan(double d) +{ + return (0); +} diff --git a/lib/nbsd_libc/arch/vax/gen/ldexp.S b/lib/nbsd_libc/arch/vax/gen/ldexp.S new file mode 100644 index 000000000..ef36ccf08 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/ldexp.S @@ -0,0 +1,93 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /*.asciz "@(#)ldexp.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: ldexp.S,v 1.6 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +/* + * double ldexp (value, exp) + * double value; + * int exp; + * + * Ldexp returns value*2**exp, if that result is in range. + * If underflow occurs, it returns zero. If overflow occurs, + * it returns a value of appropriate sign and largest + * possible magnitude. In case of either overflow or underflow, + * errno is set to ERANGE. Note that errno is not modified if + * no error occurs. + */ + +#include "DEFS.h" + +/* + * don't include errno.h, ANSI C says it defines errno. + * + * #include + */ +#define ERANGE 34 + + .globl _C_LABEL(errno) + +ENTRY(ldexp, 0) + movd 4(%ap),%r0 /* fetch "value" */ + extzv $7,$8,%r0,%r2 /* %r2 := biased exponent */ + jeql 1f /* if zero, done */ + + addl2 12(%ap),%r2 /* %r2 := new biased exponent */ + jleq 2f /* if <= 0, underflow */ + cmpl %r2,$256 /* otherwise check if too big */ + jgeq 3f /* jump if overflow */ + insv %r2,$7,$8,%r0 /* put exponent back in result */ +1: + ret +2: + clrd %r0 + jbr 1f +3: + movd huge,%r0 /* largest possible floating magnitude */ + jbc $15,4(%ap),1f /* jump if argument was positive */ + mnegd %r0,%r0 /* if arg < 0, make result negative */ +1: +#ifdef _REENTRANT + pushl %r0 + calls $0,_C_LABEL(__errno) + movl $ ERANGE,(%r0) + movl (%sp)+,%r0 +#else + movl $ ERANGE,_C_LABEL(errno) +#endif + ret + + .data +huge: .word 0x7fff /* the largest number that can */ + .word 0xffff /* be represented in a long floating */ + .word 0xffff /* number. This is given in hex in order */ + .word 0xffff /* to avoid floating conversions */ diff --git a/lib/nbsd_libc/arch/vax/gen/makecontext.c b/lib/nbsd_libc/arch/vax/gen/makecontext.c new file mode 100644 index 000000000..87ee3bf90 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/makecontext.c @@ -0,0 +1,79 @@ +/* $NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + va_list ap; + int *sp; + int i; + + /* Compute and align stack pointer. */ + sp = (int *) + (((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) & ~0x3); + + /* + * Allocate necessary stack space for arguments including arg count + * and call frame + */ + sp -= argc + 1 + 5; + + va_start(ap, argc); + sp[5] = argc; + for (i = 1; argc > 0; argc--, i++) + sp[5 + i] = va_arg(ap, int); + va_end(ap); + + sp[0] = 0; /* condition handler is null */ + sp[1] = 0x20000000; /* make this a CALLS frame */ + sp[2] = 0; /* saved argument pointer */ + sp[3] = 0; /* saved frame pointer */ + sp[4] = (int)_resumecontext+2; /* return via trampoline code */ + + gr[_REG_AP] = (__greg_t)(sp + 5); + gr[_REG_SP] = (__greg_t)sp; + gr[_REG_FP] = (__greg_t)sp; + gr[_REG_PC] = (__greg_t)func+2; + +} diff --git a/lib/nbsd_libc/arch/vax/gen/modf.S b/lib/nbsd_libc/arch/vax/gen/modf.S new file mode 100644 index 000000000..e17570564 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/modf.S @@ -0,0 +1,61 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + /* .asciz "@(#)modf.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: modf.S,v 1.4 2003/08/07 16:42:31 agc Exp $" +#endif /* LIBC_SCCS and not lint */ + +/* + * double modf (value, iptr) + * double value, *iptr; + * + * Modf returns the fractional part of "value", + * and stores the integer part indirectly through "iptr". + */ + +#include "DEFS.h" + +ENTRY(modf, 0) + emodd 4(%ap),$0,$0f1.0,%r2,%r0 + jvs 1f # integer overflow + cvtld %r2,*12(%ap) + ret +1: + subd3 %r0,4(%ap),*12(%ap) + ret + +ENTRY(modff, 0) + emodf 4(%ap),$0,$0f1.0,%r2,%r0 + jvs 1f # integer overflow + cvtlf %r2,*8(%ap) + ret +1: + subf3 %r0,4(%ap),*8(%ap) + ret diff --git a/lib/nbsd_libc/arch/vax/gen/resumecontext.c b/lib/nbsd_libc/arch/vax/gen/resumecontext.c new file mode 100644 index 000000000..7baafea1e --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/resumecontext.c @@ -0,0 +1,54 @@ +/* $NetBSD: resumecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: resumecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include +#include "extern.h" + +void +_resumecontext() +{ + ucontext_t uct; + + (void)getcontext(&uct); + if (uct.uc_link == NULL) + exit(0); + (void)setcontext(uct.uc_link); + _exit(-1); + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/arch/vax/gen/signbitd.c b/lib/nbsd_libc/arch/vax/gen/signbitd.c new file mode 100644 index 000000000..7f313e1e3 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/signbitd.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitd.c,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: signbitd.c,v 1.2 2008/04/28 20:22:58 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * VAX D_floating version + */ +int +__signbitd(double x) +{ + union vax_dfloating_u u; + + u.dfltu_d = x; + + return (u.dfltu_dflt.dflt_sign == 1); +} diff --git a/lib/nbsd_libc/arch/vax/gen/signbitf.c b/lib/nbsd_libc/arch/vax/gen/signbitf.c new file mode 100644 index 000000000..02cedaab5 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/signbitf.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitf.c,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: signbitf.c,v 1.2 2008/04/28 20:22:58 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * VAX F_floating version + */ +int +__signbitf(float x) +{ + union vax_ffloating_u u; + + u.ffltu_f = x; + + return (u.ffltu_fflt.fflt_sign == 1); +} diff --git a/lib/nbsd_libc/arch/vax/gen/swapcontext.S b/lib/nbsd_libc/arch/vax/gen/swapcontext.S new file mode 100644 index 000000000..45f5afbd1 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/gen/swapcontext.S @@ -0,0 +1,50 @@ +/* $NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas. + * + * 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 "SYS.h" + +ENTRY(swapcontext, 0) + clrl %r0 /* assume getcontext succeeds */ + chmk $SYS_getcontext /* getcontext(oucp) */ + jcc 1f + jmp CERROR+2 /* something bad happened */ + +1: movl 4(%ap),%r0 /* get oucp */ + addl3 (%ap),$(5+1),%r1 /* size of callframe + arglist (LW) */ + bbs $13,6(%fp),2f /* was this a CALLG? */ + movl $5,%r1 /* size of callframe (in LW) */ +2: movl 8(%fp),(36+12*4)(%r0) /* restore AP */ + moval (%sp)[%r1],(36+13*4)(%r0) /* restore SP */ + movq 12(%fp),(36+14*4)(%r0) /* restore FP + PC */ + + pushl 8(%ap) + calls $1,_C_LABEL(setcontext) /* setcontext(ucp) */ + ret diff --git a/lib/nbsd_libc/arch/vax/genassym.cf b/lib/nbsd_libc/arch/vax/genassym.cf new file mode 100644 index 000000000..ddb2b0851 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/genassym.cf @@ -0,0 +1,83 @@ +# $NetBSD: genassym.cf,v 1.4 2008/04/28 20:22:57 martin Exp $ + +# +# Copyright (c) 2001 The NetBSD Foundation, Inc. +# All rights reserved. +# +# This code is derived from software contributed to The NetBSD Foundation +# by Matt Thomas . +# +# 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 + +define UC_LEN sizeof(ucontext_t) +define UC_FLAGS offsetof(ucontext_t, uc_flags) +define UC_LINK offsetof(ucontext_t, uc_link) +define UC_STACK offsetof(ucontext_t, uc_stack) +define UC_SIGMASK offsetof(ucontext_t, uc_sigmask) +define UC_GREGS offsetof(ucontext_t, uc_mcontext.__gregs) +define _UC_SIGMASK _UC_SIGMASK +define _UC_STACK _UC_STACK +define _UC_CPU _UC_CPU + +define _REG_R0 4*_REG_R0 +define _REG_R1 4*_REG_R1 +define _REG_R2 4*_REG_R2 +define _REG_R4 4*_REG_R4 +define _REG_R6 4*_REG_R6 +define _REG_R8 4*_REG_R8 +define _REG_R10 4*_REG_R10 +define _REG_AP 4*_REG_AP +define _REG_SP 4*_REG_SP +define _REG_FP 4*_REG_FP +define _REG_PC 4*_REG_PC +define _REG_PSL 4*_REG_PSL + +define SS_SP offsetof(stack_t, ss_sp) +define SS_SIZE offsetof(stack_t, ss_size) +define SS_FLAGS offsetof(stack_t, ss_flags) +define SS_ONSTACK SS_ONSTACK + +define SC13_LEN sizeof(struct sigcontext13) +define SC13_ONSTACK offsetof(struct sigcontext13, sc_onstack) +define SC13_MASK offsetof(struct sigcontext13, sc_mask) +define SC13_SP offsetof(struct sigcontext13, sc_sp) +define SC13_FP offsetof(struct sigcontext13, sc_fp) +define SC13_AP offsetof(struct sigcontext13, sc_ap) +define SC13_PC offsetof(struct sigcontext13, sc_pc) +define SC13_PS offsetof(struct sigcontext13, sc_ps) + +define SC_LEN sizeof(struct sigcontext) +define SC_ONSTACK offsetof(struct sigcontext, sc_onstack) +define SC_MASK13 offsetof(struct sigcontext, __sc_mask13) +define SC_SP offsetof(struct sigcontext, sc_sp) +define SC_FP offsetof(struct sigcontext, sc_fp) +define SC_AP offsetof(struct sigcontext, sc_ap) +define SC_PC offsetof(struct sigcontext, sc_pc) +define SC_PS offsetof(struct sigcontext, sc_ps) +define SC_MASK offsetof(struct sigcontext, sc_mask) + +define SIG_BLOCK SIG_BLOCK +define SS_LEN sizeof(sigset_t) diff --git a/lib/nbsd_libc/arch/vax/net/Makefile.inc b/lib/nbsd_libc/arch/vax/net/Makefile.inc new file mode 100644 index 000000000..db4492c3d --- /dev/null +++ b/lib/nbsd_libc/arch/vax/net/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.4 2004/06/06 17:09:48 he Exp $ + +# ntoh* and htn* are in ../gen/byte_swap_* +SRCS+= diff --git a/lib/nbsd_libc/arch/vax/stdlib/Makefile.inc b/lib/nbsd_libc/arch/vax/stdlib/Makefile.inc new file mode 100644 index 000000000..60574235e --- /dev/null +++ b/lib/nbsd_libc/arch/vax/stdlib/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.5 2009/07/31 20:40:00 dsl Exp $ + +SRCS+= erand48.c +NO_SRCS+= erand48_ieee754.c diff --git a/lib/nbsd_libc/arch/vax/string/Makefile.inc b/lib/nbsd_libc/arch/vax/string/Makefile.inc new file mode 100644 index 000000000..57d25c571 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/string/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.5 2010/03/12 09:12:35 uwe Exp $ + +SRCS+= bcmp.S bcopy.S bzero.S ffs.S memcmp.S +SRCS+= memcpy.S memmove.S memset.S diff --git a/lib/nbsd_libc/arch/vax/string/bcmp.S b/lib/nbsd_libc/arch/vax/string/bcmp.S new file mode 100644 index 000000000..e032c29b4 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/string/bcmp.S @@ -0,0 +1,58 @@ +/* $NetBSD: bcmp.S,v 1.3 2003/08/07 16:42:32 agc Exp $ */ +/* + * Copyright (c) 1983, 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. + */ + +/* bcmp(s1, s2, n) */ + +#include "DEFS.h" + +/* Since "cmpc3" is not portable across VAXen, do it the hard way */ +/* still, this is four times faster than the generic C version on a uvax2 */ + +ENTRY(bcmp, 0) + movl 12(%ap),%r0 # %r0 = n + jeql 9f + movq 4(%ap),%r1 # %r1 = s1, %r2 = s2 + ashl $-2,%r0,%r3 # convert len to # of long words + jeql 2f +1: + cmpl (%r1)+,(%r2)+ # no "cmpq" alas, so four bytes at a time + jneq 9f + sobgtr %r3,1b +2: + bicl3 $-4,%r0,%r3 # handle at most 3 extra bytes + jeql 8f +3: + cmpb (%r1)+,(%r2)+ + jneq 9f + sobgtr %r3,3b +8: + clrl %r0 # we have a match! +9: + ret diff --git a/lib/nbsd_libc/arch/vax/string/bcopy.S b/lib/nbsd_libc/arch/vax/string/bcopy.S new file mode 100644 index 000000000..6a9c8fe78 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/string/bcopy.S @@ -0,0 +1,75 @@ +/* $NetBSD: bcopy.S,v 1.3 2003/08/07 16:42:32 agc Exp $ */ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)bcopy.s 8.1 (Berkeley) 6/4/93" +#endif /* LIBC_SCCS and not lint */ + +/* bcopy(from, to, size) */ + +#include "DEFS.h" + +ENTRY(bcopy, R6) + movl 4(%ap),%r1 + movl 8(%ap),%r3 + movl 12(%ap),%r6 + cmpl %r1,%r3 + bgtr 2f # normal forward case + blss 3f # overlapping, must do backwards + ret # equal, nothing to do +1: + subl2 %r0,%r6 + movc3 %r0,(%r1),(%r3) +2: + movzwl $65535,%r0 + cmpl %r6,%r0 + jgtr 1b + movc3 %r6,(%r1),(%r3) + ret +3: + addl2 %r6,%r1 + addl2 %r6,%r3 + movzwl $65535,%r0 + jbr 5f +4: + subl2 %r0,%r6 + subl2 %r0,%r1 + subl2 %r0,%r3 + movc3 %r0,(%r1),(%r3) + movzwl $65535,%r0 + subl2 %r0,%r1 + subl2 %r0,%r3 +5: + cmpl %r6,%r0 + jgtr 4b + subl2 %r6,%r1 + subl2 %r6,%r3 + movc3 %r6,(%r1),(%r3) + ret diff --git a/lib/nbsd_libc/arch/vax/string/bzero.S b/lib/nbsd_libc/arch/vax/string/bzero.S new file mode 100644 index 000000000..291bdf8d0 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/string/bzero.S @@ -0,0 +1,50 @@ +/* $NetBSD: bzero.S,v 1.3 2003/08/07 16:42:32 agc Exp $ */ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)bzero.s 8.1 (Berkeley) 6/4/93" +#endif /* LIBC_SCCS and not lint */ + +/* bzero(base, length) */ + +#include "DEFS.h" + +ENTRY(bzero, 0) + movl 4(%ap),%r3 + jbr 2f +1: + subl2 %r0,8(%ap) + movc5 $0,(%r3),$0,%r0,(%r3) +2: + movzwl $65535,%r0 + cmpl 8(%ap),%r0 + jgtr 1b + movc5 $0,(%r3),$0,8(%ap),(%r3) + ret diff --git a/lib/nbsd_libc/arch/vax/string/ffs.S b/lib/nbsd_libc/arch/vax/string/ffs.S new file mode 100644 index 000000000..04d78bf10 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/string/ffs.S @@ -0,0 +1,45 @@ +/* $NetBSD: ffs.S,v 1.3 2003/08/07 16:42:32 agc Exp $ */ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)ffs.s 8.1 (Berkeley) 6/4/93" +#endif /* LIBC_SCCS and not lint */ + +/* bit = ffs(value) */ + +#include "DEFS.h" + +ENTRY(ffs, 0) + ffs $0,$32,4(%ap),%r0 + bneq 1f + mnegl $1,%r0 +1: + incl %r0 + ret diff --git a/lib/nbsd_libc/arch/vax/string/index.S b/lib/nbsd_libc/arch/vax/string/index.S new file mode 100644 index 000000000..097f35c70 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/string/index.S @@ -0,0 +1,59 @@ +/* $NetBSD: index.S,v 1.3 2003/08/07 16:42:32 agc Exp $ */ +/* + * Copyright (c) 1980, 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. + */ + +/* + * Find the first occurence of c in the string cp. + * Return pointer to match or null pointer. + * + * char * + * index(cp, c) + * char *cp, c; + */ +#include "DEFS.h" + +/* Alas not quite twice as fast as the generic C version on a uvax2 */ + +ENTRY(index, 0) + movq 4(%ap),%r0 # %r0 = cp; %r1 = c + tstb %r1 # special case, looking for '\0' + jeql 3f +1: + cmpb (%r0),%r1 + jeql 2f + tstb (%r0)+ + jneq 1b + clrl %r0 # return NULL if no match +2: + ret +3: + tstb (%r0)+ + jneq 3b + decl %r0 + jbr 2b diff --git a/lib/nbsd_libc/arch/vax/string/memcmp.S b/lib/nbsd_libc/arch/vax/string/memcmp.S new file mode 100644 index 000000000..a129fb4fe --- /dev/null +++ b/lib/nbsd_libc/arch/vax/string/memcmp.S @@ -0,0 +1,65 @@ +/* $NetBSD: memcmp.S,v 1.3 2003/08/07 16:42:32 agc 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. + */ + +/* int memcmp(s1, s2, n) */ + +#include "DEFS.h" + +ENTRY(memcmp, 0) + movl 12(%ap),%r0 + jeql 9f + movq 4(%ap),%r1 + ashl $-2,%r0,%r3 # convert len to long words + jeql 2f +1: + cmpl (%r1)+,(%r2)+ # no "cmpq" alas + jneq 7f + sobgtr %r3,1b +2: + bicl3 $-4,%r0,%r3 # handle at most 3 extra bytes + jeql 4f +3: + cmpb (%r1)+,(%r2)+ + jneq 8f + sobgtr %r3,3b +4: + clrl %r0 # we had a match + ret +7: # backup, and do a byte compare + tstl -(%r1) + tstl -(%r2) + movl $4,%r3 + jbr 3b +8: + movzbl -(%r1),%r3 + movzbl -(%r2),%r4 + subl3 %r4,%r3,%r0 +9: + ret diff --git a/lib/nbsd_libc/arch/vax/sys/__clone.S b/lib/nbsd_libc/arch/vax/sys/__clone.S new file mode 100644 index 000000000..53dec1003 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/__clone.S @@ -0,0 +1,76 @@ +/* $NetBSD: __clone.S,v 1.3 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + +/* + * int __clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone, 0) + + /* + * Sanity checks: func and stack may not be NULL. + */ + movl 4(%ap),%r2 /* check and save function */ + beql 9f + tstl 8(%ap) /* check stack */ + beql 9f + + /* + * The system call expects (flags, stack). + */ + movl 12(%ap),4(%ap) /* XXX this doesn't work for + callg with a RO arglist */ + movl $2,(%ap) + SYSTRAP(__clone) /* only %r0/%r1 munged */ + + blbc %r1,8f /* %r1<0>: 0=parent 1=child */ + + /* Call the clone's entry point. */ + pushl 16(%ap) + calls $1,(%r2) + + /* Pass return value to _exit(). */ + pushl %r0 + calls $1,_C_LABEL(_exit) + + /* NOTREACHED */ + +8: ret + +9: movl $EINVAL,%r0 + jmp CERROR+2 diff --git a/lib/nbsd_libc/arch/vax/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/vax/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..8c0695566 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/__sigaction14_sigtramp.c @@ -0,0 +1,81 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.10 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.10 2008/04/28 20:22:58 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include + +#include "extern.h" + +__weak_alias(__sigaction14, __libc_sigaction14) + +extern const int __sigtramp_siginfo_3[]; + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); + +#ifdef __LIBC12_SOURCE__ + /* + * We select the non-SA_SIGINFO trampoline if SA_SIGINFO is not + * set in the sigaction. + */ + if ((act->sa_flags & SA_SIGINFO) == 0) { + extern const int __sigtramp_sigcontext_2[]; + int sav = errno; + int rv = __sigaction_sigtramp(sig, act, oact, + __sigtramp_sigcontext_2, 2); + if (rv >= 0 || errno != EINVAL) + return rv; + errno = sav; + } +#endif + + /* + * If SA_SIGINFO was specificed or the compatibility trampolines + * can't be used, use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_3, 3); +} diff --git a/lib/nbsd_libc/arch/vax/sys/__sigtramp3.S b/lib/nbsd_libc/arch/vax/sys/__sigtramp3.S new file mode 100644 index 000000000..e4703a26a --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/__sigtramp3.S @@ -0,0 +1,50 @@ +/* $NetBSD: __sigtramp3.S,v 1.1 2003/09/30 19:05:41 matt Exp $ */ + +/* + * Copyright (c) 2003 Matt Thomas + * 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 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. + */ + +/* + * Signal trampoline; registers when called: + * pc, psl - obvious + * sp, ap - points to argument list + * 4(ap) -- signo + * 8(ap) -- pointer to siginfo + * 12(ap) -- pointer to ucontext + * fp - address of signal handler + */ + +#include "SYS.h" + + .text + _ALIGN_TEXT + + .globl _C_LABEL(__sigtramp_siginfo_3) +_C_LABEL(__sigtramp_siginfo_3): + callg (%ap),(%fp) # use global arg list + addl2 $8,%ap # arg is pointer to ucontext + SYSTRAP(setcontext) # exit from here + halt # illegal insn diff --git a/lib/nbsd_libc/arch/vax/sys/__syscall.S b/lib/nbsd_libc/arch/vax/sys/__syscall.S new file mode 100644 index 000000000..4ceb9bb43 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/__syscall.S @@ -0,0 +1,50 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)syscall.s 8.2 (Berkeley) 1/21/94" */ + .asciz "$NetBSD: __syscall.S,v 1.3 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(__syscall, 0) + movl 4(%ap),%r0 # syscall number + addl2 $8,%ap # skip the first argument + subl3 $2,-8(%ap),(%ap) # two fewer arguments + chmk %r0 + jcs 1f + ret +1: + jmp CERROR+2 diff --git a/lib/nbsd_libc/arch/vax/sys/__vfork14.S b/lib/nbsd_libc/arch/vax/sys/__vfork14.S new file mode 100644 index 000000000..d9bdabdc9 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/__vfork14.S @@ -0,0 +1,76 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)Ovfork.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: __vfork14.S,v 1.5 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +/* + * @(#)vfork.s 4.1 (Berkeley) 12/21/80 + * C library -- vfork + */ + +#include "SYS.h" + +/* + * pid = vfork(); + * + * %r1 == 0 in parent process, %r1 == 1 in child process. + * %r0 == pid of child in parent, %r0 == pid of parent in child. + * + * trickery here, due to keith sklower, uses ret to clear the stack, + * and then returns with a jump indirect, since only one person can return + * with a ret off this stack... we do the ret before we vfork! + */ + +ENTRY(__vfork14, 0) + movl 16(%fp),%r2 # save return address before we smash it + movab here,16(%fp) + ret +here: + chmk $ SYS___vfork14 + bcs err # if failed, set errno and return -1 + /* this next trick is Chris Torek's fault */ + mnegl %r1,%r1 # %r1 = 0xffffffff if child, 0 if parent + bicl2 %r1,%r0 # %r0 &= ~%r1, i.e., 0 if child, else unchanged + jmp (%r2) + +err: +#ifdef _REENTRANT + pushr $0x5 + calls $0,_C_LABEL(__errno) + movl (%sp)+,(%r0) + mnegl $1,%r0 + rsb +#else + movl %r0,_C_LABEL(errno) + mnegl $1,%r0 + jmp (%r2) +#endif diff --git a/lib/nbsd_libc/arch/vax/sys/brk.S b/lib/nbsd_libc/arch/vax/sys/brk.S new file mode 100644 index 000000000..63be221fb --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/brk.S @@ -0,0 +1,54 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)brk.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: brk.S,v 1.12 2003/12/26 11:21:48 martin Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl CURBRK + .globl _C_LABEL(__minbrk) + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + +ENTRY(_brk, 0) + cmpl _C_LABEL(__minbrk),4(%ap) # gtr > _end + blequ 1f # is fine + movl _C_LABEL(__minbrk),4(%ap) # shrink back to _end +1: chmk $ SYS_break # do it + jcs err + movl 4(%ap),CURBRK + clrl %r0 + ret +err: + jmp CERROR+2 diff --git a/lib/nbsd_libc/arch/vax/sys/cerror.S b/lib/nbsd_libc/arch/vax/sys/cerror.S new file mode 100644 index 000000000..f9430318d --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/cerror.S @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)cerror.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: cerror.S,v 1.8 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _C_LABEL(errno) +CERROR: .word 0 +#ifdef _REENTRANT + pushl %r0 + calls $0,_C_LABEL(__errno) + movl (%sp)+,(%r0) +#else + movl %r0,_C_LABEL(errno) +#endif + mnegl $1,%r0 + movl %r0,%r1 + ret diff --git a/lib/nbsd_libc/arch/vax/sys/execl.S b/lib/nbsd_libc/arch/vax/sys/execl.S new file mode 100644 index 000000000..b1262f01d --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/execl.S @@ -0,0 +1,42 @@ +/* $NetBSD: execl.S,v 1.3 2003/08/24 21:04:53 ragge Exp $ */ +/* + * Copyright (c) 2001 Ludd, University of Lule}, Sweden. 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 at Ludd, University of + * Lule}, Sweden and its contributors. + * 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. + */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(execl, _execl) +#endif + +ENTRY(_execl, 0) + pushal 8(%ap) # Push pointer to argv vector + pushl 4(%ap) # Push path + calls $2,_C_LABEL(execv) + ret diff --git a/lib/nbsd_libc/arch/vax/sys/execle.S b/lib/nbsd_libc/arch/vax/sys/execle.S new file mode 100644 index 000000000..6c8c9ff4a --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/execle.S @@ -0,0 +1,44 @@ +/* $NetBSD: execle.S,v 1.4 2003/08/24 21:04:53 ragge Exp $ */ +/* + * Copyright (c) 2001 Ludd, University of Lule}, Sweden. 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 at Ludd, University of + * Lule}, Sweden and its contributors. + * 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. + */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(execle, _execle) +#endif + +ENTRY(_execle, 0) + movl (%ap),%r0 # Get number of args + pushl (%ap)[%r0] # Push last arg (envp) + pushal 8(%ap) # Push pointer to argv vector + pushl 4(%ap) # Push path + calls $3,_C_LABEL(execve) + ret diff --git a/lib/nbsd_libc/arch/vax/sys/execlp.S b/lib/nbsd_libc/arch/vax/sys/execlp.S new file mode 100644 index 000000000..fd275ce28 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/execlp.S @@ -0,0 +1,42 @@ +/* $NetBSD: execlp.S,v 1.3 2003/08/24 21:04:53 ragge Exp $ */ +/* + * Copyright (c) 2001 Ludd, University of Lule}, Sweden. 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 at Ludd, University of + * Lule}, Sweden and its contributors. + * 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. + */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(execlp, _execlp) +#endif + +ENTRY(_execlp, 0) + pushal 8(%ap) # Push pointer to argv vector + pushl 4(%ap) # Push path + calls $2,_C_LABEL(execvp) + ret diff --git a/lib/nbsd_libc/arch/vax/sys/exect.S b/lib/nbsd_libc/arch/vax/sys/exect.S new file mode 100644 index 000000000..3061c6fa6 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/exect.S @@ -0,0 +1,41 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)exect.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: exect.S,v 1.5 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" +#include + +ENTRY(exect, 0) + bispsw $PSL_T + chmk $ SYS_execve + jmp CERROR+2 # exect(file, argv, env) diff --git a/lib/nbsd_libc/arch/vax/sys/fork.S b/lib/nbsd_libc/arch/vax/sys/fork.S new file mode 100644 index 000000000..72ddf58fb --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/fork.S @@ -0,0 +1,41 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)fork.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: fork.S,v 1.5 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +_SYSCALL(__fork,fork) + jlbc %r1,1f # parent, since %r1 == 0 in parent, 1 in child + clrl %r0 +1: + ret # pid = fork() diff --git a/lib/nbsd_libc/arch/vax/sys/getcontext.S b/lib/nbsd_libc/arch/vax/sys/getcontext.S new file mode 100644 index 000000000..b0d78e107 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/getcontext.S @@ -0,0 +1,61 @@ +/* $NetBSD: getcontext.S,v 1.4 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas. + * + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +/* + * Can't use R4 or R5 since those needed by the phread_switch code. + */ +ENTRY(_getcontext, 0) + clrl %r0 /* assume success */ + chmk $SYS_getcontext /* getcontext(oucp) */ + jcc 1f + jmp CERROR+2 /* badness happened */ + +1: movl 4(%ap),%r2 /* ptr to ucontext */ + movl 16(%fp),%r3 /* PC to return to */ + movab 2f,16(%fp) /* let's return early */ + ret + /* + * Now we have the blown away the callframe and argument + * list so it's as if we've return from the function except + * we aren't executing the caller's code (yet). So it's + * the callers state but we still have control. + */ + _ALIGN_TEXT +2: movq %ap,(36+12*4)(%r2) /* adjust AP + SP */ + movl %fp,(36+14*4)(%r2) /* adjust FP */ + movl %r3,(36+15*4)(%r2) /* adjust PC */ + jmp (%r3) /* and return */ diff --git a/lib/nbsd_libc/arch/vax/sys/pipe.S b/lib/nbsd_libc/arch/vax/sys/pipe.S new file mode 100644 index 000000000..9163d1fe0 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/pipe.S @@ -0,0 +1,46 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)pipe.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: pipe.S,v 1.4 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +_SYSCALL(_pipe,pipe) + movl 4(%ap),%r2 + movl %r0,(%r2)+ + movl %r1,(%r2) + clrl %r0 + ret diff --git a/lib/nbsd_libc/arch/vax/sys/ptrace.S b/lib/nbsd_libc/arch/vax/sys/ptrace.S new file mode 100644 index 000000000..0aa93b1f6 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/ptrace.S @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)ptrace.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: ptrace.S,v 1.7 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(ptrace, 0) +#ifdef _REENTRANT + calls $0,_C_LABEL(__errno) + clrl (%r0) +#else + clrl _C_LABEL(errno) +#endif + chmk $ SYS_ptrace + jcs err + ret +err: + jmp CERROR+2 diff --git a/lib/nbsd_libc/arch/vax/sys/sbrk.S b/lib/nbsd_libc/arch/vax/sys/sbrk.S new file mode 100644 index 000000000..19fad3abe --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/sbrk.S @@ -0,0 +1,63 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)sbrk.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: sbrk.S,v 1.10 2003/08/07 16:42:33 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _end + .globl _C_LABEL(__minbrk) + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +_C_LABEL(__minbrk): + .long _end +CURBRK: + .long _end + .text + +ENTRY(_sbrk, 0) + addl3 CURBRK,4(%ap),-(%sp) + pushl $1 + movl %ap,%r3 + movl %sp,%ap + chmk $ SYS_break + jcs err + movl CURBRK,%r0 + addl2 4(%r3),CURBRK + ret +err: + jmp CERROR+2 diff --git a/lib/nbsd_libc/arch/vax/sys/shmat.S b/lib/nbsd_libc/arch/vax/sys/shmat.S new file mode 100644 index 000000000..216dbd807 --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2000/07/07 08:20:55 itohy Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/vax/sys/syscall.S b/lib/nbsd_libc/arch/vax/sys/syscall.S new file mode 100644 index 000000000..b55c600fa --- /dev/null +++ b/lib/nbsd_libc/arch/vax/sys/syscall.S @@ -0,0 +1,49 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)syscall.s 8.2 (Berkeley) 1/21/94" */ + .asciz "$NetBSD: syscall.S,v 1.6 2003/08/07 16:42:34 agc Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(syscall, 0) + movl 4(%ap),%r0 # syscall number + subl3 $1,(%ap)+,(%ap) # one fewer arguments + chmk %r0 + jcs 1f + ret +1: + jmp CERROR+2 diff --git a/lib/nbsd_libc/arch/x86_64/Makefile.inc b/lib/nbsd_libc/arch/x86_64/Makefile.inc new file mode 100644 index 000000000..b4b415ef8 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.5 2006/06/17 18:04:24 uwe Exp $ + +SRCS+= __sigaction14_sigtramp.c __sigtramp2.S diff --git a/lib/nbsd_libc/arch/x86_64/SYS.h b/lib/nbsd_libc/arch/x86_64/SYS.h new file mode 100644 index 000000000..2b0260346 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/SYS.h @@ -0,0 +1,93 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)SYS.h 5.5 (Berkeley) 5/7/91 + * $NetBSD: SYS.h,v 1.10 2007/11/23 07:36:05 dsl Exp $ + */ + +#include +#include + +#ifdef __STDC__ +#define SYSTRAP(x) movl $(SYS_ ## x),%eax; movq %rcx, %r10; syscall +#else +#define SYSTRAP(x) movl $(SYS_/**/x),%eax; movq %rcx, %r10; syscall +#endif + +#define CERROR _C_LABEL(__cerror) +#define CURBRK _C_LABEL(__curbrk) + +#define _SYSCALL_NOERROR(x,y) \ + ENTRY(x); \ + SYSTRAP(y) + +#ifdef PIC +#define _SYSCALL_ERR \ + mov PIC_GOT(CERROR), %rcx; \ + jmp *%rcx +#else +#define _SYSCALL_ERR \ + jmp CERROR +#endif + +#define _SYSCALL(x,y) \ + .text; _ALIGN_TEXT; \ + 2: _SYSCALL_ERR; \ + _SYSCALL_NOERROR(x,y); \ + jc 2b + +#define SYSCALL_NOERROR(x) \ + _SYSCALL_NOERROR(x,x) + +#define SYSCALL(x) \ + _SYSCALL(x,x) + +#define PSEUDO_NOERROR(x,y) \ + _SYSCALL_NOERROR(x,y); \ + ret + +#define PSEUDO(x,y) \ + _SYSCALL_NOERROR(x,y); \ + jc 2f; \ + ret; \ + 2: _SYSCALL_ERR + +#define RSYSCALL_NOERROR(x) \ + PSEUDO_NOERROR(x,x) + +#define RSYSCALL(x) \ + PSEUDO(x,x) + +#define WSYSCALL(weak,strong) \ + WEAK_ALIAS(weak,strong); \ + PSEUDO(strong,weak) + + .globl CERROR diff --git a/lib/nbsd_libc/arch/x86_64/gdtoa/Makefile.inc b/lib/nbsd_libc/arch/x86_64/gdtoa/Makefile.inc new file mode 100644 index 000000000..ce2f4854d --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gdtoa/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/15 17:35:18 kleink Exp $ + +SRCS+= strtof.c strtold_px.c +SRCS+= strtopx.c diff --git a/lib/nbsd_libc/arch/x86_64/gdtoa/arith.h b/lib/nbsd_libc/arch/x86_64/gdtoa/arith.h new file mode 100644 index 000000000..1513f1fd2 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gdtoa/arith.h @@ -0,0 +1,3 @@ +/* $NetBSD: arith.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define IEEE_LITTLE_ENDIAN diff --git a/lib/nbsd_libc/arch/x86_64/gdtoa/gd_qnan.h b/lib/nbsd_libc/arch/x86_64/gdtoa/gd_qnan.h new file mode 100644 index 000000000..98638cc8a --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gdtoa/gd_qnan.h @@ -0,0 +1,11 @@ +/* $NetBSD: gd_qnan.h,v 1.1 2006/01/25 15:33:28 kleink Exp $ */ + +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff80000 +#define ldus_QNAN0 0x0 +#define ldus_QNAN1 0x0 +#define ldus_QNAN2 0x0 +#define ldus_QNAN3 0x4000 +#define ldus_QNAN4 0x7fff +/* 6 bytes of tail padding follow, per AMD64 ABI */ diff --git a/lib/nbsd_libc/arch/x86_64/gen/Makefile.inc b/lib/nbsd_libc/arch/x86_64/gen/Makefile.inc new file mode 100644 index 000000000..3f794563a --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/Makefile.inc @@ -0,0 +1,36 @@ +# $NetBSD: Makefile.inc,v 1.15 2010/01/14 02:09:46 joerg Exp $ + +# objects built from assembler sources (need lint stubs) +SRCS+= alloca.S byte_swap_2.S byte_swap_4.S byte_swap_8.S fabs.S \ + flt_rounds.S fpgetmask.S fpgetround.S fpgetsticky.S \ + fpsetmask.S fpsetround.S fpsetsticky.S + +SRCS+= __setjmp14.S +SRCS+= _setjmp.S +SRCS+= __sigsetjmp14.S + +SRCS+= resumecontext.S swapcontext.S + +# objects built from C sources +SRCS+= _lwp.c makecontext.c + +# Common ieee754 constants and functions +SRCS+= infinityf_ieee754.c infinity_ieee754.c +SRCS+= fpclassifyf_ieee754.c fpclassifyd_ieee754.c +SRCS+= isfinitef_ieee754.c isfinited_ieee754.c +SRCS+= isinff_ieee754.c isinfd_ieee754.c +SRCS+= isnanf_ieee754.c isnand_ieee754.c +SRCS+= signbitf_ieee754.c signbitd_ieee754.c + +SRCS+= nanf.c infinityl.c +SRCS+= fpclassifyl.c isfinitel.c isinfl.c isnanl.c signbitl.c + +LSRCS.x86_64.gen=\ + Lint__setjmp.c Lint_alloca.c Lint_bswap16.c Lint_bswap32.c \ + Lint_bswap64.c Lint_fabs.c \ + Lint___setjmp14.c Lint___sigsetjmp14.c Lint_flt_rounds.c \ + Lint_fpgetmask.c Lint_fpgetround.c Lint_fpgetsticky.c \ + Lint_fpsetmask.c Lint_fpsetround.c Lint_fpsetsticky.c +LSRCS+= ${LSRCS.x86_64.gen} +DPSRCS+= ${LSRCS.x86_64.gen} +CLEANFILES+= ${LSRCS.x86_64.gen} diff --git a/lib/nbsd_libc/arch/x86_64/gen/__setjmp14.S b/lib/nbsd_libc/arch/x86_64/gen/__setjmp14.S new file mode 100644 index 000000000..c5541cb38 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/__setjmp14.S @@ -0,0 +1,108 @@ +/* $NetBSD: __setjmp14.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: __setjmp14.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__setjmp14) + movq (%rsp),%r11 + movq %rbx,(_JB_RBX * 8)(%rdi) + movq %rbp,(_JB_RBP * 8)(%rdi) + movq %r12,(_JB_R12 * 8)(%rdi) + movq %r13,(_JB_R13 * 8)(%rdi) + movq %r14,(_JB_R14 * 8)(%rdi) + movq %r15,(_JB_R15 * 8)(%rdi) + movq %rsp,(_JB_RSP * 8)(%rdi) + movq %r11,(_JB_PC * 8)(%rdi) + + leaq (_JB_SIGMASK * 8)(%rdi),%rdx + xorl %edi,%edi + xorq %rsi,%rsi + +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif +2: xorl %eax,%eax + ret + +ENTRY(__longjmp14) + movq %rdi,%r12 + movl %esi,%r8d + + leaq (_JB_SIGMASK * 8)(%rdi),%rsi + movl $3,%edi /* SIG_SETMASK */ + xorq %rdx,%rdx + + pushq %r8 +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif + popq %r8 + movq (_JB_RBX * 8)(%r12),%rbx + movq (_JB_RBP * 8)(%r12),%rbp + movq (_JB_R13 * 8)(%r12),%r13 + movq (_JB_R14 * 8)(%r12),%r14 + movq (_JB_R15 * 8)(%r12),%r15 + movq (_JB_RSP * 8)(%r12),%rsp + movq (_JB_PC * 8)(%r12),%r11 + movq (_JB_R12 * 8)(%r12),%r12 + + movl %r8d,%eax + testl %eax,%eax + jnz 1f + incl %eax +1: movq %r11,0(%rsp) + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/__sigsetjmp14.S b/lib/nbsd_libc/arch/x86_64/gen/__sigsetjmp14.S new file mode 100644 index 000000000..07b3db61d --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/__sigsetjmp14.S @@ -0,0 +1,112 @@ +/* $NetBSD: __sigsetjmp14.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: __sigsetjmp14.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(__sigsetjmp14) + movq (%rsp),%r11 + movq %rbx,(_JB_RBX * 8)(%rdi) + movq %rbp,(_JB_RBP * 8)(%rdi) + movq %r12,(_JB_R12 * 8)(%rdi) + movq %r13,(_JB_R13 * 8)(%rdi) + movq %r14,(_JB_R14 * 8)(%rdi) + movq %r15,(_JB_R15 * 8)(%rdi) + movq %rsp,(_JB_RSP * 8)(%rdi) + movq %r11,(_JB_PC * 8)(%rdi) + + movq %rsi,(_JB_SIGFLAG * 8)(%rdi) + testl %esi,%esi + jz 2f + + leaq (_JB_SIGMASK * 8)(%rdi),%rdx + xorl %edi,%edi + xorq %rsi,%rsi + +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif +2: xorl %eax,%eax + ret + +ENTRY(__siglongjmp14) + movq %rdi,%r12 + pushq %rsi + cmpl $0, (_JB_SIGFLAG * 8)(%rdi) + + jz 2f + leaq (_JB_SIGMASK * 8)(%rdi),%rsi + movl $3,%edi /* SIG_SETMASK */ + xorq %rdx,%rdx + +#ifdef PIC + call PIC_PLT(_C_LABEL(__sigprocmask14)) +#else + call _C_LABEL(__sigprocmask14) +#endif +2: popq %rax + movq (_JB_RBX * 8)(%r12),%rbx + movq (_JB_RBP * 8)(%r12),%rbp + movq (_JB_R13 * 8)(%r12),%r13 + movq (_JB_R14 * 8)(%r12),%r14 + movq (_JB_R15 * 8)(%r12),%r15 + movq (_JB_RSP * 8)(%r12),%rsp + movq (_JB_PC * 8)(%r12),%r11 + movq (_JB_R12 * 8)(%r12),%r12 + + testl %eax,%eax + jnz 1f + incl %eax +1: movq %r11,0(%rsp) + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/_lwp.c b/lib/nbsd_libc/arch/x86_64/gen/_lwp.c new file mode 100644 index 000000000..511c12696 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/_lwp.c @@ -0,0 +1,70 @@ +/* $NetBSD: _lwp.c,v 1.6 2009/07/02 09:57:09 joerg Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nathan J. Williams. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _lwp.c,v 1.6 2009/07/02 09:57:09 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + __greg_t *gr = u->uc_mcontext.__gregs; + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + /* LINTED uintptr_t is safe */ + gr[_REG_RIP] = (uintptr_t)start; + + sp = (void **) (((uintptr_t)(stack_base + stack_size) & ~15)); + + /* LINTED __greg_t is safe */ + gr[_REG_RDI] = (__greg_t)arg; + *--sp = (void *) _lwp_exit; + + /* LINTED uintptr_t is safe */ + gr[_REG_URSP] = (uintptr_t) sp; + + /* LINTED private is currently unused */ +} diff --git a/lib/nbsd_libc/arch/x86_64/gen/_setjmp.S b/lib/nbsd_libc/arch/x86_64/gen/_setjmp.S new file mode 100644 index 000000000..fb95430ac --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/_setjmp.S @@ -0,0 +1,84 @@ +/* $NetBSD: _setjmp.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 +#if defined(LIBC_SCCS) + RCSID("$NetBSD: _setjmp.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from the last call to + * _setjmp(a) + * by restoring registers from the stack. + * The previous signal state is NOT restored. + */ + +ENTRY(_setjmp) + movq (%rsp),%r11 + movq %rbx,(_JB_RBX * 8)(%rdi) + movq %rbp,(_JB_RBP * 8)(%rdi) + movq %r12,(_JB_R12 * 8)(%rdi) + movq %r13,(_JB_R13 * 8)(%rdi) + movq %r14,(_JB_R14 * 8)(%rdi) + movq %r15,(_JB_R15 * 8)(%rdi) + movq %rsp,(_JB_RSP * 8)(%rdi) + movq %r11,(_JB_PC * 8)(%rdi) + + xorl %eax,%eax + ret + +ENTRY(_longjmp) + movq (_JB_RBX * 8)(%rdi),%rbx + movq (_JB_RBP * 8)(%rdi),%rbp + movq (_JB_R12 * 8)(%rdi),%r12 + movq (_JB_R13 * 8)(%rdi),%r13 + movq (_JB_R14 * 8)(%rdi),%r14 + movq (_JB_R15 * 8)(%rdi),%r15 + movq (_JB_RSP * 8)(%rdi),%rsp + movq (_JB_PC * 8)(%rdi),%r11 + + movl %esi,%eax + testl %eax,%eax + jnz 1f + incl %eax +1: movq %r11,0(%rsp) + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/alloca.S b/lib/nbsd_libc/arch/x86_64/gen/alloca.S new file mode 100644 index 000000000..ab232f60b --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/alloca.S @@ -0,0 +1,16 @@ +/* $NetBSD: alloca.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $ */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: alloca.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $") +#endif + +ENTRY(alloca) + popq %rdx + movq %rsp,%rcx + addq $15,%rdi /* round up to 16 bytes */ + andq $~15,%rdi + subq %rdi,%rsp + movq %rsp,%rax + jmp *%rdx diff --git a/lib/nbsd_libc/arch/x86_64/gen/fabs.S b/lib/nbsd_libc/arch/x86_64/gen/fabs.S new file mode 100644 index 000000000..69d577cc4 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fabs.S @@ -0,0 +1,17 @@ +/* $NetBSD: fabs.S,v 1.3 2004/03/23 17:11:35 drochner Exp $ */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: fabs.S,v 1.3 2004/03/23 17:11:35 drochner Exp $") +#endif + + .section .rodata + .align 8 +__signmask: + .long 0xffffffff + .long 0x7fffffff + +ENTRY(fabs) + movsd __signmask(%rip),%xmm1 + andpd %xmm1,%xmm0 + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/flt_rounds.S b/lib/nbsd_libc/arch/x86_64/gen/flt_rounds.S new file mode 100644 index 000000000..1b23501cd --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/flt_rounds.S @@ -0,0 +1,29 @@ +/* $NetBSD: flt_rounds.S,v 1.2 2002/06/06 23:04:35 fvdl Exp $ */ + +/* + * Written by J.T. Conklin, Apr 4, 1995 + * Public domain. + */ + +#include + + .text + _ALIGN_TEXT +_map: + .byte 1 /* round to nearest */ + .byte 3 /* round to negative infinity */ + .byte 2 /* round to positive infinity */ + .byte 0 /* round to zero */ + +ENTRY(__flt_rounds) + fnstcw -4(%rsp) + movl -4(%rsp),%eax + shrl $10,%eax + andl $3,%eax +#ifdef PIC + leaq PIC_GOT(_map),%rcx + movb (%rcx,%rax,1),%al +#else + movb _map(,%rax,1),%al +#endif + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/fpclassifyl.c b/lib/nbsd_libc/arch/x86_64/gen/fpclassifyl.c new file mode 100644 index 000000000..edaf28350 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fpclassifyl.c @@ -0,0 +1,70 @@ +/* $NetBSD: fpclassifyl.c,v 1.4 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyl.c,v 1.4 2008/04/28 20:22:58 martin Exp $"); +#endif + +#include +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__fpclassifyl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + _DIAGASSERT(u.extu_ext.ext_exp == 0 || + (u.extu_ext.ext_frach & 0x80000000)); + + if (u.extu_ext.ext_exp == 0) { + if ((u.extu_ext.ext_frach & 0x7fffffff) == 0 && + u.extu_ext.ext_fracl == 0) + return FP_ZERO; + else + return FP_SUBNORMAL; + } else if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) { + if ((u.extu_ext.ext_frach & 0x7fffffff) == 0 && + u.extu_ext.ext_fracl == 0) + return FP_INFINITE; + else + return FP_NAN; + } + + return FP_NORMAL; +} diff --git a/lib/nbsd_libc/arch/x86_64/gen/fpgetmask.S b/lib/nbsd_libc/arch/x86_64/gen/fpgetmask.S new file mode 100644 index 000000000..2b367a961 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fpgetmask.S @@ -0,0 +1,26 @@ +/* $NetBSD: fpgetmask.S,v 1.3 2002/06/12 19:17:22 fvdl Exp $ */ + +/* + * Written by J.T. Conklin, Apr 4, 1995 + * Public domain. + */ + +#include + +/* + * XXX only read x87 control word here. If an application only + * uses the fp* interface to manipulate FP bits, it should + * always remain in sync with the SSE mxcsr register. + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetmask, _fpgetmask) +ENTRY(_fpgetmask) +#else +ENTRY(fpgetmask) +#endif + fnstcw -4(%rsp) + movl -4(%rsp),%eax + notl %eax + andl $63,%eax + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/fpgetround.S b/lib/nbsd_libc/arch/x86_64/gen/fpgetround.S new file mode 100644 index 000000000..2693c2791 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fpgetround.S @@ -0,0 +1,25 @@ +/* $NetBSD: fpgetround.S,v 1.3 2002/06/12 19:17:22 fvdl Exp $ */ + +/* + * Written by J.T. Conklin, Apr 4, 1995 + * Public domain. + */ + +#include + +/* + * XXX store only x87 state. If an application only uses the fp* + * interface, this should be in sync with the SSE mxcsr register. + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetround, _fpgetround) +ENTRY(_fpgetround) +#else +ENTRY(fpgetround) +#endif + fnstcw -4(%rsp) + movl -4(%rsp),%eax + rorl $10,%eax + andl $3,%eax + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/fpgetsticky.S b/lib/nbsd_libc/arch/x86_64/gen/fpgetsticky.S new file mode 100644 index 000000000..f7158640e --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fpgetsticky.S @@ -0,0 +1,27 @@ +/* $NetBSD: fpgetsticky.S,v 1.3 2002/06/12 19:17:22 fvdl Exp $ */ + +/* + * Written by Frank van der Linden at Wasabi Systems for NetBSD. + * Public domain. + * + */ + +#include + +/* + * XXX read both the x87 and SSE status words here, and OR + * them to get a complete picture of exceptions. + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpgetsticky, _fpgetsticky) +ENTRY(_fpgetsticky) +#else +ENTRY(fpgetsticky) +#endif + fnstsw -4(%rsp) + stmxcsr -8(%rsp) + movl -4(%rsp),%eax + orl -8(%rsp),%eax + andl $63,%eax + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/fpsetmask.S b/lib/nbsd_libc/arch/x86_64/gen/fpsetmask.S new file mode 100644 index 000000000..0e1809928 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fpsetmask.S @@ -0,0 +1,41 @@ +/* $NetBSD: fpsetmask.S,v 1.3 2002/06/12 19:17:22 fvdl Exp $ */ + +/* + * Written by Frank van der Linden at Wasabi Systems for NetBSD. + * Public domain. + */ + +#include + +/* + * XXX set both the x87 control word and the SSE mxcsr register. + * Applications should only set exception and round flags + * via the fp*() interface, otherwise the status words + * will get our of sync. + */ + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetmask, _fpsetmask) +ENTRY(_fpsetmask) +#else +ENTRY(fpsetmask) +#endif + fnstcw -4(%rsp) + stmxcsr -8(%rsp) + andl $63,%edi + notl %edi + + movl -4(%rsp),%edx + movl %edx,%eax + andl %edi,%edx + movl %edx,-4(%rsp) + + movl -8(%rsp),%edx + roll $7,%edi + andl %edi,%edx + movl %edx,-8(%rsp) + + fldcw -4(%rsp) + ldmxcsr -8(%rsp) + andl $63,%eax + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/fpsetround.S b/lib/nbsd_libc/arch/x86_64/gen/fpsetround.S new file mode 100644 index 000000000..68e275f77 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fpsetround.S @@ -0,0 +1,48 @@ +/* $NetBSD: fpsetround.S,v 1.3 2002/06/12 19:17:22 fvdl Exp $ */ + +/* + * Written by Frank van der Linden at Wasabi Systems for NetBSD. + * Public domain. + */ + +#include + +/* + * XXX set both the x87 control word and the SSE mxcsr register. + * Applications should only set exception and round flags + * via the fp*() interface, otherwise the status words + * will get our of sync. + */ + + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetround, _fpsetround) +ENTRY(_fpsetround) +#else +ENTRY(fpsetround) +#endif + fnstcw -4(%rsp) + stmxcsr -8(%rsp) + + andl $3,%edi + + movl -4(%rsp),%edx + rorl $10,%edx + movl %edx,%eax + andl $3,%eax + + andl $~3,%edx + orl %edi,%edx + roll $10,%edx + movl %edx,-4(%rsp) + + movl -8(%rsp),%edx + rorl $13,%edx + andl $~3,%edx + orl %edi,%edx + roll $13,%edx + movl %edx,-8(%rsp) + + ldmxcsr -8(%rsp) + fldcw -4(%rsp) + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/fpsetsticky.S b/lib/nbsd_libc/arch/x86_64/gen/fpsetsticky.S new file mode 100644 index 000000000..71c29cfc4 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/fpsetsticky.S @@ -0,0 +1,45 @@ +/* $NetBSD: fpsetsticky.S,v 1.5 2004/03/09 17:16:13 drochner Exp $ */ + +/* + * Written by Frank van der Linden at Wasabi Systems for NetBSD + * Public domain. + */ + +#include + +/* + * XXX set both the x87 status word and the SSE mxcsr register. + * Applications should only set exception and round flags + * via the fp*() interface, otherwise the status words + * will get our of sync. + */ + + +#ifdef WEAK_ALIAS +WEAK_ALIAS(fpsetsticky, _fpsetsticky) +ENTRY(_fpsetsticky) +#else +ENTRY(fpsetsticky) +#endif + fnstenv -28(%rsp) + stmxcsr -32(%rsp) + + andl $63,%edi + + movl -24(%rsp),%eax + movl %eax,%edx + andb $0xc0,%dl + + orl %edi,%edx + movl %edx,-24(%rsp) + + movl -32(%rsp),%edx + orl %edx,%eax + andl $63,%eax + andb $0xc0,%dl + orl %edi,%edx + movl %edx,-32(%rsp) + + ldmxcsr -32(%rsp) + fldenv -28(%rsp) + ret diff --git a/lib/nbsd_libc/arch/x86_64/gen/infinityl.c b/lib/nbsd_libc/arch/x86_64/gen/infinityl.c new file mode 100644 index 000000000..d931768dd --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/infinityl.c @@ -0,0 +1,16 @@ +/* $NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * IEEE-compatible infinityl.c for little-endian 80-bit format -- public domain. + * Note that the representation includes 48 bits of tail padding per amd64 ABI. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinityl.c,v 1.2 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include + +const union __long_double_u __infinityl = + { { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f, 0, 0, 0, 0, 0, 0 } }; diff --git a/lib/nbsd_libc/arch/x86_64/gen/isfinitel.c b/lib/nbsd_libc/arch/x86_64/gen/isfinitel.c new file mode 100644 index 000000000..d0f960a0f --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/isfinitel.c @@ -0,0 +1,59 @@ +/* $NetBSD: isfinitel.c,v 1.3 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinitel.c,v 1.3 2008/04/28 20:22:58 martin Exp $"); +#endif + +#include +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__isfinitel(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + _DIAGASSERT(u.extu_ext.ext_exp == 0 || + (u.extu_ext.ext_frach & 0x80000000)); + + if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/arch/x86_64/gen/isinfl.c b/lib/nbsd_libc/arch/x86_64/gen/isinfl.c new file mode 100644 index 000000000..330451122 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/isinfl.c @@ -0,0 +1,63 @@ +/* $NetBSD: isinfl.c,v 1.6 2007/02/02 23:19:25 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinfl.c,v 1.6 2007/02/02 23:19:25 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.3 isinf - test for infinity + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__isinfl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + u.extu_ext.ext_frach == 0x80000000 && u.extu_ext.ext_fracl == 0); +} diff --git a/lib/nbsd_libc/arch/x86_64/gen/isnanl.c b/lib/nbsd_libc/arch/x86_64/gen/isnanl.c new file mode 100644 index 000000000..8694ec83c --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/isnanl.c @@ -0,0 +1,64 @@ +/* $NetBSD: isnanl.c,v 1.6 2007/02/02 23:19:25 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnanl.c,v 1.6 2007/02/02 23:19:25 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.4 isnan - test for a NaN + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__isnanl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_exp == EXT_EXP_INFNAN && + (u.extu_ext.ext_frach & 0x80000000) != 0 && + (u.extu_ext.ext_frach != 0x80000000 || u.extu_ext.ext_fracl != 0)); +} diff --git a/lib/nbsd_libc/arch/x86_64/gen/makecontext.c b/lib/nbsd_libc/arch/x86_64/gen/makecontext.c new file mode 100644 index 000000000..e68976105 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/makecontext.c @@ -0,0 +1,97 @@ +/* $NetBSD: makecontext.c,v 1.4 2009/07/30 20:57:17 dsl 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. + * + * Modified from the i386 version for x86_64 by fvdl@wasabisystems.com. + * + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.4 2009/07/30 20:57:17 dsl Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + uintptr_t *sp; + va_list ap; + int stackargs, i; + + stackargs = argc - 6; + + /* LINTED __greg_t is safe */ + gr[_REG_RIP] = (__greg_t)func; + + /* LINTED uintptr_t is safe */ + sp = (uintptr_t *) + ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + + /* LINTED uintptr_t is safe */ + sp = (uintptr_t *)(((uintptr_t)sp & ~15)); + sp--; + if (stackargs > 0) + sp -= stackargs; + /* LINTED __greg_t is safe */ + gr[_REG_RSP] = (__greg_t)sp; + gr[_REG_RBP] = (__greg_t)0; /* Wipe out frame pointer. */ + + /* Put return address on top of stack. */ + /* LINTED uintptr_t is safe */ + *sp++ = (uintptr_t)_resumecontext; + + /* + * Construct argument list. + * The registers used to pass the first 6 arguments + * (rdi, rsi, rdx, rcx, r8, r9) are the first 6 in gregs, + * in that order, so those arguments can just be copied to + * the gregs array. + */ + va_start(ap, argc); + for (i = 0; i < 6 && argc > 0; i++) { + argc--; + /* LINTED __greg_t is safe */ + gr[i] = va_arg(ap, __greg_t); + } + + while (stackargs-- > 0) { + /* LINTED uintptr_t is safe */ + *sp++ = va_arg(ap, uintptr_t); + } + + va_end(ap); +} diff --git a/lib/nbsd_libc/arch/x86_64/gen/nanf.c b/lib/nbsd_libc/arch/x86_64/gen/nanf.c new file mode 100644 index 000000000..ffc388eba --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/nanf.c @@ -0,0 +1,15 @@ +/* $NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nanf.c,v 1.4 2009/02/22 01:34:02 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* bytes for quiet NaN (IEEE single precision) */ +const union __float_u __nanf = + { { 0, 0, 0xc0, 0x7f } }; + +__warn_references(__nanf, "warning: defines NAN incorrectly for your compiler.") diff --git a/lib/nbsd_libc/arch/x86_64/gen/resumecontext.S b/lib/nbsd_libc/arch/x86_64/gen/resumecontext.S new file mode 100644 index 000000000..693169fe7 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/resumecontext.S @@ -0,0 +1,79 @@ +/* $NetBSD: resumecontext.S,v 1.4 2008/04/28 20:22:58 martin 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. + * + * Modified for x86-64 by fvdl@wasabisystems.com + */ + +#include +#include "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: resumecontext.S,v 1.4 2008/04/28 20:22:58 martin Exp $") +#endif /* LIBC_SCCS && !lint */ + +/* + * This assembly-language implementation differs from the (obvious) + * C-language implementation only in not clobbering the previous + * function's return address (us), which is the point of the exercise. + */ + +NENTRY(_resumecontext) /* profiling prologue would clobber TOS */ + leaq -(8 + 784)(%rsp),%rdi /* retaddr + sizeof (ucontext_t) */ + andq $~15,%rdi /* align on _UC_UCONTEXT_ALIGN */ + movq %rdi,%rsp +#ifdef PIC + call PIC_PLT(_C_LABEL(_getcontext)) +#else + call _C_LABEL(_getcontext) +#endif + movq 8(%rsp),%rdi /* uc_link */ + testq %rdi,%rdi /* link end? */ + jnz 9f + + /* normal exit */ +#ifdef PIC + call PIC_PLT(_C_LABEL(exit)) +#else + call _C_LABEL(exit) +#endif + +9: +#ifdef PIC + call PIC_PLT(_C_LABEL(setcontext)) +#else + call _C_LABEL(setcontext) +#endif + /* NOTREACHED */ + + /* something is wrong, pull the brake */ + movq $-1,%rdi + movq $SYS_exit,%rax + syscall + /* NOTREACHED */ diff --git a/lib/nbsd_libc/arch/x86_64/gen/signbitl.c b/lib/nbsd_libc/arch/x86_64/gen/signbitl.c new file mode 100644 index 000000000..7f9f2edc1 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/signbitl.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitl.c,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: signbitl.c,v 1.2 2008/04/28 20:22:58 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * IEEE 754 compatible 80-bit extended-precision Intel 386 version + */ +int +__signbitl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_sign == 1); +} diff --git a/lib/nbsd_libc/arch/x86_64/gen/swapcontext.S b/lib/nbsd_libc/arch/x86_64/gen/swapcontext.S new file mode 100644 index 000000000..49744bf07 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/gen/swapcontext.S @@ -0,0 +1,73 @@ +/* $NetBSD: swapcontext.S,v 1.4 2007/04/12 12:33:26 pooka Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.4 2007/04/12 12:33:26 pooka Exp $") +#endif /* LIBC_SCCS && !lint */ + +/* + * 56 == offsetof(ucontext_t, uc_mcontext) + * 21 == _REG_RIP + * 24 == _REG_URSP + */ + +ENTRY(swapcontext) + pushq %rdi /* preserve oucp */ + pushq %rsi /* preserve ucp */ +#ifdef PIC + call PIC_PLT(_C_LABEL(_getcontext)) +#else + call _C_LABEL(_getcontext) +#endif + popq %rsi + popq %rdi + testl %eax,%eax + jnz 2f + movq 0(%rsp),%r11 + movq %r11,(56 + 21 * 8)(%rdi) + leaq 8(%rsp),%r11 + movq %r11,(56 + 24 * 8)(%rdi) + movq %rsi,%rdi +#ifdef PIC + call PIC_PLT(_C_LABEL(setcontext)) +#else + call _C_LABEL(setcontext) +#endif +2: + ret diff --git a/lib/nbsd_libc/arch/x86_64/net/Makefile.inc b/lib/nbsd_libc/arch/x86_64/net/Makefile.inc new file mode 100644 index 000000000..6c0eeab3b --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/net/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.3 2009/12/06 07:12:18 uebayasi Exp $ + +# objects built from assembler sources (need lint stubs) +# hton* and nto* functions provided by ../gen/byte_swap_*.S +SRCS+= + +LSRCS.x86_64.net=Lint_htonl.c Lint_htons.c Lint_ntohl.c Lint_ntohs.c +LSRCS+= ${LSRCS.x86_64.net} +DPSRCS+= ${LSRCS.x86_64.net} +CLEANFILES+= ${LSRCS.x86_64.net} diff --git a/lib/nbsd_libc/arch/x86_64/stdlib/Makefile.inc b/lib/nbsd_libc/arch/x86_64/stdlib/Makefile.inc new file mode 100644 index 000000000..8737f1b11 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/stdlib/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.6 2009/08/11 17:30:43 dsl Exp $ + +# objects built from assembler sources +SRCS+= abs.S div.S labs.S ldiv.S +NO_SRCS+= llabs.S imaxabs.S imaxdiv.S diff --git a/lib/nbsd_libc/arch/x86_64/stdlib/abs.S b/lib/nbsd_libc/arch/x86_64/stdlib/abs.S new file mode 100644 index 000000000..8a454dcc6 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/stdlib/abs.S @@ -0,0 +1,47 @@ +/* $NetBSD: abs.S,v 1.2 2003/08/07 16:42:36 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)abs.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: abs.S,v 1.2 2003/08/07 16:42:36 agc Exp $") +#endif + +ENTRY(abs) + movl %edi,%eax + testl %eax,%eax + jns 1f + negl %eax +1: ret diff --git a/lib/nbsd_libc/arch/x86_64/stdlib/div.S b/lib/nbsd_libc/arch/x86_64/stdlib/div.S new file mode 100644 index 000000000..5e98cc569 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/stdlib/div.S @@ -0,0 +1,19 @@ +/* $NetBSD: div.S,v 1.1 2001/06/19 00:25:04 fvdl Exp $ */ +/* + * Written by Frank van der Linden (fvdl@wasabisystems.com) + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) +RCSID("$NetBSD: div.S,v 1.1 2001/06/19 00:25:04 fvdl Exp $") +#endif + +ENTRY(div) + movl %edi, %eax + cltd + idivl %esi + salq $32, %rdx + orq %rdx,%rax + ret diff --git a/lib/nbsd_libc/arch/x86_64/stdlib/labs.S b/lib/nbsd_libc/arch/x86_64/stdlib/labs.S new file mode 100644 index 000000000..ffc8f2bb9 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/stdlib/labs.S @@ -0,0 +1,31 @@ +/* $NetBSD: labs.S,v 1.2 2008/08/04 20:21:26 matt Exp $ */ + +/* + * Written by Frank van der Linden (fvdl@wasabisystems.com) + * Public Domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: labs.S,v 1.2 2008/08/04 20:21:26 matt Exp $") +#endif + +#ifdef WEAK_ALIAS +WEAK_ALIAS(imaxabs, _llabs) +WEAK_ALIAS(llabs, _llabs) +WEAK_ALIAS(labs, _labs) +#endif + +#ifdef WEAK_ALIAS +ENTRY(_llabs) +ENTRY(_labs) +#else +ENTRY(llabs) +ENTRY(labs) +#endif + movq %rdi,%rax + testq %rax,%rax + jns 1f + negq %rax +1: ret diff --git a/lib/nbsd_libc/arch/x86_64/stdlib/ldiv.S b/lib/nbsd_libc/arch/x86_64/stdlib/ldiv.S new file mode 100644 index 000000000..ba62fdfaa --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/stdlib/ldiv.S @@ -0,0 +1,26 @@ +/* $NetBSD: ldiv.S,v 1.2 2008/08/04 20:26:38 matt Exp $ */ + +/* + * Written by gcc 3.0. + * Copy/pasted by Frank van der Linden (fvdl@wasabisystems.com) + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: ldiv.S,v 1.2 2008/08/04 20:26:38 matt Exp $") +#endif + +#ifdef WEAK_ALIAS +WEAK_ALIAS(ldiv, _ldiv) +#endif + +#ifdef WEAK_ALIAS +ENTRY(_ldiv) +#else +ENTRY(ldiv) +#endif + movq %rdi, %rax + cqto + idivq %rsi + ret diff --git a/lib/nbsd_libc/arch/x86_64/string/Makefile.inc b/lib/nbsd_libc/arch/x86_64/string/Makefile.inc new file mode 100644 index 000000000..ba5d077d2 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/string/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/07/30 20:57:17 dsl Exp $ + +# objects built from assembler sources +SRCS+= bcmp.S bcopy.S bzero.S ffs.S memchr.S memcmp.S \ + memcpy.S memmove.S memset.S strcat.S strchr.S \ + strcmp.S strcpy.S strlen.S strncmp.S strrchr.S swab.S diff --git a/lib/nbsd_libc/arch/x86_64/string/strncmp.S b/lib/nbsd_libc/arch/x86_64/string/strncmp.S new file mode 100644 index 000000000..7e1db1067 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/string/strncmp.S @@ -0,0 +1,107 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: strncmp.S,v 1.3 2005/08/03 22:59:50 rpaulo Exp $") +#endif + +/* + * NOTE: I've unrolled the loop eight times: large enough to make a + * significant difference, and small enough not to totally trash the + * cache. + */ + +ENTRY(strncmp) + testq %rdx,%rdx + jmp L2 /* Jump into the loop! */ + +L1: incq %rdi + incq %rsi + decq %rdx +L2: jz L4 /* strings are equal */ + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + jne L3 + + incq %rdi + incq %rsi + decq %rdx + jz L4 + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + jne L3 + + incq %rdi + incq %rsi + decq %rdx + jz L4 + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + jne L3 + + incq %rdi + incq %rsi + decq %rdx + jz L4 + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + jne L3 + + incq %rdi + incq %rsi + decq %rdx + jz L4 + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + jne L3 + + incq %rdi + incq %rsi + decq %rdx + jz L4 + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + jne L3 + + incq %rdi + incq %rsi + decq %rdx + jz L4 + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + jne L3 + + incq %rdi + incq %rsi + decq %rdx + jz L4 + movb (%rdi),%al + testb %al,%al + jz L3 + cmpb %al,(%rsi) + je L1 + +L3: movzbl (%rdi),%eax /* unsigned comparison */ + movzbl (%rsi),%ecx + subl %ecx,%eax + ret +L4: xorl %eax,%eax + ret diff --git a/lib/nbsd_libc/arch/x86_64/string/swab.S b/lib/nbsd_libc/arch/x86_64/string/swab.S new file mode 100644 index 000000000..a9012d821 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/string/swab.S @@ -0,0 +1,46 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: swab.S,v 1.3 2005/08/02 14:20:49 rpaulo Exp $") +#endif + +#define LOAD_SWAP_STORE_WORD \ + lodsw ; \ + xchgb %al,%ah ; \ + stosw + +ENTRY(swab) + xchgq %rdi,%rsi + cld # set direction forward + + shrq $1,%rdx + testq $7,%rdx # copy first group of 1 to 7 words + jz L2 # while swapping alternate bytes. +L1: lodsw + rorw $8,%ax + stosw + decq %rdx + testq $7,%rdx + jnz L1 + +L2: shrq $3,%rdx # copy remainder 8 words at a time + jz L4 # while swapping alternate bytes. +L3: + LOAD_SWAP_STORE_WORD + LOAD_SWAP_STORE_WORD + LOAD_SWAP_STORE_WORD + LOAD_SWAP_STORE_WORD + LOAD_SWAP_STORE_WORD + LOAD_SWAP_STORE_WORD + LOAD_SWAP_STORE_WORD + LOAD_SWAP_STORE_WORD + + decq %rdx + jnz L3 +L4: + ret diff --git a/lib/nbsd_libc/arch/x86_64/sys/__clone.S b/lib/nbsd_libc/arch/x86_64/sys/__clone.S new file mode 100644 index 000000000..76d687425 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/__clone.S @@ -0,0 +1,101 @@ +/* $NetBSD: __clone.S,v 1.2 2002/06/06 20:51:17 fvdl Exp $ */ + +/* + * Copyright (c) 2002 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(clone, __clone) +#endif + .text + +/* + * int clone(int (*fn)(void *), void *stack, int flags, void *arg); + */ +ENTRY(__clone) + pushq %r12 + pushq %r13 + /* + * Sanity checks: func and stack may not be NULL. + */ + testq %rdi,%rdi + je 3f + testq %rsi,%rsi + je 3f + + + movq %rdi,%r12 + movq %rcx,%r13 + + movq %rdx,%rdi + + pushq $0 /* dummy return address */ + + SYSTRAP(__clone) + jc 4f + cmpl $0,%eax + jne 2f /* we're the parent */ + movq %r13,%rdi /* restore argument */ + call *%r12 /* this is the clone, call the function */ + + movq %rax,%rdi +#ifdef PIC + call PIC_PLT(_C_LABEL(_exit)) +#else + call _C_LABEL(_exit) +#endif + +2: + addq $8,%rsp + popq %r13 + popq %r12 + ret +3: + movl $EINVAL,%eax + jmp 5f +4: + addq $8,%rsp +5: + popq %r13 + popq %r12 +#ifdef PIC + movq PIC_GOT(CERROR),%rdx + jmp *%rdx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/x86_64/sys/__sigaction14_sigtramp.c b/lib/nbsd_libc/arch/x86_64/sys/__sigaction14_sigtramp.c new file mode 100644 index 000000000..fc022ff58 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/__sigaction14_sigtramp.c @@ -0,0 +1,65 @@ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.10 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __sigaction14_sigtramp.c,v 1.10 2008/04/28 20:22:58 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include "extern.h" + + __weak_alias(__sigaction14, __libc_sigaction14) + +int +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +{ + extern const int __sigtramp_siginfo_2[]; + +#if 0 + /* + * If no sigaction, use the "default" trampoline since it won't + * be used. + */ + if (act == NULL) + return __sigaction_sigtramp(sig, act, oact, NULL, 0); +#endif + + /* + * Don't provide sigcontext compatibility, + * always use the siginfo trampoline. + */ + return __sigaction_sigtramp(sig, act, oact, __sigtramp_siginfo_2, 2); +} diff --git a/lib/nbsd_libc/arch/x86_64/sys/__sigtramp2.S b/lib/nbsd_libc/arch/x86_64/sys/__sigtramp2.S new file mode 100644 index 000000000..e0489cc71 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/__sigtramp2.S @@ -0,0 +1,50 @@ +/* $NetBSD: __sigtramp2.S,v 1.5 2006/11/08 21:16:40 drochner Exp $ */ + +/* + * Copyright (c) 2001 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 "SYS.h" + +/* + * The x86-64 signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + */ +NENTRY(__sigtramp_siginfo_2) + movq %r15,%rdi + movq $SYS_setcontext, %rax + syscall + movq $-1,%rdi /* if we return here, something is wrong */ + movq $SYS_exit, %rax + syscall diff --git a/lib/nbsd_libc/arch/x86_64/sys/__syscall.S b/lib/nbsd_libc/arch/x86_64/sys/__syscall.S new file mode 100644 index 000000000..9550377ef --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/__syscall.S @@ -0,0 +1,44 @@ +/* $NetBSD: __syscall.S,v 1.2 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __syscall.S,v 1.2 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +RSYSCALL(__syscall) diff --git a/lib/nbsd_libc/arch/x86_64/sys/__vfork14.S b/lib/nbsd_libc/arch/x86_64/sys/__vfork14.S new file mode 100644 index 000000000..bbd682164 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/__vfork14.S @@ -0,0 +1,65 @@ +/* $NetBSD: __vfork14.S,v 1.3 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: __vfork14.S,v 1.3 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * pid = vfork(); + * + * %edx == 0 in parent process, %edx == 1 in child process. + * %eax == pid of child in parent, %eax == pid of parent in child. + * + */ +ENTRY(__vfork14) + popq %r9 /* my rta into r9 */ + SYSTRAP(__vfork14) + jc err + decl %edx + andl %edx,%eax + jmp *%r9 +err: + pushq %r9 +#ifdef PIC + movq PIC_GOT(CERROR), %rcx + jmp *%rcx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/x86_64/sys/brk.S b/lib/nbsd_libc/arch/x86_64/sys/brk.S new file mode 100644 index 000000000..7d969e9d7 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/brk.S @@ -0,0 +1,85 @@ +/* $NetBSD: brk.S,v 1.3 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)brk.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: brk.S,v 1.3 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _end + .globl _C_LABEL(__minbrk) + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(brk, _brk) +#endif + + .data +_C_LABEL(__minbrk): + .quad _end + .text + +ENTRY(_brk) +#ifdef PIC + movq PIC_GOT(_C_LABEL(__minbrk)),%rdx + cmpq %rdi,(%rdx) + jb 1f + movq (%rdx),%rdi +1: + SYSTRAP(break) + jc err + movq PIC_GOT(CURBRK),%rdx # set up GOT addressing + movq %rdi,(%rdx) + xorl %eax,%eax + ret +err: + movq PIC_GOT(CERROR),%rdx + jmp *%rdx +#else + cmpq %rdi,_C_LABEL(__minbrk)(%rip) + jb 1f + movq _C_LABEL(__minbrk)(%rip),%rdi +1: + SYSTRAP(break) + jc err + movq %rdi,CURBRK(%rip) + xorl %eax,%eax + ret +err: + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/x86_64/sys/cerror.S b/lib/nbsd_libc/arch/x86_64/sys/cerror.S new file mode 100644 index 000000000..6c06fa4b5 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/cerror.S @@ -0,0 +1,58 @@ +/* $NetBSD: cerror.S,v 1.3 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)cerror.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: cerror.S,v 1.3 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _C_LABEL(__errno) + +_ENTRY(CERROR) + pushq %r12 + movl %eax,%edi + movl %eax,%r12d +#ifdef PIC + call PIC_PLT(_C_LABEL(__errno)) +#else + call _C_LABEL(__errno) +#endif /* PIC */ + movl %r12d,(%rax) + movq $-1,%rax + popq %r12 + ret diff --git a/lib/nbsd_libc/arch/x86_64/sys/exect.S b/lib/nbsd_libc/arch/x86_64/sys/exect.S new file mode 100644 index 000000000..9f32e5e5c --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/exect.S @@ -0,0 +1,55 @@ +/* $NetBSD: exect.S,v 1.2 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)exect.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: exect.S,v 1.2 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" +#include + +ENTRY(exect) + pushfq + orb $(PSL_T>>8),1(%rsp) + popfq + SYSTRAP(execve) +#ifdef PIC + movq PIC_GOT(CERROR), %rcx + jmp *%rcx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/x86_64/sys/fork.S b/lib/nbsd_libc/arch/x86_64/sys/fork.S new file mode 100644 index 000000000..00a64c40e --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/fork.S @@ -0,0 +1,47 @@ +/* $NetBSD: fork.S,v 1.3 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)fork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: fork.S,v 1.3 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +_SYSCALL(__fork,fork) + decl %edx /* from 1 to 0 in child, 0 to -1 in parent */ + andl %edx,%eax + ret /* pid = fork(); */ diff --git a/lib/nbsd_libc/arch/x86_64/sys/getcontext.S b/lib/nbsd_libc/arch/x86_64/sys/getcontext.S new file mode 100644 index 000000000..bb5ea32fc --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/getcontext.S @@ -0,0 +1,63 @@ +/* $NetBSD: getcontext.S,v 1.3 2004/03/09 20:21:22 drochner Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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 "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.3 2004/03/09 20:21:22 drochner Exp $") +#endif + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +/* + * 56 == offsetof(ucontext_t, uc_mcontext) + * 14 == _REG_RAX + * 21 == _REG_RIP + * 24 == _REG_URSP + */ + +_SYSCALL(_getcontext,getcontext) + movq (%rsp),%r11 + movq %r11,(56 + 21 * 8)(%rdi) + leaq 8(%rsp),%r11 + movq %r11,(56 + 24 * 8)(%rdi) + movq $0,(56 + 14 * 8)(%rdi) + xorl %eax,%eax + ret diff --git a/lib/nbsd_libc/arch/x86_64/sys/pipe.S b/lib/nbsd_libc/arch/x86_64/sys/pipe.S new file mode 100644 index 000000000..0d21c4e7c --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/pipe.S @@ -0,0 +1,52 @@ +/* $NetBSD: pipe.S,v 1.2 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)pipe.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: pipe.S,v 1.2 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +#ifdef WEAK_ALIAS +WEAK_ALIAS(pipe, _pipe) +#endif + +_SYSCALL(_pipe,pipe) + movl %eax,(%rdi) + movl %edx,4(%rdi) + xorl %eax,%eax + ret diff --git a/lib/nbsd_libc/arch/x86_64/sys/ptrace.S b/lib/nbsd_libc/arch/x86_64/sys/ptrace.S new file mode 100644 index 000000000..8b7cc33ae --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/ptrace.S @@ -0,0 +1,62 @@ +/* $NetBSD: ptrace.S,v 1.2 2003/08/07 16:42:37 agc Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)ptrace.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: ptrace.S,v 1.2 2003/08/07 16:42:37 agc Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _C_LABEL(__errno) + +ENTRY(ptrace) +#ifdef PIC + call PIC_PLT(_C_LABEL(__errno)) +#else + call _C_LABEL(__errno) +#endif /* PIC */ + movl $0,(%rax) + SYSTRAP(ptrace) + jc err + ret +err: +#ifdef PIC + movq PIC_GOT(CERROR), %rcx + jmp *%rcx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/x86_64/sys/sbrk.S b/lib/nbsd_libc/arch/x86_64/sys/sbrk.S new file mode 100644 index 000000000..65c33625b --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/sbrk.S @@ -0,0 +1,86 @@ +/* $NetBSD: sbrk.S,v 1.3 2008/07/02 20:07:43 rmind Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sbrk.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: sbrk.S,v 1.3 2008/07/02 20:07:43 rmind Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + + .globl _end + .globl CURBRK + +#ifdef WEAK_ALIAS +WEAK_ALIAS(sbrk, _sbrk) +#endif + + .data +CURBRK: .quad _end + .text + +ENTRY(_sbrk) +#ifdef PIC + movq PIC_GOT(CURBRK),%rdx + movq (%rdx),%rax + test %rdi,%rdi + jz out + addq %rax,%rdi + SYSTRAP(break) + jc err + movq PIC_GOT(CURBRK),%rdx + movq (%rdx),%rax + movq %rdi,(%rdx) +out: + ret +err: + mov PIC_GOT(CERROR),%rdx + jmp *%rdx +#else + movq CURBRK(%rip),%rax + test %rdi,%rdi + jz out + movq %rdi,%rsi + addq %rax,%rdi + SYSTRAP(break) + jc err + movq CURBRK(%rip),%rax + addq %rsi,CURBRK(%rip) +out: + ret +err: + jmp CERROR +#endif diff --git a/lib/nbsd_libc/arch/x86_64/sys/shmat.S b/lib/nbsd_libc/arch/x86_64/sys/shmat.S new file mode 100644 index 000000000..2c81105e3 --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/shmat.S @@ -0,0 +1,5 @@ +/* $NetBSD: shmat.S,v 1.1 2001/06/19 00:25:06 fvdl Exp $ */ + +#include "SYS.h" + +RSYSCALL(shmat) diff --git a/lib/nbsd_libc/arch/x86_64/sys/syscall.S b/lib/nbsd_libc/arch/x86_64/sys/syscall.S new file mode 100644 index 000000000..e802b61fb --- /dev/null +++ b/lib/nbsd_libc/arch/x86_64/sys/syscall.S @@ -0,0 +1,44 @@ +/* $NetBSD: syscall.S,v 1.4 2007/11/05 20:30:07 dsl Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)syscall.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: syscall.S,v 1.4 2007/11/05 20:30:07 dsl Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +RSYSCALL(syscall) diff --git a/lib/nbsd_libc/atomic/Makefile.inc b/lib/nbsd_libc/atomic/Makefile.inc new file mode 100644 index 000000000..80951a7c6 --- /dev/null +++ b/lib/nbsd_libc/atomic/Makefile.inc @@ -0,0 +1,76 @@ +# $NetBSD: Makefile.inc,v 1.2 2010/02/12 22:34:38 dyoung Exp $ +# from: @(#)Makefile.inc 8.6 (Berkeley) 5/4/95 + +# gen sources +.PATH: ${.CURDIR}/atomic + +MAN+= atomic_add.3 atomic_and.3 atomic_cas.3 atomic_dec.3 \ + atomic_inc.3 atomic_ops.3 atomic_or.3 atomic_swap.3 \ + membar_ops.3 + +MLINKS+=atomic_add.3 atomic_add_32.3 \ + atomic_add.3 atomic_add_int.3 \ + atomic_add.3 atomic_add_long.3 \ + atomic_add.3 atomic_add_ptr.3 \ + atomic_add.3 atomic_add_64.3 \ + atomic_add.3 atomic_add_32_nv.3 \ + atomic_add.3 atomic_add_int_nv.3 \ + atomic_add.3 atomic_add_long_nv.3 \ + atomic_add.3 atomic_add_ptr_nv.3 \ + atomic_add.3 atomic_add_64_nv.3 \ + atomic_and.3 atomic_and_32.3 \ + atomic_and.3 atomic_and_uint.3 \ + atomic_and.3 atomic_and_ulong.3 \ + atomic_and.3 atomic_and_64.3 \ + atomic_and.3 atomic_and_32_nv.3 \ + atomic_and.3 atomic_and_uint_nv.3 \ + atomic_and.3 atomic_and_ulong_nv.3 \ + atomic_and.3 atomic_and_64_nv.3 \ + atomic_cas.3 atomic_cas_32.3 \ + atomic_cas.3 atomic_cas_uint.3 \ + atomic_cas.3 atomic_cas_ulong.3 \ + atomic_cas.3 atomic_cas_ptr.3 \ + atomic_cas.3 atomic_cas_64.3 \ + atomic_cas.3 atomic_cas_32_ni.3 \ + atomic_cas.3 atomic_cas_uint_ni.3 \ + atomic_cas.3 atomic_cas_ulong_ni.3 \ + atomic_cas.3 atomic_cas_ptr_ni.3 \ + atomic_cas.3 atomic_cas_64_ni.3 \ + atomic_dec.3 atomic_dec_32.3 \ + atomic_dec.3 atomic_dec_uint.3 \ + atomic_dec.3 atomic_dec_ulong.3 \ + atomic_dec.3 atomic_dec_ptr.3 \ + atomic_dec.3 atomic_dec_64.3 \ + atomic_dec.3 atomic_dec_32_nv.3 \ + atomic_dec.3 atomic_dec_uint_nv.3 \ + atomic_dec.3 atomic_dec_ulong_nv.3 \ + atomic_dec.3 atomic_dec_ptr_nv.3 \ + atomic_dec.3 atomic_dec_64_nv.3 \ + atomic_inc.3 atomic_inc_32.3 \ + atomic_inc.3 atomic_inc_uint.3 \ + atomic_inc.3 atomic_inc_ulong.3 \ + atomic_inc.3 atomic_inc_ptr.3 \ + atomic_inc.3 atomic_inc_64.3 \ + atomic_inc.3 atomic_inc_32_nv.3 \ + atomic_inc.3 atomic_inc_uint_nv.3 \ + atomic_inc.3 atomic_inc_ulong_nv.3 \ + atomic_inc.3 atomic_inc_ptr_nv.3 \ + atomic_inc.3 atomic_inc_64_nv.3 \ + atomic_or.3 atomic_or_32.3 \ + atomic_or.3 atomic_or_uint.3 \ + atomic_or.3 atomic_or_ulong.3 \ + atomic_or.3 atomic_or_64.3 \ + atomic_or.3 atomic_or_32_nv.3 \ + atomic_or.3 atomic_or_uint_nv.3 \ + atomic_or.3 atomic_or_ulong_nv.3 \ + atomic_or.3 atomic_or_64_nv.3 \ + atomic_swap.3 atomic_swap_32.3 \ + atomic_swap.3 atomic_swap_uint.3 \ + atomic_swap.3 atomic_swap_ulong.3 \ + atomic_swap.3 atomic_swap_ptr.3 \ + atomic_swap.3 atomic_swap_64.3 \ + membar_ops.3 membar_enter.3 \ + membar_ops.3 membar_exit.3 \ + membar_ops.3 membar_producer.3 \ + membar_ops.3 membar_consumer.3 \ + membar_ops.3 membar_sync.3 diff --git a/lib/nbsd_libc/atomic/atomic_add.3 b/lib/nbsd_libc/atomic/atomic_add.3 new file mode 100644 index 000000000..b6f8adbb4 --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_add.3 @@ -0,0 +1,95 @@ +.\" $NetBSD: atomic_add.3,v 1.1 2008/06/23 10:22:40 ad Exp $ +.\" +.\" Copyright (c) 2007 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 April 11, 2007 +.Dt ATOMIC_ADD 3 +.Os +.Sh NAME +.Nm atomic_add , +.Nm atomic_add_32 , +.Nm atomic_add_int , +.Nm atomic_add_long , +.Nm atomic_add_ptr , +.Nm atomic_add_64 , +.Nm atomic_add_32_nv , +.Nm atomic_add_int_nv , +.Nm atomic_add_long_nv , +.Nm atomic_add_ptr_nv , +.Nm atomic_add_64_nv +.Nd atomic add operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Ft void +.Fn atomic_add_32 "volatile uint32_t *ptr" "int32_t delta" +.Ft void +.Fn atomic_add_int "volatile unsigned int *ptr" "int delta" +.Ft void +.Fn atomic_add_long "volatile unsigned long *ptr" "long delta" +.Ft void +.Fn atomic_add_ptr "volatile void *ptr" "ssize_t delta" +.Ft void +.Fn atomic_add_64 "volatile uint64_t *ptr" "int64_t delta" +.Ft uint32_t +.Fn atomic_add_32_nv "volatile uint32_t *ptr" "int32_t delta" +.Ft unsigned int +.Fn atomic_add_int_nv "volatile unsigned int *ptr" "int delta" +.Ft unsigned long +.Fn atomic_add_long_nv "volatile unsigned long *ptr" "long delta" +.Ft void * +.Fn atomic_add_ptr_nv "volatile void *ptr" "ssize_t delta" +.Ft uint64_t +.Fn atomic_add_64_nv "volatile uint64_t *ptr" "int64_t delta" +.Sh DESCRIPTION +The +.Nm atomic_add +family of functions add a signed value +.Fa delta +to the variable referenced by +.Fa ptr +in an atomic fashion. +.Pp +The +.Fn *_nv +variants of these functions return the new value. +.Pp +The 64-bit variants of these functions are available only on platforms +that can support atomic 64-bit memory access. +Applications can check for the availability of 64-bit atomic memory +operations by testing if the pre-processor macro +.Dv __HAVE_ATOMIC64_OPS +is defined. +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm atomic_add +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/atomic/atomic_and.3 b/lib/nbsd_libc/atomic/atomic_and.3 new file mode 100644 index 000000000..7f5ed50d4 --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_and.3 @@ -0,0 +1,95 @@ +.\" $NetBSD: atomic_and.3,v 1.1 2008/06/23 10:22:40 ad Exp $ +.\" +.\" Copyright (c) 2007 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 April 11, 2007 +.Dt ATOMIC_AND 3 +.Os +.Sh NAME +.Nm atomic_and , +.Nm atomic_and_32 , +.Nm atomic_and_uint , +.Nm atomic_and_ulong , +.Nm atomic_and_64 , +.Nm atomic_and_32_nv , +.Nm atomic_and_uint_nv , +.Nm atomic_and_ulong_nv , +.Nm atomic_and_64_nv +.Nd atomic logical +.Sq and +operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Ft void +.Fn atomic_and_32 "volatile uint32_t *ptr" "uint32_t bits" +.Ft void +.Fn atomic_and_uint "volatile unsigned int *ptr" "unsigned int bits" +.Ft void +.Fn atomic_and_ulong "volatile unsigned long *ptr" "unsigned long bits" +.Ft void +.Fn atomic_and_64 "volatile uint64_t *ptr" "uint64_t bits" +.Ft uint32_t +.Fn atomic_and_32_nv "volatile uint32_t *ptr" "uint32_t bits" +.Ft unsigned int +.Fn atomic_and_uint_nv "volatile unsigned int *ptr" "unsigned int bits" +.Ft unsigned long +.Fn atomic_and_ulong_nv "volatile unsigned long *ptr" "unsigned long bits" +.Ft uint64_t +.Fn atomic_and_64_nv "volatile uint64_t *ptr" "uint64_t bits" +.Sh DESCRIPTION +The +.Nm atomic_and +family of functions load the value of the variable referenced by +.Fa ptr , +perform a logical +.Sq and +with the value +.Fa bits , +and store the result back to the variable referenced by +.Fa ptr +in an atomic fashion. +.Pp +The +.Fn *_nv +variants of these functions return the new value. +.Pp +The 64-bit variants of these functions are available only on platforms +that can support atomic 64-bit memory access. +Applications can check for the availability of 64-bit atomic memory +operations by testing if the pre-processor macro +.Dv __HAVE_ATOMIC64_OPS +is defined. +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm atomic_and +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/atomic/atomic_cas.3 b/lib/nbsd_libc/atomic/atomic_cas.3 new file mode 100644 index 000000000..06804b7fc --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_cas.3 @@ -0,0 +1,129 @@ +.\" $NetBSD: atomic_cas.3,v 1.2 2010/02/12 22:23:17 dyoung Exp $ +.\" +.\" Copyright (c) 2007, 2010 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 February 11, 2010 +.Dt ATOMIC_CAS 3 +.Os +.Sh NAME +.Nm atomic_cas , +.Nm atomic_cas_32 , +.Nm atomic_cas_uint , +.Nm atomic_cas_ulong , +.Nm atomic_cas_ptr , +.Nm atomic_cas_64 , +.Nm atomic_cas_32_ni , +.Nm atomic_cas_uint_ni , +.Nm atomic_cas_ulong_ni , +.Nm atomic_cas_ptr_ni , +.Nm atomic_cas_64_ni +.Nd atomic compare-and-swap operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Ft uint32_t +.Fn atomic_cas_32 "volatile uint32_t *ptr" "uint32_t old" "uint32_t new" +.Ft unsigned int +.Fn atomic_cas_uint "volatile unsigned int *ptr" "unsigned int old" \ + "unsigned int new" +.Ft unsigned long +.Fn atomic_cas_ulong "volatile unsigned long *ptr" "unsigned long old" \ + "unsigned long new" +.Ft void * +.Fn atomic_cas_ptr "volatile void *ptr" "void *old" "void *new" +.Ft uint64_t +.Fn atomic_cas_64 "volatile uint64_t *ptr" "uint64_t old" "uint64_t new" +.Ft uint32_t +.Fn atomic_cas_32_ni "volatile uint32_t *ptr" "uint32_t old" "uint32_t new" +.Ft unsigned int +.Fn atomic_cas_uint_ni "volatile unsigned int *ptr" "unsigned int old" \ + "unsigned int new" +.Ft unsigned long +.Fn atomic_cas_ulong_ni "volatile unsigned long *ptr" "unsigned long old" \ + "unsigned long new" +.Ft void * +.Fn atomic_cas_ptr_ni "volatile void *ptr" "void *old" "void *new" +.Ft uint64_t +.Fn atomic_cas_64_ni "volatile uint64_t *ptr" "uint64_t old" "uint64_t new" +.Sh DESCRIPTION +The +.Nm atomic_cas +family of functions perform a compare-and-swap operation in an atomic fashion. +The value of the variable referenced by +.Fa ptr +is compared against +.Fa old . +If the values are equal, +.Fa new +is stored in the variable referenced by +.Fa ptr . +.Pp +The old value of the variable referenced by +.Fa ptr +is always returned regardless of whether or not the new value was stored. +Applications can test for success of the operation by comparing the +return value to the value passed as +.Fa old ; +if they are equal then the new value was stored. +.Pp +The non-interlocked variants, +.Fn *_ni , +guarantee atomicity within the same CPU with respect to +interrupts and preemption. +For example, they are suitable for synchronizing compare-and-swap +operations on a variable shared by a thread and an interrupt +that are bound to the same CPU. +The +.Fn *_ni +variants are not atomic with respect to different CPUs. +.Fn *_ni +variants should avoid the interprocessor synchronization overhead +of the standard compare-and-swap operations. +.Pp +The 64-bit variants of these functions are available only on platforms +that can support atomic 64-bit memory access. +Applications can check for the availability of 64-bit atomic memory +operations by testing if the pre-processor macro +.Dv __HAVE_ATOMIC64_OPS +is defined. +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm atomic_cas +functions first appeared in +.Nx 5.0 . +.Sh NOTES +On some architectures, a +.Fn *_ni +variant is merely an alias for the corresponding standard +compare-and-swap operation. +While the non-interlocked variant behaves correctly on those +architectures, it does not avoid the interprocessor synchronization +overhead. diff --git a/lib/nbsd_libc/atomic/atomic_dec.3 b/lib/nbsd_libc/atomic/atomic_dec.3 new file mode 100644 index 000000000..654ed24a2 --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_dec.3 @@ -0,0 +1,95 @@ +.\" $NetBSD: atomic_dec.3,v 1.1 2008/06/23 10:22:40 ad Exp $ +.\" +.\" Copyright (c) 2007 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 April 11, 2007 +.Dt ATOMIC_DEC 3 +.Os +.Sh NAME +.Nm atomic_dec , +.Nm atomic_dec_32 , +.Nm atomic_dec_uint , +.Nm atomic_dec_ulong , +.Nm atomic_dec_ptr , +.Nm atomic_dec_64 , +.Nm atomic_dec_32_nv , +.Nm atomic_dec_uint_nv , +.Nm atomic_dec_ulong_nv , +.Nm atomic_dec_ptr_nv , +.Nm atomic_dec_64_nv +.Nd atomic decrement operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Ft void +.Fn atomic_dec_32 "volatile uint32_t *ptr" +.Ft void +.Fn atomic_dec_uint "volatile unsigned int *ptr" +.Ft void +.Fn atomic_dec_ulong "volatile unsigned long *ptr" +.Ft void +.Fn atomic_dec_ptr "volatile void *ptr" +.Ft void +.Fn atomic_dec_64 "volatile uint64_t *ptr" +.Ft uint32_t +.Fn atomic_dec_32_nv "volatile uint32_t *ptr" +.Ft unsigned int +.Fn atomic_dec_uint_nv "volatile unsigned int *ptr" +.Ft unsigned long +.Fn atomic_dec_ulong_nv "volatile unsigned long *ptr" +.Ft void * +.Fn atomic_dec_ptr_nv "volatile void *ptr" +.Ft uint64_t +.Fn atomic_dec_64_nv "volatile uint64_t *ptr" +.Sh DESCRIPTION +The +.Nm atomic_dec +family of functions decrement +.Pq by one +the variable referenced by +.Fa ptr +in an atomic fashion. +.Pp +The +.Fn *_nv +variants of these functions return the new value. +.Pp +The 64-bit variants of these functions are available only on platforms +that can support atomic 64-bit memory access. +Applications can check for the availability of 64-bit atomic memory +operations by testing if the pre-processor macro +.Dv __HAVE_ATOMIC64_OPS +is defined. +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm atomic_dec +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/atomic/atomic_inc.3 b/lib/nbsd_libc/atomic/atomic_inc.3 new file mode 100644 index 000000000..a03429e99 --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_inc.3 @@ -0,0 +1,95 @@ +.\" $NetBSD: atomic_inc.3,v 1.1 2008/06/23 10:22:40 ad Exp $ +.\" +.\" Copyright (c) 2007 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 April 11, 2007 +.Dt ATOMIC_INC 3 +.Os +.Sh NAME +.Nm atomic_inc , +.Nm atomic_inc_32 , +.Nm atomic_inc_uint , +.Nm atomic_inc_ulong , +.Nm atomic_inc_ptr , +.Nm atomic_inc_64 , +.Nm atomic_inc_32_nv , +.Nm atomic_inc_uint_nv , +.Nm atomic_inc_ulong_nv , +.Nm atomic_inc_ptr_nv , +.Nm atomic_inc_64_nv +.Nd atomic increment operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Ft void +.Fn atomic_inc_32 "volatile uint32_t *ptr" +.Ft void +.Fn atomic_inc_uint "volatile unsigned int *ptr" +.Ft void +.Fn atomic_inc_ulong "volatile unsigned long *ptr" +.Ft void +.Fn atomic_inc_ptr "volatile void *ptr" +.Ft void +.Fn atomic_inc_64 "volatile uint64_t *ptr" +.Ft uint32_t +.Fn atomic_inc_32_nv "volatile uint32_t *ptr" +.Ft unsigned int +.Fn atomic_inc_uint_nv "volatile unsigned int *ptr" +.Ft unsigned long +.Fn atomic_inc_ulong_nv "volatile unsigned long *ptr" +.Ft void * +.Fn atomic_inc_ptr_nv "volatile void *ptr" +.Ft uint64_t +.Fn atomic_inc_64_nv "volatile uint64_t *ptr" +.Sh DESCRIPTION +The +.Nm atomic_inc +family of functions increment +.Pq by one +the variable referenced by +.Fa ptr +in an atomic fashion. +.Pp +The +.Fn *_nv +variants of these functions return the new value. +.Pp +The 64-bit variants of these functions are available only on platforms +that can support atomic 64-bit memory access. +Applications can check for the availability of 64-bit atomic memory +operations by testing if the pre-processor macro +.Dv __HAVE_ATOMIC64_OPS +is defined. +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm atomic_inc +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/atomic/atomic_ops.3 b/lib/nbsd_libc/atomic/atomic_ops.3 new file mode 100644 index 000000000..77f6c72ef --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_ops.3 @@ -0,0 +1,129 @@ +.\" $NetBSD: atomic_ops.3,v 1.5 2010/04/14 08:49:49 jruoho Exp $ +.\" +.\" Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 April 14, 2010 +.Dt ATOMIC_OPS 3 +.Os +.Sh NAME +.Nm atomic_ops +.Nd atomic memory operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Sh DESCRIPTION +The +.Nm atomic_ops +family of functions provide atomic memory operations. +There are 7 classes of atomic memory operations available: +.Pp +.Bl -tag -width "atomic_swap(3)" -offset indent +.It Xr atomic_add 3 +These functions perform atomic addition. +.It Xr atomic_and 3 +These functions perform atomic logical +.Dq and . +.It Xr atomic_cas 3 +These functions perform atomic compare-and-swap. +.It Xr atomic_dec 3 +These functions perform atomic decrement. +.It Xr atomic_inc 3 +These functions perform atomic increment. +.It Xr atomic_or 3 +These functions perform atomic logical +.Dq or . +.It Xr atomic_swap 3 +These functions perform atomic swap. +.El +.Ss Synchronization Mechanisms +Where the architecture does not provide hardware support for atomic compare +and swap (CAS), atomicity is provided by a restartable sequence or by a +spinlock. +The chosen method is not ordinarily distinguishable by or visible to users +of the interface. +The following architectures can be assumed to provide CAS in hardware: +alpha, amd64, i386, powerpc, powerpc64, sparc64. +.Ss Scope and Restrictions +If hardware CAS is available, the atomic operations are globally atomic: +operations within a memory region shared between processes are +guaranteed to be performed atomically. +If hardware CAS is not available, it may only be assumed that the operations +are atomic with respect to threads in the same process. +Additionally, if hardware CAS is not available, the atomic operations must +not be used within a signal handler. +.Pp +Users of atomic memory operations should not make assumptions about how +the memory access is performed +.Pq specifically, the width of the memory access . +For this reason, applications making use of atomic memory operations should +limit their use to regular memory. +The results of using atomic memory operations on anything other than +regular memory are undefined. +.Pp +Users of atomic memory operations should take care to modify any given +memory location either entirely with atomic operations or entirely with +some other synchronization mechanism. +Intermixing of atomic operations with other synchronization mechanisms +for the same memory location results in undefined behavior. +.Ss Visibility and Ordering of Memory Accesses +If hardware CAS is available, stores to the target memory location by an +atomic operation will reach global visibility before the operation +completes. +If hardware CAS is not available, the store may not reach global visibility +until some time after the atomic operation has completed. +However, in all cases a subsequent atomic operation on the same memory cell +will be delayed until the result of any preceeding operation has reached +global visibility. +.Pp +Atomic operations are strongly ordered with respect to each other. +The global visibility of other loads and stores before and after an atomic +operation is undefined. +Applications that require synchronization of loads and stores with respect +to an atomic operation must use memory barriers. +See +.Xr membar_ops 3 . +.Ss Performance +Because atomic memory operations require expensive synchronization at the +hardware level, applications should take care to minimize their use. +In certain cases, it may be more appropriate to use a mutex, especially +if more than one memory location will be modified. +.Sh SEE ALSO +.Xr atomic_add 3 , +.Xr atomic_and 3 , +.Xr atomic_cas 3 , +.Xr atomic_dec 3 , +.Xr atomic_inc 3 , +.Xr atomic_or 3 , +.Xr atomic_swap 3 , +.Xr membar_ops 3 +.Sh HISTORY +The +.Nm atomic_ops +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/atomic/atomic_or.3 b/lib/nbsd_libc/atomic/atomic_or.3 new file mode 100644 index 000000000..903f5b02c --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_or.3 @@ -0,0 +1,95 @@ +.\" $NetBSD: atomic_or.3,v 1.1 2008/06/23 10:22:40 ad Exp $ +.\" +.\" Copyright (c) 2007 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 April 11, 2007 +.Dt ATOMIC_OR 3 +.Os +.Sh NAME +.Nm atomic_or , +.Nm atomic_or_32 , +.Nm atomic_or_uint , +.Nm atomic_or_ulong , +.Nm atomic_or_64 , +.Nm atomic_or_32_nv , +.Nm atomic_or_uint_nv , +.Nm atomic_or_ulong_nv , +.Nm atomic_or_64_nv +.Nd atomic logical +.Sq or +operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Ft void +.Fn atomic_or_32 "volatile uint32_t *ptr" "uint32_t bits" +.Ft void +.Fn atomic_or_uint "volatile unsigned int *ptr" "unsigned int bits" +.Ft void +.Fn atomic_or_ulong "volatile unsigned long *ptr" "unsigned long bits" +.Ft void +.Fn atomic_or_64 "volatile uint64_t *ptr" "uint64_t bits" +.Ft uint32_t +.Fn atomic_or_32_nv "volatile uint32_t *ptr" "uint32_t bits" +.Ft unsigned int +.Fn atomic_or_uint_nv "volatile unsigned int *ptr" "unsigned int bits" +.Ft unsigned long +.Fn atomic_or_ulong_nv "volatile unsigned long *ptr" "unsigned long bits" +.Ft uint64_t +.Fn atomic_or_64_nv "volatile uint64_t *ptr" "uint64_t bits" +.Sh DESCRIPTION +The +.Nm atomic_or +family of functions load the value of the variable referenced by +.Fa ptr , +perform a logical +.Sq or +with the value +.Fa bits , +and store the result back to the variable referenced by +.Fa ptr +in an atomic fashion. +.Pp +The +.Fn *_nv +variants of these functions return the new value. +.Pp +The 64-bit variants of these functions are available only on platforms +that can support atomic 64-bit memory access. +Applications can check for the availability of 64-bit atomic memory +operations by testing if the pre-processor macro +.Dv __HAVE_ATOMIC64_OPS +is defined. +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm atomic_or +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/atomic/atomic_swap.3 b/lib/nbsd_libc/atomic/atomic_swap.3 new file mode 100644 index 000000000..274365250 --- /dev/null +++ b/lib/nbsd_libc/atomic/atomic_swap.3 @@ -0,0 +1,77 @@ +.\" $NetBSD: atomic_swap.3,v 1.1 2008/06/23 10:22:40 ad Exp $ +.\" +.\" Copyright (c) 2007 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 April 11, 2007 +.Dt ATOMIC_SWAP 3 +.Os +.Sh NAME +.Nm atomic_swap , +.Nm atomic_swap_32 , +.Nm atomic_swap_uint , +.Nm atomic_swap_ulong , +.Nm atomic_swap_ptr , +.Nm atomic_swap_64 +.Nd atomic swap operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.Ft uint32_t +.Fn atomic_swap_32 "volatile uint32_t *ptr" "uint32_t new" +.Ft unsigned int +.Fn atomic_swap_uint "volatile unsigned int *ptr" "unsigned int new" +.Ft unsigned long +.Fn atomic_swap_ulong "volatile unsigned long *ptr" "unsigned long new" +.Ft void * +.Fn atomic_swap_ptr "volatile void *ptr" "void *new" +.Ft uint64_t +.Fn atomic_swap_64 "volatile uint64_t *ptr" "uint64_t new" +.Sh DESCRIPTION +The +.Nm atomic_swap +family of functions perform a swap operation in an atomic fashion. +The value of the variable referenced by +.Fa ptr +is replaced by +.Fa new +and the old value returned. +.Pp +The 64-bit variants of these functions are available only on platforms +that can support atomic 64-bit memory access. +Applications can check for the availability of 64-bit atomic memory +operations by testing if the pre-processor macro +.Dv __HAVE_ATOMIC64_OPS +is defined. +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm atomic_swap +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/atomic/membar_ops.3 b/lib/nbsd_libc/atomic/membar_ops.3 new file mode 100644 index 000000000..125d696b4 --- /dev/null +++ b/lib/nbsd_libc/atomic/membar_ops.3 @@ -0,0 +1,97 @@ +.\" $NetBSD: membar_ops.3,v 1.2 2009/05/18 12:39:17 wiz Exp $ +.\" +.\" Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe. +.\" +.\" 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 February 11, 2007 +.Dt MEMBAR_OPS 3 +.Os +.Sh NAME +.Nm membar_ops , +.Nm membar_enter , +.Nm membar_exit , +.Nm membar_producer , +.Nm membar_consumer , +.Nm membar_sync +.Nd memory access barrier operations +.\" .Sh LIBRARY +.\" .Lb libc +.Sh SYNOPSIS +.In sys/atomic.h +.\" +.Ft void +.Fn membar_enter "void" +.Ft void +.Fn membar_exit "void" +.Ft void +.Fn membar_producer "void" +.Ft void +.Fn membar_consumer "void" +.Ft void +.Fn membar_sync "void" +.Sh DESCRIPTION +The +.Nm membar_ops +family of functions provide memory access barrier operations necessary +for synchronization in multiprocessor execution environments that have +relaxed load and store order. +.Pp +.Bl -tag -width "mem" +.It Fn membar_enter +Any store preceeding +.Fn membar_enter +will reach global visibility before all loads and stores following it. +.Pp +.Fn membar_enter +is typically used in code that implements locking primitives to ensure +that a lock protects its data. +.It Fn membar_exit +All loads and stores preceding +.Fn membar_exit +will reach global visibility before any store that follows it. +.Pp +.Fn membar_exit +is typically used in code that implements locking primitives to ensure +that a lock protects its data. +.It Fn membar_producer +All stores preceding the memory barrier will reach global visibility +before any stores after the memory barrier reach global visibility. +.It Fn membar_consumer +All loads preceding the memory barrier will complete before any loads +after the memory barrier complete. +.It Fn membar_sync +All loads and stores preceding the memory barrier will complete and +reach global visibility before any loads and stores after the memory +barrier complete and reach global visibility. +.El +.Sh SEE ALSO +.Xr atomic_ops 3 +.Sh HISTORY +The +.Nm membar_ops +functions first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/cdb/Makefile.inc b/lib/nbsd_libc/cdb/Makefile.inc new file mode 100644 index 000000000..2fb9d05c1 --- /dev/null +++ b/lib/nbsd_libc/cdb/Makefile.inc @@ -0,0 +1,21 @@ +# $NetBSD: Makefile.inc,v 1.1 2010/04/25 00:54:46 joerg Exp $ + +# Constant database reader/writer + +.PATH: ${.CURDIR}/cdb + +SRCS+= cdbr.c cdbw.c + +MAN+= cdbr.3 cdbw.3 cdb.5 + +MLINKS+= cdbr.3 cdbr_open.3 +MLINKS+= cdbr.3 cdbr_entries.3 +MLINKS+= cdbr.3 cdbr_get.3 +MLINKS+= cdbr.3 cdbr_find.3 +MLINKS+= cdbr.3 cdbr_close.3 +MLINKS+= cdbw.3 cdbw_open.3 +MLINKS+= cdbw.3 cdbw_put.3 +MLINKS+= cdbw.3 cdbw_put_data.3 +MLINKS+= cdbw.3 cdbw_put_key.3 +MLINKS+= cdbw.3 cdbw_output.3 +MLINKS+= cdbw.3 cdbw_close.3 diff --git a/lib/nbsd_libc/cdb/cdb.5 b/lib/nbsd_libc/cdb/cdb.5 new file mode 100644 index 000000000..0c1ec86d7 --- /dev/null +++ b/lib/nbsd_libc/cdb/cdb.5 @@ -0,0 +1,104 @@ +.\" $NetBSD: cdb.5,v 1.3 2010/04/27 14:26:52 jruoho Exp $ +.\" +.\" Copyright (c) 2010 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Joerg Sonnenberger. +.\" 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 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. +.Dd April 27, 2010 +.Dt CDB 5 +.Os +.Sh NAME +.Nm cdb +.Nd format of the constant database +.Sh DESCRIPTION +The +.Nm +database format provides a space-efficient (key,value) database. +The format doesn't allow updates in any convenient form. +The file overhead is around 5 bytes per key and 5 bytes per entry. +Keys are not stored and it is the responsibility of the caller +to validate matches. +The index structure is based on a minimal perfect hash table, so exactly +one entry has to be checked for a match. +.Ss General Format +The header record of a +.Nm +database consists of the following: +.Bd -literal -offset indent +struct header_cdb { + uint8_t magic[7]; + uint8_t version; + uint8_t description[16]; + uint32_t data_size; + uint32_t entries; + uint32_t entries_index; + uint32_t seed; +}; +.Ed +.Pp +All fields are in Little Endian byte order. +.Pp +This is followed by a description of the hash function of +.Va entries_index +records. +The size of each index entry is the logarithm of +.Va entries +to base 256, rounded up. +.Pp +The index records are followed by the start offsets of the entries, +followed by +.Va data_size . +The offsets are relative to the end of the offset record table and are +monotically increasing. +The size of each offset record is the logarithm of +.Va data_size +to base 256, rounded up. +.Pp +The offset table is followed by the entries in order. +No separation or padding is added. +.Ss Limitations +The +.Nm +file format is by design intended for a database that can be +mapped into memory. +The hard limit for the number of entries and keys is 3435973836. +The total size of all values must be smaller than 4GiB. +.Sh SEE ALSO +.Xr cdbr 3 , +.Xr cdbw 3 +.Sh HISTORY +Support for the +.Nm cdb +format first appeared in +.Nx 6.0 . +.Sh AUTHORS +The +.Nm cdbr +and +.Nm cdbw +functions have been written by +.An Joerg Sonnenberger Aq joerg@NetBSD.org . diff --git a/lib/nbsd_libc/cdb/cdbr.3 b/lib/nbsd_libc/cdb/cdbr.3 new file mode 100644 index 000000000..7b93ebeaf --- /dev/null +++ b/lib/nbsd_libc/cdb/cdbr.3 @@ -0,0 +1,119 @@ +.\" $NetBSD: cdbr.3,v 1.2 2010/04/25 10:32:44 wiz Exp $ +.\" +.\" Copyright (c) 2010 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Joerg Sonnenberger. +.\" 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 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. +.Dd March 3, 2010 +.Dt CDBR 3 +.Os +.Sh NAME +.Nm cdbr +.Nm cdbr_open , +.Nm cdbr_entries , +.Nm cdbr_get , +.Nm cdbr_find , +.Nm cdbr_close , +.Nm cdbr_write +.Nd constant database access methods +.Sh SYNOPSIS +.Ft "struct cdbr *" +.Fn cdbr_open "const char *path" "int flags" +.Ft uint32_t +.Fn cdbr_entries "struct cdbr *cdbr" +.Ft int +.Fn cdbr_get "struct cdbr *cdbr" "uint32_t index" "const void **data" "size_t *datalen" +.Ft int +.Fo cdbr_find +.Fa "struct cdbr *cdbr" +.Fa "const void *key" +.Fa "size_t keylen" +.Fa "const void **data" +.Fa "size_t *datalen" +.Fc +.Ft void +.Fn cdbr_close "struct cdbr *cdbr" +.Sh DESCRIPTION +The +.Nm +library provides a space efficient (key,value) database based +on perfect hashing. +.Pp +A cdb database is opened for reading by calling +.Fn cdbr_open . +The only supported value for +.Va flags +is +.Dv CDBR_DEFAULT . +The function returns a handle to pass to the other functions. +The database is closed by invoking +.Fn cdbr_close . +All resources associated with the handle are freed and the memory +returned by +.Fn cdbr_get +and +.Fn cdbr_find +is invalidated. +.Pp +The number of records in the database can be obtained by calling +.Fn cdbr_entries . +Records can be obtained by record number using +.Fn cdbr_get +or by key using +.Fn cdbr_find . +Both functions return 0 on success and update +.Va data +and +.Va datalen +accordingly. +The location +.Va *data +remains valid until +.Fn cdbr_close +is called. +It is the responsibility of the caller of +.Fn cdbr_find +to ensure that the key matches the returned data. +The function returns the only possible match, but the database doesn't store +the keys to minimize overhead. +.Sh SEE ALSO +.Xr nbperf 1 , +.Xr cdbw 3 , +.Xr db 3 , +.Xr cdb 5 +.Sh HISTORY +Support for the +.Nm cdb +format first appeared in +.Nx 6.0 . +.Sh AUTHORS +The +.Nm cdbr +and +.Nm cdbw +functions have been written by +.An Joerg Sonnenberger Aq joerg@NetBSD.org . diff --git a/lib/nbsd_libc/cdb/cdbr.c b/lib/nbsd_libc/cdb/cdbr.c new file mode 100644 index 000000000..a71ac42ae --- /dev/null +++ b/lib/nbsd_libc/cdb/cdbr.c @@ -0,0 +1,246 @@ +/* $NetBSD: cdbr.c,v 1.2 2010/06/03 12:40:52 veego Exp $ */ +/*- + * Copyright (c) 2010 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Joerg Sonnenberger. + * + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: cdbr.c,v 1.2 2010/06/03 12:40:52 veego Exp $"); + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(cdbr_close,_cdbr_close) +__weak_alias(cdbr_find,_cdbr_find) +__weak_alias(cdbr_get,_cdbr_get) +__weak_alias(cdbr_open,_cdbr_open) +#endif + +struct cdbr { + uint8_t *mmap_base; + size_t mmap_size; + + uint8_t *hash_base; + uint8_t *offset_base; + uint8_t *data_base; + + uint32_t data_size; + uint32_t entries; + uint32_t entries_index; + uint32_t seed; + + uint8_t offset_size; + uint8_t index_size; + + uint32_t entries_m; + uint32_t entries_index_m; + uint8_t entries_s1, entries_s2; + uint8_t entries_index_s1, entries_index_s2; +}; + +/* ARGSUSED */ +struct cdbr * +cdbr_open(const char *path, int flags) +{ + uint8_t buf[40]; + int fd; + struct cdbr *cdbr; + struct stat sb; + + if ((fd = open(path, O_RDONLY)) == -1) + return NULL; + + errno = EINVAL; + if (fstat(fd, &sb) == -1 || + read(fd, buf, sizeof(buf)) != sizeof(buf) || + memcmp(buf, "NBCDB\n\0\001", 8) || + (cdbr = malloc(sizeof(*cdbr))) == NULL) { + close(fd); + return NULL; + } + + cdbr->data_size = le32dec(buf + 24); + cdbr->entries = le32dec(buf + 28); + cdbr->entries_index = le32dec(buf + 32); + cdbr->seed = le32dec(buf + 36); + + if (cdbr->data_size < 0x100) + cdbr->offset_size = 1; + else if (cdbr->data_size < 0x10000) + cdbr->offset_size = 2; + else + cdbr->offset_size = 4; + + if (cdbr->entries_index < 0x100) + cdbr->index_size = 1; + else if (cdbr->entries_index < 0x10000) + cdbr->index_size = 2; + else + cdbr->index_size = 4; + + cdbr->mmap_size = (size_t)sb.st_size; + cdbr->mmap_base = mmap(NULL, cdbr->mmap_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0); + close(fd); + + if (cdbr->mmap_base == MAP_FAILED) { + free(cdbr); + return NULL; + } + + cdbr->hash_base = cdbr->mmap_base + 40; + cdbr->offset_base = cdbr->hash_base + cdbr->entries_index * cdbr->index_size; + if (cdbr->entries_index * cdbr->index_size % cdbr->offset_size) + cdbr->offset_base += cdbr->offset_size - + cdbr->entries_index * cdbr->index_size % cdbr->offset_size; + cdbr->data_base = cdbr->offset_base + (cdbr->entries + 1) * cdbr->offset_size; + + if (cdbr->hash_base < cdbr->mmap_base || + cdbr->offset_base < cdbr->mmap_base || + cdbr->data_base < cdbr->mmap_base || + cdbr->data_base + cdbr->data_size < cdbr->mmap_base || + cdbr->data_base + cdbr->data_size > + cdbr->mmap_base + cdbr->mmap_size || + cdbr->entries == 0 || cdbr->entries_index == 0) { + errno = EINVAL; + cdbr_close(cdbr); + return NULL; + } + + fast_divide32_prepare(cdbr->entries, &cdbr->entries_m, + &cdbr->entries_s1, &cdbr->entries_s2); + fast_divide32_prepare(cdbr->entries_index, &cdbr->entries_index_m, + &cdbr->entries_index_s1, &cdbr->entries_index_s2); + + return cdbr; +} + +static inline uint32_t +get_uintX(const uint8_t *addr, uint32_t idx, int size) +{ + addr += idx * size; + + if (size == 4) + return /* LINTED */le32toh(*(const uint32_t *)addr); + else if (size == 2) + return /* LINTED */le16toh(*(const uint16_t *)addr); + else + return *addr; +} + +uint32_t +cdbr_entries(struct cdbr *cdbr) +{ + + return cdbr->entries; +} + +int +cdbr_get(struct cdbr *cdbr, uint32_t idx, const void **data, size_t *data_len) +{ + uint32_t start, end; + + if (idx >= cdbr->entries) { + errno = EINVAL; + return -1; + } + + start = get_uintX(cdbr->offset_base, idx, cdbr->offset_size); + end = get_uintX(cdbr->offset_base, idx + 1, cdbr->offset_size); + + if (start > end) { + errno = EIO; + return -1; + } + + if (end > cdbr->data_size) { + errno = EIO; + return -1; + } + + *data = cdbr->data_base + start; + *data_len = end - start; + + return 0; +} + +int +cdbr_find(struct cdbr *cdbr, const void *key, size_t key_len, + const void **data, size_t *data_len) +{ + uint32_t hashes[3], idx; + + if (cdbr->entries_index == 0) { + errno = EINVAL; + return -1; + } + + mi_vector_hash(key, key_len, cdbr->seed, hashes); + + hashes[0] = fast_remainder32(hashes[0], cdbr->entries_index, + cdbr->entries_index_m, cdbr->entries_index_s1, + cdbr->entries_index_s2); + hashes[1] = fast_remainder32(hashes[1], cdbr->entries_index, + cdbr->entries_index_m, cdbr->entries_index_s1, + cdbr->entries_index_s2); + hashes[2] = fast_remainder32(hashes[2], cdbr->entries_index, + cdbr->entries_index_m, cdbr->entries_index_s1, + cdbr->entries_index_s2); + + idx = get_uintX(cdbr->hash_base, hashes[0], cdbr->index_size); + idx += get_uintX(cdbr->hash_base, hashes[1], cdbr->index_size); + idx += get_uintX(cdbr->hash_base, hashes[2], cdbr->index_size); + + return cdbr_get(cdbr, fast_remainder32(idx, cdbr->entries, + cdbr->entries_m, cdbr->entries_s1, cdbr->entries_s2), data, + data_len); +} + +void +cdbr_close(struct cdbr *cdbr) +{ + munmap(cdbr->mmap_base, cdbr->mmap_size); + free(cdbr); +} diff --git a/lib/nbsd_libc/cdb/cdbw.3 b/lib/nbsd_libc/cdb/cdbw.3 new file mode 100644 index 000000000..bd6e152d0 --- /dev/null +++ b/lib/nbsd_libc/cdb/cdbw.3 @@ -0,0 +1,130 @@ +.\" $NetBSD: cdbw.3,v 1.3 2010/11/03 16:17:48 plunky Exp $ +.\" +.\" Copyright (c) 2010 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Joerg Sonnenberger. +.\" 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 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. +.Dd November 3, 2010 +.Dt CDBW 3 +.Os +.Sh NAME +.Nm cdbw_open , +.Nm cdbw_put , +.Nm cdbw_put_data , +.Nm cdbw_put_key , +.Nm cdbw_close +.Nd create constant databases +.Sh SYNOPSIS +.In archive_entry.h +.Ft "struct cdbw *" +.Fn cdbw_open "void" +.Ft int +.Fo cdbw_put +.Fa "struct cdbw *cdbw" +.Fa "const void *key" +.Fa "size_t keylen" +.Fa "const void *data" +.Fa "size_t datalen" +.Fc +.Ft int +.Fo cdbw_put_data +.Fa "struct cdbw *cdbw" +.Fa "const void *data" +.Fa "size_t datalen" +.Fa "uint32_t *index" +.Fc +.Ft int +.Fo cdbw_put_key +.Fa "struct cdbw *cdbw" +.Fa "const void *key" +.Fa "size_t keylen" +.Fa "uint32_t index" +.Fc +.Ft int +.Fo cdbw_output +.Fa "struct cdbw *cdbw" +.Fa "int output" +.Fa "const char descr[16]" +.Fa "uint32_t (*seedgen)(void)" +.Fc +.Ft void +.Fn cdbw_close "struct cdbw *cdbw" +.Sh DESCRIPTION +The +.Nm cdbw +functions are used to create a constant databases for use with +.Xr cdbr 3 . +Details about the file format, including overhead and limitations, +can be found in +.Xr cdb 5 . +.Pp +.Fn cdbw_open +prepares a new +.Nm cdb +writer. +The function returns a handle to pass to the other functions. +.Pp +.Fn cdbw_close +frees all resources associated with the handle. +.Pp +.Fn cdbw_put +adds the given (key,value) pair after checking for a duplicate key. +.Fn cdbw_put_data +adds the given value to the writer without adding a key reference. +The returned index can be used in subsequent calls to +.Fn cdbw_put_key +to add one or more keys pointing to this value. +.Fn cdbw_put_key +checks for duplicate keys and valid index arguments. +On success it adds the given key. +.Pp +.Fn cdbw_output +computes the database file and writes it to the given descriptor. +The function returns an error if the file cannot be written correctly. +The +.Fn descr +parameter provides a human readable description of the database content. +The +.Fn seedgen +parameter can be used to override the default PRNG. +The function should return a different value for each invokation. +The bitwise layout of the output depends on the chosen seed. +.Sh SEE ALSO +.Xr cdbr 3 , +.Xr cdb 5 +.Sh HISTORY +Support for the +.Nm cdb +format first appeared in +.Nx 6.0 . +.Sh AUTHORS +The +.Nm cdbr +and +.Nm cdbw +functions have been written by +.An Joerg Sonnenberger Aq joerg@NetBSD.org . diff --git a/lib/nbsd_libc/cdb/cdbw.c b/lib/nbsd_libc/cdb/cdbw.c new file mode 100644 index 000000000..d670740d2 --- /dev/null +++ b/lib/nbsd_libc/cdb/cdbw.c @@ -0,0 +1,599 @@ +/* $NetBSD: cdbw.c,v 1.1 2010/04/25 00:54:46 joerg Exp $ */ +/*- + * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Joerg Sonnenberger. + * + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: cdbw.c,v 1.1 2010/04/25 00:54:46 joerg Exp $"); + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(cdbw_close,_cdbw_close) +__weak_alias(cdbw_open,_cdbw_open) +__weak_alias(cdbw_output,_cdbw_output) +__weak_alias(cdbw_put,_cdbw_put) +__weak_alias(cdbw_put_data,_cdbw_put_data) +__weak_alias(cdbw_put_key,_cdbw_put_key) +#endif + +struct key_hash { + SLIST_ENTRY(key_hash) link; + uint32_t hashes[3]; + uint32_t idx; + void *key; + size_t keylen; +}; + +SLIST_HEAD(key_hash_head, key_hash); + +struct cdbw { + size_t data_counter; + size_t data_allocated; + size_t data_size; + size_t *data_len; + void **data_ptr; + + size_t hash_size; + struct key_hash_head *hash; + size_t key_counter; +}; + + /* Max. data counter that allows the index size to be 32bit. */ +static const uint32_t max_data_counter = 0xccccccccU; + +struct cdbw * +cdbw_open(void) +{ + struct cdbw *cdbw; + size_t i; + + cdbw = calloc(sizeof(*cdbw), 1); + if (cdbw == NULL) + return NULL; + + cdbw->hash_size = 1024; + cdbw->hash = calloc(cdbw->hash_size, sizeof(*cdbw->hash)); + if (cdbw->hash == NULL) { + free(cdbw); + return NULL; + } + + for (i = 0; i < cdbw->hash_size; ++i) + SLIST_INIT(cdbw->hash + i); + + return cdbw; +} + +int +cdbw_put(struct cdbw *cdbw, const void *key, size_t keylen, + const void *data, size_t datalen) +{ + uint32_t idx; + int rv; + + rv = cdbw_put_data(cdbw, data, datalen, &idx); + if (rv) + return rv; + rv = cdbw_put_key(cdbw, key, keylen, idx); + if (rv) { + --cdbw->data_counter; + free(cdbw->data_ptr[cdbw->data_counter]); + cdbw->data_size -= datalen; + return rv; + } + return 0; +} + +int +cdbw_put_data(struct cdbw *cdbw, const void *data, size_t datalen, + uint32_t *idx) +{ + + if (cdbw->data_counter == max_data_counter) + return -1; + + if (cdbw->data_size + datalen < cdbw->data_size || + cdbw->data_size + datalen > 0xffffffffU) + return -1; /* Overflow */ + + if (cdbw->data_allocated == cdbw->data_counter) { + void **new_data_ptr; + size_t *new_data_len; + size_t new_allocated; + + if (cdbw->data_allocated == 0) + new_allocated = 256; + else + new_allocated = cdbw->data_allocated * 2; + + new_data_ptr = realloc(cdbw->data_ptr, + sizeof(*cdbw->data_ptr) * new_allocated); + if (new_data_ptr == NULL) + return -1; + cdbw->data_ptr = new_data_ptr; + + new_data_len = realloc(cdbw->data_len, + sizeof(*cdbw->data_len) * new_allocated); + if (new_data_len == NULL) + return -1; + cdbw->data_len = new_data_len; + + cdbw->data_allocated = new_allocated; + } + + cdbw->data_ptr[cdbw->data_counter] = malloc(datalen); + if (cdbw->data_ptr[cdbw->data_counter] == NULL) + return -1; + memcpy(cdbw->data_ptr[cdbw->data_counter], data, datalen); + cdbw->data_len[cdbw->data_counter] = datalen; + cdbw->data_size += datalen; + *idx = cdbw->data_counter++; + return 0; +} + +int +cdbw_put_key(struct cdbw *cdbw, const void *key, size_t keylen, uint32_t idx) +{ + uint32_t hashes[3]; + struct key_hash_head *head, *head2, *new_head; + struct key_hash *key_hash; + size_t new_hash_size, i; + + if (idx >= cdbw->data_counter || + cdbw->key_counter == max_data_counter) + return -1; + + mi_vector_hash(key, keylen, 0, hashes); + + head = cdbw->hash + (hashes[0] & (cdbw->hash_size - 1)); + SLIST_FOREACH(key_hash, head, link) { + if (key_hash->keylen != keylen) + continue; + if (key_hash->hashes[0] != hashes[0]) + continue; + if (key_hash->hashes[1] != hashes[1]) + continue; + if (key_hash->hashes[2] != hashes[2]) + continue; + if (memcmp(key, key_hash->key, keylen)) + continue; + return -1; + } + key_hash = malloc(sizeof(*key_hash)); + if (key_hash == NULL) + return -1; + key_hash->key = malloc(keylen); + if (key_hash->key == NULL) { + free(key_hash); + return -1; + } + memcpy(key_hash->key, key, keylen); + key_hash->hashes[0] = hashes[0]; + key_hash->hashes[1] = hashes[1]; + key_hash->hashes[2] = hashes[2]; + key_hash->keylen = keylen; + key_hash->idx = idx; + SLIST_INSERT_HEAD(head, key_hash, link); + ++cdbw->key_counter; + + if (cdbw->key_counter <= cdbw->hash_size) + return 0; + + /* Try to resize the hash table, but ignore errors. */ + new_hash_size = cdbw->hash_size * 2; + new_head = calloc(sizeof(*new_head), new_hash_size); + if (new_head == NULL) + return 0; + + head = &cdbw->hash[hashes[0] & (cdbw->hash_size - 1)]; + for (i = 0; i < new_hash_size; ++i) + SLIST_INIT(new_head + i); + + for (i = 0; i < cdbw->hash_size; ++i) { + head = cdbw->hash + i; + + while ((key_hash = SLIST_FIRST(head)) != NULL) { + SLIST_REMOVE_HEAD(head, link); + head2 = new_head + + (key_hash->hashes[0] & (new_hash_size - 1)); + SLIST_INSERT_HEAD(head2, key_hash, link); + } + } + free(cdbw->hash); + cdbw->hash_size = new_hash_size; + cdbw->hash = new_head; + + return 0; +} + +void +cdbw_close(struct cdbw *cdbw) +{ + struct key_hash_head *head; + struct key_hash *key_hash; + size_t i; + + for (i = 0; i < cdbw->hash_size; ++i) { + head = cdbw->hash + i; + while ((key_hash = SLIST_FIRST(head)) != NULL) { + SLIST_REMOVE_HEAD(head, link); + free(key_hash->key); + free(key_hash); + } + } + + for (i = 0; i < cdbw->data_counter; ++i) + free(cdbw->data_ptr[i]); + free(cdbw->data_ptr); + free(cdbw->data_len); + free(cdbw->hash); + free(cdbw); +} + +#define unused 0xffffffffU + +struct vertex { + uint32_t l_edge, m_edge, r_edge; +}; + +struct edge { + uint32_t idx; + + uint32_t left, middle, right; + uint32_t l_prev, m_prev, l_next; + uint32_t r_prev, m_next, r_next; +}; + +struct state { + uint32_t data_entries; + uint32_t entries; + uint32_t keys; + uint32_t seed; + + uint32_t *g; + char *visited; + + struct vertex *verts; + struct edge *edges; + uint32_t output_index; + uint32_t *output_order; +}; + +static void +remove_vertex(struct state *state, struct vertex *v) +{ + struct edge *e; + struct vertex *vl, *vm, *vr; + + if (v->l_edge != unused && v->m_edge != unused) + return; + if (v->l_edge != unused && v->r_edge != unused) + return; + if (v->m_edge != unused && v->r_edge != unused) + return; + if (v->l_edge == unused && v->m_edge == unused && v->r_edge == unused) + return; + + if (v->l_edge != unused) { + e = &state->edges[v->l_edge]; + if (e->l_next != unused) + return; + } else if (v->m_edge != unused) { + e = &state->edges[v->m_edge]; + if (e->m_next != unused) + return; + } else { + if (v->r_edge == unused) + abort(); + e = &state->edges[v->r_edge]; + if (e->r_next != unused) + return; + } + + state->output_order[--state->output_index] = e - state->edges; + + vl = &state->verts[e->left]; + vm = &state->verts[e->middle]; + vr = &state->verts[e->right]; + + if (e->l_prev == unused) + vl->l_edge = e->l_next; + else + state->edges[e->l_prev].l_next = e->l_next; + if (e->l_next != unused) + state->edges[e->l_next].l_prev = e->l_prev; + + if (e->m_prev == unused) + vm->m_edge = e->m_next; + else + state->edges[e->m_prev].m_next = e->m_next; + if (e->m_next != unused) + state->edges[e->m_next].m_prev = e->m_prev; + + if (e->r_prev == unused) + vr->r_edge = e->r_next; + else + state->edges[e->r_prev].r_next = e->r_next; + if (e->r_next != unused) + state->edges[e->r_next].r_prev = e->r_prev; +} + +static int +build_graph(struct cdbw *cdbw, struct state *state) +{ + struct key_hash_head *head; + struct key_hash *key_hash; + struct vertex *v; + struct edge *e; + uint32_t hashes[3]; + size_t i; + + e = state->edges; + for (i = 0; i < cdbw->hash_size; ++i) { + head = &cdbw->hash[i]; + SLIST_FOREACH(key_hash, head, link) { + e->idx = key_hash->idx; + mi_vector_hash(key_hash->key, key_hash->keylen, + state->seed, hashes); + e->left = hashes[0] % state->entries; + e->middle = hashes[1] % state->entries; + e->right = hashes[2] % state->entries; + + ++e; + } + } + + for (i = 0; i < state->entries; ++i) { + v = state->verts + i; + v->l_edge = unused; + v->m_edge = unused; + v->r_edge = unused; + } + + for (i = 0; i < state->keys; ++i) { + e = state->edges + i; + v = state->verts + e->left; + if (v->l_edge != unused) + state->edges[v->l_edge].l_prev = i; + e->l_next = v->l_edge; + e->l_prev = unused; + v->l_edge = i; + + v = &state->verts[e->middle]; + if (v->m_edge != unused) + state->edges[v->m_edge].m_prev = i; + e->m_next = v->m_edge; + e->m_prev = unused; + v->m_edge = i; + + v = &state->verts[e->right]; + if (v->r_edge != unused) + state->edges[v->r_edge].r_prev = i; + e->r_next = v->r_edge; + e->r_prev = unused; + v->r_edge = i; + } + + state->output_index = state->keys; + for (i = 0; i < state->entries; ++i) + remove_vertex(state, state->verts + i); + + i = state->keys; + while (i > 0 && i > state->output_index) { + --i; + e = state->edges + state->output_order[i]; + remove_vertex(state, state->verts + e->left); + remove_vertex(state, state->verts + e->middle); + remove_vertex(state, state->verts + e->right); + } + + return state->output_index == 0 ? 0 : -1; +} + +static void +assign_nodes(struct state *state) +{ + struct edge *e; + size_t i; + + for (i = 0; i < state->keys; ++i) { + e = state->edges + state->output_order[i]; + + if (!state->visited[e->left]) { + state->g[e->left] = + (2 * state->data_entries + e->idx + - state->g[e->middle] - state->g[e->right]) + % state->data_entries; + } else if (!state->visited[e->middle]) { + state->g[e->middle] = + (2 * state->data_entries + e->idx + - state->g[e->left] - state->g[e->right]) + % state->data_entries; + } else { + state->g[e->right] = + (2 * state->data_entries + e->idx + - state->g[e->left] - state->g[e->middle]) + % state->data_entries; + } + state->visited[e->left] = 1; + state->visited[e->middle] = 1; + state->visited[e->right] = 1; + } +} + +static size_t +compute_size(uint32_t size) +{ + if (size < 0x100) + return 1; + else if (size < 0x10000) + return 2; + else + return 4; +} + +#define COND_FLUSH_BUFFER(n) do { \ + if (__predict_false(cur_pos + (n) >= sizeof(buf))) { \ + ret = write(fd, buf, cur_pos); \ + if (ret == -1 || (size_t)ret != cur_pos) \ + return -1; \ + cur_pos = 0; \ + } \ +} while (/* CONSTCOND */ 0) + +static int +print_hash(struct cdbw *cdbw, struct state *state, int fd, const char *descr) +{ + uint32_t data_size; + uint8_t buf[90000]; + size_t i, size, size2, cur_pos; + ssize_t ret; + + memcpy(buf, "NBCDB\n\0", 7); + buf[7] = 1; + strncpy((char *)buf + 8, descr, 16); + le32enc(buf + 24, cdbw->data_size); + le32enc(buf + 28, cdbw->data_counter); + le32enc(buf + 32, state->entries); + le32enc(buf + 36, state->seed); + cur_pos = 40; + + size = compute_size(state->entries); + for (i = 0; i < state->entries; ++i) { + COND_FLUSH_BUFFER(4); + le32enc(buf + cur_pos, state->g[i]); + cur_pos += size; + } + size2 = compute_size(cdbw->data_size); + size = size * state->entries % size2; + if (size != 0) { + size = size2 - size; + COND_FLUSH_BUFFER(4); + le32enc(buf + cur_pos, 0); + cur_pos += size; + } + for (data_size = 0, i = 0; i < cdbw->data_counter; ++i) { + COND_FLUSH_BUFFER(4); + le32enc(buf + cur_pos, data_size); + cur_pos += size2; + data_size += cdbw->data_len[i]; + } + COND_FLUSH_BUFFER(4); + le32enc(buf + cur_pos, data_size); + cur_pos += size2; + + for (i = 0; i < cdbw->data_counter; ++i) { + COND_FLUSH_BUFFER(cdbw->data_len[i]); + if (cdbw->data_len[i] < sizeof(buf)) { + memcpy(buf + cur_pos, cdbw->data_ptr[i], + cdbw->data_len[i]); + cur_pos += cdbw->data_len[i]; + } else { + ret = write(fd, cdbw->data_ptr[i], cdbw->data_len[i]); + if (ret == -1 || (size_t)ret != cdbw->data_len[i]) + return -1; + } + } + if (cur_pos != 0) { + ret = write(fd, buf, cur_pos); + if (ret == -1 || (size_t)ret != cur_pos) + return -1; + } + return 0; +} + +int +cdbw_output(struct cdbw *cdbw, int fd, const char descr[16], + uint32_t (*seedgen)(void)) +{ + struct state state; + int rv; + + if (cdbw->data_counter == 0 || cdbw->key_counter == 0) { + state.entries = 0; + state.seed = 0; + print_hash(cdbw, &state, fd, descr); + return 0; + } + + if (seedgen == NULL) + seedgen = arc4random; + + rv = 0; + + state.keys = cdbw->key_counter; + state.data_entries = cdbw->data_counter; + state.entries = state.keys + (state.keys + 3) / 4; + if (state.entries < 10) + state.entries = 10; + +#define NALLOC(var, n) var = calloc(sizeof(*var), n) + NALLOC(state.g, state.entries); + NALLOC(state.visited, state.entries); + NALLOC(state.verts, state.entries); + NALLOC(state.edges, state.entries); + NALLOC(state.output_order, state.keys); +#undef NALLOC + + if (state.g == NULL || state.visited == NULL || state.verts == NULL || + state.edges == NULL || state.output_order == NULL) { + rv = -1; + goto release; + } + + do { + state.seed = (*seedgen)(); + } while (build_graph(cdbw, &state)); + + assign_nodes(&state); + rv = print_hash(cdbw, &state, fd, descr); + +release: + free(state.g); + free(state.visited); + free(state.verts); + free(state.edges); + free(state.output_order); + + return rv; +} diff --git a/lib/nbsd_libc/citrus/Makefile.inc b/lib/nbsd_libc/citrus/Makefile.inc new file mode 100644 index 000000000..abab17511 --- /dev/null +++ b/lib/nbsd_libc/citrus/Makefile.inc @@ -0,0 +1,25 @@ +# $NetBSD: Makefile.inc,v 1.8 2010/05/30 08:28:53 tnozaki Exp $ + +# sources +.PATH: ${ARCHDIR}/citrus ${.CURDIR}/citrus + +MODULE_SHLIB_MAJOR!= . ${NETBSDSRCDIR}/lib/i18n_module/shlib_version ; echo $$major +CPPFLAGS.citrus_module.c+=-DI18NMODULE_MAJOR=${MODULE_SHLIB_MAJOR} +SRCS+= citrus_bcs.c citrus_bcs_strtol.c citrus_bcs_strtoul.c \ + citrus_csmapper.c citrus_ctype.c citrus_ctype_fallback.c \ + citrus_db.c citrus_db_hash.c citrus_esdb.c citrus_hash.c \ + citrus_iconv.c citrus_lookup.c \ + citrus_mapper.c citrus_memstream.c citrus_mmap.c citrus_module.c \ + citrus_none.c citrus_stdenc.c +SRCS+= citrus_lc_ctype.c \ + citrus_lc_monetary.c \ + citrus_lc_numeric.c \ + citrus_lc_time.c \ + citrus_lc_messages.c +CPPFLAGS.citrus_bcs_strtol.c+= -I${LIBCDIR}/../../common/lib/libc/stdlib +CPPFLAGS.citrus_bcs_strtoul.c+= -I${LIBCDIR}/../../common/lib/libc/stdlib +CPPFLAGS.citrus_lc_ctype.c+= -I${LIBCDIR}/locale +CPPFLAGS.citrus_lc_monetary.c+= -I${LIBCDIR}/locale +CPPFLAGS.citrus_lc_numeric.c+= -I${LIBCDIR}/locale +CPPFLAGS.citrus_lc_time.c+= -I${LIBCDIR}/locale +CPPFLAGS.citrus_lc_messages.c+= -I${LIBCDIR}/locale diff --git a/lib/nbsd_libc/citrus/citrus_aliasname_local.h b/lib/nbsd_libc/citrus/citrus_aliasname_local.h new file mode 100644 index 000000000..d76cb418b --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_aliasname_local.h @@ -0,0 +1,48 @@ +/* $NetBSD: citrus_aliasname_local.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ALIASNAME_LOCAL_H_ +#define _CITRUS_ALIASNAME_LOCAL_H_ + +static __inline const char * +__unaliasname(const char *dbname, const char *alias, + void *buf, size_t bufsize) +{ + return _lookup_simple(dbname, alias, + buf, bufsize, _LOOKUP_CASE_SENSITIVE); +} + +static __inline int +__isforcemapping(const char *name) +{ + _DIAGASSERT(name != NULL); + + return _bcs_strcasecmp("/force", name); +} + +#endif /*_CITRUS_ALIASNAME_LOCAL_H_*/ diff --git a/lib/nbsd_libc/citrus/citrus_bcs.c b/lib/nbsd_libc/citrus/citrus_bcs.c new file mode 100644 index 000000000..b14186de6 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_bcs.c @@ -0,0 +1,171 @@ +/* $NetBSD: citrus_bcs.c,v 1.5 2005/05/14 17:55:42 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_bcs.c,v 1.5 2005/05/14 17:55:42 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef HOSTPROG +#include "namespace.h" +#endif +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" + +/* + * case insensitive comparison between two C strings. + */ +int +_citrus_bcs_strcasecmp(const char * __restrict str1, + const char * __restrict str2) +{ + int c1 = 1, c2 = 1; + + while (c1 && c2 && c1 == c2) { + c1 = _bcs_toupper(*str1++); + c2 = _bcs_toupper(*str2++); + } + + return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1)); +} + +/* + * case insensitive comparison between two C strings with limitation of length. + */ +int +_citrus_bcs_strncasecmp(const char * __restrict str1, + const char * __restrict str2, size_t sz) +{ + int c1 = 1, c2 = 1; + + while (c1 && c2 && c1 == c2 && sz != 0) { + c1 = _bcs_toupper(*str1++); + c2 = _bcs_toupper(*str2++); + sz--; + } + + return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1)); +} + +/* + * skip white space characters. + */ +const char * +_citrus_bcs_skip_ws(const char *p) +{ + + while (*p && _bcs_isspace(*p)) + p++; + + return (p); +} + +/* + * skip non white space characters. + */ +const char * +_citrus_bcs_skip_nonws(const char *p) +{ + + while (*p && !_bcs_isspace(*p)) + p++; + + return (p); +} + +/* + * skip white space characters with limitation of length. + */ +const char * +_citrus_bcs_skip_ws_len(const char * __restrict p, size_t * __restrict len) +{ + + while (*p && *len > 0 && _bcs_isspace(*p)) { + p++; + (*len)--; + } + + return (p); +} + +/* + * skip non white space characters with limitation of length. + */ +const char * +_citrus_bcs_skip_nonws_len(const char * __restrict p, size_t * __restrict len) +{ + + while (*p && *len > 0 && !_bcs_isspace(*p)) { + p++; + (*len)--; + } + + return (p); +} + +/* + * truncate trailing white space characters. + */ +void +_citrus_bcs_trunc_rws_len(const char * __restrict p, size_t * __restrict len) +{ + + while (*len > 0 && _bcs_isspace(p[*len - 1])) + (*len)--; +} + +/* + * destructive transliterate to lowercase. + */ +void +_citrus_bcs_convert_to_lower(char *s) +{ + while (*s) { + *s = _bcs_tolower(*s); + s++; + } +} + +/* + * destructive transliterate to uppercase. + */ +void +_citrus_bcs_convert_to_upper(char *s) +{ + while (*s) { + *s = _bcs_toupper(*s); + s++; + } +} diff --git a/lib/nbsd_libc/citrus/citrus_bcs.h b/lib/nbsd_libc/citrus/citrus_bcs.h new file mode 100644 index 000000000..784da76da --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_bcs.h @@ -0,0 +1,98 @@ +/* $NetBSD: citrus_bcs.h,v 1.6 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_BCS_H_ +#define _CITRUS_BCS_H_ + +/* + * predicate/conversion for basic character set. + * + * `Basic character set' is a term defined in the ISO C standard. + * Citrus bcs is, if anything, close to `portable character set' + * defined in the POSIX. + */ + +#define _CITRUS_BCS_PRED(_name_, _cond_) \ +static __inline int _citrus_bcs_##_name_(uint8_t c) { return (_cond_); } + +/* + * predicates. + * Unlike predicates defined in ctype.h, these do not accept EOF. + */ +_CITRUS_BCS_PRED(isblank, c == ' ' || c == '\t') +_CITRUS_BCS_PRED(iseol, c == '\n' || c == '\r') +_CITRUS_BCS_PRED(isspace, + _citrus_bcs_isblank(c) || _citrus_bcs_iseol(c) || + c == '\f' || c == '\v') +_CITRUS_BCS_PRED(isdigit, c >= '0' && c <= '9') +_CITRUS_BCS_PRED(isupper, c >= 'A' && c <= 'Z') +_CITRUS_BCS_PRED(islower, c >= 'a' && c <= 'z') +_CITRUS_BCS_PRED(isalpha, _citrus_bcs_isupper(c) || _citrus_bcs_islower(c)) +_CITRUS_BCS_PRED(isalnum, _citrus_bcs_isdigit(c) || _citrus_bcs_isalpha(c)) +_CITRUS_BCS_PRED(isxdigit, + _citrus_bcs_isdigit(c) || + (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) + +/* + * transliterate between uppercase and lowercase. + * Unlike transliterator defined in ctype.h, these do not accept EOF. + */ +static __inline uint8_t +_citrus_bcs_toupper(uint8_t c) +{ + return (_citrus_bcs_islower(c) ? (c - 'a' + 'A') : c); +} + +static __inline uint8_t +_citrus_bcs_tolower(uint8_t c) +{ + return (_citrus_bcs_isupper(c) ? (c - 'A' + 'a') : c); +} + +__BEGIN_DECLS +int _citrus_bcs_strcasecmp(const char * __restrict, const char * __restrict); +int _citrus_bcs_strncasecmp(const char * __restrict, const char * __restrict, + size_t); +const char *_citrus_bcs_skip_ws(const char * __restrict); +const char *_citrus_bcs_skip_nonws(const char * __restrict); +const char *_citrus_bcs_skip_ws_len(const char * __restrict, + size_t * __restrict); +const char *_citrus_bcs_skip_nonws_len(const char * __restrict, + size_t * __restrict); +void _citrus_bcs_trunc_rws_len(const char * __restrict, size_t * __restrict); +void _citrus_bcs_convert_to_lower(char *); +void _citrus_bcs_convert_to_upper(char *); + +long int _citrus_bcs_strtol( + const char * __restrict, char ** __restrict, int); +unsigned long int _citrus_bcs_strtoul( + const char * __restrict, char ** __restrict, int); + +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_bcs_strtol.c b/lib/nbsd_libc/citrus/citrus_bcs_strtol.c new file mode 100644 index 000000000..4a30bc8a6 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_bcs_strtol.c @@ -0,0 +1,59 @@ +/* $NetBSD: citrus_bcs_strtol.c,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003, 2008 Citrus Project, + * 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 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 +__RCSID("$NetBSD: citrus_bcs_strtol.c,v 1.2 2009/01/11 02:46:24 christos Exp $"); + +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" + +#define _FUNCNAME _bcs_strtol +#define __INT long int +#define __INT_MIN LONG_MIN +#define __INT_MAX LONG_MAX + +#undef isspace +#define isspace(c) _bcs_isspace(c) + +#undef isdigit +#define isdigit(c) _bcs_isdigit(c) + +#undef isalpha +#define isalpha(c) _bcs_isalpha(c) + +#undef isupper +#define isupper(c) _bcs_isupper(c) + +#include "_strtol.h" diff --git a/lib/nbsd_libc/citrus/citrus_bcs_strtoul.c b/lib/nbsd_libc/citrus/citrus_bcs_strtoul.c new file mode 100644 index 000000000..984030fa0 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_bcs_strtoul.c @@ -0,0 +1,64 @@ +/* $NetBSD: citrus_bcs_strtoul.c,v 1.3 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003, 2008 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if !defined(lint) +__RCSID("$NetBSD: citrus_bcs_strtoul.c,v 1.3 2009/01/11 02:46:24 christos Exp $"); +#endif /* not lint */ + +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" + +#define _FUNCNAME _bcs_strtoul +#define __UINT unsigned long int +#define __UINT_MAX ULONG_MAX + +#undef isspace +#define isspace(c) _bcs_isspace(c) + +#undef isdigit +#define isdigit(c) _bcs_isdigit(c) + +#undef isalpha +#define isalpha(c) _bcs_isalpha(c) + +#undef isupper +#define isupper(c) _bcs_isupper(c) + +#include "_strtoul.h" diff --git a/lib/nbsd_libc/citrus/citrus_csmapper.c b/lib/nbsd_libc/citrus/citrus_csmapper.c new file mode 100644 index 000000000..bcbc9776b --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_csmapper.c @@ -0,0 +1,387 @@ +/* $NetBSD: citrus_csmapper.c,v 1.10 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_csmapper.c,v 1.10 2009/01/11 02:46:24 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_csmapper.h" +#include "citrus_pivot_file.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_lookup.h" + +#ifdef _REENTRANT +static rwlock_t lock = RWLOCK_INITIALIZER; +#endif +static struct _citrus_mapper_area *maparea = NULL; + +#define CS_ALIAS _PATH_CSMAPPER "/charset.alias" +#define CS_PIVOT _PATH_CSMAPPER "/charset.pivot" + + +/* ---------------------------------------------------------------------- */ + +static int +get32(struct _region *r, uint32_t *rval) +{ + if (_region_size(r) != 4) + return EFTYPE; + + memcpy(rval, _region_head(r), (size_t)4); + *rval = be32toh(*rval); + + return 0; +} + +static int +open_subdb(struct _citrus_db **subdb, struct _citrus_db *db, const char *src) +{ + int ret; + struct _region r; + + ret = _db_lookup_by_s(db, src, &r, NULL); + if (ret) + return ret; + ret = _db_open(subdb, &r, _CITRUS_PIVOT_SUB_MAGIC, _db_hash_std, NULL); + if (ret) + return ret; + + return 0; +} + + +#define NO_SUCH_FILE EOPNOTSUPP +static int +find_best_pivot_pvdb(const char *src, const char *dst, char *pivot, + size_t pvlen, unsigned long *rnorm) +{ + int ret, num, i; + struct _region fr, r1, r2; + struct _citrus_db *db1, *db2, *db3; + char buf[LINE_MAX]; + unsigned long norm; + uint32_t val32; + + ret = _map_file(&fr, CS_PIVOT ".pvdb"); + if (ret) { + if (ret == ENOENT) + ret = NO_SUCH_FILE; + return ret; + } + ret = _db_open(&db1, &fr, _CITRUS_PIVOT_MAGIC, _db_hash_std, NULL); + if (ret) + goto quit1; + ret = open_subdb(&db2, db1, src); + if (ret) + goto quit2; + + num = _db_get_num_entries(db2); + *rnorm = ULONG_MAX; + for (i = 0; i < num; i++) { + /* iterate each pivot */ + ret = _db_get_entry(db2, i, &r1, &r2); + if (ret) + goto quit3; + /* r1:pivot name, r2:norm among src and pivot */ + ret = get32(&r2, &val32); + if (ret) + goto quit3; + norm = val32; + snprintf(buf, sizeof(buf), "%.*s", + (int)_region_size(&r1), (char *)_region_head(&r1)); + /* buf: pivot name */ + ret = open_subdb(&db3, db1, buf); + if (ret) + goto quit3; + if (_db_lookup_by_s(db3, dst, &r2, NULL) != 0) + goto quit4; + /* r2: norm among pivot and dst */ + ret = get32(&r2, &val32); + if (ret) + goto quit4; + norm += val32; + /* judge minimum norm */ + if (norm < *rnorm) { + *rnorm = norm; + strlcpy(pivot, buf, pvlen); + } +quit4: + _db_close(db3); + if (ret) + goto quit3; + } +quit3: + _db_close(db2); +quit2: + _db_close(db1); +quit1: + _unmap_file(&fr); + if (ret) + return ret; + + if (*rnorm == ULONG_MAX) + return ENOENT; + + return 0; +} + +/* ---------------------------------------------------------------------- */ + +struct zone { + const char *begin, *end; +}; + +struct parse_arg { + char dst[PATH_MAX]; + unsigned long norm; +}; + +static int +parse_line(struct parse_arg *pa, struct _region *r) +{ + char buf[20]; + struct zone z1, z2; + size_t len; + + len = _region_size(r); + z1.begin = _bcs_skip_ws_len(_region_head(r), &len); + if (len == 0) + return EFTYPE; + z1.end = _bcs_skip_nonws_len(z1.begin, &len); + if (len == 0) + return EFTYPE; + z2.begin = _bcs_skip_ws_len(z1.end, &len); + if (len == 0) + return EFTYPE; + z2.end = _bcs_skip_nonws_len(z2.begin, &len); + + /* z1 : dst name, z2 : norm */ + snprintf(pa->dst, sizeof(pa->dst), + "%.*s", (int)(z1.end-z1.begin), z1.begin); + snprintf(buf, sizeof(buf), + "%.*s", (int)(z2.end-z2.begin), z2.begin); + pa->norm = _bcs_strtoul(buf, NULL, 0); + + return 0; +} + +static int +find_dst(struct parse_arg *pasrc, const char *dst) +{ + int ret; + struct parse_arg padst; + struct _lookup *cl; + struct _region data; + + ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE); + if (ret) + return ret; + + ret = _lookup_seq_lookup(cl, pasrc->dst, &data); + while (ret == 0) { + ret = parse_line(&padst, &data); + if (ret) + break; + if (strcmp(dst, padst.dst) == 0) { + pasrc->norm += padst.norm; + break; + } + ret = _lookup_seq_next(cl, NULL, &data); + } + _lookup_seq_close(cl); + + return ret; +} + +static int +find_best_pivot_lookup(const char *src, const char *dst, char *pivot, + size_t pvlen, unsigned long *rnorm) +{ + int ret; + struct _lookup *cl; + struct _region data; + struct parse_arg pa; + unsigned long norm_min; + char pivot_min[PATH_MAX]; + + ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE); + if (ret) + return ret; + + norm_min = ULONG_MAX; + + /* find pivot code */ + ret = _lookup_seq_lookup(cl, src, &data); + while (ret == 0) { + ret = parse_line(&pa, &data); + if (ret) + break; + ret = find_dst(&pa, dst); + if (ret) + break; + if (pa.norm < norm_min) { + norm_min = pa.norm; + strlcpy(pivot_min, pa.dst, sizeof(pivot_min)); + } + ret = _lookup_seq_next(cl, NULL, &data); + } + _lookup_seq_close(cl); + + if (ret != ENOENT) + return ret; + if (norm_min == ULONG_MAX) + return ENOENT; + strlcpy(pivot, pivot_min, pvlen); + if (rnorm) + *rnorm = norm_min; + + return 0; +} + +static int +find_best_pivot(const char *src, const char *dst, char *pivot, size_t pvlen, + unsigned long *rnorm) +{ + int ret; + + ret = find_best_pivot_pvdb(src, dst, pivot, pvlen, rnorm); + if (ret == NO_SUCH_FILE) + ret = find_best_pivot_lookup(src, dst, pivot, pvlen, rnorm); + + return ret; +} + +static __inline int +open_serial_mapper(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char *src, const char *pivot, const char *dst) +{ + char buf[PATH_MAX]; + + snprintf(buf, sizeof(buf), "%s/%s,%s/%s", src, pivot, pivot, dst); + + return _mapper_open_direct(ma, rcm, "mapper_serial", buf); +} + +static struct _citrus_csmapper *csm_none = NULL; +static int +get_none(struct _citrus_mapper_area *__restrict ma, + struct _citrus_csmapper *__restrict *__restrict rcsm) +{ + int ret; + + rwlock_wrlock(&lock); + if (csm_none) { + *rcsm = csm_none; + ret = 0; + goto quit; + } + + ret = _mapper_open_direct(ma, &csm_none, "mapper_none", ""); + if (ret) + goto quit; + _mapper_set_persistent(csm_none); + + *rcsm = csm_none; + ret = 0; +quit: + rwlock_unlock(&lock); + return ret; +} + +int +_citrus_csmapper_open(struct _citrus_csmapper * __restrict * __restrict rcsm, + const char * __restrict src, const char * __restrict dst, + uint32_t flags, unsigned long *rnorm) +{ + int ret; + char buf1[PATH_MAX], buf2[PATH_MAX], key[PATH_MAX], pivot[PATH_MAX]; + const char *realsrc, *realdst; + unsigned long norm; + + norm = 0; /* XXX gcc */ + + ret = _citrus_mapper_create_area(&maparea, _PATH_CSMAPPER); + if (ret) + return ret; + + realsrc = _lookup_alias(CS_ALIAS, src, buf1, sizeof(buf1), + _LOOKUP_CASE_IGNORE); + realdst = _lookup_alias(CS_ALIAS, dst, buf2, sizeof(buf2), + _LOOKUP_CASE_IGNORE); + if (!strcmp(realsrc, realdst)) { + ret = get_none(maparea, rcsm); + if (ret == 0 && rnorm != NULL) + *rnorm = 0; + return ret; + } + + snprintf(key, sizeof(key), "%s/%s", realsrc, realdst); + + ret = _mapper_open(maparea, rcsm, key); + if (ret == 0) { + if (rnorm != NULL) + *rnorm = 0; + return 0; + } + if (ret != ENOENT || (flags & _CSMAPPER_F_PREVENT_PIVOT)!=0) + return ret; + + ret = find_best_pivot(realsrc, realdst, pivot, sizeof(pivot), &norm); + if (ret) + return ret; + + ret = open_serial_mapper(maparea, rcsm, realsrc, pivot, realdst); + if (ret == 0 && rnorm != NULL) + *rnorm = norm; + + return ret; +} diff --git a/lib/nbsd_libc/citrus/citrus_csmapper.h b/lib/nbsd_libc/citrus/citrus_csmapper.h new file mode 100644 index 000000000..31b06e401 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_csmapper.h @@ -0,0 +1,48 @@ +/* $NetBSD: citrus_csmapper.h,v 1.2 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_CSMAPPER_H_ +#define _CITRUS_CSMAPPER_H + +#define _citrus_csmapper _citrus_mapper +#define _citrus_csmapper_close _citrus_mapper_close +#define _citrus_csmapper_convert _citrus_mapper_convert +#define _citrus_csmapper_init_state _citrus_mapper_init_state +#define _citrus_csmapper_get_state_size _citrus_mapper_get_state_size +#define _citrus_csmapper_get_src_max _citrus_mapper_get_src_max +#define _citrus_csmapper_get_dst_max _citrus_mapper_get_dst_max + +#define _CITRUS_CSMAPPER_F_PREVENT_PIVOT 0x00000001 +__BEGIN_DECLS +int _citrus_csmapper_open(struct _citrus_csmapper *__restrict *__restrict, + const char *__restrict, + const char *__restrict, uint32_t, + unsigned long *); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_ctype.c b/lib/nbsd_libc/citrus/citrus_ctype.c new file mode 100644 index 000000000..d661c2a9f --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_ctype.c @@ -0,0 +1,210 @@ +/* $NetBSD: citrus_ctype.c,v 1.5 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)1999, 2000, 2001, 2002 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_ctype.c,v 1.5 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_ctype_fallback.h" +#include "citrus_none.h" +#include _CITRUS_DEFAULT_CTYPE_HEADER + +_citrus_ctype_rec_t _citrus_ctype_default = { + &_CITRUS_DEFAULT_CTYPE_OPS, /* cc_ops */ + NULL, /* cc_closure */ + NULL /* cc_module */ +}; + +#ifdef _I18N_DYNAMIC + +static int _initctypemodule(_citrus_ctype_t, char const *, _citrus_module_t, + void *, size_t, size_t); + +static int +_initctypemodule(_citrus_ctype_t cc, char const *modname, + _citrus_module_t handle, void *variable, size_t lenvar, + size_t szpriv) +{ + int ret; + _citrus_ctype_getops_t getops; + + _DIAGASSERT(cc != NULL); + + cc->cc_module = handle; + + getops = (_citrus_ctype_getops_t)_citrus_find_getops(cc->cc_module, + modname, + "ctype"); + if (getops == NULL) + return (EINVAL); + + cc->cc_ops = (_citrus_ctype_ops_rec_t *)malloc(sizeof(*cc->cc_ops)); + if (cc->cc_ops == NULL) + return (ENOMEM); + + ret = (*getops)(cc->cc_ops, sizeof(*cc->cc_ops), + _CITRUS_CTYPE_ABI_VERSION); + if (ret) + goto bad; + + /* If return ABI version is not expected, fixup it here*/ + switch (cc->cc_ops->co_abi_version) { + case 0x00000001: + cc->cc_ops->co_btowc = &_citrus_ctype_btowc_fallback; + cc->cc_ops->co_wctob = &_citrus_ctype_wctob_fallback; + /* FALLTHROUGH */ + case 0x00000002: + /* FALLTHROUGH */ + default: + break; + } + + /* validation check */ + if (cc->cc_ops->co_init == NULL || + cc->cc_ops->co_uninit == NULL || + cc->cc_ops->co_get_mb_cur_max == NULL || + cc->cc_ops->co_mblen == NULL || + cc->cc_ops->co_mbrlen == NULL || + cc->cc_ops->co_mbrtowc == NULL || + cc->cc_ops->co_mbsinit == NULL || + cc->cc_ops->co_mbsrtowcs == NULL || + cc->cc_ops->co_mbstowcs == NULL || + cc->cc_ops->co_mbtowc == NULL || + cc->cc_ops->co_wcrtomb == NULL || + cc->cc_ops->co_wcsrtombs == NULL || + cc->cc_ops->co_wcstombs == NULL || + cc->cc_ops->co_wctomb == NULL || + cc->cc_ops->co_btowc == NULL || + cc->cc_ops->co_wctob == NULL) + goto bad; + + /* init and get closure */ + ret = (*cc->cc_ops->co_init)( + &cc->cc_closure, variable, lenvar, szpriv); + if (ret) + goto bad; + + return (0); + +bad: + if (cc->cc_ops) + free(cc->cc_ops); + cc->cc_ops = NULL; + + return (ret); +} + +int +_citrus_ctype_open(_citrus_ctype_t *rcc, + char const *encname, void *variable, size_t lenvar, + size_t szpriv) +{ + int ret; + _citrus_module_t handle; + _citrus_ctype_t cc; + + _DIAGASSERT(encname != NULL); + _DIAGASSERT(!lenvar || variable!=NULL); + _DIAGASSERT(rcc != NULL); + + if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) { + *rcc = &_citrus_ctype_default; + return (0); + } + ret = _citrus_load_module(&handle, encname); + if (ret) + return (ret); + + cc = calloc(1, sizeof(*cc)); + if (!cc) { + _citrus_unload_module(handle); + return (errno); + } + + ret = _initctypemodule(cc, encname, handle, variable, lenvar, szpriv); + if (ret) { + _citrus_unload_module(cc->cc_module); + free(cc); + return (ret); + } + + *rcc = cc; + + return (0); +} + +void +_citrus_ctype_close(_citrus_ctype_t cc) +{ + + _DIAGASSERT(cc != NULL); + + if (cc == &_citrus_ctype_default) + return; + (*cc->cc_ops->co_uninit)(cc->cc_closure); + free(cc->cc_ops); + _citrus_unload_module(cc->cc_module); + free(cc); +} + +#else +/* !_I18N_DYNAMIC */ + +int +/*ARGSUSED*/ +_citrus_ctype_open(_citrus_ctype_t *rcc, + char const *encname, void *variable, size_t lenvar, + size_t szpriv) +{ + if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) { + *rcc = &_citrus_ctype_default; + return (0); + } + return (EINVAL); +} + +void +/*ARGSUSED*/ +_citrus_ctype_close(_citrus_ctype_t cc) +{ +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_ctype.h b/lib/nbsd_libc/citrus/citrus_ctype.h new file mode 100644 index 000000000..aeb0a2bea --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_ctype.h @@ -0,0 +1,170 @@ +/* $NetBSD: citrus_ctype.h,v 1.2 2003/03/05 20:18:15 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_CTYPE_H_ +#define _CITRUS_CTYPE_H_ + +#include "citrus_ctype_local.h" + +typedef struct _citrus_ctype_rec *_citrus_ctype_t; + +__BEGIN_DECLS +int _citrus_ctype_open(_citrus_ctype_t * __restrict, + char const * __restrict, void * __restrict, + size_t, size_t); +void _citrus_ctype_close(_citrus_ctype_t); +__END_DECLS + +static __inline unsigned +_citrus_ctype_get_mb_cur_max(_citrus_ctype_t cc) +{ + + _DIAGASSERT(cc && cc->cc_ops); + return (*cc->cc_ops->co_get_mb_cur_max)(cc->cc_closure); +} + +static __inline int +_citrus_ctype_mblen(_citrus_ctype_t cc, const char *s, size_t n, int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mblen && nresult); + return (*cc->cc_ops->co_mblen)(cc->cc_closure, s, n, nresult); +} + +static __inline int +_citrus_ctype_mbrlen(_citrus_ctype_t cc, const char *s, size_t n, + void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbrlen && nresult); + return (*cc->cc_ops->co_mbrlen)(cc->cc_closure, s, n, pspriv, nresult); +} + +static __inline int +_citrus_ctype_mbrtowc(_citrus_ctype_t cc, wchar_t *pwc, const char *s, + size_t n, void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbrtowc); + return (*cc->cc_ops->co_mbrtowc)(cc->cc_closure, pwc, s, n, pspriv, + nresult); +} + +static __inline int +_citrus_ctype_mbsinit(_citrus_ctype_t cc, void const *pspriv, int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbsinit && nresult); + return (*cc->cc_ops->co_mbsinit)(cc->cc_closure, pspriv, nresult); +} + +static __inline int +_citrus_ctype_mbsrtowcs(_citrus_ctype_t cc, wchar_t *pwcs, const char **s, + size_t n, void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbsrtowcs && nresult); + return (*cc->cc_ops->co_mbsrtowcs)(cc->cc_closure, pwcs, s, n, pspriv, + nresult); +} + +static __inline int +_citrus_ctype_mbstowcs(_citrus_ctype_t cc, wchar_t *pwcs, const char *s, + size_t n, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbstowcs && nresult); + return (*cc->cc_ops->co_mbstowcs)(cc->cc_closure, pwcs, s, n, nresult); +} + +static __inline int +_citrus_ctype_mbtowc(_citrus_ctype_t cc, wchar_t *pw, const char *s, size_t n, + int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbtowc && nresult); + return (*cc->cc_ops->co_mbtowc)(cc->cc_closure, pw, s, n, nresult); +} + +static __inline int +_citrus_ctype_wcrtomb(_citrus_ctype_t cc, char *s, wchar_t wc, + void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wcrtomb && nresult); + return (*cc->cc_ops->co_wcrtomb)(cc->cc_closure, s, wc, pspriv, + nresult); +} + +static __inline int +_citrus_ctype_wcsrtombs(_citrus_ctype_t cc, char *s, const wchar_t **ppwcs, + size_t n, void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wcsrtombs && nresult); + return (*cc->cc_ops->co_wcsrtombs)(cc->cc_closure, s, ppwcs, n, + pspriv, nresult); +} + +static __inline int +_citrus_ctype_wcstombs(_citrus_ctype_t cc, char *s, const wchar_t *wcs, + size_t n, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wcstombs && nresult); + return (*cc->cc_ops->co_wcstombs)(cc->cc_closure, s, wcs, n, nresult); +} + +static __inline int +_citrus_ctype_wctomb(_citrus_ctype_t cc, char *s, wchar_t wc, int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wctomb && nresult); + return (*cc->cc_ops->co_wctomb)(cc->cc_closure, s, wc, nresult); +} + +static __inline int +_citrus_ctype_btowc(_citrus_ctype_t cc, int c, wint_t *wcresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_btowc && wcresult); + return (*cc->cc_ops->co_btowc)(cc, c, wcresult); +} + +static __inline int +_citrus_ctype_wctob(_citrus_ctype_t cc, wint_t c, int *cresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wctob && cresult); + return (*cc->cc_ops->co_wctob)(cc, c, cresult); +} + +extern _citrus_ctype_rec_t _citrus_ctype_default; + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_ctype_fallback.c b/lib/nbsd_libc/citrus/citrus_ctype_fallback.c new file mode 100644 index 000000000..2adbb8b3b --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_ctype_fallback.c @@ -0,0 +1,110 @@ +/* $NetBSD: citrus_ctype_fallback.c,v 1.2 2003/06/27 14:52:25 yamt Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_ctype_fallback.c,v 1.2 2003/06/27 14:52:25 yamt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include + +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_ctype_fallback.h" + +/* + * for ABI version >= 0x00000002 + */ + +int +_citrus_ctype_btowc_fallback(_citrus_ctype_rec_t * __restrict cc, + int c, wint_t * __restrict wcresult) +{ + char mb; + /* + * what we need is _PRIVSIZE + * and we know that it's smaller than sizeof(mbstate_t). + */ + char pspriv[sizeof(mbstate_t)]; + wchar_t wc; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (c == EOF) { + *wcresult = WEOF; + return 0; + } + + memset(&pspriv, 0, sizeof(pspriv)); + mb = (char)(unsigned)c; + err = _citrus_ctype_mbrtowc(cc, &wc, &mb, 1, (void *)&pspriv, &nr); + if (!err && (nr == 0 || nr == 1)) + *wcresult = wc; + else + *wcresult = WEOF; + + return 0; +} + +int +_citrus_ctype_wctob_fallback(_citrus_ctype_rec_t * __restrict cc, + wint_t wc, int * __restrict cresult) +{ + /* + * what we need is _PRIVSIZE + * and we know that it's smaller than sizeof(mbstate_t). + */ + char pspriv[sizeof(mbstate_t)]; + char buf[MB_LEN_MAX]; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (wc == WEOF) { + *cresult = EOF; + return 0; + } + memset(&pspriv, 0, sizeof(pspriv)); + err = _citrus_ctype_wcrtomb(cc, buf, (wchar_t)wc, (void *)&pspriv, &nr); + if (!err && nr == 1) + *cresult = buf[0]; + else + *cresult = EOF; + + return 0; +} diff --git a/lib/nbsd_libc/citrus/citrus_ctype_fallback.h b/lib/nbsd_libc/citrus/citrus_ctype_fallback.h new file mode 100644 index 000000000..81a20c79a --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_ctype_fallback.h @@ -0,0 +1,38 @@ +/* $NetBSD: citrus_ctype_fallback.h,v 1.1 2003/03/05 20:18:15 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_CTYPE_FALLBACK_H_ +#define _CITRUS_CTYPE_FALLBACK_H_ + +/* fallback functions for 0x00000002 */ +int _citrus_ctype_btowc_fallback(_citrus_ctype_rec_t * __restrict, + int, wint_t * __restrict); +int _citrus_ctype_wctob_fallback(_citrus_ctype_rec_t * __restrict, + wint_t, int * __restrict); + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_ctype_local.h b/lib/nbsd_libc/citrus/citrus_ctype_local.h new file mode 100644 index 000000000..496994619 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_ctype_local.h @@ -0,0 +1,198 @@ +/* $NetBSD: citrus_ctype_local.h,v 1.3 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_CTYPE_LOCAL_H_ +#define _CITRUS_CTYPE_LOCAL_H_ + +#define _CITRUS_CTYPE_GETOPS_FUNC_BASE(_n_) \ +int _n_(_citrus_ctype_ops_rec_t *, size_t, uint32_t) +#define _CITRUS_CTYPE_GETOPS_FUNC(_n_) \ +_CITRUS_CTYPE_GETOPS_FUNC_BASE(_citrus_##_n_##_ctype_getops) + +#define _CITRUS_CTYPE_DECLS(_e_) \ +static int _citrus_##_e_##_ctype_init \ + (void ** __restrict, void * __restrict, size_t, size_t); \ +static void _citrus_##_e_##_ctype_uninit(void *); \ +static unsigned _citrus_##_e_##_ctype_get_mb_cur_max(void *); \ +static int _citrus_##_e_##_ctype_mblen(void * __restrict, \ + const char * __restrict, \ + size_t, int * __restrict); \ +static int _citrus_##_e_##_ctype_mbrlen(void * __restrict, \ + const char * __restrict, \ + size_t, void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbrtowc(void * __restrict, \ + wchar_t * __restrict, \ + const char * __restrict, size_t, \ + void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbsinit(void * __restrict, \ + void const * __restrict, \ + int * __restrict); \ +static int _citrus_##_e_##_ctype_mbsrtowcs(void * __restrict, \ + wchar_t * __restrict, \ + const char ** __restrict, \ + size_t, void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbstowcs(void * __restrict, \ + wchar_t * __restrict, \ + const char * __restrict, \ + size_t, size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbtowc(void * __restrict, \ + wchar_t * __restrict, \ + const char * __restrict, \ + size_t, int * __restrict); \ +static int _citrus_##_e_##_ctype_wcrtomb(void * __restrict, \ + char * __restrict, wchar_t, \ + void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_wcsrtombs(void * __restrict, \ + char * __restrict, \ + const wchar_t ** __restrict, \ + size_t, void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_wcstombs(void * __restrict, \ + char * __restrict, \ + const wchar_t * __restrict, \ + size_t, size_t * __restrict); \ +static int _citrus_##_e_##_ctype_wctomb(void * __restrict, \ + char * __restrict, \ + wchar_t, int * __restrict); \ +static int _citrus_##_e_##_ctype_btowc(_citrus_ctype_rec_t * __restrict, \ + int, wint_t * __restrict); \ +static int _citrus_##_e_##_ctype_wctob(_citrus_ctype_rec_t * __restrict, \ + wint_t, int * __restrict) + +#define _CITRUS_CTYPE_DEF_OPS(_e_) \ +_citrus_ctype_ops_rec_t _citrus_##_e_##_ctype_ops = { \ + /* co_abi_version */ _CITRUS_CTYPE_ABI_VERSION, \ + /* co_init */ &_citrus_##_e_##_ctype_init, \ + /* co_uninit */ &_citrus_##_e_##_ctype_uninit, \ + /* co_get_mb_cur_max */ &_citrus_##_e_##_ctype_get_mb_cur_max, \ + /* co_mblen */ &_citrus_##_e_##_ctype_mblen, \ + /* co_mbrlen */ &_citrus_##_e_##_ctype_mbrlen, \ + /* co_mbrtowc */ &_citrus_##_e_##_ctype_mbrtowc, \ + /* co_mbsinit */ &_citrus_##_e_##_ctype_mbsinit, \ + /* co_mbsrtowcs */ &_citrus_##_e_##_ctype_mbsrtowcs, \ + /* co_mbstowcs */ &_citrus_##_e_##_ctype_mbstowcs, \ + /* co_mbtowc */ &_citrus_##_e_##_ctype_mbtowc, \ + /* co_wcrtomb */ &_citrus_##_e_##_ctype_wcrtomb, \ + /* co_wcsrtombs */ &_citrus_##_e_##_ctype_wcsrtombs, \ + /* co_wcstombs */ &_citrus_##_e_##_ctype_wcstombs, \ + /* co_wctomb */ &_citrus_##_e_##_ctype_wctomb, \ + /* co_btowc */ &_citrus_##_e_##_ctype_btowc, \ + /* co_wctob */ &_citrus_##_e_##_ctype_wctob \ +} + +typedef struct _citrus_ctype_ops_rec _citrus_ctype_ops_rec_t; +typedef struct _citrus_ctype_rec _citrus_ctype_rec_t; + +typedef int (*_citrus_ctype_init_t) + (void ** __restrict, void * __restrict, size_t, size_t); +typedef void (*_citrus_ctype_uninit_t)(void *); +typedef unsigned (*_citrus_ctype_get_mb_cur_max_t)(void *); +typedef int (*_citrus_ctype_mblen_t) + (void * __restrict, const char * __restrict, size_t, int * __restrict); +typedef int (*_citrus_ctype_mbrlen_t) + (void * __restrict, const char * __restrict, size_t, + void * __restrict, size_t * __restrict); +typedef int (*_citrus_ctype_mbrtowc_t) + (void * __restrict, wchar_t * __restrict, const char * __restrict, + size_t, void * __restrict, size_t * __restrict); +typedef int (*_citrus_ctype_mbsinit_t) + (void * __restrict, const void * __restrict, int * __restrict); +typedef int (*_citrus_ctype_mbsrtowcs_t) + (void * __restrict, wchar_t * __restrict, const char ** __restrict, + size_t, void * __restrict, + size_t * __restrict); +typedef int (*_citrus_ctype_mbstowcs_t) + (void * __restrict, wchar_t * __restrict, const char * __restrict, + size_t, size_t * __restrict); +typedef int (*_citrus_ctype_mbtowc_t) + (void * __restrict, wchar_t * __restrict, const char * __restrict, + size_t, int * __restrict); +typedef int (*_citrus_ctype_wcrtomb_t) + (void * __restrict, char * __restrict, wchar_t, void * __restrict, + size_t * __restrict); +typedef int (*_citrus_ctype_wcsrtombs_t) + (void * __restrict, char * __restrict, const wchar_t ** __restrict, + size_t, void * __restrict, size_t * __restrict); +typedef int (*_citrus_ctype_wcstombs_t) + (void * __restrict, char * __restrict, const wchar_t * __restrict, + size_t, size_t * __restrict); +typedef int (*_citrus_ctype_wctomb_t) + (void * __restrict, char * __restrict, wchar_t, int * __restrict); +typedef int (*_citrus_ctype_btowc_t) + (_citrus_ctype_rec_t * __restrict, int, wint_t * __restrict); +typedef int (*_citrus_ctype_wctob_t) + (_citrus_ctype_rec_t * __restrict, wint_t, int * __restrict); + +/* + * ABI Version change log: + * 0x00000001 + * initial version + * 0x00000002 + * ops record: btowc and wctob are added. + * ctype record: unchanged. + */ +#define _CITRUS_CTYPE_ABI_VERSION 0x00000002 +struct _citrus_ctype_ops_rec { + uint32_t co_abi_version; + /* version 0x00000001 */ + _citrus_ctype_init_t co_init; + _citrus_ctype_uninit_t co_uninit; + _citrus_ctype_get_mb_cur_max_t co_get_mb_cur_max; + _citrus_ctype_mblen_t co_mblen; + _citrus_ctype_mbrlen_t co_mbrlen; + _citrus_ctype_mbrtowc_t co_mbrtowc; + _citrus_ctype_mbsinit_t co_mbsinit; + _citrus_ctype_mbsrtowcs_t co_mbsrtowcs; + _citrus_ctype_mbstowcs_t co_mbstowcs; + _citrus_ctype_mbtowc_t co_mbtowc; + _citrus_ctype_wcrtomb_t co_wcrtomb; + _citrus_ctype_wcsrtombs_t co_wcsrtombs; + _citrus_ctype_wcstombs_t co_wcstombs; + _citrus_ctype_wctomb_t co_wctomb; + /* version 0x00000002 */ + _citrus_ctype_btowc_t co_btowc; + _citrus_ctype_wctob_t co_wctob; +}; + +#define _CITRUS_DEFAULT_CTYPE_NAME "NONE" +#define _CITRUS_DEFAULT_CTYPE_OPS _citrus_NONE_ctype_ops +#define _CITRUS_DEFAULT_CTYPE_HEADER "citrus_none.h" + +typedef _CITRUS_CTYPE_GETOPS_FUNC_BASE((*_citrus_ctype_getops_t)); +struct _citrus_ctype_rec { + _citrus_ctype_ops_rec_t *cc_ops; + void *cc_closure; + _citrus_module_t cc_module; +}; + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_ctype_template.h b/lib/nbsd_libc/citrus/citrus_ctype_template.h new file mode 100644 index 000000000..c61dedf89 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_ctype_template.h @@ -0,0 +1,767 @@ +/* $NetBSD: citrus_ctype_template.h,v 1.35 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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. + */ + + +/* + * CAUTION: THIS IS NOT STANDALONE FILE + * + * function templates of ctype encoding handler for each encodings. + * + * you need to define the macros below: + * + * _FUNCNAME(method) : + * It should convine the real function name for the method. + * e.g. _FUNCNAME(mbrtowc) should be expanded to + * _EUC_ctype_mbrtowc + * for EUC locale. + * + * _CEI_TO_STATE(cei, method) : + * It should be expanded to the pointer of the method-internal state + * structures. + * e.g. _CEI_TO_STATE(cei, mbrtowc) might be expanded to + * (cei)->states.s_mbrtowc + * This structure may use if the function is called as + * mbrtowc(&wc, s, n, NULL); + * Such individual structures are needed by: + * mblen + * mbrlen + * mbrtowc + * mbtowc + * mbsrtowcs + * wcrtomb + * wcsrtombs + * wctomb + * These need to be keeped in the ctype encoding information structure, + * pointed by "cei". + * + * _ENCODING_INFO : + * It should be expanded to the name of the encoding information structure. + * e.g. For EUC encoding, this macro is expanded to _EUCInfo. + * Encoding information structure need to contain the common informations + * for the codeset. + * + * _ENCODING_STATE : + * It should be expanded to the name of the encoding state structure. + * e.g. For EUC encoding, this macro is expanded to _EUCState. + * Encoding state structure need to contain the context-dependent states, + * which are "unpacked-form" of mbstate_t type and keeped during sequent + * calls of mb/wc functions, + * + * _ENCODING_IS_STATE_DEPENDENT : + * If the encoding is state dependent, this should be expanded to + * non-zero integral value. Otherwise, 0. + * + * _STATE_NEEDS_EXPLICIT_INIT(ps) : + * some encodings, states needs some explicit initialization. + * (ie. initialization with memset isn't enough.) + * If the encoding state pointed by "ps" needs to be initialized + * explicitly, return non-zero. Otherwize, 0. + * + */ + + +/* prototypes */ + +__BEGIN_DECLS +static void _FUNCNAME(init_state)(_ENCODING_INFO * __restrict, + _ENCODING_STATE * __restrict); +static void _FUNCNAME(pack_state)(_ENCODING_INFO * __restrict, + void * __restrict, + const _ENCODING_STATE * __restrict); +static void _FUNCNAME(unpack_state)(_ENCODING_INFO * __restrict, + _ENCODING_STATE * __restrict, + const void * __restrict); +#if _ENCODING_IS_STATE_DEPENDENT +static int _FUNCNAME(put_state_reset)(_ENCODING_INFO * __restrict, + char * __restrict, size_t, + _ENCODING_STATE * __restrict, + size_t * __restrict); +#endif + +/* + * standard form of mbrtowc_priv. + * + * note (differences from real mbrtowc): + * - 3rd parameter is not "const char *s" but "const char **s". + * after the call of the function, *s will point the first byte of + * the next character. + * - additional 4th parameter is the size of src buffer. + * - 5th parameter is unpacked encoding-dependent state structure. + * - additional 6th parameter is the storage to be stored + * the return value in the real mbrtowc context. + * - return value means "errno" in the real mbrtowc context. + */ + +static int _FUNCNAME(mbrtowc_priv)(_ENCODING_INFO * __restrict, + wchar_t * __restrict, + const char ** __restrict, + size_t, _ENCODING_STATE * __restrict, + size_t * __restrict); + +/* + * standard form of wcrtomb_priv. + * + * note (differences from real wcrtomb): + * - additional 3th parameter is the size of src buffer. + * - 5th parameter is unpacked encoding-dependent state structure. + * - additional 6th parameter is the storage to be stored + * the return value in the real mbrtowc context. + * - return value means "errno" in the real wcrtomb context. + * - caller should ensure that 2nd parameter isn't NULL. + * (XXX inconsist with mbrtowc_priv) + */ + +static int _FUNCNAME(wcrtomb_priv)(_ENCODING_INFO * __restrict, + char * __restrict, size_t, wchar_t, + _ENCODING_STATE * __restrict, + size_t * __restrict); +__END_DECLS + + +/* + * macros + */ + +#define _TO_CEI(_cl_) ((_CTYPE_INFO*)(_cl_)) + + +/* + * templates + */ + +/* internal routines */ + +static __inline int +_FUNCNAME(mbtowc_priv)(_ENCODING_INFO * __restrict ei, + wchar_t * __restrict pwc, const char * __restrict s, + size_t n, _ENCODING_STATE * __restrict psenc, + int * __restrict nresult) +{ + _ENCODING_STATE state; + size_t nr; + int err = 0; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + + if (s == NULL) { + _FUNCNAME(init_state)(ei, psenc); + *nresult = _ENCODING_IS_STATE_DEPENDENT; + return (0); + } + + state = *psenc; + err = _FUNCNAME(mbrtowc_priv)(ei, pwc, (const char **)&s, n, psenc, &nr); + if (nr == (size_t)-2) + err = EILSEQ; + if (err) { + /* In error case, we should restore the state. */ + *psenc = state; + *nresult = -1; + return (err); + } + + *nresult = (int)nr; + + return (0); +} + +static int +_FUNCNAME(mbsrtowcs_priv)(_ENCODING_INFO * __restrict ei, + wchar_t * __restrict pwcs, + const char ** __restrict s, + size_t n, _ENCODING_STATE * __restrict psenc, + size_t * __restrict nresult) +{ + int err, cnt; + size_t siz; + const char *s0; + size_t mbcurmax; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s == NULL); + _DIAGASSERT(*s == NULL); + + /* if pwcs is NULL, ignore n */ + if (pwcs == NULL) + n = 1; /* arbitrary >0 value */ + + err = cnt = 0; + s0 = *s; /* to keep *s unchanged for now, use copy instead. */ + mbcurmax = _ENCODING_MB_CUR_MAX(ei); + while (n > 0) { + err = _FUNCNAME(mbrtowc_priv)(ei, pwcs, &s0, mbcurmax, + psenc, &siz); + if (siz == (size_t)-2) + err = EILSEQ; + if (err) { + cnt = -1; + goto bye; + } + switch (siz) { + case 0: + if (pwcs) { + _FUNCNAME(init_state)(ei, psenc); + } + s0 = 0; + goto bye; + default: + if (pwcs) { + pwcs++; + n--; + } + cnt++; + break; + } + } +bye: + if (pwcs) + *s = s0; + + *nresult = (size_t)cnt; + + return err; +} + + +static int +_FUNCNAME(wcsrtombs_priv)(_ENCODING_INFO * __restrict ei, char * __restrict s, + const wchar_t ** __restrict pwcs, + size_t n, _ENCODING_STATE * __restrict psenc, + size_t * __restrict nresult) +{ + int cnt = 0, err; + char buf[MB_LEN_MAX]; + size_t siz; + const wchar_t* pwcs0; +#if _ENCODING_IS_STATE_DEPENDENT + _ENCODING_STATE state; +#endif + + pwcs0 = *pwcs; + + if (!s) + n = 1; + + while (n > 0) { +#if _ENCODING_IS_STATE_DEPENDENT + state = *psenc; +#endif + err = _FUNCNAME(wcrtomb_priv)(ei, buf, sizeof(buf), + *pwcs0, psenc, &siz); + if (siz == (size_t)-1) { + *nresult = siz; + return (err); + } + + if (s) { + if (n < siz) { +#if _ENCODING_IS_STATE_DEPENDENT + *psenc = state; +#endif + break; + } + memcpy(s, buf, siz); + s += siz; + n -= siz; + } + cnt += siz; + if (!*pwcs0) { + if (s) { + _FUNCNAME(init_state)(ei, psenc); + } + pwcs0 = 0; + cnt--; /* don't include terminating null */ + break; + } + pwcs0++; + } + if (s) + *pwcs = pwcs0; + + *nresult = (size_t)cnt; + return (0); +} + + +/* ---------------------------------------------------------------------- + * templates for public functions + */ + +#define _RESTART_BEGIN(_func_, _cei_, _pspriv_, _pse_) \ +do { \ + _ENCODING_STATE _state; \ + do { \ + if (_pspriv_ == NULL) { \ + _pse_ = &_CEI_TO_STATE(_cei_, _func_); \ + if (_STATE_NEEDS_EXPLICIT_INIT(_pse_)) \ + _FUNCNAME(init_state)(_CEI_TO_EI(_cei_), \ + (_pse_)); \ + } else { \ + _pse_ = &_state; \ + _FUNCNAME(unpack_state)(_CEI_TO_EI(_cei_), \ + _pse_, _pspriv_); \ + } \ + } while (/*CONSTCOND*/0) + +#define _RESTART_END(_func_, _cei_, _pspriv_, _pse_) \ + if (_pspriv_ != NULL) { \ + _FUNCNAME(pack_state)(_CEI_TO_EI(_cei_), _pspriv_, \ + _pse_); \ + } \ +} while (/*CONSTCOND*/0) + +int +_FUNCNAME(ctype_getops)(_citrus_ctype_ops_rec_t *ops, size_t lenops, + uint32_t expected_version) +{ + if (expected_version<_CITRUS_CTYPE_ABI_VERSION || lenops lenps) + return (EINVAL); + + cei = calloc(1, sizeof(_CTYPE_INFO)); + if (cei == NULL) + return (ENOMEM); + + *cl = (void *)cei; + + return _FUNCNAME(encoding_module_init)(_CEI_TO_EI(cei), var, lenvar); +} + +static void +_FUNCNAME(ctype_uninit)(void *cl) +{ + if (cl) { + _FUNCNAME(encoding_module_uninit)(_CEI_TO_EI(_TO_CEI(cl))); + free(cl); + } +} + +static unsigned +/*ARGSUSED*/ +_FUNCNAME(ctype_get_mb_cur_max)(void *cl) +{ + return _ENCODING_MB_CUR_MAX(_CEI_TO_EI(_TO_CEI(cl))); +} + +static int +_FUNCNAME(ctype_mblen)(void * __restrict cl, + const char * __restrict s, size_t n, + int * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + + _DIAGASSERT(cl != NULL); + + psenc = &_CEI_TO_STATE(_TO_CEI(cl), mblen); + ei = _CEI_TO_EI(_TO_CEI(cl)); + if (_STATE_NEEDS_EXPLICIT_INIT(psenc)) + _FUNCNAME(init_state)(ei, psenc); + return _FUNCNAME(mbtowc_priv)(ei, NULL, s, n, psenc, nresult); +} + +static int +_FUNCNAME(ctype_mbrlen)(void * __restrict cl, const char * __restrict s, + size_t n, void * __restrict pspriv, + size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(mbrlen, _TO_CEI(cl), pspriv, psenc); + if (s == NULL) { + _FUNCNAME(init_state)(ei, psenc); + *nresult = 0; + } else { + err = _FUNCNAME(mbrtowc_priv)(ei, NULL, (const char **)&s, n, + (void *)psenc, nresult); + } + _RESTART_END(mbrlen, _TO_CEI(cl), pspriv, psenc); + + return (err); +} + +static int +_FUNCNAME(ctype_mbrtowc)(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + void * __restrict pspriv, size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(mbrtowc, _TO_CEI(cl), pspriv, psenc); + if (s == NULL) { + _FUNCNAME(init_state)(ei, psenc); + *nresult = 0; + } else { + err = _FUNCNAME(mbrtowc_priv)(ei, pwc, (const char **)&s, n, + (void *)psenc, nresult); + } + _RESTART_END(mbrtowc, _TO_CEI(cl), pspriv, psenc); + + return (err); +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_mbsinit)(void * __restrict cl, const void * __restrict pspriv, + int * __restrict nresult) +{ + _ENCODING_STATE state; + + if (pspriv == NULL) { + *nresult = 1; + return (0); + } + + _FUNCNAME(unpack_state)(_CEI_TO_EI(_TO_CEI(cl)), &state, pspriv); + + *nresult = (state.chlen == 0); /* XXX: FIXME */ + + return (0); +} + +static int +_FUNCNAME(ctype_mbsrtowcs)(void * __restrict cl, wchar_t * __restrict pwcs, + const char ** __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(mbsrtowcs, _TO_CEI(cl), pspriv, psenc); + err = _FUNCNAME(mbsrtowcs_priv)(ei, pwcs, s, n, psenc, nresult); + _RESTART_END(mbsrtowcs, _TO_CEI(cl), pspriv, psenc); + + return (err); +} + +static int +_FUNCNAME(ctype_mbstowcs)(void * __restrict cl, wchar_t * __restrict pwcs, + const char * __restrict s, size_t n, + size_t * __restrict nresult) +{ + int err; + _ENCODING_STATE state; + _ENCODING_INFO *ei; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _FUNCNAME(init_state)(ei, &state); + err = _FUNCNAME(mbsrtowcs_priv)(ei, pwcs, (const char **)&s, n, + &state, nresult); + if (*nresult == (size_t)-2) { + err = EILSEQ; + *nresult = (size_t)-1; + } + + return (err); +} + +static int +_FUNCNAME(ctype_mbtowc)(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + int * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + + _DIAGASSERT(cl != NULL); + + psenc = &_CEI_TO_STATE(_TO_CEI(cl), mbtowc); + ei = _CEI_TO_EI(_TO_CEI(cl)); + if (_STATE_NEEDS_EXPLICIT_INIT(psenc)) + _FUNCNAME(init_state)(ei, psenc); + return _FUNCNAME(mbtowc_priv)(ei, pwc, s, n, psenc, nresult); +} + +static int +_FUNCNAME(ctype_wcrtomb)(void * __restrict cl, char * __restrict s, wchar_t wc, + void * __restrict pspriv, size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + char buf[MB_LEN_MAX]; + int err = 0; + size_t sz; +#if _ENCODING_IS_STATE_DEPENDENT + size_t rsz = 0; +#endif + + _DIAGASSERT(cl != NULL); + + if (s == NULL) { + /* + * use internal buffer. + */ + s = buf; + wc = L'\0'; /* SUSv3 */ + } + + _RESTART_BEGIN(wcrtomb, _TO_CEI(cl), pspriv, psenc); + sz = _ENCODING_MB_CUR_MAX(_CEI_TO_EI(_TO_CEI(cl))); +#if _ENCODING_IS_STATE_DEPENDENT + if (wc == L'\0') { + /* reset state */ + err = _FUNCNAME(put_state_reset)(_CEI_TO_EI(_TO_CEI(cl)), s, + sz, psenc, &rsz); + if (err) { + *nresult = -1; + goto quit; + } + s += rsz; + sz -= rsz; + } +#endif + err = _FUNCNAME(wcrtomb_priv)(_CEI_TO_EI(_TO_CEI(cl)), s, sz, + wc, psenc, nresult); +#if _ENCODING_IS_STATE_DEPENDENT + if (err == 0) + *nresult += rsz; +quit: +#endif + if (err == E2BIG) + err = EINVAL; + _RESTART_END(wcrtomb, _TO_CEI(cl), pspriv, psenc); + + return err; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_wcsrtombs)(void * __restrict cl, char * __restrict s, + const wchar_t ** __restrict pwcs, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(wcsrtombs, _TO_CEI(cl), pspriv, psenc); + err = _FUNCNAME(wcsrtombs_priv)(ei, s, pwcs, n, psenc, nresult); + _RESTART_END(wcsrtombs, _TO_CEI(cl), pspriv, psenc); + + return err; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_wcstombs)(void * __restrict cl, char * __restrict s, + const wchar_t * __restrict pwcs, size_t n, + size_t * __restrict nresult) +{ + _ENCODING_STATE state; + _ENCODING_INFO *ei; + int err; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _FUNCNAME(init_state)(ei, &state); + err = _FUNCNAME(wcsrtombs_priv)(ei, s, (const wchar_t **)&pwcs, n, + &state, nresult); + + return err; +} + +static int +_FUNCNAME(ctype_wctomb)(void * __restrict cl, char * __restrict s, wchar_t wc, + int * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + size_t nr, sz; +#if _ENCODING_IS_STATE_DEPENDENT + size_t rsz = 0; +#endif + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + psenc = &_CEI_TO_STATE(_TO_CEI(cl), wctomb); + if (_STATE_NEEDS_EXPLICIT_INIT(psenc)) + _FUNCNAME(init_state)(ei, psenc); + if (s == NULL) { + _FUNCNAME(init_state)(ei, psenc); + *nresult = _ENCODING_IS_STATE_DEPENDENT; + return 0; + } + sz = _ENCODING_MB_CUR_MAX(_CEI_TO_EI(_TO_CEI(cl))); +#if _ENCODING_IS_STATE_DEPENDENT + if (wc == L'\0') { + /* reset state */ + err = _FUNCNAME(put_state_reset)(_CEI_TO_EI(_TO_CEI(cl)), s, + sz, psenc, &rsz); + if (err) { + *nresult = -1; /* XXX */ + return 0; + } + s += rsz; + sz -= rsz; + } +#endif + err = _FUNCNAME(wcrtomb_priv)(ei, s, sz, wc, psenc, &nr); +#if _ENCODING_IS_STATE_DEPENDENT + if (err == 0) + *nresult = (int)(nr + rsz); + else +#endif + *nresult = (int)nr; + + return 0; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_btowc)(_citrus_ctype_rec_t * __restrict cc, + int c, wint_t * __restrict wcresult) +{ + _ENCODING_STATE state; + _ENCODING_INFO *ei; + char mb; + char const *s; + wchar_t wc; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (c == EOF) { + *wcresult = WEOF; + return 0; + } + ei = _CEI_TO_EI(_TO_CEI(cc->cc_closure)); + _FUNCNAME(init_state)(ei, &state); + mb = (char)(unsigned)c; + s = &mb; + err = _FUNCNAME(mbrtowc_priv)(ei, &wc, &s, 1, &state, &nr); + if (!err && (nr == 0 || nr == 1)) + *wcresult = (wint_t)wc; + else + *wcresult = WEOF; + + return 0; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_wctob)(_citrus_ctype_rec_t * __restrict cc, + wint_t wc, int * __restrict cresult) +{ + _ENCODING_STATE state; + _ENCODING_INFO *ei; + char buf[MB_LEN_MAX]; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (wc == WEOF) { + *cresult = EOF; + return 0; + } + ei = _CEI_TO_EI(_TO_CEI(cc->cc_closure)); + _FUNCNAME(init_state)(ei, &state); + err = _FUNCNAME(wcrtomb_priv)(ei, buf, _ENCODING_MB_CUR_MAX(ei), + (wchar_t)wc, &state, &nr); + if (!err && nr == 1) + *cresult = buf[0]; + else + *cresult = EOF; + + return 0; +} diff --git a/lib/nbsd_libc/citrus/citrus_db.c b/lib/nbsd_libc/citrus/citrus_db.c new file mode 100644 index 000000000..fc7e0140f --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_db.c @@ -0,0 +1,340 @@ +/* $NetBSD: citrus_db.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_db.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_db.h" +#include "citrus_db_file.h" + +struct _citrus_db { + /* private */ + struct _region db_region; + uint32_t (*db_hashfunc)(void *, struct _citrus_region *); + void *db_hashfunc_closure; +}; + +int +_citrus_db_open(struct _citrus_db **rdb, struct _region *r, const char *magic, + uint32_t (*hashfunc)(void *, struct _citrus_region *), + void *hashfunc_closure) +{ + struct _memstream ms; + struct _citrus_db *db; + struct _citrus_db_header_x *dhx; + + _memstream_bind(&ms, r); + + /* sanity check */ + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + if (dhx == NULL) + return EFTYPE; + if (strncmp(dhx->dhx_magic, magic, _CITRUS_DB_MAGIC_SIZE) != 0) + return EFTYPE; + if (_memstream_seek(&ms, be32toh(dhx->dhx_entry_offset), SEEK_SET)) + return EFTYPE; + + if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE > + _memstream_remainder(&ms)) + return EFTYPE; + + db = malloc(sizeof(*db)); + if (db==NULL) + return errno; + db->db_region = *r; + db->db_hashfunc = hashfunc; + db->db_hashfunc_closure = hashfunc_closure; + *rdb = db; + + return 0; +} + +void +_citrus_db_close(struct _citrus_db *db) +{ + free(db); +} + +int +_citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key, + struct _citrus_region *data, struct _citrus_db_locator *dl) +{ + uint32_t hashval, num_entries; + size_t offset; + struct _memstream ms; + struct _citrus_db_header_x *dhx; + struct _citrus_db_entry_x *dex; + struct _citrus_region r; + + _memstream_bind(&ms, &db->db_region); + + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + _DIAGASSERT(dhx); + num_entries = be32toh(dhx->dhx_num_entries); + if (num_entries == 0) + return ENOENT; + + if (dl != NULL && dl->dl_offset>0) { + hashval = dl->dl_hashval; + offset = dl->dl_offset; + if (offset >= _region_size(&db->db_region)) + return ENOENT; + } else { + hashval = + db->db_hashfunc(db->db_hashfunc_closure, key)%num_entries; + offset = + be32toh(dhx->dhx_entry_offset) + + hashval * _CITRUS_DB_ENTRY_SIZE; + if (dl) + dl->dl_hashval = hashval; + } + do { + /* seek to the next entry */ + if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET)) + return EFTYPE; + /* get the entry record */ + dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE); + if (dex == NULL) + return EFTYPE; + + /* jump to next entry having the same hash value. */ + offset = be32toh(dex->dex_next_offset); + + /* save the current position */ + if (dl) { + dl->dl_offset = offset; + if (offset==0) + dl->dl_offset = _region_size(&db->db_region); + } + + /* compare hash value. */ + if (be32toh(dex->dex_hash_value) != hashval) + /* not found */ + break; + /* compare key length */ + if (be32toh(dex->dex_key_size) == _region_size(key)) { + /* seek to the head of the key. */ + if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), + SEEK_SET)) + return EFTYPE; + /* get the region of the key */ + if (_memstream_getregion(&ms, &r, + _region_size(key)) == NULL) + return EFTYPE; + /* compare key byte stream */ + if (memcmp(_region_head(&r), _region_head(key), + _region_size(key)) == 0) { + /* match */ + if (_memstream_seek( + &ms, be32toh(dex->dex_data_offset), + SEEK_SET)) + return EFTYPE; + if (_memstream_getregion( + &ms, data, + be32toh(dex->dex_data_size)) == NULL) + return EFTYPE; + return 0; + } + } + } while (offset != 0); + + return ENOENT; +} + +int +_citrus_db_lookup_by_string(struct _citrus_db *db, const char *key, + struct _citrus_region *data, + struct _citrus_db_locator *dl) +{ + struct _region r; + + _region_init(&r, __UNCONST(key), strlen(key)); + + return _citrus_db_lookup(db, &r, data, dl); +} + +int +_citrus_db_lookup8_by_string(struct _citrus_db *db, const char *key, + uint8_t *rval, struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + if (_region_size(&r) != 1) + return EFTYPE; + + if (rval) + memcpy(rval, _region_head(&r), 1); + + return 0; +} + +int +_citrus_db_lookup16_by_string(struct _citrus_db *db, const char *key, + uint16_t *rval, struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + uint16_t val; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + if (_region_size(&r) != 2) + return EFTYPE; + + if (rval) { + memcpy(&val, _region_head(&r), 2); + *rval = be16toh(val); + } + + return 0; +} + +int +_citrus_db_lookup32_by_string(struct _citrus_db *db, const char *key, + uint32_t *rval, struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + uint32_t val; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + if (_region_size(&r) != 4) + return EFTYPE; + + if (rval) { + memcpy(&val, _region_head(&r), 4); + *rval = be32toh(val); + } + + return 0; +} + +int +_citrus_db_lookup_string_by_string(struct _citrus_db *db, const char *key, + const char **rdata, + struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + /* check whether the string is null terminated */ + if (_region_size(&r) == 0) + return EFTYPE; + if (*((const char*)_region_head(&r)+_region_size(&r)-1) != '\0') + return EFTYPE; + + if (rdata) + *rdata = _region_head(&r); + + return 0; +} + +int +_citrus_db_get_number_of_entries(struct _citrus_db *db) +{ + struct _memstream ms; + struct _citrus_db_header_x *dhx; + + _memstream_bind(&ms, &db->db_region); + + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + _DIAGASSERT(dhx); + return (int)be32toh(dhx->dhx_num_entries); +} + +int +_citrus_db_get_entry(struct _citrus_db *db, int idx, + struct _region *key, struct _region *data) +{ + uint32_t num_entries; + size_t offset; + struct _memstream ms; + struct _citrus_db_header_x *dhx; + struct _citrus_db_entry_x *dex; + + _memstream_bind(&ms, &db->db_region); + + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + _DIAGASSERT(dhx); + num_entries = be32toh(dhx->dhx_num_entries); + if (idx < 0 || (uint32_t)idx >= num_entries) + return EINVAL; + + /* seek to the next entry */ + offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE; + if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET)) + return EFTYPE; + /* get the entry record */ + dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE); + if (dex == NULL) + return EFTYPE; + /* seek to the head of the key. */ + if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET)) + return EFTYPE; + /* get the region of the key. */ + if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL) + return EFTYPE; + /* seek to the head of the data. */ + if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET)) + return EFTYPE; + /* get the region of the data. */ + if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL) + return EFTYPE; + + return 0; +} diff --git a/lib/nbsd_libc/citrus/citrus_db.h b/lib/nbsd_libc/citrus/citrus_db.h new file mode 100644 index 000000000..239e1cd98 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_db.h @@ -0,0 +1,74 @@ +/* $NetBSD: citrus_db.h,v 1.2 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_DB_H_ +#define _CITRUS_DB_H_ + +struct _citrus_db; +struct _citrus_db_locator { + uint32_t dl_hashval; + size_t dl_offset; +}; + +__BEGIN_DECLS +int _citrus_db_open(struct _citrus_db **, struct _citrus_region *, + const char *, + uint32_t (*)(void *, struct _citrus_region *), + void *); +void _citrus_db_close(struct _citrus_db *); +int _citrus_db_lookup(struct _citrus_db *, struct _citrus_region *, + struct _citrus_region *, + struct _citrus_db_locator *); +int _citrus_db_lookup_by_string(struct _citrus_db *, const char *, + struct _citrus_region *, + struct _citrus_db_locator *); +int _citrus_db_lookup8_by_string(struct _citrus_db *, const char *, + uint8_t *, + struct _citrus_db_locator *); +int _citrus_db_lookup16_by_string(struct _citrus_db *, const char *, + uint16_t *, + struct _citrus_db_locator *); +int _citrus_db_lookup32_by_string(struct _citrus_db *, const char *, + uint32_t *, + struct _citrus_db_locator *); +int _citrus_db_lookup_string_by_string(struct _citrus_db *, const char *, + const char **, + struct _citrus_db_locator *); +int _citrus_db_get_number_of_entries(struct _citrus_db *); +int _citrus_db_get_entry(struct _citrus_db *, int, + struct _citrus_region *, struct _citrus_region *); +__END_DECLS + +static __inline void +_citrus_db_locator_init(struct _citrus_db_locator *dl) +{ + dl->dl_hashval = 0; + dl->dl_offset = 0; +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_db_factory.c b/lib/nbsd_libc/citrus/citrus_db_factory.c new file mode 100644 index 000000000..44475af45 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_db_factory.c @@ -0,0 +1,353 @@ +/* $NetBSD: citrus_db_factory.c,v 1.9 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_db_factory.c,v 1.9 2008/02/09 14:56:20 junyoung Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_db_file.h" +#include "citrus_db_factory.h" + +struct _citrus_db_factory_entry { + SIMPLEQ_ENTRY(_citrus_db_factory_entry) de_entry; + struct _citrus_db_factory_entry *de_next; + uint32_t de_hashvalue; + struct _region de_key; + int de_key_free; + struct _region de_data; + int de_data_free; + int de_idx; +}; + +struct _citrus_db_factory { + size_t df_num_entries; + SIMPLEQ_HEAD(, _citrus_db_factory_entry) df_entries; + size_t df_total_key_size; + size_t df_total_data_size; + uint32_t (*df_hashfunc)(void *, struct _citrus_region *); + void *df_hashfunc_closure; +}; + +#define DB_ALIGN 16 + +int +_citrus_db_factory_create(struct _citrus_db_factory **rdf, + _citrus_db_hash_func_t hashfunc, + void *hashfunc_closure) +{ + struct _citrus_db_factory *df; + + df = malloc(sizeof(*df)); + if (df == NULL) + return errno; + df->df_num_entries = 0; + df->df_total_key_size = df->df_total_data_size = 0; + SIMPLEQ_INIT(&df->df_entries); + df->df_hashfunc = hashfunc; + df->df_hashfunc_closure = hashfunc_closure; + + *rdf = df; + + return 0; +} + +void +_citrus_db_factory_free(struct _citrus_db_factory *df) +{ + struct _citrus_db_factory_entry *de; + + while ((de = SIMPLEQ_FIRST(&df->df_entries)) != NULL) { + SIMPLEQ_REMOVE_HEAD(&df->df_entries, de_entry); + if (de->de_key_free) + free(_region_head(&de->de_key)); + if (de->de_data_free) + free(_region_head(&de->de_data)); + free(de); + } + free(df); +} + +static __inline size_t +ceilto(size_t sz) +{ + return (sz+DB_ALIGN-1) & ~(DB_ALIGN-1); +} + +int +_citrus_db_factory_add(struct _citrus_db_factory *df, + struct _region *key, int keyfree, + struct _region *data, int datafree) +{ + struct _citrus_db_factory_entry *de; + + de = malloc(sizeof(*de)); + if (de == NULL) + return -1; + + de->de_hashvalue = df->df_hashfunc(df->df_hashfunc_closure, key); + de->de_key = *key; + de->de_key_free = keyfree; + de->de_data = *data; + de->de_data_free = datafree; + de->de_idx = -1; + + SIMPLEQ_INSERT_TAIL(&df->df_entries, de, de_entry); + df->df_total_key_size += _region_size(key); + df->df_total_data_size += ceilto(_region_size(data)); + df->df_num_entries++; + + return 0; + +} + +int +_citrus_db_factory_add_by_string(struct _citrus_db_factory *df, + const char *key, + struct _citrus_region *data, int datafree) +{ + struct _region r; + char *tmp; + tmp = strdup(key); + if (tmp == NULL) + return errno; + _region_init(&r, tmp, strlen(key)); + return _citrus_db_factory_add(df, &r, 1, data, datafree); +} + +int +_citrus_db_factory_add8_by_string(struct _citrus_db_factory *df, + const char *key, uint8_t val) +{ + struct _region r; + uint8_t *p; + + p = malloc(sizeof(*p)); + if (p == NULL) + return errno; + *p = val; + _region_init(&r, p, 1); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +int +_citrus_db_factory_add16_by_string(struct _citrus_db_factory *df, + const char *key, uint16_t val) +{ + struct _region r; + uint16_t *p; + + p = malloc(sizeof(*p)); + if (p == NULL) + return errno; + *p = htons(val); + _region_init(&r, p, 2); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +int +_citrus_db_factory_add32_by_string(struct _citrus_db_factory *df, + const char *key, uint32_t val) +{ + struct _region r; + uint32_t *p; + + p = malloc(sizeof(*p)); + if (p == NULL) + return errno; + *p = htonl(val); + _region_init(&r, p, 4); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +int +_citrus_db_factory_add_string_by_string(struct _citrus_db_factory *df, + const char *key, const char *data) +{ + char *p; + struct _region r; + + p = strdup(data); + if (p == NULL) + return errno; + _region_init(&r, p, strlen(p)+1); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +size_t +_citrus_db_factory_calc_size(struct _citrus_db_factory *df) +{ + size_t sz; + + sz = ceilto(_CITRUS_DB_HEADER_SIZE); + sz += ceilto(_CITRUS_DB_ENTRY_SIZE * df->df_num_entries); + sz += ceilto(df->df_total_key_size); + sz += df->df_total_data_size; + + return sz; +} + +static __inline void +put8(struct _region *r, size_t *rofs, uint8_t val) +{ + *(uint8_t *)_region_offset(r, *rofs) = val; + *rofs += 1; +} + +static __inline void +put16(struct _region *r, size_t *rofs, uint16_t val) +{ + val = htons(val); + memcpy(_region_offset(r, *rofs), &val, 2); + *rofs += 2; +} + +static __inline void +put32(struct _region *r, size_t *rofs, uint32_t val) +{ + val = htonl(val); + memcpy(_region_offset(r, *rofs), &val, 4); + *rofs += 4; +} + +static __inline void +putpad(struct _region *r, size_t *rofs) +{ + size_t i; + for (i = ceilto(*rofs) - *rofs; i > 0; i--) + put8(r, rofs, 0); +} + +static __inline void +dump_header(struct _region *r, const char *magic, size_t *rofs, + size_t num_entries) +{ + while (*rofs<_CITRUS_DB_MAGIC_SIZE) + put8(r, rofs, *magic++); + put32(r, rofs, num_entries); + put32(r, rofs, _CITRUS_DB_HEADER_SIZE); +} + +int +_citrus_db_factory_serialize(struct _citrus_db_factory *df, const char *magic, + struct _region *r) +{ + size_t i, ofs, keyofs, dataofs, nextofs; + struct _citrus_db_factory_entry *de, **depp, *det; + + ofs = 0; + /* check whether more than 0 entries exist */ + if (df->df_num_entries == 0) { + dump_header(r, magic, &ofs, 0); + return 0; + } + /* allocate hash table */ + depp = malloc(sizeof(*depp) * df->df_num_entries); + if (depp == NULL) + return -1; + for (i = 0; i < df->df_num_entries; i++) + depp[i] = NULL; + + /* step1: store the entries which are not conflicting */ + SIMPLEQ_FOREACH(de, &df->df_entries, de_entry) { + de->de_hashvalue %= df->df_num_entries; + de->de_idx = -1; + de->de_next = NULL; + if (depp[de->de_hashvalue] == NULL) { + depp[de->de_hashvalue] = de; + de->de_idx = (int)de->de_hashvalue; + } + } + + /* step2: resolve conflicts */ + i = 0; + SIMPLEQ_FOREACH(de, &df->df_entries, de_entry) { + if (de->de_idx == -1) { + det = depp[de->de_hashvalue]; + while (det->de_next != NULL) + det = det->de_next; + det->de_next = de; + while (depp[i] != NULL) + i++; + depp[i] = de; + de->de_idx = (int)i; + } + } + + keyofs = + _CITRUS_DB_HEADER_SIZE + + ceilto(df->df_num_entries*_CITRUS_DB_ENTRY_SIZE); + dataofs = keyofs + ceilto(df->df_total_key_size); + + /* dump header */ + dump_header(r, magic, &ofs, df->df_num_entries); + + /* dump entries */ + for (i = 0; i < df->df_num_entries; i++) { + de = depp[i]; + nextofs = 0; + if (de->de_next) { + nextofs = + _CITRUS_DB_HEADER_SIZE + + de->de_next->de_idx * _CITRUS_DB_ENTRY_SIZE; + } + put32(r, &ofs, de->de_hashvalue); + put32(r, &ofs, nextofs); + put32(r, &ofs, keyofs); + put32(r, &ofs, _region_size(&de->de_key)); + put32(r, &ofs, dataofs); + put32(r, &ofs, _region_size(&de->de_data)); + memcpy(_region_offset(r, keyofs), + _region_head(&de->de_key), _region_size(&de->de_key)); + keyofs += _region_size(&de->de_key); + memcpy(_region_offset(r, dataofs), + _region_head(&de->de_data), _region_size(&de->de_data)); + dataofs += _region_size(&de->de_data); + putpad(r, &dataofs); + } + putpad(r, &ofs); + putpad(r, &keyofs); + free(depp); + + return 0; +} diff --git a/lib/nbsd_libc/citrus/citrus_db_factory.h b/lib/nbsd_libc/citrus/citrus_db_factory.h new file mode 100644 index 000000000..8b2f994a0 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_db_factory.h @@ -0,0 +1,58 @@ +/* $NetBSD: citrus_db_factory.h,v 1.3 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_DB_FACTORY_H_ +#define _CITRUS_DB_FACTORY_H_ + +struct _citrus_db_factory; +typedef uint32_t (*_citrus_db_hash_func_t)(void *, struct _citrus_region *); + +__BEGIN_DECLS +int _citrus_db_factory_create(struct _citrus_db_factory **, + _citrus_db_hash_func_t, void *); +void _citrus_db_factory_free(struct _citrus_db_factory *); +int _citrus_db_factory_add(struct _citrus_db_factory *, + struct _citrus_region *, int, + struct _citrus_region *, int); +int _citrus_db_factory_add_by_string(struct _citrus_db_factory *, + const char *, + struct _citrus_region *, int); +int _citrus_db_factory_add8_by_string(struct _citrus_db_factory *, + const char *, uint8_t); +int _citrus_db_factory_add16_by_string(struct _citrus_db_factory *, + const char *, uint16_t); +int _citrus_db_factory_add32_by_string(struct _citrus_db_factory *, + const char *, uint32_t); +int _citrus_db_factory_add_string_by_string(struct _citrus_db_factory *, + const char *, const char *); +size_t _citrus_db_factory_calc_size(struct _citrus_db_factory *); +int _citrus_db_factory_serialize(struct _citrus_db_factory *, + const char *, struct _citrus_region *); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_db_file.h b/lib/nbsd_libc/citrus/citrus_db_file.h new file mode 100644 index 000000000..4e5295736 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_db_file.h @@ -0,0 +1,84 @@ +/* $NetBSD: citrus_db_file.h,v 1.4 2008/02/10 05:58:22 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_DB_FILE_H_ +#define _CITRUS_DB_FILE_H_ + +/* + * db format: + * +--- + * | header + * | - magic + * | - num entries + * +--- + * | entry directory + * | +------------ + * | | entry0 + * | | - hash value + * | | - next entry + * | | - key offset + * | | - key len + * | | - data offset + * | | - data size + * | |--- + * | | entry1 + * | | .. + * | | entryN + * | +--- + * +--- + * | key table + * | - key0 + * | ... + * | - keyN + * +--- + * | data table + * | - data0 + * | ... + * | - dataN + * +--- + */ + +#define _CITRUS_DB_MAGIC_SIZE 8 +#define _CITRUS_DB_HEADER_SIZE 16 +struct _citrus_db_header_x { + char dhx_magic[_CITRUS_DB_MAGIC_SIZE]; + uint32_t dhx_num_entries; + uint32_t dhx_entry_offset; +} __packed; + +struct _citrus_db_entry_x { + uint32_t dex_hash_value; + uint32_t dex_next_offset; + uint32_t dex_key_offset; + uint32_t dex_key_size; + uint32_t dex_data_offset; + uint32_t dex_data_size; +} __packed; +#define _CITRUS_DB_ENTRY_SIZE 24 + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_db_hash.c b/lib/nbsd_libc/citrus/citrus_db_hash.c new file mode 100644 index 000000000..9702391a0 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_db_hash.c @@ -0,0 +1,71 @@ +/* $NetBSD: citrus_db_hash.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_db_hash.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef HOSTPROG +#include "namespace.h" +#endif +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_db_hash.h" + +uint32_t +/*ARGSUSED*/ +_citrus_db_hash_std(void *closure, struct _region *r) +{ + const uint8_t *p = _region_head(r); + uint32_t hash = 0, tmp; + size_t i; + + for (i = _region_size(r); i > 0; i--) { + hash <<= 4; + hash += _bcs_tolower(*p); + tmp = hash & 0xF0000000; + if (tmp != 0) { + hash ^= tmp; + hash ^= tmp >> 24; + } + p++; + } + return hash; +} diff --git a/lib/nbsd_libc/citrus/citrus_db_hash.h b/lib/nbsd_libc/citrus/citrus_db_hash.h new file mode 100644 index 000000000..55ee45e88 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_db_hash.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_db_hash.h,v 1.2 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_DB_HASH_H_ +#define _CITRUS_DB_HASH_H_ + +__BEGIN_DECLS +uint32_t _citrus_db_hash_std(void *, struct _citrus_region *); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_esdb.c b/lib/nbsd_libc/citrus/citrus_esdb.c new file mode 100644 index 000000000..493400794 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_esdb.c @@ -0,0 +1,355 @@ +/* $NetBSD: citrus_esdb.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_esdb.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_lookup.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_esdb.h" +#include "citrus_esdb_file.h" + +#define ESDB_DIR "esdb.dir" +#define ESDB_ALIAS "esdb.alias" + +/* + * _citrus_esdb_alias: + * resolve encoding scheme name aliases. + */ +const char * +_citrus_esdb_alias(const char *esname, char *buf, size_t bufsize) +{ + return _lookup_alias(_PATH_ESDB "/" ESDB_ALIAS, esname, buf, bufsize, + _LOOKUP_CASE_IGNORE); +} + + +/* + * conv_esdb: + * external representation -> local structure. + */ +static int +conv_esdb(struct _citrus_esdb *esdb, struct _region *fr) +{ + int ret; + struct _citrus_db *db; + uint32_t version, num_charsets, csid, i, tmp; + char buf[100]; + const char *str; + + /* open db */ + ret = _db_open(&db, fr, _CITRUS_ESDB_MAGIC, &_db_hash_std, NULL); + if (ret) + goto err0; + + /* check version */ + ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_VERSION, &version, NULL); + if (ret) + goto err1; + switch (version) { + case 0x00000001: + /* current version */ + /* initial version */ + break; + default: + ret = EFTYPE; + goto err1; + } + + /* get encoding/variable */ + ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_ENCODING, &str, NULL); + if (ret) + goto err1; + esdb->db_encname = strdup(str); + if (esdb->db_encname == NULL) { + ret = errno; + goto err1; + } + + esdb->db_len_variable = 0; + esdb->db_variable = NULL; + ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_VARIABLE, &str, NULL); + if (ret == 0) { + esdb->db_len_variable = strlen(str)+1; + esdb->db_variable = strdup(str); + if (esdb->db_variable == NULL) { + ret = errno; + goto err2; + } + } else if (ret != ENOENT) + goto err2; + + /* get number of charsets */ + ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_NUM_CHARSETS, + &num_charsets, NULL); + if (ret) + goto err3; + esdb->db_num_charsets = num_charsets; + + /* get invalid character */ + ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_INVALID, &tmp, NULL); + if (ret == 0) { + esdb->db_use_invalid = 1; + esdb->db_invalid = tmp; + } else if (ret == ENOENT) + esdb->db_use_invalid = 0; + else + goto err3; + + /* get charsets */ + esdb->db_charsets = malloc(num_charsets * sizeof(*esdb->db_charsets)); + if (esdb->db_charsets == NULL) { + ret = errno; + goto err3; + } + for (i = 0; i < num_charsets; i++) { + snprintf(buf, sizeof(buf), + _CITRUS_ESDB_SYM_CSID_PREFIX "%d", i); + ret = _db_lookup32_by_s(db, buf, &csid, NULL); + if (ret) + goto err4; + esdb->db_charsets[i].ec_csid = csid; + + snprintf(buf, sizeof(buf), + _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d", i); + ret = _db_lookupstr_by_s(db, buf, &str, NULL); + if (ret) + goto err4; + esdb->db_charsets[i].ec_csname = strdup(str); + if (esdb->db_charsets[i].ec_csname == NULL) { + ret = errno; + goto err4; + } + } + + _db_close(db); + return 0; + +err4: + for (; i > 0; i--) + free(esdb->db_charsets[i - 1].ec_csname); + free(esdb->db_charsets); +err3: + free(esdb->db_variable); +err2: + free(esdb->db_encname); +err1: + _db_close(db); + if (ret == ENOENT) + ret = EFTYPE; +err0: + return ret; +} + +/* + * _citrus_esdb_open: + * open an ESDB file. + */ +int +_citrus_esdb_open(struct _citrus_esdb *db, const char *esname) +{ + int ret; + const char *realname, *encfile; + char buf1[PATH_MAX], buf2[PATH_MAX], path[PATH_MAX]; + struct _region fr; + + _DIAGASSERT(esname != NULL); + + snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_ALIAS); + realname = _lookup_alias(path, esname, buf1, sizeof(buf1), + _LOOKUP_CASE_IGNORE); + + snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_DIR); + encfile = _lookup_simple(path, realname, buf2, sizeof(buf2), + _LOOKUP_CASE_IGNORE); + if (encfile==NULL) + return ENOENT; + + /* open file */ + snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, encfile); + ret = _map_file(&fr, path); + if (ret) + return ret; + + ret = conv_esdb(db, &fr); + + _unmap_file(&fr); + + return ret; +} + +/* + * _citrus_esdb_close: + * free an ESDB. + */ +void +_citrus_esdb_close(struct _citrus_esdb *db) +{ + int i; + + _DIAGASSERT(db != NULL); + _DIAGASSERT(db->db_num_charsets == 0 || db->db_charsets != NULL); + + for (i = 0; i < db->db_num_charsets; i++) + free(db->db_charsets[i].ec_csname); + db->db_num_charsets = 0; + free(db->db_charsets); db->db_charsets = NULL; + free(db->db_encname); db->db_encname = NULL; + db->db_len_variable = 0; + free(db->db_variable); db->db_variable = NULL; +} + +/* + * _citrus_esdb_free_list: + * free the list. + */ +void +_citrus_esdb_free_list(char **list, size_t num) +{ + size_t i; + + for (i = 0; i < num; i++) + free(list[i]); + free(list); +} + +/* + * _citrus_esdb_get_list: + * get esdb entries. + */ +int +_citrus_esdb_get_list(char ***rlist, size_t *rnum) +{ + int ret; + struct _region key; + size_t num; + struct _citrus_lookup *cla, *cld; + char **list, **q; + char buf[PATH_MAX]; + + num = 0; + + ret = _lookup_seq_open(&cla, _PATH_ESDB "/" ESDB_ALIAS, + _LOOKUP_CASE_IGNORE); + if (ret) + goto quit0; + + ret = _lookup_seq_open(&cld, _PATH_ESDB "/" ESDB_DIR, + _LOOKUP_CASE_IGNORE); + if (ret) + goto quit1; + + /* count number of entries */ + num = _lookup_get_num_entries(cla) + _lookup_get_num_entries(cld); + + _lookup_seq_rewind(cla); + _lookup_seq_rewind(cld); + + /* allocate list pointer space */ + list = malloc(num * sizeof(char *)); + num = 0; + if (list == NULL) { + ret = errno; + goto quit3; + } + + /* get alias entries */ + while ((ret = _lookup_seq_next(cla, &key, NULL)) == 0) { + snprintf(buf, sizeof(buf), "%.*s", + (int)_region_size(&key), + (const char *)_region_head(&key)); + _bcs_convert_to_lower(buf); + list[num] = strdup(buf); + if (list[num] == NULL) { + ret = errno; + goto quit3; + } + num++; + } + if (ret != ENOENT) + goto quit3; + /* get dir entries */ + while ((ret = _lookup_seq_next(cld, &key, NULL)) == 0) { + /* check duplicated entry */ + snprintf(buf, sizeof(buf), "%.*s", + (int)_region_size(&key), + (const char *)_region_head(&key)); + _bcs_convert_to_lower(buf); + ret = _lookup_seq_lookup(cla, buf, NULL); + if (ret) { + if (ret != ENOENT) + goto quit3; + /* not duplicated */ + list[num] = strdup(buf); + if (list[num] == NULL) { + ret = errno; + goto quit3; + } + num++; + } + } + if (ret != ENOENT) + goto quit3; + + ret = 0; + q = realloc(list, num * sizeof(char *)); + if (!q) { + ret = ENOMEM; + goto quit3; + } + list = q; + *rlist = list; + *rnum = num; +quit3: + if (ret) + _citrus_esdb_free_list(list, num); + _lookup_seq_close(cld); +quit1: + _lookup_seq_close(cla); +quit0: + return ret; +} diff --git a/lib/nbsd_libc/citrus/citrus_esdb.h b/lib/nbsd_libc/citrus/citrus_esdb.h new file mode 100644 index 000000000..dce973dad --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_esdb.h @@ -0,0 +1,55 @@ +/* $NetBSD: citrus_esdb.h,v 1.1 2003/06/25 09:51:32 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ESDB_H_ +#define _CITRUS_ESDB_H_ + +struct _citrus_esdb_charset { + _citrus_csid_t ec_csid; + char *ec_csname; +}; + +struct _citrus_esdb { + char *db_encname; + void *db_variable; + size_t db_len_variable; + int db_num_charsets; + struct _citrus_esdb_charset *db_charsets; + int db_use_invalid; + _citrus_wc_t db_invalid; +}; + +__BEGIN_DECLS +const char *_citrus_esdb_alias(const char *, char *, size_t); +int _citrus_esdb_open(struct _citrus_esdb *, const char *); +void _citrus_esdb_close(struct _citrus_esdb *); +void _citrus_esdb_free_list(char **, size_t); +int _citrus_esdb_get_list(char ***, size_t *); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_esdb_file.h b/lib/nbsd_libc/citrus/citrus_esdb_file.h new file mode 100644 index 000000000..5bb05a27e --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_esdb_file.h @@ -0,0 +1,44 @@ +/* $NetBSD: citrus_esdb_file.h,v 1.1 2003/06/25 09:51:32 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ESDB_FILE_H_ +#define _CITRUS_ESDB_FILE_H_ + +#define _CITRUS_ESDB_MAGIC "ESDB\0\0\0\0" + +#define _CITRUS_ESDB_SYM_VERSION "version" +#define _CITRUS_ESDB_SYM_ENCODING "encoding" +#define _CITRUS_ESDB_SYM_VARIABLE "variable" +#define _CITRUS_ESDB_SYM_NUM_CHARSETS "num_charsets" +#define _CITRUS_ESDB_SYM_INVALID "invalid" +#define _CITRUS_ESDB_SYM_CSNAME_PREFIX "csname_" +#define _CITRUS_ESDB_SYM_CSID_PREFIX "csid_" + +#define _CITRUS_ESDB_VERSION 0x00000001 + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_fix_grouping.h b/lib/nbsd_libc/citrus/citrus_fix_grouping.h new file mode 100644 index 000000000..f4e85e539 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_fix_grouping.h @@ -0,0 +1,55 @@ +/* $NetBSD: citrus_fix_grouping.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_FIX_GROUPING_H_ +#define _CITRUS_FIX_GROUPING_H_ + +#define _CITRUS_LC_GROUPING_VALUE_MIN 0 +#define _CITRUS_LC_GROUPING_VALUE_MAX 126 +#define _CITRUS_LC_GROUPING_VALUE_NO_FUTHER 127 + +#if CHAR_MAX != _CITRUS_LC_GROUPING_VALUE_NO_FUTHER +static __inline void +_citrus_fixup_char_max_md(char *grouping) +{ + char *p; + + _DIAGASSERT(grouping != NULL); + + for (p = grouping; *p != '\0'; ++p) { + if (*p == _CITRUS_LC_GROUPING_VALUE_NO_FUTHER) + *p = (char)CHAR_MAX; + } +} +#define _CITRUS_FIXUP_CHAR_MAX_MD(grouping) \ + _citrus_fixup_char_max_md(__UNCONST(grouping)) +#else +#define _CITRUS_FIXUP_CHAR_MAX_MD(grouping) /* nothing to do */ +#endif + +#endif /*_CITRUS_FIX_GROUPING_H_*/ diff --git a/lib/nbsd_libc/citrus/citrus_hash.c b/lib/nbsd_libc/citrus/citrus_hash.c new file mode 100644 index 000000000..954ce21e8 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_hash.c @@ -0,0 +1,54 @@ +/* $NetBSD: citrus_hash.c,v 1.3 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_hash.c,v 1.3 2008/02/09 14:56:20 junyoung Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_region.h" +#include "citrus_hash.h" +#include "citrus_db_hash.h" + +int +_citrus_string_hash_func(const char *key, int hashsize) +{ + struct _region r; + + _region_init(&r, __UNCONST(key), strlen(key)); + + return (int)(_db_hash_std(NULL, &r) % (uint32_t)hashsize); +} diff --git a/lib/nbsd_libc/citrus/citrus_hash.h b/lib/nbsd_libc/citrus/citrus_hash.h new file mode 100644 index 000000000..2e677a17f --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_hash.h @@ -0,0 +1,58 @@ +/* $NetBSD: citrus_hash.h,v 1.3 2004/01/02 21:49:35 itojun Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_HASH_H_ +#define _CITRUS_HASH_H_ + +#define _CITRUS_HASH_ENTRY(type) LIST_ENTRY(type) +#define _CITRUS_HASH_HEAD(headname, type, hashsize) \ +struct headname { \ + LIST_HEAD(, type) chh_table[hashsize]; \ +} +#define _CITRUS_HASH_INIT(head, hashsize) \ +do { \ + int _ch_loop; \ + for (_ch_loop = 0; _ch_loop < hashsize; _ch_loop++) \ + LIST_INIT(&(head)->chh_table[_ch_loop]); \ +} while (/*CONSTCOND*/0) +#define _CITRUS_HASH_REMOVE(elm, field) LIST_REMOVE(elm, field) +#define _CITRUS_HASH_INSERT(head, elm, field, hashval) \ +LIST_INSERT_HEAD(&(head)->chh_table[hashval], elm, field) +#define _CITRUS_HASH_SEARCH(head, elm, field, matchfunc, key, hashval) \ +do { \ + LIST_FOREACH((elm), &(head)->chh_table[hashval], field) { \ + if (matchfunc((elm), key)==0) \ + break; \ + } \ +} while (/*CONSTCOND*/0) + +__BEGIN_DECLS +int _citrus_string_hash_func(const char *, int); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_iconv.c b/lib/nbsd_libc/citrus/citrus_iconv.c new file mode 100644 index 000000000..585b285a6 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_iconv.c @@ -0,0 +1,387 @@ +/* $NetBSD: citrus_iconv.c,v 1.8 2009/09/05 06:44:27 dholland Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_iconv.c,v 1.8 2009/09/05 06:44:27 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_lookup.h" +#include "citrus_hash.h" +#include "citrus_iconv.h" + +#define _CITRUS_ICONV_DIR "iconv.dir" +#define _CITRUS_ICONV_ALIAS "iconv.alias" + +#define CI_HASH_SIZE 101 +#define CI_INITIAL_MAX_REUSE 5 +#define CI_ENV_MAX_REUSE "ICONV_MAX_REUSE" + +#ifdef _REENTRANT +static rwlock_t lock = RWLOCK_INITIALIZER; +#endif +static int isinit = 0; +static _CITRUS_HASH_HEAD(, _citrus_iconv_shared, CI_HASH_SIZE) shared_pool; +static TAILQ_HEAD(, _citrus_iconv_shared) shared_unused; +static int shared_num_unused, shared_max_reuse; + +static __inline void +init_cache(void) +{ + rwlock_wrlock(&lock); + if (!isinit) { + _CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE); + TAILQ_INIT(&shared_unused); + shared_max_reuse = -1; + if (!issetugid() && getenv(CI_ENV_MAX_REUSE)) + shared_max_reuse = atoi(getenv(CI_ENV_MAX_REUSE)); + if (shared_max_reuse < 0) + shared_max_reuse = CI_INITIAL_MAX_REUSE; + isinit = 1; + } + rwlock_unlock(&lock); +} + +/* + * lookup_iconv_entry: + * lookup iconv.dir entry in the specified directory. + * + * line format of iconv.dir file: + * key module arg + * key : lookup key. + * module : iconv module name. + * arg : argument for the module (generally, description file name) + * + */ +static __inline int +lookup_iconv_entry(const char *curdir, const char *key, + char *linebuf, size_t linebufsize, + const char **module, const char **variable) +{ + const char *cp, *cq; + char *p, path[PATH_MAX]; + + /* iconv.dir path */ + snprintf(path, (size_t)PATH_MAX, ("%s/" _CITRUS_ICONV_DIR), curdir); + + /* lookup db */ + cp = p = _lookup_simple(path, key, linebuf, linebufsize, + _LOOKUP_CASE_IGNORE); + if (p == NULL) + return ENOENT; + + /* get module name */ + *module = p; + cq = _bcs_skip_nonws(cp); + p[cq-cp] = '\0'; + p += cq-cp+1; + cq++; + + /* get variable */ + cp = _bcs_skip_ws(cq); + *variable = p += cp - cq; + cq = _bcs_skip_nonws(cp); + p[cq-cp] = '\0'; + + return 0; +} + +static __inline void +close_shared(struct _citrus_iconv_shared *ci) +{ + if (ci) { + if (ci->ci_module) { + if (ci->ci_ops) { + if (ci->ci_closure) + (*ci->ci_ops->io_uninit_shared)(ci); + free(ci->ci_ops); + } + _citrus_unload_module(ci->ci_module); + } + free(ci); + } +} + +static __inline int +open_shared(struct _citrus_iconv_shared * __restrict * __restrict rci, + const char * __restrict basedir, const char * __restrict convname, + const char * __restrict src, const char * __restrict dst) +{ + int ret; + struct _citrus_iconv_shared *ci; + _citrus_iconv_getops_t getops; + char linebuf[LINE_MAX]; + const char *module, *variable; + size_t len_convname; + + /* search converter entry */ + ret = lookup_iconv_entry(basedir, convname, linebuf, sizeof(linebuf), + &module, &variable); + if (ret) { + if (ret == ENOENT) + /* fallback */ + ret = lookup_iconv_entry(basedir, "*", + linebuf, sizeof(linebuf), + &module, &variable); + if (ret) + return ret; + } + + /* initialize iconv handle */ + len_convname = strlen(convname); + ci = malloc(sizeof(*ci)+len_convname+1); + if (!ci) { + ret = errno; + goto err; + } + ci->ci_module = NULL; + ci->ci_ops = NULL; + ci->ci_closure = NULL; + ci->ci_convname = (void *)&ci[1]; + memcpy(ci->ci_convname, convname, len_convname+1); + + /* load module */ + ret = _citrus_load_module(&ci->ci_module, module); + if (ret) + goto err; + + /* get operators */ + getops = (_citrus_iconv_getops_t) + _citrus_find_getops(ci->ci_module, module, "iconv"); + if (!getops) { + ret = EOPNOTSUPP; + goto err; + } + ci->ci_ops = malloc(sizeof(*ci->ci_ops)); + if (!ci->ci_ops) { + ret = errno; + goto err; + } + ret = (*getops)(ci->ci_ops, sizeof(*ci->ci_ops), + _CITRUS_ICONV_ABI_VERSION); + if (ret) + goto err; + + /* version check */ + if (ci->ci_ops->io_abi_version == 1) { + /* binary compatibility broken at ver.2 */ + ret = EINVAL; + goto err; + } + + if (ci->ci_ops->io_init_shared == NULL || + ci->ci_ops->io_uninit_shared == NULL || + ci->ci_ops->io_init_context == NULL || + ci->ci_ops->io_uninit_context == NULL || + ci->ci_ops->io_convert == NULL) + goto err; + + /* initialize the converter */ + ret = (*ci->ci_ops->io_init_shared)(ci, basedir, src, dst, + (const void *)variable, + strlen(variable)+1); + if (ret) + goto err; + + *rci = ci; + + return 0; +err: + close_shared(ci); + return ret; +} + +static __inline int +hash_func(const char *key) +{ + return _string_hash_func(key, CI_HASH_SIZE); +} + +static __inline int +match_func(struct _citrus_iconv_shared * __restrict ci, + const char * __restrict key) +{ + return strcmp(ci->ci_convname, key); +} + +static int +get_shared(struct _citrus_iconv_shared * __restrict * __restrict rci, + const char *basedir, const char *src, const char *dst) +{ + int ret = 0; + int hashval; + struct _citrus_iconv_shared * ci; + char convname[PATH_MAX]; + + snprintf(convname, sizeof(convname), "%s/%s", src, dst); + + rwlock_wrlock(&lock); + + /* lookup alread existing entry */ + hashval = hash_func(convname); + _CITRUS_HASH_SEARCH(&shared_pool, ci, ci_hash_entry, match_func, + convname, hashval); + if (ci != NULL) { + /* found */ + if (ci->ci_used_count == 0) { + TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry); + shared_num_unused--; + } + ci->ci_used_count++; + *rci = ci; + goto quit; + } + + /* create new entry */ + ret = open_shared(&ci, basedir, convname, src, dst); + if (ret) + goto quit; + + _CITRUS_HASH_INSERT(&shared_pool, ci, ci_hash_entry, hashval); + ci->ci_used_count = 1; + *rci = ci; + +quit: + rwlock_unlock(&lock); + + return ret; +} + +static void +release_shared(struct _citrus_iconv_shared * __restrict ci) +{ + rwlock_wrlock(&lock); + + ci->ci_used_count--; + if (ci->ci_used_count == 0) { + /* put it into unused list */ + shared_num_unused++; + TAILQ_INSERT_TAIL(&shared_unused, ci, ci_tailq_entry); + /* flood out */ + while (shared_num_unused > shared_max_reuse) { + ci = TAILQ_FIRST(&shared_unused); + _DIAGASSERT(ci != NULL); + TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry); + _CITRUS_HASH_REMOVE(ci, ci_hash_entry); + shared_num_unused--; + close_shared(ci); + } + } + + rwlock_unlock(&lock); +} + +/* + * _citrus_iconv_open: + * open a converter for the specified in/out codes. + */ +int +_citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict rcv, + const char * __restrict basedir, + const char * __restrict src, const char * __restrict dst) +{ + int ret; + struct _citrus_iconv_shared *ci = NULL; + struct _citrus_iconv *cv; + char realsrc[PATH_MAX], realdst[PATH_MAX]; + char buf[PATH_MAX], path[PATH_MAX]; + + init_cache(); + + /* resolve codeset name aliases */ + snprintf(path, sizeof(path), "%s/%s", basedir, _CITRUS_ICONV_ALIAS); + strlcpy(realsrc, + _lookup_alias(path, src, buf, (size_t)PATH_MAX, + _LOOKUP_CASE_IGNORE), + (size_t)PATH_MAX); + strlcpy(realdst, + _lookup_alias(path, dst, buf, (size_t)PATH_MAX, + _LOOKUP_CASE_IGNORE), + (size_t)PATH_MAX); + + /* sanity check */ + if (strchr(realsrc, '/') != NULL || strchr(realdst, '/')) + return EINVAL; + + /* get shared record */ + ret = get_shared(&ci, basedir, realsrc, realdst); + if (ret) + return ret; + + /* create/init context */ + cv = malloc(sizeof(*cv)); + if (cv == NULL) { + ret = errno; + release_shared(ci); + return ret; + } + cv->cv_shared = ci; + ret = (*ci->ci_ops->io_init_context)(cv); + if (ret) { + release_shared(ci); + free(cv); + return ret; + } + *rcv = cv; + + return 0; +} + +/* + * _citrus_iconv_close: + * close the specified converter. + */ +void +_citrus_iconv_close(struct _citrus_iconv *cv) +{ + if (cv) { + (*cv->cv_shared->ci_ops->io_uninit_context)(cv); + release_shared(cv->cv_shared); + free(cv); + } +} diff --git a/lib/nbsd_libc/citrus/citrus_iconv.h b/lib/nbsd_libc/citrus/citrus_iconv.h new file mode 100644 index 000000000..37192600d --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_iconv.h @@ -0,0 +1,69 @@ +/* $NetBSD: citrus_iconv.h,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ICONV_H_ +#define _CITRUS_ICONV_H_ + +struct _citrus_iconv_shared; +struct _citrus_iconv_ops; +struct _citrus_iconv; + +__BEGIN_DECLS +int _citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict, + const char * __restrict, + const char * __restrict, const char * __restrict); +void _citrus_iconv_close(struct _citrus_iconv *); +__END_DECLS + + +#include "citrus_iconv_local.h" + +#define _CITRUS_ICONV_F_HIDE_INVALID 0x0001 + +/* + * _citrus_iconv_convert: + * convert a string. + */ +static __inline int +_citrus_iconv_convert(struct _citrus_iconv * __restrict cv, + const char * __restrict * __restrict in, + size_t * __restrict inbytes, + char * __restrict * __restrict out, + size_t * __restrict outbytes, uint32_t flags, + size_t * __restrict nresults) +{ + + _DIAGASSERT(cv && cv->cv_shared && cv->cv_shared->ci_ops && + cv->cv_shared->ci_ops->io_convert); + _DIAGASSERT(out || outbytes == 0); + + return (*cv->cv_shared->ci_ops->io_convert)(cv, in, inbytes, out, + outbytes, flags, nresults); +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_iconv_local.h b/lib/nbsd_libc/citrus/citrus_iconv_local.h new file mode 100644 index 000000000..fd42e84d2 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_iconv_local.h @@ -0,0 +1,107 @@ +/* $NetBSD: citrus_iconv_local.h,v 1.3 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ICONV_LOCAL_H_ +#define _CITRUS_ICONV_LOCAL_H_ + +#define _CITRUS_ICONV_GETOPS_FUNC_BASE(_n_) \ +int _n_(struct _citrus_iconv_ops *, size_t, uint32_t) +#define _CITRUS_ICONV_GETOPS_FUNC(_n_) \ +_CITRUS_ICONV_GETOPS_FUNC_BASE(_citrus_##_n_##_iconv_getops) + +#define _CITRUS_ICONV_DECLS(_m_) \ +static int _citrus_##_m_##_iconv_init_shared \ + (struct _citrus_iconv_shared * __restrict, \ + const char * __restrict, \ + const char * __restrict, const char * __restrict, \ + const void * __restrict, size_t); \ +static void _citrus_##_m_##_iconv_uninit_shared \ + (struct _citrus_iconv_shared *); \ +static int _citrus_##_m_##_iconv_convert \ + (struct _citrus_iconv * __restrict, \ + const char * __restrict * __restrict, size_t * __restrict, \ + char * __restrict * __restrict, size_t * __restrict outbytes, \ + uint32_t, size_t * __restrict); \ +static int _citrus_##_m_##_iconv_init_context \ + (struct _citrus_iconv *); \ +static void _citrus_##_m_##_iconv_uninit_context \ + (struct _citrus_iconv *) + + +#define _CITRUS_ICONV_DEF_OPS(_m_) \ +struct _citrus_iconv_ops _citrus_##_m_##_iconv_ops = { \ + /* io_abi_version */ _CITRUS_ICONV_ABI_VERSION, \ + /* io_init_shared */ &_citrus_##_m_##_iconv_init_shared, \ + /* io_uninit_shared */ &_citrus_##_m_##_iconv_uninit_shared, \ + /* io_init_context */ &_citrus_##_m_##_iconv_init_context, \ + /* io_uninit_context */ &_citrus_##_m_##_iconv_uninit_context, \ + /* io_convert */ &_citrus_##_m_##_iconv_convert \ +} + +typedef _CITRUS_ICONV_GETOPS_FUNC_BASE((*_citrus_iconv_getops_t)); +typedef int (*_citrus_iconv_init_shared_t) + (struct _citrus_iconv_shared * __restrict, + const char * __restrict, const char * __restrict, + const char * __restrict, const void * __restrict, size_t); +typedef void (*_citrus_iconv_uninit_shared_t) + (struct _citrus_iconv_shared *); +typedef int (*_citrus_iconv_convert_t) + (struct _citrus_iconv * __restrict, + const char *__restrict* __restrict, size_t * __restrict, + char * __restrict * __restrict, size_t * __restrict, uint32_t, + size_t * __restrict); +typedef int (*_citrus_iconv_init_context_t)(struct _citrus_iconv *); +typedef void (*_citrus_iconv_uninit_context_t)(struct _citrus_iconv *); + +struct _citrus_iconv_ops { + uint32_t io_abi_version; + _citrus_iconv_init_shared_t io_init_shared; + _citrus_iconv_uninit_shared_t io_uninit_shared; + _citrus_iconv_init_context_t io_init_context; + _citrus_iconv_uninit_context_t io_uninit_context; + _citrus_iconv_convert_t io_convert; +}; +#define _CITRUS_ICONV_ABI_VERSION 2 + +struct _citrus_iconv_shared { + struct _citrus_iconv_ops *ci_ops; + void *ci_closure; + /* private */ + _CITRUS_HASH_ENTRY(_citrus_iconv_shared) ci_hash_entry; + TAILQ_ENTRY(_citrus_iconv_shared) ci_tailq_entry; + _citrus_module_t ci_module; + unsigned int ci_used_count; + char *ci_convname; +}; + +struct _citrus_iconv { + struct _citrus_iconv_shared *cv_shared; + void *cv_closure; +}; + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_lc_ctype.c b/lib/nbsd_libc/citrus/citrus_lc_ctype.c new file mode 100644 index 000000000..e6d55e7c8 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_ctype.c @@ -0,0 +1,133 @@ +/* $NetBSD: citrus_lc_ctype.c,v 1.9 2010/06/19 13:26:51 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_lc_ctype.c,v 1.9 2010/06/19 13:26:51 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_lookup.h" +#include "citrus_aliasname_local.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_mmap.h" + +#include "runetype_local.h" +#include "multibyte.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_citrus_LC_CTYPE_, name) + +/* + * macro required by nb_lc_template(_decl).h + */ +#define _CATEGORY_TYPE _RuneLocale + +#include "nb_lc_template_decl.h" + +static int +_citrus_LC_CTYPE_create_impl(const char * __restrict root, + const char * __restrict name, _RuneLocale ** __restrict pdata) +{ + char path[PATH_MAX + 1]; + int ret; + struct _region r; + + _DIAGASSERT(root != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(pdata != NULL); + + snprintf(path, sizeof(path), + "%s/%s/LC_CTYPE", root, name); + ret = _citrus_map_file(&r, path); + if (!ret) { + ret = _rune_load((const char *)r.r_head, r.r_size, pdata); + _citrus_unmap_file(&r); + } + return ret; +} + +static __inline void +_PREFIX(build_cache)(struct _locale_cache_t * __restrict cache, + _RuneLocale * __restrict data) +{ + _DIAGASSERT(cache != NULL); + _DIAGASSERT(cache->items != NULL); + _DIAGASSERT(data != NULL); + + cache->ctype_tab = data->rl_ctype_tab; + cache->tolower_tab = data->rl_tolower_tab; + cache->toupper_tab = data->rl_toupper_tab; + cache->mb_cur_max = _citrus_ctype_get_mb_cur_max(data->rl_citrus_ctype); + cache->items[(size_t)CODESET] = data->rl_codeset; +} + +static __inline void +_PREFIX(fixup)(_RuneLocale *data) +{ + _DIAGASSERT(data != NULL); + + __mb_cur_max = _citrus_ctype_get_mb_cur_max(data->rl_citrus_ctype); + _ctype_ = data->rl_ctype_tab; + _tolower_tab_ = data->rl_tolower_tab; + _toupper_tab_ = data->rl_toupper_tab; + _CurrentRuneLocale = data; +} + +/* + * macro required by nb_lc_template.h + */ +#define _CATEGORY_ID LC_CTYPE +#define _CATEGORY_NAME "LC_CTYPE" +#define _CATEGORY_DEFAULT _DefaultRuneLocale + +#include "nb_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_citrus_LC_CTYPE_); diff --git a/lib/nbsd_libc/citrus/citrus_lc_messages.c b/lib/nbsd_libc/citrus/citrus_lc_messages.c new file mode 100644 index 000000000..6e62db5b8 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_messages.c @@ -0,0 +1,163 @@ +/* $NetBSD: citrus_lc_messages.c,v 1.4 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_lc_messages.c,v 1.4 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_lookup.h" +#include "citrus_aliasname_local.h" +#include "citrus_module.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_memstream.h" +#include "runetype_local.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_citrus_LC_MESSAGES_, name) + +#include "nb_lc_messages_misc.h" +#include "citrus_lc_template_decl.h" + +static __inline void +_citrus_LC_MESSAGES_uninit(_MessagesLocale *data) +{ + free(__UNCONST(data->yesexpr)); + free(__UNCONST(data->noexpr)); + free(__UNCONST(data->yesstr)); + free(__UNCONST(data->nostr)); +} + +#include "citrus_lc_messages.h" + +struct _citrus_LC_MESSAGES_key { + const char *name; + size_t offset; +}; + +#define OFFSET(field) (offsetof(_MessagesLocale, field)) +static const struct _citrus_LC_MESSAGES_key keys[] = { + { _CITRUS_LC_MESSAGES_SYM_YESEXPR, OFFSET(yesexpr) }, + { _CITRUS_LC_MESSAGES_SYM_NOEXPR, OFFSET(noexpr ) }, + { _CITRUS_LC_MESSAGES_SYM_YESSTR, OFFSET(yesstr ) }, + { _CITRUS_LC_MESSAGES_SYM_NOSTR, OFFSET(nostr ) }, + { NULL, 0 } +}; + +static __inline int +_citrus_LC_MESSAGES_init_normal(_MessagesLocale * __restrict data, + struct _citrus_db * __restrict db) +{ + const struct _citrus_LC_MESSAGES_key *key; + char **p; + const char *s; + + _DIAGASSERT(data != NULL); + _DIAGASSERT(db != NULL); + + memset(data, 0, sizeof(*data)); + for (key = &keys[0]; key->name != NULL; ++key) { + if (_db_lookupstr_by_s(db, key->name, &s, NULL)) + goto fatal; + p = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p = strdup(s); + if (*p == NULL) + goto fatal; + } + return 0; + +fatal: + _citrus_LC_MESSAGES_uninit(data); + return EFTYPE; +} + +static __inline int +_citrus_LC_MESSAGES_init_fallback(_MessagesLocale * __restrict data, + struct _memstream * __restrict ms) +{ + const struct _citrus_LC_MESSAGES_key *key; + char **p; + const char *s; + size_t n; + + _DIAGASSERT(data != NULL); + _DIAGASSERT(ms != NULL); + + memset(data, 0, sizeof(*data)); + for (key = &keys[0]; key->name != NULL; ++key) { + if ((s = _memstream_getln(ms, &n)) == NULL) + goto fatal; + p = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p = strndup(s, n - 1); + if (*p == NULL) + goto fatal; + } + return 0; + +fatal: + _citrus_LC_MESSAGES_uninit(data); + return EFTYPE; +} + +/* + * macro required by citrus_lc_template.h + */ +#define _CATEGORY_DB "LC_MESSAGES/SYS_LC_MESSAGES" +#define _CATEGORY_MAGIC _CITRUS_LC_MESSAGES_MAGIC_1 + +#include "citrus_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_citrus_LC_MESSAGES_); diff --git a/lib/nbsd_libc/citrus/citrus_lc_messages.h b/lib/nbsd_libc/citrus/citrus_lc_messages.h new file mode 100644 index 000000000..cdf7d395c --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_messages.h @@ -0,0 +1,47 @@ +/* $NetBSD: citrus_lc_messages.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LC_MESSAGES_H_ +#define _CITRUS_LC_MESSAGES_H_ + +/* + * + */ +#define _CITRUS_LC_MESSAGES_MAGIC_1 "CtrsME10" +#define _CITRUS_LC_MESSAGES_SYM_VERSION "version" +#define _CITRUS_LC_MESSAGES_VERSION 0x00000001 + +/* + * + */ +#define _CITRUS_LC_MESSAGES_SYM_YESEXPR "yesexpr" +#define _CITRUS_LC_MESSAGES_SYM_NOEXPR "noexpr" +#define _CITRUS_LC_MESSAGES_SYM_YESSTR "yesstr" +#define _CITRUS_LC_MESSAGES_SYM_NOSTR "nostr" + +#endif /*_CITRUS_LC_MESSAGES_H_*/ diff --git a/lib/nbsd_libc/citrus/citrus_lc_monetary.c b/lib/nbsd_libc/citrus/citrus_lc_monetary.c new file mode 100644 index 000000000..833a5a126 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_monetary.c @@ -0,0 +1,220 @@ +/* $NetBSD: citrus_lc_monetary.c,v 1.4 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_lc_monetary.c,v 1.4 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_lookup.h" +#include "citrus_aliasname_local.h" +#include "citrus_module.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_memstream.h" +#include "runetype_local.h" + +#include "fix_grouping.h" +#include "citrus_fix_grouping.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_citrus_LC_MONETARY_, name) + +#include "nb_lc_monetary_misc.h" +#include "citrus_lc_template_decl.h" + +static __inline void +_citrus_LC_MONETARY_uninit(_MonetaryLocale *data) +{ + _DIAGASSERT(data != NULL); + + free(__UNCONST(data->int_curr_symbol)); + free(__UNCONST(data->currency_symbol)); + free(__UNCONST(data->mon_decimal_point)); + free(__UNCONST(data->mon_thousands_sep)); + free(__UNCONST(data->mon_grouping)); + free(__UNCONST(data->positive_sign)); + free(__UNCONST(data->negative_sign)); +} + +#include "citrus_lc_monetary.h" + +struct _citrus_LC_MONETARY_key { + const char *name; + size_t offset; +}; + +#define OFFSET(field) (offsetof(_MonetaryLocale, field)) +static const struct _citrus_LC_MONETARY_key keys_string[] = { + { _CITRUS_LC_MONETARY_SYM_INT_CURR_SYMBOL, OFFSET(int_curr_symbol ) }, + { _CITRUS_LC_MONETARY_SYM_CURRENCY_SYMBOL, OFFSET(currency_symbol ) }, + { _CITRUS_LC_MONETARY_SYM_MON_DECIMAL_POINT, OFFSET(mon_decimal_point ) }, + { _CITRUS_LC_MONETARY_SYM_MON_THOUSANDS_SEP, OFFSET(mon_thousands_sep ) }, + { _CITRUS_LC_MONETARY_SYM_MON_GROUPING, OFFSET(mon_grouping ) }, + { _CITRUS_LC_MONETARY_SYM_POSITIVE_SIGN, OFFSET(positive_sign ) }, + { _CITRUS_LC_MONETARY_SYM_NEGATIVE_SIGN, OFFSET(negative_sign ) }, + { NULL, (size_t)0 } +}; +static const struct _citrus_LC_MONETARY_key keys_char[] = { + { _CITRUS_LC_MONETARY_SYM_INT_FRAC_DIGITS, OFFSET(int_frac_digits ) }, + { _CITRUS_LC_MONETARY_SYM_FRAC_DIGITS, OFFSET(frac_digits ) }, + { _CITRUS_LC_MONETARY_SYM_P_CS_PRECEDES, OFFSET(p_cs_precedes ) }, + { _CITRUS_LC_MONETARY_SYM_P_SEP_BY_SPACE, OFFSET(p_sep_by_space ) }, + { _CITRUS_LC_MONETARY_SYM_N_CS_PRECEDES, OFFSET(n_cs_precedes ) }, + { _CITRUS_LC_MONETARY_SYM_N_SEP_BY_SPACE, OFFSET(n_sep_by_space ) }, + { _CITRUS_LC_MONETARY_SYM_P_SIGN_POSN, OFFSET(p_sign_posn ) }, + { _CITRUS_LC_MONETARY_SYM_N_SIGN_POSN, OFFSET(n_sign_posn ) }, + { _CITRUS_LC_MONETARY_SYM_INT_P_CS_PRECEDES, OFFSET(int_p_cs_precedes ) }, + { _CITRUS_LC_MONETARY_SYM_INT_N_CS_PRECEDES, OFFSET(int_n_cs_precedes ) }, + { _CITRUS_LC_MONETARY_SYM_INT_P_SEP_BY_SPACE, OFFSET(int_p_sep_by_space) }, + { _CITRUS_LC_MONETARY_SYM_INT_N_SEP_BY_SPACE, OFFSET(int_n_sep_by_space) }, + { _CITRUS_LC_MONETARY_SYM_INT_P_SIGN_POSN, OFFSET(int_p_sign_posn ) }, + { _CITRUS_LC_MONETARY_SYM_INT_N_SIGN_POSN, OFFSET(int_n_sign_posn ) }, + { NULL, (size_t)0 } +}; + +static __inline int +_citrus_LC_MONETARY_init_normal(_MonetaryLocale * __restrict data, + struct _citrus_db * __restrict db) +{ + const struct _citrus_LC_MONETARY_key *key; + char **p_string, *p_char; + const char *s; + uint8_t u8; + + _DIAGASSERT(data != NULL); + _DIAGASSERT(db != NULL); + + memset(data, 0, sizeof(*data)); + for (key = &keys_string[0]; key->name != NULL; ++key) { + if (_db_lookupstr_by_s(db, key->name, &s, NULL)) + goto fatal; + p_string = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p_string = strdup(s); + if (*p_string == NULL) + goto fatal; + } + for (key = &keys_char[0]; key->name != NULL; ++key) { + if (_db_lookup8_by_s(db, key->name, &u8, NULL)) + goto fatal; + p_char = ((char *)(void *)data) + key->offset; + *p_char = (char)(unsigned char)u8; + } + _CITRUS_FIXUP_CHAR_MAX_MD(data->mon_grouping); + + return 0; + +fatal: + _citrus_LC_MONETARY_uninit(data); + return EFTYPE; +} + +static __inline int +_citrus_LC_MONETARY_init_fallback(_MonetaryLocale * __restrict data, + struct _memstream * __restrict ms) +{ + const struct _citrus_LC_MONETARY_key *key; + char **p_string, *p_char; + const char *s; + size_t n; + char *t; + long int l; + + _DIAGASSERT(data != NULL); + _DIAGASSERT(ms != NULL); + + memset(data, 0, sizeof(*data)); + for (key = &keys_string[0]; key->name != NULL; ++key) { + if ((s = _memstream_getln(ms, &n)) == NULL) + goto fatal; + p_string = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p_string = strndup(s, n - 1); + if (*p_string == NULL) + goto fatal; + } + for (key = &keys_char[0]; key->name != NULL; ++key) { + if ((s = _memstream_getln(ms, &n)) == NULL) + goto fatal; + t = strndup(s, n - 1); + if (t == NULL) + goto fatal; + s = (const char *)t; + l = _bcs_strtol(s, &t, 0); + if (s == t || l < 0 || l > 0x7fL) { + free(t); + goto fatal; + } + free(t); + p_char = ((char *)(void *)data) + key->offset; + *p_char = (char)(l & 0x7fL); + } + data->mon_grouping = + __fix_locale_grouping_str(data->mon_grouping); + return 0; + +fatal: + _citrus_LC_MONETARY_uninit(data); + return EFTYPE; +} + +/* + * macro required by citrus_lc_template.h + */ +#define _CATEGORY_DB "LC_MONETARY" +#define _CATEGORY_MAGIC _CITRUS_LC_MONETARY_MAGIC_1 + +#include "citrus_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_citrus_LC_MONETARY_); diff --git a/lib/nbsd_libc/citrus/citrus_lc_monetary.h b/lib/nbsd_libc/citrus/citrus_lc_monetary.h new file mode 100644 index 000000000..0ef146c88 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_monetary.h @@ -0,0 +1,64 @@ +/* $NetBSD: citrus_lc_monetary.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LC_MONETARY_H_ +#define _CITRUS_LC_MONETARY_H_ + +/* + * + */ +#define _CITRUS_LC_MONETARY_MAGIC_1 "CtrsMO10" +#define _CITRUS_LC_MONETARY_SYM_VERSION "version" +#define _CITRUS_LC_MONETARY_VERSION 0x00000001 + +/* + * + */ +#define _CITRUS_LC_MONETARY_SYM_INT_CURR_SYMBOL "int_curr_symbol" +#define _CITRUS_LC_MONETARY_SYM_CURRENCY_SYMBOL "currency_symbol" +#define _CITRUS_LC_MONETARY_SYM_MON_DECIMAL_POINT "mon_decimal_point" +#define _CITRUS_LC_MONETARY_SYM_MON_THOUSANDS_SEP "mon_thousands_sep" +#define _CITRUS_LC_MONETARY_SYM_MON_GROUPING "mon_grouping" +#define _CITRUS_LC_MONETARY_SYM_POSITIVE_SIGN "positive_sign" +#define _CITRUS_LC_MONETARY_SYM_NEGATIVE_SIGN "negative_sign" +#define _CITRUS_LC_MONETARY_SYM_INT_FRAC_DIGITS "int_frac_digits" +#define _CITRUS_LC_MONETARY_SYM_FRAC_DIGITS "frac_digits" +#define _CITRUS_LC_MONETARY_SYM_P_CS_PRECEDES "p_cs_precedes" +#define _CITRUS_LC_MONETARY_SYM_P_SEP_BY_SPACE "p_sep_by_space" +#define _CITRUS_LC_MONETARY_SYM_N_CS_PRECEDES "n_cs_precedes" +#define _CITRUS_LC_MONETARY_SYM_N_SEP_BY_SPACE "n_sep_by_space" +#define _CITRUS_LC_MONETARY_SYM_P_SIGN_POSN "p_sign_posn" +#define _CITRUS_LC_MONETARY_SYM_N_SIGN_POSN "n_sign_posn" +#define _CITRUS_LC_MONETARY_SYM_INT_P_CS_PRECEDES "int_p_cs_precedes" +#define _CITRUS_LC_MONETARY_SYM_INT_N_CS_PRECEDES "int_n_cs_precedes" +#define _CITRUS_LC_MONETARY_SYM_INT_P_SEP_BY_SPACE "int_p_sep_by_space" +#define _CITRUS_LC_MONETARY_SYM_INT_N_SEP_BY_SPACE "int_n_sep_by_space" +#define _CITRUS_LC_MONETARY_SYM_INT_P_SIGN_POSN "int_p_sign_posn" +#define _CITRUS_LC_MONETARY_SYM_INT_N_SIGN_POSN "int_n_sign_posn" + +#endif /*_CITRUS_LC_MONETARY_H_*/ diff --git a/lib/nbsd_libc/citrus/citrus_lc_numeric.c b/lib/nbsd_libc/citrus/citrus_lc_numeric.c new file mode 100644 index 000000000..65616ee20 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_numeric.c @@ -0,0 +1,169 @@ +/* $NetBSD: citrus_lc_numeric.c,v 1.4 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_lc_numeric.c,v 1.4 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_lookup.h" +#include "citrus_aliasname_local.h" +#include "citrus_module.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_memstream.h" +#include "runetype_local.h" + +#include "fix_grouping.h" +#include "citrus_fix_grouping.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_citrus_LC_NUMERIC_, name) + +#include "nb_lc_numeric_misc.h" +#include "citrus_lc_template_decl.h" + +static __inline void +_citrus_LC_NUMERIC_uninit(_NumericLocale *data) +{ + free(__UNCONST(data->decimal_point)); + free(__UNCONST(data->thousands_sep)); + free(__UNCONST(data->grouping)); +} + +#include "citrus_lc_numeric.h" + +struct _citrus_LC_NUMERIC_key { + const char *name; + size_t offset; +}; + +#define OFFSET(field) (offsetof(_NumericLocale, field)) +static const struct _citrus_LC_NUMERIC_key keys[] = { + { _CITRUS_LC_NUMERIC_SYM_DECIMAL_POINT, OFFSET(decimal_point) }, + { _CITRUS_LC_NUMERIC_SYM_THOUSANDS_SEP, OFFSET(thousands_sep) }, + { _CITRUS_LC_NUMERIC_SYM_GROUPING, OFFSET(grouping ) }, + { NULL, 0 } +}; + +static __inline int +_citrus_LC_NUMERIC_init_normal(_NumericLocale * __restrict data, + struct _citrus_db * __restrict db) +{ + const struct _citrus_LC_NUMERIC_key *key; + char **p; + const char *s; + + _DIAGASSERT(data != NULL); + _DIAGASSERT(db != NULL); + + memset(data, 0, sizeof(*data)); + for (key = &keys[0]; key->name != NULL; ++key) { + if (_db_lookupstr_by_s(db, key->name, &s, NULL)) + goto fatal; + p = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p = strdup(s); + if (*p == NULL) + goto fatal; + } + _CITRUS_FIXUP_CHAR_MAX_MD(data->grouping); + + return 0; + +fatal: + _citrus_LC_NUMERIC_uninit(data); + return EFTYPE; +} + +static __inline int +_citrus_LC_NUMERIC_init_fallback(_NumericLocale * __restrict data, + struct _memstream * __restrict ms) +{ + const struct _citrus_LC_NUMERIC_key *key; + char **p; + const char *s; + size_t n; + + _DIAGASSERT(data != NULL); + _DIAGASSERT(ms != NULL); + + memset(data, 0, sizeof(*data)); + for (key = &keys[0]; key->name != NULL; ++key) { + if ((s = _memstream_getln(ms, &n)) == NULL) + goto fatal; + p = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p = strndup(s, n - 1); + if (*p == NULL) + goto fatal; + } + data->grouping = + __fix_locale_grouping_str(data->grouping); + + return 0; + +fatal: + _citrus_LC_NUMERIC_uninit(data); + return EFTYPE; +} + +/* + * macro required by citrus_lc_template.h + */ +#define _CATEGORY_DB "LC_NUMERIC" +#define _CATEGORY_MAGIC _CITRUS_LC_NUMERIC_MAGIC_1 + +#include "citrus_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_citrus_LC_NUMERIC_); diff --git a/lib/nbsd_libc/citrus/citrus_lc_numeric.h b/lib/nbsd_libc/citrus/citrus_lc_numeric.h new file mode 100644 index 000000000..cf9bdd837 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_numeric.h @@ -0,0 +1,46 @@ +/* $NetBSD: citrus_lc_numeric.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LC_NUMERIC_H_ +#define _CITRUS_LC_NUMERIC_H_ + +/* + * + */ +#define _CITRUS_LC_NUMERIC_MAGIC_1 "CtrsNU10" +#define _CITRUS_LC_NUMERIC_SYM_VERSION "version" +#define _CITRUS_LC_NUMERIC_VERSION 0x00000001 + +/* + * + */ +#define _CITRUS_LC_NUMERIC_SYM_DECIMAL_POINT "decimal_point" +#define _CITRUS_LC_NUMERIC_SYM_THOUSANDS_SEP "thousands_sep" +#define _CITRUS_LC_NUMERIC_SYM_GROUPING "grouping" + +#endif /*_CITRUS_LC_NUMERIC_H_*/ diff --git a/lib/nbsd_libc/citrus/citrus_lc_template.h b/lib/nbsd_libc/citrus/citrus_lc_template.h new file mode 100644 index 000000000..42b2c2299 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_template.h @@ -0,0 +1,74 @@ +/* $NetBSD: citrus_lc_template.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LC_TEMPLATE_H_ +#define _CITRUS_LC_TEMPLATE_H_ + +static __inline int +_PREFIX(create_impl)(const char * __restrict root, + const char * __restrict name, _CATEGORY_TYPE ** __restrict pdata) +{ + char path[PATH_MAX + 1]; + int ret; + struct _region file; + struct _citrus_db *db; + struct _memstream ms; + _CATEGORY_TYPE *data; + + snprintf(path, sizeof(path), + "%s/%s/" _CATEGORY_DB, root, name); + ret = _map_file(&file, path); + if (ret) { + ret = ENOENT; + } else { + data = malloc(sizeof(*data)); + if (!data) { + ret = ENOMEM; + } else { + ret = _db_open(&db, &file, _CATEGORY_MAGIC, + &_db_hash_std, NULL); + if (!ret) { + ret = _PREFIX(init_normal)(data, db); + _db_close(db); + } else { + _memstream_bind(&ms, &file); + ret = _PREFIX(init_fallback)(data, &ms); + } + if (!ret) + *pdata = data; + else + free(data); + } + _unmap_file(&file); + } + return ret; +} + +#include "nb_lc_template.h" + +#endif /* _CITRUS_LC_TEMPLATE_H_ */ diff --git a/lib/nbsd_libc/citrus/citrus_lc_template_decl.h b/lib/nbsd_libc/citrus/citrus_lc_template_decl.h new file mode 100644 index 000000000..e83c691ad --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_template_decl.h @@ -0,0 +1,42 @@ +/* $NetBSD: citrus_lc_template_decl.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LC_TEMPLATE_DECL_H_ +#define _CITRUS_LC_TEMPLATE_DECL_H_ + +#include "nb_lc_template_decl.h" + +static __inline int +_PREFIX(init_normal)(_CATEGORY_TYPE * __restrict, + struct _citrus_db * __restrict db); + +static __inline int +_PREFIX(init_fallback)(_CATEGORY_TYPE * __restrict, + struct _memstream * __restrict); + +#endif /*_CITRUS_LC_TEMPLATE_DECL_H_*/ diff --git a/lib/nbsd_libc/citrus/citrus_lc_time.c b/lib/nbsd_libc/citrus/citrus_lc_time.c new file mode 100644 index 000000000..e2100af4e --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_time.c @@ -0,0 +1,214 @@ +/* $NetBSD: citrus_lc_time.c,v 1.5 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_lc_time.c,v 1.5 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_lookup.h" +#include "citrus_aliasname_local.h" +#include "citrus_module.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_memstream.h" +#include "runetype_local.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_citrus_LC_TIME_, name) + +#include "nb_lc_time_misc.h" +#include "citrus_lc_template_decl.h" + +static __inline void +_citrus_LC_TIME_uninit(_TimeLocale *data) +{ + size_t i, j; + + _DIAGASSERT(data != NULL); + + for (i = ABDAY_IDX(ABDAY_1), j = ABDAY_IDX(ABDAY_7); i <= j; ++i) + free(__UNCONST(data->abday[i])); + for (i = DAY_IDX(DAY_1), j = DAY_IDX(DAY_7); i <= j; ++i) + free(__UNCONST(data->day[i])); + for (i = ABMON_IDX(ABMON_1), j = ABMON_IDX(ABMON_12); i <= j; ++i) + free(__UNCONST(data->abmon[i])); + for (i = MON_IDX(MON_1), j = MON_IDX(MON_12); i <= j; ++i) + free(__UNCONST(data->mon[i])); + for (i = AM_PM_IDX(AM_STR), j = AM_PM_IDX(PM_STR); i <= j; ++i) + free(__UNCONST(data->am_pm[i])); + free(__UNCONST(data->d_t_fmt)); + free(__UNCONST(data->d_fmt)); + free(__UNCONST(data->t_fmt)); + free(__UNCONST(data->t_fmt_ampm)); +} + +#include "citrus_lc_time.h" + +struct _citrus_LC_TIME_key { + const char *name; + size_t offset; +}; + +#define OFFSET(field) offsetof(_TimeLocale, field) +static const struct _citrus_LC_TIME_key keys[] = { + { _CITRUS_LC_TIME_SYM_ABDAY_1, OFFSET(abday[ABDAY_IDX(ABDAY_1)] ) }, + { _CITRUS_LC_TIME_SYM_ABDAY_2, OFFSET(abday[ABDAY_IDX(ABDAY_2)] ) }, + { _CITRUS_LC_TIME_SYM_ABDAY_3, OFFSET(abday[ABDAY_IDX(ABDAY_3)] ) }, + { _CITRUS_LC_TIME_SYM_ABDAY_4, OFFSET(abday[ABDAY_IDX(ABDAY_4)] ) }, + { _CITRUS_LC_TIME_SYM_ABDAY_5, OFFSET(abday[ABDAY_IDX(ABDAY_5)] ) }, + { _CITRUS_LC_TIME_SYM_ABDAY_6, OFFSET(abday[ABDAY_IDX(ABDAY_6)] ) }, + { _CITRUS_LC_TIME_SYM_ABDAY_7, OFFSET(abday[ABDAY_IDX(ABDAY_7)] ) }, + { _CITRUS_LC_TIME_SYM_DAY_1, OFFSET(day[DAY_IDX(DAY_1)] ) }, + { _CITRUS_LC_TIME_SYM_DAY_2, OFFSET(day[DAY_IDX(DAY_2)] ) }, + { _CITRUS_LC_TIME_SYM_DAY_3, OFFSET(day[DAY_IDX(DAY_3)] ) }, + { _CITRUS_LC_TIME_SYM_DAY_4, OFFSET(day[DAY_IDX(DAY_4)] ) }, + { _CITRUS_LC_TIME_SYM_DAY_5, OFFSET(day[DAY_IDX(DAY_5)] ) }, + { _CITRUS_LC_TIME_SYM_DAY_6, OFFSET(day[DAY_IDX(DAY_6)] ) }, + { _CITRUS_LC_TIME_SYM_DAY_7, OFFSET(day[DAY_IDX(DAY_7)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_1, OFFSET(abmon[ABMON_IDX(ABMON_1)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_2, OFFSET(abmon[ABMON_IDX(ABMON_2)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_3, OFFSET(abmon[ABMON_IDX(ABMON_3)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_4, OFFSET(abmon[ABMON_IDX(ABMON_4)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_5, OFFSET(abmon[ABMON_IDX(ABMON_5)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_6, OFFSET(abmon[ABMON_IDX(ABMON_6)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_7, OFFSET(abmon[ABMON_IDX(ABMON_7)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_8, OFFSET(abmon[ABMON_IDX(ABMON_8)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_9, OFFSET(abmon[ABMON_IDX(ABMON_9)] ) }, + { _CITRUS_LC_TIME_SYM_ABMON_10, OFFSET(abmon[ABMON_IDX(ABMON_10)]) }, + { _CITRUS_LC_TIME_SYM_ABMON_11, OFFSET(abmon[ABMON_IDX(ABMON_11)]) }, + { _CITRUS_LC_TIME_SYM_ABMON_12, OFFSET(abmon[ABMON_IDX(ABMON_12)]) }, + { _CITRUS_LC_TIME_SYM_MON_1, OFFSET(mon[MON_IDX(MON_1)] ) }, + { _CITRUS_LC_TIME_SYM_MON_2, OFFSET(mon[MON_IDX(MON_2)] ) }, + { _CITRUS_LC_TIME_SYM_MON_3, OFFSET(mon[MON_IDX(MON_3)] ) }, + { _CITRUS_LC_TIME_SYM_MON_4, OFFSET(mon[MON_IDX(MON_4)] ) }, + { _CITRUS_LC_TIME_SYM_MON_5, OFFSET(mon[MON_IDX(MON_5)] ) }, + { _CITRUS_LC_TIME_SYM_MON_6, OFFSET(mon[MON_IDX(MON_6)] ) }, + { _CITRUS_LC_TIME_SYM_MON_7, OFFSET(mon[MON_IDX(MON_7)] ) }, + { _CITRUS_LC_TIME_SYM_MON_8, OFFSET(mon[MON_IDX(MON_8)] ) }, + { _CITRUS_LC_TIME_SYM_MON_9, OFFSET(mon[MON_IDX(MON_9)] ) }, + { _CITRUS_LC_TIME_SYM_MON_10, OFFSET(mon[MON_IDX(MON_10)] ) }, + { _CITRUS_LC_TIME_SYM_MON_11, OFFSET(mon[MON_IDX(MON_11)] ) }, + { _CITRUS_LC_TIME_SYM_MON_12, OFFSET(mon[MON_IDX(MON_12)] ) }, + { _CITRUS_LC_TIME_SYM_AM_STR, OFFSET(am_pm[AM_PM_IDX(AM_STR)] ) }, + { _CITRUS_LC_TIME_SYM_PM_STR, OFFSET(am_pm[AM_PM_IDX(PM_STR)] ) }, + { _CITRUS_LC_TIME_SYM_D_T_FMT, OFFSET(d_t_fmt ) }, + { _CITRUS_LC_TIME_SYM_D_FMT, OFFSET(d_fmt ) }, + { _CITRUS_LC_TIME_SYM_T_FMT, OFFSET(t_fmt ) }, + { _CITRUS_LC_TIME_SYM_T_FMT_AMPM, OFFSET(t_fmt_ampm ) }, + { NULL, 0 } +}; + +static __inline int +_citrus_LC_TIME_init_normal(_TimeLocale * __restrict data, + struct _citrus_db * __restrict db) +{ + const struct _citrus_LC_TIME_key *key; + char **p; + const char *s; + + _DIAGASSERT(data != NULL); + _DIAGASSERT(db != NULL); + + memset(data, 0, sizeof(*data)); + for (key = &keys[0]; key->name != NULL; ++key) { + if (_db_lookupstr_by_s(db, key->name, &s, NULL)) + goto fatal; + p = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p = strdup(s); + if (*p == NULL) + goto fatal; + } + return 0; + +fatal: + _citrus_LC_TIME_uninit(data); + return EFTYPE; +} + +static __inline int +_citrus_LC_TIME_init_fallback(_TimeLocale * __restrict data, + struct _memstream * __restrict ms) +{ + const struct _citrus_LC_TIME_key *key; + char **p; + const char *s; + size_t n; + + memset(data, 0, sizeof(*data)); + for (key = &keys[0]; key->name != NULL; ++key) { + if ((s = _memstream_getln(ms, &n)) == NULL) + goto fatal; + p = (char **)(void *) + (((char *)(void *)data) + key->offset); + *p = strndup(s, n - 1); + if (*p == NULL) + goto fatal; + } + return 0; + +fatal: + _citrus_LC_TIME_uninit(data); + return EFTYPE; +} + +/* + * macro required by citrus_lc_template.h + */ +#define _CATEGORY_DB "LC_TIME" +#define _CATEGORY_MAGIC _CITRUS_LC_TIME_MAGIC_1 + +#include "citrus_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_citrus_LC_TIME_); diff --git a/lib/nbsd_libc/citrus/citrus_lc_time.h b/lib/nbsd_libc/citrus/citrus_lc_time.h new file mode 100644 index 000000000..3b9616f3a --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lc_time.h @@ -0,0 +1,87 @@ +/* $NetBSD: citrus_lc_time.h,v 1.2 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LC_TIME_H_ +#define _CITRUS_LC_TIME_H_ + +/* + * + */ +#define _CITRUS_LC_TIME_MAGIC_1 "CtrsTI10" +#define _CITRUS_LC_TIME_SYM_VERSION "version" +#define _CITRUS_LC_TIME_VERSION 0x00000001 + +/* + * + */ +#define _CITRUS_LC_TIME_SYM_ABDAY_1 "abday1" +#define _CITRUS_LC_TIME_SYM_ABDAY_2 "abday2" +#define _CITRUS_LC_TIME_SYM_ABDAY_3 "abday3" +#define _CITRUS_LC_TIME_SYM_ABDAY_4 "abday4" +#define _CITRUS_LC_TIME_SYM_ABDAY_5 "abday5" +#define _CITRUS_LC_TIME_SYM_ABDAY_6 "abday6" +#define _CITRUS_LC_TIME_SYM_ABDAY_7 "abday7" +#define _CITRUS_LC_TIME_SYM_DAY_1 "day1" +#define _CITRUS_LC_TIME_SYM_DAY_2 "day2" +#define _CITRUS_LC_TIME_SYM_DAY_3 "day3" +#define _CITRUS_LC_TIME_SYM_DAY_4 "day4" +#define _CITRUS_LC_TIME_SYM_DAY_5 "day5" +#define _CITRUS_LC_TIME_SYM_DAY_6 "day6" +#define _CITRUS_LC_TIME_SYM_DAY_7 "day7" +#define _CITRUS_LC_TIME_SYM_ABMON_1 "abmon_1" +#define _CITRUS_LC_TIME_SYM_ABMON_2 "abmon_2" +#define _CITRUS_LC_TIME_SYM_ABMON_3 "abmon_3" +#define _CITRUS_LC_TIME_SYM_ABMON_4 "abmon_4" +#define _CITRUS_LC_TIME_SYM_ABMON_5 "abmon_5" +#define _CITRUS_LC_TIME_SYM_ABMON_6 "abmon_6" +#define _CITRUS_LC_TIME_SYM_ABMON_7 "abmon_7" +#define _CITRUS_LC_TIME_SYM_ABMON_8 "abmon_8" +#define _CITRUS_LC_TIME_SYM_ABMON_9 "abmon_9" +#define _CITRUS_LC_TIME_SYM_ABMON_10 "abmon_10" +#define _CITRUS_LC_TIME_SYM_ABMON_11 "abmon_11" +#define _CITRUS_LC_TIME_SYM_ABMON_12 "abmon_12" +#define _CITRUS_LC_TIME_SYM_MON_1 "mon_1" +#define _CITRUS_LC_TIME_SYM_MON_2 "mon_2" +#define _CITRUS_LC_TIME_SYM_MON_3 "mon_3" +#define _CITRUS_LC_TIME_SYM_MON_4 "mon_4" +#define _CITRUS_LC_TIME_SYM_MON_5 "mon_5" +#define _CITRUS_LC_TIME_SYM_MON_6 "mon_6" +#define _CITRUS_LC_TIME_SYM_MON_7 "mon_7" +#define _CITRUS_LC_TIME_SYM_MON_8 "mon_8" +#define _CITRUS_LC_TIME_SYM_MON_9 "mon_9" +#define _CITRUS_LC_TIME_SYM_MON_10 "mon_10" +#define _CITRUS_LC_TIME_SYM_MON_11 "mon_11" +#define _CITRUS_LC_TIME_SYM_MON_12 "mon_12" +#define _CITRUS_LC_TIME_SYM_AM_STR "am_str" +#define _CITRUS_LC_TIME_SYM_PM_STR "pm_str" +#define _CITRUS_LC_TIME_SYM_D_T_FMT "d_t_fmt" +#define _CITRUS_LC_TIME_SYM_D_FMT "d_fmt" +#define _CITRUS_LC_TIME_SYM_T_FMT "t_fmt" +#define _CITRUS_LC_TIME_SYM_T_FMT_AMPM "t_fmt_ampm" + +#endif /*_CITRUS_LC_TIME_H_*/ diff --git a/lib/nbsd_libc/citrus/citrus_lookup.c b/lib/nbsd_libc/citrus/citrus_lookup.c new file mode 100644 index 000000000..95adc585e --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lookup.c @@ -0,0 +1,356 @@ +/* $NetBSD: citrus_lookup.c,v 1.6 2009/02/03 04:58:38 lukem Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_lookup.c,v 1.6 2009/02/03 04:58:38 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_lookup.h" +#include "citrus_lookup_file.h" + +struct _citrus_lookup { + union { + struct { + struct _citrus_db *db; + struct _citrus_region file; + int num, idx; + struct _db_locator locator; + } db; + struct { + struct _region r; + struct _memstream ms; + } plain; + } u; +#define cl_db u.db.db +#define cl_dbidx u.db.idx +#define cl_dbfile u.db.file +#define cl_dbnum u.db.num +#define cl_dblocator u.db.locator +#define cl_plainr u.plain.r +#define cl_plainms u.plain.ms + int cl_ignore_case; + int cl_rewind; + char *cl_key; + size_t cl_keylen; + int (*cl_next)(struct _citrus_lookup *, struct _region *, + struct _region *); + int (*cl_lookup)(struct _citrus_lookup *, const char *, + struct _region *); + int (*cl_num_entries)(struct _citrus_lookup *); + void (*cl_close)(struct _citrus_lookup *); +}; + +static int +seq_get_num_entries_db(struct _citrus_lookup *cl) +{ + return cl->cl_dbnum; +} + +static int +seq_next_db(struct _citrus_lookup *cl, + struct _region *key, struct _region *data) +{ + + if (cl->cl_key) { + if (key) + _region_init(key, cl->cl_key, cl->cl_keylen); + return _db_lookup_by_s(cl->cl_db, cl->cl_key, data, + &cl->cl_dblocator); + } + + if (cl->cl_rewind) { + cl->cl_dbidx = 0; + } + cl->cl_rewind = 0; + if (cl->cl_dbidx >= cl->cl_dbnum) + return ENOENT; + + return _db_get_entry(cl->cl_db, cl->cl_dbidx++, key, data); +} + +static int +seq_lookup_db(struct _citrus_lookup *cl, const char *key, + struct _region *data) +{ + cl->cl_rewind = 0; + free(cl->cl_key); + cl->cl_key = strdup(key); + if (cl->cl_ignore_case) + _bcs_convert_to_lower(cl->cl_key); + cl->cl_keylen = strlen(cl->cl_key); + _db_locator_init(&cl->cl_dblocator); + return _db_lookup_by_s(cl->cl_db, cl->cl_key, data, &cl->cl_dblocator); +} + +static void +seq_close_db(struct _citrus_lookup *cl) +{ + _db_close(cl->cl_db); + _unmap_file(&cl->cl_dbfile); +} + +static int +seq_open_db(struct _citrus_lookup *cl, const char *name) +{ + int ret; + struct _region r; + char path[PATH_MAX]; + + snprintf(path, sizeof(path), "%s.db", name); + ret = _map_file(&r, path); + if (ret) + return ret; + + ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC, + _db_hash_std, NULL); + if (ret) { + _unmap_file(&r); + return ret; + } + + cl->cl_dbfile = r; + cl->cl_dbnum = _db_get_num_entries(cl->cl_db); + cl->cl_dbidx = 0; + cl->cl_rewind = 1; + cl->cl_lookup = &seq_lookup_db; + cl->cl_next = &seq_next_db; + cl->cl_num_entries = &seq_get_num_entries_db; + cl->cl_close = &seq_close_db; + + return 0; +} + +#define T_COMM '#' +static int +seq_next_plain(struct _citrus_lookup *cl, struct _region *key, + struct _region *data) +{ + const char *p, *q; + size_t len; + + if (cl->cl_rewind) + _memstream_bind(&cl->cl_plainms, &cl->cl_plainr); + cl->cl_rewind = 0; + +retry: + p = _memstream_getln(&cl->cl_plainms, &len); + if (p == NULL) + return ENOENT; + /* ignore comment */ + q = memchr(p, T_COMM, len); + if (q) { + len = q-p; + } + /* ignore trailing spaces */ + _bcs_trunc_rws_len(p, &len); + p = _bcs_skip_ws_len(p, &len); + q = _bcs_skip_nonws_len(p, &len); + if (p==q) + goto retry; + if (cl->cl_key && ((size_t)(q-p) != cl->cl_keylen || + memcmp(p, cl->cl_key, (size_t)(q-p)) != 0)) + goto retry; + + /* found a entry */ + if (key) + _region_init(key, __UNCONST(p), (size_t)(q-p)); + p = _bcs_skip_ws_len(q, &len); + if (data) + _region_init(data, len ? __UNCONST(p) : NULL, len); + + return 0; +} + +static int +seq_get_num_entries_plain(struct _citrus_lookup *cl) +{ + int num; + + num = 0; + while (seq_next_plain(cl, NULL, NULL) == 0) + num++; + + return num; +} + +static int +seq_lookup_plain(struct _citrus_lookup *cl, const char *key, + struct _region *data) +{ + size_t len; + const char *p; + + cl->cl_rewind = 0; + free(cl->cl_key); + cl->cl_key = strdup(key); + if (cl->cl_ignore_case) + _bcs_convert_to_lower(cl->cl_key); + cl->cl_keylen = strlen(cl->cl_key); + _memstream_bind(&cl->cl_plainms, &cl->cl_plainr); + p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0); + if (p == NULL) + return ENOENT; + if (data) + _region_init(data, __UNCONST(p), len); + + return 0; +} + +static void +seq_close_plain(struct _citrus_lookup *cl) +{ + _unmap_file(&cl->cl_plainr); +} + +static int +seq_open_plain(struct _citrus_lookup *cl, const char *name) +{ + int ret; + + /* open read stream */ + ret = _map_file(&cl->cl_plainr, name); + if (ret) + return ret; + + cl->cl_rewind = 1; + cl->cl_next = &seq_next_plain; + cl->cl_lookup = &seq_lookup_plain; + cl->cl_num_entries = &seq_get_num_entries_plain; + cl->cl_close = &seq_close_plain; + + return 0; +} + +int +_citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name, + int ignore_case) +{ + int ret; + struct _citrus_lookup *cl; + + cl = malloc(sizeof(*cl)); + if (cl == NULL) + return errno; + + cl->cl_key = NULL; + cl->cl_keylen = 0; + cl->cl_ignore_case = ignore_case; + ret = seq_open_db(cl, name); + if (ret == ENOENT) + ret = seq_open_plain(cl, name); + if (!ret) + *rcl = cl; + else + free(cl); + + return ret; +} + +void +_citrus_lookup_seq_rewind(struct _citrus_lookup *cl) +{ + cl->cl_rewind = 1; + free(cl->cl_key); + cl->cl_key = NULL; + cl->cl_keylen = 0; +} + +int +_citrus_lookup_seq_next(struct _citrus_lookup *cl, + struct _region *key, struct _region *data) +{ + return (*cl->cl_next)(cl, key, data); +} + +int +_citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key, + struct _region *data) +{ + return (*cl->cl_lookup)(cl, key, data); +} + +int +_citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl) +{ + return (*cl->cl_num_entries)(cl); +} + +void +_citrus_lookup_seq_close(struct _citrus_lookup *cl) +{ + free(cl->cl_key); + (*cl->cl_close)(cl); + free(cl); +} + +char * +_citrus_lookup_simple(const char *name, const char *key, + char *linebuf, size_t linebufsize, int ignore_case) +{ + int ret; + struct _citrus_lookup *cl; + struct _region data; + + ret = _citrus_lookup_seq_open(&cl, name, ignore_case); + if (ret) + return NULL; + + ret = _citrus_lookup_seq_lookup(cl, key, &data); + if (ret) { + _citrus_lookup_seq_close(cl); + return NULL; + } + + snprintf(linebuf, linebufsize, "%.*s", + (int)_region_size(&data), (const char *)_region_head(&data)); + + _citrus_lookup_seq_close(cl); + + return linebuf; +} diff --git a/lib/nbsd_libc/citrus/citrus_lookup.h b/lib/nbsd_libc/citrus/citrus_lookup.h new file mode 100644 index 000000000..92b721fb2 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lookup.h @@ -0,0 +1,61 @@ +/* $NetBSD: citrus_lookup.h,v 1.2 2004/07/21 14:16:34 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LOOKUP_H_ +#define _CITRUS_LOOKUP_H_ + +#define _CITRUS_LOOKUP_CASE_SENSITIVE 0 +#define _CITRUS_LOOKUP_CASE_IGNORE 1 + +struct _citrus_lookup; +__BEGIN_DECLS +char *_citrus_lookup_simple(const char *, const char *, char *, size_t, int); +int _citrus_lookup_seq_open(struct _citrus_lookup **, const char *, int); +void _citrus_lookup_seq_rewind(struct _citrus_lookup *); +int _citrus_lookup_seq_next(struct _citrus_lookup *, + struct _region *, struct _region *); +int _citrus_lookup_seq_lookup(struct _citrus_lookup *, const char *, + struct _region *); +int _citrus_lookup_get_number_of_entries(struct _citrus_lookup *); +void _citrus_lookup_seq_close(struct _citrus_lookup *); +__END_DECLS + +static __inline const char * +_citrus_lookup_alias(const char *path, const char *key, char *buf, size_t n, + int ignore_case) +{ + const char *ret; + + ret = _citrus_lookup_simple(path, key, buf, n, ignore_case); + if (ret == NULL) + ret = key; + + return ret; +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_lookup_factory.c b/lib/nbsd_libc/citrus/citrus_lookup_factory.c new file mode 100644 index 000000000..7d14b5151 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lookup_factory.c @@ -0,0 +1,127 @@ +/* $NetBSD: citrus_lookup_factory.c,v 1.4 2003/10/27 00:12:42 lukem Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_lookup_factory.c,v 1.4 2003/10/27 00:12:42 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_bcs.h" +#include "citrus_db_factory.h" +#include "citrus_db_hash.h" +#include "citrus_lookup_factory.h" +#include "citrus_lookup_file.h" + +#define T_COMM '#' +static int +convert_line(struct _citrus_db_factory *df, const char *line, size_t len) +{ + const char *p; + char key[LINE_MAX], data[LINE_MAX]; + + /* cut off trailing comment */ + p = memchr(line, T_COMM, len); + if (p) + len = p - line; + + /* key */ + line = _bcs_skip_ws_len(line, &len); + if (len == 0) + return 0; + p = _bcs_skip_nonws_len(line, &len); + if (p==line) + return 0; + snprintf(key, sizeof(key), "%.*s", (int)(p-line), line); + _bcs_convert_to_lower(key); + + /* data */ + line = _bcs_skip_ws_len(p, &len); + _bcs_trunc_rws_len(line, &len); + snprintf(data, sizeof(data), "%.*s", (int)len, line); + + return _db_factory_addstr_by_s(df, key, data); +} + +static int +dump_db(struct _citrus_db_factory *df, struct _region *r) +{ + size_t size; + void *ptr; + + size = _db_factory_calc_size(df); + ptr = malloc(size); + if (ptr == NULL) + return errno; + _region_init(r, ptr, size); + + return _db_factory_serialize(df, _CITRUS_LOOKUP_MAGIC, r); +} + +int +_citrus_lookup_factory_convert(FILE *out, FILE *in) +{ + struct _citrus_db_factory *df; + struct _region r; + char *line; + size_t size; + int ret; + + ret = _db_factory_create(&df, &_db_hash_std, NULL); + if (ret) + return ret; + + while ((line = fgetln(in, &size)) != NULL) + if ((ret = convert_line(df, line, size))) { + _db_factory_free(df); + return ret; + } + + ret = dump_db(df, &r); + _db_factory_free(df); + if (ret) + return ret; + + if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1) + return errno; + + return 0; +} diff --git a/lib/nbsd_libc/citrus/citrus_lookup_factory.h b/lib/nbsd_libc/citrus/citrus_lookup_factory.h new file mode 100644 index 000000000..d50a1e892 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lookup_factory.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_lookup_factory.h,v 1.1 2003/06/25 09:51:35 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LOOKUP_FACTORY_H_ +#define _CITRUS_LOOKUP_FACTORY_H_ + +__BEGIN_DECLS +int _citrus_lookup_factory_convert(FILE *, FILE *); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_lookup_file.h b/lib/nbsd_libc/citrus/citrus_lookup_file.h new file mode 100644 index 000000000..0c56b6126 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_lookup_file.h @@ -0,0 +1,34 @@ +/* $NetBSD: citrus_lookup_file.h,v 1.1 2003/06/25 09:51:36 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_LOOKUP_FILE_H_ +#define _CITRUS_LOOKUP_FILE_H_ + +#define _CITRUS_LOOKUP_MAGIC "LOOKUP\0\0" + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_mapper.c b/lib/nbsd_libc/citrus/citrus_mapper.c new file mode 100644 index 000000000..cef71bac6 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_mapper.c @@ -0,0 +1,404 @@ +/* $NetBSD: citrus_mapper.c,v 1.7 2008/07/25 14:05:25 christos Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mapper.c,v 1.7 2008/07/25 14:05:25 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_bcs.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" + +#define _CITRUS_MAPPER_DIR "mapper.dir" + +#define CM_HASH_SIZE 101 +#define REFCOUNT_PERSISTENT -1 + +#ifdef _REENTRANT +static rwlock_t lock = RWLOCK_INITIALIZER; +#endif + +struct _citrus_mapper_area { + _CITRUS_HASH_HEAD(, _citrus_mapper, CM_HASH_SIZE) ma_cache; + char *ma_dir; +}; + +/* + * _citrus_mapper_create_area: + * create mapper area + */ + +int +_citrus_mapper_create_area( + struct _citrus_mapper_area *__restrict *__restrict rma, + const char *__restrict area) +{ + struct stat st; + int ret; + char path[PATH_MAX]; + struct _citrus_mapper_area *ma; + + rwlock_wrlock(&lock); + + if (*rma != NULL) { + ret = 0; + goto quit; + } + + snprintf(path, (size_t)PATH_MAX, "%s/%s", area, _CITRUS_MAPPER_DIR); + + ret = stat(path, &st); + if (ret) + goto quit; + + ma = malloc(sizeof(*ma)); + if (ma == NULL) { + ret = errno; + goto quit; + } + ma->ma_dir = strdup(area); + if (ma->ma_dir == NULL) { + ret = errno; + free(ma->ma_dir); + goto quit; + } + _CITRUS_HASH_INIT(&ma->ma_cache, CM_HASH_SIZE); + + *rma = ma; + ret = 0; +quit: + rwlock_unlock(&lock); + + return ret; +} + + +/* + * lookup_mapper_entry: + * lookup mapper.dir entry in the specified directory. + * + * line format of iconv.dir file: + * mapper module arg + * mapper : mapper name. + * module : mapper module name. + * arg : argument for the module (generally, description file name) + */ + +static int +lookup_mapper_entry(const char *dir, const char *mapname, + void *linebuf, size_t linebufsize, + const char **module, const char **variable) +{ + struct _region r; + struct _memstream ms; + int ret; + const char *cp, *cq; + char *p; + size_t len; + char path[PATH_MAX]; + + /* create mapper.dir path */ + snprintf(path, (size_t)PATH_MAX, "%s/%s", dir, _CITRUS_MAPPER_DIR); + + /* open read stream */ + ret = _map_file(&r, path); + if (ret) + return ret; + + _memstream_bind(&ms, &r); + + /* search the line matching to the map name */ + cp = _memstream_matchline(&ms, mapname, &len, 0); + if (!cp) { + ret = ENOENT; + goto quit; + } + if (!len || len>linebufsize-1) { + ret = EINVAL; + goto quit; + } + + p = linebuf; + /* get module name */ + *module = p; + cq = _bcs_skip_nonws_len(cp, &len); + strlcpy(p, cp, (size_t)(cq-cp+1)); + p += cq-cp+1; + + /* get variable */ + *variable = p; + cp = _bcs_skip_ws_len(cq, &len); + strlcpy(p, cp, len+1); + + ret = 0; + +quit: + _unmap_file(&r); + return ret; +} + +/* + * mapper_close: + * simply close a mapper. (without handling hash) + */ +static void +mapper_close(struct _citrus_mapper *cm) +{ + if (cm->cm_module) { + if (cm->cm_ops) { + if (cm->cm_closure) + (*cm->cm_ops->mo_uninit)(cm); + free(cm->cm_ops); + } + _citrus_unload_module(cm->cm_module); + } + free(cm->cm_traits); + free(cm); +} + +/* + * mapper_open: + * simply open a mapper. (without handling hash) + */ +static int +mapper_open(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char * __restrict module, + const char * __restrict variable) +{ + int ret; + struct _citrus_mapper *cm; + _citrus_mapper_getops_t getops; + + /* initialize mapper handle */ + cm = malloc(sizeof(*cm)); + if (!cm) + return errno; + + cm->cm_module = NULL; + cm->cm_ops = NULL; + cm->cm_closure = NULL; + cm->cm_traits = NULL; + cm->cm_refcount = 0; + cm->cm_key = NULL; + + /* load module */ + ret = _citrus_load_module(&cm->cm_module, module); + if (ret) + goto err; + + /* get operators */ + getops = (_citrus_mapper_getops_t) + _citrus_find_getops(cm->cm_module, module, "mapper"); + if (!getops) { + ret = EOPNOTSUPP; + goto err; + } + cm->cm_ops = malloc(sizeof(*cm->cm_ops)); + if (!cm->cm_ops) { + ret = errno; + goto err; + } + ret = (*getops)(cm->cm_ops, sizeof(*cm->cm_ops), + _CITRUS_MAPPER_ABI_VERSION); + if (ret) + goto err; + + if (!cm->cm_ops->mo_init || + !cm->cm_ops->mo_uninit || + !cm->cm_ops->mo_convert || + !cm->cm_ops->mo_init_state) + goto err; + + /* allocate traits structure */ + cm->cm_traits = malloc(sizeof(*cm->cm_traits)); + if (cm->cm_traits == NULL) { + ret = errno; + goto err; + } + /* initialize the mapper */ + ret = (*cm->cm_ops->mo_init)(ma, cm, ma->ma_dir, + (const void *)variable, + strlen(variable)+1, + cm->cm_traits, sizeof(*cm->cm_traits)); + if (ret) + goto err; + + *rcm = cm; + + return 0; + +err: + mapper_close(cm); + return ret; +} + +/* + * _citrus_mapper_open_direct: + * open a mapper. + */ +int +_citrus_mapper_open_direct(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char * __restrict module, + const char * __restrict variable) +{ + return mapper_open(ma, rcm, module, variable); +} + +/* + * hash_func + */ +static __inline int +hash_func(const char *key) +{ + return _string_hash_func(key, CM_HASH_SIZE); +} + +/* + * match_func + */ +static __inline int +match_func(struct _citrus_mapper *cm, const char *key) +{ + return strcmp(cm->cm_key, key); +} + +/* + * _citrus_mapper_open: + * open a mapper with looking up "mapper.dir". + */ +int +_citrus_mapper_open(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char * __restrict mapname) +{ + int ret; + char linebuf[PATH_MAX]; + const char *module, *variable = NULL; + struct _citrus_mapper *cm; + int hashval; + + rwlock_wrlock(&lock); + + /* search in the cache */ + hashval = hash_func(mapname); + _CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname, + hashval); + if (cm) { + /* found */ + cm->cm_refcount++; + *rcm = cm; + ret = 0; + goto quit; + } + + /* search mapper entry */ + ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf, + (size_t)PATH_MAX, &module, &variable); + if (ret) + goto quit; + + /* open mapper */ + ret = mapper_open(ma, &cm, module, variable); + if (ret) + goto quit; + cm->cm_key = strdup(mapname); + if (cm->cm_key == NULL) { + ret = errno; + rwlock_unlock(&lock); + _mapper_close(cm); + return ret; + } + + /* insert to the cache */ + cm->cm_refcount = 1; + _CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval); + + *rcm = cm; + ret = 0; +quit: + rwlock_unlock(&lock); + return ret; +} + +/* + * _citrus_mapper_close: + * close the specified mapper. + */ +void +_citrus_mapper_close(struct _citrus_mapper *cm) +{ + if (cm) { + rwlock_wrlock(&lock); + if (cm->cm_refcount == REFCOUNT_PERSISTENT) + goto quit; + if (cm->cm_refcount > 0) { + if (--cm->cm_refcount > 0) + goto quit; + _CITRUS_HASH_REMOVE(cm, cm_entry); + free(cm->cm_key); + } + rwlock_unlock(&lock); + mapper_close(cm); + return; +quit: + rwlock_unlock(&lock); + } +} + +/* + * _citrus_mapper_set_persistent: + * set persistent count. + */ +void +_citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm) +{ + rwlock_wrlock(&lock); + cm->cm_refcount = REFCOUNT_PERSISTENT; + rwlock_unlock(&lock); +} diff --git a/lib/nbsd_libc/citrus/citrus_mapper.h b/lib/nbsd_libc/citrus/citrus_mapper.h new file mode 100644 index 000000000..2eb1d22ab --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_mapper.h @@ -0,0 +1,142 @@ +/* $NetBSD: citrus_mapper.h,v 1.3 2003/07/12 15:39:19 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_H_ +#define _CITRUS_MAPPER_H_ + +struct _citrus_mapper_area; +struct _citrus_mapper; +struct _citrus_mapper_ops; +struct _citrus_mapper_traits; + +__BEGIN_DECLS +int _citrus_mapper_create_area( + struct _citrus_mapper_area *__restrict *__restrict, + const char *__restrict); +int _citrus_mapper_open(struct _citrus_mapper_area *__restrict, + struct _citrus_mapper *__restrict *__restrict, + const char *__restrict); +int _citrus_mapper_open_direct( + struct _citrus_mapper_area *__restrict, + struct _citrus_mapper *__restrict *__restrict, + const char *__restrict, const char *__restrict); +void _citrus_mapper_close(struct _citrus_mapper *); +void _citrus_mapper_set_persistent(struct _citrus_mapper * __restrict); +__END_DECLS + +#include "citrus_mapper_local.h" + +/* return values of _citrus_mapper_convert */ +#define _CITRUS_MAPPER_CONVERT_SUCCESS (0) +#define _CITRUS_MAPPER_CONVERT_NONIDENTICAL (1) +#define _CITRUS_MAPPER_CONVERT_SRC_MORE (2) +#define _CITRUS_MAPPER_CONVERT_DST_MORE (3) +#define _CITRUS_MAPPER_CONVERT_ILSEQ (4) +#define _CITRUS_MAPPER_CONVERT_FATAL (5) + +/* + * _citrus_mapper_convert: + * convert an index. + * - if the converter supports M:1 converter, the function may return + * _CITRUS_MAPPER_CONVERT_SRC_MORE and the storage pointed by dst + * may be unchanged in this case, although the internal status of + * the mapper is affected. + * - if the converter supports 1:N converter, the function may return + * _CITRUS_MAPPER_CONVERT_DST_MORE. In this case, the contiguous + * call of this function ignores src and changes the storage pointed + * by dst. + * - if the converter supports M:N converter, the function may behave + * the combination of the above. + * + */ +static __inline int +_citrus_mapper_convert(struct _citrus_mapper * __restrict cm, + _citrus_index_t * __restrict dst, + _citrus_index_t src, + void * __restrict ps) +{ + + _DIAGASSERT(cm && cm->cm_ops && cm->cm_ops->mo_convert && dst); + + return (*cm->cm_ops->mo_convert)(cm, dst, src, ps); +} + +/* + * _citrus_mapper_init_state: + * initialize the state. + */ +static __inline void +_citrus_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ + + _DIAGASSERT(cm && cm->cm_ops && cm->cm_ops->mo_init_state); + + (*cm->cm_ops->mo_init_state)(cm, ps); +} + +/* + * _citrus_mapper_get_state_size: + * get the size of state storage. + */ +static __inline size_t +_citrus_mapper_get_state_size(struct _citrus_mapper * __restrict cm) +{ + + _DIAGASSERT(cm && cm->cm_traits); + + return cm->cm_traits->mt_state_size; +} + +/* + * _citrus_mapper_get_src_max: + * get the maximum number of suspended sources. + */ +static __inline size_t +_citrus_mapper_get_src_max(struct _citrus_mapper * __restrict cm) +{ + + _DIAGASSERT(cm && cm->cm_traits); + + return cm->cm_traits->mt_src_max; +} + +/* + * _citrus_mapper_get_dst_max: + * get the maximum number of suspended destinations. + */ +static __inline size_t +_citrus_mapper_get_dst_max(struct _citrus_mapper * __restrict cm) +{ + + _DIAGASSERT(cm && cm->cm_traits); + + return cm->cm_traits->mt_dst_max; +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_mapper_local.h b/lib/nbsd_libc/citrus/citrus_mapper_local.h new file mode 100644 index 000000000..4861e6655 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_mapper_local.h @@ -0,0 +1,105 @@ +/* $NetBSD: citrus_mapper_local.h,v 1.2 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_LOCAL_H_ +#define _CITRUS_MAPPER_LOCAL_H_ + +#define _CITRUS_MAPPER_GETOPS_FUNC_BASE(_n_) \ +int _n_(struct _citrus_mapper_ops *, size_t, uint32_t) +#define _CITRUS_MAPPER_GETOPS_FUNC(_n_) \ +_CITRUS_MAPPER_GETOPS_FUNC_BASE(_citrus_##_n_##_mapper_getops) + +#define _CITRUS_MAPPER_DECLS(_m_) \ +static int _citrus_##_m_##_mapper_init \ + (struct _citrus_mapper_area *__restrict, \ + struct _citrus_mapper * __restrict, const char * __restrict, \ + const void * __restrict, size_t, \ + struct _citrus_mapper_traits * __restrict, size_t); \ +static void _citrus_##_m_##_mapper_uninit(struct _citrus_mapper *); \ +static int _citrus_##_m_##_mapper_convert \ + (struct _citrus_mapper * __restrict, \ + _citrus_index_t * __restrict, _citrus_index_t, \ + void * __restrict); \ +static void _citrus_##_m_##_mapper_init_state \ + (struct _citrus_mapper * __restrict, void * __restrict); + +#define _CITRUS_MAPPER_DEF_OPS(_m_) \ +struct _citrus_mapper_ops _citrus_##_m_##_mapper_ops = { \ + /* mo_abi_version */ _CITRUS_MAPPER_ABI_VERSION, \ + /* mo_init */ &_citrus_##_m_##_mapper_init, \ + /* mo_uninit */ &_citrus_##_m_##_mapper_uninit, \ + /* mo_convert */ &_citrus_##_m_##_mapper_convert, \ + /* mo_init_state */ &_citrus_##_m_##_mapper_init_state \ +} + +typedef _CITRUS_MAPPER_GETOPS_FUNC_BASE((*_citrus_mapper_getops_t)); +typedef int (*_citrus_mapper_init_t)( + struct _citrus_mapper_area *__restrict, + struct _citrus_mapper *__restrict, const char *__restrict, + const void *__restrict, size_t, + struct _citrus_mapper_traits * __restrict, size_t); +typedef void (*_citrus_mapper_uninit_t)(struct _citrus_mapper *); +typedef int (*_citrus_mapper_convert_t)(struct _citrus_mapper * __restrict, + _citrus_index_t * __restrict, + _citrus_index_t, + void * __restrict); +typedef void (*_citrus_mapper_init_state_t)( + struct _citrus_mapper * __restrict, void * __restrict); + +/* + * ABI version change log + * 0x00000001 + * initial version + */ +#define _CITRUS_MAPPER_ABI_VERSION 0x00000001 +struct _citrus_mapper_ops { + uint32_t mo_abi_version; + /* version 0x00000001 */ + _citrus_mapper_init_t mo_init; + _citrus_mapper_uninit_t mo_uninit; + _citrus_mapper_convert_t mo_convert; + _citrus_mapper_init_state_t mo_init_state; +}; + +struct _citrus_mapper_traits { + /* version 0x00000001 */ + size_t mt_state_size; + size_t mt_src_max; + size_t mt_dst_max; +}; + +struct _citrus_mapper { + struct _citrus_mapper_ops *cm_ops; + void *cm_closure; + _citrus_module_t cm_module; + struct _citrus_mapper_traits *cm_traits; + _CITRUS_HASH_ENTRY(_citrus_mapper) cm_entry; + int cm_refcount; + char *cm_key; +}; +#endif diff --git a/lib/nbsd_libc/citrus/citrus_memstream.c b/lib/nbsd_libc/citrus/citrus_memstream.c new file mode 100644 index 000000000..e32a97c4a --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_memstream.c @@ -0,0 +1,153 @@ +/* $NetBSD: citrus_memstream.c,v 1.4 2009/02/03 05:02:12 lukem Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_memstream.c,v 1.4 2009/02/03 05:02:12 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_bcs.h" + +const char * +_citrus_memory_stream_getln(struct _citrus_memory_stream * __restrict ms, + size_t * __restrict rlen) +{ + int i; + const uint8_t *h, *p; + size_t ret; + + if (ms->ms_pos>=_region_size(&ms->ms_region)) + return (NULL); + + h = p = (uint8_t *)_region_offset(&ms->ms_region, ms->ms_pos); + ret = 0; + for (i = _region_size(&ms->ms_region) - ms->ms_pos; i > 0; i--) { + ret++; + if (_bcs_iseol(*p)) + break; + p++; + } + + ms->ms_pos += ret; + *rlen = ret; + return ((const char *)h); +} + +#define T_COMM '#' + +const char * +_citrus_memory_stream_matchline(struct _citrus_memory_stream * __restrict ms, + const char * __restrict key, + size_t * __restrict rlen, + int iscasesensitive) +{ + const char *p, *q; + size_t len, keylen; + + keylen = strlen(key); + while (/*CONSTCOND*/ 1) { + p = _citrus_memory_stream_getln(ms, &len); + if (p == NULL) + return (NULL); + + /* ignore comment */ + q = memchr(p, T_COMM, len); + if (q) { + len = q-p; + } + /* ignore trailing white space and newline */ + _bcs_trunc_rws_len(p, &len); + if (len == 0) + continue; /* ignore null line */ + + /* skip white spaces at the head of the line */ + p = _bcs_skip_ws_len(p, &len); + q = _bcs_skip_nonws_len(p, &len); + + if ((size_t)(q-p) == keylen) { + if (iscasesensitive) { + if (memcmp(key, p, keylen) == 0) + break; /* match */ + } else { + if (_bcs_strncasecmp(key, p, keylen) == 0) + break; /* match */ + } + } + } + + p = _bcs_skip_ws_len(q, &len); + *rlen = len; + + return (p); +} + +void * +_citrus_memory_stream_chr(struct _citrus_memory_stream *ms, + struct _citrus_region *r, char ch) +{ + void *head, *chr; + size_t sz; + + if (ms->ms_pos >= _region_size(&ms->ms_region)) + return NULL; + + head = _region_offset(&ms->ms_region, ms->ms_pos); + chr = memchr(head, ch, _memstream_remainder(ms)); + if (chr == NULL) { + _region_init(r, head, _memstream_remainder(ms)); + ms->ms_pos = _region_size(&ms->ms_region); + return NULL; + } + sz = (char *)chr - (char *)head; + + _region_init(r, head, sz); + ms->ms_pos += sz+1; + + return chr; +} + +void +_citrus_memory_stream_skip_ws(struct _citrus_memory_stream *ms) +{ + int ch; + + while ((ch = _memstream_peek(ms)) != EOF) { + if (!_bcs_isspace(ch)) + break; + _memstream_getc(ms); + } +} diff --git a/lib/nbsd_libc/citrus/citrus_memstream.h b/lib/nbsd_libc/citrus/citrus_memstream.h new file mode 100644 index 000000000..baeb14bc4 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_memstream.h @@ -0,0 +1,213 @@ +/* $NetBSD: citrus_memstream.h,v 1.3 2005/05/14 17:55:42 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_MEMSTREAM_H_ +#define _CITRUS_MEMSTREAM_H_ + +struct _citrus_memory_stream { + struct _citrus_region ms_region; + size_t ms_pos; +}; + +__BEGIN_DECLS +const char * _citrus_memory_stream_getln( + struct _citrus_memory_stream * __restrict, size_t * __restrict); +const char * _citrus_memory_stream_matchline( + struct _citrus_memory_stream * __restrict, const char * __restrict, + size_t * __restrict, int); +void * _citrus_memory_stream_chr(struct _citrus_memory_stream *, + struct _citrus_region *, char); +void _citrus_memory_stream_skip_ws(struct _citrus_memory_stream *); +__END_DECLS + +static __inline int +_citrus_memory_stream_iseof(struct _citrus_memory_stream *ms) +{ + return ms->ms_pos >= _citrus_region_size(&ms->ms_region); +} + +static __inline void +_citrus_memory_stream_bind(struct _citrus_memory_stream * __restrict ms, + const struct _citrus_region * __restrict r) +{ + ms->ms_region = *r; + ms->ms_pos = 0; +} + +static __inline void +_citrus_memory_stream_bind_ptr(struct _citrus_memory_stream * __restrict ms, + void *ptr, size_t sz) +{ + struct _citrus_region r; + + _citrus_region_init(&r, ptr, sz); + _citrus_memory_stream_bind(ms, &r); +} + +static __inline void +_citrus_memory_stream_rewind(struct _citrus_memory_stream *ms) +{ + ms->ms_pos = 0; +} + +static __inline size_t +_citrus_memory_stream_tell(struct _citrus_memory_stream *ms) +{ + return ms->ms_pos; +} + +static __inline size_t +_citrus_memory_stream_remainder(struct _citrus_memory_stream *ms) +{ + size_t sz; + sz = _citrus_region_size(&ms->ms_region); + if (ms->ms_pos>sz) + return 0; + return sz-ms->ms_pos; +} + +static __inline int +_citrus_memory_stream_seek(struct _citrus_memory_stream *ms, size_t pos, int w) +{ + size_t sz = _citrus_region_size(&ms->ms_region); + switch (w) { + case SEEK_SET: + if (pos>=sz) + return -1; + ms->ms_pos = pos; + break; + case SEEK_CUR: + pos += (ssize_t)ms->ms_pos; + if (pos>=sz) + return -1; + ms->ms_pos = pos; + break; + case SEEK_END: + if (szms_pos = sz - pos; + break; + } + return 0; +} + +static __inline int +_citrus_memory_stream_getc(struct _citrus_memory_stream *ms) +{ + if (_citrus_memory_stream_iseof(ms)) + return (EOF); + return _citrus_region_peek8(&ms->ms_region, ms->ms_pos++); +} + +static __inline void +_citrus_memory_stream_ungetc(struct _citrus_memory_stream *ms, int ch) +{ + if (ch != EOF && ms->ms_pos > 0) + ms->ms_pos--; +} + +static __inline int +_citrus_memory_stream_peek(struct _citrus_memory_stream *ms) +{ + if (_citrus_memory_stream_iseof(ms)) + return (EOF); + return _citrus_region_peek8(&ms->ms_region, ms->ms_pos); +} + +static __inline void * +_citrus_memory_stream_getregion(struct _citrus_memory_stream *ms, + struct _citrus_region *r, size_t sz) +{ + void *ret; + + if (ms->ms_pos + sz > _citrus_region_size(&ms->ms_region)) + return NULL; + + ret = _citrus_region_offset(&ms->ms_region, ms->ms_pos); + ms->ms_pos += sz; + if (r) + _citrus_region_init(r, ret, sz); + + return ret; +} + +static __inline int +_citrus_memory_stream_get8(struct _citrus_memory_stream *ms, uint8_t *rval) +{ + + if (ms->ms_pos + 1 > _citrus_region_size(&ms->ms_region)) + return -1; + + *rval = _citrus_region_peek8(&ms->ms_region, ms->ms_pos); + ms->ms_pos += 2; + + return 0; +} + +static __inline int +_citrus_memory_stream_get16(struct _citrus_memory_stream *ms, uint16_t *rval) +{ + + if (ms->ms_pos + 2 > _citrus_region_size(&ms->ms_region)) + return -1; + + *rval = _citrus_region_peek16(&ms->ms_region, ms->ms_pos); + ms->ms_pos += 2; + + return 0; +} + +static __inline int +_citrus_memory_stream_get32(struct _citrus_memory_stream *ms, uint32_t *rval) +{ + + if (ms->ms_pos + 4 > _citrus_region_size(&ms->ms_region)) + return -1; + + *rval = _citrus_region_peek32(&ms->ms_region, ms->ms_pos); + ms->ms_pos += 4; + + return 0; +} + +static __inline int +_citrus_memory_stream_getln_region(struct _citrus_memory_stream *ms, + struct _citrus_region *r) +{ + const char *ptr; + size_t sz; + + ptr = _citrus_memory_stream_getln(ms, &sz); + if (ptr) + _citrus_region_init(r, __UNCONST(ptr), sz); + + return ptr == NULL; +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_mmap.c b/lib/nbsd_libc/citrus/citrus_mmap.c new file mode 100644 index 000000000..7bbc51800 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_mmap.c @@ -0,0 +1,100 @@ +/* $NetBSD: citrus_mmap.c,v 1.3 2005/01/19 00:52:37 mycroft Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mmap.c,v 1.3 2005/01/19 00:52:37 mycroft Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_mmap.h" + +int +_citrus_map_file(struct _citrus_region * __restrict r, + const char * __restrict path) +{ + int fd, ret = 0; + struct stat st; + void *head; + + _DIAGASSERT(r != NULL); + + _region_init(r, NULL, 0); + + if ((fd = open(path, O_RDONLY)) == -1) + return errno; + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { + ret = errno; + goto error; + } + + if (fstat(fd, &st) == -1) { + ret = errno; + goto error; + } + if (!S_ISREG(st.st_mode)) { + ret = EOPNOTSUPP; + goto error; + } + + head = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, + fd, (off_t)0); + if (head == MAP_FAILED) { + ret = errno; + goto error; + } + _region_init(r, head, (size_t)st.st_size); + +error: + (void)close(fd); + return ret; +} + +void +_citrus_unmap_file(struct _citrus_region *r) +{ + + _DIAGASSERT(r != NULL); + + if (_region_head(r) != NULL) { + (void)munmap(_region_head(r), _region_size(r)); + _region_init(r, NULL, 0); + } +} diff --git a/lib/nbsd_libc/citrus/citrus_mmap.h b/lib/nbsd_libc/citrus/citrus_mmap.h new file mode 100644 index 000000000..1a968a4ca --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_mmap.h @@ -0,0 +1,39 @@ +/* $NetBSD: citrus_mmap.h,v 1.1 2003/06/25 09:51:38 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_MMAP_H_ +#define _CITRUS_MMAP_H_ + +__BEGIN_DECLS +int _citrus_map_file(struct _citrus_region * __restrict, + const char * __restrict); +void _citrus_unmap_file(struct _citrus_region *); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_module.c b/lib/nbsd_libc/citrus/citrus_module.c new file mode 100644 index 000000000..e0f542cc7 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_module.c @@ -0,0 +1,357 @@ +/* $NetBSD: citrus_module.c,v 1.9 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)1999, 2000, 2001, 2002 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Paul Kranenburg. + * + * 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_module.c,v 1.9 2009/01/11 02:46:24 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_module.h" + +#include +#include +#include + +#ifdef _I18N_DYNAMIC + +static int _getdewey(int [], char *); +static int _cmpndewey(int [], int, int [], int); +static const char *_findshlib(char *, int *, int *); + +static const char *_pathI18nModule = NULL; + +/* from libexec/ld.aout_so/shlib.c */ +#undef major +#undef minor +#define MAXDEWEY 3 /*ELF*/ + +static int +_getdewey(int dewey[], char *cp) +{ + int i, n; + + _DIAGASSERT(dewey != NULL); + _DIAGASSERT(cp != NULL); + + for (n = 0, i = 0; i < MAXDEWEY; i++) { + if (*cp == '\0') + break; + + if (*cp == '.') cp++; + if (*cp < '0' || '9' < *cp) + return 0; + + dewey[n++] = (int)_bcs_strtol(cp, &cp, 10); + } + + return n; +} + +/* + * Compare two dewey arrays. + * Return -1 if `d1' represents a smaller value than `d2'. + * Return 1 if `d1' represents a greater value than `d2'. + * Return 0 if equal. + */ +static int +_cmpndewey(int d1[], int n1, int d2[], int n2) +{ + register int i; + + _DIAGASSERT(d1 != NULL); + _DIAGASSERT(d2 != NULL); + + for (i = 0; i < n1 && i < n2; i++) { + if (d1[i] < d2[i]) + return -1; + if (d1[i] > d2[i]) + return 1; + } + + if (n1 == n2) + return 0; + + if (i == n1) + return -1; + + if (i == n2) + return 1; + + /* XXX cannot happen */ + return 0; +} + +static const char * +_findshlib(char *name, int *majorp, int *minorp) +{ + int dewey[MAXDEWEY]; + int ndewey; + int tmp[MAXDEWEY]; + int i; + int len; + char *lname; + static char path[PATH_MAX]; + int major, minor; + const char *search_dirs[1]; + const int n_search_dirs = 1; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(majorp != NULL); + _DIAGASSERT(minorp != NULL); + + major = *majorp; + minor = *minorp; + path[0] = '\0'; + search_dirs[0] = _pathI18nModule; + len = strlen(name); + lname = name; + + ndewey = 0; + + for (i = 0; i < n_search_dirs; i++) { + DIR *dd = opendir(search_dirs[i]); + struct dirent *dp; + int found_dot_a = 0; + int found_dot_so = 0; + + if (dd == NULL) + continue; + + while ((dp = readdir(dd)) != NULL) { + int n; + + if (dp->d_namlen < len + 4) + continue; + if (strncmp(dp->d_name, lname, (size_t)len) != 0) + continue; + if (strncmp(dp->d_name+len, ".so.", 4) != 0) + continue; + + if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0) + continue; + + if (major != -1 && found_dot_a) + found_dot_a = 0; + + /* XXX should verify the library is a.out/ELF? */ + + if (major == -1 && minor == -1) { + goto compare_version; + } else if (major != -1 && minor == -1) { + if (tmp[0] == major) + goto compare_version; + } else if (major != -1 && minor != -1) { + if (tmp[0] == major) { + if (n == 1 || tmp[1] >= minor) + goto compare_version; + } + } + + /* else, this file does not qualify */ + continue; + + compare_version: + if (_cmpndewey(tmp, n, dewey, ndewey) <= 0) + continue; + + /* We have a better version */ + found_dot_so = 1; + snprintf(path, sizeof(path), "%s/%s", search_dirs[i], + dp->d_name); + found_dot_a = 0; + bcopy(tmp, dewey, sizeof(dewey)); + ndewey = n; + *majorp = dewey[0]; + *minorp = dewey[1]; + } + closedir(dd); + + if (found_dot_a || found_dot_so) + /* + * There's a lib in this dir; take it. + */ + return path[0] ? path : NULL; + } + + return path[0] ? path : NULL; +} + +void * +_citrus_find_getops(_citrus_module_t handle, const char *modname, + const char *ifname) +{ + char name[PATH_MAX]; + void *p; + + _DIAGASSERT(handle != NULL); + _DIAGASSERT(modname != NULL); + _DIAGASSERT(ifname != NULL); + + snprintf(name, sizeof(name), _C_LABEL_STRING("_citrus_%s_%s_getops"), + modname, ifname); + p = dlsym((void *)handle, name); + return p; +} + +int +_citrus_load_module(_citrus_module_t *rhandle, const char *encname) +{ + const char *p; + char path[PATH_MAX]; + int maj, min; + void *handle; + + _DIAGASSERT(rhandle != NULL); + + if (_pathI18nModule == NULL) { + p = getenv("PATH_I18NMODULE"); + if (p != NULL && !issetugid()) { + _pathI18nModule = strdup(p); + if (_pathI18nModule == NULL) + return ENOMEM; + } else + _pathI18nModule = _PATH_I18NMODULE; + } + + (void)snprintf(path, sizeof(path), "lib%s", encname); + maj = I18NMODULE_MAJOR; + min = -1; + p = _findshlib(path, &maj, &min); + if (!p) + return (EINVAL); + handle = dlopen(p, RTLD_LAZY); + if (!handle) + return (EINVAL); + + *rhandle = (_citrus_module_t)handle; + + return (0); +} + +void +_citrus_unload_module(_citrus_module_t handle) +{ + if (handle) + dlclose((void *)handle); +} +#else +/* !_I18N_DYNAMIC */ + +void * +/*ARGSUSED*/ +_citrus_find_getops(_citrus_module_t handle, const char *modname, + const char *ifname) +{ + return (NULL); +} + +int +/*ARGSUSED*/ +_citrus_load_module(_citrus_module_t *rhandle, char const *modname) +{ + return (EINVAL); +} + +void +/*ARGSUSED*/ +_citrus_unload_module(_citrus_module_t handle) +{ +} +#endif diff --git a/lib/nbsd_libc/citrus/citrus_module.h b/lib/nbsd_libc/citrus/citrus_module.h new file mode 100644 index 000000000..d7e68b837 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_module.h @@ -0,0 +1,43 @@ +/* $NetBSD: citrus_module.h,v 1.1 2002/03/17 22:14:20 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_MODULE_H_ +#define _CITRUS_MODULE_H_ + +typedef struct _citrus_module_rec *_citrus_module_t; + +__BEGIN_DECLS +void *_citrus_find_getops(_citrus_module_t __restrict, + const char * __restrict, const char * __restrict); +int _citrus_load_module(_citrus_module_t * __restrict, + const char * __restrict); +void _citrus_unload_module(_citrus_module_t); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_namespace.h b/lib/nbsd_libc/citrus/citrus_namespace.h new file mode 100644 index 000000000..bb0937d6f --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_namespace.h @@ -0,0 +1,238 @@ +/* $NetBSD: citrus_namespace.h,v 1.8 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_NAMESPACE_H_ +#define _CITRUS_NAMESPACE_H_ + +/* citrus_alias */ +#ifndef _CITRUS_ALIAS_NO_NAMESPACE +#define _alias_lookup _citrus_alias_lookup +#endif /* _CITRUS_ALIAS_NO_NAMESPACE */ + +/* citrus_bcs */ +#ifndef _CITRUS_BCS_NO_NAMESPACE +#define _bcs_isalnum _citrus_bcs_isalnum +#define _bcs_isalpha _citrus_bcs_isalpha +#define _bcs_isblank _citrus_bcs_isblank +#define _bcs_isdigit _citrus_bcs_isdigit +#define _bcs_islower _citrus_bcs_islower +#define _bcs_iseol _citrus_bcs_iseol +#define _bcs_isspace _citrus_bcs_isspace +#define _bcs_isupper _citrus_bcs_isupper +#define _bcs_isxdigit _citrus_bcs_isxdigit +#define _bcs_skip_nonws _citrus_bcs_skip_nonws +#define _bcs_skip_nonws_len _citrus_bcs_skip_nonws_len +#define _bcs_skip_ws _citrus_bcs_skip_ws +#define _bcs_skip_ws_len _citrus_bcs_skip_ws_len +#define _bcs_strcasecmp _citrus_bcs_strcasecmp +#define _bcs_strncasecmp _citrus_bcs_strncasecmp +#define _bcs_tolower _citrus_bcs_tolower +#define _bcs_toupper _citrus_bcs_toupper +#define _bcs_trunc_rws_len _citrus_bcs_trunc_rws_len +#define _bcs_convert_to_lower _citrus_bcs_convert_to_lower +#define _bcs_convert_to_upper _citrus_bcs_convert_to_upper +#define _bcs_strtol _citrus_bcs_strtol +#define _bcs_strtoul _citrus_bcs_strtoul +#endif /* _CITRUS_BCS_NO_NAMESPACE */ + +/* citrus_csmapper */ +#ifndef _CITRUS_CSMAPPER_NO_NAMESPACE +#define _csmapper _citrus_csmapper +#define _csmapper_open _citrus_csmapper_open +#define _csmapper_close _citrus_csmapper_close +#define _csmapper_convert _citrus_csmapper_convert +#define _csmapper_init_state _citrus_csmapper_init_state +#define _csmapper_get_state_size _citrus_csmapper_get_state_size +#define _csmapper_get_src_max _citrus_csmapper_get_src_max +#define _csmapper_get_dst_max _citrus_csmapper_get_dst_max +#define _CSMAPPER_F_PREVENT_PIVOT _CITRUS_CSMAPPER_F_PREVENT_PIVOT +#endif /* _CITRUS_CSMAPPER_NO_NAMESPACE */ + +/* citrus_db */ +#ifndef _CITRUS_DB_NO_NAMESPACE +#define _db_open _citrus_db_open +#define _db_close _citrus_db_close +#define _db_lookup _citrus_db_lookup +#define _db_lookup_by_s _citrus_db_lookup_by_string +#define _db_lookup8_by_s _citrus_db_lookup8_by_string +#define _db_lookup16_by_s _citrus_db_lookup16_by_string +#define _db_lookup32_by_s _citrus_db_lookup32_by_string +#define _db_lookupstr_by_s _citrus_db_lookup_string_by_string +#define _db_hash_std _citrus_db_hash_std +#define _db_get_num_entries _citrus_db_get_number_of_entries +#define _db_get_entry _citrus_db_get_entry +#define _db_locator _citrus_db_locator +#define _db_locator_init _citrus_db_locator_init +#endif /* _CITRUS_DB_NO_NAMESPACE */ + +/* citrus_db_factory */ +#ifndef _CITRUS_DB_FACTORY_NO_NAMESPACE +#define _db_factory _citrus_db_factory +#define _db_factory_create _citrus_db_factory_create +#define _db_factory_free _citrus_db_factory_free +#define _db_factory_add _citrus_db_factory_add +#define _db_factory_add_by_s _citrus_db_factory_add_by_string +#define _db_factory_add8_by_s _citrus_db_factory_add8_by_string +#define _db_factory_add16_by_s _citrus_db_factory_add16_by_string +#define _db_factory_add32_by_s _citrus_db_factory_add32_by_string +#define _db_factory_addstr_by_s _citrus_db_factory_add_string_by_string +#define _db_factory_calc_size _citrus_db_factory_calc_size +#define _db_factory_serialize _citrus_db_factory_serialize +#endif /* _CITRUS_DB_FACTORY_NO_NAMESPACE */ + +/* citrus_lookup */ +#ifndef _CITRUS_DB_NO_NAMESPACE +#define _LOOKUP_CASE_SENSITIVE _CITRUS_LOOKUP_CASE_SENSITIVE +#define _LOOKUP_CASE_IGNORE _CITRUS_LOOKUP_CASE_IGNORE +#define _lookup _citrus_lookup +#define _lookup_simple _citrus_lookup_simple +#define _lookup_alias _citrus_lookup_alias +#define _lookup_seq_open _citrus_lookup_seq_open +#define _lookup_seq_rewind _citrus_lookup_seq_rewind +#define _lookup_seq_next _citrus_lookup_seq_next +#define _lookup_seq_lookup _citrus_lookup_seq_lookup +#define _lookup_get_num_entries _citrus_lookup_get_number_of_entries +#define _lookup_seq_close _citrus_lookup_seq_close +#define _lookup_factory_convert _citrus_lookup_factory_convert +#endif /* _CITRUS_DB_NO_NAMESPACE */ + +/* citrus_esdb */ +#ifndef _CITRUS_ESDB_NO_NAMESPACE +#define _esdb _citrus_esdb +#define _esdb_charset _citrus_esdb_charset +#define _esdb_open _citrus_esdb_open +#define _esdb_close _citrus_esdb_close +#define _esdb_get_list _citrus_esdb_get_list +#define _esdb_free_list _citrus_esdb_free_list +#endif /* _CITRUS_ESDB_NO_NAMESPACE */ + +/* citrus_hash */ +#ifndef _CITRUS_HASH_NO_NAMESPACE +#define _citrus_string_hash_func _string_hash_func +#endif /* _CITRUS_HASH_NO_NAMESPACE */ + +/* citrus_mapper */ +#ifndef _CITRUS_MAPPER_NO_NAMESPACE +#define _mapper _citrus_mapper +#define _mapper_ops _citrus_mapper_ops +#define _mapper_traits _citrus_mapper_traits +#define _mapper_open _citrus_mapper_open +#define _mapper_open_direct _citrus_mapper_open_direct +#define _mapper_close _citrus_mapper_close +#define _MAPPER_CONVERT_SUCCESS _CITRUS_MAPPER_CONVERT_SUCCESS +#define _MAPPER_CONVERT_NONIDENTICAL _CITRUS_MAPPER_CONVERT_NONIDENTICAL +#define _MAPPER_CONVERT_SRC_MORE _CITRUS_MAPPER_CONVERT_SRC_MORE +#define _MAPPER_CONVERT_DST_MORE _CITRUS_MAPPER_CONVERT_DST_MORE +#define _MAPPER_CONVERT_ILSEQ _CITRUS_MAPPER_CONVERT_ILSEQ +#define _MAPPER_CONVERT_FATAL _CITRUS_MAPPER_CONVERT_FATAL +#define _mapper_convert _citrus_mapper_convert +#define _mapper_init_state _citrus_mapper_init_state +#define _mapper_get_state_size _citrus_mapper_get_state_size +#define _mapper_get_src_max _citrus_mapper_get_src_max +#define _mapper_get_dst_max _citrus_mapper_get_dst_max +#define _mapper_set_persistent _citrus_mapper_set_persistent +#endif /* _CITRUS_MAPPER_NO_NAMESPACE */ + +/* citrus_memstream */ +#ifndef _CITRUS_MEMSTREAM_NO_NAMESPACE +#define _memstream _citrus_memory_stream +#define _memstream_getln _citrus_memory_stream_getln +#define _memstream_matchline _citrus_memory_stream_matchline +#define _memstream_chr _citrus_memory_stream_chr +#define _memstream_skip_ws _citrus_memory_stream_skip_ws +#define _memstream_iseof _citrus_memory_stream_iseof +#define _memstream_bind _citrus_memory_stream_bind +#define _memstream_bind_ptr _citrus_memory_stream_bind_ptr +#define _memstream_seek _citrus_memory_stream_seek +#define _memstream_rewind _citrus_memory_stream_rewind +#define _memstream_tell _citrus_memory_stream_tell +#define _memstream_remainder _citrus_memory_stream_remainder +#define _memstream_getc _citrus_memory_stream_getc +#define _memstream_ungetc _citrus_memory_stream_ungetc +#define _memstream_peek _citrus_memory_stream_peek +#define _memstream_getregion _citrus_memory_stream_getregion +#define _memstream_getln_region _citrus_memory_stream_getln_region +#endif /* _CITRUS_MEMSTREAM_NO_NAMESPACE */ + +/* citrus_mmap */ +#ifndef _CITRUS_MMAP_NO_NAMESPACE +#define _map_file _citrus_map_file +#define _unmap_file _citrus_unmap_file +#endif /* _CITRUS_MMAP_NO_NAMESPACE */ + +#ifndef _CITRUS_PIVOT_NO_NAMESPACE +#define _pivot_factory_convert _citrus_pivot_factory_convert +#endif /* _CITRUS_PIVOT_NO_NAMESPACE */ + +/* citrus_region.h */ +#ifndef _CITRUS_REGION_NO_NAMESPACE +#define _region _citrus_region +#define _region_init _citrus_region_init +#define _region_head _citrus_region_head +#define _region_size _citrus_region_size +#define _region_check _citrus_region_check +#define _region_offset _citrus_region_offset +#define _region_peek8 _citrus_region_peek8 +#define _region_peek16 _citrus_region_peek16 +#define _region_peek32 _citrus_region_peek32 +#define _region_get_subregion _citrus_region_get_subregion +#endif /* _CITRUS_REGION_NO_NAMESPACE */ + +/* citrus_stdenc.h */ +#ifndef _CITRUS_STDENC_NO_NAMESPACE +#define _stdenc _citrus_stdenc +#define _stdenc_ops _citrus_stdenc_ops +#define _stdenc_traits _citrus_stdenc_traits +#define _stdenc_state_desc _citrus_stdenc_state_desc +#define _stdenc_open _citrus_stdenc_open +#define _stdenc_close _citrus_stdenc_close +#define _stdenc_init_state _citrus_stdenc_init_state +#define _stdenc_mbtocs _citrus_stdenc_mbtocs +#define _stdenc_cstomb _citrus_stdenc_cstomb +#define _stdenc_mbtowc _citrus_stdenc_mbtowc +#define _stdenc_wctomb _citrus_stdenc_wctomb +#define _stdenc_put_state_reset _citrus_stdenc_put_state_reset +#define _stdenc_get_state_size _citrus_stdenc_get_state_size +#define _stdenc_get_mb_cur_max _citrus_stdenc_get_mb_cur_max +#define _stdenc_get_state_desc _citrus_stdenc_get_state_desc +#define _STDENC_SDID_GENERIC _CITRUS_STDENC_SDID_GENERIC +#define _STDENC_SDGEN_UNKNOWN _CITRUS_STDENC_SDGEN_UNKNOWN +#define _STDENC_SDGEN_INITIAL _CITRUS_STDENC_SDGEN_INITIAL +#define _STDENC_SDGEN_STABLE _CITRUS_STDENC_SDGEN_STABLE +#define _STDENC_SDGEN_INCOMPLETE_CHAR _CITRUS_STDENC_SDGEN_INCOMPLETE_CHAR +#define _STDENC_SDGEN_INCOMPLETE_SHIFT _CITRUS_STDENC_SDGEN_INCOMPLETE_SHIFT +#endif /* _CITRUS_STDENC_NO_NAMESPACE */ + +/* citrus_types.h */ +#ifndef _CITRUS_TYPES_NO_NAMESPACE +#define _index_t _citrus_index_t +#define _csid_t _citrus_csid_t +#define _wc_t _citrus_wc_t +#endif /* _CITRUS_TYPES_NO_NAMESPACE */ + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_none.c b/lib/nbsd_libc/citrus/citrus_none.c new file mode 100644 index 000000000..047219732 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_none.c @@ -0,0 +1,503 @@ +/* $NetBSD: citrus_none.c,v 1.18 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_none.c,v 1.18 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_none.h" +#include "citrus_stdenc.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_CTYPE_DECLS(NONE); +_CITRUS_CTYPE_DEF_OPS(NONE); + + +/* ---------------------------------------------------------------------- */ + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_init(void ** __restrict cl, void * __restrict var, + size_t lenvar, size_t lenps) +{ + *cl = NULL; + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_NONE_ctype_uninit(void *cl) +{ +} + +static unsigned +/*ARGSUSED*/ +_citrus_NONE_ctype_get_mb_cur_max(void *cl) +{ + return (1); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mblen(void * __restrict cl, const char * __restrict s, + size_t n, int * __restrict nresult) +{ + if (!s) { + *nresult = 0; /* state independent */ + return (0); + } + if (n==0) { + *nresult = -1; + return (EILSEQ); + } + *nresult = (*s == 0) ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbrlen(void * __restrict cl, const char * __restrict s, + size_t n, void * __restrict pspriv, + size_t * __restrict nresult) +{ + if (!s) { + *nresult = 0; + return (0); + } + if (n==0) { + *nresult = (size_t)-2; + return (0); + } + *nresult = (*s == 0) ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbrtowc(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + if (s == NULL) { + *nresult = 0; + return (0); + } + if (n == 0) { + *nresult = (size_t)-2; + return (0); + } + + if (pwc != NULL) + *pwc = (wchar_t)(unsigned char) *s; + + *nresult = *s == '\0' ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbsinit(void * __restrict cl, + const void * __restrict pspriv, + int * __restrict nresult) +{ + *nresult = 1; /* always initial state */ + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbsrtowcs(void * __restrict cl, wchar_t * __restrict pwcs, + const char ** __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + int cnt; + const char *s0; + + /* if pwcs is NULL, ignore n */ + if (pwcs == NULL) + n = 1; /* arbitrary >0 value */ + + cnt = 0; + s0 = *s; /* to keep *s unchanged for now, use copy instead. */ + while (n > 0) { + if (pwcs != NULL) { + *pwcs = (wchar_t)(unsigned char)*s0; + } + if (*s0 == '\0') { + s0 = NULL; + break; + } + s0++; + if (pwcs != NULL) { + pwcs++; + n--; + } + cnt++; + } + if (pwcs) + *s = s0; + + *nresult = (size_t)cnt; + + return (0); +} + +static int +_citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs, + const char * __restrict s, size_t n, + size_t * __restrict nresult) +{ + const char *rs = s; + + return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, &rs, n, NULL, nresult)); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbtowc(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + int * __restrict nresult) +{ + + if (s == NULL) { + *nresult = 0; /* state independent */ + return (0); + } + if (n == 0) { + return (EILSEQ); + } + if (pwc == NULL) { + if (*s == '\0') { + *nresult = 0; + } else { + *nresult = 1; + } + return (0); + } + + *pwc = (wchar_t)(unsigned char)*s; + *nresult = *s == '\0' ? 0 : 1; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_wcrtomb(void * __restrict cl, char * __restrict s, + wchar_t wc, void * __restrict pspriv, + size_t * __restrict nresult) +{ + if ((wc&~0xFFU) != 0) { + *nresult = (size_t)-1; + return (EILSEQ); + } + + *nresult = 1; + if (s!=NULL) + *s = (char)wc; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_wcsrtombs(void * __restrict cl, char * __restrict s, + const wchar_t ** __restrict pwcs, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + size_t count; + const wchar_t *pwcs0; + + pwcs0 = *pwcs; + count = 0; + + if (s == NULL) + n = 1; + + while (n > 0) { + if ((*pwcs0 & ~0xFFU) != 0) { + *nresult = (size_t)-1; + return (EILSEQ); + } + if (s != NULL) { + *s++ = (char)*pwcs0; + n--; + } + if (*pwcs0 == L'\0') { + pwcs0 = NULL; + break; + } + count++; + pwcs0++; + } + if (s != NULL) + *pwcs = pwcs0; + + *nresult = count; + + return (0); +} + +static int +_citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s, + const wchar_t * __restrict pwcs, size_t n, + size_t * __restrict nresult) +{ + const wchar_t *rpwcs = pwcs; + + return (_citrus_NONE_ctype_wcsrtombs(cl, s, &rpwcs, n, NULL, nresult)); +} + +static int +_citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s, + wchar_t wc, int * __restrict nresult) +{ + int ret; + size_t nr; + + if (s == 0) { + /* + * initialize state here. + * (nothing to do for us.) + */ + *nresult = 0; /* we're state independent */ + return (0); + } + + ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr); + *nresult = (int)nr; + + return (ret); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_btowc(_citrus_ctype_rec_t * __restrict cc, + int c, wint_t * __restrict wcresult) +{ + if (c == EOF || c & ~0xFF) + *wcresult = WEOF; + else + *wcresult = (wint_t)c; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_wctob(_citrus_ctype_rec_t * __restrict cc, + wint_t wc, int * __restrict cresult) +{ + if (wc == WEOF || wc & ~0xFF) + *cresult = EOF; + else + *cresult = (int)wc; + return (0); +} + +/* ---------------------------------------------------------------------- */ + +_CITRUS_STDENC_DECLS(NONE); +_CITRUS_STDENC_DEF_OPS(NONE); +struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits = { + 0, /* et_state_size */ + 1, /* mb_cur_max */ +}; + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_init(struct _citrus_stdenc * __restrict ce, + const void *var, size_t lenvar, + struct _citrus_stdenc_traits * __restrict et) +{ + + et->et_state_size = 0; + et->et_mb_cur_max = 1; + + ce->ce_closure = NULL; + + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_NONE_stdenc_uninit(struct _citrus_stdenc *ce) +{ +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_init_state(struct _citrus_stdenc * __restrict ce, + void * __restrict ps) +{ + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce, + _csid_t *csid, _index_t *idx, + const char **s, size_t n, + void *ps, size_t *nresult) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + if (n<1) { + *nresult = (size_t)-2; + return (0); + } + + *csid = 0; + *idx = (_index_t)(unsigned char)*(*s)++; + *nresult = *idx == 0 ? 0 : 1; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * __restrict ce, + char *s, size_t n, + _csid_t csid, _index_t idx, + void *ps, size_t *nresult) +{ + + if (csid == _CITRUS_CSID_INVALID) { + *nresult = 0; + return (0); + } + if (n<1) { + *nresult = (size_t)-1; + return (E2BIG); + } + if (csid != 0 || (idx&0xFF) != idx) + return (EILSEQ); + + *s = (char)idx; + *nresult = 1; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce, + _wc_t * __restrict pwc, + const char ** __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + if (s == NULL) { + *nresult = 0; + return (0); + } + if (n == 0) { + *nresult = (size_t)-2; + return (0); + } + + if (pwc != NULL) + *pwc = (_wc_t)(unsigned char) **s; + + *nresult = *s == '\0' ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + _wc_t wc, void * __restrict pspriv, + size_t * __restrict nresult) +{ + if ((wc&~0xFFU) != 0) { + *nresult = (size_t)-1; + return (EILSEQ); + } + if (n==0) { + *nresult = (size_t)-1; + return (E2BIG); + } + + *nresult = 1; + if (s!=NULL && n>0) + *s = (char)wc; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + + *nresult = 0; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_get_state_desc(struct _stdenc * __restrict ce, + void * __restrict ps, + int id, + struct _stdenc_state_desc * __restrict d) +{ + int ret = 0; + + switch (id) { + case _STDENC_SDID_GENERIC: + d->u.generic.state = _STDENC_SDGEN_INITIAL; + break; + default: + ret = EOPNOTSUPP; + } + + return ret; +} diff --git a/lib/nbsd_libc/citrus/citrus_none.h b/lib/nbsd_libc/citrus/citrus_none.h new file mode 100644 index 000000000..1a75f2c0f --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_none.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_none.h,v 1.3 2003/06/25 09:51:38 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_NONE_H_ +#define _CITRUS_NONE_H_ + +extern struct _citrus_ctype_ops_rec _citrus_NONE_ctype_ops; +extern struct _citrus_stdenc_ops _citrus_NONE_stdenc_ops; +extern struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits; + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_pivot_factory.c b/lib/nbsd_libc/citrus/citrus_pivot_factory.c new file mode 100644 index 000000000..54369caf6 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_pivot_factory.c @@ -0,0 +1,231 @@ +/* $NetBSD: citrus_pivot_factory.c,v 1.7 2009/04/12 14:20:19 lukem Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_pivot_factory.c,v 1.7 2009/04/12 14:20:19 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_bcs.h" +#include "citrus_db_factory.h" +#include "citrus_db_hash.h" +#include "citrus_pivot_file.h" +#include "citrus_pivot_factory.h" + +struct src_entry { + char *se_name; + struct _citrus_db_factory *se_df; + SIMPLEQ_ENTRY(src_entry) se_entry; +}; +SIMPLEQ_HEAD(src_head, src_entry); + +static int +find_src(struct src_head *sh, struct src_entry **rse, const char *name) +{ + int ret; + struct src_entry *se; + + SIMPLEQ_FOREACH(se, sh, se_entry) { + if (_bcs_strcasecmp(se->se_name, name) == 0) { + *rse = se; + return 0; + } + } + se = malloc(sizeof(*se)); + if (se == NULL) + return errno; + se->se_name = strdup(name); + if (se->se_name == NULL) { + ret = errno; + free(se); + return ret; + } + ret = _db_factory_create(&se->se_df, &_db_hash_std, NULL); + if (ret) { + free(se->se_name); + free(se); + return ret; + } + SIMPLEQ_INSERT_TAIL(sh, se, se_entry); + *rse = se; + + return 0; +} + +static void +free_src(struct src_head *sh) +{ + struct src_entry *se; + + while ((se = SIMPLEQ_FIRST(sh)) != NULL) { + SIMPLEQ_REMOVE_HEAD(sh, se_entry); + _db_factory_free(se->se_df); + free(se->se_name); + free(se); + } +} + + +#define T_COMM '#' +static int +convert_line(struct src_head *sh, const char *line, size_t len) +{ + int ret; + struct src_entry *se; + const char *p; + char key1[LINE_MAX], key2[LINE_MAX], data[LINE_MAX]; + char *ep; + uint32_t val; + + se = NULL; /* XXX gcc */ + + /* cut off trailing comment */ + p = memchr(line, T_COMM, len); + if (p) + len = p - line; + + /* key1 */ + line = _bcs_skip_ws_len(line, &len); + if (len == 0) + return 0; + p = _bcs_skip_nonws_len(line, &len); + if (p==line) + return 0; + snprintf(key1, sizeof(key1), "%.*s", (int)(p-line), line); + + /* key2 */ + line = _bcs_skip_ws_len(p, &len); + if (len == 0) + return 0; + p = _bcs_skip_nonws_len(line, &len); + if (p==line) + return 0; + snprintf(key2, sizeof(key2), "%.*s", (int)(p-line), line); + + /* data */ + line = _bcs_skip_ws_len(p, &len); + _bcs_trunc_rws_len(line, &len); + snprintf(data, sizeof(data), "%.*s", (int)len, line); + val = strtoul(data, &ep, 0); + if (*ep != '\0') + return EFTYPE; + + /* insert to DB */ + ret = find_src(sh, &se, key1); + if (ret) + return ret; + + return _db_factory_add32_by_s(se->se_df, key2, val); +} + +static int +dump_db(struct src_head *sh, struct _region *r) +{ + int ret; + struct _db_factory *df; + struct src_entry *se; + size_t size; + void *ptr; + struct _region subr; + + ret = _db_factory_create(&df, &_db_hash_std, NULL); + if (ret) + return ret; + + SIMPLEQ_FOREACH(se, sh, se_entry) { + size = _db_factory_calc_size(se->se_df); + ptr = malloc(size); + if (ptr == NULL) + goto quit; + _region_init(&subr, ptr, size); + ret = _db_factory_serialize(se->se_df, _CITRUS_PIVOT_SUB_MAGIC, + &subr); + if (ret) + goto quit; + ret = _db_factory_add_by_s(df, se->se_name, &subr, 1); + if (ret) + goto quit; + } + + size = _db_factory_calc_size(df); + ptr = malloc(size); + if (ptr == NULL) + goto quit; + _region_init(r, ptr, size); + + ret = _db_factory_serialize(df, _CITRUS_PIVOT_MAGIC, r); + ptr = NULL; + +quit: + free(ptr); + _db_factory_free(df); + return ret; +} + +int +_citrus_pivot_factory_convert(FILE *out, FILE *in) +{ + struct src_head sh; + struct _region r; + char *line; + size_t size; + int ret; + + SIMPLEQ_INIT(&sh); + + while ((line = fgetln(in, &size)) != NULL) + if ((ret = convert_line(&sh, line, size))) { + free_src(&sh); + return ret; + } + + ret = dump_db(&sh, &r); + free_src(&sh); + if (ret) + return ret; + + if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1) + return errno; + + return 0; +} diff --git a/lib/nbsd_libc/citrus/citrus_pivot_factory.h b/lib/nbsd_libc/citrus/citrus_pivot_factory.h new file mode 100644 index 000000000..dd5d80488 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_pivot_factory.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_pivot_factory.h,v 1.1 2003/06/25 09:51:39 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_PIVOT_FACTORY_H_ +#define _CITRUS_PIVOT_FACTORY_H_ + +__BEGIN_DECLS +int _citrus_pivot_factory_convert(FILE *, FILE *); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_pivot_file.h b/lib/nbsd_libc/citrus/citrus_pivot_file.h new file mode 100644 index 000000000..eb50645a1 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_pivot_file.h @@ -0,0 +1,35 @@ +/* $NetBSD: citrus_pivot_file.h,v 1.1 2003/06/25 09:51:39 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_PIVOT_FILE_H_ +#define _CITRUS_PIVOT_FILE_H_ + +#define _CITRUS_PIVOT_MAGIC "CSPIVOT\0" +#define _CITRUS_PIVOT_SUB_MAGIC "CSPIVSUB" + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_prop.c b/lib/nbsd_libc/citrus/citrus_prop.c new file mode 100644 index 000000000..0b23628b1 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_prop.c @@ -0,0 +1,452 @@ +/* $NetBSD: citrus_prop.c,v 1.3 2006/11/22 23:47:21 tnozaki Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_prop.c,v 1.3 2006/11/22 23:47:21 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_prop.h" + +typedef struct { + _citrus_prop_type_t type; + union { + const char *str; + int bool, chr; + uint64_t num; + } u; +} _citrus_prop_object_t; + +static __inline void +_citrus_prop_object_init(_citrus_prop_object_t *obj, _citrus_prop_type_t type) +{ + _DIAGASSERT(obj != NULL); + + obj->type = type; + memset(&obj->u, 0, sizeof(obj->u)); +} + +static __inline void +_citrus_prop_object_uninit(_citrus_prop_object_t *obj) +{ + _DIAGASSERT(obj != NULL); + + if (obj->type == _CITRUS_PROP_STR) + free(__UNCONST(obj->u.str)); +} + +static const char *xdigit = "0123456789ABCDEF"; + +#define _CITRUS_PROP_READ_UINT_COMMON(_func_, _type_, _max_) \ +static int \ +_citrus_prop_read_##_func_##_common(struct _memstream * __restrict ms, \ + _type_ * __restrict result, int base) \ +{ \ + _type_ acc, cutoff; \ + int n, ch, cutlim; \ + char *p; \ + \ + _DIAGASSERT(ms != NULL); \ + _DIAGASSERT(result != NULL); \ + \ + acc = (_type_)0; \ + cutoff = _max_ / base; \ + cutlim = _max_ % base; \ + for (;;) { \ + ch = _memstream_getc(ms); \ + p = strchr(xdigit, _bcs_toupper(ch)); \ + if (p == NULL || (n = (p - xdigit)) >= base) \ + break; \ + if (acc > cutoff || (acc == cutoff && n > cutlim)) \ + break; \ + acc *= base; \ + acc += n; \ + } \ + _memstream_ungetc(ms, ch); \ + *result = acc; \ + return 0; \ +} +_CITRUS_PROP_READ_UINT_COMMON(chr, int, UCHAR_MAX) +_CITRUS_PROP_READ_UINT_COMMON(num, uint64_t, UINT64_MAX) +#undef _CITRUS_PROP_READ_UINT_COMMON + +#define _CITRUS_PROP_READ_INT(_func_, _type_) \ +static int \ +_citrus_prop_read_##_func_(struct _memstream * __restrict ms, \ + _citrus_prop_object_t * __restrict obj) \ +{ \ + int ch, neg, base; \ + \ + _DIAGASSERT(ms != NULL); \ + _DIAGASSERT(obj != NULL); \ + \ + _memstream_skip_ws(ms); \ + ch = _memstream_getc(ms); \ + neg = 0; \ + switch (ch) { \ + case '-': \ + neg = 1; \ + case '+': \ + ch = _memstream_getc(ms); \ + } \ + base = 10; \ + if (ch == '0') { \ + base -= 2; \ + ch = _memstream_getc(ms); \ + if (ch == 'x' || ch == 'X') { \ + ch = _memstream_getc(ms); \ + if (_bcs_isxdigit(ch) == 0) { \ + _memstream_ungetc(ms, ch); \ + obj->u._func_ = 0; \ + return 0; \ + } \ + base += 8; \ + } \ + } else if (_bcs_isdigit(ch) == 0) \ + return EINVAL; \ + _memstream_ungetc(ms, ch); \ + return _citrus_prop_read_##_func_##_common \ + (ms, &obj->u._func_, base); \ +} +_CITRUS_PROP_READ_INT(chr, int) +_CITRUS_PROP_READ_INT(num, uint64_t) +#undef _CITRUS_PROP_READ_INT + +static int +_citrus_prop_read_character_common(struct _memstream * __restrict ms, + int * __restrict result) +{ + int ch, base; + + _DIAGASSERT(ms != NULL); + _DIAGASSERT(result != NULL); + + ch = _memstream_getc(ms); + if (ch != '\\') { + *result = ch; + } else { + ch = _memstream_getc(ms); + base = 16; + switch (ch) { + case 'a': *result = '\a'; break; + case 'b': *result = '\b'; break; + case 'f': *result = '\f'; break; + case 'n': *result = '\n'; break; + case 'r': *result = '\r'; break; + case 't': *result = '\t'; break; + case 'v': *result = '\v'; break; + /*FALLTHROUGH*/ + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + _memstream_ungetc(ms, ch); + base -= 8; + case 'x': + return _citrus_prop_read_chr_common(ms, result, base); + + default: + /* unknown escape */ + *result = ch; + } + } + return 0; +} + +static int +_citrus_prop_read_character(struct _memstream * __restrict ms, + _citrus_prop_object_t * __restrict obj) +{ + int ch, errnum; + + _DIAGASSERT(ms != NULL); + _DIAGASSERT(obj != NULL); + + _memstream_skip_ws(ms); + ch = _memstream_getc(ms); + if (ch != '\'') { + _memstream_ungetc(ms, ch); + return _citrus_prop_read_chr(ms, obj); + } + errnum = _citrus_prop_read_character_common(ms, &ch); + if (errnum != 0) + return errnum; + obj->u.chr = ch; + ch = _memstream_getc(ms); + if (ch != '\'') + return EINVAL; + return 0; +} + +static int +_citrus_prop_read_bool(struct _memstream * __restrict ms, + _citrus_prop_object_t * __restrict obj) +{ + _DIAGASSERT(ms != NULL); + _DIAGASSERT(obj != NULL); + + _memstream_skip_ws(ms); + switch (_bcs_tolower(_memstream_getc(ms))) { + case 't': + if (_bcs_tolower(_memstream_getc(ms)) == 'r' && + _bcs_tolower(_memstream_getc(ms)) == 'u' && + _bcs_tolower(_memstream_getc(ms)) == 'e') { + obj->u.bool = 1; + return 0; + } + break; + case 'f': + if (_bcs_tolower(_memstream_getc(ms)) == 'a' && + _bcs_tolower(_memstream_getc(ms)) == 'l' && + _bcs_tolower(_memstream_getc(ms)) == 's' && + _bcs_tolower(_memstream_getc(ms)) == 'e') { + obj->u.bool = 0; + return 0; + } + } + return EINVAL; +} + +static int +_citrus_prop_read_str(struct _memstream * __restrict ms, + _citrus_prop_object_t * __restrict obj) +{ + int errnum, quot, ch; + char *s, *t; +#define _CITRUS_PROP_STR_BUFSIZ 512 + size_t n, m; + + _DIAGASSERT(ms != NULL); + _DIAGASSERT(obj != NULL); + + m = _CITRUS_PROP_STR_BUFSIZ; + s = malloc(m); + if (s == NULL) + return ENOMEM; + n = 0; + _memstream_skip_ws(ms); + quot = _memstream_getc(ms); + switch (quot) { + case EOF: + goto done; + case '\\': + _memstream_ungetc(ms, quot); + quot = EOF; + /*FALLTHROUGH*/ + case '\"': case '\'': + break; + default: + s[n] = quot; + ++n, --m; + quot = EOF; + } + for (;;) { + if (m < 1) { + m = _CITRUS_PROP_STR_BUFSIZ; + t = realloc(s, n + m); + if (t == NULL) { + free(s); + return ENOMEM; + } + s = t; + } + ch = _memstream_getc(ms); + if (quot == ch || (quot == EOF && + (ch == ';' || _bcs_isspace(ch)))) { +done: + s[n] = '\0'; + obj->u.str = (const char *)s; + return 0; + } + _memstream_ungetc(ms, ch); + errnum = _citrus_prop_read_character_common(ms, &ch); + if (errnum != 0) + return errnum; + s[n] = ch; + ++n, --m; + } + free(s); + return EINVAL; +#undef _CITRUS_PROP_STR_BUFSIZ +} + +typedef int (*_citrus_prop_read_type_t)(struct _memstream * __restrict, + _citrus_prop_object_t * __restrict); + +static const _citrus_prop_read_type_t readers[] = { + _citrus_prop_read_bool, + _citrus_prop_read_str, + _citrus_prop_read_character, + _citrus_prop_read_num, +}; + +static __inline int +_citrus_prop_read_symbol(struct _memstream * __restrict ms, + char * __restrict s, size_t n) +{ + int ch; + size_t m; + + _DIAGASSERT(ms != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(n > 0); + + for (m = 0; m < n; ++m) { + ch = _memstream_getc(ms); + if (ch != '_' && _bcs_isalnum(ch) == 0) + goto name_found; + s[m] = ch; + } + ch = _memstream_getc(ms); + if (ch == '_' || _bcs_isalnum(ch) != 0) + return EINVAL; + +name_found: + _memstream_ungetc(ms, ch); + s[m] = '\0'; + + return 0; +} + +static int +_citrus_prop_parse_element(struct _memstream * __restrict ms, + const _citrus_prop_hint_t * __restrict hints, + void ** __restrict context) +{ + int ch, errnum; +#define _CITRUS_PROP_HINT_NAME_LEN_MAX 255 + char name[_CITRUS_PROP_HINT_NAME_LEN_MAX + 1]; + const _citrus_prop_hint_t *hint; + _citrus_prop_object_t ostart, oend; + + _DIAGASSERT(ms != NULL); + _DIAGASSERT(hints != NULL); + + errnum = _citrus_prop_read_symbol(ms, name, sizeof(name)); + if (errnum != 0) + return errnum; + for (hint = hints; hint->name != NULL; ++hint) { + if (_citrus_bcs_strcasecmp(name, hint->name) == 0) + goto hint_found; + } + return EINVAL; + +hint_found: + _memstream_skip_ws(ms); + ch = _memstream_getc(ms); + if (ch != '=' && ch != ':') + _memstream_ungetc(ms, ch); + do { + _citrus_prop_object_init(&ostart, hint->type); + _citrus_prop_object_init(&oend, hint->type); + errnum = (*readers[hint->type])(ms, &ostart); + if (errnum != 0) + return errnum; + _memstream_skip_ws(ms); + ch = _memstream_getc(ms); + switch (hint->type) { + case _CITRUS_PROP_BOOL: + case _CITRUS_PROP_STR: + break; + default: + if (ch != '-') + break; + errnum = (*readers[hint->type])(ms, &oend); + if (errnum != 0) + return errnum; + _memstream_skip_ws(ms); + ch = _memstream_getc(ms); + } +#define CALL0(_func_) \ +do { \ + _DIAGASSERT(hint->cb._func_.func != NULL); \ + errnum = (*hint->cb._func_.func)(context, \ + hint->name, ostart.u._func_); \ +} while (/*CONSTCOND*/0) +#define CALL1(_func_) \ +do { \ + _DIAGASSERT(hint->cb._func_.func != NULL); \ + errnum = (*hint->cb._func_.func)(context, \ + hint->name, ostart.u._func_, oend.u._func_);\ +} while (/*CONSTCOND*/0) + switch (hint->type) { + case _CITRUS_PROP_BOOL: CALL0(bool); break; + case _CITRUS_PROP_STR : CALL0( str); break; + case _CITRUS_PROP_CHR : CALL1( chr); break; + case _CITRUS_PROP_NUM : CALL1( num); break; + default: + abort(); + /*NOTREACHED*/ + } +#undef CALL0 +#undef CALL1 + _citrus_prop_object_uninit(&ostart); + _citrus_prop_object_uninit(&oend); + if (errnum != 0) + return errnum; + } while (ch == ','); + if (ch != ';') + _memstream_ungetc(ms, ch); + return 0; +} + +int +_citrus_prop_parse_variable(const _citrus_prop_hint_t * __restrict hints, + void * __restrict context, const void *var, size_t lenvar) +{ + struct _memstream ms; + int errnum, ch; + + _DIAGASSERT(hints != NULL); + + _memstream_bind_ptr(&ms, __UNCONST(var), lenvar); + for (;;) { + _memstream_skip_ws(&ms); + ch = _memstream_getc(&ms); + if (ch == EOF || ch == '\0') + break; + _memstream_ungetc(&ms, ch); + errnum = _citrus_prop_parse_element( + &ms, hints, (void **)&context); + if (errnum != 0) + return errnum; + } + return 0; +} diff --git a/lib/nbsd_libc/citrus/citrus_prop.h b/lib/nbsd_libc/citrus/citrus_prop.h new file mode 100644 index 000000000..86d3ed179 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_prop.h @@ -0,0 +1,91 @@ +/* $NetBSD: citrus_prop.h,v 1.3 2006/11/23 13:59:03 tnozaki Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_PROP_H_ +#define _CITRUS_PROP_H_ + +typedef enum { + _CITRUS_PROP_BOOL = 0, + _CITRUS_PROP_STR = 1, + _CITRUS_PROP_CHR = 2, + _CITRUS_PROP_NUM = 3, +} _citrus_prop_type_t; + +typedef struct _citrus_prop_hint_t _citrus_prop_hint_t; + +#define _CITRUS_PROP_CB0_T(_func_, _type_) \ +typedef int (*_citrus_prop_##_func_##_cb_func_t) \ + (void ** __restrict, const char *, _type_); \ +typedef struct { \ + _citrus_prop_##_func_##_cb_func_t func; \ +} _citrus_prop_##_func_##_cb_t; +_CITRUS_PROP_CB0_T(bool, int) +_CITRUS_PROP_CB0_T(str, const char *) +#undef _CITRUS_PROP_CB0_T + +#define _CITRUS_PROP_CB1_T(_func_, _type_) \ +typedef int (*_citrus_prop_##_func_##_cb_func_t) \ + (void ** __restrict, const char *, _type_, _type_); \ +typedef struct { \ + _citrus_prop_##_func_##_cb_func_t func; \ +} _citrus_prop_##_func_##_cb_t; +_CITRUS_PROP_CB1_T(chr, int) +_CITRUS_PROP_CB1_T(num, uint64_t) +#undef _CITRUS_PROP_CB1_T + +struct _citrus_prop_hint_t { + const char *name; + _citrus_prop_type_t type; +#define _CITRUS_PROP_CB_T_OPS(_name_) \ + _citrus_prop_##_name_##_cb_t _name_ + union { + _CITRUS_PROP_CB_T_OPS(bool); + _CITRUS_PROP_CB_T_OPS(str); + _CITRUS_PROP_CB_T_OPS(chr); + _CITRUS_PROP_CB_T_OPS(num); + } cb; +}; + +#define _CITRUS_PROP_HINT_BOOL(name, cb) \ + { name, _CITRUS_PROP_BOOL, { .bool = { cb } } } +#define _CITRUS_PROP_HINT_STR(name, cb) \ + { name, _CITRUS_PROP_STR, { .str = { cb } } } +#define _CITRUS_PROP_HINT_CHR(name, cb) \ + { name, _CITRUS_PROP_CHR, { .chr = { cb } } } +#define _CITRUS_PROP_HINT_NUM(name, cb) \ + { name, _CITRUS_PROP_NUM, { .num = { cb } } } +#define _CITRUS_PROP_HINT_END \ + { NULL } + +__BEGIN_DECLS +int _citrus_prop_parse_variable(const _citrus_prop_hint_t * __restrict, + void * __restrict, const void *, size_t); +__END_DECLS + +#endif /* !_CITRUS_PROP_H_ */ diff --git a/lib/nbsd_libc/citrus/citrus_region.h b/lib/nbsd_libc/citrus/citrus_region.h new file mode 100644 index 000000000..18c9db72b --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_region.h @@ -0,0 +1,108 @@ +/* $NetBSD: citrus_region.h,v 1.7 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_REGION_H_ +#define _CITRUS_REGION_H_ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +struct _citrus_region { +/* private: */ + void *r_head; + size_t r_size; +}; + +static __inline void +_citrus_region_init(struct _citrus_region *r, void *h, size_t sz) +{ + _DIAGASSERT(r); + r->r_head = h; + r->r_size = sz; +} + +static __inline void * +_citrus_region_head(const struct _citrus_region *r) +{ + return r->r_head; +} + +static __inline size_t +_citrus_region_size(const struct _citrus_region *r) +{ + return r->r_size; +} + +static __inline int +_citrus_region_check(const struct _citrus_region *r, size_t ofs, size_t sz) +{ + return r->r_size >= ofs + sz ? 0 : -1; +} + +static __inline void * +_citrus_region_offset(const struct _citrus_region *r, size_t pos) +{ + return (void *)((uint8_t *)r->r_head + pos); +} + +static __inline uint8_t +_citrus_region_peek8(const struct _citrus_region *r, size_t pos) +{ + return *(uint8_t *)_citrus_region_offset(r, pos); +} + +static __inline uint16_t +_citrus_region_peek16(const struct _citrus_region *r, size_t pos) +{ + uint16_t val; + memcpy(&val, _citrus_region_offset(r, pos), (size_t)2); + return val; +} + +static __inline uint32_t +_citrus_region_peek32(const struct _citrus_region *r, size_t pos) +{ + uint32_t val; + memcpy(&val, _citrus_region_offset(r, pos), (size_t)4); + return val; +} + +static __inline int +_citrus_region_get_subregion(struct _citrus_region *subr, + const struct _citrus_region *r, + size_t ofs, size_t sz) +{ + if (_citrus_region_check(r, ofs, sz)) + return -1; + _citrus_region_init(subr, _citrus_region_offset(r, ofs), sz); + return 0; +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_stdenc.c b/lib/nbsd_libc/citrus/citrus_stdenc.c new file mode 100644 index 000000000..d78407569 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_stdenc.c @@ -0,0 +1,197 @@ +/* $NetBSD: citrus_stdenc.c,v 1.3 2005/10/29 18:02:04 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_stdenc.c,v 1.3 2005/10/29 18:02:04 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_stdenc.h" +#include "citrus_none.h" + +struct _citrus_stdenc _citrus_stdenc_default = { + &_citrus_NONE_stdenc_ops, /* ce_ops */ + NULL, /* ce_closure */ + NULL, /* ce_module */ + &_citrus_NONE_stdenc_traits, /* ce_traits */ +}; + +#ifdef _I18N_DYNAMIC + +static int +/*ARGSUSED*/ +get_state_desc_default(struct _citrus_stdenc * __restrict ce, + void * __restrict ps, + int id, + struct _citrus_stdenc_state_desc * __restrict d) +{ + return EOPNOTSUPP; +} + +int +_citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce, + char const * __restrict encname, + const void * __restrict variable, size_t lenvar) +{ + int ret; + _citrus_module_t handle; + struct _citrus_stdenc *ce; + _citrus_stdenc_getops_t getops; + + _DIAGASSERT(encname != NULL); + _DIAGASSERT(!lenvar || variable!=NULL); + _DIAGASSERT(rce != NULL); + + if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) { + *rce = &_citrus_stdenc_default; + return (0); + } + ce = malloc(sizeof(*ce)); + if (ce==NULL) { + ret = errno; + goto bad; + } + ce->ce_ops = NULL; + ce->ce_closure = NULL; + ce->ce_module = NULL; + ce->ce_traits = NULL; + + ret = _citrus_load_module(&handle, encname); + if (ret) + goto bad; + + ce->ce_module = handle; + + getops = + (_citrus_stdenc_getops_t)_citrus_find_getops(ce->ce_module, + encname, "stdenc"); + if (getops == NULL) { + ret = EINVAL; + goto bad; + } + + ce->ce_ops = (struct _citrus_stdenc_ops *)malloc(sizeof(*ce->ce_ops)); + if (ce->ce_ops == NULL) { + ret = errno; + goto bad; + } + + ret = (*getops)(ce->ce_ops, sizeof(*ce->ce_ops), + _CITRUS_STDENC_ABI_VERSION); + if (ret) + goto bad; + + /* If return ABI version is not expected, should fixup it */ + if (ce->ce_ops->eo_abi_version < 0x00000002) { + ce->ce_ops->eo_get_state_desc = &get_state_desc_default; + } + + /* validation check */ + if (ce->ce_ops->eo_init == NULL || + ce->ce_ops->eo_uninit == NULL || + ce->ce_ops->eo_init_state == NULL || + ce->ce_ops->eo_mbtocs == NULL || + ce->ce_ops->eo_cstomb == NULL || + ce->ce_ops->eo_mbtowc == NULL || + ce->ce_ops->eo_wctomb == NULL || + ce->ce_ops->eo_get_state_desc == NULL) + goto bad; + + /* allocate traits */ + ce->ce_traits = malloc(sizeof(*ce->ce_traits)); + if (ce->ce_traits == NULL) { + ret = errno; + goto bad; + } + /* init and get closure */ + ret = (*ce->ce_ops->eo_init)(ce, variable, lenvar, ce->ce_traits); + if (ret) + goto bad; + + *rce = ce; + + return (0); + +bad: + _citrus_stdenc_close(ce); + return (ret); +} + +void +_citrus_stdenc_close(struct _citrus_stdenc *ce) +{ + + _DIAGASSERT(ce != NULL); + + if (ce == &_citrus_stdenc_default) + return; + + if (ce->ce_module) { + if (ce->ce_ops) { + if (ce->ce_closure && ce->ce_ops->eo_uninit) + (*ce->ce_ops->eo_uninit)(ce); + free(ce->ce_ops); + } + free(ce->ce_traits); + _citrus_unload_module(ce->ce_module); + } + free(ce); +} + +#else +/* !_I18N_DYNAMIC */ + +int +/*ARGSUSED*/ +_citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce, + char const * __restrict encname, + const void * __restrict variable, size_t lenvar) +{ + if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) { + *rce = &_citrus_stdenc_default; + return (0); + } + return (EINVAL); +} + +void +/*ARGSUSED*/ +_citrus_stdenc_close(struct _citrus_stdenc *ce) +{ +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_stdenc.h b/lib/nbsd_libc/citrus/citrus_stdenc.h new file mode 100644 index 000000000..ce198db68 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_stdenc.h @@ -0,0 +1,145 @@ +/* $NetBSD: citrus_stdenc.h,v 1.4 2005/10/29 18:02:04 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_STDENC_H_ +#define _CITRUS_STDENC_H_ + +struct _citrus_stdenc; +struct _citrus_stdenc_ops; +struct _citrus_stdenc_traits; + +#define _CITRUS_STDENC_SDID_GENERIC 0 +struct _citrus_stdenc_state_desc +{ + union { + struct { + int state; +#define _CITRUS_STDENC_SDGEN_UNKNOWN 0 +#define _CITRUS_STDENC_SDGEN_INITIAL 1 +#define _CITRUS_STDENC_SDGEN_STABLE 2 +#define _CITRUS_STDENC_SDGEN_INCOMPLETE_CHAR 3 +#define _CITRUS_STDENC_SDGEN_INCOMPLETE_SHIFT 4 + } generic; + } u; +}; + +#include "citrus_stdenc_local.h" + +__BEGIN_DECLS +int _citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict, + char const * __restrict, + const void * __restrict, size_t); +void _citrus_stdenc_close(struct _citrus_stdenc *); +__END_DECLS + +static __inline int +_citrus_stdenc_init_state(struct _citrus_stdenc * __restrict ce, + void * __restrict ps) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_init_state); + return (*ce->ce_ops->eo_init_state)(ce, ps); +} + +static __inline int +_citrus_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce, + _citrus_csid_t * __restrict csid, + _citrus_index_t * __restrict idx, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_mbtocs); + return (*ce->ce_ops->eo_mbtocs)(ce, csid, idx, s, n, ps, nresult); +} + +static __inline int +_citrus_stdenc_cstomb(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + _citrus_csid_t csid, _citrus_index_t idx, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_cstomb); + return (*ce->ce_ops->eo_cstomb)(ce, s, n, csid, idx, ps, nresult); +} + +static __inline int +_citrus_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce, + _citrus_wc_t * __restrict wc, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_mbtocs); + return (*ce->ce_ops->eo_mbtowc)(ce, wc, s, n, ps, nresult); +} + +static __inline int +_citrus_stdenc_wctomb(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, _citrus_wc_t wc, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_cstomb); + return (*ce->ce_ops->eo_wctomb)(ce, s, n, wc, ps, nresult); +} + +static __inline int +_citrus_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + void * __restrict ps, + size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_put_state_reset); + return (*ce->ce_ops->eo_put_state_reset)(ce, s, n, ps, nresult); +} + +static __inline size_t +_citrus_stdenc_get_state_size(struct _citrus_stdenc *ce) +{ + _DIAGASSERT(ce && ce->ce_traits); + return ce->ce_traits->et_state_size; +} + +static __inline size_t +_citrus_stdenc_get_mb_cur_max(struct _citrus_stdenc *ce) +{ + _DIAGASSERT(ce && ce->ce_traits); + return ce->ce_traits->et_mb_cur_max; +} + +static __inline int +_citrus_stdenc_get_state_desc(struct _citrus_stdenc * __restrict ce, + void * __restrict ps, + int id, + struct _citrus_stdenc_state_desc * __restrict d) +{ + + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_get_state_desc); + + return (*ce->ce_ops->eo_get_state_desc)(ce, ps, id, d); +} + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_stdenc_local.h b/lib/nbsd_libc/citrus/citrus_stdenc_local.h new file mode 100644 index 000000000..bac89c624 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_stdenc_local.h @@ -0,0 +1,153 @@ +/* $NetBSD: citrus_stdenc_local.h,v 1.4 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + * + */ + +#ifndef _CITRUS_STDENC_LOCAL_H_ +#define _CITRUS_STDENC_LOCAL_H_ + +#define _CITRUS_STDENC_GETOPS_FUNC_BASE(n) \ +int n(struct _citrus_stdenc_ops *, size_t, uint32_t) +#define _CITRUS_STDENC_GETOPS_FUNC(_e_) \ +_CITRUS_STDENC_GETOPS_FUNC_BASE(_citrus_##_e_##_stdenc_getops) +typedef _CITRUS_STDENC_GETOPS_FUNC_BASE((*_citrus_stdenc_getops_t)); + + +#define _CITRUS_STDENC_DECLS(_e_) \ +static int _citrus_##_e_##_stdenc_init \ + (struct _citrus_stdenc * __restrict, const void * __restrict, \ + size_t, struct _citrus_stdenc_traits * __restrict); \ +static void _citrus_##_e_##_stdenc_uninit(struct _citrus_stdenc *); \ +static int _citrus_##_e_##_stdenc_init_state \ + (struct _citrus_stdenc * __restrict, void * __restrict); \ +static int _citrus_##_e_##_stdenc_mbtocs \ + (struct _citrus_stdenc * __restrict, \ + _citrus_csid_t * __restrict, _citrus_index_t * __restrict, \ + const char ** __restrict, size_t, \ + void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_cstomb \ + (struct _citrus_stdenc * __restrict, char * __restrict, \ + size_t, _citrus_csid_t, _citrus_index_t, \ + void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_mbtowc \ + (struct _citrus_stdenc * __restrict, \ + _citrus_wc_t * __restrict, \ + const char ** __restrict, size_t, \ + void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_wctomb \ + (struct _citrus_stdenc * __restrict, char * __restrict, size_t, \ + _citrus_wc_t, void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_put_state_reset \ + (struct _citrus_stdenc * __restrict, char * __restrict, size_t, \ + void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_get_state_desc \ + (struct _citrus_stdenc * __restrict, void * __restrict, int, \ + struct _citrus_stdenc_state_desc * __restrict) + +#define _CITRUS_STDENC_DEF_OPS(_e_) \ +struct _citrus_stdenc_ops _citrus_##_e_##_stdenc_ops = { \ + /* eo_abi_version */ _CITRUS_STDENC_ABI_VERSION, \ + /* eo_init */ &_citrus_##_e_##_stdenc_init, \ + /* eo_uninit */ &_citrus_##_e_##_stdenc_uninit, \ + /* eo_init_state */ &_citrus_##_e_##_stdenc_init_state, \ + /* eo_mbtocs */ &_citrus_##_e_##_stdenc_mbtocs, \ + /* eo_cstomb */ &_citrus_##_e_##_stdenc_cstomb, \ + /* eo_mbtowc */ &_citrus_##_e_##_stdenc_mbtowc, \ + /* eo_wctomb */ &_citrus_##_e_##_stdenc_wctomb, \ + /* eo_put_state_reset */&_citrus_##_e_##_stdenc_put_state_reset,\ + /* eo_get_state_desc */ &_citrus_##_e_##_stdenc_get_state_desc \ +} + +typedef int (*_citrus_stdenc_init_t) + (struct _citrus_stdenc * __reatrict, const void * __restrict , size_t, + struct _citrus_stdenc_traits * __restrict); +typedef void (*_citrus_stdenc_uninit_t)(struct _citrus_stdenc * __restrict); +typedef int (*_citrus_stdenc_init_state_t) + (struct _citrus_stdenc * __restrict, void * __restrict); +typedef int (*_citrus_stdenc_mbtocs_t) + (struct _citrus_stdenc * __restrict, + _citrus_csid_t * __restrict, _citrus_index_t * __restrict, + const char ** __restrict, size_t, + void * __restrict, size_t * __restrict); +typedef int (*_citrus_stdenc_cstomb_t) + (struct _citrus_stdenc *__restrict, char * __restrict, size_t, + _citrus_csid_t, _citrus_index_t, void * __restrict, + size_t * __restrict); +typedef int (*_citrus_stdenc_mbtowc_t) + (struct _citrus_stdenc * __restrict, + _citrus_wc_t * __restrict, + const char ** __restrict, size_t, + void * __restrict, size_t * __restrict); +typedef int (*_citrus_stdenc_wctomb_t) + (struct _citrus_stdenc *__restrict, char * __restrict, size_t, + _citrus_wc_t, void * __restrict, size_t * __restrict); +typedef int (*_citrus_stdenc_put_state_reset_t) + (struct _citrus_stdenc *__restrict, char * __restrict, size_t, + void * __restrict, size_t * __restrict); +typedef int (*_citrus_stdenc_get_state_desc_t) + (struct _citrus_stdenc * __restrict, void * __restrict, int, + struct _citrus_stdenc_state_desc * __restrict); +/* + * ABI version change log + * 0x00000001 + * initial version + */ +#define _CITRUS_STDENC_ABI_VERSION 0x00000002 +struct _citrus_stdenc_ops { + uint32_t eo_abi_version; + /* version 0x00000001 */ + _citrus_stdenc_init_t eo_init; + _citrus_stdenc_uninit_t eo_uninit; + _citrus_stdenc_init_state_t eo_init_state; + _citrus_stdenc_mbtocs_t eo_mbtocs; + _citrus_stdenc_cstomb_t eo_cstomb; + _citrus_stdenc_mbtowc_t eo_mbtowc; + _citrus_stdenc_wctomb_t eo_wctomb; + _citrus_stdenc_put_state_reset_t eo_put_state_reset; + /* version 0x00000002 */ + _citrus_stdenc_get_state_desc_t eo_get_state_desc; +}; + +struct _citrus_stdenc_traits { + /* version 0x00000001 */ + size_t et_state_size; + size_t et_mb_cur_max; +}; + +struct _citrus_stdenc { + /* public */ + /* version 0x00000001 */ + struct _citrus_stdenc_ops *ce_ops; + void *ce_closure; + /* private */ + _citrus_module_t ce_module; + struct _citrus_stdenc_traits *ce_traits; +}; + +#define _CITRUS_DEFAULT_STDENC_NAME "NONE" + +#endif diff --git a/lib/nbsd_libc/citrus/citrus_stdenc_template.h b/lib/nbsd_libc/citrus/citrus_stdenc_template.h new file mode 100644 index 000000000..54088cd54 --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_stdenc_template.h @@ -0,0 +1,206 @@ +/* $NetBSD: citrus_stdenc_template.h,v 1.4 2008/02/09 14:56:20 junyoung Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +/* + * CAUTION: THIS IS NOT STANDALONE FILE + * + * function templates of iconv standard encoding handler for each encodings. + * + */ + +/* + * macros + */ + +#undef _TO_EI +#undef _CE_TO_EI +#undef _TO_STATE +#define _TO_EI(_cl_) ((_ENCODING_INFO*)(_cl_)) +#define _CE_TO_EI(_ce_) (_TO_EI((_ce_)->ce_closure)) +#define _TO_STATE(_ps_) ((_ENCODING_STATE*)(_ps_)) + +/* ---------------------------------------------------------------------- + * templates for public functions + */ + +int +_FUNCNAME(stdenc_getops)(struct _citrus_stdenc_ops *ops, size_t lenops, + uint32_t expected_version) +{ + if (expected_version<_CITRUS_STDENC_ABI_VERSION || lenops 0) { + ei = calloc(1, sizeof(_ENCODING_INFO)); + if (ei == NULL) { + return errno; + } + } + + ret = _FUNCNAME(encoding_module_init)(ei, var, lenvar); + if (ret) { + free((void *)ei); + return ret; + } + + ce->ce_closure = ei; + et->et_state_size = sizeof(_ENCODING_STATE); + et->et_mb_cur_max = _ENCODING_MB_CUR_MAX(_CE_TO_EI(ce)); + + return 0; +} + +static void +_FUNCNAME(stdenc_uninit)(struct _citrus_stdenc * __restrict ce) +{ + if (ce) { + _FUNCNAME(encoding_module_uninit)(_CE_TO_EI(ce)); + free(ce->ce_closure); + } +} + +static int +_FUNCNAME(stdenc_init_state)(struct _citrus_stdenc * __restrict ce, + void * __restrict ps) +{ + _FUNCNAME(init_state)(_CE_TO_EI(ce), _TO_STATE(ps)); + + return 0; +} + +static int +_FUNCNAME(stdenc_mbtocs)(struct _citrus_stdenc * __restrict ce, + _citrus_csid_t * __restrict csid, + _citrus_index_t * __restrict idx, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + int ret; + wchar_t wc; + + _DIAGASSERT(nresult != NULL); + + ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), &wc, s, n, + _TO_STATE(ps), nresult); + + if (!ret && *nresult != (size_t)-2) + _FUNCNAME(stdenc_wctocs)(_CE_TO_EI(ce), csid, idx, wc); + + return ret; +} + +static int +_FUNCNAME(stdenc_cstomb)(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + _citrus_csid_t csid, _citrus_index_t idx, + void * __restrict ps, size_t * __restrict nresult) +{ + int ret; + wchar_t wc; + + _DIAGASSERT(nresult != NULL); + + wc = 0; + + if (csid != _CITRUS_CSID_INVALID) { + ret = _FUNCNAME(stdenc_cstowc)(_CE_TO_EI(ce), &wc, csid, idx); + if (ret) + return ret; + } + + return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps), + nresult); +} + +static int +_FUNCNAME(stdenc_mbtowc)(struct _citrus_stdenc * __restrict ce, + _citrus_wc_t * __restrict wc, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + return _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), wc, s, n, + _TO_STATE(ps), nresult); +} + +static int +_FUNCNAME(stdenc_wctomb)(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, _citrus_wc_t wc, + void * __restrict ps, size_t * __restrict nresult) +{ + return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps), + nresult); +} + +static int +_FUNCNAME(stdenc_put_state_reset)(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + void * __restrict ps, + size_t * __restrict nresult) +{ +#if _ENCODING_IS_STATE_DEPENDENT + return _FUNCNAME(put_state_reset)(_CE_TO_EI(ce), s, n, _TO_STATE(ps), + nresult); +#else + *nresult = 0; + return 0; +#endif +} + +static int +_FUNCNAME(stdenc_get_state_desc)(struct _citrus_stdenc * __restrict ce, + void * __restrict ps, + int id, + struct _citrus_stdenc_state_desc * __restrict d) +{ + int ret; + + switch (id) { + case _STDENC_SDID_GENERIC: + ret = _FUNCNAME(stdenc_get_state_desc_generic)( + _CE_TO_EI(ce), _TO_STATE(ps), &d->u.generic.state); + break; + default: + ret = EOPNOTSUPP; + } + + return ret; +} diff --git a/lib/nbsd_libc/citrus/citrus_types.h b/lib/nbsd_libc/citrus/citrus_types.h new file mode 100644 index 000000000..6692fd3dd --- /dev/null +++ b/lib/nbsd_libc/citrus/citrus_types.h @@ -0,0 +1,41 @@ +/* $NetBSD: citrus_types.h,v 1.3 2003/10/27 00:12:42 lukem Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_TYPES_H_ +#define _CITRUS_TYPES_H_ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +typedef uint32_t _citrus_wc_t; +typedef uint32_t _citrus_index_t; +typedef uint32_t _citrus_csid_t; +#define _CITRUS_CSID_INVALID ((_citrus_csid_t)-1) + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_big5.c b/lib/nbsd_libc/citrus/modules/citrus_big5.c new file mode 100644 index 000000000..eda3a718f --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_big5.c @@ -0,0 +1,512 @@ +/* $NetBSD: citrus_big5.c,v 1.12 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)2002, 2006 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_big5.c,v 1.12 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_big5.h" + +#include "citrus_prop.h" + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + char ch[2]; + int chlen; +} _BIG5State; + +typedef struct _BIG5Exclude { + TAILQ_ENTRY(_BIG5Exclude) entry; + wint_t start, end; +} _BIG5Exclude; + +typedef TAILQ_HEAD(_BIG5ExcludeList, _BIG5Exclude) _BIG5ExcludeList; + +typedef struct { + int cell[0x100]; + _BIG5ExcludeList excludes; +} _BIG5EncodingInfo; + +typedef struct { + _BIG5EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _BIG5State s_mblen; + _BIG5State s_mbrlen; + _BIG5State s_mbrtowc; + _BIG5State s_mbtowc; + _BIG5State s_mbsrtowcs; + _BIG5State s_wcrtomb; + _BIG5State s_wcsrtombs; + _BIG5State s_wctomb; + } states; +} _BIG5CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_BIG5_##m +#define _ENCODING_INFO _BIG5EncodingInfo +#define _CTYPE_INFO _BIG5CTypeInfo +#define _ENCODING_STATE _BIG5State +#define _ENCODING_MB_CUR_MAX(_ei_) 2 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline void +/*ARGSUSED*/ +_citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei, + _BIG5State * __restrict s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei, + void * __restrict pspriv, + const _BIG5State * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei, + _BIG5State * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static __inline int +_citrus_BIG5_check(_BIG5EncodingInfo *ei, u_int c) +{ + _DIAGASSERT(ei != NULL); + + return (ei->cell[c & 0xFF] & 0x1) ? 2 : 1; +} + +static __inline int +_citrus_BIG5_check2(_BIG5EncodingInfo *ei, u_int c) +{ + _DIAGASSERT(ei != NULL); + + return (ei->cell[c & 0xFF] & 0x2) ? 1 : 0; +} + +static __inline int +_citrus_BIG5_check_excludes(_BIG5EncodingInfo *ei, wint_t c) +{ + _BIG5Exclude *exclude; + + _DIAGASSERT(ei != NULL); + + TAILQ_FOREACH(exclude, &ei->excludes, entry) { + if (c >= exclude->start && c <= exclude->end) + return EILSEQ; + } + return 0; +} + +static int +_citrus_BIG5_fill_rowcol(void ** __restrict ctx, const char * __restrict s, + uint64_t start, uint64_t end) +{ + _BIG5EncodingInfo *ei; + int i; + uint64_t n; + + _DIAGASSERT(ctx != NULL && *ctx != NULL); + + if (start > 0xFF || end > 0xFF) + return EINVAL; + ei = (_BIG5EncodingInfo *)*ctx; + i = strcmp("row", s) ? 1 : 0; + i = 1 << i; + for (n = start; n <= end; ++n) + ei->cell[n & 0xFF] |= i; + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_BIG5_fill_excludes(void ** __restrict ctx, const char * __restrict s, + uint64_t start, uint64_t end) +{ + _BIG5EncodingInfo *ei; + _BIG5Exclude *exclude; + + _DIAGASSERT(ctx != NULL && *ctx != NULL); + + if (start > 0xFFFF || end > 0xFFFF) + return EINVAL; + ei = (_BIG5EncodingInfo *)*ctx; + exclude = TAILQ_LAST(&ei->excludes, _BIG5ExcludeList); + if (exclude != NULL && (wint_t)start <= exclude->end) + return EINVAL; + exclude = (void *)malloc(sizeof(*exclude)); + if (exclude == NULL) + return ENOMEM; + exclude->start = (wint_t)start; + exclude->end = (wint_t)end; + TAILQ_INSERT_TAIL(&ei->excludes, exclude, entry); + + return 0; +} + +static const _citrus_prop_hint_t root_hints[] = { + _CITRUS_PROP_HINT_NUM("row", &_citrus_BIG5_fill_rowcol), + _CITRUS_PROP_HINT_NUM("col", &_citrus_BIG5_fill_rowcol), + _CITRUS_PROP_HINT_NUM("excludes", &_citrus_BIG5_fill_excludes), + _CITRUS_PROP_HINT_END +}; + +static void +/*ARGSUSED*/ +_citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei) +{ + _BIG5Exclude *exclude; + + _DIAGASSERT(ei != NULL); + + while ((exclude = TAILQ_FIRST(&ei->excludes)) != NULL) { + TAILQ_REMOVE(&ei->excludes, exclude, entry); + free(exclude); + } +} + +static int +/*ARGSUSED*/ +_citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + int err; + const char *s; + + _DIAGASSERT(ei != NULL); + + memset((void *)ei, 0, sizeof(*ei)); + TAILQ_INIT(&ei->excludes); + + if (lenvar > 0 && var != NULL) { + s = _bcs_skip_ws_len((const char *)var, &lenvar); + if (lenvar > 0 && *s != '\0') { + err = _citrus_prop_parse_variable( + root_hints, (void *)ei, s, lenvar); + if (err == 0) + return 0; + + _citrus_BIG5_encoding_module_uninit(ei); + memset((void *)ei, 0, sizeof(*ei)); + TAILQ_INIT(&ei->excludes); + } + } + + /* fallback Big5-1984, for backward compatibility. */ + _citrus_BIG5_fill_rowcol((void **)&ei, "row", 0xA1, 0xFE); + _citrus_BIG5_fill_rowcol((void **)&ei, "col", 0x40, 0x7E); + _citrus_BIG5_fill_rowcol((void **)&ei, "col", 0xA1, 0xFE); + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, size_t n, + _BIG5State * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + int c; + int chlenbak; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL && *s != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_BIG5_init_state(ei, psenc); + *nresult = 0; + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + break; + default: + /* illegal state */ + goto ilseq; + } + + c = _citrus_BIG5_check(ei, psenc->ch[0] & 0xff); + if (c == 0) + goto ilseq; + while (psenc->chlen < c) { + if (n < 1) { + goto restart; + } + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + + switch (c) { + case 1: + wchar = psenc->ch[0] & 0xff; + break; + case 2: + if (!_citrus_BIG5_check2(ei, psenc->ch[1] & 0xff)) + goto ilseq; + wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); + break; + default: + /* illegal state */ + goto ilseq; + } + + if (_citrus_BIG5_check_excludes(ei, (wint_t)wchar) != 0) + goto ilseq; + + *s = s0; + psenc->chlen = 0; + if (pwc) + *pwc = wchar; + if (!wchar) + *nresult = 0; + else + *nresult = c - chlenbak; + + return (0); + +ilseq: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *s = s0; + *nresult = (size_t)-2; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei, + char * __restrict s, + size_t n, wchar_t wc, _BIG5State * __restrict psenc, + size_t * __restrict nresult) +{ + int l, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + /* check invalid sequence */ + if (wc & ~0xffff || + _citrus_BIG5_check_excludes(ei, (wint_t)wc) != 0) { + ret = EILSEQ; + goto err; + } + + if (wc & 0x8000) { + if (_citrus_BIG5_check(ei, (wc >> 8) & 0xff) != 2 || + !_citrus_BIG5_check2(ei, wc & 0xff)) { + ret = EILSEQ; + goto err; + } + l = 2; + } else { + if (wc & ~0xff || !_citrus_BIG5_check(ei, wc & 0xff)) { + ret = EILSEQ; + goto err; + } + l = 1; + } + + if (n < l) { + /* bound check failure */ + ret = E2BIG; + goto err; + } + + if (l == 2) { + s[0] = (wc >> 8) & 0xff; + s[1] = wc & 0xff; + } else + s[0] = wc & 0xff; + + *nresult = l; + + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + *csid = (wc < 0x100) ? 0 : 1; + *idx = (_index_t)wc; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: + case 1: + *wc = (wchar_t)idx; + break; + default: + return EILSEQ; + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_BIG5_stdenc_get_state_desc_generic(_BIG5EncodingInfo * __restrict ei, + _BIG5State * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(BIG5); +_CITRUS_CTYPE_DEF_OPS(BIG5); + +#include "citrus_ctype_template.h" + + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(BIG5); +_CITRUS_STDENC_DEF_OPS(BIG5); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_big5.h b/lib/nbsd_libc/citrus/modules/citrus_big5.h new file mode 100644 index 000000000..60a0f5990 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_big5.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_big5.h,v 1.2 2003/06/25 09:51:41 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_BIG5_H_ +#define _CITRUS_BIG5_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(BIG5); +_CITRUS_STDENC_GETOPS_FUNC(BIG5); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_dechanyu.c b/lib/nbsd_libc/citrus/modules/citrus_dechanyu.c new file mode 100644 index 000000000..09c40f935 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_dechanyu.c @@ -0,0 +1,441 @@ +/* $NetBSD: citrus_dechanyu.c,v 1.3 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)2007 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_dechanyu.c,v 1.3 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_dechanyu.h" + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + int chlen; + char ch[4]; +} _DECHanyuState; + +typedef struct { + int dummy; +} _DECHanyuEncodingInfo; + +typedef struct { + _DECHanyuEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _DECHanyuState s_mblen; + _DECHanyuState s_mbrlen; + _DECHanyuState s_mbrtowc; + _DECHanyuState s_mbtowc; + _DECHanyuState s_mbsrtowcs; + _DECHanyuState s_wcrtomb; + _DECHanyuState s_wcsrtombs; + _DECHanyuState s_wctomb; + } states; +} _DECHanyuCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.__CONCAT(s_,_func_) + +#define _FUNCNAME(m) __CONCAT(_citrus_DECHanyu_,m) +#define _ENCODING_INFO _DECHanyuEncodingInfo +#define _CTYPE_INFO _DECHanyuCTypeInfo +#define _ENCODING_STATE _DECHanyuState +#define _ENCODING_MB_CUR_MAX(_ei_) 4 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline void +/*ARGSUSED*/ +_citrus_DECHanyu_init_state(_DECHanyuEncodingInfo * __restrict ei, + _DECHanyuState * __restrict psenc) +{ + /* ei may be null */ + _DIAGASSERT(psenc != NULL); + + psenc->chlen = 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_DECHanyu_pack_state(_DECHanyuEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _DECHanyuState * __restrict psenc) +{ + /* ei may be null */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(psenc != NULL); + + memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_DECHanyu_unpack_state(_DECHanyuEncodingInfo * __restrict ei, + _DECHanyuState * __restrict psenc, + const void * __restrict pspriv) +{ + /* ei may be null */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)psenc, pspriv, sizeof(*psenc)); +} + +static void +/*ARGSUSED*/ +_citrus_DECHanyu_encoding_module_uninit(_DECHanyuEncodingInfo *ei) +{ + /* ei may be null */ +} + +static int +/*ARGSUSED*/ +_citrus_DECHanyu_encoding_module_init(_DECHanyuEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + /* ei may be null */ + return 0; +} + +static __inline int +is_singlebyte(int c) +{ + return c <= 0x7F; +} + +static __inline int +is_leadbyte(int c) +{ + return c >= 0xA1 && c <= 0xFE; +} + +static __inline int +is_trailbyte(int c) +{ + c &= ~0x80; + return c >= 0x21 && c <= 0x7E; +} + +static __inline int +is_hanyu1(int c) +{ + return c == 0xC2; +} + +static __inline int +is_hanyu2(int c) +{ + return c == 0xCB; +} + +#define HANYUBIT 0xC2CB0000 + +static __inline int +is_94charset(int c) +{ + return c >= 0x21 && c <= 0x7E; +} + +static int +/*ARGSUSED*/ +_citrus_DECHanyu_mbrtowc_priv(_DECHanyuEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char ** __restrict s, size_t n, + _DECHanyuState * __restrict psenc, size_t * __restrict nresult) +{ + const char *s0; + int ch, i; + wchar_t wc; + + /* ei may be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (*s == NULL) { + _citrus_DECHanyu_init_state(ei, psenc); + *nresult = _ENCODING_IS_STATE_DEPENDENT; + return 0; + } + s0 = *s; + + wc = (wchar_t)0; + switch (psenc->chlen) { + case 0: + if (n-- < 1) + goto restart; + ch = *s0++ & 0xFF; + if (is_singlebyte(ch) != 0) { + if (pwc != NULL) + *pwc = (wchar_t)ch; + *nresult = (size_t)((ch == 0) ? 0 : 1); + *s = s0; + return 0; + } + if (is_leadbyte(ch) == 0) + goto ilseq; + psenc->ch[psenc->chlen++] = ch; + break; + case 1: + ch = psenc->ch[0] & 0xFF; + if (is_leadbyte(ch) == 0) + return EINVAL; + break; + case 2: case 3: + ch = psenc->ch[0] & 0xFF; + if (is_hanyu1(ch) != 0) { + ch = psenc->ch[1] & 0xFF; + if (is_hanyu2(ch) != 0) { + wc |= (wchar_t)HANYUBIT; + break; + } + } + /*FALLTHROUGH*/ + default: + return EINVAL; + } + + switch (psenc->chlen) { + case 1: + if (is_hanyu1(ch) != 0) { + if (n-- < 1) + goto restart; + ch = *s0++ & 0xFF; + if (is_hanyu2(ch) == 0) + goto ilseq; + psenc->ch[psenc->chlen++] = ch; + wc |= (wchar_t)HANYUBIT; + if (n-- < 1) + goto restart; + ch = *s0++ & 0xFF; + if (is_leadbyte(ch) == 0) + goto ilseq; + psenc->ch[psenc->chlen++] = ch; + } + break; + case 2: + if (n-- < 1) + goto restart; + ch = *s0++ & 0xFF; + if (is_leadbyte(ch) == 0) + goto ilseq; + psenc->ch[psenc->chlen++] = ch; + break; + case 3: + ch = psenc->ch[2] & 0xFF; + if (is_leadbyte(ch) == 0) + return EINVAL; + } + if (n-- < 1) + goto restart; + wc |= (wchar_t)(ch << 8); + ch = *s0++ & 0xFF; + if (is_trailbyte(ch) == 0) + goto ilseq; + wc |= (wchar_t)ch; + if (pwc != NULL) + *pwc = wc; + *nresult = (size_t)(s0 - *s); + *s = s0; + psenc->chlen = 0; + + return 0; + +restart: + *nresult = (size_t)-2; + *s = s0; + return 0; + +ilseq: + *nresult = (size_t)-1; + return EILSEQ; +} + +static int +/*ARGSUSED*/ +_citrus_DECHanyu_wcrtomb_priv(_DECHanyuEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _DECHanyuState * __restrict psenc, size_t * __restrict nresult) +{ + int ch; + + /* ei may be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (psenc->chlen != 0) + return EINVAL; + + /* XXX: assume wchar_t as int */ + if ((uint32_t)wc <= 0x7F) { + ch = wc & 0xFF; + } else { + if ((uint32_t)wc > 0xFFFF) { + if ((wc & ~0xFFFF) != HANYUBIT) + goto ilseq; + psenc->ch[psenc->chlen++] = (wc >> 24) & 0xFF; + psenc->ch[psenc->chlen++] = (wc >> 16) & 0xFF; + wc &= 0xFFFF; + } + ch = (wc >> 8) & 0xFF; + if (!is_leadbyte(ch)) + goto ilseq; + psenc->ch[psenc->chlen++] = ch; + ch = wc & 0xFF; + if (is_trailbyte(ch) == 0) + goto ilseq; + } + psenc->ch[psenc->chlen++] = ch; + if (n < psenc->chlen) { + *nresult = (size_t)-1; + return E2BIG; + } + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + psenc->chlen = 0; + + return 0; + +ilseq: + *nresult = (size_t)-1; + return EILSEQ; +} + +static __inline int +/*ARGSUSED*/ +_citrus_DECHanyu_stdenc_wctocs(_DECHanyuEncodingInfo * __restrict ei, + _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) +{ + int plane; + wchar_t mask; + + /* ei may be unused */ + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + plane = 0; + mask = 0x7F; + /* XXX: assume wchar_t as int */ + if ((uint32_t)wc > 0x7F) { + if ((uint32_t)wc > 0xFFFF) { + if ((wc & ~0xFFFF) != HANYUBIT) + return EILSEQ; + plane += 2; + } + if (is_leadbyte((wc >> 8) & 0xFF) == 0 || + is_trailbyte(wc & 0xFF) == 0) + return EILSEQ; + plane += (wc & 0x80) ? 1 : 2; + mask |= 0x7F00; + } + *csid = plane; + *idx = (_index_t)(wc & mask); + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_DECHanyu_stdenc_cstowc(_DECHanyuEncodingInfo * __restrict ei, + wchar_t * __restrict wc, _csid_t csid, _index_t idx) +{ + /* ei may be unused */ + _DIAGASSERT(wc != NULL); + + if (csid == 0) { + if (idx > 0x7F) + return EILSEQ; + } else if (csid <= 4) { + if (is_94charset(idx >> 8) == 0) + return EILSEQ; + if (is_94charset(idx & 0xFF) == 0) + return EILSEQ; + if (csid % 2) + idx |= 0x80; + idx |= 0x8000; + if (csid > 2) + idx |= HANYUBIT; + } else + return EILSEQ; + *wc = (wchar_t)idx; + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_DECHanyu_stdenc_get_state_desc_generic( + _DECHanyuEncodingInfo * __restrict ei, + _DECHanyuState * __restrict psenc, int * __restrict rstate) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(rstate != NULL); + + *rstate = (psenc->chlen == 0) + ? _STDENC_SDGEN_INITIAL + : _STDENC_SDGEN_INCOMPLETE_CHAR; + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(DECHanyu); +_CITRUS_CTYPE_DEF_OPS(DECHanyu); + +#include "citrus_ctype_template.h" + + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(DECHanyu); +_CITRUS_STDENC_DEF_OPS(DECHanyu); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_dechanyu.h b/lib/nbsd_libc/citrus/modules/citrus_dechanyu.h new file mode 100644 index 000000000..ed774bcab --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_dechanyu.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_dechanyu.h,v 1.1 2007/04/01 18:52:32 tnozaki Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_DECHANYU_H_ +#define _CITRUS_DECHANYU_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(DECHanyu); +_CITRUS_STDENC_GETOPS_FUNC(DECHanyu); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_euc.c b/lib/nbsd_libc/citrus/modules/citrus_euc.c new file mode 100644 index 000000000..32292f894 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_euc.c @@ -0,0 +1,435 @@ +/* $NetBSD: citrus_euc.c,v 1.14 2009/01/11 02:46:24 christos Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_euc.c,v 1.14 2009/01/11 02:46:24 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_euc.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + char ch[3]; + int chlen; +} _EUCState; + +typedef struct { + unsigned count[4]; + wchar_t bits[4]; + wchar_t mask; + unsigned mb_cur_max; +} _EUCEncodingInfo; + +typedef struct { + _EUCEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _EUCState s_mblen; + _EUCState s_mbrlen; + _EUCState s_mbrtowc; + _EUCState s_mbtowc; + _EUCState s_mbsrtowcs; + _EUCState s_wcrtomb; + _EUCState s_wcsrtombs; + _EUCState s_wctomb; + } states; +} _EUCCTypeInfo; + +#define _SS2 0x008e +#define _SS3 0x008f + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_EUC_##m +#define _ENCODING_INFO _EUCEncodingInfo +#define _CTYPE_INFO _EUCCTypeInfo +#define _ENCODING_STATE _EUCState +#define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline int +_citrus_EUC_cs(unsigned int c) +{ + c &= 0xff; + + return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0); +} + +static __inline int +_citrus_EUC_parse_variable(_EUCEncodingInfo *ei, + const void *var, size_t lenvar) +{ + const char *v, *e; + int x; + + /* parse variable string */ + if (!var) + return (EFTYPE); + + v = (const char *) var; + + while (*v == ' ' || *v == '\t') + ++v; + + ei->mb_cur_max = 1; + for (x = 0; x < 4; ++x) { + ei->count[x] = (int)_bcs_strtol(v, (char **)&e, 0); + if (v == e || !(v = e) || ei->count[x]<1 || ei->count[x]>4) { + return (EFTYPE); + } + if (ei->mb_cur_max < ei->count[x]) + ei->mb_cur_max = ei->count[x]; + while (*v == ' ' || *v == '\t') + ++v; + ei->bits[x] = (int)_bcs_strtol(v, (char **)&e, 0); + if (v == e || !(v = e)) { + return (EFTYPE); + } + while (*v == ' ' || *v == '\t') + ++v; + } + ei->mask = (int)_bcs_strtol(v, (char **)&e, 0); + if (v == e || !(v = e)) { + return (EFTYPE); + } + + return 0; +} + + +static __inline void +/*ARGSUSED*/ +_citrus_EUC_init_state(_EUCEncodingInfo *ei, _EUCState *s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUC_pack_state(_EUCEncodingInfo *ei, void *pspriv, const _EUCState *s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUC_unpack_state(_EUCEncodingInfo *ei, _EUCState *s, + const void *pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +_citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, const char **s, + size_t n, _EUCState *psenc, size_t *nresult) +{ + wchar_t wchar; + int c, cs, len; + int chlenbak; + const char *s0, *s1 = NULL; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_EUC_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + case 2: + break; + default: + /* illgeal state */ + goto encoding_error; + } + + c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)]; + if (c == 0) + goto encoding_error; + while (psenc->chlen < c) { + if (n < 1) + goto restart; + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + *s = s0; + + switch (cs) { + case 3: + case 2: + /* skip SS2/SS3 */ + len = c - 1; + s1 = &psenc->ch[1]; + break; + case 1: + case 0: + len = c; + s1 = &psenc->ch[0]; + break; + default: + goto encoding_error; + } + wchar = 0; + while (len-- > 0) + wchar = (wchar << 8) | (*s1++ & 0xff); + wchar = (wchar & ~ei->mask) | ei->bits[cs]; + + psenc->chlen = 0; + if (pwc) + *pwc = wchar; + + if (!wchar) { + *nresult = 0; + } else { + *nresult = (size_t)(c - chlenbak); + } + + return 0; + +encoding_error: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *nresult = (size_t)-2; + *s = s0; + return (0); +} + +static int +_citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc, + _EUCState *psenc, size_t *nresult) +{ + wchar_t m, nm; + int cs, i, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + m = wc & ei->mask; + nm = wc & ~m; + + for (cs = 0; + cs < sizeof(ei->count)/sizeof(ei->count[0]); + cs++) { + if (m == ei->bits[cs]) + break; + } + /* fallback case - not sure if it is necessary */ + if (cs == sizeof(ei->count)/sizeof(ei->count[0])) + cs = 1; + + i = ei->count[cs]; + if (n < i) { + ret = E2BIG; + goto err; + } + m = (cs) ? 0x80 : 0x00; + switch (cs) { + case 2: + *s++ = _SS2; + i--; + break; + case 3: + *s++ = _SS3; + i--; + break; + } + + while (i-- > 0) + *s++ = ((nm >> (i << 3)) & 0xff) | m; + + *nresult = (size_t)ei->count[cs]; + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + wchar_t m, nm; + + _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL); + + m = wc & ei->mask; + nm = wc & ~m; + + *csid = (_citrus_csid_t)m; + *idx = (_citrus_index_t)nm; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(ei != NULL && wc != NULL); + + if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0) + return (EINVAL); + + *wc = (wchar_t)csid | (wchar_t)idx; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei, + _EUCState * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + + _DIAGASSERT(ei != NULL); + + return (_citrus_EUC_parse_variable(ei, var, lenvar)); +} + +static void +/*ARGSUSED*/ +_citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei) +{ +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(EUC); +_CITRUS_CTYPE_DEF_OPS(EUC); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(EUC); +_CITRUS_STDENC_DEF_OPS(EUC); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_euc.h b/lib/nbsd_libc/citrus/modules/citrus_euc.h new file mode 100644 index 000000000..9f67756ca --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_euc.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_euc.h,v 1.2 2003/06/25 09:51:42 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_EUC_H_ +#define _CITRUS_EUC_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(EUC); +_CITRUS_STDENC_GETOPS_FUNC(EUC); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_euctw.c b/lib/nbsd_libc/citrus/modules/citrus_euctw.c new file mode 100644 index 000000000..7ab35e885 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_euctw.c @@ -0,0 +1,428 @@ +/* $NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c)1999 Citrus Project, + * 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 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. + * + * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $ + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_euctw.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + char ch[4]; + int chlen; +} _EUCTWState; + +typedef struct { + int dummy; +} _EUCTWEncodingInfo; +typedef struct { + _EUCTWEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _EUCTWState s_mblen; + _EUCTWState s_mbrlen; + _EUCTWState s_mbrtowc; + _EUCTWState s_mbtowc; + _EUCTWState s_mbsrtowcs; + _EUCTWState s_wcrtomb; + _EUCTWState s_wcsrtombs; + _EUCTWState s_wctomb; + } states; +} _EUCTWCTypeInfo; + +#define _SS2 0x008e +#define _SS3 0x008f + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_EUCTW_##m +#define _ENCODING_INFO _EUCTWEncodingInfo +#define _CTYPE_INFO _EUCTWCTypeInfo +#define _ENCODING_STATE _EUCTWState +#define _ENCODING_MB_CUR_MAX(_ei_) 4 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline int +_citrus_EUCTW_cs(u_int c) +{ + c &= 0xff; + + return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0); +} + +static __inline int +_citrus_EUCTW_count(int cs) +{ + switch (cs) { + case 0: + return 1; + case 1: + return 2; + case 2: + return 4; + case 3: + abort(); + /*NOTREACHED*/ + } + return 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei, + _EUCTWState * __restrict s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _EUCTWState * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei, + _EUCTWState * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +/*ARGSUSED*/ +_citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + + _DIAGASSERT(ei != NULL); + + memset((void *)ei, 0, sizeof(*ei)); + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei) +{ +} + +static int +_citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, + size_t n, _EUCTWState * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + int c, cs; + int chlenbak; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_EUCTW_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + case 2: + break; + default: + /* illgeal state */ + goto ilseq; + } + + c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff)); + if (c == 0) + goto ilseq; + while (psenc->chlen < c) { + if (n < 1) + goto ilseq; + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + + wchar = 0; + switch (cs) { + case 0: + if (psenc->ch[0] & 0x80) + goto ilseq; + wchar = psenc->ch[0] & 0xff; + break; + case 1: + if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80)) + goto ilseq; + wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); + wchar |= 'G' << 24; + break; + case 2: + if ((u_char)psenc->ch[1] < 0xa1 || 0xa7 < (u_char)psenc->ch[1]) + goto ilseq; + if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80)) + goto ilseq; + wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff); + wchar |= ('G' + psenc->ch[1] - 0xa1) << 24; + break; + default: + goto ilseq; + } + + *s = s0; + psenc->chlen = 0; + + if (pwc) + *pwc = wchar; + + if (!wchar) + *nresult = 0; + else + *nresult = c - chlenbak; + + return (0); + +ilseq: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *s = s0; + *nresult = (size_t)-1; + return (0); +} + +static int +_citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _EUCTWState * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t cs = wc & 0x7f000080; + wchar_t v; + int i, len, clen, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + clen = 1; + if (wc & 0x00007f00) + clen = 2; + if ((wc & 0x007f0000) && !(wc & 0x00800000)) + clen = 3; + + if (clen == 1 && cs == 0x00000000) { + /* ASCII */ + len = 1; + if (n < len) { + ret = E2BIG; + goto err; + } + v = wc & 0x0000007f; + } else if (clen == 2 && cs == ('G' << 24)) { + /* CNS-11643-1 */ + len = 2; + if (n < len) { + ret = E2BIG; + goto err; + } + v = wc & 0x00007f7f; + v |= 0x00008080; + } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') { + /* CNS-11643-[2-7] */ + len = 4; + if (n < len) { + ret = E2BIG; + goto err; + } + *s++ = _SS2; + *s++ = (cs >> 24) - 'H' + 0xa2; + v = wc & 0x00007f7f; + v |= 0x00008080; + } else { + ret = EILSEQ; + goto err; + } + + i = clen; + while (i-- > 0) + *s++ = (v >> (i << 3)) & 0xff; + + *nresult = len; + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + + _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL); + + *csid = (_csid_t)(wc >> 24) & 0xFF; + *idx = (_index_t)(wc & 0x7F7F); + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(ei != NULL && wc != NULL); + + if (csid==0) { + if ((idx & ~0x7F) != 0) + return (EINVAL); + *wc = (wchar_t)idx; + } else { + if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0) + return (EINVAL); + *wc = (wchar_t)idx | ((wchar_t)csid<<24); + } + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei, + _EUCTWState * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(EUCTW); +_CITRUS_CTYPE_DEF_OPS(EUCTW); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(EUCTW); +_CITRUS_STDENC_DEF_OPS(EUCTW); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_euctw.h b/lib/nbsd_libc/citrus/modules/citrus_euctw.h new file mode 100644 index 000000000..dc01412ee --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_euctw.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_euctw.h,v 1.2 2003/06/25 09:51:42 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_EUCTW_H_ +#define _CITRUS_EUCTW_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(EUCTW); +_CITRUS_STDENC_GETOPS_FUNC(EUCTW); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_gbk2k.c b/lib/nbsd_libc/citrus/modules/citrus_gbk2k.c new file mode 100644 index 000000000..77d7dd2b3 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_gbk2k.c @@ -0,0 +1,474 @@ +/* $NetBSD: citrus_gbk2k.c,v 1.7 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_gbk2k.c,v 1.7 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_gbk2k.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct _GBK2KState { + char ch[4]; + int chlen; +} _GBK2KState; + +typedef struct { + int mb_cur_max; +} _GBK2KEncodingInfo; + +typedef struct { + _GBK2KEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _GBK2KState s_mblen; + _GBK2KState s_mbrlen; + _GBK2KState s_mbrtowc; + _GBK2KState s_mbtowc; + _GBK2KState s_mbsrtowcs; + _GBK2KState s_wcrtomb; + _GBK2KState s_wcsrtombs; + _GBK2KState s_wctomb; + } states; +} _GBK2KCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_GBK2K_##m +#define _ENCODING_INFO _GBK2KEncodingInfo +#define _CTYPE_INFO _GBK2KCTypeInfo +#define _ENCODING_STATE _GBK2KState +#define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline void +/*ARGSUSED*/ +_citrus_GBK2K_init_state(_GBK2KEncodingInfo * __restrict ei, + _GBK2KState * __restrict s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_GBK2K_pack_state(_GBK2KEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _GBK2KState * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_GBK2K_unpack_state(_GBK2KEncodingInfo * __restrict ei, + _GBK2KState * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static __inline int +_mb_singlebyte(int c) +{ + c &= 0xff; + return (c <= 0x7f); +} + +static __inline int +_mb_leadbyte(int c) +{ + c &= 0xff; + return (0x81 <= c && c <= 0xfe); +} + +static __inline int +_mb_trailbyte(int c) +{ + c &= 0xff; + return ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfe)); +} + +static __inline int +_mb_surrogate(int c) +{ + c &= 0xff; + return (0x30 <= c && c <= 0x39); +} + +static __inline int +_mb_count(wchar_t v) +{ + u_int32_t c; + + c = (u_int32_t)v; /* XXX */ + if (!(c & 0xffffff00)) + return (1); + if (!(c & 0xffff0000)) + return (2); + return (4); +} + +#define _PSENC (psenc->ch[psenc->chlen - 1]) +#define _PUSH_PSENC(c) (psenc->ch[psenc->chlen++] = (c)) + +static int +_citrus_GBK2K_mbrtowc_priv(_GBK2KEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, size_t n, + _GBK2KState * __restrict psenc, + size_t * __restrict nresult) +{ + int chlenbak, len; + const char *s0, *s1; + wchar_t wc; + + _DIAGASSERT(ei != NULL); + /* pwc may be NULL */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + /* _citrus_GBK2K_init_state(ei, psenc); */ + psenc->chlen = 0; + *nresult = 0; + return (0); + } + + chlenbak = psenc->chlen; + + switch (psenc->chlen) { + case 3: + if (!_mb_leadbyte (_PSENC)) + goto invalid; + /* FALLTHROUGH */ + case 2: + if (!_mb_surrogate(_PSENC) || _mb_trailbyte(_PSENC)) + goto invalid; + /* FALLTHROUGH */ + case 1: + if (!_mb_leadbyte (_PSENC)) + goto invalid; + /* FALLTHOROUGH */ + case 0: + break; + default: + goto invalid; + } + + for (;;) { + if (n-- < 1) + goto restart; + + _PUSH_PSENC(*s0++); + + switch (psenc->chlen) { + case 1: + if (_mb_singlebyte(_PSENC)) + goto convert; + if (_mb_leadbyte (_PSENC)) + continue; + goto ilseq; + case 2: + if (_mb_trailbyte (_PSENC)) + goto convert; + if (ei->mb_cur_max == 4 && + _mb_surrogate (_PSENC)) + continue; + goto ilseq; + case 3: + if (_mb_leadbyte (_PSENC)) + continue; + goto ilseq; + case 4: + if (_mb_surrogate (_PSENC)) + goto convert; + goto ilseq; + } + } + +convert: + len = psenc->chlen; + s1 = &psenc->ch[0]; + wc = 0; + while (len-- > 0) + wc = (wc << 8) | (*s1++ & 0xff); + + if (pwc != NULL) + *pwc = wc; + *s = s0; + *nresult = (wc == 0) ? 0 : psenc->chlen - chlenbak; + /* _citrus_GBK2K_init_state(ei, psenc); */ + psenc->chlen = 0; + + return (0); + +restart: + *s = s0; + *nresult = (size_t)-2; + + return (0); + +invalid: + return (EINVAL); + +ilseq: + *nresult = (size_t)-1; + return (EILSEQ); +} + +static int +_citrus_GBK2K_wcrtomb_priv(_GBK2KEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _GBK2KState * __restrict psenc, + size_t * __restrict nresult) +{ + int len, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + if (psenc->chlen != 0) { + ret = EINVAL; + goto err; + } + + len = _mb_count(wc); + if (n < len) { + ret = E2BIG; + goto err; + } + + switch (len) { + case 1: + if (!_mb_singlebyte(_PUSH_PSENC(wc ))) { + ret = EILSEQ; + goto err; + } + break; + case 2: + if (!_mb_leadbyte (_PUSH_PSENC(wc >> 8)) || + !_mb_trailbyte (_PUSH_PSENC(wc ))) { + ret = EILSEQ; + goto err; + } + break; + case 4: + if (ei->mb_cur_max != 4 || + !_mb_leadbyte (_PUSH_PSENC(wc >> 24)) || + !_mb_surrogate (_PUSH_PSENC(wc >> 16)) || + !_mb_leadbyte (_PUSH_PSENC(wc >> 8)) || + !_mb_surrogate (_PUSH_PSENC(wc ))) { + ret = EILSEQ; + goto err; + } + break; + } + + _DIAGASSERT(len == psenc->chlen); + + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + /* _citrus_GBK2K_init_state(ei, psenc); */ + psenc->chlen = 0; + + return (0); + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_GBK2K_stdenc_wctocs(_GBK2KEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + u_int8_t ch, cl; + + _DIAGASSERT(csid != NULL && idx != NULL); + + if ((u_int32_t)wc<0x80) { + /* ISO646 */ + *csid = 0; + *idx = (_index_t)wc; + } else if ((u_int32_t)wc>=0x10000) { + /* GBKUCS : XXX */ + *csid = 3; + *idx = (_index_t)wc; + } else { + ch = (u_int8_t)(wc >> 8); + cl = (u_int8_t)wc; + if (ch>=0xA1 && cl>=0xA1) { + /* EUC G1 */ + *csid = 1; + *idx = (_index_t)wc & 0x7F7FU; + } else { + /* extended area (0x8140-) */ + *csid = 2; + *idx = (_index_t)wc; + } + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_GBK2K_stdenc_cstowc(_GBK2KEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: + /* ISO646 */ + *wc = (wchar_t)idx; + break; + case 1: + /* EUC G1 */ + *wc = (wchar_t)idx | 0x8080U; + break; + case 2: + /* extended area */ + *wc = (wchar_t)idx; + break; + case 3: + /* GBKUCS : XXX */ + if (ei->mb_cur_max != 4) + return EINVAL; + *wc = (wchar_t)idx; + break; + default: + return EILSEQ; + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_GBK2K_stdenc_get_state_desc_generic(_GBK2KEncodingInfo * __restrict ei, + _GBK2KState * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_GBK2K_encoding_module_init(_GBK2KEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + const char *p; + + _DIAGASSERT(ei != NULL); + + p = var; +#define MATCH(x, act) \ +do { \ + if (lenvar >= (sizeof(#x)-1) && \ + _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ + act; \ + lenvar -= sizeof(#x)-1; \ + p += sizeof(#x)-1; \ + } \ +} while (/*CONSTCOND*/0) + memset((void *)ei, 0, sizeof(*ei)); + ei->mb_cur_max = 4; + while (lenvar>0) { + switch (_bcs_tolower(*p)) { + case '2': + MATCH("2byte", ei->mb_cur_max = 2); + break; + } + p++; + lenvar--; + } + + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_GBK2K_encoding_module_uninit(_GBK2KEncodingInfo *ei) +{ +} + + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(GBK2K); +_CITRUS_CTYPE_DEF_OPS(GBK2K); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(GBK2K); +_CITRUS_STDENC_DEF_OPS(GBK2K); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_gbk2k.h b/lib/nbsd_libc/citrus/modules/citrus_gbk2k.h new file mode 100644 index 000000000..d0ea49115 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_gbk2k.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_gbk2k.h,v 1.2 2003/06/25 09:51:43 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_GBK2K_H_ +#define _CITRUS_GBK2K_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(GBK2K); +_CITRUS_STDENC_GETOPS_FUNC(GBK2K); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_hz.c b/lib/nbsd_libc/citrus/modules/citrus_hz.c new file mode 100644 index 000000000..caebd2f50 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_hz.c @@ -0,0 +1,726 @@ +/* $NetBSD: citrus_hz.c,v 1.2 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)2004, 2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_hz.c,v 1.2 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" + +#include "citrus_hz.h" +#include "citrus_prop.h" + +/* + * wchar_t mapping: + * + * CTRL/ASCII 00000000 00000000 00000000 gxxxxxxx + * GB2312 00000000 00000000 0xxxxxxx gxxxxxxx + * 94/96*n (~M) 0mmmmmmm 0xxxxxxx 0xxxxxxx gxxxxxxx + */ + +#define ESCAPE_CHAR '~' + +typedef enum { + CTRL = 0, ASCII = 1, GB2312 = 2, CS94 = 3, CS96 = 4 +} charset_t; + +typedef struct { + int start, end, width; +} range_t; + +static const range_t ranges[] = { +#define RANGE(start, end) { start, end, (end - start) + 1 } +/* CTRL */ RANGE(0x00, 0x1F), +/* ASCII */ RANGE(0x20, 0x7F), +/* GB2312 */ RANGE(0x21, 0x7E), +/* CS94 */ RANGE(0x21, 0x7E), +/* CS96 */ RANGE(0x20, 0x7F), +#undef RANGE +}; + +typedef struct escape_t escape_t; +typedef struct { + charset_t charset; + size_t length; +#define ROWCOL_MAX 3 + escape_t *escape; +} graphic_t; + +typedef TAILQ_HEAD(escape_list, escape_t) escape_list; +struct escape_t { + TAILQ_ENTRY(escape_t) entry; + int ch; + graphic_t *left, *right; + escape_list *set; +}; + +#define GL(escape) ((escape)->left) +#define GR(escape) ((escape)->right) +#define SET(escape) ((escape)->set) +#define ESC(escape) ((escape)->ch) +#define INIT(escape) (TAILQ_FIRST(SET(escape))) + +static __inline escape_t * +find_escape(escape_list *set, int ch) +{ + escape_t *escape; + + _DIAGASSERT(set != NULL); + + TAILQ_FOREACH(escape, set, entry) { + if (ESC(escape) == ch) + break; + } + + return escape; +} + +typedef struct { + escape_list e0, e1; + graphic_t *ascii, *gb2312; +} _HZEncodingInfo; + +#define E0SET(ei) (&(ei)->e0) +#define E1SET(ei) (&(ei)->e1) +#define INIT0(ei) (TAILQ_FIRST(E0SET(ei))) +#define INIT1(ei) (TAILQ_FIRST(E1SET(ei))) + +typedef struct { + int chlen; + char ch[ROWCOL_MAX]; + escape_t *inuse; +} _HZState; + +typedef struct { + _HZEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _HZState s_mblen; + _HZState s_mbrlen; + _HZState s_mbrtowc; + _HZState s_mbtowc; + _HZState s_mbsrtowcs; + _HZState s_wcrtomb; + _HZState s_wcsrtombs; + _HZState s_wctomb; + } states; +} _HZCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_HZ_##m +#define _ENCODING_INFO _HZEncodingInfo +#define _CTYPE_INFO _HZCTypeInfo +#define _ENCODING_STATE _HZState +#define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX +#define _ENCODING_IS_STATE_DEPENDENT 1 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) ((_ps_)->inuse == NULL) + +static __inline void +_citrus_HZ_init_state(_HZEncodingInfo * __restrict ei, + _HZState * __restrict psenc) +{ + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + + psenc->chlen = 0; + psenc->inuse = INIT0(ei); +} + +static __inline void +/*ARGSUSED*/ +_citrus_HZ_pack_state(_HZEncodingInfo * __restrict ei, + void *__restrict pspriv, const _HZState * __restrict psenc) +{ + /* ei may be unused */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(psenc != NULL); + + memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_HZ_unpack_state(_HZEncodingInfo * __restrict ei, + _HZState * __restrict psenc, const void * __restrict pspriv) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)psenc, pspriv, sizeof(*psenc)); +} + +static int +_citrus_HZ_mbrtowc_priv(_HZEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char ** __restrict s, size_t n, + _HZState * __restrict psenc, size_t * __restrict nresult) +{ + const char *s0; + wchar_t wc; + int bit, head, tail, len, ch; + graphic_t *graphic; + escape_t *candidate, *init; + const range_t *range; + + _DIAGASSERT(ei != NULL); + /* pwc may be null */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (*s == NULL) { + _citrus_HZ_init_state(ei, psenc); + *nresult = 1; + return 0; + } + s0 = *s; + if (psenc->chlen < 0 || psenc->inuse == NULL) + return EINVAL; + + wc = (wchar_t)0; + bit = head = tail = 0; + graphic = NULL; + for (len = 0; len <= MB_LEN_MAX; /**/) { + if (psenc->chlen == tail) { + if (n-- < 1) { + *s = s0; + *nresult = (size_t)-2; + return 0; + } + psenc->ch[psenc->chlen++] = *s0++; + ++len; + } + ch = (unsigned char)psenc->ch[tail++]; + if (tail == 1) { + if ((ch & ~0x80) <= 0x1F) { + if (psenc->inuse != INIT0(ei)) + break; + wc = (wchar_t)ch; + goto done; + } + if (ch & 0x80) { + graphic = GR(psenc->inuse); + bit = 0x80; + ch &= ~0x80; + } else { + graphic = GL(psenc->inuse); + if (ch == ESCAPE_CHAR) + continue; + bit = 0x0; + } + if (graphic == NULL) + break; + } else if (tail == 2 && psenc->ch[0] == ESCAPE_CHAR) { + if (tail < psenc->chlen) + return EINVAL; + if (ch == ESCAPE_CHAR) { + ++head; + } else if (ch == '\n') { + if (psenc->inuse != INIT0(ei)) + break; + tail = psenc->chlen = 0; + continue; + } else { + candidate = NULL; + init = INIT0(ei); + _DIAGASSERT(init != NULL); + if (psenc->inuse == init) { + init = INIT1(ei); + } else if (INIT(psenc->inuse) == init) { + if (ESC(init) != ch) + break; + candidate = init; + } + if (candidate == NULL) { + candidate = find_escape( + SET(psenc->inuse), ch); + if (candidate == NULL) { + if (init == NULL || + ESC(init) != ch) + break; + candidate = init; + } + } + psenc->inuse = candidate; + tail = psenc->chlen = 0; + continue; + } + } else if (ch & 0x80) { + if (graphic != GR(psenc->inuse)) + break; + ch &= ~0x80; + } else { + if (graphic != GL(psenc->inuse)) + break; + } + _DIAGASSERT(graphic != NULL); + range = &ranges[(size_t)graphic->charset]; + if (range->start > ch || range->end < ch) + break; + wc <<= 8; + wc |= ch; + if (graphic->length == (tail - head)) { + if (graphic->charset > GB2312) + bit |= ESC(psenc->inuse) << 24; + wc |= bit; + goto done; + } + } + *nresult = (size_t)-1; + return EILSEQ; +done: + if (tail < psenc->chlen) + return EINVAL; + *s = s0; + if (pwc != NULL) + *pwc = wc; + psenc->chlen = 0; + *nresult = (wc == 0) ? 0 : len; + + return 0; +} + +static int +_citrus_HZ_wcrtomb_priv(_HZEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _HZState * __restrict psenc, size_t * __restrict nresult) +{ + int bit, ch; + escape_t *candidate, *init; + graphic_t *graphic; + size_t len; + const range_t *range; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (psenc->chlen != 0 || psenc->inuse == NULL) + return EINVAL; + if (wc & 0x80) { + bit = 0x80; + wc &= ~0x80; + } else { + bit = 0x0; + } + if ((uint32_t)wc <= 0x1F) { + candidate = INIT0(ei); + graphic = (bit == 0) + ? candidate->left : candidate->right; + if (graphic == NULL) + goto ilseq; + range = &ranges[(size_t)CTRL]; + len = 1; + } else if ((uint32_t)wc <= 0x7F) { + graphic = ei->ascii; + if (graphic == NULL) + goto ilseq; + candidate = graphic->escape; + range = &ranges[(size_t)graphic->charset]; + len = graphic->length; + } else if ((uint32_t)wc <= 0x7F7F) { + graphic = ei->gb2312; + if (graphic == NULL) + goto ilseq; + candidate = graphic->escape; + range = &ranges[(size_t)graphic->charset]; + len = graphic->length; + } else { + ch = (wc >> 24) & 0xFF; + candidate = find_escape(E0SET(ei), ch); + if (candidate == NULL) { + candidate = find_escape(E1SET(ei), ch); + if (candidate == NULL) + goto ilseq; + } + wc &= ~0xFF000000; + graphic = (bit == 0) + ? candidate->left : candidate->right; + if (graphic == NULL) + goto ilseq; + range = &ranges[(size_t)graphic->charset]; + len = graphic->length; + } + if (psenc->inuse != candidate) { + init = INIT0(ei); + if (SET(psenc->inuse) == SET(candidate)) { + if (INIT(psenc->inuse) != init || + psenc->inuse == init || candidate == init) + init = NULL; + } else if (candidate == (init = INIT(candidate))) { + init = NULL; + } + if (init != NULL) { + if (n < 2) + return E2BIG; + n -= 2; + psenc->ch[psenc->chlen++] = ESCAPE_CHAR; + psenc->ch[psenc->chlen++] = ESC(init); + } + if (n < 2) + return E2BIG; + n -= 2; + psenc->ch[psenc->chlen++] = ESCAPE_CHAR; + psenc->ch[psenc->chlen++] = ESC(candidate); + psenc->inuse = candidate; + } + if (n < len) + return E2BIG; + while (len-- > 0) { + ch = (wc >> (len * 8)) & 0xFF; + if (range->start > ch || range->end < ch) + goto ilseq; + psenc->ch[psenc->chlen++] = ch | bit; + } + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + psenc->chlen = 0; + + return 0; + +ilseq: + *nresult = (size_t)-1; + return EILSEQ; +} + +static __inline int +_citrus_HZ_put_state_reset(_HZEncodingInfo * __restrict ei, + char * __restrict s, size_t n, _HZState * __restrict psenc, + size_t * __restrict nresult) +{ + escape_t *candidate; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (psenc->chlen != 0 || psenc->inuse == NULL) + return EINVAL; + candidate = INIT0(ei); + if (psenc->inuse != candidate) { + if (n < 2) + return E2BIG; + n -= 2; + psenc->ch[psenc->chlen++] = ESCAPE_CHAR; + psenc->ch[psenc->chlen++] = ESC(candidate); + } + if (n < 1) + return E2BIG; + if (psenc->chlen > 0) + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + _citrus_HZ_init_state(ei, psenc); + + return 0; +} + +static __inline int +_citrus_HZ_stdenc_get_state_desc_generic(_HZEncodingInfo * __restrict ei, + _HZState * __restrict psenc, int * __restrict rstate) +{ + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(rstate != NULL); + + if (psenc->chlen < 0 || psenc->inuse == NULL) + return EINVAL; + *rstate = (psenc->chlen == 0) + ? ((psenc->inuse == INIT0(ei)) + ? _STDENC_SDGEN_INITIAL + : _STDENC_SDGEN_STABLE) + : ((psenc->ch[0] == ESCAPE_CHAR) + ? _STDENC_SDGEN_INCOMPLETE_SHIFT + : _STDENC_SDGEN_INCOMPLETE_CHAR); + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_HZ_stdenc_wctocs(_HZEncodingInfo * __restrict ei, + _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) +{ + int bit; + + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + if (wc & 0x80) { + bit = 0x80; + wc &= ~0x80; + } else { + bit = 0x0; + } + if ((uint32_t)wc <= 0x7F) { + *csid = (_csid_t)bit; + *idx = (_index_t)wc; + } else if ((uint32_t)wc <= 0x7F7F) { + *csid = (_csid_t)(bit | 0x8000); + *idx = (_index_t)wc; + } else { + *csid = (_index_t)(wc & ~0x00FFFF7F); + *idx = (_csid_t)(wc & 0x00FFFF7F); + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_HZ_stdenc_cstowc(_HZEncodingInfo * __restrict ei, + wchar_t * __restrict wc, _csid_t csid, _index_t idx) +{ + _DIAGASSERT(ei != NULL); + _DIAGASSERT(wc != NULL); + + *wc = (wchar_t)idx; + switch (csid) { + case 0x80: + case 0x8080: + *wc |= (wchar_t)0x80; + /*FALLTHROUGH*/ + case 0x0: + case 0x8000: + break; + default: + *wc |= (wchar_t)csid; + } + + return 0; +} + +static void +_citrus_HZ_encoding_module_uninit(_HZEncodingInfo *ei) +{ + escape_t *escape; + + _DIAGASSERT(ei != NULL); + while ((escape = TAILQ_FIRST(E0SET(ei))) != NULL) { + TAILQ_REMOVE(E0SET(ei), escape, entry); + free(GL(escape)); + free(GR(escape)); + free(escape); + } + while ((escape = TAILQ_FIRST(E1SET(ei))) != NULL) { + TAILQ_REMOVE(E1SET(ei), escape, entry); + free(GL(escape)); + free(GR(escape)); + free(escape); + } +} + +static int +_citrus_HZ_parse_char(void **context, const char *name, const char *s) +{ + void **p; + escape_t *escape; + + _DIAGASSERT(context != NULL && *context != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(s != NULL); + + p = (void **)*context; + escape = (escape_t *)p[0]; + if (escape->ch != '\0') + return EINVAL; + escape->ch = *s++; + if (escape->ch == ESCAPE_CHAR || *s != '\0') + return EINVAL; + + return 0; +} + +static int +_citrus_HZ_parse_graphic(void **context, const char *name, const char *s) +{ + void **p; + _HZEncodingInfo *ei; + escape_t *escape; + graphic_t *graphic; + + _DIAGASSERT(context != NULL && *context != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(s != NULL); + + p = (void **)*context; + escape = (escape_t *)p[0]; + ei = (_HZEncodingInfo *)p[1]; + graphic = malloc(sizeof(*graphic)); + if (graphic == NULL) + return ENOMEM; + memset(graphic, 0, sizeof(*graphic)); + if (strcmp("GL", name) == 0) { + if (GL(escape) != NULL) + goto release; + GL(escape) = graphic; + } else if (strcmp("GR", name) == 0) { + if (GR(escape) != NULL) + goto release; + GR(escape) = graphic; + } else { +release: + free(graphic); + return EINVAL; + } + graphic->escape = escape; + if (_bcs_strncasecmp("ASCII", s, 5) == 0) { + if (s[5] != '\0') + return EINVAL; + graphic->charset = ASCII; + graphic->length = 1; + ei->ascii = graphic; + return 0; + } else if (_bcs_strncasecmp("GB2312", s, 6) == 0) { + if (s[6] != '\0') + return EINVAL; + graphic->charset = GB2312; + graphic->length = 2; + ei->gb2312 = graphic; + return 0; + } else if (strncmp("94*", s, 3) == 0) { + graphic->charset = CS94; + } else if (strncmp("96*", s, 3) == 0) { + graphic->charset = CS96; + } else { + return EINVAL; + } + s += 3; + switch(*s) { + case '1': case '2': case '3': + graphic->length = (size_t)(*s - '0'); + if (*++s == '\0') + break; + /*FALLTHROUGH*/ + default: + return EINVAL; + } + return 0; +} + +static const _citrus_prop_hint_t escape_hints[] = { +_CITRUS_PROP_HINT_STR("CH", &_citrus_HZ_parse_char), +_CITRUS_PROP_HINT_STR("GL", &_citrus_HZ_parse_graphic), +_CITRUS_PROP_HINT_STR("GR", &_citrus_HZ_parse_graphic), +_CITRUS_PROP_HINT_END +}; + +static int +_citrus_HZ_parse_escape(void **context, const char *name, const char *s) +{ + _HZEncodingInfo *ei; + escape_t *escape; + void *p[2]; + + _DIAGASSERT(context != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(s != NULL); + + ei = (_HZEncodingInfo *)*context; + escape = malloc(sizeof(*escape)); + if (escape == NULL) + return EINVAL; + memset(escape, 0, sizeof(*escape)); + if (strcmp("0", name) == 0) { + escape->set = E0SET(ei); + TAILQ_INSERT_TAIL(E0SET(ei), escape, entry); + } else if (strcmp("1", name) == 0) { + escape->set = E1SET(ei); + TAILQ_INSERT_TAIL(E1SET(ei), escape, entry); + } else { + free(escape); + return EINVAL; + } + p[0] = (void *)escape; + p[1] = (void *)ei; + return _citrus_prop_parse_variable( + escape_hints, (void *)&p[0], s, strlen(s)); +} + +static const _citrus_prop_hint_t root_hints[] = { +_CITRUS_PROP_HINT_STR("0", &_citrus_HZ_parse_escape), +_CITRUS_PROP_HINT_STR("1", &_citrus_HZ_parse_escape), +_CITRUS_PROP_HINT_END +}; + +static int +_citrus_HZ_encoding_module_init(_HZEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + int errnum; + + _DIAGASSERT(ei != NULL); + + memset(ei, 0, sizeof(*ei)); + TAILQ_INIT(E0SET(ei)); + TAILQ_INIT(E1SET(ei)); + errnum = _citrus_prop_parse_variable( + root_hints, (void *)ei, var, lenvar); + if (errnum != 0) + _citrus_HZ_encoding_module_uninit(ei); + return errnum; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(HZ); +_CITRUS_CTYPE_DEF_OPS(HZ); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(HZ); +_CITRUS_STDENC_DEF_OPS(HZ); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_hz.h b/lib/nbsd_libc/citrus/modules/citrus_hz.h new file mode 100644 index 000000000..55435210c --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_hz.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_hz.h,v 1.1 2006/11/22 23:38:27 tnozaki Exp $ */ + +/*- + * Copyright (c)2004, 2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_HZ_H_ +#define _CITRUS_HZ_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(HZ); +_CITRUS_STDENC_GETOPS_FUNC(HZ); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_iconv_none.c b/lib/nbsd_libc/citrus/modules/citrus_iconv_none.c new file mode 100644 index 000000000..45cc2b15f --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_iconv_none.c @@ -0,0 +1,127 @@ +/* $NetBSD: citrus_iconv_none.c,v 1.2 2003/07/01 09:42:16 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_iconv_none.c,v 1.2 2003/07/01 09:42:16 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include + +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_iconv.h" +#include "citrus_iconv_none.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_ICONV_DECLS(iconv_none); +_CITRUS_ICONV_DEF_OPS(iconv_none); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_iconv_none_iconv_getops(struct _citrus_iconv_ops *ops, size_t lenops, + uint32_t expected_version) +{ + if (expected_version<_CITRUS_ICONV_ABI_VERSION || lenopsci_closure = NULL; + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_iconv_none_iconv_uninit_shared(struct _citrus_iconv_shared *ci) +{ +} + +static int +/*ARGSUSED*/ +_citrus_iconv_none_iconv_init_context(struct _citrus_iconv *cv) +{ + cv->cv_closure = NULL; +} + +static void +/*ARGSUSED*/ +_citrus_iconv_none_iconv_uninit_context(struct _citrus_iconv *cv) +{ +} + +static int +/*ARGSUSED*/ +_citrus_iconv_none_iconv_convert(struct _citrus_iconv * __restrict ci, + const char * __restrict * __restrict in, + size_t * __restrict inbytes, + char * __restrict * __restrict out, + size_t * __restrict outbytes, + u_int32_t flags, size_t * __restrict invalids) +{ + int e2big; + size_t len; + + len = *inbytes; + e2big = 0; + if (*outbytes +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_iconv_std.c,v 1.15 2006/11/13 19:08:19 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_iconv.h" +#include "citrus_stdenc.h" +#include "citrus_mapper.h" +#include "citrus_csmapper.h" +#include "citrus_memstream.h" +#include "citrus_iconv_std.h" +#include "citrus_esdb.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_ICONV_DECLS(iconv_std); +_CITRUS_ICONV_DEF_OPS(iconv_std); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_iconv_std_iconv_getops(struct _citrus_iconv_ops *ops, size_t lenops, + u_int32_t expected_version) +{ + if (expected_version<_CITRUS_ICONV_ABI_VERSION || lenopsse_ps) + memcpy(se->se_pssaved, se->se_ps, + _stdenc_get_state_size(se->se_handle)); +} + +static __inline void +restore_encoding_state(struct _citrus_iconv_std_encoding *se) +{ + if (se->se_ps) + memcpy(se->se_ps, se->se_pssaved, + _stdenc_get_state_size(se->se_handle)); +} + +static __inline void +init_encoding_state(struct _citrus_iconv_std_encoding *se) +{ + if (se->se_ps) + _stdenc_init_state(se->se_handle, se->se_ps); +} + +static __inline int +mbtocsx(struct _citrus_iconv_std_encoding *se, + _csid_t *csid, _index_t *idx, const char **s, size_t n, + size_t *nresult) +{ + return _stdenc_mbtocs(se->se_handle, csid, idx, s, n, se->se_ps, + nresult); +} + +static __inline int +cstombx(struct _citrus_iconv_std_encoding *se, + char *s, size_t n, _csid_t csid, _index_t idx, size_t *nresult) +{ + return _stdenc_cstomb(se->se_handle, s, n, csid, idx, se->se_ps, + nresult); +} + +static __inline int +wctombx(struct _citrus_iconv_std_encoding *se, + char *s, size_t n, _wc_t wc, size_t *nresult) +{ + return _stdenc_wctomb(se->se_handle, s, n, wc, se->se_ps, nresult); +} + +static __inline int +put_state_resetx(struct _citrus_iconv_std_encoding *se, + char *s, size_t n, size_t *nresult) +{ + return _stdenc_put_state_reset(se->se_handle, s, n, se->se_ps, nresult); +} + +static __inline int +get_state_desc_gen(struct _citrus_iconv_std_encoding *se, int *rstate) +{ + int ret; + struct _stdenc_state_desc ssd; + + ret = _stdenc_get_state_desc(se->se_handle, se->se_ps, + _STDENC_SDID_GENERIC, &ssd); + if (!ret) + *rstate = ssd.u.generic.state; + + return ret; +} + +/* + * init encoding context + */ +static int +init_encoding(struct _citrus_iconv_std_encoding *se, struct _stdenc *cs, + void *ps1, void *ps2) +{ + int ret = -1; + + se->se_handle = cs; + se->se_ps = ps1; + se->se_pssaved = ps2; + + if (se->se_ps) + ret = _stdenc_init_state(cs, se->se_ps); + if (!ret && se->se_pssaved) + ret = _stdenc_init_state(cs, se->se_pssaved); + + return ret; +} + +static int +open_csmapper(struct _csmapper **rcm, const char *src, const char *dst, + unsigned long *rnorm) +{ + int ret; + struct _csmapper *cm; + + ret = _csmapper_open(&cm, src, dst, 0, rnorm); + if (ret) + return ret; + if (_csmapper_get_src_max(cm) != 1 || _csmapper_get_dst_max(cm) != 1 || + _csmapper_get_state_size(cm) != 0) { + _csmapper_close(cm); + return EINVAL; + } + + *rcm = cm; + + return 0; +} + +static void +close_dsts(struct _citrus_iconv_std_dst_list *dl) +{ + struct _citrus_iconv_std_dst *sd; + + while ((sd=TAILQ_FIRST(dl)) != NULL) { + TAILQ_REMOVE(dl, sd, sd_entry); + _csmapper_close(sd->sd_mapper); + free(sd); + } +} + +static int +open_dsts(struct _citrus_iconv_std_dst_list *dl, + const struct _esdb_charset *ec, const struct _esdb *dbdst) +{ + int i, ret; + struct _citrus_iconv_std_dst *sd, *sdtmp; + unsigned long norm; + + sd = malloc(sizeof(*sd)); + if (sd == NULL) + return errno; + + for (i=0; idb_num_charsets; i++) { + ret = open_csmapper(&sd->sd_mapper, ec->ec_csname, + dbdst->db_charsets[i].ec_csname, &norm); + if (ret == 0) { + sd->sd_csid = dbdst->db_charsets[i].ec_csid; + sd->sd_norm = norm; + /* insert this mapper by sorted order. */ + TAILQ_FOREACH(sdtmp, dl, sd_entry) { + if (sdtmp->sd_norm > norm) { + TAILQ_INSERT_BEFORE(sdtmp, sd, + sd_entry); + sd = NULL; + break; + } + } + if (sd) + TAILQ_INSERT_TAIL(dl, sd, sd_entry); + sd = malloc(sizeof(*sd)); + if (sd == NULL) { + ret = errno; + close_dsts(dl); + return ret; + } + } else if (ret != ENOENT) { + close_dsts(dl); + free(sd); + return ret; + } + } + free(sd); + return 0; +} + +static void +close_srcs(struct _citrus_iconv_std_src_list *sl) +{ + struct _citrus_iconv_std_src *ss; + + while ((ss=TAILQ_FIRST(sl)) != NULL) { + TAILQ_REMOVE(sl, ss, ss_entry); + close_dsts(&ss->ss_dsts); + free(ss); + } +} + +static int +open_srcs(struct _citrus_iconv_std_src_list *sl, + const struct _esdb *dbsrc, const struct _esdb *dbdst) +{ + int i, ret, count = 0; + struct _citrus_iconv_std_src *ss; + + ss = malloc(sizeof(*ss)); + if (ss == NULL) + return errno; + + TAILQ_INIT(&ss->ss_dsts); + + for (i=0; idb_num_charsets; i++) { + ret = open_dsts(&ss->ss_dsts, &dbsrc->db_charsets[i], dbdst); + if (ret) + goto err; + if (!TAILQ_EMPTY(&ss->ss_dsts)) { + ss->ss_csid = dbsrc->db_charsets[i].ec_csid; + TAILQ_INSERT_TAIL(sl, ss, ss_entry); + ss = malloc(sizeof(*ss)); + if (ss == NULL) { + ret = errno; + goto err; + } + count++; + TAILQ_INIT(&ss->ss_dsts); + } + } + free(ss); + + return count ? 0 : ENOENT; + +err: + free(ss); + close_srcs(sl); + return ret; +} + +/* do convert a character */ +#define E_NO_CORRESPONDING_CHAR ENOENT /* XXX */ +static int +/*ARGSUSED*/ +do_conv(const struct _citrus_iconv_std_shared *is, + struct _citrus_iconv_std_context *sc, _csid_t *csid, _index_t *idx) +{ + _index_t tmpidx; + int ret; + struct _citrus_iconv_std_src *ss; + struct _citrus_iconv_std_dst *sd; + + TAILQ_FOREACH(ss, &is->is_srcs, ss_entry) { + if (ss->ss_csid == *csid) { + TAILQ_FOREACH(sd, &ss->ss_dsts, sd_entry) { + ret = _csmapper_convert(sd->sd_mapper, + &tmpidx, *idx, NULL); + switch (ret) { + case _MAPPER_CONVERT_SUCCESS: + *csid = sd->sd_csid; + *idx = tmpidx; + return 0; + case _MAPPER_CONVERT_NONIDENTICAL: + break; + case _MAPPER_CONVERT_SRC_MORE: + /*FALLTHROUGH*/ + case _MAPPER_CONVERT_DST_MORE: + /*FALLTHROUGH*/ + case _MAPPER_CONVERT_FATAL: + return EINVAL; + case _MAPPER_CONVERT_ILSEQ: + return EILSEQ; + } + } + break; + } + } + + return E_NO_CORRESPONDING_CHAR; +} +/* ---------------------------------------------------------------------- */ + +static int +/*ARGSUSED*/ +_citrus_iconv_std_iconv_init_shared(struct _citrus_iconv_shared *ci, + const char * __restrict curdir, + const char * __restrict src, + const char * __restrict dst, + const void * __restrict var, size_t lenvar) +{ + int ret; + struct _citrus_iconv_std_shared *is; + struct _citrus_esdb esdbsrc, esdbdst; + + is = malloc(sizeof(*is)); + if (is==NULL) { + ret = errno; + goto err0; + } + ret = _citrus_esdb_open(&esdbsrc, src); + if (ret) + goto err1; + ret = _citrus_esdb_open(&esdbdst, dst); + if (ret) + goto err2; + ret = _stdenc_open(&is->is_src_encoding, esdbsrc.db_encname, + esdbsrc.db_variable, esdbsrc.db_len_variable); + if (ret) + goto err3; + ret = _stdenc_open(&is->is_dst_encoding, esdbdst.db_encname, + esdbdst.db_variable, esdbdst.db_len_variable); + if (ret) + goto err4; + is->is_use_invalid = esdbdst.db_use_invalid; + is->is_invalid = esdbdst.db_invalid; + + TAILQ_INIT(&is->is_srcs); + ret = open_srcs(&is->is_srcs, &esdbsrc, &esdbdst); + if (ret) + goto err5; + + _esdb_close(&esdbsrc); + _esdb_close(&esdbdst); + ci->ci_closure = is; + + return 0; + +err5: + _stdenc_close(is->is_dst_encoding); +err4: + _stdenc_close(is->is_src_encoding); +err3: + _esdb_close(&esdbdst); +err2: + _esdb_close(&esdbsrc); +err1: + free(is); +err0: + return ret; +} + +static void +_citrus_iconv_std_iconv_uninit_shared(struct _citrus_iconv_shared *ci) +{ + struct _citrus_iconv_std_shared *is = ci->ci_closure; + + if (is == NULL) + return; + + _stdenc_close(is->is_src_encoding); + _stdenc_close(is->is_dst_encoding); + close_srcs(&is->is_srcs); + free(is); +} + +static int +_citrus_iconv_std_iconv_init_context(struct _citrus_iconv *cv) +{ + const struct _citrus_iconv_std_shared *is = cv->cv_shared->ci_closure; + struct _citrus_iconv_std_context *sc; + int ret; + size_t szpssrc, szpsdst, sz; + char *ptr; + + szpssrc = _stdenc_get_state_size(is->is_src_encoding); + szpsdst = _stdenc_get_state_size(is->is_dst_encoding); + + sz = (szpssrc + szpsdst)*2 + sizeof(struct _citrus_iconv_std_context); + sc = malloc(sz); + if (sc == NULL) + return errno; + + ptr = (char *)&sc[1]; + if (szpssrc) + init_encoding(&sc->sc_src_encoding, is->is_src_encoding, + ptr, ptr+szpssrc); + else + init_encoding(&sc->sc_src_encoding, is->is_src_encoding, + NULL, NULL); + ptr += szpssrc*2; + if (szpsdst) + init_encoding(&sc->sc_dst_encoding, is->is_dst_encoding, + ptr, ptr+szpsdst); + else + init_encoding(&sc->sc_dst_encoding, is->is_dst_encoding, + NULL, NULL); + + cv->cv_closure = (void *)sc; + + return 0; +} + +static void +_citrus_iconv_std_iconv_uninit_context(struct _citrus_iconv *cv) +{ + free(cv->cv_closure); +} + +static int +_citrus_iconv_std_iconv_convert(struct _citrus_iconv * __restrict cv, + const char * __restrict * __restrict in, + size_t * __restrict inbytes, + char * __restrict * __restrict out, + size_t * __restrict outbytes, u_int32_t flags, + size_t * __restrict invalids) +{ + const struct _citrus_iconv_std_shared *is = cv->cv_shared->ci_closure; + struct _citrus_iconv_std_context *sc = cv->cv_closure; + _index_t idx; + _csid_t csid; + int ret, state; + size_t szrin, szrout; + size_t inval; + const char *tmpin; + + inval = 0; + if (in==NULL || *in==NULL) { + /* special cases */ + if (out!=NULL && *out!=NULL) { + /* init output state and store the shift sequence */ + save_encoding_state(&sc->sc_src_encoding); + save_encoding_state(&sc->sc_dst_encoding); + szrout = 0; + + ret = put_state_resetx(&sc->sc_dst_encoding, + *out, *outbytes, + &szrout); + if (ret) + goto err; + + if (szrout == (size_t)-2) { + /* too small to store the character */ + ret = EINVAL; + goto err; + } + *out += szrout; + *outbytes -= szrout; + } else + /* otherwise, discard the shift sequence */ + init_encoding_state(&sc->sc_dst_encoding); + init_encoding_state(&sc->sc_src_encoding); + *invalids = 0; + return 0; + } + + /* normal case */ + for (;;) { + if (*inbytes==0) { + ret = get_state_desc_gen(&sc->sc_src_encoding, &state); + if (state == _STDENC_SDGEN_INITIAL || + state == _STDENC_SDGEN_STABLE) + break; + } + + /* save the encoding states for the error recovery */ + save_encoding_state(&sc->sc_src_encoding); + save_encoding_state(&sc->sc_dst_encoding); + + /* mb -> csid/index */ + tmpin = *in; + szrin = szrout = 0; + ret = mbtocsx(&sc->sc_src_encoding, &csid, &idx, + &tmpin, *inbytes, &szrin); + if (ret) + goto err; + + if (szrin == (size_t)-2) { + /* incompleted character */ + ret = get_state_desc_gen(&sc->sc_src_encoding, &state); + if (ret) { + ret = EINVAL; + goto err; + } + switch (state) { + case _STDENC_SDGEN_INITIAL: + case _STDENC_SDGEN_STABLE: + /* fetch shift sequences only. */ + goto next; + } + ret = EINVAL; + goto err; + } + /* convert the character */ + ret = do_conv(is, sc, &csid, &idx); + if (ret) { + if (ret == E_NO_CORRESPONDING_CHAR) { + inval++; + szrout = 0; + if ((flags&_CITRUS_ICONV_F_HIDE_INVALID)==0 && + is->is_use_invalid) { + ret = wctombx(&sc->sc_dst_encoding, + *out, *outbytes, + is->is_invalid, + &szrout); + if (ret) + goto err; + } + goto next; + } else { + goto err; + } + } + /* csid/index -> mb */ + ret = cstombx(&sc->sc_dst_encoding, + *out, *outbytes, csid, idx, &szrout); + if (ret) + goto err; +next: + _DIAGASSERT(*inbytes>=szrin && *outbytes>=szrout); + *inbytes -= tmpin-*in; /* szrin is insufficient on \0. */ + *in = tmpin; + *outbytes -= szrout; + *out += szrout; + } + *invalids = inval; + + return 0; + +err: + restore_encoding_state(&sc->sc_src_encoding); + restore_encoding_state(&sc->sc_dst_encoding); +err_norestore: + *invalids = inval; + + return ret; +} diff --git a/lib/nbsd_libc/citrus/modules/citrus_iconv_std.h b/lib/nbsd_libc/citrus/modules/citrus_iconv_std.h new file mode 100644 index 000000000..c1d43018a --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_iconv_std.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_iconv_std.h,v 1.1 2003/06/25 09:51:44 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ICONV_STD_H_ +#define _CITRUS_ICONV_STD_H_ + +#include "citrus_iconv_std_local.h" + +_CITRUS_ICONV_GETOPS_FUNC(iconv_std); + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_iconv_std_local.h b/lib/nbsd_libc/citrus/modules/citrus_iconv_std_local.h new file mode 100644 index 000000000..4b0f583dd --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_iconv_std_local.h @@ -0,0 +1,81 @@ +/* $NetBSD: citrus_iconv_std_local.h,v 1.2 2003/07/01 09:42:16 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ICONV_STD_LOCAL_H_ +#define _CITRUS_ICONV_STD_LOCAL_H_ + +/* + * encoding + */ +struct _citrus_iconv_std_encoding { + struct _citrus_stdenc *se_handle; + void *se_ps; + void *se_pssaved; +}; + +/* + * dst + */ +struct _citrus_iconv_std_dst { + TAILQ_ENTRY(_citrus_iconv_std_dst) sd_entry; + _citrus_csid_t sd_csid; + unsigned long sd_norm; + struct _citrus_csmapper *sd_mapper; +}; +TAILQ_HEAD(_citrus_iconv_std_dst_list, _citrus_iconv_std_dst); + +/* + * src + */ +struct _citrus_iconv_std_src { + TAILQ_ENTRY(_citrus_iconv_std_src) ss_entry; + _citrus_csid_t ss_csid; + struct _citrus_iconv_std_dst_list ss_dsts; +}; +TAILQ_HEAD(_citrus_iconv_std_src_list, _citrus_iconv_std_src); + +/* + * iconv_std handle + */ +struct _citrus_iconv_std_shared { + struct _citrus_stdenc *is_src_encoding; + struct _citrus_stdenc *is_dst_encoding; + struct _citrus_iconv_std_src_list is_srcs; + int is_use_invalid; + _citrus_wc_t is_invalid; +}; + +/* + * iconv_std context + */ +struct _citrus_iconv_std_context { + struct _citrus_iconv_std_encoding sc_src_encoding; + struct _citrus_iconv_std_encoding sc_dst_encoding; +}; + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_iso2022.c b/lib/nbsd_libc/citrus/modules/citrus_iso2022.c new file mode 100644 index 000000000..3f0eec6ff --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_iso2022.c @@ -0,0 +1,1347 @@ +/* $NetBSD: citrus_iso2022.c,v 1.20 2010/12/07 22:01:45 joerg Exp $ */ + +/*- + * Copyright (c)1999, 2002 Citrus Project, + * 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 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. + * + * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/iso2022.c,v 1.23 2001/06/21 01:51:44 yamt Exp $ + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_iso2022.c,v 1.20 2010/12/07 22:01:45 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_iso2022.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + + +/* + * wchar_t mappings: + * ASCII (ESC ( B) 00000000 00000000 00000000 0xxxxxxx + * iso-8859-1 (ESC , A) 00000000 00000000 00000000 1xxxxxxx + * 94 charset (ESC ( F) 0fffffff 00000000 00000000 0xxxxxxx + * 94 charset (ESC ( M F) 0fffffff 1mmmmmmm 00000000 0xxxxxxx + * 96 charset (ESC , F) 0fffffff 00000000 00000000 1xxxxxxx + * 96 charset (ESC , M F) 0fffffff 1mmmmmmm 00000000 1xxxxxxx + * 94x94 charset (ESC $ ( F) 0fffffff 00000000 0xxxxxxx 0xxxxxxx + * 96x96 charset (ESC $ , F) 0fffffff 00000000 0xxxxxxx 1xxxxxxx + * 94x94 charset (ESC & V ESC $ ( F) + * 0fffffff 1vvvvvvv 0xxxxxxx 0xxxxxxx + * 94x94x94 charset (ESC $ ( F) 0fffffff 0xxxxxxx 0xxxxxxx 0xxxxxxx + * 96x96x96 charset (ESC $ , F) 0fffffff 0xxxxxxx 0xxxxxxx 1xxxxxxx + * reserved for UCS4 co-existence (UCS4 is 31bit encoding thanks to mohta bit) + * 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx + */ + +typedef struct { + u_char type; +#define CS94 (0U) +#define CS96 (1U) +#define CS94MULTI (2U) +#define CS96MULTI (3U) + + u_char final; + u_char interm; + u_char vers; +} _ISO2022Charset; + +static const _ISO2022Charset ascii = { CS94, 'B', '\0', '\0' }; +static const _ISO2022Charset iso88591 = { CS96, 'A', '\0', '\0' }; + +typedef struct { + _ISO2022Charset g[4]; + /* need 3 bits to hold -1, 0, ..., 3 */ + int gl:3, + gr:3, + singlegl:3, + singlegr:3; + char ch[7]; /* longest escape sequence (ESC & V ESC $ ( F) */ + int chlen; + int flags; +#define _ISO2022STATE_FLAG_INITIALIZED 1 +} _ISO2022State; + +typedef struct { + _ISO2022Charset *recommend[4]; + size_t recommendsize[4]; + _ISO2022Charset initg[4]; + int maxcharset; + int flags; +#define F_8BIT 0x0001 +#define F_NOOLD 0x0002 +#define F_SI 0x0010 /*0F*/ +#define F_SO 0x0020 /*0E*/ +#define F_LS0 0x0010 /*0F*/ +#define F_LS1 0x0020 /*0E*/ +#define F_LS2 0x0040 /*ESC n*/ +#define F_LS3 0x0080 /*ESC o*/ +#define F_LS1R 0x0100 /*ESC ~*/ +#define F_LS2R 0x0200 /*ESC }*/ +#define F_LS3R 0x0400 /*ESC |*/ +#define F_SS2 0x0800 /*ESC N*/ +#define F_SS3 0x1000 /*ESC O*/ +#define F_SS2R 0x2000 /*8E*/ +#define F_SS3R 0x4000 /*8F*/ +} _ISO2022EncodingInfo; +typedef struct { + _ISO2022EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _ISO2022State s_mblen; + _ISO2022State s_mbrlen; + _ISO2022State s_mbrtowc; + _ISO2022State s_mbtowc; + _ISO2022State s_mbsrtowcs; + _ISO2022State s_wcrtomb; + _ISO2022State s_wcsrtombs; + _ISO2022State s_wctomb; + } states; +} _ISO2022CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_ISO2022_##m +#define _ENCODING_INFO _ISO2022EncodingInfo +#define _CTYPE_INFO _ISO2022CTypeInfo +#define _ENCODING_STATE _ISO2022State +#define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX +#define _ENCODING_IS_STATE_DEPENDENT 1 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) \ + (!((_ps_)->flags & _ISO2022STATE_FLAG_INITIALIZED)) + + +#define _ISO2022INVALID (wchar_t)-1 + +static __inline int isc0(__uint8_t x) { return ((x & 0x1f) == x); } +static __inline int isc1(__uint8_t x) { return (0x80 <= x && x <= 0x9f); } +static __inline int iscntl(__uint8_t x) { return (isc0(x) || isc1(x) || x == 0x7f); } +static __inline int is94(__uint8_t x) { return (0x21 <= x && x <= 0x7e); } +static __inline int is96(__uint8_t x) { return (0x20 <= x && x <= 0x7f); } +static __inline int isecma(__uint8_t x) { return (0x30 <= x && x <= 0x7f); } +static __inline int isinterm(__uint8_t x) { return (0x20 <= x && x <= 0x2f); } +static __inline int isthree(__uint8_t x) { return (0x60 <= x && x <= 0x6f); } + +static __inline int +getcs(const char * __restrict p, _ISO2022Charset * __restrict cs) +{ + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + if (!strncmp(p, "94$", 3) && p[3] && !p[4]) { + cs->final = (u_char)(p[3] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS94MULTI; + } else if (!strncmp(p, "96$", 3) && p[3] && !p[4]) { + cs->final = (u_char)(p[3] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS96MULTI; + } else if (!strncmp(p, "94", 2) && p[2] && !p[3]) { + cs->final = (u_char)(p[2] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS94; + } else if (!strncmp(p, "96", 2) && p[2] && !p[3]) { + cs->final = (u_char )(p[2] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS96; + } else { + return 1; + } + + return 0; +} + + +#define _NOTMATCH 0 +#define _MATCH 1 +#define _PARSEFAIL 2 + +static __inline int +get_recommend(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + int i; + _ISO2022Charset cs, *p; + + if (!strchr("0123", token[0]) || token[1] != '=') + return (_NOTMATCH); + + if (getcs(&token[2], &cs) == 0) + ; + else if (!strcmp(&token[2], "94")) { + cs.final = (u_char)(token[4]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS94; + } else if (!strcmp(&token[2], "96")) { + cs.final = (u_char)(token[4]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS96; + } else if (!strcmp(&token[2], "94$")) { + cs.final = (u_char)(token[5]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS94MULTI; + } else if (!strcmp(&token[2], "96$")) { + cs.final = (u_char)(token[5]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS96MULTI; + } else { + return (_PARSEFAIL); + } + + i = token[0] - '0'; + if (!ei->recommend[i]) { + ei->recommend[i] = malloc(sizeof(_ISO2022Charset)); + } else { + p = realloc(ei->recommend[i], + sizeof(_ISO2022Charset) * (ei->recommendsize[i] + 1)); + if (!p) + return (_PARSEFAIL); + ei->recommend[i] = p; + } + if (!ei->recommend[i]) + return (_PARSEFAIL); + ei->recommendsize[i]++; + + (ei->recommend[i] + (ei->recommendsize[i] - 1))->final = cs.final; + (ei->recommend[i] + (ei->recommendsize[i] - 1))->interm = cs.interm; + (ei->recommend[i] + (ei->recommendsize[i] - 1))->vers = cs.vers; + (ei->recommend[i] + (ei->recommendsize[i] - 1))->type = cs.type; + + return (_MATCH); +} + +static __inline int +get_initg(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + _ISO2022Charset cs; + + if (strncmp("INIT", &token[0], 4) || + !strchr("0123", token[4]) || + token[5] != '=') + return (_NOTMATCH); + + if (getcs(&token[6], &cs) != 0) + return (_PARSEFAIL); + + ei->initg[token[4] - '0'].type = cs.type; + ei->initg[token[4] - '0'].final = cs.final; + ei->initg[token[4] - '0'].interm = cs.interm; + ei->initg[token[4] - '0'].vers = cs.vers; + + return (_MATCH); +} + +static __inline int +get_max(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + if (!strcmp(token, "MAX1")) { + ei->maxcharset = 1; + } else if (!strcmp(token, "MAX2")) { + ei->maxcharset = 2; + } else if (!strcmp(token, "MAX3")) { + ei->maxcharset = 3; + } else + return (_NOTMATCH); + + return (_MATCH); +} + + +static __inline int +get_flags(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + int i; + static struct { + const char *tag; + int flag; + } const tags[] = { + { "DUMMY", 0 }, + { "8BIT", F_8BIT }, + { "NOOLD", F_NOOLD }, + { "SI", F_SI }, + { "SO", F_SO }, + { "LS0", F_LS0 }, + { "LS1", F_LS1 }, + { "LS2", F_LS2 }, + { "LS3", F_LS3 }, + { "LS1R", F_LS1R }, + { "LS2R", F_LS2R }, + { "LS3R", F_LS3R }, + { "SS2", F_SS2 }, + { "SS3", F_SS3 }, + { "SS2R", F_SS2R }, + { "SS3R", F_SS3R }, + { NULL, 0 } + }; + + for (i = 0; tags[i].tag; i++) { + if (!strcmp(token, tags[i].tag)) { + ei->flags |= tags[i].flag; + return (_MATCH); + } + } + + return (_NOTMATCH); +} + + +static __inline int +_citrus_ISO2022_parse_variable(_ISO2022EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + char const *v, *e; + char buf[20]; + int i, len, ret; + + _DIAGASSERT(ei != NULL); + + + /* + * parse VARIABLE section. + */ + + if (!var) + return (EFTYPE); + + v = (const char *) var; + + /* initialize structure */ + ei->maxcharset = 0; + for (i = 0; i < 4; i++) { + ei->recommend[i] = NULL; + ei->recommendsize[i] = 0; + } + ei->flags = 0; + + while (*v) { + while (*v == ' ' || *v == '\t') + ++v; + + /* find the token */ + e = v; + while (*e && *e != ' ' && *e != '\t') + ++e; + + len = e-v; + if (len == 0) + break; + if (len>=sizeof(buf)) + goto parsefail; + snprintf(buf, sizeof(buf), "%.*s", len, v); + + if ((ret = get_recommend(ei, buf)) != _NOTMATCH) + ; + else if ((ret = get_initg(ei, buf)) != _NOTMATCH) + ; + else if ((ret = get_max(ei, buf)) != _NOTMATCH) + ; + else if ((ret = get_flags(ei, buf)) != _NOTMATCH) + ; + else + ret = _PARSEFAIL; + if (ret==_PARSEFAIL) + goto parsefail; + v = e; + + } + + return (0); + +parsefail: + free(ei->recommend[0]); + free(ei->recommend[1]); + free(ei->recommend[2]); + free(ei->recommend[3]); + + return (EFTYPE); +} + +static __inline void +/*ARGSUSED*/ +_citrus_ISO2022_init_state(_ISO2022EncodingInfo * __restrict ei, + _ISO2022State * __restrict s) +{ + int i; + + memset(s, 0, sizeof(*s)); + s->gl = 0; + s->gr = (ei->flags & F_8BIT) ? 1 : -1; + + for (i = 0; i < 4; i++) { + if (ei->initg[i].final) { + s->g[i].type = ei->initg[i].type; + s->g[i].final = ei->initg[i].final; + s->g[i].interm = ei->initg[i].interm; + } + } + s->singlegl = s->singlegr = -1; + s->flags |= _ISO2022STATE_FLAG_INITIALIZED; +} + +static __inline void +/*ARGSUSED*/ +_citrus_ISO2022_pack_state(_ISO2022EncodingInfo * __restrict ei, + void * __restrict pspriv, + const _ISO2022State * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_ISO2022_unpack_state(_ISO2022EncodingInfo * __restrict ei, + _ISO2022State * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +/*ARGSUSED*/ +_citrus_ISO2022_encoding_module_init(_ISO2022EncodingInfo * __restrict ei, + const void * __restrict var, + size_t lenvar) +{ + + _DIAGASSERT(ei != NULL); + + return _citrus_ISO2022_parse_variable(ei, var, lenvar); +} + +static void +/*ARGSUSED*/ +_citrus_ISO2022_encoding_module_uninit(_ISO2022EncodingInfo *ei) +{ +} + +#define ESC '\033' +#define ECMA -1 +#define INTERM -2 +#define OECMA -3 +static const struct seqtable { + int type; + int csoff; + int finaloff; + int intermoff; + int versoff; + int len; + int chars[10]; +} seqtable[] = { + /* G0 94MULTI special */ + { CS94MULTI, -1, 2, -1, -1, 3, { ESC, '$', OECMA }, }, + /* G0 94MULTI special with version identification */ + { CS94MULTI, -1, 5, -1, 2, 6, { ESC, '&', ECMA, ESC, '$', OECMA }, }, + /* G? 94 */ + { CS94, 1, 2, -1, -1, 3, { ESC, CS94, ECMA, }, }, + /* G? 94 with 2nd intermediate char */ + { CS94, 1, 3, 2, -1, 4, { ESC, CS94, INTERM, ECMA, }, }, + /* G? 96 */ + { CS96, 1, 2, -1, -1, 3, { ESC, CS96, ECMA, }, }, + /* G? 96 with 2nd intermediate char */ + { CS96, 1, 3, 2, -1, 4, { ESC, CS96, INTERM, ECMA, }, }, + /* G? 94MULTI */ + { CS94MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS94, ECMA, }, }, + /* G? 96MULTI */ + { CS96MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS96, ECMA, }, }, + /* G? 94MULTI with version specification */ + { CS94MULTI, 5, 6, -1, 2, 7, { ESC, '&', ECMA, ESC, '$', CS94, ECMA, }, }, + /* LS2/3 */ + { -1, -1, -1, -1, -1, 2, { ESC, 'n', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, 'o', }, }, + /* LS1/2/3R */ + { -1, -1, -1, -1, -1, 2, { ESC, '~', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, /*{*/ '}', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, '|', }, }, + /* SS2/3 */ + { -1, -1, -1, -1, -1, 2, { ESC, 'N', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, 'O', }, }, + /* end of records */ + { 0, } +}; + +static int +seqmatch(const char * __restrict s, size_t n, + const struct seqtable * __restrict sp) +{ + const int *p; + + _DIAGASSERT(s != NULL); + _DIAGASSERT(sp != NULL); + + p = sp->chars; + while (p - sp->chars < n && p - sp->chars < sp->len) { + switch (*p) { + case ECMA: + if (!isecma(*s)) + goto terminate; + break; + case OECMA: + if (*s && strchr("@AB", *s)) + break; + else + goto terminate; + case INTERM: + if (!isinterm(*s)) + goto terminate; + break; + case CS94: + if (*s && strchr("()*+", *s)) + break; + else + goto terminate; + case CS96: + if (*s && strchr(",-./", *s)) + break; + else + goto terminate; + default: + if (*s != *p) + goto terminate; + break; + } + + p++; + s++; + } + +terminate: + return p - sp->chars; +} + +static wchar_t +_ISO2022_sgetwchar(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict string, size_t n, + const char ** __restrict result, + _ISO2022State * __restrict psenc) +{ + wchar_t wchar = 0; + int cur; + const struct seqtable *sp; + int nmatch; + int i; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(string != NULL); + /* result may be NULL */ + + while (1) { + /* SI/SO */ + if (1 <= n && string[0] == '\017') { + psenc->gl = 0; + string++; + n--; + continue; + } + if (1 <= n && string[0] == '\016') { + psenc->gl = 1; + string++; + n--; + continue; + } + + /* SS2/3R */ + if (1 <= n && string[0] && strchr("\217\216", string[0])) { + psenc->singlegl = psenc->singlegr = + (string[0] - '\216') + 2; + string++; + n--; + continue; + } + + /* eat the letter if this is not ESC */ + if (1 <= n && string[0] != '\033') + break; + + /* look for a perfect match from escape sequences */ + for (sp = &seqtable[0]; sp->len; sp++) { + nmatch = seqmatch(string, n, sp); + if (sp->len == nmatch && n >= sp->len) + break; + } + + if (!sp->len) + goto notseq; + + if (sp->type != -1) { + if (sp->csoff == -1) + i = 0; + else { + switch (sp->type) { + case CS94: + case CS94MULTI: + i = string[sp->csoff] - '('; + break; + case CS96: + case CS96MULTI: + i = string[sp->csoff] - ','; + break; + default: + return (_ISO2022INVALID); + } + } + psenc->g[i].type = sp->type; + psenc->g[i].final = '\0'; + psenc->g[i].interm = '\0'; + psenc->g[i].vers = '\0'; + /* sp->finaloff must not be -1 */ + if (sp->finaloff != -1) + psenc->g[i].final = string[sp->finaloff]; + if (sp->intermoff != -1) + psenc->g[i].interm = string[sp->intermoff]; + if (sp->versoff != -1) + psenc->g[i].vers = string[sp->versoff]; + + string += sp->len; + n -= sp->len; + continue; + } + + /* LS2/3 */ + if (2 <= n && string[0] == '\033' + && string[1] && strchr("no", string[1])) { + psenc->gl = string[1] - 'n' + 2; + string += 2; + n -= 2; + continue; + } + + /* LS1/2/3R */ + /* XXX: { for vi showmatch */ + if (2 <= n && string[0] == '\033' + && string[1] && strchr("~}|", string[1])) { + psenc->gr = 3 - (string[1] - '|'); + string += 2; + n -= 2; + continue; + } + + /* SS2/3 */ + if (2 <= n && string[0] == '\033' + && string[1] && strchr("NO", string[1])) { + psenc->singlegl = (string[1] - 'N') + 2; + string += 2; + n -= 2; + continue; + } + + notseq: + /* + * if we've got an unknown escape sequence, eat the ESC at the + * head. otherwise, wait till full escape sequence comes. + */ + for (sp = &seqtable[0]; sp->len; sp++) { + nmatch = seqmatch(string, n, sp); + if (!nmatch) + continue; + + /* + * if we are in the middle of escape sequence, + * we still need to wait for more characters to come + */ + if (n < sp->len) { + if (nmatch == n) { + if (result) + *result = string; + return (_ISO2022INVALID); + } + } else { + if (nmatch == sp->len) { + /* this case should not happen */ + goto eat; + } + } + } + + break; + } + +eat: + /* no letter to eat */ + if (n < 1) { + if (result) + *result = string; + return (_ISO2022INVALID); + } + + /* normal chars. always eat C0/C1 as is. */ + if (iscntl(*string & 0xff)) + cur = -1; + else if (*string & 0x80) { + cur = (psenc->singlegr == -1) + ? psenc->gr : psenc->singlegr; + } else { + cur = (psenc->singlegl == -1) + ? psenc->gl : psenc->singlegl; + } + + if (cur == -1) { +asis: + wchar = *string++ & 0xff; + if (result) + *result = string; + /* reset single shift state */ + psenc->singlegr = psenc->singlegl = -1; + return wchar; + } + + /* length error check */ + switch (psenc->g[cur].type) { + case CS94MULTI: + case CS96MULTI: + if (!isthree(psenc->g[cur].final)) { + if (2 <= n + && (string[0] & 0x80) == (string[1] & 0x80)) + break; + } else { + if (3 <= n + && (string[0] & 0x80) == (string[1] & 0x80) + && (string[0] & 0x80) == (string[2] & 0x80)) + break; + } + + /* we still need to wait for more characters to come */ + if (result) + *result = string; + return (_ISO2022INVALID); + + case CS94: + case CS96: + if (1 <= n) + break; + + /* we still need to wait for more characters to come */ + if (result) + *result = string; + return (_ISO2022INVALID); + } + + /* range check */ + switch (psenc->g[cur].type) { + case CS94: + if (!(is94(string[0] & 0x7f))) + goto asis; + case CS96: + if (!(is96(string[0] & 0x7f))) + goto asis; + break; + case CS94MULTI: + if (!(is94(string[0] & 0x7f) && is94(string[1] & 0x7f))) + goto asis; + break; + case CS96MULTI: + if (!(is96(string[0] & 0x7f) && is96(string[1] & 0x7f))) + goto asis; + break; + } + + /* extract the character. */ + switch (psenc->g[cur].type) { + case CS94: + /* special case for ASCII. */ + if (psenc->g[cur].final == 'B' && !psenc->g[cur].interm) { + wchar = *string++; + wchar &= 0x7f; + break; + } + wchar = psenc->g[cur].final; + wchar = (wchar << 8); + wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0); + wchar = (wchar << 8); + wchar = (wchar << 8) | (*string++ & 0x7f); + break; + case CS96: + /* special case for ISO-8859-1. */ + if (psenc->g[cur].final == 'A' && !psenc->g[cur].interm) { + wchar = *string++; + wchar &= 0x7f; + wchar |= 0x80; + break; + } + wchar = psenc->g[cur].final; + wchar = (wchar << 8); + wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0); + wchar = (wchar << 8); + wchar = (wchar << 8) | (*string++ & 0x7f); + wchar |= 0x80; + break; + case CS94MULTI: + case CS96MULTI: + wchar = psenc->g[cur].final; + wchar = (wchar << 8); + if (isthree(psenc->g[cur].final)) + wchar |= (*string++ & 0x7f); + wchar = (wchar << 8) | (*string++ & 0x7f); + wchar = (wchar << 8) | (*string++ & 0x7f); + if (psenc->g[cur].type == CS96MULTI) + wchar |= 0x80; + break; + } + + if (result) + *result = string; + /* reset single shift state */ + psenc->singlegr = psenc->singlegl = -1; + return wchar; +} + + + +static int +_citrus_ISO2022_mbrtowc_priv(_ISO2022EncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, + size_t n, _ISO2022State * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + const char *s0, *p, *result; + int c; + int chlenbak; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + if (*s == NULL) { + _citrus_ISO2022_init_state(ei, psenc); + *nresult = _ENCODING_IS_STATE_DEPENDENT; + return 0; + } + s0 = *s; + c = 0; + chlenbak = psenc->chlen; + + /* + * if we have something in buffer, use that. + * otherwise, skip here + */ + if (psenc->chlen < 0 || psenc->chlen > sizeof(psenc->ch)) { + /* illgeal state */ + _citrus_ISO2022_init_state(ei, psenc); + goto encoding_error; + } + if (psenc->chlen == 0) + goto emptybuf; + + /* buffer is not empty */ + p = psenc->ch; + while (psenc->chlen < sizeof(psenc->ch)) { + if (n > 0) { + psenc->ch[psenc->chlen++] = *s0++; + n--; + } + + wchar = _ISO2022_sgetwchar(ei, p, psenc->chlen - (p-psenc->ch), + &result, psenc); + c += result - p; + if (wchar != _ISO2022INVALID) { + if (psenc->chlen > c) + memmove(psenc->ch, result, psenc->chlen - c); + if (psenc->chlen < c) + psenc->chlen = 0; + else + psenc->chlen -= c; + goto output; + } + + if (n == 0) { + if ((result - p) == psenc->chlen) + /* complete shift sequence. */ + psenc->chlen = 0; + goto restart; + } + + p = result; + } + + /* escape sequence too long? */ + goto encoding_error; + +emptybuf: + wchar = _ISO2022_sgetwchar(ei, s0, n, &result, psenc); + if (wchar != _ISO2022INVALID) { + c += result - s0; + psenc->chlen = 0; + s0 = result; + goto output; + } + if (result > s0) { + c += (result - s0); + n -= (result - s0); + s0 = result; + if (n>0) + goto emptybuf; + /* complete shift sequence. */ + goto restart; + } + n += c; + if (n < sizeof(psenc->ch)) { + memcpy(psenc->ch, s0 - c, n); + psenc->chlen = n; + s0 = result; + goto restart; + } + + /* escape sequence too long? */ + +encoding_error: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +output: + *s = s0; + if (pwc) + *pwc = wchar; + + if (!wchar) + *nresult = 0; + else + *nresult = c - chlenbak; + + return (0); + +restart: + *s = s0; + *nresult = (size_t)-2; + + return (0); +} + +static int +recommendation(_ISO2022EncodingInfo * __restrict ei, + _ISO2022Charset * __restrict cs) +{ + int i, j; + _ISO2022Charset *recommend; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(cs != NULL); + + /* first, try a exact match. */ + for (i = 0; i < 4; i++) { + recommend = ei->recommend[i]; + for (j = 0; j < ei->recommendsize[i]; j++) { + if (cs->type != recommend[j].type) + continue; + if (cs->final != recommend[j].final) + continue; + if (cs->interm != recommend[j].interm) + continue; + + return i; + } + } + + /* then, try a wildcard match over final char. */ + for (i = 0; i < 4; i++) { + recommend = ei->recommend[i]; + for (j = 0; j < ei->recommendsize[i]; j++) { + if (cs->type != recommend[j].type) + continue; + if (cs->final && (cs->final != recommend[j].final)) + continue; + if (cs->interm && (cs->interm != recommend[j].interm)) + continue; + + return i; + } + } + + /* there's no recommendation. make a guess. */ + if (ei->maxcharset == 0) { + return 0; + } else { + switch (cs->type) { + case CS94: + case CS94MULTI: + return 0; + case CS96: + case CS96MULTI: + return 1; + } + } + return 0; +} + +static int +_ISO2022_sputwchar(_ISO2022EncodingInfo * __restrict ei, wchar_t wc, + char * __restrict string, size_t n, + char ** __restrict result, + _ISO2022State * __restrict psenc, + size_t * __restrict nresult) +{ + int i = 0; + size_t len; + _ISO2022Charset cs; + char *p; + char tmp[MB_LEN_MAX]; + int target; + u_char mask; + int bit8; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(string != NULL); + /* result may be NULL */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (isc0(wc & 0xff)) { + /* go back to INIT0 or ASCII on control chars */ + cs = ei->initg[0].final ? ei->initg[0] : ascii; + } else if (isc1(wc & 0xff)) { + /* go back to INIT1 or ISO-8859-1 on control chars */ + cs = ei->initg[1].final ? ei->initg[1] : iso88591; + } else if (!(wc & ~0xff)) { + if (wc & 0x80) { + /* special treatment for ISO-8859-1 */ + cs = iso88591; + } else { + /* special treatment for ASCII */ + cs = ascii; + } + } else { + cs.final = (wc >> 24) & 0x7f; + if ((wc >> 16) & 0x80) + cs.interm = (wc >> 16) & 0x7f; + else + cs.interm = '\0'; + if (wc & 0x80) + cs.type = (wc & 0x00007f00) ? CS96MULTI : CS96; + else + cs.type = (wc & 0x00007f00) ? CS94MULTI : CS94; + } + target = recommendation(ei, &cs); + p = tmp; + bit8 = ei->flags & F_8BIT; + + /* designate the charset onto the target plane(G0/1/2/3). */ + if (psenc->g[target].type == cs.type + && psenc->g[target].final == cs.final + && psenc->g[target].interm == cs.interm) + goto planeok; + + *p++ = '\033'; + if (cs.type == CS94MULTI || cs.type == CS96MULTI) + *p++ = '$'; + if (target == 0 && cs.type == CS94MULTI && strchr("@AB", cs.final) + && !cs.interm && !(ei->flags & F_NOOLD)) + ; + else if (cs.type == CS94 || cs.type == CS94MULTI) + *p++ = "()*+"[target]; + else + *p++ = ",-./"[target]; + if (cs.interm) + *p++ = cs.interm; + *p++ = cs.final; + + psenc->g[target].type = cs.type; + psenc->g[target].final = cs.final; + psenc->g[target].interm = cs.interm; + +planeok: + /* invoke the plane onto GL or GR. */ + if (psenc->gl == target) + goto sideok; + if (bit8 && psenc->gr == target) + goto sideok; + + if (target == 0 && (ei->flags & F_LS0)) { + *p++ = '\017'; + psenc->gl = 0; + } else if (target == 1 && (ei->flags & F_LS1)) { + *p++ = '\016'; + psenc->gl = 1; + } else if (target == 2 && (ei->flags & F_LS2)) { + *p++ = '\033'; + *p++ = 'n'; + psenc->gl = 2; + } else if (target == 3 && (ei->flags & F_LS3)) { + *p++ = '\033'; + *p++ = 'o'; + psenc->gl = 3; + } else if (bit8 && target == 1 && (ei->flags & F_LS1R)) { + *p++ = '\033'; + *p++ = '~'; + psenc->gr = 1; + } else if (bit8 && target == 2 && (ei->flags & F_LS2R)) { + *p++ = '\033'; + /*{*/ + *p++ = '}'; + psenc->gr = 2; + } else if (bit8 && target == 3 && (ei->flags & F_LS3R)) { + *p++ = '\033'; + *p++ = '|'; + psenc->gr = 3; + } else if (target == 2 && (ei->flags & F_SS2)) { + *p++ = '\033'; + *p++ = 'N'; + psenc->singlegl = 2; + } else if (target == 3 && (ei->flags & F_SS3)) { + *p++ = '\033'; + *p++ = 'O'; + psenc->singlegl = 3; + } else if (bit8 && target == 2 && (ei->flags & F_SS2R)) { + *p++ = '\216'; + *p++ = 'N'; + psenc->singlegl = psenc->singlegr = 2; + } else if (bit8 && target == 3 && (ei->flags & F_SS3R)) { + *p++ = '\217'; + *p++ = 'O'; + psenc->singlegl = psenc->singlegr = 3; + } else + goto ilseq; + +sideok: + if (psenc->singlegl == target) + mask = 0x00; + else if (psenc->singlegr == target) + mask = 0x80; + else if (psenc->gl == target) + mask = 0x00; + else if ((ei->flags & F_8BIT) && psenc->gr == target) + mask = 0x80; + else + goto ilseq; + + switch (cs.type) { + case CS94: + case CS96: + i = 1; + break; + case CS94MULTI: + case CS96MULTI: + i = !iscntl(wc & 0xff) ? + (isthree(cs.final) ? 3 : 2) : 1; + break; + } + while (i-- > 0) + *p++ = ((wc >> (i << 3)) & 0x7f) | mask; + + /* reset single shift state */ + psenc->singlegl = psenc->singlegr = -1; + + len = (size_t)(p - tmp); + if (n < len) { + if (result) + *result = (char *)0; + *nresult = (size_t)-1; + return E2BIG; + } + if (result) + *result = string + len; + memcpy(string, tmp, len); + *nresult = len; + + return 0; + +ilseq: + *nresult = (size_t)-1; + return EILSEQ; +} + +static int +_citrus_ISO2022_put_state_reset(_ISO2022EncodingInfo * __restrict ei, + char * __restrict s, size_t n, + _ISO2022State * __restrict psenc, + size_t * __restrict nresult) +{ + char buf[MB_LEN_MAX]; + char *result; + int ret; + size_t len; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + /* XXX state will be modified after this operation... */ + ret = _ISO2022_sputwchar(ei, L'\0', buf, sizeof(buf), &result, psenc, + &len); + if (ret) { + *nresult = len; + return ret; + } + + if (sizeof(buf) < len || n < len-1) { + /* XXX should recover state? */ + *nresult = (size_t)-1; + return E2BIG; + } + + memcpy(s, buf, len-1); + *nresult = len-1; + return (0); +} + +static int +_citrus_ISO2022_wcrtomb_priv(_ISO2022EncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _ISO2022State * __restrict psenc, + size_t * __restrict nresult) +{ + char buf[MB_LEN_MAX]; + char *result; + int ret; + size_t len; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != 0); + + /* XXX state will be modified after this operation... */ + ret = _ISO2022_sputwchar(ei, wc, buf, sizeof(buf), &result, psenc, + &len); + if (ret) { + *nresult = len; + return ret; + } + + if (sizeof(buf) < len || n < len) { + /* XXX should recover state? */ + *nresult = (size_t)-1; + return E2BIG; + } + + memcpy(s, buf, len); + *nresult = len; + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_ISO2022_stdenc_wctocs(_ISO2022EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + wchar_t m, nm; + + _DIAGASSERT(csid != NULL && idx != NULL); + + m = wc & 0x7FFF8080; + nm = wc & 0x007F7F7F; + if (m & 0x00800000) { + nm &= 0x00007F7F; + } else { + m &= 0x7F008080; + } + if (nm & 0x007F0000) { + /* ^3 mark */ + m |= 0x007F0000; + } else if (nm & 0x00007F00) { + /* ^2 mark */ + m |= 0x00007F00; + } + *csid = (_csid_t)m; + *idx = (_index_t)nm; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_ISO2022_stdenc_cstowc(_ISO2022EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(ei != NULL && wc != NULL); + + *wc = (wchar_t)(csid & 0x7F808080) | (wchar_t)idx; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_ISO2022_stdenc_get_state_desc_generic(_ISO2022EncodingInfo * __restrict ei, + _ISO2022State * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) { + /* XXX: it should distinguish initial and stable. */ + *rstate = _STDENC_SDGEN_STABLE; + } else { + if (psenc->ch[0] == '\033') + *rstate = _STDENC_SDGEN_INCOMPLETE_SHIFT; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + } + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(ISO2022); +_CITRUS_CTYPE_DEF_OPS(ISO2022); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(ISO2022); +_CITRUS_STDENC_DEF_OPS(ISO2022); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_iso2022.h b/lib/nbsd_libc/citrus/modules/citrus_iso2022.h new file mode 100644 index 000000000..e82e0b3ac --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_iso2022.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_iso2022.h,v 1.2 2003/06/25 09:51:44 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ISO2022_H_ +#define _CITRUS_ISO2022_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(ISO2022); +_CITRUS_STDENC_GETOPS_FUNC(ISO2022); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_johab.c b/lib/nbsd_libc/citrus/modules/citrus_johab.c new file mode 100644 index 000000000..bebf939a5 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_johab.c @@ -0,0 +1,392 @@ +/* $NetBSD: citrus_johab.c,v 1.4 2008/06/14 16:01:07 tnozaki Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_johab.c,v 1.4 2008/06/14 16:01:07 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_johab.h" + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + int chlen; + char ch[2]; +} _JOHABState; + +typedef struct { + int dummy; +} _JOHABEncodingInfo; + +typedef struct { + _JOHABEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _JOHABState s_mblen; + _JOHABState s_mbrlen; + _JOHABState s_mbrtowc; + _JOHABState s_mbtowc; + _JOHABState s_mbsrtowcs; + _JOHABState s_wcrtomb; + _JOHABState s_wcsrtombs; + _JOHABState s_wctomb; + } states; +} _JOHABCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_JOHAB_##m +#define _ENCODING_INFO _JOHABEncodingInfo +#define _CTYPE_INFO _JOHABCTypeInfo +#define _ENCODING_STATE _JOHABState +#define _ENCODING_MB_CUR_MAX(_ei_) 2 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline void +/*ARGSUSED*/ +_citrus_JOHAB_init_state(_JOHABEncodingInfo * __restrict ei, + _JOHABState * __restrict psenc) +{ + /* ei may be null */ + _DIAGASSERT(psenc != NULL); + + psenc->chlen = 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_JOHAB_pack_state(_JOHABEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _JOHABState * __restrict psenc) +{ + /* ei may be null */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(psenc != NULL); + + memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_JOHAB_unpack_state(_JOHABEncodingInfo * __restrict ei, + _JOHABState * __restrict psenc, + const void * __restrict pspriv) +{ + /* ei may be null */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)psenc, pspriv, sizeof(*psenc)); +} + +static void +/*ARGSUSED*/ +_citrus_JOHAB_encoding_module_uninit(_JOHABEncodingInfo *ei) +{ + /* ei may be null */ +} + +static int +/*ARGSUSED*/ +_citrus_JOHAB_encoding_module_init(_JOHABEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + /* ei may be null */ + return 0; +} + +static __inline int +ishangul(int l, int t) +{ + + return (l >= 0x84 && l <= 0xD3) && + ((t >= 0x41 && t <= 0x7E) || (t >= 0x81 && t <= 0xFE)); +} + +static __inline int +isuda(int l, int t) +{ + return (l == 0xD8) && + ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)); +} + +static __inline int +ishanja(int l, int t) +{ + return ((l >= 0xD9 && l <= 0xDE) || (l >= 0xE0 && l <= 0xF9)) && + ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)); +} + +static int +/*ARGSUSED*/ +_citrus_JOHAB_mbrtowc_priv(_JOHABEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char ** __restrict s, size_t n, + _JOHABState * __restrict psenc, size_t * __restrict nresult) +{ + const char *s0; + int l, t; + + /* ei may be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != 0); + + if (*s == NULL) { + _citrus_JOHAB_init_state(ei, psenc); + *nresult = _ENCODING_IS_STATE_DEPENDENT; + return 0; + } + s0 = *s; + + switch (psenc->chlen) { + case 0: + if (n-- < 1) + goto restart; + l = *s0++ & 0xFF; + if (l <= 0x7F) { + if (pwc != NULL) + *pwc = (wchar_t)l; + *nresult = (l == 0) ? 0 : 1; + *s = s0; + return 0; + } + psenc->ch[psenc->chlen++] = l; + break; + case 1: + l = psenc->ch[0] & 0xFF; + break; + default: + return EINVAL; + } + if (n-- < 1) { +restart: + *nresult = (size_t)-2; + *s = s0; + return 0; + } + t = *s0++ & 0xFF; + if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) { + *nresult = (size_t)-1; + return EILSEQ; + } + if (pwc != NULL) + *pwc = (wchar_t)(l << 8 | t); + *nresult = s0 - *s; + *s = s0; + psenc->chlen = 0; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_JOHAB_wcrtomb_priv(_JOHABEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _JOHABState * __restrict psenc, size_t * __restrict nresult) +{ + int l, t; + + /* ei may be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (psenc->chlen != 0) + return EINVAL; + + /* XXX assume wchar_t as int */ + if ((uint32_t)wc <= 0x7F) { + if (n < 1) + goto e2big; + *s = wc & 0xFF; + *nresult = 1; + } else if ((uint32_t)wc <= 0xFFFF) { + if (n < 2) { +e2big: + *nresult = (size_t)-1; + return E2BIG; + } + l = (wc >> 8) & 0xFF; + t = wc & 0xFF; + if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) + goto ilseq; + *s++ = l; + *s = t; + *nresult = 2; + } else { +ilseq: + *nresult = (size_t)-1; + return EILSEQ; + } + return 0; + +} + +static __inline int +/*ARGSUSED*/ +_citrus_JOHAB_stdenc_wctocs(_JOHABEncodingInfo * __restrict ei, + _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) +{ + int m, l, t, linear; + + /* ei may be unused */ + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + /* XXX assume wchar_t as int */ + if ((uint32_t)wc <= 0x7F) { + *idx = (_index_t)wc; + *csid = 0; + } else if ((uint32_t)wc <= 0xFFFF) { + l = (wc >> 8) & 0xFF; + t = wc & 0xFF; + if (ishangul(l, t) || isuda(l, t)) { + *idx = (_index_t)wc; + *csid = 1; + } else { + if (l >= 0xD9 && l <= 0xDE) { + linear = l - 0xD9; + m = 0x21; + } else if (l >= 0xE0 && l <= 0xF9) { + linear = l - 0xE0; + m = 0x4A; + } else { + return EILSEQ; + } + linear *= 188; + if (t >= 0x31 && t <= 0x7E) { + linear += t - 0x31; + } else if (t >= 0x91 && t <= 0xFE) { + linear += t - 0x43; + } else { + return EILSEQ; + } + l = (linear / 94) + m; + t = (linear % 94) + 0x21; + *idx = (_index_t)((l << 8) | t); + *csid = 2; + } + } else { + return EILSEQ; + } + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_JOHAB_stdenc_cstowc(_JOHABEncodingInfo * __restrict ei, + wchar_t * __restrict wc, _csid_t csid, _index_t idx) +{ + int m, n, l, t, linear; + + /* ei may be unused */ + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: + case 1: + *wc = (wchar_t)idx; + break; + case 2: + if (idx >= 0x2121 && idx <= 0x2C71) { + m = 0xD9; + n = 0x21; + } else if (idx >= 0x4A21 && idx <= 0x7D7E) { + m = 0xE0; + n = 0x4A; + } else { + return EILSEQ; + } + l = ((idx >> 8) & 0xFF) - n; + t = (idx & 0xFF) - 0x21; + linear = (l * 94) + t; + l = (linear / 188) + m; + t = linear % 188; + t += (t <= 0x4D) ? 0x31 : 0x43; + break; + default: + return EILSEQ; + } + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_JOHAB_stdenc_get_state_desc_generic(_JOHABEncodingInfo * __restrict ei, + _JOHABState * __restrict psenc, int * __restrict rstate) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(rstate != NULL); + + *rstate = (psenc->chlen == 0) + ? _STDENC_SDGEN_INITIAL + : _STDENC_SDGEN_INCOMPLETE_CHAR; + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(JOHAB); +_CITRUS_CTYPE_DEF_OPS(JOHAB); + +#include "citrus_ctype_template.h" + + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(JOHAB); +_CITRUS_STDENC_DEF_OPS(JOHAB); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_johab.h b/lib/nbsd_libc/citrus/modules/citrus_johab.h new file mode 100644 index 000000000..f3878ad6c --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_johab.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_johab.h,v 1.1 2006/10/18 17:54:55 tnozaki Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_JOHAB_H_ +#define _CITRUS_JOHAB_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(JOHAB); +_CITRUS_STDENC_GETOPS_FUNC(JOHAB); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_646.c b/lib/nbsd_libc/citrus/modules/citrus_mapper_646.c new file mode 100644 index 000000000..14e6fd2c0 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_646.c @@ -0,0 +1,259 @@ +/* $NetBSD: citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_646.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_646); +_CITRUS_MAPPER_DEF_OPS(mapper_646); + +/* ---------------------------------------------------------------------- */ + +#define ILSEQ 0xFFFFFFFE +#define INVALID 0xFFFFFFFF +#define SPECIALS(x) \ + x(0x23) \ + x(0x24) \ + x(0x40) \ + x(0x5B) \ + x(0x5C) \ + x(0x5D) \ + x(0x5E) \ + x(0x60) \ + x(0x7B) \ + x(0x7C) \ + x(0x7D) \ + x(0x7E) + +#define INDEX(x) INDEX_##x, + +enum { + SPECIALS(INDEX) + NUM_OF_SPECIALS +}; +struct _citrus_mapper_646 { + int m6_forward; + _index_t m6_map[NUM_OF_SPECIALS]; +}; + +int +_citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopsm6_map[i] = strtoul(buf, (char **)&p, 0); + p = _bcs_skip_ws(buf); + if (*p != T_COMM && !*p) { + ret = EINVAL; + break; + } + } + _unmap_file(&r); + + return ret; +}; + +static int +parse_var(struct _citrus_mapper_646 *m6, struct _memstream *ms, + const char *dir) +{ + struct _region r; + char path[PATH_MAX]; + + m6->m6_forward = 1; + _memstream_skip_ws(ms); + /* whether backward */ + if (_memstream_peek(ms) == '!') { + _memstream_getc(ms); + m6->m6_forward = 0; + } + /* get description file path */ + _memstream_getregion(ms, &r, _memstream_remainder(ms)); + snprintf(path, sizeof(path), "%s/%.*s", + dir, (int)_region_size(&r), (char *)_region_head(&r)); + /* remove trailing white spaces */ + path[_bcs_skip_nonws(path)-path] = '\0'; + return parse_file(m6, path); +} + +static int +/*ARGSUSED*/ +_citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict dir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + struct _citrus_mapper_646 *m6; + struct _memstream ms; + struct _region r; + int ret; + + _DIAGASSERT(cm && dir && mt); + + if (lenmtcm_closure = m6; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm) +{ + if (cm && cm->cm_closure) { + free(cm->cm_closure); + } +} + +static int +/*ARGSUSED*/ +_citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + struct _citrus_mapper_646 *m6; + + _DIAGASSERT(cm && cm->cm_closure); + + m6 = cm->cm_closure; + if (m6->m6_forward) { + /* forward */ + if (src>=0x80) + return _MAPPER_CONVERT_ILSEQ; +#define FORWARD(x) \ +if (src==(x)) { \ + if (m6->m6_map[INDEX_##x]==INVALID) \ + return _MAPPER_CONVERT_NONIDENTICAL; \ + *dst = m6->m6_map[INDEX_##x]; \ + return 0; \ +} else + SPECIALS(FORWARD); + *dst = src; + } else { + /* backward */ +#define BACKWARD(x) \ +if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) { \ + *dst = (x); \ + return 0; \ +} else if (src==(x)) \ + return _MAPPER_CONVERT_ILSEQ; \ +else + SPECIALS(BACKWARD); + if (src>=0x80) + return _MAPPER_CONVERT_NONIDENTICAL; + *dst = src; + } + + return _MAPPER_CONVERT_SUCCESS; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_646_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_646.h b/lib/nbsd_libc/citrus/modules/citrus_mapper_646.h new file mode 100644 index 000000000..cdacae0f3 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_646.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_mapper_646.h,v 1.1 2003/06/25 09:51:45 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_646_H_ +#define _CITRUS_MAPPER_646_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_646); +_CITRUS_MAPPER_GETOPS_FUNC(mapper_646); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_none.c b/lib/nbsd_libc/citrus/modules/citrus_mapper_none.c new file mode 100644 index 000000000..12b31410e --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_none.c @@ -0,0 +1,111 @@ +/* $NetBSD: citrus_mapper_none.c,v 1.2 2003/06/27 17:53:31 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mapper_none.c,v 1.2 2003/06/27 17:53:31 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_none.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_none); +_CITRUS_MAPPER_DEF_OPS(mapper_none); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_mapper_none_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopscm_closure = NULL; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_none_mapper_uninit(struct _citrus_mapper *cm) +{ +} + +static int +/*ARGSUSED*/ +_citrus_mapper_none_mapper_convert(struct _citrus_mapper * __restrict cm, + _citrus_index_t * __restrict dst, + _citrus_index_t src, void * __restrict ps) +{ + *dst = src; + return _CITRUS_MAPPER_CONVERT_SUCCESS; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_none_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_none.h b/lib/nbsd_libc/citrus/modules/citrus_mapper_none.h new file mode 100644 index 000000000..c0d6a7193 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_none.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_mapper_none.h,v 1.1 2003/06/25 09:51:45 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_NONE_H_ +#define _CITRUS_MAPPER_NONE_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_none); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_serial.c b/lib/nbsd_libc/citrus/modules/citrus_mapper_serial.c new file mode 100644 index 000000000..be006b595 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_serial.c @@ -0,0 +1,263 @@ +/* $NetBSD: citrus_mapper_serial.c,v 1.2 2003/07/12 15:39:20 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mapper_serial.c,v 1.2 2003/07/12 15:39:20 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_serial.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_serial); +_CITRUS_MAPPER_DEF_OPS(mapper_serial); + +#define _citrus_mapper_parallel_mapper_init \ + _citrus_mapper_serial_mapper_init +#define _citrus_mapper_parallel_mapper_uninit \ + _citrus_mapper_serial_mapper_uninit +#define _citrus_mapper_parallel_mapper_init_state \ + _citrus_mapper_serial_mapper_init_state +static int _citrus_mapper_parallel_mapper_convert( + struct _citrus_mapper * __restrict, _index_t * __restrict, _index_t, + void * __restrict); +_CITRUS_MAPPER_DEF_OPS(mapper_parallel); +#undef _citrus_mapper_parallel_mapper_init +#undef _citrus_mapper_parallel_mapper_uninit +#undef _citrus_mapper_parallel_mapper_init_state + + +/* ---------------------------------------------------------------------- */ + +struct maplink { + SIMPLEQ_ENTRY(maplink) ml_entry; + struct _mapper *ml_mapper; +}; +SIMPLEQ_HEAD(maplist, maplink); + +struct _citrus_mapper_serial { + struct maplist sr_mappers; +}; + +int +_citrus_mapper_serial_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopssr_mappers)) != NULL) { + SIMPLEQ_REMOVE_HEAD(&sr->sr_mappers, ml_entry); + _mapper_close(ml->ml_mapper); + free(ml); + } +} + +static int +parse_var(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper_serial *sr, struct _memstream *ms) +{ + int ret; + struct _region r; + char mapname[PATH_MAX]; + struct maplink *ml; + + SIMPLEQ_INIT(&sr->sr_mappers); + while (1) { + /* remove beginning white spaces */ + _memstream_skip_ws(ms); + if (_memstream_iseof(ms)) + break; + /* cut down a mapper name */ + _memstream_chr(ms, &r, ','); + snprintf(mapname, sizeof(mapname), "%.*s", + (int)_region_size(&r), (char *)_region_head(&r)); + /* remove trailing white spaces */ + mapname[_bcs_skip_nonws(mapname)-mapname] = '\0'; + /* create a new mapper record */ + ml = malloc(sizeof(*ml)); + if (ml == NULL) + return errno; + ret = _mapper_open(ma, &ml->ml_mapper, mapname); + if (ret) { + free(ml); + return ret; + } + /* support only 1:1 and stateless converter */ + if (_mapper_get_src_max(ml->ml_mapper) != 1 || + _mapper_get_dst_max(ml->ml_mapper) != 1 || + _mapper_get_state_size(ml->ml_mapper) != 0) { + free(ml); + return EINVAL; + } + SIMPLEQ_INSERT_TAIL(&sr->sr_mappers, ml, ml_entry); + } + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict dir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + struct _citrus_mapper_serial *sr; + struct _memstream ms; + struct _region r; + + _DIAGASSERT(cm && dir && mt); + + if (lenmtcm_closure = sr; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_uninit(struct _citrus_mapper *cm) +{ + if (cm && cm->cm_closure) { + uninit(cm->cm_closure); + free(cm->cm_closure); + } +} + +static int +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + int ret; + struct _citrus_mapper_serial *sr; + struct maplink *ml; + + _DIAGASSERT(cm && cm->cm_closure); + + sr = cm->cm_closure; + SIMPLEQ_FOREACH(ml, &sr->sr_mappers, ml_entry) { + ret = _mapper_convert(ml->ml_mapper, &src, src, NULL); + if (ret != _MAPPER_CONVERT_SUCCESS) + return ret; + } + *dst = src; + return _MAPPER_CONVERT_SUCCESS; +} + +static int +/*ARGSUSED*/ +_citrus_mapper_parallel_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + int ret; + struct _citrus_mapper_serial *sr; + struct maplink *ml; + _index_t tmp; + + _DIAGASSERT(cm && cm->cm_closure); + + sr = cm->cm_closure; + SIMPLEQ_FOREACH(ml, &sr->sr_mappers, ml_entry) { + ret = _mapper_convert(ml->ml_mapper, &tmp, src, NULL); + if (ret == _MAPPER_CONVERT_SUCCESS) { + *dst = tmp; + return _MAPPER_CONVERT_SUCCESS; + } else if (ret == _MAPPER_CONVERT_ILSEQ) + return _MAPPER_CONVERT_ILSEQ; + } + return _MAPPER_CONVERT_NONIDENTICAL; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_serial.h b/lib/nbsd_libc/citrus/modules/citrus_mapper_serial.h new file mode 100644 index 000000000..107f0ed6c --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_serial.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_mapper_serial.h,v 1.1 2003/06/25 09:51:46 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_SERIAL_H_ +#define _CITRUS_MAPPER_SERIAL_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_serial); +_CITRUS_MAPPER_GETOPS_FUNC(mapper_parallel); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_std.c b/lib/nbsd_libc/citrus/modules/citrus_mapper_std.c new file mode 100644 index 000000000..7d49f5ff5 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_std.c @@ -0,0 +1,461 @@ +/* $NetBSD: citrus_mapper_std.c,v 1.8 2006/09/11 13:06:33 tnozaki Exp $ */ + +/*- + * Copyright (c)2003, 2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mapper_std.c,v 1.8 2006/09/11 13:06:33 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" + +#include "citrus_mapper_std.h" +#include "citrus_mapper_std_file.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_std); +_CITRUS_MAPPER_DEF_OPS(mapper_std); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops, size_t lenops, + u_int32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopsms_rowcol; + + for (i = rc->rc_src_rowcol_len * rc->rc_src_rowcol_bits, + lz = &rc->rc_src_rowcol[0]; i > 0; ++lz) { + i -= rc->rc_src_rowcol_bits; + n = (src >> i) & rc->rc_src_rowcol_mask; + if (n < lz->begin || n > lz->end) { + switch (rc->rc_oob_mode) { + case _CITRUS_MAPPER_STD_OOB_NONIDENTICAL: + *dst = rc->rc_dst_invalid; + return _MAPPER_CONVERT_NONIDENTICAL; + case _CITRUS_MAPPER_STD_OOB_ILSEQ: + return _MAPPER_CONVERT_ILSEQ; + default: + return _MAPPER_CONVERT_FATAL; + } + } + idx = idx * lz->width + n - lz->begin; + } + switch (rc->rc_dst_unit_bits) { + case 8: + conv = _region_peek8(&rc->rc_table, idx); + break; + case 16: + conv = be16toh(_region_peek16(&rc->rc_table, idx*2)); + break; + case 32: + conv = be32toh(_region_peek32(&rc->rc_table, idx*4)); + break; + default: + return _MAPPER_CONVERT_FATAL; + } + + if (conv == rc->rc_dst_invalid) { + *dst = rc->rc_dst_invalid; + return _MAPPER_CONVERT_NONIDENTICAL; + } + if (conv == rc->rc_dst_ilseq) + return _MAPPER_CONVERT_ILSEQ; + + *dst = conv; + + return _MAPPER_CONVERT_SUCCESS; +} + +static __inline int +set_linear_zone(struct _citrus_mapper_std_linear_zone *lz, + u_int32_t begin, u_int32_t end) +{ + _DIAGASSERT(lz != NULL); + + if (begin > end) + return EFTYPE; + + lz->begin = begin; + lz->end = end; + lz->width= end - begin + 1; + + return 0; +} + +static __inline int +rowcol_parse_variable_compat(struct _citrus_mapper_std_rowcol *rc, + struct _region *r) +{ + const struct _citrus_mapper_std_rowcol_info_compat_x *rcx; + struct _citrus_mapper_std_linear_zone *lz; + u_int32_t m, n; + int ret; + + _DIAGASSERT(rc != NULL); + _DIAGASSERT(r != NULL && _region_size(r) == sizeof(*rcx)); + rcx = _region_head(r); + + rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid); + rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits); + m = be32toh(rcx->rcx_src_col_bits); + n = 1 << (m - 1); + n |= n - 1; + rc->rc_src_rowcol_bits = m; + rc->rc_src_rowcol_mask = n; + + rc->rc_src_rowcol = malloc(2 * + sizeof(*rc->rc_src_rowcol)); + if (rc->rc_src_rowcol == NULL) + return ENOMEM; + lz = rc->rc_src_rowcol; + rc->rc_src_rowcol_len = 1; + m = be32toh(rcx->rcx_src_row_begin); + n = be32toh(rcx->rcx_src_row_end); + if (m + n > 0) { + ret = set_linear_zone(lz, m, n); + if (ret != 0) + return ret; + ++rc->rc_src_rowcol_len, ++lz; + } + m = be32toh(rcx->rcx_src_col_begin); + n = be32toh(rcx->rcx_src_col_end); + + return set_linear_zone(lz, m, n); +} + +static __inline int +rowcol_parse_variable(struct _citrus_mapper_std_rowcol *rc, + struct _region *r) +{ + const struct _citrus_mapper_std_rowcol_info_x *rcx; + struct _citrus_mapper_std_linear_zone *lz; + u_int32_t m, n; + size_t i; + int ret; + + _DIAGASSERT(rc != NULL); + _DIAGASSERT(r != NULL && _region_size(r) == sizeof(*rcx)); + rcx = _region_head(r); + + rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid); + rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits); + + m = be32toh(rcx->rcx_src_rowcol_bits); + n = 1 << (m - 1); + n |= n - 1; + rc->rc_src_rowcol_bits = m; + rc->rc_src_rowcol_mask = n; + + rc->rc_src_rowcol_len = be32toh(rcx->rcx_src_rowcol_len); + if (rc->rc_src_rowcol_len > _CITRUS_MAPPER_STD_ROWCOL_MAX) + return EFTYPE; + rc->rc_src_rowcol = malloc(rc->rc_src_rowcol_len * + sizeof(*rc->rc_src_rowcol)); + if (rc->rc_src_rowcol == NULL) + return ENOMEM; + for (i = 0, lz = rc->rc_src_rowcol; + i < rc->rc_src_rowcol_len; ++i, ++lz) { + m = be32toh(rcx->rcx_src_rowcol[i].begin), + n = be32toh(rcx->rcx_src_rowcol[i].end); + ret = set_linear_zone(lz, m, n); + if (ret != 0) { + free(rc->rc_src_rowcol); + rc->rc_src_rowcol = NULL; + return ret; + } + } + return 0; +} + +static void +rowcol_uninit(struct _citrus_mapper_std *ms) +{ + struct _citrus_mapper_std_rowcol *rc; + _DIAGASSERT(ms != NULL); + + rc = &ms->ms_rowcol; + free(rc->rc_src_rowcol); +} + +static int +rowcol_init(struct _citrus_mapper_std *ms) +{ + int ret; + struct _citrus_mapper_std_rowcol *rc; + const struct _citrus_mapper_std_rowcol_ext_ilseq_info_x *eix; + struct _region r; + u_int64_t table_size; + size_t i; + struct _citrus_mapper_std_linear_zone *lz; + + _DIAGASSERT(ms != NULL); + ms->ms_convert = &rowcol_convert; + ms->ms_uninit = &rowcol_uninit; + rc = &ms->ms_rowcol; + + /* get table region */ + ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE, + &rc->rc_table, NULL); + if (ret) { + if (ret==ENOENT) + ret = EFTYPE; + return ret; + } + + /* get table information */ + ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL); + if (ret) { + if (ret==ENOENT) + ret = EFTYPE; + return ret; + } + switch (_region_size(&r)) { + case _CITRUS_MAPPER_STD_ROWCOL_INFO_COMPAT_SIZE: + ret = rowcol_parse_variable_compat(rc, &r); + break; + case _CITRUS_MAPPER_STD_ROWCOL_INFO_SIZE: + ret = rowcol_parse_variable(rc, &r); + break; + default: + return EFTYPE; + } + if (ret != 0) + return ret; + /* sanity check */ + switch (rc->rc_src_rowcol_bits) { + case 8: case 16: case 32: + if (rc->rc_src_rowcol_len <= 32 / rc->rc_src_rowcol_bits) + break; + /*FALLTHROUGH*/ + default: + return EFTYPE; + } + + /* ilseq extension */ + rc->rc_oob_mode = _CITRUS_MAPPER_STD_OOB_NONIDENTICAL; + rc->rc_dst_ilseq = rc->rc_dst_invalid; + ret = _db_lookup_by_s(ms->ms_db, + _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ, + &r, NULL); + if (ret && ret != ENOENT) + return ret; + if (_region_size(&r) < sizeof(*eix)) + return EFTYPE; + if (ret == 0) { + eix = _region_head(&r); + rc->rc_oob_mode = be32toh(eix->eix_oob_mode); + rc->rc_dst_ilseq = be32toh(eix->eix_dst_ilseq); + } + + /* calcurate expected table size */ + i = rc->rc_src_rowcol_len; + lz = &rc->rc_src_rowcol[--i]; + table_size = lz->width; + while (i > 0) { + lz = &rc->rc_src_rowcol[--i]; + table_size *= lz->width; + } + table_size *= rc->rc_dst_unit_bits/8; + + if (table_size > UINT32_MAX || + _region_size(&rc->rc_table) < table_size) + return EFTYPE; + + return 0; +} + +typedef int (*initfunc_t)(struct _citrus_mapper_std *); +static const struct { + const char *t_name; + initfunc_t t_init; +} types[] = { + { _CITRUS_MAPPER_STD_TYPE_ROWCOL, &rowcol_init }, +}; +#define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0]))) + +static int +/*ARGSUSED*/ +_citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict curdir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + char path[PATH_MAX]; + const char *type; + int ret, id; + struct _citrus_mapper_std *ms; + + /* set traits */ + if (lenmtmt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + /* alloc mapper std structure */ + ms = malloc(sizeof(*ms)); + if (ms==NULL) { + ret = errno; + goto err0; + } + + /* open mapper file */ + snprintf(path, sizeof(path), + "%s/%.*s", curdir, (int)lenvar, (const char *)var); + ret = _map_file(&ms->ms_file, path); + if (ret) + goto err1; + + ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC, + &_db_hash_std, NULL); + if (ret) + goto err2; + + /* get mapper type */ + ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE, + &type, NULL); + if (ret) { + if (ret==ENOENT) + ret = EFTYPE; + goto err3; + } + for (id=0; idcm_closure = ms; + + return 0; + +err3: + _db_close(ms->ms_db); +err2: + _unmap_file(&ms->ms_file); +err1: + free(ms); +err0: + return ret; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm) +{ + struct _citrus_mapper_std *ms; + + _DIAGASSERT(cm!=NULL & cm->cm_closure!=NULL); + + ms = cm->cm_closure; + if (ms->ms_uninit) + (*ms->ms_uninit)(ms); + _db_close(ms->ms_db); + _unmap_file(&ms->ms_file); + free(ms); +} + +static void +/*ARGSUSED*/ +_citrus_mapper_std_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} + +static int +/*ARGSUSED*/ +_citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + struct _citrus_mapper_std *ms; + + _DIAGASSERT(cm!=NULL && cm->cm_closure!=NULL); + + ms = cm->cm_closure; + + _DIAGASSERT(ms->ms_convert != NULL); + + return (*ms->ms_convert)(ms, dst, src, ps); +} diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_std.h b/lib/nbsd_libc/citrus/modules/citrus_mapper_std.h new file mode 100644 index 000000000..83eae8237 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_std.h @@ -0,0 +1,38 @@ +/* $NetBSD: citrus_mapper_std.h,v 1.1 2003/06/25 09:51:47 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_STD_H_ +#define _CITRUS_MAPPER_STD_H_ + +#include "citrus_mapper_std_local.h" + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_std); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_std_file.h b/lib/nbsd_libc/citrus/modules/citrus_mapper_std_file.h new file mode 100644 index 000000000..c9d17cab2 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_std_file.h @@ -0,0 +1,75 @@ +/* $NetBSD: citrus_mapper_std_file.h,v 1.3 2006/09/09 14:35:17 tnozaki Exp $ */ + +/*- + * Copyright (c)2003, 2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_STD_FILE_H_ +#define _CITRUS_MAPPER_STD_FILE_H_ + +#define _CITRUS_MAPPER_STD_MAGIC "MAPPER\0\0" + +#define _CITRUS_MAPPER_STD_SYM_TYPE "type" +#define _CITRUS_MAPPER_STD_SYM_INFO "info" +#define _CITRUS_MAPPER_STD_SYM_TABLE "table" + +#define _CITRUS_MAPPER_STD_TYPE_ROWCOL "rowcol" +struct _citrus_mapper_std_rowcol_info_x { + u_int32_t rcx_src_rowcol_bits; + u_int32_t rcx_dst_invalid; +#define _CITRUS_MAPPER_STD_ROWCOL_MAX 4 + struct { + u_int32_t begin; + u_int32_t end; + } __packed rcx_src_rowcol[_CITRUS_MAPPER_STD_ROWCOL_MAX]; + u_int32_t rcx_dst_unit_bits; + u_int32_t rcx_src_rowcol_len; +} __packed; +#define _CITRUS_MAPPER_STD_ROWCOL_INFO_SIZE 48 + +/* old file layout */ +struct _citrus_mapper_std_rowcol_info_compat_x { + u_int32_t rcx_src_col_bits; + u_int32_t rcx_dst_invalid; + u_int32_t rcx_src_row_begin; + u_int32_t rcx_src_row_end; + u_int32_t rcx_src_col_begin; + u_int32_t rcx_src_col_end; + u_int32_t rcx_dst_unit_bits; + u_int32_t rcx_pad; +} __packed; +#define _CITRUS_MAPPER_STD_ROWCOL_INFO_COMPAT_SIZE 32 + +/* rowcol oob extension info */ +#define _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ "rowcol_ext_ilseq" +struct _citrus_mapper_std_rowcol_ext_ilseq_info_x { +#define _CITRUS_MAPPER_STD_OOB_NONIDENTICAL 0 +#define _CITRUS_MAPPER_STD_OOB_ILSEQ 1 + u_int32_t eix_oob_mode; + u_int32_t eix_dst_ilseq; +} __packed; +#define _CITRUS_MAPPER_STD_ROWCOL_EXT_ILSEQ_SIZE 8 + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_std_local.h b/lib/nbsd_libc/citrus/modules/citrus_mapper_std_local.h new file mode 100644 index 000000000..631a296a3 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_std_local.h @@ -0,0 +1,70 @@ +/* $NetBSD: citrus_mapper_std_local.h,v 1.3 2006/09/09 14:35:17 tnozaki Exp $ */ + +/*- + * Copyright (c)2003, 2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_STD_LOCAL_H_ +#define _CITRUS_MAPPER_STD_LOCAL_H_ + +typedef u_int32_t (*_citrus_mapper_std_getvalfunc_t)(const void *, u_int32_t); + +struct _citrus_mapper_std_linear_zone { + _citrus_index_t begin; + _citrus_index_t end; + _citrus_index_t width; +}; +struct _citrus_mapper_std_rowcol { + struct _citrus_region rc_table; + size_t rc_src_rowcol_len; + struct _citrus_mapper_std_linear_zone + *rc_src_rowcol; + _citrus_index_t rc_src_rowcol_bits; + _citrus_index_t rc_src_rowcol_mask; + _citrus_index_t rc_dst_invalid; + _citrus_index_t rc_dst_unit_bits; + int rc_oob_mode; + _citrus_index_t rc_dst_ilseq; +}; + +struct _citrus_mapper_std; + +typedef int (*_citrus_mapper_std_convert_t)( + struct _citrus_mapper_std *__restrict, + _index_t *__restrict, _index_t, void *__restrict); +typedef void (*_citrus_mapper_std_uninit_t)(struct _citrus_mapper_std *); + +struct _citrus_mapper_std { + struct _citrus_region ms_file; + struct _citrus_db *ms_db; + _citrus_mapper_std_convert_t ms_convert; + _citrus_mapper_std_uninit_t ms_uninit; + union { + struct _citrus_mapper_std_rowcol rowcol; + } u; +#define ms_rowcol u.rowcol +}; + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_zone.c b/lib/nbsd_libc/citrus/modules/citrus_mapper_zone.c new file mode 100644 index 000000000..8eddefaf8 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_zone.c @@ -0,0 +1,401 @@ +/* $NetBSD: citrus_mapper_zone.c,v 1.4 2003/07/12 15:39:21 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mapper_zone.c,v 1.4 2003/07/12 15:39:21 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_zone.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_zone); +_CITRUS_MAPPER_DEF_OPS(mapper_zone); + + +/* ---------------------------------------------------------------------- */ + +struct _zone { + u_int32_t z_begin; + u_int32_t z_end; +}; + +struct _citrus_mapper_zone { + struct _zone mz_row; + struct _zone mz_col; + int mz_col_bits; + int32_t mz_row_offset; + int32_t mz_col_offset; +}; + +struct _parse_state { + enum { S_BEGIN, S_OFFSET } ps_state; + union { + u_int32_t u_imm; + int32_t s_imm; + struct _zone zone; + } u; +#define ps_u_imm u.u_imm +#define ps_s_imm u.s_imm +#define ps_zone u.zone + int ps_top; +}; + +int +_citrus_mapper_zone_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopsps_u_imm = strtoul(buf, &p, 0); + if ((p-buf) != i) + return T_ERR; + if (sign == '-') + ps->ps_u_imm = (unsigned long)-(long)ps->ps_u_imm; + return T_IMM; +} + +static int +get_tok(struct _memstream *ms, struct _parse_state *ps) +{ + int c; + +loop: + c = _memstream_peek(ms); + if (c==0x00) + return EOF; + if (_bcs_isspace(c)) { + _memstream_getc(ms); + goto loop; + } + + switch (ps->ps_state) { + case S_BEGIN: + switch (c) { + case ':': + case '-': + case '/': + _memstream_getc(ms); + return c; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return get_imm(ms, ps); + } + break; + case S_OFFSET: + switch (c) { + case '/': + _memstream_getc(ms); + return c; + case '+': + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return get_imm(ms, ps); + } + break; + } + return T_ERR; +} + +static int +parse_zone(struct _memstream *ms, struct _parse_state *ps, struct _zone *z) +{ + if (get_tok(ms, ps) != T_IMM) + return -1; + z->z_begin = ps->ps_u_imm; + if (get_tok(ms, ps) != '-') + return -1; + if (get_tok(ms, ps) != T_IMM) + return -1; + z->z_end = ps->ps_u_imm; + + if (z->z_begin > z->z_end) + return -1; + + return 0; +} + +static int +check_rowcol(struct _zone *z, int32_t ofs, uint32_t maxval) +{ + u_int32_t remain; + + if (maxval != 0 && z->z_end >= maxval) + return -1; + + if (ofs > 0) { + if (maxval == 0) { + /* this should 0x100000000 - z->z_end */ + if (z->z_end == 0) { + remain = 0xFFFFFFFF; + } else { + remain = 0xFFFFFFFF - z->z_end + 1; + } + } else + remain = maxval - z->z_end; + if ((u_int32_t)ofs > remain) + return -1; + } else if (ofs < 0) { + if (z->z_begin < (u_int32_t)-ofs) + return -1; + } + + return 0; +} + +static int +parse_var(struct _citrus_mapper_zone *mz, struct _memstream *ms) +{ + struct _parse_state ps; + int ret, isrc; + uint32_t rowmax, colmax; + + ps.ps_state = S_BEGIN; + + if (parse_zone(ms, &ps, &mz->mz_col)) + return -1; + + ret = get_tok(ms, &ps); + if (ret == '/') { + /* rowzone / colzone / bits */ + isrc = 1; + mz->mz_row = mz->mz_col; + + if (parse_zone(ms, &ps, &mz->mz_col)) + return -1; + if (get_tok(ms, &ps) != '/') + return -1; + if (get_tok(ms, &ps) != T_IMM) + return -1; + mz->mz_col_bits = ps.ps_u_imm; + if (mz->mz_col_bits<0 || mz->mz_col_bits>32) + return -1; + ret = get_tok(ms, &ps); + } else { + /* colzone */ + isrc = 0; + mz->mz_col_bits = 32; + mz->mz_row.z_begin = mz->mz_row.z_end = 0; + } + if (ret == ':') { + /* offset */ + ps.ps_state = S_OFFSET; + if (get_tok(ms, &ps) != T_IMM) + return -1; + mz->mz_col_offset = ps.ps_s_imm; + if (isrc) { + /* row/col */ + mz->mz_row_offset = mz->mz_col_offset; + if (get_tok(ms, &ps) != '/') + return -1; + if (get_tok(ms, &ps) != T_IMM) + return -1; + mz->mz_col_offset = ps.ps_s_imm; + } else + mz->mz_row_offset = 0; + ret = get_tok(ms, &ps); + } + if (ret != EOF) + return -1; + + /* sanity check */ + if (mz->mz_col_bits==32) + colmax = 0; + else + colmax = 1 << mz->mz_col_bits; + if (mz->mz_col_bits==0) + rowmax = 0; + else + rowmax = 1 << (32-mz->mz_col_bits); + if (check_rowcol(&mz->mz_col, mz->mz_col_offset, colmax)) + return -1; + if (check_rowcol(&mz->mz_row, mz->mz_row_offset, rowmax)) + return -1; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict dir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + struct _citrus_mapper_zone *mz; + struct _memstream ms; + struct _region r; + + _DIAGASSERT(cm && dir && mt); + + if (lenmtmz_col.z_begin = mz->mz_col.z_end = 0; + mz->mz_row.z_begin = mz->mz_row.z_end = 0; + mz->mz_col_bits = 0; + mz->mz_row_offset = 0; + mz->mz_col_offset = 0; + + _region_init(&r, (void *)var, lenvar); + _memstream_bind(&ms, &r); + if (parse_var(mz, &ms)) { + free(mz); + return EINVAL; + } + cm->cm_closure = mz; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_uninit(struct _citrus_mapper *cm) +{ +} + +static int +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_convert(struct _citrus_mapper * __restrict cm, + _citrus_index_t * __restrict dst, + _citrus_index_t src, void * __restrict ps) +{ + u_int32_t row, col; + struct _citrus_mapper_zone *mz = cm->cm_closure; + + if (mz->mz_col_bits == 32) { + col = src; + row = 0; + if (col < mz->mz_col.z_begin || col > mz->mz_col.z_end) + return _CITRUS_MAPPER_CONVERT_NONIDENTICAL; + if (mz->mz_col_offset>0) + col += (u_int32_t)mz->mz_col_offset; + else + col -= (u_int32_t)-mz->mz_col_offset; + *dst = col; + } else { + col = src & (((u_int32_t)1<mz_col_bits)-1); + row = src >> mz->mz_col_bits; + if (row < mz->mz_row.z_begin || row > mz->mz_row.z_end || + col < mz->mz_col.z_begin || col > mz->mz_col.z_end) + return _CITRUS_MAPPER_CONVERT_NONIDENTICAL; + if (mz->mz_col_offset>0) + col += (u_int32_t)mz->mz_col_offset; + else + col -= (u_int32_t)-mz->mz_col_offset; + if (mz->mz_row_offset>0) + row += (u_int32_t)mz->mz_row_offset; + else + row -= (u_int32_t)-mz->mz_row_offset; + *dst = col | (row << mz->mz_col_bits); + } + return _CITRUS_MAPPER_CONVERT_SUCCESS; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/nbsd_libc/citrus/modules/citrus_mapper_zone.h b/lib/nbsd_libc/citrus/modules/citrus_mapper_zone.h new file mode 100644 index 000000000..d975dd127 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mapper_zone.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_mapper_zone.h,v 1.1 2003/06/25 09:51:48 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MAPPER_ZONE_H_ +#define _CITRUS_MAPPER_ZONE_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_zone); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_mskanji.c b/lib/nbsd_libc/citrus/modules/citrus_mskanji.c new file mode 100644 index 000000000..c6011c441 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mskanji.c @@ -0,0 +1,544 @@ +/* $NetBSD: citrus_mskanji.c,v 1.13 2008/06/14 16:01:08 tnozaki Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +/* + * ja_JP.SJIS locale table for BSD4.4/rune + * version 1.0 + * (C) Sin'ichiro MIYATANI / Phase One, Inc + * May 12, 1995 + * + * 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 by Phase One, Inc. + * 4. The name of Phase One, Inc. 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_mskanji.c,v 1.13 2008/06/14 16:01:08 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_mskanji.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct _MSKanjiState { + char ch[2]; + int chlen; +} _MSKanjiState; + +typedef struct { + int mode; +#define MODE_JIS2004 1 +} _MSKanjiEncodingInfo; + +typedef struct { + _MSKanjiEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _MSKanjiState s_mblen; + _MSKanjiState s_mbrlen; + _MSKanjiState s_mbrtowc; + _MSKanjiState s_mbtowc; + _MSKanjiState s_mbsrtowcs; + _MSKanjiState s_wcrtomb; + _MSKanjiState s_wcsrtombs; + _MSKanjiState s_wctomb; + } states; +} _MSKanjiCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_MSKanji_##m +#define _ENCODING_INFO _MSKanjiEncodingInfo +#define _CTYPE_INFO _MSKanjiCTypeInfo +#define _ENCODING_STATE _MSKanjiState +#define _ENCODING_MB_CUR_MAX(_ei_) 2 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static int +_mskanji1(int c) +{ + + if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) + return 1; + else + return 0; +} + +static int +_mskanji2(int c) +{ + + if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc)) + return 1; + else + return 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei, + _MSKanjiState * __restrict s) +{ + s->chlen = 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _MSKanjiState * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei, + _MSKanjiState * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +/*ARGSUSED*/ +_citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, size_t n, + _MSKanjiState * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + int len; + int chlenbak; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_MSKanji_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + break; + default: + /* illegal state */ + goto encoding_error; + } + + len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1; + while (psenc->chlen < len) { + if (n < 1) + goto restart; + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + + *s = s0; + + switch (len) { + case 1: + wchar = psenc->ch[0] & 0xff; + break; + case 2: + if (!_mskanji2(psenc->ch[1] & 0xff)) + goto encoding_error; + wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); + break; + default: + /* illegal state */ + goto encoding_error; + } + + psenc->chlen = 0; + + if (pwc) + *pwc = wchar; + + if (!wchar) + *nresult = 0; + else + *nresult = len - chlenbak; + + return (0); + +encoding_error: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *nresult = (size_t)-2; + *s = s0; + return (0); +} + + +static int +_citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _MSKanjiState * __restrict psenc, + size_t * __restrict nresult) +{ + int ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + /* check invalid sequence */ + if (wc & ~0xffff) { + ret = EILSEQ; + goto err; + } + + if (wc & 0xff00) { + if (n < 2) { + ret = E2BIG; + goto err; + } + + s[0] = (wc >> 8) & 0xff; + s[1] = wc & 0xff; + if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) { + ret = EILSEQ; + goto err; + } + + *nresult = 2; + return 0; + } else { + if (n < 1) { + ret = E2BIG; + goto err; + } + + s[0] = wc & 0xff; + if (_mskanji1(s[0] & 0xff)) { + ret = EILSEQ; + goto err; + } + + *nresult = 1; + return 0; + } + +err: + *nresult = (size_t)-1; + return ret; +} + + +static __inline int +/*ARGSUSED*/ +_citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + _index_t row, col; + int offset; + + _DIAGASSERT(csid != NULL && idx != NULL); + + if ((_wc_t)wc < 0x80) { + /* ISO-646 */ + *csid = 0; + *idx = (_index_t)wc; + } else if ((_wc_t)wc < 0x100) { + /* KANA */ + *csid = 1; + *idx = (_index_t)wc & 0x7F; + } else { + /* Kanji (containing Gaiji zone) */ + /* + * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)): + * 0x8140 - 0x817E -> 0x2121 - 0x215F + * 0x8180 - 0x819E -> 0x2160 - 0x217E + * 0x819F - 0x81FC -> 0x2221 - 0x227E + * + * 0x8240 - 0x827E -> 0x2321 - 0x235F + * ... + * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E + * + * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F + * ... + * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E + * + * extended Gaiji zone: + * 0xF040 - 0xFCFC + * + * JIS X0213-plane2: + * 0xF040 - 0xF09E -> 0x2121 - 0x217E + * 0xF140 - 0xF19E -> 0x2321 - 0x237E + * ... + * 0xF240 - 0xF29E -> 0x2521 - 0x257E + * + * 0xF09F - 0xF0FC -> 0x2821 - 0x287E + * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E + * ... + * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E + * + * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E + * ... + * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E + */ + row = ((_wc_t)wc >> 8) & 0xFF; + col = (_wc_t)wc & 0xFF; + if (!_mskanji1(row) || !_mskanji2(col)) + return EILSEQ; + if ((ei->mode & MODE_JIS2004) == 0 || row < 0xF0) { + *csid = 2; + offset = 0x81; + } else { + *csid = 3; + if ((_wc_t)wc <= 0xF49E) { + offset = (_wc_t)wc >= 0xF29F || + ((_wc_t)wc >= 0xF09F && (_wc_t)wc <= 0xF0FC) + ? 0xED : 0xF0; + } else + offset = 0xCE; + } + row -= offset; + if (row >= 0x5F) + row -= 0x40; + row = row * 2 + 0x21; + col -= 0x1F; + if (col >= 0x61) + col -= 1; + if (col > 0x7E) { + row += 1; + col -= 0x5E; + } + *idx = ((_index_t)row << 8) | col; + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + u_int32_t row, col; + int offset; + + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: + /* ISO-646 */ + if (idx >= 0x80) + return EILSEQ; + *wc = (wchar_t)idx; + break; + case 1: + /* kana */ + if (idx >= 0x80) + return EILSEQ; + *wc = (wchar_t)idx + 0x80; + break; + case 3: + if ((ei->mode & MODE_JIS2004) == 0) + return EILSEQ; + /*FALLTHROUGH*/ + case 2: + /* kanji */ + row = (idx >> 8); + if (row < 0x21) + return EILSEQ; + if (csid == 3) { + if (row <= 0x2F) + offset = (row == 0x22 || row >= 0x26) + ? 0xED : 0xF0; + else if (row >= 0x4D && row <= 0x7E) + offset = 0xCE; + else + return EILSEQ; + } else { + if (row > 0x97) + return EILSEQ; + offset = (row < 0x5F) ? 0x81 : 0xC1; + } + col = idx & 0xFF; + if (col < 0x21 || col > 0x7E) + return EILSEQ; + row -= 0x21; col -= 0x21; + if ((row & 1) == 0) { + col += 0x40; + if (col >= 0x7F) + col += 1; + } else + col += 0x9F; + row = row / 2 + offset; + *wc = ((wchar_t)row << 8) | col; + break; + default: + return EILSEQ; + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * __restrict ei, + _MSKanjiState * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei, + const void * __restrict var, + size_t lenvar) +{ + const char *p; + + _DIAGASSERT(ei != NULL); + + p = var; +#define MATCH(x, act) \ +do { \ + if (lenvar >= (sizeof(#x)-1) && \ + _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ + act; \ + lenvar -= sizeof(#x)-1; \ + p += sizeof(#x)-1; \ + } \ +} while (/*CONSTCOND*/0) + memset((void *)ei, 0, sizeof(*ei)); + while (lenvar > 0) { + switch (_bcs_toupper(*p)) { + case 'J': + MATCH(JIS2004, ei->mode |= MODE_JIS2004); + break; + } + ++p; + --lenvar; + } + + return 0; +} + +static void +_citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei) +{ +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(MSKanji); +_CITRUS_CTYPE_DEF_OPS(MSKanji); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(MSKanji); +_CITRUS_STDENC_DEF_OPS(MSKanji); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_mskanji.h b/lib/nbsd_libc/citrus/modules/citrus_mskanji.h new file mode 100644 index 000000000..90aebe1b1 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_mskanji.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_mskanji.h,v 1.2 2003/06/25 09:51:48 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_MSKANJI_H_ +#define _CITRUS_MSKANJI_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(MSKanji); +_CITRUS_STDENC_GETOPS_FUNC(MSKanji); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_ues.c b/lib/nbsd_libc/citrus/modules/citrus_ues.c new file mode 100644 index 000000000..08b23d506 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_ues.c @@ -0,0 +1,472 @@ +/* $NetBSD: citrus_ues.c,v 1.2 2010/12/07 22:01:22 joerg Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_ues.c,v 1.2 2010/12/07 22:01:22 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_ues.h" + +typedef struct { + int mode; +#define MODE_C99 1 + size_t mb_cur_max; +} _UESEncodingInfo; + +typedef struct { + int chlen; + char ch[12]; +} _UESState; + +typedef struct { + _UESEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _UESState s_mblen; + _UESState s_mbrlen; + _UESState s_mbrtowc; + _UESState s_mbtowc; + _UESState s_mbsrtowcs; + _UESState s_wcrtomb; + _UESState s_wcsrtombs; + _UESState s_wctomb; + } states; +} _UESCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_UES_##m +#define _ENCODING_INFO _UESEncodingInfo +#define _CTYPE_INFO _UESCTypeInfo +#define _ENCODING_STATE _UESState +#define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline void +/*ARGSUSED*/ +_citrus_UES_init_state(_UESEncodingInfo * __restrict ei, + _UESState * __restrict psenc) +{ + psenc->chlen = 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_UES_pack_state(_UESEncodingInfo * __restrict ei, + void *__restrict pspriv, const _UESState * __restrict psenc) +{ + /* ei seem to be unused */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(psenc != NULL); + + memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UES_unpack_state(_UESEncodingInfo * __restrict ei, + _UESState * __restrict psenc, const void * __restrict pspriv) +{ + /* ei seem to be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)psenc, pspriv, sizeof(*psenc)); +} + +static __inline int +to_int(int ch) +{ + if (ch >= '0' && ch <= '9') + return ch - '0'; + else if (ch >= 'A' && ch <= 'F') + return (ch - 'A') + 10; + else if (ch >= 'a' && ch <= 'f') + return (ch - 'a') + 10; + return -1; +} + +#define ESCAPE '\\' +#define UCS2_ESC 'u' +#define UCS4_ESC 'U' + +#define UCS2_BIT 16 +#define UCS4_BIT 32 +#define BMP_MAX UINT32_C(0xFFFF) +#define UCS2_MAX UINT32_C(0x10FFFF) +#define UCS4_MAX UINT32_C(0x7FFFFFFF) + +static const char *xdig = "0123456789abcdef"; + +static __inline int +to_str(char *s, wchar_t wc, int bit) +{ + char *p; + + p = s; + *p++ = ESCAPE; + switch (bit) { + case UCS2_BIT: + *p++ = UCS2_ESC; + break; + case UCS4_BIT: + *p++ = UCS4_ESC; + break; + default: + abort(); + } + do { + *p++ = xdig[(wc >> (bit -= 4)) & 0xF]; + } while (bit > 0); + return p - s; +} + +static __inline int +is_hi_surrogate(wchar_t wc) +{ + return wc >= 0xD800 && wc <= 0xDBFF; +} + +static __inline int +is_lo_surrogate(wchar_t wc) +{ + return wc >= 0xDC00 && wc <= 0xDFFF; +} + +static __inline wchar_t +surrogate_to_ucs(wchar_t hi, wchar_t lo) +{ + _DIAGASSERT(is_hi_surrogate(hi)); + _DIAGASSERT(is_lo_surrogate(lo)); + + hi -= 0xD800; + lo -= 0xDC00; + return (hi << 10 | lo) + 0x10000; +} + +static __inline void +ucs_to_surrogate(wchar_t wc, wchar_t * __restrict hi, wchar_t * __restrict lo) +{ + _DIAGASSERT(hi != NULL); + _DIAGASSERT(lo != NULL); + _DIAGASSERT(wc >= 0x10000); + + wc -= 0x10000; + *hi = (wc >> 10) + 0xD800; + *lo = (wc & 0x3FF) + 0xDC00; +} + +static __inline int +is_basic(wchar_t wc) +{ + return (uint32_t)wc <= 0x9F && + wc != 0x24 && wc != 0x40 && wc != 0x60; +} + +static int +_citrus_UES_mbrtowc_priv(_UESEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char ** __restrict s, size_t n, + _UESState * __restrict psenc, size_t * __restrict nresult) +{ + const char *s0; + int ch, head, tail, i, num; + wchar_t hi, wc; + + _DIAGASSERT(ei != NULL); + /* pwc may be null */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (*s == NULL) { + _citrus_UES_init_state(ei, psenc); + *nresult = 0; + return 0; + } + s0 = *s; + + hi = (wchar_t)0; + tail = 0; + +surrogate: + wc = (wchar_t)0; + head = tail; + if (psenc->chlen == head) { + if (n-- < 1) + goto restart; + psenc->ch[psenc->chlen++] = *s0++; + } + ch = (unsigned char)psenc->ch[head++]; + if (ch == ESCAPE) { + if (psenc->chlen == head) { + if (n-- < 1) + goto restart; + psenc->ch[psenc->chlen++] = *s0++; + } + switch (psenc->ch[head]) { + case UCS2_ESC: + tail += 6; + break; + case UCS4_ESC: + if (ei->mode & MODE_C99) { + tail = 10; + break; + } + /*FALLTHROUGH*/ + default: + tail = 0; + } + ++head; + } + for (; head < tail; ++head) { + if (psenc->chlen == head) { + if (n-- < 1) { +restart: + *s = s0; + *nresult = (size_t)-2; + return 0; + } + psenc->ch[psenc->chlen++] = *s0++; + } + num = to_int((int)(unsigned char)psenc->ch[head]); + if (num < 0) { + tail = 0; + break; + } + wc = (wc << 4) | num; + } + head = 0; + switch (tail) { + case 0: + break; + case 6: + if (hi != (wchar_t)0) + break; + if ((ei->mode & MODE_C99) == 0) { + if (is_hi_surrogate(wc) != 0) { + hi = wc; + goto surrogate; + } + if ((uint32_t)wc <= 0x7F /* XXX */ || + is_lo_surrogate(wc) != 0) + break; + goto done; + } + /*FALLTHROUGH*/ + case 10: + if (is_basic(wc) == 0 && (uint32_t)wc <= UCS4_MAX && + is_hi_surrogate(wc) == 0 && is_lo_surrogate(wc) == 0) + goto done; + *nresult = (size_t)-1; + return EILSEQ; + case 12: + if (is_lo_surrogate(wc) == 0) + break; + wc = surrogate_to_ucs(hi, wc); + goto done; + } + ch = (unsigned char)psenc->ch[0]; + head = psenc->chlen; + if (--head > 0) + memmove(&psenc->ch[0], &psenc->ch[1], head); + wc = (wchar_t)ch; +done: + psenc->chlen = head; + if (pwc != NULL) + *pwc = wc; + *nresult = (size_t)((wc == 0) ? 0 : (s0 - *s)); + *s = s0; + + return 0; +} + +static int +_citrus_UES_wcrtomb_priv(_UESEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _UESState * __restrict psenc, size_t * __restrict nresult) +{ + wchar_t hi, lo; + + if (psenc->chlen != 0) + return EINVAL; + + if ((ei->mode & MODE_C99) ? is_basic(wc) : (uint32_t)wc <= 0x7F) { + if (n-- < 1) + goto e2big; + psenc->ch[psenc->chlen++] = (char)wc; + } else if ((uint32_t)wc <= BMP_MAX) { + if (n < 6) + goto e2big; + psenc->chlen = to_str(&psenc->ch[0], wc, UCS2_BIT); + } else if ((ei->mode & MODE_C99) == 0 && (uint32_t)wc <= UCS2_MAX) { + if (n < 12) + goto e2big; + ucs_to_surrogate(wc, &hi, &lo); + psenc->chlen += to_str(&psenc->ch[0], hi, UCS2_BIT); + psenc->chlen += to_str(&psenc->ch[6], lo, UCS2_BIT); + } else if ((ei->mode & MODE_C99) && (uint32_t)wc <= UCS4_MAX) { + if (n < 10) + goto e2big; + psenc->chlen = to_str(&psenc->ch[0], wc, UCS4_BIT); + } else { + *nresult = (size_t)-1; + return EILSEQ; + } + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + psenc->chlen = 0; + + return 0; + +e2big: + *nresult = (size_t)-1; + return E2BIG; +} + +/*ARGSUSED*/ +static int +_citrus_UES_stdenc_wctocs(_UESEncodingInfo * __restrict ei, + _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) +{ + /* ei seem to be unused */ + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + *csid = 0; + *idx = (_index_t)wc; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_UES_stdenc_cstowc(_UESEncodingInfo * __restrict ei, + wchar_t * __restrict wc, _csid_t csid, _index_t idx) +{ + /* ei seem to be unused */ + _DIAGASSERT(wc != NULL); + + if (csid != 0) + return EILSEQ; + *wc = (wchar_t)idx; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_UES_stdenc_get_state_desc_generic(_UESEncodingInfo * __restrict ei, + _UESState * __restrict psenc, int * __restrict rstate) +{ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(rstate != NULL); + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; /* XXX */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_UES_encoding_module_uninit(_UESEncodingInfo *ei) +{ + /* ei seems to be unused */ +} + +static int +/*ARGSUSED*/ +_citrus_UES_encoding_module_init(_UESEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + const char *p; + + _DIAGASSERT(ei != NULL); + + p = var; +#define MATCH(x, act) \ +do { \ + if (lenvar >= (sizeof(#x)-1) && \ + _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ + act; \ + lenvar -= sizeof(#x)-1; \ + p += sizeof(#x)-1; \ + } \ +} while (/*CONSTCOND*/0) + memset((void *)ei, 0, sizeof(*ei)); + while (lenvar > 0) { + switch (_bcs_toupper(*p)) { + case 'C': + MATCH(C99, ei->mode |= MODE_C99); + break; + } + ++p; + --lenvar; + } + ei->mb_cur_max = (ei->mode & MODE_C99) ? 10 : 12; + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(UES); +_CITRUS_CTYPE_DEF_OPS(UES); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(UES); +_CITRUS_STDENC_DEF_OPS(UES); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_ues.h b/lib/nbsd_libc/citrus/modules/citrus_ues.h new file mode 100644 index 000000000..0df7f27a0 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_ues.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_ues.h,v 1.1 2006/11/13 15:16:31 tnozaki Exp $ */ + +/*- + * Copyright (c)2004, 2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_UES_H_ +#define _CITRUS_UES_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(UES); +_CITRUS_STDENC_GETOPS_FUNC(UES); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_utf1632.c b/lib/nbsd_libc/citrus/modules/citrus_utf1632.c new file mode 100644 index 000000000..e582fd60b --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_utf1632.c @@ -0,0 +1,486 @@ +/* $NetBSD: citrus_utf1632.c,v 1.11 2010/03/20 18:15:32 tnozaki Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_utf1632.c,v 1.11 2010/03/20 18:15:32 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_stdenc.h" +#include "citrus_bcs.h" + +#include "citrus_utf1632.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + u_int8_t ch[4]; + int chlen; + int current_endian; +} _UTF1632State; + +typedef struct { + int preffered_endian; + unsigned int cur_max; +#define _ENDIAN_UNKNOWN 0 +#define _ENDIAN_BIG 1 +#define _ENDIAN_LITTLE 2 + u_int32_t mode; +#define _MODE_UTF32 0x00000001U +#define _MODE_FORCE_ENDIAN 0x00000002U +} _UTF1632EncodingInfo; + +#define _FUNCNAME(m) _citrus_UTF1632_##m +#define _ENCODING_INFO _UTF1632EncodingInfo +#define _ENCODING_STATE _UTF1632State +#define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max) +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline void +/*ARGSUSED*/ +_citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei, _UTF1632State *s) +{ + memset(s, 0, sizeof(*s)); +} + +static int +_citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc, + const char **s, size_t n, _UTF1632State *psenc, + size_t *nresult) +{ + int chlenbak, endian, needlen; + wchar_t wc; + size_t result; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_UTF1632_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + result = 0; + chlenbak = psenc->chlen; + +refetch: + if ((ei->mode & _MODE_UTF32) != 0 || chlenbak>=2) + needlen = 4; + else + needlen = 2; + + while (chlenbak < needlen) { + if (n==0) + goto restart; + psenc->ch[chlenbak++] = *s0++; + n--; + result++; + } + + if (psenc->current_endian == _ENDIAN_UNKNOWN) { + if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) { + /* judge endian marker */ + if ((ei->mode & _MODE_UTF32) == 0) { + /* UTF16 */ + if (psenc->ch[0]==0xFE && psenc->ch[1]==0xFF) { + psenc->current_endian = _ENDIAN_BIG; + chlenbak = 0; + goto refetch; + } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE) { + psenc->current_endian = _ENDIAN_LITTLE; + chlenbak = 0; + goto refetch; + } + } else { + /* UTF32 */ + if (psenc->ch[0]==0x00 && psenc->ch[1]==0x00 && + psenc->ch[2]==0xFE && psenc->ch[3]==0xFF) { + psenc->current_endian = _ENDIAN_BIG; + chlenbak = 0; + goto refetch; + } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE && + psenc->ch[2]==0x00 && psenc->ch[3]==0x00) { + psenc->current_endian = _ENDIAN_LITTLE; + chlenbak = 0; + goto refetch; + } + } + } + psenc->current_endian = ei->preffered_endian; + } + endian = psenc->current_endian; + + /* get wc */ + if ((ei->mode & _MODE_UTF32) == 0) { + /* UTF16 */ + if (needlen==2) { + switch (endian) { + case _ENDIAN_LITTLE: + wc = (psenc->ch[0] | + ((wchar_t)psenc->ch[1] << 8)); + break; + case _ENDIAN_BIG: + wc = (psenc->ch[1] | + ((wchar_t)psenc->ch[0] << 8)); + break; + default: + goto ilseq; + } + if (wc >= 0xD800 && wc <= 0xDBFF) { + /* surrogate high */ + needlen=4; + goto refetch; + } + } else { + /* surrogate low */ + wc -= 0xD800; /* wc : surrogate high (see above) */ + wc <<= 10; + switch (endian) { + case _ENDIAN_LITTLE: + if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF) + goto ilseq; + wc |= psenc->ch[2]; + wc |= (wchar_t)(psenc->ch[3] & 3) << 8; + break; + case _ENDIAN_BIG: + if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF) + goto ilseq; + wc |= psenc->ch[3]; + wc |= (wchar_t)(psenc->ch[2] & 3) << 8; + break; + default: + goto ilseq; + } + wc += 0x10000; + } + } else { + /* UTF32 */ + switch (endian) { + case _ENDIAN_LITTLE: + wc = (psenc->ch[0] | + ((wchar_t)psenc->ch[1] << 8) | + ((wchar_t)psenc->ch[2] << 16) | + ((wchar_t)psenc->ch[3] << 24)); + break; + case _ENDIAN_BIG: + wc = (psenc->ch[3] | + ((wchar_t)psenc->ch[2] << 8) | + ((wchar_t)psenc->ch[1] << 16) | + ((wchar_t)psenc->ch[0] << 24)); + break; + default: + goto ilseq; + } + if (wc >= 0xD800 && wc <= 0xDFFF) + goto ilseq; + } + + + *pwc = wc; + psenc->chlen = 0; + *nresult = result; + *s = s0; + + return (0); + +ilseq: + *nresult = (size_t)-1; + psenc->chlen = 0; + return (EILSEQ); + +restart: + *nresult = (size_t)-2; + psenc->chlen = chlenbak; + *s = s0; + return (0); +} + +static int +_citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n, + wchar_t wc, _UTF1632State *psenc, + size_t *nresult) +{ + int ret; + wchar_t wc2; + static const char _bom[4] = { +#if BYTE_ORDER == BIG_ENDIAN + 0x00, 0x00, 0xFE, 0xFF, +#else + 0xFF, 0xFE, 0x00, 0x00, +#endif + }; + const char *bom = &_bom[0]; + size_t cnt; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + cnt = (size_t)0; + if (psenc->current_endian == _ENDIAN_UNKNOWN) { + if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) { + if (ei->mode & _MODE_UTF32) { + cnt = 4; + } else { + cnt = 2; +#if BYTE_ORDER == BIG_ENDIAN + bom += 2; +#endif + } + if (n < cnt) + goto e2big; + memcpy(s, bom, cnt); + s += cnt, n -= cnt; + } + psenc->current_endian = ei->preffered_endian; + } + + wc2 = 0; + if ((ei->mode & _MODE_UTF32)==0) { + /* UTF16 */ + if (wc>0xFFFF) { + /* surrogate */ + if (wc>0x10FFFF) + goto ilseq; + if (n < 4) + goto e2big; + cnt += 4; + wc -= 0x10000; + wc2 = (wc & 0x3FF) | 0xDC00; + wc = (wc>>10) | 0xD800; + } else { + if (n < 2) + goto e2big; + cnt += 2; + } + +surrogate: + switch (psenc->current_endian) { + case _ENDIAN_BIG: + s[1] = wc; + s[0] = (wc >>= 8); + break; + case _ENDIAN_LITTLE: + s[0] = wc; + s[1] = (wc >>= 8); + break; + } + if (wc2!=0) { + wc = wc2; + wc2 = 0; + s += 2; + goto surrogate; + } + } else { + /* UTF32 */ + if (wc >= 0xD800 && wc <= 0xDFFF) + goto ilseq; + if (n < 4) + goto e2big; + cnt += 4; + switch (psenc->current_endian) { + case _ENDIAN_BIG: + s[3] = wc; + s[2] = (wc >>= 8); + s[1] = (wc >>= 8); + s[0] = (wc >>= 8); + break; + case _ENDIAN_LITTLE: + s[0] = wc; + s[1] = (wc >>= 8); + s[2] = (wc >>= 8); + s[3] = (wc >>= 8); + break; + } + } + *nresult = cnt; + + return 0; + +ilseq: + *nresult = (size_t)-1; + return EILSEQ; +e2big: + *nresult = (size_t)-1; + return E2BIG; +} + +static void +parse_variable(_UTF1632EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ +#define MATCH(x, act) \ +do { \ + if (lenvar >= (sizeof(#x)-1) && \ + _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ + act; \ + lenvar -= sizeof(#x)-1; \ + p += sizeof(#x)-1; \ + } \ +} while (/*CONSTCOND*/0) + const char *p; + p = var; + while (lenvar>0) { + switch (*p) { + case 'B': + case 'b': + MATCH(big, ei->preffered_endian = _ENDIAN_BIG); + break; + case 'L': + case 'l': + MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE); + break; + case 'F': + case 'f': + MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN); + break; + case 'U': + case 'u': + MATCH(utf32, ei->mode |= _MODE_UTF32); + break; + } + p++; + lenvar--; + } +} + +static int +/*ARGSUSED*/ +_citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei, + const void * __restrict var, + size_t lenvar) +{ + _DIAGASSERT(ei != NULL); + + memset((void *)ei, 0, sizeof(*ei)); + + parse_variable(ei, var, lenvar); + + if ((ei->mode&_MODE_UTF32)==0) + ei->cur_max = 6; /* endian + surrogate */ + else + ei->cur_max = 8; /* endian + normal */ + + if (ei->preffered_endian == _ENDIAN_UNKNOWN) { +#if BYTE_ORDER == BIG_ENDIAN + ei->preffered_endian = _ENDIAN_BIG; +#else + ei->preffered_endian = _ENDIAN_LITTLE; +#endif + } + + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei) +{ +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, + _wc_t wc) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + *csid = 0; + *idx = (_index_t)wc; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei, + _wc_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(wc != NULL); + + if (csid != 0) + return (EILSEQ); + + *wc = (_wc_t)idx; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo * __restrict ei, + _UTF1632State * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(UTF1632); +_CITRUS_STDENC_DEF_OPS(UTF1632); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_utf1632.h b/lib/nbsd_libc/citrus/modules/citrus_utf1632.h new file mode 100644 index 000000000..0fac6f312 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_utf1632.h @@ -0,0 +1,36 @@ +/* $NetBSD: citrus_utf1632.h,v 1.1 2003/06/25 09:51:49 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_UTF1632_H_ +#define _CITRUS_UTF1632_H_ + +__BEGIN_DECLS +_CITRUS_STDENC_GETOPS_FUNC(UTF1632); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_utf7.c b/lib/nbsd_libc/citrus/modules/citrus_utf7.c new file mode 100644 index 000000000..fabb048b0 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_utf7.c @@ -0,0 +1,571 @@ +/* $NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki Exp $ */ + +/*- + * Copyright (c)2004, 2005 Citrus Project, + * 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 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 +#if defined(LIB_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki Exp $"); +#endif /* LIB_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_utf7.h" + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + uint16_t cell[0x80]; +#define EI_MASK UINT16_C(0xff) +#define EI_DIRECT UINT16_C(0x100) +#define EI_OPTION UINT16_C(0x200) +#define EI_SPACE UINT16_C(0x400) +} _UTF7EncodingInfo; + +typedef struct { + unsigned int + mode: 1, /* whether base64 mode */ + bits: 4, /* need to hold 0 - 15 */ + cache: 22, /* 22 = BASE64_BIT + UTF16_BIT */ + surrogate: 1; /* whether surrogate pair or not */ + int chlen; + char ch[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */ +} _UTF7State; + +typedef struct { + _UTF7EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _UTF7State s_mblen; + _UTF7State s_mbrlen; + _UTF7State s_mbrtowc; + _UTF7State s_mbtowc; + _UTF7State s_mbsrtowcs; + _UTF7State s_wcrtomb; + _UTF7State s_wcsrtombs; + _UTF7State s_wctomb; + } states; +} _UTF7CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_UTF7_##m +#define _ENCODING_INFO _UTF7EncodingInfo +#define _CTYPE_INFO _UTF7CTypeInfo +#define _ENCODING_STATE _UTF7State +#define _ENCODING_MB_CUR_MAX(_ei_) 4 +#define _ENCODING_IS_STATE_DEPENDENT 1 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline void +/*ARGSUSED*/ +_citrus_UTF7_init_state(_UTF7EncodingInfo * __restrict ei, + _UTF7State * __restrict s) +{ + /* ei appears to be unused */ + _DIAGASSERT(s != NULL); + + memset((void *)s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF7_pack_state(_UTF7EncodingInfo * __restrict ei, + void *__restrict pspriv, const _UTF7State * __restrict s) +{ + /* ei seem to be unused */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(s != NULL); + + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF7_unpack_state(_UTF7EncodingInfo * __restrict ei, + _UTF7State * __restrict s, const void * __restrict pspriv) +{ + /* ei seem to be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static const char base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +static const char direct[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789(),-./:?"; + +static const char option[] = "!\"#$%&';<=>@[]^_`{|}"; +static const char spaces[] = " \t\r\n"; + +#define BASE64_BIT 6 +#define UTF16_BIT 16 + +#define BASE64_MAX 0x3f +#define UTF16_MAX UINT16_C(0xffff) +#define UTF32_MAX UINT32_C(0x10ffff) + +#define BASE64_IN '+' +#define BASE64_OUT '-' + +#define SHIFT7BIT(c) ((c) >> 7) +#define ISSPECIAL(c) ((c) == '\0' || (c) == BASE64_IN) + +#define FINDLEN(ei, c) \ + (SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1)) + +#define ISDIRECT(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \ + ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE))) + +#define ISSAFE(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \ + (c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE)))) + +/* surrogate pair */ +#define SRG_BASE UINT32_C(0x10000) +#define HISRG_MIN UINT16_C(0xd800) +#define HISRG_MAX UINT16_C(0xdbff) +#define LOSRG_MIN UINT16_C(0xdc00) +#define LOSRG_MAX UINT16_C(0xdfff) + +static int +_citrus_UTF7_mbtoutf16(_UTF7EncodingInfo * __restrict ei, + uint16_t * __restrict u16, const char ** __restrict s, size_t n, + _UTF7State * __restrict psenc, size_t * __restrict nresult) +{ + _UTF7State sv; + const char *s0; + int i, done, len; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL && *s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + sv = *psenc; + + for (i = 0, done = 0; done == 0; i++) { + _DIAGASSERT(i <= psenc->chlen); + if (i == psenc->chlen) { + if (n-- < 1) { + *nresult = (size_t)-2; + *s = s0; + sv.chlen = psenc->chlen; + *psenc = sv; + return 0; + } + psenc->ch[psenc->chlen++] = *s0++; + } + if (SHIFT7BIT((int)psenc->ch[i])) + goto ilseq; + if (!psenc->mode) { + if (psenc->bits > 0 || psenc->cache > 0) + return EINVAL; + if (psenc->ch[i] == BASE64_IN) { + psenc->mode = 1; + } else { + if (!ISDIRECT(ei, (int)psenc->ch[i])) + goto ilseq; + *u16 = (uint16_t)psenc->ch[i]; + done = 1; + continue; + } + } else { + if (psenc->ch[i] == BASE64_OUT && psenc->cache == 0) { + psenc->mode = 0; + *u16 = (uint16_t)BASE64_IN; + done = 1; + continue; + } + len = FINDLEN(ei, (int)psenc->ch[i]); + if (len < 0) { + if (psenc->bits >= BASE64_BIT) + return EINVAL; + psenc->mode = 0; + psenc->bits = psenc->cache = 0; + if (psenc->ch[i] != BASE64_OUT) { + if (!ISDIRECT(ei, (int)psenc->ch[i])) + goto ilseq; + *u16 = (uint16_t)psenc->ch[i]; + done = 1; + } + } else { + psenc->cache = + (psenc->cache << BASE64_BIT) | len; + switch (psenc->bits) { + case 0: case 2: case 4: case 6: case 8: + psenc->bits += BASE64_BIT; + break; + case 10: case 12: case 14: + psenc->bits -= (UTF16_BIT - BASE64_BIT); + *u16 = (psenc->cache >> psenc->bits) + & UTF16_MAX; + done = 1; + break; + default: + return EINVAL; + } + } + } + } + + if (psenc->chlen > i) + return EINVAL; + psenc->chlen = 0; + *nresult = (size_t)((*u16 == 0) ? 0 : s0 - *s); + *s = s0; + + return 0; + +ilseq: + *nresult = (size_t)-1; + return EILSEQ; +} + +static int +_citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char ** __restrict s, size_t n, + _UTF7State * __restrict psenc, size_t * __restrict nresult) +{ + const char *s0; + uint32_t u32; + uint16_t hi, lo; + size_t siz, nr; + int err; + + _DIAGASSERT(ei != NULL); + /* pwc may be null */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + if (*s == NULL) { + _citrus_UTF7_init_state(ei, psenc); + *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT; + return 0; + } + s0 = *s; + if (psenc->surrogate) { + hi = (psenc->cache >> 2) & UTF16_MAX; + if (hi < HISRG_MIN || hi > HISRG_MAX) + return EINVAL; + siz = 0; + } else { + err = _citrus_UTF7_mbtoutf16(ei, &hi, &s0, n, psenc, &nr); + if (nr == (size_t)-1 || nr == (size_t)-2) { + *nresult = nr; + return err; + } + if (err != 0) + return err; + n -= nr; + siz = nr; + if (hi < HISRG_MIN || hi > HISRG_MAX) { + u32 = (uint32_t)hi; + goto done; + } + psenc->surrogate = 1; + } + err = _citrus_UTF7_mbtoutf16(ei, &lo, &s0, n, psenc, &nr); + if (nr == (size_t)-1 || nr == (size_t)-2) { + *nresult = nr; + return err; + } + if (err != 0) + return err; + hi -= HISRG_MIN; + lo -= LOSRG_MIN; + u32 = (hi << 10 | lo) + SRG_BASE; + siz += nr; +done: + *s = s0; + if (pwc != NULL) + *pwc = (wchar_t)u32; + if (u32 == (uint32_t)0) { + *nresult = (size_t)0; + _citrus_UTF7_init_state(ei, psenc); + } else { + *nresult = siz; + psenc->surrogate = 0; + } + return err; +} + +static int +_citrus_UTF7_utf16tomb(_UTF7EncodingInfo * __restrict ei, + char * __restrict s, size_t n, uint16_t u16, + _UTF7State * __restrict psenc, size_t * __restrict nresult) +{ + int bits, i; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + + if (psenc->chlen != 0 || psenc->bits > BASE64_BIT) + return EINVAL; + + if (ISSAFE(ei, u16)) { + if (psenc->mode) { + if (psenc->bits > 0) { + bits = BASE64_BIT - psenc->bits; + i = (psenc->cache << bits) & BASE64_MAX; + psenc->ch[psenc->chlen++] = base64[i]; + psenc->bits = psenc->cache = 0; + } + if (u16 == BASE64_OUT || FINDLEN(ei, u16) >= 0) + psenc->ch[psenc->chlen++] = BASE64_OUT; + psenc->mode = 0; + } + if (psenc->bits != 0) + return EINVAL; + psenc->ch[psenc->chlen++] = (char)u16; + if (u16 == BASE64_IN) + psenc->ch[psenc->chlen++] = BASE64_OUT; + } else { + if (!psenc->mode) { + if (psenc->bits > 0) + return EINVAL; + psenc->ch[psenc->chlen++] = BASE64_IN; + psenc->mode = 1; + } + psenc->cache = (psenc->cache << UTF16_BIT) | u16; + bits = UTF16_BIT + psenc->bits; + psenc->bits = bits % BASE64_BIT; + while ((bits -= BASE64_BIT) >= 0) { + i = (psenc->cache >> bits) & BASE64_MAX; + psenc->ch[psenc->chlen++] = base64[i]; + } + } + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + psenc->chlen = 0; + + return 0; +} + +static int +_citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wchar, + _UTF7State * __restrict psenc, size_t * __restrict nresult) +{ + uint32_t u32; + uint16_t u16[2]; + int err, len, i; + size_t siz, nr; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + u32 = (uint32_t)wchar; + if (u32 <= UTF16_MAX) { + u16[0] = (uint16_t)u32; + len = 1; + } else if (u32 <= UTF32_MAX) { + u32 -= SRG_BASE; + u16[0] = (u32 >> 10) + HISRG_MIN; + u16[1] = ((uint16_t)(u32 & UINT32_C(0x3ff))) + LOSRG_MIN; + len = 2; + } else { + *nresult = (size_t)-1; + return EILSEQ; + } + siz = 0; + for (i = 0; i < len; ++i) { + err = _citrus_UTF7_utf16tomb(ei, s, n, u16[i], psenc, &nr); + if (err != 0) + return err; /* XXX: state has been modified */ + s += nr; + n -= nr; + siz += nr; + } + *nresult = siz; + + return 0; +} + +static int +/* ARGSUSED */ +_citrus_UTF7_put_state_reset(_UTF7EncodingInfo * __restrict ei, + char * __restrict s, size_t n, _UTF7State * __restrict psenc, + size_t * __restrict nresult) +{ + int bits, pos; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (psenc->chlen != 0 || psenc->bits > BASE64_BIT || psenc->surrogate) + return EINVAL; + + if (psenc->mode) { + if (psenc->bits > 0) { + if (n-- < 1) + return E2BIG; + bits = BASE64_BIT - psenc->bits; + pos = (psenc->cache << bits) & BASE64_MAX; + psenc->ch[psenc->chlen++] = base64[pos]; + psenc->ch[psenc->chlen++] = BASE64_OUT; + psenc->bits = psenc->cache = 0; + } + psenc->mode = 0; + } + if (psenc->bits != 0) + return EINVAL; + if (n-- < 1) + return E2BIG; + + _DIAGASSERT(n >= psenc->chlen); + *nresult = (size_t)psenc->chlen; + if (psenc->chlen > 0) { + memcpy(s, psenc->ch, psenc->chlen); + psenc->chlen = 0; + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + /* ei seem to be unused */ + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + *csid = 0; + *idx = (_index_t)wc; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + /* ei seem to be unused */ + _DIAGASSERT(wc != NULL); + + if (csid != 0) + return EILSEQ; + *wc = (wchar_t)idx; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo * __restrict ei, + _UTF7State * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo *ei) +{ + /* ei seems to be unused */ +} + +static int +/*ARGSUSED*/ +_citrus_UTF7_encoding_module_init(_UTF7EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + const char *s; + + _DIAGASSERT(ei != NULL); + /* var may be null */ + + memset(ei, 0, sizeof(*ei)); + +#define FILL(str, flag) \ +do { \ + for (s = str; *s != '\0'; s++) \ + ei->cell[*s & 0x7f] |= flag; \ +} while (/*CONSTCOND*/0) + + FILL(base64, (s - base64) + 1); + FILL(direct, EI_DIRECT); + FILL(option, EI_OPTION); + FILL(spaces, EI_SPACE); + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(UTF7); +_CITRUS_CTYPE_DEF_OPS(UTF7); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(UTF7); +_CITRUS_STDENC_DEF_OPS(UTF7); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_utf7.h b/lib/nbsd_libc/citrus/modules/citrus_utf7.h new file mode 100644 index 000000000..8502acefd --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_utf7.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_utf7.h,v 1.1 2005/03/05 18:05:15 tnozaki Exp $ */ + +/*- + * Copyright (c)2004,2005 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_UTF7_H_ +#define _CITRUS_UTF7_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(UTF7); +_CITRUS_STDENC_GETOPS_FUNC(UTF7); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_utf8.c b/lib/nbsd_libc/citrus/modules/citrus_utf8.c new file mode 100644 index 000000000..de22faeaa --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_utf8.c @@ -0,0 +1,395 @@ +/* $NetBSD: citrus_utf8.c,v 1.17 2008/06/14 16:01:08 tnozaki Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_utf8.c,v 1.17 2008/06/14 16:01:08 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_utf8.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +static int _UTF8_count_array[256]; +static int const *_UTF8_count = NULL; + +static const u_int32_t _UTF8_range[] = { + 0, /*dummy*/ + 0x00000000, 0x00000080, 0x00000800, 0x00010000, + 0x00200000, 0x04000000, 0x80000000, +}; + +typedef struct { + char ch[6]; + int chlen; +} _UTF8State; + +typedef struct { +} _UTF8EncodingInfo; + +typedef struct { + _UTF8EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _UTF8State s_mblen; + _UTF8State s_mbrlen; + _UTF8State s_mbrtowc; + _UTF8State s_mbtowc; + _UTF8State s_mbsrtowcs; + _UTF8State s_wcrtomb; + _UTF8State s_wcsrtombs; + _UTF8State s_wctomb; + } states; +} _UTF8CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_ei_, _func_) (_ei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_UTF8_##m +#define _ENCODING_INFO _UTF8EncodingInfo +#define _CTYPE_INFO _UTF8CTypeInfo +#define _ENCODING_STATE _UTF8State +#define _ENCODING_MB_CUR_MAX(_ei_) 6 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline void +_UTF8_init_count(void) +{ + int i; + if (!_UTF8_count) { + memset(_UTF8_count_array, 0, sizeof(_UTF8_count_array)); + for (i = 0; i <= 0x7f; i++) + _UTF8_count_array[i] = 1; + for (i = 0xc0; i <= 0xdf; i++) + _UTF8_count_array[i] = 2; + for (i = 0xe0; i <= 0xef; i++) + _UTF8_count_array[i] = 3; + for (i = 0xf0; i <= 0xf7; i++) + _UTF8_count_array[i] = 4; + for (i = 0xf8; i <= 0xfb; i++) + _UTF8_count_array[i] = 5; + for (i = 0xfc; i <= 0xfd; i++) + _UTF8_count_array[i] = 6; + _UTF8_count = _UTF8_count_array; + } +} + +static int +_UTF8_findlen(wchar_t v) +{ + int i; + u_int32_t c; + + c = (u_int32_t)v; /*XXX*/ + for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]) - 1; i++) + if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1]) + return i; + + return -1; /*out of range*/ +} + +static __inline int +_UTF8_surrogate(wchar_t wc) +{ + return wc >= 0xd800 && wc <= 0xdfff; +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF8_init_state(_UTF8EncodingInfo *ei, _UTF8State *s) +{ + s->chlen = 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF8_pack_state(_UTF8EncodingInfo *ei, void *pspriv, + const _UTF8State *s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei, _UTF8State *s, + const void *pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +_citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s, + size_t n, _UTF8State *psenc, size_t *nresult) +{ + wchar_t wchar; + const char *s0; + int c; + int i; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_UTF8_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return 0; + } + + /* make sure we have the first byte in the buffer */ + if (psenc->chlen == 0) { + if (n-- < 1) + goto restart; + psenc->ch[psenc->chlen++] = *s0++; + } + + c = _UTF8_count[psenc->ch[0] & 0xff]; + if (c < 1 || c < psenc->chlen) + goto ilseq; + + if (c == 1) + wchar = psenc->ch[0] & 0xff; + else { + while (psenc->chlen < c) { + if (n-- < 1) + goto restart; + psenc->ch[psenc->chlen++] = *s0++; + } + wchar = psenc->ch[0] & (0x7f >> c); + for (i = 1; i < c; i++) { + if ((psenc->ch[i] & 0xc0) != 0x80) + goto ilseq; + wchar <<= 6; + wchar |= (psenc->ch[i] & 0x3f); + } + if (_UTF8_surrogate(wchar) || _UTF8_findlen(wchar) != c) + goto ilseq; + } + if (pwc != NULL) + *pwc = wchar; + *nresult = (wchar == 0) ? 0 : s0 - *s; + *s = s0; + psenc->chlen = 0; + + return 0; + +ilseq: + *nresult = (size_t)-1; + return EILSEQ; + +restart: + *s = s0; + *nresult = (size_t)-2; + return 0; +} + +static int +_citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei, char *s, size_t n, wchar_t wc, + _UTF8State *psenc, size_t *nresult) +{ + int cnt, i, ret; + wchar_t c; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + if (_UTF8_surrogate(wc)) { + ret = EILSEQ; + goto err; + } + cnt = _UTF8_findlen(wc); + if (cnt <= 0 || cnt > 6) { + /* invalid UCS4 value */ + ret = EILSEQ; + goto err; + } + if (n < cnt) { + /* bound check failure */ + ret = E2BIG; + goto err; + } + + c = wc; + if (s) { + for (i = cnt - 1; i > 0; i--) { + s[i] = 0x80 | (c & 0x3f); + c >>= 6; + } + s[0] = c; + if (cnt == 1) + s[0] &= 0x7f; + else { + s[0] &= (0x7f >> cnt); + s[0] |= ((0xff00 >> cnt) & 0xff); + } + } + + *nresult = (size_t)cnt; + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, + wchar_t wc) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + *csid = 0; + *idx = (_citrus_index_t)wc; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(wc != NULL); + + if (csid != 0) + return (EILSEQ); + + *wc = (wchar_t)idx; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo * __restrict ei, + _UTF8State * __restrict psenc, + int * __restrict rstate) +{ + + if (psenc->chlen == 0) + *rstate = _STDENC_SDGEN_INITIAL; + else + *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + _UTF8_init_count(); + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei) +{ +} + + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(UTF8); +_CITRUS_CTYPE_DEF_OPS(UTF8); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(UTF8); +_CITRUS_STDENC_DEF_OPS(UTF8); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_utf8.h b/lib/nbsd_libc/citrus/modules/citrus_utf8.h new file mode 100644 index 000000000..9e57b6b69 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_utf8.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_utf8.h,v 1.2 2003/06/25 09:51:49 tshiozak Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_UTF8_H_ +#define _CITRUS_UTF8_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(UTF8); +_CITRUS_STDENC_GETOPS_FUNC(UTF8); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_viqr.c b/lib/nbsd_libc/citrus/modules/citrus_viqr.c new file mode 100644 index 000000000..f27c6cf0e --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_viqr.c @@ -0,0 +1,572 @@ +/* $NetBSD: citrus_viqr.c,v 1.4 2008/06/14 16:01:08 tnozaki Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_viqr.c,v 1.4 2008/06/14 16:01:08 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_viqr.h" + +#define ESCAPE '\\' + +/* + * this table generated from RFC 1456. + */ +static const char *mnemonic_rfc1456[0x100] = { + NULL , NULL , "A(?", NULL , NULL , "A(~", "A^~", NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , "Y?" , NULL , NULL , NULL , + NULL , "Y~" , NULL , NULL , NULL , NULL , "Y." , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , + "A." , "A('", "A(`", "A(.", "A^'", "A^`", "A^?", "A^.", + "E~" , "E." , "E^'", "E^`", "E^?", "E^~", "E^.", "O^'", + "O^`", "O^?", "O^~", "O^.", "O+.", "O+'", "O+`", "O+?", + "I." , "O?" , "O." , "I?" , "U?" , "U~" , "U." , "Y`" , + "O~" , "a('", "a(`", "a(.", "a^'", "a^`", "a^?", "a^.", + "e~" , "e." , "e^'", "e^`", "e^?", "e^~", "e^.", "o^'", + "o^`", "o^?", "o^~", "O+~", "O+" , "o^.", "o+`", "o+?", + "i." , "U+.", "U+'", "U+`", "U+?", "o+" , "o+'", "U+" , + "A`" , "A'" , "A^" , "A~" , "A?" , "A(" , "a(?", "a(~", + "E`" , "E'" , "E^" , "E?" , "I`" , "I'" , "I~" , "y`" , + "DD" , "u+'", "O`" , "O'" , "O^" , "a." , "y?" , "u+`", + "u+?", "U`" , "U'" , "y~" , "y." , "Y'" , "o+~", "u+" , + "a`" , "a'" , "a^" , "a~" , "a?" , "a(" , "u+~", "a^~", + "e`" , "e'" , "e^" , "e?" , "i`" , "i'" , "i~" , "i?" , + "dd" , "u+.", "o`" , "o'" , "o^" , "o~" , "o?" , "o." , + "u." , "u`" , "u'" , "u~" , "u?" , "y'" , "o+.", "U+~", +}; + +typedef struct { + const char *name; + wchar_t value; +} mnemonic_def_t; + +static const mnemonic_def_t mnemonic_ext[] = { +/* add extra mnemonic here (should be sorted by wchar_t order). */ +}; +static const size_t mnemonic_ext_size = + sizeof(mnemonic_ext) / sizeof(mnemonic_def_t); + +static const char * +mnemonic_ext_find(wchar_t wc, const mnemonic_def_t *head, size_t n) +{ + const mnemonic_def_t *mid; + + _DIAGASSERT(head != NULL); + + for (; n > 0; n >>= 1) { + mid = head + (n >> 1); + if (mid->value == wc) { + return mid->name; + } else if (mid->value < wc) { + head = mid + 1; + --n; + } + } + return NULL; +} + +struct mnemonic_t; +typedef TAILQ_HEAD(mnemonic_list_t, mnemonic_t) mnemonic_list_t; +typedef struct mnemonic_t { + TAILQ_ENTRY(mnemonic_t) entry; + int ascii; + struct mnemonic_t *parent; + mnemonic_list_t child; + wchar_t value; +} mnemonic_t; + +static mnemonic_t * +mnemonic_list_find(mnemonic_list_t *ml, int ch) +{ + mnemonic_t *m; + + _DIAGASSERT(ml != NULL); + + TAILQ_FOREACH(m, ml, entry) { + if (m->ascii == ch) + return m; + } + + return NULL; +} + +static mnemonic_t * +mnemonic_create(mnemonic_t *parent, int ascii, wchar_t value) +{ + mnemonic_t *m; + + _DIAGASSERT(parent != NULL); + + m = malloc(sizeof(*m)); + if (m != NULL) { + m->parent = parent; + m->ascii = ascii; + m->value = value; + TAILQ_INIT(&m->child); + } + + return m; +} + +static int +mnemonic_append_child(mnemonic_t *m, const char *s, + wchar_t value, wchar_t invalid) +{ + int ch; + mnemonic_t *m0; + + _DIAGASSERT(m != NULL); + _DIAGASSERT(s != NULL); + + ch = (unsigned char)*s++; + if (ch == '\0') + return EINVAL; + m0 = mnemonic_list_find(&m->child, ch); + if (m0 == NULL) { + m0 = mnemonic_create(m, ch, (wchar_t)ch); + if (m0 == NULL) + return ENOMEM; + TAILQ_INSERT_TAIL(&m->child, m0, entry); + } + m = m0; + for (m0 = NULL; (ch = (unsigned char)*s) != '\0'; ++s) { + m0 = mnemonic_list_find(&m->child, ch); + if (m0 == NULL) { + m0 = mnemonic_create(m, ch, invalid); + if (m0 == NULL) + return ENOMEM; + TAILQ_INSERT_TAIL(&m->child, m0, entry); + } + m = m0; + } + if (m0 == NULL) + return EINVAL; + m0->value = value; + + return 0; +} + +static void +mnemonic_destroy(mnemonic_t *m) +{ + mnemonic_t *m0; + + _DIAGASSERT(m != NULL); + + TAILQ_FOREACH(m0, &m->child, entry) + mnemonic_destroy(m0); + free(m); +} + +typedef struct { + size_t mb_cur_max; + wchar_t invalid; + mnemonic_t *mroot; +} _VIQREncodingInfo; + +typedef struct { + int chlen; + char ch[MB_LEN_MAX]; +} _VIQRState; + +typedef struct { + _VIQREncodingInfo ei; + struct { + /* for future multi-locale facility */ + _VIQRState s_mblen; + _VIQRState s_mbrlen; + _VIQRState s_mbrtowc; + _VIQRState s_mbtowc; + _VIQRState s_mbsrtowcs; + _VIQRState s_wcrtomb; + _VIQRState s_wcsrtombs; + _VIQRState s_wctomb; + } states; +} _VIQRCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_VIQR_##m +#define _ENCODING_INFO _VIQREncodingInfo +#define _CTYPE_INFO _VIQRCTypeInfo +#define _ENCODING_STATE _VIQRState +#define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max +#define _ENCODING_IS_STATE_DEPENDENT 1 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline void +/*ARGSUSED*/ +_citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei, + _VIQRState * __restrict psenc) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + + psenc->chlen = 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_VIQR_pack_state(_VIQREncodingInfo * __restrict ei, + void *__restrict pspriv, const _VIQRState * __restrict psenc) +{ + /* ei may be unused */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(psenc != NULL); + + memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_VIQR_unpack_state(_VIQREncodingInfo * __restrict ei, + _VIQRState * __restrict psenc, const void * __restrict pspriv) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)psenc, pspriv, sizeof(*psenc)); +} + +static int +_citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char ** __restrict s, size_t n, + _VIQRState * __restrict psenc, size_t * __restrict nresult) +{ + const char *s0; + wchar_t wc; + mnemonic_t *m, *m0; + size_t i; + int ch, escape; + + _DIAGASSERT(ei != NULL); + /* pwc may be null */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (*s == NULL) { + _citrus_VIQR_init_state(ei, psenc); + *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT; + return 0; + } + s0 = *s; + + i = 0; + m = ei->mroot; + for (escape = 0;;) { + if (psenc->chlen == i) { + if (n-- < 1) { + *s = s0; + *nresult = (size_t)-2; + return 0; + } + psenc->ch[psenc->chlen++] = *s0++; + } + ch = (unsigned char)psenc->ch[i++]; + if (ch == ESCAPE) { + if (m != ei->mroot) + break; + escape = 1; + continue; + } + if (escape != 0) + break; + m0 = mnemonic_list_find(&m->child, ch); + if (m0 == NULL) + break; + m = m0; + } + while (m != ei->mroot) { + --i; + if (m->value != ei->invalid) + break; + m = m->parent; + } + if (ch == ESCAPE && m != ei->mroot) + ++i; + psenc->chlen -= i; + memmove(&psenc->ch[0], &psenc->ch[i], psenc->chlen); + wc = (m == ei->mroot) ? (wchar_t)ch : m->value; + if (pwc != NULL) + *pwc = wc; + *nresult = (size_t)(wc == 0 ? 0 : s0 - *s); + *s = s0; + + return 0; +} + +static int +_citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _VIQRState * __restrict psenc, size_t * __restrict nresult) +{ + mnemonic_t *m; + int ch, escape; + const char *p; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + switch (psenc->chlen) { + case 0: case 1: + break; + default: + return EINVAL; + } + m = NULL; + if ((uint32_t)wc <= 0xFF) { + p = mnemonic_rfc1456[wc & 0xFF]; + if (p != NULL) + goto mnemonic_found; + if (n-- < 1) + goto e2big; + ch = (unsigned int)wc; + m = ei->mroot; + if (psenc->chlen > 0) { + m = mnemonic_list_find(&m->child, psenc->ch[0]); + if (m == NULL) + return EINVAL; + psenc->ch[0] = ESCAPE; + } + if (mnemonic_list_find(&m->child, ch) == NULL) { + psenc->chlen = 0; + m = NULL; + } + psenc->ch[psenc->chlen++] = ch; + } else { + p = mnemonic_ext_find(wc, &mnemonic_ext[0], mnemonic_ext_size); + if (p == NULL) { + *nresult = (size_t)-1; + return EILSEQ; + } else { +mnemonic_found: + psenc->chlen = 0; + while (*p != '\0') { + if (n-- < 1) + goto e2big; + psenc->ch[psenc->chlen++] = *p++; + } + } + } + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + if (m == ei->mroot) { + psenc->ch[0] = ch; + psenc->chlen = 1; + } else { + psenc->chlen = 0; + } + + return 0; + +e2big: + *nresult = (size_t)-1; + return E2BIG; +} + +static int +/* ARGSUSED */ +_citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei, + char * __restrict s, size_t n, _VIQRState * __restrict psenc, + size_t * __restrict nresult) +{ + /* ei may be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + switch (psenc->chlen) { + case 0: case 1: + break; + default: + return EINVAL; + } + *nresult = 0; + psenc->chlen = 0; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo * __restrict ei, + _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) +{ + /* ei may be unused */ + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + *csid = 0; + *idx = (_index_t)wc; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo * __restrict ei, + wchar_t * __restrict pwc, _csid_t csid, _index_t idx) +{ + /* ei may be unused */ + _DIAGASSERT(pwc != NULL); + + if (csid != 0) + return EILSEQ; + *pwc = (wchar_t)idx; + + return 0; +} + +static void +_citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo *ei) +{ + _DIAGASSERT(ei != NULL); + + mnemonic_destroy(ei->mroot); +} + +static int +/*ARGSUSED*/ +_citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + int errnum; + const char *s; + size_t i, n; + const mnemonic_def_t *p; + + _DIAGASSERT(ei != NULL); + /* var may be unused */ + + ei->mb_cur_max = 1; + ei->invalid = (wchar_t)-1; + ei->mroot = mnemonic_create(NULL, '\0', ei->invalid); + if (ei->mroot == NULL) + return ENOMEM; + for (i = 0; i < sizeof(mnemonic_rfc1456) / sizeof(const char *); ++i) { + s = mnemonic_rfc1456[i]; + if (s == NULL) + continue; + n = strlen(s); + _DIAGASSERT(n <= MB_LEN_MAX); + if (ei->mb_cur_max < n) + ei->mb_cur_max = n; + errnum = mnemonic_append_child(ei->mroot, + s, (wchar_t)i, ei->invalid); + if (errnum != 0) { + _citrus_VIQR_encoding_module_uninit(ei); + return errnum; + } + } + for (i = 0; i < mnemonic_ext_size; ++i) { + p = &mnemonic_ext[i]; + _DIAGASSERT(p != NULL && p->name != NULL); + n = strlen(p->name); + _DIAGASSERT(n <= MB_LEN_MAX); + if (ei->mb_cur_max < n) + ei->mb_cur_max = n; + errnum = mnemonic_append_child(ei->mroot, + p->name, p->value, ei->invalid); + if (errnum != 0) { + _citrus_VIQR_encoding_module_uninit(ei); + return errnum; + } + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei, + _VIQRState * __restrict psenc, int * __restrict rstate) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(rstate != NULL); + + *rstate = (psenc->chlen == 0) + ? _STDENC_SDGEN_INITIAL + : _STDENC_SDGEN_INCOMPLETE_CHAR; + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(VIQR); +_CITRUS_CTYPE_DEF_OPS(VIQR); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(VIQR); +_CITRUS_STDENC_DEF_OPS(VIQR); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_viqr.h b/lib/nbsd_libc/citrus/modules/citrus_viqr.h new file mode 100644 index 000000000..93154294a --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_viqr.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_viqr.h,v 1.1 2006/11/13 15:16:31 tnozaki Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_VIQR_H_ +#define _CITRUS_VIQR_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(VIQR); +_CITRUS_STDENC_GETOPS_FUNC(VIQR); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/citrus/modules/citrus_zw.c b/lib/nbsd_libc/citrus/modules/citrus_zw.c new file mode 100644 index 000000000..485d53a6c --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_zw.c @@ -0,0 +1,512 @@ +/* $NetBSD: citrus_zw.c,v 1.4 2008/06/14 16:01:08 tnozaki Exp $ */ + +/*- + * Copyright (c)2004, 2006 Citrus Project, + * 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 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 +#if defined(LIB_SCCS) && !defined(lint) +__RCSID("$NetBSD: citrus_zw.c,v 1.4 2008/06/14 16:01:08 tnozaki Exp $"); +#endif /* LIB_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_zw.h" + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + int dummy; +} _ZWEncodingInfo; + +typedef enum { + NONE, AMBIGIOUS, ASCII, GB2312 +} _ZWCharset; + +typedef struct { + int chlen; + char ch[4]; + _ZWCharset charset; +} _ZWState; + +typedef struct { + _ZWEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _ZWState s_mblen; + _ZWState s_mbrlen; + _ZWState s_mbrtowc; + _ZWState s_mbtowc; + _ZWState s_mbsrtowcs; + _ZWState s_wcrtomb; + _ZWState s_wcsrtombs; + _ZWState s_wctomb; + } states; +} _ZWCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_ZW_##m +#define _ENCODING_INFO _ZWEncodingInfo +#define _CTYPE_INFO _ZWCTypeInfo +#define _ENCODING_STATE _ZWState +#define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX +#define _ENCODING_IS_STATE_DEPENDENT 1 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) ((_ps_)->charset != NONE) + +static __inline void +/*ARGSUSED*/ +_citrus_ZW_init_state(_ZWEncodingInfo * __restrict ei, + _ZWState * __restrict psenc) +{ + /* ei my be unused */ + _DIAGASSERT(psenc != NULL); + + psenc->chlen = 0; + psenc->charset = NONE; +} + +static __inline void +/*ARGSUSED*/ +_citrus_ZW_pack_state(_ZWEncodingInfo * __restrict ei, + void *__restrict pspriv, const _ZWState * __restrict psenc) +{ + /* ei may be unused */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(psenc != NULL); + + memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_ZW_unpack_state(_ZWEncodingInfo * __restrict ei, + _ZWState * __restrict psenc, const void * __restrict pspriv) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)psenc, pspriv, sizeof(*psenc)); +} + +static int +_citrus_ZW_mbrtowc_priv(_ZWEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char **__restrict s, size_t n, + _ZWState * __restrict psenc, size_t * __restrict nresult) +{ + const char *s0; + int ch, len; + wchar_t wc; + + /* ei may be unused */ + /* pwc may be null */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (*s == NULL) { + _citrus_ZW_init_state(ei, psenc); + *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT; + return 0; + } + s0 = *s; + len = 0; + +#define STORE \ +do { \ + if (n-- < 1) { \ + *nresult = (size_t)-2; \ + *s = s0; \ + return 0; \ + } \ + ch = (unsigned char)*s0++; \ + if (len++ > MB_LEN_MAX || ch > 0x7F)\ + goto ilseq; \ + psenc->ch[psenc->chlen++] = ch; \ +} while (/*CONSTCOND*/0) + +loop: + switch (psenc->charset) { + case ASCII: + switch (psenc->chlen) { + case 0: + STORE; + switch (psenc->ch[0]) { + case '\0': case '\n': + psenc->charset = NONE; + } + /*FALLTHROUGH*/ + case 1: + break; + default: + return EINVAL; + } + ch = (unsigned char)psenc->ch[0]; + if (ch > 0x7F) + goto ilseq; + wc = (wchar_t)ch; + psenc->chlen = 0; + break; + case NONE: + if (psenc->chlen != 0) + return EINVAL; + STORE; + ch = (unsigned char)psenc->ch[0]; + if (ch != 'z') { + if (ch != '\n' && ch != '\0') + psenc->charset = ASCII; + wc = (wchar_t)ch; + psenc->chlen = 0; + break; + } + psenc->charset = AMBIGIOUS; + psenc->chlen = 0; + /* FALLTHROUGH */ + case AMBIGIOUS: + if (psenc->chlen != 0) + return EINVAL; + STORE; + if (psenc->ch[0] != 'W') { + psenc->charset = ASCII; + wc = L'z'; + break; + } + psenc->charset = GB2312; + psenc->chlen = 0; + /* FALLTHROUGH */ + case GB2312: + switch (psenc->chlen) { + case 0: + STORE; + ch = (unsigned char)psenc->ch[0]; + if (ch == '\0') { + psenc->charset = NONE; + wc = (wchar_t)ch; + psenc->chlen = 0; + break; + } else if (ch == '\n') { + psenc->charset = NONE; + psenc->chlen = 0; + goto loop; + } + /*FALLTHROUGH*/ + case 1: + STORE; + if (psenc->ch[0] == ' ') { + ch = (unsigned char)psenc->ch[1]; + wc = (wchar_t)ch; + psenc->chlen = 0; + break; + } else if (psenc->ch[0] == '#') { + ch = (unsigned char)psenc->ch[1]; + if (ch == '\n') { + psenc->charset = NONE; + wc = (wchar_t)ch; + psenc->chlen = 0; + break; + } else if (ch == ' ') { + wc = (wchar_t)ch; + psenc->chlen = 0; + break; + } + } + ch = (unsigned char)psenc->ch[0]; + if (ch < 0x21 || ch > 0x7E) + goto ilseq; + wc = (wchar_t)(ch << 8); + ch = (unsigned char)psenc->ch[1]; + if (ch < 0x21 || ch > 0x7E) { +ilseq: + *nresult = (size_t)-1; + return EILSEQ; + } + wc |= (wchar_t)ch; + psenc->chlen = 0; + break; + default: + return EINVAL; + } + break; + default: + return EINVAL; + } + if (pwc != NULL) + *pwc = wc; + + *nresult = (size_t)(wc == 0 ? 0 : len); + *s = s0; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_ZW_wcrtomb_priv(_ZWEncodingInfo * __restrict ei, + char *__restrict s, size_t n, wchar_t wc, + _ZWState * __restrict psenc, size_t * __restrict nresult) +{ + int ch; + + /* ei may be null */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (psenc->chlen != 0) + return EINVAL; + if ((uint32_t)wc <= 0x7F) { + ch = (unsigned char)wc; + switch (psenc->charset) { + case NONE: + if (ch == '\0' || ch == '\n') { + psenc->ch[psenc->chlen++] = ch; + } else { + if (n < 4) + return E2BIG; + n -= 4; + psenc->ch[psenc->chlen++] = 'z'; + psenc->ch[psenc->chlen++] = 'W'; + psenc->ch[psenc->chlen++] = ' '; + psenc->ch[psenc->chlen++] = ch; + psenc->charset = GB2312; + } + break; + case GB2312: + if (n < 2) + return E2BIG; + n -= 2; + if (ch == '\0') { + psenc->ch[psenc->chlen++] = '\n'; + psenc->ch[psenc->chlen++] = '\0'; + psenc->charset = NONE; + } else if (ch == '\n') { + psenc->ch[psenc->chlen++] = '#'; + psenc->ch[psenc->chlen++] = '\n'; + psenc->charset = NONE; + } else { + psenc->ch[psenc->chlen++] = ' '; + psenc->ch[psenc->chlen++] = ch; + } + break; + default: + return EINVAL; + } + } else if ((uint32_t)wc <= 0x7E7E) { + switch (psenc->charset) { + case NONE: + if (n < 2) + return E2BIG; + n -= 2; + psenc->ch[psenc->chlen++] = 'z'; + psenc->ch[psenc->chlen++] = 'W'; + psenc->charset = GB2312; + /* FALLTHROUGH*/ + case GB2312: + if (n < 2) + return E2BIG; + n -= 2; + ch = (wc >> 8) & 0xFF; + if (ch < 0x21 || ch > 0x7E) + goto ilseq; + psenc->ch[psenc->chlen++] = ch; + ch = wc & 0xFF; + if (ch < 0x21 || ch > 0x7E) + goto ilseq; + psenc->ch[psenc->chlen++] = ch; + break; + default: + return EINVAL; + } + } else { +ilseq: + *nresult = (size_t)-1; + return EILSEQ; + } + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + psenc->chlen = 0; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_ZW_put_state_reset(_ZWEncodingInfo * __restrict ei, + char * __restrict s, size_t n, + _ZWState * __restrict psenc, size_t * __restrict nresult) +{ + /* ei may be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + if (psenc->chlen != 0) + return EINVAL; + switch (psenc->charset) { + case GB2312: + if (n-- < 1) + return E2BIG; + psenc->ch[psenc->chlen++] = '\n'; + psenc->charset = NONE; + /*FALLTHROUGH*/ + case NONE: + *nresult = psenc->chlen; + if (psenc->chlen > 0) { + memcpy(s, psenc->ch, psenc->chlen); + psenc->chlen = 0; + } + break; + default: + return EINVAL; + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_ZW_stdenc_get_state_desc_generic(_ZWEncodingInfo * __restrict ei, + _ZWState * __restrict psenc, int * __restrict rstate) +{ + /* ei may be unused */ + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(rstate != NULL); + + switch (psenc->charset) { + case NONE: + if (psenc->chlen != 0) + return EINVAL; + *rstate = _STDENC_SDGEN_INITIAL; + break; + case AMBIGIOUS: + if (psenc->chlen != 0) + return EINVAL; + *rstate = _STDENC_SDGEN_INCOMPLETE_SHIFT; + break; + case ASCII: + case GB2312: + switch (psenc->chlen) { + case 0: + *rstate = _STDENC_SDGEN_STABLE; + break; + case 1: + *rstate = (psenc->ch[0] == '#') + ? _STDENC_SDGEN_INCOMPLETE_SHIFT + : _STDENC_SDGEN_INCOMPLETE_CHAR; + break; + default: + return EINVAL; + } + break; + default: + return EINVAL; + } + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_ZW_stdenc_wctocs(_ZWEncodingInfo * __restrict ei, + _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) +{ + /* ei seems to be unused */ + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + *csid = (_csid_t)(wc <= 0x7FU) ? 0 : 1; + *idx = (_index_t)wc; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_ZW_stdenc_cstowc(_ZWEncodingInfo * __restrict ei, + wchar_t * __restrict wc, _csid_t csid, _index_t idx) +{ + /* ei seems to be unused */ + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: case 1: + break; + default: + return EINVAL; + } + *wc = (wchar_t)idx; + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_ZW_encoding_module_uninit(_ZWEncodingInfo *ei) +{ +} + +static int +/*ARGSUSED*/ +_citrus_ZW_encoding_module_init(_ZWEncodingInfo * __restrict ei, + const void *__restrict var, size_t lenvar) +{ + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(ZW); +_CITRUS_CTYPE_DEF_OPS(ZW); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(ZW); +_CITRUS_STDENC_DEF_OPS(ZW); + +#include "citrus_stdenc_template.h" diff --git a/lib/nbsd_libc/citrus/modules/citrus_zw.h b/lib/nbsd_libc/citrus/modules/citrus_zw.h new file mode 100644 index 000000000..9f7a20db9 --- /dev/null +++ b/lib/nbsd_libc/citrus/modules/citrus_zw.h @@ -0,0 +1,37 @@ +/* $NetBSD: citrus_zw.h,v 1.1 2006/11/22 23:38:27 tnozaki Exp $ */ + +/*- + * Copyright (c)2004,2006 Citrus Project, + * 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 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. + */ + +#ifndef _CITRUS_ZW_H_ +#define _CITRUS_ZW_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(ZW); +_CITRUS_STDENC_GETOPS_FUNC(ZW); +__END_DECLS + +#endif diff --git a/lib/nbsd_libc/compat-43/Makefile.inc b/lib/nbsd_libc/compat-43/Makefile.inc new file mode 100644 index 000000000..98e5390f3 --- /dev/null +++ b/lib/nbsd_libc/compat-43/Makefile.inc @@ -0,0 +1,21 @@ +# $NetBSD: Makefile.inc,v 1.16 2006/04/24 17:15:06 drochner Exp $ +# from: @(#)Makefile.inc 8.1 (Berkeley) 6/2/93 + +# compat-43 sources +.PATH: ${ARCHDIR}/compat-43 ${.CURDIR}/compat-43 + +SRCS+= creat.c getdtablesize.c gethostid.c \ + killpg.c sethostid.c setpgrp.c \ + setrgid.c setruid.c sigcompat.c +.if !defined(AUDIT) +SRCS+= getwd.c +.endif + +# XXX this is to get +CPPFLAGS.sigcompat.c+= -I${NETBSDSRCDIR}/sys + +MAN+= creat.3 getdtablesize.3 gethostid.3 killpg.3 \ + setruid.3 sigblock.3 sigpause.3 sigsetmask.3 sigvec.3 + +MLINKS+=setruid.3 setrgid.3 +MLINKS+=gethostid.3 sethostid.3 diff --git a/lib/nbsd_libc/compat-43/creat.3 b/lib/nbsd_libc/compat-43/creat.3 new file mode 100644 index 000000000..a5905b466 --- /dev/null +++ b/lib/nbsd_libc/compat-43/creat.3 @@ -0,0 +1,65 @@ +.\" Copyright (c) 1989, 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. +.\" +.\" from: @(#)creat.2 8.1 (Berkeley) 6/2/93 +.\" $NetBSD: creat.3,v 1.16 2003/08/07 16:42:39 agc Exp $ +.\" +.Dd June 2, 1993 +.Dt CREAT 3 +.Os +.Sh NAME +.Nm creat +.Nd create a new file +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In fcntl.h +.Ft int +.Fn creat "const char *path" "mode_t mode" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by: +.Ef +.Xr open 2 . +.Pp +.Fn creat +is the same as: +.Bd -literal -offset indent +open(path, O_CREAT | O_TRUNC | O_WRONLY, mode); +.Ed +.Sh SEE ALSO +.Xr open 2 +.Sh STANDARDS +The +.Fn creat +function call conforms to +.St -p1003.1-90 . +.Sh HISTORY +The +.Fn creat +function call appeared in +.At v6 . diff --git a/lib/nbsd_libc/compat-43/creat.c b/lib/nbsd_libc/compat-43/creat.c new file mode 100644 index 000000000..9560beafb --- /dev/null +++ b/lib/nbsd_libc/compat-43/creat.c @@ -0,0 +1,52 @@ +/* $NetBSD: creat.c,v 1.10 2003/08/07 16:42:39 agc Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)creat.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: creat.c,v 1.10 2003/08/07 16:42:39 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +int +creat(const char *path, mode_t mode) +{ + + _DIAGASSERT(path != NULL); + + return(open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)); +} diff --git a/lib/nbsd_libc/compat-43/getdtablesize.3 b/lib/nbsd_libc/compat-43/getdtablesize.3 new file mode 100644 index 000000000..5f19f7fed --- /dev/null +++ b/lib/nbsd_libc/compat-43/getdtablesize.3 @@ -0,0 +1,62 @@ +.\" Copyright (c) 1983, 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. +.\" +.\" from: @(#)getdtablesize.2 8.1 (Berkeley) 6/4/93 +.\" $NetBSD: getdtablesize.3,v 1.13 2003/08/07 16:42:39 agc Exp $ +.\" +.Dd June 4, 1993 +.Dt GETDTABLESIZE 3 +.Os +.Sh NAME +.Nm getdtablesize +.Nd get descriptor table size +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn getdtablesize void +.Sh DESCRIPTION +Each process has a fixed size descriptor table, +which is guaranteed to have at least 20 slots. +The entries in +the descriptor table are numbered with small integers starting at 0. +The call +.Fn getdtablesize +returns the size of this table. +.Sh SEE ALSO +.Xr close 2 , +.Xr dup 2 , +.Xr getrlimit 2 , +.Xr open 2 , +.Xr select 2 , +.Xr sysconf 3 +.Sh HISTORY +The +.Fn getdtablesize +function call appeared in +.Bx 4.2 . diff --git a/lib/nbsd_libc/compat-43/getdtablesize.c b/lib/nbsd_libc/compat-43/getdtablesize.c new file mode 100644 index 000000000..eff7c1847 --- /dev/null +++ b/lib/nbsd_libc/compat-43/getdtablesize.c @@ -0,0 +1,20 @@ +/* $NetBSD: getdtablesize.c,v 1.9 2003/07/26 19:24:41 salo Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getdtablesize.c,v 1.9 2003/07/26 19:24:41 salo Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +int +getdtablesize() +{ + return ((int)sysconf(_SC_OPEN_MAX)); +} diff --git a/lib/nbsd_libc/compat-43/gethostid.3 b/lib/nbsd_libc/compat-43/gethostid.3 new file mode 100644 index 000000000..a9f4ab21c --- /dev/null +++ b/lib/nbsd_libc/compat-43/gethostid.3 @@ -0,0 +1,77 @@ +.\" $NetBSD: gethostid.3,v 1.12 2003/08/07 16:42:39 agc Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)gethostid.3 8.1 (Berkeley) 6/2/93 +.\" +.Dd June 2, 1993 +.Dt GETHOSTID 3 +.Os +.Sh NAME +.Nm gethostid , +.Nm sethostid +.Nd get/set unique identifier of current host +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft long +.Fn gethostid void +.Ft int +.Fn sethostid "long hostid" +.Sh DESCRIPTION +.Fn sethostid +establishes a 32-bit identifier for the current processor that is +intended to be unique among all +.Ux +systems in existence. +This is normally a DARPA Internet address for the local machine. +This call is allowed only to the super-user and is normally performed +at boot time. +.Pp +.Fn gethostid +returns the 32-bit identifier for the current processor. +.Pp +This function has been deprecated. +The hostid should be set or retrieved by use of +.Xr sysctl 3 . +.Sh SEE ALSO +.Xr gethostname 3 , +.Xr sysctl 3 , +.Xr sysctl 8 +.Sh HISTORY +The +.Fn gethostid +and +.Fn sethostid +syscalls appeared in +.Bx 4.2 +and were dropped in +.Bx 4.4 . +.Sh BUGS +32 bits for the identifier is too small. diff --git a/lib/nbsd_libc/compat-43/gethostid.c b/lib/nbsd_libc/compat-43/gethostid.c new file mode 100644 index 000000000..b95f5982d --- /dev/null +++ b/lib/nbsd_libc/compat-43/gethostid.c @@ -0,0 +1,58 @@ +/* $NetBSD: gethostid.c,v 1.6 2004/04/19 13:16:42 atatat Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)gethostid.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: gethostid.c,v 1.6 2004/04/19 13:16:42 atatat Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +long +gethostid(void) +{ + int mib[2], value; + size_t size; + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTID; + size = sizeof(value); + if (sysctl(mib, 2, &value, &size, NULL, (size_t)0) == -1) + return (-1); + return (value); +} diff --git a/lib/nbsd_libc/compat-43/getwd.c b/lib/nbsd_libc/compat-43/getwd.c new file mode 100644 index 000000000..98b72c193 --- /dev/null +++ b/lib/nbsd_libc/compat-43/getwd.c @@ -0,0 +1,65 @@ +/* $NetBSD: getwd.c,v 1.14 2003/08/07 16:42:39 agc 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getwd.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: getwd.c,v 1.14 2003/08/07 16:42:39 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include + +__warn_references(getwd, + "warning: getwd() possibly used unsafely, consider using getcwd()") + +char * +getwd(buf) + char *buf; +{ + char *p; + + _DIAGASSERT(buf != NULL); + + if ((p = getcwd(buf, MAXPATHLEN)) != NULL) + return(p); + (void)strlcpy(buf, strerror(errno), MAXPATHLEN); + return((char *)NULL); +} diff --git a/lib/nbsd_libc/compat-43/killpg.3 b/lib/nbsd_libc/compat-43/killpg.3 new file mode 100644 index 000000000..d3afef034 --- /dev/null +++ b/lib/nbsd_libc/compat-43/killpg.3 @@ -0,0 +1,94 @@ +.\" Copyright (c) 1980, 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. +.\" +.\" from: @(#)killpg.2 8.1 (Berkeley) 6/2/93 +.\" $NetBSD: killpg.3,v 1.16 2003/08/07 16:42:39 agc Exp $ +.\" +.Dd June 2, 1993 +.Dt KILLPG 3 +.Os +.Sh NAME +.Nm killpg +.Nd send signal to a process group +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn killpg "pid_t pgrp" "int sig" +.Sh DESCRIPTION +.Fn killpg +sends the signal +.Fa sig +to the process group +.Fa pgrp . +See +.Xr sigaction 2 +for a list of signals. +If +.Fa pgrp +is 0, +.Fn killpg +sends the signal to the sending process's process group. +.Pp +The sending process and members of the process group must +have the same effective user ID, or +the sender must be the super-user. +As a single special case the continue signal SIGCONT may be sent +to any process that is a descendant of the current process. +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. +Otherwise, a value of \-1 is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn killpg +will fail and no signal will be sent if: +.Bl -tag -width Er +.It Bq Er EINVAL +.Fa sig +is not a valid signal number. +.It Bq Er ESRCH +No process can be found in the process group specified by +.Fa pgrp . +.It Bq Er ESRCH +The process group was given as 0 +but the sending process does not have a process group. +.It Bq Er EPERM +The sending process is not the super-user and one or more +of the target processes has an effective user ID different from that +of the sending process. +.El +.Sh SEE ALSO +.Xr getpgrp 2 , +.Xr kill 2 , +.Xr sigaction 2 +.Sh HISTORY +The +.Fn killpg +function call appeared in +.Bx 4.0 . diff --git a/lib/nbsd_libc/compat-43/killpg.c b/lib/nbsd_libc/compat-43/killpg.c new file mode 100644 index 000000000..ceac3f486 --- /dev/null +++ b/lib/nbsd_libc/compat-43/killpg.c @@ -0,0 +1,56 @@ +/* $NetBSD: killpg.c,v 1.8 2003/08/07 16:42:39 agc Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)killpg.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: killpg.c,v 1.8 2003/08/07 16:42:39 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +/* + * Backwards-compatible killpg(). + */ +int +killpg(pid_t pgid, int sig) +{ + if (pgid == 1) { + errno = ESRCH; + return (-1); + } + return (kill(-pgid, sig)); +} diff --git a/lib/nbsd_libc/compat-43/sethostid.c b/lib/nbsd_libc/compat-43/sethostid.c new file mode 100644 index 000000000..b38bf8a8c --- /dev/null +++ b/lib/nbsd_libc/compat-43/sethostid.c @@ -0,0 +1,57 @@ +/* $NetBSD: sethostid.c,v 1.7 2004/04/19 13:16:42 atatat Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sethostid.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: sethostid.c,v 1.7 2004/04/19 13:16:42 atatat Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +int +sethostid(long hostid) +{ + int mib[2], value; + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTID; + value = (int)hostid; + if (sysctl(mib, 2, NULL, NULL, &value, sizeof(value)) == -1) + return (-1); + return (0); +} diff --git a/lib/nbsd_libc/compat-43/setpgrp.c b/lib/nbsd_libc/compat-43/setpgrp.c new file mode 100644 index 000000000..3c031cce7 --- /dev/null +++ b/lib/nbsd_libc/compat-43/setpgrp.c @@ -0,0 +1,48 @@ +/* $NetBSD: setpgrp.c,v 1.8 2003/08/07 16:42:40 agc 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)setpgrp.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: setpgrp.c,v 1.8 2003/08/07 16:42:40 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +int +setpgrp(pid_t pid, pid_t pgid) +{ + return(setpgid(pid, pgid)); +} diff --git a/lib/nbsd_libc/compat-43/setrgid.c b/lib/nbsd_libc/compat-43/setrgid.c new file mode 100644 index 000000000..a0f933486 --- /dev/null +++ b/lib/nbsd_libc/compat-43/setrgid.c @@ -0,0 +1,50 @@ +/* $NetBSD: setrgid.c,v 1.12 2003/08/07 16:42:40 agc Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)setrgid.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: setrgid.c,v 1.12 2003/08/07 16:42:40 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +__warn_references(setrgid, "warning: this program uses setrgid(), which is deprecated.") + +int +setrgid(gid_t rgid) +{ + return (setregid(rgid, (gid_t)-1)); +} diff --git a/lib/nbsd_libc/compat-43/setruid.3 b/lib/nbsd_libc/compat-43/setruid.3 new file mode 100644 index 000000000..8a557b1da --- /dev/null +++ b/lib/nbsd_libc/compat-43/setruid.3 @@ -0,0 +1,78 @@ +.\" Copyright (c) 1983, 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. +.\" +.\" from: @(#)setruid.3 8.1 (Berkeley) 6/2/93 +.\" $NetBSD: setruid.3,v 1.10 2003/08/07 16:42:40 agc Exp $ +.\" +.Dd June 2, 1993 +.Dt SETRUID 3 +.Os +.Sh NAME +.Nm setruid , +.Nm setrgid +.Nd set user and group ID +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.Ft int +.Fn setruid "uid_t ruid" +.Ft int +.Fn setrgid "gid_t rgid" +.Sh DESCRIPTION +The +.Fn setruid +function +.Pq Fn setrgid +sets the real user ID (group ID) of the +current process. +.Sh RETURN VALUES +Upon success, these functions return 0; +otherwise \-1 is returned. +.Pp +If the user is not the super user, or the uid +specified is not the real or effective ID, these +functions return \-1. +.Pp +The use of these calls is not portable. +Their use is discouraged; they will be removed in the future. +.Sh SEE ALSO +.Xr getgid 2 , +.Xr getuid 2 , +.Xr setegid 2 , +.Xr seteuid 2 , +.Xr setgid 2 , +.Xr setuid 2 +.Sh HISTORY +The +.Fn setruid +and +.Fn setrgid +syscalls appeared in +.Bx 4.2 +and were dropped in +.Bx 4.4 . diff --git a/lib/nbsd_libc/compat-43/setruid.c b/lib/nbsd_libc/compat-43/setruid.c new file mode 100644 index 000000000..64b843302 --- /dev/null +++ b/lib/nbsd_libc/compat-43/setruid.c @@ -0,0 +1,50 @@ +/* $NetBSD: setruid.c,v 1.13 2003/08/07 16:42:40 agc Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)setruid.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: setruid.c,v 1.13 2003/08/07 16:42:40 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +__warn_references(setruid, "warning: this program uses setruid(), which is deprecated.") + +int +setruid(uid_t ruid) +{ + return (setreuid(ruid, (uid_t)-1)); +} diff --git a/lib/nbsd_libc/compat-43/sigblock.3 b/lib/nbsd_libc/compat-43/sigblock.3 new file mode 100644 index 000000000..86c8dff0e --- /dev/null +++ b/lib/nbsd_libc/compat-43/sigblock.3 @@ -0,0 +1,119 @@ +.\" Copyright (c) 1983, 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. +.\" +.\" from: @(#)sigblock.2 8.1 (Berkeley) 6/2/93 +.\" $NetBSD: sigblock.3,v 1.17 2003/08/07 16:42:40 agc Exp $ +.\" +.Dd August 10, 2002 +.Dt SIGBLOCK 3 +.Os +.Sh NAME +.Nm sigblock +.Nd block signals +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn sigblock "int mask" +.Ft int +.Fn sigmask signum +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by: +.Xr sigprocmask 2 . +.Ef +.Pp +.Fn sigblock +adds the signals specified in +.Fa mask +to the set of signals currently +being blocked from delivery. +Signals are blocked if the +corresponding bit in +.Fa mask +is a 1; the macro +.Fn sigmask +is provided to construct the mask for a given +.Fa signum . +.Pp +It is not possible to block +.Dv SIGKILL +or +.Dv SIGSTOP ; +this restriction is silently +imposed by the system. +.Sh RETURN VALUES +The previous set of masked signals is returned. +.Sh EXAMPLES +The following example using +.Fn sigblock : +.Bd -literal -offset indent +int omask; + +omask = sigblock(sigmask(SIGINT) | sigmask(SIGHUP)); +.Ed +.Pp +Becomes: +.Bd -literal -offset indent +sigset_t set, oset; + +sigemptyset(&set); +sigaddset(&set, SIGINT); +sigaddset(&set, SIGHUP); +sigprocmask(SIG_BLOCK, &set, &oset); +.Ed +.Pp +Another use of +.Fn sigblock +is to get the current set of masked signals without changing what +is actually blocked. +Instead of: +.Bd -literal -offset indent +int set; + +set = sigblock(0); +.Ed +.Pp +Use the following: +.Bd -literal -offset indent +sigset_t set; + +sigprocmask(SIG_BLOCK, NULL, &set); +.Ed +.Sh SEE ALSO +.Xr kill 2 , +.Xr sigaction 2 , +.Xr sigprocmask 2 , +.Xr sigsetmask 3 , +.Xr sigsetops 3 +.Sh HISTORY +The +.Fn sigblock +function call appeared in +.Bx 4.2 +and has been deprecated. diff --git a/lib/nbsd_libc/compat-43/sigcompat.c b/lib/nbsd_libc/compat-43/sigcompat.c new file mode 100644 index 000000000..a8ae696ff --- /dev/null +++ b/lib/nbsd_libc/compat-43/sigcompat.c @@ -0,0 +1,120 @@ +/* $NetBSD: sigcompat.c,v 1.13 2005/12/24 21:11:16 perry Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sigcompat.c 8.1 (Berkeley) 6/2/93"; +#else +__RCSID("$NetBSD: sigcompat.c,v 1.13 2005/12/24 21:11:16 perry Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +static inline void sv2sa(struct sigaction *, const struct sigvec *); +static inline void sa2sv(struct sigvec *, const struct sigaction *); + +static inline void +sv2sa(struct sigaction *sa, const struct sigvec *sv) +{ + sigemptyset(&sa->sa_mask); + sa->sa_mask.__bits[0] = sv->sv_mask; + sa->sa_handler = sv->sv_handler; + sa->sa_flags = sv->sv_flags ^ SV_INTERRUPT; /* !SA_INTERRUPT */ +} + +static inline void +sa2sv(struct sigvec *sv, const struct sigaction *sa) +{ + sv->sv_mask = sa->sa_mask.__bits[0]; + sv->sv_handler = sa->sa_handler; + sv->sv_flags = sa->sa_flags ^ SV_INTERRUPT; /* !SA_INTERRUPT */ +} + +int +sigvec(int signo, struct sigvec *nsv, struct sigvec *osv) +{ + int ret; + struct sigaction osa, nsa; + + if (nsv) + sv2sa(&nsa, nsv); + + ret = sigaction(signo, nsv ? &nsa : NULL, osv ? &osa : NULL); + + if (ret == 0 && osv) + sa2sv(osv, &osa); + + return (ret); +} + +int +sigsetmask(int mask) +{ + sigset_t nmask, omask; + int n; + + sigemptyset(&nmask); + nmask.__bits[0] = mask; + + n = sigprocmask(SIG_SETMASK, &nmask, &omask); + if (n) + return (n); + return (omask.__bits[0]); +} + +int +sigblock(int mask) +{ + sigset_t nmask, omask; + int n; + + sigemptyset(&nmask); + nmask.__bits[0] = mask; + + n = sigprocmask(SIG_BLOCK, &nmask, &omask); + if (n) + return (n); + return (omask.__bits[0]); +} + +int +sigpause(int mask) +{ + sigset_t nmask; + + sigemptyset(&nmask); + nmask.__bits[0] = mask; + return (sigsuspend(&nmask)); +} diff --git a/lib/nbsd_libc/compat-43/sigpause.3 b/lib/nbsd_libc/compat-43/sigpause.3 new file mode 100644 index 000000000..431ad501c --- /dev/null +++ b/lib/nbsd_libc/compat-43/sigpause.3 @@ -0,0 +1,75 @@ +.\" Copyright (c) 1983, 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. +.\" +.\" from: @(#)sigpause.2 8.1 (Berkeley) 6/2/93 +.\" $NetBSD: sigpause.3,v 1.15 2003/08/07 16:42:40 agc Exp $ +.\" +.Dd June 2, 1993 +.Dt SIGPAUSE 3 +.Os +.Sh NAME +.Nm sigpause +.Nd atomically release blocked signals and wait for interrupt +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn sigpause "int sigmask" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by +.Xr sigsuspend 2 . +.Ef +.Pp +.Fn sigpause +assigns +.Fa sigmask +to the set of masked signals +and then waits for a signal to arrive; +on return the set of masked signals is restored. +.Fa sigmask +is usually 0 to indicate that no +signals are to be blocked. +.Fn sigpause +always terminates by being interrupted, returning -1 with +.Va errno +set to +.Dv EINTR . +.Sh SEE ALSO +.Xr kill 2 , +.Xr sigaction 2 , +.Xr sigprocmask 2 , +.Xr sigsuspend 2 , +.Xr sigblock 3 , +.Xr sigvec 3 +.Sh HISTORY +The +.Fn sigpause +function call appeared in +.Bx 4.2 +and has been deprecated. diff --git a/lib/nbsd_libc/compat-43/sigsetmask.3 b/lib/nbsd_libc/compat-43/sigsetmask.3 new file mode 100644 index 000000000..3b36b0c85 --- /dev/null +++ b/lib/nbsd_libc/compat-43/sigsetmask.3 @@ -0,0 +1,140 @@ +.\" Copyright (c) 1983, 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. +.\" +.\" from: @(#)sigsetmask.2 8.1 (Berkeley) 6/2/93 +.\" $NetBSD: sigsetmask.3,v 1.18 2003/08/07 16:42:40 agc Exp $ +.\" +.Dd August 10, 2002 +.Dt SIGSETMASK 3 +.Os +.Sh NAME +.Nm sigsetmask +.Nd set current signal mask +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn sigsetmask "int mask" +.Fn sigmask signum +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by: +.Ef +.Xr sigprocmask 2 . +.Pp +.Fn sigsetmask +sets the current signal mask +Signals are blocked from delivery if the +corresponding bit in +.Fa mask +is a 1; the macro +.Fn sigmask +is provided to construct the mask for a given +.Fa signum . +.Pp +The system +quietly disallows +.Dv SIGKILL +or +.Dv SIGSTOP +to be blocked. +.Sh RETURN VALUES +The previous set of masked signals is returned. +.Sh EXAMPLES +The following example using +.Fn sigsetmask : +.Bd -literal -offset indent +int omask; + +omask = sigblock(sigmask(SIGINT) | sigmask(SIGHUP)); + +\&... + +sigsetmask(omask & ~(sigmask(SIGINT) | sigmask(SIGHUP))); +.Ed +.Pp +Could be converted literally to: +.Bd -literal -offset indent +sigset_t set, oset; + +sigemptyset(&set); +sigaddset(&set, SIGINT); +sigaddset(&set, SIGHUP); +sigprocmask(SIG_BLOCK, &set, &oset); + +\&... + +sigdelset(&oset, SIGINT); +sigdelset(&oset, SIGHUP); +sigprocmask(SIG_SETMASK, &oset, NULL); +.Ed +.Pp +Another, clearer, alternative is: +.Bd -literal -offset indent +sigset_t set; + +sigemptyset(&set); +sigaddset(&set, SIGINT); +sigaddset(&set, SIGHUP); +sigprocmask(SIG_BLOCK, &set, NULL); + +\&... + +sigprocmask(SIG_UNBLOCK, &set, NULL); +.Ed +.Pp +To completely clear the signal mask using +.Fn sigsetmask +one can do: +.Bd -literal -offset indent +(void) sigsetmask(0); +.Ed +.Pp +Which can be expressed via +.Xr sigprocmask 2 +as: +.Bd -literal -offset indent +sigset_t eset; + +sigemptyset(&eset); +(void) sigprocmask(SIG_SETMASK, &eset, NULL); +.Ed +.Sh SEE ALSO +.Xr kill 2 , +.Xr sigaction 2 , +.Xr sigprocmask 2 , +.Xr sigsuspend 2 , +.Xr sigblock 3 , +.Xr sigsetops 3 , +.Xr sigvec 3 +.Sh HISTORY +The +.Fn sigsetmask +function call appeared in +.Bx 4.2 +and has been deprecated. diff --git a/lib/nbsd_libc/compat-43/sigvec.3 b/lib/nbsd_libc/compat-43/sigvec.3 new file mode 100644 index 000000000..18c2a4c2c --- /dev/null +++ b/lib/nbsd_libc/compat-43/sigvec.3 @@ -0,0 +1,313 @@ +.\" $NetBSD: sigvec.3,v 1.26 2010/03/22 19:30:53 joerg Exp $ +.\" +.\" Copyright (c) 1980, 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. +.\" +.\" from: @(#)sigvec.2 8.2 (Berkeley) 4/19/94 +.\" +.Dd December 3, 2005 +.Dt SIGVEC 3 +.Os +.Sh NAME +.Nm sigvec +.Nd software signal facilities +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Bd -literal +struct sigvec { + void (*sv_handler)(); + int sv_mask; + int sv_flags; +}; +.Ed +.Ft int +.Fn sigvec "int sig" "struct sigvec *vec" "struct sigvec *ovec" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by +.Xr sigaction 2 . +The structure, flags, and function declaration have been removed from +the header files but the function is kept in the c library for binary +compatibility. +.Ef +.Pp +The system defines a set of signals that may be delivered to a process. +Signal delivery resembles the occurrence of a hardware interrupt: +the signal is blocked from further occurrence, the current process +context is saved, and a new one is built. +A process may specify a +.Em handler +to which a signal is delivered, or specify that a signal is to be +.Em ignored . +A process may also specify that a default action is to be taken +by the system when a signal occurs. +A signal may also be +.Em blocked , +in which case its delivery is postponed until it is +.Em unblocked . +The action to be taken on delivery is determined at the time +of delivery. +Normally, signal handlers execute on the current stack +of the process. +This may be changed, on a per-handler basis, so that signals are +taken on a special +.Em "signal stack" . +.Pp +Signal routines execute with the signal that caused their +invocation +.Em blocked , +but other signals may yet occur. +A global +.Em "signal mask" +defines the set of signals currently blocked from delivery +to a process. +The signal mask for a process is initialized from that of its parent +(normally 0). +It may be changed with a +.Xr sigblock 3 +or +.Xr sigsetmask 3 +call, or when a signal is delivered to the process. +.Pp +When a signal +condition arises for a process, the signal is added to a set of +signals pending for the process. +If the signal is not currently +.Em blocked +by the process then it is delivered to the process. +When a caught signal +is delivered, the current state of the process is saved, +a new signal mask is calculated (as described below), +and the signal handler is invoked. +The call to the handler is arranged so that if the signal handling +routine returns normally the process will resume execution in the +context from before the signal's delivery. +If the process wishes to resume in a different context, then it +must arrange to restore the previous context itself. +.Pp +When a signal is delivered to a process a new signal mask is +installed for the duration of the process' signal handler +(or until a +.Xr sigblock 3 +or +.Xr sigsetmask 3 +call is made). +This mask is formed by taking the union of the current signal mask, +the signal to be delivered, and +the signal mask associated with the handler to be invoked. +.Pp +.Fn sigvec +assigns a handler for a specific signal. +If +.Fa vec +is non-zero, it +specifies an action +.Pf ( Dv SIG_DFL , +.Dv SIG_IGN , +or a handler routine) and mask +to be used when delivering the specified signal. +Further, if the +.Dv SV_ONSTACK +bit is set in +.Fa sv_flags , +the system will deliver the signal to the process on a +.Em "signal stack" , +specified with +.Xr sigaltstack 2 . +If +.Fa ovec +is non-zero, the previous handling information for the signal +is returned to the user. +.Pp +Once a signal handler is installed, it remains installed +until another +.Fn sigvec +call is made, or an +.Xr execve 2 +is performed. +A signal-specific default action may be reset by +setting +.Fa sv_handler +to +.Dv SIG_DFL . +The defaults are process termination, possibly with core dump; +no action; stopping the process; or continuing the process. +See the signal list below for each signal's default action. +If +.Fa sv_handler +is set to +.Dv SIG_DFL , +the default action for the signal is to discard the signal, +and if a signal is pending, +the pending signal is discarded even if the signal is masked. +If +.Fa sv_handler +is set to +.Dv SIG_IGN , +current and pending instances +of the signal are ignored and discarded. +.Pp +Options may be specified by setting +.Em sv_flags . +If the +.Dv SV_ONSTACK +bit is set in +.Fa sv_flags , +the system will deliver the signal to the process on a +.Em "signal stack" , +specified with +.Xr sigstack 2 . +.Pp +If a signal is caught during the system calls listed below, +the call may be restarted, +the call may return with a data transfer shorter than requested, +or the call may be forced to terminate +with the error +.Dv EINTR . +Interrupting of pending calls is requested +by setting the +.Dv SV_INTERRUPT +bit in +.Ar sv_flags . +The affected system calls include +.Xr open 2 , +.Xr read 2 , +.Xr write 2 , +.Xr sendto 2 , +.Xr recvfrom 2 , +.Xr sendmsg 2 +and +.Xr recvmsg 2 +on a communications channel or a slow device (such as a terminal, +but not a regular file) +and during a +.Xr wait 2 +or +.Xr ioctl 2 . +However, calls that have already committed are not restarted, +but instead return a partial success (for example, a short read count). +.Pp +After a +.Xr fork 2 +or +.Xr vfork 2 +all signals, the signal mask, the signal stack, +and the interrupt/restart flags are inherited by the child. +.Pp +The +.Xr execve 2 +system call reinstates the default action for all signals which +were caught and resets all signals to be caught on the user stack. +Ignored signals remain ignored; +the signal mask remains the same; +signals that interrupt pending system calls continue to do so. +.Pp +See +.Xr signal 7 +for comprehensive list of supported signals. +.Sh NOTES +The mask specified in +.Fa vec +is not allowed to block +.Dv SIGKILL +or +.Dv SIGSTOP . +This is enforced silently by the system. +.Pp +The +.Dv SV_INTERRUPT +flag is not available in +.Bx 4.2 , +hence it should not be used if backward compatibility is needed. +.Sh RETURN VALUES +A 0 value indicated that the call succeeded. +A \-1 return value indicates an error occurred and +.Va errno +is set to indicated the reason. +.Sh EXAMPLES +The handler routine can be declared: +.Bd -literal -offset indent +void +handler(sig, code, scp) + int sig, code; + struct sigcontext *scp; +.Ed +.Pp +Here +.Fa sig +is the signal number, into which the hardware faults and traps are +mapped as defined below. +.Fa code +is a parameter that is either a constant +or the code provided by the hardware. +.Fa scp +is a pointer to the +.Fa sigcontext +structure (defined in +.In signal.h ) , +used to restore the context from before the signal. +.Sh ERRORS +.Fn sigvec +will fail and no new signal handler will be installed if one +of the following occurs: +.Bl -tag -width Er +.It Bq Er EFAULT +Either +.Fa vec +or +.Fa ovec +points to memory that is not a valid part of the process +address space. +.It Bq Er EINVAL +.Fa sig +is not a valid signal number. +.It Bq Er EINVAL +An attempt is made to ignore or supply a handler for +.Dv SIGKILL +or +.Dv SIGSTOP . +.El +.Sh SEE ALSO +.Xr kill 1 , +.Xr kill 2 , +.Xr ptrace 2 , +.Xr sigaction 2 , +.Xr sigaltstack 2 , +.Xr sigprocmask 2 , +.Xr sigstack 2 , +.Xr sigsuspend 2 , +.Xr setjmp 3 , +.Xr sigblock 3 , +.Xr siginterrupt 3 , +.Xr signal 3 , +.Xr sigpause 3 , +.Xr sigsetmask 3 , +.Xr sigsetops 3 , +.Xr tty 4 , +.Xr signal 7 diff --git a/lib/nbsd_libc/compat/Makefile b/lib/nbsd_libc/compat/Makefile new file mode 100644 index 000000000..d9eec76a7 --- /dev/null +++ b/lib/nbsd_libc/compat/Makefile @@ -0,0 +1,26 @@ +# $NetBSD: Makefile,v 1.5 2010/06/07 13:52:29 tnozaki Exp $ +# @(#)Makefile 8.2 (Berkeley) 2/3/94 +# + +.include "${.CURDIR}/../Makefile.inc" + +LIB= cold +CPPFLAGS+= -I${.CURDIR}/../include -I${.CURDIR}/.. +CPPFLAGS+= -I${.CURDIR}/../arch/${ARCHSUBDIR} + +COMPATDIR=${.CURDIR} + +.include "${.CURDIR}/Makefile.inc" + +.include "${.CURDIR}/db/Makefile.inc" +.include "${.CURDIR}/gen/Makefile.inc" +.include "${.CURDIR}/locale/Makefile.inc" +.include "${.CURDIR}/net/Makefile.inc" +.include "${.CURDIR}/rpc/Makefile.inc" +.include "${.CURDIR}/stdlib/Makefile.inc" +.include "${.CURDIR}/sys/Makefile.inc" + +COMPATARCHDIR=${.CURDIR}/arch/${ARCHSUBDIR} +.PATH: ${COMPATARCHDIR}/gen ${COMPATARCHDIR}/sys + +.include diff --git a/lib/nbsd_libc/compat/Makefile.inc b/lib/nbsd_libc/compat/Makefile.inc new file mode 100644 index 000000000..e6a64787f --- /dev/null +++ b/lib/nbsd_libc/compat/Makefile.inc @@ -0,0 +1,16 @@ +# $NetBSD: Makefile.inc,v 1.6 2010/06/07 13:52:29 tnozaki Exp $ + +CPPFLAGS+= -I${NETBSDSRCDIR}/sys + +COMPATARCHDIR=${COMPATDIR}/arch/${ARCHSUBDIR} +.PATH: ${COMPATARCHDIR}/gen ${COMPATARCHDIR}/sys + +.include "${COMPATDIR}/db/Makefile.inc" +.include "${COMPATDIR}/locale/Makefile.inc" +.include "${COMPATDIR}/gen/Makefile.inc" +.include "${COMPATDIR}/net/Makefile.inc" +.include "${COMPATDIR}/rpc/Makefile.inc" +.include "${COMPATDIR}/stdlib/Makefile.inc" +.include "${COMPATDIR}/sys/Makefile.inc" +.include "${COMPATDIR}/time/Makefile.inc" +.include "${COMPATARCHDIR}/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/alpha/Makefile.inc b/lib/nbsd_libc/compat/arch/alpha/Makefile.inc new file mode 100644 index 000000000..aa2ae6647 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 23:04:03 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/alpha/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/alpha/gen/Makefile.inc new file mode 100644 index 000000000..4fc8c9ab5 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/gen/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/06/28 14:22:15 drochner Exp $ + +# objects built from assembler sources +SRCS+= compat_setjmp.S compat__setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/alpha/gen/compat__setjmp.S b/lib/nbsd_libc/compat/arch/alpha/gen/compat__setjmp.S new file mode 100644 index 000000000..40b4b2b76 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/gen/compat__setjmp.S @@ -0,0 +1,123 @@ +/* $NetBSD: compat__setjmp.S,v 1.1 2005/09/16 18:21:19 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +/* + * C library -- _setjmp, _longjmp + * + * _longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * _setjmp(a) + * by restoring registers from the stack, + * The previous signal state is NOT restored. + */ + + .set noreorder + +LEAF(_setjmp, 1) + LDGP(pv) + stq ra, (2 * 8)(a0) /* sc_pc = return address */ + stq s0, (( 9 + 4) * 8)(a0) /* saved bits of sc_regs */ + stq s1, ((10 + 4) * 8)(a0) + stq s2, ((11 + 4) * 8)(a0) + stq s3, ((12 + 4) * 8)(a0) + stq s4, ((13 + 4) * 8)(a0) + stq s5, ((14 + 4) * 8)(a0) + stq s6, ((15 + 4) * 8)(a0) + stq ra, ((26 + 4) * 8)(a0) + stq sp, ((30 + 4) * 8)(a0) + ldiq t0, 0xacedbadd /* sigcontext magic number */ + stq t0, ((31 + 4) * 8)(a0) /* magic in sc_regs[31] */ + /* Too bad we can't check if we actually used FP */ + ldiq t0, 1 + stq t0, (36 * 8)(a0) /* say we've used FP. */ + stt fs0, ((2 + 37) * 8)(a0) /* saved bits of sc_fpregs */ + stt fs1, ((3 + 37) * 8)(a0) + stt fs2, ((4 + 37) * 8)(a0) + stt fs3, ((5 + 37) * 8)(a0) + stt fs4, ((6 + 37) * 8)(a0) + stt fs5, ((7 + 37) * 8)(a0) + stt fs6, ((8 + 37) * 8)(a0) + stt fs7, ((9 + 37) * 8)(a0) + mf_fpcr ft0 /* get FP control reg */ + stt ft0, (69 * 8)(a0) /* and store it in sc_fpcr */ + stq zero, (70 * 8)(a0) /* FP software control XXX */ + stq zero, (71 * 8)(a0) /* sc_reserved[0] */ + stq zero, (72 * 8)(a0) /* sc_reserved[1] */ + stq zero, (73 * 8)(a0) /* sc_xxx[0] */ + stq zero, (74 * 8)(a0) /* sc_xxx[1] */ + stq zero, (75 * 8)(a0) /* sc_xxx[2] */ + stq zero, (76 * 8)(a0) /* sc_xxx[3] */ + stq zero, (77 * 8)(a0) /* sc_xxx[4] */ + stq zero, (78 * 8)(a0) /* sc_xxx[5] */ + stq zero, (79 * 8)(a0) /* sc_xxx[6] */ + stq zero, (80 * 8)(a0) /* sc_xxx[7] */ + + mov zero, v0 /* return zero */ + RET +END(_setjmp) + +LEAF(_longjmp, 2) + LDGP(pv) + ldq t0, ((31 + 4) * 8)(a0) /* magic in sc_regs[31] */ + ldiq t1, 0xacedbadd + cmpeq t0, t1, t0 + beq t0, botch /* If the magic was bad, punt */ + + ldq ra, (2 * 8)(a0) /* sc_pc = return address */ + ldq s0, (( 9 + 4) * 8)(a0) /* saved bits of sc_regs */ + ldq s1, ((10 + 4) * 8)(a0) + ldq s2, ((11 + 4) * 8)(a0) + ldq s3, ((12 + 4) * 8)(a0) + ldq s4, ((13 + 4) * 8)(a0) + ldq s5, ((14 + 4) * 8)(a0) + ldq s6, ((15 + 4) * 8)(a0) + /* ldq ra, ((26 + 4) * 8)(a0) set above */ + ldq sp, ((30 + 4) * 8)(a0) + ldt fs0, ((2 + 37) * 8)(a0) /* saved bits of sc_fpregs */ + ldt fs1, ((3 + 37) * 8)(a0) + ldt fs2, ((4 + 37) * 8)(a0) + ldt fs3, ((5 + 37) * 8)(a0) + ldt fs4, ((6 + 37) * 8)(a0) + ldt fs5, ((7 + 37) * 8)(a0) + ldt fs6, ((8 + 37) * 8)(a0) + ldt fs7, ((9 + 37) * 8)(a0) + ldt ft0, (69 * 8)(a0) /* get sc_fpcr */ + mt_fpcr ft0 /* and restore it. */ + + mov a1, v0 /* return second arg */ + RET + +botch: + CALL(longjmperror) + CALL(abort) + RET /* "can't" get here... */ +END(_longjmp) diff --git a/lib/nbsd_libc/compat/arch/alpha/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/alpha/gen/compat_setjmp.S new file mode 100644 index 000000000..29781cdea --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/gen/compat_setjmp.S @@ -0,0 +1,124 @@ +/* $NetBSD: compat_setjmp.S,v 1.1 2005/09/16 18:21:19 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and the previous signal state. + */ + + .set noreorder + +LEAF(setjmp, 1) + LDGP(pv) + stq ra, (2 * 8)(a0) /* sc_pc = return address */ + stq s0, (( 9 + 4) * 8)(a0) /* saved bits of sc_regs */ + stq s1, ((10 + 4) * 8)(a0) + stq s2, ((11 + 4) * 8)(a0) + stq s3, ((12 + 4) * 8)(a0) + stq s4, ((13 + 4) * 8)(a0) + stq s5, ((14 + 4) * 8)(a0) + stq s6, ((15 + 4) * 8)(a0) + stq ra, ((26 + 4) * 8)(a0) + stq sp, ((30 + 4) * 8)(a0) + + /* + * get signal information + */ + mov a0, s0 /* squirrel away ptr to sc */ + + /* see what's blocked */ + mov zero, a0 + CALL(sigblock) /* see what's blocked */ + stq v0, (1 * 8)(s0) /* and remember it in sc_mask */ + + lda sp, -24(sp) /* sizeof struct sigaltstack */ + mov zero, a0 + mov sp, a1 + CALL(__sigaltstack14) + ldl t0, 16(sp) /* offset of ss_flags */ + lda sp, 24(sp) /* sizeof struct sigaltstack */ + ldq ra, ((26 + 4) * 8)(s0) /* restore return address */ + blt v0, botch /* check for error */ + and t0, 0x1, t0 /* get SA_ONSTACK flag */ + stq t0, (0 * 8)(s0) /* and save it in sc_onstack */ + /* + * Restore old s0 and a0, and continue saving registers + */ + mov s0, a0 + ldq s0, (( 9 + 4) * 8)(a0) + + ldiq t0, 0xacedbade /* sigcontext magic number */ + stq t0, ((31 + 4) * 8)(a0) /* magic in sc_regs[31] */ + /* Too bad we can't check if we actually used FP */ + ldiq t0, 1 + stq t0, (36 * 8)(a0) /* say we've used FP. */ + stt fs0, ((2 + 37) * 8)(a0) /* saved bits of sc_fpregs */ + stt fs1, ((3 + 37) * 8)(a0) + stt fs2, ((4 + 37) * 8)(a0) + stt fs3, ((5 + 37) * 8)(a0) + stt fs4, ((6 + 37) * 8)(a0) + stt fs5, ((7 + 37) * 8)(a0) + stt fs6, ((8 + 37) * 8)(a0) + stt fs7, ((9 + 37) * 8)(a0) + mf_fpcr ft0 /* get FP control reg */ + stt ft0, (69 * 8)(a0) /* and store it in sc_fpcr */ + stq zero, (70 * 8)(a0) /* FP software control XXX */ + stq zero, (71 * 8)(a0) /* sc_reserved[0] */ + stq zero, (72 * 8)(a0) /* sc_reserved[1] */ + stq zero, (73 * 8)(a0) /* sc_xxx[0] */ + stq zero, (74 * 8)(a0) /* sc_xxx[1] */ + stq zero, (75 * 8)(a0) /* sc_xxx[2] */ + stq zero, (76 * 8)(a0) /* sc_xxx[3] */ + stq zero, (77 * 8)(a0) /* sc_xxx[4] */ + stq zero, (78 * 8)(a0) /* sc_xxx[5] */ + stq zero, (79 * 8)(a0) /* sc_xxx[6] */ + stq zero, (80 * 8)(a0) /* sc_xxx[7] */ + + mov zero, v0 /* return zero */ + RET +END(setjmp) + +LEAF(longjmp, 2) + LDGP(pv) + stq a1, (( 0 + 4) * 8)(a0) /* save return value */ + CALL(sigreturn) /* use sigreturn to return */ + +botch: + CALL(longjmperror) + CALL(abort) + RET /* "can't" get here... */ +END(longjmp) diff --git a/lib/nbsd_libc/compat/arch/alpha/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/alpha/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..dba6f6487 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/gen/compat_sigsetjmp.S @@ -0,0 +1,62 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.1 2005/09/16 18:21:19 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a, mask) + * by restoring registers from the stack. + * If `mask' is non-zero, the previous signal + * state will be restored. + */ + + .set noreorder + +LEAF(sigsetjmp, 2) + LDGP(pv) + stq a1, (81 * 8)(a0) /* save the mask */ + bne a1, Lsavesig /* if !zero, save signals */ + jmp zero, _setjmp /* else don't. */ +Lsavesig: + jmp zero, setjmp +END(sigsetjmp) + +LEAF(siglongjmp, 2) + LDGP(pv) + ldq t0, (81 * 8)(a0) /* get the mask */ + bne t0, Lrestoresig /* if !zero, restore signals */ + jmp zero, _longjmp +Lrestoresig: + jmp zero, longjmp +END(siglongjmp) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/alpha/sys/Makefile.inc new file mode 100644 index 000000000..1786884d0 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/16 18:21:19 drochner Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_Ovfork.S new file mode 100644 index 000000000..b061d6dc2 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_Ovfork.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_Ovfork.S,v 1.1 2005/09/16 18:21:19 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +SYSCALL(vfork) + cmovne a4, zero, v0 /* a4 (rv[1]) != 0, child */ + RET +END(vfork) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat___sigreturn14.S new file mode 100644 index 000000000..708ef2f6b --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat___sigreturn14.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2005/09/16 18:21:19 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + * However, that doesn't involve any special work on the Alpha. + * (XXX PROFILING) + */ + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat___sigtramp1.S new file mode 100644 index 000000000..df48f30f8 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat___sigtramp1.S @@ -0,0 +1,45 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.1 2005/09/16 18:21:19 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +/* + * The Alpha signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, the stack looks like: + * + * sp-> sigcontext structure + */ +NESTED_NOPROFILE(__sigtramp_sigcontext_1,0,0,ra,0,0) + mov sp, a0 /* get pointer to sigcontext */ + CALLSYS_NOERROR(compat_16___sigreturn14) /* and call sigreturn() with it */ + mov v0, a0 /* if that failed, get error code */ + CALLSYS_NOERROR(exit) /* and call exit() with it */ +END(__sigtramp_sigcontext_1) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_msgctl.S new file mode 100644 index 000000000..cc08d76ef --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_shmctl.S new file mode 100644 index 000000000..10d43f71d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigaction.S new file mode 100644 index 000000000..a5f8ad810 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigpending.S new file mode 100644 index 000000000..7ecf6a348 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigpending.S @@ -0,0 +1,40 @@ +/* $NetBSD: compat_sigpending.S,v 1.1 2005/09/16 18:21:20 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +LEAF(sigpending,0) /* XXX # of args? */ + CALLSYS_ERROR(compat_13_sigpending13) + stl v0, 0(a0) + mov zero, v0 + RET +END(sigpending) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigprocmask.S new file mode 100644 index 000000000..316a80ad9 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigprocmask.S @@ -0,0 +1,47 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.1 2005/09/16 18:21:20 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +LEAF(sigprocmask, 3) + mov a2, a5 /* safe */ + cmoveq a1, 1, a0 /* if set == NULL, how = SIG_BLOCK */ + beq a1, Ldoit /* and set = 0, and do it. */ + ldl a1, 0(a1) /* load the set from *set */ +Ldoit: CALLSYS_ERROR(compat_13_sigprocmask13) + beq a5, Lret /* if they don't want old mask, done */ + stl v0, 0(a5) /* otherwise, give it to them. */ +Lret: mov zero, v0 + RET + + END(sigprocmask) + diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigreturn.S new file mode 100644 index 000000000..76983d0bf --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigreturn.S @@ -0,0 +1,41 @@ +/* $NetBSD: compat_sigreturn.S,v 1.1 2005/09/16 18:21:20 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + * However, that doesn't involve any special work on the Alpha. + * (XXX PROFILING) + */ + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +PSEUDO(sigreturn,compat_13_sigreturn13) diff --git a/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigsuspend.S new file mode 100644 index 000000000..b8f3b5743 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/alpha/sys/compat_sigsuspend.S @@ -0,0 +1,40 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.1 2005/09/16 18:21:20 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +LEAF(sigsuspend, 1) + ldl a0, 0(a0) /* pass *mask instead of mask */ + CALLSYS_ERROR(compat_13_sigsuspend13) + mov zero, v0 /* shouldn't need; just in case... */ + RET +END(sigsuspend) diff --git a/lib/nbsd_libc/compat/arch/arm/Makefile.inc b/lib/nbsd_libc/compat/arch/arm/Makefile.inc new file mode 100644 index 000000000..78ab575bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 23:04:03 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/arm/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/arm/gen/Makefile.inc new file mode 100644 index 000000000..81b2b912d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/gen/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/06/28 14:46:33 drochner Exp $ + +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/arm/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/arm/gen/compat_setjmp.S new file mode 100644 index 000000000..1514ef4ec --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/gen/compat_setjmp.S @@ -0,0 +1,130 @@ +/* $NetBSD: compat_setjmp.S,v 1.1 2005/10/16 17:27:50 christos Exp $ */ + +/* + * Copyright (c) 1997 Mark Brinicombe + * 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 by Mark Brinicombe + * 4. 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 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 + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(setjmp) + /* Block all signals and retrieve the old signal mask */ + stmfd sp!, {r0, r14} + mov r0, #0x00000000 + + bl PIC_SYM(_C_LABEL(sigblock), PLT) + mov r1, r0 + + ldmfd sp!, {r0, r14} + + /* Store signal mask */ + str r1, [r0, #(25 * 4)] + + ldr r1, .Lsetjmp_magic + str r1, [r0], #4 + +#ifdef SOFTFLOAT + add r0, r0, #52 +#else + /* Store fp registers */ + sfm f4, 4, [r0], #48 + /* Store fpsr */ + rfs r1 + str r1, [r0], #0x0004 +#endif /*SOFTFLOAT*/ + /* Store integer registers */ + stmia r0, {r4-r14} + mov r0, #0x00000000 + RET + +.Lsetjmp_magic: + .word _JB_MAGIC_SETJMP + + +ENTRY(longjmp) + ldr r2, .Lsetjmp_magic + ldr r3, [r0] + teq r2, r3 + bne botch + + /* Fetch signal mask */ + ldr r2, [r0, #(25 * 4)] + + /* Set signal mask */ + stmfd sp!, {r0, r1, r14} + sub sp, sp, #4 /* align the stack */ + + mov r0, r2 + bl PIC_SYM(_C_LABEL(sigsetmask), PLT) + + add sp, sp, #4 /* unalign the stack */ + ldmfd sp!, {r0, r1, r14} + + add r0, r0, #4 +#ifdef SOFTFLOAT + add r0, r0, #52 +#else + /* Restore fp registers */ + lfm f4, 4, [r0], #48 + /* Restore FPSR */ + ldr r4, [r0], #0x0004 + wfs r4 +#endif /* SOFTFLOAT */ + /* Restore integer registers */ + ldmia r0, {r4-r14} + + /* Validate sp and r14 */ + teq sp, #0 + teqne r14, #0 + beq botch + + /* Set return value */ + + mov r0, r1 + teq r0, #0x00000000 + moveq r0, #0x00000001 + RET + + /* validation failed, die die die. */ +botch: + bl PIC_SYM(_C_LABEL(longjmperror), PLT) + bl PIC_SYM(_C_LABEL(abort), PLT) + b . - 8 /* Cannot get here */ diff --git a/lib/nbsd_libc/compat/arch/arm/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/arm/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..8ad14fb0b --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/gen/compat_sigsetjmp.S @@ -0,0 +1,61 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.1 2005/10/16 17:27:50 christos Exp $ */ + +/* + * Copyright (c) 1997 Mark Brinicombe + * 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 by Mark Brinicombe + * 4. 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 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 + +/* + * C library -- sigsetjmp, siglongjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a, m) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(sigsetjmp) + teq r1, #0 + beq PIC_SYM(_C_LABEL(_setjmp), PLT) + b PIC_SYM(_C_LABEL(setjmp), PLT) + +.L_setjmp_magic: + .word _JB_MAGIC__SETJMP + +ENTRY(siglongjmp) + ldr r2, .L_setjmp_magic + ldr r3, [r0] + teq r2, r3 + beq PIC_SYM(_C_LABEL(_longjmp), PLT) + b PIC_SYM(_C_LABEL(longjmp), PLT) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/arm/sys/Makefile.inc new file mode 100644 index 000000000..64c52469b --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/10/16 17:27:50 christos Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_Ovfork.S new file mode 100644 index 000000000..317094e80 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_Ovfork.S @@ -0,0 +1,56 @@ +/* $NetBSD: compat_Ovfork.S,v 1.1 2005/10/16 17:27:50 christos 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +/* + * pid = vfork(); + * + * On return from the SWI: + * r1 == 0 in parent process, r1 == 1 in child process. + * r0 == pid of child in parent, r0 == pid of parent in child. + */ + .text + .align 0 + +ENTRY(vfork) + mov r2, r14 + SYSTRAP(vfork) + bcs PIC_SYM(CERROR, PLT) + mov r14, r2 + sub r1, r1, #1 /* r1 == 0xffffffff if parent, 0 if child */ + and r0, r0, r1 /* r0 == 0 if child, else unchanged */ + RET diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/arm/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/arm/sys/compat___sigreturn14.S new file mode 100644 index 000000000..72f0c1d5c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat___sigreturn14.S @@ -0,0 +1,40 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2005/10/16 17:27:50 christos 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/arm/sys/compat___sigtramp1.S new file mode 100644 index 000000000..8404824ff --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat___sigtramp1.S @@ -0,0 +1,47 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +/* + * The ARM signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, the stack looks like: + * + * sp-> sigcontext structure + */ +ENTRY_NP(__sigtramp_sigcontext_1) + mov r0, sp /* get pointer to sigcontext */ + SYSTRAP(compat_16___sigreturn14) /* and call sigreturn */ + + /* If that failed, exit with the error code. */ + SYSTRAP(exit) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_msgctl.S new file mode 100644 index 000000000..cc08d76ef --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_shmctl.S new file mode 100644 index 000000000..10d43f71d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigaction.S new file mode 100644 index 000000000..a5f8ad810 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigpending.S new file mode 100644 index 000000000..7449a52f3 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigpending.S @@ -0,0 +1,45 @@ +/* $NetBSD: compat_sigpending.S,v 1.1 2005/10/16 17:27:50 christos 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. + * + * from: @(#)sigpending.s 5.1 (Berkeley) 7/1/90 + */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +ENTRY(sigpending) + mov r2, r0 + SYSTRAP(compat_13_sigpending13) + bcs PIC_SYM(CERROR, PLT) + str r0, [r2] + mov r0, #0x00000000 + RET diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigprocmask.S new file mode 100644 index 000000000..9e73bd1a6 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigprocmask.S @@ -0,0 +1,49 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.1 2005/10/16 17:27:50 christos 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. + * + * from: @(#)sigprocmask.s 5.2 (Berkeley) 12/17/90 + */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +ENTRY(sigprocmask) + teq r1, #0x00000000 + moveq r0, #0x00000001 + moveq r1, #0x00000000 + ldrne r1, [r1] + SYSTRAP(compat_13_sigprocmask13) + bcs PIC_SYM(CERROR, PLT) + teq r2, #0x00000000 + strne r0, [r2] + mov r0, #0x00000000 + RET diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigreturn.S new file mode 100644 index 000000000..6428869d2 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigreturn.S @@ -0,0 +1,43 @@ +/* $NetBSD: compat_sigreturn.S,v 1.2 2005/10/16 22:16:05 christos 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +PSEUDO(sigreturn,compat_13_sigreturn13) diff --git a/lib/nbsd_libc/compat/arch/arm/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigsuspend.S new file mode 100644 index 000000000..2b22059a3 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/arm/sys/compat_sigsuspend.S @@ -0,0 +1,44 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.1 2005/10/16 17:27:50 christos 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. + * + * from: @(#)sigsuspend.s 5.2 (Berkeley) 12/17/90 + */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend) + ldr r0, [r0] + SYSTRAP(compat_13_sigsuspend13) + bcs PIC_SYM(CERROR, PLT) + mov r0, #0x00000000 + RET diff --git a/lib/nbsd_libc/compat/arch/hppa/Makefile.inc b/lib/nbsd_libc/compat/arch/hppa/Makefile.inc new file mode 100644 index 000000000..e9f6ca5ca --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/06/28 14:46:33 drochner Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/hppa/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/hppa/gen/Makefile.inc new file mode 100644 index 000000000..a9e9667c1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/gen/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/06/28 14:46:33 drochner Exp $ + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/hppa/locale/compat_setlocale32.c b/lib/nbsd_libc/compat/arch/hppa/locale/compat_setlocale32.c new file mode 100644 index 000000000..dc035cd31 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/locale/compat_setlocale32.c @@ -0,0 +1,53 @@ +/* $NetBSD: compat_setlocale32.c,v 1.1 2010/06/07 13:52:29 tnozaki Exp $ */ + +/*- + * Copyright (c)1999 Citrus Project, + * 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 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. + * + * NetBSD: setlocale32.c,v 1.6 2010/05/22 13:50:02 tnozaki Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_setlocale32.c,v 1.1 2010/06/07 13:52:29 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include "setlocale_local.h" + +__warn_references(__setlocale_mb_len_max_32, + "warning: reference to compatibility __setlocale_mb_len_max_32();" + "include for correct reference") + +char * +__setlocale_mb_len_max_32(int category, const char *locale) +{ + + /* locale may be NULL */ + + __mb_len_max_runtime = 6; + return __setlocale(category, locale); +} diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/hppa/sys/Makefile.inc new file mode 100644 index 000000000..a38ba1171 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/11 19:54:56 christos Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_Ovfork.S new file mode 100644 index 000000000..bf0f9a254 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_Ovfork.S @@ -0,0 +1,44 @@ +/* $NetBSD: compat_Ovfork.S,v 1.2 2009/11/03 05:07:25 snj Exp $ */ + +/* $OpenBSD: Ovfork.S,v 1.7 2001/06/04 23:14:04 mickey Exp $ */ + +/* + * Copyright (c) 1999,2002 Michael Shalayeff + * 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 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 OR HIS RELATIVES 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 MIND, 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_Ovfork.S,v 1.2 2009/11/03 05:07:25 snj Exp $") +#endif /* LIBC_SCCS and not lint */ + +ENTRY(vfork,0) + SYSCALL(vfork) + addi -1, %ret1, %ret1 + bv %r0(%rp) + and %ret1, %ret0, %ret0 +EXIT(vfork) + + .end diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat___sigreturn14.S new file mode 100644 index 000000000..515a88801 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat___sigreturn14.S @@ -0,0 +1,42 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2006/03/11 19:54:56 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat___sigreturn14.S,v 1.1 2006/03/11 19:54:56 christos Exp $") +#endif /* LIBC_SCCS and not lint */ + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_msgctl.S new file mode 100644 index 000000000..cc08d76ef --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_shmctl.S new file mode 100644 index 000000000..10d43f71d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigaction.S new file mode 100644 index 000000000..a5f8ad810 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigpending.S new file mode 100644 index 000000000..0ed8d51f4 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigpending.S @@ -0,0 +1,47 @@ +/* $NetBSD: compat_sigpending.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + + .import __cerror, code + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +ENTRY(sigpending, 0) + /* + * AFAICT, since the hppa port came after __sigpending14, + * we shouldn't have to supply the compatability sigpending. + */ + ldi EINVAL, %t1 + b,n __cerror +EXIT(sigpending) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigprocmask.S new file mode 100644 index 000000000..99c00931f --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigprocmask.S @@ -0,0 +1,47 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + + .import __cerror, code + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +ENTRY(sigprocmask, 0) + /* + * AFAICT, since the hppa port came after __sigprocmask14, + * we shouldn't have to supply the compatability sigprocmask. + */ + ldi EINVAL, %t1 + b,n __cerror +EXIT(sigprocmask) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigreturn.S new file mode 100644 index 000000000..d02624bdb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigreturn.S @@ -0,0 +1,47 @@ +/* $NetBSD: compat_sigreturn.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + + .import __cerror, code + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn(); include for correct reference") + +ENTRY(sigreturn, 0) + /* + * AFAICT, since the hppa port came after __sigreturn14, + * we shouldn't have to supply the compatability sigreturn. + */ + ldi EINVAL, %t1 + b,n __cerror +EXIT(sigreturn) diff --git a/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigsuspend.S new file mode 100644 index 000000000..2cdbb3eff --- /dev/null +++ b/lib/nbsd_libc/compat/arch/hppa/sys/compat_sigsuspend.S @@ -0,0 +1,47 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Fredette. + * + * 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 "SYS.h" + + .import __cerror, code + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend, 0) + /* + * AFAICT, since the hppa port came after __sigsuspend14, + * we shouldn't have to supply the compatability sigsuspend. + */ + ldi EINVAL, %t1 + b,n __cerror +EXIT(sigsuspend) diff --git a/lib/nbsd_libc/compat/arch/i386/Makefile.inc b/lib/nbsd_libc/compat/arch/i386/Makefile.inc new file mode 100644 index 000000000..78ab575bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 23:04:03 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/i386/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/i386/gen/Makefile.inc new file mode 100644 index 000000000..81d6059a5 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/gen/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/06/27 18:19:57 drochner Exp $ + +# objects built from assembler sources (need lint stubs) +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/i386/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/i386/gen/compat_setjmp.S new file mode 100644 index 000000000..67e7f7255 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/gen/compat_setjmp.S @@ -0,0 +1,101 @@ +/* $NetBSD: compat_setjmp.S,v 1.1 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: compat_setjmp.S,v 1.1 2005/09/13 01:44:08 christos Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(setjmp) + + PIC_PROLOGUE + pushl $0 +#ifdef PIC + call PIC_PLT(_C_LABEL(sigblock)) +#else + call _C_LABEL(sigblock) +#endif + addl $4,%esp + PIC_EPILOGUE + + movl 4(%esp),%ecx + movl 0(%esp),%edx + movl %edx, 0(%ecx) + movl %ebx, 4(%ecx) + movl %esp, 8(%ecx) + movl %ebp,12(%ecx) + movl %esi,16(%ecx) + movl %edi,20(%ecx) + movl %eax,24(%ecx) + xorl %eax,%eax + ret + +ENTRY(longjmp) + movl 4(%esp),%edx + + PIC_PROLOGUE + pushl 24(%edx) +#ifdef PIC + call PIC_PLT(_C_LABEL(sigsetmask)) +#else + call _C_LABEL(sigsetmask) +#endif + addl $4,%esp + PIC_EPILOGUE + + movl 4(%esp),%edx + movl 8(%esp),%eax + movl 0(%edx),%ecx + movl 4(%edx),%ebx + movl 8(%edx),%esp + movl 12(%edx),%ebp + movl 16(%edx),%esi + movl 20(%edx),%edi + testl %eax,%eax + jnz 1f + incl %eax +1: movl %ecx,0(%esp) + ret diff --git a/lib/nbsd_libc/compat/arch/i386/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/i386/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..f9cc9e958 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/gen/compat_sigsetjmp.S @@ -0,0 +1,98 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.1 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90" + */ + +#include +#if defined(LIBC_SCCS) + RCSID("$NetBSD: compat_sigsetjmp.S,v 1.1 2005/09/13 01:44:08 christos Exp $") +#endif + +ENTRY(sigsetjmp) + movl 4(%esp),%ecx + movl 8(%esp),%eax + movl %eax,28(%ecx) + testl %eax,%eax + jz 2f + + PIC_PROLOGUE + pushl $0 +#ifdef PIC + call PIC_PLT(_C_LABEL(sigblock)) +#else + call _C_LABEL(sigblock) +#endif + addl $4,%esp + PIC_EPILOGUE + + movl 4(%esp),%ecx + movl %eax,24(%ecx) +2: movl 0(%esp),%edx + movl %edx, 0(%ecx) + movl %ebx, 4(%ecx) + movl %esp, 8(%ecx) + movl %ebp,12(%ecx) + movl %esi,16(%ecx) + movl %edi,20(%ecx) + xorl %eax,%eax + ret + +ENTRY(siglongjmp) + movl 4(%esp),%edx + cmpl $0,28(%edx) + jz 2f + + PIC_PROLOGUE + pushl 24(%edx) +#ifdef PIC + call PIC_PLT(_C_LABEL(sigsetmask)) +#else + call _C_LABEL(sigsetmask) +#endif + addl $4,%esp + PIC_EPILOGUE + +2: movl 4(%esp),%edx + movl 8(%esp),%eax + movl 0(%edx),%ecx + movl 4(%edx),%ebx + movl 8(%edx),%esp + movl 12(%edx),%ebp + movl 16(%edx),%esi + movl 20(%edx),%edi + testl %eax,%eax + jnz 3f + incl %eax +3: movl %ecx,0(%esp) + ret diff --git a/lib/nbsd_libc/compat/arch/i386/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/i386/sys/Makefile.inc new file mode 100644 index 000000000..60a6c2d1b --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/13 01:44:08 christos Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_Ovfork.S new file mode 100644 index 000000000..385d075b5 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_Ovfork.S @@ -0,0 +1,70 @@ +/* $NetBSD: compat_Ovfork.S,v 1.1 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_Ovfork.S,v 1.1 2005/09/13 01:44:08 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +/* + * pid = vfork(); + * + * %edx == 0 in parent process, %edx == 1 in child process. + * %eax == pid of child in parent, %eax == pid of parent in child. + * + */ +ENTRY(vfork) + popl %ecx /* my rta into ecx */ + SYSTRAP(vfork) + jc err + decl %edx + andl %edx,%eax + jmp *%ecx +err: + pushl %ecx +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/i386/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/i386/sys/compat___sigreturn14.S new file mode 100644 index 000000000..08fdc5155 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat___sigreturn14.S @@ -0,0 +1,59 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.2 2011/01/12 23:12:11 joerg Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat___sigreturn14.S,v 1.2 2011/01/12 23:12:11 joerg Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ +#ifdef GPROF +#undef ENTRY +#ifdef __ELF__ +#define MCOUNT_SYMBOL __mcount +#else +#define MCOUNT_SYMBOL mcount +#endif +#define ENTRY(x) \ + .globl _ ## x; _ALIGN_TEXT; _ ## x: pusha ; \ + .data; 1:; .long 0; .text; movl $1b,%eax; call MCOUNT_SYMBOL; popa ; nop +#endif /* GPROF */ + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/i386/sys/compat___sigtramp1.S new file mode 100644 index 000000000..4ae754692 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat___sigtramp1.S @@ -0,0 +1,51 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +/* + * The i386 signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * sigcontext structure [12] + * pointer to sigcontext structure [8] + * signal specific code [4] + * sp-> signal number [0] + */ +NENTRY(__sigtramp_sigcontext_1) + leal 12(%esp),%eax /* get pointer to sigcontext */ + movl %eax,4(%esp) /* put it in the argument slot */ + /* fake return address already there */ + SYSTRAP(compat_16___sigreturn14) /* do sigreturn */ + movl %eax,4(%esp) /* error code */ + SYSTRAP(exit) /* exit */ diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_msgctl.S new file mode 100644 index 000000000..cc08d76ef --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_shmctl.S new file mode 100644 index 000000000..10d43f71d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigaction.S new file mode 100644 index 000000000..a5f8ad810 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigpending.S new file mode 100644 index 000000000..55ad1b7db --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigpending.S @@ -0,0 +1,51 @@ +/* $NetBSD: compat_sigpending.S,v 1.1 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigpending.s 5.1 (Berkeley) 7/1/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigpending.S,v 1.1 2005/09/13 01:44:08 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +_SYSCALL(sigpending,compat_13_sigpending13) + movl 4(%esp),%ecx # fetch pointer to... + movl %eax,(%ecx) # store old mask + xorl %eax,%eax + ret diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigprocmask.S new file mode 100644 index 000000000..246cf690d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigprocmask.S @@ -0,0 +1,73 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.1 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigprocmask.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigprocmask.S,v 1.1 2005/09/13 01:44:08 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +ENTRY(sigprocmask) + movl 8(%esp),%ecx # fetch new sigset pointer + testl %ecx,%ecx # check new sigset pointer + jnz 1f # if not null, indirect +/* movl $0,8(%esp) # null mask pointer: block empty set */ + movl $1,4(%esp) # SIG_BLOCK + jmp 2f +1: movl (%ecx),%ecx # fetch indirect ... + movl %ecx,8(%esp) # to new mask arg +2: SYSTRAP(compat_13_sigprocmask13) + jc err + movl 12(%esp),%ecx # fetch old mask requested + testl %ecx,%ecx # test if old mask requested + jz out + movl %eax,(%ecx) # store old mask +out: + xorl %eax,%eax + ret +err: +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigreturn.S new file mode 100644 index 000000000..c3da3cb39 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigreturn.S @@ -0,0 +1,62 @@ +/* $NetBSD: compat_sigreturn.S,v 1.2 2011/01/12 23:12:11 joerg Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigreturn.S,v 1.2 2011/01/12 23:12:11 joerg Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ +#ifdef GPROF +#undef ENTRY +#ifdef __ELF__ +#define MCOUNT_SYMBOL __mcount +#else +#define MCOUNT_SYMBOL mcount +#endif +#define ENTRY(x) \ + .globl _ ## x; _ALIGN_TEXT; _ ## x: pusha ; \ + .data; 1:; .long 0; .text; movl $1b,%eax; call MCOUNT_SYMBOL; popa ; nop +#endif /* GPROF */ + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +PSEUDO(sigreturn,compat_13_sigreturn13) diff --git a/lib/nbsd_libc/compat/arch/i386/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigsuspend.S new file mode 100644 index 000000000..b1bdb2ac0 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/i386/sys/compat_sigsuspend.S @@ -0,0 +1,63 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.1 2005/09/13 01:44:08 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigsuspend.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigsuspend.S,v 1.1 2005/09/13 01:44:08 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend) + movl 4(%esp),%eax # fetch mask arg + movl (%eax),%eax # indirect to mask arg + movl %eax,4(%esp) + SYSTRAP(compat_13_sigsuspend13) + jc err + xorl %eax,%eax # shouldn t happen + ret +err: +#ifdef PIC + PIC_PROLOGUE + mov PIC_GOT(CERROR), %ecx + PIC_EPILOGUE + jmp *%ecx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/compat/arch/ia64/Makefile.inc b/lib/nbsd_libc/compat/arch/ia64/Makefile.inc new file mode 100644 index 000000000..81c5297f9 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/ia64/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/09/23 17:38:03 cherry Exp $ + +.include "${COMPATARCHDIR}/sys/Makefile.inc" + diff --git a/lib/nbsd_libc/compat/arch/ia64/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/ia64/sys/Makefile.inc new file mode 100644 index 000000000..f7d1a2c49 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/ia64/sys/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/09/23 17:38:03 cherry Exp $ + +SRCS+= compat___semctl.S diff --git a/lib/nbsd_libc/compat/arch/ia64/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/ia64/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/ia64/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/m68k/Makefile.inc b/lib/nbsd_libc/compat/arch/m68k/Makefile.inc new file mode 100644 index 000000000..78ab575bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 23:04:03 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/m68k/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/m68k/gen/Makefile.inc new file mode 100644 index 000000000..260fd9ce4 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/gen/Makefile.inc @@ -0,0 +1,8 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/07/03 12:54:34 drochner Exp $ + +# objects built from assembler sources (need lint stubs) +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c +#SRCS+= compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/m68k/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/m68k/gen/compat_setjmp.S new file mode 100644 index 000000000..49045e0d3 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/gen/compat_setjmp.S @@ -0,0 +1,95 @@ +/* $NetBSD: compat_setjmp.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)setjmp.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: compat_setjmp.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +ENTRY(setjmp) + lea %sp@(-12),%sp /* space for sigstack args/rvals */ + clrl %sp@ /* don't change it... */ + movl %sp,%sp@(4) /* ...but return the current val */ + jbsr PIC_PLT(_C_LABEL(__sigaltstack14)) + /* note: flags returned in sp@(8) */ + clrl %sp@ /* don't change mask, just return */ + jbsr PIC_PLT(_C_LABEL(sigblock)) /* old value */ + movl %sp@(8),%d1 /* old flags value */ + andl #1,%d1 /* extract onstack flag */ + lea %sp@(12),%sp + movl %sp@(4),%a0 /* save area pointer */ + movl %d1,%a0@+ /* save old onstack value */ + movl %d0,%a0@+ /* save old signal mask */ + lea %sp@(4),%a1 /* adjust saved SP since we won't rts */ + movl %a1,%a0@+ /* save old SP */ + movl %a6,%a0@+ /* save old FP */ + clrl %a0@+ /* no AP */ + movl %sp@,%a0@+ /* save old PC */ + clrl %a0@+ /* clean PS */ + moveml #0x3CFC,%a0@ /* save remaining non-scratch regs */ + clrl %d0 /* return 0 */ + rts + +ENTRY(longjmp) + movl %sp@(4),%a0 /* save area pointer */ + tstl %a0@(8) /* ensure non-zero SP */ + jeq botch /* oops! */ + movl %sp@(8),%d0 /* grab return value */ + jne ok /* non-zero ok */ + moveq #1,%d0 /* else make non-zero */ +ok: + moveml %a0@(28),#0x3CFC /* restore non-scratch regs */ + movl %a0,%sp@- /* let sigreturn */ + jbsr PIC_PLT(_C_LABEL(sigreturn)) /* finish for us */ + +botch: + jbsr PIC_PLT(_C_LABEL(longjmperror)) + stop #0 diff --git a/lib/nbsd_libc/compat/arch/m68k/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/m68k/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..4c57960ec --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/gen/compat_sigsetjmp.S @@ -0,0 +1,77 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)_setjmp.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: compat_sigsetjmp.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a,m) + * by restoring registers from the stack, + * The previous signal state is restored if 'm' was non-zero. + */ + +/* grab _JBLEN */ +#include + +ENTRY(sigsetjmp) + movl %sp@(8),%d1 /* grab the mask */ + movl %sp@(4),%a0 /* grab the area pointer */ + movl %d1,%a0@(_JBLEN * 4 ) /* save at end of area */ + tstl %d1 + bne dosig + jra PIC_PLT(_C_LABEL(_setjmp)) +dosig: + jra PIC_PLT(_C_LABEL(setjmp)) + + +ENTRY(siglongjmp) + movl %sp@(4),%a0 /* save area pointer */ + tstl %a0@(_JBLEN * 4) /* check mask... */ + bne didsig + jra PIC_PLT(_C_LABEL(_longjmp)) +didsig: + jra PIC_PLT(_C_LABEL(longjmp)) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/m68k/sys/Makefile.inc new file mode 100644 index 000000000..768d04cf0 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/09 16:20:55 christos Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_Ovfork.S new file mode 100644 index 000000000..4935ec9fd --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_Ovfork.S @@ -0,0 +1,110 @@ +/* $NetBSD: compat_Ovfork.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)Ovfork.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: compat_Ovfork.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * @(#)vfork.s 4.1 (Berkeley) 12/21/80 + * C library -- vfork + */ + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +/* + * pid = vfork(); + * + * d1 == 0 in parent process, d1 == 1 in child process. + * d0 == pid of child in parent, d0 == pid of parent in child. + * + * trickery here, due to keith sklower, uses ret to clear the stack, + * and then returns with a jump indirect, since only one person can return + * with a ret off this stack... we do the ret before we vfork! + */ + +ENTRY(vfork) + movl %sp@+,%a1 + movl #SYS_vfork,%d0 + trap #0 + jcs err + subql #1,%d1 /* from 1 to 0 in child, 0 to -1 in parent */ + andl %d1,%d0 + jmp %a1@ +err: +#ifdef _REENTRANT + .globl _C_LABEL(__errno) + movl %a1,%sp@- + movl %d0,%sp@- +#if defined(PIC) && !defined(__ELF__) + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a1 + lea %pc@(0,a1:l),%a1 + movl %a1@(_C_LABEL(__errno):l),%a1 + jsr %a1@ +#else + jbsr PIC_PLT(_C_LABEL(__errno)) +#endif /* PIC */ +#ifdef __SVR4_ABI__ + movl %sp@+,%a0@ +#else + movl %d0,%a1 + movl %sp@+,%a1@ +#endif /* __SVR4_ABI__ */ + movl %sp@+,%a1 +#else + .globl _C_LABEL(errno) +#ifdef PIC +#ifdef __ELF__ + lea %pc@(_GLOBAL_OFFSET_TABLE_@GOTPC),%a0 + movl %a0@(_C_LABEL(errno)@GOT:w),%a0 +#else + movl #_C_LABEL(_GLOBAL_OFFSET_TABLE_),%a0 + lea %pc@(0,a0:l),%a0 + movl %a0@(_C_LABEL(errno):w),%a0 +#endif + movl %d0,%a0@ +#else + movl %d0,_C_LABEL(errno) +#endif /* PIC */ +#endif /* _REENTRANT */ + moveq #-1,%d0 + jmp %a1@ diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat___sigreturn14.S new file mode 100644 index 000000000..8a97da0fa --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat___sigreturn14.S @@ -0,0 +1,66 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigreturn.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: compat___sigreturn14.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * We must preserve the state of the registers as the user has set them up. + */ +#ifdef GPROF +#undef ENTRY +#ifdef __STDC__ +#define ENTRY(x) \ + .globl _C_LABEL(x); .even; _C_LABEL(x):; moveml #0xC0C0,%sp@-; .data; \ + PROF ## x:; .long 0; .text; lea PROF ## x,%a0; jbsr MCOUNT_ENTRY; \ + moveml %sp@+,#0x0303 +#else +#define ENTRY(x) \ + .globl _C_LABEL(x); .even; _C_LABEL(x):; moveml #0xC0C0,%sp@-; .data; \ + PROF/**/x:; .long 0; .text; lea PROF/**/x,%a0; jbsr MCOUNT_ENTRY; \ + moveml %sp@+,#0x0303 +#endif /* __STDC__ */ +#endif /* GPROF */ + +ENTRY(__sigreturn14) + trap #3 /* special sigreturn syscall entry point */ + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat___sigtramp1.S new file mode 100644 index 000000000..8c3162bc1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat___sigtramp1.S @@ -0,0 +1,51 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +/* + * The m68k signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * sigcontext structure [12] + * pointer to sigcontext structure [8] + * signal specific code [4] + * sp-> signal number [0] + */ +ENTRY_NOPROFILE(__sigtramp_sigcontext_1) + leal %sp@(12),%a0 /* get pointer to sigcontext */ + movl %a0,%sp@(4) /* put it in the argument slot */ + /* fake return address already there */ + trap #3 /* special sigreturn trap */ + movl %d0,%sp@(4) /* failed, exit with errno */ + SYSTRAP(exit) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_msgctl.S new file mode 100644 index 000000000..cc08d76ef --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_shmctl.S new file mode 100644 index 000000000..10d43f71d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigaction.S new file mode 100644 index 000000000..a5f8ad810 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigpending.S new file mode 100644 index 000000000..4202f3a39 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigpending.S @@ -0,0 +1,53 @@ +/* $NetBSD: compat_sigpending.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigpending.s 5.2 (Berkeley) 8/6/90") +#else + RCSID("$NetBSD: compat_sigpending.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +_SYSCALL(sigpending,compat_13_sigpending13) + movl %sp@(4),%a0 + movl %d0,%a0@ + clrl %d0 + rts diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigprocmask.S new file mode 100644 index 000000000..485ae33c7 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigprocmask.S @@ -0,0 +1,70 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigprocmask.s 5.2 (Berkeley) 6/6/90") +#else + RCSID("$NetBSD: compat_sigprocmask.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +ENTRY(sigprocmask) + tstl %sp@(8) /* check new sigset pointer */ + jne gotptr /* if not null, indirect */ +/* movl #0,%sp@(8) /* null mask pointer: block empty set */ + movl #1,%sp@(4) /* SIG_BLOCK */ + jra doit +gotptr: + movl %sp@(8),%a0 + movl %a0@,%sp@(8) /* indirect to new mask arg */ +doit: + movl #SYS_compat_13_sigprocmask13,%d0 + trap #0 + jcs err + tstl %sp@(12) /* test if old mask requested */ + jeq out + movl %sp@(12),%a0 + movl %d0,%a0@ /* store old mask */ +out: + clrl %d0 + rts +err: + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigreturn.S new file mode 100644 index 000000000..33357edbb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigreturn.S @@ -0,0 +1,72 @@ +/* $NetBSD: compat_sigreturn.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigreturn.s 5.1 (Berkeley) 5/12/90") +#else + RCSID("$NetBSD: compat_sigreturn.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * We must preserve the state of the registers as the user has set them up. + */ +#ifdef GPROF +#undef ENTRY +#ifdef __STDC__ +#define ENTRY(x) \ + .globl _C_LABEL(x); .even; _C_LABEL(x):; moveml #0xC0C0,%sp@-; .data; \ + PROF ## x:; .long 0; .text; lea PROF ## x,%a0; jbsr MCOUNT_ENTRY; \ + moveml %sp@+,#0x0303 +#else +#define ENTRY(x) \ + .globl _C_LABEL(x); .even; _C_LABEL(x):; moveml #0xC0C0,%sp@-; .data; \ + PROF/**/x:; .long 0; .text; lea PROF/**/x,%a0; jbsr MCOUNT_ENTRY; \ + moveml %sp@+,#0x0303 +#endif /* __STDC__ */ +#endif /* GPROF */ + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +/* + * NOTE: Trap #1 is used for compat_13_sigreturn13 on the m68k! + */ +ENTRY(sigreturn) + trap #1 /* signals compat_13_sigreturn13() */ + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigsuspend.S new file mode 100644 index 000000000..af7c3edea --- /dev/null +++ b/lib/nbsd_libc/compat/arch/m68k/sys/compat_sigsuspend.S @@ -0,0 +1,58 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.1 2006/03/09 16:20:55 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * the Systems Programming Group of the University of Utah Computer + * Science Department. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigsuspend.s 5.2 (Berkeley) 6/6/90") +#else + RCSID("$NetBSD: compat_sigsuspend.S,v 1.1 2006/03/09 16:20:55 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend) + movl %sp@(4),%a0 + movl %a0@,%sp@(4) /* indirect to mask arg */ + movl #SYS_compat_13_sigsuspend13,%d0 + trap #0 + jcs err + clrl %d0 /* shouldn't happen */ + rts +err: + jra PIC_PLT(CERROR) diff --git a/lib/nbsd_libc/compat/arch/mips/Makefile.inc b/lib/nbsd_libc/compat/arch/mips/Makefile.inc new file mode 100644 index 000000000..78ab575bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 23:04:03 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/mips/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/mips/gen/Makefile.inc new file mode 100644 index 000000000..3aa7a9002 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/gen/Makefile.inc @@ -0,0 +1,8 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/07/03 12:58:55 drochner Exp $ + +# objects built from assembler sources (need lint stubs) +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c +#SRCS+= compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/mips/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/mips/gen/compat_setjmp.S new file mode 100644 index 000000000..a3028f787 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/gen/compat_setjmp.S @@ -0,0 +1,162 @@ +/* $NetBSD: compat_setjmp.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "assym.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)setjmp.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: compat_setjmp.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +#define SETJMP_FRAME_SIZE (CALLFRAME_SIZ + STACK_T_SIZE) + +NON_LEAF(setjmp, SETJMP_FRAME_SIZE, ra) + .mask 0x80010000, (CALLFRAME_RA - CALLFRAME_SIZ) + SETUP_GP + PTR_SUBU sp, sp, SETJMP_FRAME_SIZE # allocate stack frame + SAVE_GP(CALLFRAME_GP) + SETUP_GP64(CALLFRAME_GP, setjmp) + + REG_S ra, CALLFRAME_RA(sp) # save RA + REG_S s0, CALLFRAME_S0(sp) # save S0 + move s0, a0 # save sigcontext + + /* Get the signal mask. */ + move a0, zero # get current sigmask + jal _C_LABEL(sigblock) + nop + INT_S v0, _OFFSETOF_SC_MASK13(s0) # save sc_mask13 + + /* Get the signal stack. */ + move a0, zero + PTR_ADDU a1, sp, CALLFRAME_SIZ # pointer to stack_t + jal _C_LABEL(__sigaltstack14) + + move a0, s0 # restore jmpbuf + INT_L v1, CALLFRAME_SIZ+_OFFSETOF_STACK_T_FLAGS(sp) + # get old ss_onstack + and v1, v1, SS_ONSTACK # extract onstack flag + INT_S v1, _OFFSETOF_SC_ONSTACK(a0) # save it in sc_onstack + + REG_L s0, CALLFRAME_S0(sp) # restore S0 + REG_L ra, CALLFRAME_RA(sp) # restore RA + blt v0, zero, botch # check for sigaltstack() error + nop + /* + * We know we won't need this routine's GP anymore. + */ + RESTORE_GP64 + PTR_ADDU sp, sp, SETJMP_FRAME_SIZE # pop stack frame + + REG_PROLOGUE + REG_S ra, _OFFSETOF_SC_PC(a0) # sc_pc = return address + REG_LI v0, 0xACEDBADE # sigcontext magic number + REG_S v0, _OFFSETOF_SC_REGS(a0) # saved in sc_regs[0] + REG_S s0, _OFFSETOF_SC_REGS_S0(a0) + REG_S s1, _OFFSETOF_SC_REGS_S1(a0) + REG_S s2, _OFFSETOF_SC_REGS_S2(a0) + REG_S s3, _OFFSETOF_SC_REGS_S3(a0) + REG_S s4, _OFFSETOF_SC_REGS_S4(a0) + REG_S s5, _OFFSETOF_SC_REGS_S5(a0) + REG_S s6, _OFFSETOF_SC_REGS_S6(a0) + REG_S s7, _OFFSETOF_SC_REGS_S7(a0) + REG_S gp, _OFFSETOF_SC_REGS_GP(a0) + REG_S sp, _OFFSETOF_SC_REGS_SP(a0) + REG_S s8, _OFFSETOF_SC_REGS_S8(a0) + li v0, 1 # be nice if we could tell + INT_S v0, _OFFSETOF_SC_FPUSED(a0) # sc_fpused = 1 + cfc1 v0, $31 + INT_S v0, _OFFSETOF_SC_FPREGS_FCSR(a0) +#if defined(__mips_o32) || defined(__mips_o64) || defined(__mips_n32) + FP_S $f20, _OFFSETOF_SC_FPREGS_F20(a0) + FP_S $f22, _OFFSETOF_SC_FPREGS_F22(a0) +#endif +#if defined(__mips_o32) || defined(__mips_o64) + FP_S $f21, _OFFSETOF_SC_FPREGS_F21(a0) + FP_S $f23, _OFFSETOF_SC_FPREGS_F23(a0) +#endif +#if defined(__mips_n32) || defined(__mips_n64) + FP_S $f24, _OFFSETOF_SC_FPREGS_F24(a0) + FP_S $f26, _OFFSETOF_SC_FPREGS_F26(a0) + FP_S $f28, _OFFSETOF_SC_FPREGS_F28(a0) + FP_S $f30, _OFFSETOF_SC_FPREGS_F30(a0) +#endif +#if defined(__mips_n64) + FP_S $f25, _OFFSETOF_SC_FPREGS_F25(a0) + FP_S $f27, _OFFSETOF_SC_FPREGS_F27(a0) + FP_S $f29, _OFFSETOF_SC_FPREGS_F29(a0) + FP_S $f31, _OFFSETOF_SC_FPREGS_F31(a0) +#endif + REG_EPILOGUE + j ra + move v0, zero + +botch: + jal _C_LABEL(abort) +END(setjmp) + +LEAF(longjmp) + SETUP_GP + PTR_SUBU sp, sp, CALLFRAME_SIZ + SAVE_GP(CALLFRAME_S0) + SETUP_GP64(s0, longjmp) + + REG_PROLOGUE + /* save return value in sc_regs[_R_V0] */ + REG_S a1, _OFFSETOF_SC_REGS_V0(a0) + REG_EPILOGUE + + li v0, SYS_compat_13_sigreturn13 + syscall + + jal _C_LABEL(longjmperror) + jal _C_LABEL(abort) +END(longjmp) diff --git a/lib/nbsd_libc/compat/arch/mips/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/mips/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..e7c8b968c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/gen/compat_sigsetjmp.S @@ -0,0 +1,83 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1995, + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Havard Eidnes. + * + * 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 "assym.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + ASMSTR("from: @(#)setjmp.s 8.1 (Berkeley) 6/4/93") +#else + ASMSTR("$NetBSD: compat_sigsetjmp.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- sigsetjmp, siglongjmp + * + * siglongjmp(a,v) + * will generate a "return(v)" from + * the last call to + * sigsetjmp(a, savemask) + * by restoring registers from the stack, + * and dependent on savemask restores the + * signal mask. + */ + +LEAF(sigsetjmp) + SETUP_GP + SETUP_GP64(t0, sigsetjmp) + INT_S a1, _OFFSETOF_SC_MASK13(a0) # save "savemask" + bne a1, 0x0, 1f # do saving of signal mask? + PTR_LA t9, _C_LABEL(_setjmp) + jr t9 + +1: PTR_LA t9, _C_LABEL(setjmp) + jr t9 +END(sigsetjmp) + +LEAF(siglongjmp) + SETUP_GP + SETUP_GP64(t0, siglongjmp) + INT_L t1, _OFFSETOF_SC_MASK13(a0) # get "savemask" + bne t1, 0x0, 1f # restore signal mask? + PTR_LA t9, _C_LABEL(_longjmp) + jr t9 + +1: PTR_LA t9, _C_LABEL(longjmp) + jr t9 +END(siglongjmp) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/mips/sys/Makefile.inc new file mode 100644 index 000000000..73e76ccd9 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/17 11:49:39 tsutsui Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_Ovfork.S new file mode 100644 index 000000000..758f9ace4 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_Ovfork.S @@ -0,0 +1,68 @@ +/* $NetBSD: compat_Ovfork.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)Ovfork.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: compat_Ovfork.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +/* + * pid = vfork(); + * + * v1 == 0 in parent process, v1 == 1 in child process. + * v0 == pid of child in parent, v0 == pid of parent in child. + */ + +LEAF(vfork) + PIC_PROLOGUE(vfork) + + SYSTRAP(vfork) # system call number for vfork + beq a3, zero, 1f # jump if no errors + + PIC_TAILCALL(__cerror) + +1: + beq v1, zero, 2f # parent process ? + move v0, zero # return zero in child +2: + PIC_RETURN() +END(vfork) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/mips/sys/compat___semctl.S new file mode 100644 index 000000000..53f21ef57 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat___semctl.S @@ -0,0 +1,39 @@ +/* $NetBSD: compat___semctl.S,v 1.4 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat___semctl.S,v 1.4 2009/12/14 03:04:33 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/mips/sys/compat___sigreturn14.S new file mode 100644 index 000000000..b2536af10 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat___sigreturn14.S @@ -0,0 +1,49 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigreturn.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: compat___sigreturn14.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * We must preserve the state of the registers as the user has set them up. + */ + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/mips/sys/compat___sigtramp1.S new file mode 100644 index 000000000..71680d006 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat___sigtramp1.S @@ -0,0 +1,51 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.4 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat___sigtramp1.S,v 1.4 2009/12/14 03:04:33 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +/* + * The MIPS signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * sp-> sigcontext structure + */ +LEAF_NOPROFILE(__sigtramp_sigcontext_1) + move a0, sp /* address of sigcontext */ + SYSTRAP(compat_16___sigreturn14) /* and do sigreturn */ + move a0, v0 /* exit with errno */ + SYSTRAP(exit) /* if sigreturn fails */ +END(__sigtramp_sigcontext_1) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_msgctl.S new file mode 100644 index 000000000..5457c6c4c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_msgctl.S @@ -0,0 +1,42 @@ +/* $NetBSD: compat_msgctl.S,v 1.3 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_msgctl.S,v 1.3 2009/12/14 01:07:42 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_shmctl.S new file mode 100644 index 000000000..57c842155 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_shmctl.S @@ -0,0 +1,42 @@ +/* $NetBSD: compat_shmctl.S,v 1.3 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_shmctl.S,v 1.3 2009/12/14 01:07:42 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigaction.S new file mode 100644 index 000000000..b2506acd5 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigaction.S @@ -0,0 +1,42 @@ +/* $NetBSD: compat_sigaction.S,v 1.3 2009/12/14 01:07:42 matt Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigaction.S,v 1.3 2009/12/14 01:07:42 matt Exp $") +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigpending.S new file mode 100644 index 000000000..93c99e824 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigpending.S @@ -0,0 +1,60 @@ +/* $NetBSD: compat_sigpending.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigpending.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: compat_sigpending.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +LEAF(sigpending) + PIC_PROLOGUE(sigpending) + + SYSTRAP(compat_13_sigpending13) + bne a3, zero, 1f + INT_S v0, _OFFSETOF_SC_ONSTACK(a0) + move v0, zero + PIC_RETURN() + +1: + PIC_TAILCALL(__cerror) +END(sigpending) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigprocmask.S new file mode 100644 index 000000000..913a139d1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigprocmask.S @@ -0,0 +1,66 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigprocmask.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: compat_sigprocmask.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +LEAF(sigprocmask) # sigprocmask(how, new, old) sigset_t *new, *old; + PIC_PROLOGUE(sigprocmask) + bne a1, zero, gotptr # if new sigset pointer not null + li a0, 1 # how = SIG_BLOCK + b doit # mask = zero +gotptr: + INT_L a1, 0(a1) # indirect to new mask arg +doit: + SYSTRAP(compat_13_sigprocmask13) + + bne a3, zero, err + beq a2, zero, out # test if old mask requested + INT_S v0, 0(a2) # store old mask +out: + move v0, zero + PIC_RETURN() +err: + PIC_TAILCALL(__cerror) +END(sigprocmask) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigreturn.S new file mode 100644 index 000000000..ac5df9dfc --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigreturn.S @@ -0,0 +1,52 @@ +/* $NetBSD: compat_sigreturn.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigreturn.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: compat_sigreturn.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * We must preserve the state of the registers as the user has set them up. + */ + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +PSEUDO(sigreturn,compat_13_sigreturn13) diff --git a/lib/nbsd_libc/compat/arch/mips/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigsuspend.S new file mode 100644 index 000000000..8c7b7c519 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/mips/sys/compat_sigsuspend.S @@ -0,0 +1,58 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.2 2009/12/14 03:04:33 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * 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 "SYS.h" + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + RCSID("from: @(#)sigsuspend.s 8.1 (Berkeley) 6/4/93") +#else + RCSID("$NetBSD: compat_sigsuspend.S,v 1.2 2009/12/14 03:04:33 matt Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +LEAF(sigsuspend) + PIC_PROLOGUE(sigsuspend) + + INT_L a0, 0(a0) # indirect to mask arg + SYSTRAP(compat_13_sigsuspend13) + bne a3, zero, 1f + move v0, zero # should not happen + PIC_RETURN() +1: + PIC_TAILCALL(__cerror) +END(sigsuspend) diff --git a/lib/nbsd_libc/compat/arch/powerpc/Makefile.inc b/lib/nbsd_libc/compat/arch/powerpc/Makefile.inc new file mode 100644 index 000000000..2f234ec4d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.3 2011/01/15 07:31:13 matt Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" + +CPPFLAGS+= -I. diff --git a/lib/nbsd_libc/compat/arch/powerpc/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/powerpc/gen/Makefile.inc new file mode 100644 index 000000000..0018c2bee --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/gen/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/06/28 14:46:33 drochner Exp $ + +# objects built from assembler sources (need lint stubs) +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/powerpc/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/powerpc/gen/compat_setjmp.S new file mode 100644 index 000000000..49d928326 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/gen/compat_setjmp.S @@ -0,0 +1,49 @@ +/* $NetBSD: compat_setjmp.S,v 1.2 2011/01/15 07:31:13 matt Exp $ */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: compat_setjmp.S,v 1.2 2011/01/15 07:31:13 matt Exp $") +#endif + +/* + * C library -- _setjmp, _longjmp + * + * longjmp(a,v) + * will generate a "return(v?v:1)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(setjmp) + mr %r6,%r3 + li %r3,SIG_BLOCK + li %r4,0 + _DOSYSCALL(compat_13_sigprocmask13) # assume no error XXX + mflr %r11 + mfcr %r12 + mr %r10,%r1 + mr %r9,%r2 + mr %r8,%r3 + stmw %r8,4(%r6) + li %r3,0 + blr +END(setjmp) + +ENTRY(longjmp) + lmw %r8,4(%r3) + mr %r6,%r4 + mtlr %r11 + mtcr %r12 + mr %r2,%r9 + mr %r1,%r10 + mr %r4,%r8 + li %r3,SIG_SETMASK + _DOSYSCALL(compat_13_sigprocmask13) # assume no error XXX + or. %r3,%r6,%r6 + bnelr + li %r3,1 + blr +END(longjmp) diff --git a/lib/nbsd_libc/compat/arch/powerpc/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/powerpc/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..9ebe269b2 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/gen/compat_sigsetjmp.S @@ -0,0 +1,45 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.2 2011/01/15 07:31:13 matt Exp $ */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: compat_sigsetjmp.S,v 1.2 2011/01/15 07:31:13 matt Exp $") +#endif + +ENTRY(sigsetjmp) + mr %r6,%r3 + or. %r7,%r4,%r4 + beq 1f + li %r3,SIG_BLOCK + li %r4,0 + _DOSYSCALL(compat_13_sigprocmask13) # assume no error XXX +1: + mflr %r11 + mfcr %r12 + mr %r10,%r1 + mr %r9,%r2 + mr %r8,%r3 + stmw %r7,0(%r6) + li %r3,0 + blr +END(sigsetjmp) + +ENTRY(siglongjmp) + lmw %r7,0(%r3) + mr %r6,%r4 + mtlr %r11 + mtcr %r12 + mr %r2,%r9 + mr %r1,%r10 + or. %r7,%r7,%r7 + beq 1f + mr %r4,%r8 + li %r3,SIG_SETMASK + _DOSYSCALL(compat_13_sigprocmask13) # assume no error XXX +1: + or. %r3,%r6,%r6 + bnelr + li %r3,1 + blr +END(siglongjmp) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/powerpc/sys/Makefile.inc new file mode 100644 index 000000000..425cfc4da --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/02/18 00:21:28 matt Exp $ + +SRCS+= compat_Ovfork.S \ + compat___sigreturn14.S compat___sigtramp1.S \ + compat_msgctl.S compat___semctl.S compat_shmctl.S \ + compat_sigaction13.S compat_sigpending13.S compat_sigprocmask13.S \ + compat_sigreturn13.S compat_sigsuspend13.S diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_Ovfork.S new file mode 100644 index 000000000..4c39ceba9 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_Ovfork.S @@ -0,0 +1,23 @@ +/* $NetBSD: compat_Ovfork.S,v 1.2 2011/01/15 07:31:13 matt Exp $ */ + +/* + * pid = vfork(); + * + * r4 == 0 in parent process, 1 in child process. + * r3 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: compat_Ovfork.S,v 1.2 2011/01/15 07:31:13 matt Exp $") +#endif + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +SYSCALL(vfork) + addi %r4,%r4,-1 # from 1 to 0 in child, 0 to -1 in parent + and %r3,%r3,%r4 # return 0 in child, pid in parent + blr +END(vfork) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat___sigreturn14.S new file mode 100644 index 000000000..b075c3135 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat___sigreturn14.S @@ -0,0 +1,5 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2006/02/18 00:21:28 matt Exp $ */ + +#include "SYS.h" + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat___sigtramp1.S new file mode 100644 index 000000000..ff6e30ac9 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat___sigtramp1.S @@ -0,0 +1,55 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.3 2011/01/15 07:31:13 matt Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: compat___sigtramp1.S,v 1.3 2011/01/15 07:31:13 matt Exp $") +#endif + +/* + * On entry, stack and registers look like: + * + * r3 signal number + * r4 signal specific code + * r5 pointer to sigcontext structure + * lr address of handler + * + * sp-> sigcontext structure + */ +ENTRY_NOPROFILE(__sigtramp_sigcontext_1) + addi %r1,%r1,-CALLFRAMELEN /* space for callee */ + blrl /* call handler */ + addi %r3,%r1,CALLFRAMELEN /* compute address of sigcontext */ + _DOSYSCALL(compat_16___sigreturn14) /* and call sigreturn */ + _DOSYSCALL(exit) /* or exit with errno if failed */ +END(__sigtramp_sigcontext_1) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_msgctl.S new file mode 100644 index 000000000..26ccc3ced --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_msgctl.S @@ -0,0 +1,8 @@ +/* $NetBSD: compat_msgctl.S,v 1.1 2006/02/18 00:21:28 matt Exp $ */ + +#include "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_shmctl.S new file mode 100644 index 000000000..10d43f71d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigaction13.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigaction13.S new file mode 100644 index 000000000..42c5ed6b4 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigaction13.S @@ -0,0 +1,8 @@ +/* $NetBSD: compat_sigaction13.S,v 1.1 2006/02/18 00:21:28 matt Exp $ */ + +#include "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigpending13.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigpending13.S new file mode 100644 index 000000000..27867cc30 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigpending13.S @@ -0,0 +1,21 @@ +/* $NetBSD: compat_sigpending13.S,v 1.3 2011/01/16 02:45:48 matt Exp $ */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: compat_sigpending13.S,v 1.3 2011/01/16 02:45:48 matt Exp $") +#endif + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +ENTRY(sigpending) + mr %r5,%r3 # save pointer + _DOSYSCALL(compat_13_sigpending13) # sigpending() + bso 1f + stw %r3,0(%r5) # store return value + li %r3,0 # and return 0 + blr +1: + b _C_LABEL(__cerror) +END(sigpending) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigprocmask13.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigprocmask13.S new file mode 100644 index 000000000..08ee8bfcc --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigprocmask13.S @@ -0,0 +1,34 @@ +/* $NetBSD: compat_sigprocmask13.S,v 1.2 2011/01/15 07:31:13 matt Exp $ */ + +#include "SYS.h" +#include "assym.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: compat_sigprocmask13.S,v 1.2 2011/01/15 07:31:13 matt Exp $") +#endif + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +/* + * sigprocmask(int how, sigset_t *set, sigset_t *oset) + */ +ENTRY(sigprocmask) + or. %r4,%r4,%r4 # set == NULL? + li %r6,SIG_BLOCK + beq 1f + lwz %r4,0(%r4) # if not, replace it in r4 with *set + mr %r6,%r3 +1: + mr %r3,%r6 # ... using sigprocmask(SIG_BLOCK) + _DOSYSCALL(compat_13_sigprocmask13) + bso 3f + or. %r5,%r5,%r5 # check to see of oset requested + beq 2f # if oset != NULL, + stw %r3,0(%r5) # *oset = oldmask +2: + li %r3,0 + blr # in any case, return 0 +3: + b _C_LABEL(__cerror) +END(sigprocmask) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigreturn13.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigreturn13.S new file mode 100644 index 000000000..0c5020943 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigreturn13.S @@ -0,0 +1,8 @@ +/* $NetBSD: compat_sigreturn13.S,v 1.1 2006/02/18 00:21:28 matt Exp $ */ + +#include "SYS.h" + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +PSEUDO(sigreturn,compat_13_sigreturn13) diff --git a/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigsuspend13.S b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigsuspend13.S new file mode 100644 index 000000000..fa0374a01 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc/sys/compat_sigsuspend13.S @@ -0,0 +1,16 @@ +/* $NetBSD: compat_sigsuspend13.S,v 1.2 2011/01/15 07:31:13 matt Exp $ */ + +#include "SYS.h" + +#if defined(LIBC_SCCS) +__RCSID("$NetBSD: compat_sigsuspend13.S,v 1.2 2011/01/15 07:31:13 matt Exp $") +#endif + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend) + lwz %r3,0(%r3) # indirect to mask arg + _DOSYSCALL(compat_13_sigsuspend13) + b _C_LABEL(__cerror) # always terminates with EINTR +END(sigsuspend) diff --git a/lib/nbsd_libc/compat/arch/powerpc64/Makefile.inc b/lib/nbsd_libc/compat/arch/powerpc64/Makefile.inc new file mode 100644 index 000000000..edf1a86ed --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc64/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/07/01 19:14:49 ross Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/powerpc64/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/powerpc64/gen/Makefile.inc new file mode 100644 index 000000000..91d77fbeb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc64/gen/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/07/01 19:17:32 ross Exp $ + +# objects built from assembler sources (need lint stubs) +SRCS+= + +# objects built from C sources in compat/gen +SRCS+= diff --git a/lib/nbsd_libc/compat/arch/powerpc64/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/powerpc64/sys/Makefile.inc new file mode 100644 index 000000000..2b441c41e --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc64/sys/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/07/07 06:59:04 ross Exp $ + +SRCS+= compat_msgctl.S compat___semctl.S compat_shmctl.S diff --git a/lib/nbsd_libc/compat/arch/powerpc64/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/powerpc64/sys/compat___semctl.S new file mode 100644 index 000000000..80503100d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc64/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/powerpc64/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/powerpc64/sys/compat_msgctl.S new file mode 100644 index 000000000..1e3309aa0 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc64/sys/compat_msgctl.S @@ -0,0 +1,8 @@ +/* $NetBSD: compat_msgctl.S,v 1.1 2006/07/07 06:59:04 ross Exp $ */ + +#include "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/powerpc64/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/powerpc64/sys/compat_shmctl.S new file mode 100644 index 000000000..10d43f71d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/powerpc64/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:58 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/sh3/Makefile.inc b/lib/nbsd_libc/compat/arch/sh3/Makefile.inc new file mode 100644 index 000000000..30de512ae --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/03/11 23:04:04 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/sh3/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/sh3/gen/Makefile.inc new file mode 100644 index 000000000..4faa8a2b2 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/gen/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.3 2006/06/28 14:46:33 drochner Exp $ + +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/sh3/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/sh3/gen/compat_setjmp.S new file mode 100644 index 000000000..ce296fd7c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/gen/compat_setjmp.S @@ -0,0 +1,125 @@ +/* $NetBSD: compat_setjmp.S,v 1.3 2006/01/05 19:21:37 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: compat_setjmp.S,v 1.3 2006/01/05 19:21:37 uwe Exp $") +#endif + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is restored. + */ + +ENTRY(setjmp) + PIC_PROLOGUE(.L_got_1) + sts.l pr, @-sp + mov.l r4, @-sp + + mov.l .L_sigblock, r0 +1: CALL r0 /* int mask = sigblock(0); */ + mov #0, r4 + + mov.l @sp+, r4 + lds.l @sp+, pr + PIC_EPILOGUE + + /* identical to _setjmp except that mask is present */ + add #((_JB_SIGMASK + 1) * 4), r4 + mov.l r0, @-r4 /* current mask returned by sigblock */ + mov #1, r0 + mov.l r0, @-r4 /* has signal mask */ + mov.l r15, @-r4 + mov.l r14, @-r4 + mov.l r13, @-r4 + mov.l r12, @-r4 + mov.l r11, @-r4 + mov.l r10, @-r4 + mov.l r9, @-r4 + mov.l r8, @-r4 + sts.l pr, @-r4 + rts + xor r0, r0 + + .align 2 +.L_got_1: PIC_GOT_DATUM +.L_sigblock: CALL_DATUM(_C_LABEL(sigblock), 1b) + SET_ENTRY_SIZE(setjmp) + + +ENTRY(longjmp) + /* we won't return here, so we don't need to save pr and r12 */ + PIC_PROLOGUE_NOSAVE(.L_got_2) + mov.l r5, @-sp + mov.l r4, @-sp + + mov.l .L_sigsetmask, r0 +1: CALL r0 /* sigsetmask(saved mask) */ + mov.l @(_JB_SIGMASK * 4, r4), r4 + + mov.l @sp+, r4 + mov.l @sp+, r5 + + /* identical to _longjmp */ + lds.l @r4+, pr + mov.l @r4+, r8 + mov.l @r4+, r9 + mov.l @r4+, r10 + mov.l @r4+, r11 + mov.l @r4+, r12 + mov.l @r4+, r13 + mov.l @r4+, r14 + mov.l @r4+, r15 + + mov r5, r0 + tst r0, r0 /* make sure return value is non-zero */ + bf .L0 + add #1, r0 +.L0: + rts + nop + + .align 2 +.L_got_2: PIC_GOT_DATUM +.L_sigsetmask: CALL_DATUM(_C_LABEL(sigsetmask), 1b) + SET_ENTRY_SIZE(longjmp) diff --git a/lib/nbsd_libc/compat/arch/sh3/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/sh3/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..5260f7b66 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/gen/compat_sigsetjmp.S @@ -0,0 +1,122 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.5 2006/01/05 19:21:37 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)setjmp.s 5.1 (Berkeley) 4/23/90" + */ + +#include +#include + +#if defined(LIBC_SCCS) + RCSID("$NetBSD: compat_sigsetjmp.S,v 1.5 2006/01/05 19:21:37 uwe Exp $") +#endif + +ENTRY(sigsetjmp) + tst r5, r5 + mov.l r5, @(_JB_HAS_MASK * 4, r4) + bt 2f /* if (savemask == 0) */ + + PIC_PROLOGUE(.L_got_1) + sts.l pr, @-sp + mov.l r4, @-sp + + mov.l .L_sigblock, r0 +1: CALL r0 /* int mask = sigblock(0); */ + mov #0, r4 + + mov.l @sp+, r4 + lds.l @sp+, pr + PIC_EPILOGUE + + mov.l r0, @(_JB_SIGMASK * 4, r4) + +2: /* identical to _setjmp sans mask */ + add #((_JB_REG_R15 + 1) * 4), r4 + mov.l r15, @-r4 + mov.l r14, @-r4 + mov.l r13, @-r4 + mov.l r12, @-r4 + mov.l r11, @-r4 + mov.l r10, @-r4 + mov.l r9, @-r4 + mov.l r8, @-r4 + sts.l pr, @-r4 + rts + xor r0, r0 + + .align 2 +.L_got_1: PIC_GOT_DATUM +.L_sigblock: CALL_DATUM(_C_LABEL(sigblock), 1b) + SET_ENTRY_SIZE(sigsetjmp) + + +ENTRY(siglongjmp) + mov.l @(_JB_HAS_MASK * 4, r4), r0 + tst r0, r0 + bt 2f /* if no mask */ + + /* we won't return here, so we don't need to save pr and r12 */ + PIC_PROLOGUE_NOSAVE(.L_got_2) + mov.l r5, @-sp + mov.l r4, @-sp + + mov.l .L_sigsetmask, r0 +1: CALL r0 /* sigsetmask(saved mask) */ + mov.l @(_JB_SIGMASK * 4, r4), r4 + + mov.l @sp+, r4 + mov.l @sp+, r5 + +2: /* identical to _longjmp */ + lds.l @r4+, pr + mov.l @r4+, r8 + mov.l @r4+, r9 + mov.l @r4+, r10 + mov.l @r4+, r11 + mov.l @r4+, r12 + mov.l @r4+, r13 + mov.l @r4+, r14 + mov.l @r4+, r15 + + mov r5, r0 + tst r0, r0 /* make sure return value is non-zero */ + bf .L0 + add #1, r0 +.L0: + rts + nop + + .align 2 +.L_got_2: PIC_GOT_DATUM +.L_sigsetmask: CALL_DATUM(_C_LABEL(sigsetmask), 1b) + SET_ENTRY_SIZE(siglongjmp) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/sh3/sys/Makefile.inc new file mode 100644 index 000000000..44f37408d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/15 21:39:16 uwe Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_Ovfork.S new file mode 100644 index 000000000..7ed4d04a8 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_Ovfork.S @@ -0,0 +1,66 @@ +/* $NetBSD: compat_Ovfork.S,v 1.2 2006/01/06 04:30:33 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_Ovfork.S,v 1.2 2006/01/06 04:30:33 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +/* + * pid = vfork(); + * + * r1 == 0 in parent process, r1 == 1 in child process. + * r0 == pid of child in parent, r0 == pid of parent in child. + * + */ +ENTRY(vfork) + mov #SYS_vfork, r0 + trapa #0x80 + bf err + + add #0xff, r1 /* from 1 to 0 in child, 0 to -1 in parent */ + rts + and r1, r0 /* 0 in child, child pid in parent */ + +err: + JUMP_CERROR + + SET_ENTRY_SIZE(vfork) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat___semctl.S new file mode 100644 index 000000000..c64b6d3bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat___sigreturn14.S new file mode 100644 index 000000000..b151804e8 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat___sigreturn14.S @@ -0,0 +1,48 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2005/09/15 21:39:16 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat___sigreturn14.S,v 1.1 2005/09/15 21:39:16 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat___sigtramp1.S new file mode 100644 index 000000000..671b5cc0d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat___sigtramp1.S @@ -0,0 +1,46 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe. + * + * 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 "SYS.h" + +/* + * The SH signal trampoline is invoked only to return from + * the signal; the kernel calls the signal handler directly. + * + * On entry, stack looks like: + * + * sp-> sigcontext structure + */ +NENTRY(__sigtramp_sigcontext_1) + mov r15, r4 /* get pointer to sigcontext */ + SYSTRAP(compat_16___sigreturn14) /* and call sigreturn() */ + mov r0, r4 /* exit with errno */ + SYSTRAP(exit) /* if sigreturn fails */ diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_msgctl.S new file mode 100644 index 000000000..a5598b0b1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_shmctl.S new file mode 100644 index 000000000..a9f938c68 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigaction.S new file mode 100644 index 000000000..7d778432c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigpending.S new file mode 100644 index 000000000..39f2e62b6 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigpending.S @@ -0,0 +1,54 @@ +/* $NetBSD: compat_sigpending.S,v 1.2 2006/01/06 04:14:35 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigpending.s 5.1 (Berkeley) 7/1/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigpending.S,v 1.2 2006/01/06 04:14:35 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +/* + * int + * sigpending(sigset_t *set); + */ +_SYSCALL(sigpending,compat_13_sigpending13) + mov.l r0, @r4 /* store returned mask */ + rts + mov #0, r0 diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigprocmask.S new file mode 100644 index 000000000..f67a41446 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigprocmask.S @@ -0,0 +1,74 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.3 2006/01/06 04:08:47 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigprocmask.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigprocmask.S,v 1.3 2006/01/06 04:08:47 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +/* + * int + * sigprocmask(int how, const sigset_t * restrict set, + * sigset_t * restrict oset); + */ +ENTRY(sigprocmask) + tst r5, r5 /* set == NULL? */ + bf 1f + + bra 2f /* r5 (new mask) is already zero */ + mov #1, r4 /* how = SIG_BLOCK */ + +1: mov.l @r5, r5 /* fetch new mask */ + +2: mov #SYS_compat_13_sigprocmask13, r0 + trapa #0x80 + bf err + + tst r6, r6 /* oset == NULL? */ + bt 3f + mov.l r0, @r6 /* store old mask */ +3: rts + mov #0, r0 + +err: + JUMP_CERROR + + SET_ENTRY_SIZE(sigprocmask) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigreturn.S new file mode 100644 index 000000000..9376a7aa1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigreturn.S @@ -0,0 +1,51 @@ +/* $NetBSD: compat_sigreturn.S,v 1.1 2005/09/15 21:39:16 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigreturn.S,v 1.1 2005/09/15 21:39:16 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +PSEUDO(sigreturn,compat_13_sigreturn13) diff --git a/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigsuspend.S new file mode 100644 index 000000000..3f6fdf225 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sh3/sys/compat_sigsuspend.S @@ -0,0 +1,60 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.3 2006/01/06 04:05:55 uwe Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigsuspend.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigsuspend.S,v 1.3 2006/01/06 04:05:55 uwe Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +/* + * int sigsuspend(const sigset_t *sigmask); + */ +ENTRY(sigsuspend) + mov.l @r4, r4 /* syscall expects mask as the argument */ + mov #SYS_compat_13_sigsuspend13, r0 + trapa #0x80 + bf err + rts /* shouldn t happen */ + mov #0, r0 +err: + JUMP_CERROR + + SET_ENTRY_SIZE(sigsuspend) diff --git a/lib/nbsd_libc/compat/arch/sparc/Makefile.inc b/lib/nbsd_libc/compat/arch/sparc/Makefile.inc new file mode 100644 index 000000000..fc44d319a --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 23:04:04 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/sparc/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/sparc/gen/Makefile.inc new file mode 100644 index 000000000..ec25fc1bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/gen/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/07/03 13:06:12 drochner Exp $ + +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c +#SRCS+= compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/sparc/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/sparc/gen/compat_setjmp.S new file mode 100644 index 000000000..dc56cef13 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/gen/compat_setjmp.S @@ -0,0 +1,116 @@ +/* $NetBSD: compat_setjmp.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: setjmp.s,v 1.2 92/06/25 03:18:43 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)setjmp.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_setjmp.S,v 1.1 2005/10/15 22:11:22 uwe Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +#include "SYS.h" + +ENTRY(setjmp) + /* + * We use the caller's `arg dump' area (%sp+0x44; there are 6 ints + * reserved there for us) to avoid having to allocate stack space + * here. + */ + mov %o0, %o2 /* build sigcontext in [%o2] */ + mov 1, %o0 /* SIG_BLOCK */ + mov SYS_compat_13_sigprocmask13, %g1 + clr %o1 /* sigprocmask(SIG_BLOCK, (sigset_t *)NULL) */ + t ST_SYSCALL + st %o0, [%o2 + 4] /* sc.sc_mask = current mask; */ + mov SYS___sigaltstack14, %g1 + clr %o0 /* sigaltstack(NULL, &foo) */ + add %sp, 0x48, %o1 /* (foo being in arg dump area) */ + t ST_SYSCALL + ld [%sp + 0x50], %o0 /* foo.ss_flags */ + and %o0, 1, %o1 /* onstack = foo.ss_flags & 1; */ + st %o0, [%o2 + 0] /* sc.sc_onstack = current onstack; */ + st %sp, [%o2 + 8] /* sc.sc_sp = sp (both ours and caller's) */ + add %o7, 8, %o0 + st %o0, [%o2 + 12] /* sc.sc_pc = return_pc */ + add %o7, 12, %o0 + st %o0, [%o2 + 16] /* sc.sc_npc = return_pc + 4 */ + st %g0, [%o2 + 20] /* sc.sc_psr = (clean psr) */ + st %fp, [%o2 + 24] /* sc.sc_g1 = %fp (misuse, but what the heck) */ + /* sc.sc_o0 = random(), set in longjmp */ + retl /* return 0 */ + clr %o0 + +/* + * All we need to do here is force sigreturn to load a new stack pointer, + * new , and appropriate %o0 return value from the sigcontext built + * in setjmp. The %i and %l registers will be reloaded from the place to + * which %sp points, due to sigreturn() semantics (sigreturn does not modify + * the window pointer in the psr, hence it must force all windows to reload). + */ +ENTRY(longjmp) + save %sp, -96, %sp + ld [%i0 + 8], %o2 /* make sure sc->sc_sp, sc->sc_fp nonzero */ + ld [%i0 + 24], %o3 + orcc %o2, %o3, %g0 + bz Lbotch + tst %i1 /* if (v == 0) v = 1; */ + bz,a 1f + mov 1, %i1 +1: + st %i1, [%i0 + 28] /* sc.sc_o0 = v; */ + mov SYS_compat_13_sigreturn13, %g1 + mov %i0, %o0 + t ST_SYSCALL /* sigreturn(scp); */ + +Lbotch: + /* oops, caller botched it */ + call _C_LABEL(longjmperror) + nop + unimp 0 diff --git a/lib/nbsd_libc/compat/arch/sparc/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/sparc/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..5b1060dec --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/gen/compat_sigsetjmp.S @@ -0,0 +1,93 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.2 2007/10/08 13:06:00 uwe Exp $ */ +/* + * Copyright (c) 1995 Paul Kranenburg + * 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 by Paul Kranenburg. + * 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. + * + */ + +#include "SYS.h" + +#ifdef PIC + +ENTRY(sigsetjmp) + PIC_PROLOGUE(%g1, %g2) ! %g1 = _GLOBAL_OFFSET_TABLE + tst %o1 + bnz 1f + st %o1, [%o0 + 56] ! jmpbuf[JBLEN] = savemask + + !! if (savemask == 0) goto _setjmp + set _C_LABEL(_setjmp), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + + !! if (savemask != 0) goto setjmp +1: set _C_LABEL(setjmp), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + + +ENTRY(siglongjmp) + PIC_PROLOGUE(%g1, %g2) ! %g1 = _GLOBAL_OFFSET_TABLE + ld [%o0 + 56], %g2 ! restoremask = jmpbuf[JBLEN] + tst %g2 + bnz 1f + nop + + !! if (restoremask == 0) goto _longjmp + set _C_LABEL(_longjmp), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + + !! if (restoremask != 0) goto longjmp +1: set _C_LABEL(longjmp), %g2 + ld [%g1 + %g2], %g1 + jmp %g1 + nop + +#else /* !PIC */ + +ENTRY(sigsetjmp) + cmp %o1,0 + be _C_LABEL(_setjmp) + st %o1,[%o0+40] ! jmpbuf[JBLEN] + nop + ba,a _C_LABEL(setjmp) + unimp 0 + +ENTRY(siglongjmp) + ld [%o0+40],%g1 + cmp %g1,0 + be _C_LABEL(_longjmp) + nop + ba,a _C_LABEL(longjmp) + unimp 0 + +#endif /* !PIC */ diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/sparc/sys/Makefile.inc new file mode 100644 index 000000000..0a7475a99 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/10/15 22:11:22 uwe Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_Ovfork.S new file mode 100644 index 000000000..e56f145d4 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_Ovfork.S @@ -0,0 +1,62 @@ +/* $NetBSD: compat_Ovfork.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: Ovfork.s,v 1.1 91/07/06 13:05:56 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)Ovfork.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_Ovfork.S,v 1.1 2005/10/15 22:11:22 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +/* + * pid = vfork(); + * + * %o1 == 0 in parent process, 1 in child process. + * %o0 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +SYSCALL(vfork) + dec %o1 ! from 1 to 0 in child, 0 to -1 in parent + retl + and %o0, %o1, %o0 ! return 0 in child, pid in parent diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat___semctl.S new file mode 100644 index 000000000..c64b6d3bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat___sigreturn14.S new file mode 100644 index 000000000..d6620e763 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat___sigreturn14.S @@ -0,0 +1,52 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigreturn.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigreturn.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat___sigreturn14.S,v 1.1 2005/10/15 22:11:22 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(__sigreturn14) + mov SYS_compat_16___sigreturn14, %g1 + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat___sigtramp1.S new file mode 100644 index 000000000..12385b159 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat___sigtramp1.S @@ -0,0 +1,170 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ + +/* + * Copyright (c) 1996 Paul Kranenburg + * Copyright (c) 1996 + * The President and Fellows of Harvard College. All rights reserved. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Lawrence Berkeley Laboratory. + * This product includes software developed by Harvard 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Harvard University. + * This product includes software developed by Paul Kranenburg. + * 4. 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 "SYS.h" + +#define _LOCORE +#include +#include +#include + +/* + * When this code is run, the stack looks like: + * [%sp] 64 bytes to which registers can be dumped + * [%sp + 64] signal number (goes in %o0) + * [%sp + 64 + 4] signal code (goes in %o1) + * [%sp + 64 + 8] placeholder + * [%sp + 64 + 12] argument for %o3, currently unsupported (always 0) + * [%sp + 64 + 16] first word of saved state (sigcontext) + * . + * . + * . + * [%sp + NNN] last word of saved state + * (followed by previous stack contents or top of signal stack). + * The address of the function to call is in %g1; the old %g1 and %o0 + * have already been saved in the sigcontext. We are running in a clean + * window, all previous windows now being saved to the stack. + * + * Note that [%sp + 64 + 8] == %sp + 64 + 16. The copy at %sp+64+8 + * will eventually be removed, with a hole left in its place, if things + * work out. + */ +#define SAVE_STATE \ + /* \ + * XXX the `save' and `restore' below are unnecessary: should \ + * replace with simple arithmetic on %sp \ + * \ + * Make room on the stack for 32 %f registers + %fsr. This comes \ + * out to 33*4 or 132 bytes, but this must be aligned to a multiple \ + * of 8, or 136 bytes. \ + */ \ + save %sp, -CCFSZ - 136, %sp; \ + mov %g2, %l2; /* save globals in %l registers */ \ + mov %g3, %l3; \ + mov %g4, %l4; \ + mov %g5, %l5; \ + mov %g6, %l6; \ + mov %g7, %l7; \ + /* \ + * Saving the fpu registers is expensive, so do it iff the fsr \ + * stored in the sigcontext shows that the fpu is enabled. \ + */ \ + ld [%fp + 64 + 16 + SC_PSR_OFFSET], %l0; \ + sethi %hi(PSR_EF), %l1; /* FPU enable is too high for andcc */ \ + andcc %l0, %l1, %l0; /* %l0 = fpu enable bit */ \ + be 1f; /* if not set, skip the saves */ \ + rd %y, %l1; /* in any case, save %y */ \ + /* fpu is enabled, oh well */ \ + st %fsr, [%sp + CCFSZ + 0]; \ + std %f0, [%sp + CCFSZ + 8]; \ + std %f2, [%sp + CCFSZ + 16]; \ + std %f4, [%sp + CCFSZ + 24]; \ + std %f6, [%sp + CCFSZ + 32]; \ + std %f8, [%sp + CCFSZ + 40]; \ + std %f10, [%sp + CCFSZ + 48]; \ + std %f12, [%sp + CCFSZ + 56]; \ + std %f14, [%sp + CCFSZ + 64]; \ + std %f16, [%sp + CCFSZ + 72]; \ + std %f18, [%sp + CCFSZ + 80]; \ + std %f20, [%sp + CCFSZ + 88]; \ + std %f22, [%sp + CCFSZ + 96]; \ + std %f24, [%sp + CCFSZ + 104]; \ + std %f26, [%sp + CCFSZ + 112]; \ + std %f28, [%sp + CCFSZ + 120]; \ + std %f30, [%sp + CCFSZ + 128]; \ +1: + +#define RESTORE_STATE \ + /* \ + * Now that the handler has returned, re-establish all the state \ + * we just saved above, then do a sigreturn. \ + */ \ + tst %l0; /* reload fpu registers? */ \ + be 1f; /* if not, skip the loads */ \ + wr %l1, %g0, %y; /* in any case, restore %y */ \ + ld [%sp + CCFSZ + 0], %fsr; \ + ldd [%sp + CCFSZ + 8], %f0; \ + ldd [%sp + CCFSZ + 16], %f2; \ + ldd [%sp + CCFSZ + 24], %f4; \ + ldd [%sp + CCFSZ + 32], %f6; \ + ldd [%sp + CCFSZ + 40], %f8; \ + ldd [%sp + CCFSZ + 48], %f10; \ + ldd [%sp + CCFSZ + 56], %f12; \ + ldd [%sp + CCFSZ + 64], %f14; \ + ldd [%sp + CCFSZ + 72], %f16; \ + ldd [%sp + CCFSZ + 80], %f18; \ + ldd [%sp + CCFSZ + 88], %f20; \ + ldd [%sp + CCFSZ + 96], %f22; \ + ldd [%sp + CCFSZ + 104], %f24; \ + ldd [%sp + CCFSZ + 112], %f26; \ + ldd [%sp + CCFSZ + 120], %f28; \ + ldd [%sp + CCFSZ + 128], %f30; \ +1: \ + mov %l2, %g2; \ + mov %l3, %g3; \ + mov %l4, %g4; \ + mov %l5, %g5; \ + mov %l6, %g6; \ + mov %l7, %g7 + +ENTRY_NOPROFILE(__sigtramp_sigcontext_1) + SAVE_STATE + + ldd [%fp + 64], %o0 /* sig, code */ + ld [%fp + 76], %o3 /* arg3 */ + call %g1 /* call handler */ + add %fp, 64 + 16, %o2 /* pointer to sigcontext */ + + RESTORE_STATE + + /* Get registers back and set syscall # */ + restore %g0, SYS_compat_16___sigreturn14, %g1 + add %sp, 64 + 16, %o0 /* compute scp */ + t ST_SYSCALL /* call sigreturn */ + mov SYS_exit, %g1 /* exit with errno */ + t ST_SYSCALL /* if sigreturn fails */ diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_msgctl.S new file mode 100644 index 000000000..a5598b0b1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_shmctl.S new file mode 100644 index 000000000..a9f938c68 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigaction.S new file mode 100644 index 000000000..7d778432c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigpending.S new file mode 100644 index 000000000..c5891599c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigpending.S @@ -0,0 +1,60 @@ +/* $NetBSD: compat_sigpending.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigpending.s,v 1.1 91/07/06 13:06:00 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigpending.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigpending.S,v 1.1 2005/10/15 22:11:22 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +ENTRY(sigpending) + mov %o0, %o2 ! save pointer + mov SYS_compat_13_sigpending13, %g1 + t ST_SYSCALL ! sigpending() + bcc,a 1f ! if success, + st %o0, [%o2] ! store return value + ERROR() +1: + retl ! and return 0 + clr %o0 diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigprocmask.S new file mode 100644 index 000000000..d38d7874f --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigprocmask.S @@ -0,0 +1,72 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigprocmask.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigprocmask.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigprocmask.S,v 1.1 2005/10/15 22:11:22 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +/* + * sigprocmask(int how, sigset_t *set, sigset_t *oset) + */ +ENTRY(sigprocmask) + tst %o1 ! set == NULL? + bne,a 1f ! if not, + ld [%o1], %o1 ! replace it in %o1 with *set +! clr %o1 ! else block no signals ... + mov 1, %o0 ! ... using sigprocmask(SIG_BLOCK) +1: + mov SYS_compat_13_sigprocmask13, %g1 + t ST_SYSCALL + bcc 2f ! if success, + tst %o2 ! check to see if oset requested + ERROR() +2: + bne,a 3f ! if oset != NULL, + st %o0, [%o2] ! *oset = oldmask +3: + retl ! in any case, return 0 + clr %o0 diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigreturn.S new file mode 100644 index 000000000..041392b0b --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigreturn.S @@ -0,0 +1,55 @@ +/* $NetBSD: compat_sigreturn.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigreturn.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigreturn.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigreturn.S,v 1.1 2005/10/15 22:11:22 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +ENTRY(sigreturn) + mov SYS_compat_13_sigreturn13, %g1 + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigsuspend.S new file mode 100644 index 000000000..a50ff7126 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc/sys/compat_sigsuspend.S @@ -0,0 +1,55 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.1 2005/10/15 22:11:22 uwe Exp $ */ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigsuspend.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigsuspend.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigsuspend.S,v 1.1 2005/10/15 22:11:22 uwe Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend) + ld [%o0], %o0 ! indirect to mask argument + mov SYS_compat_13_sigsuspend13, %g1 + t ST_SYSCALL + ERROR() ! always terminates with EINTR diff --git a/lib/nbsd_libc/compat/arch/sparc64/Makefile.inc b/lib/nbsd_libc/compat/arch/sparc64/Makefile.inc new file mode 100644 index 000000000..fc44d319a --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 23:04:04 christos Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/sparc64/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/sparc64/gen/Makefile.inc new file mode 100644 index 000000000..ec25fc1bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/gen/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/07/03 13:06:12 drochner Exp $ + +SRCS+= compat_setjmp.S compat_sigsetjmp.S + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c +#SRCS+= compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/sparc64/gen/compat_setjmp.S b/lib/nbsd_libc/compat/arch/sparc64/gen/compat_setjmp.S new file mode 100644 index 000000000..628a5e08f --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/gen/compat_setjmp.S @@ -0,0 +1,119 @@ +/* $NetBSD: compat_setjmp.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: setjmp.s,v 1.2 92/06/25 03:18:43 torek Exp + */ + +#define _LOCORE +#include +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)setjmp.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_setjmp.S,v 1.1 2005/10/16 04:41:34 christos Exp $") +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from + * the last call to + * setjmp(a) + * by restoring registers from the stack, + * and a struct sigcontext, see + */ + +#include "SYS.h" + +#define STACK_T_SZ +ENTRY(setjmp) + /* + * We use the part of the sigcontext structure, the sp, pc, and npc fields, + * for the sigstack call so we don't need to get our own stackframe. It + * won't be filled out till later anyway. + */ + mov %o0, %o3 /* Save our jmp_buf in %o3 */ + mov %o0, %o2 /* build sigcontext in [%o2] */ + mov 1, %o0 /* SIG_BLOCK */ + mov SYS_compat_13_sigprocmask13, %g1 + clr %o1 /* sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *)a) */ + t ST_SYSCALL + + st %o0, [%o3 + 0x04] /* sc.sc_mask = current mask; */ + mov SYS___sigaltstack14, %g1 + clr %o0 /* sigstack(NULL, &foo) */ + add %o3, 0x38, %o1 /* (foo being the sigcontext14 sc_mask) */ + t ST_SYSCALL + + lduw [%o3 + 0x38+0x10], %o0 /* foo.ss_flags */ + and %o0, 1, %o1 /* onstack = foo.ss_flags & 1; */ + st %o0, [%o3 + 0x00] /* sc.sc_onstack = current onstack; */ + stx %sp, [%o3 + 0x08] /* sc.sc_sp = sp (both ours and caller's) */ + add %o7, 8, %o0 + stx %o0, [%o3 + 0x10] /* sc.sc_pc = return_pc */ + add %o7, 12, %o0 + stx %o0, [%o3 + 0x18] /* sc.sc_npc = return_pc + 4 */ + stx %g0, [%o3 + 0x20] /* sc.sc_psr = (clean psr) */ + stx %fp, [%o3 + 0x28] /* sc.sc_g1 = %fp (misuse, but what the heck) */ + /* sc.sc_o0 = random(), set in longjmp */ + retl /* return 0 */ + clr %o0 + +/* + * All we need to do here is force sigreturn to load a new stack pointer, + * new , and appropriate %o0 return value from the sigcontext built + * in setjmp. The %i and %l registers will be reloaded from the place to + * which %sp points, due to sigreturn() semantics (sigreturn does not modify + * the window pointer in the psr, hence it must force all windows to reload). + */ +ENTRY(longjmp) + save %sp, -CC64FSZ, %sp + ldx [%i0 + 0x08], %o2 /* make sure sc->sc_sp, sc->sc_fp nonzero */ + ldx [%i0 + 0x28], %o3 + orcc %o2, %o3, %g0 + bz,pn %xcc, Lbotch + movrz %i1, 1, %i1 /* if (v == 0) v = 1; */ + st %i1, [%i0 + 0x30] /* sc.sc_o0 = v; */ + mov SYS_compat_13_sigreturn13, %g1 + mov %i0, %o0 + t ST_SYSCALL /* sigreturn(scp); */ + +Lbotch: + /* oops, caller botched it */ + call _C_LABEL(longjmperror) + nop + unimp 0 diff --git a/lib/nbsd_libc/compat/arch/sparc64/gen/compat_sigsetjmp.S b/lib/nbsd_libc/compat/arch/sparc64/gen/compat_sigsetjmp.S new file mode 100644 index 000000000..9d851590f --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/gen/compat_sigsetjmp.S @@ -0,0 +1,101 @@ +/* $NetBSD: compat_sigsetjmp.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ +/* + * Copyright (c) 1995 Paul Kranenburg + * 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 by Paul Kranenburg. + * 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. + * + */ + +#include "SYS.h" + +#ifdef PIC +#ifdef BIGPIC + +ENTRY(sigsetjmp) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + set _C_LABEL(setjmp), %o2 ! if (%o1 != 0) goto _setjmp; + set _C_LABEL(_setjmp), %o3 ! else goto __setjmp; + movrnz %o1, %o2, %o3 + ldx [%g1+%o3], %g1 + jmp %g1 + st %o1,[%o0+0x48] ! jmpbuf[JBLEN] + + +ENTRY(siglongjmp) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + ld [%o0 + 0x48],%o2 ! jmpbuf[JBLEN] + set _C_LABEL(longjmp), %o3 ! if (%o2 != 0) goto _longjmp; + set _C_LABEL(_longjmp), %o4 ! else goto __longjmp; + movrnz %o2, %o3, %o4 + ldx [%g1 + %o4], %g1 + jmp %g1 + nop + unimp 0 + +#else + +ENTRY(sigsetjmp) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + cmp %o1, 0 + bne,a 1f + ldx [%g1 + _C_LABEL(setjmp)], %g1 ! if (%o1 != 0) goto _setjmp; + ldx [%g1 + _C_LABEL(_setjmp)], %g1 ! else goto __setjmp; +1: + jmp %g1 + st %o1,[%o0+0x48] ! jmpbuf[JBLEN] + + +ENTRY(siglongjmp) + PIC_PROLOGUE(%g1,%o2) ! %g1 = _GLOBAL_OFFSET_TABLE + cmp %o2, 0 + ld [%o0 + 0x48],%o2 ! jmpbuf[JBLEN] + bne,a 1f + ldx [%g1 + _C_LABEL(longjmp)], %g1 ! if (%o2 != 0) goto _longjmp; + ldx [%g1 + _C_LABEL(_longjmp)], %g1 ! else goto __longjmp; +1: + jmp %g1 + nop + unimp 0 + +#endif /* BIGPIC */ +#else /* PIC */ + +ENTRY(sigsetjmp) + brz,pt %o1,_C_LABEL(_setjmp) + st %o1,[%o0+0x48] ! jmpbuf[JBLEN] + ba,a,pt %icc,_C_LABEL(setjmp) + nop ! spitfire bug + unimp 0 + +ENTRY(siglongjmp) + ld [%o0 + 0x48], %g1 + brz,pt %g1, _C_LABEL(_longjmp) + nop + ba,a,pt %icc, _C_LABEL(longjmp) + unimp 0 + +#endif /* PIC */ diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/sparc64/sys/Makefile.inc new file mode 100644 index 000000000..f45e2b8b8 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/10/16 04:41:34 christos Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat___sigtramp1.S compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_Ovfork.S new file mode 100644 index 000000000..e11a6aa4e --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_Ovfork.S @@ -0,0 +1,62 @@ +/* $NetBSD: compat_Ovfork.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: Ovfork.s,v 1.1 91/07/06 13:05:56 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)Ovfork.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_Ovfork.S,v 1.1 2005/10/16 04:41:34 christos Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +/* + * pid = vfork(); + * + * %o1 == 0 in parent process, 1 in child process. + * %o0 == pid of child in parent, pid of parent in child. + */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +SYSCALL(vfork) + dec %o1 /* from 1 to 0 in child, 0 to -1 in parent */ + retl + and %o0, %o1, %o0 /* return 0 in child, pid in parent */ diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat___semctl.S new file mode 100644 index 000000000..c64b6d3bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat___sigreturn14.S new file mode 100644 index 000000000..b65b83946 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat___sigreturn14.S @@ -0,0 +1,52 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigreturn.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigreturn.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat___sigreturn14.S,v 1.1 2005/10/16 04:41:34 christos Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +ENTRY(__sigreturn14) + mov SYS_compat_16___sigreturn14, %g1 + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat___sigtramp1.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat___sigtramp1.S new file mode 100644 index 000000000..38810fbfb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat___sigtramp1.S @@ -0,0 +1,169 @@ +/* $NetBSD: compat___sigtramp1.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ + +/* + * Copyright (c) 1996-2002 Eduardo Horvath + * Copyright (c) 1996 Paul Kranenburg + * Copyright (c) 1996 + * The President and Fellows of Harvard College. All rights reserved. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Lawrence Berkeley Laboratory. + * This product includes software developed by Harvard 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Harvard University. + * This product includes software developed by Paul Kranenburg. + * 4. 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 "SYS.h" + +#define _LOCORE +#include +#include +#include +#include + + .register %g2,#ignore + .register %g3,#ignore + +/* + * When this code is run, the stack looks like: + * [%sp] 128 bytes to which registers can be dumped + * [%sp + 128] signal number (goes in %o0) + * [%sp + 128 + 4] signal code (goes in %o1) + * [%sp + 128 + 8] first word of saved state (sigcontext) + * . + * . + * . + * [%sp + NNN] last word of saved state + * (followed by previous stack contents or top of signal stack). + * The address of the function to call is in %g1; the old %g1 and %o0 + * have already been saved in the sigcontext. We are running in a clean + * window, all previous windows now being saved to the stack. + * + * Note that [%sp + 128 + 8] == %sp + 128 + 16. The copy at %sp+128+8 + * will eventually be removed, with a hole left in its place, if things + * work out. + */ +ENTRY_NOPROFILE(__sigtramp_sigcontext_1) + /* + * XXX the `save' and `restore' below are unnecessary: should + * replace with simple arithmetic on %sp + * + * Make room on the stack for 64 %f registers + %fsr. This comes + * out to 64*4+8 or 264 bytes, but this must be aligned to a multiple + * of 64, or 320 bytes. + */ + save %sp, -CC64FSZ - 320, %sp + mov %g2, %l2 /* save globals in %l registers */ + mov %g3, %l3 + mov %g4, %l4 + mov %g5, %l5 + mov %g6, %l6 + mov %g7, %l7 + /* + * Saving the fpu registers is expensive, so do it iff it is + * enabled and dirty. + */ + rd %fprs, %l0 + btst FPRS_DL|FPRS_DU, %l0 /* All clean? */ + bz,pt %icc, 2f + btst FPRS_DL, %l0 /* test dl */ + bz,pt %icc, 1f + btst FPRS_DU, %l0 /* test du */ + + /* fpu is enabled, oh well */ + stx %fsr, [%sp + CC64FSZ + BIAS + 0] + add %sp, BIAS+CC64FSZ+BLOCK_SIZE, %l0 /* Generate a pointer so */ + andn %l0, BLOCK_ALIGN, %l0 /* we can do a block store */ + stda %f0, [%l0] ASI_BLK_P + inc BLOCK_SIZE, %l0 + stda %f16, [%l0] ASI_BLK_P +1: + bz,pt %icc, 2f + add %sp, BIAS+CC64FSZ+BLOCK_SIZE, %l0 /* Generate a pointer so */ + andn %l0, BLOCK_ALIGN, %l0 /* we can do a block store */ + add %l0, 2*BLOCK_SIZE, %l0 /* and skip what we already stored */ + stda %f32, [%l0] ASI_BLK_P + inc BLOCK_SIZE, %l0 + stda %f48, [%l0] ASI_BLK_P +2: + membar #Sync + rd %y, %l1 /* in any case, save %y */ + lduw [%fp + BIAS + 128], %o0 /* sig */ + lduw [%fp + BIAS + 128 + 4], %o1 /* code */ + call %g1 /* call handler */ + add %fp, BIAS + 128 + 8, %o2 /* scp */ + + /* + * Now that the handler has returned, re-establish all the state + * we just saved above, then do a sigreturn. + */ + btst 3, %l0 /* All clean? */ + bz,pt %icc, 2f + btst 1, %l0 /* test dl */ + bz,pt %icc, 1f + btst 2, %l0 /* test du */ + + ldx [%sp + CC64FSZ + BIAS + 0], %fsr + add %sp, BIAS+CC64FSZ+BLOCK_SIZE, %l0 /* Generate a pointer so */ + andn %l0, BLOCK_ALIGN, %l0 /* we can do a block load */ + ldda [%l0] ASI_BLK_P, %f0 + inc BLOCK_SIZE, %l0 + ldda [%l0] ASI_BLK_P, %f16 +1: + bz,pt %icc, 2f + wr %l1, %g0, %y /* in any case, restore %y */ + add %sp, BIAS+CC64FSZ+BLOCK_SIZE, %l0 /* Generate a pointer so */ + andn %l0, BLOCK_ALIGN, %l0 /* we can do a block load */ + inc 2*BLOCK_SIZE, %l0 /* and skip what we already loaded */ + ldda [%l0] ASI_BLK_P, %f32 + inc BLOCK_SIZE, %l0 + ldda [%l0] ASI_BLK_P, %f48 +2: + mov %l2, %g2 + mov %l3, %g3 + mov %l4, %g4 + mov %l5, %g5 + mov %l6, %g6 + mov %l7, %g7 + membar #Sync + + /* get registers back and set syscall # */ + restore %g0, SYS_compat_16___sigreturn14, %g1 + add %sp, BIAS + 128 + 8, %o0 /* compute scp */ + t ST_SYSCALL /* call sigreturn */ + mov SYS_exit, %g1 /* exit with errno */ + t ST_SYSCALL /* if sigreturn fails */ diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_msgctl.S new file mode 100644 index 000000000..a5598b0b1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_shmctl.S new file mode 100644 index 000000000..a9f938c68 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigaction.S new file mode 100644 index 000000000..7d778432c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigpending.S new file mode 100644 index 000000000..21d62906a --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigpending.S @@ -0,0 +1,60 @@ +/* $NetBSD: compat_sigpending.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigpending.s,v 1.1 91/07/06 13:06:00 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigpending.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigpending.S,v 1.1 2005/10/16 04:41:34 christos Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +ENTRY(sigpending) + mov %o0, %o2 /* save pointer */ + mov SYS_compat_13_sigpending13, %g1 + t ST_SYSCALL /* sigpending() */ + bcc,a 1f /* if success, */ + st %o0, [%o2] /* store return value */ + ERROR() +1: + retl /* and return 0 */ + clr %o0 diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigprocmask.S new file mode 100644 index 000000000..bbdd339bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigprocmask.S @@ -0,0 +1,73 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigprocmask.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigprocmask.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigprocmask.S,v 1.1 2005/10/16 04:41:34 christos Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +/* + * sigprocmask(int how, sigset_t *set, sigset_t *oset) + */ +ENTRY(sigprocmask) + brnz,a %o1, 1f /* if (set != NULL) */ + ld [%o1], %o1 /* replace it in %o1 with *set */ +#if 0 + clr %o1 /* else block no signals ... */ +#endif + mov 1, %o0 /* ... using sigprocmask(SIG_BLOCK) */ +1: + mov SYS_compat_13_sigprocmask13, %g1 + t ST_SYSCALL + bcc 2f /* if success, */ + tst %o2 /* check to see if oset requested */ + ERROR() +2: + bne,a 3f /* if oset != NULL, */ + st %o0, [%o2] /* *oset = oldmask */ +3: + retl /* in any case, return 0 */ + clr %o0 diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigreturn.S new file mode 100644 index 000000000..2bb980e24 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigreturn.S @@ -0,0 +1,55 @@ +/* $NetBSD: compat_sigreturn.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigreturn.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigreturn.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigreturn.S,v 1.1 2005/10/16 04:41:34 christos Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +ENTRY(sigreturn) + mov SYS_compat_13_sigreturn13, %g1 + t ST_SYSCALL + ERROR() diff --git a/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigsuspend.S new file mode 100644 index 000000000..0fefb342d --- /dev/null +++ b/lib/nbsd_libc/compat/arch/sparc64/sys/compat_sigsuspend.S @@ -0,0 +1,55 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.1 2005/10/16 04:41:34 christos Exp $ */ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: sigsuspend.s,v 1.1 91/07/06 13:06:01 torek Exp + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) +#if 0 + .asciz "@(#)sigsuspend.s 8.1 (Berkeley) 6/4/93" +#else + RCSID("$NetBSD: compat_sigsuspend.S,v 1.1 2005/10/16 04:41:34 christos Exp $") +#endif +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend) + ld [%o0], %o0 /* indirect to mask argument */ + mov SYS_compat_13_sigsuspend13, %g1 + t ST_SYSCALL + ERROR() /* always terminates with EINTR */ diff --git a/lib/nbsd_libc/compat/arch/vax/Makefile.inc b/lib/nbsd_libc/compat/arch/vax/Makefile.inc new file mode 100644 index 000000000..04c44bf66 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/Makefile.inc @@ -0,0 +1,3 @@ +# $NetBSD: Makefile.inc,v 1.3 2010/07/06 05:59:52 mrg Exp $ + +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/vax/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/vax/sys/Makefile.inc new file mode 100644 index 000000000..add529716 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/02/18 22:54:51 matt Exp $ + +SRCS+= compat_Ovfork.S \ + compat___sigreturn14.S compat___sigtramp2.S \ + compat_msgctl.S compat___semctl.S compat_shmctl.S \ + compat_sigaction13.S compat_sigpending13.S compat_sigprocmask13.S \ + compat_sigreturn13.S compat_sigsuspend13.S diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_Ovfork.S new file mode 100644 index 000000000..0fdda43da --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_Ovfork.S @@ -0,0 +1,79 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)Ovfork.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: compat_Ovfork.S,v 1.1 2006/02/18 22:54:51 matt Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +/* + * @(#)vfork.s 4.1 (Berkeley) 12/21/80 + * C library -- vfork + */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +/* + * pid = vfork(); + * + * %r1 == 0 in parent process, %r1 == 1 in child process. + * %r0 == pid of child in parent, %r0 == pid of parent in child. + * + * trickery here, due to keith sklower, uses ret to clear the stack, + * and then returns with a jump indirect, since only one person can return + * with a ret off this stack... we do the ret before we vfork! + */ + +ENTRY(vfork, 0) + movl 16(%fp),%r2 # save return address before we smash it + movab here,16(%fp) + ret +here: + chmk $ SYS_vfork + bcs err # if failed, set errno and return -1 + /* this next trick is Chris Torek's fault */ + mnegl %r1,%r1 # %r1 = 0xffffffff if child, 0 if parent + bicl2 %r1,%r0 # %r0 &= ~%r1, i.e., 0 if child, else unchanged + jmp (%r2) + +err: +#ifdef _REENTRANT + pushr $0x5 + calls $0,_C_LABEL(__errno) + movl (%sp)+,(%r0) + mnegl $1,%r0 + rsb +#else + movl %r0,_C_LABEL(errno) + mnegl $1,%r0 + jmp (%r2) +#endif diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/vax/sys/compat___semctl.S new file mode 100644 index 000000000..c64b6d3bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/vax/sys/compat___sigreturn14.S new file mode 100644 index 000000000..4a530aaab --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat___sigreturn14.S @@ -0,0 +1,45 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)sigreturn.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: compat___sigreturn14.S,v 1.1 2006/02/18 22:54:51 matt Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ +#ifdef GPROF +#undef ENTRY +#define ENTRY(x, y) _ENTRY(x, y); pushr $0x3f; _PROF_PROLOGUE; popr $0x3f +#endif /* GPROF */ + +PSEUDO(__sigreturn14,compat_16___sigreturn14) diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat___sigtramp2.S b/lib/nbsd_libc/compat/arch/vax/sys/compat___sigtramp2.S new file mode 100644 index 000000000..7f0b6883e --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat___sigtramp2.S @@ -0,0 +1,49 @@ +/* $NetBSD: compat___sigtramp2.S,v 1.1 2006/02/18 22:54:51 matt Exp $ */ + +/* + * Copyright (c) 2002 Anders Magnusson, (ragge@NetBSD.org). + * 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 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. + */ + +/* + * Signal trampoline; registers when called: + * sp, pc, psl - obvious + * ap - points to argument list, see machdep.c:sendsig() + * fp - address of signal handler + */ + +#include "SYS.h" + + .text + _ALIGN_TEXT + + .globl _C_LABEL(__sigtramp_sigcontext_2) +_C_LABEL(__sigtramp_sigcontext_2): + pushr $(R0|R1|R2|R3|R4|R5) # save scratch registers + callg (%ap),(%fp) # use global arg list + popr $(R0|R1|R2|R3|R4|R5) # restore regs + movab 8(%ap),%ap # arg is pointer to sigctx + SYSTRAP(compat_16___sigreturn14) # exit from here + halt # illegal insn diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_msgctl.S new file mode 100644 index 000000000..a5598b0b1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_shmctl.S new file mode 100644 index 000000000..a9f938c68 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_sigaction13.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigaction13.S new file mode 100644 index 000000000..19d986075 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigaction13.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction13.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_sigpending13.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigpending13.S new file mode 100644 index 000000000..fa423f893 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigpending13.S @@ -0,0 +1,43 @@ +/* + * Copyright (c) 1989, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)sigpending.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: compat_sigpending13.S,v 1.1 2006/02/18 22:54:51 matt Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +_SYSCALL(sigpending,compat_13_sigpending13) + movl %r0,*4(%ap) # store old mask + clrl %r0 + ret diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_sigprocmask13.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigprocmask13.S new file mode 100644 index 000000000..14df4faca --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigprocmask13.S @@ -0,0 +1,56 @@ +/* + * Copyright (c) 1989, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)sigprocmask.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: compat_sigprocmask13.S,v 1.1 2006/02/18 22:54:51 matt Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +ENTRY(sigprocmask, 0) + tstl 8(%ap) # check new sigset pointer + bneq 1f # if not null, indirect +/* movl $0,8(%ap) # null mask pointer: block empty set */ + movl $1,4(%ap) # SIG_BLOCK + jbr 2f +1: movl *8(%ap),8(%ap) # indirect to new mask arg +2: chmk $ SYS_compat_13_sigprocmask13 + jcc 3f + jmp CERROR+2 + +3: tstl 12(%ap) # test if old mask requested + beql out + movl %r0,*12(%ap) # store old mask +out: + clrl %r0 + ret diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_sigreturn13.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigreturn13.S new file mode 100644 index 000000000..f66856d64 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigreturn13.S @@ -0,0 +1,49 @@ +/* + * Copyright (c) 1983, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)sigreturn.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: compat_sigreturn13.S,v 1.1 2006/02/18 22:54:51 matt Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +/* + * We must preserve the state of the registers as the user has set them up. + */ +#ifdef GPROF +#undef ENTRY +#define ENTRY(x, y) \ + _ENTRY(x, y); pushr $0x3f; _PROF_PROLOGUE; popr $0x3f; +#endif /* GPROF */ + +WARN_REFERENCES(sigreturn, \ + "warning: reference to compatibility sigreturn()") + +PSEUDO(sigreturn,compat_13_sigreturn13) diff --git a/lib/nbsd_libc/compat/arch/vax/sys/compat_sigsuspend13.S b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigsuspend13.S new file mode 100644 index 000000000..057ce40b2 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/vax/sys/compat_sigsuspend13.S @@ -0,0 +1,46 @@ +/* + * Copyright (c) 1989, 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. + */ + +#if defined(SYSLIBC_SCCS) && !defined(lint) + /* .asciz "@(#)sigsuspend.s 8.1 (Berkeley) 6/4/93" */ + .asciz "$NetBSD: compat_sigsuspend13.S,v 1.1 2006/02/18 22:54:51 matt Exp $" +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend, 0) + movl *4(%ap),4(%ap) # indirect to mask arg + chmk $ SYS_compat_13_sigsuspend13 + jcc 1f + jmp CERROR+2 +1: clrl %r0 # shouldnt happen + ret diff --git a/lib/nbsd_libc/compat/arch/x86_64/Makefile.inc b/lib/nbsd_libc/compat/arch/x86_64/Makefile.inc new file mode 100644 index 000000000..ccf4c0702 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.4 2006/07/03 13:13:53 drochner Exp $ + +.include "${COMPATARCHDIR}/gen/Makefile.inc" +.include "${COMPATARCHDIR}/sys/Makefile.inc" diff --git a/lib/nbsd_libc/compat/arch/x86_64/gen/Makefile.inc b/lib/nbsd_libc/compat/arch/x86_64/gen/Makefile.inc new file mode 100644 index 000000000..e06093eab --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/gen/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/07/03 13:13:53 drochner Exp $ + +# objects built from C sources in compat/gen +SRCS+= compat_frexp_ieee754.c compat_ldexp_ieee754.c compat_modf_ieee754.c diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/Makefile.inc b/lib/nbsd_libc/compat/arch/x86_64/sys/Makefile.inc new file mode 100644 index 000000000..28fe0557a --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/03/11 18:59:21 christos Exp $ + +SRCS+=compat_Ovfork.S compat___semctl.S compat___sigreturn14.S \ + compat_msgctl.S compat_shmctl.S compat_sigaction.S \ + compat_sigpending.S compat_sigprocmask.S compat_sigreturn.S \ + compat_sigsuspend.S diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_Ovfork.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_Ovfork.S new file mode 100644 index 000000000..bd6afd925 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_Ovfork.S @@ -0,0 +1,68 @@ +/* $NetBSD: compat_Ovfork.S,v 1.1 2006/03/11 18:59:21 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)Ovfork.s 5.1 (Berkeley) 4/23/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_Ovfork.S,v 1.1 2006/03/11 18:59:21 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(vfork, \ + "warning: reference to compatibility vfork(); include for correct reference") + +/* + * pid = vfork(); + * + * %edx == 0 in parent process, %edx == 1 in child process. + * %eax == pid of child in parent, %eax == pid of parent in child. + * + */ +ENTRY(vfork) + popq %r9 /* my rta into r9 */ + SYSTRAP(vfork) + jc err + decl %edx + andl %edx,%eax + jmp *%r9 +err: + pushq %r9 +#ifdef PIC + movq PIC_GOT(CERROR), %rcx + jmp *%rcx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat___semctl.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat___semctl.S new file mode 100644 index 000000000..c64b6d3bb --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat___semctl.S @@ -0,0 +1,35 @@ +/* $NetBSD: compat___semctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +PSEUDO(__semctl,compat_14___semctl) diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat___sigreturn14.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat___sigreturn14.S new file mode 100644 index 000000000..4704ae88f --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat___sigreturn14.S @@ -0,0 +1,42 @@ +/* $NetBSD: compat___sigreturn14.S,v 1.1 2006/03/11 18:59:21 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat___sigreturn14.S,v 1.1 2006/03/11 18:59:21 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +/* Nothing here. We don't need NetBSD-1.6 binary compatibility. */ diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_msgctl.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_msgctl.S new file mode 100644 index 000000000..a5598b0b1 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_msgctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_msgctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(msgctl, \ + "warning: reference to compatibility msgctl(); include for correct reference") + +PSEUDO(msgctl,compat_14_msgctl) diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_shmctl.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_shmctl.S new file mode 100644 index 000000000..a9f938c68 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_shmctl.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_shmctl.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(shmctl, \ + "warning: reference to compatibility shmctl(); include for correct reference") + +PSEUDO(shmctl,compat_14_shmctl) diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigaction.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigaction.S new file mode 100644 index 000000000..7d778432c --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigaction.S @@ -0,0 +1,38 @@ +/* $NetBSD: compat_sigaction.S,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * 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 "SYS.h" + +WARN_REFERENCES(sigaction, \ + "warning: reference to compatibility sigaction(); include for correct reference") + +PSEUDO(sigaction,compat_13_sigaction13) diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigpending.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigpending.S new file mode 100644 index 000000000..d368146f4 --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigpending.S @@ -0,0 +1,50 @@ +/* $NetBSD: compat_sigpending.S,v 1.1 2006/03/11 18:59:21 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigpending.s 5.1 (Berkeley) 7/1/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigpending.S,v 1.1 2006/03/11 18:59:21 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigpending, \ + "warning: reference to compatibility sigpending(); include for correct reference") + +_SYSCALL(sigpending,compat_13_sigpending13) + movl %eax,(%rdi) # store old mask + xorl %eax,%eax + ret diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigprocmask.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigprocmask.S new file mode 100644 index 000000000..db21a724e --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigprocmask.S @@ -0,0 +1,67 @@ +/* $NetBSD: compat_sigprocmask.S,v 1.1 2006/03/11 18:59:21 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigprocmask.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigprocmask.S,v 1.1 2006/03/11 18:59:21 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigprocmask, \ + "warning: reference to compatibility sigprocmask(); include for correct reference") + +ENTRY(sigprocmask) + testq %rsi,%rsi # check new sigset pointer + jnz 1f # if not null, indirect + movl $1,%edi # SIG_BLOCK + jmp 2f +1: movl (%rsi),%esi # fetch indirect ... +2: SYSTRAP(compat_13_sigprocmask13) + jc err + testq %rdx,%rdx # test if old mask requested + jz out + movq %rax,(%rdx) # store old mask +out: + xorl %eax,%eax + ret +err: +#ifdef PIC + movq PIC_GOT(CERROR), %rcx + jmp *%rcx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigreturn.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigreturn.S new file mode 100644 index 000000000..bdfb76bab --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigreturn.S @@ -0,0 +1,42 @@ +/* $NetBSD: compat_sigreturn.S,v 1.1 2006/03/11 18:59:21 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigreturn.s 5.2 (Berkeley) 12/17/90" + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigreturn.S,v 1.1 2006/03/11 18:59:21 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +/* Nothing here. We don't need NetBSD-1.3 binary compatibility. */ diff --git a/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigsuspend.S b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigsuspend.S new file mode 100644 index 000000000..f5ee4d6ef --- /dev/null +++ b/lib/nbsd_libc/compat/arch/x86_64/sys/compat_sigsuspend.S @@ -0,0 +1,59 @@ +/* $NetBSD: compat_sigsuspend.S,v 1.1 2006/03/11 18:59:21 christos Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * 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. + * + * from: @(#)sigsuspend.s 5.2 (Berkeley) 12/17/90 + */ + +#include +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: compat_sigsuspend.S,v 1.1 2006/03/11 18:59:21 christos Exp $") +#endif /* SYSLIBC_SCCS and not lint */ + +#include "SYS.h" + +WARN_REFERENCES(sigsuspend, \ + "warning: reference to compatibility sigsuspend(); include for correct reference") + +ENTRY(sigsuspend) + movl (%rdi),%edi # indirect to mask arg + SYSTRAP(compat_13_sigsuspend13) + jc err + xorl %eax,%eax # shouldn t happen + ret +err: +#ifdef PIC + movq PIC_GOT(CERROR), %rcx + jmp *%rcx +#else + jmp CERROR +#endif diff --git a/lib/nbsd_libc/compat/db/Makefile.inc b/lib/nbsd_libc/compat/db/Makefile.inc new file mode 100644 index 000000000..91236ce43 --- /dev/null +++ b/lib/nbsd_libc/compat/db/Makefile.inc @@ -0,0 +1,2 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 21:07:18 christos Exp $ +.include "${COMPATDIR}/db/hash/Makefile.inc" diff --git a/lib/nbsd_libc/compat/db/hash/Makefile.inc b/lib/nbsd_libc/compat/db/hash/Makefile.inc new file mode 100644 index 000000000..b801df5e6 --- /dev/null +++ b/lib/nbsd_libc/compat/db/hash/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 21:07:18 christos Exp $ + +.PATH: ${COMPATDIR}/db/hash +SRCS+=compat_ndbmdatum.c diff --git a/lib/nbsd_libc/compat/db/hash/compat_ndbmdatum.c b/lib/nbsd_libc/compat/db/hash/compat_ndbmdatum.c new file mode 100644 index 000000000..098e0e81f --- /dev/null +++ b/lib/nbsd_libc/compat/db/hash/compat_ndbmdatum.c @@ -0,0 +1,31 @@ +/* $NetBSD: compat_ndbmdatum.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Written by Klaus Klein , April 28, 2004. + * Public domain. + */ + +#define __LIBC12_SOURCE__ +#include "namespace.h" +#include +#include +#include + +__warn_references(dbm_delete, + "warning: reference to compatibility dbm_delete();" + " include for correct reference") +__warn_references(dbm_fetch, + "warning: reference to compatibility dbm_fetch();" + " include for correct reference") +__warn_references(dbm_firstkey, + "warning: reference to compatibility dbm_firstkey();" + " include for correct reference") +__warn_references(dbm_nextkey, + "warning: reference to compatibility dbm_nextkey();" + " include for correct reference") +__warn_references(dbm_store, + "warning: reference to compatibility dbm_store();" + " include for correct reference") + +#define datum datum12 +#include "db/hash/ndbmdatum.c" diff --git a/lib/nbsd_libc/compat/gen/Makefile.inc b/lib/nbsd_libc/compat/gen/Makefile.inc new file mode 100644 index 000000000..501a8dfaf --- /dev/null +++ b/lib/nbsd_libc/compat/gen/Makefile.inc @@ -0,0 +1,11 @@ +# $NetBSD: Makefile.inc,v 1.13 2010/04/23 19:04:54 drochner Exp $ + +.PATH: ${COMPATDIR}/gen +SRCS+=compat_errlist.c compat_fts.c compat___fts13.c compat___fts30.c \ + compat___fts31.c compat_getmntinfo.c compat_glob.c compat___glob13.c \ + compat_opendir.c compat_readdir.c compat__readdir_unlocked30.c \ + compat_scandir.c compat_siglist.c compat_signame.c compat_sigsetops.c \ + compat_times.c compat_timezone.c compat_unvis.c compat_utmpx.c \ + compat__sys_errlist.c compat__sys_nerr.c compat__sys_siglist.c \ + compat_time.c compat_utime.c compat_devname.c compat_alphasort.c \ + compat_getpwent.c compat___fts32.c compat_utmp.c compat___fts50.c diff --git a/lib/nbsd_libc/compat/gen/compat___fts13.c b/lib/nbsd_libc/compat/gen/compat___fts13.c new file mode 100644 index 000000000..f39a62499 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat___fts13.c @@ -0,0 +1,58 @@ +/* $NetBSD: compat___fts13.c,v 1.6 2009/10/19 17:52:01 christos Exp $ */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ + +__warn_references(__fts_children13, + "warning: reference to compatibility __fts_children13();" + " include for correct reference") +__warn_references(__fts_close13, + "warning: reference to compatibility __fts_close13();" + " include for correct reference") +__warn_references(__fts_open13, + "warning: reference to compatibility __fts_open13();" + " include for correct reference") +__warn_references(__fts_read13, + "warning: reference to compatibility __fts_read13();" + " include for correct reference") +__warn_references(__fts_set13, + "warning: reference to compatibility __fts_set13();" + " include for correct reference") + +#include +#include +#include + +#define __fts_stat_t struct stat13 +#define __fts_ino_t u_int32_t +#define __fts_length_t u_short +#define __fts_number_t long +#define __fts_dev_t uint32_t +#define __fts_level_t short + +#define stat __stat13 +#define lstat __lstat13 +#define fstat __fstat13 + +#undef fts_children +#define fts_children __fts_children13 +#undef fts_close +#define fts_close __fts_close13 +#undef fts_open +#define fts_open __fts_open13 +#undef fts_read +#define fts_read __fts_read13 +#undef fts_set +#define fts_set __fts_set13 + +#include +#include + +#define __FTS_COMPAT_TAILINGSLASH +#define __FTS_COMPAT_LENGTH +#define __FTS_COMPAT_LEVEL + +#include "gen/fts.c" diff --git a/lib/nbsd_libc/compat/gen/compat___fts30.c b/lib/nbsd_libc/compat/gen/compat___fts30.c new file mode 100644 index 000000000..03ec5cec9 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat___fts30.c @@ -0,0 +1,56 @@ +/* $NetBSD: compat___fts30.c,v 1.3 2009/10/19 17:52:04 christos Exp $ */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ + +__warn_references(__fts_children30, + "warning: reference to compatibility __fts_children30();" + " include for correct reference") +__warn_references(__fts_close30, + "warning: reference to compatibility __fts_close30();" + " include for correct reference") +__warn_references(__fts_open30, + "warning: reference to compatibility __fts_open30();" + " include for correct reference") +__warn_references(__fts_read30, + "warning: reference to compatibility __fts_read30();" + " include for correct reference") +__warn_references(__fts_set30, + "warning: reference to compatibility __fts_set30();" + " include for correct reference") + +#include +#include +#include + +#define __fts_stat_t struct stat30 +#define __fts_length_t u_short +#define __fts_number_t long +#define __fts_dev_t uint32_t +#define __fts_level_t short + +#define stat __stat30 +#define lstat __lstat30 +#define fstat __fstat30 + +#undef fts_children +#define fts_children __fts_children30 +#undef fts_close +#define fts_close __fts_close30 +#undef fts_open +#define fts_open __fts_open30 +#undef fts_read +#define fts_read __fts_read30 +#undef fts_set +#define fts_set __fts_set30 + +#include +#include + +#define __FTS_COMPAT_LENGTH +#define __FTS_COMPAT_LEVEL + +#include "gen/fts.c" diff --git a/lib/nbsd_libc/compat/gen/compat___fts31.c b/lib/nbsd_libc/compat/gen/compat___fts31.c new file mode 100644 index 000000000..ea5184711 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat___fts31.c @@ -0,0 +1,54 @@ +/* $NetBSD: compat___fts31.c,v 1.2 2009/10/19 17:52:05 christos Exp $ */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ + +__warn_references(__fts_children31, + "warning: reference to compatibility __fts_children31();" + " include for correct reference") +__warn_references(__fts_close31, + "warning: reference to compatibility __fts_close31();" + " include for correct reference") +__warn_references(__fts_open31, + "warning: reference to compatibility __fts_open31();" + " include for correct reference") +__warn_references(__fts_read31, + "warning: reference to compatibility __fts_read31();" + " include for correct reference") +__warn_references(__fts_set31, + "warning: reference to compatibility __fts_set31();" + " include for correct reference") + +#include +#include +#include + +#define __fts_stat_t struct stat30 +#define __fts_number_t long +#define __fts_dev_t uint32_t +#define __fts_level_t short + +#define stat __stat30 +#define lstat __lstat30 +#define fstat __fstat30 + +#undef fts_children +#define fts_children __fts_children31 +#undef fts_close +#define fts_close __fts_close31 +#undef fts_open +#define fts_open __fts_open31 +#undef fts_read +#define fts_read __fts_read31 +#undef fts_set +#define fts_set __fts_set31 + +#include +#include + +#define __FTS_COMPAT_LEVEL + +#include "gen/fts.c" diff --git a/lib/nbsd_libc/compat/gen/compat___fts32.c b/lib/nbsd_libc/compat/gen/compat___fts32.c new file mode 100644 index 000000000..99c7388f8 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat___fts32.c @@ -0,0 +1,53 @@ +/* $NetBSD: compat___fts32.c,v 1.3 2009/10/19 17:52:05 christos Exp $ */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ +__warn_references(__fts_children32, + "warning: reference to compatibility __fts_children32();" + " include for correct reference") +__warn_references(__fts_close32, + "warning: reference to compatibility __fts_close32();" + " include for correct reference") +__warn_references(__fts_open32, + "warning: reference to compatibility __fts_open32();" + " include for correct reference") +__warn_references(__fts_read32, + "warning: reference to compatibility __fts_read32();" + " include for correct reference") +__warn_references(__fts_set32, + "warning: reference to compatibility __fts_set32();" + " include for correct reference") + +#define __fts_stat_t struct stat30 +#define __fts_dev_t uint32_t +#define __fts_level_t short + +#undef fts_children +#define fts_children __fts_children32 +#undef fts_close +#define fts_close __fts_close32 +#undef fts_open +#define fts_open __fts_open32 +#undef fts_read +#define fts_read __fts_read32 +#undef fts_set +#define fts_set __fts_set32 + +#include +#include +#include +#include + +#include +#include + +#define __FTS_COMPAT_LEVEL + +#define stat __stat30 +#define lstat __lstat30 +#define fstat __fstat30 + +#include "gen/fts.c" diff --git a/lib/nbsd_libc/compat/gen/compat___fts50.c b/lib/nbsd_libc/compat/gen/compat___fts50.c new file mode 100644 index 000000000..dbf913b36 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat___fts50.c @@ -0,0 +1,43 @@ +/* $NetBSD: compat___fts50.c,v 1.2 2009/10/19 17:52:05 christos Exp $ */ + +#include "namespace.h" +#include +#include +#include +#include + +#define __LIBC12_SOURCE__ +__warn_references(__fts_children50, + "warning: reference to compatibility __fts_children50();" + " include for correct reference") +__warn_references(__fts_close50, + "warning: reference to compatibility __fts_close50();" + " include for correct reference") +__warn_references(__fts_open50, + "warning: reference to compatibility __fts_open50();" + " include for correct reference") +__warn_references(__fts_read50, + "warning: reference to compatibility __fts_read50();" + " include for correct reference") +__warn_references(__fts_set50, + "warning: reference to compatibility __fts_set50();" + " include for correct reference") + +#define __fts_level_t short +#undef fts_children +#define fts_children __fts_children50 +#undef fts_close +#define fts_close __fts_close50 +#undef fts_open +#define fts_open __fts_open50 +#undef fts_read +#define fts_read __fts_read50 +#undef fts_set +#define fts_set __fts_set50 + +#include +#include + +#define __FTS_COMPAT_LEVEL + +#include "gen/fts.c" diff --git a/lib/nbsd_libc/compat/gen/compat___glob13.c b/lib/nbsd_libc/compat/gen/compat___glob13.c new file mode 100644 index 000000000..f3ea45bae --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat___glob13.c @@ -0,0 +1,27 @@ +/* $NetBSD: compat___glob13.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/* + * Written by Jason R. Thorpe , October 21, 1997. + * Public domain. + */ +#include "namespace.h" +#include +#include +#include +#define __gl_size_t int + +#define __LIBC12_SOURCE__ +__warn_references(__glob13, + "warning: reference to compatibility __glob13(); include for correct reference") +__warn_references(__globfree13, + "warning: reference to compatibility __globfree13(); include for correct reference") + +#undef glob +#undef globfree +#define glob __glob13 +#define globfree __globfree13 + +#include +#include + +#include "gen/glob.c" diff --git a/lib/nbsd_libc/compat/gen/compat__readdir_unlocked30.c b/lib/nbsd_libc/compat/gen/compat__readdir_unlocked30.c new file mode 100644 index 000000000..df6fe22ee --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat__readdir_unlocked30.c @@ -0,0 +1,20 @@ +/* $NetBSD: compat__readdir_unlocked30.c,v 1.1 2008/05/04 18:53:26 tonnerre Exp $ */ + +#define __LIBC12_SOURCE__ +#include "namespace.h" +#include +#include + +#ifdef __warn_references +__warn_references(___readdir_unlocked30, + "warning: reference to compatibility _readdir_unlocked(); include for correct reference") +#endif + +/** + * Compat version of _readdir_unlocked which always skips directories + */ +struct dirent * +___readdir_unlocked30(DIR *dirp) +{ + return ___readdir_unlocked50(dirp, 1); +} diff --git a/lib/nbsd_libc/compat/gen/compat__sys_errlist.c b/lib/nbsd_libc/compat/gen/compat__sys_errlist.c new file mode 100644 index 000000000..b4b34324b --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat__sys_errlist.c @@ -0,0 +1,45 @@ +/* $NetBSD: compat__sys_errlist.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Written by J.T. Conklin, December 12, 1994 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat__sys_errlist.c,v 1.1 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +__warn_references(sys_errlist, + "warning: reference to compatibility sys_errlist[]; include for correct reference") +__warn_references(__sys_errlist, + "warning: reference to deprecated __sys_errlist[]; include and use sys_errlist") + +__warn_references(sys_nerr, + "warning: reference to compatibility sys_nerr; include for correct reference") +__warn_references(__sys_nerr, + "warning: reference to deprecated __sys_nerr; include and use sys_nerr") + + +#if defined(__indr_reference) +__indr_reference(_sys_errlist, sys_errlist) +__indr_reference(_sys_errlist, __sys_errlist) /* Backwards compat with v.12 */ +#else + +#undef _sys_errlist +#undef _sys_nerr +#undef rcsid +#define _sys_errlist sys_errlist +#define _sys_nerr sys_nerr +#define rcsid _rcsid +#include "__errlist.c" + +#undef _sys_errlist +#undef _sys_nerr +#undef rcsid +#define _sys_errlist __sys_errlist +#define _sys_nerr __sys_nerr +#define rcsid __rcsid +#include "__errlist.c" + +#endif diff --git a/lib/nbsd_libc/compat/gen/compat__sys_nerr.c b/lib/nbsd_libc/compat/gen/compat__sys_nerr.c new file mode 100644 index 000000000..7d095864d --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat__sys_nerr.c @@ -0,0 +1,17 @@ +/* $NetBSD: compat__sys_nerr.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Written by J.T. Conklin, December 12, 1994 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat__sys_nerr.c,v 1.1 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) && !defined(__lint__) +__indr_reference(_sys_nerr, sys_nerr) +__indr_reference(_sys_nerr, __sys_nerr) /* Backwards compat with v.12 */ +#endif +/* LINTED empty translation unit */ diff --git a/lib/nbsd_libc/compat/gen/compat__sys_siglist.c b/lib/nbsd_libc/compat/gen/compat__sys_siglist.c new file mode 100644 index 000000000..54435bf53 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat__sys_siglist.c @@ -0,0 +1,35 @@ +/* $NetBSD: compat__sys_siglist.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Written by J.T. Conklin, December 12, 1994 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat__sys_siglist.c,v 1.1 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +__warn_references(sys_siglist, + "warning: reference to compatibility sys_siglist[]; include or for correct reference") +__warn_references(__sys_siglist, + "warning: reference to deprecated __sys_siglist[]; include or and use sys_siglist") + +#if defined(__indr_reference) && !defined(__lint__) +__indr_reference(_sys_siglist, sys_siglist) +__indr_reference(_sys_siglist, __sys_siglist) /* Backwards compat with v.12 */ +#else + +#undef _sys_siglist +#undef rcsid +#define _sys_siglist sys_siglist +#define rcsid _rcsid +#include "compat_siglist.c" + +#undef _sys_siglist +#undef rcsid +#define _sys_siglist __sys_siglist +#define rcsid __rcsid +#include "compat_siglist.c" + +#endif diff --git a/lib/nbsd_libc/compat/gen/compat_alphasort.c b/lib/nbsd_libc/compat/gen/compat_alphasort.c new file mode 100644 index 000000000..25201718f --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_alphasort.c @@ -0,0 +1,20 @@ +/* $NetBSD: compat_alphasort.c,v 1.1 2009/02/24 18:42:36 christos Exp $ */ + +#include +#define __LIBC12_SOURCE__ +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(alphasort,_alphasort) +#endif + +#ifdef __warn_references +__warn_references(alphasort, + "warning: reference to compatibility alphasort(); include for correct reference") +#endif + +#define dirent dirent12 + +#include "gen/alphasort.c" diff --git a/lib/nbsd_libc/compat/gen/compat_devname.c b/lib/nbsd_libc/compat/gen/compat_devname.c new file mode 100644 index 000000000..399c002d2 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_devname.c @@ -0,0 +1,62 @@ +/* $NetBSD: compat_devname.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_devname.c,v 1.2 2009/01/11 02:46:25 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include + +__warn_references(devname, + "warning: reference to compatibility devname(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(devname, _devname) +#endif + +__aconst char * +devname(int32_t dev, mode_t mode) +{ + return __devname50(dev, mode); +} diff --git a/lib/nbsd_libc/compat/gen/compat_errlist.c b/lib/nbsd_libc/compat/gen/compat_errlist.c new file mode 100644 index 000000000..7c8c8cb3b --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_errlist.c @@ -0,0 +1,153 @@ +/* $NetBSD: compat_errlist.c,v 1.2 2006/10/31 00:38:07 cbiere Exp $ */ + +/* + * Copyright (c) 1982, 1985, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)errlst.c 8.2 (Berkeley) 11/16/93"; +#else +__RCSID("$NetBSD: compat_errlist.c,v 1.2 2006/10/31 00:38:07 cbiere Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#define __LIBC12_SOURCE__ +#include +#ifdef __weak_alias +__weak_alias(sys_errlist, _sys_errlist) +__weak_alias(__sys_errlist, _sys_errlist) +__weak_alias(sys_nerr, _sys_nerr) +__weak_alias(__sys_nerr, _sys_nerr) +#endif + +const char *const sys_errlist[] = { + "Undefined error: 0", /* 0 - ENOERROR */ + "Operation not permitted", /* 1 - EPERM */ + "No such file or directory", /* 2 - ENOENT */ + "No such process", /* 3 - ESRCH */ + "Interrupted system call", /* 4 - EINTR */ + "Input/output error", /* 5 - EIO */ + "Device not configured", /* 6 - ENXIO */ + "Argument list too long", /* 7 - E2BIG */ + "Exec format error", /* 8 - ENOEXEC */ + "Bad file descriptor", /* 9 - EBADF */ + "No child processes", /* 10 - ECHILD */ + "Resource deadlock avoided", /* 11 - EDEADLK */ + "Cannot allocate memory", /* 12 - ENOMEM */ + "Permission denied", /* 13 - EACCES */ + "Bad address", /* 14 - EFAULT */ + "Block device required", /* 15 - ENOTBLK */ + "Device busy", /* 16 - EBUSY */ + "File exists", /* 17 - EEXIST */ + "Cross-device link", /* 18 - EXDEV */ + "Operation not supported by device", /* 19 - ENODEV */ + "Not a directory", /* 20 - ENOTDIR */ + "Is a directory", /* 21 - EISDIR */ + "Invalid argument", /* 22 - EINVAL */ + "Too many open files in system", /* 23 - ENFILE */ + "Too many open files", /* 24 - EMFILE */ + "Inappropriate ioctl for device", /* 25 - ENOTTY */ + "Text file busy", /* 26 - ETXTBSY */ + "File too large", /* 27 - EFBIG */ + "No space left on device", /* 28 - ENOSPC */ + "Illegal seek", /* 29 - ESPIPE */ + "Read-only file system", /* 30 - EROFS */ + "Too many links", /* 31 - EMLINK */ + "Broken pipe", /* 32 - EPIPE */ + +/* math software */ + "Numerical argument out of domain", /* 33 - EDOM */ + "Result too large or too small", /* 34 - ERANGE */ + +/* non-blocking and interrupt i/o */ + "Resource temporarily unavailable", /* 35 - EAGAIN */ + /* 35 - EWOULDBLOCK */ + "Operation now in progress", /* 36 - EINPROGRESS */ + "Operation already in progress", /* 37 - EALREADY */ + +/* ipc/network software -- argument errors */ + "Socket operation on non-socket", /* 38 - ENOTSOCK */ + "Destination address required", /* 39 - EDESTADDRREQ */ + "Message too long", /* 40 - EMSGSIZE */ + "Protocol wrong type for socket", /* 41 - EPROTOTYPE */ + "Protocol option not available", /* 42 - ENOPROTOOPT */ + "Protocol not supported", /* 43 - EPROTONOSUPPORT */ + "Socket type not supported", /* 44 - ESOCKTNOSUPPORT */ + "Operation not supported", /* 45 - EOPNOTSUPP */ + "Protocol family not supported", /* 46 - EPFNOSUPPORT */ + /* 47 - EAFNOSUPPORT */ + "Address family not supported by protocol family", + "Address already in use", /* 48 - EADDRINUSE */ + "Can't assign requested address", /* 49 - EADDRNOTAVAIL */ + +/* ipc/network software -- operational errors */ + "Network is down", /* 50 - ENETDOWN */ + "Network is unreachable", /* 51 - ENETUNREACH */ + "Network dropped connection on reset", /* 52 - ENETRESET */ + "Software caused connection abort", /* 53 - ECONNABORTED */ + "Connection reset by peer", /* 54 - ECONNRESET */ + "No buffer space available", /* 55 - ENOBUFS */ + "Socket is already connected", /* 56 - EISCONN */ + "Socket is not connected", /* 57 - ENOTCONN */ + "Can't send after socket shutdown", /* 58 - ESHUTDOWN */ + "Too many references: can't splice", /* 59 - ETOOMANYREFS */ + "Operation timed out", /* 60 - ETIMEDOUT */ + "Connection refused", /* 61 - ECONNREFUSED */ + + "Too many levels of symbolic links", /* 62 - ELOOP */ + "File name too long", /* 63 - ENAMETOOLONG */ + +/* should be rearranged */ + "Host is down", /* 64 - EHOSTDOWN */ + "No route to host", /* 65 - EHOSTUNREACH */ + "Directory not empty", /* 66 - ENOTEMPTY */ + +/* quotas & mush */ + "Too many processes", /* 67 - EPROCLIM */ + "Too many users", /* 68 - EUSERS */ + "Disc quota exceeded", /* 69 - EDQUOT */ + +/* Network File System */ + "Stale NFS file handle", /* 70 - ESTALE */ + "Too many levels of remote in path", /* 71 - EREMOTE */ + "RPC struct is bad", /* 72 - EBADRPC */ + "RPC version wrong", /* 73 - ERPCMISMATCH */ + "RPC prog. not avail", /* 74 - EPROGUNAVAIL */ + "Program version wrong", /* 75 - EPROGMISMATCH */ + "Bad procedure for program", /* 76 - EPROCUNAVAIL */ + + "No locks available", /* 77 - ENOLCK */ + "Function not implemented", /* 78 - ENOSYS */ + + "Inappropriate file type or format", /* 79 - EFTYPE */ +}; +const int sys_nerr = { sizeof(sys_errlist) / sizeof(sys_errlist[0]) }; diff --git a/lib/nbsd_libc/compat/gen/compat_frexp_ieee754.c b/lib/nbsd_libc/compat/gen/compat_frexp_ieee754.c new file mode 100644 index 000000000..947039bfc --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_frexp_ieee754.c @@ -0,0 +1,83 @@ +/* $NetBSD: compat_frexp_ieee754.c,v 1.5 2010/04/23 19:04:54 drochner Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: frexp.c,v 1.1 91/07/07 04:45:01 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)frexp.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_frexp_ieee754.c,v 1.5 2010/04/23 19:04:54 drochner Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +double frexp(double, int *); + +/* + * Split the given value into a fraction in the range [0.5, 1.0) and + * an exponent, such that frac * (2^exp) == value. If value is 0, + * return 0. + */ +double +frexp(double value, int *eptr) +{ + union ieee_double_u u; + + if (value) { + /* + * Fractions in [0.5..1.0) have an exponent of 2^-1. + * Leave Inf and NaN alone, however. + */ + u.dblu_d = value; + if (u.dblu_dbl.dbl_exp != DBL_EXP_INFNAN) { + *eptr = 0; + if (u.dblu_dbl.dbl_exp == 0) { + /* denormal, scale out of mantissa */ + *eptr = -DBL_FRACBITS; + u.dblu_d *= 4.50359962737049600000e+15; + } + *eptr += u.dblu_dbl.dbl_exp - (DBL_EXP_BIAS - 1); + u.dblu_dbl.dbl_exp = DBL_EXP_BIAS - 1; + } + return (u.dblu_d); + } else { + *eptr = 0; + return (value); + } +} diff --git a/lib/nbsd_libc/compat/gen/compat_fts.c b/lib/nbsd_libc/compat/gen/compat_fts.c new file mode 100644 index 000000000..b3bfdceb7 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_fts.c @@ -0,0 +1,56 @@ +/* $NetBSD: compat_fts.c,v 1.4 2009/10/19 17:52:05 christos Exp $ */ + +/* + * Written by Jason R. Thorpe , October 21, 1997. + * Public domain. + */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include +#include + +#define __fts_stat_t struct stat12 +#define __fts_nlink_t u_int16_t +#define __fts_ino_t u_int32_t +#define __fts_length_t unsigned short +#define __fts_number_t long +#define __fts_dev_t uint32_t +#define __fts_level_t short + +#include +#include + +#ifdef __weak_alias +__weak_alias(fts_children,_fts_children) +__weak_alias(fts_close,_fts_close) +__weak_alias(fts_open,_fts_open) +__weak_alias(fts_read,_fts_read) +__weak_alias(fts_set,_fts_set) +#endif /* __weak_alias */ + +__warn_references(fts_children, + "warning: reference to compatibility fts_children();" + " include for correct reference") +__warn_references(fts_close, + "warning: reference to compatibility fts_close();" + " include for correct reference") +__warn_references(fts_open, + "warning: reference to compatibility fts_open();" + " include for correct reference") +__warn_references(fts_read, + "warning: reference to compatibility fts_read();" + " include for correct reference") +__warn_references(fts_set, + "warning: reference to compatibility fts_set();" + " include for correct reference") + +#define __FTS_COMPAT_TAILINGSLASH +#define __FTS_COMPAT_LENGTH +#define __FTS_COMPAT_LEVEL + +#include "gen/fts.c" diff --git a/lib/nbsd_libc/compat/gen/compat_getmntinfo.c b/lib/nbsd_libc/compat/gen/compat_getmntinfo.c new file mode 100644 index 000000000..3db1895f3 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_getmntinfo.c @@ -0,0 +1,88 @@ +/* $NetBSD: compat_getmntinfo.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getmntinfo.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_getmntinfo.c,v 1.1 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getmntinfo,_getmntinfo) +#endif + +/* + * Return information about mounted filesystems. + */ +int +getmntinfo(mntbufp, flags) + struct statfs12 **mntbufp; + int flags; +{ + static struct statfs12 *mntbuf; + static int mntsize; + static size_t bufsize; + + _DIAGASSERT(mntbufp != NULL); + + if (mntsize <= 0 && + (mntsize = getfsstat(NULL, 0L, MNT_NOWAIT)) == -1) + return (0); + if (bufsize > 0 && + (mntsize = getfsstat(mntbuf, (long)bufsize, flags)) == -1) + return (0); + while (bufsize <= mntsize * sizeof(struct statfs12)) { + if (mntbuf) + free(mntbuf); + bufsize = (mntsize + 1) * sizeof(struct statfs12); + if ((mntbuf = malloc(bufsize)) == NULL) + return (0); + if ((mntsize = getfsstat(mntbuf, (long)bufsize, flags)) == -1) + return (0); + } + *mntbufp = mntbuf; + return (mntsize); +} diff --git a/lib/nbsd_libc/compat/gen/compat_getpwent.c b/lib/nbsd_libc/compat/gen/compat_getpwent.c new file mode 100644 index 000000000..886f49545 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_getpwent.c @@ -0,0 +1,187 @@ +/* $NetBSD: compat_getpwent.c,v 1.3 2009/06/01 06:04:37 yamt Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_getpwent.c,v 1.3 2009/06/01 06:04:37 yamt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include + +__warn_references(getpwuid, + "warning: reference to compatibility getpwuid(); include to generate correct reference") +__warn_references(getpwnam, + "warning: reference to compatibility getpwnam(); include to generate correct reference") +__warn_references(getpwnam_r, + "warning: reference to compatibility getpwnam_r(); include to generate correct reference") +__warn_references(getpwuid_r, + "warning: reference to compatibility getpwuid_r(); include to generate correct reference") +__warn_references(getpwent, + "warning: reference to compatibility getpwent(); include to generate correct reference") +#ifdef notdef /* for libutil */ +__warn_references(pw_scan, + "warning: reference to compatibility pw_scan(); include to generate correct reference") +#endif +__warn_references(getpwent_r, + "warning: reference to compatibility getpwent_r(); include to generate correct reference") +__warn_references(pwcache_userdb, + "warning: reference to compatibility pwcache_userdb(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(getpwent, _getpwent) +__weak_alias(getpwent_r, _getpwent_r) +__weak_alias(getpwuid, _getpwuid) +__weak_alias(getpwuid_r, _getpwuid_r) +__weak_alias(getpwnam, _getpwnam) +__weak_alias(getpwnam_r, _getpwnam_r) +__weak_alias(pwcache_userdb, _pwcache_userdb) +#endif + +static struct passwd50 * +cvt(struct passwd *p) +{ + struct passwd50 *q = (void *)p; + + if (q == NULL) { + return NULL; + } + q->pw_change = (int32_t)p->pw_change; + q->pw_class = p->pw_class; + q->pw_gecos = p->pw_gecos; + q->pw_dir = p->pw_dir; + q->pw_shell = p->pw_shell; + q->pw_expire = (int32_t)p->pw_expire; + return q; +} + +struct passwd50 * +getpwuid(uid_t uid) +{ + return cvt(__getpwuid50(uid)); + +} + +struct passwd50 * +getpwnam(const char *name) +{ + return cvt(__getpwnam50(name)); +} + +int +getpwnam_r(const char *name , struct passwd50 *p, char *buf, size_t len, + struct passwd50 **q) +{ + struct passwd px, *qx; + int rv = __getpwnam_r50(name, &px, buf, len, &qx); + *q = p; + passwd_to_passwd50(&px, p); + return rv; +} + +int +getpwuid_r(uid_t uid, struct passwd50 *p, char *buf, size_t len, + struct passwd50 **q) +{ + struct passwd px, *qx; + int rv = __getpwuid_r50(uid, &px, buf, len, &qx); + *q = p; + passwd_to_passwd50(&px, p); + return rv; +} + +struct passwd50 * +getpwent(void) +{ + return cvt(__getpwent50()); +} + +#ifdef notdef /* for libutil */ +int +pw_scan(char *buf, struct passwd50 *p, int *flags) +{ + struct passwd px; + int rv = __pw_scan50(buf, &px, flags); + passwd_to_passwd50(&px, p); + return rv; +} +#endif + +int +getpwent_r(struct passwd50 *p, char *buf, size_t len, struct passwd50 **q) +{ + struct passwd px, *qx; + int rv = __getpwent_r50(&px, buf, len, &qx); + *q = p; + passwd_to_passwd50(&px, p); + return rv; +} + +static struct passwd50 * (*__getpwnamf)(const char *); +static struct passwd50 * (*__getpwuidf)(uid_t); +static struct passwd pw; + +static struct passwd * +internal_getpwnam(const char *name) +{ + struct passwd50 *p = (*__getpwnamf)(name); + passwd50_to_passwd(p, &pw); + return &pw; +} + +static struct passwd * +internal_getpwuid(uid_t uid) +{ + struct passwd50 *p = (*__getpwuidf)(uid); + passwd50_to_passwd(p, &pw); + return &pw; +} + +int pwcache_userdb(int (*setpassentf)(int), void (*endpwentf)(void), + struct passwd50 * (*getpwnamf)(const char *), + struct passwd50 * (*getpwuidf)(uid_t)) +{ + __getpwnamf = getpwnamf; + __getpwuidf = getpwuidf; + return __pwcache_userdb50(setpassentf, endpwentf, internal_getpwnam, + internal_getpwuid); +} diff --git a/lib/nbsd_libc/compat/gen/compat_glob.c b/lib/nbsd_libc/compat/gen/compat_glob.c new file mode 100644 index 000000000..222b4a42d --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_glob.c @@ -0,0 +1,31 @@ +/* $NetBSD: compat_glob.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/* + * Written by Jason R. Thorpe , October 21, 1997. + * Public domain. + */ +#include "namespace.h" +#include +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include +#include +#define __gl_stat_t struct stat12 + +#include +#include + +#ifdef __weak_alias +__weak_alias(glob,_glob) +__weak_alias(globfree,_globfree) +#endif /* __weak_alias */ + +__warn_references(glob, + "warning: reference to compatibility glob(); include for correct reference") +__warn_references(globfree, + "warning: reference to compatibility globfree(); include for correct reference") + +#include "gen/glob.c" diff --git a/lib/nbsd_libc/compat/gen/compat_lastlogx.c b/lib/nbsd_libc/compat/gen/compat_lastlogx.c new file mode 100644 index 000000000..8d1af36c2 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_lastlogx.c @@ -0,0 +1,91 @@ +/* $NetBSD: compat_lastlogx.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_lastlogx.c,v 1.2 2009/01/11 02:46:25 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +/* don't define earlier, has side effects in fcntl.h */ +#define __LIBC12_SOURCE__ +#include +#include + +__warn_references(getlastlogx, + "warning: reference to compatibility getlastlogx(); include for correct reference") +__warn_references(lastlogxname, + "warning: reference to deprecated lastlogxname()") + +static char llfile[MAXPATHLEN] = _PATH_LASTLOGX; + +int +lastlogxname(const char *fname) +{ + size_t len; + + _DIAGASSERT(fname != NULL); + + len = strlen(fname); + + if (len >= sizeof(llfile)) + return 0; + + /* must end in x! */ + if (fname[len - 1] != 'x') + return 0; + + (void)strlcpy(llfile, fname, sizeof(llfile)); + return 1; +} + +struct lastlogx * +getlastlogx(uid_t uid, struct lastlogx *ll) +{ + + return __getlastlogx13(llfile, uid, ll); +} diff --git a/lib/nbsd_libc/compat/gen/compat_ldexp_ieee754.c b/lib/nbsd_libc/compat/gen/compat_ldexp_ieee754.c new file mode 100644 index 000000000..a21d385e2 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_ldexp_ieee754.c @@ -0,0 +1,143 @@ +/* $NetBSD: compat_ldexp_ieee754.c,v 1.5 2010/04/23 19:04:54 drochner Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles M. Hannum. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.5 2010/04/23 19:04:54 drochner Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +double ldexp(double, int); + +/* + * Multiply the given value by 2^expon. + */ +double +ldexp(double val, int expon) +{ + int oldexp, newexp; + union ieee_double_u u, mul; + + u.dblu_d = val; + oldexp = u.dblu_dbl.dbl_exp; + + /* + * If input is zero, Inf or NaN, just return it. + */ + if (u.dblu_d == 0.0 || oldexp == DBL_EXP_INFNAN) + return (val); + + if (oldexp == 0) { + /* + * u.v is denormal. We must adjust it so that the exponent + * arithmetic below will work. + */ + if (expon <= DBL_EXP_BIAS) { + /* + * Optimization: if the scaling can be done in a single + * multiply, or underflows, just do it now. + */ + if (expon <= -DBL_FRACBITS) { + errno = ERANGE; + return (val < 0.0 ? -0.0 : 0.0); + } + mul.dblu_d = 0.0; + mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS; + u.dblu_d *= mul.dblu_d; + if (u.dblu_d == 0.0) { + errno = ERANGE; + return (val < 0.0 ? -0.0 : 0.0); + } + return (u.dblu_d); + } else { + /* + * We know that expon is very large, and therefore the + * result cannot be denormal (though it may be Inf). + * Shift u.v by just enough to make it normal. + */ + mul.dblu_d = 0.0; + mul.dblu_dbl.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS; + u.dblu_d *= mul.dblu_d; + expon -= DBL_FRACBITS; + oldexp = u.dblu_dbl.dbl_exp; + } + } + + /* + * u.v is now normalized and oldexp has been adjusted if necessary. + * Calculate the new exponent and check for underflow and overflow. + */ + newexp = oldexp + expon; + + if (newexp <= 0) { + /* + * The output number is either denormal or underflows (see + * comments in machine/ieee.h). + */ + if (newexp <= -DBL_FRACBITS) { + errno = ERANGE; + return (val < 0.0 ? -0.0 : 0.0); + } + /* + * Denormalize the result. We do this with a multiply. If + * expon is very large, it won't fit in a double, so we have + * to adjust the exponent first. This is safe because we know + * that u.v is normal at this point. + */ + if (expon <= -DBL_EXP_BIAS) { + u.dblu_dbl.dbl_exp = 1; + expon += oldexp - 1; + } + mul.dblu_d = 0.0; + mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS; + u.dblu_d *= mul.dblu_d; + return (u.dblu_d); + } else if (newexp >= DBL_EXP_INFNAN) { + /* + * The result overflowed; return +/-Inf. + */ + u.dblu_dbl.dbl_exp = DBL_EXP_INFNAN; + u.dblu_dbl.dbl_frach = 0; + u.dblu_dbl.dbl_fracl = 0; + errno = ERANGE; + return (u.dblu_d); + } else { + /* + * The result is normal; just replace the old exponent with the + * new one. + */ + u.dblu_dbl.dbl_exp = newexp; + return (u.dblu_d); + } +} diff --git a/lib/nbsd_libc/compat/gen/compat_modf_ieee754.c b/lib/nbsd_libc/compat/gen/compat_modf_ieee754.c new file mode 100644 index 000000000..660697d6f --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_modf_ieee754.c @@ -0,0 +1,103 @@ +/* $NetBSD: compat_modf_ieee754.c,v 1.4 2010/04/23 19:04:54 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include +#include +#include + +double modf(double, double *); + +/* + * double modf(double val, double *iptr) + * returns: f and i such that |f| < 1.0, (f + i) = val, and + * sign(f) == sign(i) == sign(val). + * + * Beware signedness when doing subtraction, and also operand size! + */ +double +modf(double val, double *iptr) +{ + union ieee_double_u u, v; + u_int64_t frac; + + /* + * If input is +/-Inf or NaN, return +/-0 or NaN. + */ + u.dblu_d = val; + if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN) { + *iptr = u.dblu_d; + return (0.0 / u.dblu_d); + } + + /* + * If input can't have a fractional part, return + * (appropriately signed) zero, and make i be the input. + */ + if ((int)u.dblu_dbl.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) { + *iptr = u.dblu_d; + v.dblu_d = 0.0; + v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign; + return (v.dblu_d); + } + + /* + * If |input| < 1.0, return it, and set i to the appropriately + * signed zero. + */ + if (u.dblu_dbl.dbl_exp < DBL_EXP_BIAS) { + v.dblu_d = 0.0; + v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign; + *iptr = v.dblu_d; + return (u.dblu_d); + } + + /* + * There can be a fractional part of the input. + * If you look at the math involved for a few seconds, it's + * plain to see that the integral part is the input, with the + * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed, + * the fractional part is the part with the rest of the + * bits zeroed. Just zeroing the high bits to get the + * fractional part would yield a fraction in need of + * normalization. Therefore, we take the easy way out, and + * just use subtraction to get the fractional part. + */ + v.dblu_d = u.dblu_d; + /* Zero the low bits of the fraction, the sleazy way. */ + frac = ((u_int64_t)v.dblu_dbl.dbl_frach << 32) + v.dblu_dbl.dbl_fracl; + frac >>= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS); + frac <<= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS); + v.dblu_dbl.dbl_fracl = (unsigned int)frac & 0xffffffff; + v.dblu_dbl.dbl_frach = (unsigned int)(frac >> 32); + *iptr = v.dblu_d; + + u.dblu_d -= v.dblu_d; + u.dblu_dbl.dbl_sign = v.dblu_dbl.dbl_sign; + return (u.dblu_d); +} diff --git a/lib/nbsd_libc/compat/gen/compat_opendir.c b/lib/nbsd_libc/compat/gen/compat_opendir.c new file mode 100644 index 000000000..24eaef0b8 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_opendir.c @@ -0,0 +1,24 @@ +/* $NetBSD: compat_opendir.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include + +#ifdef __weak_alias +__weak_alias(opendir,_opendir) +#endif + +#ifdef __warn_references +__warn_references(opendir, + "warning: reference to compatibility opendir(); include for correct reference") +__warn_references(__opendir2, + "warning: reference to compatibility __opendir2(); include for correct reference") +#endif + +#define dirent dirent12 + +#include "gen/opendir.c" diff --git a/lib/nbsd_libc/compat/gen/compat_readdir.c b/lib/nbsd_libc/compat/gen/compat_readdir.c new file mode 100644 index 000000000..08632979e --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_readdir.c @@ -0,0 +1,22 @@ +/* $NetBSD: compat_readdir.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +#define __LIBC12_SOURCE__ +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(readdir,_readdir) +__weak_alias(readdir_r,_readdir_r) +#endif + +#ifdef __warn_references +__warn_references(readdir, + "warning: reference to compatibility readdir(); include for correct reference") +__warn_references(readdir_r, + "warning: reference to compatibility readdir_r(); include for correct reference") +#endif + +#define dirent dirent12 + +#include "gen/readdir.c" diff --git a/lib/nbsd_libc/compat/gen/compat_scandir.c b/lib/nbsd_libc/compat/gen/compat_scandir.c new file mode 100644 index 000000000..7aac0f257 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_scandir.c @@ -0,0 +1,20 @@ +/* $NetBSD: compat_scandir.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +#include +#define __LIBC12_SOURCE__ +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(scandir,_scandir) +#endif + +#ifdef __warn_references +__warn_references(scandir, + "warning: reference to compatibility scandir(); include for correct reference") +#endif + +#define dirent dirent12 + +#include "gen/scandir.c" diff --git a/lib/nbsd_libc/compat/gen/compat_siglist.c b/lib/nbsd_libc/compat/gen/compat_siglist.c new file mode 100644 index 000000000..0627f4486 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_siglist.c @@ -0,0 +1,79 @@ +/* $NetBSD: compat_siglist.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)siglist.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_siglist.c,v 1.1 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef __weak_alias +__weak_alias(sys_siglist, _sys_siglist) +__weak_alias(__sys_siglist, _sys_siglist) +#endif + +const char *const _sys_siglist[] = { + "Signal 0", + "Hangup", /* SIGHUP */ + "Interrupt", /* SIGINT */ + "Quit", /* SIGQUIT */ + "Illegal instruction", /* SIGILL */ + "Trace/BPT trap", /* SIGTRAP */ + "Abort trap", /* SIGABRT */ + "EMT trap", /* SIGEMT */ + "Floating point exception", /* SIGFPE */ + "Killed", /* SIGKILL */ + "Bus error", /* SIGBUS */ + "Segmentation fault", /* SIGSEGV */ + "Bad system call", /* SIGSYS */ + "Broken pipe", /* SIGPIPE */ + "Alarm clock", /* SIGALRM */ + "Terminated", /* SIGTERM */ + "Urgent I/O condition", /* SIGURG */ + "Suspended (signal)", /* SIGSTOP */ + "Suspended", /* SIGTSTP */ + "Continued", /* SIGCONT */ + "Child exited", /* SIGCHLD */ + "Stopped (tty input)", /* SIGTTIN */ + "Stopped (tty output)", /* SIGTTOU */ + "I/O possible", /* SIGIO */ + "Cputime limit exceeded", /* SIGXCPU */ + "Filesize limit exceeded", /* SIGXFSZ */ + "Virtual timer expired", /* SIGVTALRM */ + "Profiling timer expired", /* SIGPROF */ + "Window size changes", /* SIGWINCH */ + "Information request", /* SIGINFO */ + "User defined signal 1", /* SIGUSR1 */ + "User defined signal 2" /* SIGUSR2 */ +}; diff --git a/lib/nbsd_libc/compat/gen/compat_signame.c b/lib/nbsd_libc/compat/gen/compat_signame.c new file mode 100644 index 000000000..b2dcddc67 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_signame.c @@ -0,0 +1,77 @@ +/* $NetBSD: compat_signame.c,v 1.1 2005/09/13 01:44:09 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "from: @(#)siglist.c 5.6 (Berkeley) 2/23/91";*/ +#else +__RCSID("$NetBSD: compat_signame.c,v 1.1 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +__warn_references(sys_signame, + "warning: reference to compatibility sys_signame[]; include for correct reference") + +const char *const sys_signame[] = { + "Signal 0", /* 0 */ + "HUP", /* 1 SIGHUP */ + "INT", /* 2 SIGINT */ + "QUIT", /* 3 SIGQUIT */ + "ILL", /* 4 SIGILL */ + "TRAP", /* 5 SIGTRAP */ + "ABRT", /* 6 SIGABRT */ + "EMT", /* 7 SIGEMT */ + "FPE", /* 8 SIGFPE */ + "KILL", /* 9 SIGKILL */ + "BUS", /* 10 SIGBUS */ + "SEGV", /* 11 SIGSEGV */ + "SYS", /* 12 SIGSYS */ + "PIPE", /* 13 SIGPIPE */ + "ALRM", /* 14 SIGALRM */ + "TERM", /* 15 SIGTERM */ + "URG", /* 16 SIGURG */ + "STOP", /* 17 SIGSTOP */ + "TSTP", /* 18 SIGTSTP */ + "CONT", /* 19 SIGCONT */ + "CHLD", /* 20 SIGCHLD */ + "TTIN", /* 21 SIGTTIN */ + "TTOU", /* 22 SIGTTOU */ + "IO", /* 23 SIGIO */ + "XCPU", /* 24 SIGXCPU */ + "XFSZ", /* 25 SIGXFSZ */ + "VTALRM", /* 26 SIGVTALRM */ + "PROF", /* 27 SIGPROF */ + "WINCH", /* 28 SIGWINCH */ + "INFO", /* 29 SIGINFO */ + "USR1", /* 30 SIGUSR1 */ + "USR2", /* 31 SIGUSR2 */ +}; diff --git a/lib/nbsd_libc/compat/gen/compat_sigsetops.c b/lib/nbsd_libc/compat/gen/compat_sigsetops.c new file mode 100644 index 000000000..dee1ec6a5 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_sigsetops.c @@ -0,0 +1,120 @@ +/* $NetBSD: compat_sigsetops.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 1989, 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. + * + * @(#)sigsetops.c 8.1 (Berkeley) 6/4/93 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sigsetops.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_sigsetops.c,v 1.2 2009/01/11 02:46:25 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include +#include + +#undef sigemptyset +#undef sigfillset +#undef sigaddset +#undef sigdelset +#undef sigismember + +__warn_references(sigaddset, + "warning: reference to compatibility sigaddset(); include for correct reference") +__warn_references(sigdelset, + "warning: reference to compatibility sigdelset(); include for correct reference") +__warn_references(sigemptyset, + "warning: reference to compatibility sigemptyset(); include for correct reference") +__warn_references(sigfillset, + "warning: reference to compatibility sigfillset(); include for correct reference") +__warn_references(sigismember, + "warning: reference to compatibility sigismember(); include for correct reference") + +int +sigemptyset(set) + sigset13_t *set; +{ + *set = 0; + return (0); +} + +int +sigfillset(set) + sigset13_t *set; +{ + *set = ~(sigset13_t)0; + return (0); +} + +int +sigaddset(set, signo) + sigset13_t *set; + int signo; +{ + if (signo <= 0 || signo >= NSIG13) { + errno = EINVAL; + return -1; + } + *set |= __sigmask13(signo); + return (0); +} + +int +sigdelset(set, signo) + sigset13_t *set; + int signo; +{ + if (signo <= 0 || signo >= NSIG13) { + errno = EINVAL; + return -1; + } + *set &= ~__sigmask13(signo); + return (0); +} + +int +sigismember(set, signo) + const sigset13_t *set; + int signo; +{ + if (signo <= 0 || signo >= NSIG13) { + errno = EINVAL; + return -1; + } + return ((*set & __sigmask13(signo)) != 0); +} diff --git a/lib/nbsd_libc/compat/gen/compat_time.c b/lib/nbsd_libc/compat/gen/compat_time.c new file mode 100644 index 000000000..969949034 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_time.c @@ -0,0 +1,27 @@ +/* $NetBSD: compat_time.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/* + * Written by Jason R. Thorpe , October 21, 1997. + * Public domain. + */ + +#include "namespace.h" +#include + +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(time,_time) +#endif + +__warn_references(time, + "warning: reference to compatibility time();" + " include for correct reference") + +#define timeval timeval50 +#define time_t int32_t +#include "gen/time.c" diff --git a/lib/nbsd_libc/compat/gen/compat_times.c b/lib/nbsd_libc/compat/gen/compat_times.c new file mode 100644 index 000000000..8c4a7e5f0 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_times.c @@ -0,0 +1,28 @@ +/* $NetBSD: compat_times.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/* + * Ben Harris, 2002. + * This file is in the Public Domain. + */ + +#define __LIBC12_SOURCE__ +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(times,_times) +#endif + +__warn_references(times, + "warning: reference to compatibility times(); include for correct reference") + +#define __times_rusage struct rusage50 +#define __times_timeval struct timeval50 + +#include "gen/times.c" diff --git a/lib/nbsd_libc/compat/gen/compat_timezone.c b/lib/nbsd_libc/compat/gen/compat_timezone.c new file mode 100644 index 000000000..91a4f0147 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_timezone.c @@ -0,0 +1,143 @@ +/* $NetBSD: compat_timezone.c,v 1.3 2009/01/11 02:46:25 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)timezone.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_timezone.c,v 1.3 2009/01/11 02:46:25 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +__warn_references(timezone, + "warning: reference to compatibility timezone; include to generate correct reference") + +/* + * timezone -- + * The arguments are the number of minutes of time you are westward + * from Greenwich and whether DST is in effect. It returns a string + * giving the name of the local timezone. Should be replaced, in the + * application code, by a call to localtime. + */ + +char *_tztab(int, int); + +static char czone[TZ_MAX_CHARS]; /* space for zone name */ + +char *timezone(int, int); +char * +timezone(int zone, int dst) +{ + char *beg, + *end; + + if ((beg = getenv("TZNAME")) != NULL) { /* set in environment */ + if ((end = strchr(beg, ',')) != NULL) { /* "PST,PDT" */ + if (dst) + return(++end); + *end = '\0'; + (void)strlcpy(czone, beg, sizeof(czone)); + *end = ','; + return(czone); + } + return(beg); + } + return(_tztab(zone,dst)); /* default: table or created zone */ +} + +static const struct zone { + int offset; + const char *stdzone; + const char *dlzone; +} zonetab[] = { + { -1*60, "MET", "MET DST" }, /* Middle European */ + { -2*60, "EET", "EET DST" }, /* Eastern European */ + { 4*60, "AST", "ADT" }, /* Atlantic */ + { 5*60, "EST", "EDT" }, /* Eastern */ + { 6*60, "CST", "CDT" }, /* Central */ + { 7*60, "MST", "MDT" }, /* Mountain */ + { 8*60, "PST", "PDT" }, /* Pacific */ +#ifdef notdef + /* there's no way to distinguish this from WET */ + { 0, "GMT", 0 }, /* Greenwich */ +#endif + { 0*60, "WET", "WET DST" }, /* Western European */ + { -10*60, "EST", "EST" }, /* Aust: Eastern */ + { -10*60+30, "CST", "CST" }, /* Aust: Central */ + { -8*60, "WST", 0 }, /* Aust: Western */ + { -1, NULL, NULL } +}; + +/* + * _tztab -- + * check static tables or create a new zone name; broken out so that + * we can make a guess as to what the zone is if the standard tables + * aren't in place in /etc. DO NOT USE THIS ROUTINE OUTSIDE OF THE + * STANDARD LIBRARY. + */ +char * +_tztab(zone,dst) + int zone; + int dst; +{ + const struct zone *zp; + char sign; + + for (zp = zonetab; zp->offset != -1;++zp) /* static tables */ + if (zp->offset == zone) { + if (dst && zp->dlzone) + return __UNCONST(zp->dlzone); + if (!dst && zp->stdzone) + return __UNCONST(zp->stdzone); + } + + if (zone < 0) { /* create one */ + zone = -zone; + sign = '+'; + } + else + sign = '-'; + (void)snprintf(czone, TZ_MAX_CHARS, "GMT%c%d:%02d", sign, zone / 60, + zone % 60); + return(czone); +} diff --git a/lib/nbsd_libc/compat/gen/compat_unvis.c b/lib/nbsd_libc/compat/gen/compat_unvis.c new file mode 100644 index 000000000..a90512cdd --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_unvis.c @@ -0,0 +1,64 @@ +/* $NetBSD: compat_unvis.c,v 1.2 2007/03/10 01:13:14 hubertf Exp $ */ + +/*- + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_unvis.c,v 1.2 2007/03/10 01:13:14 hubertf Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(unvis,_unvis) +#endif + +#ifdef __warn_references +__warn_references(unvis, + "warning: reference to compatibility unvis(); include for correct reference") +#endif + +int +unvis(char *cp, int c, int *astate, int flag) +{ + return __unvis13(cp, c, astate, flag); +} diff --git a/lib/nbsd_libc/compat/gen/compat_utime.c b/lib/nbsd_libc/compat/gen/compat_utime.c new file mode 100644 index 000000000..1c9c0e3e9 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_utime.c @@ -0,0 +1,77 @@ +/* $NetBSD: compat_utime.c,v 1.2 2009/01/11 02:46:25 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)utime.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_utime.c,v 1.2 2009/01/11 02:46:25 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include + +__warn_references(utime, + "warning: reference to compatibility utime(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(utime, _utime) +#endif + +int +utime(const char *path, const struct utimbuf50 *times50) +{ + struct timeval tv[2], *tvp; + + _DIAGASSERT(path != NULL); + + if (times50 == NULL) + tvp = NULL; + else { + tv[0].tv_sec = times50->actime; + tv[1].tv_sec = times50->modtime; + tv[0].tv_usec = tv[1].tv_usec = 0; + tvp = tv; + } + return __utimes50(path, tvp); +} diff --git a/lib/nbsd_libc/compat/gen/compat_utmp.c b/lib/nbsd_libc/compat/gen/compat_utmp.c new file mode 100644 index 000000000..be986822e --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_utmp.c @@ -0,0 +1,66 @@ +/* $NetBSD: compat_utmp.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_utmp.c,v 1.2 2009/01/11 02:46:25 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include +#include +#include +#include +#include + +__warn_references(getutent, + "warning: reference to compatibility getutent(); include for correct reference") + +static struct utmp50 * +cvt(struct utmp *ut) +{ + struct utmp50 *ut50 = (void *)ut; + if (ut == NULL) + return NULL; + ut50->ut_time = (int32_t)ut->ut_time; + return ut50; +} + +struct utmp50 * +getutent(void) +{ + return cvt(__getutent50()); +} diff --git a/lib/nbsd_libc/compat/gen/compat_utmpx.c b/lib/nbsd_libc/compat/gen/compat_utmpx.c new file mode 100644 index 000000000..44784a268 --- /dev/null +++ b/lib/nbsd_libc/compat/gen/compat_utmpx.c @@ -0,0 +1,194 @@ +/* $NetBSD: compat_utmpx.c,v 1.3 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_utmpx.c,v 1.3 2009/01/11 02:46:25 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#define __LIBC12_SOURCE__ +#include +#include +#include +#include +#include +#include + +__warn_references(getutxent, + "warning: reference to compatibility getutxent(); include for correct reference") +__warn_references(getutxid, + "warning: reference to compatibility getutxid(); include for correct reference") +__warn_references(getutxline, + "warning: reference to compatibility getutxline(); include for correct reference") +__warn_references(pututxline, + "warning: reference to compatibility pututxline(); include for correct reference") +__warn_references(updwtmpx, + "warning: reference to compatibility updwtmpx(); include for correct reference") +__warn_references(getlastlogx, + "warning: reference to compatibility getlastlogx(); include for correct reference") +__warn_references(updlastlogx, + "warning: reference to compatibility updlastlogx(); include for correct reference") +__warn_references(getutmp, + "warning: reference to compatibility getutmp(); include for correct reference") +__warn_references(getutmpx, + "warning: reference to compatibility getutmpx(); include for correct reference") + +static struct utmpx50 * +cvt(struct utmpx *ut) +{ + if (ut == NULL) + return NULL; + timeval_to_timeval50(&ut->ut_tv, (void *)&ut->ut_tv); + return (void *)ut; +} + +static void +lastlogx50_to_lastlogx(const struct lastlogx50 *ll50, struct lastlogx *ll) +{ + (void)memcpy(ll->ll_line, ll50->ll_line, sizeof(ll->ll_line)); + (void)memcpy(ll->ll_host, ll50->ll_host, sizeof(ll->ll_host)); + (void)memcpy(&ll->ll_ss, &ll50->ll_ss, sizeof(&ll->ll_ss)); + timeval50_to_timeval(&ll50->ll_tv, &ll->ll_tv); +} + +static void +lastlogx_to_lastlogx50(const struct lastlogx *ll, struct lastlogx50 *ll50) +{ + (void)memcpy(ll50->ll_line, ll->ll_line, sizeof(ll50->ll_line)); + (void)memcpy(ll50->ll_host, ll->ll_host, sizeof(ll50->ll_host)); + (void)memcpy(&ll50->ll_ss, &ll->ll_ss, sizeof(&ll50->ll_ss)); + timeval_to_timeval50(&ll->ll_tv, &ll50->ll_tv); +} + +struct utmpx50 * +getutxent(void) +{ + return cvt(__getutxent50()); +} + +struct utmpx50 * +getutxid(const struct utmpx50 *ut50) +{ + struct utmpx ut; + utmpx50_to_utmpx(ut50, &ut); + return cvt(__getutxid50(&ut)); +} + +struct utmpx50 * +getutxline(const struct utmpx50 *ut50) +{ + struct utmpx ut; + utmpx50_to_utmpx(ut50, &ut); + return cvt(__getutxline50(&ut)); +} + +struct utmpx50 * +pututxline(const struct utmpx50 *ut50) +{ + struct utmpx ut; + utmpx50_to_utmpx(ut50, &ut); + return cvt(__pututxline50(&ut)); +} + +int +updwtmpx(const char *fname, const struct utmpx50 *ut50) +{ + struct utmpx ut; + utmpx50_to_utmpx(ut50, &ut); + return __updwtmpx50(fname, &ut); +} + +struct lastlogx50 * +__getlastlogx13(const char *fname, uid_t uid, struct lastlogx50 *ll50) +{ + struct lastlogx ll; + if (__getlastlogx50(fname, uid, &ll) == NULL) + return NULL; + lastlogx_to_lastlogx50(&ll, ll50); + return ll50; +} + +static char llfile[MAXPATHLEN] = _PATH_LASTLOGX; + +int +lastlogxname(const char *fname) +{ + size_t len; + + _DIAGASSERT(fname != NULL); + + len = strlen(fname); + + if (len >= sizeof(llfile)) + return 0; + + /* must end in x! */ + if (fname[len - 1] != 'x') + return 0; + + (void)strlcpy(llfile, fname, sizeof(llfile)); + return 1; +} + +struct lastlogx50 * +getlastlogx(uid_t uid, struct lastlogx50 *ll) +{ + + return __getlastlogx13(llfile, uid, ll); +} + +int +updlastlogx(const char *fname, uid_t uid, struct lastlogx50 *ll50) +{ + struct lastlogx ll; + lastlogx50_to_lastlogx(ll50, &ll); + return __updlastlogx50(fname, uid, &ll); +} + +void +getutmp(const struct utmpx50 *utx50, struct utmp *ut) +{ + struct utmpx utx; + utmpx50_to_utmpx(utx50, &utx); + __getutmp50(&utx, ut); +} + +void +getutmpx(const struct utmp *ut, struct utmpx50 *utx50) +{ + struct utmpx utx; + __getutmpx50(ut, &utx); + utmpx_to_utmpx50(&utx, utx50); +} diff --git a/lib/nbsd_libc/compat/include/aio.h b/lib/nbsd_libc/compat/include/aio.h new file mode 100644 index 000000000..60d80ee20 --- /dev/null +++ b/lib/nbsd_libc/compat/include/aio.h @@ -0,0 +1,51 @@ +/* $NetBSD: aio.h,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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. + */ +#ifndef _COMPAT_AIO_H_ +#define _COMPAT_AIO_H_ + +__BEGIN_DECLS +struct aiocb; +struct timespec50; +struct timespec; +int aio_suspend(const struct aiocb * const [], int, + const struct timespec50 *); +int __aio_suspend50(const struct aiocb * const [], int, + const struct timespec *); +__END_DECLS + +#endif /* _COMPAT_AIO_H_ */ diff --git a/lib/nbsd_libc/compat/include/dirent.h b/lib/nbsd_libc/compat/include/dirent.h new file mode 100644 index 000000000..f41fc32f9 --- /dev/null +++ b/lib/nbsd_libc/compat/include/dirent.h @@ -0,0 +1,78 @@ +/* $NetBSD: dirent.h,v 1.7 2009/02/24 18:42:36 christos Exp $ */ + +/*- + * Copyright (c) 1989, 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. + * + * @(#)dirent.h 8.2 (Berkeley) 7/28/94 + */ + +#ifndef _COMPAT_DIRENT_H_ +#define _COMPAT_DIRENT_H_ + +#include + +__BEGIN_DECLS + +DIR *opendir(const char *); +DIR *__opendir30(const char *); + +int readdir_r(DIR *, struct dirent12 * __restrict, + struct dirent12 ** __restrict); +int __readdir_r30(DIR * __restrict, struct dirent * __restrict, + struct dirent ** __restrict); + +struct dirent12 *readdir(DIR *); +struct dirent *__readdir30(DIR *); + +#if defined(_NETBSD_SOURCE) + +struct dirent12 *_readdir_unlocked(DIR *, int); +struct dirent *___readdir_unlocked50(DIR *, int); +struct dirent *___readdir_unlocked30(DIR *); + +DIR *__opendir2(const char *, int); +DIR *__opendir230(const char *, int); + +int scandir(const char *, struct dirent12 ***, + int (*)(const struct dirent12 *), int (*)(const void *, const void *)); +int __scandir30(const char *, struct dirent ***, + int (*)(const struct dirent *), int (*)(const void *, const void *)); + +int getdents(int, char *, size_t); +int __getdents30(int, char *, size_t); + +int alphasort(const void *, const void *); +int __alphasort30(const void *, const void *); + +int getdirentries(int, char *, int, long *); + +#endif /* defined(_NETBSD_SOURCE) */ + +__END_DECLS + +#endif /* !_COMPAT_DIRENT_H_ */ diff --git a/lib/nbsd_libc/compat/include/fstypes.h b/lib/nbsd_libc/compat/include/fstypes.h new file mode 100644 index 000000000..06868b768 --- /dev/null +++ b/lib/nbsd_libc/compat/include/fstypes.h @@ -0,0 +1,50 @@ +/* $NetBSD: fstypes.h,v 1.4 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Martin Husemann . + * + * 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. + */ + +#ifndef _COMPAT_FSTYPES_H_ +#define _COMPAT_FSTYPES_H_ + +#include + +/* Old, fixed size filehandle structures (used upto (including) 3.x) */ +struct compat_30_fid { + unsigned short fid30_len; + unsigned short fid30_reserved; + char fid30_data[16]; +}; +struct compat_30_fhandle { + fsid_t fh_fsid; + struct compat_30_fid fh_fid; +}; + +#define FHANDLE30_SIZE sizeof(struct compat_30_fhandle) /* 28 */ + +#endif /* _COMPAT_FSTYPES_H_ */ diff --git a/lib/nbsd_libc/compat/include/fts.h b/lib/nbsd_libc/compat/include/fts.h new file mode 100644 index 000000000..de95bcfd6 --- /dev/null +++ b/lib/nbsd_libc/compat/include/fts.h @@ -0,0 +1,46 @@ +/* $NetBSD: fts.h,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Copyright (c) 1989, 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. + * + * @(#)fts.h 8.3 (Berkeley) 8/14/94 + */ + +#ifndef _COMPAT_FTS_H_ +#define _COMPAT_FTS_H_ + +__BEGIN_DECLS +FTSENT *fts_children(FTS *, int); +int fts_close(FTS *); +FTS *fts_open(char * const *, int, + int (*)(const FTSENT **, const FTSENT **)); +FTSENT *fts_read(FTS *); +int fts_set(FTS *, FTSENT *, int); +__END_DECLS + +#endif /* !_COMPAT_FTS_H_ */ diff --git a/lib/nbsd_libc/compat/include/glob.h b/lib/nbsd_libc/compat/include/glob.h new file mode 100644 index 000000000..cd6860724 --- /dev/null +++ b/lib/nbsd_libc/compat/include/glob.h @@ -0,0 +1,46 @@ +/* $NetBSD: glob.h,v 1.1 2005/09/13 01:44:09 christos 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 + * Guido van Rossum. + * + * 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. + * + * @(#)glob.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _COMPAT_GLOB_H_ +#define _COMPAT_GLOB_H_ + +__BEGIN_DECLS +int glob(const char * __restrict, int, + int (*)(const char *, int), glob_t * __restrict); +void globfree(glob_t *); +__END_DECLS + +#endif /* !_COMPAT_GLOB_H_ */ diff --git a/lib/nbsd_libc/compat/include/locale.h b/lib/nbsd_libc/compat/include/locale.h new file mode 100644 index 000000000..455be3079 --- /dev/null +++ b/lib/nbsd_libc/compat/include/locale.h @@ -0,0 +1,44 @@ +/* $NetBSD: locale.h,v 1.1 2010/06/07 13:52:30 tnozaki Exp $ */ + +/* + * Copyright (c) 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. + * + * @(#)locale.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _COMPAT_LOCALE_H_ +#define _COMPAT_LOCALE_H_ + +#define _LC_LAST 7 /* marks end */ + +__BEGIN_DECLS +char *setlocale(int, const char *); +char *__setlocale_mb_len_max_32(int, const char *); +__END_DECLS + +#endif /* _COMPAT_LOCALE_H_ */ diff --git a/lib/nbsd_libc/compat/include/lwp.h b/lib/nbsd_libc/compat/include/lwp.h new file mode 100644 index 000000000..bdad6335b --- /dev/null +++ b/lib/nbsd_libc/compat/include/lwp.h @@ -0,0 +1,46 @@ +/* $NetBSD: lwp.h,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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. + */ +#ifndef _COMPAT_LWP_H_ +#define _COMPAT_LWP_H_ + +__BEGIN_DECLS +int _lwp_park(const struct timespec50 *, lwpid_t, const void *, const void *); +int ___lwp_park50(const struct timespec *, lwpid_t, const void *, const void *); +__END_DECLS + +#endif /* !_COMPAT_LWP_H_ */ diff --git a/lib/nbsd_libc/compat/include/mqueue.h b/lib/nbsd_libc/compat/include/mqueue.h new file mode 100644 index 000000000..dd0cf61d8 --- /dev/null +++ b/lib/nbsd_libc/compat/include/mqueue.h @@ -0,0 +1,52 @@ +/* $NetBSD: mqueue.h,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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. + */ +#ifndef _COMPAT_MQUEUE_H_ +#define _COMPAT_MQUEUE_H_ + +__BEGIN_DECLS +ssize_t mq_timedreceive(mqd_t, char * __restrict, size_t, + unsigned * __restrict, const struct timespec50 * __restrict); +int mq_timedsend(mqd_t, const char *, size_t, unsigned, + const struct timespec50 *); +ssize_t __mq_timedreceive50(mqd_t, char * __restrict, size_t, + unsigned * __restrict, const struct timespec * __restrict); +int __mq_timedsend50(mqd_t, const char *, size_t, unsigned, + const struct timespec *); +__END_DECLS + +#endif /* _COMPAT_MQUEUE_H_ */ diff --git a/lib/nbsd_libc/compat/include/ndbm.h b/lib/nbsd_libc/compat/include/ndbm.h new file mode 100644 index 000000000..99a0a9b22 --- /dev/null +++ b/lib/nbsd_libc/compat/include/ndbm.h @@ -0,0 +1,53 @@ +/* $NetBSD: ndbm.h,v 1.1 2005/09/13 01:44:09 christos 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 + * Margo Seltzer. + * + * 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. + * + * @(#)ndbm.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _COMPAT_NDBM_H_ +#define _COMPAT_NDBM_H_ + +typedef struct { + void *dptr; + int dsize; +} datum12; + +__BEGIN_DECLS +int dbm_delete(DBM *, datum12); +datum12 dbm_fetch(DBM *, datum12); +datum12 dbm_firstkey(DBM *); +datum12 dbm_nextkey(DBM *); +int dbm_store(DBM *, datum12, datum12, int); +__END_DECLS + +#endif /* !_COMPAT_NDBM_H_ */ diff --git a/lib/nbsd_libc/compat/include/ns.h b/lib/nbsd_libc/compat/include/ns.h new file mode 100644 index 000000000..97d9cd473 --- /dev/null +++ b/lib/nbsd_libc/compat/include/ns.h @@ -0,0 +1,66 @@ +/* $NetBSD: ns.h,v 1.1 2006/08/26 16:07:01 matt Exp $ */ + +/* + * Copyright (c) 1984, 1985, 1986, 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. + * + * @(#)ns.h 8.1 (Berkeley) 6/10/93 + */ + +#ifndef _NETNS_NS_H_ +#define _NETNS_NS_H_ + +/* + * NS addressing + */ +union ns_host { + u_int8_t c_host[6]; + u_int16_t s_host[3]; +}; + +union ns_net { + u_int8_t c_net[4]; + u_int16_t s_net[2]; +}; + +union ns_net_u { + union ns_net net_e; + u_int32_t long_e; +}; + +struct ns_addr { + union ns_net x_net; + union ns_host x_host; + u_int16_t x_port; +}; + +__BEGIN_DECLS +extern struct ns_addr ns_addr __P((const char *)); +extern char *ns_ntoa __P((struct ns_addr)); +__END_DECLS + +#endif /* !_NETNS_NS_H_ */ diff --git a/lib/nbsd_libc/compat/include/pwd.h b/lib/nbsd_libc/compat/include/pwd.h new file mode 100644 index 000000000..b9a805aaa --- /dev/null +++ b/lib/nbsd_libc/compat/include/pwd.h @@ -0,0 +1,99 @@ +/* $NetBSD: pwd.h,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +#ifndef _COMPAT_PWD_H_ +#define _COMPAT_PWD_H_ + +#include +#include +#include + +struct passwd; +struct passwd50 { + __aconst char *pw_name; /* user name */ + __aconst char *pw_passwd; /* encrypted password */ + uid_t pw_uid; /* user uid */ + gid_t pw_gid; /* user gid */ + int32_t pw_change; /* password change time */ + __aconst char *pw_class; /* user login class */ + __aconst char *pw_gecos; /* general information */ + __aconst char *pw_dir; /* home directory */ + __aconst char *pw_shell; /* default shell */ + int32_t pw_expire; /* account expiration */ +}; + +__BEGIN_DECLS +static __inline void +passwd_to_passwd50(const struct passwd *p, struct passwd50 *q) +{ + q->pw_name = p->pw_name; + q->pw_passwd = p->pw_passwd; + q->pw_uid = p->pw_uid; + q->pw_gid = p->pw_gid; + q->pw_change = (int32_t)p->pw_change; + q->pw_class = p->pw_class; + q->pw_gecos = p->pw_gecos; + q->pw_dir = p->pw_dir; + q->pw_shell = p->pw_shell; + q->pw_expire = (int32_t)p->pw_expire; +} + +static __inline void +passwd50_to_passwd(const struct passwd50 *p, struct passwd *q) +{ + q->pw_name = p->pw_name; + q->pw_passwd = p->pw_passwd; + q->pw_uid = p->pw_uid; + q->pw_gid = p->pw_gid; + q->pw_change = p->pw_change; + q->pw_class = p->pw_class; + q->pw_gecos = p->pw_gecos; + q->pw_dir = p->pw_dir; + q->pw_shell = p->pw_shell; + q->pw_expire = p->pw_expire; +} + +struct passwd50 *getpwuid(uid_t); +struct passwd50 *getpwnam(const char *); +#if (_POSIX_C_SOURCE - 0) >= 199506L || (_XOPEN_SOURCE - 0) >= 500 || \ + defined(_REENTRANT) || defined(_NETBSD_SOURCE) +int getpwnam_r(const char *, struct passwd50 *, char *, size_t, + struct passwd50 **); +int getpwuid_r(uid_t, struct passwd50 *, char *, size_t, + struct passwd50 **); +#endif +#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) +struct passwd50 *getpwent(void); +#endif +#if defined(_NETBSD_SOURCE) +int pw_scan(char *, struct passwd50 *, int *); +int getpwent_r(struct passwd50 *, char *, size_t, struct passwd50 **); +#endif +#if defined(_NETBSD_SOURCE) +int pwcache_userdb(int (*)(int), void (*)(void), + struct passwd50 * (*)(const char *), struct passwd50 * (*)(uid_t)); +#endif + +struct passwd *__getpwuid50(uid_t); +struct passwd *__getpwnam50(const char *); +#if (_POSIX_C_SOURCE - 0) >= 199506L || (_XOPEN_SOURCE - 0) >= 500 || \ + defined(_REENTRANT) || defined(_NETBSD_SOURCE) +int __getpwnam_r50(const char *, struct passwd *, char *, size_t, + struct passwd **); +int __getpwuid_r50(uid_t, struct passwd *, char *, size_t, + struct passwd **); +#endif +#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) +struct passwd *__getpwent50(void); +#endif +#if defined(_NETBSD_SOURCE) +int __pw_scan50(char *, struct passwd *, int *); +int __getpwent_r50(struct passwd *, char *, size_t, struct passwd **); +#endif +int setpassent(int); +#if defined(_NETBSD_SOURCE) +int __pwcache_userdb50(int (*)(int), void (*)(void), + struct passwd * (*)(const char *), struct passwd * (*)(uid_t)); +#endif +__END_DECLS + +#endif /* !_COMPAT_PWD_H_ */ diff --git a/lib/nbsd_libc/compat/include/rpc/pmap_clnt.h b/lib/nbsd_libc/compat/include/rpc/pmap_clnt.h new file mode 100644 index 000000000..5e075c2c5 --- /dev/null +++ b/lib/nbsd_libc/compat/include/rpc/pmap_clnt.h @@ -0,0 +1,52 @@ +/* $NetBSD: pmap_clnt.h,v 1.2 2009/01/11 03:56:22 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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. + */ + +#ifndef _COMPAT_RPC_PMAP_CLNT_H_ +#define _COMPAT_RPC_PMAP_CLNT_H_ +#include + +__BEGIN_DECLS +extern enum clnt_stat pmap_rmtcall(struct sockaddr_in *, + u_long, u_long, u_long, xdrproc_t, caddr_t, xdrproc_t, caddr_t, + struct timeval50, u_long *); +extern enum clnt_stat __pmap_rmtcall50(struct sockaddr_in *, + u_long, u_long, u_long, xdrproc_t, caddr_t, xdrproc_t, caddr_t, + struct timeval, u_long *); +__END_DECLS + +#endif /* !_RPC_PMAP_CLNT_H_ */ diff --git a/lib/nbsd_libc/compat/include/rpc/rpcb_clnt.h b/lib/nbsd_libc/compat/include/rpc/rpcb_clnt.h new file mode 100644 index 000000000..e751b659b --- /dev/null +++ b/lib/nbsd_libc/compat/include/rpc/rpcb_clnt.h @@ -0,0 +1,57 @@ +/* $NetBSD: rpcb_clnt.h,v 1.2 2009/01/11 03:56:22 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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. + */ + +#ifndef _COMPAT_RPC_RPCB_CLNT_H +#define _COMPAT_RPC_RPCB_CLNT_H + +__BEGIN_DECLS + +extern enum clnt_stat rpcb_rmtcall(const struct netconfig *, + const char *, const rpcprog_t, const rpcvers_t, const rpcproc_t, + const xdrproc_t, const char *, const xdrproc_t, caddr_t, + const struct timeval50, const struct netbuf *); +extern bool_t rpcb_gettime(const char *, int32_t *); +extern enum clnt_stat __rpcb_rmtcall50(const struct netconfig *, + const char *, const rpcprog_t, const rpcvers_t, const rpcproc_t, + const xdrproc_t, const char *, const xdrproc_t, caddr_t, + const struct timeval, const struct netbuf *); +extern bool_t __rpcb_gettime50(const char *, time_t *); + +__END_DECLS + +#endif /* !_COMPAT_RPC_RPCB_CLNT_H */ diff --git a/lib/nbsd_libc/compat/include/setjmp.h b/lib/nbsd_libc/compat/include/setjmp.h new file mode 100644 index 000000000..6160931df --- /dev/null +++ b/lib/nbsd_libc/compat/include/setjmp.h @@ -0,0 +1,50 @@ +/* $NetBSD: setjmp.h,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * 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. + * + * @(#)setjmp.h 8.2 (Berkeley) 1/21/94 + */ + +#ifndef _COMPAT_SETJMP_H_ +#define _COMPAT_SETJMP_H_ + +__BEGIN_DECLS +int __setjmp14(jmp_buf); +void __longjmp14(jmp_buf, int) __attribute__((__noreturn__)); + +int __sigsetjmp14(sigjmp_buf, int); +void __siglongjmp14(sigjmp_buf, int) __attribute__((__noreturn__)); +__END_DECLS + +#endif /* !_COMPAT_SETJMP_H_ */ diff --git a/lib/nbsd_libc/compat/include/signal.h b/lib/nbsd_libc/compat/include/signal.h new file mode 100644 index 000000000..e78a2a906 --- /dev/null +++ b/lib/nbsd_libc/compat/include/signal.h @@ -0,0 +1,83 @@ +/* $NetBSD: signal.h,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 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. + * + * @(#)signal.h 8.3 (Berkeley) 3/30/94 + */ + +#ifndef _COMPAT_SIGNAL_H_ +#define _COMPAT_SIGNAL_H_ + +#include + +__BEGIN_DECLS +int sigaction(int, const struct sigaction13 * __restrict, + struct sigaction13 * __restrict); +int __sigaction14(int, const struct sigaction * __restrict, + struct sigaction * __restrict); +int sigaddset(sigset13_t *, int); +int __sigaddset14(sigset_t *, int); +int sigdelset(sigset13_t *, int); +int __sigdelset14(sigset_t *, int); +int sigemptyset(sigset13_t *); +int __sigemptyset14(sigset_t *); +int sigfillset(sigset13_t *); +int __sigfillset14(sigset_t *); +int sigismember(const sigset13_t *, int); +int __sigismember14(const sigset_t *, int); +int sigpending(sigset13_t *); +int __sigpending14(sigset_t *); +int sigprocmask(int, const sigset13_t * __restrict, + sigset13_t * __restrict); +int __sigprocmask14(int, const sigset_t * __restrict, + sigset_t * __restrict); +int sigsuspend(const sigset13_t *); +int __sigsuspend14(const sigset_t *); + +int sigtimedwait(const sigset_t * __restrict, + siginfo_t * __restrict, const struct timespec50 * __restrict); +int __sigtimedwait(const sigset_t * __restrict, + siginfo_t * __restrict, struct timespec50 * __restrict); +int __sigtimedwait50(const sigset_t * __restrict, + siginfo_t * __restrict, const struct timespec * __restrict); +int ____sigtimedwait50(const sigset_t * __restrict, + siginfo_t * __restrict, struct timespec * __restrict); +/* + * X/Open CAE Specification Issue 4 Version 2 + */ +#if (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \ + (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE) +int sigaltstack(const struct sigaltstack13 * __restrict, + struct sigaltstack13 * __restrict); +int __sigaltstack14(const stack_t * __restrict, stack_t * __restrict); +#endif /* _XOPEN_SOURCE_EXTENDED || _XOPEN_SOURCE >= 500 || _NETBSD_SOURCE */ + +__END_DECLS + +#endif /* !_COMPAT_SIGNAL_H_ */ diff --git a/lib/nbsd_libc/compat/include/stdlib.h b/lib/nbsd_libc/compat/include/stdlib.h new file mode 100644 index 000000000..1295041fc --- /dev/null +++ b/lib/nbsd_libc/compat/include/stdlib.h @@ -0,0 +1,46 @@ +/* $NetBSD: stdlib.h,v 1.4 2009/01/26 15:05:56 drochner 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. + * + * @(#)stdlib.h 8.5 (Berkeley) 5/19/95 + */ + +#ifndef _COMPAT_STDLIB_H_ +#define _COMPAT_STDLIB_H_ + +__BEGIN_DECLS + +void unsetenv(const char *); +int __unsetenv13(const char *); +__aconst char *devname(int32_t, mode_t); +__aconst char *__devname50(dev_t, mode_t); + +__END_DECLS + +#endif /* !_COMPAT_STDLIB_H_ */ diff --git a/lib/nbsd_libc/compat/include/time.h b/lib/nbsd_libc/compat/include/time.h new file mode 100644 index 000000000..c0842f585 --- /dev/null +++ b/lib/nbsd_libc/compat/include/time.h @@ -0,0 +1,106 @@ +/* $NetBSD: time.h,v 1.3 2010/12/16 18:38:06 christos Exp $ */ + +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * 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. + * + * @(#)time.h 8.3 (Berkeley) 1/21/94 + */ + +#ifndef _COMPAT_TIME_H_ +#define _COMPAT_TIME_H_ + +#include +#if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) || \ + defined(_NETBSD_SOURCE) +#define CLK_TCK 100 +#endif + +__BEGIN_DECLS +#if (_XOPEN_SOURCE - 0) >= 4 || defined(_NETBSD_SOURCE) +char *strptime(const char * __restrict, const char * __restrict, + struct tm * __restrict); +#if 0 +#if defined(_NETBSD_SOURCE) +char *timezone(int, int); +#endif /* _NETBSD_SOURCE */ +#endif + +#endif /* !_ANSI_SOURCE */ +char *ctime(const int32_t *); +double difftime(int32_t, int32_t); +struct tm *gmtime(const int32_t *); +struct tm *localtime(const int32_t *); +int32_t time(int32_t *); +int32_t mktime(struct tm *); +void tzset(void); +void tzsetwall(void); +void __tzset50(void); +void __tzsetwall50(void); + +int clock_getres(clockid_t, struct timespec50 *); +int clock_gettime(clockid_t, struct timespec50 *); +int clock_settime(clockid_t, const struct timespec50 *); +int __clock_getres50(clockid_t, struct timespec *); +int __clock_gettime50(clockid_t, struct timespec *); +int __clock_settime50(clockid_t, const struct timespec *); +int nanosleep(const struct timespec50 *, struct timespec50 *); +int __nanosleep50(const struct timespec *, struct timespec *); +int timer_gettime(timer_t, struct itimerspec50 *); +int timer_settime(timer_t, int, const struct itimerspec50 * __restrict, + struct itimerspec50 * __restrict); +int __timer_gettime50(timer_t, struct itimerspec *); +int __timer_settime50(timer_t, int, const struct itimerspec * __restrict, + struct itimerspec * __restrict); +int __timer_getres50(timer_t, struct itimerspec *); +char *ctime_r(const int32_t *, char *); +struct tm *gmtime_r(const int32_t * __restrict, struct tm * __restrict); +struct tm *localtime_r(const int32_t * __restrict, struct tm * __restrict); +struct tm *offtime(const int32_t *, long); +struct tm *offtime_r(const int32_t *, long, struct tm *); +int32_t timelocal(struct tm *); +int32_t timegm(struct tm *); +int32_t timeoff(struct tm *, long); +int32_t time2posix(int32_t); +int32_t posix2time(int32_t); +struct tm *localtime_rz(const timezone_t, const int32_t * __restrict, + struct tm * __restrict); +char *ctime_rz(const timezone_t, const int32_t *, char *); +int32_t mktime_z(const timezone_t, struct tm *); +int32_t timelocal_z(const timezone_t, struct tm *); +int32_t time2posix_z(const timezone_t, int32_t); +int32_t posix2time_z(const timezone_t, int32_t); +timezone_t tzalloc(const char *); +void tzfree(const timezone_t); +const char *tzgetname(const timezone_t, int); + +#endif /* !_COMPAT_TIME_H_ */ diff --git a/lib/nbsd_libc/compat/include/unistd.h b/lib/nbsd_libc/compat/include/unistd.h new file mode 100644 index 000000000..d607a01b2 --- /dev/null +++ b/lib/nbsd_libc/compat/include/unistd.h @@ -0,0 +1,71 @@ +/* $NetBSD: unistd.h,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1998, 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. + */ + +/* + * Copyright (c) 1991, 1993, 1994 + * 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. + * + * @(#)unistd.h 8.12 (Berkeley) 4/27/95 + */ + +#ifndef _COMPAT_UNISTD_H_ +#define _COMPAT_UNISTD_H_ + +__BEGIN_DECLS +pid_t vfork(void); +pid_t __vfork14(void); +__END_DECLS + +#endif /* !_COMPAT_UNISTD_H_ */ diff --git a/lib/nbsd_libc/compat/include/utime.h b/lib/nbsd_libc/compat/include/utime.h new file mode 100644 index 000000000..33be9e926 --- /dev/null +++ b/lib/nbsd_libc/compat/include/utime.h @@ -0,0 +1,51 @@ +/* $NetBSD: utime.h,v 1.2 2009/01/11 02:46:25 christos 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. + * + * @(#)utime.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _COMPAT_UTIME_H_ +#define _COMPAT_UTIME_H_ + +#include + +struct utimbuf50 { + int32_t actime; /* Access time */ + int32_t modtime; /* Modification time */ +}; + +#include + +__BEGIN_DECLS +int utime(const char *, const struct utimbuf50 *); +int __utime50(const char *, const struct utimbuf *); +__END_DECLS + +#endif /* !_COMPAT_UTIME_H_ */ diff --git a/lib/nbsd_libc/compat/include/utmp.h b/lib/nbsd_libc/compat/include/utmp.h new file mode 100644 index 000000000..f6b7f13d4 --- /dev/null +++ b/lib/nbsd_libc/compat/include/utmp.h @@ -0,0 +1,68 @@ +/* $NetBSD: utmp.h,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * 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. + * + * @(#)utmp.h 8.2 (Berkeley) 1/21/94 + */ + +#ifndef _COMPAT_UTMP_H_ +#define _COMPAT_UTMP_H_ + +struct utmp50 { + char ut_line[UT_LINESIZE]; + char ut_name[UT_NAMESIZE]; + char ut_host[UT_HOSTSIZE]; + int32_t ut_time; +}; + +__BEGIN_DECLS +static __inline void +utmp_to_utmp50(const struct utmp *ut, struct utmp50 *ut50) +{ + (void)memcpy(ut50, ut, sizeof(*ut50)); + ut50->ut_time = (int32_t)ut->ut_time; +} + +static __inline void +utmp50_to_utmp(const struct utmp50 *ut50, struct utmp *ut) +{ + (void)memcpy(ut, ut50, sizeof(*ut50)); + ut->ut_time = ut50->ut_time; +} + +struct utmp50 *getutent(void); +struct utmp *__getutent50(void); +__END_DECLS + +#endif /* !_COMPAT_UTMP_H_ */ diff --git a/lib/nbsd_libc/compat/include/utmpx.h b/lib/nbsd_libc/compat/include/utmpx.h new file mode 100644 index 000000000..ee201964c --- /dev/null +++ b/lib/nbsd_libc/compat/include/utmpx.h @@ -0,0 +1,101 @@ +/* $NetBSD: utmpx.h,v 1.3 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + */ +#ifndef _COMPAT_UTMPX_H_ +#define _COMPAT_UTMPX_H_ + +#include + +struct utmpx50 { + char ut_name[_UTX_USERSIZE]; /* login name */ + char ut_id[_UTX_IDSIZE]; /* inittab id */ + char ut_line[_UTX_LINESIZE]; /* tty name */ + char ut_host[_UTX_HOSTSIZE]; /* host name */ + uint16_t ut_session; /* session id used for windowing */ + uint16_t ut_type; /* type of this entry */ + pid_t ut_pid; /* process id creating the entry */ + struct { + uint16_t e_termination; /* process termination signal */ + uint16_t e_exit; /* process exit status */ + } ut_exit; + struct sockaddr_storage ut_ss; /* address where entry was made from */ + struct timeval50 ut_tv; /* time entry was created */ + uint32_t ut_pad[10]; /* reserved for future use */ +}; + +struct lastlogx50 { + struct timeval50 ll_tv; /* time entry was created */ + char ll_line[_UTX_LINESIZE]; /* tty name */ + char ll_host[_UTX_HOSTSIZE]; /* host name */ + struct sockaddr_storage ll_ss; /* address where entry was made from */ +}; + +__BEGIN_DECLS + +static __inline void +utmpx50_to_utmpx(const struct utmpx50 *ut50, struct utmpx *ut) +{ + (void)memcpy(ut, ut50, sizeof(*ut)); + timeval50_to_timeval(&ut50->ut_tv, &ut->ut_tv); +} + +static __inline void +utmpx_to_utmpx50(const struct utmpx *ut, struct utmpx50 *ut50) +{ + (void)memcpy(ut50, ut, sizeof(*ut50)); + timeval_to_timeval50(&ut->ut_tv, &ut50->ut_tv); +} + +struct utmpx50 *getutxent(void); +struct utmpx *__getutxent50(void); +struct utmpx50 *getutxid(const struct utmpx50 *); +struct utmpx *__getutxid50(const struct utmpx *); +struct utmpx50 *getutxline(const struct utmpx50 *); +struct utmpx *__getutxline50(const struct utmpx *); +struct utmpx50 *pututxline(const struct utmpx50 *); +struct utmpx *__pututxline50(const struct utmpx *); +int updwtmpx(const char *, const struct utmpx50 *); +int __updwtmpx50(const char *, const struct utmpx *); +int updlastlogx(const char *, uid_t, struct lastlogx50 *); +int __updlastlogx50(const char *, uid_t, struct lastlogx *); +struct utmp; +void getutmp(const struct utmpx50 *, struct utmp *); +void __getutmp50(const struct utmpx *, struct utmp *); +void getutmpx(const struct utmp *, struct utmpx50 *); +void __getutmpx50(const struct utmp *, struct utmpx *); + +int lastlogxname(const char *); +struct lastlogx50 *getlastlogx(uid_t, struct lastlogx50 *); +struct lastlogx50 *__getlastlogx13(const char *, uid_t, struct lastlogx50 *); +struct lastlogx *__getlastlogx50(const char *, uid_t, struct lastlogx *); + +__END_DECLS + +#endif /* !_COMPAT_UTMPX_H_ */ diff --git a/lib/nbsd_libc/compat/include/vis.h b/lib/nbsd_libc/compat/include/vis.h new file mode 100644 index 000000000..83b17c6e8 --- /dev/null +++ b/lib/nbsd_libc/compat/include/vis.h @@ -0,0 +1,42 @@ +/* $NetBSD: vis.h,v 1.1 2005/09/13 01:44:09 christos 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. + * + * @(#)vis.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _COMPAT_VIS_H_ +#define _COMPAT_VIS_H_ + +__BEGIN_DECLS +int unvis(char *, int, int *, int); +int __unvis13(char *, int, int *, int); +__END_DECLS + +#endif /* !_COMPAT_VIS_H_ */ diff --git a/lib/nbsd_libc/compat/locale/Makefile.inc b/lib/nbsd_libc/compat/locale/Makefile.inc new file mode 100644 index 000000000..b714fccf9 --- /dev/null +++ b/lib/nbsd_libc/compat/locale/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2010/06/07 13:52:30 tnozaki Exp $ + +.PATH: ${COMPATDIR}/arch/${MACHINE_ARCH}/locale ${COMPATDIR}/locale + +CPPFLAGS+= -I${COMPATDIR}/../locale +SRCS+= compat_setlocale1.c compat_setlocale32.c diff --git a/lib/nbsd_libc/compat/locale/compat_setlocale1.c b/lib/nbsd_libc/compat/locale/compat_setlocale1.c new file mode 100644 index 000000000..bb4230dfa --- /dev/null +++ b/lib/nbsd_libc/compat/locale/compat_setlocale1.c @@ -0,0 +1,57 @@ +/* $NetBSD: compat_setlocale1.c,v 1.1 2010/06/07 13:52:30 tnozaki Exp $ */ + +/*- + * Copyright (c)1999 Citrus Project, + * 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 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. + * + * NetBSD: setlocale1.c,v 1.4 2010/05/22 13:50:02 tnozaki Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_setlocale1.c,v 1.1 2010/06/07 13:52:30 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include "setlocale_local.h" + +__warn_references(setlocale, + "warning: reference to compatibility setlocale();" + "include for correct reference") + +/* + * Preparation for the future import of multibyte locale. + * This function will ensure binary compatibility for old executables. + */ +char * +setlocale(int category, const char *locale) +{ + + /* locale may be NULL */ + + __mb_len_max_runtime = 1; + return __setlocale(category, locale); +} diff --git a/lib/nbsd_libc/compat/locale/compat_setlocale32.c b/lib/nbsd_libc/compat/locale/compat_setlocale32.c new file mode 100644 index 000000000..28cd07e17 --- /dev/null +++ b/lib/nbsd_libc/compat/locale/compat_setlocale32.c @@ -0,0 +1,53 @@ +/* $NetBSD: compat_setlocale32.c,v 1.1 2010/06/07 13:52:30 tnozaki Exp $ */ + +/*- + * Copyright (c)1999 Citrus Project, + * 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 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. + * + * NetBSD: setlocale32.c,v 1.6 2010/05/22 13:50:02 tnozaki Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_setlocale32.c,v 1.1 2010/06/07 13:52:30 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include "setlocale_local.h" + +__warn_references(__setlocale_mb_len_max_32, + "warning: reference to compatibility __setlocale_mb_len_max_32();" + "include for correct reference") + +char * +__setlocale_mb_len_max_32(int category, const char *locale) +{ + + /* locale may be NULL */ + + __mb_len_max_runtime = 32; + return __setlocale(category, locale); +} diff --git a/lib/nbsd_libc/compat/net/Makefile.inc b/lib/nbsd_libc/compat/net/Makefile.inc new file mode 100644 index 000000000..1a59b9380 --- /dev/null +++ b/lib/nbsd_libc/compat/net/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.1 2006/08/26 16:07:01 matt Exp $ + +.PATH: ${COMPATDIR}/net +SRCS+=compat_ns_addr.c compat_ns_ntoa.c diff --git a/lib/nbsd_libc/compat/net/compat_ns_addr.c b/lib/nbsd_libc/compat/net/compat_ns_addr.c new file mode 100644 index 000000000..be55f3bbf --- /dev/null +++ b/lib/nbsd_libc/compat/net/compat_ns_addr.c @@ -0,0 +1,250 @@ +/* $NetBSD: compat_ns_addr.c,v 1.1 2006/08/26 16:07:01 matt Exp $ */ + +/* + * Copyright (c) 1986, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * J.Q. Johnson. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93"; +#else +__RCSID("$NetBSD: compat_ns_addr.c,v 1.1 2006/08/26 16:07:01 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include + +static void Field __P((char *, u_int8_t *, int)); +static void cvtbase __P((long, int, int[], int, u_int8_t[], int)); + +struct ns_addr +ns_addr(name) + const char *name; +{ + char separator; + char *hostname, *socketname, *cp; + char buf[50]; + static struct ns_addr addr; + + _DIAGASSERT(name != NULL); + + (void)strlcpy(buf, name, sizeof(buf)); + + /* + * First, figure out what he intends as a field separtor. + * Despite the way this routine is written, the prefered + * form 2-272.AA001234H.01777, i.e. XDE standard. + * Great efforts are made to insure backward compatibility. + */ + if ((hostname = strchr(buf, '#')) != NULL) + separator = '#'; + else { + hostname = strchr(buf, '.'); + if ((cp = strchr(buf, ':')) && + ((hostname && cp < hostname) || (hostname == 0))) { + hostname = cp; + separator = ':'; + } else + separator = '.'; + } + if (hostname) + *hostname++ = 0; + + memset(&addr, '\0', sizeof(addr)); + Field(buf, addr.x_net.c_net, 4); + if (hostname == 0) + return (addr); /* No separator means net only */ + + socketname = strchr(hostname, separator); + if (socketname) { + *socketname++ = 0; + Field(socketname, (u_int8_t *)(void *)&addr.x_port, 2); + } + + Field(hostname, addr.x_host.c_host, 6); + + return (addr); +} + +static void +Field(buf, out, len) + char *buf; + u_int8_t *out; + int len; +{ + register char *bp = buf; + int i, ibase, base16 = 0, base10 = 0, clen = 0; + int hb[6], *hp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(out != NULL); + + /* + * first try 2-273#2-852-151-014#socket + */ + if ((*buf != '-') && + (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { + cvtbase(1000L, 256, hb, i, out, len); + return; + } + /* + * try form 8E1#0.0.AA.0.5E.E6#socket + */ + if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { + cvtbase(256L, 256, hb, i, out, len); + return; + } + /* + * try form 8E1#0:0:AA:0:5E:E6#socket + */ + if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { + cvtbase(256L, 256, hb, i, out, len); + return; + } + /* + * This is REALLY stretching it but there was a + * comma notation separting shorts -- definitely non standard + */ + if (1 < (i = sscanf(buf,"%x,%x,%x", + &hb[0], &hb[1], &hb[2]))) { + hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); + hb[2] = htons(hb[2]); + cvtbase(65536L, 256, hb, i, out, len); + return; + } + + /* Need to decide if base 10, 16 or 8 */ + while (*bp) switch (*bp++) { + + case '0': case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '-': + break; + + case '8': case '9': + base10 = 1; + break; + + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + base16 = 1; + break; + + case 'x': case 'X': + *--bp = '0'; + base16 = 1; + break; + + case 'h': case 'H': + base16 = 1; + /* FALLTHROUGH */ + + default: + *--bp = 0; /* Ends Loop */ + } + if (base16) { + ibase = 4096; + } else if (base10 == 0 && *buf == '0') { + ibase = 512; + } else { + base10 = 1; + ibase = 1000; + } + + for (bp = buf; *bp++; ) clen++; + if (clen == 0) clen++; + if (clen > 18) clen = 18; + i = ((clen - 1) / 3) + 1; + bp = clen + buf - 3; + hp = hb + i - 1; + + while (hp > hb) { + if (base16) + (void)sscanf(bp, "%3x", hp); + else if (base10) + (void)sscanf(bp, "%3d", hp); + else + (void)sscanf(bp, "%3o", hp); + + bp[0] = 0; + hp--; + bp -= 3; + } + if (base16) + (void)sscanf(buf, "%3x", hp); + else if (base10) + (void)sscanf(buf, "%3d", hp); + else + (void)sscanf(buf, "%3o", hp); + + cvtbase((long)ibase, 256, hb, i, out, len); +} + +static void +cvtbase(oldbase,newbase,input,inlen,result,reslen) + long oldbase; + int newbase; + int input[]; + int inlen; + unsigned char result[]; + int reslen; +{ + int d, e; + long sum; + + _DIAGASSERT(input != NULL); + _DIAGASSERT(result != NULL); + + e = 1; + while (e > 0 && reslen > 0) { + d = 0; e = 0; sum = 0; + /* long division: input=input/newbase */ + while (d < inlen) { + sum = sum*oldbase + (long) input[d]; + e += (sum > 0); + input[d++] = (int) (sum / newbase); + sum %= newbase; + } + /* accumulate remainder */ + result[--reslen] = (unsigned char)sum; + } + for (d=0; d < reslen; d++) + result[d] = 0; +} diff --git a/lib/nbsd_libc/compat/net/compat_ns_ntoa.c b/lib/nbsd_libc/compat/net/compat_ns_ntoa.c new file mode 100644 index 000000000..469b2e731 --- /dev/null +++ b/lib/nbsd_libc/compat/net/compat_ns_ntoa.c @@ -0,0 +1,111 @@ +/* $NetBSD: compat_ns_ntoa.c,v 1.1 2006/08/26 16:07:01 matt Exp $ */ + +/* + * Copyright (c) 1986, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_ns_ntoa.c,v 1.1 2006/08/26 16:07:01 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include +#include + +static char *spectHex __P((char *)); + +char * +ns_ntoa(addr) + struct ns_addr addr; +{ + static char obuf[40]; + union { union ns_net net_e; u_int32_t long_e; } net; + u_int16_t port = htons(addr.x_port); + char *cp; + char *cp2; + u_int8_t *up = addr.x_host.c_host; + u_int8_t *uplim = up + 6; + + net.net_e = addr.x_net; + sprintf(obuf, "%x", ntohl(net.long_e)); + cp = spectHex(obuf); + cp2 = cp + 1; + while (up < uplim && *up==0) + up++; + if (up == uplim) { + if (port) { + sprintf(cp, ".0"); + cp += 2; + } + } else { + sprintf(cp, ".%x", *up++); + while (up < uplim) { + while (*cp) cp++; + sprintf(cp, "%02x", *up++); + } + cp = spectHex(cp2); + } + if (port) { + sprintf(cp, ".%x", port); + spectHex(cp + 1); + } + return (obuf); +} + +static char * +spectHex(p0) + char *p0; +{ + int ok = 0; + int nonzero = 0; + char *p = p0; + + _DIAGASSERT(p0 != NULL); + + for (; *p; p++) + switch (*p) { + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + *p += ('A' - 'a'); + /* FALLTHROUGH */ + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + ok = 1; + /* FALLTHROUGH */ + case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '8': case '9': + nonzero = 1; + } + if (nonzero && !ok) { *p++ = 'H'; *p = 0; } + return (p); +} diff --git a/lib/nbsd_libc/compat/rpc/Makefile.inc b/lib/nbsd_libc/compat/rpc/Makefile.inc new file mode 100644 index 000000000..32de61fe2 --- /dev/null +++ b/lib/nbsd_libc/compat/rpc/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.2 2009/01/11 03:41:28 christos Exp $ + +.PATH: ${COMPATDIR}/rpc + +SRCS+=compat_pmap_rmtcall.c compat_rpcb.c diff --git a/lib/nbsd_libc/compat/rpc/compat_pmap_rmtcall.c b/lib/nbsd_libc/compat/rpc/compat_pmap_rmtcall.c new file mode 100644 index 000000000..fb82c7a7a --- /dev/null +++ b/lib/nbsd_libc/compat/rpc/compat_pmap_rmtcall.c @@ -0,0 +1,69 @@ +/* $NetBSD: compat_pmap_rmtcall.c,v 1.2 2009/01/11 03:41:28 christos Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_pmap_rmtcall.c,v 1.2 2009/01/11 03:41:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include + +__warn_references(pmap_rmtcall, + "warning: reference to compatibility pmap_rmtcall(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(pmap_rmtcall, _pmap_rmtcall) +#endif + +enum clnt_stat +pmap_rmtcall(struct sockaddr_in *addr, u_long prognum, u_long versnum, + u_long procnum, xdrproc_t inproc, char *in, xdrproc_t outproc, + char *out, struct timeval50 tout50, u_long *portp) +{ + struct timeval tout; + timeval50_to_timeval(&tout50, &tout); + return __pmap_rmtcall50(addr, prognum, versnum, procnum, inproc, in, + outproc, out, tout, portp); +} diff --git a/lib/nbsd_libc/compat/rpc/compat_rpcb.c b/lib/nbsd_libc/compat/rpc/compat_rpcb.c new file mode 100644 index 000000000..b547d5348 --- /dev/null +++ b/lib/nbsd_libc/compat/rpc/compat_rpcb.c @@ -0,0 +1,83 @@ +/* $NetBSD: compat_rpcb.c,v 1.2 2009/01/11 03:41:28 christos Exp $ */ + +/*- + * Copyright (c) 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_rpcb.c,v 1.2 2009/01/11 03:41:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include + +__warn_references(rpcb_rmtcall, + "warning: reference to compatibility rpcb_rmtcall(); include to generate correct reference") +__warn_references(rpcb_gettime, + "warning: reference to compatibility rpcb_gettime(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(rpcb_rmtcall, _rpcb_rmtcall) +__weak_alias(rpcb_gettime, _rpcb_gettime) +#endif + +enum clnt_stat +rpcb_rmtcall(const struct netconfig *nc, + const char *name, const rpcprog_t prog, const rpcvers_t vers, + const rpcproc_t proc, const xdrproc_t inproc, const char *inbuf, + const xdrproc_t outproc, caddr_t outbuf, + const struct timeval50 tout50, const struct netbuf *nb) +{ + struct timeval tout; + timeval50_to_timeval(&tout50, &tout); + return __rpcb_rmtcall50(nc, name, prog, vers, proc, inproc, inbuf, + outproc, outbuf, tout, nb); +} + +bool_t +rpcb_gettime(const char *name, int32_t *t50) +{ + time_t t; + bool_t rv = __rpcb_gettime50(name, &t); + *t50 = (int32_t)t; + return rv; +} diff --git a/lib/nbsd_libc/compat/shlib_version b/lib/nbsd_libc/compat/shlib_version new file mode 100644 index 000000000..5b1659f3c --- /dev/null +++ b/lib/nbsd_libc/compat/shlib_version @@ -0,0 +1,5 @@ +# $NetBSD: shlib_version,v 1.1 2006/03/11 21:07:18 christos Exp $ +# Remember to update distrib/sets/lists/base/shl.* when changing +# +major=0 +minor=0 diff --git a/lib/nbsd_libc/compat/stdlib/Makefile.inc b/lib/nbsd_libc/compat/stdlib/Makefile.inc new file mode 100644 index 000000000..c844080ce --- /dev/null +++ b/lib/nbsd_libc/compat/stdlib/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/03/11 21:07:18 christos Exp $ + +.PATH: ${COMPATDIR}/stdlib +CPPFLAGS+=-I${COMPATDIR}/stdlib -I${COMPATDIR}/../stdlib +SRCS+=compat_unsetenv.c diff --git a/lib/nbsd_libc/compat/stdlib/compat_unsetenv.c b/lib/nbsd_libc/compat/stdlib/compat_unsetenv.c new file mode 100644 index 000000000..3c89812c9 --- /dev/null +++ b/lib/nbsd_libc/compat/stdlib/compat_unsetenv.c @@ -0,0 +1,87 @@ +/* $NetBSD: compat_unsetenv.c,v 1.3 2010/11/14 18:11:42 tron 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "from: @(#)setenv.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: compat_unsetenv.c,v 1.3 2010/11/14 18:11:42 tron Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include "reentrant.h" +#include "local.h" + +#ifdef __weak_alias +__weak_alias(unsetenv,_unsetenv) +#endif + +#include "env.h" + +__warn_references(unsetenv, + "warning: reference to compatibility unsetenv();" + " include for correct reference") + +/* + * unsetenv(name) -- + * Delete environmental variable "name". + */ +void +unsetenv(const char *name) +{ + size_t l_name; + + _DIAGASSERT(name != NULL); + + l_name = strlen(name); + if (__writelockenv()) { + ssize_t offset; + + while ((offset = __getenvslot(name, l_name, false)) != -1) { + char **p; + for (p = &environ[offset]; ; ++p) { + if ((*p = *(p + 1)) == NULL) + break; + } + } + (void)__unlockenv(); + } +} diff --git a/lib/nbsd_libc/compat/sys/Lint_Ovfork.c b/lib/nbsd_libc/compat/sys/Lint_Ovfork.c new file mode 100644 index 000000000..33cd226ac --- /dev/null +++ b/lib/nbsd_libc/compat/sys/Lint_Ovfork.c @@ -0,0 +1,21 @@ +/* $NetBSD: Lint_Ovfork.c,v 1.1 2006/03/11 21:07:18 christos Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#if 0 +#include +#else +#include +#include +pid_t vfork __P((void)); +#endif + +/*ARGSUSED*/ +pid_t +vfork() +{ + return (0); +} diff --git a/lib/nbsd_libc/compat/sys/Makefile.inc b/lib/nbsd_libc/compat/sys/Makefile.inc new file mode 100644 index 000000000..25031df12 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/Makefile.inc @@ -0,0 +1,20 @@ +# $NetBSD: Makefile.inc,v 1.8 2009/01/11 02:46:25 christos Exp $ + +.PATH: ${COMPATDIR}/sys +SRCS+= compat_getdents.c compat_getdirentries.c compat_msync.c \ + compat_ntp_gettime.c compat_sched.c \ + compat_semctl.c compat_sigaltstack.c compat_stat.c compat___stat13.c \ + compat_statfs.c compat_socket.c compat_getfh.c \ + compat_fhopen.c compat___fhstat30.c compat_fhstatvfs.c compat_fhstatvfs1.c \ + compat_mount.c compat_getrusage.c compat_gettimeofday.c compat_utimes.c \ + compat_wait4.c compat_sigtimedwait.c compat_settimeofday.c compat_timer.c \ + compat_clock.c compat_itimer.c compat_select.c compat_nanosleep.c \ + compat___stat30.c compat_aio_suspend.c compat_adjtime.c compat_mknod.c \ + compat_mqueue.c compat__lwp_park.c compat_lfs_segwait.c compat_kevent.c \ + compat___ntp_gettime30.c compat___semctl13.c compat___shmctl13.c \ + compat___msgctl13.c +MAN+= getdirentries.3 + +.if ${MKLINT} != "no" +LSRCS+= Lint_Ovfork.c +.endif diff --git a/lib/nbsd_libc/compat/sys/compat___fhstat30.c b/lib/nbsd_libc/compat/sys/compat___fhstat30.c new file mode 100644 index 000000000..39068c937 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat___fhstat30.c @@ -0,0 +1,55 @@ +/* $NetBSD: compat___fhstat30.c,v 1.4 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Martin Husemann . + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat___fhstat30.c,v 1.4 2009/01/11 02:46:25 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include + +__warn_references(fhstat, + "warning: reference to compatibility __fhstat30(); include to generate correct reference") + +/* + * Convert old fhstat() call to new calling convention + */ +int +__fhstat30(const struct compat_30_fhandle *fhp, struct stat30 *sb) +{ + return __fhstat40(fhp, FHANDLE30_SIZE, sb); +} diff --git a/lib/nbsd_libc/compat/sys/compat___msgctl13.c b/lib/nbsd_libc/compat/sys/compat___msgctl13.c new file mode 100644 index 000000000..a69b4adc9 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat___msgctl13.c @@ -0,0 +1,68 @@ +/* $NetBSD: compat___msgctl13.c,v 1.2 2009/01/11 02:46:25 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat___msgctl13.c,v 1.2 2009/01/11 02:46:25 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +__warn_references(__msgctl13, + "warning: reference to compatibility __msgctl13(); include to generate correct reference") + +int +__msgctl13(int msqid, int cmd, struct msqid_ds13 *ds13) +{ + struct msqid_ds ds; + int error; + + if (cmd == IPC_SET) + __msqid_ds13_to_native(ds13, &ds); + + error = __msgctl50(msqid, cmd, &ds); + if (error) + return error; + if (cmd == IPC_STAT) + __native_to_msqid_ds13(&ds, ds13); + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat___ntp_gettime30.c b/lib/nbsd_libc/compat/sys/compat___ntp_gettime30.c new file mode 100644 index 000000000..8511ec401 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat___ntp_gettime30.c @@ -0,0 +1,30 @@ +/* $NetBSD: compat___ntp_gettime30.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat___ntp_gettime30.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include + +__warn_references(__ntp_gettime30, + "warning: reference to compatibility __ntp_gettime30(); include for correct reference") + +int +__ntp_gettime30(struct ntptimeval50 *ontvp) +{ + struct ntptimeval ntv; + int res; + + res = __ntp_gettime50(&ntv); + ontvp->time.tv_sec = (int32_t)ntv.time.tv_sec; + ontvp->time.tv_nsec = ntv.time.tv_nsec; + ontvp->maxerror = ntv.maxerror; + ontvp->esterror = ntv.esterror; + + return (res); +} diff --git a/lib/nbsd_libc/compat/sys/compat___semctl13.c b/lib/nbsd_libc/compat/sys/compat___semctl13.c new file mode 100644 index 000000000..94ca2000c --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat___semctl13.c @@ -0,0 +1,98 @@ +/* $NetBSD: compat___semctl13.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/* $NetBSD: compat___semctl13.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat___semctl13.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include +#include + +__warn_references(__semctl13, + "warning: reference to compatibility __semctl13(); include to generate correct reference") + +/* + * Copy timeout to local variable and call the syscall. + */ +int +__semctl13(int semid, int semnum, int cmd, ...) +{ + va_list ap; + union __semun semun; + struct semid_ds13 *ds13; + struct semid_ds ds; + int error; + + va_start(ap, cmd); + switch (cmd) { + case IPC_SET: + case IPC_STAT: + case GETALL: + case SETVAL: + case SETALL: +#ifdef __lint__ + memcpy(&semun, &ap, sizeof(semun)); +#else + semun = va_arg(ap, union __semun); +#endif + } + va_end(ap); + + ds13 = (struct semid_ds13 *)(void *)semun.buf; + semun.buf = &ds; + + if (cmd == IPC_SET) + __semid_ds13_to_native(ds13, &ds); + + error = ____semctl50(semid, semnum, cmd, &semun); + if (error) + return error; + + if (cmd == IPC_STAT) + __native_to_semid_ds13(&ds, ds13); + + semun.buf = (struct semid_ds *)(void *)ds13; + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat___shmctl13.c b/lib/nbsd_libc/compat/sys/compat___shmctl13.c new file mode 100644 index 000000000..00c399917 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat___shmctl13.c @@ -0,0 +1,68 @@ +/* $NetBSD: compat___shmctl13.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat___shmctl13.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +__warn_references(__shmctl13, + "warning: reference to compatibility __shmctl13(); include to generate correct reference") + +int +__shmctl13(int shmid, int cmd, struct shmid_ds13 *ds13) +{ + struct shmid_ds ds; + int error; + + if (cmd == IPC_SET) + __shmid_ds13_to_native(ds13, &ds); + + error = __shmctl50(shmid, cmd, &ds); + if (error) + return error; + if (cmd == IPC_STAT) + __native_to_shmid_ds13(&ds, ds13); + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat___stat13.c b/lib/nbsd_libc/compat/sys/compat___stat13.c new file mode 100644 index 000000000..e01e07ddf --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat___stat13.c @@ -0,0 +1,140 @@ +/* $NetBSD: compat___stat13.c,v 1.4 2009/01/11 02:46:26 christos Exp $ */ + +/* + * Copyright (c) 1997 Frank van der Linden + * 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 + * by Frank van der Linden + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat___stat13.c,v 1.4 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include +#include +#include +#include + +__warn_references(__stat13, + "warning: reference to compatibility __stat13(); include to generate correct reference") + +__warn_references(__fstat13, + "warning: reference to compatibility __fstat13(); include to generate correct reference") + +__warn_references(__lstat13, + "warning: reference to compatibility __lstat13(); include to generate correct reference") + +__warn_references(fhstat, + "warning: reference to compatibility fhstat(); include to generate correct reference") + + +/* + * Convert from a new to an old stat structure. + */ + +static void cvtstat(struct stat13 *, const struct stat *); + +static void +cvtstat(struct stat13 *ost, const struct stat *st) +{ + + ost->st_dev = (uint32_t)st->st_dev; + ost->st_ino = (uint32_t)st->st_ino; + ost->st_mode = st->st_mode; + ost->st_nlink = st->st_nlink; + ost->st_uid = st->st_uid; + ost->st_gid = st->st_gid; + ost->st_rdev = (uint32_t)st->st_rdev; + ost->st_atimespec.tv_sec = (int32_t)st->st_atimespec.tv_sec; + ost->st_atimespec.tv_nsec = st->st_atimespec.tv_nsec; + ost->st_mtimespec.tv_sec = (int32_t)st->st_mtimespec.tv_sec; + ost->st_mtimespec.tv_nsec = st->st_mtimespec.tv_nsec; + ost->st_ctimespec.tv_sec = (int32_t)st->st_ctimespec.tv_sec; + ost->st_ctimespec.tv_nsec = st->st_ctimespec.tv_nsec; + ost->st_birthtimespec.tv_sec = (int32_t)st->st_birthtimespec.tv_sec; + ost->st_birthtimespec.tv_nsec = st->st_birthtimespec.tv_nsec; + ost->st_size = st->st_size; + ost->st_blocks = st->st_blocks; + ost->st_blksize = st->st_blksize; + ost->st_flags = st->st_flags; + ost->st_gen = st->st_gen; +} + +int +__stat13(const char *file, struct stat13 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __stat50(file, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} + +int +__fstat13(int f, struct stat13 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __fstat50(f, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} + +int +__lstat13(const char *file, struct stat13 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __lstat50(file, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} + +int +fhstat(const struct compat_30_fhandle *fh, struct stat13 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __fhstat50((const void *)fh, /* FHANDLE_SIZE_COMPAT */28, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} diff --git a/lib/nbsd_libc/compat/sys/compat___stat30.c b/lib/nbsd_libc/compat/sys/compat___stat30.c new file mode 100644 index 000000000..ec6c8aa44 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat___stat30.c @@ -0,0 +1,136 @@ +/* $NetBSD: compat___stat30.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/* + * Copyright (c) 1997 Frank van der Linden + * 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 + * by Frank van der Linden + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat___stat30.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include +#include +#include +#include + +__warn_references(__stat30, + "warning: reference to compatibility __stat30(); include to generate correct reference") + +__warn_references(__fstat30, + "warning: reference to compatibility __fstat30(); include to generate correct reference") + +__warn_references(__lstat30, + "warning: reference to compatibility __lstat30(); include to generate correct reference") + +__warn_references(__fhstat40, + "warning: reference to compatibility __fhstat40(); include to generate correct reference") + + +/* + * Convert from a new to an old stat structure. + */ + +static void cvtstat(struct stat30 *, const struct stat *); + +static void +cvtstat(struct stat30 *ost, const struct stat *st) +{ + + ost->st_dev = (uint32_t)st->st_dev; + ost->st_ino = st->st_ino; + ost->st_mode = st->st_mode; + ost->st_nlink = st->st_nlink; + ost->st_uid = st->st_uid; + ost->st_gid = st->st_gid; + ost->st_rdev = (uint32_t)st->st_rdev; + timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec); + timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec); + timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec); + timespec_to_timespec50(&st->st_birthtimespec, &ost->st_birthtimespec); + ost->st_size = st->st_size; + ost->st_blocks = st->st_blocks; + ost->st_blksize = st->st_blksize; + ost->st_flags = st->st_flags; + ost->st_gen = st->st_gen; +} + +int +__stat30(const char *file, struct stat30 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __stat50(file, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} + +int +__fstat30(int f, struct stat30 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __fstat50(f, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} + +int +__lstat30(const char *file, struct stat30 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __lstat50(file, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} + +int +__fhstat40(const void *fh, size_t fh_size, struct stat30 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __fhstat50(fh, fh_size, &nst)) == -1) + return ret; + cvtstat(ost, &nst); + return ret; +} diff --git a/lib/nbsd_libc/compat/sys/compat__lwp_park.c b/lib/nbsd_libc/compat/sys/compat__lwp_park.c new file mode 100644 index 000000000..f4f9b1902 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat__lwp_park.c @@ -0,0 +1,67 @@ +/* $NetBSD: compat__lwp_park.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat__lwp_park.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +__warn_references(_lwp_park, + "warning: reference to compatibility _lwp_park(); include to generate correct reference") + +/* + * Copy timeout to local variable and call the syscall. + */ +int +_lwp_park(const struct timespec50 *ts50, lwpid_t unpark, + const void *hint, const void *unparkhint) +{ + struct timespec ts, *tsp; + + if (ts50) + timespec50_to_timespec(ts50, tsp = &ts); + else + tsp = NULL; + return ___lwp_park50(tsp, unpark, hint, unparkhint); +} diff --git a/lib/nbsd_libc/compat/sys/compat_adjtime.c b/lib/nbsd_libc/compat/sys/compat_adjtime.c new file mode 100644 index 000000000..b6a242f53 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_adjtime.c @@ -0,0 +1,77 @@ +/* $NetBSD: compat_adjtime.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without valific prior written permission. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_adjtime.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include + +__warn_references(adjtime, + "warning: reference to compatibility adjtime(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(adjtime, _adjtime) +__weak_alias(__adjtime, _adjtime) +#endif + +/* + * Copy timeout to local variable and call the syscall. + */ +int +adjtime(const struct timeval50 *ts50, struct timeval50 *rts50) +{ + struct timeval ts, *tsp; + struct timeval rts, *rtsp; + int error; + + rtsp = rts50 ? &rts : NULL; + if (ts50) + timeval50_to_timeval(ts50, tsp = &ts); + else + tsp = NULL; + error = __adjtime50(tsp, rtsp); + if (error) + return error; + if (rts50) + timeval_to_timeval50(rtsp, rts50); + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_aio_suspend.c b/lib/nbsd_libc/compat/sys/compat_aio_suspend.c new file mode 100644 index 000000000..3639f5ba9 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_aio_suspend.c @@ -0,0 +1,70 @@ +/* $NetBSD: compat_aio_suspend.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_aio_suspend.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include + +__warn_references(aio_suspend, + "warning: reference to compatibility aio_suspend(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(aio_suspend, _aio_suspend) +__weak_alias(_sys_aio_suspend, _aio_suspend) +#endif +/* + * Copy timeout to local variable and call the syscall. + */ +int +aio_suspend(const struct aiocb * const list[], int nent, + const struct timespec50 * ts50) +{ + struct timespec ts, *tsp; + + if (ts50) + timespec50_to_timespec(ts50, tsp = &ts); + else + tsp = NULL; + return __aio_suspend50(list, nent, tsp); +} diff --git a/lib/nbsd_libc/compat/sys/compat_clock.c b/lib/nbsd_libc/compat/sys/compat_clock.c new file mode 100644 index 000000000..c3f56451a --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_clock.c @@ -0,0 +1,98 @@ +/* $NetBSD: compat_clock.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_clock.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include + +__warn_references(clock_gettime, + "warning: reference to compatibility clock_gettime(); include to generate correct reference") +__warn_references(clock_settime, + "warning: reference to compatibility clock_settime(); include to generate correct reference") +__warn_references(clock_getres, + "warning: reference to compatibility clock_getres(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(clock_settime, _clock_settime) +__weak_alias(__clock_settime, _clock_settime) +__weak_alias(clock_gettime, _clock_gettime) +__weak_alias(clock_getres, _clock_getres) +#endif + +int +clock_settime(clockid_t clockid, const struct timespec50 * tim50) +{ + struct timespec tim, *timp; + int error; + + if (tim50) + timespec50_to_timespec(tim50, timp = &tim); + else + timp = NULL; + error = __clock_settime50(clockid, timp); + if (error) + return error; + return 0; +} + +int +clock_gettime(clockid_t clockid, struct timespec50 *tim50) +{ + struct timespec tim, *timp = tim50 ? &tim : NULL; + int error = __clock_gettime50(clockid, timp); + if (error) + return error; + if (tim50) + timespec_to_timespec50(timp, tim50); + return 0; +} + +int +clock_getres(clockid_t clockid, struct timespec50 *tim50) +{ + struct timespec tim, *timp = tim50 ? &tim : NULL; + int error = __clock_getres50(clockid, timp); + if (error) + return error; + if (tim50) + timespec_to_timespec50(timp, tim50); + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_fhopen.c b/lib/nbsd_libc/compat/sys/compat_fhopen.c new file mode 100644 index 000000000..438231655 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_fhopen.c @@ -0,0 +1,58 @@ +/* $NetBSD: compat_fhopen.c,v 1.4 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Martin Husemann . + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_fhopen.c,v 1.4 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include + +__warn_references(fhopen, + "warning: reference to compatibility fhopen(); include to generate correct reference") + + +int __fhopen40(const void *, size_t, int); +int fhopen(const struct compat_30_fhandle *, int); + +/* + * Convert old fhopen() call to new calling convention + */ +int +fhopen(const struct compat_30_fhandle *fhp, int flags) +{ + return __fhopen40(fhp, FHANDLE30_SIZE, flags); +} diff --git a/lib/nbsd_libc/compat/sys/compat_fhstatvfs.c b/lib/nbsd_libc/compat/sys/compat_fhstatvfs.c new file mode 100644 index 000000000..1e2207cfc --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_fhstatvfs.c @@ -0,0 +1,62 @@ +/* $NetBSD: compat_fhstatvfs.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Martin Husemann . + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_fhstatvfs.c,v 1.3 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include + +__warn_references(fhstatvfs, + "warning: reference to compatibility fhstatvfs(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(fhstatvfs, _fhstatvfs) +#endif + +int _fhstatvfs(const struct compat_30_fhandle *fhp, struct statvfs *buf); +int __fhstatvfs140(const void *fhp, size_t fh_size, struct statvfs *buf, + int flags); + +/* + * Convert old fhstatvs() call to new calling convention + */ +int +_fhstatvfs(const struct compat_30_fhandle *fhp, struct statvfs *buf) +{ + return __fhstatvfs140(fhp, FHANDLE30_SIZE, buf, ST_WAIT); +} diff --git a/lib/nbsd_libc/compat/sys/compat_fhstatvfs1.c b/lib/nbsd_libc/compat/sys/compat_fhstatvfs1.c new file mode 100644 index 000000000..6eb7db3b0 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_fhstatvfs1.c @@ -0,0 +1,59 @@ +/* $NetBSD: compat_fhstatvfs1.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Martin Husemann . + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_fhstatvfs1.c,v 1.3 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include + +__warn_references(fhstatvfs1, + "warning: reference to compatibility fhstatvfs1(); include to generate correct reference") + +int fhstatvfs1(const struct compat_30_fhandle *fhp, struct statvfs *buf, + int flags); +int __fhstatvfs140(const void *fhp, size_t fh_size, struct statvfs *buf, + int flags); + +/* + * Convert old fhstatvs1() call to new calling convention + */ +int +fhstatvfs1(const struct compat_30_fhandle *fhp, struct statvfs *buf, int flags) +{ + return __fhstatvfs140(fhp, FHANDLE30_SIZE, buf, flags); +} diff --git a/lib/nbsd_libc/compat/sys/compat_getdents.c b/lib/nbsd_libc/compat/sys/compat_getdents.c new file mode 100644 index 000000000..20a70e498 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_getdents.c @@ -0,0 +1,82 @@ +/* $NetBSD: compat_getdents.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2005 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_getdents.c,v 1.3 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include + +/* + * libc12 compatible getdents routine. + */ +int +getdents(int fd, char *buf, size_t nbytes) +{ + struct dirent *ndp, *nndp, *endp; + struct dirent12 *odp; + int rv; + + if ((rv = __getdents30(fd, buf, nbytes)) == -1) + return rv; + + odp = (struct dirent12 *)(void *)buf; + ndp = (struct dirent *)(void *)buf; + endp = (struct dirent *)(void *)&buf[rv]; + + /* + * In-place conversion. This works because odp + * is smaller than ndp, but it has to be done + * in the right sequence. + */ + for (; ndp < endp; ndp = nndp) { + nndp = _DIRENT_NEXT(ndp); + /* XXX: avoid unaligned 64-bit access on sparc64 */ + odp->d_ino = ((u_int32_t *)(void *)&ndp->d_ino)[_QUAD_LOWWORD]; + if (ndp->d_namlen >= sizeof(odp->d_name)) + odp->d_namlen = sizeof(odp->d_name) - 1; + else + odp->d_namlen = (u_int8_t)ndp->d_namlen; + odp->d_type = ndp->d_type; + (void)memcpy(odp->d_name, ndp->d_name, (size_t)odp->d_namlen); + odp->d_name[odp->d_namlen] = '\0'; + odp->d_reclen = _DIRENT_SIZE(odp); + odp = _DIRENT_NEXT(odp); + } + return ((char *)(void *)odp) - buf; +} diff --git a/lib/nbsd_libc/compat/sys/compat_getdirentries.c b/lib/nbsd_libc/compat/sys/compat_getdirentries.c new file mode 100644 index 000000000..5942cc778 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_getdirentries.c @@ -0,0 +1,58 @@ +/* $NetBSD: compat_getdirentries.c,v 1.2 2009/02/22 06:33:38 dholland Exp $ */ + +/* + * Copyright (c) 1997 Frank van der Linden + * 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 by Frank van der Linden for + * the NetBSD Project. + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_getdirentries.c,v 1.2 2009/02/22 06:33:38 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ +#include "namespace.h" +#include +#include +#include +#include + +__warn_references(getdirentries, + "reference to compatibility-only getdirentries(); this will break; use getdents() or readdir() instead") + +int +getdirentries(fd, buf, nbytes, basep) + int fd, nbytes; + char *buf; + long *basep; +{ + *basep = (long)lseek(fd, (off_t)0, SEEK_CUR); + return getdents(fd, buf, (size_t)nbytes); +} diff --git a/lib/nbsd_libc/compat/sys/compat_getfh.c b/lib/nbsd_libc/compat/sys/compat_getfh.c new file mode 100644 index 000000000..5c7695004 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_getfh.c @@ -0,0 +1,71 @@ +/* $NetBSD: compat_getfh.c,v 1.6 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Martin Husemann . + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_getfh.c,v 1.6 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include + +#include + +__warn_references(getfh, + "warning: reference to compatibility getfh(); include to generate correct reference") + + +int __getfh30(const char *, void*, size_t *); +int getfh(const char *path, struct compat_30_fhandle *fhp); + +/* + * Convert old getfh() call to new calling convention + */ +int +getfh(const char *path, struct compat_30_fhandle *fhp) +{ + size_t fh_size = sizeof(*fhp); + int ret; + + ret = __getfh30(path, (void*)fhp, &fh_size); + if (ret != 0) { + return ret; + } + if (fh_size != FHANDLE30_SIZE) { + errno = EINVAL; + return -1; + } + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_getrusage.c b/lib/nbsd_libc/compat/sys/compat_getrusage.c new file mode 100644 index 000000000..a00f0ff13 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_getrusage.c @@ -0,0 +1,74 @@ +/* $NetBSD: compat_getrusage.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_getrusage.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include + +__warn_references(getrusage, + "warning: reference to compatibility getrusage(); include to generate correct reference") + +void __rusage_to_rusage50(const struct rusage *, struct rusage50 *); + +void +__rusage_to_rusage50(const struct rusage *ru, struct rusage50 *ru50) +{ + (void)memcpy(&ru50->ru_first, &ru->ru_first, + (size_t)((char *)(void *)&ru50->ru_last - + (char *)(void *)&ru50->ru_first)); + ru50->ru_maxrss = ru->ru_maxrss; + timeval_to_timeval50(&ru->ru_utime, &ru50->ru_utime); + timeval_to_timeval50(&ru->ru_stime, &ru50->ru_stime); +} + +/* + * libc12 compatible getrusage routine. + */ +int +getrusage(int who, struct rusage50 *ru50) +{ + struct rusage ru; + int rv; + + if ((rv = __getrusage50(who, &ru)) == -1) + return rv; + __rusage_to_rusage50(&ru, ru50); + return rv; +} diff --git a/lib/nbsd_libc/compat/sys/compat_gettimeofday.c b/lib/nbsd_libc/compat/sys/compat_gettimeofday.c new file mode 100644 index 000000000..694cfbd2b --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_gettimeofday.c @@ -0,0 +1,60 @@ +/* $NetBSD: compat_gettimeofday.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_gettimeofday.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include + +__warn_references(gettimeofday, + "warning: reference to compatibility gettimeofday(); include to generate correct reference") + +/* + * libc12 compatible gettimeofday routine. + */ +int +gettimeofday(struct timeval50 *tv50, void *tzp) +{ + struct timeval tv; + int rv; + + if ((rv = __gettimeofday50(&tv, tzp)) == -1) + return rv; + timeval_to_timeval50(&tv, tv50); + return rv; +} diff --git a/lib/nbsd_libc/compat/sys/compat_itimer.c b/lib/nbsd_libc/compat/sys/compat_itimer.c new file mode 100644 index 000000000..e31dec76a --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_itimer.c @@ -0,0 +1,82 @@ +/* $NetBSD: compat_itimer.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_itimer.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include + +__warn_references(getitimer, + "warning: reference to compatibility getitimer(); include to generate correct reference") +__warn_references(setitimer, + "warning: reference to compatibility setitimer(); include to generate correct reference") + +int +setitimer(int timerid, const struct itimerval50 * __restrict tim50, + struct itimerval50 * __restrict otim50) +{ + struct itimerval tim, *timp; + struct itimerval otim, *otimp; + int error; + + otimp = otim50 ? &otim : NULL; + if (tim50) + itimerval50_to_itimerval(tim50, timp = &tim); + else + timp = NULL; + error = __setitimer50(timerid, timp, otimp); + if (error) + return error; + if (otim50) + itimerval_to_itimerval50(otimp, otim50); + return 0; +} + +int +getitimer(int timerid, struct itimerval50 *tim50) +{ + struct itimerval tim, *timp = tim50 ? &tim : NULL; + int error = __getitimer50(timerid, timp); + if (error) + return error; + if (tim50) + itimerval_to_itimerval50(timp, tim50); + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_kevent.c b/lib/nbsd_libc/compat/sys/compat_kevent.c new file mode 100644 index 000000000..c613f0735 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_kevent.c @@ -0,0 +1,64 @@ +/* $NetBSD: compat_kevent.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_kevent.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +__warn_references(kevent, + "warning: reference to compatibility kevent(); include to generate correct reference") + +int +kevent(int kq, const struct kevent *changelist, size_t nchanges, + struct kevent *eventlist, size_t nevents, const struct timespec50 *ts50) +{ + struct timespec ts, *tsp; + + if (ts50) + timespec50_to_timespec(ts50, tsp = &ts); + else + tsp = NULL; + return __kevent50(kq, changelist, nchanges, eventlist, nevents, tsp); +} diff --git a/lib/nbsd_libc/compat/sys/compat_lfs_segwait.c b/lib/nbsd_libc/compat/sys/compat_lfs_segwait.c new file mode 100644 index 000000000..5a4cead1c --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_lfs_segwait.c @@ -0,0 +1,65 @@ +/* $NetBSD: compat_lfs_segwait.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without valific prior written permission. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_lfs_segwait.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +__warn_references(lfs_segwait, + "warning: reference to compatibility lfs_segwait(); include to generate correct reference") + +int lfs_segwait(fsid_t *, struct timeval50 *); +int __lfs_segwait50(fsid_t *, struct timeval *); +int +lfs_segwait(fsid_t *fsid, struct timeval50 *ts50) +{ + struct timeval ts, *tsp; + + if (ts50) + timeval50_to_timeval(ts50, tsp = &ts); + else + tsp = NULL; + return __lfs_segwait50(fsid, tsp); +} diff --git a/lib/nbsd_libc/compat/sys/compat_mknod.c b/lib/nbsd_libc/compat/sys/compat_mknod.c new file mode 100644 index 000000000..eea52f496 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_mknod.c @@ -0,0 +1,57 @@ +/* $NetBSD: compat_mknod.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_mknod.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +__warn_references(mknod, + "warning: reference to compatibility mknod(); include to generate correct reference") + +int +mknod(const char *path, mode_t mode, uint32_t dev) +{ + return __mknod50(path, mode, dev); +} diff --git a/lib/nbsd_libc/compat/sys/compat_mount.c b/lib/nbsd_libc/compat/sys/compat_mount.c new file mode 100644 index 000000000..83f8f3c3f --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_mount.c @@ -0,0 +1,29 @@ +/* $NetBSD: compat_mount.c,v 1.2 2007/07/18 20:10:47 dsl Exp $ */ + + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_mount.c,v 1.2 2007/07/18 20:10:47 dsl Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include + +__warn_references(mount, + "warning: reference to compatibility mount(); include to generate correct reference") + +int mount(const char *, const char *, int, void *); +int __mount50(const char *, const char *, int, void *, size_t); + +/* + * Convert old mount() call to new calling convention + * The kernel will treat length 0 as 'default for the fs'. + * We need to throw away the +ve response for MNT_GETARGS. + */ +int +mount(const char *type, const char *dir, int flags, void *data) +{ + return __mount50(type, dir, flags, data, 0) == -1 ? -1 : 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_mqueue.c b/lib/nbsd_libc/compat/sys/compat_mqueue.c new file mode 100644 index 000000000..41244f81a --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_mqueue.c @@ -0,0 +1,86 @@ +/* $NetBSD: compat_mqueue.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_mqueue.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +__warn_references(mq_timedreceive, + "warning: reference to compatibility mq_timedreceive(); include to generate correct reference") +__warn_references(mq_timedsend, + "warning: reference to compatibility mq_timedsend(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(mq_timedreceive, _mq_timedreceive) +__weak_alias(mq_timedsend, _mq_timedsend) +__weak_alias(_sys_mq_timedreceive, _mq_timedreceive) +__weak_alias(_sys_mq_timedsend, _mq_timedsend) +#endif + +ssize_t +mq_timedreceive(mqd_t mq, char * __restrict name, size_t len, + unsigned * __restrict buf, const struct timespec50 * __restrict ts50) +{ + struct timespec ts, *tsp; + + if (ts50) + timespec50_to_timespec(ts50, tsp = &ts); + else + tsp = NULL; + return __mq_timedreceive50(mq, name, len, buf, tsp); +} + +int +mq_timedsend(mqd_t mq, const char *name, size_t len, + unsigned buf, const struct timespec50 *ts50) +{ + struct timespec ts, *tsp; + + if (ts50) + timespec50_to_timespec(ts50, tsp = &ts); + else + tsp = NULL; + return __mq_timedsend50(mq, name, len, buf, tsp); +} diff --git a/lib/nbsd_libc/compat/sys/compat_msync.c b/lib/nbsd_libc/compat/sys/compat_msync.c new file mode 100644 index 000000000..3e05ff64b --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_msync.c @@ -0,0 +1,54 @@ +/* $NetBSD: compat_msync.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Copyright (c) 1997 Frank van der Linden + * 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 + * by Frank van der Linden + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_msync.c,v 1.1 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include + +__warn_references(msync, + "warning: reference to compatibility msync(); include for correct reference") + +int +msync(addr, size) + void *addr; + size_t size; +{ + return __msync13(addr, size, MS_SYNC|MS_INVALIDATE); +} diff --git a/lib/nbsd_libc/compat/sys/compat_nanosleep.c b/lib/nbsd_libc/compat/sys/compat_nanosleep.c new file mode 100644 index 000000000..5d76722e0 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_nanosleep.c @@ -0,0 +1,77 @@ +/* $NetBSD: compat_nanosleep.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_nanosleep.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include + +__warn_references(nanosleep, + "warning: reference to compatibility nanosleep(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(nanosleep, _nanosleep) +__weak_alias(_sys_nanosleep, _nanosleep) +#endif + +/* + * Copy timeout to local variable and call the syscall. + */ +int +nanosleep(const struct timespec50 *ts50, struct timespec50 *rts50) +{ + struct timespec ts, *tsp; + struct timespec rts, *rtsp; + int error; + + rtsp = rts50 ? &rts : NULL; + if (ts50) + timespec50_to_timespec(ts50, tsp = &ts); + else + tsp = NULL; + error = __nanosleep50(tsp, rtsp); + if (error) + return error; + if (rts50) + timespec_to_timespec50(rtsp, rts50); + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_ntp_gettime.c b/lib/nbsd_libc/compat/sys/compat_ntp_gettime.c new file mode 100644 index 000000000..6fbbbbcbe --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_ntp_gettime.c @@ -0,0 +1,30 @@ +/* $NetBSD: compat_ntp_gettime.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_ntp_gettime.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include + +__warn_references(ntp_gettime, + "warning: reference to compatibility ntp_gettime(); include for correct reference") + +int +ntp_gettime(struct ntptimeval30 *ontvp) +{ + struct ntptimeval ntv; + int res; + + res = __ntp_gettime50(&ntv); + ontvp->time.tv_sec = (int32_t)ntv.time.tv_sec; + ontvp->time.tv_usec = ntv.time.tv_nsec / 1000; + ontvp->maxerror = ntv.maxerror; + ontvp->esterror = ntv.esterror; + + return (res); +} diff --git a/lib/nbsd_libc/compat/sys/compat_sched.c b/lib/nbsd_libc/compat/sys/compat_sched.c new file mode 100644 index 000000000..ceeb099fb --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_sched.c @@ -0,0 +1,57 @@ +/* $NetBSD: compat_sched.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_sched.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include + +__warn_references(sched_rr_get_interval, + "warning: reference to compatibility sched_rr_get_interval(); include to generate correct reference") + +int sched_rr_get_interval(pid_t, struct timespec50 *); + +int +/*ARGSUSED*/ +sched_rr_get_interval(pid_t pid, struct timespec50 *interval) +{ + interval->tv_sec = 0; + interval->tv_nsec = sysconf(_SC_SCHED_RT_TS) * 1000; + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_select.c b/lib/nbsd_libc/compat/sys/compat_select.c new file mode 100644 index 000000000..c81de97a9 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_select.c @@ -0,0 +1,102 @@ +/* $NetBSD: compat_select.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_select.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +__warn_references(pollts, + "warning: reference to compatibility pollts(); include to generate correct reference") +__warn_references(select, + "warning: reference to compatibility select(); include to generate correct reference") +__warn_references(pselect, + "warning: reference to compatibility pselect(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(select, _select) +__weak_alias(_sys_select, _select) +__weak_alias(pselect, _pselect) +__weak_alias(_sys_pselect, _pselect) +__weak_alias(pollts, _pollts) +__weak_alias(_sys_pollts, _pollts) +#endif +int +pollts(struct pollfd * __restrict fds, nfds_t nfds, + const struct timespec50 * __restrict tim50, + const sigset_t * __restrict sigmask) +{ + struct timespec tim, *timp; + + if (tim50) + timespec50_to_timespec(tim50, timp = &tim); + else + timp = NULL; + return __pollts50(fds, nfds, timp, sigmask); +} + +int +select(int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds, + fd_set * __restrict exceptfds, struct timeval50 * __restrict tim50) +{ + struct timeval tim, *timp; + + if (tim50) + timeval50_to_timeval(tim50, timp = &tim); + else + timp = NULL; + return __select50(nfds, readfds, writefds, exceptfds, timp); +} + +int +pselect(int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds, + fd_set * __restrict exceptfds, const struct timespec50 *__restrict tim50, + const sigset_t * __restrict sigmask) +{ + struct timespec tim, *timp; + + if (tim50) + timespec50_to_timespec(tim50, timp = &tim); + else + timp = NULL; + return __pselect50(nfds, readfds, writefds, exceptfds, timp, sigmask); +} diff --git a/lib/nbsd_libc/compat/sys/compat_semctl.c b/lib/nbsd_libc/compat/sys/compat_semctl.c new file mode 100644 index 000000000..8e5f05aab --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_semctl.c @@ -0,0 +1,94 @@ +/* $NetBSD: compat_semctl.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/* + * Copyright (c) 1994, 1995 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_semctl.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ +#include +#include +#include +#include +#include +#include + +int +semctl(int semid, int semnum, int cmd, ...) +{ + va_list ap; + union __semun semun; + struct semid_ds ds; + struct semid_ds14 *ds14 = NULL; + int error; + + va_start(ap, cmd); + switch (cmd) { + case IPC_SET: + case IPC_STAT: + case GETALL: + case SETVAL: + case SETALL: +#ifdef __lint__ + memcpy(&semun, &ap, sizeof(semun)); +#else + semun = va_arg(ap, union __semun); +#endif + } + va_end(ap); + + switch (cmd) { + case IPC_SET: + case IPC_STAT: + ds14 = (struct semid_ds14 *)(void *)semun.buf; + if (cmd == IPC_SET) + __semid_ds14_to_native(ds14, &ds); + semun.buf = &ds; + break; + } + + error = __semctl50(semid, semnum, cmd, &semun); + switch (cmd) { + case IPC_SET: + case IPC_STAT: + semun.buf = (struct semid_ds *)(void *)ds14; + if (cmd == IPC_STAT && error == 0) + __native_to_semid_ds14(&ds, ds14); + break; + } + return error; +} diff --git a/lib/nbsd_libc/compat/sys/compat_settimeofday.c b/lib/nbsd_libc/compat/sys/compat_settimeofday.c new file mode 100644 index 000000000..cafbc3fe6 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_settimeofday.c @@ -0,0 +1,65 @@ +/* $NetBSD: compat_settimeofday.c,v 1.2 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_settimeofday.c,v 1.2 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include + +__warn_references(settimeofday, + "warning: reference to compatibility settimeofday(); include to generate correct reference") + +#ifdef __weak_alias +__weak_alias(settimeofday, _settimeofday) +__weak_alias(__settimeofday, _settimeofday) +#endif +/* + * Copy timeout to local variable and call the syscall. + */ +int +settimeofday(const struct timeval50 * __restrict tv50, + const void * __restrict tvp) +{ + struct timeval tv; + timeval50_to_timeval(tv50, &tv); + return __settimeofday50(&tv, tvp); +} diff --git a/lib/nbsd_libc/compat/sys/compat_sigaltstack.c b/lib/nbsd_libc/compat/sys/compat_sigaltstack.c new file mode 100644 index 000000000..909e84cfe --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_sigaltstack.c @@ -0,0 +1,70 @@ +/* $NetBSD: compat_sigaltstack.c,v 1.3 2009/01/11 02:46:26 christos Exp $ */ + +/*- + * Copyright (c) 1997 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_sigaltstack.c,v 1.3 2009/01/11 02:46:26 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include +#include +#include + +int +sigaltstack(onss, ooss) + const struct sigaltstack13 *onss; + struct sigaltstack13 *ooss; +{ + stack_t nss, oss; + int error; + + nss.ss_sp = onss->ss_sp; + nss.ss_size = onss->ss_size; + nss.ss_flags = onss->ss_flags; + + error = __sigaltstack14(&nss, &oss); + + if (error == 0 && ooss != NULL) { + ooss->ss_sp = oss.ss_sp; + if (oss.ss_size > INT_MAX) + ooss->ss_size = INT_MAX; + else + ooss->ss_size = oss.ss_size; + ooss->ss_flags = oss.ss_flags; + } + + return (error); +} diff --git a/lib/nbsd_libc/compat/sys/compat_sigtimedwait.c b/lib/nbsd_libc/compat/sys/compat_sigtimedwait.c new file mode 100644 index 000000000..1d7349536 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_sigtimedwait.c @@ -0,0 +1,74 @@ +/* $NetBSD: compat_sigtimedwait.c,v 1.2 2009/01/11 02:46:27 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_sigtimedwait.c,v 1.2 2009/01/11 02:46:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(sigtimedwait, _sigtimedwait) +#endif +/* + * Copy timeout to local variable and call the syscall. + */ +int +sigtimedwait(const sigset_t * __restrict set, siginfo_t * __restrict info, + const struct timespec50 * __restrict timeout) +{ + struct timespec ts; + + if (timeout) { + timespec50_to_timespec(timeout, &ts); + return ____sigtimedwait50(set, info, &ts); + } else + return ____sigtimedwait50(set, info, NULL); +} + +int +__sigtimedwait(const sigset_t * __restrict set, siginfo_t * __restrict info, + struct timespec50 * __restrict timeout) +{ + return sigtimedwait(set, info, timeout); +} diff --git a/lib/nbsd_libc/compat/sys/compat_socket.c b/lib/nbsd_libc/compat/sys/compat_socket.c new file mode 100644 index 000000000..31a6dc7a5 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_socket.c @@ -0,0 +1,27 @@ +/* $NetBSD: compat_socket.c,v 1.1 2006/06/26 21:23:56 mrg Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_socket.c,v 1.1 2006/06/26 21:23:56 mrg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include + +__warn_references(socket, + "warning: reference to compatibility socket(); include for correct reference") + +int +socket(int domain, int type, int protocol) +{ + int res; + + res = __socket30(domain, type, protocol); + if (errno == EAFNOSUPPORT) + errno = EPROTONOSUPPORT; + return res; +} diff --git a/lib/nbsd_libc/compat/sys/compat_stat.c b/lib/nbsd_libc/compat/sys/compat_stat.c new file mode 100644 index 000000000..9425edb6c --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_stat.c @@ -0,0 +1,122 @@ +/* $NetBSD: compat_stat.c,v 1.2 2009/01/11 02:46:27 christos Exp $ */ + +/* + * Copyright (c) 1997 Frank van der Linden + * 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 + * by Frank van der Linden + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_stat.c,v 1.2 2009/01/11 02:46:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include +#include +#include +#include + +__warn_references(stat, + "warning: reference to compatibility stat(); include to generate correct reference") + +__warn_references(fstat, + "warning: reference to compatibility fstat(); include to generate correct reference") + +__warn_references(lstat, + "warning: reference to compatibility lstat(); include to generate correct reference") + +/* + * Convert from a new to an old stat structure. + */ + +static void cvtstat(const struct stat *, struct stat12 *); + +static void +cvtstat(const struct stat *st, struct stat12 *ost) +{ + + ost->st_dev = (uint32_t)st->st_dev; + ost->st_ino = (uint32_t)st->st_ino; + ost->st_mode = st->st_mode; + if (st->st_nlink >= (1 << 15)) + ost->st_nlink = (1 << 15) - 1; + else + ost->st_nlink = st->st_nlink; + ost->st_uid = st->st_uid; + ost->st_gid = st->st_gid; + ost->st_rdev = (uint32_t)st->st_rdev; + ost->st_atimespec.tv_sec = (int32_t)st->st_atimespec.tv_sec; + ost->st_atimespec.tv_nsec = st->st_atimespec.tv_nsec; + ost->st_mtimespec.tv_sec = (int32_t)st->st_mtimespec.tv_sec; + ost->st_mtimespec.tv_nsec = st->st_mtimespec.tv_nsec; + ost->st_ctimespec.tv_sec = (int32_t)st->st_ctimespec.tv_sec; + ost->st_ctimespec.tv_nsec = st->st_ctimespec.tv_nsec; + ost->st_size = st->st_size; + ost->st_blocks = st->st_blocks; + ost->st_blksize = st->st_blksize; + ost->st_flags = st->st_flags; + ost->st_gen = st->st_gen; +} + +int +stat(const char *file, struct stat12 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __stat50(file, &nst)) < 0) + return ret; + cvtstat(&nst, ost); + return ret; +} + +int +fstat(int f, struct stat12 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __fstat50(f, &nst)) < 0) + return ret; + cvtstat(&nst, ost); + return ret; +} + +int +lstat(const char *file, struct stat12 *ost) +{ + struct stat nst; + int ret; + + if ((ret = __lstat50(file, &nst)) < 0) + return ret; + cvtstat(&nst, ost); + return ret; +} diff --git a/lib/nbsd_libc/compat/sys/compat_statfs.c b/lib/nbsd_libc/compat/sys/compat_statfs.c new file mode 100644 index 000000000..1dfc6b541 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_statfs.c @@ -0,0 +1,188 @@ +/* $NetBSD: compat_statfs.c,v 1.5 2009/02/03 05:04:52 lukem Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_statfs.c,v 1.5 2009/02/03 05:04:52 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +__warn_references(statfs, + "warning: reference to obsolete statfs(); use statvfs()") + +__warn_references(fstatfs, + "warning: reference to obsolete fstatfs(); use fstatvfs()") + +__warn_references(fhstatfs, + "warning: reference to obsolete fhstatfs(); use fhstatvfs()") + +__warn_references(getfsstat, + "warning: reference to obsolete getfsstat(); use getvfsstat()") + +/* + * Convert from a new statvfs to an old statfs structure. + */ + +static void vfs2fs(struct statfs12 *, const struct statvfs *); + +#define MOUNTNO_NONE 0 +#define MOUNTNO_UFS 1 /* UNIX "Fast" Filesystem */ +#define MOUNTNO_NFS 2 /* Network Filesystem */ +#define MOUNTNO_MFS 3 /* Memory Filesystem */ +#define MOUNTNO_MSDOS 4 /* MSDOS Filesystem */ +#define MOUNTNO_CD9660 5 /* iso9660 cdrom */ +#define MOUNTNO_FDESC 6 /* /dev/fd filesystem */ +#define MOUNTNO_KERNFS 7 /* kernel variable filesystem */ +#define MOUNTNO_DEVFS 8 /* device node filesystem */ +#define MOUNTNO_AFS 9 /* AFS 3.x */ +static const struct { + const char *name; + const int value; +} nv[] = { + { MOUNT_UFS, MOUNTNO_UFS }, + { MOUNT_NFS, MOUNTNO_NFS }, + { MOUNT_MFS, MOUNTNO_MFS }, + { MOUNT_MSDOS, MOUNTNO_MSDOS }, + { MOUNT_CD9660, MOUNTNO_CD9660 }, + { MOUNT_FDESC, MOUNTNO_FDESC }, + { MOUNT_KERNFS, MOUNTNO_KERNFS }, + { MOUNT_AFS, MOUNTNO_AFS }, +}; + +static void +vfs2fs(struct statfs12 *bfs, const struct statvfs *fs) +{ + size_t i = 0; + bfs->f_type = 0; + bfs->f_oflags = (short)fs->f_flag; + + for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) { + if (strcmp(nv[i].name, fs->f_fstypename) == 0) { + bfs->f_type = nv[i].value; + break; + } + } +#define CLAMP(a) (long)(((a) & ~LONG_MAX) ? LONG_MAX : (a)) + bfs->f_bsize = CLAMP(fs->f_frsize); + bfs->f_iosize = CLAMP(fs->f_iosize); + bfs->f_blocks = CLAMP(fs->f_blocks); + bfs->f_bfree = CLAMP(fs->f_bfree); + if (fs->f_bfree > fs->f_bresvd) + bfs->f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd); + else + bfs->f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree); + bfs->f_files = CLAMP(fs->f_files); + bfs->f_ffree = CLAMP(fs->f_ffree); + bfs->f_fsid = fs->f_fsidx; + bfs->f_owner = fs->f_owner; + bfs->f_flags = (long)fs->f_flag; + bfs->f_syncwrites = CLAMP(fs->f_syncwrites); + bfs->f_asyncwrites = CLAMP(fs->f_asyncwrites); + (void)strncpy(bfs->f_fstypename, fs->f_fstypename, + sizeof(bfs->f_fstypename)); + (void)strncpy(bfs->f_mntonname, fs->f_mntonname, + sizeof(bfs->f_mntonname)); + (void)strncpy(bfs->f_mntfromname, fs->f_mntfromname, + sizeof(bfs->f_mntfromname)); +} + +int +statfs(const char *file, struct statfs12 *ost) +{ + struct statvfs nst; + int ret; + + if ((ret = statvfs(file, &nst)) == -1) + return ret; + vfs2fs(ost, &nst); + return ret; +} + +int +fstatfs(int f, struct statfs12 *ost) +{ + struct statvfs nst; + int ret; + + if ((ret = fstatvfs(f, &nst)) == -1) + return ret; + vfs2fs(ost, &nst); + return ret; +} + +int __fhstatvfs140(const void *fhp, size_t fh_size, struct statvfs *buf, + int flags); + +int +fhstatfs(const struct compat_30_fhandle *fh, struct statfs12 *ost) +{ + struct statvfs nst; + int ret; + + if ((ret = __fhstatvfs140(fh, FHANDLE30_SIZE, &nst, ST_WAIT)) == -1) + return ret; + vfs2fs(ost, &nst); + return ret; +} + +int +getfsstat(struct statfs12 *ost, long size, int flags) +{ + struct statvfs *nst; + int ret, i; + size_t bsize = (size_t)(size / sizeof(*ost)) * sizeof(*nst); + + if (ost != NULL) { + if ((nst = malloc(bsize)) == NULL) + return -1; + } else + nst = NULL; + + if ((ret = getvfsstat(nst, bsize, flags)) == -1) + goto done; + if (nst) + for (i = 0; i < ret; i++) + vfs2fs(&ost[i], &nst[i]); +done: + if (nst) + free(nst); + return ret; +} diff --git a/lib/nbsd_libc/compat/sys/compat_timer.c b/lib/nbsd_libc/compat/sys/compat_timer.c new file mode 100644 index 000000000..8c09a393a --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_timer.c @@ -0,0 +1,83 @@ +/* $NetBSD: compat_timer.c,v 1.2 2009/01/11 02:46:27 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_timer.c,v 1.2 2009/01/11 02:46:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include + +__warn_references(timer_gettime, + "warning: reference to compatibility timer_gettime(); include to generate correct reference") +__warn_references(timer_settime, + "warning: reference to compatibility timer_settime(); include to generate correct reference") + +int +timer_settime(timer_t timerid, int flags, + const struct itimerspec50 * __restrict tim50, + struct itimerspec50 * __restrict otim50) +{ + struct itimerspec tim, *timp; + struct itimerspec otim, *otimp; + int error; + + otimp = otim50 ? &otim : NULL; + if (tim50) + itimerspec50_to_itimerspec(tim50, timp = &tim); + else + timp = NULL; + error = __timer_settime50(timerid, flags, timp, otimp); + if (error) + return error; + if (otim50) + itimerspec_to_itimerspec50(otimp, otim50); + return 0; +} + +int +timer_gettime(timer_t timerid, struct itimerspec50 *tim50) +{ + struct itimerspec tim, *timp = tim50 ? &tim : NULL; + int error = __timer_gettime50(timerid, timp); + if (error) + return error; + if (tim50) + itimerspec_to_itimerspec50(timp, tim50); + return 0; +} diff --git a/lib/nbsd_libc/compat/sys/compat_utimes.c b/lib/nbsd_libc/compat/sys/compat_utimes.c new file mode 100644 index 000000000..a5664341e --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_utimes.c @@ -0,0 +1,88 @@ +/* $NetBSD: compat_utimes.c,v 1.2 2009/01/11 02:46:27 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_utimes.c,v 1.2 2009/01/11 02:46:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include + +__warn_references(utimes, + "warning: reference to compatibility utimes(); include to generate correct reference") +__warn_references(lutimes, + "warning: reference to compatibility lutimes(); include to generate correct reference") +__warn_references(futimes, + "warning: reference to compatibility futimes(); include to generate correct reference") + +/* + * libc12 compatible f,l,utimes routine. + */ +int +utimes(const char *path, const struct timeval50 *tv50) +{ + struct timeval tv[2]; + + if (tv50) { + timeval50_to_timeval(&tv50[0], &tv[0]); + timeval50_to_timeval(&tv50[1], &tv[1]); + } + return __utimes50(path, tv50 ? tv : NULL); +} + +int +lutimes(const char *path, const struct timeval50 *tv50) +{ + struct timeval tv[2]; + + if (tv50) { + timeval50_to_timeval(&tv50[0], &tv[0]); + timeval50_to_timeval(&tv50[1], &tv[1]); + } + return __lutimes50(path, tv50 ? tv : NULL); +} + +int +futimes(int fd, const struct timeval50 *tv50) +{ + struct timeval tv[2]; + + if (tv50) { + timeval50_to_timeval(&tv50[0], &tv[0]); + timeval50_to_timeval(&tv50[1], &tv[1]); + } + return __futimes50(fd, tv50 ? tv : NULL); +} diff --git a/lib/nbsd_libc/compat/sys/compat_wait4.c b/lib/nbsd_libc/compat/sys/compat_wait4.c new file mode 100644 index 000000000..9f1f38b02 --- /dev/null +++ b/lib/nbsd_libc/compat/sys/compat_wait4.c @@ -0,0 +1,86 @@ +/* $NetBSD: compat_wait4.c,v 1.2 2009/01/11 02:46:27 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: compat_wait4.c,v 1.2 2009/01/11 02:46:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define __LIBC12_SOURCE__ + +#include "namespace.h" +#include +#include +#include +#include +#include + +__warn_references(wait3, + "warning: reference to compatibility wait3(); include to generate correct reference") +__warn_references(wait4, + "warning: reference to compatibility wait4(); include to generate correct reference") + +extern void __rusage_to_rusage50(const struct rusage *, struct rusage50 *); + +#ifdef __weak_alias +__weak_alias(wait4, _wait4) +__weak_alias(_sys_wait4, _wait4) +__weak_alias(wait3, _wait3) +#endif + +/* + * libc12 compatible wait4 routine. + */ +int +wait3(int *status, int options, struct rusage50 *ru50) +{ + struct rusage ru; + int rv; + + if ((rv = __wait350(status, options, ru50 ? &ru : NULL)) == -1) + return rv; + if (ru50) + __rusage_to_rusage50(&ru, ru50); + return rv; +} + +int +wait4(pid_t wpid, int *status, int options, struct rusage50 *ru50) +{ + struct rusage ru; + int rv; + + if ((rv = __wait450(wpid, status, options, ru50 ? &ru : NULL)) == -1) + return rv; + if (ru50) + __rusage_to_rusage50(&ru, ru50); + return rv; +} diff --git a/lib/nbsd_libc/compat/sys/getdirentries.3 b/lib/nbsd_libc/compat/sys/getdirentries.3 new file mode 100644 index 000000000..b1f4b6e5f --- /dev/null +++ b/lib/nbsd_libc/compat/sys/getdirentries.3 @@ -0,0 +1,164 @@ +.\" $NetBSD: getdirentries.3,v 1.1 2005/09/13 01:44:09 christos 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. +.\" +.\" @(#)getdirentries.2 8.1 (Berkeley) 6/9/93 +.\" +.Dd June 9, 1993 +.Dt GETDIRENTRIES 3 +.Os +.Sh NAME +.Nm getdirentries +.Nd "get directory entries in a filesystem independent format" +.Sh SYNOPSIS +.In dirent.h +.Ft int +.Fn getdirentries "int fd" "char *buf" "int nbytes" "long *basep" +.Sh DESCRIPTION +.Bf -symbolic +.\" This interface is available from the compatibility library, libcompat and +This interface is provided for compatibility only and +has been obsoleted by +.Xr getdents 2 . +.Ef +.Pp +.Fn getdirentries +reads directory entries from the directory +referenced by the file descriptor +.Fa fd +into the buffer pointed to by +.Fa buf , +in a filesystem independent format. +Up to +.Fa nbytes +of data will be transferred. +.Fa nbytes +must be greater than or equal to the +block size associated with the file, +see +.Xr stat 2 . +Some filesystems may not support +.Fn getdirentries +with buffers smaller than this size. +.Pp +The data in the buffer is a series of +.Em dirent +structures each containing the following entries: +.Bd -literal -offset indent +unsigned long d_fileno; +unsigned short d_reclen; +unsigned short d_namlen; +char d_name[MAXNAMELEN + 1]; /* see below */ +.Ed +.Pp +The +.Fa d_fileno +entry is a number which is unique for each +distinct file in the filesystem. +Files that are linked by hard links (see +.Xr link 2 ) +have the same +.Fa d_fileno . +If +.Fa d_fileno +is zero, the entry refers to a deleted file. +.Pp +The +.Fa d_reclen +entry is the length, in bytes, of the directory record. +.Pp +The +.Fa d_namlen +entry specifies the length of the file name excluding the null byte. +Thus the actual size of +.Fa d_name +may vary from 1 to +.Dv MAXNAMELEN +\&+ 1. +.Pp +The +.Fa d_name +entry contains a null terminated file name. +.Pp +Entries may be separated by extra space. +The +.Fa d_reclen +entry may be used as an offset from the start of a +.Fa dirent +structure to the next structure, if any. +.Pp +The actual number of bytes transferred is returned. +The current position pointer associated with +.Fa fd +is set to point to the next block of entries. +The pointer may not advance by the number of bytes returned by +.Fn getdirentries . +A value of zero is returned when +the end of the directory has been reached. +.Pp +.Fn getdirentries +writes the position of the block read into the location pointed to by +.Fa basep . +Alternatively, the current position pointer may be set and retrieved by +.Xr lseek 2 . +The current position pointer should only be set to a value returned by +.Xr lseek 2 , +a value returned in the location pointed to by +.Fa basep , +or zero. +.Sh RETURN VALUES +If successful, the number of bytes actually transferred is returned. +Otherwise, \-1 is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn getdirentries +will fail if: +.Bl -tag -width Er +.It Bq Er EBADF +.Fa fd +is not a valid file descriptor open for reading. +.It Bq Er EFAULT +Either +.Fa buf +or +.Fa basep +point outside the allocated address space. +.It Bq Er EIO +An +.Tn I/O +error occurred while reading from or writing to the file system. +.El +.Sh SEE ALSO +.Xr lseek 2 , +.Xr open 2 +.Sh HISTORY +The +.Fn getdirentries +function first appeared in +.Bx 4.4 . diff --git a/lib/nbsd_libc/compat/time/Makefile.inc b/lib/nbsd_libc/compat/time/Makefile.inc new file mode 100644 index 000000000..9728ed1c3 --- /dev/null +++ b/lib/nbsd_libc/compat/time/Makefile.inc @@ -0,0 +1,4 @@ +# $NetBSD: Makefile.inc,v 1.2 2009/01/11 03:46:25 christos Exp $ + +.PATH: ${COMPATDIR}/time +SRCS+= compat_localtime.c compat_difftime.c diff --git a/lib/nbsd_libc/compat/time/compat_difftime.c b/lib/nbsd_libc/compat/time/compat_difftime.c new file mode 100644 index 000000000..30f08ff01 --- /dev/null +++ b/lib/nbsd_libc/compat/time/compat_difftime.c @@ -0,0 +1,54 @@ +/* $NetBSD: compat_difftime.c,v 1.2 2009/01/11 03:46:25 christos Exp $ */ +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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 "namespace.h" +#include + +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(difftime,_difftime) +#endif + +#define timeval timeval50 +#define timespec timespec50 +#define time_t int32_t + +#include "time/difftime.c" diff --git a/lib/nbsd_libc/compat/time/compat_localtime.c b/lib/nbsd_libc/compat/time/compat_localtime.c new file mode 100644 index 000000000..8a2224e30 --- /dev/null +++ b/lib/nbsd_libc/compat/time/compat_localtime.c @@ -0,0 +1,69 @@ +/* $NetBSD: compat_localtime.c,v 1.2 2009/01/11 03:46:25 christos Exp $ */ + +/* + * Written by Jason R. Thorpe , October 21, 1997. + * Public domain. + */ + +#include "namespace.h" +#include + +#define __LIBC12_SOURCE__ +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(ctime_r,_ctime_r) +__weak_alias(gmtime_r,_gmtime_r) +__weak_alias(localtime_r,_localtime_r) +__weak_alias(offtime,_offtime) +__weak_alias(posix2time,_posix2time) +__weak_alias(time2posix,_time2posix) +__weak_alias(timegm,_timegm) +__weak_alias(timelocal,_timelocal) +__weak_alias(timeoff,_timeoff) +__weak_alias(tzset,_tzset) +__weak_alias(tzsetwall,_tzsetwall) +#endif + +__warn_references(ctime_r, + "warning: reference to compatibility ctime_r();" + " include for correct reference") +__warn_references(gmtime_r, + "warning: reference to compatibility gmtime_r();" + " include for correct reference") +__warn_references(localtime_r, + "warning: reference to compatibility localtime_r();" + " include for correct reference") +__warn_references(offtime, + "warning: reference to compatibility offtime();" + " include for correct reference") +__warn_references(posix2time, + "warning: reference to compatibility posix2time();" + " include for correct reference") +__warn_references(time2posix, + "warning: reference to compatibility time2posix();" + " include for correct reference") +__warn_references(timegm, + "warning: reference to compatibility timegm();" + " include for correct reference") +__warn_references(timelocal, + "warning: reference to compatibility timelocal();" + " include for correct reference") +__warn_references(timeoff, + "warning: reference to compatibility timeoff();" + " include for correct reference") +__warn_references(tzset, + "warning: reference to compatibility tzset();" + " include for correct reference") +__warn_references(tzsetwall, + "warning: reference to compatibility tzsetwall();" + " include for correct reference") + +#define timeval timeval50 +#define timespec timespec50 +#define time_t int32_t + +#include "time/localtime.c" diff --git a/lib/nbsd_libc/db/Makefile.inc b/lib/nbsd_libc/db/Makefile.inc new file mode 100644 index 000000000..224719d65 --- /dev/null +++ b/lib/nbsd_libc/db/Makefile.inc @@ -0,0 +1,11 @@ +# $NetBSD: Makefile.inc,v 1.6 1997/10/22 23:14:11 lukem Exp $ +# @(#)Makefile.inc 8.2 (Berkeley) 2/21/94 +# +CPPFLAGS+=-D__DBINTERFACE_PRIVATE + +.include "${.CURDIR}/db/btree/Makefile.inc" +.include "${.CURDIR}/db/db/Makefile.inc" +.include "${.CURDIR}/db/hash/Makefile.inc" +.include "${.CURDIR}/db/man/Makefile.inc" +.include "${.CURDIR}/db/mpool/Makefile.inc" +.include "${.CURDIR}/db/recno/Makefile.inc" diff --git a/lib/nbsd_libc/db/README b/lib/nbsd_libc/db/README new file mode 100644 index 000000000..d06cdf14f --- /dev/null +++ b/lib/nbsd_libc/db/README @@ -0,0 +1,41 @@ +# $NetBSD: README,v 1.3 1996/05/03 21:17:07 cgd Exp $ +# @(#)README 8.27 (Berkeley) 9/1/94 + +This is version 1.85 of the Berkeley DB code. + +For information on compiling and installing this software, see the file +PORT/README. + +Newer versions of this software will periodically be made available by +anonymous ftp from ftp.cs.berkeley.edu. An archive in compressed format +is in ucb/4bsd/db.tar.Z, or in gzip format in ucb/4bsd/db.tar.gz. If +you'd like to receive announcements of future releases of this software, +send email to the contact address below. + +Email questions may be addressed to Keith Bostic at bostic@cs.berkeley.edu. + +============================================ +Distribution contents: + +Makefile.inc Ignore this, it's the 4.4BSD subsystem Makefile. +PORT The per OS/architecture directories to use to build + libdb.a, if you're not running 4.4BSD. See the file + PORT/README for more information. +README This file. +btree The B+tree routines. +changelog List of changes, per version. +db The dbopen(3) interface routine. +docs Various USENIX papers, and the formatted manual pages. +hash The extended linear hashing routines. +man The unformatted manual pages. +mpool The memory pool routines. +recno The fixed/variable length record routines. +test Test package. + +============================================ +Debugging: + +If you're running a memory checker (e.g. Purify) on DB, make sure that +you recompile it with "-DPURIFY" in the CFLAGS, first. By default, +allocated pages are not initialized by the DB code, and they will show +up as reads of uninitialized memory in the buffer write routines. diff --git a/lib/nbsd_libc/db/btree/Makefile.inc b/lib/nbsd_libc/db/btree/Makefile.inc new file mode 100644 index 000000000..46fbb029e --- /dev/null +++ b/lib/nbsd_libc/db/btree/Makefile.inc @@ -0,0 +1,8 @@ +# $NetBSD: Makefile.inc,v 1.6 1996/05/03 21:50:36 cgd Exp $ +# @(#)Makefile.inc 8.2 (Berkeley) 7/14/94 + +.PATH: ${.CURDIR}/db/btree + +SRCS+= bt_close.c bt_conv.c bt_debug.c bt_delete.c bt_get.c bt_open.c \ + bt_overflow.c bt_page.c bt_put.c bt_search.c bt_seq.c bt_split.c \ + bt_utils.c diff --git a/lib/nbsd_libc/db/btree/bt_close.c b/lib/nbsd_libc/db/btree/bt_close.c new file mode 100644 index 000000000..a085952e0 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_close.c @@ -0,0 +1,180 @@ +/* $NetBSD: bt_close.c,v 1.14 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_close.c,v 1.14 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include + +#include +#include "btree.h" + +static int bt_meta(BTREE *); + +/* + * BT_CLOSE -- Close a btree. + * + * Parameters: + * dbp: pointer to access method + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__bt_close(DB *dbp) +{ + BTREE *t; + int fd; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* Sync the tree. */ + if (__bt_sync(dbp, 0) == RET_ERROR) + return (RET_ERROR); + + /* Close the memory pool. */ + if (mpool_close(t->bt_mp) == RET_ERROR) + return (RET_ERROR); + + /* Free random memory. */ + if (t->bt_cursor.key.data != NULL) { + free(t->bt_cursor.key.data); + t->bt_cursor.key.size = 0; + t->bt_cursor.key.data = NULL; + } + if (t->bt_rkey.data) { + free(t->bt_rkey.data); + t->bt_rkey.size = 0; + t->bt_rkey.data = NULL; + } + if (t->bt_rdata.data) { + free(t->bt_rdata.data); + t->bt_rdata.size = 0; + t->bt_rdata.data = NULL; + } + + fd = t->bt_fd; + free(t); + free(dbp); + return (close(fd) ? RET_ERROR : RET_SUCCESS); +} + +/* + * BT_SYNC -- sync the btree to disk. + * + * Parameters: + * dbp: pointer to access method + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +int +__bt_sync(const DB *dbp, u_int flags) +{ + BTREE *t; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* Sync doesn't currently take any flags. */ + if (flags != 0) { + errno = EINVAL; + return (RET_ERROR); + } + + if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED)) + return (RET_SUCCESS); + + if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR) + return (RET_ERROR); + + if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS) + F_CLR(t, B_MODIFIED); + + return (status); +} + +/* + * BT_META -- write the tree meta data to disk. + * + * Parameters: + * t: tree + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +static int +bt_meta(BTREE *t) +{ + BTMETA m; + void *p; + + if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL) + return (RET_ERROR); + + /* Fill in metadata. */ + m.magic = BTREEMAGIC; + m.version = BTREEVERSION; + m.psize = t->bt_psize; + m.free = t->bt_free; + m.nrecs = t->bt_nrecs; + m.flags = F_ISSET(t, SAVEMETA); + + memmove(p, &m, sizeof(BTMETA)); + mpool_put(t->bt_mp, p, MPOOL_DIRTY); + return (RET_SUCCESS); +} diff --git a/lib/nbsd_libc/db/btree/bt_conv.c b/lib/nbsd_libc/db/btree/bt_conv.c new file mode 100644 index 000000000..2c5217a39 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_conv.c @@ -0,0 +1,214 @@ +/* $NetBSD: bt_conv.c,v 1.14 2008/09/10 17:52:35 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_conv.c,v 1.14 2008/09/10 17:52:35 joerg Exp $"); + +#include +#include + +#include +#include "btree.h" + +static void mswap(PAGE *); + +/* + * __BT_BPGIN, __BT_BPGOUT -- + * Convert host-specific number layout to/from the host-independent + * format stored on disk. + * + * Parameters: + * t: tree + * pg: page number + * h: page to convert + */ +void +__bt_pgin(void *t, pgno_t pg, void *pp) +{ + PAGE *h; + indx_t i, top; + uint8_t flags; + char *p; + + if (!F_ISSET(((BTREE *)t), B_NEEDSWAP)) + return; + if (pg == P_META) { + mswap(pp); + return; + } + + h = pp; + M_32_SWAP(h->pgno); + M_32_SWAP(h->prevpg); + M_32_SWAP(h->nextpg); + M_32_SWAP(h->flags); + M_16_SWAP(h->lower); + M_16_SWAP(h->upper); + + top = NEXTINDEX(h); + if ((h->flags & P_TYPE) == P_BINTERNAL) + for (i = 0; i < top; i++) { + M_16_SWAP(h->linp[i]); + p = (char *)(void *)GETBINTERNAL(h, i); + P_32_SWAP(p); + p += sizeof(uint32_t); + P_32_SWAP(p); + p += sizeof(pgno_t); + if (*(uint8_t *)p & P_BIGKEY) { + p += sizeof(uint8_t); + P_32_SWAP(p); + p += sizeof(pgno_t); + P_32_SWAP(p); + } + } + else if ((h->flags & P_TYPE) == P_BLEAF) + for (i = 0; i < top; i++) { + M_16_SWAP(h->linp[i]); + p = (char *)(void *)GETBLEAF(h, i); + P_32_SWAP(p); + p += sizeof(uint32_t); + P_32_SWAP(p); + p += sizeof(uint32_t); + flags = *(uint8_t *)p; + if (flags & (P_BIGKEY | P_BIGDATA)) { + p += sizeof(uint8_t); + if (flags & P_BIGKEY) { + P_32_SWAP(p); + p += sizeof(pgno_t); + P_32_SWAP(p); + } + if (flags & P_BIGDATA) { + p += sizeof(uint32_t); + P_32_SWAP(p); + p += sizeof(pgno_t); + P_32_SWAP(p); + } + } + } +} + +void +__bt_pgout(void *t, pgno_t pg, void *pp) +{ + PAGE *h; + indx_t i, top; + uint8_t flags; + char *p; + + if (!F_ISSET(((BTREE *)t), B_NEEDSWAP)) + return; + if (pg == P_META) { + mswap(pp); + return; + } + + h = pp; + top = NEXTINDEX(h); + if ((h->flags & P_TYPE) == P_BINTERNAL) + for (i = 0; i < top; i++) { + p = (char *)(void *)GETBINTERNAL(h, i); + P_32_SWAP(p); + p += sizeof(uint32_t); + P_32_SWAP(p); + p += sizeof(pgno_t); + if (*(uint8_t *)p & P_BIGKEY) { + p += sizeof(uint8_t); + P_32_SWAP(p); + p += sizeof(pgno_t); + P_32_SWAP(p); + } + M_16_SWAP(h->linp[i]); + } + else if ((h->flags & P_TYPE) == P_BLEAF) + for (i = 0; i < top; i++) { + p = (char *)(void *)GETBLEAF(h, i); + P_32_SWAP(p); + p += sizeof(uint32_t); + P_32_SWAP(p); + p += sizeof(uint32_t); + flags = *(uint8_t *)p; + if (flags & (P_BIGKEY | P_BIGDATA)) { + p += sizeof(uint8_t); + if (flags & P_BIGKEY) { + P_32_SWAP(p); + p += sizeof(pgno_t); + P_32_SWAP(p); + } + if (flags & P_BIGDATA) { + p += sizeof(uint32_t); + P_32_SWAP(p); + p += sizeof(pgno_t); + P_32_SWAP(p); + } + } + M_16_SWAP(h->linp[i]); + } + + M_32_SWAP(h->pgno); + M_32_SWAP(h->prevpg); + M_32_SWAP(h->nextpg); + M_32_SWAP(h->flags); + M_16_SWAP(h->lower); + M_16_SWAP(h->upper); +} + +/* + * MSWAP -- Actually swap the bytes on the meta page. + * + * Parameters: + * p: page to convert + */ +static void +mswap(PAGE *pg) +{ + char *p; + + p = (char *)(void *)pg; + P_32_SWAP(p); /* magic */ + p += sizeof(uint32_t); + P_32_SWAP(p); /* version */ + p += sizeof(uint32_t); + P_32_SWAP(p); /* psize */ + p += sizeof(uint32_t); + P_32_SWAP(p); /* free */ + p += sizeof(uint32_t); + P_32_SWAP(p); /* nrecs */ + p += sizeof(uint32_t); + P_32_SWAP(p); /* flags */ + p += sizeof(uint32_t); +} diff --git a/lib/nbsd_libc/db/btree/bt_debug.c b/lib/nbsd_libc/db/btree/bt_debug.c new file mode 100644 index 000000000..590ca4ac2 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_debug.c @@ -0,0 +1,324 @@ +/* $NetBSD: bt_debug.c,v 1.15 2008/09/10 17:52:35 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_debug.c,v 1.15 2008/09/10 17:52:35 joerg Exp $"); + +#include +#include +#include +#include + +#include +#include "btree.h" + +#ifdef DEBUG +/* + * BT_DUMP -- Dump the tree + * + * Parameters: + * dbp: pointer to the DB + */ +void +__bt_dump(DB *dbp) +{ + BTREE *t; + PAGE *h; + pgno_t i; + const char *sep; + + t = dbp->internal; + (void)fprintf(stderr, "%s: pgsz %d", + F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize); + if (F_ISSET(t, R_RECNO)) + (void)fprintf(stderr, " keys %lu", (unsigned long) t->bt_nrecs); +#undef X +#define X(flag, name) \ + if (F_ISSET(t, flag)) { \ + (void)fprintf(stderr, "%s%s", sep, name); \ + sep = ", "; \ + } + if (t->flags != 0) { + sep = " flags ("; + X(R_FIXLEN, "FIXLEN"); + X(B_INMEM, "INMEM"); + X(B_NODUPS, "NODUPS"); + X(B_RDONLY, "RDONLY"); + X(R_RECNO, "RECNO"); + X(B_METADIRTY,"METADIRTY"); + (void)fprintf(stderr, ")\n"); + } +#undef X + + for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { + __bt_dpage(h); + (void)mpool_put(t->bt_mp, h, 0); + } +} + +/* + * BT_DMPAGE -- Dump the meta page + * + * Parameters: + * h: pointer to the PAGE + */ +void +__bt_dmpage(PAGE *h) +{ + BTMETA *m; + const char *sep; + + m = (BTMETA *)(void *)h; + (void)fprintf(stderr, "magic %lx\n", (unsigned long) m->magic); + (void)fprintf(stderr, "version %lu\n", (unsigned long) m->version); + (void)fprintf(stderr, "psize %lu\n", (unsigned long) m->psize); + (void)fprintf(stderr, "free %lu\n", (unsigned long) m->free); + (void)fprintf(stderr, "nrecs %lu\n", (unsigned long) m->nrecs); + (void)fprintf(stderr, "flags %lu", (unsigned long) m->flags); +#undef X +#define X(flag, name) \ + if (m->flags & flag) { \ + (void)fprintf(stderr, "%s%s", sep, name); \ + sep = ", "; \ + } + if (m->flags) { + sep = " ("; + X(B_NODUPS, "NODUPS"); + X(R_RECNO, "RECNO"); + (void)fprintf(stderr, ")"); + } +} + +/* + * BT_DNPAGE -- Dump the page + * + * Parameters: + * n: page number to dump. + */ +void +__bt_dnpage(DB *dbp, pgno_t pgno) +{ + BTREE *t; + PAGE *h; + + t = dbp->internal; + if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) { + __bt_dpage(h); + (void)mpool_put(t->bt_mp, h, 0); + } +} + +/* + * BT_DPAGE -- Dump the page + * + * Parameters: + * h: pointer to the PAGE + */ +void +__bt_dpage(PAGE *h) +{ + BINTERNAL *bi; + BLEAF *bl; + RINTERNAL *ri; + RLEAF *rl; + indx_t cur, top; + const char *sep; + + (void)fprintf(stderr, " page %d: (", h->pgno); +#undef X +#define X(flag, name) \ + if (h->flags & flag) { \ + (void)fprintf(stderr, "%s%s", sep, name); \ + sep = ", "; \ + } + sep = ""; + X(P_BINTERNAL, "BINTERNAL") /* types */ + X(P_BLEAF, "BLEAF") + X(P_RINTERNAL, "RINTERNAL") /* types */ + X(P_RLEAF, "RLEAF") + X(P_OVERFLOW, "OVERFLOW") + X(P_PRESERVE, "PRESERVE"); + (void)fprintf(stderr, ")\n"); +#undef X + + (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg); + if (h->flags & P_OVERFLOW) + return; + + top = NEXTINDEX(h); + (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n", + h->lower, h->upper, top); + for (cur = 0; cur < top; cur++) { + (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]); + switch (h->flags & P_TYPE) { + case P_BINTERNAL: + bi = GETBINTERNAL(h, cur); + (void)fprintf(stderr, + "size %03d pgno %03d", bi->ksize, bi->pgno); + if (bi->flags & P_BIGKEY) + (void)fprintf(stderr, " (indirect)"); + else if (bi->ksize) + (void)fprintf(stderr, + " {%.*s}", (int)bi->ksize, bi->bytes); + break; + case P_RINTERNAL: + ri = GETRINTERNAL(h, cur); + (void)fprintf(stderr, "entries %03d pgno %03d", + ri->nrecs, ri->pgno); + break; + case P_BLEAF: + bl = GETBLEAF(h, cur); + if (bl->flags & P_BIGKEY) + (void)fprintf(stderr, + "big key page %lu size %u/", + (unsigned long) *(pgno_t *)(void *)bl->bytes, + *(uint32_t *)(void *)(bl->bytes + sizeof(pgno_t))); + else if (bl->ksize) + (void)fprintf(stderr, "%s/", bl->bytes); + if (bl->flags & P_BIGDATA) + (void)fprintf(stderr, + "big data page %lu size %u", + (unsigned long) *(pgno_t *)(void *)(bl->bytes + bl->ksize), + *(uint32_t *)(void *)(bl->bytes + bl->ksize + + sizeof(pgno_t))); + else if (bl->dsize) + (void)fprintf(stderr, "%.*s", + (int)bl->dsize, bl->bytes + bl->ksize); + break; + case P_RLEAF: + rl = GETRLEAF(h, cur); + if (rl->flags & P_BIGDATA) + (void)fprintf(stderr, + "big data page %lu size %u", + (unsigned long) *(pgno_t *)(void *)rl->bytes, + *(uint32_t *)(void *)(rl->bytes + sizeof(pgno_t))); + else if (rl->dsize) + (void)fprintf(stderr, + "%.*s", (int)rl->dsize, rl->bytes); + break; + } + (void)fprintf(stderr, "\n"); + } +} +#endif + +#ifdef STATISTICS +/* + * BT_STAT -- Gather/print the tree statistics + * + * Parameters: + * dbp: pointer to the DB + */ +void +__bt_stat(DB *dbp) +{ + extern unsigned long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit; + extern unsigned long bt_sortsplit, bt_split; + BTREE *t; + PAGE *h; + pgno_t i, pcont, pinternal, pleaf; + unsigned long ifree, lfree, nkeys; + int levels; + + t = dbp->internal; + pcont = pinternal = pleaf = 0; + nkeys = ifree = lfree = 0; + for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { + switch (h->flags & P_TYPE) { + case P_BINTERNAL: + case P_RINTERNAL: + ++pinternal; + ifree += h->upper - h->lower; + break; + case P_BLEAF: + case P_RLEAF: + ++pleaf; + lfree += h->upper - h->lower; + nkeys += NEXTINDEX(h); + break; + case P_OVERFLOW: + ++pcont; + break; + } + (void)mpool_put(t->bt_mp, h, 0); + } + + /* Count the levels of the tree. */ + for (i = P_ROOT, levels = 0 ;; ++levels) { + h = mpool_get(t->bt_mp, i, 0); + if (h->flags & (P_BLEAF|P_RLEAF)) { + if (levels == 0) + levels = 1; + (void)mpool_put(t->bt_mp, h, 0); + break; + } + i = F_ISSET(t, R_RECNO) ? + GETRINTERNAL(h, 0)->pgno : + GETBINTERNAL(h, 0)->pgno; + (void)mpool_put(t->bt_mp, h, 0); + } + + (void)fprintf(stderr, "%d level%s with %ld keys", + levels, levels == 1 ? "" : "s", nkeys); + if (F_ISSET(t, R_RECNO)) + (void)fprintf(stderr, " (%ld header count)", (long)t->bt_nrecs); + (void)fprintf(stderr, + "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n", + (long)pinternal + pleaf + pcont, (long)pleaf, (long)pinternal, + (long)pcont); + (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n", + bt_cache_hit, bt_cache_miss); + (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n", + bt_split, bt_rootsplit, bt_sortsplit); + pleaf *= t->bt_psize - BTDATAOFF; + if (pleaf) + (void)fprintf(stderr, + "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n", + ((double)(pleaf - lfree) / pleaf) * 100, + pleaf - lfree, lfree); + pinternal *= t->bt_psize - BTDATAOFF; + if (pinternal) + (void)fprintf(stderr, + "%.0f%% internal fill (%ld bytes used, %ld bytes free\n", + ((double)(pinternal - ifree) / pinternal) * 100, + pinternal - ifree, ifree); + if (bt_pfxsaved) + (void)fprintf(stderr, "prefix checking removed %lu bytes.\n", + bt_pfxsaved); +} +#endif diff --git a/lib/nbsd_libc/db/btree/bt_delete.c b/lib/nbsd_libc/db/btree/bt_delete.c new file mode 100644 index 000000000..701c8b201 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_delete.c @@ -0,0 +1,642 @@ +/* $NetBSD: bt_delete.c,v 1.17 2009/01/29 02:02:36 lukem Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_delete.c,v 1.17 2009/01/29 02:02:36 lukem Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#include +#include "btree.h" + +static int __bt_bdelete(BTREE *, const DBT *); +static int __bt_curdel(BTREE *, const DBT *, PAGE *, u_int); +static int __bt_pdelete(BTREE *, PAGE *); +static int __bt_relink(BTREE *, PAGE *); +static int __bt_stkacq(BTREE *, PAGE **, CURSOR *); + +/* + * __bt_delete + * Delete the item(s) referenced by a key. + * + * Return RET_SPECIAL if the key is not found. + */ +int +__bt_delete(const DB *dbp, const DBT *key, u_int flags) +{ + BTREE *t; + CURSOR *c; + PAGE *h; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* Check for change to a read-only tree. */ + if (F_ISSET(t, B_RDONLY)) { + errno = EPERM; + return (RET_ERROR); + } + + switch (flags) { + case 0: + status = __bt_bdelete(t, key); + break; + case R_CURSOR: + /* + * If flags is R_CURSOR, delete the cursor. Must already + * have started a scan and not have already deleted it. + */ + c = &t->bt_cursor; + if (F_ISSET(c, CURS_INIT)) { + if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) + return (RET_SPECIAL); + if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL) + return (RET_ERROR); + + /* + * If the page is about to be emptied, we'll need to + * delete it, which means we have to acquire a stack. + */ + if (NEXTINDEX(h) == 1) + if (__bt_stkacq(t, &h, &t->bt_cursor)) + return (RET_ERROR); + + status = __bt_dleaf(t, NULL, h, (u_int)c->pg.index); + + if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) { + if (__bt_pdelete(t, h)) + return (RET_ERROR); + } else + mpool_put(t->bt_mp, h, + (u_int)(status == RET_SUCCESS ? + MPOOL_DIRTY : 0)); + break; + } + /* FALLTHROUGH */ + default: + errno = EINVAL; + return (RET_ERROR); + } + if (status == RET_SUCCESS) + F_SET(t, B_MODIFIED); + return (status); +} + +/* + * __bt_stkacq -- + * Acquire a stack so we can delete a cursor entry. + * + * Parameters: + * t: tree + * hp: pointer to current, pinned PAGE pointer + * c: pointer to the cursor + * + * Returns: + * 0 on success, 1 on failure + */ +static int +__bt_stkacq(BTREE *t, PAGE **hp, CURSOR *c) +{ + BINTERNAL *bi; + EPG *e; + EPGNO *parent; + PAGE *h; + indx_t idx = 0; /* Pacify gcc */ + pgno_t pgno; + recno_t nextpg, prevpg; + int exact, level; + + /* + * Find the first occurrence of the key in the tree. Toss the + * currently locked page so we don't hit an already-locked page. + */ + h = *hp; + mpool_put(t->bt_mp, h, 0); + if ((e = __bt_search(t, &c->key, &exact)) == NULL) + return (1); + h = e->page; + + /* See if we got it in one shot. */ + if (h->pgno == c->pg.pgno) + goto ret; + + /* + * Move right, looking for the page. At each move we have to move + * up the stack until we don't have to move to the next page. If + * we have to change pages at an internal level, we have to fix the + * stack back up. + */ + while (h->pgno != c->pg.pgno) { + if ((nextpg = h->nextpg) == P_INVALID) + break; + mpool_put(t->bt_mp, h, 0); + + /* Move up the stack. */ + for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { + /* Get the parent page. */ + if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) + return (1); + + /* Move to the next index. */ + if (parent->index != NEXTINDEX(h) - 1) { + idx = parent->index + 1; + BT_PUSH(t, h->pgno, idx); + break; + } + mpool_put(t->bt_mp, h, 0); + } + + /* Restore the stack. */ + while (level--) { + /* Push the next level down onto the stack. */ + bi = GETBINTERNAL(h, idx); + pgno = bi->pgno; + BT_PUSH(t, pgno, 0); + + /* Lose the currently pinned page. */ + mpool_put(t->bt_mp, h, 0); + + /* Get the next level down. */ + if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) + return (1); + idx = 0; + } + mpool_put(t->bt_mp, h, 0); + if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL) + return (1); + } + + if (h->pgno == c->pg.pgno) + goto ret; + + /* Reacquire the original stack. */ + mpool_put(t->bt_mp, h, 0); + if ((e = __bt_search(t, &c->key, &exact)) == NULL) + return (1); + h = e->page; + + /* + * Move left, looking for the page. At each move we have to move + * up the stack until we don't have to change pages to move to the + * next page. If we have to change pages at an internal level, we + * have to fix the stack back up. + */ + while (h->pgno != c->pg.pgno) { + if ((prevpg = h->prevpg) == P_INVALID) + break; + mpool_put(t->bt_mp, h, 0); + + /* Move up the stack. */ + for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { + /* Get the parent page. */ + if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) + return (1); + + /* Move to the next index. */ + if (parent->index != 0) { + idx = parent->index - 1; + BT_PUSH(t, h->pgno, idx); + break; + } + mpool_put(t->bt_mp, h, 0); + } + + /* Restore the stack. */ + while (level--) { + /* Push the next level down onto the stack. */ + bi = GETBINTERNAL(h, idx); + pgno = bi->pgno; + + /* Lose the currently pinned page. */ + mpool_put(t->bt_mp, h, 0); + + /* Get the next level down. */ + if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) + return (1); + + idx = NEXTINDEX(h) - 1; + BT_PUSH(t, pgno, idx); + } + mpool_put(t->bt_mp, h, 0); + if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL) + return (1); + } + + +ret: mpool_put(t->bt_mp, h, 0); + return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL); +} + +/* + * __bt_bdelete -- + * Delete all key/data pairs matching the specified key. + * + * Parameters: + * t: tree + * key: key to delete + * + * Returns: + * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. + */ +static int +__bt_bdelete(BTREE *t, const DBT *key) +{ + EPG *e; + PAGE *h; + int deleted, exact, redo; + + deleted = 0; + + /* Find any matching record; __bt_search pins the page. */ +loop: if ((e = __bt_search(t, key, &exact)) == NULL) + return (deleted ? RET_SUCCESS : RET_ERROR); + if (!exact) { + mpool_put(t->bt_mp, e->page, 0); + return (deleted ? RET_SUCCESS : RET_SPECIAL); + } + + /* + * Delete forward, then delete backward, from the found key. If + * there are duplicates and we reach either side of the page, do + * the key search again, so that we get them all. + */ + redo = 0; + h = e->page; + do { + if (__bt_dleaf(t, key, h, (u_int)e->index)) { + mpool_put(t->bt_mp, h, 0); + return (RET_ERROR); + } + if (F_ISSET(t, B_NODUPS)) { + if (NEXTINDEX(h) == 0) { + if (__bt_pdelete(t, h)) + return (RET_ERROR); + } else + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + return (RET_SUCCESS); + } + deleted = 1; + } while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0); + + /* Check for right-hand edge of the page. */ + if (e->index == NEXTINDEX(h)) + redo = 1; + + /* Delete from the key to the beginning of the page. */ + while (e->index-- > 0) { + if (__bt_cmp(t, key, e) != 0) + break; + if (__bt_dleaf(t, key, h, (u_int)e->index) == RET_ERROR) { + mpool_put(t->bt_mp, h, 0); + return (RET_ERROR); + } + if (e->index == 0) + redo = 1; + } + + /* Check for an empty page. */ + if (NEXTINDEX(h) == 0) { + if (__bt_pdelete(t, h)) + return (RET_ERROR); + goto loop; + } + + /* Put the page. */ + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + + if (redo) + goto loop; + return (RET_SUCCESS); +} + +/* + * __bt_pdelete -- + * Delete a single page from the tree. + * + * Parameters: + * t: tree + * h: leaf page + * + * Returns: + * RET_SUCCESS, RET_ERROR. + * + * Side-effects: + * mpool_put's the page + */ +static int +__bt_pdelete(BTREE *t, PAGE *h) +{ + BINTERNAL *bi; + PAGE *pg; + EPGNO *parent; + indx_t cnt, idx, *ip, offset; + uint32_t nksize; + char *from; + + /* + * Walk the parent page stack -- a LIFO stack of the pages that were + * traversed when we searched for the page where the delete occurred. + * Each stack entry is a page number and a page index offset. The + * offset is for the page traversed on the search. We've just deleted + * a page, so we have to delete the key from the parent page. + * + * If the delete from the parent page makes it empty, this process may + * continue all the way up the tree. We stop if we reach the root page + * (which is never deleted, it's just not worth the effort) or if the + * delete does not empty the page. + */ + while ((parent = BT_POP(t)) != NULL) { + /* Get the parent page. */ + if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) + return (RET_ERROR); + + idx = parent->index; + bi = GETBINTERNAL(pg, idx); + + /* Free any overflow pages. */ + if (bi->flags & P_BIGKEY && + __ovfl_delete(t, bi->bytes) == RET_ERROR) { + mpool_put(t->bt_mp, pg, 0); + return (RET_ERROR); + } + + /* + * Free the parent if it has only the one key and it's not the + * root page. If it's the rootpage, turn it back into an empty + * leaf page. + */ + if (NEXTINDEX(pg) == 1) { + if (pg->pgno == P_ROOT) { + pg->lower = BTDATAOFF; + pg->upper = t->bt_psize; + pg->flags = P_BLEAF; + } else { + if (__bt_relink(t, pg) || __bt_free(t, pg)) + return (RET_ERROR); + continue; + } + } else { + /* Pack remaining key items at the end of the page. */ + nksize = NBINTERNAL(bi->ksize); + from = (char *)(void *)pg + pg->upper; + memmove(from + nksize, from, + (size_t)((char *)(void *)bi - from)); + pg->upper += nksize; + + /* Adjust indices' offsets, shift the indices down. */ + offset = pg->linp[idx]; + for (cnt = idx, ip = &pg->linp[0]; cnt--; ++ip) + if (ip[0] < offset) + ip[0] += nksize; + for (cnt = NEXTINDEX(pg) - idx; --cnt; ++ip) + ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1]; + pg->lower -= sizeof(indx_t); + } + + mpool_put(t->bt_mp, pg, MPOOL_DIRTY); + break; + } + + /* Free the leaf page, as long as it wasn't the root. */ + if (h->pgno == P_ROOT) { + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + return (RET_SUCCESS); + } + return (__bt_relink(t, h) || __bt_free(t, h)); +} + +/* + * __bt_dleaf -- + * Delete a single record from a leaf page. + * + * Parameters: + * t: tree + * key: referenced key + * h: page + * idx: index on page to delete + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +int +__bt_dleaf(BTREE *t, const DBT *key, PAGE *h, u_int idx) +{ + BLEAF *bl; + indx_t cnt, *ip, offset; + uint32_t nbytes; + void *to; + char *from; + + /* If this record is referenced by the cursor, delete the cursor. */ + if (F_ISSET(&t->bt_cursor, CURS_INIT) && + !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && + t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == idx && + __bt_curdel(t, key, h, idx)) + return (RET_ERROR); + + /* If the entry uses overflow pages, make them available for reuse. */ + to = bl = GETBLEAF(h, idx); + if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR) + return (RET_ERROR); + if (bl->flags & P_BIGDATA && + __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR) + return (RET_ERROR); + + /* Pack the remaining key/data items at the end of the page. */ + nbytes = NBLEAF(bl); + from = (char *)(void *)h + h->upper; + memmove(from + nbytes, from, (size_t)((char *)(void *)to - from)); + h->upper += nbytes; + + /* Adjust the indices' offsets, shift the indices down. */ + offset = h->linp[idx]; + for (cnt = idx, ip = &h->linp[0]; cnt--; ++ip) + if (ip[0] < offset) + ip[0] += nbytes; + for (cnt = NEXTINDEX(h) - idx; --cnt; ++ip) + ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1]; + h->lower -= sizeof(indx_t); + + /* If the cursor is on this page, adjust it as necessary. */ + if (F_ISSET(&t->bt_cursor, CURS_INIT) && + !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && + t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > idx) + --t->bt_cursor.pg.index; + + return (RET_SUCCESS); +} + +/* + * __bt_curdel -- + * Delete the cursor. + * + * Parameters: + * t: tree + * key: referenced key (or NULL) + * h: page + * idx: index on page to delete + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +static int +__bt_curdel(BTREE *t, const DBT *key, PAGE *h, u_int idx) +{ + CURSOR *c; + EPG e; + PAGE *pg; + int curcopy, status; + + /* + * If there are duplicates, move forward or backward to one. + * Otherwise, copy the key into the cursor area. + */ + c = &t->bt_cursor; + F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE); + + curcopy = 0; + if (!F_ISSET(t, B_NODUPS)) { + /* + * We're going to have to do comparisons. If we weren't + * provided a copy of the key, i.e. the user is deleting + * the current cursor position, get one. + */ + if (key == NULL) { + e.page = h; + e.index = idx; + if ((status = __bt_ret(t, &e, + &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS) + return (status); + curcopy = 1; + key = &c->key; + } + /* Check previous key, if not at the beginning of the page. */ + if (idx > 0) { + e.page = h; + e.index = idx - 1; + if (__bt_cmp(t, key, &e) == 0) { + F_SET(c, CURS_BEFORE); + goto dup2; + } + } + /* Check next key, if not at the end of the page. */ + if (idx < (unsigned)(NEXTINDEX(h) - 1)) { + e.page = h; + e.index = idx + 1; + if (__bt_cmp(t, key, &e) == 0) { + F_SET(c, CURS_AFTER); + goto dup2; + } + } + /* Check previous key if at the beginning of the page. */ + if (idx == 0 && h->prevpg != P_INVALID) { + if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) + return (RET_ERROR); + e.page = pg; + e.index = NEXTINDEX(pg) - 1; + if (__bt_cmp(t, key, &e) == 0) { + F_SET(c, CURS_BEFORE); + goto dup1; + } + mpool_put(t->bt_mp, pg, 0); + } + /* Check next key if at the end of the page. */ + if (idx == (unsigned)(NEXTINDEX(h) - 1) && h->nextpg != P_INVALID) { + if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) + return (RET_ERROR); + e.page = pg; + e.index = 0; + if (__bt_cmp(t, key, &e) == 0) { + F_SET(c, CURS_AFTER); +dup1: mpool_put(t->bt_mp, pg, 0); +dup2: c->pg.pgno = e.page->pgno; + c->pg.index = e.index; + return (RET_SUCCESS); + } + mpool_put(t->bt_mp, pg, 0); + } + } + e.page = h; + e.index = idx; + if (curcopy || (status = + __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) { + F_SET(c, CURS_ACQUIRE); + return (RET_SUCCESS); + } + return (status); +} + +/* + * __bt_relink -- + * Link around a deleted page. + * + * Parameters: + * t: tree + * h: page to be deleted + */ +static int +__bt_relink(BTREE *t, PAGE *h) +{ + PAGE *pg; + + if (h->nextpg != P_INVALID) { + if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) + return (RET_ERROR); + pg->prevpg = h->prevpg; + mpool_put(t->bt_mp, pg, MPOOL_DIRTY); + } + if (h->prevpg != P_INVALID) { + if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) + return (RET_ERROR); + pg->nextpg = h->nextpg; + mpool_put(t->bt_mp, pg, MPOOL_DIRTY); + } + return (0); +} diff --git a/lib/nbsd_libc/db/btree/bt_get.c b/lib/nbsd_libc/db/btree/bt_get.c new file mode 100644 index 000000000..772210a7b --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_get.c @@ -0,0 +1,104 @@ +/* $NetBSD: bt_get.c,v 1.13 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_get.c,v 1.13 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#include +#include "btree.h" + +/* + * __BT_GET -- Get a record from the btree. + * + * Parameters: + * dbp: pointer to access method + * key: key to find + * data: data to return + * flag: currently unused + * + * Returns: + * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. + */ +int +__bt_get(const DB *dbp, const DBT *key, DBT *data, u_int flags) +{ + BTREE *t; + EPG *e; + int exact, status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* Get currently doesn't take any flags. */ + if (flags) { + errno = EINVAL; + return (RET_ERROR); + } + + if ((e = __bt_search(t, key, &exact)) == NULL) + return (RET_ERROR); + if (!exact) { + mpool_put(t->bt_mp, e->page, 0); + return (RET_SPECIAL); + } + + status = __bt_ret(t, e, NULL, NULL, data, &t->bt_rdata, 0); + + /* + * If the user is doing concurrent access, we copied the + * key/data, toss the page. + */ + if (F_ISSET(t, B_DB_LOCK)) + mpool_put(t->bt_mp, e->page, 0); + else + t->bt_pinned = e->page; + return (status); +} diff --git a/lib/nbsd_libc/db/btree/bt_open.c b/lib/nbsd_libc/db/btree/bt_open.c new file mode 100644 index 000000000..c5256d171 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_open.c @@ -0,0 +1,456 @@ +/* $NetBSD: bt_open.c,v 1.24 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_open.c,v 1.24 2008/09/11 12:58:00 joerg Exp $"); + +/* + * Implementation of btree access method for 4.4BSD. + * + * The design here was originally based on that of the btree access method + * used in the Postgres database system at UC Berkeley. This implementation + * is wholly independent of the Postgres code. + */ + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "btree.h" + +#ifdef DEBUG +#undef MINPSIZE +#define MINPSIZE 128 +#endif + +static int byteorder(void); +static int nroot(BTREE *); +static int tmp(void); + +/* + * __BT_OPEN -- Open a btree. + * + * Creates and fills a DB struct, and calls the routine that actually + * opens the btree. + * + * Parameters: + * fname: filename (NULL for in-memory trees) + * flags: open flag bits + * mode: open permission bits + * b: BTREEINFO pointer + * + * Returns: + * NULL on failure, pointer to DB on success. + * + */ +DB * +__bt_open(const char *fname, int flags, mode_t mode, const BTREEINFO *openinfo, + int dflags) +{ + struct stat sb; + BTMETA m; + BTREE *t; + BTREEINFO b; + DB *dbp; + pgno_t ncache; + ssize_t nr; + size_t temp; + int machine_lorder; + + t = NULL; + + /* + * Intention is to make sure all of the user's selections are okay + * here and then use them without checking. Can't be complete, since + * we don't know the right page size, lorder or flags until the backing + * file is opened. Also, the file's page size can cause the cachesize + * to change. + */ + machine_lorder = byteorder(); + if (openinfo) { + b = *openinfo; + + /* Flags: R_DUP. */ + if (b.flags & ~(R_DUP)) + goto einval; + + /* + * Page size must be indx_t aligned and >= MINPSIZE. Default + * page size is set farther on, based on the underlying file + * transfer size. + */ + if (b.psize && + (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 || + b.psize & (sizeof(indx_t) - 1))) + goto einval; + + /* Minimum number of keys per page; absolute minimum is 2. */ + if (b.minkeypage) { + if (b.minkeypage < 2) + goto einval; + } else + b.minkeypage = DEFMINKEYPAGE; + + /* If no comparison, use default comparison and prefix. */ + if (b.compare == NULL) { + b.compare = __bt_defcmp; + if (b.prefix == NULL) + b.prefix = __bt_defpfx; + } + + if (b.lorder == 0) + b.lorder = machine_lorder; + } else { + b.compare = __bt_defcmp; + b.cachesize = 0; + b.flags = 0; + b.lorder = machine_lorder; + b.minkeypage = DEFMINKEYPAGE; + b.prefix = __bt_defpfx; + b.psize = 0; + } + + /* Check for the ubiquitous PDP-11. */ + if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) + goto einval; + + /* Allocate and initialize DB and BTREE structures. */ + if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL) + goto err; + memset(t, 0, sizeof(BTREE)); + t->bt_fd = -1; /* Don't close unopened fd on error. */ + t->bt_lorder = b.lorder; + t->bt_order = NOT; + t->bt_cmp = b.compare; + t->bt_pfx = b.prefix; + t->bt_rfd = -1; + + if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL) + goto err; + memset(t->bt_dbp, 0, sizeof(DB)); + if (t->bt_lorder != machine_lorder) + F_SET(t, B_NEEDSWAP); + + dbp->type = DB_BTREE; + dbp->internal = t; + dbp->close = __bt_close; + dbp->del = __bt_delete; + dbp->fd = __bt_fd; + dbp->get = __bt_get; + dbp->put = __bt_put; + dbp->seq = __bt_seq; + dbp->sync = __bt_sync; + + /* + * If no file name was supplied, this is an in-memory btree and we + * open a backing temporary file. Otherwise, it's a disk-based tree. + */ + if (fname) { + switch (flags & O_ACCMODE) { + case O_RDONLY: + F_SET(t, B_RDONLY); + break; + case O_RDWR: + break; + case O_WRONLY: + default: + goto einval; + } + + if ((t->bt_fd = open(fname, flags, mode)) == -1) + goto err; + if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1) + goto err; + } else { + if ((flags & O_ACCMODE) != O_RDWR) + goto einval; + if ((t->bt_fd = tmp()) == -1) + goto err; + F_SET(t, B_INMEM); + } + + if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1) + goto err; + + if (fstat(t->bt_fd, &sb)) + goto err; + if (sb.st_size) { + if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0) + goto err; + if (nr != sizeof(BTMETA)) + goto eftype; + + /* + * Read in the meta-data. This can change the notion of what + * the lorder, page size and flags are, and, when the page size + * changes, the cachesize value can change too. If the user + * specified the wrong byte order for an existing database, we + * don't bother to return an error, we just clear the NEEDSWAP + * bit. + */ + if (m.magic == BTREEMAGIC) + F_CLR(t, B_NEEDSWAP); + else { + F_SET(t, B_NEEDSWAP); + M_32_SWAP(m.magic); + M_32_SWAP(m.version); + M_32_SWAP(m.psize); + M_32_SWAP(m.free); + M_32_SWAP(m.nrecs); + M_32_SWAP(m.flags); + } + if (m.magic != BTREEMAGIC || m.version != BTREEVERSION) + goto eftype; + if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 || + m.psize & (sizeof(indx_t) - 1)) + goto eftype; + if (m.flags & ~SAVEMETA) + goto eftype; + b.psize = m.psize; + F_SET(t, m.flags); + t->bt_free = m.free; + t->bt_nrecs = m.nrecs; + } else { + /* + * Set the page size to the best value for I/O to this file. + * Don't overflow the page offset type. + */ + if (b.psize == 0) { + b.psize = sb.st_blksize; + if (b.psize < MINPSIZE) + b.psize = MINPSIZE; + if (b.psize > MAX_PAGE_OFFSET + 1) + b.psize = MAX_PAGE_OFFSET + 1; + } + + /* Set flag if duplicates permitted. */ + if (!(b.flags & R_DUP)) + F_SET(t, B_NODUPS); + + t->bt_free = P_INVALID; + t->bt_nrecs = 0; + F_SET(t, B_METADIRTY); + } + + t->bt_psize = b.psize; + + /* Set the cache size; must be a multiple of the page size. */ + if (b.cachesize && b.cachesize & (b.psize - 1)) + b.cachesize += (~b.cachesize & (b.psize - 1)) + 1; + if (b.cachesize < b.psize * MINCACHE) + b.cachesize = b.psize * MINCACHE; + + /* Calculate number of pages to cache. */ + ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; + + /* + * The btree data structure requires that at least two keys can fit on + * a page, but other than that there's no fixed requirement. The user + * specified a minimum number per page, and we translated that into the + * number of bytes a key/data pair can use before being placed on an + * overflow page. This calculation includes the page header, the size + * of the index referencing the leaf item and the size of the leaf item + * structure. Also, don't let the user specify a minkeypage such that + * a key/data pair won't fit even if both key and data are on overflow + * pages. + */ + temp = (t->bt_psize - BTDATAOFF) / b.minkeypage - + (sizeof(indx_t) + NBLEAFDBT(0, 0)); + _DBFIT(temp, indx_t); + t->bt_ovflsize = (indx_t)temp; + if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t)) + t->bt_ovflsize = + NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t); + + /* Initialize the buffer pool. */ + if ((t->bt_mp = + mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) + goto err; + if (!F_ISSET(t, B_INMEM)) + mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); + + /* Create a root page if new tree. */ + if (nroot(t) == RET_ERROR) + goto err; + + /* Global flags. */ + if (dflags & DB_LOCK) + F_SET(t, B_DB_LOCK); + if (dflags & DB_SHMEM) + F_SET(t, B_DB_SHMEM); + if (dflags & DB_TXN) + F_SET(t, B_DB_TXN); + + return (dbp); + +einval: errno = EINVAL; + goto err; + +eftype: errno = EFTYPE; + goto err; + +err: if (t) { + if (t->bt_dbp) + free(t->bt_dbp); + if (t->bt_fd != -1) + (void)close(t->bt_fd); + free(t); + } + return (NULL); +} + +/* + * NROOT -- Create the root of a new tree. + * + * Parameters: + * t: tree + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +static int +nroot(BTREE *t) +{ + PAGE *meta, *root; + pgno_t npg; + + if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { + mpool_put(t->bt_mp, meta, 0); + return (RET_SUCCESS); + } + if (errno != EINVAL) /* It's OK to not exist. */ + return (RET_ERROR); + errno = 0; + + if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) + return (RET_ERROR); + + if ((root = mpool_new(t->bt_mp, &npg)) == NULL) + return (RET_ERROR); + + if (npg != P_ROOT) + return (RET_ERROR); + root->pgno = npg; + root->prevpg = root->nextpg = P_INVALID; + root->lower = BTDATAOFF; + root->upper = t->bt_psize; + root->flags = P_BLEAF; + memset(meta, 0, t->bt_psize); + mpool_put(t->bt_mp, meta, MPOOL_DIRTY); + mpool_put(t->bt_mp, root, MPOOL_DIRTY); + return (RET_SUCCESS); +} + +static int +tmp(void) +{ + sigset_t set, oset; + size_t len; + int fd; + char *envtmp; + char path[PATH_MAX]; + + if (issetugid()) + envtmp = NULL; + else + envtmp = getenv("TMPDIR"); + + len = snprintf(path, + sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : _PATH_TMP); + if (len >= sizeof(path)) + return -1; + + (void)sigfillset(&set); + (void)sigprocmask(SIG_BLOCK, &set, &oset); + if ((fd = mkstemp(path)) != -1) { + (void)unlink(path); + (void)fcntl(fd, F_SETFD, FD_CLOEXEC); + } + (void)sigprocmask(SIG_SETMASK, &oset, NULL); + return(fd); +} + +static int +byteorder(void) +{ + uint32_t x; + uint8_t *p; + + x = 0x01020304; + p = (uint8_t *)(void *)&x; + switch (*p) { + case 1: + return (BIG_ENDIAN); + case 4: + return (LITTLE_ENDIAN); + default: + return (0); + } +} + +int +__bt_fd(const DB *dbp) +{ + BTREE *t; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* In-memory database can't have a file descriptor. */ + if (F_ISSET(t, B_INMEM)) { + errno = ENOENT; + return (-1); + } + return (t->bt_fd); +} diff --git a/lib/nbsd_libc/db/btree/bt_overflow.c b/lib/nbsd_libc/db/btree/bt_overflow.c new file mode 100644 index 000000000..965480487 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_overflow.c @@ -0,0 +1,232 @@ +/* $NetBSD: bt_overflow.c,v 1.16 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_overflow.c,v 1.16 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#include +#include "btree.h" + +/* + * Big key/data code. + * + * Big key and data entries are stored on linked lists of pages. The initial + * reference is byte string stored with the key or data and is the page number + * and size. The actual record is stored in a chain of pages linked by the + * nextpg field of the PAGE header. + * + * The first page of the chain has a special property. If the record is used + * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set + * in the header. + * + * XXX + * A single DBT is written to each chain, so a lot of space on the last page + * is wasted. This is a fairly major bug for some data sets. + */ + +/* + * __OVFL_GET -- Get an overflow key/data item. + * + * Parameters: + * t: tree + * p: pointer to { pgno_t, uint32_t } + * buf: storage address + * bufsz: storage size + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz) +{ + PAGE *h; + pgno_t pg; + uint32_t sz, nb, plen; + size_t temp; + + memmove(&pg, p, sizeof(pgno_t)); + memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t)); + *ssz = sz; + +#ifdef DEBUG + if (pg == P_INVALID || sz == 0) + abort(); +#endif + /* Make the buffer bigger as necessary. */ + if (*bufsz < sz) { + *buf = (char *)(*buf == NULL ? malloc(sz) : realloc(*buf, sz)); + if (*buf == NULL) + return (RET_ERROR); + *bufsz = sz; + } + + /* + * Step through the linked list of pages, copying the data on each one + * into the buffer. Never copy more than the data's length. + */ + temp = t->bt_psize - BTDATAOFF; + _DBFIT(temp, uint32_t); + plen = (uint32_t)temp; + for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) { + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + + nb = MIN(sz, plen); + memmove(p, (char *)(void *)h + BTDATAOFF, nb); + mpool_put(t->bt_mp, h, 0); + + if ((sz -= nb) == 0) + break; + } + return (RET_SUCCESS); +} + +/* + * __OVFL_PUT -- Store an overflow key/data item. + * + * Parameters: + * t: tree + * data: DBT to store + * pgno: storage page number + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg) +{ + PAGE *h, *last; + void *p; + pgno_t npg; + uint32_t sz, nb, plen; + size_t temp; + + /* + * Allocate pages and copy the key/data record into them. Store the + * number of the first page in the chain. + */ + temp = t->bt_psize - BTDATAOFF; + _DBFIT(temp, uint32_t); + plen = (uint32_t)temp; + last = NULL; + p = dbt->data; + temp = dbt->size; + _DBFIT(temp, uint32_t); + sz = temp; + for (;; p = (char *)p + plen, last = h) { + if ((h = __bt_new(t, &npg)) == NULL) + return (RET_ERROR); + + h->pgno = npg; + h->nextpg = h->prevpg = P_INVALID; + h->flags = P_OVERFLOW; + h->lower = h->upper = 0; + + nb = MIN(sz, plen); + (void)memmove((char *)(void *)h + BTDATAOFF, p, (size_t)nb); + + if (last) { + last->nextpg = h->pgno; + mpool_put(t->bt_mp, last, MPOOL_DIRTY); + } else + *pg = h->pgno; + + if ((sz -= nb) == 0) { + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + break; + } + } + return (RET_SUCCESS); +} + +/* + * __OVFL_DELETE -- Delete an overflow chain. + * + * Parameters: + * t: tree + * p: pointer to { pgno_t, uint32_t } + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__ovfl_delete(BTREE *t, void *p) +{ + PAGE *h; + pgno_t pg; + uint32_t sz, plen; + size_t temp; + + (void)memmove(&pg, p, sizeof(pgno_t)); + (void)memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(uint32_t)); + +#ifdef DEBUG + if (pg == P_INVALID || sz == 0) + abort(); +#endif + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + + /* Don't delete chains used by internal pages. */ + if (h->flags & P_PRESERVE) { + mpool_put(t->bt_mp, h, 0); + return (RET_SUCCESS); + } + + /* Step through the chain, calling the free routine for each page. */ + temp = t->bt_psize - BTDATAOFF; + _DBFIT(temp, uint32_t); + plen = (uint32_t)temp; + for (;; sz -= plen) { + pg = h->nextpg; + __bt_free(t, h); + if (sz <= plen) + break; + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + } + return (RET_SUCCESS); +} diff --git a/lib/nbsd_libc/db/btree/bt_page.c b/lib/nbsd_libc/db/btree/bt_page.c new file mode 100644 index 000000000..0db4aeb8b --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_page.c @@ -0,0 +1,99 @@ +/* $NetBSD: bt_page.c,v 1.13 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_page.c,v 1.13 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include + +#include +#include "btree.h" + +/* + * __bt_free -- + * Put a page on the freelist. + * + * Parameters: + * t: tree + * h: page to free + * + * Returns: + * RET_ERROR, RET_SUCCESS + * + * Side-effect: + * mpool_put's the page. + */ +int +__bt_free(BTREE *t, PAGE *h) +{ + /* Insert the page at the head of the free list. */ + h->prevpg = P_INVALID; + h->nextpg = t->bt_free; + t->bt_free = h->pgno; + F_SET(t, B_METADIRTY); + + /* Make sure the page gets written back. */ + return (mpool_put(t->bt_mp, h, MPOOL_DIRTY)); +} + +/* + * __bt_new -- + * Get a new page, preferably from the freelist. + * + * Parameters: + * t: tree + * npg: storage for page number. + * + * Returns: + * Pointer to a page, NULL on error. + */ +PAGE * +__bt_new(BTREE *t, pgno_t *npg) +{ + PAGE *h; + + if (t->bt_free != P_INVALID && + (h = mpool_get(t->bt_mp, t->bt_free, 0)) != NULL) { + *npg = t->bt_free; + t->bt_free = h->nextpg; + F_SET(t, B_METADIRTY); + return (h); + } + return (mpool_new(t->bt_mp, npg)); +} diff --git a/lib/nbsd_libc/db/btree/bt_put.c b/lib/nbsd_libc/db/btree/bt_put.c new file mode 100644 index 000000000..37630b819 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_put.c @@ -0,0 +1,319 @@ +/* $NetBSD: bt_put.c,v 1.19 2009/02/12 06:40:14 lukem Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_put.c,v 1.19 2009/02/12 06:40:14 lukem Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include + +#include +#include "btree.h" + +static EPG *bt_fast(BTREE *, const DBT *, const DBT *, int *); + +/* + * __BT_PUT -- Add a btree item to the tree. + * + * Parameters: + * dbp: pointer to access method + * key: key + * data: data + * flag: R_NOOVERWRITE + * + * Returns: + * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the + * tree and R_NOOVERWRITE specified. + */ +int +__bt_put(const DB *dbp, DBT *key, const DBT *data, u_int flags) +{ + BTREE *t; + DBT tkey, tdata; + EPG *e = NULL; /* pacify gcc */ + PAGE *h; + indx_t idx, nxtindex; + pgno_t pg; + uint32_t nbytes, temp; + int dflags, exact, status; + char *dest, db[NOVFLSIZE], kb[NOVFLSIZE]; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* Check for change to a read-only tree. */ + if (F_ISSET(t, B_RDONLY)) { + errno = EPERM; + return (RET_ERROR); + } + + switch (flags) { + case 0: + case R_NOOVERWRITE: + break; + case R_CURSOR: + /* + * If flags is R_CURSOR, put the cursor. Must already + * have started a scan and not have already deleted it. + */ + if (F_ISSET(&t->bt_cursor, CURS_INIT) && + !F_ISSET(&t->bt_cursor, + CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) + break; + /* FALLTHROUGH */ + default: + errno = EINVAL; + return (RET_ERROR); + } + + /* + * If the key/data pair won't fit on a page, store it on overflow + * pages. Only put the key on the overflow page if the pair are + * still too big after moving the data to an overflow page. + * + * XXX + * If the insert fails later on, the overflow pages aren't recovered. + */ + dflags = 0; + if (key->size + data->size > t->bt_ovflsize) { + if (key->size > t->bt_ovflsize) { +storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR) + return (RET_ERROR); + tkey.data = kb; + tkey.size = NOVFLSIZE; + memmove(kb, &pg, sizeof(pgno_t)); + memmove(kb + sizeof(pgno_t), + &key->size, sizeof(uint32_t)); + dflags |= P_BIGKEY; + key = &tkey; + } + if (key->size + data->size > t->bt_ovflsize) { + if (__ovfl_put(t, data, &pg) == RET_ERROR) + return (RET_ERROR); + tdata.data = db; + tdata.size = NOVFLSIZE; + memmove(db, &pg, sizeof(pgno_t)); + _DBFIT(data->size, uint32_t); + temp = (uint32_t)data->size; + (void)memmove(db + sizeof(pgno_t), + &temp, sizeof(uint32_t)); + dflags |= P_BIGDATA; + data = &tdata; + } + if (key->size + data->size > t->bt_ovflsize) + goto storekey; + } + + /* Replace the cursor. */ + if (flags == R_CURSOR) { + if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL) + return (RET_ERROR); + idx = t->bt_cursor.pg.index; + goto delete; + } + + /* + * Find the key to delete, or, the location at which to insert. + * Bt_fast and __bt_search both pin the returned page. + */ + if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL) + if ((e = __bt_search(t, key, &exact)) == NULL) + return (RET_ERROR); + h = e->page; + idx = e->index; + + /* + * Add the key/data pair to the tree. If an identical key is already + * in the tree, and R_NOOVERWRITE is set, an error is returned. If + * R_NOOVERWRITE is not set, the key is either added (if duplicates are + * permitted) or an error is returned. + */ + switch (flags) { + case R_NOOVERWRITE: + if (!exact) + break; + mpool_put(t->bt_mp, h, 0); + return (RET_SPECIAL); + default: + if (!exact || !F_ISSET(t, B_NODUPS)) + break; + /* + * !!! + * Note, the delete may empty the page, so we need to put a + * new entry into the page immediately. + */ +delete: if (__bt_dleaf(t, key, h, (u_int)idx) == RET_ERROR) { + mpool_put(t->bt_mp, h, 0); + return (RET_ERROR); + } + break; + } + + /* + * If not enough room, or the user has put a ceiling on the number of + * keys permitted in the page, split the page. The split code will + * insert the key and data and unpin the current page. If inserting + * into the offset array, shift the pointers up. + */ + nbytes = NBLEAFDBT(key->size, data->size); + if ((uint32_t)h->upper - (uint32_t)h->lower < nbytes + sizeof(indx_t)) { + if ((status = __bt_split(t, h, key, + data, dflags, nbytes, (u_int)idx)) != RET_SUCCESS) + return (status); + goto success; + } + + if (idx < (nxtindex = NEXTINDEX(h))) + memmove(h->linp + idx + 1, h->linp + idx, + (nxtindex - idx) * sizeof(indx_t)); + h->lower += sizeof(indx_t); + + h->linp[idx] = h->upper -= nbytes; + dest = (char *)(void *)h + h->upper; + WR_BLEAF(dest, key, data, dflags); + + /* If the cursor is on this page, adjust it as necessary. */ + if (F_ISSET(&t->bt_cursor, CURS_INIT) && + !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && + t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx) + ++t->bt_cursor.pg.index; + + if (t->bt_order == NOT) { + if (h->nextpg == P_INVALID) { + if (idx == NEXTINDEX(h) - 1) { + t->bt_order = FORWARD; + t->bt_last.index = idx; + t->bt_last.pgno = h->pgno; + } + } else if (h->prevpg == P_INVALID) { + if (idx == 0) { + t->bt_order = BACK; + t->bt_last.index = 0; + t->bt_last.pgno = h->pgno; + } + } + } + + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + +success: + if (flags == R_SETCURSOR) + __bt_setcur(t, e->page->pgno, (u_int)e->index); + + F_SET(t, B_MODIFIED); + return (RET_SUCCESS); +} + +#ifdef STATISTICS +unsigned long bt_cache_hit, bt_cache_miss; +#endif + +/* + * BT_FAST -- Do a quick check for sorted data. + * + * Parameters: + * t: tree + * key: key to insert + * + * Returns: + * EPG for new record or NULL if not found. + */ +static EPG * +bt_fast(BTREE *t, const DBT *key, const DBT *data, int *exactp) +{ + PAGE *h; + uint32_t nbytes; + int cmp; + + if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) { + t->bt_order = NOT; + return (NULL); + } + t->bt_cur.page = h; + t->bt_cur.index = t->bt_last.index; + + /* + * If won't fit in this page or have too many keys in this page, + * have to search to get split stack. + */ + nbytes = NBLEAFDBT(key->size, data->size); + if ((uint32_t)h->upper - (uint32_t)h->lower < nbytes + sizeof(indx_t)) + goto miss; + + if (t->bt_order == FORWARD) { + if (t->bt_cur.page->nextpg != P_INVALID) + goto miss; + if (t->bt_cur.index != NEXTINDEX(h) - 1) + goto miss; + if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0) + goto miss; + t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index; + } else { + if (t->bt_cur.page->prevpg != P_INVALID) + goto miss; + if (t->bt_cur.index != 0) + goto miss; + if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0) + goto miss; + t->bt_last.index = 0; + } + *exactp = cmp == 0; +#ifdef STATISTICS + ++bt_cache_hit; +#endif + return (&t->bt_cur); + +miss: +#ifdef STATISTICS + ++bt_cache_miss; +#endif + t->bt_order = NOT; + mpool_put(t->bt_mp, h, 0); + return (NULL); +} diff --git a/lib/nbsd_libc/db/btree/bt_search.c b/lib/nbsd_libc/db/btree/bt_search.c new file mode 100644 index 000000000..00da7c9b6 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_search.c @@ -0,0 +1,205 @@ +/* $NetBSD: bt_search.c,v 1.17 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_search.c,v 1.17 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include + +#include +#include "btree.h" + +static int __bt_snext(BTREE *, PAGE *, const DBT *, int *); +static int __bt_sprev(BTREE *, PAGE *, const DBT *, int *); + +/* + * __bt_search -- + * Search a btree for a key. + * + * Parameters: + * t: tree to search + * key: key to find + * exactp: pointer to exact match flag + * + * Returns: + * The EPG for matching record, if any, or the EPG for the location + * of the key, if it were inserted into the tree, is entered into + * the bt_cur field of the tree. A pointer to the field is returned. + */ +EPG * +__bt_search(BTREE *t, const DBT *key, int *exactp) +{ + PAGE *h; + indx_t base, idx, lim; + pgno_t pg; + int cmp; + + BT_CLR(t); + for (pg = P_ROOT;;) { + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (NULL); + + /* Do a binary search on the current page. */ + t->bt_cur.page = h; + for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) { + t->bt_cur.index = idx = base + ((uint32_t)lim >> 1); + if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) { + if (h->flags & P_BLEAF) { + *exactp = 1; + return (&t->bt_cur); + } + goto next; + } + if (cmp > 0) { + base = idx + 1; + --lim; + } + } + + /* + * If it's a leaf page, we're almost done. If no duplicates + * are allowed, or we have an exact match, we're done. Else, + * it's possible that there were matching keys on this page, + * which later deleted, and we're on a page with no matches + * while there are matches on other pages. If at the start or + * end of a page, check the adjacent page. + */ + if (h->flags & P_BLEAF) { + if (!F_ISSET(t, B_NODUPS)) { + if (base == 0 && + h->prevpg != P_INVALID && + __bt_sprev(t, h, key, exactp)) + return (&t->bt_cur); + if (base == NEXTINDEX(h) && + h->nextpg != P_INVALID && + __bt_snext(t, h, key, exactp)) + return (&t->bt_cur); + } + *exactp = 0; + t->bt_cur.index = base; + return (&t->bt_cur); + } + + /* + * No match found. Base is the smallest index greater than + * key and may be zero or a last + 1 index. If it's non-zero, + * decrement by one, and record the internal page which should + * be a parent page for the key. If a split later occurs, the + * inserted page will be to the right of the saved page. + */ + idx = base ? base - 1 : base; + +next: BT_PUSH(t, h->pgno, idx); + pg = GETBINTERNAL(h, idx)->pgno; + mpool_put(t->bt_mp, h, 0); + } +} + +/* + * __bt_snext -- + * Check for an exact match after the key. + * + * Parameters: + * t: tree + * h: current page + * key: key + * exactp: pointer to exact match flag + * + * Returns: + * If an exact match found. + */ +static int +__bt_snext(BTREE *t, PAGE *h, const DBT *key, int *exactp) +{ + EPG e; + + /* + * Get the next page. The key is either an exact + * match, or not as good as the one we already have. + */ + if ((e.page = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) + return (0); + e.index = 0; + if (__bt_cmp(t, key, &e) == 0) { + mpool_put(t->bt_mp, h, 0); + t->bt_cur = e; + *exactp = 1; + return (1); + } + mpool_put(t->bt_mp, e.page, 0); + return (0); +} + +/* + * __bt_sprev -- + * Check for an exact match before the key. + * + * Parameters: + * t: tree + * h: current page + * key: key + * exactp: pointer to exact match flag + * + * Returns: + * If an exact match found. + */ +static int +__bt_sprev(BTREE *t, PAGE *h, const DBT *key, int *exactp) +{ + EPG e; + + /* + * Get the previous page. The key is either an exact + * match, or not as good as the one we already have. + */ + if ((e.page = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) + return (0); + e.index = NEXTINDEX(e.page) - 1; + if (__bt_cmp(t, key, &e) == 0) { + mpool_put(t->bt_mp, h, 0); + t->bt_cur = e; + *exactp = 1; + return (1); + } + mpool_put(t->bt_mp, e.page, 0); + return (0); +} diff --git a/lib/nbsd_libc/db/btree/bt_seq.c b/lib/nbsd_libc/db/btree/bt_seq.c new file mode 100644 index 000000000..58f25a065 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_seq.c @@ -0,0 +1,442 @@ +/* $NetBSD: bt_seq.c,v 1.17 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_seq.c,v 1.17 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include + +#include +#include "btree.h" + +static int __bt_first(BTREE *, const DBT *, EPG *, int *); +static int __bt_seqadv(BTREE *, EPG *, int); +static int __bt_seqset(BTREE *, EPG *, DBT *, int); + +/* + * Sequential scan support. + * + * The tree can be scanned sequentially, starting from either end of the + * tree or from any specific key. A scan request before any scanning is + * done is initialized as starting from the least node. + */ + +/* + * __bt_seq -- + * Btree sequential scan interface. + * + * Parameters: + * dbp: pointer to access method + * key: key for positioning and return value + * data: data return value + * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV. + * + * Returns: + * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key. + */ +int +__bt_seq(const DB *dbp, DBT *key, DBT *data, u_int flags) +{ + BTREE *t; + EPG e; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* + * If scan unitialized as yet, or starting at a specific record, set + * the scan to a specific key. Both __bt_seqset and __bt_seqadv pin + * the page the cursor references if they're successful. + */ + switch (flags) { + case R_NEXT: + case R_PREV: + if (F_ISSET(&t->bt_cursor, CURS_INIT)) { + status = __bt_seqadv(t, &e, (int)flags); + break; + } + /* FALLTHROUGH */ + case R_FIRST: + case R_LAST: + case R_CURSOR: + status = __bt_seqset(t, &e, key, (int)flags); + break; + default: + errno = EINVAL; + return (RET_ERROR); + } + + if (status == RET_SUCCESS) { + __bt_setcur(t, e.page->pgno, (u_int)e.index); + + status = + __bt_ret(t, &e, key, &t->bt_rkey, data, &t->bt_rdata, 0); + + /* + * If the user is doing concurrent access, we copied the + * key/data, toss the page. + */ + if (F_ISSET(t, B_DB_LOCK)) + mpool_put(t->bt_mp, e.page, 0); + else + t->bt_pinned = e.page; + } + return (status); +} + +/* + * __bt_seqset -- + * Set the sequential scan to a specific key. + * + * Parameters: + * t: tree + * ep: storage for returned key + * key: key for initial scan position + * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV + * + * Side effects: + * Pins the page the cursor references. + * + * Returns: + * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key. + */ +static int +__bt_seqset(BTREE *t, EPG *ep, DBT *key, int flags) +{ + PAGE *h; + pgno_t pg; + int exact; + + /* + * Find the first, last or specific key in the tree and point the + * cursor at it. The cursor may not be moved until a new key has + * been found. + */ + switch (flags) { + case R_CURSOR: /* Keyed scan. */ + /* + * Find the first instance of the key or the smallest key + * which is greater than or equal to the specified key. + */ + if (key->data == NULL || key->size == 0) { + errno = EINVAL; + return (RET_ERROR); + } + return (__bt_first(t, key, ep, &exact)); + case R_FIRST: /* First record. */ + case R_NEXT: + /* Walk down the left-hand side of the tree. */ + for (pg = P_ROOT;;) { + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + + /* Check for an empty tree. */ + if (NEXTINDEX(h) == 0) { + mpool_put(t->bt_mp, h, 0); + return (RET_SPECIAL); + } + + if (h->flags & (P_BLEAF | P_RLEAF)) + break; + pg = GETBINTERNAL(h, 0)->pgno; + mpool_put(t->bt_mp, h, 0); + } + ep->page = h; + ep->index = 0; + break; + case R_LAST: /* Last record. */ + case R_PREV: + /* Walk down the right-hand side of the tree. */ + for (pg = P_ROOT;;) { + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + + /* Check for an empty tree. */ + if (NEXTINDEX(h) == 0) { + mpool_put(t->bt_mp, h, 0); + return (RET_SPECIAL); + } + + if (h->flags & (P_BLEAF | P_RLEAF)) + break; + pg = GETBINTERNAL(h, NEXTINDEX(h) - 1)->pgno; + mpool_put(t->bt_mp, h, 0); + } + + ep->page = h; + ep->index = NEXTINDEX(h) - 1; + break; + } + return (RET_SUCCESS); +} + +/* + * __bt_seqadvance -- + * Advance the sequential scan. + * + * Parameters: + * t: tree + * flags: R_NEXT, R_PREV + * + * Side effects: + * Pins the page the new key/data record is on. + * + * Returns: + * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key. + */ +static int +__bt_seqadv(BTREE *t, EPG *ep, int flags) +{ + CURSOR *c; + PAGE *h; + indx_t idx = 0; /* pacify gcc */ + pgno_t pg; + int exact; + + /* + * There are a couple of states that we can be in. The cursor has + * been initialized by the time we get here, but that's all we know. + */ + c = &t->bt_cursor; + + /* + * The cursor was deleted where there weren't any duplicate records, + * so the key was saved. Find out where that key would go in the + * current tree. It doesn't matter if the returned key is an exact + * match or not -- if it's an exact match, the record was added after + * the delete so we can just return it. If not, as long as there's + * a record there, return it. + */ + if (F_ISSET(c, CURS_ACQUIRE)) + return (__bt_first(t, &c->key, ep, &exact)); + + /* Get the page referenced by the cursor. */ + if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL) + return (RET_ERROR); + + /* + * Find the next/previous record in the tree and point the cursor at + * it. The cursor may not be moved until a new key has been found. + */ + switch (flags) { + case R_NEXT: /* Next record. */ + /* + * The cursor was deleted in duplicate records, and moved + * forward to a record that has yet to be returned. Clear + * that flag, and return the record. + */ + if (F_ISSET(c, CURS_AFTER)) + goto usecurrent; + idx = c->pg.index; + if (++idx == NEXTINDEX(h)) { + pg = h->nextpg; + mpool_put(t->bt_mp, h, 0); + if (pg == P_INVALID) + return (RET_SPECIAL); + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + idx = 0; + } + break; + case R_PREV: /* Previous record. */ + /* + * The cursor was deleted in duplicate records, and moved + * backward to a record that has yet to be returned. Clear + * that flag, and return the record. + */ + if (F_ISSET(c, CURS_BEFORE)) { +usecurrent: F_CLR(c, CURS_AFTER | CURS_BEFORE); + ep->page = h; + ep->index = c->pg.index; + return (RET_SUCCESS); + } + idx = c->pg.index; + if (idx == 0) { + pg = h->prevpg; + mpool_put(t->bt_mp, h, 0); + if (pg == P_INVALID) + return (RET_SPECIAL); + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + idx = NEXTINDEX(h) - 1; + } else + --idx; + break; + } + + ep->page = h; + ep->index = idx; + return (RET_SUCCESS); +} + +/* + * __bt_first -- + * Find the first entry. + * + * Parameters: + * t: the tree + * key: the key + * erval: return EPG + * exactp: pointer to exact match flag + * + * Returns: + * The first entry in the tree greater than or equal to key, + * or RET_SPECIAL if no such key exists. + */ +static int +__bt_first(BTREE *t, const DBT *key, EPG *erval, int *exactp) +{ + PAGE *h; + EPG *ep, save; + pgno_t pg; + + /* + * Find any matching record; __bt_search pins the page. + * + * If it's an exact match and duplicates are possible, walk backwards + * in the tree until we find the first one. Otherwise, make sure it's + * a valid key (__bt_search may return an index just past the end of a + * page) and return it. + */ + if ((ep = __bt_search(t, key, exactp)) == NULL) + return (0); + if (*exactp) { + if (F_ISSET(t, B_NODUPS)) { + *erval = *ep; + return (RET_SUCCESS); + } + + /* + * Walk backwards, as long as the entry matches and there are + * keys left in the tree. Save a copy of each match in case + * we go too far. + */ + save = *ep; + h = ep->page; + do { + if (save.page->pgno != ep->page->pgno) { + mpool_put(t->bt_mp, save.page, 0); + save = *ep; + } else + save.index = ep->index; + + /* + * Don't unpin the page the last (or original) match + * was on, but make sure it's unpinned if an error + * occurs. + */ + if (ep->index == 0) { + if (h->prevpg == P_INVALID) + break; + if (h->pgno != save.page->pgno) + mpool_put(t->bt_mp, h, 0); + if ((h = mpool_get(t->bt_mp, + h->prevpg, 0)) == NULL) + return (RET_ERROR); + ep->page = h; + ep->index = NEXTINDEX(h); + } + --ep->index; + } while (__bt_cmp(t, key, ep) == 0); + + /* + * Reach here with the last page that was looked at pinned, + * which may or may not be the same as the last (or original) + * match page. If it's not useful, release it. + */ + if (h->pgno != save.page->pgno) + mpool_put(t->bt_mp, h, 0); + + *erval = save; + return (RET_SUCCESS); + } + + /* If at the end of a page, find the next entry. */ + if (ep->index == NEXTINDEX(ep->page)) { + h = ep->page; + pg = h->nextpg; + mpool_put(t->bt_mp, h, 0); + if (pg == P_INVALID) + return (RET_SPECIAL); + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + ep->index = 0; + ep->page = h; + } + *erval = *ep; + return (RET_SUCCESS); +} + +/* + * __bt_setcur -- + * Set the cursor to an entry in the tree. + * + * Parameters: + * t: the tree + * pgno: page number + * idx: page index + */ +void +__bt_setcur(BTREE *t, pgno_t pgno, u_int idx) +{ + /* Lose any already deleted key. */ + if (t->bt_cursor.key.data != NULL) { + free(t->bt_cursor.key.data); + t->bt_cursor.key.size = 0; + t->bt_cursor.key.data = NULL; + } + F_CLR(&t->bt_cursor, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE); + + /* Update the cursor. */ + t->bt_cursor.pg.pgno = pgno; + t->bt_cursor.pg.index = idx; + F_SET(&t->bt_cursor, CURS_INIT); +} diff --git a/lib/nbsd_libc/db/btree/bt_split.c b/lib/nbsd_libc/db/btree/bt_split.c new file mode 100644 index 000000000..8a97501b5 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_split.c @@ -0,0 +1,827 @@ +/* $NetBSD: bt_split.c,v 1.19 2009/04/22 18:44:06 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_split.c,v 1.19 2009/04/22 18:44:06 christos Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include + +#include +#include "btree.h" + +static int bt_broot(BTREE *, PAGE *, PAGE *, PAGE *); +static PAGE *bt_page(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t); +static int bt_preserve(BTREE *, pgno_t); +static PAGE *bt_psplit(BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t); +static PAGE *bt_root(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t); +static int bt_rroot(BTREE *, PAGE *, PAGE *, PAGE *); +static recno_t rec_total(PAGE *); + +#ifdef STATISTICS +unsigned long bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved; +#endif + +/* + * __BT_SPLIT -- Split the tree. + * + * Parameters: + * t: tree + * sp: page to split + * key: key to insert + * data: data to insert + * flags: BIGKEY/BIGDATA flags + * ilen: insert length + * skip: index to leave open + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags, + size_t ilen, uint32_t argskip) +{ + BINTERNAL *bi = NULL; /* pacify gcc */ + BLEAF *bl = NULL, *tbl; /* pacify gcc */ + DBT a, b; + EPGNO *parent; + PAGE *h, *l, *r, *lchild, *rchild; + indx_t nxtindex; + uint16_t skip; + uint32_t n, nbytes, nksize = 0; /* pacify gcc */ + int parentsplit; + char *dest; + + /* + * Split the page into two pages, l and r. The split routines return + * a pointer to the page into which the key should be inserted and with + * skip set to the offset which should be used. Additionally, l and r + * are pinned. + */ + skip = argskip; + h = sp->pgno == P_ROOT ? + bt_root(t, sp, &l, &r, &skip, ilen) : + bt_page(t, sp, &l, &r, &skip, ilen); + if (h == NULL) + return (RET_ERROR); + + /* + * Insert the new key/data pair into the leaf page. (Key inserts + * always cause a leaf page to split first.) + */ + _DBFIT(ilen, indx_t); + h->upper -= (indx_t)ilen; + h->linp[skip] = h->upper; + dest = (char *)(void *)h + h->upper; + if (F_ISSET(t, R_RECNO)) + WR_RLEAF(dest, data, flags); + else + WR_BLEAF(dest, key, data, flags); + + /* If the root page was split, make it look right. */ + if (sp->pgno == P_ROOT && + (F_ISSET(t, R_RECNO) ? + bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR) + goto err2; + + /* + * Now we walk the parent page stack -- a LIFO stack of the pages that + * were traversed when we searched for the page that split. Each stack + * entry is a page number and a page index offset. The offset is for + * the page traversed on the search. We've just split a page, so we + * have to insert a new key into the parent page. + * + * If the insert into the parent page causes it to split, may have to + * continue splitting all the way up the tree. We stop if the root + * splits or the page inserted into didn't have to split to hold the + * new key. Some algorithms replace the key for the old page as well + * as the new page. We don't, as there's no reason to believe that the + * first key on the old page is any better than the key we have, and, + * in the case of a key being placed at index 0 causing the split, the + * key is unavailable. + * + * There are a maximum of 5 pages pinned at any time. We keep the left + * and right pages pinned while working on the parent. The 5 are the + * two children, left parent and right parent (when the parent splits) + * and the root page or the overflow key page when calling bt_preserve. + * This code must make sure that all pins are released other than the + * root page or overflow page which is unlocked elsewhere. + */ + while ((parent = BT_POP(t)) != NULL) { + lchild = l; + rchild = r; + + /* Get the parent page. */ + if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) + goto err2; + + /* + * The new key goes ONE AFTER the index, because the split + * was to the right. + */ + skip = parent->index + 1; + + /* + * Calculate the space needed on the parent page. + * + * Prefix trees: space hack when inserting into BINTERNAL + * pages. Retain only what's needed to distinguish between + * the new entry and the LAST entry on the page to its left. + * If the keys compare equal, retain the entire key. Note, + * we don't touch overflow keys, and the entire key must be + * retained for the next-to-left most key on the leftmost + * page of each level, or the search will fail. Applicable + * ONLY to internal pages that have leaf pages as children. + * Further reduction of the key between pairs of internal + * pages loses too much information. + */ + switch (rchild->flags & P_TYPE) { + case P_BINTERNAL: + bi = GETBINTERNAL(rchild, 0); + nbytes = NBINTERNAL(bi->ksize); + break; + case P_BLEAF: + bl = GETBLEAF(rchild, 0); + nbytes = NBINTERNAL(bl->ksize); + if (t->bt_pfx && !(bl->flags & P_BIGKEY) && + (h->prevpg != P_INVALID || skip > 1)) { + size_t temp; + tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1); + a.size = tbl->ksize; + a.data = tbl->bytes; + b.size = bl->ksize; + b.data = bl->bytes; + temp = t->bt_pfx(&a, &b); + _DBFIT(temp, uint32_t); + nksize = (uint32_t)temp; + n = NBINTERNAL(nksize); + if (n < nbytes) { +#ifdef STATISTICS + bt_pfxsaved += nbytes - n; +#endif + nbytes = n; + } else + nksize = 0; + } else + nksize = 0; + break; + case P_RINTERNAL: + case P_RLEAF: + nbytes = NRINTERNAL; + break; + default: + abort(); + } + + /* Split the parent page if necessary or shift the indices. */ + if ((uint32_t)h->upper - (uint32_t)h->lower < nbytes + sizeof(indx_t)) { + sp = h; + h = h->pgno == P_ROOT ? + bt_root(t, h, &l, &r, &skip, nbytes) : + bt_page(t, h, &l, &r, &skip, nbytes); + if (h == NULL) + goto err1; + parentsplit = 1; + } else { + if (skip < (nxtindex = NEXTINDEX(h))) + memmove(h->linp + skip + 1, h->linp + skip, + (nxtindex - skip) * sizeof(indx_t)); + h->lower += sizeof(indx_t); + parentsplit = 0; + } + + /* Insert the key into the parent page. */ + switch (rchild->flags & P_TYPE) { + case P_BINTERNAL: + h->linp[skip] = h->upper -= nbytes; + dest = (char *)(void *)h + h->linp[skip]; + memmove(dest, bi, nbytes); + ((BINTERNAL *)(void *)dest)->pgno = rchild->pgno; + break; + case P_BLEAF: + h->linp[skip] = h->upper -= nbytes; + dest = (char *)(void *)h + h->linp[skip]; + WR_BINTERNAL(dest, nksize ? nksize : bl->ksize, + rchild->pgno, bl->flags & P_BIGKEY); + memmove(dest, bl->bytes, nksize ? nksize : bl->ksize); + if (bl->flags & P_BIGKEY && + bt_preserve(t, *(pgno_t *)(void *)bl->bytes) == + RET_ERROR) + goto err1; + break; + case P_RINTERNAL: + /* + * Update the left page count. If split + * added at index 0, fix the correct page. + */ + if (skip > 0) + dest = (char *)(void *)h + h->linp[skip - 1]; + else + dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1]; + ((RINTERNAL *)(void *)dest)->nrecs = rec_total(lchild); + ((RINTERNAL *)(void *)dest)->pgno = lchild->pgno; + + /* Update the right page count. */ + h->linp[skip] = h->upper -= nbytes; + dest = (char *)(void *)h + h->linp[skip]; + ((RINTERNAL *)(void *)dest)->nrecs = rec_total(rchild); + ((RINTERNAL *)(void *)dest)->pgno = rchild->pgno; + break; + case P_RLEAF: + /* + * Update the left page count. If split + * added at index 0, fix the correct page. + */ + if (skip > 0) + dest = (char *)(void *)h + h->linp[skip - 1]; + else + dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1]; + ((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(lchild); + ((RINTERNAL *)(void *)dest)->pgno = lchild->pgno; + + /* Update the right page count. */ + h->linp[skip] = h->upper -= nbytes; + dest = (char *)(void *)h + h->linp[skip]; + ((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(rchild); + ((RINTERNAL *)(void *)dest)->pgno = rchild->pgno; + break; + default: + abort(); + } + + /* Unpin the held pages. */ + if (!parentsplit) { + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + break; + } + + /* If the root page was split, make it look right. */ + if (sp->pgno == P_ROOT && + (F_ISSET(t, R_RECNO) ? + bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR) + goto err1; + + mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); + mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); + } + + /* Unpin the held pages. */ + mpool_put(t->bt_mp, l, MPOOL_DIRTY); + mpool_put(t->bt_mp, r, MPOOL_DIRTY); + + /* Clear any pages left on the stack. */ + return (RET_SUCCESS); + + /* + * If something fails in the above loop we were already walking back + * up the tree and the tree is now inconsistent. Nothing much we can + * do about it but release any memory we're holding. + */ +err1: mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); + mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); + +err2: mpool_put(t->bt_mp, l, 0); + mpool_put(t->bt_mp, r, 0); + __dbpanic(t->bt_dbp); + return (RET_ERROR); +} + +/* + * BT_PAGE -- Split a non-root page of a btree. + * + * Parameters: + * t: tree + * h: root page + * lp: pointer to left page pointer + * rp: pointer to right page pointer + * skip: pointer to index to leave open + * ilen: insert length + * + * Returns: + * Pointer to page in which to insert or NULL on error. + */ +static PAGE * +bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen) +{ + PAGE *l, *r, *tp; + pgno_t npg; + +#ifdef STATISTICS + ++bt_split; +#endif + /* Put the new right page for the split into place. */ + if ((r = __bt_new(t, &npg)) == NULL) + return (NULL); + r->pgno = npg; + r->lower = BTDATAOFF; + r->upper = t->bt_psize; + r->nextpg = h->nextpg; + r->prevpg = h->pgno; + r->flags = h->flags & P_TYPE; + + /* + * If we're splitting the last page on a level because we're appending + * a key to it (skip is NEXTINDEX()), it's likely that the data is + * sorted. Adding an empty page on the side of the level is less work + * and can push the fill factor much higher than normal. If we're + * wrong it's no big deal, we'll just do the split the right way next + * time. It may look like it's equally easy to do a similar hack for + * reverse sorted data, that is, split the tree left, but it's not. + * Don't even try. + */ + if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) { +#ifdef STATISTICS + ++bt_sortsplit; +#endif + h->nextpg = r->pgno; + r->lower = BTDATAOFF + sizeof(indx_t); + *skip = 0; + *lp = h; + *rp = r; + return (r); + } + + /* Put the new left page for the split into place. */ + if ((l = calloc(1, t->bt_psize)) == NULL) { + mpool_put(t->bt_mp, r, 0); + return (NULL); + } +#ifdef PURIFY + memset(l, 0xff, t->bt_psize); +#endif + l->pgno = h->pgno; + l->nextpg = r->pgno; + l->prevpg = h->prevpg; + l->lower = BTDATAOFF; + l->upper = t->bt_psize; + l->flags = h->flags & P_TYPE; + + /* Fix up the previous pointer of the page after the split page. */ + if (h->nextpg != P_INVALID) { + if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) { + free(l); + /* XXX mpool_free(t->bt_mp, r->pgno); */ + return (NULL); + } + tp->prevpg = r->pgno; + mpool_put(t->bt_mp, tp, MPOOL_DIRTY); + } + + /* + * Split right. The key/data pairs aren't sorted in the btree page so + * it's simpler to copy the data from the split page onto two new pages + * instead of copying half the data to the right page and compacting + * the left page in place. Since the left page can't change, we have + * to swap the original and the allocated left page after the split. + */ + tp = bt_psplit(t, h, l, r, skip, ilen); + + /* Move the new left page onto the old left page. */ + memmove(h, l, t->bt_psize); + if (tp == l) + tp = h; + free(l); + + *lp = h; + *rp = r; + return (tp); +} + +/* + * BT_ROOT -- Split the root page of a btree. + * + * Parameters: + * t: tree + * h: root page + * lp: pointer to left page pointer + * rp: pointer to right page pointer + * skip: pointer to index to leave open + * ilen: insert length + * + * Returns: + * Pointer to page in which to insert or NULL on error. + */ +static PAGE * +bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen) +{ + PAGE *l, *r, *tp; + pgno_t lnpg, rnpg; + +#ifdef STATISTICS + ++bt_split; + ++bt_rootsplit; +#endif + /* Put the new left and right pages for the split into place. */ + if ((l = __bt_new(t, &lnpg)) == NULL || + (r = __bt_new(t, &rnpg)) == NULL) + return (NULL); + l->pgno = lnpg; + r->pgno = rnpg; + l->nextpg = r->pgno; + r->prevpg = l->pgno; + l->prevpg = r->nextpg = P_INVALID; + l->lower = r->lower = BTDATAOFF; + l->upper = r->upper = t->bt_psize; + l->flags = r->flags = h->flags & P_TYPE; + + /* Split the root page. */ + tp = bt_psplit(t, h, l, r, skip, ilen); + + *lp = l; + *rp = r; + return (tp); +} + +/* + * BT_RROOT -- Fix up the recno root page after it has been split. + * + * Parameters: + * t: tree + * h: root page + * l: left page + * r: right page + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +static int +bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r) +{ + char *dest; + uint32_t sz; + size_t temp; + + temp = t->bt_psize - NRINTERNAL; + _DBFIT(temp, uint32_t); + sz = (uint32_t)temp; + + /* Insert the left and right keys, set the header information. */ + _DBFIT(sz, indx_t); + h->linp[0] = h->upper = (indx_t)sz; + dest = (char *)(void *)h + h->upper; + WR_RINTERNAL(dest, + l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno); + + h->linp[1] = h->upper -= NRINTERNAL; + dest = (char *)(void *)h + h->upper; + WR_RINTERNAL(dest, + r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno); + + h->lower = BTDATAOFF + 2 * sizeof(indx_t); + + /* Unpin the root page, set to recno internal page. */ + h->flags &= ~P_TYPE; + h->flags |= P_RINTERNAL; + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + + return (RET_SUCCESS); +} + +/* + * BT_BROOT -- Fix up the btree root page after it has been split. + * + * Parameters: + * t: tree + * h: root page + * l: left page + * r: right page + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +static int +bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r) +{ + BINTERNAL *bi = NULL; /* pacify gcc */ + BLEAF *bl; + uint32_t nbytes; + char *dest; + + /* + * If the root page was a leaf page, change it into an internal page. + * We copy the key we split on (but not the key's data, in the case of + * a leaf page) to the new root page. + * + * The btree comparison code guarantees that the left-most key on any + * level of the tree is never used, so it doesn't need to be filled in. + */ + nbytes = NBINTERNAL(0); + h->linp[0] = h->upper = t->bt_psize - nbytes; + dest = (char *)(void *)h + h->upper; + WR_BINTERNAL(dest, 0, l->pgno, 0); + + switch (h->flags & P_TYPE) { + case P_BLEAF: + bl = GETBLEAF(r, 0); + nbytes = NBINTERNAL(bl->ksize); + h->linp[1] = h->upper -= nbytes; + dest = (char *)(void *)h + h->upper; + WR_BINTERNAL(dest, bl->ksize, r->pgno, 0); + memmove(dest, bl->bytes, bl->ksize); + + /* + * If the key is on an overflow page, mark the overflow chain + * so it isn't deleted when the leaf copy of the key is deleted. + */ + if (bl->flags & P_BIGKEY && + bt_preserve(t, *(pgno_t *)(void *)bl->bytes) == RET_ERROR) + return (RET_ERROR); + break; + case P_BINTERNAL: + bi = GETBINTERNAL(r, 0); + nbytes = NBINTERNAL(bi->ksize); + h->linp[1] = h->upper -= nbytes; + dest = (char *)(void *)h + h->upper; + memmove(dest, bi, nbytes); + ((BINTERNAL *)(void *)dest)->pgno = r->pgno; + break; + default: + abort(); + } + + /* There are two keys on the page. */ + h->lower = BTDATAOFF + 2 * sizeof(indx_t); + + /* Unpin the root page, set to btree internal page. */ + h->flags &= ~P_TYPE; + h->flags |= P_BINTERNAL; + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + + return (RET_SUCCESS); +} + +/* + * BT_PSPLIT -- Do the real work of splitting the page. + * + * Parameters: + * t: tree + * h: page to be split + * l: page to put lower half of data + * r: page to put upper half of data + * pskip: pointer to index to leave open + * ilen: insert length + * + * Returns: + * Pointer to page in which to insert. + */ +static PAGE * +bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen) +{ + BINTERNAL *bi; + BLEAF *bl; + CURSOR *c; + RLEAF *rl; + PAGE *rval; + void *src = NULL; /* pacify gcc */ + indx_t full, half, nxt, off, skip, top, used; + uint32_t nbytes; + size_t temp; + int bigkeycnt, isbigkey; + + /* + * Split the data to the left and right pages. Leave the skip index + * open. Additionally, make some effort not to split on an overflow + * key. This makes internal page processing faster and can save + * space as overflow keys used by internal pages are never deleted. + */ + bigkeycnt = 0; + skip = *pskip; + temp = t->bt_psize - BTDATAOFF; + _DBFIT(temp, indx_t); + full = (indx_t)temp; + half = full / 2; + used = 0; + for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) { + if (skip == off) { + _DBFIT(ilen, uint32_t); + nbytes = (uint32_t)ilen; + isbigkey = 0; /* XXX: not really known. */ + } else + switch (h->flags & P_TYPE) { + case P_BINTERNAL: + src = bi = GETBINTERNAL(h, nxt); + nbytes = NBINTERNAL(bi->ksize); + isbigkey = bi->flags & P_BIGKEY; + break; + case P_BLEAF: + src = bl = GETBLEAF(h, nxt); + nbytes = NBLEAF(bl); + isbigkey = bl->flags & P_BIGKEY; + break; + case P_RINTERNAL: + src = GETRINTERNAL(h, nxt); + nbytes = NRINTERNAL; + isbigkey = 0; + break; + case P_RLEAF: + src = rl = GETRLEAF(h, nxt); + nbytes = NRLEAF(rl); + isbigkey = 0; + break; + default: + abort(); + } + + /* + * If the key/data pairs are substantial fractions of the max + * possible size for the page, it's possible to get situations + * where we decide to try and copy too much onto the left page. + * Make sure that doesn't happen. + */ + if ((skip <= off && used + nbytes + sizeof(indx_t) >= full) || + nxt == top - 1) { + --off; + break; + } + + /* Copy the key/data pair, if not the skipped index. */ + if (skip != off) { + ++nxt; + + l->linp[off] = l->upper -= nbytes; + memmove((char *)(void *)l + l->upper, src, nbytes); + } + + temp = nbytes + sizeof(indx_t); + _DBFIT(temp, indx_t); + used += (indx_t)temp; + if (used >= half) { + if (!isbigkey || bigkeycnt == 3) + break; + else + ++bigkeycnt; + } + } + + /* + * Off is the last offset that's valid for the left page. + * Nxt is the first offset to be placed on the right page. + */ + temp = (off + 1) * sizeof(indx_t); + _DBFIT(temp, indx_t); + l->lower += (indx_t)temp; + + /* + * If splitting the page that the cursor was on, the cursor has to be + * adjusted to point to the same record as before the split. If the + * cursor is at or past the skipped slot, the cursor is incremented by + * one. If the cursor is on the right page, it is decremented by the + * number of records split to the left page. + */ + c = &t->bt_cursor; + if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) { + if (c->pg.index >= skip) + ++c->pg.index; + if (c->pg.index < nxt) /* Left page. */ + c->pg.pgno = l->pgno; + else { /* Right page. */ + c->pg.pgno = r->pgno; + c->pg.index -= nxt; + } + } + + /* + * If the skipped index was on the left page, just return that page. + * Otherwise, adjust the skip index to reflect the new position on + * the right page. + */ + if (skip <= off) { + skip = MAX_PAGE_OFFSET; + rval = l; + } else { + rval = r; + *pskip -= nxt; + } + + for (off = 0; nxt < top; ++off) { + if (skip == nxt) { + ++off; + skip = MAX_PAGE_OFFSET; + } + switch (h->flags & P_TYPE) { + case P_BINTERNAL: + src = bi = GETBINTERNAL(h, nxt); + nbytes = NBINTERNAL(bi->ksize); + break; + case P_BLEAF: + src = bl = GETBLEAF(h, nxt); + nbytes = NBLEAF(bl); + break; + case P_RINTERNAL: + src = GETRINTERNAL(h, nxt); + nbytes = NRINTERNAL; + break; + case P_RLEAF: + src = rl = GETRLEAF(h, nxt); + nbytes = NRLEAF(rl); + break; + default: + abort(); + } + ++nxt; + r->linp[off] = r->upper -= nbytes; + memmove((char *)(void *)r + r->upper, src, nbytes); + } + temp = off * sizeof(indx_t); + _DBFIT(temp, indx_t); + r->lower += (indx_t)temp; + + /* If the key is being appended to the page, adjust the index. */ + if (skip == top) + r->lower += sizeof(indx_t); + + return (rval); +} + +/* + * BT_PRESERVE -- Mark a chain of pages as used by an internal node. + * + * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the + * record that references them gets deleted. Chains pointed to by internal + * pages never get deleted. This routine marks a chain as pointed to by an + * internal page. + * + * Parameters: + * t: tree + * pg: page number of first page in the chain. + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +static int +bt_preserve(BTREE *t, pgno_t pg) +{ + PAGE *h; + + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + return (RET_ERROR); + h->flags |= P_PRESERVE; + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + return (RET_SUCCESS); +} + +/* + * REC_TOTAL -- Return the number of recno entries below a page. + * + * Parameters: + * h: page + * + * Returns: + * The number of recno entries below a page. + * + * XXX + * These values could be set by the bt_psplit routine. The problem is that the + * entry has to be popped off of the stack etc. or the values have to be passed + * all the way back to bt_split/bt_rroot and it's not very clean. + */ +static recno_t +rec_total(PAGE *h) +{ + recno_t recs; + indx_t nxt, top; + + for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt) + recs += GETRINTERNAL(h, nxt)->nrecs; + return (recs); +} diff --git a/lib/nbsd_libc/db/btree/bt_utils.c b/lib/nbsd_libc/db/btree/bt_utils.c new file mode 100644 index 000000000..db0d058e1 --- /dev/null +++ b/lib/nbsd_libc/db/btree/bt_utils.c @@ -0,0 +1,253 @@ +/* $NetBSD: bt_utils.c,v 1.13 2008/09/10 17:52:35 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: bt_utils.c,v 1.13 2008/09/10 17:52:35 joerg Exp $"); + +#include + +#include +#include +#include +#include + +#include +#include "btree.h" + +/* + * __bt_ret -- + * Build return key/data pair. + * + * Parameters: + * t: tree + * e: key/data pair to be returned + * key: user's key structure (NULL if not to be filled in) + * rkey: memory area to hold key + * data: user's data structure (NULL if not to be filled in) + * rdata: memory area to hold data + * copy: always copy the key/data item + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +int +__bt_ret(BTREE *t, EPG *e, DBT *key, DBT *rkey, DBT *data, DBT *rdata, int copy) +{ + BLEAF *bl; + void *p; + + bl = GETBLEAF(e->page, e->index); + + /* + * We must copy big keys/data to make them contigous. Otherwise, + * leave the page pinned and don't copy unless the user specified + * concurrent access. + */ + if (key == NULL) + goto dataonly; + + if (bl->flags & P_BIGKEY) { + if (__ovfl_get(t, bl->bytes, + &key->size, &rkey->data, &rkey->size)) + return (RET_ERROR); + key->data = rkey->data; + } else if (copy || F_ISSET(t, B_DB_LOCK)) { + if (bl->ksize > rkey->size) { + p = (void *)(rkey->data == NULL ? + malloc(bl->ksize) : realloc(rkey->data, bl->ksize)); + if (p == NULL) + return (RET_ERROR); + rkey->data = p; + rkey->size = bl->ksize; + } + memmove(rkey->data, bl->bytes, bl->ksize); + key->size = bl->ksize; + key->data = rkey->data; + } else { + key->size = bl->ksize; + key->data = bl->bytes; + } + +dataonly: + if (data == NULL) + return (RET_SUCCESS); + + if (bl->flags & P_BIGDATA) { + if (__ovfl_get(t, bl->bytes + bl->ksize, + &data->size, &rdata->data, &rdata->size)) + return (RET_ERROR); + data->data = rdata->data; + } else if (copy || F_ISSET(t, B_DB_LOCK)) { + /* Use +1 in case the first record retrieved is 0 length. */ + if (bl->dsize + 1 > rdata->size) { + p = (void *)(rdata->data == NULL ? + malloc(bl->dsize + 1) : + realloc(rdata->data, bl->dsize + 1)); + if (p == NULL) + return (RET_ERROR); + rdata->data = p; + rdata->size = bl->dsize + 1; + } + memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize); + data->size = bl->dsize; + data->data = rdata->data; + } else { + data->size = bl->dsize; + data->data = bl->bytes + bl->ksize; + } + + return (RET_SUCCESS); +} + +/* + * __BT_CMP -- Compare a key to a given record. + * + * Parameters: + * t: tree + * k1: DBT pointer of first arg to comparison + * e: pointer to EPG for comparison + * + * Returns: + * < 0 if k1 is < record + * = 0 if k1 is = record + * > 0 if k1 is > record + */ +int +__bt_cmp(BTREE *t, const DBT *k1, EPG *e) +{ + BINTERNAL *bi; + BLEAF *bl; + DBT k2; + PAGE *h; + void *bigkey; + + /* + * The left-most key on internal pages, at any level of the tree, is + * guaranteed by the following code to be less than any user key. + * This saves us from having to update the leftmost key on an internal + * page when the user inserts a new key in the tree smaller than + * anything we've yet seen. + */ + h = e->page; + if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF)) + return (1); + + bigkey = NULL; + if (h->flags & P_BLEAF) { + bl = GETBLEAF(h, e->index); + if (bl->flags & P_BIGKEY) + bigkey = bl->bytes; + else { + k2.data = bl->bytes; + k2.size = bl->ksize; + } + } else { + bi = GETBINTERNAL(h, e->index); + if (bi->flags & P_BIGKEY) + bigkey = bi->bytes; + else { + k2.data = bi->bytes; + k2.size = bi->ksize; + } + } + + if (bigkey) { + if (__ovfl_get(t, bigkey, + &k2.size, &t->bt_rdata.data, &t->bt_rdata.size)) + return (RET_ERROR); + k2.data = t->bt_rdata.data; + } + return ((*t->bt_cmp)(k1, &k2)); +} + +/* + * __BT_DEFCMP -- Default comparison routine. + * + * Parameters: + * a: DBT #1 + * b: DBT #2 + * + * Returns: + * < 0 if a is < b + * = 0 if a is = b + * > 0 if a is > b + */ +int +__bt_defcmp(const DBT *a, const DBT *b) +{ + size_t len; + uint8_t *p1, *p2; + + /* + * XXX + * If a size_t doesn't fit in an int, this routine can lose. + * What we need is a integral type which is guaranteed to be + * larger than a size_t, and there is no such thing. + */ + len = MIN(a->size, b->size); + for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2) + if (*p1 != *p2) + return ((int)*p1 - (int)*p2); + return ((int)a->size - (int)b->size); +} + +/* + * __BT_DEFPFX -- Default prefix routine. + * + * Parameters: + * a: DBT #1 + * b: DBT #2 + * + * Returns: + * Number of bytes needed to distinguish b from a. + */ +size_t +__bt_defpfx(const DBT *a, const DBT *b) +{ + uint8_t *p1, *p2; + size_t cnt, len; + + cnt = 1; + len = MIN(a->size, b->size); + for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt) + if (*p1 != *p2) + return (cnt); + + /* a->size must be <= b->size, or they wouldn't be in this order. */ + return (a->size < b->size ? a->size + 1 : a->size); +} diff --git a/lib/nbsd_libc/db/btree/btree.h b/lib/nbsd_libc/db/btree/btree.h new file mode 100644 index 000000000..b28f082ff --- /dev/null +++ b/lib/nbsd_libc/db/btree/btree.h @@ -0,0 +1,433 @@ +/* $NetBSD: btree.h,v 1.16 2008/08/26 21:18:38 joerg Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * 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. + * + * @(#)btree.h 8.11 (Berkeley) 8/17/94 + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +/* Macros to set/clear/test flags. */ +#define F_SET(p, f) (p)->flags |= (f) +#define F_CLR(p, f) (p)->flags &= ~(f) +#define F_ISSET(p, f) ((p)->flags & (f)) + +#include + +#define DEFMINKEYPAGE (2) /* Minimum keys per page */ +#define MINCACHE (5) /* Minimum cached pages */ +#define MINPSIZE (512) /* Minimum page size */ + +/* + * Page 0 of a btree file contains a copy of the meta-data. This page is also + * used as an out-of-band page, i.e. page pointers that point to nowhere point + * to page 0. Page 1 is the root of the btree. + */ +#define P_INVALID 0 /* Invalid tree page number. */ +#define P_META 0 /* Tree metadata page number. */ +#define P_ROOT 1 /* Tree root page number. */ + +/* + * There are five page layouts in the btree: btree internal pages (BINTERNAL), + * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages + * (RLEAF) and overflow pages. All five page types have a page header (PAGE). + * This implementation requires that values within structures NOT be padded. + * (ANSI C permits random padding.) If your compiler pads randomly you'll have + * to do some work to get this package to run. + */ +typedef struct _page { + pgno_t pgno; /* this page's page number */ + pgno_t prevpg; /* left sibling */ + pgno_t nextpg; /* right sibling */ + +#define P_BINTERNAL 0x01 /* btree internal page */ +#define P_BLEAF 0x02 /* leaf page */ +#define P_OVERFLOW 0x04 /* overflow page */ +#define P_RINTERNAL 0x08 /* recno internal page */ +#define P_RLEAF 0x10 /* leaf page */ +#define P_TYPE 0x1f /* type mask */ +#define P_PRESERVE 0x20 /* never delete this chain of pages */ + uint32_t flags; + + indx_t lower; /* lower bound of free space on page */ + indx_t upper; /* upper bound of free space on page */ + indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */ +} PAGE; + +/* First and next index. */ +#define BTDATAOFF \ + (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \ + sizeof(uint32_t) + sizeof(indx_t) + sizeof(indx_t)) + +#define _NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t)) +#ifdef _DIAGNOSTIC +static __inline indx_t +NEXTINDEX(const PAGE *p) { + size_t x = _NEXTINDEX(p); + _DBFIT(x, indx_t); + return (indx_t)x; +} +#else +#define NEXTINDEX(p) (indx_t)_NEXTINDEX(p) +#endif + +/* + * For pages other than overflow pages, there is an array of offsets into the + * rest of the page immediately following the page header. Each offset is to + * an item which is unique to the type of page. The h_lower offset is just + * past the last filled-in index. The h_upper offset is the first item on the + * page. Offsets are from the beginning of the page. + * + * If an item is too big to store on a single page, a flag is set and the item + * is a { page, size } pair such that the page is the first page of an overflow + * chain with size bytes of item. Overflow pages are simply bytes without any + * external structure. + * + * The page number and size fields in the items are pgno_t-aligned so they can + * be manipulated without copying. (This presumes that 32 bit items can be + * manipulated on this system.) + */ +#define BTLALIGN(n) (((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1)) +#define NOVFLSIZE (sizeof(pgno_t) + sizeof(uint32_t)) + +/* + * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno} + * pairs, such that the key compares less than or equal to all of the records + * on that page. For a tree without duplicate keys, an internal page with two + * consecutive keys, a and b, will have all records greater than or equal to a + * and less than b stored on the page associated with a. Duplicate keys are + * somewhat special and can cause duplicate internal and leaf page records and + * some minor modifications of the above rule. + */ +typedef struct _binternal { + uint32_t ksize; /* key size */ + pgno_t pgno; /* page number stored on */ +#define P_BIGDATA 0x01 /* overflow data */ +#define P_BIGKEY 0x02 /* overflow key */ + uint8_t flags; + char bytes[1]; /* data */ +} BINTERNAL; + +/* Get the page's BINTERNAL structure at index indx. */ +#define GETBINTERNAL(pg, indx) \ + ((BINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx])) + +/* Get the number of bytes in the entry. */ +#define _NBINTERNAL(len) \ + BTLALIGN(sizeof(uint32_t) + sizeof(pgno_t) + sizeof(uint8_t) + (len)) +#ifdef _DIAGNOSTIC +static __inline uint32_t +NBINTERNAL(uint32_t len) { + size_t x = _NBINTERNAL(len); + _DBFIT(x, uint32_t); + return (uint32_t)x; +} +#else +#define NBINTERNAL(len) (uint32_t)_NBINTERNAL(len) +#endif + +/* Copy a BINTERNAL entry to the page. */ +#define WR_BINTERNAL(p, size, pgno, flags) do { \ + _DBFIT(size, uint32_t); \ + *(uint32_t *)(void *)p = (uint32_t)size; \ + p += sizeof(uint32_t); \ + *(pgno_t *)(void *)p = pgno; \ + p += sizeof(pgno_t); \ + *(uint8_t *)(void *)p = flags; \ + p += sizeof(uint8_t); \ +} while (/*CONSTCOND*/0) + +/* + * For the recno internal pages, the item is a page number with the number of + * keys found on that page and below. + */ +typedef struct _rinternal { + recno_t nrecs; /* number of records */ + pgno_t pgno; /* page number stored below */ +} RINTERNAL; + +/* Get the page's RINTERNAL structure at index indx. */ +#define GETRINTERNAL(pg, indx) \ + ((RINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx])) + +/* Get the number of bytes in the entry. */ +#define NRINTERNAL \ + BTLALIGN(sizeof(recno_t) + sizeof(pgno_t)) + +/* Copy a RINTERAL entry to the page. */ +#define WR_RINTERNAL(p, nrecs, pgno) do { \ + *(recno_t *)(void *)p = nrecs; \ + p += sizeof(recno_t); \ + *(pgno_t *)(void *)p = pgno; \ +} while (/*CONSTCOND*/0) + +/* For the btree leaf pages, the item is a key and data pair. */ +typedef struct _bleaf { + uint32_t ksize; /* size of key */ + uint32_t dsize; /* size of data */ + uint8_t flags; /* P_BIGDATA, P_BIGKEY */ + char bytes[1]; /* data */ +} BLEAF; + +/* Get the page's BLEAF structure at index indx. */ +#define GETBLEAF(pg, indx) \ + ((BLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx])) + + +/* Get the number of bytes in the user's key/data pair. */ +#define _NBLEAFDBT(ksize, dsize) \ + BTLALIGN(sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t) + \ + (ksize) + (dsize)) +#ifdef _DIAGNOSTIC +static __inline uint32_t +NBLEAFDBT(size_t k, size_t d) { + size_t x = _NBLEAFDBT(k, d); + _DBFIT(x, uint32_t); + return (uint32_t)x; +} +#else +#define NBLEAFDBT(p, q) (uint32_t)_NBLEAFDBT(p, q) +#endif + +/* Get the number of bytes in the entry. */ +#define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize) + +/* Copy a BLEAF entry to the page. */ +#define WR_BLEAF(p, key, data, flags) do { \ + _DBFIT(key->size, uint32_t); \ + *(uint32_t *)(void *)p = (uint32_t)key->size; \ + p += sizeof(uint32_t); \ + _DBFIT(data->size, uint32_t); \ + *(uint32_t *)(void *)p = (uint32_t)data->size; \ + p += sizeof(uint32_t); \ + *(uint8_t *)(void *)p = flags; \ + p += sizeof(uint8_t); \ + (void)memmove(p, key->data, key->size); \ + p += key->size; \ + (void)memmove(p, data->data, data->size); \ +} while (/*CONSTCOND*/0) + +/* For the recno leaf pages, the item is a data entry. */ +typedef struct _rleaf { + uint32_t dsize; /* size of data */ + uint8_t flags; /* P_BIGDATA */ + char bytes[1]; +} RLEAF; + +/* Get the page's RLEAF structure at index indx. */ +#define GETRLEAF(pg, indx) \ + ((RLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx])) + +#define _NRLEAFDBT(dsize) \ + BTLALIGN(sizeof(uint32_t) + sizeof(uint8_t) + (dsize)) + +#ifdef _DIAGNOSTIC +static __inline uint32_t +NRLEAFDBT(size_t d) { + size_t x = _NRLEAFDBT(d); + _DBFIT(x, uint32_t); + return (uint32_t)x; +} +#else +#define NRLEAFDBT(d) (uint32_t)_NRLEAFDBT(d) +#endif + +/* Get the number of bytes in the entry. */ +#define NRLEAF(p) NRLEAFDBT((p)->dsize) + +/* Get the number of bytes from the user's data. */ + +/* Copy a RLEAF entry to the page. */ +#define WR_RLEAF(p, data, flags) do { \ + _DBFIT(data->size, uint32_t); \ + *(uint32_t *)(void *)p = (uint32_t)data->size; \ + p += sizeof(uint32_t); \ + *(uint8_t *)(void *)p = flags; \ + p += sizeof(uint8_t); \ + memmove(p, data->data, data->size); \ +} while (/*CONSTCOND*/0) + +/* + * A record in the tree is either a pointer to a page and an index in the page + * or a page number and an index. These structures are used as a cursor, stack + * entry and search returns as well as to pass records to other routines. + * + * One comment about searches. Internal page searches must find the largest + * record less than key in the tree so that descents work. Leaf page searches + * must find the smallest record greater than key so that the returned index + * is the record's correct position for insertion. + */ +typedef struct _epgno { + pgno_t pgno; /* the page number */ + indx_t index; /* the index on the page */ +} EPGNO; + +typedef struct _epg { + PAGE *page; /* the (pinned) page */ + indx_t index; /* the index on the page */ +} EPG; + +/* + * About cursors. The cursor (and the page that contained the key/data pair + * that it referenced) can be deleted, which makes things a bit tricky. If + * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set + * or there simply aren't any duplicates of the key) we copy the key that it + * referenced when it's deleted, and reacquire a new cursor key if the cursor + * is used again. If there are duplicates keys, we move to the next/previous + * key, and set a flag so that we know what happened. NOTE: if duplicate (to + * the cursor) keys are added to the tree during this process, it is undefined + * if they will be returned or not in a cursor scan. + * + * The flags determine the possible states of the cursor: + * + * CURS_INIT The cursor references *something*. + * CURS_ACQUIRE The cursor was deleted, and a key has been saved so that + * we can reacquire the right position in the tree. + * CURS_AFTER, CURS_BEFORE + * The cursor was deleted, and now references a key/data pair + * that has not yet been returned, either before or after the + * deleted key/data pair. + * XXX + * This structure is broken out so that we can eventually offer multiple + * cursors as part of the DB interface. + */ +typedef struct _cursor { + EPGNO pg; /* B: Saved tree reference. */ + DBT key; /* B: Saved key, or key.data == NULL. */ + recno_t rcursor; /* R: recno cursor (1-based) */ + +#define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */ +#define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */ +#define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */ +#define CURS_INIT 0x08 /* RB: Cursor initialized. */ + uint8_t flags; +} CURSOR; + +/* + * The metadata of the tree. The nrecs field is used only by the RECNO code. + * This is because the btree doesn't really need it and it requires that every + * put or delete call modify the metadata. + */ +typedef struct _btmeta { + uint32_t magic; /* magic number */ + uint32_t version; /* version */ + uint32_t psize; /* page size */ + uint32_t free; /* page number of first free page */ + uint32_t nrecs; /* R: number of records */ + +#define SAVEMETA (B_NODUPS | R_RECNO) + uint32_t flags; /* bt_flags & SAVEMETA */ +} BTMETA; + +/* The in-memory btree/recno data structure. */ +typedef struct _btree { + MPOOL *bt_mp; /* memory pool cookie */ + + DB *bt_dbp; /* pointer to enclosing DB */ + + EPG bt_cur; /* current (pinned) page */ + PAGE *bt_pinned; /* page pinned across calls */ + + CURSOR bt_cursor; /* cursor */ + +#define BT_PUSH(t, p, i) { \ + t->bt_sp->pgno = p; \ + t->bt_sp->index = i; \ + ++t->bt_sp; \ +} +#define BT_POP(t) (t->bt_sp == t->bt_stack ? NULL : --t->bt_sp) +#define BT_CLR(t) (t->bt_sp = t->bt_stack) + EPGNO bt_stack[50]; /* stack of parent pages */ + EPGNO *bt_sp; /* current stack pointer */ + + DBT bt_rkey; /* returned key */ + DBT bt_rdata; /* returned data */ + + int bt_fd; /* tree file descriptor */ + + pgno_t bt_free; /* next free page */ + uint32_t bt_psize; /* page size */ + indx_t bt_ovflsize; /* cut-off for key/data overflow */ + int bt_lorder; /* byte order */ + /* sorted order */ + enum { NOT, BACK, FORWARD } bt_order; + EPGNO bt_last; /* last insert */ + + /* B: key comparison function */ + int (*bt_cmp)(const DBT *, const DBT *); + /* B: prefix comparison function */ + size_t (*bt_pfx)(const DBT *, const DBT *); + /* R: recno input function */ + int (*bt_irec)(struct _btree *, recno_t); + + FILE *bt_rfp; /* R: record FILE pointer */ + int bt_rfd; /* R: record file descriptor */ + + caddr_t bt_cmap; /* R: current point in mapped space */ + caddr_t bt_smap; /* R: start of mapped space */ + caddr_t bt_emap; /* R: end of mapped space */ + size_t bt_msize; /* R: size of mapped region. */ + + recno_t bt_nrecs; /* R: number of records */ + size_t bt_reclen; /* R: fixed record length */ + uint8_t bt_bval; /* R: delimiting byte/pad character */ + +/* + * NB: + * B_NODUPS and R_RECNO are stored on disk, and may not be changed. + */ +#define B_INMEM 0x00001 /* in-memory tree */ +#define B_METADIRTY 0x00002 /* need to write metadata */ +#define B_MODIFIED 0x00004 /* tree modified */ +#define B_NEEDSWAP 0x00008 /* if byte order requires swapping */ +#define B_RDONLY 0x00010 /* read-only tree */ + +#define B_NODUPS 0x00020 /* no duplicate keys permitted */ +#define R_RECNO 0x00080 /* record oriented tree */ + +#define R_CLOSEFP 0x00040 /* opened a file pointer */ +#define R_EOF 0x00100 /* end of input file reached. */ +#define R_FIXLEN 0x00200 /* fixed length records */ +#define R_MEMMAPPED 0x00400 /* memory mapped file. */ +#define R_INMEM 0x00800 /* in-memory file */ +#define R_MODIFIED 0x01000 /* modified file */ +#define R_RDONLY 0x02000 /* read-only file */ + +#define B_DB_LOCK 0x04000 /* DB_LOCK specified. */ +#define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */ +#define B_DB_TXN 0x10000 /* DB_TXN specified. */ + uint32_t flags; +} BTREE; + +#include "extern.h" diff --git a/lib/nbsd_libc/db/btree/extern.h b/lib/nbsd_libc/db/btree/extern.h new file mode 100644 index 000000000..665f852a5 --- /dev/null +++ b/lib/nbsd_libc/db/btree/extern.h @@ -0,0 +1,72 @@ +/* $NetBSD: extern.h,v 1.12 2008/09/26 11:41:06 tsutsui Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1994 + * 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. + * + * @(#)extern.h 8.10 (Berkeley) 7/20/94 + */ + +#ifndef _BTREE_EXTERN_H_ +#define _BTREE_EXTERN_H_ +int __bt_close(DB *); +int __bt_cmp(BTREE *, const DBT *, EPG *); +int __bt_crsrdel(BTREE *, EPGNO *); +int __bt_defcmp(const DBT *, const DBT *); +size_t __bt_defpfx(const DBT *, const DBT *); +int __bt_delete(const DB *, const DBT *, unsigned int); +int __bt_dleaf(BTREE *, const DBT *, PAGE *, unsigned int); +int __bt_fd(const DB *); +int __bt_free(BTREE *, PAGE *); +int __bt_get(const DB *, const DBT *, DBT *, unsigned int); +PAGE *__bt_new(BTREE *, pgno_t *); +void __bt_pgin(void *, pgno_t, void *); +void __bt_pgout(void *, pgno_t, void *); +int __bt_push(BTREE *, pgno_t, int); +int __bt_put(const DB *dbp, DBT *, const DBT *, unsigned int); +int __bt_ret(BTREE *, EPG *, DBT *, DBT *, DBT *, DBT *, int); +EPG *__bt_search(BTREE *, const DBT *, int *); +int __bt_seq(const DB *, DBT *, DBT *, unsigned int); +void __bt_setcur(BTREE *, pgno_t, unsigned int); +int __bt_split(BTREE *, PAGE *, + const DBT *, const DBT *, int, size_t, uint32_t); +int __bt_sync(const DB *, unsigned int); + +int __ovfl_delete(BTREE *, void *); +int __ovfl_get(BTREE *, void *, size_t *, void **, size_t *); +int __ovfl_put(BTREE *, const DBT *, pgno_t *); + +#ifdef DEBUG +void __bt_dmpage(PAGE *); +void __bt_dnpage(DB *, pgno_t); +void __bt_dpage(PAGE *); +void __bt_dump(DB *); +#endif +#ifdef STATISTICS +void __bt_stat(DB *); +#endif +#endif /* _BTREE_EXTERN_H_ */ diff --git a/lib/nbsd_libc/db/changelog b/lib/nbsd_libc/db/changelog new file mode 100644 index 000000000..f820e56b5 --- /dev/null +++ b/lib/nbsd_libc/db/changelog @@ -0,0 +1,105 @@ +# $NetBSD: changelog,v 1.2 1996/05/03 21:20:56 cgd Exp $ + +1.84 -> 1.85 + recno: #ifdef out use of mmap, it's not portable enough. + +1.83 -> 1.84 Thu Aug 18 15:46:07 EDT 1994 + recno: Rework fixed-length records so that closing and reopening + the file now works. Pad short records on input. Never do + signed comparison in recno input reading functions. + +1.82 -> 1.83 Tue Jul 26 15:33:44 EDT 1994 + btree: Rework cursor deletion code yet again; bugs with + deleting empty pages that only contained the cursor + record. + +1.81 -> 1.82 Sat Jul 16 11:01:50 EDT 1994 + btree: Fix bugs introduced by new cursor/deletion code. + Replace return kbuf/dbuf with real DBT's. + +1.80 -> 1.81 + btree: Fix bugs introduced by new cursor/deletion code. + all: Add #defines for Purify. + +1.79 -> 1.80 Wed Jul 13 22:41:54 EDT 1994 + btree Change deletion to coalesce empty pages. This is a major + change, cursors and duplicate pages all had to be reworked. + Return to a fixed stack. + recno: Affected by cursor changes. New cursor structures should + permit multiple cursors in the future. + +1.78 -> 1.79 Mon Jun 20 17:36:47 EDT 1994 + all: Minor cleanups of 1.78 for porting reasons; only + major change was inlining check of NULL pointer + so that __fix_realloc goes away. + +1.77 -> 1.78 Thu Jun 16 19:06:43 EDT 1994 + all: Move "standard" size typedef's into db.h. + +1.76 -> 1.77 Thu Jun 16 16:48:38 EDT 1994 + hash: Delete __init_ routine, has special meaning to OSF 2.0. + +1.74 -> 1.76 + all: Finish up the port to the Alpha. + +1.73 -> 1.74 + recno: Don't put the record if rec_search fails, in rec_rdelete. + Create fixed-length intermediate records past "end" of DB + correctly. + Realloc bug when reading in fixed records. + all: First cut at port to Alpha (64-bit architecture) using + 4.4BSD basic integral types typedef's. + Cast allocation pointers to shut up old compilers. + Rework PORT directory into OS/machine directories. + +1.72 -> 1.73 + btree: If enough duplicate records were inserted and then deleted + that internal pages had references to empty pages of the + duplicate keys, the search function ended up on the wrong + page. + +1.7 -> 1.72 12 Oct 1993 + hash: Support NET/2 hash formats. + +1.7 -> 1.71 16 Sep 1993 + btree/recno: + Fix bug in internal search routines that caused + return of invalid pointers. + +1.6 -> 1.7 07 Sep 1993 + hash: Fixed big key overflow bugs. + test: Portability hacks, rewrite test script, Makefile. + btree/recno: + Stop copying non-overflow key/data pairs. + PORT: Break PORT directory up into per architecture/OS + subdirectories. + +1.5 -> 1.6 06 Jun 1993 + hash: In PAIRFITS, the first comparison should look at (P)[2]. + The hash_realloc function was walking off the end of memory. + The overflow page number was wrong when bumping splitpoint. + +1.4 -> 1.5 23 May 1993 + hash: Set hash default fill factor dynamically. + recno: Fixed bug in sorted page splits. + Add page size parameter support. + Allow recno to specify the name of the underlying btree; + used for vi recovery. + btree/recno: + Support 64K pages. + btree/hash/recno: + Provide access to an underlying file descriptor. + Change sync routines to take a flag argument, recno + uses this to sync out the underlying btree. + +1.3 -> 1.4 10 May 1993 + recno: Delete the R_CURSORLOG flag from the recno interface. + Zero-length record fix for non-mmap reads. + Try and make SIZE_T_MAX test in open portable. + +1.2 -> 1.3 01 May 1993 + btree: Ignore user byte-order setting when reading already + existing database. Fixes to byte-order conversions. + +1.1 -> 1.2 15 Apr 1993 + No bug fixes, only compatibility hacks. diff --git a/lib/nbsd_libc/db/db/Makefile.inc b/lib/nbsd_libc/db/db/Makefile.inc new file mode 100644 index 000000000..9755e8f20 --- /dev/null +++ b/lib/nbsd_libc/db/db/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.4 1995/02/27 13:21:22 cgd Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +.PATH: ${.CURDIR}/db/db + +SRCS+= db.c diff --git a/lib/nbsd_libc/db/db/db.c b/lib/nbsd_libc/db/db/db.c new file mode 100644 index 000000000..059183698 --- /dev/null +++ b/lib/nbsd_libc/db/db/db.c @@ -0,0 +1,102 @@ +/* $NetBSD: db.c,v 1.16 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: db.c,v 1.16 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#include +static int __dberr(void); + +#ifdef __weak_alias +__weak_alias(dbopen,_dbopen) +#endif + +DB * +dbopen(const char *fname, int flags, mode_t mode, DBTYPE type, + const void *openinfo) +{ + +#define DB_FLAGS (DB_LOCK | DB_SHMEM | DB_TXN) +#define USE_OPEN_FLAGS \ + (O_CREAT | O_EXCL | O_EXLOCK | O_NONBLOCK | O_RDONLY | \ + O_RDWR | O_SHLOCK | O_TRUNC) + + if ((flags & ~(USE_OPEN_FLAGS | DB_FLAGS)) == 0) + switch (type) { + case DB_BTREE: + return (__bt_open(fname, flags & USE_OPEN_FLAGS, + mode, openinfo, (int)(flags & DB_FLAGS))); + case DB_HASH: + return (__hash_open(fname, flags & USE_OPEN_FLAGS, + mode, openinfo, (int)(flags & DB_FLAGS))); + case DB_RECNO: + return (__rec_open(fname, flags & USE_OPEN_FLAGS, + mode, openinfo, (int)(flags & DB_FLAGS))); + } + errno = EINVAL; + return (NULL); +} + +static int +__dberr(void) +{ + return (RET_ERROR); +} + +/* + * __DBPANIC -- Stop. + * + * Parameters: + * dbp: pointer to the DB structure. + */ +void +__dbpanic(DB *dbp) +{ + /* The only thing that can succeed is a close. */ + dbp->del = (int (*)(const struct __db *, const DBT*, u_int))__dberr; + dbp->fd = (int (*)(const struct __db *))__dberr; + dbp->get = (int (*)(const struct __db *, const DBT*, DBT *, u_int))__dberr; + dbp->put = (int (*)(const struct __db *, DBT *, const DBT *, u_int))__dberr; + dbp->seq = (int (*)(const struct __db *, DBT *, DBT *, u_int))__dberr; + dbp->sync = (int (*)(const struct __db *, u_int))__dberr; +} diff --git a/lib/nbsd_libc/db/db2netbsd b/lib/nbsd_libc/db/db2netbsd new file mode 100755 index 000000000..d5abedaa9 --- /dev/null +++ b/lib/nbsd_libc/db/db2netbsd @@ -0,0 +1,31 @@ +#!/bin/sh +# $NetBSD: db2netbsd,v 1.2 1999/02/16 18:01:37 kleink Exp $ + +# This version transforms a Berkeley DB distribution into something +# which can be 'cvs import'ed into the NetBSD source repository. +# It is to be run in the untarred Berkeley DB distribution directory +# (e.g. the "db.1.85" directory created by tar xvf), and sets up +# the destination tree in place. + +version=`basename $PWD | sed -e 's/db\.//'` +releasetag=`basename $PWD | sed -e 's/\./-/g'` + +CLEANFILES="PORT docs hash/search.h test/btree.tests test/hash.tests" + +# clean up pieces that we never import +/bin/rm -rf $CLEANFILES +find . -type l -o -name tags | xargs /bin/rm -f + +# The include files are already in place + +# Put the regression tests in the right place +mkdir -p regress/lib/libc +mv test regress/lib/libc/db + +# Put the libc pieces in the right place. +mkdir -p lib/libc/db +mv Makefile.inc README btree changelog db hash man mpool recno lib/libc/db + +echo "import with:" +echo "cvs import -m \"Import of Berkeley DB version $version\" \ +src CSRG $releasetag" diff --git a/lib/nbsd_libc/db/hash/Makefile.inc b/lib/nbsd_libc/db/hash/Makefile.inc new file mode 100644 index 000000000..d65b3106d --- /dev/null +++ b/lib/nbsd_libc/db/hash/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.9 2005/09/13 01:44:09 christos Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +.PATH: ${.CURDIR}/db/hash + +SRCS+= hash.c hash_bigkey.c hash_buf.c hash_func.c hash_log2.c \ + hash_page.c ndbmdatum.c ndbm.c diff --git a/lib/nbsd_libc/db/hash/README b/lib/nbsd_libc/db/hash/README new file mode 100644 index 000000000..02e16556b --- /dev/null +++ b/lib/nbsd_libc/db/hash/README @@ -0,0 +1,69 @@ +# $NetBSD: README,v 1.5 1999/02/16 17:59:18 kleink Exp $ +# @(#)README 8.1 (Berkeley) 6/4/93 + +This package implements a superset of the hsearch and dbm/ndbm libraries. + +Test Programs: + All test programs which need key/data pairs expect them entered + with key and data on separate lines + + tcreat3.c + Takes + bucketsize (bsize), + fill factor (ffactor), and + initial number of elements (nelem). + Creates a hash table named hashtest containing the + keys/data pairs entered from standard in. + thash4.c + Takes + bucketsize (bsize), + fill factor (ffactor), + initial number of elements (nelem) + bytes of cache (ncached), and + file from which to read data (fname) + Creates a table from the key/data pairs on standard in and + then does a read of each key/data in fname + tdel.c + Takes + bucketsize (bsize), and + fill factor (ffactor). + file from which to read data (fname) + Reads each key/data pair from fname and deletes the + key from the hash table hashtest + tseq.c + Reads the key/data pairs in the file hashtest and writes them + to standard out. + tread2.c + Takes + butes of cache (ncached). + Reads key/data pairs from standard in and looks them up + in the file hashtest. + tverify.c + Reads key/data pairs from standard in, looks them up + in the file hashtest, and verifies that the data is + correct. + +NOTES: + +The man page ../man/db.3 explains the interface to the hashing system. +The file hash.ps is a postscript copy of a paper explaining +the history, implementation, and performance of the hash package. + +"bugs" or idiosyncracies + +If you have a lot of overflows, it is possible to run out of overflow +pages. Currently, this will cause a message to be printed on stderr. +Eventually, this will be indicated by a return error code. + +If you are using the ndbm interface and exit without flushing or closing the +file, you may lose updates since the package buffers all writes. Also, +the db interface only creates a single database file. To avoid overwriting +the user's original file, the suffix ".db" is appended to the file name +passed to dbm_open. Additionally, if your code "knows" about the historic +.dir and .pag files, it will break. + +There is a fundamental difference between this package and the old hsearch. +Hsearch requires the user to maintain the keys and data in the application's +allocated memory while hash takes care of all storage management. The down +side is that the byte strings passed in the ENTRY structure must be null +terminated (both the keys and the data). diff --git a/lib/nbsd_libc/db/hash/extern.h b/lib/nbsd_libc/db/hash/extern.h new file mode 100644 index 000000000..455bf51d8 --- /dev/null +++ b/lib/nbsd_libc/db/hash/extern.h @@ -0,0 +1,63 @@ +/* $NetBSD: extern.h,v 1.9 2008/08/26 21:18:38 joerg Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1994 + * 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. + * + * @(#)extern.h 8.4 (Berkeley) 6/16/94 + */ + +BUFHEAD *__add_ovflpage(HTAB *, BUFHEAD *); +int __addel(HTAB *, BUFHEAD *, const DBT *, const DBT *); +int __big_delete(HTAB *, BUFHEAD *); +int __big_insert(HTAB *, BUFHEAD *, const DBT *, const DBT *); +int __big_keydata(HTAB *, BUFHEAD *, DBT *, DBT *, int); +int __big_return(HTAB *, BUFHEAD *, int, DBT *, int); +int __big_split(HTAB *, BUFHEAD *, BUFHEAD *, BUFHEAD *, + int, uint32_t, SPLIT_RETURN *); +int __buf_free(HTAB *, int, int); +void __buf_init(HTAB *, u_int); +uint32_t __call_hash(HTAB *, char *, int); +int __delpair(HTAB *, BUFHEAD *, int); +int __expand_table(HTAB *); +int __find_bigpair(HTAB *, BUFHEAD *, int, char *, int); +uint16_t __find_last_page(HTAB *, BUFHEAD **); +void __free_ovflpage(HTAB *, BUFHEAD *); +BUFHEAD *__get_buf(HTAB *, uint32_t, BUFHEAD *, int); +int __get_page(HTAB *, char *, uint32_t, int, int, int); +int __ibitmap(HTAB *, int, int, int); +uint32_t __log2(uint32_t); +int __put_page(HTAB *, char *, uint32_t, int, int); +void __reclaim_buf(HTAB *, BUFHEAD *); +int __split_page(HTAB *, uint32_t, uint32_t); + +/* Default hash routine. */ +extern uint32_t (*__default_hash)(const void *, size_t); + +#ifdef HASH_STATISTICS +extern int hash_accesses, hash_collisions, hash_expansions, hash_overflows; +#endif diff --git a/lib/nbsd_libc/db/hash/hash.c b/lib/nbsd_libc/db/hash/hash.c new file mode 100644 index 000000000..358a0719a --- /dev/null +++ b/lib/nbsd_libc/db/hash/hash.c @@ -0,0 +1,972 @@ +/* $NetBSD: hash.c,v 1.31 2009/02/12 06:35:54 lukem Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: hash.c,v 1.31 2009/02/12 06:35:54 lukem Exp $"); + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "hash.h" +#include "page.h" +#include "extern.h" + +static int alloc_segs(HTAB *, int); +static int flush_meta(HTAB *); +static int hash_access(HTAB *, ACTION, DBT *, DBT *); +static int hash_close(DB *); +static int hash_delete(const DB *, const DBT *, uint32_t); +static int hash_fd(const DB *); +static int hash_get(const DB *, const DBT *, DBT *, uint32_t); +static int hash_put(const DB *, DBT *, const DBT *, uint32_t); +static void *hash_realloc(SEGMENT **, size_t, size_t); +static int hash_seq(const DB *, DBT *, DBT *, uint32_t); +static int hash_sync(const DB *, uint32_t); +static int hdestroy(HTAB *); +static HTAB *init_hash(HTAB *, const char *, const HASHINFO *); +static int init_htab(HTAB *, size_t); +#if BYTE_ORDER == LITTLE_ENDIAN +static void swap_header(HTAB *); +static void swap_header_copy(HASHHDR *, HASHHDR *); +#endif + +/* Fast arithmetic, relying on powers of 2, */ +#define MOD(x, y) ((x) & ((y) - 1)) + +#define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; } + +/* Return values */ +#define SUCCESS (0) +#define ERROR (-1) +#define ABNORMAL (1) + +#ifdef HASH_STATISTICS +int hash_accesses, hash_collisions, hash_expansions, hash_overflows; +#endif + +/************************** INTERFACE ROUTINES ***************************/ +/* OPEN/CLOSE */ + +/* ARGSUSED */ +DB * +__hash_open(const char *file, int flags, mode_t mode, const HASHINFO *info, + int dflags) +{ + HTAB *hashp; + struct stat statbuf; + DB *dbp; + int bpages, new_table, nsegs, save_errno; + ssize_t hdrsize; + + if ((flags & O_ACCMODE) == O_WRONLY) { + errno = EINVAL; + return (NULL); + } + + if (!(hashp = calloc(1, sizeof(HTAB)))) + return (NULL); + hashp->fp = -1; + + /* + * Even if user wants write only, we need to be able to read + * the actual file, so we need to open it read/write. But, the + * field in the hashp structure needs to be accurate so that + * we can check accesses. + */ + hashp->flags = flags; + + new_table = 0; + if (!file || (flags & O_TRUNC) || + (stat(file, &statbuf) && (errno == ENOENT))) { + if (errno == ENOENT) + errno = 0; /* Just in case someone looks at errno */ + new_table = 1; + } + if (file) { + if ((hashp->fp = open(file, flags, mode)) == -1) + RETURN_ERROR(errno, error0); + if (fcntl(hashp->fp, F_SETFD, FD_CLOEXEC) == -1) + RETURN_ERROR(errno, error1); + if (fstat(hashp->fp, &statbuf) == -1) + RETURN_ERROR(errno, error1); + new_table |= statbuf.st_size == 0; + } + if (new_table) { + if (!(hashp = init_hash(hashp, file, info))) + RETURN_ERROR(errno, error1); + } else { + /* Table already exists */ + if (info && info->hash) + hashp->hash = info->hash; + else + hashp->hash = __default_hash; + + hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR)); +#if BYTE_ORDER == LITTLE_ENDIAN + swap_header(hashp); +#endif + if (hdrsize == -1) + RETURN_ERROR(errno, error1); + if (hdrsize != sizeof(HASHHDR)) + RETURN_ERROR(EFTYPE, error1); + /* Verify file type, versions and hash function */ + if (hashp->MAGIC != HASHMAGIC) + RETURN_ERROR(EFTYPE, error1); +#define OLDHASHVERSION 1 + if (hashp->VERSION != HASHVERSION && + hashp->VERSION != OLDHASHVERSION) + RETURN_ERROR(EFTYPE, error1); + if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != + (uint32_t)hashp->H_CHARKEY) + RETURN_ERROR(EFTYPE, error1); + /* + * Figure out how many segments we need. Max_Bucket is the + * maximum bucket number, so the number of buckets is + * max_bucket + 1. + */ + nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) / + hashp->SGSIZE; + hashp->nsegs = 0; + if (alloc_segs(hashp, nsegs)) + /* + * If alloc_segs fails, table will have been destroyed + * and errno will have been set. + */ + return (NULL); + /* Read in bitmaps */ + bpages = (hashp->SPARES[hashp->OVFL_POINT] + + (unsigned int)(hashp->BSIZE << BYTE_SHIFT) - 1) >> + (hashp->BSHIFT + BYTE_SHIFT); + + hashp->nmaps = bpages; + (void)memset(&hashp->mapp[0], 0, bpages * sizeof(uint32_t *)); + } + + /* Initialize Buffer Manager */ + if (info && info->cachesize) + __buf_init(hashp, info->cachesize); + else + __buf_init(hashp, DEF_BUFSIZE); + + hashp->new_file = new_table; + hashp->save_file = file && (hashp->flags & O_RDWR); + hashp->cbucket = -1; + if (!(dbp = malloc(sizeof(DB)))) { + save_errno = errno; + hdestroy(hashp); + errno = save_errno; + return (NULL); + } + dbp->internal = hashp; + dbp->close = hash_close; + dbp->del = hash_delete; + dbp->fd = hash_fd; + dbp->get = hash_get; + dbp->put = hash_put; + dbp->seq = hash_seq; + dbp->sync = hash_sync; + dbp->type = DB_HASH; + +#ifdef DEBUG + (void)fprintf(stderr, +"%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n", + "init_htab:", + "TABLE POINTER ", hashp, + "BUCKET SIZE ", hashp->BSIZE, + "BUCKET SHIFT ", hashp->BSHIFT, + "DIRECTORY SIZE ", hashp->DSIZE, + "SEGMENT SIZE ", hashp->SGSIZE, + "SEGMENT SHIFT ", hashp->SSHIFT, + "FILL FACTOR ", hashp->FFACTOR, + "MAX BUCKET ", hashp->MAX_BUCKET, + "OVFL POINT ", hashp->OVFL_POINT, + "LAST FREED ", hashp->LAST_FREED, + "HIGH MASK ", hashp->HIGH_MASK, + "LOW MASK ", hashp->LOW_MASK, + "NSEGS ", hashp->nsegs, + "NKEYS ", hashp->NKEYS); +#endif +#ifdef HASH_STATISTICS + hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0; +#endif + return (dbp); + +error1: + if (hashp != NULL) + (void)close(hashp->fp); + +error0: + free(hashp); + errno = save_errno; + return (NULL); +} + +static int +hash_close(DB *dbp) +{ + HTAB *hashp; + int retval; + + if (!dbp) + return (ERROR); + + hashp = dbp->internal; + retval = hdestroy(hashp); + free(dbp); + return (retval); +} + +static int +hash_fd(const DB *dbp) +{ + HTAB *hashp; + + if (!dbp) + return (ERROR); + + hashp = dbp->internal; + if (hashp->fp == -1) { + errno = ENOENT; + return (-1); + } + return (hashp->fp); +} + +/************************** LOCAL CREATION ROUTINES **********************/ +static HTAB * +init_hash(HTAB *hashp, const char *file, const HASHINFO *info) +{ + struct stat statbuf; + int nelem; + + nelem = 1; + hashp->NKEYS = 0; + hashp->LORDER = BYTE_ORDER; + hashp->BSIZE = DEF_BUCKET_SIZE; + hashp->BSHIFT = DEF_BUCKET_SHIFT; + hashp->SGSIZE = DEF_SEGSIZE; + hashp->SSHIFT = DEF_SEGSIZE_SHIFT; + hashp->DSIZE = DEF_DIRSIZE; + hashp->FFACTOR = DEF_FFACTOR; + hashp->hash = __default_hash; + memset(hashp->SPARES, 0, sizeof(hashp->SPARES)); + memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS)); + + /* Fix bucket size to be optimal for file system */ + if (file != NULL) { + if (stat(file, &statbuf)) + return (NULL); + hashp->BSIZE = MIN(statbuf.st_blksize, MAX_BSIZE); + hashp->BSHIFT = __log2((uint32_t)hashp->BSIZE); + } + + if (info) { + if (info->bsize) { + /* Round pagesize up to power of 2 */ + hashp->BSHIFT = __log2(info->bsize); + hashp->BSIZE = 1 << hashp->BSHIFT; + if (hashp->BSIZE > MAX_BSIZE) { + errno = EINVAL; + return (NULL); + } + } + if (info->ffactor) + hashp->FFACTOR = info->ffactor; + if (info->hash) + hashp->hash = info->hash; + if (info->nelem) + nelem = info->nelem; + if (info->lorder) { + if (info->lorder != BIG_ENDIAN && + info->lorder != LITTLE_ENDIAN) { + errno = EINVAL; + return (NULL); + } + hashp->LORDER = info->lorder; + } + } + /* init_htab should destroy the table and set errno if it fails */ + if (init_htab(hashp, (size_t)nelem)) + return (NULL); + else + return (hashp); +} +/* + * This calls alloc_segs which may run out of memory. Alloc_segs will destroy + * the table and set errno, so we just pass the error information along. + * + * Returns 0 on No Error + */ +static int +init_htab(HTAB *hashp, size_t nelem) +{ + int nbuckets; + uint32_t nsegs; + int l2; + + /* + * Divide number of elements by the fill factor and determine a + * desired number of buckets. Allocate space for the next greater + * power of two number of buckets. + */ + nelem = (nelem - 1) / hashp->FFACTOR + 1; + + _DBFIT(nelem, uint32_t); + l2 = __log2(MAX((uint32_t)nelem, 2)); + nbuckets = 1 << l2; + + hashp->SPARES[l2] = l2 + 1; + hashp->SPARES[l2 + 1] = l2 + 1; + hashp->OVFL_POINT = l2; + hashp->LAST_FREED = 2; + + /* First bitmap page is at: splitpoint l2 page offset 1 */ + if (__ibitmap(hashp, (int)OADDR_OF(l2, 1), l2 + 1, 0)) + return (-1); + + hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1; + hashp->HIGH_MASK = (nbuckets << 1) - 1; + /* LINTED constant in conditional context */ + hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >> + hashp->BSHIFT) + 1; + + nsegs = (nbuckets - 1) / hashp->SGSIZE + 1; + nsegs = 1 << __log2(nsegs); + + if (nsegs > (uint32_t)hashp->DSIZE) + hashp->DSIZE = nsegs; + return (alloc_segs(hashp, (int)nsegs)); +} + +/********************** DESTROY/CLOSE ROUTINES ************************/ + +/* + * Flushes any changes to the file if necessary and destroys the hashp + * structure, freeing all allocated space. + */ +static int +hdestroy(HTAB *hashp) +{ + int i, save_errno; + + save_errno = 0; + +#ifdef HASH_STATISTICS + (void)fprintf(stderr, "hdestroy: accesses %d collisions %d\n", + hash_accesses, hash_collisions); + (void)fprintf(stderr, "hdestroy: expansions %d\n", + hash_expansions); + (void)fprintf(stderr, "hdestroy: overflows %d\n", + hash_overflows); + (void)fprintf(stderr, "keys %d maxp %d segmentcount %d\n", + hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs); + + for (i = 0; i < NCACHED; i++) + (void)fprintf(stderr, + "spares[%d] = %d\n", i, hashp->SPARES[i]); +#endif + /* + * Call on buffer manager to free buffers, and if required, + * write them to disk. + */ + if (__buf_free(hashp, 1, hashp->save_file)) + save_errno = errno; + if (hashp->dir) { + free(*hashp->dir); /* Free initial segments */ + /* Free extra segments */ + while (hashp->exsegs--) + free(hashp->dir[--hashp->nsegs]); + free(hashp->dir); + } + if (flush_meta(hashp) && !save_errno) + save_errno = errno; + /* Free Bigmaps */ + for (i = 0; i < hashp->nmaps; i++) + if (hashp->mapp[i]) + free(hashp->mapp[i]); + + if (hashp->fp != -1) + (void)close(hashp->fp); + + free(hashp); + + if (save_errno) { + errno = save_errno; + return (ERROR); + } + return (SUCCESS); +} +/* + * Write modified pages to disk + * + * Returns: + * 0 == OK + * -1 ERROR + */ +static int +hash_sync(const DB *dbp, uint32_t flags) +{ + HTAB *hashp; + + if (flags != 0) { + errno = EINVAL; + return (ERROR); + } + + if (!dbp) + return (ERROR); + + hashp = dbp->internal; + if (!hashp->save_file) + return (0); + if (__buf_free(hashp, 0, 1) || flush_meta(hashp)) + return (ERROR); + hashp->new_file = 0; + return (0); +} + +/* + * Returns: + * 0 == OK + * -1 indicates that errno should be set + */ +static int +flush_meta(HTAB *hashp) +{ + HASHHDR *whdrp; +#if BYTE_ORDER == LITTLE_ENDIAN + HASHHDR whdr; +#endif + int fp, i; + ssize_t wsize; + + if (!hashp->save_file) + return (0); + hashp->MAGIC = HASHMAGIC; + hashp->VERSION = HASHVERSION; + hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY)); + + fp = hashp->fp; + whdrp = &hashp->hdr; +#if BYTE_ORDER == LITTLE_ENDIAN + whdrp = &whdr; + swap_header_copy(&hashp->hdr, whdrp); +#endif + if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), (off_t)0)) == -1) + return (-1); + else + if (wsize != sizeof(HASHHDR)) { + errno = EFTYPE; + hashp->err = errno; + return (-1); + } + for (i = 0; i < NCACHED; i++) + if (hashp->mapp[i]) + if (__put_page(hashp, (char *)(void *)hashp->mapp[i], + (u_int)hashp->BITMAPS[i], 0, 1)) + return (-1); + return (0); +} + +/*******************************SEARCH ROUTINES *****************************/ +/* + * All the access routines return + * + * Returns: + * 0 on SUCCESS + * 1 to indicate an external ERROR (i.e. key not found, etc) + * -1 to indicate an internal ERROR (i.e. out of memory, etc) + */ +static int +hash_get(const DB *dbp, const DBT *key, DBT *data, uint32_t flag) +{ + HTAB *hashp; + + hashp = dbp->internal; + if (flag) { + hashp->err = errno = EINVAL; + return (ERROR); + } + return (hash_access(hashp, HASH_GET, __UNCONST(key), data)); +} + +static int +hash_put(const DB *dbp, DBT *key, const DBT *data, uint32_t flag) +{ + HTAB *hashp; + + hashp = dbp->internal; + if (flag && flag != R_NOOVERWRITE) { + hashp->err = errno = EINVAL; + return (ERROR); + } + if ((hashp->flags & O_ACCMODE) == O_RDONLY) { + hashp->err = errno = EPERM; + return (ERROR); + } + /* LINTED const castaway */ + return (hash_access(hashp, flag == R_NOOVERWRITE ? + HASH_PUTNEW : HASH_PUT, __UNCONST(key), __UNCONST(data))); +} + +static int +hash_delete(const DB *dbp, const DBT *key, uint32_t flag) +{ + HTAB *hashp; + + hashp = dbp->internal; + if (flag && flag != R_CURSOR) { + hashp->err = errno = EINVAL; + return (ERROR); + } + if ((hashp->flags & O_ACCMODE) == O_RDONLY) { + hashp->err = errno = EPERM; + return (ERROR); + } + return hash_access(hashp, HASH_DELETE, __UNCONST(key), NULL); +} + +/* + * Assume that hashp has been set in wrapper routine. + */ +static int +hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val) +{ + BUFHEAD *rbufp; + BUFHEAD *bufp, *save_bufp; + uint16_t *bp; + int n, ndx, off; + size_t size; + char *kp; + uint16_t pageno; + +#ifdef HASH_STATISTICS + hash_accesses++; +#endif + + off = hashp->BSIZE; + size = key->size; + kp = (char *)key->data; + rbufp = __get_buf(hashp, __call_hash(hashp, kp, (int)size), NULL, 0); + if (!rbufp) + return (ERROR); + save_bufp = rbufp; + + /* Pin the bucket chain */ + rbufp->flags |= BUF_PIN; + for (bp = (uint16_t *)(void *)rbufp->page, n = *bp++, ndx = 1; ndx < n;) + if (bp[1] >= REAL_KEY) { + /* Real key/data pair */ + if (size == (size_t)(off - *bp) && + memcmp(kp, rbufp->page + *bp, size) == 0) + goto found; + off = bp[1]; +#ifdef HASH_STATISTICS + hash_collisions++; +#endif + bp += 2; + ndx += 2; + } else if (bp[1] == OVFLPAGE) { + rbufp = __get_buf(hashp, (uint32_t)*bp, rbufp, 0); + if (!rbufp) { + save_bufp->flags &= ~BUF_PIN; + return (ERROR); + } + /* FOR LOOP INIT */ + bp = (uint16_t *)(void *)rbufp->page; + n = *bp++; + ndx = 1; + off = hashp->BSIZE; + } else if (bp[1] < REAL_KEY) { + if ((ndx = + __find_bigpair(hashp, rbufp, ndx, kp, (int)size)) > 0) + goto found; + if (ndx == -2) { + bufp = rbufp; + if (!(pageno = + __find_last_page(hashp, &bufp))) { + ndx = 0; + rbufp = bufp; + break; /* FOR */ + } + rbufp = __get_buf(hashp, (uint32_t)pageno, + bufp, 0); + if (!rbufp) { + save_bufp->flags &= ~BUF_PIN; + return (ERROR); + } + /* FOR LOOP INIT */ + bp = (uint16_t *)(void *)rbufp->page; + n = *bp++; + ndx = 1; + off = hashp->BSIZE; + } else { + save_bufp->flags &= ~BUF_PIN; + return (ERROR); + } + } + + /* Not found */ + switch (action) { + case HASH_PUT: + case HASH_PUTNEW: + if (__addel(hashp, rbufp, key, val)) { + save_bufp->flags &= ~BUF_PIN; + return (ERROR); + } else { + save_bufp->flags &= ~BUF_PIN; + return (SUCCESS); + } + case HASH_GET: + case HASH_DELETE: + default: + save_bufp->flags &= ~BUF_PIN; + return (ABNORMAL); + } + +found: + switch (action) { + case HASH_PUTNEW: + save_bufp->flags &= ~BUF_PIN; + return (ABNORMAL); + case HASH_GET: + bp = (uint16_t *)(void *)rbufp->page; + if (bp[ndx + 1] < REAL_KEY) { + if (__big_return(hashp, rbufp, ndx, val, 0)) + return (ERROR); + } else { + val->data = (uint8_t *)rbufp->page + (int)bp[ndx + 1]; + val->size = bp[ndx] - bp[ndx + 1]; + } + break; + case HASH_PUT: + if ((__delpair(hashp, rbufp, ndx)) || + (__addel(hashp, rbufp, key, val))) { + save_bufp->flags &= ~BUF_PIN; + return (ERROR); + } + break; + case HASH_DELETE: + if (__delpair(hashp, rbufp, ndx)) + return (ERROR); + break; + default: + abort(); + } + save_bufp->flags &= ~BUF_PIN; + return (SUCCESS); +} + +static int +hash_seq(const DB *dbp, DBT *key, DBT *data, uint32_t flag) +{ + uint32_t bucket; + BUFHEAD *bufp = NULL; /* XXX: gcc */ + HTAB *hashp; + uint16_t *bp, ndx; + + hashp = dbp->internal; + if (flag && flag != R_FIRST && flag != R_NEXT) { + hashp->err = errno = EINVAL; + return (ERROR); + } +#ifdef HASH_STATISTICS + hash_accesses++; +#endif + if ((hashp->cbucket < 0) || (flag == R_FIRST)) { + hashp->cbucket = 0; + hashp->cndx = 1; + hashp->cpage = NULL; + } + + for (bp = NULL; !bp || !bp[0]; ) { + if (!(bufp = hashp->cpage)) { + for (bucket = hashp->cbucket; + bucket <= (uint32_t)hashp->MAX_BUCKET; + bucket++, hashp->cndx = 1) { + bufp = __get_buf(hashp, bucket, NULL, 0); + if (!bufp) + return (ERROR); + hashp->cpage = bufp; + bp = (uint16_t *)(void *)bufp->page; + if (bp[0]) + break; + } + hashp->cbucket = bucket; + if (hashp->cbucket > hashp->MAX_BUCKET) { + hashp->cbucket = -1; + return (ABNORMAL); + } + } else + bp = (uint16_t *)(void *)hashp->cpage->page; + + _DIAGASSERT(bp != NULL); + _DIAGASSERT(bufp != NULL); + while (bp[hashp->cndx + 1] == OVFLPAGE) { + bufp = hashp->cpage = + __get_buf(hashp, (uint32_t)bp[hashp->cndx], bufp, + 0); + if (!bufp) + return (ERROR); + bp = (uint16_t *)(void *)(bufp->page); + hashp->cndx = 1; + } + if (!bp[0]) { + hashp->cpage = NULL; + ++hashp->cbucket; + } + } + ndx = hashp->cndx; + if (bp[ndx + 1] < REAL_KEY) { + if (__big_keydata(hashp, bufp, key, data, 1)) + return (ERROR); + } else { + if (hashp->cpage == NULL) + return (ERROR); + key->data = (uint8_t *)hashp->cpage->page + bp[ndx]; + key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx]; + data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1]; + data->size = bp[ndx] - bp[ndx + 1]; + ndx += 2; + if (ndx > bp[0]) { + hashp->cpage = NULL; + hashp->cbucket++; + hashp->cndx = 1; + } else + hashp->cndx = ndx; + } + return (SUCCESS); +} + +/********************************* UTILITIES ************************/ + +/* + * Returns: + * 0 ==> OK + * -1 ==> Error + */ +int +__expand_table(HTAB *hashp) +{ + uint32_t old_bucket, new_bucket; + int new_segnum, spare_ndx; + size_t dirsize; + +#ifdef HASH_STATISTICS + hash_expansions++; +#endif + new_bucket = ++hashp->MAX_BUCKET; + old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK); + + new_segnum = new_bucket >> hashp->SSHIFT; + + /* Check if we need a new segment */ + if (new_segnum >= hashp->nsegs) { + /* Check if we need to expand directory */ + if (new_segnum >= hashp->DSIZE) { + /* Reallocate directory */ + dirsize = hashp->DSIZE * sizeof(SEGMENT *); + if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1)) + return (-1); + hashp->DSIZE = dirsize << 1; + } + if ((hashp->dir[new_segnum] = + calloc((size_t)hashp->SGSIZE, sizeof(SEGMENT))) == NULL) + return (-1); + hashp->exsegs++; + hashp->nsegs++; + } + /* + * If the split point is increasing (MAX_BUCKET's log base 2 + * * increases), we need to copy the current contents of the spare + * split bucket to the next bucket. + */ + spare_ndx = __log2((uint32_t)(hashp->MAX_BUCKET + 1)); + if (spare_ndx > hashp->OVFL_POINT) { + hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT]; + hashp->OVFL_POINT = spare_ndx; + } + + if (new_bucket > (uint32_t)hashp->HIGH_MASK) { + /* Starting a new doubling */ + hashp->LOW_MASK = hashp->HIGH_MASK; + hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK; + } + /* Relocate records to the new bucket */ + return (__split_page(hashp, old_bucket, new_bucket)); +} + +/* + * If realloc guarantees that the pointer is not destroyed if the realloc + * fails, then this routine can go away. + */ +static void * +hash_realloc(SEGMENT **p_ptr, size_t oldsize, size_t newsize) +{ + void *p; + + if ((p = malloc(newsize)) != NULL) { + memmove(p, *p_ptr, oldsize); + memset((char *)p + oldsize, 0, newsize - oldsize); + free(*p_ptr); + *p_ptr = p; + } + return (p); +} + +uint32_t +__call_hash(HTAB *hashp, char *k, int len) +{ + int n, bucket; + + n = hashp->hash(k, (size_t)len); + bucket = n & hashp->HIGH_MASK; + if (bucket > hashp->MAX_BUCKET) + bucket = bucket & hashp->LOW_MASK; + return (bucket); +} + +/* + * Allocate segment table. On error, destroy the table and set errno. + * + * Returns 0 on success + */ +static int +alloc_segs(HTAB *hashp, int nsegs) +{ + int i; + SEGMENT store; + + int save_errno; + + hashp->dir = calloc((size_t)hashp->DSIZE, sizeof(SEGMENT *)); + if (hashp->dir == NULL) { + save_errno = errno; + (void)hdestroy(hashp); + errno = save_errno; + return (-1); + } + hashp->nsegs = nsegs; + if (nsegs == 0) + return 0; + /* Allocate segments */ + store = calloc((size_t)(nsegs << hashp->SSHIFT), sizeof(SEGMENT)); + if (store == NULL) { + save_errno = errno; + (void)hdestroy(hashp); + errno = save_errno; + return (-1); + } + for (i = 0; i < nsegs; i++) + hashp->dir[i] = &store[i << hashp->SSHIFT]; + return (0); +} + +#if BYTE_ORDER == LITTLE_ENDIAN +/* + * Hashp->hdr needs to be byteswapped. + */ +static void +swap_header_copy(HASHHDR *srcp, HASHHDR *destp) +{ + size_t i; + + P_32_COPY(srcp->magic, destp->magic); + P_32_COPY(srcp->version, destp->version); + P_32_COPY(srcp->lorder, destp->lorder); + P_32_COPY(srcp->bsize, destp->bsize); + P_32_COPY(srcp->bshift, destp->bshift); + P_32_COPY(srcp->dsize, destp->dsize); + P_32_COPY(srcp->ssize, destp->ssize); + P_32_COPY(srcp->sshift, destp->sshift); + P_32_COPY(srcp->ovfl_point, destp->ovfl_point); + P_32_COPY(srcp->last_freed, destp->last_freed); + P_32_COPY(srcp->max_bucket, destp->max_bucket); + P_32_COPY(srcp->high_mask, destp->high_mask); + P_32_COPY(srcp->low_mask, destp->low_mask); + P_32_COPY(srcp->ffactor, destp->ffactor); + P_32_COPY(srcp->nkeys, destp->nkeys); + P_32_COPY(srcp->hdrpages, destp->hdrpages); + P_32_COPY(srcp->h_charkey, destp->h_charkey); + for (i = 0; i < NCACHED; i++) { + P_32_COPY(srcp->spares[i], destp->spares[i]); + P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]); + } +} + +static void +swap_header(HTAB *hashp) +{ + HASHHDR *hdrp; + size_t i; + + hdrp = &hashp->hdr; + + M_32_SWAP(hdrp->magic); + M_32_SWAP(hdrp->version); + M_32_SWAP(hdrp->lorder); + M_32_SWAP(hdrp->bsize); + M_32_SWAP(hdrp->bshift); + M_32_SWAP(hdrp->dsize); + M_32_SWAP(hdrp->ssize); + M_32_SWAP(hdrp->sshift); + M_32_SWAP(hdrp->ovfl_point); + M_32_SWAP(hdrp->last_freed); + M_32_SWAP(hdrp->max_bucket); + M_32_SWAP(hdrp->high_mask); + M_32_SWAP(hdrp->low_mask); + M_32_SWAP(hdrp->ffactor); + M_32_SWAP(hdrp->nkeys); + M_32_SWAP(hdrp->hdrpages); + M_32_SWAP(hdrp->h_charkey); + for (i = 0; i < NCACHED; i++) { + M_32_SWAP(hdrp->spares[i]); + M_16_SWAP(hdrp->bitmaps[i]); + } +} +#endif diff --git a/lib/nbsd_libc/db/hash/hash.h b/lib/nbsd_libc/db/hash/hash.h new file mode 100644 index 000000000..f3c87a86e --- /dev/null +++ b/lib/nbsd_libc/db/hash/hash.h @@ -0,0 +1,295 @@ +/* $NetBSD: hash.h,v 1.15 2008/08/26 21:18:38 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * 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. + * + * @(#)hash.h 8.3 (Berkeley) 5/31/94 + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +/* Operations */ +typedef enum { + HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT +} ACTION; + +/* Buffer Management structures */ +typedef struct _bufhead BUFHEAD; + +struct _bufhead { + BUFHEAD *prev; /* LRU links */ + BUFHEAD *next; /* LRU links */ + BUFHEAD *ovfl; /* Overflow page buffer header */ + uint32_t addr; /* Address of this page */ + char *page; /* Actual page data */ + char flags; +#define BUF_MOD 0x0001 +#define BUF_DISK 0x0002 +#define BUF_BUCKET 0x0004 +#define BUF_PIN 0x0008 +}; + +#define IS_BUCKET(X) ((X) & BUF_BUCKET) + +typedef BUFHEAD **SEGMENT; + +/* Hash Table Information */ +typedef struct hashhdr { /* Disk resident portion */ + int32_t magic; /* Magic NO for hash tables */ + int32_t version; /* Version ID */ + uint32_t lorder; /* Byte Order */ + int32_t bsize; /* Bucket/Page Size */ + int32_t bshift; /* Bucket shift */ + int32_t dsize; /* Directory Size */ + int32_t ssize; /* Segment Size */ + int32_t sshift; /* Segment shift */ + int32_t ovfl_point; /* Where overflow pages are being + * allocated */ + int32_t last_freed; /* Last overflow page freed */ + int32_t max_bucket; /* ID of Maximum bucket in use */ + int32_t high_mask; /* Mask to modulo into entire table */ + int32_t low_mask; /* Mask to modulo into lower half of + * table */ + int32_t ffactor; /* Fill factor */ + int32_t nkeys; /* Number of keys in hash table */ + int32_t hdrpages; /* Size of table header */ + int32_t h_charkey; /* value of hash(CHARKEY) */ +#define NCACHED 32 /* number of bit maps and spare + * points */ + int32_t spares[NCACHED];/* spare pages for overflow */ + uint16_t bitmaps[NCACHED]; /* address of overflow page + * bitmaps */ +} HASHHDR; + +typedef struct htab { /* Memory resident data structure */ + HASHHDR hdr; /* Header */ + int nsegs; /* Number of allocated segments */ + int exsegs; /* Number of extra allocated + * segments */ + uint32_t (*hash)(const void *, size_t); /* Hash function */ + int flags; /* Flag values */ + int fp; /* File pointer */ + char *tmp_buf; /* Temporary Buffer for BIG data */ + char *tmp_key; /* Temporary Buffer for BIG keys */ + BUFHEAD *cpage; /* Current page */ + int cbucket; /* Current bucket */ + int cndx; /* Index of next item on cpage */ + int err; /* Error Number -- for DBM + * compatibility */ + int new_file; /* Indicates if fd is backing store + * or no */ + int save_file; /* Indicates whether we need to flush + * file at + * exit */ + uint32_t *mapp[NCACHED]; /* Pointers to page maps */ + int nmaps; /* Initial number of bitmaps */ + int nbufs; /* Number of buffers left to + * allocate */ + BUFHEAD bufhead; /* Header of buffer lru list */ + SEGMENT *dir; /* Hash Bucket directory */ +} HTAB; + +/* + * Constants + */ +#define MAX_BSIZE 65536 /* 2^16 */ +#define MIN_BUFFERS 6 +#define MINHDRSIZE 512 +#define DEF_BUFSIZE 65536 /* 64 K */ +#define DEF_BUCKET_SIZE 4096 +#define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */ +#define DEF_SEGSIZE 256 +#define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */ +#define DEF_DIRSIZE 256 +#define DEF_FFACTOR 65536 +#define MIN_FFACTOR 4 +#define SPLTMAX 8 +#define CHARKEY "%$sniglet^&" +#define NUMKEY 1038583 +#define BYTE_SHIFT 3 +#define INT_TO_BYTE 2 +#define INT_BYTE_SHIFT 5 +#define ALL_SET ((uint32_t)0xFFFFFFFF) +#define ALL_CLEAR 0 + +#define PTROF(X) ((BUFHEAD *)(void *)((u_long)(X)&~0x3)) +#define ISMOD(X) ((uint32_t)(u_long)(X)&0x1) +#define DOMOD(X) ((X) = (char *)(void *)((u_long)(X)|0x1)) +#define ISDISK(X) ((uint32_t)(u_long)(X)&0x2) +#define DODISK(X) ((X) = (char *)(void *)((u_long)(X)|0x2)) + +#define BITS_PER_MAP 32 + +/* Given the address of the beginning of a big map, clear/set the nth bit */ +#define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP))) +#define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP))) +#define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP))) + +/* Overflow management */ +/* + * Overflow page numbers are allocated per split point. At each doubling of + * the table, we can allocate extra pages. So, an overflow page number has + * the top 5 bits indicate which split point and the lower 11 bits indicate + * which page at that split point is indicated (pages within split points are + * numberered starting with 1). + */ + +#define SPLITSHIFT 11 +#define SPLITMASK 0x7FF +#define SPLITNUM(N) (((uint32_t)(N)) >> SPLITSHIFT) +#define OPAGENUM(N) ((N) & SPLITMASK) +#define OADDR_OF(S,O) ((uint32_t)((uint32_t)(S) << SPLITSHIFT) + (O)) + +#define BUCKET_TO_PAGE(B) \ + (B) + hashp->HDRPAGES + \ + ((B) ? hashp->SPARES[__log2((uint32_t)((B)+1))-1] : 0) +#define OADDR_TO_PAGE(B) \ + BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B)); + +/* + * page.h contains a detailed description of the page format. + * + * Normally, keys and data are accessed from offset tables in the top of + * each page which point to the beginning of the key and data. There are + * four flag values which may be stored in these offset tables which indicate + * the following: + * + * + * OVFLPAGE Rather than a key data pair, this pair contains + * the address of an overflow page. The format of + * the pair is: + * OVERFLOW_PAGE_NUMBER OVFLPAGE + * + * PARTIAL_KEY This must be the first key/data pair on a page + * and implies that page contains only a partial key. + * That is, the key is too big to fit on a single page + * so it starts on this page and continues on the next. + * The format of the page is: + * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE + * + * KEY_OFF -- offset of the beginning of the key + * PARTIAL_KEY -- 1 + * OVFL_PAGENO - page number of the next overflow page + * OVFLPAGE -- 0 + * + * FULL_KEY This must be the first key/data pair on the page. It + * is used in two cases. + * + * Case 1: + * There is a complete key on the page but no data + * (because it wouldn't fit). The next page contains + * the data. + * + * Page format it: + * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE + * + * KEY_OFF -- offset of the beginning of the key + * FULL_KEY -- 2 + * OVFL_PAGENO - page number of the next overflow page + * OVFLPAGE -- 0 + * + * Case 2: + * This page contains no key, but part of a large + * data field, which is continued on the next page. + * + * Page format it: + * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE + * + * KEY_OFF -- offset of the beginning of the data on + * this page + * FULL_KEY -- 2 + * OVFL_PAGENO - page number of the next overflow page + * OVFLPAGE -- 0 + * + * FULL_KEY_DATA + * This must be the first key/data pair on the page. + * There are two cases: + * + * Case 1: + * This page contains a key and the beginning of the + * data field, but the data field is continued on the + * next page. + * + * Page format is: + * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF + * + * KEY_OFF -- offset of the beginning of the key + * FULL_KEY_DATA -- 3 + * OVFL_PAGENO - page number of the next overflow page + * DATA_OFF -- offset of the beginning of the data + * + * Case 2: + * This page contains the last page of a big data pair. + * There is no key, only the tail end of the data + * on this page. + * + * Page format is: + * DATA_OFF FULL_KEY_DATA + * + * DATA_OFF -- offset of the beginning of the data on + * this page + * FULL_KEY_DATA -- 3 + * OVFL_PAGENO - page number of the next overflow page + * OVFLPAGE -- 0 + * + * OVFL_PAGENO and OVFLPAGE are optional (they are + * not present if there is no next page). + */ + +#define OVFLPAGE 0 +#define PARTIAL_KEY 1 +#define FULL_KEY 2 +#define FULL_KEY_DATA 3 +#define REAL_KEY 4 + +/* Short hands for accessing structure */ +#define BSIZE hdr.bsize +#define BSHIFT hdr.bshift +#define DSIZE hdr.dsize +#define SGSIZE hdr.ssize +#define SSHIFT hdr.sshift +#define LORDER hdr.lorder +#define OVFL_POINT hdr.ovfl_point +#define LAST_FREED hdr.last_freed +#define MAX_BUCKET hdr.max_bucket +#define FFACTOR hdr.ffactor +#define HIGH_MASK hdr.high_mask +#define LOW_MASK hdr.low_mask +#define NKEYS hdr.nkeys +#define HDRPAGES hdr.hdrpages +#define SPARES hdr.spares +#define BITMAPS hdr.bitmaps +#define VERSION hdr.version +#define MAGIC hdr.magic +#define NEXT_FREE hdr.next_free +#define H_CHARKEY hdr.h_charkey diff --git a/lib/nbsd_libc/db/hash/hash_bigkey.c b/lib/nbsd_libc/db/hash/hash_bigkey.c new file mode 100644 index 000000000..5c6fc9b18 --- /dev/null +++ b/lib/nbsd_libc/db/hash/hash_bigkey.c @@ -0,0 +1,674 @@ +/* $NetBSD: hash_bigkey.c,v 1.23 2009/02/12 06:33:13 lukem Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: hash_bigkey.c,v 1.23 2009/02/12 06:33:13 lukem Exp $"); + +/* + * PACKAGE: hash + * DESCRIPTION: + * Big key/data handling for the hashing package. + * + * ROUTINES: + * External + * __big_keydata + * __big_split + * __big_insert + * __big_return + * __big_delete + * __find_last_page + * Internal + * collect_key + * collect_data + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include "hash.h" +#include "page.h" +#include "extern.h" + +static int collect_key(HTAB *, BUFHEAD *, int, DBT *, int); +static int collect_data(HTAB *, BUFHEAD *, int, int); + +/* + * Big_insert + * + * You need to do an insert and the key/data pair is too big + * + * Returns: + * 0 ==> OK + *-1 ==> ERROR + */ +int +__big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val) +{ + uint16_t *p, n; + size_t key_size, val_size; + uint16_t space, move_bytes, off; + char *cp, *key_data, *val_data; + size_t temp; + + cp = bufp->page; /* Character pointer of p. */ + p = (uint16_t *)(void *)cp; + + key_data = (char *)key->data; + _DBFIT(key->size, int); + key_size = key->size; + val_data = (char *)val->data; + _DBFIT(val->size, int); + val_size = val->size; + + /* First move the Key */ + + temp = FREESPACE(p) - BIGOVERHEAD; + _DBFIT(temp, uint16_t); + space = (uint16_t)temp; + while (key_size) { + move_bytes = MIN(space, key_size); + off = OFFSET(p) - move_bytes; + memmove(cp + off, key_data, (size_t)move_bytes); + key_size -= move_bytes; + key_data += move_bytes; + n = p[0]; + p[++n] = off; + p[0] = ++n; + temp = off - PAGE_META(n); + _DBFIT(temp, uint16_t); + FREESPACE(p) = (uint16_t)temp; + OFFSET(p) = off; + p[n] = PARTIAL_KEY; + bufp = __add_ovflpage(hashp, bufp); + if (!bufp) + return (-1); + n = p[0]; + if (!key_size) { + space = FREESPACE(p); + if (space) { + move_bytes = MIN(space, val_size); + /* + * If the data would fit exactly in the + * remaining space, we must overflow it to the + * next page; otherwise the invariant that the + * data must end on a page with FREESPACE + * non-zero would fail. + */ + if (space == val_size && val_size == val->size) + goto toolarge; + off = OFFSET(p) - move_bytes; + memmove(cp + off, val_data, (size_t)move_bytes); + val_data += move_bytes; + val_size -= move_bytes; + p[n] = off; + p[n - 2] = FULL_KEY_DATA; + FREESPACE(p) = FREESPACE(p) - move_bytes; + OFFSET(p) = off; + } else { + toolarge: + p[n - 2] = FULL_KEY; + } + } + p = (uint16_t *)(void *)bufp->page; + cp = bufp->page; + bufp->flags |= BUF_MOD; + temp = FREESPACE(p) - BIGOVERHEAD; + _DBFIT(temp, uint16_t); + space = (uint16_t)temp; + } + + /* Now move the data */ + temp = FREESPACE(p) - BIGOVERHEAD; + _DBFIT(temp, uint16_t); + space = (uint16_t)temp; + while (val_size) { + move_bytes = MIN(space, val_size); + /* + * Here's the hack to make sure that if the data ends on the + * same page as the key ends, FREESPACE is at least one. + */ + if (space == val_size && val_size == val->size) + move_bytes--; + off = OFFSET(p) - move_bytes; + memmove(cp + off, val_data, (size_t)move_bytes); + val_size -= move_bytes; + val_data += move_bytes; + n = p[0]; + p[++n] = off; + p[0] = ++n; + temp = off - PAGE_META(n); + _DBFIT(temp, uint16_t); + FREESPACE(p) = (uint16_t)temp; + OFFSET(p) = off; + if (val_size) { + p[n] = FULL_KEY; + bufp = __add_ovflpage(hashp, bufp); + if (!bufp) + return (-1); + cp = bufp->page; + p = (uint16_t *)(void *)cp; + } else + p[n] = FULL_KEY_DATA; + bufp->flags |= BUF_MOD; + temp = FREESPACE(p) - BIGOVERHEAD; + _DBFIT(temp, uint16_t); + space = (uint16_t)temp; + } + return (0); +} + +/* + * Called when bufp's page contains a partial key (index should be 1) + * + * All pages in the big key/data pair except bufp are freed. We cannot + * free bufp because the page pointing to it is lost and we can't get rid + * of its pointer. + * + * Returns: + * 0 => OK + *-1 => ERROR + */ +int +__big_delete(HTAB *hashp, BUFHEAD *bufp) +{ + BUFHEAD *last_bfp, *rbufp; + uint16_t *bp, pageno; + int key_done, n; + size_t temp; + + rbufp = bufp; + last_bfp = NULL; + bp = (uint16_t *)(void *)bufp->page; + pageno = 0; + key_done = 0; + + while (!key_done || (bp[2] != FULL_KEY_DATA)) { + if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) + key_done = 1; + + /* + * If there is freespace left on a FULL_KEY_DATA page, then + * the data is short and fits entirely on this page, and this + * is the last page. + */ + if (bp[2] == FULL_KEY_DATA && FREESPACE(bp)) + break; + pageno = bp[bp[0] - 1]; + rbufp->flags |= BUF_MOD; + rbufp = __get_buf(hashp, (uint32_t)pageno, rbufp, 0); + if (last_bfp) + __free_ovflpage(hashp, last_bfp); + last_bfp = rbufp; + if (!rbufp) + return (-1); /* Error. */ + bp = (uint16_t *)(void *)rbufp->page; + } + + /* + * If we get here then rbufp points to the last page of the big + * key/data pair. Bufp points to the first one -- it should now be + * empty pointing to the next page after this pair. Can't free it + * because we don't have the page pointing to it. + */ + + /* This is information from the last page of the pair. */ + n = bp[0]; + pageno = bp[n - 1]; + + /* Now, bp is the first page of the pair. */ + bp = (uint16_t *)(void *)bufp->page; + if (n > 2) { + /* There is an overflow page. */ + bp[1] = pageno; + bp[2] = OVFLPAGE; + bufp->ovfl = rbufp->ovfl; + } else + /* This is the last page. */ + bufp->ovfl = NULL; + n -= 2; + bp[0] = n; + temp = hashp->BSIZE - PAGE_META(n); + _DBFIT(temp, uint16_t); + FREESPACE(bp) = (uint16_t)temp; + OFFSET(bp) = hashp->BSIZE; + + bufp->flags |= BUF_MOD; + if (rbufp) + __free_ovflpage(hashp, rbufp); + if (last_bfp && last_bfp != rbufp) + __free_ovflpage(hashp, last_bfp); + + hashp->NKEYS--; + return (0); +} +/* + * Returns: + * 0 = key not found + * -1 = get next overflow page + * -2 means key not found and this is big key/data + * -3 error + */ +int +__find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size) +{ + uint16_t *bp; + char *p; + int ksize; + uint16_t bytes; + char *kkey; + + bp = (uint16_t *)(void *)bufp->page; + p = bufp->page; + ksize = size; + kkey = key; + + for (bytes = hashp->BSIZE - bp[ndx]; + bytes <= size && bp[ndx + 1] == PARTIAL_KEY; + bytes = hashp->BSIZE - bp[ndx]) { + if (memcmp(p + bp[ndx], kkey, (size_t)bytes)) + return (-2); + kkey += bytes; + ksize -= bytes; + bufp = __get_buf(hashp, (uint32_t)bp[ndx + 2], bufp, 0); + if (!bufp) + return (-3); + p = bufp->page; + bp = (uint16_t *)(void *)p; + ndx = 1; + } + + if (bytes != ksize || memcmp(p + bp[ndx], kkey, (size_t)bytes)) { +#ifdef HASH_STATISTICS + ++hash_collisions; +#endif + return (-2); + } else + return (ndx); +} + +/* + * Given the buffer pointer of the first overflow page of a big pair, + * find the end of the big pair + * + * This will set bpp to the buffer header of the last page of the big pair. + * It will return the pageno of the overflow page following the last page + * of the pair; 0 if there isn't any (i.e. big pair is the last key in the + * bucket) + */ +uint16_t +__find_last_page(HTAB *hashp, BUFHEAD **bpp) +{ + BUFHEAD *bufp; + uint16_t *bp, pageno; + int n; + + bufp = *bpp; + bp = (uint16_t *)(void *)bufp->page; + for (;;) { + n = bp[0]; + + /* + * This is the last page if: the tag is FULL_KEY_DATA and + * either only 2 entries OVFLPAGE marker is explicit there + * is freespace on the page. + */ + if (bp[2] == FULL_KEY_DATA && + ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp)))) + break; + + pageno = bp[n - 1]; + bufp = __get_buf(hashp, (uint32_t)pageno, bufp, 0); + if (!bufp) + return (0); /* Need to indicate an error! */ + bp = (uint16_t *)(void *)bufp->page; + } + + *bpp = bufp; + if (bp[0] > 2) + return (bp[3]); + else + return (0); +} + +/* + * Return the data for the key/data pair that begins on this page at this + * index (index should always be 1). + */ +int +__big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current) +{ + BUFHEAD *save_p; + uint16_t *bp, len, off, save_addr; + char *tp; + + bp = (uint16_t *)(void *)bufp->page; + while (bp[ndx + 1] == PARTIAL_KEY) { + bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0); + if (!bufp) + return (-1); + bp = (uint16_t *)(void *)bufp->page; + ndx = 1; + } + + if (bp[ndx + 1] == FULL_KEY) { + bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0); + if (!bufp) + return (-1); + bp = (uint16_t *)(void *)bufp->page; + save_p = bufp; + save_addr = save_p->addr; + off = bp[1]; + len = 0; + } else + if (!FREESPACE(bp)) { + /* + * This is a hack. We can't distinguish between + * FULL_KEY_DATA that contains complete data or + * incomplete data, so we require that if the data + * is complete, there is at least 1 byte of free + * space left. + */ + off = bp[bp[0]]; + len = bp[1] - off; + save_p = bufp; + save_addr = bufp->addr; + bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, + 0); + if (!bufp) + return (-1); + bp = (uint16_t *)(void *)bufp->page; + } else { + /* The data is all on one page. */ + tp = (char *)(void *)bp; + off = bp[bp[0]]; + val->data = (uint8_t *)tp + off; + val->size = bp[1] - off; + if (set_current) { + if (bp[0] == 2) { /* No more buckets in + * chain */ + hashp->cpage = NULL; + hashp->cbucket++; + hashp->cndx = 1; + } else { + hashp->cpage = __get_buf(hashp, + (uint32_t)bp[bp[0] - 1], bufp, 0); + if (!hashp->cpage) + return (-1); + hashp->cndx = 1; + if (!((uint16_t *)(void *) + hashp->cpage->page)[0]) { + hashp->cbucket++; + hashp->cpage = NULL; + } + } + } + return (0); + } + + val->size = collect_data(hashp, bufp, (int)len, set_current); + if (val->size == (size_t)-1) + return (-1); + if (save_p->addr != save_addr) { + /* We are pretty short on buffers. */ + errno = EINVAL; /* OUT OF BUFFERS */ + return (-1); + } + memmove(hashp->tmp_buf, (save_p->page) + off, (size_t)len); + val->data = (uint8_t *)hashp->tmp_buf; + return (0); +} +/* + * Count how big the total datasize is by recursing through the pages. Then + * allocate a buffer and copy the data as you recurse up. + */ +static int +collect_data(HTAB *hashp, BUFHEAD *bufp, int len, int set) +{ + uint16_t *bp; + char *p; + BUFHEAD *xbp; + uint16_t save_addr; + int mylen, totlen; + + p = bufp->page; + bp = (uint16_t *)(void *)p; + mylen = hashp->BSIZE - bp[1]; + save_addr = bufp->addr; + + if (bp[2] == FULL_KEY_DATA) { /* End of Data */ + totlen = len + mylen; + if (hashp->tmp_buf) + free(hashp->tmp_buf); + if ((hashp->tmp_buf = calloc(1, (size_t)totlen)) == NULL) + return (-1); + if (set) { + hashp->cndx = 1; + if (bp[0] == 2) { /* No more buckets in chain */ + hashp->cpage = NULL; + hashp->cbucket++; + } else { + hashp->cpage = + __get_buf(hashp, (uint32_t)bp[bp[0] - 1], + bufp, 0); + if (!hashp->cpage) + return (-1); + else if (!((uint16_t *)(void *)hashp->cpage->page)[0]) { + hashp->cbucket++; + hashp->cpage = NULL; + } + } + } + } else { + xbp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0); + if (!xbp || ((totlen = + collect_data(hashp, xbp, len + mylen, set)) < 1)) + return (-1); + } + if (bufp->addr != save_addr) { + errno = EINVAL; /* Out of buffers. */ + return (-1); + } + memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], (size_t)mylen); + return (totlen); +} + +/* + * Fill in the key and data for this big pair. + */ +int +__big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set) +{ + key->size = collect_key(hashp, bufp, 0, val, set); + if (key->size == (size_t)-1) + return (-1); + key->data = (uint8_t *)hashp->tmp_key; + return (0); +} + +/* + * Count how big the total key size is by recursing through the pages. Then + * collect the data, allocate a buffer and copy the key as you recurse up. + */ +static int +collect_key(HTAB *hashp, BUFHEAD *bufp, int len, DBT *val, int set) +{ + BUFHEAD *xbp; + char *p; + int mylen, totlen; + uint16_t *bp, save_addr; + + p = bufp->page; + bp = (uint16_t *)(void *)p; + mylen = hashp->BSIZE - bp[1]; + + save_addr = bufp->addr; + totlen = len + mylen; + if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */ + if (hashp->tmp_key != NULL) + free(hashp->tmp_key); + if ((hashp->tmp_key = calloc(1, (size_t)totlen)) == NULL) + return (-1); + if (__big_return(hashp, bufp, 1, val, set)) + return (-1); + } else { + xbp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0); + if (!xbp || ((totlen = + collect_key(hashp, xbp, totlen, val, set)) < 1)) + return (-1); + } + if (bufp->addr != save_addr) { + errno = EINVAL; /* MIS -- OUT OF BUFFERS */ + return (-1); + } + memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], (size_t)mylen); + return (totlen); +} + +/* + * Returns: + * 0 => OK + * -1 => error + */ +int +__big_split( + HTAB *hashp, + BUFHEAD *op, /* Pointer to where to put keys that go in old bucket */ + BUFHEAD *np, /* Pointer to new bucket page */ + /* Pointer to first page containing the big key/data */ + BUFHEAD *big_keyp, + int addr, /* Address of big_keyp */ + uint32_t obucket,/* Old Bucket */ + SPLIT_RETURN *ret +) +{ + BUFHEAD *tmpp; + uint16_t *tp; + BUFHEAD *bp; + DBT key, val; + uint32_t change; + uint16_t free_space, n, off; + size_t temp; + + bp = big_keyp; + + /* Now figure out where the big key/data goes */ + if (__big_keydata(hashp, big_keyp, &key, &val, 0)) + return (-1); + change = (__call_hash(hashp, key.data, (int)key.size) != obucket); + + if ((ret->next_addr = __find_last_page(hashp, &big_keyp)) != 0) { + if (!(ret->nextp = + __get_buf(hashp, (uint32_t)ret->next_addr, big_keyp, 0))) + return (-1); + } else + ret->nextp = NULL; + + /* Now make one of np/op point to the big key/data pair */ + _DIAGASSERT(np->ovfl == NULL); + if (change) + tmpp = np; + else + tmpp = op; + + tmpp->flags |= BUF_MOD; +#ifdef DEBUG1 + (void)fprintf(stderr, + "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr, + (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0)); +#endif + tmpp->ovfl = bp; /* one of op/np point to big_keyp */ + tp = (uint16_t *)(void *)tmpp->page; + _DIAGASSERT(FREESPACE(tp) >= OVFLSIZE); + n = tp[0]; + off = OFFSET(tp); + free_space = FREESPACE(tp); + tp[++n] = (uint16_t)addr; + tp[++n] = OVFLPAGE; + tp[0] = n; + OFFSET(tp) = off; + temp = free_space - OVFLSIZE; + _DBFIT(temp, uint16_t); + FREESPACE(tp) = (uint16_t)temp; + + /* + * Finally, set the new and old return values. BIG_KEYP contains a + * pointer to the last page of the big key_data pair. Make sure that + * big_keyp has no following page (2 elements) or create an empty + * following page. + */ + + ret->newp = np; + ret->oldp = op; + + tp = (uint16_t *)(void *)big_keyp->page; + big_keyp->flags |= BUF_MOD; + if (tp[0] > 2) { + /* + * There may be either one or two offsets on this page. If + * there is one, then the overflow page is linked on normally + * and tp[4] is OVFLPAGE. If there are two, tp[4] contains + * the second offset and needs to get stuffed in after the + * next overflow page is added. + */ + n = tp[4]; + free_space = FREESPACE(tp); + off = OFFSET(tp); + tp[0] -= 2; + temp = free_space + OVFLSIZE; + _DBFIT(temp, uint16_t); + FREESPACE(tp) = (uint16_t)temp; + OFFSET(tp) = off; + tmpp = __add_ovflpage(hashp, big_keyp); + if (!tmpp) + return (-1); + tp[4] = n; + } else + tmpp = big_keyp; + + if (change) + ret->newp = tmpp; + else + ret->oldp = tmpp; + return (0); +} diff --git a/lib/nbsd_libc/db/hash/hash_buf.c b/lib/nbsd_libc/db/hash/hash_buf.c new file mode 100644 index 000000000..6469736b7 --- /dev/null +++ b/lib/nbsd_libc/db/hash/hash_buf.c @@ -0,0 +1,338 @@ +/* $NetBSD: hash_buf.c,v 1.18 2009/04/23 22:09:23 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: hash_buf.c,v 1.18 2009/04/23 22:09:23 christos Exp $"); + +/* + * PACKAGE: hash + * + * DESCRIPTION: + * Contains buffer management + * + * ROUTINES: + * External + * __buf_init + * __get_buf + * __buf_free + * __reclaim_buf + * Internal + * newbuf + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include "hash.h" +#include "page.h" +#include "extern.h" + +static BUFHEAD *newbuf(HTAB *, uint32_t, BUFHEAD *); + +/* Unlink B from its place in the lru */ +#define BUF_REMOVE(B) { \ + (B)->prev->next = (B)->next; \ + (B)->next->prev = (B)->prev; \ +} + +/* Insert B after P */ +#define BUF_INSERT(B, P) { \ + (B)->next = (P)->next; \ + (B)->prev = (P); \ + (P)->next = (B); \ + (B)->next->prev = (B); \ +} + +#define MRU hashp->bufhead.next +#define LRU hashp->bufhead.prev + +#define MRU_INSERT(B) BUF_INSERT((B), &hashp->bufhead) +#define LRU_INSERT(B) BUF_INSERT((B), LRU) + +/* + * We are looking for a buffer with address "addr". If prev_bp is NULL, then + * address is a bucket index. If prev_bp is not NULL, then it points to the + * page previous to an overflow page that we are trying to find. + * + * CAVEAT: The buffer header accessed via prev_bp's ovfl field may no longer + * be valid. Therefore, you must always verify that its address matches the + * address you are seeking. + */ +BUFHEAD * +__get_buf( + HTAB *hashp, + uint32_t addr, + BUFHEAD *prev_bp, + int newpage /* If prev_bp set, indicates a new overflow page. */ +) +{ + BUFHEAD *bp; + uint32_t is_disk_mask; + int is_disk, segment_ndx = 0; /* pacify gcc */ + SEGMENT segp = NULL; /* pacify gcc */ + + is_disk = 0; + is_disk_mask = 0; + if (prev_bp) { + bp = prev_bp->ovfl; + if (!bp || (bp->addr != addr)) + bp = NULL; + if (!newpage) + is_disk = BUF_DISK; + } else { + /* Grab buffer out of directory */ + segment_ndx = addr & (hashp->SGSIZE - 1); + + /* valid segment ensured by __call_hash() */ + segp = hashp->dir[addr >> hashp->SSHIFT]; + _DIAGASSERT(segp != NULL); + bp = PTROF(segp[segment_ndx]); + is_disk_mask = ISDISK(segp[segment_ndx]); + is_disk = is_disk_mask || !hashp->new_file; + } + + if (!bp) { + bp = newbuf(hashp, addr, prev_bp); + if (!bp || + __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0)) + return (NULL); + if (!prev_bp) + segp[segment_ndx] = + (BUFHEAD *)(void *)((u_long)bp | is_disk_mask); + } else { + BUF_REMOVE(bp); + MRU_INSERT(bp); + } + return (bp); +} + +/* + * We need a buffer for this page. Either allocate one, or evict a resident + * one (if we have as many buffers as we're allowed) and put this one in. + * + * If newbuf finds an error (returning NULL), it also sets errno. + */ +static BUFHEAD * +newbuf(HTAB *hashp, uint32_t addr, BUFHEAD *prev_bp) +{ + BUFHEAD *bp; /* The buffer we're going to use */ + BUFHEAD *xbp; /* Temp pointer */ + BUFHEAD *next_xbp; + SEGMENT segp; + int segment_ndx; + uint16_t oaddr, *shortp; + + oaddr = 0; + bp = LRU; + /* + * If LRU buffer is pinned, the buffer pool is too small. We need to + * allocate more buffers. + */ + if (hashp->nbufs || (bp->flags & BUF_PIN)) { + /* Allocate a new one */ + if ((bp = calloc(1, sizeof(BUFHEAD))) == NULL) + return (NULL); + if ((bp->page = calloc(1, (size_t)hashp->BSIZE)) == NULL) { + free(bp); + return (NULL); + } + if (hashp->nbufs) + hashp->nbufs--; + } else { + /* Kick someone out */ + BUF_REMOVE(bp); + /* + * If this is an overflow page with addr 0, it's already been + * flushed back in an overflow chain and initialized. + */ + if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) { + /* + * Set oaddr before __put_page so that you get it + * before bytes are swapped. + */ + shortp = (uint16_t *)(void *)bp->page; + if (shortp[0]) + oaddr = shortp[shortp[0] - 1]; + if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page, + bp->addr, (int)IS_BUCKET(bp->flags), 0)) + return (NULL); + /* + * Update the pointer to this page (i.e. invalidate it). + * + * If this is a new file (i.e. we created it at open + * time), make sure that we mark pages which have been + * written to disk so we retrieve them from disk later, + * rather than allocating new pages. + */ + if (IS_BUCKET(bp->flags)) { + segment_ndx = bp->addr & (hashp->SGSIZE - 1); + segp = hashp->dir[bp->addr >> hashp->SSHIFT]; + _DIAGASSERT(segp != NULL); + + if (hashp->new_file && + ((bp->flags & BUF_MOD) || + ISDISK(segp[segment_ndx]))) + segp[segment_ndx] = (BUFHEAD *)BUF_DISK; + else + segp[segment_ndx] = NULL; + } + /* + * Since overflow pages can only be access by means of + * their bucket, free overflow pages associated with + * this bucket. + */ + for (xbp = bp; xbp->ovfl;) { + next_xbp = xbp->ovfl; + xbp->ovfl = 0; + xbp = next_xbp; + + /* Check that ovfl pointer is up date. */ + if (IS_BUCKET(xbp->flags) || + (oaddr != xbp->addr)) + break; + + shortp = (uint16_t *)(void *)xbp->page; + if (shortp[0]) + /* set before __put_page */ + oaddr = shortp[shortp[0] - 1]; + if ((xbp->flags & BUF_MOD) && __put_page(hashp, + xbp->page, xbp->addr, 0, 0)) + return (NULL); + xbp->addr = 0; + xbp->flags = 0; + BUF_REMOVE(xbp); + LRU_INSERT(xbp); + } + } + } + + /* Now assign this buffer */ + bp->addr = addr; +#ifdef DEBUG1 + (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n", + bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0); +#endif + bp->ovfl = NULL; + if (prev_bp) { + /* + * If prev_bp is set, this is an overflow page, hook it in to + * the buffer overflow links. + */ +#ifdef DEBUG1 + (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n", + prev_bp->addr, (prev_bp->ovfl ? prev_bp->ovfl->addr : 0), + (bp ? bp->addr : 0)); +#endif + prev_bp->ovfl = bp; + bp->flags = 0; + } else + bp->flags = BUF_BUCKET; + MRU_INSERT(bp); + return (bp); +} + +void +__buf_init(HTAB *hashp, u_int nbytes) +{ + BUFHEAD *bfp; + int npages; + + bfp = &(hashp->bufhead); + npages = (unsigned int)(nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT; + npages = MAX(npages, MIN_BUFFERS); + + hashp->nbufs = npages; + bfp->next = bfp; + bfp->prev = bfp; + /* + * This space is calloc'd so these are already null. + * + * bfp->ovfl = NULL; + * bfp->flags = 0; + * bfp->page = NULL; + * bfp->addr = 0; + */ +} + +int +__buf_free(HTAB *hashp, int do_free, int to_disk) +{ + BUFHEAD *bp; + + /* Need to make sure that buffer manager has been initialized */ + if (!LRU) + return (0); + for (bp = LRU; bp != &hashp->bufhead;) { + /* Check that the buffer is valid */ + if (bp->addr || IS_BUCKET(bp->flags)) { + if (to_disk && (bp->flags & BUF_MOD) && + __put_page(hashp, bp->page, + bp->addr, IS_BUCKET(bp->flags), 0)) + return (-1); + } + /* Check if we are freeing stuff */ + if (do_free) { + if (bp->page) { + (void)memset(bp->page, 0, (size_t)hashp->BSIZE); + free(bp->page); + } + BUF_REMOVE(bp); + free(bp); + bp = LRU; + } else + bp = bp->prev; + } + return (0); +} + +void +__reclaim_buf(HTAB *hashp, BUFHEAD *bp) +{ + bp->ovfl = 0; + bp->addr = 0; + bp->flags = 0; + BUF_REMOVE(bp); + LRU_INSERT(bp); +} diff --git a/lib/nbsd_libc/db/hash/hash_func.c b/lib/nbsd_libc/db/hash/hash_func.c new file mode 100644 index 000000000..6dd136dc2 --- /dev/null +++ b/lib/nbsd_libc/db/hash/hash_func.c @@ -0,0 +1,208 @@ +/* $NetBSD: hash_func.c,v 1.13 2008/09/10 17:52:35 joerg 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 + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: hash_func.c,v 1.13 2008/09/10 17:52:35 joerg Exp $"); + +#include + +#include +#include "hash.h" +#include "page.h" +#include "extern.h" + +#if 0 +static uint32_t hash1(const void *, size_t) __attribute__((__unused__)); +static uint32_t hash2(const void *, size_t) __attribute__((__unused__)); +static uint32_t hash3(const void *, size_t) __attribute__((__unused__)); +#endif +static uint32_t hash4(const void *, size_t) __attribute__((__unused__)); + +/* Global default hash function */ +uint32_t (*__default_hash)(const void *, size_t) = hash4; +#if 0 +/* + * HASH FUNCTIONS + * + * Assume that we've already split the bucket to which this key hashes, + * calculate that bucket, and check that in fact we did already split it. + * + * This came from ejb's hsearch. + */ + +#define PRIME1 37 +#define PRIME2 1048583 + +static uint32_t +hash1(const void *keyarg, size_t len) +{ + const uint8_t *key; + uint32_t h; + + /* Convert string to integer */ + for (key = keyarg, h = 0; len--;) + h = h * PRIME1 ^ (*key++ - ' '); + h %= PRIME2; + return (h); +} + +/* + * Phong's linear congruential hash + */ +#define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c)) + +static uint32_t +hash2(const void *keyarg, size_t len) +{ + const uint8_t *e, *key; + uint32_t h; + uint8_t c; + + key = keyarg; + e = key + len; + for (h = 0; key != e;) { + c = *key++; + if (!c && key > e) + break; + dcharhash(h, c); + } + return (h); +} + +/* + * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte + * units. On the first time through the loop we get the "leftover bytes" + * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle + * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If + * this routine is heavily used enough, it's worth the ugly coding. + * + * OZ's original sdbm hash + */ +static uint32_t +hash3(const void *keyarg, size_t len) +{ + const uint8_t *key; + size_t loop; + uint32_t h; + +#define HASHC h = *key++ + 65599 * h + + h = 0; + key = keyarg; + if (len > 0) { + loop = (len + 8 - 1) >> 3; + + switch (len & (8 - 1)) { + case 0: + do { + HASHC; + /* FALLTHROUGH */ + case 7: + HASHC; + /* FALLTHROUGH */ + case 6: + HASHC; + /* FALLTHROUGH */ + case 5: + HASHC; + /* FALLTHROUGH */ + case 4: + HASHC; + /* FALLTHROUGH */ + case 3: + HASHC; + /* FALLTHROUGH */ + case 2: + HASHC; + /* FALLTHROUGH */ + case 1: + HASHC; + } while (--loop); + } + } + return (h); +} +#endif + +/* Hash function from Chris Torek. */ +static uint32_t +hash4(const void *keyarg, size_t len) +{ + const uint8_t *key; + size_t loop; + uint32_t h; + +#define HASH4a h = (h << 5) - h + *key++; +#define HASH4b h = (h << 5) + h + *key++; +#define HASH4 HASH4b + + h = 0; + key = keyarg; + if (len > 0) { + loop = (len + 8 - 1) >> 3; + + switch (len & (8 - 1)) { + case 0: + do { + HASH4; + /* FALLTHROUGH */ + case 7: + HASH4; + /* FALLTHROUGH */ + case 6: + HASH4; + /* FALLTHROUGH */ + case 5: + HASH4; + /* FALLTHROUGH */ + case 4: + HASH4; + /* FALLTHROUGH */ + case 3: + HASH4; + /* FALLTHROUGH */ + case 2: + HASH4; + /* FALLTHROUGH */ + case 1: + HASH4; + } while (--loop); + } + } + return (h); +} diff --git a/lib/nbsd_libc/db/hash/hash_log2.c b/lib/nbsd_libc/db/hash/hash_log2.c new file mode 100644 index 000000000..74343da17 --- /dev/null +++ b/lib/nbsd_libc/db/hash/hash_log2.c @@ -0,0 +1,62 @@ +/* $NetBSD: hash_log2.c,v 1.13 2008/09/11 12:33:55 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: hash_log2.c,v 1.13 2008/09/11 12:33:55 joerg Exp $"); + +#include + +#include +#include "hash.h" +#include "page.h" +#include "extern.h" + +uint32_t +__log2(uint32_t num) +{ + uint32_t i, limit; + + if (num == 0) + return 0; + --num; + + limit = 0; + for (i = 0; limit < num; limit = limit * 2 + 1, i++) + continue; + return (i); +} diff --git a/lib/nbsd_libc/db/hash/hash_page.c b/lib/nbsd_libc/db/hash/hash_page.c new file mode 100644 index 000000000..e79dfd9bf --- /dev/null +++ b/lib/nbsd_libc/db/hash/hash_page.c @@ -0,0 +1,962 @@ +/* $NetBSD: hash_page.c,v 1.23 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: hash_page.c,v 1.23 2008/09/11 12:58:00 joerg Exp $"); + +/* + * PACKAGE: hashing + * + * DESCRIPTION: + * Page manipulation for hashing package. + * + * ROUTINES: + * + * External + * __get_page + * __add_ovflpage + * Internal + * overflow_page + * open_temp + */ + +#include "namespace.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "hash.h" +#include "page.h" +#include "extern.h" + +static uint32_t *fetch_bitmap(HTAB *, int); +static uint32_t first_free(uint32_t); +static int open_temp(HTAB *); +static uint16_t overflow_page(HTAB *); +static void putpair(char *, const DBT *, const DBT *); +static void squeeze_key(uint16_t *, const DBT *, const DBT *); +static int ugly_split(HTAB *, uint32_t, BUFHEAD *, BUFHEAD *, int, int); + +#define PAGE_INIT(P) { \ + ((uint16_t *)(void *)(P))[0] = 0; \ + temp = 3 * sizeof(uint16_t); \ + _DIAGASSERT(hashp->BSIZE >= temp); \ + ((uint16_t *)(void *)(P))[1] = (uint16_t)(hashp->BSIZE - temp); \ + ((uint16_t *)(void *)(P))[2] = hashp->BSIZE; \ +} + +/* + * This is called AFTER we have verified that there is room on the page for + * the pair (PAIRFITS has returned true) so we go right ahead and start moving + * stuff on. + */ +static void +putpair(char *p, const DBT *key, const DBT *val) +{ + uint16_t *bp, n, off; + size_t temp; + + bp = (uint16_t *)(void *)p; + + /* Enter the key first. */ + n = bp[0]; + + temp = OFFSET(bp); + _DIAGASSERT(temp >= key->size); + off = (uint16_t)(temp - key->size); + memmove(p + off, key->data, key->size); + bp[++n] = off; + + /* Now the data. */ + _DIAGASSERT(off >= val->size); + off -= (uint16_t)val->size; + memmove(p + off, val->data, val->size); + bp[++n] = off; + + /* Adjust page info. */ + bp[0] = n; + temp = (n + 3) * sizeof(uint16_t); + _DIAGASSERT(off >= temp); + bp[n + 1] = (uint16_t)(off - temp); + bp[n + 2] = off; +} + +/* + * Returns: + * 0 OK + * -1 error + */ +int +__delpair(HTAB *hashp, BUFHEAD *bufp, int ndx) +{ + uint16_t *bp, newoff; + int n; + uint16_t pairlen; + size_t temp; + + bp = (uint16_t *)(void *)bufp->page; + n = bp[0]; + + if (bp[ndx + 1] < REAL_KEY) + return (__big_delete(hashp, bufp)); + if (ndx != 1) + newoff = bp[ndx - 1]; + else + newoff = hashp->BSIZE; + pairlen = newoff - bp[ndx + 1]; + + if (ndx != (n - 1)) { + /* Hard Case -- need to shuffle keys */ + int i; + char *src = bufp->page + (int)OFFSET(bp); + char *dst = src + (int)pairlen; + memmove(dst, src, (size_t)(bp[ndx + 1] - OFFSET(bp))); + + /* Now adjust the pointers */ + for (i = ndx + 2; i <= n; i += 2) { + if (bp[i + 1] == OVFLPAGE) { + bp[i - 2] = bp[i]; + bp[i - 1] = bp[i + 1]; + } else { + bp[i - 2] = bp[i] + pairlen; + bp[i - 1] = bp[i + 1] + pairlen; + } + } + } + /* Finally adjust the page data */ + bp[n] = OFFSET(bp) + pairlen; + temp = bp[n + 1] + pairlen + 2 * sizeof(uint16_t); + _DIAGASSERT(temp <= 0xffff); + bp[n - 1] = (uint16_t)temp; + bp[0] = n - 2; + hashp->NKEYS--; + + bufp->flags |= BUF_MOD; + return (0); +} +/* + * Returns: + * 0 ==> OK + * -1 ==> Error + */ +int +__split_page(HTAB *hashp, uint32_t obucket, uint32_t nbucket) +{ + BUFHEAD *new_bufp, *old_bufp; + uint16_t *ino; + char *np; + DBT key, val; + int n, ndx, retval; + uint16_t copyto, diff, off, moved; + char *op; + size_t temp; + + copyto = (uint16_t)hashp->BSIZE; + off = (uint16_t)hashp->BSIZE; + old_bufp = __get_buf(hashp, obucket, NULL, 0); + if (old_bufp == NULL) + return (-1); + new_bufp = __get_buf(hashp, nbucket, NULL, 0); + if (new_bufp == NULL) + return (-1); + + old_bufp->flags |= (BUF_MOD | BUF_PIN); + new_bufp->flags |= (BUF_MOD | BUF_PIN); + + ino = (uint16_t *)(void *)(op = old_bufp->page); + np = new_bufp->page; + + moved = 0; + + for (n = 1, ndx = 1; n < ino[0]; n += 2) { + if (ino[n + 1] < REAL_KEY) { + retval = ugly_split(hashp, obucket, old_bufp, new_bufp, + (int)copyto, (int)moved); + old_bufp->flags &= ~BUF_PIN; + new_bufp->flags &= ~BUF_PIN; + return (retval); + + } + key.data = (uint8_t *)op + ino[n]; + key.size = off - ino[n]; + + if (__call_hash(hashp, key.data, (int)key.size) == obucket) { + /* Don't switch page */ + diff = copyto - off; + if (diff) { + copyto = ino[n + 1] + diff; + memmove(op + copyto, op + ino[n + 1], + (size_t)(off - ino[n + 1])); + ino[ndx] = copyto + ino[n] - ino[n + 1]; + ino[ndx + 1] = copyto; + } else + copyto = ino[n + 1]; + ndx += 2; + } else { + /* Switch page */ + val.data = (uint8_t *)op + ino[n + 1]; + val.size = ino[n] - ino[n + 1]; + putpair(np, &key, &val); + moved += 2; + } + + off = ino[n + 1]; + } + + /* Now clean up the page */ + ino[0] -= moved; + temp = sizeof(uint16_t) * (ino[0] + 3); + _DIAGASSERT(copyto >= temp); + FREESPACE(ino) = (uint16_t)(copyto - temp); + OFFSET(ino) = copyto; + +#ifdef DEBUG3 + (void)fprintf(stderr, "split %d/%d\n", + ((uint16_t *)np)[0] / 2, + ((uint16_t *)op)[0] / 2); +#endif + /* unpin both pages */ + old_bufp->flags &= ~BUF_PIN; + new_bufp->flags &= ~BUF_PIN; + return (0); +} + +/* + * Called when we encounter an overflow or big key/data page during split + * handling. This is special cased since we have to begin checking whether + * the key/data pairs fit on their respective pages and because we may need + * overflow pages for both the old and new pages. + * + * The first page might be a page with regular key/data pairs in which case + * we have a regular overflow condition and just need to go on to the next + * page or it might be a big key/data pair in which case we need to fix the + * big key/data pair. + * + * Returns: + * 0 ==> success + * -1 ==> failure + */ +static int +ugly_split( + HTAB *hashp, + uint32_t obucket, /* Same as __split_page. */ + BUFHEAD *old_bufp, + BUFHEAD *new_bufp, + int copyto, /* First byte on page which contains key/data values. */ + int moved /* Number of pairs moved to new page. */ +) +{ + BUFHEAD *bufp; /* Buffer header for ino */ + uint16_t *ino; /* Page keys come off of */ + uint16_t *np; /* New page */ + uint16_t *op; /* Page keys go on to if they aren't moving */ + size_t temp; + + BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */ + DBT key, val; + SPLIT_RETURN ret; + uint16_t n, off, ov_addr, scopyto; + char *cino; /* Character value of ino */ + + bufp = old_bufp; + ino = (uint16_t *)(void *)old_bufp->page; + np = (uint16_t *)(void *)new_bufp->page; + op = (uint16_t *)(void *)old_bufp->page; + last_bfp = NULL; + scopyto = (uint16_t)copyto; /* ANSI */ + + n = ino[0] - 1; + while (n < ino[0]) { + if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) { + if (__big_split(hashp, old_bufp, + new_bufp, bufp, (int)bufp->addr, obucket, &ret)) + return (-1); + old_bufp = ret.oldp; + if (!old_bufp) + return (-1); + op = (uint16_t *)(void *)old_bufp->page; + new_bufp = ret.newp; + if (!new_bufp) + return (-1); + np = (uint16_t *)(void *)new_bufp->page; + bufp = ret.nextp; + if (!bufp) + return (0); + cino = (char *)bufp->page; + ino = (uint16_t *)(void *)cino; + last_bfp = ret.nextp; + } else if (ino[n + 1] == OVFLPAGE) { + ov_addr = ino[n]; + /* + * Fix up the old page -- the extra 2 are the fields + * which contained the overflow information. + */ + ino[0] -= (moved + 2); + temp = sizeof(uint16_t) * (ino[0] + 3); + _DIAGASSERT(scopyto >= temp); + FREESPACE(ino) = (uint16_t)(scopyto - temp); + OFFSET(ino) = scopyto; + + bufp = __get_buf(hashp, (uint32_t)ov_addr, bufp, 0); + if (!bufp) + return (-1); + + ino = (uint16_t *)(void *)bufp->page; + n = 1; + scopyto = hashp->BSIZE; + moved = 0; + + if (last_bfp) + __free_ovflpage(hashp, last_bfp); + last_bfp = bufp; + } + /* Move regular sized pairs of there are any */ + off = hashp->BSIZE; + for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) { + cino = (char *)(void *)ino; + key.data = (uint8_t *)cino + ino[n]; + key.size = off - ino[n]; + val.data = (uint8_t *)cino + ino[n + 1]; + val.size = ino[n] - ino[n + 1]; + off = ino[n + 1]; + + if (__call_hash(hashp, key.data, (int)key.size) == obucket) { + /* Keep on old page */ + if (PAIRFITS(op, (&key), (&val))) + putpair((char *)(void *)op, &key, &val); + else { + old_bufp = + __add_ovflpage(hashp, old_bufp); + if (!old_bufp) + return (-1); + op = (uint16_t *)(void *)old_bufp->page; + putpair((char *)(void *)op, &key, &val); + } + old_bufp->flags |= BUF_MOD; + } else { + /* Move to new page */ + if (PAIRFITS(np, (&key), (&val))) + putpair((char *)(void *)np, &key, &val); + else { + new_bufp = + __add_ovflpage(hashp, new_bufp); + if (!new_bufp) + return (-1); + np = (uint16_t *)(void *)new_bufp->page; + putpair((char *)(void *)np, &key, &val); + } + new_bufp->flags |= BUF_MOD; + } + } + } + if (last_bfp) + __free_ovflpage(hashp, last_bfp); + return (0); +} + +/* + * Add the given pair to the page + * + * Returns: + * 0 ==> OK + * 1 ==> failure + */ +int +__addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val) +{ + uint16_t *bp, *sop; + int do_expand; + + bp = (uint16_t *)(void *)bufp->page; + do_expand = 0; + while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY)) + /* Exception case */ + if (bp[2] == FULL_KEY_DATA && bp[0] == 2) + /* This is the last page of a big key/data pair + and we need to add another page */ + break; + else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) { + bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, + 0); + if (!bufp) + return (-1); + bp = (uint16_t *)(void *)bufp->page; + } else if (bp[bp[0]] != OVFLPAGE) { + /* Short key/data pairs, no more pages */ + break; + } else { + /* Try to squeeze key on this page */ + if (bp[2] >= REAL_KEY && + FREESPACE(bp) >= PAIRSIZE(key, val)) { + squeeze_key(bp, key, val); + goto stats; + } else { + bufp = __get_buf(hashp, + (uint32_t)bp[bp[0] - 1], bufp, 0); + if (!bufp) + return (-1); + bp = (uint16_t *)(void *)bufp->page; + } + } + + if (PAIRFITS(bp, key, val)) + putpair(bufp->page, key, val); + else { + do_expand = 1; + bufp = __add_ovflpage(hashp, bufp); + if (!bufp) + return (-1); + sop = (uint16_t *)(void *)bufp->page; + + if (PAIRFITS(sop, key, val)) + putpair((char *)(void *)sop, key, val); + else + if (__big_insert(hashp, bufp, key, val)) + return (-1); + } +stats: + bufp->flags |= BUF_MOD; + /* + * If the average number of keys per bucket exceeds the fill factor, + * expand the table. + */ + hashp->NKEYS++; + if (do_expand || + (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR)) + return (__expand_table(hashp)); + return (0); +} + +/* + * + * Returns: + * pointer on success + * NULL on error + */ +BUFHEAD * +__add_ovflpage(HTAB *hashp, BUFHEAD *bufp) +{ + uint16_t *sp; + uint16_t ndx, ovfl_num; + size_t temp; +#ifdef DEBUG1 + int tmp1, tmp2; +#endif + sp = (uint16_t *)(void *)bufp->page; + + /* Check if we are dynamically determining the fill factor */ + if (hashp->FFACTOR == DEF_FFACTOR) { + hashp->FFACTOR = (uint32_t)sp[0] >> 1; + if (hashp->FFACTOR < MIN_FFACTOR) + hashp->FFACTOR = MIN_FFACTOR; + } + bufp->flags |= BUF_MOD; + ovfl_num = overflow_page(hashp); +#ifdef DEBUG1 + tmp1 = bufp->addr; + tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0; +#endif + if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, (uint32_t)ovfl_num, + bufp, 1))) + return (NULL); + bufp->ovfl->flags |= BUF_MOD; +#ifdef DEBUG1 + (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n", + tmp1, tmp2, bufp->ovfl->addr); +#endif + ndx = sp[0]; + /* + * Since a pair is allocated on a page only if there's room to add + * an overflow page, we know that the OVFL information will fit on + * the page. + */ + sp[ndx + 4] = OFFSET(sp); + temp = FREESPACE(sp); + _DIAGASSERT(temp >= OVFLSIZE); + sp[ndx + 3] = (uint16_t)(temp - OVFLSIZE); + sp[ndx + 1] = ovfl_num; + sp[ndx + 2] = OVFLPAGE; + sp[0] = ndx + 2; +#ifdef HASH_STATISTICS + hash_overflows++; +#endif + return (bufp->ovfl); +} + +/* + * Returns: + * 0 indicates SUCCESS + * -1 indicates FAILURE + */ +int +__get_page(HTAB *hashp, char *p, uint32_t bucket, int is_bucket, int is_disk, + int is_bitmap) +{ + int fd, page, size; + ssize_t rsize; + uint16_t *bp; + size_t temp; + + fd = hashp->fp; + size = hashp->BSIZE; + + if ((fd == -1) || !is_disk) { + PAGE_INIT(p); + return (0); + } + if (is_bucket) + page = BUCKET_TO_PAGE(bucket); + else + page = OADDR_TO_PAGE(bucket); + if ((rsize = pread(fd, p, (size_t)size, (off_t)page << hashp->BSHIFT)) == -1) + return (-1); + bp = (uint16_t *)(void *)p; + if (!rsize) + bp[0] = 0; /* We hit the EOF, so initialize a new page */ + else + if (rsize != size) { + errno = EFTYPE; + return (-1); + } + if (!is_bitmap && !bp[0]) { + PAGE_INIT(p); + } else + if (hashp->LORDER != BYTE_ORDER) { + int i, max; + + if (is_bitmap) { + max = (uint32_t)hashp->BSIZE >> 2; /* divide by 4 */ + for (i = 0; i < max; i++) + M_32_SWAP(((int *)(void *)p)[i]); + } else { + M_16_SWAP(bp[0]); + max = bp[0] + 2; + for (i = 1; i <= max; i++) + M_16_SWAP(bp[i]); + } + } + return (0); +} + +/* + * Write page p to disk + * + * Returns: + * 0 ==> OK + * -1 ==>failure + */ +int +__put_page(HTAB *hashp, char *p, uint32_t bucket, int is_bucket, int is_bitmap) +{ + int fd, page, size; + ssize_t wsize; + + size = hashp->BSIZE; + if ((hashp->fp == -1) && open_temp(hashp)) + return (-1); + fd = hashp->fp; + + if (hashp->LORDER != BYTE_ORDER) { + int i; + int max; + + if (is_bitmap) { + max = (uint32_t)hashp->BSIZE >> 2; /* divide by 4 */ + for (i = 0; i < max; i++) + M_32_SWAP(((int *)(void *)p)[i]); + } else { + max = ((uint16_t *)(void *)p)[0] + 2; + for (i = 0; i <= max; i++) + M_16_SWAP(((uint16_t *)(void *)p)[i]); + } + } + if (is_bucket) + page = BUCKET_TO_PAGE(bucket); + else + page = OADDR_TO_PAGE(bucket); + if ((wsize = pwrite(fd, p, (size_t)size, (off_t)page << hashp->BSHIFT)) == -1) + /* Errno is set */ + return (-1); + if (wsize != size) { + errno = EFTYPE; + return (-1); + } + return (0); +} + +#define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1) +/* + * Initialize a new bitmap page. Bitmap pages are left in memory + * once they are read in. + */ +int +__ibitmap(HTAB *hashp, int pnum, int nbits, int ndx) +{ + uint32_t *ip; + int clearbytes, clearints; + + if ((ip = malloc((size_t)hashp->BSIZE)) == NULL) + return (1); + hashp->nmaps++; + clearints = ((uint32_t)(nbits - 1) >> INT_BYTE_SHIFT) + 1; + clearbytes = clearints << INT_TO_BYTE; + (void)memset(ip, 0, (size_t)clearbytes); + (void)memset(((char *)(void *)ip) + clearbytes, 0xFF, + (size_t)(hashp->BSIZE - clearbytes)); + ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK); + SETBIT(ip, 0); + hashp->BITMAPS[ndx] = (uint16_t)pnum; + hashp->mapp[ndx] = ip; + return (0); +} + +static uint32_t +first_free(uint32_t map) +{ + uint32_t i, mask; + + mask = 0x1; + for (i = 0; i < BITS_PER_MAP; i++) { + if (!(mask & map)) + return (i); + mask = mask << 1; + } + return (i); +} + +static uint16_t +overflow_page(HTAB *hashp) +{ + uint32_t *freep = NULL; + int max_free, offset, splitnum; + uint16_t addr; + int bit, first_page, free_bit, free_page, i, in_use_bits, j; +#ifdef DEBUG2 + int tmp1, tmp2; +#endif + splitnum = hashp->OVFL_POINT; + max_free = hashp->SPARES[splitnum]; + + free_page = (uint32_t)(max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT); + free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1); + + /* Look through all the free maps to find the first free block */ + first_page = (uint32_t)hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT); + for ( i = first_page; i <= free_page; i++ ) { + if (!(freep = (uint32_t *)hashp->mapp[i]) && + !(freep = fetch_bitmap(hashp, i))) + return (0); + if (i == free_page) + in_use_bits = free_bit; + else + in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1; + + if (i == first_page) { + bit = hashp->LAST_FREED & + ((hashp->BSIZE << BYTE_SHIFT) - 1); + j = bit / BITS_PER_MAP; + bit = bit & ~(BITS_PER_MAP - 1); + } else { + bit = 0; + j = 0; + } + for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP) + if (freep[j] != ALL_SET) + goto found; + } + + /* No Free Page Found */ + hashp->LAST_FREED = hashp->SPARES[splitnum]; + hashp->SPARES[splitnum]++; + offset = hashp->SPARES[splitnum] - + (splitnum ? hashp->SPARES[splitnum - 1] : 0); + +#define OVMSG "HASH: Out of overflow pages. Increase page size\n" + if (offset > SPLITMASK) { + if (++splitnum >= NCACHED) { + (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); + errno = EFBIG; + return (0); + } + hashp->OVFL_POINT = splitnum; + hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; + hashp->SPARES[splitnum-1]--; + offset = 1; + } + + /* Check if we need to allocate a new bitmap page */ + if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) { + free_page++; + if (free_page >= NCACHED) { + (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); + errno = EFBIG; + return (0); + } + /* + * This is tricky. The 1 indicates that you want the new page + * allocated with 1 clear bit. Actually, you are going to + * allocate 2 pages from this map. The first is going to be + * the map page, the second is the overflow page we were + * looking for. The init_bitmap routine automatically, sets + * the first bit of itself to indicate that the bitmap itself + * is in use. We would explicitly set the second bit, but + * don't have to if we tell init_bitmap not to leave it clear + * in the first place. + */ + if (__ibitmap(hashp, + (int)OADDR_OF(splitnum, offset), 1, free_page)) + return (0); + hashp->SPARES[splitnum]++; +#ifdef DEBUG2 + free_bit = 2; +#endif + offset++; + if (offset > SPLITMASK) { + if (++splitnum >= NCACHED) { + (void)write(STDERR_FILENO, OVMSG, + sizeof(OVMSG) - 1); + errno = EFBIG; + return (0); + } + hashp->OVFL_POINT = splitnum; + hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; + hashp->SPARES[splitnum-1]--; + offset = 0; + } + } else { + /* + * Free_bit addresses the last used bit. Bump it to address + * the first available bit. + */ + free_bit++; + SETBIT(freep, free_bit); + } + + /* Calculate address of the new overflow page */ + addr = OADDR_OF(splitnum, offset); +#ifdef DEBUG2 + (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", + addr, free_bit, free_page); +#endif + return (addr); + +found: + bit = bit + first_free(freep[j]); + SETBIT(freep, bit); +#ifdef DEBUG2 + tmp1 = bit; + tmp2 = i; +#endif + /* + * Bits are addressed starting with 0, but overflow pages are addressed + * beginning at 1. Bit is a bit addressnumber, so we need to increment + * it to convert it to a page number. + */ + bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT)); + if (bit >= hashp->LAST_FREED) + hashp->LAST_FREED = bit - 1; + + /* Calculate the split number for this page */ + for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++); + offset = (i ? bit - hashp->SPARES[i - 1] : bit); + if (offset >= SPLITMASK) { + (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); + errno = EFBIG; + return (0); /* Out of overflow pages */ + } + addr = OADDR_OF(i, offset); +#ifdef DEBUG2 + (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", + addr, tmp1, tmp2); +#endif + + /* Allocate and return the overflow page */ + return (addr); +} + +/* + * Mark this overflow page as free. + */ +void +__free_ovflpage(HTAB *hashp, BUFHEAD *obufp) +{ + uint16_t addr; + uint32_t *freep; + int bit_address, free_page, free_bit; + uint16_t ndx; + + addr = obufp->addr; +#ifdef DEBUG1 + (void)fprintf(stderr, "Freeing %d\n", addr); +#endif + ndx = (((uint32_t)addr) >> SPLITSHIFT); + bit_address = + (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1; + if (bit_address < hashp->LAST_FREED) + hashp->LAST_FREED = bit_address; + free_page = ((uint32_t)bit_address >> (hashp->BSHIFT + BYTE_SHIFT)); + free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1); + + if (!(freep = hashp->mapp[free_page])) + freep = fetch_bitmap(hashp, free_page); + /* + * This had better never happen. It means we tried to read a bitmap + * that has already had overflow pages allocated off it, and we + * failed to read it from the file. + */ + _DIAGASSERT(freep != NULL); + CLRBIT(freep, free_bit); +#ifdef DEBUG2 + (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n", + obufp->addr, free_bit, free_page); +#endif + __reclaim_buf(hashp, obufp); +} + +/* + * Returns: + * 0 success + * -1 failure + */ +static int +open_temp(HTAB *hashp) +{ + sigset_t set, oset; + char *envtmp; + char namestr[PATH_MAX]; + + if (issetugid()) + envtmp = NULL; + else + envtmp = getenv("TMPDIR"); + + if (-1 == snprintf(namestr, sizeof(namestr), "%s/_hashXXXXXX", + envtmp ? envtmp : _PATH_TMP)) + return -1; + + /* Block signals; make sure file goes away at process exit. */ + (void)sigfillset(&set); + (void)sigprocmask(SIG_BLOCK, &set, &oset); + if ((hashp->fp = mkstemp(namestr)) != -1) { + (void)unlink(namestr); + (void)fcntl(hashp->fp, F_SETFD, FD_CLOEXEC); + } + (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL); + return (hashp->fp != -1 ? 0 : -1); +} + +/* + * We have to know that the key will fit, but the last entry on the page is + * an overflow pair, so we need to shift things. + */ +static void +squeeze_key(uint16_t *sp, const DBT *key, const DBT *val) +{ + char *p; + uint16_t free_space, n, off, pageno; + size_t temp; + + p = (char *)(void *)sp; + n = sp[0]; + free_space = FREESPACE(sp); + off = OFFSET(sp); + + pageno = sp[n - 1]; + _DIAGASSERT(off >= key->size); + off -= (uint16_t)key->size; + sp[n - 1] = off; + memmove(p + off, key->data, key->size); + _DIAGASSERT(off >= val->size); + off -= (uint16_t)val->size; + sp[n] = off; + memmove(p + off, val->data, val->size); + sp[0] = n + 2; + sp[n + 1] = pageno; + sp[n + 2] = OVFLPAGE; + temp = PAIRSIZE(key, val); + _DIAGASSERT(free_space >= temp); + FREESPACE(sp) = (uint16_t)(free_space - temp); + OFFSET(sp) = off; +} + +static uint32_t * +fetch_bitmap(HTAB *hashp, int ndx) +{ + if (ndx >= hashp->nmaps) + return (NULL); + if ((hashp->mapp[ndx] = malloc((size_t)hashp->BSIZE)) == NULL) + return (NULL); + if (__get_page(hashp, + (char *)(void *)hashp->mapp[ndx], (uint32_t)hashp->BITMAPS[ndx], 0, 1, 1)) { + free(hashp->mapp[ndx]); + return (NULL); + } + return (hashp->mapp[ndx]); +} + +#ifdef DEBUG4 +void print_chain(HTAB *, uint32_t); +void +print_chain(HTAB *hashp, uint32_t addr) +{ + BUFHEAD *bufp; + uint16_t *bp, oaddr; + + (void)fprintf(stderr, "%d ", addr); + bufp = __get_buf(hashp, addr, NULL, 0); + bp = (uint16_t *)bufp->page; + while (bp[0] && ((bp[bp[0]] == OVFLPAGE) || + ((bp[0] > 2) && bp[2] < REAL_KEY))) { + oaddr = bp[bp[0] - 1]; + (void)fprintf(stderr, "%d ", (int)oaddr); + bufp = __get_buf(hashp, (uint32_t)oaddr, bufp, 0); + bp = (uint16_t *)bufp->page; + } + (void)fprintf(stderr, "\n"); +} +#endif diff --git a/lib/nbsd_libc/db/hash/ndbm.c b/lib/nbsd_libc/db/hash/ndbm.c new file mode 100644 index 000000000..1aea409b7 --- /dev/null +++ b/lib/nbsd_libc/db/hash/ndbm.c @@ -0,0 +1,115 @@ +/* $NetBSD: ndbm.c,v 1.23 2008/09/11 12:58:00 joerg Exp $ */ +/* from: NetBSD: ndbm.c,v 1.18 2004/04/27 20:03:45 kleink 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 + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: ndbm.c,v 1.23 2008/09/11 12:58:00 joerg Exp $"); + +/* + * This package provides a dbm compatible interface to the new hashing + * package described in db(3). + */ +#include "namespace.h" +#include + +#include +#include +#include + +#include +#include "hash.h" + +/* + * Returns: + * *DBM on success + * NULL on failure + */ +DBM * +dbm_open(const char *file, int flags, mode_t mode) +{ + HASHINFO info; + char path[MAXPATHLEN]; + + info.bsize = 4096; + info.ffactor = 40; + info.nelem = 1; + info.cachesize = 0; + info.hash = NULL; + info.lorder = 0; + (void)strncpy(path, file, sizeof(path) - 1); + (void)strncat(path, DBM_SUFFIX, sizeof(path) - strlen(path) - 1); + if ((flags & O_ACCMODE) == O_WRONLY) { + flags &= ~O_WRONLY; + flags |= O_RDWR; + } + return ((DBM *)__hash_open(path, flags, mode, &info, 0)); +} + +void +dbm_close(DBM *db) +{ + (void)(db->close)(db); +} + +int +dbm_error(DBM *db) +{ + HTAB *hp; + + hp = db->internal; + return (hp->err); +} + +int +dbm_clearerr(DBM *db) +{ + HTAB *hp; + + hp = db->internal; + hp->err = 0; + return (0); +} + +int +dbm_dirfno(DBM *db) +{ + HTAB *hp; + + hp = db->internal; + return hp->fp; +} diff --git a/lib/nbsd_libc/db/hash/ndbmdatum.c b/lib/nbsd_libc/db/hash/ndbmdatum.c new file mode 100644 index 000000000..75c6ab87c --- /dev/null +++ b/lib/nbsd_libc/db/hash/ndbmdatum.c @@ -0,0 +1,158 @@ +/* $NetBSD: ndbmdatum.c,v 1.4 2008/09/11 12:58:00 joerg Exp $ */ +/* from: NetBSD: ndbm.c,v 1.18 2004/04/27 20:03:45 kleink 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 + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: ndbmdatum.c,v 1.4 2008/09/11 12:58:00 joerg Exp $"); + +/* + * This package provides a dbm compatible interface to the new hashing + * package described in db(3). + */ +#include "namespace.h" +#include + +#include +#include +#include + +#include +#include "hash.h" + +/* + * Returns: + * DATUM on success + * NULL on failure + */ +datum +dbm_fetch(DBM *db, datum key) +{ + datum retdata; + int status; + DBT dbtkey, dbtretdata; + + dbtkey.data = key.dptr; + dbtkey.size = key.dsize; + status = (db->get)(db, &dbtkey, &dbtretdata, 0); + if (status) { + dbtretdata.data = NULL; + dbtretdata.size = 0; + } + retdata.dptr = dbtretdata.data; + retdata.dsize = dbtretdata.size; + return (retdata); +} + +/* + * Returns: + * DATUM on success + * NULL on failure + */ +datum +dbm_firstkey(DBM *db) +{ + int status; + datum retkey; + DBT dbtretkey, dbtretdata; + + status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST); + if (status) + dbtretkey.data = NULL; + retkey.dptr = dbtretkey.data; + retkey.dsize = dbtretkey.size; + return (retkey); +} + +/* + * Returns: + * DATUM on success + * NULL on failure + */ +datum +dbm_nextkey(DBM *db) +{ + int status; + datum retkey; + DBT dbtretkey, dbtretdata; + + status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT); + if (status) + dbtretkey.data = NULL; + retkey.dptr = dbtretkey.data; + retkey.dsize = dbtretkey.size; + return (retkey); +} + +/* + * Returns: + * 0 on success + * <0 failure + */ +int +dbm_delete(DBM *db, datum key) +{ + int status; + DBT dbtkey; + + dbtkey.data = key.dptr; + dbtkey.size = key.dsize; + status = (db->del)(db, &dbtkey, 0); + if (status) + return (-1); + else + return (0); +} + +/* + * Returns: + * 0 on success + * <0 failure + * 1 if DBM_INSERT and entry exists + */ +int +dbm_store(DBM *db, datum key, datum data, int flags) +{ + DBT dbtkey, dbtdata; + + dbtkey.data = key.dptr; + dbtkey.size = key.dsize; + dbtdata.data = data.dptr; + dbtdata.size = data.dsize; + return ((db->put)(db, &dbtkey, &dbtdata, + (u_int)((flags == DBM_INSERT) ? R_NOOVERWRITE : 0))); +} diff --git a/lib/nbsd_libc/db/hash/page.h b/lib/nbsd_libc/db/hash/page.h new file mode 100644 index 000000000..40dc77d02 --- /dev/null +++ b/lib/nbsd_libc/db/hash/page.h @@ -0,0 +1,90 @@ +/* $NetBSD: page.h,v 1.8 2008/08/26 21:18:38 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * 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. + * + * @(#)page.h 8.2 (Berkeley) 5/31/94 + */ + +/* + * Definitions for hashing page file format. + */ + +/* + * routines dealing with a data page + * + * page format: + * +------------------------------+ + * p | n | keyoff | datoff | keyoff | + * +------------+--------+--------+ + * | datoff | free | ptr | --> | + * +--------+---------------------+ + * | F R E E A R E A | + * +--------------+---------------+ + * | <---- - - - | data | + * +--------+-----+----+----------+ + * | key | data | key | + * +--------+----------+----------+ + * + * Pointer to the free space is always: p[p[0] + 2] + * Amount of free space on the page is: p[p[0] + 1] + */ + +/* + * How many bytes required for this pair? + * 2 shorts in the table at the top of the page + room for the + * key and room for the data + * + * We prohibit entering a pair on a page unless there is also room to append + * an overflow page. The reason for this it that you can get in a situation + * where a single key/data pair fits on a page, but you can't append an + * overflow page and later you'd have to split the key/data and handle like + * a big pair. + * You might as well do this up front. + */ + +#define PAIRSIZE(K,D) (2*sizeof(uint16_t) + (K)->size + (D)->size) +#define BIGOVERHEAD (4*sizeof(uint16_t)) +#define KEYSIZE(K) (4*sizeof(uint16_t) + (K)->size); +#define OVFLSIZE (2*sizeof(uint16_t)) +#define FREESPACE(P) ((P)[(P)[0]+1]) +#define OFFSET(P) ((P)[(P)[0]+2]) +#define PAIRFITS(P,K,D) \ + (((P)[2] >= REAL_KEY) && \ + (PAIRSIZE((K),(D)) + OVFLSIZE) <= FREESPACE((P))) +#define PAGE_META(N) (((N)+3) * sizeof(uint16_t)) + +typedef struct { + BUFHEAD *newp; + BUFHEAD *oldp; + BUFHEAD *nextp; + uint16_t next_addr; +} SPLIT_RETURN; diff --git a/lib/nbsd_libc/db/man/Makefile.inc b/lib/nbsd_libc/db/man/Makefile.inc new file mode 100644 index 000000000..f45c41630 --- /dev/null +++ b/lib/nbsd_libc/db/man/Makefile.inc @@ -0,0 +1,15 @@ +# $NetBSD: Makefile.inc,v 1.10 2004/04/30 21:13:23 kleink Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +.PATH: ${.CURDIR}/db/man + +MAN+= btree.3 dbm_clearerr.3 dbopen.3 hash.3 recno.3 mpool.3 +MLINKS+= dbm_clearerr.3 dbm_close.3 dbm_clearerr.3 dbm_delete.3 +MLINKS+= dbm_clearerr.3 dbm_dirfno.3 dbm_clearerr.3 dbm_error.3 +MLINKS+= dbm_clearerr.3 dbm_fetch.3 dbm_clearerr.3 dbm_firstkey.3 +MLINKS+= dbm_clearerr.3 dbm_nextkey.3 dbm_clearerr.3 dbm_open.3 +MLINKS+= dbm_clearerr.3 dbm_store.3 dbm_clearerr.3 ndbm.3 +MLINKS+= dbopen.3 db.3 +MLINKS+= mpool.3 mpool_open.3 mpool.3 mpool_filter.3 mpool.3 mpool_new.3 +MLINKS+= mpool.3 mpool_get.3 mpool.3 mpool_put.3 mpool.3 mpool_sync.3 +MLINKS+= mpool.3 mpool_close.3 diff --git a/lib/nbsd_libc/db/man/btree.3 b/lib/nbsd_libc/db/man/btree.3 new file mode 100644 index 000000000..193638696 --- /dev/null +++ b/lib/nbsd_libc/db/man/btree.3 @@ -0,0 +1,255 @@ +.\" $NetBSD: btree.3,v 1.12 2010/03/22 19:30:53 joerg 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. +.\" +.\" @(#)btree.3 8.4 (Berkeley) 8/18/94 +.\" +.Dd April 17, 2003 +.Dt BTREE 3 +.Os +.Sh NAME +.Nm btree +.Nd btree database access method +.Sh SYNOPSIS +.In sys/types.h +.In db.h +.Sh DESCRIPTION +The routine +.Fn dbopen +is the library interface to database files. +One of the supported file formats is btree files. +The general description of the database access methods is in +.Xr dbopen 3 , +this manual page describes only the btree specific information. +.Pp +The btree data structure is a sorted, balanced tree structure storing +associated key/data pairs. +.Pp +The btree access method specific data structure provided to +.Fn dbopen +is defined in the +.In db.h +include file as follows: +.Bd -literal +typedef struct { + u_long flags; + u_int cachesize; + int maxkeypage; + int minkeypage; + u_int psize; + int (*compare)(const DBT *key1, const DBT *key2); + size_t (*prefix)(const DBT *key1, const DBT *key2); + int lorder; +} BTREEINFO; +.Ed +.Pp +The elements of this structure are as follows: +.Bl -tag -width maxkeypagex +.It Fa flags +The flag value is specified by or'ing any of the following values: +.Bl -tag -width R_DUP -offset indent +.It Dv R_DUP +Permit duplicate keys in the tree, i.e. permit insertion if the key to +be inserted already exists in the tree. +The default behavior, as described in +.Xr dbopen 3 , +is to overwrite a matching key when inserting a new key or to fail if +the +.Dv R_NOOVERWRITE +flag is specified. +The +.Dv R_DUP +flag is overridden by the +.Dv R_NOOVERWRITE +flag, and if the +.Dv R_NOOVERWRITE +flag is specified, attempts to insert duplicate keys into the tree +will fail. +.Pp +If the database contains duplicate keys, the order of retrieval of +key/data pairs is undefined if the +.Em get +routine is used, however, +.Em seq +routine calls with the +.Dv R_CURSOR +flag set will always return the logical +.Dq first +of any group of duplicate keys. +.El +.It Fa cachesize +A suggested maximum size (in bytes) of the memory cache. +This value is +.Em only +advisory, and the access method will allocate more memory rather than +fail. +Since every search examines the root page of the tree, caching the +most recently used pages substantially improves access time. +In addition, physical writes are delayed as long as possible, so a +moderate cache can reduce the number of I/O operations significantly. +Obviously, using a cache increases (but only increases) the likelihood +of corruption or lost data if the system crashes while a tree is being +modified. +If +.Fa cachesize +is 0 (no size is specified) a default cache is used. +.It Fa maxkeypage +The maximum number of keys which will be stored on any single page. +Not currently implemented. +.\" The maximum number of keys which will be stored on any single page. +.\" Because of the way the btree data structure works, +.\" .Fa maxkeypage +.\" must always be greater than or equal to 2. +.\" If +.\" .Fa maxkeypage +.\" is 0 (no maximum number of keys is specified) the page fill factor is +.\" made as large as possible (which is almost invariably what is wanted). +.It Fa minkeypage +The minimum number of keys which will be stored on any single page. +This value is used to determine which keys will be stored on overflow +pages, i.e., if a key or data item is longer than the pagesize divided +by the +.Fa minkeypage +value, it will be stored on overflow pages instead of in the page +itself. +If +.Fa minkeypage +is 0 (no minimum number of keys is specified) a value of 2 is used. +.It Fa psize +Page size is the size (in bytes) of the pages used for nodes in the +tree. +The minimum page size is 512 bytes and the maximum page size is 64K. +If +.Fa psize +is 0 (no page size is specified) a page size is chosen based on the +underlying file system I/O block size. +.It Fa compare +Compare is the key comparison function. +It must return an integer less than, equal to, or greater than zero if +the first key argument is considered to be respectively less than, +equal to, or greater than the second key argument. +The same comparison function must be used on a given tree every time +it is opened. +If +.Fa compare +is +.Dv NULL +(no comparison function is specified), the keys are compared +lexically, with shorter keys considered less than longer keys. +.It Fa prefix +Prefix is the prefix comparison function. +If specified, this routine must return the number of bytes of the +second key argument which are necessary to determine that it is +greater than the first key argument. +If the keys are equal, the key length should be returned. +Note, the usefulness of this routine is very data dependent, but, in +some data sets can produce significantly reduced tree sizes and search +times. +If +.Fa prefix +is +.Dv NULL +(no prefix function is specified), +.Em and +no comparison function is specified, a default lexical comparison +routine is used. +If +.Fa prefix +is +.Dv NULL +and a comparison routine is specified, no prefix comparison is done. +.It Fa lorder +The byte order for integers in the stored database metadata. +The number should represent the order as an integer; for example, +big endian order would be the number 4,321. +If +.Fa lorder +is 0 (no order is specified) the current host order is used. +.El +.Pp +If the file already exists (and the +.Dv O_TRUNC +flag is not specified), the values specified for the parameters flags, +lorder and psize are ignored in favor of the values used when the tree +was created. +.Pp +Forward sequential scans of a tree are from the least key to the +greatest. +.Pp +Space freed up by deleting key/data pairs from the tree is never +reclaimed, although it is normally made available for reuse. +This means that the btree storage structure is grow-only. +The only solutions are to avoid excessive deletions, or to create a +fresh tree periodically from a scan of an existing one. +.Pp +Searches, insertions, and deletions in a btree will all complete in +O lg base N where base is the average fill factor. +Often, inserting ordered data into btrees results in a low fill +factor. +This implementation has been modified to make ordered insertion the +best case, resulting in a much better than normal page fill factor. +.Sh ERRORS +The +.Nm +access method routines may fail and set +.Va errno +for any of the errors specified for the library routine +.Xr dbopen 3 . +.Sh SEE ALSO +.Xr dbopen 3 , +.Xr hash 3 , +.Xr mpool 3 , +.Xr recno 3 +.Pp +.Rs +.%T "The Ubiquitous B-tree" +.%A "Douglas Comer" +.%J "ACM Comput. Surv." +.%V 2 +.%N 11 +.%D June 1979 +.%P 121-138 +.Re +.Rs +.%T "Prefix B-trees" +.%A "Bayer" +.%A "Unterauer" +.%J "ACM Transactions on Database Systems" +.%V Vol. 2 +.%N 1 +.%D March 1977 +.%P 11-26 +.Re +.Rs +.%B "The Art of Computer Programming Vol. 3: Sorting and Searching" +.%A "D.E. Knuth" +.%D 1968 +.%P 471-480 +.Re +.Sh BUGS +Only big and little endian byte order is supported. diff --git a/lib/nbsd_libc/db/man/dbm_clearerr.3 b/lib/nbsd_libc/db/man/dbm_clearerr.3 new file mode 100644 index 000000000..d9f06721c --- /dev/null +++ b/lib/nbsd_libc/db/man/dbm_clearerr.3 @@ -0,0 +1,306 @@ +.\" $NetBSD: dbm_clearerr.3,v 1.5 2010/05/05 06:55:57 jruoho Exp $ +.\" +.\" Copyright (c) 2004 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 May 5, 2010 +.Dt DBM_CLEARERR 3 +.Os +.Sh NAME +.Nm dbm_clearerr , +.Nm dbm_close , +.Nm dbm_delete , +.Nm dbm_dirfno , +.Nm dbm_error , +.Nm dbm_fetch , +.Nm dbm_firstkey , +.Nm dbm_nextkey , +.Nm dbm_open , +.Nm dbm_store , +.Nm ndbm +.Nd database functions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ndbm.h +.Ft int +.Fn dbm_clearerr "DBM *db" +.Ft void +.Fn dbm_close "DBM *db" +.Ft int +.Fn dbm_delete "DBM *db" "datum key" +.Ft int +.Fn dbm_dirfno "DBM *db" +.Ft int +.Fn dbm_error "DBM *db" +.Ft datum +.Fn dbm_fetch "DBM *db" "datum key" +.Ft datum +.Fn dbm_firstkey "DBM *db" +.Ft datum +.Fn dbm_nextkey "DBM *db" +.Ft DBM * +.Fn dbm_open "const char *file" "int open_flags" "mode_t file_mode" +.Ft int +.Fn dbm_store "DBM *db" "datum key" "datum content" "int store_mode" +.Sh DESCRIPTION +The +.Nm ndbm +facility provides access to hash database files. +.Pp +Two data types are fundamental to the +.Nm ndbm +facility. +.Fa DBM +serves as a handle to a database. +It is an opaque type. +.Pp +The other data type is +.Fa datum , +which is a structure type which includes the following members: +.Bd -literal -offset indent +void * dptr +size_t dsize +.Ed +.Pp +A +.Fa datum +is thus given by +.Fa dptr +pointing at an object of +.Fa dsize +bytes in length. +.Pp +The +.Fn dbm_open +function opens a database. +The +.Fa file +argument is the pathname which the actual database file pathname +is based on. +This implementation uses a single file with the suffix +.Pa .db +appended to +.Fa file . +The +.Fa open_flags +argument has the same meaning as the +.Fa flags +argument to +.Xr open 2 +except that when opening a database for write-only access the file +is opened for read/write access, and the +.Dv O_APPEND +flag must not be specified. +The +.Fa file_mode +argument has the same meaning as the +.Fa mode +argument to +.Xr open 2 . +.Pp +For the following functions, the +.Fa db +argument is a handle previously returned by a call to +.Fn dbm_open . +.Pp +The +.Fn dbm_close +function closes a database. +.Pp +The +.Fn dbm_fetch +function retrieves a record from the database. +The +.Fa key +argument is a +.Fa datum +that identifies the record to be fetched. +.Pp +The +.Fn dbm_store +function stores a record into the database. +The +.Fa key +argument is a +.Fa datum +that identifies the record to be stored. +The +.Fa content +argument is a +.Fa datum +that specifies the value of the record to be stored. +The +.Fa store_mode +argument specifies the behavior of +.Fn dbm_store +if a record matching +.Fa key +is already present in the database, +.Fa db . +.Fa store_mode +must be one of the following: +.Bl -tag -width DBM_REPLACEXX -offset indent +.It Dv DBM_INSERT +If a record matching +.Fa key +is already present, it is left unchanged. +.It Dv DBM_REPLACE +If a record matching +.Fa key +is already present, its value is replaced by +.Fa content . +.El +.Pp +If no record matching +.Fa key +is present, a new record is inserted regardless of +.Fa store_mode . +.Pp +The +.Fn dbm_delete +function deletes a record from the database. +The +.Fa key +argument is a +.Fa datum +that identifies the record to be deleted. +.Pp +The +.Fn dbm_firstkey +function returns the first key in the database. +.Pp +The +.Fn dbm_nextkey +function returns the next key in the database. +In order to be meaningful, it must be preceded by a call to +.Fn dbm_firstkey . +.Pp +The +.Fn dbm_error +function returns the error indicator of the database. +.Pp +The +.Fn dbm_clearerr +function clears the error indicator of the database. +.Pp +The +.Fn dbm_dirfno +function returns the file descriptor of the underlying database file. +.Sh IMPLEMENTATION NOTES +The +.Nm ndbm +facility is implemented on top of the +.Xr hash 3 +access method of the +.Xr db 3 +database facility. +.Sh RETURN VALUES +The +.Fn dbm_open +function returns a pointer to a +.Fa DBM +when successful; otherwise a null pointer is returned. +.Pp +The +.Fn dbm_close +function returns no value. +.Pp +The +.Fn dbm_fetch +function returns a content +.Fa datum ; +if no record matching +.Fa key +was found or if an error occured, its +.Fa dptr +member is a null pointer. +.Pp +The +.Fn dbm_store +function returns 0 when then record was successfully inserted; +it returns 1 when called with +.Fa store_mode +being +.Dv DBM_INSERT +and a record matching +.Fa key +is already present; +otherwise a negative value is returned. +.Pp +The +.Fn dbm_delete +function returns 0 when the record was successfully deleted; +otherwise a negative value is returned. +.Pp +The +.Fn dbm_firstkey +and +.Fn dbm_nextkey +functions return a key +.Fa datum . +When the end of the database is reached or if an error occured, its +.Fa dptr +member is a null pointer. +.Pp +The +.Fn dbm_error +function returns 0 if the error indicator is clear; +if the error indicator is set a non-zero value is returned. +.Pp +The +.Fn dbm_clearerr +function always returns 0. +.Pp +The +.Fn dbm_dirfno +function returns the file descriptor of the underlying database file. +.Sh ERRORS +No errors are defined. +.Sh SEE ALSO +.Xr open 2 , +.Xr db 3 , +.Xr hash 3 +.Sh STANDARDS +The +.Fn dbm_clearerr , +.Fn dbm_close , +.Fn dbm_delete , +.Fn dbm_error , +.Fn dbm_fetch , +.Fn dbm_firstkey , +.Fn dbm_nextkey , +.Fn dbm_open , +and +.Fn dbm_store +functions conform to +.St -xpg4.2 +and +.St -susv2 . +The +.Fn dbm_dirfno +function is an extension. diff --git a/lib/nbsd_libc/db/man/dbopen.3 b/lib/nbsd_libc/db/man/dbopen.3 new file mode 100644 index 000000000..1db9a25f6 --- /dev/null +++ b/lib/nbsd_libc/db/man/dbopen.3 @@ -0,0 +1,538 @@ +.\" $NetBSD: dbopen.3,v 1.19 2010/12/16 12:08:16 jruoho 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. +.\" +.\" @(#)dbopen.3 8.5 (Berkeley) 1/2/94 +.\" +.Dd December 16, 2010 +.Dt DBOPEN 3 +.Os +.Sh NAME +.Nm dbopen , +.Nm db +.Nd database access methods +.Sh SYNOPSIS +.In sys/types.h +.In limits.h +.In db.h +.In fcntl.h +.Ft DB * +.Fn dbopen "const char *file" "int flags" "mode_t mode" \ +"DBTYPE type" "const void *openinfo" +.Sh DESCRIPTION +.Nm +is the library interface to database files. +The supported file formats are btree, hashed, and UNIX file oriented. +The btree format is a representation of a sorted, balanced tree +structure. +The hashed format is an extensible, dynamic hashing scheme. +The flat-file format is a byte stream file with fixed or variable +length records. +The formats and file format specific information are described in +detail in their respective manual pages +.Xr btree 3 , +.Xr hash 3 , +and +.Xr recno 3 . +.Pp +The +.Fn dbopen +function opens +.Fa file +for reading and/or writing. +Files never intended to be preserved on disk may be created by setting +the file parameter to +.Dv NULL . +.Pp +The +.Fa flags +and +.Fa mode +arguments are as specified to the +.Xr open 2 +routine, however, only the +.Dv O_CREAT , +.Dv O_EXCL , +.Dv O_EXLOCK , +.Dv O_NONBLOCK , +.Dv O_RDONLY , +.Dv O_RDWR , +.Dv O_SHLOCK , +and +.Dv O_TRUNC +flags are meaningful. +(Note, opening a database file +.Dv O_WRONLY +is not possible.) +.\"Three additional options may be specified by or'ing +.\"them into the +.\".Fa flags +.\"argument. +.\".Pp +.\".Dv DB_LOCK +.\"Do the necessary locking in the database to support concurrent access. +.\"If concurrent access isn't needed or the database is read-only this +.\"flag should not be set, as it tends to have an associated performance +.\"penalty. +.\".Pp +.\".Dv DB_SHMEM +.\"Place the underlying memory pool used by the database in shared +.\"memory. +.\"Necessary for concurrent access. +.\".Pp +.\".Dv DB_TXN +.\"Support transactions in the database. +.\"The +.\".Dv DB_LOCK +.\"and +.\".Dv DB_SHMEM +.\"flags must be set as well. +.Pp +The +.Fa type +argument is of type +.Vt DBTYPE +(as defined in the +.In db.h +include file) and may be set to +.Dv DB_BTREE , +.Dv DB_HASH , +or +.Dv DB_RECNO . +.Pp +The +.Fa openinfo +argument is a pointer to an access method specific structure described +in the access method's manual page. +If +.Fa openinfo +is +.Dv NULL , +each access method will use defaults appropriate for the system and +the access method. +.Ss The DB Structure +The +.Fn dbopen +function returns a pointer to a DB structure on success and +.Dv NULL +on error. +The DB structure is defined in the +.In db.h +include file, and contains at least the following fields: +.Bd -literal -offset indent +typedef struct { + DBTYPE type; + int (*close)(const DB *db); + int (*del)(const DB *db, const DBT *key, u_int flags); + int (*fd)(const DB *db); + int (*get)(const DB *db, DBT *key, DBT *data, u_int flags); + int (*put)(const DB *db, DBT *key, const DBT *data, + u_int flags); + int (*sync)(const DB *db, u_int flags); + int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags); +} DB; +.Ed +.Pp +These elements describe a database type and a set of functions +performing various actions. +These functions take a pointer to a structure as returned by +.Nm , +and sometimes one or more pointers to key/data structures and a flag +value. +.Bl -tag -width closex -offset indent +.It Fa type +The type of the underlying access method (and file format). +.It Fa close +A pointer to a routine to flush any cached information to disk, free +any allocated resources, and close the underlying file(s). +Since key/data pairs may be cached in memory, failing to sync the file +with a +.Fa close +or +.Fa sync +function may result in inconsistent or lost information. +.Fa close +routines return \-1 on error (setting +.Va errno ) +and 0 on success. +.It Fa del +A pointer to a routine to remove key/data pairs from the database. +.Pp +The parameter +.Fa flag +may be set to the following value: +.Bl -tag -width R_CURSORX +.It Dv R_CURSOR +Delete the record referenced by the cursor. +The cursor must have previously been initialized. +.El +.Pp +.Fa delete +routines return \-1 on error (setting +.Va errno ) , +0 on success, and 1 if the specified +.Fa key +was not in the file. +.It Fa fd +A pointer to a routine which returns a file descriptor representative +of the underlying database. +A file descriptor referencing the same file will be returned to all +processes which call +.Nm +with the same +.Fa file +name. +This file descriptor may be safely used as an argument to the +.Xr fcntl 2 +and +.Xr flock 2 +locking functions. +The file descriptor is not necessarily associated with any of the +underlying files used by the access method. +No file descriptor is available for in memory databases. +.Fa fd +routines return \-1 on error (setting +.Va errno ) , +and the file descriptor on success. +.It Fa get +A pointer to a routine which is the interface for keyed retrieval from +the database. +The address and length of the data associated with the specified +.Fa key +are returned in the structure referenced by +.Fa data . +.Fa get +routines return \-1 on error (setting +.Va errno ) , +0 on success, and 1 if the +.Fa key +was not in the file. +.It Fa put +A pointer to a routine to store key/data pairs in the database. +.Pp +The parameter +.Fa flag +may be set to one of the following values: +.Bl -tag -width R_NOOVERWRITEX +.It Dv R_CURSOR +Replace the key/data pair referenced by the cursor. +The cursor must have previously been initialized. +.It Dv R_IAFTER +Append the data immediately after the data referenced by +.Fa key , +creating a new key/data pair. +The record number of the appended key/data pair is returned in the +.Fa key +structure. +(Applicable only to the +.Dv DB_RECNO +access method.) +.It Dv R_IBEFORE +Insert the data immediately before the data referenced by +.Fa key , +creating a new key/data pair. +The record number of the inserted key/data pair is returned in the +.Fa key +structure. +(Applicable only to the +.Dv DB_RECNO +access method.) +.It Dv R_NOOVERWRITE +Enter the new key/data pair only if the key does not previously +exist. +.It Dv R_SETCURSOR +Store the key/data pair, setting or initializing the position of the +cursor to reference it. +(Applicable only to the +.Dv DB_BTREE +and +.Dv DB_RECNO +access methods.) +.El +.Pp +.Dv R_SETCURSOR +is available only for the +.Dv DB_BTREE +and +.Dv DB_RECNO +access methods because it implies that the keys have an inherent order +which does not change. +.Pp +.Dv R_IAFTER +and +.Dv R_IBEFORE +are available only for the +.Dv DB_RECNO +access method because they each imply that the access method is able +to create new keys. +This is only true if the keys are ordered and independent, record +numbers for example. +.Pp +The default behavior of the +.Fa put +routines is to enter the new key/data pair, replacing any previously +existing key. +.Pp +.Fa put +routines return \-1 on error (setting +.Va errno ) , +0 on success, and 1 if the +.Dv R_NOOVERWRITE +.Fa flag +was set and the key already exists in the file. +.It Fa seq +A pointer to a routine which is the interface for sequential +retrieval from the database. +The address and length of the key are returned in the structure +referenced by +.Fa key , +and the address and length of the data are returned in the +structure referenced by +.Fa data . +.Pp +Sequential key/data pair retrieval may begin at any time, and the +position of the +.Dq cursor +is not affected by calls to the +.Fa del , +.Fa get , +.Fa put , +or +.Fa sync +routines. +Modifications to the database during a sequential scan will be +reflected in the scan, i.e., records inserted behind the cursor will +not be returned while records inserted in front of the cursor will be +returned. +.Pp +The flag value +.Em must +be set to one of the following values: +.Bl -tag -width R_CURSORX +.It Dv R_CURSOR +The data associated with the specified key is returned. +This differs from the +.Fa get +routines in that it sets or initializes the cursor to the location of +the key as well. +(Note, for the +.Dv DB_BTREE +access method, the returned key is not necessarily an exact match for +the specified key. +The returned key is the smallest key greater than or equal to the +specified key, permitting partial key matches and range searches.) +.It Dv R_FIRST +The first key/data pair of the database is returned, and the cursor +is set or initialized to reference it. +.It Dv R_LAST +The last key/data pair of the database is returned, and the cursor +is set or initialized to reference it. +(Applicable only to the +.Dv DB_BTREE +and +.Dv DB_RECNO +access methods.) +.It Dv R_NEXT +Retrieve the key/data pair immediately after the cursor. +If the cursor is not yet set, this is the same as the +.Dv R_FIRST +flag. +.It Dv R_PREV +Retrieve the key/data pair immediately before the cursor. +If the cursor is not yet set, this is the same as the +.Dv R_LAST +flag. +(Applicable only to the +.Dv DB_BTREE +and +.Dv DB_RECNO +access methods.) +.El +.Pp +.Dv R_LAST +and +.Dv R_PREV +are available only for the +.Dv DB_BTREE +and +.Dv DB_RECNO +access methods because they each imply that the keys have an inherent +order which does not change. +.Pp +.Fa seq +routines return \-1 on error (setting +.Va errno ) , +0 on success and 1 if there are no key/data pairs less than or greater +than the specified or current key. +If the +.Dv DB_RECNO +access method is being used, and if the database file is a character +special file and no complete key/data pairs are currently available, +the +.Fa seq +routines return 2. +.It Fa sync +A pointer to a routine to flush any cached information to disk. +If the database is in memory only, the +.Fa sync +routine has no effect and will always succeed. +.Pp +The flag value may be set to the following value: +.Bl -tag -width ".Dv R_RECNOSYNC" +.It Dv R_RECNOSYNC +If the +.Dv DB_RECNO +access method is being used, this flag causes the sync routine to +apply to the btree file which underlies the recno file, not the recno +file itself. +(See the +.Fa bfname +field of the +.Xr recno 3 +manual page for more information.) +.El +.Pp +.Fa sync +routines return \-1 on error (setting +.Va errno ) +and 0 on success. +.El +.Ss Key/data Pairs +Access to all file types is based on key/data pairs. +Both keys and data are represented by the following data structure: +.Bd -literal -offset indent +typedef struct { + void *data; + size_t size; +} DBT; +.Ed +.Pp +The elements of the DBT structure are defined as follows: +.Bl -tag -width datax -offset indent +.It Fa data +A pointer to a byte string. +.It Fa size +The length of the byte string. +.El +.Pp +Key and data byte strings may reference strings of essentially +unlimited length although any two of them must fit into available +memory at the same time. +It should be noted that the access methods provide no guarantees about +byte string alignment. +.Sh ERRORS +The +.Nm +routine may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr open 2 +and +.Xr malloc 3 +or the following: +.Bl -tag -width Er +.It Er EFTYPE +A file is incorrectly formatted. +.It Er EINVAL +A parameter has been specified (hash function, pad byte, etc.) that is +incompatible with the current file specification or which is not +meaningful for the function (for example, use of the cursor without +prior initialization) or there is a mismatch between the version +number of file and the software. +.It Er EFBIG +The key could not be inserted due to limitations in the DB file format +(e.g., a hash database was out of overflow pages). +.El +.Pp +The +.Fa close +routines may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr close 2 , +.Xr read 2 , +.Xr write 2 , +.Xr free 3 , +or +.Xr fsync 2 . +.Pp +The +.Fa del , +.Fa get , +.Fa put , +and +.Fa seq +routines may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr read 2 , +.Xr write 2 , +.Xr free 3 , +or +.Xr malloc 3 . +.Pp +The +.Fa fd +routines will fail and set +.Va errno +to +.Er ENOENT +for in memory databases. +.Pp +The +.Fa sync +routines may fail and set +.Va errno +for any of the errors specified for the library routine +.Xr fsync 2 . +.Sh SEE ALSO +.Xr btree 3 , +.Xr hash 3 , +.Xr mpool 3 , +.Xr recno 3 +.Pp +.Rs +.%T LIBTP: Portable, Modular Transactions for UNIX +.%A Margo Seltzer +.%A Michael Olson +.%I USENIX Association +.%B Proceedings of the 1992 Winter USENIX Technical Conference +.%D 1992 +.%P 9-25 +.Re +.Sh BUGS +The typedef DBT is a mnemonic for +.Dq data base thang , +and was used because no one could think of a reasonable name that +wasn't already used. +.Pp +The file descriptor interface is a kludge and will be deleted in a +future version of the interface. +.Pp +None of the access methods provide any form of concurrent access, +locking, or transactions. diff --git a/lib/nbsd_libc/db/man/hash.3 b/lib/nbsd_libc/db/man/hash.3 new file mode 100644 index 000000000..eaac286cb --- /dev/null +++ b/lib/nbsd_libc/db/man/hash.3 @@ -0,0 +1,172 @@ +.\" $NetBSD: hash.3,v 1.14 2010/12/16 11:57:20 jruoho 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. +.\" +.\" @(#)hash.3 8.6 (Berkeley) 8/18/94 +.\" +.Dd December 16, 2010 +.Dt HASH 3 +.Os +.Sh NAME +.Nm hash +.Nd hash database access method +.Sh SYNOPSIS +.In sys/types.h +.In db.h +.Sh DESCRIPTION +The routine +.Fn dbopen +is the library interface to database files. +One of the supported file formats is hash files. +The general description of the database access methods is in +.Xr dbopen 3 , +this manual page describes only the hash specific information. +.Pp +The hash data structure is an extensible, dynamic hashing scheme. +.Pp +The access method specific data structure provided to +.Fn dbopen +is defined in the +.In db.h +header as follows: +.Bd -literal -offset indent +typedef struct { + u_int bsize; + u_int ffactor; + u_int nelem; + u_int cachesize; + uint32_t (*hash)(const void *, size_t); + int lorder; +} HASHINFO; +.Ed +.Pp +The elements of this structure are as follows: +.Bl -tag -width cachesizex +.It Fa bsize +.Fa bsize +defines the hash table bucket size, and defaults to 4096 for in-memory tables. +If +.Fa bsize +is 0 (no bucket size is specified) a bucket size is chosen based on the +underlying file system I/O block size. +It may be preferable to increase the page size for disk-resident +tables and tables with large data items. +.It Fa ffactor +.Fa ffactor +indicates a desired density within the hash table. +It is an approximation of the number of keys allowed to accumulate in +any one bucket, determining when the hash table grows or shrinks. +The default value is 8. +.It Fa nelem +.Fa nelem +is an estimate of the final size of the hash table. +If not set or set too low, hash tables will expand gracefully as keys +are entered, although a slight performance degradation may be +noticed. +The default value is 1. +.It Fa cachesize +A suggested maximum size, in bytes, of the memory cache. +This value is +.Em only +advisory, and the access method will allocate more memory rather +than fail. +.It Fa hash +.Fa hash +is a user defined hash function. +Since no hash function performs equally well on all possible data, the +user may find that the built-in hash function does poorly on a +particular data set. +User specified hash functions must take two arguments (a pointer to a +byte string and a length) and return a 32-bit quantity to be used as +the hash value. +.It Fa lorder +The byte order for integers in the stored database metadata. +The number should represent the order as an integer; for example, +big endian order would be the number 4,321. +If +.Fa lorder +is 0 (no order is specified) the current host order is used. +If the file already exists, the specified value is ignored and the +value specified when the tree was created is used. +.El +.Pp +If the file already exists (and the +.Dv O_TRUNC +flag is not specified), the values specified for the parameters +.Fa bsize , +.Fa ffactor , +.Fa lorder , +and +.Fa nelem +are ignored and the values specified when the tree was created are +used. +.Pp +If a hash function is specified, +.Fn hash_open +will attempt to determine if the hash function specified is the same +as the one with which the database was created, and will fail if it is +not. +.\".Pp +.\"Backward compatible interfaces to the routines described in +.\".Xr dbm 3 , +.\"and +.\".Xr ndbm 3 +.\"are provided, however these interfaces are not compatible with +.\"previous file formats. +.Sh ERRORS +The +.Nm +access method routines may fail and set +.Va errno +for any of the errors specified for the library routine +.Xr dbopen 3 . +.Sh SEE ALSO +.Xr btree 3 , +.Xr dbopen 3 , +.Xr mpool 3 , +.Xr recno 3 +.Pp +.Rs +.%T Dynamic Hash Tables +.%A Per-Ake Larson +.%J Communications of the ACM +.%D April 1988 +.%N Issue 4 +.%V Volume 31 +.Re +.Rs +.%T A New Hash Package for UNIX +.%A Margo Seltzer +.%I USENIX Association +.%B Proceedings of the 1991 Winter USENIX Technical Conference +.%D January 1991 +.%P 173-184 +.%U http://www.usenix.org/publications/library/proceedings/seltzer2.pdf +.Re +.Sh BUGS +Only big and little endian byte order is supported. diff --git a/lib/nbsd_libc/db/man/mpool.3 b/lib/nbsd_libc/db/man/mpool.3 new file mode 100644 index 000000000..32673f90f --- /dev/null +++ b/lib/nbsd_libc/db/man/mpool.3 @@ -0,0 +1,226 @@ +.\" $NetBSD: mpool.3,v 1.10 2010/12/16 11:49:35 jruoho 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. +.\" +.\" @(#)mpool.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd December 16, 2010 +.Dt MPOOL 3 +.Os +.Sh NAME +.Nm mpool , +.Nm mpool_open , +.Nm mpool_filter , +.Nm mpool_new , +.Nm mpool_get , +.Nm mpool_put , +.Nm mpool_sync , +.Nm mpool_close +.Nd shared memory buffer pool +.Sh SYNOPSIS +.In db.h +.In mpool.h +.Ft MPOOL * +.Fn mpool_open "DBT *key" "int fd" "pgno_t pagesize" "pgno_t maxcache" +.Ft void +.Fn mpool_filter "MPOOL *mp" "void (*pgin)(void *, pgno_t, void *)" \ +"void (*pgout)(void *, pgno_t, void *)" "void *pgcookie" +.Ft void * +.Fn mpool_new "MPOOL *mp" "pgno_t *pgnoaddr" +.Ft void * +.Fn mpool_get "MPOOL *mp" "pgno_t pgno" "u_int flags" +.Ft int +.Fn mpool_put "MPOOL *mp" "void *pgaddr" "u_int flags" +.Ft int +.Fn mpool_sync "MPOOL *mp" +.Ft int +.Fn mpool_close "MPOOL *mp" +.Sh DESCRIPTION +.Nm +is the library interface intended to provide page oriented buffer +management of files. +The buffers may be shared between processes. +.Pp +The function +.Fn mpool_open +initializes a memory pool. +The +.Fa key +argument is the byte string used to negotiate between multiple +processes wishing to share buffers. +If the file buffers are mapped in shared memory, all processes using +the same key will share the buffers. +If +.Fa key +is +.Dv NULL , +the buffers are mapped into private memory. +The +.Fa fd +argument is a file descriptor for the underlying file, which must be +seekable. +If +.Fa key +is +.No non- Ns Dv NULL +and matches a file already being mapped, the +.Fa fd +argument is ignored. +.Pp +The +.Fa pagesize +argument is the size, in bytes, of the pages into which the file is +broken up. +The +.Fa maxcache +argument is the maximum number of pages from the underlying file to +cache at any one time. +This value is not relative to the number of processes which share a +file's buffers, but will be the largest value specified by any of the +processes sharing the file. +.Pp +The +.Fn mpool_filter +function is intended to make transparent input and output processing +of the pages possible. +If the +.Fa pgin +function is specified, it is called each time a buffer is read into +the memory pool from the backing file. +If the +.Fa pgout +function is specified, it is called each time a buffer is written into +the backing file. +Both functions are are called with the +.Fa pgcookie +pointer, the page number and a pointer to the page to being read or +written. +.Pp +The function +.Fn mpool_new +takes an MPOOL pointer and an address as arguments. +If a new page can be allocated, a pointer to the page is returned and +the page number is stored into the +.Fa pgnoaddr +address. +Otherwise, +.Dv NULL +is returned and errno is set. +.Pp +The function +.Fn mpool_get +takes a MPOOL pointer and a page number as arguments. +If the page exists, a pointer to the page is returned. +Otherwise, +.Dv NULL +is returned and errno is set. +The flags parameter is not currently used. +.Pp +The function +.Fn mpool_put +unpins the page referenced by +.Fa pgaddr . +.Fa pgaddr +must be an address previously returned by +.Fn mpool_get +or +.Fn mpool_new . +The flag value is specified by or'ing any of the following values: +.Bl -tag -width MPOOL_DIRTYX -offset indent +.It Dv MPOOL_DIRTY +The page has been modified and needs to be written to the backing +file. +.El +.Pp +.Fn mpool_put +returns 0 on success and \-1 if an error occurs. +.Pp +The function +.Fn mpool_sync +writes all modified pages associated with the MPOOL pointer to the +backing file. +.Fn mpool_sync +returns 0 on success and \-1 if an error occurs. +.Pp +The +.Fn mpool_close +function frees up any allocated memory associated with the memory pool +cookie. +Modified pages are +.Em not +written to the backing file. +.Fn mpool_close +returns 0 on success and \-1 if an error occurs. +.Sh ERRORS +The +.Fn mpool_open +function may fail and set +.Va errno +for any of the errors specified for the library routine +.Xr malloc 3 . +.Pp +The +.Fn mpool_get +function may fail and set +.Va errno +for the following: +.Bl -tag -width Er -offset indent +.It Er EINVAL +The requested record doesn't exist. +.El +.Pp +The +.Fn mpool_new +and +.Fn mpool_get +functions may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr read 2 , +.Xr write 2 , +and +.Xr malloc 3 . +.Pp +The +.Fn mpool_sync +function may fail and set +.Va errno +for any of the errors specified for the library routine +.Xr write 2 . +.Pp +The +.Fn mpool_close +function may fail and set +.Va errno +for any of the errors specified for the library routine +.Xr free 3 . +.Sh SEE ALSO +.Xr btree 3 , +.Xr dbopen 3 , +.Xr hash 3 , +.Xr recno 3 diff --git a/lib/nbsd_libc/db/man/recno.3 b/lib/nbsd_libc/db/man/recno.3 new file mode 100644 index 000000000..a38871fe7 --- /dev/null +++ b/lib/nbsd_libc/db/man/recno.3 @@ -0,0 +1,214 @@ +.\" $NetBSD: recno.3,v 1.11 2010/03/22 19:30:53 joerg 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. +.\" +.\" @(#)recno.3 8.5 (Berkeley) 8/18/94 +.\" +.Dd April 17, 2003 +.Dt RECNO 3 +.Os +.Sh NAME +.Nm recno +.Nd record number database access method +.Sh SYNOPSIS +.In sys/types.h +.In db.h +.Sh DESCRIPTION +The routine +.Fn dbopen +is the library interface to database files. +One of the supported file formats is record number files. +The general description of the database access methods is in +.Xr dbopen 3 , +this manual page describes only the recno specific information. +.Pp +The record number data structure is either variable or fixed-length +records stored in a flat-file format, accessed by the logical record +number. +The existence of record number five implies the existence of records +one through four, and the deletion of record number one causes +record number five to be renumbered to record number four, as well +as the cursor, if positioned after record number one, to shift down +one record. +.Pp +The recno access method specific data structure provided to +.Fn dbopen +is defined in the +.In db.h +include file as follows: +.Bd -literal +typedef struct { + u_long flags; + u_int cachesize; + u_int psize; + int lorder; + size_t reclen; + uint8_t bval; + char *bfname; +} RECNOINFO; +.Ed +.Pp +The elements of this structure are defined as follows: +.Bl -tag -width cachesizex +.It Fa flags +The flag value is specified by or'ing any of the following values: +.Bl -tag -width R_FIXEDLENX -offset indent +.It Dv R_FIXEDLEN +The records are fixed-length, not byte delimited. +The structure element +.Fa reclen +specifies the length of the record, and the structure element +.Fa bval +is used as the pad character. +Any records, inserted into the database, that are less than +.Fa reclen +bytes long are automatically padded. +.It Dv R_NOKEY +In the interface specified by +.Fn dbopen , +the sequential record retrieval fills in both the caller's key and +data structures. +If the +.Dv R_NOKEY +flag is specified, the cursor routines are not required to fill in the +key structure. +This permits applications to retrieve records at the end of files +without reading all of the intervening records. +.It Dv R_SNAPSHOT +This flag requires that a snapshot of the file be taken when +.Fn dbopen +is called, instead of permitting any unmodified records to be read +from the original file. +.El +.It Fa cachesize +A suggested maximum size, in bytes, of the memory cache. +This value is +.Em only +advisory, and the access method will allocate more memory rather than +fail. +If +.Fa cachesize +is 0 (no size is specified) a default cache is used. +.It Fa psize +The recno access method stores the in-memory copies of its records +in a btree. +This value is the size (in bytes) of the pages used for nodes in that +tree. +If +.Fa psize +is 0 (no page size is specified) a page size is chosen based on the +underlying file system I/O block size. +See +.Xr btree 3 +for more information. +.It Fa lorder +The byte order for integers in the stored database metadata. +The number should represent the order as an integer; for example, +big endian order would be the number 4,321. +If +.Fa lorder +is 0 (no order is specified) the current host order is used. +.It Fa reclen +The length of a fixed-length record. +.It Fa bval +The delimiting byte to be used to mark the end of a record for +variable-length records, and the pad character for fixed-length +records. +If no value is specified, newlines +.Pq Dq \en +are used to mark the end of variable-length records and fixed-length +records are padded with spaces. +.It Fa bfname +The recno access method stores the in-memory copies of its records +in a btree. +If bfname is +.No non- Ns Dv NULL , +it specifies the name of the btree file, as if specified as the file +name for a +.Fn dbopen +of a btree file. +.El +.Pp +The data part of the key/data pair used by the recno access method +is the same as other access methods. +The key is different. +The +.Fa data +field of the key should be a pointer to a memory location of type +recno_t, as defined in the +.In db.h +include file. +This type is normally the largest unsigned integral type available to +the implementation. +The +.Fa size +field of the key should be the size of that type. +.Pp +Because there can be no meta-data associated with the underlying +recno access method files, any changes made to the default values +(e.g., fixed record length or byte separator value) must be explicitly +specified each time the file is opened. +.Pp +In the interface specified by +.Fn dbopen , +using the +.Fa put +interface to create a new record will cause the creation of multiple, +empty records if the record number is more than one greater than the +largest record currently in the database. +.Sh ERRORS +The +.Nm +access method routines may fail and set +.Va errno +for any of the errors specified for the library routine +.Xr dbopen 3 +or the following: +.Bl -tag -width Er +.It Er EINVAL +An attempt was made to add a record to a fixed-length database that +was too large to fit. +.El +.Sh SEE ALSO +.Xr btree 3 , +.Xr dbopen 3 , +.Xr hash 3 , +.Xr mpool 3 +.Pp +.Rs +.%T "Document Processing in a Relational Database System" +.%A Michael Stonebraker +.%A Heidi Stettner +.%A Joseph Kalash +.%A Antonin Guttman +.%A Nadene Lynn +.%J Memorandum No. UCB/ERL M82/32 +.%D May 1982 +.Re +.Sh BUGS +Only big and little endian byte order is supported. diff --git a/lib/nbsd_libc/db/mpool/Makefile.inc b/lib/nbsd_libc/db/mpool/Makefile.inc new file mode 100644 index 000000000..c9320fb0a --- /dev/null +++ b/lib/nbsd_libc/db/mpool/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.4 1995/02/27 13:23:53 cgd Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +.PATH: ${.CURDIR}/db/mpool + +SRCS+= mpool.c diff --git a/lib/nbsd_libc/db/mpool/README b/lib/nbsd_libc/db/mpool/README new file mode 100644 index 000000000..13002b30f --- /dev/null +++ b/lib/nbsd_libc/db/mpool/README @@ -0,0 +1,8 @@ +# $NetBSD: README,v 1.2 1995/02/27 13:24:00 cgd Exp $ +# @(#)README 8.1 (Berkeley) 6/4/93 + +These are the current memory pool routines. +They aren't ready for prime time, yet, and +the interface is expected to change. + +--keith diff --git a/lib/nbsd_libc/db/mpool/mpool.c b/lib/nbsd_libc/db/mpool/mpool.c new file mode 100644 index 000000000..a9ce4a0cf --- /dev/null +++ b/lib/nbsd_libc/db/mpool/mpool.c @@ -0,0 +1,462 @@ +/* $NetBSD: mpool.c,v 1.19 2009/04/22 18:44:06 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: mpool.c,v 1.19 2009/04/22 18:44:06 christos Exp $"); + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include + +#include + +#define __MPOOLINTERFACE_PRIVATE +#include + +#ifdef __weak_alias +__weak_alias(mpool_close,_mpool_close) +__weak_alias(mpool_filter,_mpool_filter) +__weak_alias(mpool_get,_mpool_get) +__weak_alias(mpool_new,_mpool_new) +__weak_alias(mpool_open,_mpool_open) +__weak_alias(mpool_put,_mpool_put) +__weak_alias(mpool_sync,_mpool_sync) +#endif + +static BKT *mpool_bkt(MPOOL *); +static BKT *mpool_look(MPOOL *, pgno_t); +static int mpool_write(MPOOL *, BKT *); + +/* + * mpool_open -- + * Initialize a memory pool. + */ +/*ARGSUSED*/ +MPOOL * +mpool_open(void *key, int fd, pgno_t pagesize, pgno_t maxcache) +{ + struct stat sb; + MPOOL *mp; + int entry; + + /* + * Get information about the file. + * + * XXX + * We don't currently handle pipes, although we should. + */ + if (fstat(fd, &sb)) + return (NULL); + if (!S_ISREG(sb.st_mode)) { + errno = ESPIPE; + return (NULL); + } + + /* Allocate and initialize the MPOOL cookie. */ + if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL) + return (NULL); + CIRCLEQ_INIT(&mp->lqh); + for (entry = 0; entry < HASHSIZE; ++entry) + CIRCLEQ_INIT(&mp->hqh[entry]); + mp->maxcache = maxcache; + mp->npages = (pgno_t)(sb.st_size / pagesize); + mp->pagesize = pagesize; + mp->fd = fd; + return (mp); +} + +/* + * mpool_filter -- + * Initialize input/output filters. + */ +void +mpool_filter(MPOOL *mp, void (*pgin)(void *, pgno_t, void *), + void (*pgout)(void *, pgno_t, void *), void *pgcookie) +{ + mp->pgin = pgin; + mp->pgout = pgout; + mp->pgcookie = pgcookie; +} + +/* + * mpool_new -- + * Get a new page of memory. + */ +void * +mpool_new( MPOOL *mp, pgno_t *pgnoaddr) +{ + struct _hqh *head; + BKT *bp; + + if (mp->npages == MAX_PAGE_NUMBER) { + (void)fprintf(stderr, "mpool_new: page allocation overflow.\n"); + abort(); + } +#ifdef STATISTICS + ++mp->pagenew; +#endif + /* + * Get a BKT from the cache. Assign a new page number, attach + * it to the head of the hash chain, the tail of the lru chain, + * and return. + */ + if ((bp = mpool_bkt(mp)) == NULL) + return (NULL); + *pgnoaddr = bp->pgno = mp->npages++; + bp->flags = MPOOL_PINNED; + + head = &mp->hqh[HASHKEY(bp->pgno)]; + CIRCLEQ_INSERT_HEAD(head, bp, hq); + CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); + return (bp->page); +} + +/* + * mpool_get + * Get a page. + */ +/*ARGSUSED*/ +void * +mpool_get(MPOOL *mp, pgno_t pgno, u_int flags) +{ + struct _hqh *head; + BKT *bp; + off_t off; + ssize_t nr; + + /* Check for attempt to retrieve a non-existent page. */ + if (pgno >= mp->npages) { + errno = EINVAL; + return (NULL); + } + +#ifdef STATISTICS + ++mp->pageget; +#endif + + /* Check for a page that is cached. */ + if ((bp = mpool_look(mp, pgno)) != NULL) { +#ifdef DEBUG + if (bp->flags & MPOOL_PINNED) { + (void)fprintf(stderr, + "mpool_get: page %d already pinned\n", bp->pgno); + abort(); + } +#endif + /* + * Move the page to the head of the hash chain and the tail + * of the lru chain. + */ + head = &mp->hqh[HASHKEY(bp->pgno)]; + CIRCLEQ_REMOVE(head, bp, hq); + CIRCLEQ_INSERT_HEAD(head, bp, hq); + CIRCLEQ_REMOVE(&mp->lqh, bp, q); + CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); + + /* Return a pinned page. */ + bp->flags |= MPOOL_PINNED; + return (bp->page); + } + + /* Get a page from the cache. */ + if ((bp = mpool_bkt(mp)) == NULL) + return (NULL); + + /* Read in the contents. */ +#ifdef STATISTICS + ++mp->pageread; +#endif + off = mp->pagesize * pgno; + if ((nr = pread(mp->fd, bp->page, (size_t)mp->pagesize, off)) != (int)mp->pagesize) { + if (nr >= 0) + errno = EFTYPE; + return (NULL); + } + + /* Set the page number, pin the page. */ + bp->pgno = pgno; + bp->flags = MPOOL_PINNED; + + /* + * Add the page to the head of the hash chain and the tail + * of the lru chain. + */ + head = &mp->hqh[HASHKEY(bp->pgno)]; + CIRCLEQ_INSERT_HEAD(head, bp, hq); + CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q); + + /* Run through the user's filter. */ + if (mp->pgin != NULL) + (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); + + return (bp->page); +} + +/* + * mpool_put + * Return a page. + */ +/*ARGSUSED*/ +int +mpool_put(MPOOL *mp, void *page, u_int flags) +{ + BKT *bp; + +#ifdef STATISTICS + ++mp->pageput; +#endif + bp = (BKT *)(void *)((char *)page - sizeof(BKT)); +#ifdef DEBUG + if (!(bp->flags & MPOOL_PINNED)) { + (void)fprintf(stderr, + "mpool_put: page %d not pinned\n", bp->pgno); + abort(); + } +#endif + bp->flags &= ~MPOOL_PINNED; + bp->flags |= flags & MPOOL_DIRTY; + return (RET_SUCCESS); +} + +/* + * mpool_close + * Close the buffer pool. + */ +int +mpool_close(MPOOL *mp) +{ + BKT *bp; + + /* Free up any space allocated to the lru pages. */ + while ((bp = mp->lqh.cqh_first) != (void *)&mp->lqh) { + CIRCLEQ_REMOVE(&mp->lqh, mp->lqh.cqh_first, q); + free(bp); + } + + /* Free the MPOOL cookie. */ + free(mp); + return (RET_SUCCESS); +} + +/* + * mpool_sync + * Sync the pool to disk. + */ +int +mpool_sync(MPOOL *mp) +{ + BKT *bp; + + /* Walk the lru chain, flushing any dirty pages to disk. */ + for (bp = mp->lqh.cqh_first; + bp != (void *)&mp->lqh; bp = bp->q.cqe_next) + if (bp->flags & MPOOL_DIRTY && + mpool_write(mp, bp) == RET_ERROR) + return (RET_ERROR); + + /* Sync the file descriptor. */ + return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS); +} + +/* + * mpool_bkt + * Get a page from the cache (or create one). + */ +static BKT * +mpool_bkt(MPOOL *mp) +{ + struct _hqh *head; + BKT *bp; + + /* If under the max cached, always create a new page. */ + if (mp->curcache < mp->maxcache) + goto new; + + /* + * If the cache is max'd out, walk the lru list for a buffer we + * can flush. If we find one, write it (if necessary) and take it + * off any lists. If we don't find anything we grow the cache anyway. + * The cache never shrinks. + */ + for (bp = mp->lqh.cqh_first; + bp != (void *)&mp->lqh; bp = bp->q.cqe_next) + if (!(bp->flags & MPOOL_PINNED)) { + /* Flush if dirty. */ + if (bp->flags & MPOOL_DIRTY && + mpool_write(mp, bp) == RET_ERROR) + return (NULL); +#ifdef STATISTICS + ++mp->pageflush; +#endif + /* Remove from the hash and lru queues. */ + head = &mp->hqh[HASHKEY(bp->pgno)]; + CIRCLEQ_REMOVE(head, bp, hq); + CIRCLEQ_REMOVE(&mp->lqh, bp, q); +#ifdef DEBUG + { + void *spage = bp->page; + (void)memset(bp, 0xff, + (size_t)(sizeof(BKT) + mp->pagesize)); + bp->page = spage; + } +#endif + return (bp); + } + +new: if ((bp = calloc(1, (size_t)(sizeof(BKT) + mp->pagesize))) == NULL) + return (NULL); +#ifdef STATISTICS + ++mp->pagealloc; +#endif +#if defined(DEBUG) || defined(PURIFY) + (void)memset(bp, 0xff, (size_t)(sizeof(BKT) + mp->pagesize)); +#endif + bp->page = (char *)(void *)bp + sizeof(BKT); + ++mp->curcache; + return (bp); +} + +/* + * mpool_write + * Write a page to disk. + */ +static int +mpool_write(MPOOL *mp, BKT *bp) +{ + off_t off; + +#ifdef STATISTICS + ++mp->pagewrite; +#endif + + /* Run through the user's filter. */ + if (mp->pgout) + (mp->pgout)(mp->pgcookie, bp->pgno, bp->page); + + off = mp->pagesize * bp->pgno; + if (pwrite(mp->fd, bp->page, (size_t)mp->pagesize, off) != (int)mp->pagesize) + return (RET_ERROR); + + /* + * Re-run through the input filter since this page may soon be + * accessed via the cache, and whatever the user's output filter + * did may screw things up if we don't let the input filter + * restore the in-core copy. + */ + if (mp->pgin) + (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); + + bp->flags &= ~MPOOL_DIRTY; + return (RET_SUCCESS); +} + +/* + * mpool_look + * Lookup a page in the cache. + */ +static BKT * +mpool_look(MPOOL *mp, pgno_t pgno) +{ + struct _hqh *head; + BKT *bp; + + head = &mp->hqh[HASHKEY(pgno)]; + for (bp = head->cqh_first; bp != (void *)head; bp = bp->hq.cqe_next) + if (bp->pgno == pgno) { +#ifdef STATISTICS + ++mp->cachehit; +#endif + return (bp); + } +#ifdef STATISTICS + ++mp->cachemiss; +#endif + return (NULL); +} + +#ifdef STATISTICS +/* + * mpool_stat + * Print out cache statistics. + */ +void +mpool_stat(mp) + MPOOL *mp; +{ + BKT *bp; + int cnt; + const char *sep; + + (void)fprintf(stderr, "%lu pages in the file\n", (u_long)mp->npages); + (void)fprintf(stderr, + "page size %lu, cacheing %lu pages of %lu page max cache\n", + (u_long)mp->pagesize, (u_long)mp->curcache, (u_long)mp->maxcache); + (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n", + mp->pageput, mp->pageget, mp->pagenew); + (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n", + mp->pagealloc, mp->pageflush); + if (mp->cachehit + mp->cachemiss) + (void)fprintf(stderr, + "%.0f%% cache hit rate (%lu hits, %lu misses)\n", + ((double)mp->cachehit / (mp->cachehit + mp->cachemiss)) + * 100, mp->cachehit, mp->cachemiss); + (void)fprintf(stderr, "%lu page reads, %lu page writes\n", + mp->pageread, mp->pagewrite); + + sep = ""; + cnt = 0; + for (bp = mp->lqh.cqh_first; + bp != (void *)&mp->lqh; bp = bp->q.cqe_next) { + (void)fprintf(stderr, "%s%d", sep, bp->pgno); + if (bp->flags & MPOOL_DIRTY) + (void)fprintf(stderr, "d"); + if (bp->flags & MPOOL_PINNED) + (void)fprintf(stderr, "P"); + if (++cnt == 10) { + sep = "\n"; + cnt = 0; + } else + sep = ", "; + + } + (void)fprintf(stderr, "\n"); +} +#endif diff --git a/lib/nbsd_libc/db/recno/Makefile.inc b/lib/nbsd_libc/db/recno/Makefile.inc new file mode 100644 index 000000000..e7f78f4ce --- /dev/null +++ b/lib/nbsd_libc/db/recno/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.5 1996/05/03 21:38:43 cgd Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +.PATH: ${.CURDIR}/db/recno + +SRCS+= rec_close.c rec_delete.c rec_get.c rec_open.c rec_put.c rec_search.c \ + rec_seq.c rec_utils.c diff --git a/lib/nbsd_libc/db/recno/extern.h b/lib/nbsd_libc/db/recno/extern.h new file mode 100644 index 000000000..f73c83de3 --- /dev/null +++ b/lib/nbsd_libc/db/recno/extern.h @@ -0,0 +1,52 @@ +/* $NetBSD: extern.h,v 1.8 2008/08/26 21:18:38 joerg Exp $ */ + +/*- + * Copyright (c) 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. + * + * @(#)extern.h 8.3 (Berkeley) 6/4/94 + */ + +#include "../btree/extern.h" + +int __rec_close(DB *); +int __rec_delete(const DB *, const DBT *, u_int); +int __rec_dleaf(BTREE *, PAGE *, uint32_t); +int __rec_fd(const DB *); +int __rec_fmap(BTREE *, recno_t); +int __rec_fout(BTREE *); +int __rec_fpipe(BTREE *, recno_t); +int __rec_get(const DB *, const DBT *, DBT *, u_int); +int __rec_iput(BTREE *, recno_t, const DBT *, u_int); +int __rec_put(const DB *dbp, DBT *, const DBT *, u_int); +int __rec_ret(BTREE *, EPG *, recno_t, DBT *, DBT *); +EPG *__rec_search(BTREE *, recno_t, enum SRCHOP); +int __rec_seq(const DB *, DBT *, DBT *, u_int); +int __rec_sync(const DB *, u_int); +int __rec_vmap(BTREE *, recno_t); +int __rec_vout(BTREE *); +int __rec_vpipe(BTREE *, recno_t); diff --git a/lib/nbsd_libc/db/recno/rec_close.c b/lib/nbsd_libc/db/recno/rec_close.c new file mode 100644 index 000000000..aa3104416 --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_close.c @@ -0,0 +1,186 @@ +/* $NetBSD: rec_close.c,v 1.15 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_close.c,v 1.15 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include "recno.h" + +/* + * __REC_CLOSE -- Close a recno tree. + * + * Parameters: + * dbp: pointer to access method + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__rec_close(DB *dbp) +{ + BTREE *t; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + if (__rec_sync(dbp, 0) == RET_ERROR) + return (RET_ERROR); + + /* Committed to closing. */ + status = RET_SUCCESS; + if (F_ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize)) + status = RET_ERROR; + + if (!F_ISSET(t, R_INMEM)) { + if (F_ISSET(t, R_CLOSEFP)) { + if (fclose(t->bt_rfp)) + status = RET_ERROR; + } else { + if (close(t->bt_rfd)) + status = RET_ERROR; + } + } + + if (__bt_close(dbp) == RET_ERROR) + status = RET_ERROR; + + return (status); +} + +/* + * __REC_SYNC -- sync the recno tree to disk. + * + * Parameters: + * dbp: pointer to access method + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +int +__rec_sync(const DB *dbp, u_int flags) +{ + struct iovec iov[2]; + BTREE *t; + DBT data, key; + off_t off; + recno_t scursor, trec; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + if (flags == R_RECNOSYNC) + return (__bt_sync(dbp, 0)); + + if (F_ISSET(t, R_RDONLY | R_INMEM) || !F_ISSET(t, R_MODIFIED)) + return (RET_SUCCESS); + + /* Read any remaining records into the tree. */ + if (!F_ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) + return (RET_ERROR); + + /* Rewind the file descriptor. */ + if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0) + return (RET_ERROR); + + /* Save the cursor. */ + scursor = t->bt_cursor.rcursor; + + key.size = sizeof(recno_t); + key.data = &trec; + + if (F_ISSET(t, R_FIXLEN)) { + /* + * We assume that fixed length records are all fixed length. + * Any that aren't are either EINVAL'd or corrected by the + * record put code. + */ + status = (dbp->seq)(dbp, &key, &data, R_FIRST); + while (status == RET_SUCCESS) { + if (write(t->bt_rfd, data.data, data.size) != + (ssize_t) data.size) + return (RET_ERROR); + status = (dbp->seq)(dbp, &key, &data, R_NEXT); + } + } else { + iov[1].iov_base = &t->bt_bval; + iov[1].iov_len = 1; + + status = (dbp->seq)(dbp, &key, &data, R_FIRST); + while (status == RET_SUCCESS) { + iov[0].iov_base = data.data; + iov[0].iov_len = data.size; + if (writev(t->bt_rfd, iov, 2) != + (ssize_t) (data.size + 1)) + return (RET_ERROR); + status = (dbp->seq)(dbp, &key, &data, R_NEXT); + } + } + + /* Restore the cursor. */ + t->bt_cursor.rcursor = scursor; + + if (status == RET_ERROR) + return (RET_ERROR); + if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1) + return (RET_ERROR); + if (ftruncate(t->bt_rfd, off)) + return (RET_ERROR); + F_CLR(t, R_MODIFIED); + return (RET_SUCCESS); +} diff --git a/lib/nbsd_libc/db/recno/rec_delete.c b/lib/nbsd_libc/db/recno/rec_delete.c new file mode 100644 index 000000000..a9dea5767 --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_delete.c @@ -0,0 +1,197 @@ +/* $NetBSD: rec_delete.c,v 1.17 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_delete.c,v 1.17 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#include +#include "recno.h" + +static int rec_rdelete(BTREE *, recno_t); + +/* + * __REC_DELETE -- Delete the item(s) referenced by a key. + * + * Parameters: + * dbp: pointer to access method + * key: key to delete + * flags: R_CURSOR if deleting what the cursor references + * + * Returns: + * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. + */ +int +__rec_delete(const DB *dbp, const DBT *key, u_int flags) +{ + BTREE *t; + recno_t nrec; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + switch(flags) { + case 0: + if ((nrec = *(recno_t *)key->data) == 0) + goto einval; + if (nrec > t->bt_nrecs) + return (RET_SPECIAL); + --nrec; + status = rec_rdelete(t, nrec); + break; + case R_CURSOR: + if (!F_ISSET(&t->bt_cursor, CURS_INIT)) + goto einval; + if (t->bt_nrecs == 0) + return (RET_SPECIAL); + status = rec_rdelete(t, t->bt_cursor.rcursor - 1); + if (status == RET_SUCCESS) + --t->bt_cursor.rcursor; + break; + default: +einval: errno = EINVAL; + return (RET_ERROR); + } + + if (status == RET_SUCCESS) + F_SET(t, B_MODIFIED | R_MODIFIED); + return (status); +} + +/* + * REC_RDELETE -- Delete the data matching the specified key. + * + * Parameters: + * tree: tree + * nrec: record to delete + * + * Returns: + * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. + */ +static int +rec_rdelete(BTREE *t, recno_t nrec) +{ + EPG *e; + PAGE *h; + int status; + + /* Find the record; __rec_search pins the page. */ + if ((e = __rec_search(t, nrec, SDELETE)) == NULL) + return (RET_ERROR); + + /* Delete the record. */ + h = e->page; + status = __rec_dleaf(t, h, (uint32_t)e->index); + if (status != RET_SUCCESS) { + mpool_put(t->bt_mp, h, 0); + return (status); + } + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + return (RET_SUCCESS); +} + +/* + * __REC_DLEAF -- Delete a single record from a recno leaf page. + * + * Parameters: + * t: tree + * index: index on current page to delete + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +int +__rec_dleaf(BTREE *t, PAGE *h, uint32_t idx) +{ + RLEAF *rl; + indx_t *ip, cnt, offset; + uint32_t nbytes; + char *from; + void *to; + size_t temp; + + /* + * Delete a record from a recno leaf page. Internal records are never + * deleted from internal pages, regardless of the records that caused + * them to be added being deleted. Pages made empty by deletion are + * not reclaimed. They are, however, made available for reuse. + * + * Pack the remaining entries at the end of the page, shift the indices + * down, overwriting the deleted record and its index. If the record + * uses overflow pages, make them available for reuse. + */ + to = rl = GETRLEAF(h, idx); + if (rl->flags & P_BIGDATA && __ovfl_delete(t, rl->bytes) == RET_ERROR) + return (RET_ERROR); + nbytes = NRLEAF(rl); + + /* + * Compress the key/data pairs. Compress and adjust the [BR]LEAF + * offsets. Reset the headers. + */ + from = (char *)(void *)h + h->upper; + memmove(from + nbytes, from, (size_t)((char *)to - from)); + h->upper += nbytes; + + offset = h->linp[idx]; + temp = &h->linp[idx] - (ip = &h->linp[0]); + _DBFIT(temp, uint16_t); + for (cnt = (uint16_t)temp; cnt--; ++ip) + if (ip[0] < offset) + ip[0] += nbytes; + temp = &h->linp[NEXTINDEX(h)] - ip; + _DBFIT(temp, uint16_t); + for (cnt = (uint16_t)temp; --cnt; ++ip) + ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1]; + h->lower -= sizeof(indx_t); + --t->bt_nrecs; + return (RET_SUCCESS); +} diff --git a/lib/nbsd_libc/db/recno/rec_get.c b/lib/nbsd_libc/db/recno/rec_get.c new file mode 100644 index 000000000..1dc80f928 --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_get.c @@ -0,0 +1,302 @@ +/* $NetBSD: rec_get.c,v 1.16 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_get.c,v 1.16 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "recno.h" + +/* + * __REC_GET -- Get a record from the btree. + * + * Parameters: + * dbp: pointer to access method + * key: key to find + * data: data to return + * flag: currently unused + * + * Returns: + * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. + */ +int +__rec_get(const DB *dbp, const DBT *key, DBT *data, u_int flags) +{ + BTREE *t; + EPG *e; + recno_t nrec; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* Get currently doesn't take any flags, and keys of 0 are illegal. */ + if (flags || (nrec = *(recno_t *)key->data) == 0) { + errno = EINVAL; + return (RET_ERROR); + } + + /* + * If we haven't seen this record yet, try to find it in the + * original file. + */ + if (nrec > t->bt_nrecs) { + if (F_ISSET(t, R_EOF | R_INMEM)) + return (RET_SPECIAL); + if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS) + return (status); + } + + --nrec; + if ((e = __rec_search(t, nrec, SEARCH)) == NULL) + return (RET_ERROR); + + status = __rec_ret(t, e, 0, NULL, data); + if (F_ISSET(t, B_DB_LOCK)) + mpool_put(t->bt_mp, e->page, 0); + else + t->bt_pinned = e->page; + return (status); +} + +/* + * __REC_FPIPE -- Get fixed length records from a pipe. + * + * Parameters: + * t: tree + * cnt: records to read + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__rec_fpipe(BTREE *t, recno_t top) +{ + DBT data; + recno_t nrec; + size_t len; + int ch; + uint8_t *p; + + if (t->bt_rdata.size < t->bt_reclen) { + t->bt_rdata.data = t->bt_rdata.data == NULL ? + malloc(t->bt_reclen) : + realloc(t->bt_rdata.data, t->bt_reclen); + if (t->bt_rdata.data == NULL) + return (RET_ERROR); + t->bt_rdata.size = t->bt_reclen; + } + data.data = t->bt_rdata.data; + data.size = t->bt_reclen; + + for (nrec = t->bt_nrecs; nrec < top;) { + len = t->bt_reclen; + for (p = t->bt_rdata.data;; *p++ = ch) + if ((ch = getc(t->bt_rfp)) == EOF || !--len) { + if (ch != EOF) + *p = ch; + if (len != 0) + memset(p, t->bt_bval, len); + if (__rec_iput(t, + nrec, &data, 0) != RET_SUCCESS) + return (RET_ERROR); + ++nrec; + break; + } + if (ch == EOF) + break; + } + if (nrec < top) { + F_SET(t, R_EOF); + return (RET_SPECIAL); + } + return (RET_SUCCESS); +} + +/* + * __REC_VPIPE -- Get variable length records from a pipe. + * + * Parameters: + * t: tree + * cnt: records to read + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__rec_vpipe(BTREE *t, recno_t top) +{ + DBT data; + recno_t nrec; + ptrdiff_t len; + size_t sz; + int bval, ch; + uint8_t *p; + + bval = t->bt_bval; + for (nrec = t->bt_nrecs; nrec < top; ++nrec) { + for (p = t->bt_rdata.data, + sz = t->bt_rdata.size;; *p++ = ch, --sz) { + if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) { + data.data = t->bt_rdata.data; + data.size = p - (uint8_t *)t->bt_rdata.data; + if (ch == EOF && data.size == 0) + break; + if (__rec_iput(t, nrec, &data, 0) + != RET_SUCCESS) + return (RET_ERROR); + break; + } + if (sz == 0) { + len = p - (uint8_t *)t->bt_rdata.data; + t->bt_rdata.size += (sz = 256); + t->bt_rdata.data = t->bt_rdata.data == NULL ? + malloc(t->bt_rdata.size) : + realloc(t->bt_rdata.data, t->bt_rdata.size); + if (t->bt_rdata.data == NULL) + return (RET_ERROR); + p = (uint8_t *)t->bt_rdata.data + len; + } + } + if (ch == EOF) + break; + } + if (nrec < top) { + F_SET(t, R_EOF); + return (RET_SPECIAL); + } + return (RET_SUCCESS); +} + +/* + * __REC_FMAP -- Get fixed length records from a file. + * + * Parameters: + * t: tree + * cnt: records to read + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__rec_fmap(BTREE *t, recno_t top) +{ + DBT data; + recno_t nrec; + uint8_t *sp, *ep, *p; + size_t len; + + if (t->bt_rdata.size < t->bt_reclen) { + t->bt_rdata.data = t->bt_rdata.data == NULL ? + malloc(t->bt_reclen) : + realloc(t->bt_rdata.data, t->bt_reclen); + if (t->bt_rdata.data == NULL) + return (RET_ERROR); + t->bt_rdata.size = t->bt_reclen; + } + data.data = t->bt_rdata.data; + data.size = t->bt_reclen; + + sp = (uint8_t *)t->bt_cmap; + ep = (uint8_t *)t->bt_emap; + for (nrec = t->bt_nrecs; nrec < top; ++nrec) { + if (sp >= ep) { + F_SET(t, R_EOF); + return (RET_SPECIAL); + } + len = t->bt_reclen; + for (p = t->bt_rdata.data; + sp < ep && len > 0; *p++ = *sp++, --len); + if (len != 0) + memset(p, t->bt_bval, len); + if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) + return (RET_ERROR); + } + t->bt_cmap = (caddr_t)sp; + return (RET_SUCCESS); +} + +/* + * __REC_VMAP -- Get variable length records from a file. + * + * Parameters: + * t: tree + * cnt: records to read + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__rec_vmap(BTREE *t, recno_t top) +{ + DBT data; + uint8_t *sp, *ep; + recno_t nrec; + int bval; + + sp = (uint8_t *)t->bt_cmap; + ep = (uint8_t *)t->bt_emap; + bval = t->bt_bval; + + for (nrec = t->bt_nrecs; nrec < top; ++nrec) { + if (sp >= ep) { + F_SET(t, R_EOF); + return (RET_SPECIAL); + } + for (data.data = sp; sp < ep && *sp != bval; ++sp); + data.size = sp - (uint8_t *)data.data; + if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) + return (RET_ERROR); + ++sp; + } + t->bt_cmap = (caddr_t)sp; + return (RET_SUCCESS); +} diff --git a/lib/nbsd_libc/db/recno/rec_open.c b/lib/nbsd_libc/db/recno/rec_open.c new file mode 100644 index 000000000..ff1852e40 --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_open.c @@ -0,0 +1,246 @@ +/* $NetBSD: rec_open.c,v 1.17 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Olson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_open.c,v 1.17 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "recno.h" + +DB * +__rec_open(const char *fname, int flags, mode_t mode, const RECNOINFO *openinfo, + int dflags) +{ + BTREE *t; + BTREEINFO btopeninfo; + DB *dbp; + PAGE *h; + struct stat sb; + int rfd = -1; /* pacify gcc */ + int sverrno; + + dbp = NULL; + /* Open the user's file -- if this fails, we're done. */ + if (fname != NULL) { + if ((rfd = open(fname, flags, mode)) == -1) + return (NULL); + if (fcntl(rfd, F_SETFD, FD_CLOEXEC) == -1) + goto err; + } + + /* Create a btree in memory (backed by disk). */ + if (openinfo) { + if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT)) + goto einval; + btopeninfo.flags = 0; + btopeninfo.cachesize = openinfo->cachesize; + btopeninfo.maxkeypage = 0; + btopeninfo.minkeypage = 0; + btopeninfo.psize = openinfo->psize; + btopeninfo.compare = NULL; + btopeninfo.prefix = NULL; + btopeninfo.lorder = openinfo->lorder; + dbp = __bt_open(openinfo->bfname, + O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags); + } else + dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags); + if (dbp == NULL) + goto err; + + /* + * Some fields in the tree structure are recno specific. Fill them + * in and make the btree structure look like a recno structure. We + * don't change the bt_ovflsize value, it's close enough and slightly + * bigger. + */ + t = dbp->internal; + if (openinfo) { + if (openinfo->flags & R_FIXEDLEN) { + F_SET(t, R_FIXLEN); + t->bt_reclen = openinfo->reclen; + if (t->bt_reclen == 0) + goto einval; + } + t->bt_bval = openinfo->bval; + } else + t->bt_bval = '\n'; + + F_SET(t, R_RECNO); + if (fname == NULL) + F_SET(t, R_EOF | R_INMEM); + else + t->bt_rfd = rfd; + + if (fname != NULL) { + /* + * In 4.4BSD, stat(2) returns true for ISSOCK on pipes. + * Unfortunately, that's not portable, so we use lseek + * and check the errno values. + */ + errno = 0; + if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { + switch (flags & O_ACCMODE) { + case O_RDONLY: + F_SET(t, R_RDONLY); + break; + default: + goto einval; + } +slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) + goto err; + F_SET(t, R_CLOSEFP); + t->bt_irec = + F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe; + } else { + switch (flags & O_ACCMODE) { + case O_RDONLY: + F_SET(t, R_RDONLY); + break; + case O_RDWR: + break; + default: + goto einval; + } + + if (fstat(rfd, &sb)) + goto err; + /* + * Kluge -- we'd like to test to see if the file is too + * big to mmap. Since, we don't know what size or type + * off_t's or size_t's are, what the largest unsigned + * integral type is, or what random insanity the local + * C compiler will perpetrate, doing the comparison in + * a portable way is flatly impossible. Hope that mmap + * fails if the file is too large. + */ + if (sb.st_size == 0) + F_SET(t, R_EOF); + else { +#ifdef MMAP_NOT_AVAILABLE + /* + * XXX + * Mmap doesn't work correctly on many current + * systems. In particular, it can fail subtly, + * with cache coherency problems. Don't use it + * for now. + */ + t->bt_msize = sb.st_size; + if ((t->bt_smap = mmap(NULL, t->bt_msize, + PROT_READ, MAP_FILE | MAP_PRIVATE, rfd, + (off_t)0)) == (caddr_t)-1) + goto slow; + t->bt_cmap = t->bt_smap; + t->bt_emap = t->bt_smap + sb.st_size; + t->bt_irec = F_ISSET(t, R_FIXLEN) ? + __rec_fmap : __rec_vmap; + F_SET(t, R_MEMMAPPED); +#else + goto slow; +#endif + } + } + } + + /* Use the recno routines. */ + dbp->close = __rec_close; + dbp->del = __rec_delete; + dbp->fd = __rec_fd; + dbp->get = __rec_get; + dbp->put = __rec_put; + dbp->seq = __rec_seq; + dbp->sync = __rec_sync; + + /* If the root page was created, reset the flags. */ + if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) + goto err; + if ((h->flags & P_TYPE) == P_BLEAF) { + F_CLR(h, P_TYPE); + F_SET(h, P_RLEAF); + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + } else + mpool_put(t->bt_mp, h, 0); + + if (openinfo && openinfo->flags & R_SNAPSHOT && + !F_ISSET(t, R_EOF | R_INMEM) && + t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) + goto err; + return (dbp); + +einval: errno = EINVAL; +err: sverrno = errno; + if (dbp != NULL) + (void)__bt_close(dbp); + if (fname != NULL) + (void)close(rfd); + errno = sverrno; + return (NULL); +} + +int +__rec_fd(const DB *dbp) +{ + BTREE *t; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* In-memory database can't have a file descriptor. */ + if (F_ISSET(t, R_INMEM)) { + errno = ENOENT; + return (-1); + } + return (t->bt_rfd); +} diff --git a/lib/nbsd_libc/db/recno/rec_put.c b/lib/nbsd_libc/db/recno/rec_put.c new file mode 100644 index 000000000..7c1181c64 --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_put.c @@ -0,0 +1,278 @@ +/* $NetBSD: rec_put.c,v 1.17 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_put.c,v 1.17 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include + +#include +#include "recno.h" + +/* + * __REC_PUT -- Add a recno item to the tree. + * + * Parameters: + * dbp: pointer to access method + * key: key + * data: data + * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE + * + * Returns: + * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is + * already in the tree and R_NOOVERWRITE specified. + */ +int +__rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags) +{ + BTREE *t; + DBT fdata, tdata; + recno_t nrec; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + /* + * If using fixed-length records, and the record is long, return + * EINVAL. If it's short, pad it out. Use the record data return + * memory, it's only short-term. + */ + if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) { + if (data->size > t->bt_reclen) + goto einval; + + if (t->bt_rdata.size < t->bt_reclen) { + t->bt_rdata.data = t->bt_rdata.data == NULL ? + malloc(t->bt_reclen) : + realloc(t->bt_rdata.data, t->bt_reclen); + if (t->bt_rdata.data == NULL) + return (RET_ERROR); + t->bt_rdata.size = t->bt_reclen; + } + memmove(t->bt_rdata.data, data->data, data->size); + memset((char *)t->bt_rdata.data + data->size, + t->bt_bval, t->bt_reclen - data->size); + fdata.data = t->bt_rdata.data; + fdata.size = t->bt_reclen; + } else { + fdata.data = data->data; + fdata.size = data->size; + } + + switch (flags) { + case R_CURSOR: + if (!F_ISSET(&t->bt_cursor, CURS_INIT)) + goto einval; + nrec = t->bt_cursor.rcursor; + break; + case R_SETCURSOR: + if ((nrec = *(recno_t *)key->data) == 0) + goto einval; + break; + case R_IAFTER: + if ((nrec = *(recno_t *)key->data) == 0) { + nrec = 1; + flags = R_IBEFORE; + } + break; + case 0: + case R_IBEFORE: + if ((nrec = *(recno_t *)key->data) == 0) + goto einval; + break; + case R_NOOVERWRITE: + if ((nrec = *(recno_t *)key->data) == 0) + goto einval; + if (nrec <= t->bt_nrecs) + return (RET_SPECIAL); + break; + default: +einval: errno = EINVAL; + return (RET_ERROR); + } + + /* + * Make sure that records up to and including the put record are + * already in the database. If skipping records, create empty ones. + */ + if (nrec > t->bt_nrecs) { + if (!F_ISSET(t, R_EOF | R_INMEM) && + t->bt_irec(t, nrec) == RET_ERROR) + return (RET_ERROR); + if (nrec > t->bt_nrecs + 1) { + if (F_ISSET(t, R_FIXLEN)) { + if ((tdata.data = + (void *)malloc(t->bt_reclen)) == NULL) + return (RET_ERROR); + tdata.size = t->bt_reclen; + memset(tdata.data, t->bt_bval, tdata.size); + } else { + tdata.data = NULL; + tdata.size = 0; + } + while (nrec > t->bt_nrecs + 1) + if (__rec_iput(t, + t->bt_nrecs, &tdata, 0) != RET_SUCCESS) + return (RET_ERROR); + if (F_ISSET(t, R_FIXLEN)) + free(tdata.data); + } + } + + if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS) + return (status); + + if (flags == R_SETCURSOR) + t->bt_cursor.rcursor = nrec; + + F_SET(t, R_MODIFIED); + return (__rec_ret(t, NULL, nrec, key, NULL)); +} + +/* + * __REC_IPUT -- Add a recno item to the tree. + * + * Parameters: + * t: tree + * nrec: record number + * data: data + * + * Returns: + * RET_ERROR, RET_SUCCESS + */ +int +__rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags) +{ + DBT tdata; + EPG *e; + PAGE *h; + indx_t idx, nxtindex; + pgno_t pg; + uint32_t nbytes; + int dflags, status; + char *dest, db[NOVFLSIZE]; + + /* + * If the data won't fit on a page, store it on indirect pages. + * + * XXX + * If the insert fails later on, these pages aren't recovered. + */ + if (data->size > t->bt_ovflsize) { + if (__ovfl_put(t, data, &pg) == RET_ERROR) + return (RET_ERROR); + tdata.data = db; + tdata.size = NOVFLSIZE; + *(pgno_t *)(void *)db = pg; + _DBFIT(data->size, uint32_t); + *(uint32_t *)(void *)(db + sizeof(pgno_t)) = + (uint32_t)data->size; + dflags = P_BIGDATA; + data = &tdata; + } else + dflags = 0; + + /* __rec_search pins the returned page. */ + if ((e = __rec_search(t, nrec, + nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ? + SINSERT : SEARCH)) == NULL) + return (RET_ERROR); + + h = e->page; + idx = e->index; + + /* + * Add the specified key/data pair to the tree. The R_IAFTER and + * R_IBEFORE flags insert the key after/before the specified key. + * + * Pages are split as required. + */ + switch (flags) { + case R_IAFTER: + ++idx; + break; + case R_IBEFORE: + break; + default: + if (nrec < t->bt_nrecs && + __rec_dleaf(t, h, (uint32_t)idx) == RET_ERROR) { + mpool_put(t->bt_mp, h, 0); + return (RET_ERROR); + } + break; + } + + /* + * If not enough room, split the page. The split code will insert + * the key and data and unpin the current page. If inserting into + * the offset array, shift the pointers up. + */ + nbytes = NRLEAFDBT(data->size); + if ((uint32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) { + status = __bt_split(t, h, NULL, data, dflags, nbytes, + (uint32_t)idx); + if (status == RET_SUCCESS) + ++t->bt_nrecs; + return (status); + } + + if (idx < (nxtindex = NEXTINDEX(h))) + memmove(h->linp + idx + 1, h->linp + idx, + (nxtindex - idx) * sizeof(indx_t)); + h->lower += sizeof(indx_t); + + h->linp[idx] = h->upper -= nbytes; + dest = (char *)(void *)h + h->upper; + WR_RLEAF(dest, data, dflags); + + ++t->bt_nrecs; + F_SET(t, B_MODIFIED); + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + + return (RET_SUCCESS); +} diff --git a/lib/nbsd_libc/db/recno/rec_search.c b/lib/nbsd_libc/db/recno/rec_search.c new file mode 100644 index 000000000..1238c8db3 --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_search.c @@ -0,0 +1,126 @@ +/* $NetBSD: rec_search.c,v 1.14 2008/09/11 12:58:00 joerg 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_search.c,v 1.14 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include + +#include +#include "recno.h" + +/* + * __REC_SEARCH -- Search a btree for a key. + * + * Parameters: + * t: tree to search + * recno: key to find + * op: search operation + * + * Returns: + * EPG for matching record, if any, or the EPG for the location of the + * key, if it were inserted into the tree. + * + * Returns: + * The EPG for matching record, if any, or the EPG for the location + * of the key, if it were inserted into the tree, is entered into + * the bt_cur field of the tree. A pointer to the field is returned. + */ +EPG * +__rec_search(BTREE *t, recno_t recno, enum SRCHOP op) +{ + indx_t idx; + PAGE *h; + EPGNO *parent; + RINTERNAL *r; + pgno_t pg; + indx_t top; + recno_t total; + int sverrno; + + BT_CLR(t); + for (pg = P_ROOT, total = 0;;) { + if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) + goto err; + if (h->flags & P_RLEAF) { + t->bt_cur.page = h; + t->bt_cur.index = recno - total; + return (&t->bt_cur); + } + for (idx = 0, top = NEXTINDEX(h);;) { + r = GETRINTERNAL(h, idx); + if (++idx == top || total + r->nrecs > recno) + break; + total += r->nrecs; + } + + BT_PUSH(t, pg, idx - 1); + + pg = r->pgno; + switch (op) { + case SDELETE: + --GETRINTERNAL(h, (idx - 1))->nrecs; + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + break; + case SINSERT: + ++GETRINTERNAL(h, (idx - 1))->nrecs; + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + break; + case SEARCH: + mpool_put(t->bt_mp, h, 0); + break; + } + + } + /* Try and recover the tree. */ +err: sverrno = errno; + if (op != SEARCH) + while ((parent = BT_POP(t)) != NULL) { + if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) + break; + if (op == SINSERT) + --GETRINTERNAL(h, parent->index)->nrecs; + else + ++GETRINTERNAL(h, parent->index)->nrecs; + mpool_put(t->bt_mp, h, MPOOL_DIRTY); + } + errno = sverrno; + return (NULL); +} diff --git a/lib/nbsd_libc/db/recno/rec_seq.c b/lib/nbsd_libc/db/recno/rec_seq.c new file mode 100644 index 000000000..3b1da48cc --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_seq.c @@ -0,0 +1,131 @@ +/* $NetBSD: rec_seq.c,v 1.14 2008/09/11 12:58:00 joerg Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_seq.c,v 1.14 2008/09/11 12:58:00 joerg Exp $"); + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include + +#include +#include "recno.h" + +/* + * __REC_SEQ -- Recno sequential scan interface. + * + * Parameters: + * dbp: pointer to access method + * key: key for positioning and return value + * data: data return value + * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV. + * + * Returns: + * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key. + */ +int +__rec_seq(const DB *dbp, DBT *key, DBT *data, u_int flags) +{ + BTREE *t; + EPG *e; + recno_t nrec; + int status; + + t = dbp->internal; + + /* Toss any page pinned across calls. */ + if (t->bt_pinned != NULL) { + mpool_put(t->bt_mp, t->bt_pinned, 0); + t->bt_pinned = NULL; + } + + switch(flags) { + case R_CURSOR: + if ((nrec = *(recno_t *)key->data) == 0) + goto einval; + break; + case R_NEXT: + if (F_ISSET(&t->bt_cursor, CURS_INIT)) { + nrec = t->bt_cursor.rcursor + 1; + break; + } + /* FALLTHROUGH */ + case R_FIRST: + nrec = 1; + break; + case R_PREV: + if (F_ISSET(&t->bt_cursor, CURS_INIT)) { + if ((nrec = t->bt_cursor.rcursor - 1) == 0) + return (RET_SPECIAL); + break; + } + /* FALLTHROUGH */ + case R_LAST: + if (!F_ISSET(t, R_EOF | R_INMEM) && + t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) + return (RET_ERROR); + nrec = t->bt_nrecs; + break; + default: +einval: errno = EINVAL; + return (RET_ERROR); + } + + if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) { + if (!F_ISSET(t, R_EOF | R_INMEM) && + (status = t->bt_irec(t, nrec)) != RET_SUCCESS) + return (status); + if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) + return (RET_SPECIAL); + } + + if ((e = __rec_search(t, nrec - 1, SEARCH)) == NULL) + return (RET_ERROR); + + F_SET(&t->bt_cursor, CURS_INIT); + t->bt_cursor.rcursor = nrec; + + status = __rec_ret(t, e, nrec, key, data); + if (F_ISSET(t, B_DB_LOCK)) + mpool_put(t->bt_mp, e->page, 0); + else + t->bt_pinned = e->page; + return (status); +} diff --git a/lib/nbsd_libc/db/recno/rec_utils.c b/lib/nbsd_libc/db/recno/rec_utils.c new file mode 100644 index 000000000..2a61c7b68 --- /dev/null +++ b/lib/nbsd_libc/db/recno/rec_utils.c @@ -0,0 +1,120 @@ +/* $NetBSD: rec_utils.c,v 1.12 2008/09/10 17:52:36 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +__RCSID("$NetBSD: rec_utils.c,v 1.12 2008/09/10 17:52:36 joerg Exp $"); + +#include + +#include +#include +#include +#include + +#include +#include "recno.h" + +/* + * __rec_ret -- + * Build return data. + * + * Parameters: + * t: tree + * e: key/data pair to be returned + * nrec: record number + * key: user's key structure + * data: user's data structure + * + * Returns: + * RET_SUCCESS, RET_ERROR. + */ +int +__rec_ret(BTREE *t, EPG *e, recno_t nrec, DBT *key, DBT *data) +{ + RLEAF *rl; + void *p; + + if (key == NULL) + goto dataonly; + + /* We have to copy the key, it's not on the page. */ + if (sizeof(recno_t) > t->bt_rkey.size) { + p = (void *)(t->bt_rkey.data == NULL ? + malloc(sizeof(recno_t)) : + realloc(t->bt_rkey.data, sizeof(recno_t))); + if (p == NULL) + return (RET_ERROR); + t->bt_rkey.data = p; + t->bt_rkey.size = sizeof(recno_t); + } + memmove(t->bt_rkey.data, &nrec, sizeof(recno_t)); + key->size = sizeof(recno_t); + key->data = t->bt_rkey.data; + +dataonly: + if (data == NULL) + return (RET_SUCCESS); + + /* + * We must copy big keys/data to make them contigous. Otherwise, + * leave the page pinned and don't copy unless the user specified + * concurrent access. + */ + rl = GETRLEAF(e->page, e->index); + if (rl->flags & P_BIGDATA) { + if (__ovfl_get(t, rl->bytes, + &data->size, &t->bt_rdata.data, &t->bt_rdata.size)) + return (RET_ERROR); + data->data = t->bt_rdata.data; + } else if (F_ISSET(t, B_DB_LOCK)) { + /* Use +1 in case the first record retrieved is 0 length. */ + if (rl->dsize + 1 > t->bt_rdata.size) { + p = (void *)(t->bt_rdata.data == NULL ? + malloc(rl->dsize + 1) : + realloc(t->bt_rdata.data, rl->dsize + 1)); + if (p == NULL) + return (RET_ERROR); + t->bt_rdata.data = p; + t->bt_rdata.size = rl->dsize + 1; + } + memmove(t->bt_rdata.data, rl->bytes, rl->dsize); + data->size = rl->dsize; + data->data = t->bt_rdata.data; + } else { + data->size = rl->dsize; + data->data = rl->bytes; + } + return (RET_SUCCESS); +} diff --git a/lib/nbsd_libc/db/recno/recno.h b/lib/nbsd_libc/db/recno/recno.h new file mode 100644 index 000000000..7ac0d176a --- /dev/null +++ b/lib/nbsd_libc/db/recno/recno.h @@ -0,0 +1,37 @@ +/* $NetBSD: recno.h,v 1.6 2003/08/07 16:42:44 agc Exp $ */ + +/*- + * Copyright (c) 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. + * + * @(#)recno.h 8.1 (Berkeley) 6/4/93 + */ + +enum SRCHOP { SDELETE, SINSERT, SEARCH}; /* Rec_search operation. */ + +#include "../btree/btree.h" +#include "extern.h" diff --git a/lib/nbsd_libc/dlfcn/Makefile.inc b/lib/nbsd_libc/dlfcn/Makefile.inc new file mode 100644 index 000000000..f9ba7f7c9 --- /dev/null +++ b/lib/nbsd_libc/dlfcn/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.3 2010/07/06 05:59:52 mrg Exp $ + +.PATH: ${.CURDIR}/dlfcn + +CPPFLAGS+= -I${NETBSDSRCDIR}/libexec/ld.elf_so +CPPFLAGS+= -I${.CURDIR}/dlfcn +SRCS+= dlfcn_elf.c diff --git a/lib/nbsd_libc/dlfcn/dlfcn_elf.c b/lib/nbsd_libc/dlfcn/dlfcn_elf.c new file mode 100644 index 000000000..1f9bb8daf --- /dev/null +++ b/lib/nbsd_libc/dlfcn/dlfcn_elf.c @@ -0,0 +1,138 @@ +/* $NetBSD: dlfcn_elf.c,v 1.7 2010/10/16 10:27:07 skrll Exp $ */ + +/* + * Copyright (c) 2000 Takuya SHIOZAKI + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: dlfcn_elf.c,v 1.7 2010/10/16 10:27:07 skrll Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#undef dlopen +#undef dlclose +#undef dlsym +#undef dlerror +#undef dladdr +#undef dfinfo + +#define dlopen ___dlopen +#define dlclose ___dlclose +#define dlsym ___dlsym +#define dlerror ___dlerror +#define dladdr ___dladdr +#define dlinfo ___dlinfo +#define dl_iterate_phdr ___dl_iterate_phdr + +#define ELFSIZE ARCH_ELFSIZE +#include "rtld.h" + +#ifdef __weak_alias +__weak_alias(dlopen,___dlopen) +__weak_alias(dlclose,___dlclose) +__weak_alias(dlsym,___dlsym) +__weak_alias(dlerror,___dlerror) +__weak_alias(dladdr,___dladdr) +__weak_alias(dlinfo,___dlinfo) +__weak_alias(dl_iterate_phdr,___dl_iterate_phdr) + +__weak_alias(__dlopen,___dlopen) +__weak_alias(__dlclose,___dlclose) +__weak_alias(__dlsym,___dlsym) +__weak_alias(__dlerror,___dlerror) +__weak_alias(__dladdr,___dladdr) +__weak_alias(__dlinfo,___dlinfo) +__weak_alias(__dl_iterate_phdr,___dl_iterate_phdr) +#endif + +/* + * For ELF, the dynamic linker directly resolves references to its + * services to functions inside the dynamic linker itself. These + * weak-symbol stubs are necessary so that "ld" won't complain about + * undefined symbols. The stubs are executed only when the program is + * linked statically, or when a given service isn't implemented in the + * dynamic linker. They must return an error if called, and they must + * be weak symbols so that the dynamic linker can override them. + */ + +static char dlfcn_error[] = "Service unavailable"; + +/*ARGSUSED*/ +void * +dlopen(const char *name, int mode) +{ + + return NULL; +} + +/*ARGSUSED*/ +int +dlclose(void *fd) +{ + + return -1; +} + +/*ARGSUSED*/ +void * +dlsym(void *handle, const char *name) +{ + + return NULL; +} + +/*ARGSUSED*/ +__aconst char * +dlerror() +{ + + return dlfcn_error; +} + +/*ARGSUSED*/ +int +dladdr(const void *addr, Dl_info *dli) +{ + + return 0; +} + +/*ARGSUSED*/ +int +dlinfo(void *handle, int req, void *v) +{ + + return -1; +} + +/*ARGSUSED*/ +int +dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *), + void *data) +{ + + return 0; +} diff --git a/lib/nbsd_libc/gdtoa/Makefile.inc b/lib/nbsd_libc/gdtoa/Makefile.inc new file mode 100644 index 000000000..7ca1d6b40 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/Makefile.inc @@ -0,0 +1,46 @@ +# $NetBSD: Makefile.inc,v 1.8 2008/02/09 02:37:21 mrg Exp $ + +# gdtoa sources +.PATH: ${.CURDIR}/gdtoa +CPPFLAGS+=-I${.CURDIR}/gdtoa + +CPPFLAGS+=-DNO_FENV_H + +# machine-dependent directory must provide the following: +# arith.h gd_qnan.h +CPPFLAGS+=-I${ARCHDIR}/gdtoa + +# machine-dependent Makefile must define names of modules to use for +# float conversions: +# strtof(): strtof{,_vaxf}.c +# and for long double conversions on platforms with extended-precision +# format, if supported (strtod.c will define an alias otherwise): +# strtold(): strtold_p{x,xL,Q}.c +# helpers: strtop{x,xL,Q}.c +.include "${ARCHDIR}/gdtoa/Makefile.inc" + +# public interfaces +SRCS+= strtod.c + +# private interfaces interfacing to libc +SRCS+= dtoa.c ldtoa.c hdtoa.c gdtoa.c + +# local locking implementation +SRCS+= gdtoa_locks.c + +# private interfaces +SRCS+= dmisc.c \ + gethex.c gmisc.c \ + hd_init.c hexnan.c \ + misc.c \ + smisc.c strtodg.c sum.c \ + ulp.c + +# wrappers +SRCS+= _strtof.c _strtold.c +#SRCS+= _hdtoa.c _ldtoa.c + +# XXX no hexadecimal floating-point string conversion for VAX FP yet +.if ${MACHINE_ARCH} != "vax" +SRCS+= strtord.c +.endif diff --git a/lib/nbsd_libc/gdtoa/README b/lib/nbsd_libc/gdtoa/README new file mode 100644 index 000000000..0c8a8f8a8 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/README @@ -0,0 +1,338 @@ +$NetBSD: README,v 1.1.1.1 2006/01/25 15:18:40 kleink Exp $ + +This directory contains source for a library of binary -> decimal +and decimal -> binary conversion routines, for single-, double-, +and extended-precision IEEE binary floating-point arithmetic, and +other IEEE-like binary floating-point, including "double double", +as in + + T. J. Dekker, "A Floating-Point Technique for Extending the + Available Precision", Numer. Math. 18 (1971), pp. 224-242 + +and + + "Inside Macintosh: PowerPC Numerics", Addison-Wesley, 1994 + +The conversion routines use double-precision floating-point arithmetic +and, where necessary, high precision integer arithmetic. The routines +are generalizations of the strtod and dtoa routines described in + + David M. Gay, "Correctly Rounded Binary-Decimal and + Decimal-Binary Conversions", Numerical Analysis Manuscript + No. 90-10, Bell Labs, Murray Hill, 1990; + http://cm.bell-labs.com/cm/cs/what/ampl/REFS/rounding.ps.gz + +(based in part on papers by Clinger and Steele & White: see the +references in the above paper). + +The present conversion routines should be able to use any of IEEE binary, +VAX, or IBM-mainframe double-precision arithmetic internally, but I (dmg) +have so far only had a chance to test them with IEEE double precision +arithmetic. + +The core conversion routines are strtodg for decimal -> binary conversions +and gdtoa for binary -> decimal conversions. These routines operate +on arrays of unsigned 32-bit integers of type ULong, a signed 32-bit +exponent of type Long, and arithmetic characteristics described in +struct FPI; FPI, Long, and ULong are defined in gdtoa.h. File arith.h +is supposed to provide #defines that cause gdtoa.h to define its +types correctly. File arithchk.c is source for a program that +generates a suitable arith.h on all systems where I've been able to +test it. + +The core conversion routines are meant to be called by helper routines +that know details of the particular binary arithmetic of interest and +convert. The present directory provides helper routines for 5 variants +of IEEE binary floating-point arithmetic, each indicated by one or +two letters: + + f IEEE single precision + d IEEE double precision + x IEEE extended precision, as on Intel 80x87 + and software emulations of Motorola 68xxx chips + that do not pad the way the 68xxx does, but + only store 80 bits + xL IEEE extended precision, as on Motorola 68xxx chips + Q quad precision, as on Sun Sparc chips + dd double double, pairs of IEEE double numbers + whose sum is the desired value + +For decimal -> binary conversions, there are three families of +helper routines: one for round-nearest: + + strtof + strtod + strtodd + strtopd + strtopf + strtopx + strtopxL + strtopQ + +one with rounding direction specified: + + strtorf + strtord + strtordd + strtorx + strtorxL + strtorQ + +and one for computing an interval (at most one bit wide) that contains +the decimal number: + + strtoIf + strtoId + strtoIdd + strtoIx + strtoIxL + strtoIQ + +The latter call strtoIg, which makes one call on strtodg and adjusts +the result to provide the desired interval. On systems where native +arithmetic can easily make one-ulp adjustments on values in the +desired floating-point format, it might be more efficient to use the +native arithmetic. Routine strtodI is a variant of strtoId that +illustrates one way to do this for IEEE binary double-precision +arithmetic -- but whether this is more efficient remains to be seen. + +Functions strtod and strtof have "natural" return types, float and +double -- strtod is specified by the C standard, and strtof appears +in the stdlib.h of some systems, such as (at least some) Linux systems. +The other functions write their results to their final argument(s): +to the final two argument for the strtoI... (interval) functions, +and to the final argument for the others (strtop... and strtor...). +Where possible, these arguments have "natural" return types (double* +or float*), to permit at least some type checking. In reality, they +are viewed as arrays of ULong (or, for the "x" functions, UShort) +values. On systems where long double is the appropriate type, one can +pass long double* final argument(s) to these routines. The int value +that these routines return is the return value from the call they make +on strtodg; see the enum of possible return values in gdtoa.h. + +Source files g_ddfmt.c, misc.c, smisc.c, strtod.c, strtodg.c, and ulp.c +should use true IEEE double arithmetic (not, e.g., double extended), +at least for storing (and viewing the bits of) the variables declared +"double" within them. + +One detail indicated in struct FPI is whether the target binary +arithmetic departs from the IEEE standard by flushing denormalized +numbers to 0. On systems that do this, the helper routines for +conversion to double-double format (when compiled with +Sudden_Underflow #defined) penalize the bottom of the exponent +range so that they return a nonzero result only when the least +significant bit of the less significant member of the pair of +double values returned can be expressed as a normalized double +value. An alternative would be to drop to 53-bit precision near +the bottom of the exponent range. To get correct rounding, this +would (in general) require two calls on strtodg (one specifying +126-bit arithmetic, then, if necessary, one specifying 53-bit +arithmetic). + +By default, the core routine strtodg and strtod set errno to ERANGE +if the result overflows to +Infinity or underflows to 0. Compile +these routines with NO_ERRNO #defined to inhibit errno assignments. + +Routine strtod is based on netlib's "dtoa.c from fp", and +(f = strtod(s,se)) is more efficient for some conversions than, say, +strtord(s,se,1,&f). Parts of strtod require true IEEE double +arithmetic with the default rounding mode (round-to-nearest) and, on +systems with IEEE extended-precision registers, double-precision +(53-bit) rounding precision. If the machine uses (the equivalent of) +Intel 80x87 arithmetic, the call + _control87(PC_53, MCW_PC); +does this with many compilers. Whether this or another call is +appropriate depends on the compiler; for this to work, it may be +necessary to #include "float.h" or another system-dependent header +file. + +Source file strtodnrp.c gives a strtod that does not require 53-bit +rounding precision on systems (such as Intel IA32 systems) that may +suffer double rounding due to use of extended-precision registers. +For some conversions this variant of strtod is less efficient than the +one in strtod.c when the latter is run with 53-bit rounding precision. + +The values that the strto* routines return for NaNs are determined by +gd_qnan.h, which the makefile generates by running the program whose +source is qnan.c. Note that the rules for distinguishing signaling +from quiet NaNs are system-dependent. For cross-compilation, you need +to determine arith.h and gd_qnan.h suitably, e.g., using the +arithmetic of the target machine. + +C99's hexadecimal floating-point constants are recognized by the +strto* routines (but this feature has not yet been heavily tested). +Compiling with NO_HEX_FP #defined disables this feature. + +When compiled with -DINFNAN_CHECK, the strto* routines recognize C99's +NaN and Infinity syntax. Moreover, unless No_Hex_NaN is #defined, the +strto* routines also recognize C99's NaN(...) syntax: they accept +(case insensitively) strings of the form NaN(x), where x is a string +of hexadecimal digits and spaces; if there is only one string of +hexadecimal digits, it is taken for the fraction bits of the resulting +NaN; if there are two or more strings of hexadecimal digits, each +string is assigned to the next available sequence of 32-bit words of +fractions bits (starting with the most significant), right-aligned in +each sequence. + +For binary -> decimal conversions, I've provided just one family +of helper routines: + + g_ffmt + g_dfmt + g_ddfmt + g_xfmt + g_xLfmt + g_Qfmt + +which do a "%g" style conversion either to a specified number of decimal +places (if their ndig argument is positive), or to the shortest +decimal string that rounds to the given binary floating-point value +(if ndig <= 0). They write into a buffer supplied as an argument +and return either a pointer to the end of the string (a null character) +in the buffer, if the buffer was long enough, or 0. Other forms of +conversion are easily done with the help of gdtoa(), such as %e or %f +style and conversions with direction of rounding specified (so that, if +desired, the decimal value is either >= or <= the binary value). + +For an example of more general conversions based on dtoa(), see +netlib's "printf.c from ampl/solvers". + +For double-double -> decimal, g_ddfmt() assumes IEEE-like arithmetic +of precision max(126, #bits(input)) bits, where #bits(input) is the +number of mantissa bits needed to represent the sum of the two double +values in the input. + +The makefile creates a library, gdtoa.a. To use the helper +routines, a program only needs to include gdtoa.h. All the +source files for gdtoa.a include a more extensive gdtoaimp.h; +among other things, gdtoaimp.h has #defines that make "internal" +names end in _D2A. To make a "system" library, one could modify +these #defines to make the names start with __. + +Various comments about possible #defines appear in gdtoaimp.h, +but for most purposes, arith.h should set suitable #defines. + +Systems with preemptive scheduling of multiple threads require some +manual intervention. On such systems, it's necessary to compile +dmisc.c, dtoa.c gdota.c, and misc.c with MULTIPLE_THREADS #defined, +and to provide (or suitably #define) two locks, acquired by +ACQUIRE_DTOA_LOCK(n) and freed by FREE_DTOA_LOCK(n) for n = 0 or 1. +(The second lock, accessed in pow5mult, ensures lazy evaluation of +only one copy of high powers of 5; omitting this lock would introduce +a small probability of wasting memory, but would otherwise be harmless.) +Routines that call dtoa or gdtoa directly must also invoke freedtoa(s) +to free the value s returned by dtoa or gdtoa. It's OK to do so whether +or not MULTIPLE_THREADS is #defined, and the helper g_*fmt routines +listed above all do this indirectly (in gfmt_D2A(), which they all call). + +By default, there is a private pool of memory of length 2000 bytes +for intermediate quantities, and MALLOC (see gdtoaimp.h) is called only +if the private pool does not suffice. 2000 is large enough that MALLOC +is called only under very unusual circumstances (decimal -> binary +conversion of very long strings) for conversions to and from double +precision. For systems with preemptively scheduled multiple threads +or for conversions to extended or quad, it may be appropriate to +#define PRIVATE_MEM nnnn, where nnnn is a suitable value > 2000. +For extended and quad precisions, -DPRIVATE_MEM=20000 is probably +plenty even for many digits at the ends of the exponent range. +Use of the private pool avoids some overhead. + +Directory test provides some test routines. See its README. +I've also tested this stuff (except double double conversions) +with Vern Paxson's testbase program: see + + V. Paxson and W. Kahan, "A Program for Testing IEEE Binary-Decimal + Conversion", manuscript, May 1991, + ftp://ftp.ee.lbl.gov/testbase-report.ps.Z . + +(The same ftp directory has source for testbase.) + +Some system-dependent additions to CFLAGS in the makefile: + + HU-UX: -Aa -Ae + OSF (DEC Unix): -ieee_with_no_inexact + SunOS 4.1x: -DKR_headers -DBad_float_h + +If you want to put this stuff into a shared library and your +operating system requires export lists for shared libraries, +the following would be an appropriate export list: + + dtoa + freedtoa + g_Qfmt + g_ddfmt + g_dfmt + g_ffmt + g_xLfmt + g_xfmt + gdtoa + strtoIQ + strtoId + strtoIdd + strtoIf + strtoIx + strtoIxL + strtod + strtodI + strtodg + strtof + strtopQ + strtopd + strtopdd + strtopf + strtopx + strtopxL + strtorQ + strtord + strtordd + strtorf + strtorx + strtorxL + +When time permits, I (dmg) hope to write in more detail about the +present conversion routines; for now, this README file must suffice. +Meanwhile, if you wish to write helper functions for other kinds of +IEEE-like arithmetic, some explanation of struct FPI and the bits +array may be helpful. Both gdtoa and strtodg operate on a bits array +described by FPI *fpi. The bits array is of type ULong, a 32-bit +unsigned integer type. Floating-point numbers have fpi->nbits bits, +with the least significant 32 bits in bits[0], the next 32 bits in +bits[1], etc. These numbers are regarded as integers multiplied by +2^e (i.e., 2 to the power of the exponent e), where e is the second +argument (be) to gdtoa and is stored in *exp by strtodg. The minimum +and maximum exponent values fpi->emin and fpi->emax for normalized +floating-point numbers reflect this arrangement. For example, the +P754 standard for binary IEEE arithmetic specifies doubles as having +53 bits, with normalized values of the form 1.xxxxx... times 2^(b-1023), +with 52 bits (the x's) and the biased exponent b represented explicitly; +b is an unsigned integer in the range 1 <= b <= 2046 for normalized +finite doubles, b = 0 for denormals, and b = 2047 for Infinities and NaNs. +To turn an IEEE double into the representation used by strtodg and gdtoa, +we multiply 1.xxxx... by 2^52 (to make it an integer) and reduce the +exponent e = (b-1023) by 52: + + fpi->emin = 1 - 1023 - 52 + fpi->emax = 1046 - 1023 - 52 + +In various wrappers for IEEE double, we actually write -53 + 1 rather +than -52, to emphasize that there are 53 bits including one implicit bit. +Field fpi->rounding indicates the desired rounding direction, with +possible values + FPI_Round_zero = toward 0, + FPI_Round_near = unbiased rounding -- the IEEE default, + FPI_Round_up = toward +Infinity, and + FPI_Round_down = toward -Infinity +given in gdtoa.h. + +Field fpi->sudden_underflow indicates whether strtodg should return +denormals or flush them to zero. Normal floating-point numbers have +bit fpi->nbits in the bits array on. Denormals have it off, with +exponent = fpi->emin. Strtodg provides distinct return values for normals +and denormals; see gdtoa.h. + +Compiling g__fmt.c, strtod.c, and strtodg.c with -DUSE_LOCALE causes +the decimal-point character to be taken from the current locale; otherwise +it is '.'. + +Please send comments to David M. Gay (dmg at acm dot org, with " at " +changed at "@" and " dot " changed to "."). diff --git a/lib/nbsd_libc/gdtoa/_strtof.c b/lib/nbsd_libc/gdtoa/_strtof.c new file mode 100644 index 000000000..7fe7a6279 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/_strtof.c @@ -0,0 +1,45 @@ +/* $NetBSD: _strtof.c,v 1.2 2009/10/21 01:07:45 snj Exp $ */ + +/* + * Copyright (c) 1996 Christos Zoulas. 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _strtof.c,v 1.2 2009/10/21 01:07:45 snj Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_strtof, strtof) +#else + +#include +float _strtof(const char * __restrict, char ** __restrict); + +float +strtof(const char *nptr, char **endptr) +{ + + return _strtof(nptr, endptr); +} +#endif diff --git a/lib/nbsd_libc/gdtoa/_strtold.c b/lib/nbsd_libc/gdtoa/_strtold.c new file mode 100644 index 000000000..475f8dab7 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/_strtold.c @@ -0,0 +1,45 @@ +/* $NetBSD: _strtold.c,v 1.2 2009/10/21 01:07:45 snj Exp $ */ + +/* + * Copyright (c) 1996 Christos Zoulas. 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _strtold.c,v 1.2 2009/10/21 01:07:45 snj Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_strtold, strtold) +#else + +#include +long double _strtold(const char * __restrict, char ** __restrict); + +long double +strtold(const char *nptr, char **endptr) +{ + + return _strtold(nptr, endptr); +} +#endif diff --git a/lib/nbsd_libc/gdtoa/arithchk.c b/lib/nbsd_libc/gdtoa/arithchk.c new file mode 100644 index 000000000..e36cdbf49 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/arithchk.c @@ -0,0 +1,191 @@ +/* $NetBSD: arithchk.c,v 1.3 2010/01/17 23:06:31 wiz Exp $ */ + +/**************************************************************** +Copyright (C) 1997, 1998 Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. +****************************************************************/ + +/* Try to deduce arith.h from arithmetic properties. */ + +#include + + static int dalign; + typedef struct +Akind { + char *name; + int kind; + } Akind; + + static Akind +IEEE_LITTLE_ENDIAN = { "IEEE_LITTLE_ENDIAN", 1 }, +IEEE_BIG_ENDIAN = { "IEEE_BIG_ENDIAN", 2 }, +IBM = { "IBM", 3 }, +VAX = { "VAX", 4 }, +CRAY = { "CRAY", 5}; + + static Akind * +Lcheck() +{ + union { + double d; + long L[2]; + } u; + struct { + double d; + long L; + } x[2]; + + if (sizeof(x) > 2*(sizeof(double) + sizeof(long))) + dalign = 1; + u.L[0] = u.L[1] = 0; + u.d = 1e13; + if (u.L[0] == 1117925532 && u.L[1] == -448790528) + return &IEEE_BIG_ENDIAN; + if (u.L[1] == 1117925532 && u.L[0] == -448790528) + return &IEEE_LITTLE_ENDIAN; + if (u.L[0] == -2065213935 && u.L[1] == 10752) + return &VAX; + if (u.L[0] == 1267827943 && u.L[1] == 704643072) + return &IBM; + return 0; + } + + static Akind * +icheck() +{ + union { + double d; + int L[2]; + } u; + struct { + double d; + int L; + } x[2]; + + if (sizeof(x) > 2*(sizeof(double) + sizeof(int))) + dalign = 1; + u.L[0] = u.L[1] = 0; + u.d = 1e13; + if (u.L[0] == 1117925532 && u.L[1] == -448790528) + return &IEEE_BIG_ENDIAN; + if (u.L[1] == 1117925532 && u.L[0] == -448790528) + return &IEEE_LITTLE_ENDIAN; + if (u.L[0] == -2065213935 && u.L[1] == 10752) + return &VAX; + if (u.L[0] == 1267827943 && u.L[1] == 704643072) + return &IBM; + return 0; + } + +char *emptyfmt = ""; /* avoid possible warning message with printf("") */ + + static Akind * +ccheck() +{ + union { + double d; + long L; + } u; + long Cray1; + + /* Cray1 = 4617762693716115456 -- without overflow on non-Crays */ + Cray1 = printf(emptyfmt) < 0 ? 0 : 4617762; + if (printf(emptyfmt, Cray1) >= 0) + Cray1 = 1000000*Cray1 + 693716; + if (printf(emptyfmt, Cray1) >= 0) + Cray1 = 1000000*Cray1 + 115456; + u.d = 1e13; + if (u.L == Cray1) + return &CRAY; + return 0; + } + + static int +fzcheck() +{ + double a, b; + int i; + + a = 1.; + b = .1; + for(i = 155;; b *= b, i >>= 1) { + if (i & 1) { + a *= b; + if (i == 1) + break; + } + } + b = a * a; + return b == 0.; + } + + int +main() +{ + Akind *a = 0; + int Ldef = 0; + FILE *f; + +#ifdef WRITE_ARITH_H /* for Symantec's buggy "make" */ + f = fopen("arith.h", "w"); + if (!f) { + printf("Cannot open arith.h\n"); + return 1; + } +#else + f = stdout; +#endif + + if (sizeof(double) == 2*sizeof(long)) + a = Lcheck(); + else if (sizeof(double) == 2*sizeof(int)) { + Ldef = 1; + a = icheck(); + } + else if (sizeof(double) == sizeof(long)) + a = ccheck(); + if (a) { + fprintf(f, "#define %s\n#define Arith_Kind_ASL %d\n", + a->name, a->kind); + if (Ldef) + fprintf(f, "#define Long int\n#define Intcast (int)(long)\n"); + if (dalign) + fprintf(f, "#define Double_Align\n"); + if (sizeof(char*) == 8) + fprintf(f, "#define X64_bit_pointers\n"); +#ifndef NO_LONG_LONG + if (sizeof(long long) < 8) +#endif + fprintf(f, "#define NO_LONG_LONG\n"); + if (a->kind <= 2 && fzcheck()) + fprintf(f, "#define Sudden_Underflow\n"); +#ifdef WRITE_ARITH_H + fclose(f); +#endif + return 0; + } + fprintf(f, "/* Unknown arithmetic */\n"); +#ifdef WRITE_ARITH_H + fclose(f); +#endif + return 1; + } diff --git a/lib/nbsd_libc/gdtoa/dmisc.c b/lib/nbsd_libc/gdtoa/dmisc.c new file mode 100644 index 000000000..121f8bbee --- /dev/null +++ b/lib/nbsd_libc/gdtoa/dmisc.c @@ -0,0 +1,227 @@ +/* $NetBSD: dmisc.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#ifndef MULTIPLE_THREADS + char *dtoa_result; +#endif + + char * +#ifdef KR_headers +rv_alloc(i) size_t i; +#else +rv_alloc(size_t i) +#endif +{ + size_t j; + int k, *r; + + j = sizeof(ULong); + for(k = 0; + sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i; + j <<= 1) + k++; + r = (int*)(void*)Balloc(k); + if (r == NULL) + return NULL; + *r = k; + return +#ifndef MULTIPLE_THREADS + dtoa_result = +#endif + (char *)(void *)(r+1); + } + + char * +#ifdef KR_headers +nrv_alloc(s, rve, n) CONST char *s; char **rve; size_t n; +#else +nrv_alloc(CONST char *s, char **rve, size_t n) +#endif +{ + char *rv, *t; + + t = rv = rv_alloc(n); + if (t == NULL) + return NULL; + while((*t = *s++) !=0) + t++; + if (rve) + *rve = t; + return rv; + } + +/* freedtoa(s) must be used to free values s returned by dtoa + * when MULTIPLE_THREADS is #defined. It should be used in all cases, + * but for consistency with earlier versions of dtoa, it is optional + * when MULTIPLE_THREADS is not defined. + */ + + void +#ifdef KR_headers +freedtoa(s) char *s; +#else +freedtoa(char *s) +#endif +{ + Bigint *b = (Bigint *)(void *)((int *)(void *)s - 1); + b->maxwds = 1 << (b->k = *(int*)(void*)b); + Bfree(b); +#ifndef MULTIPLE_THREADS + if (s == dtoa_result) + dtoa_result = 0; +#endif + } + + int +quorem +#ifdef KR_headers + (b, S) Bigint *b, *S; +#else + (Bigint *b, Bigint *S) +#endif +{ + int n; + ULong *bx, *bxe, q, *sx, *sxe; +#ifdef ULLong + ULLong borrow, carry, y, ys; +#else + ULong borrow, carry, y, ys; +#ifdef Pack_32 + ULong si, z, zs; +#endif +#endif + + n = S->wds; +#ifdef DEBUG + /*debug*/ if (b->wds > n) + /*debug*/ Bug("oversize b in quorem"); +#endif + if (b->wds < n) + return 0; + sx = S->x; + sxe = sx + --n; + bx = b->x; + bxe = bx + n; + q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ +#ifdef DEBUG + /*debug*/ if (q > 9) + /*debug*/ Bug("oversized quotient in quorem"); +#endif + if (q) { + borrow = 0; + carry = 0; + do { +#ifdef ULLong + ys = *sx++ * (ULLong)q + carry; + carry = ys >> 32; + /* LINTED conversion */ + y = *bx - (ys & 0xffffffffUL) - borrow; + borrow = y >> 32 & 1UL; + /* LINTED conversion */ + *bx++ = y & 0xffffffffUL; +#else +#ifdef Pack_32 + si = *sx++; + ys = (si & 0xffff) * q + carry; + zs = (si >> 16) * q + (ys >> 16); + carry = zs >> 16; + y = (*bx & 0xffff) - (ys & 0xffff) - borrow; + borrow = (y & 0x10000) >> 16; + z = (*bx >> 16) - (zs & 0xffff) - borrow; + borrow = (z & 0x10000) >> 16; + Storeinc(bx, z, y); +#else + ys = *sx++ * q + carry; + carry = ys >> 16; + y = *bx - (ys & 0xffff) - borrow; + borrow = (y & 0x10000) >> 16; + *bx++ = y & 0xffff; +#endif +#endif + } + while(sx <= sxe); + if (!*bxe) { + bx = b->x; + while(--bxe > bx && !*bxe) + --n; + b->wds = n; + } + } + if (cmp(b, S) >= 0) { + q++; + borrow = 0; + carry = 0; + bx = b->x; + sx = S->x; + do { +#ifdef ULLong + ys = *sx++ + carry; + carry = ys >> 32; + /* LINTED conversion */ + y = *bx - (ys & 0xffffffffUL) - borrow; + borrow = y >> 32 & 1UL; + /* LINTED conversion */ + *bx++ = y & 0xffffffffUL; +#else +#ifdef Pack_32 + si = *sx++; + ys = (si & 0xffff) + carry; + zs = (si >> 16) + (ys >> 16); + carry = zs >> 16; + y = (*bx & 0xffff) - (ys & 0xffff) - borrow; + borrow = (y & 0x10000) >> 16; + z = (*bx >> 16) - (zs & 0xffff) - borrow; + borrow = (z & 0x10000) >> 16; + Storeinc(bx, z, y); +#else + ys = *sx++ + carry; + carry = ys >> 16; + y = *bx - (ys & 0xffff) - borrow; + borrow = (y & 0x10000) >> 16; + *bx++ = y & 0xffff; +#endif +#endif + } + while(sx <= sxe); + bx = b->x; + bxe = bx + n; + if (!*bxe) { + while(--bxe > bx && !*bxe) + --n; + b->wds = n; + } + } + return q; + } diff --git a/lib/nbsd_libc/gdtoa/dtoa.c b/lib/nbsd_libc/gdtoa/dtoa.c new file mode 100644 index 000000000..ede9cb1d3 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/dtoa.c @@ -0,0 +1,815 @@ +/* $NetBSD: dtoa.c,v 1.5 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 1999 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. + * + * Inspired by "How to Print Floating-Point Numbers Accurately" by + * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. + * + * Modifications: + * 1. Rather than iterating, we use a simple numeric overestimate + * to determine k = floor(log10(d)). We scale relevant + * quantities using O(log2(k)) rather than O(k) multiplications. + * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't + * try to generate digits strictly left to right. Instead, we + * compute with fewer bits and propagate the carry if necessary + * when rounding the final digit up. This is often faster. + * 3. Under the assumption that input will be rounded nearest, + * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. + * That is, we allow equality in stopping tests when the + * round-nearest rule will give the same floating-point value + * as would satisfaction of the stopping test with strict + * inequality. + * 4. We remove common factors of powers of 2 from relevant + * quantities. + * 5. When converting floating-point integers less than 1e16, + * we use floating-point arithmetic rather than resorting + * to multiple-precision integers. + * 6. When asked to produce fewer than 15 digits, we first try + * to get by with floating-point arithmetic; we resort to + * multiple-precision integer arithmetic only if we cannot + * guarantee that the floating-point calculation has given + * the correctly rounded result. For k requested digits and + * "uniformly" distributed input, the probability is + * something like 10^(k-15) that we must resort to the Long + * calculation. + */ + +#ifdef Honor_FLT_ROUNDS +#define Rounding rounding +#undef Check_FLT_ROUNDS +#define Check_FLT_ROUNDS +#else +#define Rounding Flt_Rounds +#endif + + char * +dtoa +#ifdef KR_headers + (d, mode, ndigits, decpt, sign, rve) + double d; int mode, ndigits, *decpt, *sign; char **rve; +#else + (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) +#endif +{ + /* Arguments ndigits, decpt, sign are similar to those + of ecvt and fcvt; trailing zeros are suppressed from + the returned string. If not null, *rve is set to point + to the end of the return value. If d is +-Infinity or NaN, + then *decpt is set to 9999. + + mode: + 0 ==> shortest string that yields d when read in + and rounded to nearest. + 1 ==> like 0, but with Steele & White stopping rule; + e.g. with IEEE P754 arithmetic , mode 0 gives + 1e23 whereas mode 1 gives 9.999999999999999e22. + 2 ==> max(1,ndigits) significant digits. This gives a + return value similar to that of ecvt, except + that trailing zeros are suppressed. + 3 ==> through ndigits past the decimal point. This + gives a return value similar to that from fcvt, + except that trailing zeros are suppressed, and + ndigits can be negative. + 4,5 ==> similar to 2 and 3, respectively, but (in + round-nearest mode) with the tests of mode 0 to + possibly return a shorter string that rounds to d. + With IEEE arithmetic and compilation with + -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same + as modes 2 and 3 when FLT_ROUNDS != 1. + 6-9 ==> Debugging modes similar to mode - 4: don't try + fast floating-point estimate (if applicable). + + Values of mode other than 0-9 are treated as mode 0. + + Sufficient space is allocated to the return value + to hold the suppressed trailing zeros. + */ + + int bbits, b2, b5, be, dig, i, ieps, ilim0, + j, jj1, k, k0, k_check, leftright, m2, m5, s2, s5, + spec_case, try_quick; + int ilim = 0, ilim1 = 0; /* pacify gcc */ + Long L; +#ifndef Sudden_Underflow + int denorm; + ULong x; +#endif + Bigint *b, *b1, *delta, *mhi, *S; + Bigint *mlo = NULL; /* pacify gcc */ + double d2, ds, eps; + char *s, *s0; +#ifdef Honor_FLT_ROUNDS + int rounding; +#endif +#ifdef SET_INEXACT + int inexact, oldinexact; +#endif + +#ifndef MULTIPLE_THREADS + if (dtoa_result) { + freedtoa(dtoa_result); + dtoa_result = 0; + } +#endif + + if (word0(d) & Sign_bit) { + /* set sign for everything, including 0's and NaNs */ + *sign = 1; + word0(d) &= ~Sign_bit; /* clear sign bit */ + } + else + *sign = 0; + +#if defined(IEEE_Arith) + defined(VAX) +#ifdef IEEE_Arith + if ((word0(d) & Exp_mask) == Exp_mask) +#else + if (word0(d) == 0x8000) +#endif + { + /* Infinity or NaN */ + *decpt = 9999; +#ifdef IEEE_Arith + if (!word1(d) && !(word0(d) & 0xfffff)) + return nrv_alloc("Infinity", rve, 8); +#endif + return nrv_alloc("NaN", rve, 3); + } +#endif +#ifdef IBM + dval(d) += 0; /* normalize */ +#endif + if (!dval(d)) { + *decpt = 1; + return nrv_alloc("0", rve, 1); + } + +#ifdef SET_INEXACT + try_quick = oldinexact = get_inexact(); + inexact = 1; +#endif +#ifdef Honor_FLT_ROUNDS + if ((rounding = Flt_Rounds) >= 2) { + if (*sign) + rounding = rounding == 2 ? 0 : 2; + else + if (rounding != 2) + rounding = 0; + } +#endif + + b = d2b(dval(d), &be, &bbits); + if (b == NULL) + return NULL; +#ifdef Sudden_Underflow + i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); +#else + if (( i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)) )!=0) { +#endif + dval(d2) = dval(d); + word0(d2) &= Frac_mask1; + word0(d2) |= Exp_11; +#ifdef IBM + if (( j = 11 - hi0bits(word0(d2) & Frac_mask) )!=0) + dval(d2) /= 1 << j; +#endif + + /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 + * log10(x) = log(x) / log(10) + * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) + * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) + * + * This suggests computing an approximation k to log10(d) by + * + * k = (i - Bias)*0.301029995663981 + * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); + * + * We want k to be too large rather than too small. + * The error in the first-order Taylor series approximation + * is in our favor, so we just round up the constant enough + * to compensate for any error in the multiplication of + * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, + * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, + * adding 1e-13 to the constant term more than suffices. + * Hence we adjust the constant term to 0.1760912590558. + * (We could get a more accurate k by invoking log10, + * but this is probably not worthwhile.) + */ + + i -= Bias; +#ifdef IBM + i <<= 2; + i += j; +#endif +#ifndef Sudden_Underflow + denorm = 0; + } + else { + /* d is denormalized */ + + i = bbits + be + (Bias + (P-1) - 1); + x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32) + : word1(d) << (32 - i); + dval(d2) = x; + word0(d2) -= 31*Exp_msk1; /* adjust exponent */ + i -= (Bias + (P-1) - 1) + 1; + denorm = 1; + } +#endif + ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; + k = (int)ds; + if (ds < 0. && ds != k) + k--; /* want k = floor(ds) */ + k_check = 1; + if (k >= 0 && k <= Ten_pmax) { + if (dval(d) < tens[k]) + k--; + k_check = 0; + } + j = bbits - i - 1; + if (j >= 0) { + b2 = 0; + s2 = j; + } + else { + b2 = -j; + s2 = 0; + } + if (k >= 0) { + b5 = 0; + s5 = k; + s2 += k; + } + else { + b2 -= k; + b5 = -k; + s5 = 0; + } + if (mode < 0 || mode > 9) + mode = 0; + +#ifndef SET_INEXACT +#ifdef Check_FLT_ROUNDS + try_quick = Rounding == 1; +#else + try_quick = 1; +#endif +#endif /*SET_INEXACT*/ + + if (mode > 5) { + mode -= 4; + try_quick = 0; + } + leftright = 1; + switch(mode) { + case 0: + case 1: + ilim = ilim1 = -1; + i = 18; + ndigits = 0; + break; + case 2: + leftright = 0; + /* FALLTHROUGH */ + case 4: + if (ndigits <= 0) + ndigits = 1; + ilim = ilim1 = i = ndigits; + break; + case 3: + leftright = 0; + /* FALLTHROUGH */ + case 5: + i = ndigits + k + 1; + ilim = i; + ilim1 = i - 1; + if (i <= 0) + i = 1; + } + s = s0 = rv_alloc((size_t)i); + if (s == NULL) + return NULL; + +#ifdef Honor_FLT_ROUNDS + if (mode > 1 && rounding != 1) + leftright = 0; +#endif + + if (ilim >= 0 && ilim <= Quick_max && try_quick) { + + /* Try to get by with floating-point arithmetic. */ + + i = 0; + dval(d2) = dval(d); + k0 = k; + ilim0 = ilim; + ieps = 2; /* conservative */ + if (k > 0) { + ds = tens[k&0xf]; + j = (unsigned int)k >> 4; + if (j & Bletch) { + /* prevent overflows */ + j &= Bletch - 1; + dval(d) /= bigtens[n_bigtens-1]; + ieps++; + } + for(; j; j = (unsigned int)j >> 1, i++) + if (j & 1) { + ieps++; + ds *= bigtens[i]; + } + dval(d) /= ds; + } + else if (( jj1 = -k )!=0) { + dval(d) *= tens[jj1 & 0xf]; + for(j = jj1 >> 4; j; j >>= 1, i++) + if (j & 1) { + ieps++; + dval(d) *= bigtens[i]; + } + } + if (k_check && dval(d) < 1. && ilim > 0) { + if (ilim1 <= 0) + goto fast_failed; + ilim = ilim1; + k--; + dval(d) *= 10.; + ieps++; + } + dval(eps) = ieps*dval(d) + 7.; + word0(eps) -= (P-1)*Exp_msk1; + if (ilim == 0) { + S = mhi = 0; + dval(d) -= 5.; + if (dval(d) > dval(eps)) + goto one_digit; + if (dval(d) < -dval(eps)) + goto no_digits; + goto fast_failed; + } +#ifndef No_leftright + if (leftright) { + /* Use Steele & White method of only + * generating digits needed. + */ + dval(eps) = 0.5/tens[ilim-1] - dval(eps); + for(i = 0;;) { + L = dval(d); + dval(d) -= L; + *s++ = '0' + (int)L; + if (dval(d) < dval(eps)) + goto ret1; + if (1. - dval(d) < dval(eps)) + goto bump_up; + if (++i >= ilim) + break; + dval(eps) *= 10.; + dval(d) *= 10.; + } + } + else { +#endif + /* Generate ilim digits, then fix them up. */ + dval(eps) *= tens[ilim-1]; + for(i = 1;; i++, dval(d) *= 10.) { + L = (Long)(dval(d)); + if (!(dval(d) -= L)) + ilim = i; + *s++ = '0' + (int)L; + if (i == ilim) { + if (dval(d) > 0.5 + dval(eps)) + goto bump_up; + else if (dval(d) < 0.5 - dval(eps)) { + while(*--s == '0'); + s++; + goto ret1; + } + break; + } + } +#ifndef No_leftright + } +#endif + fast_failed: + s = s0; + dval(d) = dval(d2); + k = k0; + ilim = ilim0; + } + + /* Do we have a "small" integer? */ + + if (be >= 0 && k <= Int_max) { + /* Yes. */ + ds = tens[k]; + if (ndigits < 0 && ilim <= 0) { + S = mhi = 0; + if (ilim < 0 || dval(d) <= 5*ds) + goto no_digits; + goto one_digit; + } + for(i = 1;; i++, dval(d) *= 10.) { + L = (Long)(dval(d) / ds); + dval(d) -= L*ds; +#ifdef Check_FLT_ROUNDS + /* If FLT_ROUNDS == 2, L will usually be high by 1 */ + if (dval(d) < 0) { + L--; + dval(d) += ds; + } +#endif + *s++ = '0' + (int)L; + if (!dval(d)) { +#ifdef SET_INEXACT + inexact = 0; +#endif + break; + } + if (i == ilim) { +#ifdef Honor_FLT_ROUNDS + if (mode > 1) + switch(rounding) { + case 0: goto ret1; + case 2: goto bump_up; + } +#endif + dval(d) += dval(d); + if (dval(d) > ds || (dval(d) == ds && L & 1)) { + bump_up: + while(*--s == '9') + if (s == s0) { + k++; + *s = '0'; + break; + } + ++*s++; + } + break; + } + } + goto ret1; + } + + m2 = b2; + m5 = b5; + mhi = mlo = 0; + if (leftright) { + i = +#ifndef Sudden_Underflow + denorm ? be + (Bias + (P-1) - 1 + 1) : +#endif +#ifdef IBM + 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); +#else + 1 + P - bbits; +#endif + b2 += i; + s2 += i; + mhi = i2b(1); + if (mhi == NULL) + return NULL; + } + if (m2 > 0 && s2 > 0) { + i = m2 < s2 ? m2 : s2; + b2 -= i; + m2 -= i; + s2 -= i; + } + if (b5 > 0) { + if (leftright) { + if (m5 > 0) { + mhi = pow5mult(mhi, m5); + if (mhi == NULL) + return NULL; + b1 = mult(mhi, b); + if (b1 == NULL) + return NULL; + Bfree(b); + b = b1; + } + if (( j = b5 - m5 )!=0) + b = pow5mult(b, j); + if (b == NULL) + return NULL; + } + else + b = pow5mult(b, b5); + if (b == NULL) + return NULL; + } + S = i2b(1); + if (S == NULL) + return NULL; + if (s5 > 0) { + S = pow5mult(S, s5); + if (S == NULL) + return NULL; + } + + /* Check for special case that d is a normalized power of 2. */ + + spec_case = 0; + if ((mode < 2 || leftright) +#ifdef Honor_FLT_ROUNDS + && rounding == 1 +#endif + ) { + if (!word1(d) && !(word0(d) & Bndry_mask) +#ifndef Sudden_Underflow + && word0(d) & (Exp_mask & ~Exp_msk1) +#endif + ) { + /* The special case */ + b2 += Log2P; + s2 += Log2P; + spec_case = 1; + } + } + + /* Arrange for convenient computation of quotients: + * shift left if necessary so divisor has 4 leading 0 bits. + * + * Perhaps we should just compute leading 28 bits of S once + * and for all and pass them and a shift to quorem, so it + * can do shifts and ors to compute the numerator for q. + */ +#ifdef Pack_32 + if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f )!=0) + i = 32 - i; +#else + if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf )!=0) + i = 16 - i; +#endif + if (i > 4) { + i -= 4; + b2 += i; + m2 += i; + s2 += i; + } + else if (i < 4) { + i += 28; + b2 += i; + m2 += i; + s2 += i; + } + if (b2 > 0) { + b = lshift(b, b2); + if (b == NULL) + return NULL; + } + if (s2 > 0) { + S = lshift(S, s2); + if (S == NULL) + return NULL; + } + if (k_check) { + if (cmp(b,S) < 0) { + k--; + b = multadd(b, 10, 0); /* we botched the k estimate */ + if (b == NULL) + return NULL; + if (leftright) { + mhi = multadd(mhi, 10, 0); + if (mhi == NULL) + return NULL; + } + ilim = ilim1; + } + } + if (ilim <= 0 && (mode == 3 || mode == 5)) { + if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { + /* no digits, fcvt style */ + no_digits: + k = -1 - ndigits; + goto ret; + } + one_digit: + *s++ = '1'; + k++; + goto ret; + } + if (leftright) { + if (m2 > 0) { + mhi = lshift(mhi, m2); + if (mhi == NULL) + return NULL; + } + + /* Compute mlo -- check for special case + * that d is a normalized power of 2. + */ + + mlo = mhi; + if (spec_case) { + mhi = Balloc(mhi->k); + if (mhi == NULL) + return NULL; + Bcopy(mhi, mlo); + mhi = lshift(mhi, Log2P); + if (mhi == NULL) + return NULL; + } + + for(i = 1;;i++) { + dig = quorem(b,S) + '0'; + /* Do we yet have the shortest decimal string + * that will round to d? + */ + j = cmp(b, mlo); + delta = diff(S, mhi); + if (delta == NULL) + return NULL; + jj1 = delta->sign ? 1 : cmp(b, delta); + Bfree(delta); +#ifndef ROUND_BIASED + if (jj1 == 0 && mode != 1 && !(word1(d) & 1) +#ifdef Honor_FLT_ROUNDS + && rounding >= 1 +#endif + ) { + if (dig == '9') + goto round_9_up; + if (j > 0) + dig++; +#ifdef SET_INEXACT + else if (!b->x[0] && b->wds <= 1) + inexact = 0; +#endif + *s++ = dig; + goto ret; + } +#endif + if (j < 0 || (j == 0 && mode != 1 +#ifndef ROUND_BIASED + && !(word1(d) & 1) +#endif + )) { + if (!b->x[0] && b->wds <= 1) { +#ifdef SET_INEXACT + inexact = 0; +#endif + goto accept_dig; + } +#ifdef Honor_FLT_ROUNDS + if (mode > 1) + switch(rounding) { + case 0: goto accept_dig; + case 2: goto keep_dig; + } +#endif /*Honor_FLT_ROUNDS*/ + if (jj1 > 0) { + b = lshift(b, 1); + if (b == NULL) + return NULL; + jj1 = cmp(b, S); + if ((jj1 > 0 || (jj1 == 0 && dig & 1)) + && dig++ == '9') + goto round_9_up; + } + accept_dig: + *s++ = dig; + goto ret; + } + if (jj1 > 0) { +#ifdef Honor_FLT_ROUNDS + if (!rounding) + goto accept_dig; +#endif + if (dig == '9') { /* possible if i == 1 */ + round_9_up: + *s++ = '9'; + goto roundoff; + } + *s++ = dig + 1; + goto ret; + } +#ifdef Honor_FLT_ROUNDS + keep_dig: +#endif + *s++ = dig; + if (i == ilim) + break; + b = multadd(b, 10, 0); + if (b == NULL) + return NULL; + if (mlo == mhi) { + mlo = mhi = multadd(mhi, 10, 0); + if (mlo == NULL) + return NULL; + } + else { + mlo = multadd(mlo, 10, 0); + if (mlo == NULL) + return NULL; + mhi = multadd(mhi, 10, 0); + if (mhi == NULL) + return NULL; + } + } + } + else + for(i = 1;; i++) { + *s++ = dig = quorem(b,S) + '0'; + if (!b->x[0] && b->wds <= 1) { +#ifdef SET_INEXACT + inexact = 0; +#endif + goto ret; + } + if (i >= ilim) + break; + b = multadd(b, 10, 0); + if (b == NULL) + return NULL; + } + + /* Round off last digit */ + +#ifdef Honor_FLT_ROUNDS + switch(rounding) { + case 0: goto trimzeros; + case 2: goto roundoff; + } +#endif + b = lshift(b, 1); + j = cmp(b, S); + if (j > 0 || (j == 0 && dig & 1)) { + roundoff: + while(*--s == '9') + if (s == s0) { + k++; + *s++ = '1'; + goto ret; + } + ++*s++; + } + else { +#ifdef Honor_FLT_ROUNDS + trimzeros: +#endif + while(*--s == '0'); + s++; + } + ret: + Bfree(S); + if (mhi) { + if (mlo && mlo != mhi) + Bfree(mlo); + Bfree(mhi); + } + ret1: +#ifdef SET_INEXACT + if (inexact) { + if (!oldinexact) { + word0(d) = Exp_1 + (70 << Exp_shift); + word1(d) = 0; + dval(d) += 1.; + } + } + else if (!oldinexact) + clear_inexact(); +#endif + Bfree(b); + if (s == s0) { /* don't return empty string */ + *s++ = '0'; + k = 0; + } + *s = 0; + *decpt = k + 1; + if (rve) + *rve = s; + return s0; + } diff --git a/lib/nbsd_libc/gdtoa/g_Qfmt.c b/lib/nbsd_libc/gdtoa/g_Qfmt.c new file mode 100644 index 000000000..83a204179 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/g_Qfmt.c @@ -0,0 +1,118 @@ +/* $NetBSD: g_Qfmt.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#define _3 3 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 3 +#define _1 2 +#define _2 1 +#define _3 0 +#endif + + char* +#ifdef KR_headers +g_Qfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; unsigned bufsize; +#else +g_Qfmt(char *buf, void *V, int ndig, unsigned bufsize) +#endif +{ + static FPI fpi = { 113, 1-16383-113+1, 32766 - 16383 - 113 + 1, 1, 0 }; + char *b, *s, *se; + ULong bits[4], *L, sign; + int decpt, ex, i, mode; + + if (ndig < 0) + ndig = 0; + if (bufsize < ndig + 10) + return 0; + + L = (ULong*)V; + sign = L[_0] & 0x80000000L; + bits[3] = L[_0] & 0xffff; + bits[2] = L[_1]; + bits[1] = L[_2]; + bits[0] = L[_3]; + b = buf; + if ( (ex = (L[_0] & 0x7fff0000L) >> 16) !=0) { + if (ex == 0x7fff) { + /* Infinity or NaN */ + if (bits[0] | bits[1] | bits[2] | bits[3]) + b = strcp(b, "NaN"); + else { + b = buf; + if (sign) + *b++ = '-'; + b = strcp(b, "Infinity"); + } + return b; + } + i = STRTOG_Normal; + bits[3] |= 0x10000; + } + else if (bits[0] | bits[1] | bits[2] | bits[3]) { + i = STRTOG_Denormal; + ex = 1; + } + else { +#ifndef IGNORE_ZERO_SIGN + if (sign) + *b++ = '-'; +#endif + *b++ = '0'; + *b = 0; + return b; + } + ex -= 0x3fff + 112; + mode = 2; + if (ndig <= 0) { + if (bufsize < 48) + return 0; + mode = 0; + } + s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); + if (s == NULL) + return NULL; + return g__fmt(buf, s, se, decpt, sign); + } diff --git a/lib/nbsd_libc/gdtoa/g__fmt.c b/lib/nbsd_libc/gdtoa/g__fmt.c new file mode 100644 index 000000000..de85452b5 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/g__fmt.c @@ -0,0 +1,101 @@ +/* $NetBSD: g__fmt.c,v 1.1.1.1 2006/01/25 15:18:41 kleink Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#ifdef USE_LOCALE +#include "locale.h" +#endif + + char * +#ifdef KR_headers +g__fmt(b, s, se, decpt, sign) char *b; char *s; char *se; int decpt; ULong sign; +#else +g__fmt(char *b, char *s, char *se, int decpt, ULong sign) +#endif +{ + int i, j, k; + char *s0 = s; +#ifdef USE_LOCALE + char decimalpoint = *localeconv()->decimal_point; +#else +#define decimalpoint '.' +#endif + if (sign) + *b++ = '-'; + if (decpt <= -4 || decpt > se - s + 5) { + *b++ = *s++; + if (*s) { + *b++ = decimalpoint; + while((*b = *s++) !=0) + b++; + } + *b++ = 'e'; + /* sprintf(b, "%+.2d", decpt - 1); */ + if (--decpt < 0) { + *b++ = '-'; + decpt = -decpt; + } + else + *b++ = '+'; + for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){} + for(;;) { + i = decpt / k; + *b++ = i + '0'; + if (--j <= 0) + break; + decpt -= i*k; + decpt *= 10; + } + *b = 0; + } + else if (decpt <= 0) { + *b++ = decimalpoint; + for(; decpt < 0; decpt++) + *b++ = '0'; + while((*b = *s++) !=0) + b++; + } + else { + while((*b = *s++) !=0) { + b++; + if (--decpt == 0 && *s) + *b++ = decimalpoint; + } + for(; decpt > 0; decpt--) + *b++ = '0'; + *b = 0; + } + freedtoa(s0); + return b; + } diff --git a/lib/nbsd_libc/gdtoa/g_ddfmt.c b/lib/nbsd_libc/gdtoa/g_ddfmt.c new file mode 100644 index 000000000..9b0725d7d --- /dev/null +++ b/lib/nbsd_libc/gdtoa/g_ddfmt.c @@ -0,0 +1,173 @@ +/* $NetBSD: g_ddfmt.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg@acm.org). */ + +#include "gdtoaimp.h" +#include + + char * +#ifdef KR_headers +g_ddfmt(buf, dd, ndig, bufsize) char *buf; double *dd; int ndig; unsigned bufsize; +#else +g_ddfmt(char *buf, double *dd, int ndig, unsigned bufsize) +#endif +{ + FPI fpi; + char *b, *s, *se; + ULong *L, bits0[4], *bits, *zx; + int bx, by, decpt, ex, ey, i, j, mode; + Bigint *x, *y, *z; + double ddx[2]; + + if (bufsize < 10 || bufsize < ndig + 8) + return 0; + + L = (ULong*)dd; + if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) { + /* Infinity or NaN */ + if (L[_0] & 0xfffff || L[_1]) { + nanret: + return strcp(buf, "NaN"); + } + if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { + if (L[2+_0] & 0xfffff || L[2+_1]) + goto nanret; + if ((L[_0] ^ L[2+_0]) & 0x80000000L) + goto nanret; /* Infinity - Infinity */ + } + infret: + b = buf; + if (L[_0] & 0x80000000L) + *b++ = '-'; + return strcp(b, "Infinity"); + } + if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { + L += 2; + if (L[_0] & 0xfffff || L[_1]) + goto nanret; + goto infret; + } + if (dd[0] + dd[1] == 0.) { + b = buf; +#ifndef IGNORE_ZERO_SIGN + if (L[_0] & L[2+_0] & 0x80000000L) + *b++ = '-'; +#endif + *b++ = '0'; + *b = 0; + return b; + } + if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) { + ddx[1] = dd[0]; + ddx[0] = dd[1]; + dd = ddx; + L = (ULong*)dd; + } + z = d2b(dd[0], &ex, &bx); + if (z == NULL) + return NULL; + if (dd[1] == 0.) + goto no_y; + x = z; + y = d2b(dd[1], &ey, &by); + if (y == NULL) + return NULL; + if ( (i = ex - ey) !=0) { + if (i > 0) { + x = lshift(x, i); + if (x == NULL) + return NULL; + ex = ey; + } + else { + y = lshift(y, -i); + if (y == NULL) + return NULL; + } + } + if ((L[_0] ^ L[2+_0]) & 0x80000000L) { + z = diff(x, y); + if (z == NULL) + return NULL; + if (L[_0] & 0x80000000L) + z->sign = 1 - z->sign; + } + else { + z = sum(x, y); + if (z == NULL) + return NULL; + if (L[_0] & 0x80000000L) + z->sign = 1; + } + Bfree(x); + Bfree(y); + no_y: + bits = zx = z->x; + for(i = 0; !*zx; zx++) + i += 32; + i += lo0bits(zx); + if (i) { + rshift(z, i); + ex += i; + } + fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]); + if (fpi.nbits < 106) { + fpi.nbits = 106; + if (j < 3) { + for(i = 0; i <= j; i++) + bits0[i] = bits[i]; + while(i < 4) + bits0[i++] = 0; + bits = bits0; + } + } + mode = 2; + if (ndig <= 0) { + if (bufsize < (int)(fpi.nbits * .301029995664) + 10) { + Bfree(z); + return 0; + } + mode = 0; + } + fpi.emin = 1-1023-53+1; + fpi.emax = 2046-1023-106+1; + fpi.rounding = FPI_Round_near; + fpi.sudden_underflow = 0; + i = STRTOG_Normal; + s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); + if (s == NULL) + return NULL; + b = g__fmt(buf, s, se, decpt, z->sign); + if (b == NULL) + return NULL; + Bfree(z); + return b; + } diff --git a/lib/nbsd_libc/gdtoa/g_dfmt.c b/lib/nbsd_libc/gdtoa/g_dfmt.c new file mode 100644 index 000000000..d8379f054 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/g_dfmt.c @@ -0,0 +1,93 @@ +/* $NetBSD: g_dfmt.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + char* +#ifdef KR_headers +g_dfmt(buf, d, ndig, bufsize) char *buf; double *d; int ndig; unsigned bufsize; +#else +g_dfmt(char *buf, double *d, int ndig, unsigned bufsize) +#endif +{ + static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, 0 }; + char *b, *s, *se; + ULong bits[2], *L, sign; + int decpt, ex, i, mode; + + if (ndig < 0) + ndig = 0; + if (bufsize < ndig + 10) + return 0; + + L = (ULong*)d; + sign = L[_0] & 0x80000000L; + if ((L[_0] & 0x7ff00000) == 0x7ff00000) { + /* Infinity or NaN */ + if (L[_0] & 0xfffff || L[_1]) { + return strcp(buf, "NaN"); + } + b = buf; + if (sign) + *b++ = '-'; + return strcp(b, "Infinity"); + } + if (L[_1] == 0 && (L[_0] ^ sign) == 0 /*d == 0.*/) { + b = buf; +#ifndef IGNORE_ZERO_SIGN + if (L[_0] & 0x80000000L) + *b++ = '-'; +#endif + *b++ = '0'; + *b = 0; + return b; + } + bits[0] = L[_1]; + bits[1] = L[_0] & 0xfffff; + if ( (ex = (L[_0] >> 20) & 0x7ff) !=0) + bits[1] |= 0x100000; + else + ex = 1; + ex -= 0x3ff + 52; + mode = 2; + if (ndig <= 0) { + if (bufsize < 25) + return 0; + mode = 0; + } + i = STRTOG_Normal; + s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); + if (s == NULL) + return NULL; + return g__fmt(buf, s, se, decpt, sign); + } diff --git a/lib/nbsd_libc/gdtoa/g_ffmt.c b/lib/nbsd_libc/gdtoa/g_ffmt.c new file mode 100644 index 000000000..ca7f646b3 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/g_ffmt.c @@ -0,0 +1,92 @@ +/* $NetBSD: g_ffmt.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + char* +#ifdef KR_headers +g_ffmt(buf, f, ndig, bufsize) char *buf; float *f; int ndig; unsigned bufsize; +#else +g_ffmt(char *buf, float *f, int ndig, unsigned bufsize) +#endif +{ + static FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, 0 }; + char *b, *s, *se; + ULong bits[1], *L, sign; + int decpt, ex, i, mode; + + if (ndig < 0) + ndig = 0; + if (bufsize < ndig + 10) + return 0; + + L = (ULong*)f; + sign = L[0] & 0x80000000L; + if ((L[0] & 0x7f800000) == 0x7f800000) { + /* Infinity or NaN */ + if (L[0] & 0x7fffff) { + return strcp(buf, "NaN"); + } + b = buf; + if (sign) + *b++ = '-'; + return strcp(b, "Infinity"); + } + if (*f == 0.) { + b = buf; +#ifndef IGNORE_ZERO_SIGN + if (L[0] & 0x80000000L) + *b++ = '-'; +#endif + *b++ = '0'; + *b = 0; + return b; + } + bits[0] = L[0] & 0x7fffff; + if ( (ex = (L[0] >> 23) & 0xff) !=0) + bits[0] |= 0x800000; + else + ex = 1; + ex -= 0x7f + 23; + mode = 2; + if (ndig <= 0) { + if (bufsize < 16) + return 0; + mode = 0; + } + i = STRTOG_Normal; + s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); + if (s == NULL) + return NULL; + return g__fmt(buf, s, se, decpt, sign); + } diff --git a/lib/nbsd_libc/gdtoa/g_xLfmt.c b/lib/nbsd_libc/gdtoa/g_xLfmt.c new file mode 100644 index 000000000..a1126ffdf --- /dev/null +++ b/lib/nbsd_libc/gdtoa/g_xLfmt.c @@ -0,0 +1,112 @@ +/* $NetBSD: g_xLfmt.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 2 +#define _1 1 +#define _2 0 +#endif + + char* +#ifdef KR_headers +g_xLfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; unsigned bufsize; +#else +g_xLfmt(char *buf, void *V, int ndig, unsigned bufsize) +#endif +{ + static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, 0 }; + char *b, *s, *se; + ULong bits[2], *L, sign; + int decpt, ex, i, mode; + + if (ndig < 0) + ndig = 0; + if (bufsize < ndig + 10) + return 0; + + L = (ULong*)V; + sign = L[_0] & 0x80000000L; + bits[1] = L[_1]; + bits[0] = L[_2]; + if ( (ex = (L[_0] >> 16) & 0x7fff) !=0) { + if (ex == 0x7fff) { + /* Infinity or NaN */ + if (bits[0] | bits[1]) + b = strcp(buf, "NaN"); + else { + b = buf; + if (sign) + *b++ = '-'; + b = strcp(b, "Infinity"); + } + return b; + } + i = STRTOG_Normal; + } + else if (bits[0] | bits[1]) { + i = STRTOG_Denormal; + } + else { + b = buf; +#ifndef IGNORE_ZERO_SIGN + if (sign) + *b++ = '-'; +#endif + *b++ = '0'; + *b = 0; + return b; + } + ex -= 0x3fff + 63; + mode = 2; + if (ndig <= 0) { + if (bufsize < 32) + return 0; + mode = 0; + } + s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); + if (s == NULL) + return NULL; + return g__fmt(buf, s, se, decpt, sign); + } diff --git a/lib/nbsd_libc/gdtoa/g_xfmt.c b/lib/nbsd_libc/gdtoa/g_xfmt.c new file mode 100644 index 000000000..18b434e19 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/g_xfmt.c @@ -0,0 +1,118 @@ +/* $NetBSD: g_xfmt.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#define _3 3 +#define _4 4 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 4 +#define _1 3 +#define _2 2 +#define _3 1 +#define _4 0 +#endif + + char* +#ifdef KR_headers +g_xfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; unsigned bufsize; +#else +g_xfmt(char *buf, void *V, int ndig, unsigned bufsize) +#endif +{ + static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, 0 }; + char *b, *s, *se; + ULong bits[2], sign; + UShort *L; + int decpt, ex, i, mode; + + if (ndig < 0) + ndig = 0; + if (bufsize < ndig + 10) + return 0; + + L = (UShort *)V; + sign = L[_0] & 0x8000; + bits[1] = (L[_1] << 16) | L[_2]; + bits[0] = (L[_3] << 16) | L[_4]; + if ( (ex = L[_0] & 0x7fff) !=0) { + if (ex == 0x7fff) { + /* Infinity or NaN */ + if (bits[0] | bits[1]) + b = strcp(buf, "NaN"); + else { + b = buf; + if (sign) + *b++ = '-'; + b = strcp(b, "Infinity"); + } + return b; + } + i = STRTOG_Normal; + } + else if (bits[0] | bits[1]) { + i = STRTOG_Denormal; + ex = 1; + } + else { + b = buf; +#ifndef IGNORE_ZERO_SIGN + if (sign) + *b++ = '-'; +#endif + *b++ = '0'; + *b = 0; + return b; + } + ex -= 0x3fff + 63; + mode = 2; + if (ndig <= 0) { + if (bufsize < 32) + return 0; + mode = 0; + } + s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); + if (s == NULL) + return NULL; + return g__fmt(buf, s, se, decpt, sign); + } diff --git a/lib/nbsd_libc/gdtoa/gdtoa.c b/lib/nbsd_libc/gdtoa/gdtoa.c new file mode 100644 index 000000000..95f7f4f11 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/gdtoa.c @@ -0,0 +1,816 @@ +/* $NetBSD: gdtoa.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 1999 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + static Bigint * +#ifdef KR_headers +bitstob(bits, nbits, bbits) ULong *bits; int nbits; int *bbits; +#else +bitstob(ULong *bits, int nbits, int *bbits) +#endif +{ + int i, k; + Bigint *b; + ULong *be, *x, *x0; + + i = ULbits; + k = 0; + while(i < nbits) { + i <<= 1; + k++; + } +#ifndef Pack_32 + if (!k) + k = 1; +#endif + b = Balloc(k); + if (b == NULL) + return NULL; + be = bits + (((unsigned int)nbits - 1) >> kshift); + x = x0 = b->x; + do { + *x++ = *bits & ALL_ON; +#ifdef Pack_16 + *x++ = (*bits >> 16) & ALL_ON; +#endif + } while(++bits <= be); + i = x - x0; + while(!x0[--i]) + if (!i) { + b->wds = 0; + *bbits = 0; + goto ret; + } + b->wds = i + 1; + *bbits = i*ULbits + 32 - hi0bits(b->x[i]); + ret: + return b; + } + +/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. + * + * Inspired by "How to Print Floating-Point Numbers Accurately" by + * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. + * + * Modifications: + * 1. Rather than iterating, we use a simple numeric overestimate + * to determine k = floor(log10(d)). We scale relevant + * quantities using O(log2(k)) rather than O(k) multiplications. + * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't + * try to generate digits strictly left to right. Instead, we + * compute with fewer bits and propagate the carry if necessary + * when rounding the final digit up. This is often faster. + * 3. Under the assumption that input will be rounded nearest, + * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. + * That is, we allow equality in stopping tests when the + * round-nearest rule will give the same floating-point value + * as would satisfaction of the stopping test with strict + * inequality. + * 4. We remove common factors of powers of 2 from relevant + * quantities. + * 5. When converting floating-point integers less than 1e16, + * we use floating-point arithmetic rather than resorting + * to multiple-precision integers. + * 6. When asked to produce fewer than 15 digits, we first try + * to get by with floating-point arithmetic; we resort to + * multiple-precision integer arithmetic only if we cannot + * guarantee that the floating-point calculation has given + * the correctly rounded result. For k requested digits and + * "uniformly" distributed input, the probability is + * something like 10^(k-15) that we must resort to the Long + * calculation. + */ + + char * +gdtoa +#ifdef KR_headers + (fpi, be, bits, kindp, mode, ndigits, decpt, rve) + FPI *fpi; int be; ULong *bits; + int *kindp, mode, ndigits, *decpt; char **rve; +#else + (FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, int *decpt, char **rve) +#endif +{ + /* Arguments ndigits and decpt are similar to the second and third + arguments of ecvt and fcvt; trailing zeros are suppressed from + the returned string. If not null, *rve is set to point + to the end of the return value. If d is +-Infinity or NaN, + then *decpt is set to 9999. + + mode: + 0 ==> shortest string that yields d when read in + and rounded to nearest. + 1 ==> like 0, but with Steele & White stopping rule; + e.g. with IEEE P754 arithmetic , mode 0 gives + 1e23 whereas mode 1 gives 9.999999999999999e22. + 2 ==> max(1,ndigits) significant digits. This gives a + return value similar to that of ecvt, except + that trailing zeros are suppressed. + 3 ==> through ndigits past the decimal point. This + gives a return value similar to that from fcvt, + except that trailing zeros are suppressed, and + ndigits can be negative. + 4-9 should give the same return values as 2-3, i.e., + 4 <= mode <= 9 ==> same return as mode + 2 + (mode & 1). These modes are mainly for + debugging; often they run slower but sometimes + faster than modes 2-3. + 4,5,8,9 ==> left-to-right digit generation. + 6-9 ==> don't try fast floating-point estimate + (if applicable). + + Values of mode other than 0-9 are treated as mode 0. + + Sufficient space is allocated to the return value + to hold the suppressed trailing zeros. + */ + + int bbits, b2, b5, be0, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0, inex; + int j, jj1, k, k0, k_check, kind, leftright, m2, m5, nbits; + int rdir, s2, s5, spec_case, try_quick; + Long L; + Bigint *b, *b1, *delta, *mlo, *mhi, *mhi1, *S; + double d, d2, ds, eps; + char *s, *s0; + +#ifndef MULTIPLE_THREADS + if (dtoa_result) { + freedtoa(dtoa_result); + dtoa_result = 0; + } +#endif + inex = 0; + if (*kindp & STRTOG_NoMemory) + return NULL; + kind = *kindp &= ~STRTOG_Inexact; + switch(kind & STRTOG_Retmask) { + case STRTOG_Zero: + goto ret_zero; + case STRTOG_Normal: + case STRTOG_Denormal: + break; + case STRTOG_Infinite: + *decpt = -32768; + return nrv_alloc("Infinity", rve, 8); + case STRTOG_NaN: + *decpt = -32768; + return nrv_alloc("NaN", rve, 3); + default: + return 0; + } + b = bitstob(bits, nbits = fpi->nbits, &bbits); + if (b == NULL) + return NULL; + be0 = be; + if ( (i = trailz(b)) !=0) { + rshift(b, i); + be += i; + bbits -= i; + } + if (!b->wds) { + Bfree(b); + ret_zero: + *decpt = 1; + return nrv_alloc("0", rve, 1); + } + + dval(d) = b2d(b, &i); + i = be + bbits - 1; + word0(d) &= Frac_mask1; + word0(d) |= Exp_11; +#ifdef IBM + if ( (j = 11 - hi0bits(word0(d) & Frac_mask)) !=0) + dval(d) /= 1 << j; +#endif + + /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 + * log10(x) = log(x) / log(10) + * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) + * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) + * + * This suggests computing an approximation k to log10(d) by + * + * k = (i - Bias)*0.301029995663981 + * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); + * + * We want k to be too large rather than too small. + * The error in the first-order Taylor series approximation + * is in our favor, so we just round up the constant enough + * to compensate for any error in the multiplication of + * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, + * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, + * adding 1e-13 to the constant term more than suffices. + * Hence we adjust the constant term to 0.1760912590558. + * (We could get a more accurate k by invoking log10, + * but this is probably not worthwhile.) + */ +#ifdef IBM + i <<= 2; + i += j; +#endif + ds = (dval(d)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; + + /* correct assumption about exponent range */ + if ((j = i) < 0) + j = -j; + if ((j -= 1077) > 0) + ds += j * 7e-17; + + k = (int)ds; + if (ds < 0. && ds != k) + k--; /* want k = floor(ds) */ + k_check = 1; +#ifdef IBM + j = be + bbits - 1; + if ( (jj1 = j & 3) !=0) + dval(d) *= 1 << jj1; + word0(d) += j << Exp_shift - 2 & Exp_mask; +#else + word0(d) += (be + bbits - 1) << Exp_shift; +#endif + if (k >= 0 && k <= Ten_pmax) { + if (dval(d) < tens[k]) + k--; + k_check = 0; + } + j = bbits - i - 1; + if (j >= 0) { + b2 = 0; + s2 = j; + } + else { + b2 = -j; + s2 = 0; + } + if (k >= 0) { + b5 = 0; + s5 = k; + s2 += k; + } + else { + b2 -= k; + b5 = -k; + s5 = 0; + } + if (mode < 0 || mode > 9) + mode = 0; + try_quick = 1; + if (mode > 5) { + mode -= 4; + try_quick = 0; + } + leftright = 1; + switch(mode) { + case 0: + case 1: + ilim = ilim1 = -1; + i = (int)(nbits * .30103) + 3; + ndigits = 0; + break; + case 2: + leftright = 0; + /*FALLTHROUGH*/ + case 4: + if (ndigits <= 0) + ndigits = 1; + ilim = ilim1 = i = ndigits; + break; + case 3: + leftright = 0; + /*FALLTHROUGH*/ + case 5: + i = ndigits + k + 1; + ilim = i; + ilim1 = i - 1; + if (i <= 0) + i = 1; + } + s = s0 = rv_alloc((size_t)i); + if (s == NULL) + return NULL; + + if ( (rdir = fpi->rounding - 1) !=0) { + if (rdir < 0) + rdir = 2; + if (kind & STRTOG_Neg) + rdir = 3 - rdir; + } + + /* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */ + + if (ilim >= 0 && ilim <= Quick_max && try_quick && !rdir +#ifndef IMPRECISE_INEXACT + && k == 0 +#endif + ) { + + /* Try to get by with floating-point arithmetic. */ + + i = 0; + d2 = dval(d); +#ifdef IBM + if ( (j = 11 - hi0bits(word0(d) & Frac_mask)) !=0) + dval(d) /= 1 << j; +#endif + k0 = k; + ilim0 = ilim; + ieps = 2; /* conservative */ + if (k > 0) { + ds = tens[k&0xf]; + j = (unsigned int)k >> 4; + if (j & Bletch) { + /* prevent overflows */ + j &= Bletch - 1; + dval(d) /= bigtens[n_bigtens-1]; + ieps++; + } + for(; j; j /= 2, i++) + if (j & 1) { + ieps++; + ds *= bigtens[i]; + } + } + else { + ds = 1.; + if ( (jj1 = -k) !=0) { + dval(d) *= tens[jj1 & 0xf]; + for(j = jj1 >> 4; j; j >>= 1, i++) + if (j & 1) { + ieps++; + dval(d) *= bigtens[i]; + } + } + } + if (k_check && dval(d) < 1. && ilim > 0) { + if (ilim1 <= 0) + goto fast_failed; + ilim = ilim1; + k--; + dval(d) *= 10.; + ieps++; + } + dval(eps) = ieps*dval(d) + 7.; + word0(eps) -= (P-1)*Exp_msk1; + if (ilim == 0) { + S = mhi = 0; + dval(d) -= 5.; + if (dval(d) > dval(eps)) + goto one_digit; + if (dval(d) < -dval(eps)) + goto no_digits; + goto fast_failed; + } +#ifndef No_leftright + if (leftright) { + /* Use Steele & White method of only + * generating digits needed. + */ + dval(eps) = ds*0.5/tens[ilim-1] - dval(eps); + for(i = 0;;) { + L = (Long)(dval(d)/ds); + dval(d) -= L*ds; + *s++ = '0' + (int)L; + if (dval(d) < dval(eps)) { + if (dval(d)) + inex = STRTOG_Inexlo; + goto ret1; + } + if (ds - dval(d) < dval(eps)) + goto bump_up; + if (++i >= ilim) + break; + dval(eps) *= 10.; + dval(d) *= 10.; + } + } + else { +#endif + /* Generate ilim digits, then fix them up. */ + dval(eps) *= tens[ilim-1]; + for(i = 1;; i++, dval(d) *= 10.) { + if ( (L = (Long)(dval(d)/ds)) !=0) + dval(d) -= L*ds; + *s++ = '0' + (int)L; + if (i == ilim) { + ds *= 0.5; + if (dval(d) > ds + dval(eps)) + goto bump_up; + else if (dval(d) < ds - dval(eps)) { + while(*--s == '0'){} + s++; + if (dval(d)) + inex = STRTOG_Inexlo; + goto ret1; + } + break; + } + } +#ifndef No_leftright + } +#endif + fast_failed: + s = s0; + dval(d) = d2; + k = k0; + ilim = ilim0; + } + + /* Do we have a "small" integer? */ + + if (be >= 0 && k <= Int_max) { + /* Yes. */ + ds = tens[k]; + if (ndigits < 0 && ilim <= 0) { + S = mhi = 0; + if (ilim < 0 || dval(d) <= 5*ds) + goto no_digits; + goto one_digit; + } + for(i = 1;; i++, dval(d) *= 10.) { + L = dval(d) / ds; + dval(d) -= L*ds; +#ifdef Check_FLT_ROUNDS + /* If FLT_ROUNDS == 2, L will usually be high by 1 */ + if (dval(d) < 0) { + L--; + dval(d) += ds; + } +#endif + *s++ = '0' + (int)L; + if (dval(d) == 0.) + break; + if (i == ilim) { + if (rdir) { + if (rdir == 1) + goto bump_up; + inex = STRTOG_Inexlo; + goto ret1; + } + dval(d) += dval(d); + if (dval(d) > ds || (dval(d) == ds && L & 1)) { + bump_up: + inex = STRTOG_Inexhi; + while(*--s == '9') + if (s == s0) { + k++; + *s = '0'; + break; + } + ++*s++; + } + else + inex = STRTOG_Inexlo; + break; + } + } + goto ret1; + } + + m2 = b2; + m5 = b5; + mhi = mlo = 0; + if (leftright) { + if (mode < 2) { + i = nbits - bbits; + if (be - i++ < fpi->emin) + /* denormal */ + i = be - fpi->emin + 1; + } + else { + j = ilim - 1; + if (m5 >= j) + m5 -= j; + else { + s5 += j -= m5; + b5 += j; + m5 = 0; + } + if ((i = ilim) < 0) { + m2 -= i; + i = 0; + } + } + b2 += i; + s2 += i; + mhi = i2b(1); + } + if (m2 > 0 && s2 > 0) { + i = m2 < s2 ? m2 : s2; + b2 -= i; + m2 -= i; + s2 -= i; + } + if (b5 > 0) { + if (leftright) { + if (m5 > 0) { + mhi = pow5mult(mhi, m5); + if (mhi == NULL) + return NULL; + b1 = mult(mhi, b); + if (b1 == NULL) + return NULL; + Bfree(b); + b = b1; + } + if ( (j = b5 - m5) !=0) { + b = pow5mult(b, j); + if (b == NULL) + return NULL; + } + } + else { + b = pow5mult(b, b5); + if (b == NULL) + return NULL; + } + } + S = i2b(1); + if (S == NULL) + return NULL; + if (s5 > 0) { + S = pow5mult(S, s5); + if (S == NULL) + return NULL; + } + + /* Check for special case that d is a normalized power of 2. */ + + spec_case = 0; + if (mode < 2) { + if (bbits == 1 && be0 > fpi->emin + 1) { + /* The special case */ + b2++; + s2++; + spec_case = 1; + } + } + + /* Arrange for convenient computation of quotients: + * shift left if necessary so divisor has 4 leading 0 bits. + * + * Perhaps we should just compute leading 28 bits of S once + * and for all and pass them and a shift to quorem, so it + * can do shifts and ors to compute the numerator for q. + */ +#ifdef Pack_32 + if ( (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) !=0) + i = 32 - i; +#else + if ( (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) !=0) + i = 16 - i; +#endif + if (i > 4) { + i -= 4; + b2 += i; + m2 += i; + s2 += i; + } + else if (i < 4) { + i += 28; + b2 += i; + m2 += i; + s2 += i; + } + if (b2 > 0) + b = lshift(b, b2); + if (s2 > 0) + S = lshift(S, s2); + if (k_check) { + if (cmp(b,S) < 0) { + k--; + b = multadd(b, 10, 0); /* we botched the k estimate */ + if (b == NULL) + return NULL; + if (leftright) { + mhi = multadd(mhi, 10, 0); + if (mhi == NULL) + return NULL; + } + ilim = ilim1; + } + } + if (ilim <= 0 && mode > 2) { + if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { + /* no digits, fcvt style */ + no_digits: + k = -1 - ndigits; + inex = STRTOG_Inexlo; + goto ret; + } + one_digit: + inex = STRTOG_Inexhi; + *s++ = '1'; + k++; + goto ret; + } + if (leftright) { + if (m2 > 0) { + mhi = lshift(mhi, m2); + if (mhi == NULL) + return NULL; + } + + /* Compute mlo -- check for special case + * that d is a normalized power of 2. + */ + + mlo = mhi; + if (spec_case) { + mhi = Balloc(mhi->k); + if (mhi == NULL) + return NULL; + Bcopy(mhi, mlo); + mhi = lshift(mhi, 1); + if (mhi == NULL) + return NULL; + } + + for(i = 1;;i++) { + dig = quorem(b,S) + '0'; + /* Do we yet have the shortest decimal string + * that will round to d? + */ + j = cmp(b, mlo); + delta = diff(S, mhi); + if (delta == NULL) + return NULL; + jj1 = delta->sign ? 1 : cmp(b, delta); + Bfree(delta); +#ifndef ROUND_BIASED + if (jj1 == 0 && !mode && !(bits[0] & 1) && !rdir) { + if (dig == '9') + goto round_9_up; + if (j <= 0) { + if (b->wds > 1 || b->x[0]) + inex = STRTOG_Inexlo; + } + else { + dig++; + inex = STRTOG_Inexhi; + } + *s++ = dig; + goto ret; + } +#endif + if (j < 0 || (j == 0 && !mode +#ifndef ROUND_BIASED + && !(bits[0] & 1) +#endif + )) { + if (rdir && (b->wds > 1 || b->x[0])) { + if (rdir == 2) { + inex = STRTOG_Inexlo; + goto accept; + } + while (cmp(S,mhi) > 0) { + *s++ = dig; + mhi1 = multadd(mhi, 10, 0); + if (mhi1 == NULL) + return NULL; + if (mlo == mhi) + mlo = mhi1; + mhi = mhi1; + b = multadd(b, 10, 0); + if (b == NULL) + return NULL; + dig = quorem(b,S) + '0'; + } + if (dig++ == '9') + goto round_9_up; + inex = STRTOG_Inexhi; + goto accept; + } + if (jj1 > 0) { + b = lshift(b, 1); + if (b == NULL) + return NULL; + jj1 = cmp(b, S); + if ((jj1 > 0 || (jj1 == 0 && dig & 1)) + && dig++ == '9') + goto round_9_up; + inex = STRTOG_Inexhi; + } + if (b->wds > 1 || b->x[0]) + inex = STRTOG_Inexlo; + accept: + *s++ = dig; + goto ret; + } + if (jj1 > 0 && rdir != 2) { + if (dig == '9') { /* possible if i == 1 */ + round_9_up: + *s++ = '9'; + inex = STRTOG_Inexhi; + goto roundoff; + } + inex = STRTOG_Inexhi; + *s++ = dig + 1; + goto ret; + } + *s++ = dig; + if (i == ilim) + break; + b = multadd(b, 10, 0); + if (b == NULL) + return NULL; + if (mlo == mhi) { + mlo = mhi = multadd(mhi, 10, 0); + if (mlo == NULL) + return NULL; + } + else { + mlo = multadd(mlo, 10, 0); + if (mlo == NULL) + return NULL; + mhi = multadd(mhi, 10, 0); + if (mhi == NULL) + return NULL; + } + } + } + else + for(i = 1;; i++) { + *s++ = dig = quorem(b,S) + '0'; + if (i >= ilim) + break; + b = multadd(b, 10, 0); + if (b == NULL) + return NULL; + } + + /* Round off last digit */ + + if (rdir) { + if (rdir == 2 || (b->wds <= 1 && !b->x[0])) + goto chopzeros; + goto roundoff; + } + b = lshift(b, 1); + if (b == NULL) + return NULL; + j = cmp(b, S); + if (j > 0 || (j == 0 && dig & 1)) { + roundoff: + inex = STRTOG_Inexhi; + while(*--s == '9') + if (s == s0) { + k++; + *s++ = '1'; + goto ret; + } + ++*s++; + } + else { + chopzeros: + if (b->wds > 1 || b->x[0]) + inex = STRTOG_Inexlo; + while(*--s == '0'){} + s++; + } + ret: + Bfree(S); + if (mhi) { + if (mlo && mlo != mhi) + Bfree(mlo); + Bfree(mhi); + } + ret1: + Bfree(b); + *s = 0; + *decpt = k + 1; + if (rve) + *rve = s; + *kindp |= inex; + return s0; + } diff --git a/lib/nbsd_libc/gdtoa/gdtoa.h b/lib/nbsd_libc/gdtoa/gdtoa.h new file mode 100644 index 000000000..63840250f --- /dev/null +++ b/lib/nbsd_libc/gdtoa/gdtoa.h @@ -0,0 +1,175 @@ +/* $NetBSD: gdtoa.h,v 1.8 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#ifndef GDTOA_H_INCLUDED +#define GDTOA_H_INCLUDED + +#include "arith.h" + +#ifndef Long +#define Long int32_t +#endif +#ifndef ULong +#define ULong uint32_t +#endif +#ifndef UShort +#define UShort uint16_t +#endif + +#ifndef ANSI +#ifdef KR_headers +#define ANSI(x) () +#define Void /*nothing*/ +#else +#define ANSI(x) x +#define Void void +#endif +#endif /* ANSI */ + +#ifndef CONST +#ifdef KR_headers +#define CONST /* blank */ +#else +#define CONST const +#endif +#endif /* CONST */ + + enum { /* return values from strtodg */ + STRTOG_Zero = 0, + STRTOG_Normal = 1, + STRTOG_Denormal = 2, + STRTOG_Infinite = 3, + STRTOG_NaN = 4, + STRTOG_NaNbits = 5, + STRTOG_NoNumber = 6, + STRTOG_Retmask = 7, + + /* The following may be or-ed into one of the above values. */ + + STRTOG_Neg = 0x08, + STRTOG_Inexlo = 0x10, + STRTOG_Inexhi = 0x20, + STRTOG_Inexact = 0x30, + STRTOG_Underflow= 0x40, + STRTOG_Overflow = 0x80, + STRTOG_NoMemory = 0x100 + }; + + typedef struct +FPI { + int nbits; + int emin; + int emax; + int rounding; + int sudden_underflow; + } FPI; + +enum { /* FPI.rounding values: same as FLT_ROUNDS */ + FPI_Round_zero = 0, + FPI_Round_near = 1, + FPI_Round_up = 2, + FPI_Round_down = 3 + }; + +#ifdef __cplusplus +extern "C" { +#endif + +#define dtoa __dtoa +#define gdtoa __gdtoa +#define ldtoa __ldtoa +#define hldtoa __hldtoa +#define hdtoa __hdtoa +#define freedtoa __freedtoa +#define strtodg __strtodg_D2A +#define strtopQ __strtopQ_D2A +#define strtopx __strtopx_D2A +#define strtopxL __strtopxL_D2A +#define strtord __strtord_D2A + +extern char* dtoa ANSI((double d, int mode, int ndigits, int *decpt, + int *sign, char **rve)); +extern char* hdtoa ANSI((double d, const char *xdigs, int ndigits, int *decpt, + int *sign, char **rve)); +extern char* ldtoa ANSI((long double *ld, int mode, int ndigits, int *decpt, + int *sign, char **rve)); +extern char* hldtoa ANSI((long double e, const char *xdigs, int ndigits, + int *decpt, int *sign, char **rve)); + +extern char* gdtoa ANSI((FPI *fpi, int be, ULong *bits, int *kindp, + int mode, int ndigits, int *decpt, char **rve)); +extern void freedtoa ANSI((char*)); +extern float strtof ANSI((CONST char *, char **)); +extern double strtod ANSI((CONST char *, char **)); +extern int strtodg ANSI((CONST char*, char**, CONST FPI*, Long*, ULong*)); + +extern char* g_ddfmt ANSI((char*, double*, int, unsigned)); +extern char* g_dfmt ANSI((char*, double*, int, unsigned)); +extern char* g_ffmt ANSI((char*, float*, int, unsigned)); +extern char* g_Qfmt ANSI((char*, void*, int, unsigned)); +extern char* g_xfmt ANSI((char*, void*, int, unsigned)); +extern char* g_xLfmt ANSI((char*, void*, int, unsigned)); + +extern int strtoId ANSI((CONST char*, char**, double*, double*)); +extern int strtoIdd ANSI((CONST char*, char**, double*, double*)); +extern int strtoIf ANSI((CONST char*, char**, float*, float*)); +extern int strtoIQ ANSI((CONST char*, char**, void*, void*)); +extern int strtoIx ANSI((CONST char*, char**, void*, void*)); +extern int strtoIxL ANSI((CONST char*, char**, void*, void*)); +extern int strtord ANSI((CONST char*, char**, int, double*)); +extern int strtordd ANSI((CONST char*, char**, int, double*)); +extern int strtorf ANSI((CONST char*, char**, int, float*)); +extern int strtorQ ANSI((CONST char*, char**, int, void*)); +extern int strtorx ANSI((CONST char*, char**, int, void*)); +extern int strtorxL ANSI((CONST char*, char**, int, void*)); +#if 1 +extern int strtodI ANSI((CONST char*, char**, double*)); +extern int strtopd ANSI((CONST char*, char**, double*)); +extern int strtopdd ANSI((CONST char*, char**, double*)); +extern int strtopf ANSI((CONST char*, char**, float*)); +extern int strtopQ ANSI((CONST char*, char**, void*)); +extern int strtopx ANSI((CONST char*, char**, void*)); +extern int strtopxL ANSI((CONST char*, char**, void*)); +#else +#define strtopd(s,se,x) strtord(s,se,1,x) +#define strtopdd(s,se,x) strtordd(s,se,1,x) +#define strtopf(s,se,x) strtorf(s,se,1,x) +#define strtopQ(s,se,x) strtorQ(s,se,1,x) +#define strtopx(s,se,x) strtorx(s,se,1,x) +#define strtopxL(s,se,x) strtorxL(s,se,1,x) +#endif + +#ifdef __cplusplus +} +#endif +#endif /* GDTOA_H_INCLUDED */ diff --git a/lib/nbsd_libc/gdtoa/gdtoa_locks.c b/lib/nbsd_libc/gdtoa/gdtoa_locks.c new file mode 100644 index 000000000..81f521ce8 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/gdtoa_locks.c @@ -0,0 +1,10 @@ +/* $NetBSD: gdtoa_locks.c,v 1.1 2006/01/25 15:36:13 kleink Exp $ */ + +/* + * Written by Klaus Klein , November 16, 2005. + * Public domain. + */ + +#include "gdtoaimp.h" + +mutex_t __gdtoa_locks[2] = { MUTEX_INITIALIZER, MUTEX_INITIALIZER }; diff --git a/lib/nbsd_libc/gdtoa/gdtoaimp.h b/lib/nbsd_libc/gdtoa/gdtoaimp.h new file mode 100644 index 000000000..b897f0084 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/gdtoaimp.h @@ -0,0 +1,653 @@ +/* $NetBSD: gdtoaimp.h,v 1.8 2011/01/21 23:36:49 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998-2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* This is a variation on dtoa.c that converts arbitary binary + floating-point formats to and from decimal notation. It uses + double-precision arithmetic internally, so there are still + various #ifdefs that adapt the calculations to the native + double-precision arithmetic (any of IEEE, VAX D_floating, + or IBM mainframe arithmetic). + + Please send bug reports to David M. Gay (dmg at acm dot org, + with " at " changed at "@" and " dot " changed to "."). + */ + +/* On a machine with IEEE extended-precision registers, it is + * necessary to specify double-precision (53-bit) rounding precision + * before invoking strtod or dtoa. If the machine uses (the equivalent + * of) Intel 80x87 arithmetic, the call + * _control87(PC_53, MCW_PC); + * does this with many compilers. Whether this or another call is + * appropriate depends on the compiler; for this to work, it may be + * necessary to #include "float.h" or another system-dependent header + * file. + */ + +/* strtod for IEEE-, VAX-, and IBM-arithmetic machines. + * + * This strtod returns a nearest machine number to the input decimal + * string (or sets errno to ERANGE). With IEEE arithmetic, ties are + * broken by the IEEE round-even rule. Otherwise ties are broken by + * biased rounding (add half and chop). + * + * Inspired loosely by William D. Clinger's paper "How to Read Floating + * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 112-126]. + * + * Modifications: + * + * 1. We only require IEEE, IBM, or VAX double-precision + * arithmetic (not IEEE double-extended). + * 2. We get by with floating-point arithmetic in a case that + * Clinger missed -- when we're computing d * 10^n + * for a small integer d and the integer n is not too + * much larger than 22 (the maximum integer k for which + * we can represent 10^k exactly), we may be able to + * compute (d*10^k) * 10^(e-k) with just one roundoff. + * 3. Rather than a bit-at-a-time adjustment of the binary + * result in the hard case, we use floating-point + * arithmetic to determine the adjustment to within + * one bit; only in really hard cases do we need to + * compute a second residual. + * 4. Because of 3., we don't need a large table of powers of 10 + * for ten-to-e (just some small tables, e.g. of 10^k + * for 0 <= k <= 22). + */ + +/* + * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least + * significant byte has the lowest address. + * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most + * significant byte has the lowest address. + * #define Long int on machines with 32-bit ints and 64-bit longs. + * #define Sudden_Underflow for IEEE-format machines without gradual + * underflow (i.e., that flush to zero on underflow). + * #define IBM for IBM mainframe-style floating-point arithmetic. + * #define VAX for VAX-style floating-point arithmetic (D_floating). + * #define No_leftright to omit left-right logic in fast floating-point + * computation of dtoa. + * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3. + * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines + * that use extended-precision instructions to compute rounded + * products and quotients) with IBM. + * #define ROUND_BIASED for IEEE-format with biased rounding. + * #define Inaccurate_Divide for IEEE-format with correctly rounded + * products but inaccurate quotients, e.g., for Intel i860. + * #define NO_LONG_LONG on machines that do not have a "long long" + * integer type (of >= 64 bits). On such machines, you can + * #define Just_16 to store 16 bits per 32-bit Long when doing + * high-precision integer arithmetic. Whether this speeds things + * up or slows things down depends on the machine and the number + * being converted. If long long is available and the name is + * something other than "long long", #define Llong to be the name, + * and if "unsigned Llong" does not work as an unsigned version of + * Llong, #define #ULLong to be the corresponding unsigned type. + * #define KR_headers for old-style C function headers. + * #define Bad_float_h if your system lacks a float.h or if it does not + * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, + * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. + * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) + * if memory is available and otherwise does something you deem + * appropriate. If MALLOC is undefined, malloc will be invoked + * directly -- and assumed always to succeed. + * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making + * memory allocations from a private pool of memory when possible. + * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes, + * unless #defined to be a different length. This default length + * suffices to get rid of MALLOC calls except for unusual cases, + * such as decimal-to-binary conversion of a very long string of + * digits. When converting IEEE double precision values, the + * longest string gdtoa can return is about 751 bytes long. For + * conversions by strtod of strings of 800 digits and all gdtoa + * conversions of IEEE doubles in single-threaded executions with + * 8-byte pointers, PRIVATE_MEM >= 7400 appears to suffice; with + * 4-byte pointers, PRIVATE_MEM >= 7112 appears adequate. + * #define INFNAN_CHECK on IEEE systems to cause strtod to check for + * Infinity and NaN (case insensitively). + * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined, + * strtodg also accepts (case insensitively) strings of the form + * NaN(x), where x is a string of hexadecimal digits and spaces; + * if there is only one string of hexadecimal digits, it is taken + * for the fraction bits of the resulting NaN; if there are two or + * more strings of hexadecimal digits, each string is assigned + * to the next available sequence of 32-bit words of fractions + * bits (starting with the most significant), right-aligned in + * each sequence. + * #define MULTIPLE_THREADS if the system offers preemptively scheduled + * multiple threads. In this case, you must provide (or suitably + * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed + * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed + * in pow5mult, ensures lazy evaluation of only one copy of high + * powers of 5; omitting this lock would introduce a small + * probability of wasting memory, but would otherwise be harmless.) + * You must also invoke freedtoa(s) to free the value s returned by + * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. + * #define IMPRECISE_INEXACT if you do not care about the setting of + * the STRTOG_Inexact bits in the special case of doing IEEE double + * precision conversions (which could also be done by the strtog in + * dtoa.c). + * #define NO_HEX_FP to disable recognition of C9x's hexadecimal + * floating-point constants. + * #define -DNO_ERRNO to suppress setting errno (in strtod.c and + * strtodg.c). + * #define NO_STRING_H to use private versions of memcpy. + * On some K&R systems, it may also be necessary to + * #define DECLARE_SIZE_T in this case. + * #define YES_ALIAS to permit aliasing certain double values with + * arrays of ULongs. This leads to slightly better code with + * some compilers and was always used prior to 19990916, but it + * is not strictly legal and can cause trouble with aggressively + * optimizing compilers (e.g., gcc 2.95.1 under -O2). + * #define USE_LOCALE to use the current locale's decimal_point value. + */ + +/* #define IEEE_{BIG,LITTLE}_ENDIAN in ${ARCHDIR}/gdtoa/arith.h */ + +#include +#define Short int16_t +#define UShort uint16_t +#define Long int32_t +#define ULong uint32_t +#define LLong int64_t +#define ULLong uint64_t + +#define INFNAN_CHECK +#ifdef _REENTRANT +#define MULTIPLE_THREADS +#endif +#define USE_LOCALE + +#ifndef GDTOAIMP_H_INCLUDED +#define GDTOAIMP_H_INCLUDED +#include "gdtoa.h" +#include "gd_qnan.h" + +#ifdef DEBUG +#include "stdio.h" +#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} +#endif + +#include "stdlib.h" +#include "string.h" + +#ifdef KR_headers +#define Char char +#else +#define Char void +#endif + +#ifdef MALLOC +extern Char *MALLOC ANSI((size_t)); +#else +#define MALLOC malloc +#endif + +#undef IEEE_Arith +#undef Avoid_Underflow +#ifdef IEEE_BIG_ENDIAN +#define IEEE_Arith +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define IEEE_Arith +#endif + +#include "errno.h" +#ifdef Bad_float_h + +#ifdef IEEE_Arith +#define DBL_DIG 15 +#define DBL_MAX_10_EXP 308 +#define DBL_MAX_EXP 1024 +#define FLT_RADIX 2 +#define DBL_MAX 1.7976931348623157e+308 +#endif + +#ifdef IBM +#define DBL_DIG 16 +#define DBL_MAX_10_EXP 75 +#define DBL_MAX_EXP 63 +#define FLT_RADIX 16 +#define DBL_MAX 7.2370055773322621e+75 +#endif + +#ifdef VAX +#define DBL_DIG 16 +#define DBL_MAX_10_EXP 38 +#define DBL_MAX_EXP 127 +#define FLT_RADIX 2 +#define DBL_MAX 1.7014118346046923e+38 +#define n_bigtens 2 +#endif + +#ifndef LONG_MAX +#define LONG_MAX 2147483647 +#endif + +#else /* ifndef Bad_float_h */ +#include "float.h" +#endif /* Bad_float_h */ + +#ifdef IEEE_Arith +#define Scale_Bit 0x10 +#define n_bigtens 5 +#endif + +#ifdef IBM +#define n_bigtens 3 +#endif + +#ifdef VAX +#define n_bigtens 2 +#endif + +#include "math.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1 +Exactly one of IEEE_LITTLE_ENDIAN, IEEE_BIG_ENDIAN, VAX, or IBM should be defined. +#endif + +typedef union { double d; ULong L[2]; } __attribute__((__may_alias__)) U; + +#ifdef YES_ALIAS +#define dval(x) x +#ifdef IEEE_LITTLE_ENDIAN +#define word0(x) ((ULong *)&x)[1] +#define word1(x) ((ULong *)&x)[0] +#else +#define word0(x) ((ULong *)&x)[0] +#define word1(x) ((ULong *)&x)[1] +#endif +#else /* !YES_ALIAS */ +#ifdef IEEE_LITTLE_ENDIAN +#define word0(x) ( /* LINTED */ (U*)&x)->L[1] +#define word1(x) ( /* LINTED */ (U*)&x)->L[0] +#else +#define word0(x) ( /* LINTED */ (U*)&x)->L[0] +#define word1(x) ( /* LINTED */ (U*)&x)->L[1] +#endif +#define dval(x) ( /* LINTED */ (U*)&x)->d +#endif /* YES_ALIAS */ + +/* The following definition of Storeinc is appropriate for MIPS processors. + * An alternative that might be better on some machines is + * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) + */ +#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) +#define Storeinc(a,b,c) \ + (((unsigned short *)(void *)a)[1] = (unsigned short)b, \ + ((unsigned short *)(void *)a)[0] = (unsigned short)c, \ + a++) +#else +#define Storeinc(a,b,c) \ + (((unsigned short *)(void *)a)[0] = (unsigned short)b, \ + ((unsigned short *)(void *)a)[1] = (unsigned short)c, \ + a++) +#endif + +/* #define P DBL_MANT_DIG */ +/* Ten_pmax = floor(P*log(2)/log(5)) */ +/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ +/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ +/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ + +#ifdef IEEE_Arith +#define Exp_shift 20 +#define Exp_shift1 20 +#define Exp_msk1 0x100000 +#define Exp_msk11 0x100000 +#define Exp_mask 0x7ff00000 +#define P 53 +#define Bias 1023 +#define Emin (-1022) +#define Exp_1 0x3ff00000 +#define Exp_11 0x3ff00000 +#define Ebits 11 +#define Frac_mask 0xfffff +#define Frac_mask1 0xfffff +#define Ten_pmax 22 +#define Bletch 0x10 +#define Bndry_mask 0xfffff +#define Bndry_mask1 0xfffff +#define LSB 1 +#define Sign_bit 0x80000000 +#define Log2P 1 +#define Tiny0 0 +#define Tiny1 1 +#define Quick_max 14 +#define Int_max 14 + +#ifndef Flt_Rounds +#ifdef FLT_ROUNDS +#define Flt_Rounds FLT_ROUNDS +#else +#define Flt_Rounds 1 +#endif +#endif /*Flt_Rounds*/ + +#else /* ifndef IEEE_Arith */ +#undef Sudden_Underflow +#define Sudden_Underflow +#ifdef IBM +#undef Flt_Rounds +#define Flt_Rounds 0 +#define Exp_shift 24 +#define Exp_shift1 24 +#define Exp_msk1 0x1000000 +#define Exp_msk11 0x1000000 +#define Exp_mask 0x7f000000 +#define P 14 +#define Bias 65 +#define Exp_1 0x41000000 +#define Exp_11 0x41000000 +#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ +#define Frac_mask 0xffffff +#define Frac_mask1 0xffffff +#define Bletch 4 +#define Ten_pmax 22 +#define Bndry_mask 0xefffff +#define Bndry_mask1 0xffffff +#define LSB 1 +#define Sign_bit 0x80000000 +#define Log2P 4 +#define Tiny0 0x100000 +#define Tiny1 0 +#define Quick_max 14 +#define Int_max 15 +#else /* VAX */ +#undef Flt_Rounds +#define Flt_Rounds 1 +#define Exp_shift 23 +#define Exp_shift1 7 +#define Exp_msk1 0x80 +#define Exp_msk11 0x800000 +#define Exp_mask 0x7f80 +#define P 56 +#define Bias 129 +#define Exp_1 0x40800000 +#define Exp_11 0x4080 +#define Ebits 8 +#define Frac_mask 0x7fffff +#define Frac_mask1 0xffff007f +#define Ten_pmax 24 +#define Bletch 2 +#define Bndry_mask 0xffff007f +#define Bndry_mask1 0xffff007f +#define LSB 0x10000 +#define Sign_bit 0x8000 +#define Log2P 1 +#define Tiny0 0x80 +#define Tiny1 0 +#define Quick_max 15 +#define Int_max 15 +#endif /* IBM, VAX */ +#endif /* IEEE_Arith */ + +#ifndef IEEE_Arith +#define ROUND_BIASED +#endif + +#ifdef RND_PRODQUOT +#define rounded_product(a,b) a = rnd_prod(a, b) +#define rounded_quotient(a,b) a = rnd_quot(a, b) +#ifdef KR_headers +extern double rnd_prod(), rnd_quot(); +#else +extern double rnd_prod(double, double), rnd_quot(double, double); +#endif +#else +#define rounded_product(a,b) a *= b +#define rounded_quotient(a,b) a /= b +#endif + +#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) +#define Big1 0xffffffff + +#undef Pack_16 +#ifndef Pack_32 +#define Pack_32 +#endif + +#ifdef NO_LONG_LONG +#undef ULLong +#ifdef Just_16 +#undef Pack_32 +#define Pack_16 +/* When Pack_32 is not defined, we store 16 bits per 32-bit Long. + * This makes some inner loops simpler and sometimes saves work + * during multiplications, but it often seems to make things slightly + * slower. Hence the default is now to store 32 bits per Long. + */ +#endif +#else /* long long available */ +#ifndef Llong +#define Llong long long +#endif +#ifndef ULLong +#define ULLong unsigned Llong +#endif +#endif /* NO_LONG_LONG */ + +#ifdef Pack_32 +#define ULbits 32 +#define kshift 5 +#define kmask 31 +#define ALL_ON 0xffffffff +#else +#define ULbits 16 +#define kshift 4 +#define kmask 15 +#define ALL_ON 0xffff +#endif + +#ifndef MULTIPLE_THREADS +#define ACQUIRE_DTOA_LOCK(n) /*nothing*/ +#define FREE_DTOA_LOCK(n) /*nothing*/ +#else +#include "reentrant.h" + +extern mutex_t __gdtoa_locks[2]; + +#define ACQUIRE_DTOA_LOCK(n) \ + do { \ + if (__isthreaded) \ + mutex_lock(&__gdtoa_locks[n]); \ + } while (/* CONSTCOND */ 0) +#define FREE_DTOA_LOCK(n) \ + do { \ + if (__isthreaded) \ + mutex_unlock(&__gdtoa_locks[n]); \ + } while (/* CONSTCOND */ 0) +#endif + +#define Kmax (sizeof(size_t) << 3) + + struct +Bigint { + struct Bigint *next; + int k, maxwds, sign, wds; + ULong x[1]; + }; + + typedef struct Bigint Bigint; + +#ifdef NO_STRING_H +#ifdef DECLARE_SIZE_T +typedef unsigned int size_t; +#endif +extern void memcpy_D2A ANSI((void*, const void*, size_t)); +#define Bcopy(x,y) memcpy_D2A(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int)) +#else /* !NO_STRING_H */ +#define Bcopy(x,y) memcpy(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int)) +#endif /* NO_STRING_H */ + +#define Balloc __Balloc_D2A +#define Bfree __Bfree_D2A +#define ULtoQ __ULtoQ_D2A +#define ULtof __ULtof_D2A +#define ULtod __ULtod_D2A +#define ULtodd __ULtodd_D2A +#define ULtox __ULtox_D2A +#define ULtoxL __ULtoxL_D2A +#define any_on __any_on_D2A +#define b2d __b2d_D2A +#define bigtens __bigtens_D2A +#define cmp __cmp_D2A +#define copybits __copybits_D2A +#define d2b __d2b_D2A +#define decrement __decrement_D2A +#define diff __diff_D2A +#define dtoa_result __dtoa_result_D2A +#define g__fmt __g__fmt_D2A +#define gethex __gethex_D2A +#define hexdig __hexdig_D2A +#define hexdig_init_D2A __hexdig_init_D2A +#define hexnan __hexnan_D2A +#define hi0bits __hi0bits_D2A +#define hi0bits_D2A __hi0bits_D2A +#define i2b __i2b_D2A +#define increment __increment_D2A +#define lo0bits __lo0bits_D2A +#define lshift __lshift_D2A +#define match __match_D2A +#define mult __mult_D2A +#define multadd __multadd_D2A +#define nrv_alloc __nrv_alloc_D2A +#define pow5mult __pow5mult_D2A +#define quorem __quorem_D2A +#define ratio __ratio_D2A +#define rshift __rshift_D2A +#define rv_alloc __rv_alloc_D2A +#define s2b __s2b_D2A +#define set_ones __set_ones_D2A +#define strcp __strcp_D2A +#define strcp_D2A __strcp_D2A +#define strtoIg __strtoIg_D2A +#define sum __sum_D2A +#define tens __tens_D2A +#define tinytens __tinytens_D2A +#define tinytens __tinytens_D2A +#define trailz __trailz_D2A +#define ulp __ulp_D2A + + extern char *dtoa_result; + extern CONST double bigtens[], tens[], tinytens[]; + extern unsigned char hexdig[]; + + extern Bigint *Balloc ANSI((int)); + extern void Bfree ANSI((Bigint*)); + extern void ULtof ANSI((ULong*, ULong*, Long, int)); + extern void ULtod ANSI((ULong*, ULong*, Long, int)); + extern void ULtodd ANSI((ULong*, ULong*, Long, int)); + extern void ULtoQ ANSI((ULong*, ULong*, Long, int)); + extern void ULtox ANSI((UShort*, ULong*, Long, int)); + extern void ULtoxL ANSI((ULong*, ULong*, Long, int)); + extern ULong any_on ANSI((Bigint*, int)); + extern double b2d ANSI((Bigint*, int*)); + extern int cmp ANSI((Bigint*, Bigint*)); + extern void copybits ANSI((ULong*, int, Bigint*)); + extern Bigint *d2b ANSI((double, int*, int*)); + extern int decrement ANSI((Bigint*)); + extern Bigint *diff ANSI((Bigint*, Bigint*)); + extern char *dtoa ANSI((double d, int mode, int ndigits, + int *decpt, int *sign, char **rve)); + extern char *g__fmt ANSI((char*, char*, char*, int, ULong)); + extern int gethex ANSI((CONST char**, CONST FPI*, Long*, Bigint**, int)); + extern void hexdig_init_D2A(Void); + extern int hexnan ANSI((CONST char**, CONST FPI*, ULong*)); + extern int hi0bits_D2A ANSI((ULong)); + extern Bigint *i2b ANSI((int)); + extern Bigint *increment ANSI((Bigint*)); + extern int lo0bits ANSI((ULong*)); + extern Bigint *lshift ANSI((Bigint*, int)); + extern int match ANSI((CONST char**, CONST char*)); + extern Bigint *mult ANSI((Bigint*, Bigint*)); + extern Bigint *multadd ANSI((Bigint*, int, int)); + extern char *nrv_alloc ANSI((CONST char*, char **, size_t)); + extern Bigint *pow5mult ANSI((Bigint*, int)); + extern int quorem ANSI((Bigint*, Bigint*)); + extern double ratio ANSI((Bigint*, Bigint*)); + extern void rshift ANSI((Bigint*, int)); + extern char *rv_alloc ANSI((size_t)); + extern Bigint *s2b ANSI((CONST char*, int, int, ULong)); + extern Bigint *set_ones ANSI((Bigint*, int)); + extern char *strcp ANSI((char*, const char*)); + extern int strtoIg ANSI((CONST char*, char**, FPI*, Long*, Bigint**, int*)); + extern double strtod ANSI((const char *s00, char **se)); + extern Bigint *sum ANSI((Bigint*, Bigint*)); + extern int trailz ANSI((CONST Bigint*)); + extern double ulp ANSI((double)); + +#ifdef __cplusplus +} +#endif +/* + * NAN_WORD0 and NAN_WORD1 are only referenced in strtod.c. Prior to + * 20050115, they used to be hard-wired here (to 0x7ff80000 and 0, + * respectively), but now are determined by compiling and running + * qnan.c to generate gd_qnan.h, which specifies d_QNAN0 and d_QNAN1. + * Formerly gdtoaimp.h recommended supplying suitable -DNAN_WORD0=... + * and -DNAN_WORD1=... values if necessary. This should still work. + * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.) + */ +#ifdef IEEE_Arith +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#ifndef NAN_WORD0 +#define NAN_WORD0 d_QNAN0 +#endif +#ifndef NAN_WORD1 +#define NAN_WORD1 d_QNAN1 +#endif +#else +#define _0 1 +#define _1 0 +#ifndef NAN_WORD0 +#define NAN_WORD0 d_QNAN1 +#endif +#ifndef NAN_WORD1 +#define NAN_WORD1 d_QNAN0 +#endif +#endif +#else +#undef INFNAN_CHECK +#endif + +#undef SI +#ifdef Sudden_Underflow +#define SI 1 +#else +#define SI 0 +#endif + +#endif /* GDTOAIMP_H_INCLUDED */ diff --git a/lib/nbsd_libc/gdtoa/gethex.c b/lib/nbsd_libc/gdtoa/gethex.c new file mode 100644 index 000000000..a63ce977f --- /dev/null +++ b/lib/nbsd_libc/gdtoa/gethex.c @@ -0,0 +1,253 @@ +/* $NetBSD: gethex.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#ifdef USE_LOCALE +#include "locale.h" +#endif + + int +#ifdef KR_headers +gethex(sp, fpi, expt, bp, sign) + CONST char **sp; CONST FPI *fpi; Long *expt; Bigint **bp; int sign; +#else +gethex( CONST char **sp, CONST FPI *fpi, Long *expt, Bigint **bp, int sign) +#endif +{ + Bigint *b; + CONST unsigned char *decpt, *s0, *s, *s1; + int esign, havedig, irv, k, n, nbits, up, zret; + ULong L, lostbits, *x; + Long e, e1; +#ifdef USE_LOCALE + unsigned char decimalpoint = *localeconv()->decimal_point; +#else +#define decimalpoint '.' +#endif + + if (!hexdig['0']) + hexdig_init_D2A(); + havedig = 0; + s0 = *(CONST unsigned char **)sp + 2; + while(s0[havedig] == '0') + havedig++; + s0 += havedig; + s = s0; + decpt = 0; + zret = 0; + e = 0; + if (!hexdig[*s]) { + zret = 1; + if (*s != decimalpoint) + goto pcheck; + decpt = ++s; + if (!hexdig[*s]) + goto pcheck; + while(*s == '0') + s++; + if (hexdig[*s]) + zret = 0; + havedig = 1; + s0 = s; + } + while(hexdig[*s]) + s++; + if (*s == decimalpoint && !decpt) { + decpt = ++s; + while(hexdig[*s]) + s++; + } + if (decpt) + e = -(((Long)(s-decpt)) << 2); + pcheck: + s1 = s; + switch(*s) { + case 'p': + case 'P': + esign = 0; + switch(*++s) { + case '-': + esign = 1; + /* FALLTHROUGH */ + case '+': + s++; + } + if ((n = hexdig[*s]) == 0 || n > 0x19) { + s = s1; + break; + } + e1 = n - 0x10; + while((n = hexdig[*++s]) !=0 && n <= 0x19) + e1 = 10*e1 + n - 0x10; + if (esign) + e1 = -e1; + e += e1; + } + *sp = __UNCONST(s); + if (zret) + return havedig ? STRTOG_Zero : STRTOG_NoNumber; + n = s1 - s0 - 1; + for(k = 0; n > 7; n = (unsigned int)n >> 1) + k++; + b = Balloc(k); + if (b == NULL) + return STRTOG_NoMemory; + x = b->x; + n = 0; + L = 0; + while(s1 > s0) { + if (*--s1 == decimalpoint) + continue; + if (n == 32) { + *x++ = L; + L = 0; + n = 0; + } + L |= (hexdig[*s1] & 0x0f) << n; + n += 4; + } + *x++ = L; + b->wds = n = x - b->x; + n = 32*n - hi0bits(L); + nbits = fpi->nbits; + lostbits = 0; + x = b->x; + if (n > nbits) { + n -= nbits; + if (any_on(b,n)) { + lostbits = 1; + k = n - 1; + if (x[(unsigned int)k>>kshift] & 1 << (k & kmask)) { + lostbits = 2; + if (k > 1 && any_on(b,k-1)) + lostbits = 3; + } + } + rshift(b, n); + e += n; + } + else if (n < nbits) { + n = nbits - n; + b = lshift(b, n); + if (b == NULL) + return STRTOG_NoMemory; + e -= n; + x = b->x; + } + if (e > fpi->emax) { + ovfl: + Bfree(b); + *bp = 0; + return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; + } + irv = STRTOG_Normal; + if (e < fpi->emin) { + irv = STRTOG_Denormal; + n = fpi->emin - e; + if (n >= nbits) { + switch (fpi->rounding) { + case FPI_Round_near: + if (n == nbits && (n < 2 || any_on(b,n-1))) + goto one_bit; + break; + case FPI_Round_up: + if (!sign) + goto one_bit; + break; + case FPI_Round_down: + if (sign) { + one_bit: + *expt = fpi->emin; + x[0] = b->wds = 1; + *bp = b; + return STRTOG_Denormal | STRTOG_Inexhi + | STRTOG_Underflow; + } + } + Bfree(b); + *bp = 0; + return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow; + } + k = n - 1; + if (lostbits) + lostbits = 1; + else if (k > 0) + lostbits = any_on(b,k); + if (x[(unsigned int)k>>kshift] & 1 << (k & kmask)) + lostbits |= 2; + nbits -= n; + rshift(b,n); + e = fpi->emin; + } + if (lostbits) { + up = 0; + switch(fpi->rounding) { + case FPI_Round_zero: + break; + case FPI_Round_near: + if (lostbits & 2 + && (lostbits & 1) | (x[0] & 1)) + up = 1; + break; + case FPI_Round_up: + up = 1 - sign; + break; + case FPI_Round_down: + up = sign; + } + if (up) { + k = b->wds; + b = increment(b); + x = b->x; + if (irv == STRTOG_Denormal) { + if (nbits == fpi->nbits - 1 + && x[(unsigned int)nbits >> kshift] & 1 << (nbits & kmask)) + irv = STRTOG_Normal; + } + else if (b->wds > k + || ((n = nbits & kmask) !=0 + && hi0bits(x[k-1]) < 32-n)) { + rshift(b,1); + if (++e > fpi->emax) + goto ovfl; + } + irv |= STRTOG_Inexhi; + } + else + irv |= STRTOG_Inexlo; + } + *bp = b; + *expt = e; + return irv; + } diff --git a/lib/nbsd_libc/gdtoa/gmisc.c b/lib/nbsd_libc/gdtoa/gmisc.c new file mode 100644 index 000000000..36ae1d887 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/gmisc.c @@ -0,0 +1,89 @@ +/* $NetBSD: gmisc.c,v 1.3 2006/03/11 18:38:14 kleink Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + void +#ifdef KR_headers +rshift(b, k) Bigint *b; int k; +#else +rshift(Bigint *b, int k) +#endif +{ + ULong *x, *x1, *xe, y; + int n; + + x = x1 = b->x; + n = (unsigned int)k >> kshift; + if (n < b->wds) { + xe = x + b->wds; + x += n; + if (k &= kmask) { + n = ULbits - k; + y = *x++ >> k; + while(x < xe) { + *x1++ = (y | (*x << n)) & ALL_ON; + y = *x++ >> k; + } + if ((*x1 = y) !=0) + x1++; + } + else + while(x < xe) + *x1++ = *x++; + } + if ((b->wds = x1 - b->x) == 0) + b->x[0] = 0; + } + + int +#ifdef KR_headers +trailz(b) CONST Bigint *b; +#else +trailz(CONST Bigint *b) +#endif +{ + ULong L; + CONST ULong *x, *xe; + int n = 0; + + x = b->x; + xe = x + b->wds; + for(n = 0; x < xe && !*x; x++) + n += ULbits; + if (x < xe) { + L = *x; + n += lo0bits(&L); + } + return n; + } diff --git a/lib/nbsd_libc/gdtoa/hd_init.c b/lib/nbsd_libc/gdtoa/hd_init.c new file mode 100644 index 000000000..55d5d426c --- /dev/null +++ b/lib/nbsd_libc/gdtoa/hd_init.c @@ -0,0 +1,57 @@ +/* $NetBSD: hd_init.c,v 1.2 2006/01/25 15:27:42 kleink Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + unsigned char hexdig[256]; + + static void +#ifdef KR_headers +htinit(h, s, inc) unsigned char *h; CONST unsigned char *s; int inc; +#else +htinit(unsigned char *h, CONST unsigned char *s, int inc) +#endif +{ + int i, j; + for(i = 0; (j = s[i]) !=0; i++) + h[j] = i + inc; + } + + void +hexdig_init_D2A(Void) +{ +#define USC (CONST unsigned char *) + htinit(hexdig, USC "0123456789", 0x10); + htinit(hexdig, USC "abcdef", 0x10 + 10); + htinit(hexdig, USC "ABCDEF", 0x10 + 10); + } diff --git a/lib/nbsd_libc/gdtoa/hdtoa.c b/lib/nbsd_libc/gdtoa/hdtoa.c new file mode 100644 index 000000000..8396bbe55 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/hdtoa.c @@ -0,0 +1,363 @@ +/* $NetBSD: hdtoa.c,v 1.6 2008/03/21 23:13:48 christos Exp $ */ + +/*- + * Copyright (c) 2004, 2005 David Schultz + * 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 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 +#if 0 +__FBSDID("$FreeBSD: src/lib/libc/gdtoa/_hdtoa.c,v 1.4 2007/01/03 04:57:58 das Exp $"); +#else +__RCSID("$NetBSD: hdtoa.c,v 1.6 2008/03/21 23:13:48 christos Exp $"); +#endif + +#include +#include +#include +#ifndef __vax__ +#include +#else +#include +#define ieee_double_u vax_dfloating_u +#define dblu_d dfltu_d +#define dblu_dbl dfltu_dflt +#define dbl_sign dflt_sign +#define dbl_exp dflt_exp +#define dbl_frach dflt_frach +#define dbl_fracm dflt_fracm +#define dbl_fracl dflt_fracl +#define DBL_FRACHBITS DFLT_FRACHBITS +#define DBL_FRACMBITS DFLT_FRACMBITS +#define DBL_FRACLBITS DFLT_FRACLBITS +#define DBL_EXPBITS DFLT_EXPBITS +#endif +#include "gdtoaimp.h" + +/* Strings values used by dtoa() */ +#define INFSTR "Infinity" +#define NANSTR "NaN" + +#define DBL_ADJ (DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4)) +#define LDBL_ADJ (LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4)) + +/* + * Round up the given digit string. If the digit string is fff...f, + * this procedure sets it to 100...0 and returns 1 to indicate that + * the exponent needs to be bumped. Otherwise, 0 is returned. + */ +static int +roundup(char *s0, int ndigits) +{ + char *s; + + for (s = s0 + ndigits - 1; *s == 0xf; s--) { + if (s == s0) { + *s = 1; + return (1); + } + *s = 0; + } + ++*s; + return (0); +} + +/* + * Round the given digit string to ndigits digits according to the + * current rounding mode. Note that this could produce a string whose + * value is not representable in the corresponding floating-point + * type. The exponent pointed to by decpt is adjusted if necessary. + */ +static void +dorounding(char *s0, int ndigits, int sign, int *decpt) +{ + int adjust = 0; /* do we need to adjust the exponent? */ + + switch (FLT_ROUNDS) { + case 0: /* toward zero */ + default: /* implementation-defined */ + break; + case 1: /* to nearest, halfway rounds to even */ + if ((s0[ndigits] > 8) || + (s0[ndigits] == 8 && s0[ndigits - 1] & 1)) + adjust = roundup(s0, ndigits); + break; + case 2: /* toward +inf */ + if (sign == 0) + adjust = roundup(s0, ndigits); + break; + case 3: /* toward -inf */ + if (sign != 0) + adjust = roundup(s0, ndigits); + break; + } + + if (adjust) + *decpt += 4; +} + +/* + * This procedure converts a double-precision number in IEEE format + * into a string of hexadecimal digits and an exponent of 2. Its + * behavior is bug-for-bug compatible with dtoa() in mode 2, with the + * following exceptions: + * + * - An ndigits < 0 causes it to use as many digits as necessary to + * represent the number exactly. + * - The additional xdigs argument should point to either the string + * "0123456789ABCDEF" or the string "0123456789abcdef", depending on + * which case is desired. + * - This routine does not repeat dtoa's mistake of setting decpt + * to 9999 in the case of an infinity or NaN. INT_MAX is used + * for this purpose instead. + * + * Note that the C99 standard does not specify what the leading digit + * should be for non-zero numbers. For instance, 0x1.3p3 is the same + * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the + * first digit so that subsequent digits are aligned on nibble + * boundaries (before rounding). + * + * Inputs: d, xdigs, ndigits + * Outputs: decpt, sign, rve + */ +char * +hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, + char **rve) +{ + static const int sigfigs = (DBL_MANT_DIG + 3) / 4; + union ieee_double_u u; + char *s, *s0; + size_t bufsize; + + u.dblu_d = d; + *sign = u.dblu_dbl.dbl_sign; + + switch (fpclassify(d)) { + case FP_NORMAL: + *decpt = u.dblu_dbl.dbl_exp - DBL_ADJ; + break; + case FP_ZERO: + *decpt = 1; + return (nrv_alloc("0", rve, 1)); + case FP_SUBNORMAL: + u.dblu_d *= 0x1p514; + *decpt = u.dblu_dbl.dbl_exp - (514 + DBL_ADJ); + break; + case FP_INFINITE: + *decpt = INT_MAX; + return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1)); + case FP_NAN: + *decpt = INT_MAX; + return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1)); + default: + abort(); + } + + /* FP_NORMAL or FP_SUBNORMAL */ + + if (ndigits == 0) /* dtoa() compatibility */ + ndigits = 1; + + /* + * For simplicity, we generate all the digits even if the + * caller has requested fewer. + */ + bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; + s0 = rv_alloc(bufsize); + if (s0 == NULL) + return NULL; + + /* + * We work from right to left, first adding any requested zero + * padding, then the least significant portion of the + * mantissa, followed by the most significant. The buffer is + * filled with the byte values 0x0 through 0xf, which are + * converted to xdigs[0x0] through xdigs[0xf] after the + * rounding phase. + */ + for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) + *s = 0; + for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) { + *s = u.dblu_dbl.dbl_fracl & 0xf; + u.dblu_dbl.dbl_fracl >>= 4; + } +#ifdef DBL_FRACMBITS + for (; s > s0; s--) { + *s = u.dblu_dbl.dbl_fracm & 0xf; + u.dblu_dbl.dbl_fracm >>= 4; + } +#endif + for (; s > s0; s--) { + *s = u.dblu_dbl.dbl_frach & 0xf; + u.dblu_dbl.dbl_frach >>= 4; + } + + /* + * At this point, we have snarfed all the bits in the + * mantissa, with the possible exception of the highest-order + * (partial) nibble, which is dealt with by the next + * statement. We also tack on the implicit normalization bit. + */ + *s = u.dblu_dbl.dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4)); + + /* If ndigits < 0, we are expected to auto-size the precision. */ + if (ndigits < 0) { + for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) + continue; + } + + if (sigfigs > ndigits && s0[ndigits] != 0) + dorounding(s0, ndigits, u.dblu_dbl.dbl_sign, decpt); + + s = s0 + ndigits; + if (rve != NULL) + *rve = s; + *s-- = '\0'; + for (; s >= s0; s--) + *s = xdigs[(unsigned int)*s]; + + return (s0); +} + +#if (LDBL_MANT_DIG > DBL_MANT_DIG) + +/* + * This is the long double version of hdtoa(). + */ +char * +hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, + char **rve) +{ + static const int sigfigs = (LDBL_MANT_DIG + 3) / 4; + union ieee_ext_u u; + char *s, *s0; + size_t bufsize; + + u.extu_ld = e; + *sign = u.extu_ext.ext_sign; + + switch (fpclassify(e)) { + case FP_NORMAL: + *decpt = u.extu_ext.ext_exp - LDBL_ADJ; + break; + case FP_ZERO: + *decpt = 1; + return (nrv_alloc("0", rve, 1)); + case FP_SUBNORMAL: + u.extu_ld *= 0x1p514L; + *decpt = u.extu_ext.ext_exp - (514 + LDBL_ADJ); + break; + case FP_INFINITE: + *decpt = INT_MAX; + return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1)); + case FP_NAN: + *decpt = INT_MAX; + return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1)); + default: + abort(); + } + + /* FP_NORMAL or FP_SUBNORMAL */ + + if (ndigits == 0) /* dtoa() compatibility */ + ndigits = 1; + + /* + * For simplicity, we generate all the digits even if the + * caller has requested fewer. + */ + bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; + s0 = rv_alloc(bufsize); + if (s0 == NULL) + return NULL; + + /* + * We work from right to left, first adding any requested zero + * padding, then the least significant portion of the + * mantissa, followed by the most significant. The buffer is + * filled with the byte values 0x0 through 0xf, which are + * converted to xdigs[0x0] through xdigs[0xf] after the + * rounding phase. + */ + for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) + *s = 0; + for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) { + *s = u.extu_ext.ext_fracl & 0xf; + u.extu_ext.ext_fracl >>= 4; + } +#ifdef EXT_FRACHMBITS + for (; s > s0; s--) { + *s = u.extu_ext.ext_frachm & 0xf; + u.extu_ext.ext_frachm >>= 4; + } +#endif +#ifdef EXT_FRACLMBITS + for (; s > s0; s--) { + *s = u.extu_ext.ext_fraclm & 0xf; + u.extu_ext.ext_fraclm >>= 4; + } +#endif + for (; s > s0; s--) { + *s = u.extu_ext.ext_frach & 0xf; + u.extu_ext.ext_frach >>= 4; + } + + /* + * At this point, we have snarfed all the bits in the + * mantissa, with the possible exception of the highest-order + * (partial) nibble, which is dealt with by the next + * statement. We also tack on the implicit normalization bit. + */ + *s = u.extu_ext.ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4)); + + /* If ndigits < 0, we are expected to auto-size the precision. */ + if (ndigits < 0) { + for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) + continue; + } + + if (sigfigs > ndigits && s0[ndigits] != 0) + dorounding(s0, ndigits, u.extu_ext.ext_sign, decpt); + + s = s0 + ndigits; + if (rve != NULL) + *rve = s; + *s-- = '\0'; + for (; s >= s0; s--) + *s = xdigs[(unsigned int)*s]; + + return (s0); +} + +#else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ + +char * +hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, + char **rve) +{ + + return (hdtoa((double)e, xdigs, ndigits, decpt, sign, rve)); +} + +#endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ diff --git a/lib/nbsd_libc/gdtoa/hexnan.c b/lib/nbsd_libc/gdtoa/hexnan.c new file mode 100644 index 000000000..405328ab3 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/hexnan.c @@ -0,0 +1,133 @@ +/* $NetBSD: hexnan.c,v 1.3 2006/03/11 18:38:14 kleink Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + static void +#ifdef KR_headers +L_shift(x, x1, i) ULong *x; ULong *x1; int i; +#else +L_shift(ULong *x, ULong *x1, int i) +#endif +{ + int j; + + i = 8 - i; + i <<= 2; + j = ULbits - i; + do { + *x |= x[1] << j; + x[1] >>= i; + } while(++x < x1); + } + + int +#ifdef KR_headers +hexnan(sp, fpi, x0) + CONST char **sp; CONST FPI *fpi; ULong *x0; +#else +hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0) +#endif +{ + ULong c, h, *x, *x1, *xe; + CONST char *s; + int havedig, hd0, i, nbits; + + if (!hexdig['0']) + hexdig_init_D2A(); + nbits = fpi->nbits; + x = x0 + ((unsigned int)nbits >> kshift); + if (nbits & kmask) + x++; + *--x = 0; + x1 = xe = x; + havedig = hd0 = i = 0; + s = *sp; + while((c = *(CONST unsigned char*)++s) != 0) { + if (!(h = hexdig[c])) { + if (c <= ' ') { + if (hd0 < havedig) { + if (x < x1 && i < 8) + L_shift(x, x1, i); + if (x <= x0) { + i = 8; + continue; + } + hd0 = havedig; + *--x = 0; + x1 = x; + i = 0; + } + continue; + } + if (/*(*/ c == ')' && havedig) { + *sp = s + 1; + break; + } + return STRTOG_NaN; + } + havedig++; + if (++i > 8) { + if (x <= x0) + continue; + i = 1; + *--x = 0; + } + *x = (*x << 4) | (h & 0xf); + } + if (!havedig) + return STRTOG_NaN; + if (x < x1 && i < 8) + L_shift(x, x1, i); + if (x > x0) { + x1 = x0; + do *x1++ = *x++; + while(x <= xe); + do *x1++ = 0; + while(x1 <= xe); + } + else { + /* truncate high-order word if necessary */ + if ( (i = nbits & (ULbits-1)) !=0) + *xe &= ((ULong)0xffffffff) >> (ULbits - i); + } + for(x1 = xe;; --x1) { + if (*x1 != 0) + break; + if (x1 == x0) { + *x1 = 1; + break; + } + } + return STRTOG_NaNbits; + } diff --git a/lib/nbsd_libc/gdtoa/ldtoa.c b/lib/nbsd_libc/gdtoa/ldtoa.c new file mode 100644 index 000000000..de026025a --- /dev/null +++ b/lib/nbsd_libc/gdtoa/ldtoa.c @@ -0,0 +1,111 @@ +/* $NetBSD: ldtoa.c,v 1.4 2007/02/23 17:45:59 christos Exp $ */ + +/*- + * Copyright (c) 2003 David Schultz + * 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 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 +#if 0 +__FBSDID("$FreeBSD: src/lib/libc/gdtoa/_ldtoa.c,v 1.2 2004/01/18 07:53:49 das Exp $"); +#else +__RCSID("$NetBSD: ldtoa.c,v 1.4 2007/02/23 17:45:59 christos Exp $"); +#endif + +#include +#include +#include +#include +#include +#ifndef __vax__ +#include +#endif +#include "gdtoaimp.h" + +/* + * ldtoa() is a wrapper for gdtoa() that makes it smell like dtoa(), + * except that the floating point argument is passed by reference. + * When dtoa() is passed a NaN or infinity, it sets expt to 9999. + * However, a long double could have a valid exponent of 9999, so we + * use INT_MAX in ldtoa() instead. + */ +char * +ldtoa(long double *ld, int mode, int ndigits, int *decpt, int *sign, char **rve) +{ +#ifdef EXT_EXPBITS + static FPI fpi = { + LDBL_MANT_DIG, /* nbits */ + LDBL_MIN_EXP - LDBL_MANT_DIG, /* emin */ + LDBL_MAX_EXP - LDBL_MANT_DIG, /* emax */ + FPI_Round_near, /* rounding */ +#ifdef Sudden_Underflow /* unused, but correct anyway */ + 1 +#else + 0 +#endif + }; + int be, kind; + char *ret; + union ieee_ext_u u; + uint32_t bits[(LDBL_MANT_DIG + 31) / 32]; + + u.extu_ld = *ld; + *sign = u.extu_ext.ext_sign; + be = u.extu_ext.ext_exp - (LDBL_MAX_EXP - 1) - (LDBL_MANT_DIG - 1); + EXT_TO_ARRAY32(u, bits); + + switch (fpclassify(u.extu_ld)) { + case FP_NORMAL: + kind = STRTOG_Normal; +#ifdef LDBL_IMPLICIT_NBIT + bits[LDBL_MANT_DIG / 32] |= 1 << ((LDBL_MANT_DIG - 1) % 32); +#endif /* LDBL_IMPLICIT_NBIT */ + break; + case FP_ZERO: + kind = STRTOG_Zero; + break; + case FP_SUBNORMAL: + kind = STRTOG_Denormal; +#ifdef LDBL_IMPLICIT_NBIT + be++; +#endif + break; + case FP_INFINITE: + kind = STRTOG_Infinite; + break; + case FP_NAN: + kind = STRTOG_NaN; + break; + default: + abort(); + } + + ret = gdtoa(&fpi, be, (ULong *)bits, &kind, mode, ndigits, decpt, rve); + if (*decpt == -32768) + *decpt = INT_MAX; + return ret; +#else + return dtoa((double)*ld, mode, ndigits, decpt, sign, rve); +#endif +} diff --git a/lib/nbsd_libc/gdtoa/makefile b/lib/nbsd_libc/gdtoa/makefile new file mode 100644 index 000000000..ff33a3d8d --- /dev/null +++ b/lib/nbsd_libc/gdtoa/makefile @@ -0,0 +1,76 @@ +# $NetBSD: makefile,v 1.3 2006/01/25 16:40:57 kleink Exp $ + +# /**************************************************************** +# Copyright (C) 1998 by Lucent Technologies +# All Rights Reserved +# +# Permission to use, copy, modify, and distribute this software and +# its documentation for any purpose and without fee is hereby +# granted, provided that the above copyright notice appear in all +# copies and that both that the copyright notice and this +# permission notice and warranty disclaimer appear in supporting +# documentation, and that the name of Lucent or any of its entities +# not be used in advertising or publicity pertaining to +# distribution of the software without specific, written prior +# permission. +# +# LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +# IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +# SPECIAL, 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. +# +# ****************************************************************/ + +.SUFFIXES: .c .o +CC = cc +CFLAGS = -g -DINFNAN_CHECK + +.c.o: + $(CC) -c $(CFLAGS) $*.c + +all: arith.h gd_qnan.h gdtoa.a + +arith.h: arithchk.c + $(CC) $(CFLAGS) arithchk.c || $(CC) -DNO_LONG_LONG $(CFLAGS) arithchk.c + ./a.out >arith.h + rm -f a.out arithchk.o + +gd_qnan.h: arith.h qnan.c + $(CC) $(CFLAGS) qnan.c + ./a.out >gd_qnan.h + rm -f a.out qnan.o + +gdtoa.a: dmisc.c dtoa.c g_Qfmt.c g__fmt.c g_ddfmt.c g_dfmt.c g_ffmt.c\ + g_xLfmt.c g_xfmt.c gdtoa.c gethex.c gmisc.c hd_init.c hexnan.c\ + misc.c smisc.c strtoIQ.c strtoId.c strtoIdd.c strtoIf.c strtoIg.c\ + strtoIx.c strtoIxL.c strtod.c strtodI.c strtodg.c strtof.c strtopQ.c\ + strtopd.c strtopdd.c strtopf.c strtopx.c strtopxL.c strtorQ.c\ + strtord.c strtordd.c strtorf.c strtorx.c strtorxL.c sum.c ulp.c + $(CC) -c $(CFLAGS) $? + x=`echo $? | sed 's/\.c/.o/g'` && ar ruv gdtoa.a $$x && rm $$x + ranlib gdtoa.a || true + +# If your system lacks ranlib, you do not need it. + +xs0 = README arithchk.c dmisc.c dtoa.c g_Qfmt.c g__fmt.c g_ddfmt.c g_dfmt.c\ + g_ffmt.c g_xLfmt.c g_xfmt.c gdtoa.c gdtoa.h gdtoaimp.h gethex.c\ + gmisc.c hd_init.c hexnan.c makefile misc.c qnan.c smisc.c strtoIQ.c\ + strtoId.c strtoIdd.c strtoIf.c strtoIg.c strtoIx.c strtoIxL.c\ + strtod.c strtodI.c strtodg.c strtodnrp.c strtof.c strtopQ.c strtopd.c\ + strtopdd.c strtopf.c strtopx.c strtopxL.c strtorQ.c strtord.c strtordd.c\ + strtorf.c strtorx.c strtorxL.c sum.c ulp.c + +# "make xsum.out" to check for transmission errors; source for xsum is +# netlib's "xsum.c from f2c", e.g., +# ftp://netlib.bell-labs.com/netlib/f2c/xsum.c.gz + +xsum.out: xsum0.out $(xs0) + xsum $(xs0) >xsum1.out + cmp xsum0.out xsum1.out && mv xsum1.out xsum.out || diff xsum[01].out + +clean: + rm -f arith.h gd_qnan.h *.[ao] xsum.out xsum1.out diff --git a/lib/nbsd_libc/gdtoa/misc.c b/lib/nbsd_libc/gdtoa/misc.c new file mode 100644 index 000000000..8ae13cce6 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/misc.c @@ -0,0 +1,903 @@ +/* $NetBSD: misc.c,v 1.5 2009/01/30 23:35:35 lukem Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 1999 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + static Bigint *freelist[Kmax+1]; +#ifndef Omit_Private_Memory +#ifndef PRIVATE_MEM +#define PRIVATE_MEM 2304 +#endif +#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) +static double private_mem[PRIVATE_mem], *pmem_next = private_mem; +#endif + + Bigint * +Balloc +#ifdef KR_headers + (k) int k; +#else + (int k) +#endif +{ + int x; + Bigint *rv; +#ifndef Omit_Private_Memory + unsigned int len; +#endif + + ACQUIRE_DTOA_LOCK(0); + if ( (rv = freelist[k]) !=0) { + freelist[k] = rv->next; + } + else { + x = 1 << k; +#ifdef Omit_Private_Memory + rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong)); +#else + len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) + /sizeof(double); + if ((double *)(pmem_next - private_mem + len) <= (double *)PRIVATE_mem) { + rv = (Bigint*)(void *)pmem_next; + pmem_next += len; + } + else + rv = (Bigint*)MALLOC(len*sizeof(double)); +#endif + if (rv == NULL) + return NULL; + rv->k = k; + rv->maxwds = x; + } + FREE_DTOA_LOCK(0); + rv->sign = rv->wds = 0; + return rv; + } + + void +Bfree +#ifdef KR_headers + (v) Bigint *v; +#else + (Bigint *v) +#endif +{ + if (v) { + ACQUIRE_DTOA_LOCK(0); + v->next = freelist[v->k]; + freelist[v->k] = v; + FREE_DTOA_LOCK(0); + } + } + + int +lo0bits +#ifdef KR_headers + (y) ULong *y; +#else + (ULong *y) +#endif +{ + int k; + ULong x = *y; + + if (x & 7) { + if (x & 1) + return 0; + if (x & 2) { + *y = x >> 1; + return 1; + } + *y = x >> 2; + return 2; + } + k = 0; + if (!(x & 0xffff)) { + k = 16; + x >>= 16; + } + if (!(x & 0xff)) { + k += 8; + x >>= 8; + } + if (!(x & 0xf)) { + k += 4; + x >>= 4; + } + if (!(x & 0x3)) { + k += 2; + x >>= 2; + } + if (!(x & 1)) { + k++; + x >>= 1; + if (!x) + return 32; + } + *y = x; + return k; + } + + Bigint * +multadd +#ifdef KR_headers + (b, m, a) Bigint *b; int m, a; +#else + (Bigint *b, int m, int a) /* multiply by m and add a */ +#endif +{ + int i, wds; +#ifdef ULLong + ULong *x; + ULLong carry, y; +#else + ULong carry, *x, y; +#ifdef Pack_32 + ULong xi, z; +#endif +#endif + Bigint *b1; + + wds = b->wds; + x = b->x; + i = 0; + carry = a; + do { +#ifdef ULLong + y = *x * (ULLong)m + carry; + carry = y >> 32; + /* LINTED conversion */ + *x++ = y & 0xffffffffUL; +#else +#ifdef Pack_32 + xi = *x; + y = (xi & 0xffff) * m + carry; + z = (xi >> 16) * m + (y >> 16); + carry = z >> 16; + *x++ = (z << 16) + (y & 0xffff); +#else + y = *x * m + carry; + carry = y >> 16; + *x++ = y & 0xffff; +#endif +#endif + } + while(++i < wds); + if (carry) { + if (wds >= b->maxwds) { + b1 = Balloc(b->k+1); + if (b1 == NULL) { + Bfree(b); + return NULL; + } + Bcopy(b1, b); + Bfree(b); + b = b1; + } + /* LINTED conversion */ + b->x[wds++] = carry; + b->wds = wds; + } + return b; + } + + int +hi0bits_D2A +#ifdef KR_headers + (x) ULong x; +#else + (ULong x) +#endif +{ + int k = 0; + + if (!(x & 0xffff0000)) { + k = 16; + x <<= 16; + } + if (!(x & 0xff000000)) { + k += 8; + x <<= 8; + } + if (!(x & 0xf0000000)) { + k += 4; + x <<= 4; + } + if (!(x & 0xc0000000)) { + k += 2; + x <<= 2; + } + if (!(x & 0x80000000)) { + k++; + if (!(x & 0x40000000)) + return 32; + } + return k; + } + + Bigint * +i2b +#ifdef KR_headers + (i) int i; +#else + (int i) +#endif +{ + Bigint *b; + + b = Balloc(1); + if (b == NULL) + return NULL; + b->x[0] = i; + b->wds = 1; + return b; + } + + Bigint * +mult +#ifdef KR_headers + (a, b) Bigint *a, *b; +#else + (Bigint *a, Bigint *b) +#endif +{ + Bigint *c; + int k, wa, wb, wc; + ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; + ULong y; +#ifdef ULLong + ULLong carry, z; +#else + ULong carry, z; +#ifdef Pack_32 + ULong z2; +#endif +#endif + + if (a->wds < b->wds) { + c = a; + a = b; + b = c; + } + k = a->k; + wa = a->wds; + wb = b->wds; + wc = wa + wb; + if (wc > a->maxwds) + k++; + c = Balloc(k); + if (c == NULL) + return NULL; + for(x = c->x, xa = x + wc; x < xa; x++) + *x = 0; + xa = a->x; + xae = xa + wa; + xb = b->x; + xbe = xb + wb; + xc0 = c->x; +#ifdef ULLong + for(; xb < xbe; xc0++) { + if ( (y = *xb++) !=0) { + x = xa; + xc = xc0; + carry = 0; + do { + z = *x++ * (ULLong)y + *xc + carry; + carry = z >> 32; + /* LINTED conversion */ + *xc++ = z & 0xffffffffUL; + } + while(x < xae); + /* LINTED conversion */ + *xc = carry; + } + } +#else +#ifdef Pack_32 + for(; xb < xbe; xb++, xc0++) { + if ( (y = *xb & 0xffff) !=0) { + x = xa; + xc = xc0; + carry = 0; + do { + z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; + carry = z >> 16; + z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; + carry = z2 >> 16; + Storeinc(xc, z2, z); + } + while(x < xae); + *xc = carry; + } + if ( (y = *xb >> 16) !=0) { + x = xa; + xc = xc0; + carry = 0; + z2 = *xc; + do { + z = (*x & 0xffff) * y + (*xc >> 16) + carry; + carry = z >> 16; + Storeinc(xc, z, z2); + z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; + carry = z2 >> 16; + } + while(x < xae); + *xc = z2; + } + } +#else + for(; xb < xbe; xc0++) { + if ( (y = *xb++) !=0) { + x = xa; + xc = xc0; + carry = 0; + do { + z = *x++ * y + *xc + carry; + carry = z >> 16; + *xc++ = z & 0xffff; + } + while(x < xae); + *xc = carry; + } + } +#endif +#endif + for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; + c->wds = wc; + return c; + } + + static Bigint *p5s; + + Bigint * +pow5mult +#ifdef KR_headers + (b, k) Bigint *b; int k; +#else + (Bigint *b, int k) +#endif +{ + Bigint *b1, *p5, *p51; + int i; + static CONST int p05[3] = { 5, 25, 125 }; + + if ( (i = k & 3) !=0) { + b = multadd(b, p05[i-1], 0); + if (b == NULL) + return NULL; + } + + if (!(k = (unsigned int)k >> 2)) + return b; + if ((p5 = p5s) == 0) { + /* first time */ +#ifdef MULTIPLE_THREADS + ACQUIRE_DTOA_LOCK(1); + if (!(p5 = p5s)) { + p5 = p5s = i2b(625); + if (p5 == NULL) + return NULL; + p5->next = 0; + } + FREE_DTOA_LOCK(1); +#else + p5 = p5s = i2b(625); + if (p5 == NULL) + return NULL; + p5->next = 0; +#endif + } + for(;;) { + if (k & 1) { + b1 = mult(b, p5); + if (b1 == NULL) + return NULL; + b = b1; + } + if (!(k = (unsigned int)k >> 1)) + break; + if ((p51 = p5->next) == 0) { +#ifdef MULTIPLE_THREADS + ACQUIRE_DTOA_LOCK(1); + if (!(p51 = p5->next)) { + p51 = p5->next = mult(p5,p5); + if (p51 == NULL) + return NULL; + p51->next = 0; + } + FREE_DTOA_LOCK(1); +#else + p51 = p5->next = mult(p5,p5); + if (p51 == NULL) + return NULL; + p51->next = 0; +#endif + } + p5 = p51; + } + return b; + } + + Bigint * +lshift +#ifdef KR_headers + (b, k) Bigint *b; int k; +#else + (Bigint *b, int k) +#endif +{ + int i, k1, n, n1; + Bigint *b1; + ULong *x, *x1, *xe, z; + + n = (unsigned int)k >> kshift; + k1 = b->k; + n1 = n + b->wds + 1; + for(i = b->maxwds; n1 > i; i <<= 1) + k1++; + b1 = Balloc(k1); + if (b1 == NULL) + return NULL; + x1 = b1->x; + for(i = 0; i < n; i++) + *x1++ = 0; + x = b->x; + xe = x + b->wds; + if (k &= kmask) { +#ifdef Pack_32 + k1 = 32 - k; + z = 0; + do { + *x1++ = *x << k | z; + z = *x++ >> k1; + } + while(x < xe); + if ((*x1 = z) !=0) + ++n1; +#else + k1 = 16 - k; + z = 0; + do { + *x1++ = *x << k & 0xffff | z; + z = *x++ >> k1; + } + while(x < xe); + if (*x1 = z) + ++n1; +#endif + } + else do + *x1++ = *x++; + while(x < xe); + b1->wds = n1 - 1; + Bfree(b); + return b1; + } + + int +cmp +#ifdef KR_headers + (a, b) Bigint *a, *b; +#else + (Bigint *a, Bigint *b) +#endif +{ + ULong *xa, *xa0, *xb, *xb0; + int i, j; + + i = a->wds; + j = b->wds; +#ifdef DEBUG + if (i > 1 && !a->x[i-1]) + Bug("cmp called with a->x[a->wds-1] == 0"); + if (j > 1 && !b->x[j-1]) + Bug("cmp called with b->x[b->wds-1] == 0"); +#endif + if (i -= j) + return i; + xa0 = a->x; + xa = xa0 + j; + xb0 = b->x; + xb = xb0 + j; + for(;;) { + if (*--xa != *--xb) + return *xa < *xb ? -1 : 1; + if (xa <= xa0) + break; + } + return 0; + } + + Bigint * +diff +#ifdef KR_headers + (a, b) Bigint *a, *b; +#else + (Bigint *a, Bigint *b) +#endif +{ + Bigint *c; + int i, wa, wb; + ULong *xa, *xae, *xb, *xbe, *xc; +#ifdef ULLong + ULLong borrow, y; +#else + ULong borrow, y; +#ifdef Pack_32 + ULong z; +#endif +#endif + + i = cmp(a,b); + if (!i) { + c = Balloc(0); + if (c == NULL) + return NULL; + c->wds = 1; + c->x[0] = 0; + return c; + } + if (i < 0) { + c = a; + a = b; + b = c; + i = 1; + } + else + i = 0; + c = Balloc(a->k); + if (c == NULL) + return NULL; + c->sign = i; + wa = a->wds; + xa = a->x; + xae = xa + wa; + wb = b->wds; + xb = b->x; + xbe = xb + wb; + xc = c->x; + borrow = 0; +#ifdef ULLong + do { + y = (ULLong)*xa++ - *xb++ - borrow; + borrow = y >> 32 & 1UL; + /* LINTED conversion */ + *xc++ = y & 0xffffffffUL; + } + while(xb < xbe); + while(xa < xae) { + y = *xa++ - borrow; + borrow = y >> 32 & 1UL; + /* LINTED conversion */ + *xc++ = y & 0xffffffffUL; + } +#else +#ifdef Pack_32 + do { + y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; + borrow = (y & 0x10000) >> 16; + z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; + borrow = (z & 0x10000) >> 16; + Storeinc(xc, z, y); + } + while(xb < xbe); + while(xa < xae) { + y = (*xa & 0xffff) - borrow; + borrow = (y & 0x10000) >> 16; + z = (*xa++ >> 16) - borrow; + borrow = (z & 0x10000) >> 16; + Storeinc(xc, z, y); + } +#else + do { + y = *xa++ - *xb++ - borrow; + borrow = (y & 0x10000) >> 16; + *xc++ = y & 0xffff; + } + while(xb < xbe); + while(xa < xae) { + y = *xa++ - borrow; + borrow = (y & 0x10000) >> 16; + *xc++ = y & 0xffff; + } +#endif +#endif + while(!*--xc) + wa--; + c->wds = wa; + return c; + } + + double +b2d +#ifdef KR_headers + (a, e) Bigint *a; int *e; +#else + (Bigint *a, int *e) +#endif +{ + ULong *xa, *xa0, w, y, z; + int k; + double d; +#ifdef VAX + ULong d0, d1; +#else +#define d0 word0(d) +#define d1 word1(d) +#endif + + xa0 = a->x; + xa = xa0 + a->wds; + y = *--xa; +#ifdef DEBUG + if (!y) Bug("zero y in b2d"); +#endif + k = hi0bits(y); + *e = 32 - k; +#ifdef Pack_32 + if (k < Ebits) { + d0 = Exp_1 | y >> (Ebits - k); + w = xa > xa0 ? *--xa : 0; + d1 = y << ((32-Ebits) + k) | w >> (Ebits - k); + goto ret_d; + } + z = xa > xa0 ? *--xa : 0; + if (k -= Ebits) { + d0 = Exp_1 | y << k | z >> (32 - k); + y = xa > xa0 ? *--xa : 0; + d1 = z << k | y >> (32 - k); + } + else { + d0 = Exp_1 | y; + d1 = z; + } +#else + if (k < Ebits + 16) { + z = xa > xa0 ? *--xa : 0; + d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; + w = xa > xa0 ? *--xa : 0; + y = xa > xa0 ? *--xa : 0; + d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; + goto ret_d; + } + z = xa > xa0 ? *--xa : 0; + w = xa > xa0 ? *--xa : 0; + k -= Ebits + 16; + d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; + y = xa > xa0 ? *--xa : 0; + d1 = w << k + 16 | y << k; +#endif + ret_d: +#ifdef VAX + word0(d) = d0 >> 16 | d0 << 16; + word1(d) = d1 >> 16 | d1 << 16; +#endif + return dval(d); + } +#undef d0 +#undef d1 + + Bigint * +d2b +#ifdef KR_headers + (d, e, bits) double d; int *e, *bits; +#else + (double d, int *e, int *bits) +#endif +{ + Bigint *b; +#ifndef Sudden_Underflow + int i; +#endif + int de, k; + ULong *x, y, z; +#ifdef VAX + ULong d0, d1; + d0 = word0(d) >> 16 | word0(d) << 16; + d1 = word1(d) >> 16 | word1(d) << 16; +#else +#define d0 word0(d) +#define d1 word1(d) +#endif + +#ifdef Pack_32 + b = Balloc(1); +#else + b = Balloc(2); +#endif + if (b == NULL) + return NULL; + x = b->x; + + z = d0 & Frac_mask; + d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ +#ifdef Sudden_Underflow + de = (int)(d0 >> Exp_shift); +#ifndef IBM + z |= Exp_msk11; +#endif +#else + if ( (de = (int)(d0 >> Exp_shift)) !=0) + z |= Exp_msk1; +#endif +#ifdef Pack_32 + if ( (y = d1) !=0) { + if ( (k = lo0bits(&y)) !=0) { + x[0] = y | z << (32 - k); + z >>= k; + } + else + x[0] = y; +#ifndef Sudden_Underflow + i = +#endif + b->wds = (x[1] = z) !=0 ? 2 : 1; + } + else { +#ifdef DEBUG + if (!z) + Bug("Zero passed to d2b"); +#endif + k = lo0bits(&z); + x[0] = z; +#ifndef Sudden_Underflow + i = +#endif + b->wds = 1; + k += 32; + } +#else + if ( (y = d1) !=0) { + if ( (k = lo0bits(&y)) !=0) + if (k >= 16) { + x[0] = y | z << 32 - k & 0xffff; + x[1] = z >> k - 16 & 0xffff; + x[2] = z >> k; + i = 2; + } + else { + x[0] = y & 0xffff; + x[1] = y >> 16 | z << 16 - k & 0xffff; + x[2] = z >> k & 0xffff; + x[3] = z >> k+16; + i = 3; + } + else { + x[0] = y & 0xffff; + x[1] = y >> 16; + x[2] = z & 0xffff; + x[3] = z >> 16; + i = 3; + } + } + else { +#ifdef DEBUG + if (!z) + Bug("Zero passed to d2b"); +#endif + k = lo0bits(&z); + if (k >= 16) { + x[0] = z; + i = 0; + } + else { + x[0] = z & 0xffff; + x[1] = z >> 16; + i = 1; + } + k += 32; + } + while(!x[i]) + --i; + b->wds = i + 1; +#endif +#ifndef Sudden_Underflow + if (de) { +#endif +#ifdef IBM + *e = (de - Bias - (P-1) << 2) + k; + *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); +#else + *e = de - Bias - (P-1) + k; + *bits = P - k; +#endif +#ifndef Sudden_Underflow + } + else { + *e = de - Bias - (P-1) + 1 + k; +#ifdef Pack_32 + *bits = 32*i - hi0bits(x[i-1]); +#else + *bits = (i+2)*16 - hi0bits(x[i]); +#endif + } +#endif + return b; + } +#undef d0 +#undef d1 + + CONST double +#ifdef IEEE_Arith +bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; +CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 + }; +#else +#ifdef IBM +bigtens[] = { 1e16, 1e32, 1e64 }; +CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; +#else +bigtens[] = { 1e16, 1e32 }; +CONST double tinytens[] = { 1e-16, 1e-32 }; +#endif +#endif + + CONST double +tens[] = { + 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, + 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, + 1e20, 1e21, 1e22 +#ifdef VAX + , 1e23, 1e24 +#endif + }; + + char * +#ifdef KR_headers +strcp_D2A(a, b) char *a; char *b; +#else +strcp_D2A(char *a, CONST char *b) +#endif +{ + while((*a = *b++)) + a++; + return a; + } + +#ifdef NO_STRING_H + + Char * +#ifdef KR_headers +memcpy_D2A(a, b, len) Char *a; Char *b; size_t len; +#else +memcpy_D2A(void *a1, void *b1, size_t len) +#endif +{ + char *a = (char*)a1, *ae = a + len; + char *b = (char*)b1, *a0 = a; + while(a < ae) + *a++ = *b++; + return a0; + } + +#endif /* NO_STRING_H */ diff --git a/lib/nbsd_libc/gdtoa/qnan.c b/lib/nbsd_libc/gdtoa/qnan.c new file mode 100644 index 000000000..2e5ce8924 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/qnan.c @@ -0,0 +1,112 @@ +/* $NetBSD: qnan.c,v 1.2 2006/01/25 15:27:42 kleink Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 2005 by David M. Gay +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that the copyright notice and this permission notice and warranty +disclaimer appear in supporting documentation, and that the name of +the author or any of his current or former employers not be used in +advertising or publicity pertaining to distribution of the software +without specific, written prior permission. + +THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN +NO EVENT SHALL THE AUTHOR OR ANY OF HIS CURRENT OR FORMER EMPLOYERS BE +LIABLE FOR ANY SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +/* Program to compute quiet NaNs of various precisions (float, */ +/* double, and perhaps long double) on the current system, */ +/* provided the system uses binary IEEE (P754) arithmetic. */ +/* Note that one system's quiet NaN may be a signaling NaN on */ +/* another system. The IEEE arithmetic standards (P754, P854) */ +/* do not specify how to distinguish signaling NaNs from quiet */ +/* ones, and this detail varies across systems. The computed */ +/* NaN values are encoded in #defines for values for an */ +/* unsigned 32-bit integer type, called Ulong below, and */ +/* (for long double) perhaps as unsigned short values. Once */ +/* upon a time, there were PC compilers for Intel CPUs that */ +/* had sizeof(long double) = 10. Are such compilers still */ +/* distributed? */ + +#include +#include "arith.h" + +#ifndef Long +#define Long long +#endif + +typedef unsigned Long Ulong; + +#undef HAVE_IEEE +#ifdef IEEE_LITTLE_ENDIAN +#define _0 1 +#define _1 0 +#define HAVE_IEEE +#endif +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define HAVE_IEEE +#endif + +#define UL (unsigned long) + + int +main(void) +{ +#ifdef HAVE_IEEE + typedef union { + float f; + double d; + Ulong L[4]; +#ifndef NO_LONG_LONG + unsigned short u[5]; + long double D; +#endif + } U; + U a, b, c; + int i; + + a.L[0] = b.L[0] = 0x7f800000; + c.f = a.f - b.f; + printf("#define f_QNAN 0x%lx\n", UL c.L[0]); + a.L[_0] = b.L[_0] = 0x7ff00000; + a.L[_1] = b.L[_1] = 0; + c.d = a.d - b.d; /* quiet NaN */ + printf("#define d_QNAN0 0x%lx\n", UL c.L[0]); + printf("#define d_QNAN1 0x%lx\n", UL c.L[1]); +#ifdef NO_LONG_LONG + for(i = 0; i < 4; i++) + printf("#define ld_QNAN%d 0xffffffff\n", i); + for(i = 0; i < 5; i++) + printf("#define ldus_QNAN%d 0xffff\n", i); +#else + b.D = c.D = a.d; + if (printf("") < 0) + c.D = 37; /* never executed; just defeat optimization */ + a.L[2] = a.L[3] = 0; + a.D = b.D - c.D; + for(i = 0; i < 4; i++) + printf("#define ld_QNAN%d 0x%lx\n", i, UL a.L[i]); + for(i = 0; i < 5; i++) + printf("#define ldus_QNAN%d 0x%x\n", i, a.u[i]); +#endif +#endif /* HAVE_IEEE */ + return 0; + } diff --git a/lib/nbsd_libc/gdtoa/smisc.c b/lib/nbsd_libc/gdtoa/smisc.c new file mode 100644 index 000000000..349ff817a --- /dev/null +++ b/lib/nbsd_libc/gdtoa/smisc.c @@ -0,0 +1,203 @@ +/* $NetBSD: smisc.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 1999 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + Bigint * +s2b +#ifdef KR_headers + (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9; +#else + (CONST char *s, int nd0, int nd, ULong y9) +#endif +{ + Bigint *b; + int i, k; + Long x, y; + + x = (nd + 8) / 9; + for(k = 0, y = 1; x > y; y <<= 1, k++) ; +#ifdef Pack_32 + b = Balloc(k); + if (b == NULL) + return NULL; + b->x[0] = y9; + b->wds = 1; +#else + b = Balloc(k+1); + if (b == NULL) + return NULL; + b->x[0] = y9 & 0xffff; + b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; +#endif + + i = 9; + if (9 < nd0) { + s += 9; + do { + b = multadd(b, 10, *s++ - '0'); + if (b == NULL) + return NULL; + } while(++i < nd0); + s++; + } + else + s += 10; + for(; i < nd; i++) { + b = multadd(b, 10, *s++ - '0'); + if (b == NULL) + return NULL; + } + return b; + } + + double +ratio +#ifdef KR_headers + (a, b) Bigint *a, *b; +#else + (Bigint *a, Bigint *b) +#endif +{ + double da, db; + int k, ka, kb; + + dval(da) = b2d(a, &ka); + dval(db) = b2d(b, &kb); + k = ka - kb + ULbits*(a->wds - b->wds); +#ifdef IBM + if (k > 0) { + word0(da) += (k >> 2)*Exp_msk1; + if (k &= 3) + dval(da) *= 1 << k; + } + else { + k = -k; + word0(db) += (k >> 2)*Exp_msk1; + if (k &= 3) + dval(db) *= 1 << k; + } +#else + if (k > 0) + word0(da) += k*Exp_msk1; + else { + k = -k; + word0(db) += k*Exp_msk1; + } +#endif + return dval(da) / dval(db); + } + +#ifdef INFNAN_CHECK + + int +match +#ifdef KR_headers + (sp, t) CONST char **sp, *t; +#else + (CONST char **sp, CONST char *t) +#endif +{ + int c, d; + CONST char *s = *sp; + + while( (d = *t++) !=0) { + if ((c = *++s) >= 'A' && c <= 'Z') + c += 'a' - 'A'; + if (c != d) + return 0; + } + *sp = s + 1; + return 1; + } +#endif /* INFNAN_CHECK */ + + void +#ifdef KR_headers +copybits(c, n, b) ULong *c; int n; Bigint *b; +#else +copybits(ULong *c, int n, Bigint *b) +#endif +{ + ULong *ce, *x, *xe; +#ifdef Pack_16 + int nw, nw1; +#endif + + ce = c + ((unsigned int)(n-1) >> kshift) + 1; + x = b->x; +#ifdef Pack_32 + xe = x + b->wds; + while(x < xe) + *c++ = *x++; +#else + nw = b->wds; + nw1 = nw & 1; + for(xe = x + (nw - nw1); x < xe; x += 2) + Storeinc(c, x[1], x[0]); + if (nw1) + *c++ = *x; +#endif + while(c < ce) + *c++ = 0; + } + + ULong +#ifdef KR_headers +any_on(b, k) Bigint *b; int k; +#else +any_on(Bigint *b, int k) +#endif +{ + int n, nwds; + ULong *x, *x0, x1, x2; + + x = b->x; + nwds = b->wds; + n = (unsigned int)k >> kshift; + if (n > nwds) + n = nwds; + else if (n < nwds && (k &= kmask)) { + x1 = x2 = x[n]; + x1 >>= k; + x1 <<= k; + if (x1 != x2) + return 1; + } + x0 = x; + x += n; + while(x > x0) + if (*--x) + return 1; + return 0; + } diff --git a/lib/nbsd_libc/gdtoa/strtoIQ.c b/lib/nbsd_libc/gdtoa/strtoIQ.c new file mode 100644 index 000000000..dfb5aaf23 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtoIQ.c @@ -0,0 +1,69 @@ +/* $NetBSD: strtoIQ.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtoIQ(s, sp, a, b) CONST char *s; char **sp; void *a; void *b; +#else +strtoIQ(CONST char *s, char **sp, void *a, void *b) +#endif +{ + static FPI fpi = { 113, 1-16383-113+1, 32766-16383-113+1, 1, SI }; + Long exp[2]; + Bigint *B[2]; + int k, rv[2]; + ULong *L = (ULong *)a, *M = (ULong *)b; + + B[0] = Balloc(2); + if (B[0] == NULL) + return STRTOG_NoMemory; + B[0]->wds = 4; + k = strtoIg(s, sp, &fpi, exp, B, rv); + if (k == STRTOG_NoMemory) + return k; + ULtoQ(L, B[0]->x, exp[0], rv[0]); + Bfree(B[0]); + if (B[1]) { + ULtoQ(M, B[1]->x, exp[1], rv[1]); + Bfree(B[1]); + } + else { + M[0] = L[0]; + M[1] = L[1]; + M[2] = L[2]; + M[3] = L[3]; + } + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtoId.c b/lib/nbsd_libc/gdtoa/strtoId.c new file mode 100644 index 000000000..7cbfca213 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtoId.c @@ -0,0 +1,66 @@ +/* $NetBSD: strtoId.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtoId(s, sp, f0, f1) CONST char *s; char **sp; double *f0, *f1; +#else +strtoId(CONST char *s, char **sp, double *f0, double *f1) +#endif +{ + static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; + Long exp[2]; + Bigint *B[2]; + int k, rv[2]; + + B[0] = Balloc(1); + if (B[0] == NULL) + return STRTOG_NoMemory; + B[0]->wds = 2; + k = strtoIg(s, sp, &fpi, exp, B, rv); + if (k == STRTOG_NoMemory) + return k; + ULtod((ULong*)f0, B[0]->x, exp[0], rv[0]); + Bfree(B[0]); + if (B[1]) { + ULtod((ULong*)f1, B[1]->x, exp[1], rv[1]); + Bfree(B[1]); + } + else { + ((ULong*)f1)[0] = ((ULong*)f0)[0]; + ((ULong*)f1)[1] = ((ULong*)f0)[1]; + } + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtoIdd.c b/lib/nbsd_libc/gdtoa/strtoIdd.c new file mode 100644 index 000000000..cf938ea45 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtoIdd.c @@ -0,0 +1,72 @@ +/* $NetBSD: strtoIdd.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtoIdd(s, sp, f0, f1) CONST char *s; char **sp; double *f0, *f1; +#else +strtoIdd(CONST char *s, char **sp, double *f0, double *f1) +#endif +{ +#ifdef Sudden_Underflow + static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; +#else + static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; +#endif + Long exp[2]; + Bigint *B[2]; + int k, rv[2]; + + B[0] = Balloc(2); + if (B[0] == NULL) + return STRTOG_NoMemory; + B[0]->wds = 4; + k = strtoIg(s, sp, &fpi, exp, B, rv); + if (k == STRTOG_NoMemory) + return k; + ULtodd((ULong*)f0, B[0]->x, exp[0], rv[0]); + Bfree(B[0]); + if (B[1]) { + ULtodd((ULong*)f1, B[1]->x, exp[1], rv[1]); + Bfree(B[1]); + } + else { + ((ULong*)f1)[0] = ((ULong*)f0)[0]; + ((ULong*)f1)[1] = ((ULong*)f0)[1]; + ((ULong*)f1)[2] = ((ULong*)f0)[2]; + ((ULong*)f1)[3] = ((ULong*)f0)[3]; + } + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtoIf.c b/lib/nbsd_libc/gdtoa/strtoIf.c new file mode 100644 index 000000000..087acdc71 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtoIf.c @@ -0,0 +1,64 @@ +/* $NetBSD: strtoIf.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtoIf(s, sp, f0, f1) CONST char *s; char **sp; float *f0, *f1; +#else +strtoIf(CONST char *s, char **sp, float *f0, float *f1) +#endif +{ + static FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; + Long exp[2]; + Bigint *B[2]; + int k, rv[2]; + + B[0] = Balloc(0); + if (B[0] == NULL) + return STRTOG_NoMemory; + B[0]->wds = 1; + k = strtoIg(s, sp, &fpi, exp, B, rv); + if (k == STRTOG_NoMemory) + return k; + ULtof((ULong*)f0, B[0]->x, exp[0], rv[0]); + Bfree(B[0]); + if (B[1]) { + ULtof((ULong*)f1, B[1]->x, exp[1], rv[1]); + Bfree(B[1]); + } + else + *(ULong*)f1 = *(ULong*)f0; + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtoIg.c b/lib/nbsd_libc/gdtoa/strtoIg.c new file mode 100644 index 000000000..20a4c1cbd --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtoIg.c @@ -0,0 +1,139 @@ +/* $NetBSD: strtoIg.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtoIg(s00, se, fpi, exp, B, rvp) CONST char *s00; char **se; FPI *fpi; Long *exp; Bigint **B; int *rvp; +#else +strtoIg(CONST char *s00, char **se, FPI *fpi, Long *exp, Bigint **B, int *rvp) +#endif +{ + Bigint *b, *b1; + int i, nb, nw, nw1, rv, rv1, swap; + unsigned int nb1, nb11; + Long e1; + + b = *B; + rv = strtodg(s00, se, fpi, exp, b->x); + if (rv == STRTOG_NoMemory) + return rv; + if (!(rv & STRTOG_Inexact)) { + B[1] = 0; + return *rvp = rv; + } + e1 = exp[0]; + rv1 = rv ^ STRTOG_Inexact; + b1 = Balloc(b->k); + if (b1 == NULL) + return STRTOG_NoMemory; + Bcopy(b1, b); + nb = fpi->nbits; + nb1 = nb & 31; + nb11 = (nb1 - 1) & 31; + nw = b->wds; + nw1 = nw - 1; + if (rv & STRTOG_Inexlo) { + swap = 0; + b1 = increment(b1); + if (fpi->sudden_underflow + && (rv & STRTOG_Retmask) == STRTOG_Zero) { + b1->x[0] = 0; + b1->x[nw1] = 1L << nb11; + rv1 += STRTOG_Normal - STRTOG_Zero; + rv1 &= ~STRTOG_Underflow; + goto swapcheck; + } + if (b1->wds > nw + || nb1 && b1->x[nw1] & 1L << nb1) { + if (++e1 > fpi->emax) + rv1 = STRTOG_Infinite | STRTOG_Inexhi; + rshift(b1, 1); + } + else if ((rv & STRTOG_Retmask) == STRTOG_Denormal) { + if (b1->x[nw1] & 1L << nb11) { + rv1 += STRTOG_Normal - STRTOG_Denormal; + rv1 &= ~STRTOG_Underflow; + } + } + } + else { + swap = STRTOG_Neg; + if ((rv & STRTOG_Retmask) == STRTOG_Infinite) { + b1 = set_ones(b1, nb); + e1 = fpi->emax; + rv1 = STRTOG_Normal | STRTOG_Inexlo; + goto swapcheck; + } + decrement(b1); + if ((rv & STRTOG_Retmask) == STRTOG_Denormal) { + for(i = nw1; !b1->x[i]; --i) + if (!i) { + rv1 = STRTOG_Zero | STRTOG_Inexlo; + break; + } + goto swapcheck; + } + if (!(b1->x[nw1] & 1L << nb11)) { + if (e1 == fpi->emin) { + if (fpi->sudden_underflow) + rv1 += STRTOG_Zero - STRTOG_Normal; + else + rv1 += STRTOG_Denormal - STRTOG_Normal; + rv1 |= STRTOG_Underflow; + } + else { + b1 = lshift(b1, 1); + b1->x[0] |= 1; + --e1; + } + } + } + swapcheck: + if (swap ^ (rv & STRTOG_Neg)) { + rvp[0] = rv1; + rvp[1] = rv; + B[0] = b1; + B[1] = b; + exp[1] = exp[0]; + exp[0] = e1; + } + else { + rvp[0] = rv; + rvp[1] = rv1; + B[1] = b1; + exp[1] = e1; + } + return rv; + } diff --git a/lib/nbsd_libc/gdtoa/strtoIx.c b/lib/nbsd_libc/gdtoa/strtoIx.c new file mode 100644 index 000000000..966e4e364 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtoIx.c @@ -0,0 +1,70 @@ +/* $NetBSD: strtoIx.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtoIx(s, sp, a, b) CONST char *s; char **sp; void *a; void *b; +#else +strtoIx(CONST char *s, char **sp, void *a, void *b) +#endif +{ + static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; + Long exp[2]; + Bigint *B[2]; + int k, rv[2]; + UShort *L = (UShort *)a, *M = (UShort *)b; + + B[0] = Balloc(1); + if (B[0] == NULL) + return STRTOG_NoMemory; + B[0]->wds = 2; + k = strtoIg(s, sp, &fpi, exp, B, rv); + if (k == STRTOG_NoMemory) + return k; + ULtox(L, B[0]->x, exp[0], rv[0]); + Bfree(B[0]); + if (B[1]) { + ULtox(M, B[1]->x, exp[1], rv[1]); + Bfree(B[1]); + } + else { + M[0] = L[0]; + M[1] = L[1]; + M[2] = L[2]; + M[3] = L[3]; + M[4] = L[4]; + } + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtoIxL.c b/lib/nbsd_libc/gdtoa/strtoIxL.c new file mode 100644 index 000000000..e61069674 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtoIxL.c @@ -0,0 +1,68 @@ +/* $NetBSD: strtoIxL.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtoIxL(s, sp, a, b) CONST char *s; char **sp; void *a; void *b; +#else +strtoIxL(CONST char *s, char **sp, void *a, void *b) +#endif +{ + static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; + Long exp[2]; + Bigint *B[2]; + int k, rv[2]; + ULong *L = (ULong *)a, *M = (ULong *)b; + + B[0] = Balloc(1); + if (B[0] == NULL) + return STRTOG_NoMemory; + B[0]->wds = 2; + k = strtoIg(s, sp, &fpi, exp, B, rv); + if (k == STRTOG_NoMemory) + return k; + ULtoxL(L, B[0]->x, exp[0], rv[0]); + Bfree(B[0]); + if (B[1]) { + ULtoxL(M, B[1]->x, exp[1], rv[1]); + Bfree(B[1]); + } + else { + M[0] = L[0]; + M[1] = L[1]; + M[2] = L[2]; + } + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtod.c b/lib/nbsd_libc/gdtoa/strtod.c new file mode 100644 index 000000000..b37119301 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtod.c @@ -0,0 +1,1018 @@ +/* $NetBSD: strtod.c,v 1.5 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998-2001 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" +#ifndef NO_FENV_H +#include +#endif + +#ifdef USE_LOCALE +#include "locale.h" +#endif + +#ifdef IEEE_Arith +#ifndef NO_IEEE_Scale +#define Avoid_Underflow +#undef tinytens +/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */ +/* flag unnecessarily. It leads to a song and dance at the end of strtod. */ +static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, + 9007199254740992.e-256 + }; +#endif +#endif + +#ifdef Honor_FLT_ROUNDS +#define Rounding rounding +#undef Check_FLT_ROUNDS +#define Check_FLT_ROUNDS +#else +#define Rounding Flt_Rounds +#endif + +#ifndef __HAVE_LONG_DOUBLE +__strong_alias(_strtold, strtod) +__weak_alias(strtold, _strtold) +#endif + + double +strtod +#ifdef KR_headers + (s00, se) CONST char *s00; char **se; +#else + (CONST char *s00, char **se) +#endif +{ +#ifdef Avoid_Underflow + int scale; +#endif + int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, decpt, dsign, + e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; + CONST char *s, *s0, *s1; + double aadj, aadj1, adj, rv, rv0; + Long L; + ULong y, z; + Bigint *bb = NULL, *bb1, *bd0; + Bigint *bd = NULL, *bs = NULL, *delta = NULL; /* pacify gcc */ +#ifdef SET_INEXACT + int inexact, oldinexact; +#endif +#ifdef Honor_FLT_ROUNDS + int rounding; +#endif + + sign = nz0 = nz = decpt = 0; + dval(rv) = 0.; + for(s = s00;;s++) switch(*s) { + case '-': + sign = 1; + /* FALLTHROUGH */ + case '+': + if (*++s) + goto break2; + /* FALLTHROUGH */ + case 0: + goto ret0; + case '\t': + case '\n': + case '\v': + case '\f': + case '\r': + case ' ': + continue; + default: + goto break2; + } + break2: + if (*s == '0') { +#ifndef NO_HEX_FP + { + static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; + Long expt; + ULong bits[2]; + switch(s[1]) { + case 'x': + case 'X': + { +#if defined(FE_DOWNWARD) && defined(FE_TONEAREST) && defined(FE_TOWARDZERO) && defined(FE_UPWARD) + FPI fpi1 = fpi; + switch(fegetround()) { + case FE_TOWARDZERO: fpi1.rounding = 0; break; + case FE_UPWARD: fpi1.rounding = 2; break; + case FE_DOWNWARD: fpi1.rounding = 3; + } +#else +#define fpi1 fpi +#endif + switch((i = gethex(&s, &fpi1, &expt, &bb, sign)) & STRTOG_Retmask) { + case STRTOG_NoNumber: + s = s00; + sign = 0; + /* FALLTHROUGH */ + case STRTOG_Zero: + break; + default: + if (bb) { + copybits(bits, fpi.nbits, bb); + Bfree(bb); + } + ULtod((/* LINTED */(U*)&rv)->L, bits, expt, i); + }} + goto ret; + } + } +#endif + nz0 = 1; + while(*++s == '0') ; + if (!*s) + goto ret; + } + s0 = s; + y = z = 0; + for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) + if (nd < 9) + y = 10*y + c - '0'; + else if (nd < 16) + z = 10*z + c - '0'; + nd0 = nd; +#ifdef USE_LOCALE + if (c == *localeconv()->decimal_point) +#else + if (c == '.') +#endif + { + decpt = 1; + c = *++s; + if (!nd) { + for(; c == '0'; c = *++s) + nz++; + if (c > '0' && c <= '9') { + s0 = s; + nf += nz; + nz = 0; + goto have_dig; + } + goto dig_done; + } + for(; c >= '0' && c <= '9'; c = *++s) { + have_dig: + nz++; + if (c -= '0') { + nf += nz; + for(i = 1; i < nz; i++) + if (nd++ < 9) + y *= 10; + else if (nd <= DBL_DIG + 1) + z *= 10; + if (nd++ < 9) + y = 10*y + c; + else if (nd <= DBL_DIG + 1) + z = 10*z + c; + nz = 0; + } + } + } + dig_done: + e = 0; + if (c == 'e' || c == 'E') { + if (!nd && !nz && !nz0) { + goto ret0; + } + s00 = s; + esign = 0; + switch(c = *++s) { + case '-': + esign = 1; + /* FALLTHROUGH */ + case '+': + c = *++s; + } + if (c >= '0' && c <= '9') { + while(c == '0') + c = *++s; + if (c > '0' && c <= '9') { + L = c - '0'; + s1 = s; + while((c = *++s) >= '0' && c <= '9') + L = 10*L + c - '0'; + if (s - s1 > 8 || L > 19999) + /* Avoid confusion from exponents + * so large that e might overflow. + */ + e = 19999; /* safe for 16 bit ints */ + else + e = (int)L; + if (esign) + e = -e; + } + else + e = 0; + } + else + s = s00; + } + if (!nd) { + if (!nz && !nz0) { +#ifdef INFNAN_CHECK + /* Check for Nan and Infinity */ + ULong bits[2]; + static FPI fpinan = /* only 52 explicit bits */ + { 52, 1-1023-53+1, 2046-1023-53+1, 1, SI }; + if (!decpt) + switch(c) { + case 'i': + case 'I': + if (match(&s,"nf")) { + --s; + if (!match(&s,"inity")) + ++s; + word0(rv) = 0x7ff00000; + word1(rv) = 0; + goto ret; + } + break; + case 'n': + case 'N': + if (match(&s, "an")) { +#ifndef No_Hex_NaN + if (*s == '(' /*)*/ + && hexnan(&s, &fpinan, bits) + == STRTOG_NaNbits) { + word0(rv) = 0x7ff00000 | bits[1]; + word1(rv) = bits[0]; + } + else { +#endif + word0(rv) = NAN_WORD0; + word1(rv) = NAN_WORD1; +#ifndef No_Hex_NaN + } +#endif + goto ret; + } + } +#endif /* INFNAN_CHECK */ + ret0: + s = s00; + sign = 0; + } + goto ret; + } + e1 = e -= nf; + + /* Now we have nd0 digits, starting at s0, followed by a + * decimal point, followed by nd-nd0 digits. The number we're + * after is the integer represented by those digits times + * 10**e */ + + if (!nd0) + nd0 = nd; + k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; + dval(rv) = y; + if (k > 9) { +#ifdef SET_INEXACT + if (k > DBL_DIG) + oldinexact = get_inexact(); +#endif + dval(rv) = tens[k - 9] * dval(rv) + z; + } + bd0 = 0; + if (nd <= DBL_DIG +#ifndef RND_PRODQUOT +#ifndef Honor_FLT_ROUNDS + && Flt_Rounds == 1 +#endif +#endif + ) { + if (!e) + goto ret; + if (e > 0) { + if (e <= Ten_pmax) { +#ifdef VAX + goto vax_ovfl_check; +#else +#ifdef Honor_FLT_ROUNDS + /* round correctly FLT_ROUNDS = 2 or 3 */ + if (sign) { + rv = -rv; + sign = 0; + } +#endif + /* rv = */ rounded_product(dval(rv), tens[e]); + goto ret; +#endif + } + i = DBL_DIG - nd; + if (e <= Ten_pmax + i) { + /* A fancier test would sometimes let us do + * this for larger i values. + */ +#ifdef Honor_FLT_ROUNDS + /* round correctly FLT_ROUNDS = 2 or 3 */ + if (sign) { + rv = -rv; + sign = 0; + } +#endif + e -= i; + dval(rv) *= tens[i]; +#ifdef VAX + /* VAX exponent range is so narrow we must + * worry about overflow here... + */ + vax_ovfl_check: + word0(rv) -= P*Exp_msk1; + /* rv = */ rounded_product(dval(rv), tens[e]); + if ((word0(rv) & Exp_mask) + > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) + goto ovfl; + word0(rv) += P*Exp_msk1; +#else + /* rv = */ rounded_product(dval(rv), tens[e]); +#endif + goto ret; + } + } +#ifndef Inaccurate_Divide + else if (e >= -Ten_pmax) { +#ifdef Honor_FLT_ROUNDS + /* round correctly FLT_ROUNDS = 2 or 3 */ + if (sign) { + rv = -rv; + sign = 0; + } +#endif + /* rv = */ rounded_quotient(dval(rv), tens[-e]); + goto ret; + } +#endif + } + e1 += nd - k; + +#ifdef IEEE_Arith +#ifdef SET_INEXACT + inexact = 1; + if (k <= DBL_DIG) + oldinexact = get_inexact(); +#endif +#ifdef Avoid_Underflow + scale = 0; +#endif +#ifdef Honor_FLT_ROUNDS + if ((rounding = Flt_Rounds) >= 2) { + if (sign) + rounding = rounding == 2 ? 0 : 2; + else + if (rounding != 2) + rounding = 0; + } +#endif +#endif /*IEEE_Arith*/ + + /* Get starting approximation = rv * 10**e1 */ + + if (e1 > 0) { + if ( (i = e1 & 15) !=0) + dval(rv) *= tens[i]; + if (e1 &= ~15) { + if (e1 > DBL_MAX_10_EXP) { + ovfl: +#ifndef NO_ERRNO + errno = ERANGE; +#endif + /* Can't trust HUGE_VAL */ +#ifdef IEEE_Arith +#ifdef Honor_FLT_ROUNDS + switch(rounding) { + case 0: /* toward 0 */ + case 3: /* toward -infinity */ + word0(rv) = Big0; + word1(rv) = Big1; + break; + default: + word0(rv) = Exp_mask; + word1(rv) = 0; + } +#else /*Honor_FLT_ROUNDS*/ + word0(rv) = Exp_mask; + word1(rv) = 0; +#endif /*Honor_FLT_ROUNDS*/ +#ifdef SET_INEXACT + /* set overflow bit */ + dval(rv0) = 1e300; + dval(rv0) *= dval(rv0); +#endif +#else /*IEEE_Arith*/ + word0(rv) = Big0; + word1(rv) = Big1; +#endif /*IEEE_Arith*/ + if (bd0) + goto retfree; + goto ret; + } + e1 = (unsigned int)e1 >> 4; + for(j = 0; e1 > 1; j++, e1 = (unsigned int)e1 >> 1) + if (e1 & 1) + dval(rv) *= bigtens[j]; + /* The last multiplication could overflow. */ + word0(rv) -= P*Exp_msk1; + dval(rv) *= bigtens[j]; + if ((z = word0(rv) & Exp_mask) + > Exp_msk1*(DBL_MAX_EXP+Bias-P)) + goto ovfl; + if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { + /* set to largest number */ + /* (Can't trust DBL_MAX) */ + word0(rv) = Big0; + word1(rv) = Big1; + } + else + word0(rv) += P*Exp_msk1; + } + } + else if (e1 < 0) { + e1 = -e1; + if ( (i = e1 & 15) !=0) + dval(rv) /= tens[i]; + if (e1 >>= 4) { + if (e1 >= 1 << n_bigtens) + goto undfl; +#ifdef Avoid_Underflow + if (e1 & Scale_Bit) + scale = 2*P; + for(j = 0; e1 > 0; j++, e1 = (unsigned int)e1 >> 1) + if (e1 & 1) + dval(rv) *= tinytens[j]; + if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask) + >> Exp_shift)) > 0) { + /* scaled rv is denormal; zap j low bits */ + if (j >= 32) { + word1(rv) = 0; + if (j >= 53) + word0(rv) = (P+2)*Exp_msk1; + else + word0(rv) &= 0xffffffff << (j-32); + } + else + word1(rv) &= 0xffffffff << j; + } +#else + for(j = 0; e1 > 1; j++, e1 >>= 1) + if (e1 & 1) + dval(rv) *= tinytens[j]; + /* The last multiplication could underflow. */ + dval(rv0) = dval(rv); + dval(rv) *= tinytens[j]; + if (!dval(rv)) { + dval(rv) = 2.*dval(rv0); + dval(rv) *= tinytens[j]; +#endif + if (!dval(rv)) { + undfl: + dval(rv) = 0.; +#ifndef NO_ERRNO + errno = ERANGE; +#endif + if (bd0) + goto retfree; + goto ret; + } +#ifndef Avoid_Underflow + word0(rv) = Tiny0; + word1(rv) = Tiny1; + /* The refinement below will clean + * this approximation up. + */ + } +#endif + } + } + + /* Now the hard part -- adjusting rv to the correct value.*/ + + /* Put digits into bd: true value = bd * 10^e */ + + bd0 = s2b(s0, nd0, nd, y); + if (bd0 == NULL) + goto ovfl; + + for(;;) { + bd = Balloc(bd0->k); + if (bd == NULL) + goto ovfl; + Bcopy(bd, bd0); + bb = d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */ + if (bb == NULL) + goto ovfl; + bs = i2b(1); + if (bs == NULL) + goto ovfl; + + if (e >= 0) { + bb2 = bb5 = 0; + bd2 = bd5 = e; + } + else { + bb2 = bb5 = -e; + bd2 = bd5 = 0; + } + if (bbe >= 0) + bb2 += bbe; + else + bd2 -= bbe; + bs2 = bb2; +#ifdef Honor_FLT_ROUNDS + if (rounding != 1) + bs2++; +#endif +#ifdef Avoid_Underflow + j = bbe - scale; + i = j + bbbits - 1; /* logb(rv) */ + if (i < Emin) /* denormal */ + j += P - Emin; + else + j = P + 1 - bbbits; +#else /*Avoid_Underflow*/ +#ifdef Sudden_Underflow +#ifdef IBM + j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); +#else + j = P + 1 - bbbits; +#endif +#else /*Sudden_Underflow*/ + j = bbe; + i = j + bbbits - 1; /* logb(rv) */ + if (i < Emin) /* denormal */ + j += P - Emin; + else + j = P + 1 - bbbits; +#endif /*Sudden_Underflow*/ +#endif /*Avoid_Underflow*/ + bb2 += j; + bd2 += j; +#ifdef Avoid_Underflow + bd2 += scale; +#endif + i = bb2 < bd2 ? bb2 : bd2; + if (i > bs2) + i = bs2; + if (i > 0) { + bb2 -= i; + bd2 -= i; + bs2 -= i; + } + if (bb5 > 0) { + bs = pow5mult(bs, bb5); + if (bs == NULL) + goto ovfl; + bb1 = mult(bs, bb); + if (bb1 == NULL) + goto ovfl; + Bfree(bb); + bb = bb1; + } + if (bb2 > 0) { + bb = lshift(bb, bb2); + if (bb == NULL) + goto ovfl; + } + if (bd5 > 0) { + bd = pow5mult(bd, bd5); + if (bd == NULL) + goto ovfl; + } + if (bd2 > 0) { + bd = lshift(bd, bd2); + if (bd == NULL) + goto ovfl; + } + if (bs2 > 0) { + bs = lshift(bs, bs2); + if (bs == NULL) + goto ovfl; + } + delta = diff(bb, bd); + if (delta == NULL) + goto ovfl; + dsign = delta->sign; + delta->sign = 0; + i = cmp(delta, bs); +#ifdef Honor_FLT_ROUNDS + if (rounding != 1) { + if (i < 0) { + /* Error is less than an ulp */ + if (!delta->x[0] && delta->wds <= 1) { + /* exact */ +#ifdef SET_INEXACT + inexact = 0; +#endif + break; + } + if (rounding) { + if (dsign) { + adj = 1.; + goto apply_adj; + } + } + else if (!dsign) { + adj = -1.; + if (!word1(rv) + && !(word0(rv) & Frac_mask)) { + y = word0(rv) & Exp_mask; +#ifdef Avoid_Underflow + if (!scale || y > 2*P*Exp_msk1) +#else + if (y) +#endif + { + delta = lshift(delta,Log2P); + if (cmp(delta, bs) <= 0) + adj = -0.5; + } + } + apply_adj: +#ifdef Avoid_Underflow + if (scale && (y = word0(rv) & Exp_mask) + <= 2*P*Exp_msk1) + word0(adj) += (2*P+1)*Exp_msk1 - y; +#else +#ifdef Sudden_Underflow + if ((word0(rv) & Exp_mask) <= + P*Exp_msk1) { + word0(rv) += P*Exp_msk1; + dval(rv) += adj*ulp(dval(rv)); + word0(rv) -= P*Exp_msk1; + } + else +#endif /*Sudden_Underflow*/ +#endif /*Avoid_Underflow*/ + dval(rv) += adj*ulp(dval(rv)); + } + break; + } + adj = ratio(delta, bs); + if (adj < 1.) + adj = 1.; + if (adj <= 0x7ffffffe) { + /* adj = rounding ? ceil(adj) : floor(adj); */ + y = adj; + if (y != adj) { + if (!((rounding>>1) ^ dsign)) + y++; + adj = y; + } + } +#ifdef Avoid_Underflow + if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) + word0(adj) += (2*P+1)*Exp_msk1 - y; +#else +#ifdef Sudden_Underflow + if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { + word0(rv) += P*Exp_msk1; + adj *= ulp(dval(rv)); + if (dsign) + dval(rv) += adj; + else + dval(rv) -= adj; + word0(rv) -= P*Exp_msk1; + goto cont; + } +#endif /*Sudden_Underflow*/ +#endif /*Avoid_Underflow*/ + adj *= ulp(dval(rv)); + if (dsign) + dval(rv) += adj; + else + dval(rv) -= adj; + goto cont; + } +#endif /*Honor_FLT_ROUNDS*/ + + if (i < 0) { + /* Error is less than half an ulp -- check for + * special case of mantissa a power of two. + */ + if (dsign || word1(rv) || word0(rv) & Bndry_mask +#ifdef IEEE_Arith +#ifdef Avoid_Underflow + || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1 +#else + || (word0(rv) & Exp_mask) <= Exp_msk1 +#endif +#endif + ) { +#ifdef SET_INEXACT + if (!delta->x[0] && delta->wds <= 1) + inexact = 0; +#endif + break; + } + if (!delta->x[0] && delta->wds <= 1) { + /* exact result */ +#ifdef SET_INEXACT + inexact = 0; +#endif + break; + } + delta = lshift(delta,Log2P); + if (cmp(delta, bs) > 0) + goto drop_down; + break; + } + if (i == 0) { + /* exactly half-way between */ + if (dsign) { + if ((word0(rv) & Bndry_mask1) == Bndry_mask1 + && word1(rv) == ( +#ifdef Avoid_Underflow + (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) + ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : +#endif + 0xffffffff)) { + /*boundary case -- increment exponent*/ + word0(rv) = (word0(rv) & Exp_mask) + + Exp_msk1 +#ifdef IBM + | Exp_msk1 >> 4 +#endif + ; + word1(rv) = 0; +#ifdef Avoid_Underflow + dsign = 0; +#endif + break; + } + } + else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { + drop_down: + /* boundary case -- decrement exponent */ +#ifdef Sudden_Underflow /*{{*/ + L = word0(rv) & Exp_mask; +#ifdef IBM + if (L < Exp_msk1) +#else +#ifdef Avoid_Underflow + if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1)) +#else + if (L <= Exp_msk1) +#endif /*Avoid_Underflow*/ +#endif /*IBM*/ + goto undfl; + L -= Exp_msk1; +#else /*Sudden_Underflow}{*/ +#ifdef Avoid_Underflow + if (scale) { + L = word0(rv) & Exp_mask; + if (L <= (2*P+1)*Exp_msk1) { + if (L > (P+2)*Exp_msk1) + /* round even ==> */ + /* accept rv */ + break; + /* rv = smallest denormal */ + goto undfl; + } + } +#endif /*Avoid_Underflow*/ + L = (word0(rv) & Exp_mask) - Exp_msk1; +#endif /*Sudden_Underflow}*/ + word0(rv) = L | Bndry_mask1; + word1(rv) = 0xffffffff; +#ifdef IBM + goto cont; +#else + break; +#endif + } +#ifndef ROUND_BIASED + if (!(word1(rv) & LSB)) + break; +#endif + if (dsign) + dval(rv) += ulp(dval(rv)); +#ifndef ROUND_BIASED + else { + dval(rv) -= ulp(dval(rv)); +#ifndef Sudden_Underflow + if (!dval(rv)) + goto undfl; +#endif + } +#ifdef Avoid_Underflow + dsign = 1 - dsign; +#endif +#endif + break; + } + if ((aadj = ratio(delta, bs)) <= 2.) { + if (dsign) + aadj = aadj1 = 1.; + else if (word1(rv) || word0(rv) & Bndry_mask) { +#ifndef Sudden_Underflow + if (word1(rv) == Tiny1 && !word0(rv)) + goto undfl; +#endif + aadj = 1.; + aadj1 = -1.; + } + else { + /* special case -- power of FLT_RADIX to be */ + /* rounded down... */ + + if (aadj < 2./FLT_RADIX) + aadj = 1./FLT_RADIX; + else + aadj *= 0.5; + aadj1 = -aadj; + } + } + else { + aadj *= 0.5; + aadj1 = dsign ? aadj : -aadj; +#ifdef Check_FLT_ROUNDS + switch(Rounding) { + case 2: /* towards +infinity */ + aadj1 -= 0.5; + break; + case 0: /* towards 0 */ + case 3: /* towards -infinity */ + aadj1 += 0.5; + } +#else + if (Flt_Rounds == 0) + aadj1 += 0.5; +#endif /*Check_FLT_ROUNDS*/ + } + y = word0(rv) & Exp_mask; + + /* Check for overflow */ + + if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { + dval(rv0) = dval(rv); + word0(rv) -= P*Exp_msk1; + adj = aadj1 * ulp(dval(rv)); + dval(rv) += adj; + if ((word0(rv) & Exp_mask) >= + Exp_msk1*(DBL_MAX_EXP+Bias-P)) { + if (word0(rv0) == Big0 && word1(rv0) == Big1) + goto ovfl; + word0(rv) = Big0; + word1(rv) = Big1; + goto cont; + } + else + word0(rv) += P*Exp_msk1; + } + else { +#ifdef Avoid_Underflow + if (scale && y <= 2*P*Exp_msk1) { + if (aadj <= 0x7fffffff) { + if ((z = aadj) == 0) + z = 1; + aadj = z; + aadj1 = dsign ? aadj : -aadj; + } + word0(aadj1) += (2*P+1)*Exp_msk1 - y; + } + adj = aadj1 * ulp(dval(rv)); + dval(rv) += adj; +#else +#ifdef Sudden_Underflow + if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { + dval(rv0) = dval(rv); + word0(rv) += P*Exp_msk1; + adj = aadj1 * ulp(dval(rv)); + dval(rv) += adj; +#ifdef IBM + if ((word0(rv) & Exp_mask) < P*Exp_msk1) +#else + if ((word0(rv) & Exp_mask) <= P*Exp_msk1) +#endif + { + if (word0(rv0) == Tiny0 + && word1(rv0) == Tiny1) + goto undfl; + word0(rv) = Tiny0; + word1(rv) = Tiny1; + goto cont; + } + else + word0(rv) -= P*Exp_msk1; + } + else { + adj = aadj1 * ulp(dval(rv)); + dval(rv) += adj; + } +#else /*Sudden_Underflow*/ + /* Compute adj so that the IEEE rounding rules will + * correctly round rv + adj in some half-way cases. + * If rv * ulp(rv) is denormalized (i.e., + * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid + * trouble from bits lost to denormalization; + * example: 1.2e-307 . + */ + if (y <= (P-1)*Exp_msk1 && aadj > 1.) { + aadj1 = (double)(int)(aadj + 0.5); + if (!dsign) + aadj1 = -aadj1; + } + adj = aadj1 * ulp(dval(rv)); + dval(rv) += adj; +#endif /*Sudden_Underflow*/ +#endif /*Avoid_Underflow*/ + } + z = word0(rv) & Exp_mask; +#ifndef SET_INEXACT +#ifdef Avoid_Underflow + if (!scale) +#endif + if (y == z) { + /* Can we stop now? */ + L = (Long)aadj; + aadj -= L; + /* The tolerances below are conservative. */ + if (dsign || word1(rv) || word0(rv) & Bndry_mask) { + if (aadj < .4999999 || aadj > .5000001) + break; + } + else if (aadj < .4999999/FLT_RADIX) + break; + } +#endif + cont: + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(delta); + } +#ifdef SET_INEXACT + if (inexact) { + if (!oldinexact) { + word0(rv0) = Exp_1 + (70 << Exp_shift); + word1(rv0) = 0; + dval(rv0) += 1.; + } + } + else if (!oldinexact) + clear_inexact(); +#endif +#ifdef Avoid_Underflow + if (scale) { + word0(rv0) = Exp_1 - 2*P*Exp_msk1; + word1(rv0) = 0; + dval(rv) *= dval(rv0); +#ifndef NO_ERRNO + /* try to avoid the bug of testing an 8087 register value */ + if (word0(rv) == 0 && word1(rv) == 0) + errno = ERANGE; +#endif + } +#endif /* Avoid_Underflow */ +#ifdef SET_INEXACT + if (inexact && !(word0(rv) & Exp_mask)) { + /* set underflow bit */ + dval(rv0) = 1e-300; + dval(rv0) *= dval(rv0); + } +#endif + retfree: + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(bd0); + Bfree(delta); + ret: + if (se) + *se = __UNCONST(s); + return sign ? -dval(rv) : dval(rv); + } + diff --git a/lib/nbsd_libc/gdtoa/strtodI.c b/lib/nbsd_libc/gdtoa/strtodI.c new file mode 100644 index 000000000..2631d5cbf --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtodI.c @@ -0,0 +1,171 @@ +/* $NetBSD: strtodI.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + static double +#ifdef KR_headers +ulpdown(d) double *d; +#else +ulpdown(double *d) +#endif +{ + double u; + ULong *L = (ULong*)d; + + u = ulp(*d); + if (!(L[_1] | L[_0] & 0xfffff) + && (L[_0] & 0x7ff00000) > 0x00100000) + u *= 0.5; + return u; + } + + int +#ifdef KR_headers +strtodI(s, sp, dd) CONST char *s; char **sp; double *dd; +#else +strtodI(CONST char *s, char **sp, double *dd) +#endif +{ + static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; + ULong bits[2], sign; + Long exp; + int j, k; + typedef union { + double d[2]; + ULong L[4]; + } U; + U *u; + + k = strtodg(s, sp, &fpi, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + u = (U*)dd; + sign = k & STRTOG_Neg ? 0x80000000L : 0; + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + u->d[0] = u->d[1] = 0.; + break; + + case STRTOG_Zero: + u->d[0] = u->d[1] = 0.; +#ifdef Sudden_Underflow + if (k & STRTOG_Inexact) { + if (sign) + u->L[_0] = 0x80100000L; + else + u->L[2+_0] = 0x100000L; + } + break; +#else + goto contain; +#endif + + case STRTOG_Denormal: + u->L[_1] = bits[0]; + u->L[_0] = bits[1]; + goto contain; + + case STRTOG_Normal: + u->L[_1] = bits[0]; + u->L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); + contain: + j = k & STRTOG_Inexact; + if (sign) { + u->L[_0] |= sign; + j = STRTOG_Inexact - j; + } + switch(j) { + case STRTOG_Inexlo: +#ifdef Sudden_Underflow + if ((u->L[_0] & 0x7ff00000) < 0x3500000) { + u->L[2+_0] = u->L[_0] + 0x3500000; + u->L[2+_1] = u->L[_1]; + u->d[1] += ulp(u->d[1]); + u->L[2+_0] -= 0x3500000; + if (!(u->L[2+_0] & 0x7ff00000)) { + u->L[2+_0] = sign; + u->L[2+_1] = 0; + } + } + else +#endif + u->d[1] = u->d[0] + ulp(u->d[0]); + break; + case STRTOG_Inexhi: + u->d[1] = u->d[0]; +#ifdef Sudden_Underflow + if ((u->L[_0] & 0x7ff00000) < 0x3500000) { + u->L[_0] += 0x3500000; + u->d[0] -= ulpdown(u->d); + u->L[_0] -= 0x3500000; + if (!(u->L[_0] & 0x7ff00000)) { + u->L[_0] = sign; + u->L[_1] = 0; + } + } + else +#endif + u->d[0] -= ulpdown(u->d); + break; + default: + u->d[1] = u->d[0]; + } + break; + + case STRTOG_Infinite: + u->L[_0] = u->L[2+_0] = sign | 0x7ff00000; + u->L[_1] = u->L[2+_1] = 0; + if (k & STRTOG_Inexact) { + if (sign) { + u->L[2+_0] = 0xffefffffL; + u->L[2+_1] = 0xffffffffL; + } + else { + u->L[_0] = 0x7fefffffL; + u->L[_1] = 0xffffffffL; + } + } + break; + + case STRTOG_NaN: + u->L[0] = u->L[2] = d_QNAN0; + u->L[1] = u->L[3] = d_QNAN1; + break; + + case STRTOG_NaNbits: + u->L[_0] = u->L[2+_0] = 0x7ff00000 | sign | bits[1]; + u->L[_1] = u->L[2+_1] = bits[0]; + } + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtodg.c b/lib/nbsd_libc/gdtoa/strtodg.c new file mode 100644 index 000000000..3b3c7be7f --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtodg.c @@ -0,0 +1,1069 @@ +/* $NetBSD: strtodg.c,v 1.6 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998-2001 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#ifdef USE_LOCALE +#include "locale.h" +#endif + + static CONST int +fivesbits[] = { 0, 3, 5, 7, 10, 12, 14, 17, 19, 21, + 24, 26, 28, 31, 33, 35, 38, 40, 42, 45, + 47, 49, 52 +#ifdef VAX + , 54, 56 +#endif + }; + + Bigint * +#ifdef KR_headers +increment(b) Bigint *b; +#else +increment(Bigint *b) +#endif +{ + ULong *x, *xe; + Bigint *b1; +#ifdef Pack_16 + ULong carry = 1, y; +#endif + + x = b->x; + xe = x + b->wds; +#ifdef Pack_32 + do { + if (*x < (ULong)0xffffffffL) { + ++*x; + return b; + } + *x++ = 0; + } while(x < xe); +#else + do { + y = *x + carry; + carry = y >> 16; + *x++ = y & 0xffff; + if (!carry) + return b; + } while(x < xe); + if (carry) +#endif + { + if (b->wds >= b->maxwds) { + b1 = Balloc(b->k+1); + if (b1 == NULL) + return NULL; + Bcopy(b1,b); + Bfree(b); + b = b1; + } + b->x[b->wds++] = 1; + } + return b; + } + + int +#ifdef KR_headers +decrement(b) Bigint *b; +#else +decrement(Bigint *b) +#endif +{ + ULong *x, *xe; +#ifdef Pack_16 + ULong borrow = 1, y; +#endif + + x = b->x; + xe = x + b->wds; +#ifdef Pack_32 + do { + if (*x) { + --*x; + break; + } + *x++ = 0xffffffffUL; + } + while(x < xe); +#else + do { + y = *x - borrow; + borrow = (y & 0x10000) >> 16; + *x++ = y & 0xffff; + } while(borrow && x < xe); +#endif + return STRTOG_Inexlo; + } + + static int +#ifdef KR_headers +all_on(b, n) CONST Bigint *b; int n; +#else +all_on(CONST Bigint *b, int n) +#endif +{ + CONST ULong *x, *xe; + + x = b->x; + xe = x + ((unsigned int)n >> kshift); + while(x < xe) + if ((*x++ & ALL_ON) != ALL_ON) + return 0; + if (n &= kmask) + return ((*x | (ALL_ON << n)) & ALL_ON) == ALL_ON; + return 1; + } + + Bigint * +#ifdef KR_headers +set_ones(b, n) Bigint *b; int n; +#else +set_ones(Bigint *b, int n) +#endif +{ + int k; + ULong *x, *xe; + + k = (unsigned int)(n + ((1 << kshift) - 1)) >> kshift; + if (b->k < k) { + Bfree(b); + b = Balloc(k); + if (b == NULL) + return NULL; + } + k = (unsigned int)n >> kshift; + if (n &= kmask) + k++; + b->wds = k; + x = b->x; + xe = x + k; + while(x < xe) + *x++ = ALL_ON; + if (n) + x[-1] >>= ULbits - n; + return b; + } + + static int +rvOK +#ifdef KR_headers + (d, fpi, expt, bits, exact, rd, irv) + double d; CONST FPI *fpi; Long *expt; ULong *bits; int exact, rd, *irv; +#else + (double d, CONST FPI *fpi, Long *expt, ULong *bits, int exact, int rd, int *irv) +#endif +{ + Bigint *b; + ULong carry, inex, lostbits; + int bdif, e, j, k, k1, nb, rv; + + carry = rv = 0; + b = d2b(d, &e, &bdif); + bdif -= nb = fpi->nbits; + e += bdif; + if (bdif <= 0) { + if (exact) + goto trunc; + goto ret; + } + if (P == nb) { + if ( +#ifndef IMPRECISE_INEXACT + exact && +#endif + fpi->rounding == +#ifdef RND_PRODQUOT + FPI_Round_near +#else + Flt_Rounds +#endif + ) goto trunc; + goto ret; + } + switch(rd) { + case 1: + goto trunc; + case 2: + break; + default: /* round near */ + k = bdif - 1; + if (!k) { + if (!exact) + goto ret; + if (b->x[0] & 2) + break; + goto trunc; + } + if (b->x[(unsigned int)k>>kshift] & ((ULong)1 << (k & kmask))) + break; + goto trunc; + } + /* "break" cases: round up 1 bit, then truncate; bdif > 0 */ + carry = 1; + trunc: + inex = lostbits = 0; + if (bdif > 0) { + if ( (lostbits = any_on(b, bdif)) !=0) + inex = STRTOG_Inexlo; + rshift(b, bdif); + if (carry) { + inex = STRTOG_Inexhi; + b = increment(b); + if ( (j = nb & kmask) !=0) + j = ULbits - j; + if (hi0bits(b->x[b->wds - 1]) != j) { + if (!lostbits) + lostbits = b->x[0] & 1; + rshift(b, 1); + e++; + } + } + } + else if (bdif < 0) + b = lshift(b, -bdif); + if (e < fpi->emin) { + k = fpi->emin - e; + e = fpi->emin; + if (k > nb || fpi->sudden_underflow) { + b->wds = inex = 0; + *irv = STRTOG_Underflow | STRTOG_Inexlo; + } + else { + k1 = k - 1; + if (k1 > 0 && !lostbits) + lostbits = any_on(b, k1); + if (!lostbits && !exact) + goto ret; + lostbits |= + carry = b->x[(unsigned int)k1>>kshift] & + (1 << (k1 & kmask)); + rshift(b, k); + *irv = STRTOG_Denormal; + if (carry) { + b = increment(b); + inex = STRTOG_Inexhi | STRTOG_Underflow; + } + else if (lostbits) + inex = STRTOG_Inexlo | STRTOG_Underflow; + } + } + else if (e > fpi->emax) { + e = fpi->emax + 1; + *irv = STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; +#ifndef NO_ERRNO + errno = ERANGE; +#endif + b->wds = inex = 0; + } + *expt = e; + copybits(bits, nb, b); + *irv |= inex; + rv = 1; + ret: + Bfree(b); + return rv; + } + +#ifndef VAX + static int +#ifdef KR_headers +mantbits(d) double d; +#else +mantbits(double d) +#endif +{ + ULong L; +#ifdef VAX + L = word1(d) << 16 | word1(d) >> 16; + if (L) +#else + if ( (L = word1(d)) !=0) +#endif + return P - lo0bits(&L); +#ifdef VAX + L = word0(d) << 16 | word0(d) >> 16 | Exp_msk11; +#else + L = word0(d) | Exp_msk1; +#endif + return P - 32 - lo0bits(&L); + } +#endif /* !VAX */ + + int +strtodg +#ifdef KR_headers + (s00, se, fpi, expt, bits) + CONST char *s00; char **se; CONST FPI *fpi; Long *expt; ULong *bits; +#else + (CONST char *s00, char **se, CONST FPI *fpi, Long *expt, ULong *bits) +#endif +{ + int abe, abits, asub; + int bb0, bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, decpt, denorm; + int dsign, e, e1, e2, emin, esign, finished, i, inex, irv; + int j, k, nbits, nd, nd0, nf, nz, nz0, rd, rvbits, rve, rve1, sign; + int sudden_underflow = 0; /* pacify gcc */ + CONST char *s, *s0, *s1; + double adj, adj0, rv, tol; + Long L; + ULong y, z; + Bigint *ab, *bb, *bb1, *bd, *bd0, *bs, *delta, *rvb, *rvb0; + + e2 = 0; /* XXX gcc */ + + irv = STRTOG_Zero; + denorm = sign = nz0 = nz = 0; + dval(rv) = 0.; + rvb = 0; + nbits = fpi->nbits; + for(s = s00;;s++) switch(*s) { + case '-': + sign = 1; + /* FALLTHROUGH */ + case '+': + if (*++s) + goto break2; + /* FALLTHROUGH */ + case 0: + sign = 0; + irv = STRTOG_NoNumber; + s = s00; + goto ret; + case '\t': + case '\n': + case '\v': + case '\f': + case '\r': + case ' ': + continue; + default: + goto break2; + } + break2: + if (*s == '0') { +#ifndef NO_HEX_FP + switch(s[1]) { + case 'x': + case 'X': + irv = gethex(&s, fpi, expt, &rvb, sign); + if (irv == STRTOG_NoNumber) { + s = s00; + sign = 0; + } + goto ret; + } +#endif + nz0 = 1; + while(*++s == '0') ; + if (!*s) + goto ret; + } + sudden_underflow = fpi->sudden_underflow; + s0 = s; + y = z = 0; + for(decpt = nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) + if (nd < 9) + y = 10*y + c - '0'; + else if (nd < 16) + z = 10*z + c - '0'; + nd0 = nd; +#ifdef USE_LOCALE + if (c == *localeconv()->decimal_point) +#else + if (c == '.') +#endif + { + decpt = 1; + c = *++s; + if (!nd) { + for(; c == '0'; c = *++s) + nz++; + if (c > '0' && c <= '9') { + s0 = s; + nf += nz; + nz = 0; + goto have_dig; + } + goto dig_done; + } + for(; c >= '0' && c <= '9'; c = *++s) { + have_dig: + nz++; + if (c -= '0') { + nf += nz; + for(i = 1; i < nz; i++) + if (nd++ < 9) + y *= 10; + else if (nd <= DBL_DIG + 1) + z *= 10; + if (nd++ < 9) + y = 10*y + c; + else if (nd <= DBL_DIG + 1) + z = 10*z + c; + nz = 0; + } + } + } + dig_done: + e = 0; + if (c == 'e' || c == 'E') { + if (!nd && !nz && !nz0) { + irv = STRTOG_NoNumber; + s = s00; + goto ret; + } + s00 = s; + esign = 0; + switch(c = *++s) { + case '-': + esign = 1; + /* FALLTHROUGH */ + case '+': + c = *++s; + } + if (c >= '0' && c <= '9') { + while(c == '0') + c = *++s; + if (c > '0' && c <= '9') { + L = c - '0'; + s1 = s; + while((c = *++s) >= '0' && c <= '9') + L = 10*L + c - '0'; + if (s - s1 > 8 || L > 19999) + /* Avoid confusion from exponents + * so large that e might overflow. + */ + e = 19999; /* safe for 16 bit ints */ + else + e = (int)L; + if (esign) + e = -e; + } + else + e = 0; + } + else + s = s00; + } + if (!nd) { + if (!nz && !nz0) { +#ifdef INFNAN_CHECK + /* Check for Nan and Infinity */ + if (!decpt) + switch(c) { + case 'i': + case 'I': + if (match(&s,"nf")) { + --s; + if (!match(&s,"inity")) + ++s; + irv = STRTOG_Infinite; + goto infnanexp; + } + break; + case 'n': + case 'N': + if (match(&s, "an")) { + irv = STRTOG_NaN; + *expt = fpi->emax + 1; +#ifndef No_Hex_NaN + if (*s == '(') /*)*/ + irv = hexnan(&s, fpi, bits); +#endif + goto infnanexp; + } + } +#endif /* INFNAN_CHECK */ + irv = STRTOG_NoNumber; + s = s00; + } + goto ret; + } + + irv = STRTOG_Normal; + e1 = e -= nf; + rd = 0; + switch(fpi->rounding & 3) { + case FPI_Round_up: + rd = 2 - sign; + break; + case FPI_Round_zero: + rd = 1; + break; + case FPI_Round_down: + rd = 1 + sign; + } + + /* Now we have nd0 digits, starting at s0, followed by a + * decimal point, followed by nd-nd0 digits. The number we're + * after is the integer represented by those digits times + * 10**e */ + + if (!nd0) + nd0 = nd; + k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; + dval(rv) = y; + if (k > 9) + dval(rv) = tens[k - 9] * dval(rv) + z; + bd0 = 0; + if (nbits <= P && nd <= DBL_DIG) { + if (!e) { + if (rvOK(dval(rv), fpi, expt, bits, 1, rd, &irv)) + goto ret; + } + else if (e > 0) { + if (e <= Ten_pmax) { +#ifdef VAX + goto vax_ovfl_check; +#else + i = fivesbits[e] + mantbits(dval(rv)) <= P; + /* rv = */ rounded_product(dval(rv), tens[e]); + if (rvOK(dval(rv), fpi, expt, bits, i, rd, &irv)) + goto ret; + e1 -= e; + goto rv_notOK; +#endif + } + i = DBL_DIG - nd; + if (e <= Ten_pmax + i) { + /* A fancier test would sometimes let us do + * this for larger i values. + */ + e2 = e - i; + e1 -= i; + dval(rv) *= tens[i]; +#ifdef VAX + /* VAX exponent range is so narrow we must + * worry about overflow here... + */ + vax_ovfl_check: + dval(adj) = dval(rv); + word0(adj) -= P*Exp_msk1; + /* adj = */ rounded_product(dval(adj), tens[e2]); + if ((word0(adj) & Exp_mask) + > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) + goto rv_notOK; + word0(adj) += P*Exp_msk1; + dval(rv) = dval(adj); +#else + /* rv = */ rounded_product(dval(rv), tens[e2]); +#endif + if (rvOK(dval(rv), fpi, expt, bits, 0, rd, &irv)) + goto ret; + e1 -= e2; + } + } +#ifndef Inaccurate_Divide + else if (e >= -Ten_pmax) { + /* rv = */ rounded_quotient(dval(rv), tens[-e]); + if (rvOK(dval(rv), fpi, expt, bits, 0, rd, &irv)) + goto ret; + e1 -= e; + } +#endif + } + rv_notOK: + e1 += nd - k; + + /* Get starting approximation = rv * 10**e1 */ + + e2 = 0; + if (e1 > 0) { + if ( (i = e1 & 15) !=0) + dval(rv) *= tens[i]; + if (e1 &= ~15) { + e1 = (unsigned int)e1 >> 4; + while(e1 >= (1 << (n_bigtens-1))) { + e2 += ((word0(rv) & Exp_mask) + >> Exp_shift1) - Bias; + word0(rv) &= ~Exp_mask; + word0(rv) |= Bias << Exp_shift1; + dval(rv) *= bigtens[n_bigtens-1]; + e1 -= 1 << (n_bigtens-1); + } + e2 += ((word0(rv) & Exp_mask) >> Exp_shift1) - Bias; + word0(rv) &= ~Exp_mask; + word0(rv) |= Bias << Exp_shift1; + for(j = 0; e1 > 0; j++, e1 = (unsigned int)e1 >> 1) + if (e1 & 1) + dval(rv) *= bigtens[j]; + } + } + else if (e1 < 0) { + e1 = -e1; + if ( (i = e1 & 15) !=0) + dval(rv) /= tens[i]; + if (e1 &= ~15) { + e1 = (unsigned int)e1 >> 4; + while(e1 >= (1 << (n_bigtens-1))) { + e2 += ((word0(rv) & Exp_mask) + >> Exp_shift1) - Bias; + word0(rv) &= ~Exp_mask; + word0(rv) |= Bias << Exp_shift1; + dval(rv) *= tinytens[n_bigtens-1]; + e1 -= 1 << (n_bigtens-1); + } + e2 += ((word0(rv) & Exp_mask) >> Exp_shift1) - Bias; + word0(rv) &= ~Exp_mask; + word0(rv) |= Bias << Exp_shift1; + for(j = 0; e1 > 0; j++, e1 = (unsigned int)e1 >> 1) + if (e1 & 1) + dval(rv) *= tinytens[j]; + } + } +#ifdef IBM + /* e2 is a correction to the (base 2) exponent of the return + * value, reflecting adjustments above to avoid overflow in the + * native arithmetic. For native IBM (base 16) arithmetic, we + * must multiply e2 by 4 to change from base 16 to 2. + */ + e2 <<= 2; +#endif + rvb = d2b(dval(rv), &rve, &rvbits); /* rv = rvb * 2^rve */ + if (rvb == NULL) + return STRTOG_NoMemory; + rve += e2; + if ((j = rvbits - nbits) > 0) { + rshift(rvb, j); + rvbits = nbits; + rve += j; + } + bb0 = 0; /* trailing zero bits in rvb */ + e2 = rve + rvbits - nbits; + if (e2 > fpi->emax + 1) + goto huge; + rve1 = rve + rvbits - nbits; + if (e2 < (emin = fpi->emin)) { + denorm = 1; + j = rve - emin; + if (j > 0) { + rvb = lshift(rvb, j); + rvbits += j; + } + else if (j < 0) { + rvbits += j; + if (rvbits <= 0) { + if (rvbits < -1) { + ufl: + rvb->wds = 0; + rvb->x[0] = 0; + *expt = emin; + irv = STRTOG_Underflow | STRTOG_Inexlo; + goto ret; + } + rvb->x[0] = rvb->wds = rvbits = 1; + } + else + rshift(rvb, -j); + } + rve = rve1 = emin; + if (sudden_underflow && e2 + 1 < emin) + goto ufl; + } + + /* Now the hard part -- adjusting rv to the correct value.*/ + + /* Put digits into bd: true value = bd * 10^e */ + + bd0 = s2b(s0, nd0, nd, y); + + for(;;) { + bd = Balloc(bd0->k); + if (bd == NULL) + return STRTOG_NoMemory; + Bcopy(bd, bd0); + bb = Balloc(rvb->k); + if (bb == NULL) + return STRTOG_NoMemory; + Bcopy(bb, rvb); + bbbits = rvbits - bb0; + bbe = rve + bb0; + bs = i2b(1); + if (bs == NULL) + return STRTOG_NoMemory; + + if (e >= 0) { + bb2 = bb5 = 0; + bd2 = bd5 = e; + } + else { + bb2 = bb5 = -e; + bd2 = bd5 = 0; + } + if (bbe >= 0) + bb2 += bbe; + else + bd2 -= bbe; + bs2 = bb2; + j = nbits + 1 - bbbits; + i = bbe + bbbits - nbits; + if (i < emin) /* denormal */ + j += i - emin; + bb2 += j; + bd2 += j; + i = bb2 < bd2 ? bb2 : bd2; + if (i > bs2) + i = bs2; + if (i > 0) { + bb2 -= i; + bd2 -= i; + bs2 -= i; + } + if (bb5 > 0) { + bs = pow5mult(bs, bb5); + if (bs == NULL) + return STRTOG_NoMemory; + bb1 = mult(bs, bb); + if (bb1 == NULL) + return STRTOG_NoMemory; + Bfree(bb); + bb = bb1; + } + bb2 -= bb0; + if (bb2 > 0) { + bb = lshift(bb, bb2); + if (bb == NULL) + return STRTOG_NoMemory; + } + else if (bb2 < 0) + rshift(bb, -bb2); + if (bd5 > 0) { + bd = pow5mult(bd, bd5); + if (bd == NULL) + return STRTOG_NoMemory; + } + if (bd2 > 0) { + bd = lshift(bd, bd2); + if (bd == NULL) + return STRTOG_NoMemory; + } + if (bs2 > 0) { + bs = lshift(bs, bs2); + if (bs == NULL) + return STRTOG_NoMemory; + } + asub = 1; + inex = STRTOG_Inexhi; + delta = diff(bb, bd); + if (delta == NULL) + return STRTOG_NoMemory; + if (delta->wds <= 1 && !delta->x[0]) + break; + dsign = delta->sign; + delta->sign = finished = 0; + L = 0; + i = cmp(delta, bs); + if (rd && i <= 0) { + irv = STRTOG_Normal; + if ( (finished = dsign ^ (rd&1)) !=0) { + if (dsign != 0) { + irv |= STRTOG_Inexhi; + goto adj1; + } + irv |= STRTOG_Inexlo; + if (rve1 == emin) + goto adj1; + for(i = 0, j = nbits; j >= ULbits; + i++, j -= ULbits) { + if (rvb->x[i] & ALL_ON) + goto adj1; + } + if (j > 1 && lo0bits(rvb->x + i) < j - 1) + goto adj1; + rve = rve1 - 1; + rvb = set_ones(rvb, rvbits = nbits); + if (rvb == NULL) + return STRTOG_NoMemory; + break; + } + irv |= dsign ? STRTOG_Inexlo : STRTOG_Inexhi; + break; + } + if (i < 0) { + /* Error is less than half an ulp -- check for + * special case of mantissa a power of two. + */ + irv = dsign + ? STRTOG_Normal | STRTOG_Inexlo + : STRTOG_Normal | STRTOG_Inexhi; + if (dsign || bbbits > 1 || denorm || rve1 == emin) + break; + delta = lshift(delta,1); + if (delta == NULL) + return STRTOG_NoMemory; + if (cmp(delta, bs) > 0) { + irv = STRTOG_Normal | STRTOG_Inexlo; + goto drop_down; + } + break; + } + if (i == 0) { + /* exactly half-way between */ + if (dsign) { + if (denorm && all_on(rvb, rvbits)) { + /*boundary case -- increment exponent*/ + rvb->wds = 1; + rvb->x[0] = 1; + rve = emin + nbits - (rvbits = 1); + irv = STRTOG_Normal | STRTOG_Inexhi; + denorm = 0; + break; + } + irv = STRTOG_Normal | STRTOG_Inexlo; + } + else if (bbbits == 1) { + irv = STRTOG_Normal; + drop_down: + /* boundary case -- decrement exponent */ + if (rve1 == emin) { + irv = STRTOG_Normal | STRTOG_Inexhi; + if (rvb->wds == 1 && rvb->x[0] == 1) + sudden_underflow = 1; + break; + } + rve -= nbits; + rvb = set_ones(rvb, rvbits = nbits); + if (rvb == NULL) + return STRTOG_NoMemory; + break; + } + else + irv = STRTOG_Normal | STRTOG_Inexhi; + if ((bbbits < nbits && !denorm) || !(rvb->x[0] & 1)) + break; + if (dsign) { + rvb = increment(rvb); + if (rvb == NULL) + return STRTOG_NoMemory; + if ( (j = rvbits & kmask) !=0) + j = ULbits - j; + if (hi0bits(rvb->x[(unsigned int)(rvb->wds - 1) + >> kshift]) + != j) + rvbits++; + irv = STRTOG_Normal | STRTOG_Inexhi; + } + else { + if (bbbits == 1) + goto undfl; + decrement(rvb); + irv = STRTOG_Normal | STRTOG_Inexlo; + } + break; + } + if ((dval(adj) = ratio(delta, bs)) <= 2.) { + adj1: + inex = STRTOG_Inexlo; + if (dsign) { + asub = 0; + inex = STRTOG_Inexhi; + } + else if (denorm && bbbits <= 1) { + undfl: + rvb->wds = 0; + rve = emin; + irv = STRTOG_Underflow | STRTOG_Inexlo; + break; + } + adj0 = dval(adj) = 1.; + } + else { + adj0 = dval(adj) *= 0.5; + if (dsign) { + asub = 0; + inex = STRTOG_Inexlo; + } + if (dval(adj) < 2147483647.) { + L = adj0; + adj0 -= L; + switch(rd) { + case 0: + if (adj0 >= .5) + goto inc_L; + break; + case 1: + if (asub && adj0 > 0.) + goto inc_L; + break; + case 2: + if (!asub && adj0 > 0.) { + inc_L: + L++; + inex = STRTOG_Inexact - inex; + } + } + dval(adj) = L; + } + } + y = rve + rvbits; + + /* adj *= ulp(dval(rv)); */ + /* if (asub) rv -= adj; else rv += adj; */ + + if (!denorm && rvbits < nbits) { + rvb = lshift(rvb, j = nbits - rvbits); + if (rvb == NULL) + return STRTOG_NoMemory; + rve -= j; + rvbits = nbits; + } + ab = d2b(dval(adj), &abe, &abits); + if (ab == NULL) + return STRTOG_NoMemory; + if (abe < 0) + rshift(ab, -abe); + else if (abe > 0) + ab = lshift(ab, abe); + rvb0 = rvb; + if (asub) { + /* rv -= adj; */ + j = hi0bits(rvb->x[rvb->wds-1]); + rvb = diff(rvb, ab); + if (rvb == NULL) + return STRTOG_NoMemory; + k = rvb0->wds - 1; + if (denorm) + /* do nothing */; + else if (rvb->wds <= k + || hi0bits( rvb->x[k]) > + hi0bits(rvb0->x[k])) { + /* unlikely; can only have lost 1 high bit */ + if (rve1 == emin) { + --rvbits; + denorm = 1; + } + else { + rvb = lshift(rvb, 1); + if (rvb == NULL) + return STRTOG_NoMemory; + --rve; + --rve1; + L = finished = 0; + } + } + } + else { + rvb = sum(rvb, ab); + if (rvb == NULL) + return STRTOG_NoMemory; + k = rvb->wds - 1; + if (k >= rvb0->wds + || hi0bits(rvb->x[k]) < hi0bits(rvb0->x[k])) { + if (denorm) { + if (++rvbits == nbits) + denorm = 0; + } + else { + rshift(rvb, 1); + rve++; + rve1++; + L = 0; + } + } + } + Bfree(ab); + Bfree(rvb0); + if (finished) + break; + + z = rve + rvbits; + if (y == z && L) { + /* Can we stop now? */ + tol = dval(adj) * 5e-16; /* > max rel error */ + dval(adj) = adj0 - .5; + if (dval(adj) < -tol) { + if (adj0 > tol) { + irv |= inex; + break; + } + } + else if (dval(adj) > tol && adj0 < 1. - tol) { + irv |= inex; + break; + } + } + bb0 = denorm ? 0 : trailz(rvb); + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(delta); + } + if (!denorm && (j = nbits - rvbits)) { + if (j > 0) + rvb = lshift(rvb, j); + else + rshift(rvb, -j); + rve -= j; + } + *expt = rve; + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(bd0); + Bfree(delta); + if (rve > fpi->emax) { + huge: + rvb->wds = 0; + irv = STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; +#ifndef NO_ERRNO + errno = ERANGE; +#endif +#ifdef INFNAN_CHECK + infnanexp: +#endif + *expt = fpi->emax + 1; + } + ret: + if (denorm) { + if (sudden_underflow) { + rvb->wds = 0; + irv = STRTOG_Underflow | STRTOG_Inexlo; + } + else { + irv = (irv & ~STRTOG_Retmask) | + (rvb->wds > 0 ? STRTOG_Denormal : STRTOG_Zero); + if (irv & STRTOG_Inexact) + irv |= STRTOG_Underflow; + } + } + if (se) + *se = __UNCONST(s); + if (sign) + irv |= STRTOG_Neg; + if (rvb) { + copybits(bits, nbits, rvb); + Bfree(rvb); + } + return irv; + } diff --git a/lib/nbsd_libc/gdtoa/strtodnrp.c b/lib/nbsd_libc/gdtoa/strtodnrp.c new file mode 100644 index 000000000..cc448bfd1 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtodnrp.c @@ -0,0 +1,95 @@ +/* $NetBSD: strtodnrp.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 2004 by David M. Gay. +All Rights Reserved +Based on material in the rest of /netlib/fp/gdota.tar.gz, +which is copyright (C) 1998, 2000 by Lucent Technologies. + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* This is a variant of strtod that works on Intel ia32 systems */ +/* with the default extended-precision arithmetic -- it does not */ +/* require setting the precision control to 53 bits. */ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + double +#ifdef KR_headers +strtod(s, sp) CONST char *s; char **sp; +#else +strtod(CONST char *s, char **sp) +#endif +{ + static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; + ULong bits[2]; + Long exp; + int k; + union { ULong L[2]; double d; } u; + + k = strtodg(s, sp, &fpi, &exp, bits); + if (k == STRTOG_NoMemory) { + errno = ERANGE; + u.L[0] = Big0; + u.L[1] = Big1; + return u.d; + } + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + u.L[0] = u.L[1] = 0; + break; + + case STRTOG_Normal: + u.L[_1] = bits[0]; + u.L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); + break; + + case STRTOG_Denormal: + u.L[_1] = bits[0]; + u.L[_0] = bits[1]; + break; + + case STRTOG_Infinite: + u.L[_0] = 0x7ff00000; + u.L[_1] = 0; + break; + + case STRTOG_NaN: + u.L[0] = d_QNAN0; + u.L[1] = d_QNAN1; + break; + + case STRTOG_NaNbits: + u.L[_0] = 0x7ff00000 | bits[1]; + u.L[_1] = bits[0]; + } + if (k & STRTOG_Neg) + u.L[_0] |= 0x80000000L; + return u.d; + } diff --git a/lib/nbsd_libc/gdtoa/strtof.c b/lib/nbsd_libc/gdtoa/strtof.c new file mode 100644 index 000000000..fb07ff698 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtof.c @@ -0,0 +1,84 @@ +/* $NetBSD: strtof.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "namespace.h" +#include "gdtoaimp.h" + +#ifdef __weak_alias +__weak_alias(strtof, _strtof) +#endif + + float +#ifdef KR_headers +strtof(s, sp) CONST char *s; char **sp; +#else +strtof(CONST char *s, char **sp) +#endif +{ + static CONST FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; + ULong bits[1]; + Long expt; + int k; + union { ULong L[1]; float f; } u; + + k = strtodg(s, sp, &fpi, &expt, bits); + if (k == STRTOG_NoMemory) { + errno = ERANGE; + return HUGE_VALF; + } + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + u.L[0] = 0; + break; + + case STRTOG_Normal: + case STRTOG_NaNbits: + u.L[0] = (bits[0] & 0x7fffff) | ((expt + 0x7f + 23) << 23); + break; + + case STRTOG_Denormal: + u.L[0] = bits[0]; + break; + + case STRTOG_Infinite: + u.L[0] = 0x7f800000; + break; + + case STRTOG_NaN: + u.L[0] = f_QNAN; + } + if (k & STRTOG_Neg) + u.L[0] |= 0x80000000L; + return u.f; + } diff --git a/lib/nbsd_libc/gdtoa/strtof_vaxf.c b/lib/nbsd_libc/gdtoa/strtof_vaxf.c new file mode 100644 index 000000000..3710e62f4 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtof_vaxf.c @@ -0,0 +1,83 @@ +/* $NetBSD: strtof_vaxf.c,v 1.5 2008/03/28 00:56:54 he Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +/* Adapted to VAX F_floating by Klaus Klein . */ + +#include "namespace.h" +#include "gdtoaimp.h" + +#ifdef __weak_alias +__weak_alias(strtof, _strtof) +#endif + + float +#ifdef KR_headers +strtof(s, sp) CONST char *s; char **sp; +#else +strtof(CONST char *s, char **sp) +#endif +{ + static CONST FPI fpi = { 24, 1-128-1-24+1, 255-128-1-24+1, 1, SI }; + ULong bits[1]; + Long expt; + int k; + union { ULong L[1]; float f; } u; + + k = strtodg(s, sp, &fpi, &expt, bits); + if (k == STRTOG_NoMemory) { + errno = ERANGE; + u.L[0] = Big0; + u.L[1] = Big1; + return u.f; + } + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + u.L[0] = 0; + break; + + case STRTOG_Normal: + u.L[0] = ((bits[0] & 0x0000ffff) << 16) | /* FracLo */ + ((bits[0] & 0x007f0000) >> 16) | /* FracHi */ + ((expt + 128 + 1 + 23) << 7); /* Exp */ + break; + + case STRTOG_Infinite: + u.L[0] = 0xffff7fff; + break; + + } + if (k & STRTOG_Neg) + u.L[0] |= 0x00008000L; + return u.f; + } diff --git a/lib/nbsd_libc/gdtoa/strtold_pQ.c b/lib/nbsd_libc/gdtoa/strtold_pQ.c new file mode 100644 index 000000000..630d9dfe6 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtold_pQ.c @@ -0,0 +1,4 @@ +/* $NetBSD: strtold_pQ.c,v 1.1 2006/03/15 17:35:18 kleink Exp $ */ + +#define GDTOA_LD_FMT Q +#include "strtold_subr.c" diff --git a/lib/nbsd_libc/gdtoa/strtold_px.c b/lib/nbsd_libc/gdtoa/strtold_px.c new file mode 100644 index 000000000..779da854a --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtold_px.c @@ -0,0 +1,4 @@ +/* $NetBSD: strtold_px.c,v 1.1 2006/03/15 17:35:18 kleink Exp $ */ + +#define GDTOA_LD_FMT x +#include "strtold_subr.c" diff --git a/lib/nbsd_libc/gdtoa/strtold_pxL.c b/lib/nbsd_libc/gdtoa/strtold_pxL.c new file mode 100644 index 000000000..04c019320 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtold_pxL.c @@ -0,0 +1,4 @@ +/* $NetBSD: strtold_pxL.c,v 1.1 2006/03/15 17:35:18 kleink Exp $ */ + +#define GDTOA_LD_FMT xL +#include "strtold_subr.c" diff --git a/lib/nbsd_libc/gdtoa/strtold_subr.c b/lib/nbsd_libc/gdtoa/strtold_subr.c new file mode 100644 index 000000000..3a9059ff8 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtold_subr.c @@ -0,0 +1,47 @@ +/* $NetBSD: strtold_subr.c,v 1.1 2006/03/15 17:35:18 kleink Exp $ */ + +/* + * Written by Klaus Klein , November 16, 2005. + * Public domain. + */ + +/* + * NOTICE: This is not a standalone file. To use it, #include it in + * the format-specific strtold_*.c, like so: + * + * #define GDTOA_LD_FMT + * #include "strtold_subr.c" + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: strtold_subr.c,v 1.1 2006/03/15 17:35:18 kleink Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include "gdtoa.h" + +#ifdef __weak_alias +__weak_alias(strtold, _strtold) +#endif + +#ifndef __HAVE_LONG_DOUBLE +#error no extended-precision long double type +#endif + +#ifndef GDTOA_LD_FMT +#error GDTOA_LD_FMT must be defined by format-specific source file +#endif + +#define STRTOP(x) __CONCAT(strtop, x) + +long double +strtold(const char *nptr, char **endptr) +{ + long double ld; + + (void)STRTOP(GDTOA_LD_FMT)(nptr, endptr, &ld); + return ld; +} diff --git a/lib/nbsd_libc/gdtoa/strtopQ.c b/lib/nbsd_libc/gdtoa/strtopQ.c new file mode 100644 index 000000000..2fd7ec553 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtopQ.c @@ -0,0 +1,105 @@ +/* $NetBSD: strtopQ.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#define _3 3 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 3 +#define _1 2 +#define _2 1 +#define _3 0 +#endif + + int +#ifdef KR_headers +strtopQ(s, sp, V) CONST char *s; char **sp; void *V; +#else +strtopQ(CONST char *s, char **sp, void *V) +#endif +{ + static CONST FPI fpi = { 113, 1-16383-113+1, 32766 - 16383 - 113 + 1, 1, SI }; + ULong bits[4]; + Long expt; + int k; + ULong *L = (ULong*)V; + + k = strtodg(s, sp, &fpi, &expt, bits); + if (k == STRTOG_NoMemory) + return k; + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = L[2] = L[3] = 0; + break; + + case STRTOG_Normal: + case STRTOG_NaNbits: + L[_3] = bits[0]; + L[_2] = bits[1]; + L[_1] = bits[2]; + L[_0] = (bits[3] & ~0x10000) | ((expt + 0x3fff + 112) << 16); + break; + + case STRTOG_Denormal: + L[_3] = bits[0]; + L[_2] = bits[1]; + L[_1] = bits[2]; + L[_0] = bits[3]; + break; + + case STRTOG_Infinite: + L[_0] = 0x7fff0000; + L[_1] = L[_2] = L[_3] = 0; + break; + + case STRTOG_NaN: + L[0] = ld_QNAN0; + L[1] = ld_QNAN1; + L[2] = ld_QNAN2; + L[3] = ld_QNAN3; + } + if (k & STRTOG_Neg) + L[_0] |= 0x80000000L; + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtopd.c b/lib/nbsd_libc/gdtoa/strtopd.c new file mode 100644 index 000000000..9fe46ab12 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtopd.c @@ -0,0 +1,53 @@ +/* $NetBSD: strtopd.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtopd(s, sp, d) char *s; char **sp; double *d; +#else +strtopd(CONST char *s, char **sp, double *d) +#endif +{ + static FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; + ULong bits[2]; + Long exp; + int k; + + k = strtodg(s, sp, &fpi0, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + ULtod((ULong*)d, bits, exp, k); + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtopdd.c b/lib/nbsd_libc/gdtoa/strtopdd.c new file mode 100644 index 000000000..47357b9b2 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtopdd.c @@ -0,0 +1,182 @@ +/* $NetBSD: strtopdd.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtopdd(s, sp, dd) CONST char *s; char **sp; double *dd; +#else +strtopdd(CONST char *s, char **sp, double *dd) +#endif +{ +#ifdef Sudden_Underflow + static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; +#else + static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; +#endif + ULong bits[4]; + Long exp; + int i, j, rv; + typedef union { + double d[2]; + ULong L[4]; + } U; + U *u; + + rv = strtodg(s, sp, &fpi, &exp, bits); + if (rv == STRTOG_NoMemory) + return rv; + u = (U*)dd; + switch(rv & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + u->d[0] = u->d[1] = 0.; + break; + + case STRTOG_Normal: + u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL; + u->L[_0] = bits[2] >> 21 | bits[3] << 11 & 0xfffff + | exp + 0x3ff + 105 << 20; + exp += 0x3ff + 52; + if (bits[1] &= 0x1fffff) { + i = hi0bits(bits[1]) - 11; + if (i >= exp) { + i = exp - 1; + exp = 0; + } + else + exp -= i; + if (i > 0) { + bits[1] = bits[1] << i | bits[0] >> 32-i; + bits[0] = bits[0] << i & 0xffffffffL; + } + } + else if (bits[0]) { + i = hi0bits(bits[0]) + 21; + if (i >= exp) { + i = exp - 1; + exp = 0; + } + else + exp -= i; + if (i < 32) { + bits[1] = bits[0] >> 32 - i; + bits[0] = bits[0] << i & 0xffffffffL; + } + else { + bits[1] = bits[0] << i - 32; + bits[0] = 0; + } + } + else { + u->L[2] = u->L[3] = 0; + break; + } + u->L[2+_1] = bits[0]; + u->L[2+_0] = bits[1] & 0xfffff | exp << 20; + break; + + case STRTOG_Denormal: + if (bits[3]) + goto nearly_normal; + if (bits[2]) + goto partly_normal; + if (bits[1] & 0xffe00000) + goto hardly_normal; + /* completely denormal */ + u->L[2] = u->L[3] = 0; + u->L[_1] = bits[0]; + u->L[_0] = bits[1]; + break; + + nearly_normal: + i = hi0bits(bits[3]) - 11; /* i >= 12 */ + j = 32 - i; + u->L[_0] = (bits[3] << i | bits[2] >> j) & 0xfffff + | 65 - i << 20; + u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; + u->L[2+_0] = bits[1] & (1L << j) - 1; + u->L[2+_1] = bits[0]; + break; + + partly_normal: + i = hi0bits(bits[2]) - 11; + if (i < 0) { + j = -i; + i += 32; + u->L[_0] = bits[2] >> j & 0xfffff | (33 + j) << 20; + u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; + u->L[2+_0] = bits[1] & (1L << j) - 1; + u->L[2+_1] = bits[0]; + break; + } + if (i == 0) { + u->L[_0] = bits[2] & 0xfffff | 33 << 20; + u->L[_1] = bits[1]; + u->L[2+_0] = 0; + u->L[2+_1] = bits[0]; + break; + } + j = 32 - i; + u->L[_0] = (bits[2] << i | bits[1] >> j) & 0xfffff + | j + 1 << 20; + u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + u->L[2+_0] = 0; + u->L[2+_1] = bits[0] & (1L << j) - 1; + break; + + hardly_normal: + j = 11 - hi0bits(bits[1]); + i = 32 - j; + u->L[_0] = bits[1] >> j & 0xfffff | j + 1 << 20; + u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + u->L[2+_0] = 0; + u->L[2+_1] = bits[0] & (1L << j) - 1; + break; + + case STRTOG_Infinite: + u->L[_0] = u->L[2+_0] = 0x7ff00000; + u->L[_1] = u->L[2+_1] = 0; + break; + + case STRTOG_NaN: + u->L[0] = u->L[2] = d_QNAN0; + u->L[1] = u->L[3] = d_QNAN1; + } + if (rv & STRTOG_Neg) { + u->L[ _0] |= 0x80000000L; + u->L[2+_0] |= 0x80000000L; + } + return rv; + } diff --git a/lib/nbsd_libc/gdtoa/strtopf.c b/lib/nbsd_libc/gdtoa/strtopf.c new file mode 100644 index 000000000..3bb9d5a7b --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtopf.c @@ -0,0 +1,77 @@ +/* $NetBSD: strtopf.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + int +#ifdef KR_headers +strtopf(s, sp, f) CONST char *s; char **sp; float *f; +#else +strtopf(CONST char *s, char **sp, float *f) +#endif +{ + static FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; + ULong bits[1], *L; + Long exp; + int k; + + k = strtodg(s, sp, &fpi, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + L = (ULong*)f; + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = 0; + break; + + case STRTOG_Normal: + case STRTOG_NaNbits: + L[0] = bits[0] & 0x7fffff | exp + 0x7f + 23 << 23; + break; + + case STRTOG_Denormal: + L[0] = bits[0]; + break; + + case STRTOG_Infinite: + L[0] = 0x7f800000; + break; + + case STRTOG_NaN: + L[0] = f_QNAN; + } + if (k & STRTOG_Neg) + L[0] |= 0x80000000L; + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtopx.c b/lib/nbsd_libc/gdtoa/strtopx.c new file mode 100644 index 000000000..c2a122782 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtopx.c @@ -0,0 +1,107 @@ +/* $NetBSD: strtopx.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#define _3 3 +#define _4 4 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 4 +#define _1 3 +#define _2 2 +#define _3 1 +#define _4 0 +#endif + + int +#ifdef KR_headers +strtopx(s, sp, V) CONST char *s; char **sp; void *V; +#else +strtopx(CONST char *s, char **sp, void *V) +#endif +{ + static CONST FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; + ULong bits[2]; + Long expt; + int k; + UShort *L = (UShort*)V; + + k = strtodg(s, sp, &fpi, &expt, bits); + if (k == STRTOG_NoMemory) + return k; + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = L[2] = L[3] = L[4] = 0; + break; + + case STRTOG_Denormal: + L[_0] = 0; + goto normal_bits; + + case STRTOG_Normal: + case STRTOG_NaNbits: + L[_0] = expt + 0x3fff + 63; + normal_bits: + L[_4] = (UShort)bits[0]; + L[_3] = (UShort)(bits[0] >> 16); + L[_2] = (UShort)bits[1]; + L[_1] = (UShort)(bits[1] >> 16); + break; + + case STRTOG_Infinite: + L[_0] = 0x7fff; + L[_1] = L[_2] = L[_3] = L[_4] = 0; + break; + + case STRTOG_NaN: + L[0] = ldus_QNAN0; + L[1] = ldus_QNAN1; + L[2] = ldus_QNAN2; + L[3] = ldus_QNAN3; + L[4] = ldus_QNAN4; + } + if (k & STRTOG_Neg) + L[_0] |= 0x8000; + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtopxL.c b/lib/nbsd_libc/gdtoa/strtopxL.c new file mode 100644 index 000000000..d0890f25d --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtopxL.c @@ -0,0 +1,95 @@ +/* $NetBSD: strtopxL.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 2 +#define _1 1 +#define _2 0 +#endif + + int +#ifdef KR_headers +strtopxL(s, sp, V) CONST char *s; char **sp; void *V; +#else +strtopxL(CONST char *s, char **sp, void *V) +#endif +{ + static CONST FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; + ULong bits[2]; + Long expt; + int k; + ULong *L = (ULong*)V; + + k = strtodg(s, sp, &fpi, &expt, bits); + if (k == STRTOG_NoMemory) + return k; + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = L[2] = 0; + break; + + case STRTOG_Normal: + case STRTOG_Denormal: + case STRTOG_NaNbits: + L[_2] = bits[0]; + L[_1] = bits[1]; + L[_0] = (expt + 0x3fff + 63) << 16; + break; + + case STRTOG_Infinite: + L[_0] = 0x7fff << 16; + L[_1] = L[_2] = 0; + break; + + case STRTOG_NaN: + L[0] = ld_QNAN0; + L[1] = ld_QNAN1; + L[2] = ld_QNAN2; + } + if (k & STRTOG_Neg) + L[_0] |= 0x80000000L; + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtorQ.c b/lib/nbsd_libc/gdtoa/strtorQ.c new file mode 100644 index 000000000..349ef804f --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtorQ.c @@ -0,0 +1,121 @@ +/* $NetBSD: strtorQ.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#define _3 3 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 3 +#define _1 2 +#define _2 1 +#define _3 0 +#endif + + void +#ifdef KR_headers +ULtoQ(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; +#else +ULtoQ(ULong *L, ULong *bits, Long exp, int k) +#endif +{ + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = L[2] = L[3] = 0; + break; + + case STRTOG_Normal: + case STRTOG_NaNbits: + L[_3] = bits[0]; + L[_2] = bits[1]; + L[_1] = bits[2]; + L[_0] = (bits[3] & ~0x10000) | ((exp + 0x3fff + 112) << 16); + break; + + case STRTOG_Denormal: + L[_3] = bits[0]; + L[_2] = bits[1]; + L[_1] = bits[2]; + L[_0] = bits[3]; + break; + + case STRTOG_Infinite: + L[_0] = 0x7fff0000; + L[_1] = L[_2] = L[_3] = 0; + break; + + case STRTOG_NaN: + L[0] = ld_QNAN0; + L[1] = ld_QNAN1; + L[2] = ld_QNAN2; + L[3] = ld_QNAN3; + } + if (k & STRTOG_Neg) + L[_0] |= 0x80000000L; + } + + int +#ifdef KR_headers +strtorQ(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L; +#else +strtorQ(CONST char *s, char **sp, int rounding, void *L) +#endif +{ + static FPI fpi0 = { 113, 1-16383-113+1, 32766-16383-113+1, 1, SI }; + FPI *fpi, fpi1; + ULong bits[4]; + Long exp; + int k; + + fpi = &fpi0; + if (rounding != FPI_Round_near) { + fpi1 = fpi0; + fpi1.rounding = rounding; + fpi = &fpi1; + } + k = strtodg(s, sp, fpi, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + ULtoQ((ULong*)L, bits, exp, k); + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtord.c b/lib/nbsd_libc/gdtoa/strtord.c new file mode 100644 index 000000000..b995f264a --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtord.c @@ -0,0 +1,100 @@ +/* $NetBSD: strtord.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + void +#ifdef KR_headers +ULtod(L, bits, expt, k) ULong *L; ULong *bits; Long expt; int k; +#else +ULtod(ULong *L, ULong *bits, Long expt, int k) +#endif +{ + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = 0; + break; + + case STRTOG_Denormal: + L[_1] = bits[0]; + L[_0] = bits[1]; + break; + + case STRTOG_Normal: + case STRTOG_NaNbits: + L[_1] = bits[0]; + L[_0] = (bits[1] & ~0x100000) | ((expt + 0x3ff + 52) << 20); + break; + + case STRTOG_Infinite: + L[_0] = 0x7ff00000; + L[_1] = 0; + break; + +#ifdef d_QNAN0 + case STRTOG_NaN: + L[0] = d_QNAN0; + L[1] = d_QNAN1; +#endif + } + if (k & STRTOG_Neg) + L[_0] |= 0x80000000L; + } + + int +#ifdef KR_headers +strtord(s, sp, rounding, d) CONST char *s; char **sp; int rounding; double *d; +#else +strtord(CONST char *s, char **sp, int rounding, double *d) +#endif +{ + static CONST FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; + CONST FPI *fpi; + FPI fpi1; + ULong bits[2]; + Long expt; + int k; + + fpi = &fpi0; + if (rounding != FPI_Round_near) { + fpi1 = fpi0; + fpi1.rounding = rounding; + fpi = &fpi1; + } + k = strtodg(s, sp, fpi, &expt, bits); + if (k == STRTOG_NoMemory) + return k; + ULtod((/* LINTED */(U*)d)->L, bits, expt, k); + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtordd.c b/lib/nbsd_libc/gdtoa/strtordd.c new file mode 100644 index 000000000..3e2d1aa26 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtordd.c @@ -0,0 +1,203 @@ +/* $NetBSD: strtordd.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + void +#ifdef KR_headers +ULtodd(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; +#else +ULtodd(ULong *L, ULong *bits, Long exp, int k) +#endif +{ + int i, j; + + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = L[2] = L[3] = 0; + break; + + case STRTOG_Normal: + L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL; + L[_0] = bits[2] >> 21 | bits[3] << 11 & 0xfffff + | exp + 0x3ff + 105 << 20; + exp += 0x3ff + 52; + if (bits[1] &= 0x1fffff) { + i = hi0bits(bits[1]) - 11; + if (i >= exp) { + i = exp - 1; + exp = 0; + } + else + exp -= i; + if (i > 0) { + bits[1] = bits[1] << i | bits[0] >> 32-i; + bits[0] = bits[0] << i & (ULong)0xffffffffL; + } + } + else if (bits[0]) { + i = hi0bits(bits[0]) + 21; + if (i >= exp) { + i = exp - 1; + exp = 0; + } + else + exp -= i; + if (i < 32) { + bits[1] = bits[0] >> 32 - i; + bits[0] = bits[0] << i & (ULong)0xffffffffL; + } + else { + bits[1] = bits[0] << i - 32; + bits[0] = 0; + } + } + else { + L[2] = L[3] = 0; + break; + } + L[2+_1] = bits[0]; + L[2+_0] = bits[1] & 0xfffff | exp << 20; + break; + + case STRTOG_Denormal: + if (bits[3]) + goto nearly_normal; + if (bits[2]) + goto partly_normal; + if (bits[1] & 0xffe00000) + goto hardly_normal; + /* completely denormal */ + L[2] = L[3] = 0; + L[_1] = bits[0]; + L[_0] = bits[1]; + break; + + nearly_normal: + i = hi0bits(bits[3]) - 11; /* i >= 12 */ + j = 32 - i; + L[_0] = (bits[3] << i | bits[2] >> j) & 0xfffff + | 65 - i << 20; + L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; + L[2+_0] = bits[1] & ((ULong)1L << j) - 1; + L[2+_1] = bits[0]; + break; + + partly_normal: + i = hi0bits(bits[2]) - 11; + if (i < 0) { + j = -i; + i += 32; + L[_0] = bits[2] >> j & 0xfffff | (33 + j) << 20; + L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; + L[2+_0] = bits[1] & ((ULong)1L << j) - 1; + L[2+_1] = bits[0]; + break; + } + if (i == 0) { + L[_0] = bits[2] & 0xfffff | 33 << 20; + L[_1] = bits[1]; + L[2+_0] = 0; + L[2+_1] = bits[0]; + break; + } + j = 32 - i; + L[_0] = (bits[2] << i | bits[1] >> j) & 0xfffff + | j + 1 << 20; + L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + L[2+_0] = 0; + L[2+_1] = bits[0] & (1L << j) - 1; + break; + + hardly_normal: + j = 11 - hi0bits(bits[1]); + i = 32 - j; + L[_0] = bits[1] >> j & 0xfffff | j + 1 << 20; + L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + L[2+_0] = 0; + L[2+_1] = bits[0] & ((ULong)1L << j) - 1; + break; + + case STRTOG_Infinite: + L[_0] = L[2+_0] = 0x7ff00000; + L[_1] = L[2+_1] = 0; + break; + + case STRTOG_NaN: + L[0] = L[2] = d_QNAN0; + L[1] = L[3] = d_QNAN1; + break; + + case STRTOG_NaNbits: + L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL; + L[_0] = bits[2] >> 21 | bits[3] << 11 + | (ULong)0x7ff00000L; + L[2+_1] = bits[0]; + L[2+_0] = bits[1] | (ULong)0x7ff00000L; + } + if (k & STRTOG_Neg) { + L[_0] |= 0x80000000L; + L[2+_0] |= 0x80000000L; + } + } + + int +#ifdef KR_headers +strtordd(s, sp, rounding, dd) CONST char *s; char **sp; int rounding; double *dd; +#else +strtordd(CONST char *s, char **sp, int rounding, double *dd) +#endif +{ +#ifdef Sudden_Underflow + static FPI fpi0 = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; +#else + static FPI fpi0 = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; +#endif + FPI *fpi, fpi1; + ULong bits[4]; + Long exp; + int k; + + fpi = &fpi0; + if (rounding != FPI_Round_near) { + fpi1 = fpi0; + fpi1.rounding = rounding; + fpi = &fpi1; + } + k = strtodg(s, sp, fpi, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + ULtodd((ULong*)dd, bits, exp, k); + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtorf.c b/lib/nbsd_libc/gdtoa/strtorf.c new file mode 100644 index 000000000..8617e40dd --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtorf.c @@ -0,0 +1,93 @@ +/* $NetBSD: strtorf.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + void +#ifdef KR_headers +ULtof(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; +#else +ULtof(ULong *L, ULong *bits, Long exp, int k) +#endif +{ + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + *L = 0; + break; + + case STRTOG_Normal: + case STRTOG_NaNbits: + L[0] = bits[0] & 0x7fffff | exp + 0x7f + 23 << 23; + break; + + case STRTOG_Denormal: + L[0] = bits[0]; + break; + + case STRTOG_Infinite: + L[0] = 0x7f800000; + break; + + case STRTOG_NaN: + L[0] = f_QNAN; + } + if (k & STRTOG_Neg) + L[0] |= 0x80000000L; + } + + int +#ifdef KR_headers +strtorf(s, sp, rounding, f) CONST char *s; char **sp; int rounding; float *f; +#else +strtorf(CONST char *s, char **sp, int rounding, float *f) +#endif +{ + static FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; + FPI *fpi, fpi1; + ULong bits[1]; + Long exp; + int k; + + fpi = &fpi0; + if (rounding != FPI_Round_near) { + fpi1 = fpi0; + fpi1.rounding = rounding; + fpi = &fpi1; + } + k = strtodg(s, sp, fpi, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + ULtof((ULong*)f, bits, exp, k); + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtorx.c b/lib/nbsd_libc/gdtoa/strtorx.c new file mode 100644 index 000000000..1512486bd --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtorx.c @@ -0,0 +1,123 @@ +/* $NetBSD: strtorx.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#define _3 3 +#define _4 4 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 4 +#define _1 3 +#define _2 2 +#define _3 1 +#define _4 0 +#endif + + void +#ifdef KR_headers +ULtox(L, bits, exp, k) UShort *L; ULong *bits; Long exp; int k; +#else +ULtox(UShort *L, ULong *bits, Long exp, int k) +#endif +{ + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = L[2] = L[3] = L[4] = 0; + break; + + case STRTOG_Denormal: + L[_0] = 0; + goto normal_bits; + + case STRTOG_Normal: + case STRTOG_NaNbits: + L[_0] = exp + 0x3fff + 63; + normal_bits: + L[_4] = (UShort)bits[0]; + L[_3] = (UShort)(bits[0] >> 16); + L[_2] = (UShort)bits[1]; + L[_1] = (UShort)(bits[1] >> 16); + break; + + case STRTOG_Infinite: + L[_0] = 0x7fff; + L[_1] = L[_2] = L[_3] = L[_4] = 0; + break; + + case STRTOG_NaN: + L[0] = ldus_QNAN0; + L[1] = ldus_QNAN1; + L[2] = ldus_QNAN2; + L[3] = ldus_QNAN3; + L[4] = ldus_QNAN4; + } + if (k & STRTOG_Neg) + L[_0] |= 0x8000; + } + + int +#ifdef KR_headers +strtorx(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L; +#else +strtorx(CONST char *s, char **sp, int rounding, void *L) +#endif +{ + static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; + FPI *fpi, fpi1; + ULong bits[2]; + Long exp; + int k; + + fpi = &fpi0; + if (rounding != FPI_Round_near) { + fpi1 = fpi0; + fpi1.rounding = rounding; + fpi = &fpi1; + } + k = strtodg(s, sp, fpi, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + ULtox((UShort*)L, bits, exp, k); + return k; + } diff --git a/lib/nbsd_libc/gdtoa/strtorxL.c b/lib/nbsd_libc/gdtoa/strtorxL.c new file mode 100644 index 000000000..4f11648b2 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/strtorxL.c @@ -0,0 +1,111 @@ +/* $NetBSD: strtorxL.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 2000 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + +#undef _0 +#undef _1 + +/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ + +#ifdef IEEE_BIG_ENDIAN +#define _0 0 +#define _1 1 +#define _2 2 +#endif +#ifdef IEEE_LITTLE_ENDIAN +#define _0 2 +#define _1 1 +#define _2 0 +#endif + + void +#ifdef KR_headers +ULtoxL(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; +#else +ULtoxL(ULong *L, ULong *bits, Long exp, int k) +#endif +{ + switch(k & STRTOG_Retmask) { + case STRTOG_NoNumber: + case STRTOG_Zero: + L[0] = L[1] = L[2] = 0; + break; + + case STRTOG_Normal: + case STRTOG_Denormal: + case STRTOG_NaNbits: + L[_0] = (exp + 0x3fff + 63) << 16; + L[_1] = bits[1]; + L[_2] = bits[0]; + break; + + case STRTOG_Infinite: + L[_0] = 0x7fff << 16; + L[_1] = L[_2] = 0; + break; + + case STRTOG_NaN: + L[0] = ld_QNAN0; + L[1] = ld_QNAN1; + L[2] = ld_QNAN2; + } + if (k & STRTOG_Neg) + L[_0] |= 0x80000000L; + } + + int +#ifdef KR_headers +strtorxL(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L; +#else +strtorxL(CONST char *s, char **sp, int rounding, void *L) +#endif +{ + static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; + FPI *fpi, fpi1; + ULong bits[2]; + Long exp; + int k; + + fpi = &fpi0; + if (rounding != FPI_Round_near) { + fpi1 = fpi0; + fpi1.rounding = rounding; + fpi = &fpi1; + } + k = strtodg(s, sp, fpi, &exp, bits); + if (k == STRTOG_NoMemory) + return k; + ULtoxL((ULong*)L, bits, exp, k); + return k; + } diff --git a/lib/nbsd_libc/gdtoa/sum.c b/lib/nbsd_libc/gdtoa/sum.c new file mode 100644 index 000000000..6a987a1a4 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/sum.c @@ -0,0 +1,104 @@ +/* $NetBSD: sum.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + Bigint * +#ifdef KR_headers +sum(a, b) Bigint *a; Bigint *b; +#else +sum(Bigint *a, Bigint *b) +#endif +{ + Bigint *c; + ULong carry, *xc, *xa, *xb, *xe, y; +#ifdef Pack_32 + ULong z; +#endif + + if (a->wds < b->wds) { + c = b; b = a; a = c; + } + c = Balloc(a->k); + if (c == NULL) + return NULL; + c->wds = a->wds; + carry = 0; + xa = a->x; + xb = b->x; + xc = c->x; + xe = xc + b->wds; +#ifdef Pack_32 + do { + y = (*xa & 0xffff) + (*xb & 0xffff) + carry; + carry = (y & 0x10000) >> 16; + z = (*xa++ >> 16) + (*xb++ >> 16) + carry; + carry = (z & 0x10000) >> 16; + Storeinc(xc, z, y); + } + while(xc < xe); + xe += a->wds - b->wds; + while(xc < xe) { + y = (*xa & 0xffff) + carry; + carry = (y & 0x10000) >> 16; + z = (*xa++ >> 16) + carry; + carry = (z & 0x10000) >> 16; + Storeinc(xc, z, y); + } +#else + do { + y = *xa++ + *xb++ + carry; + carry = (y & 0x10000) >> 16; + *xc++ = y & 0xffff; + } + while(xc < xe); + xe += a->wds - b->wds; + while(xc < xe) { + y = *xa++ + carry; + carry = (y & 0x10000) >> 16; + *xc++ = y & 0xffff; + } +#endif + if (carry) { + if (c->wds == c->maxwds) { + b = Balloc(c->k + 1); + if (b == NULL) + return NULL; + Bcopy(b, c); + Bfree(c); + c = b; + } + c->x[c->wds++] = 1; + } + return c; + } diff --git a/lib/nbsd_libc/gdtoa/ulp.c b/lib/nbsd_libc/gdtoa/ulp.c new file mode 100644 index 000000000..ef909ae78 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/ulp.c @@ -0,0 +1,72 @@ +/* $NetBSD: ulp.c,v 1.2 2006/01/25 15:27:42 kleink Exp $ */ + +/**************************************************************** + +The author of this software is David M. Gay. + +Copyright (C) 1998, 1999 by Lucent Technologies +All Rights Reserved + +Permission to use, copy, modify, and distribute this software and +its documentation for any purpose and without fee is hereby +granted, provided that the above copyright notice appear in all +copies and that both that the copyright notice and this +permission notice and warranty disclaimer appear in supporting +documentation, and that the name of Lucent or any of its entities +not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY +SPECIAL, 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. + +****************************************************************/ + +/* Please send bug reports to David M. Gay (dmg at acm dot org, + * with " at " changed at "@" and " dot " changed to "."). */ + +#include "gdtoaimp.h" + + double +ulp +#ifdef KR_headers + (x) double x; +#else + (double x) +#endif +{ + Long L; + double a; + + L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; +#ifndef Sudden_Underflow + if (L > 0) { +#endif +#ifdef IBM + L |= Exp_msk1 >> 4; +#endif + word0(a) = L; + word1(a) = 0; +#ifndef Sudden_Underflow + } + else { + L = (unsigned int)-L >> Exp_shift; + if (L < Exp_shift) { + word0(a) = 0x80000 >> L; + word1(a) = 0; + } + else { + word0(a) = 0; + L -= Exp_shift; + word1(a) = L >= 31 ? 1 : 1 << (31 - L); + } + } +#endif + return a; + } diff --git a/lib/nbsd_libc/gdtoa/xsum0.out b/lib/nbsd_libc/gdtoa/xsum0.out new file mode 100644 index 000000000..701b55d31 --- /dev/null +++ b/lib/nbsd_libc/gdtoa/xsum0.out @@ -0,0 +1,48 @@ +README f2477cff 14190 +arithchk.c ebbe5bc7 4075 +dmisc.c c8daa18 4682 +dtoa.c 6a7b6fe 16876 +g_Qfmt.c f791d807 2839 +g__fmt.c 14dca85 2504 +g_ddfmt.c 10eae12a 3695 +g_dfmt.c f36c1014 2503 +g_ffmt.c fb83cfb5 2429 +g_xLfmt.c f216a096 2686 +g_xfmt.c ed824bf3 2775 +gdtoa.c e29409a6 16988 +gdtoa.h f208c204 4780 +gdtoaimp.h e3c2a970 19441 +gethex.c dba1616 5201 +gmisc.c 1859d016 2084 +hd_init.c efdbe921 1797 +hexnan.c f7ea38f9 2958 +makefile f890b12 2932 +misc.c 1757f7fc 14252 +qnan.c efd33d64 3417 +smisc.c e282e715 3655 +strtoIQ.c 1809dfcf 1939 +strtoId.c f41ddac2 1931 +strtoIdd.c f13e3bc3 2105 +strtoIf.c f12c6af4 1875 +strtoIg.c ef30d392 3454 +strtoIx.c e50f716d 1960 +strtoIxL.c ea0b821b 1931 +strtod.c eec1df60 20532 +strtodI.c 1c2440ce 3915 +strtodg.c f6c3dd52 19911 +strtodnrp.c af895e9 2538 +strtof.c 1c5192d3 2073 +strtopQ.c f116d4f0 2563 +strtopd.c f7681c7a 1671 +strtopdd.c 9864fba 4497 +strtopf.c eb15b627 2067 +strtopx.c 1cafe482 2618 +strtopxL.c 1e4b77e9 2373 +strtorQ.c 9360a0b 2885 +strtord.c af5c50e 2491 +strtordd.c 1b266865 4936 +strtorf.c f0d86e2b 2396 +strtorx.c f19a56af 2947 +strtorxL.c 167fe87c 2704 +sum.c f525bad9 2494 +ulp.c 1e2e148f 1864 diff --git a/lib/nbsd_libc/gen/Lint___setjmp14.c b/lib/nbsd_libc/gen/Lint___setjmp14.c new file mode 100644 index 000000000..1d4f47fa5 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint___setjmp14.c @@ -0,0 +1,21 @@ +/* $NetBSD: Lint___setjmp14.c,v 1.3 2005/04/09 12:48:58 dsl Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +int +__setjmp14(jmp_buf env) +{ + return 0; +} + +/*ARGSUSED*/ +void +__longjmp14(jmp_buf env, int val) +{ +} diff --git a/lib/nbsd_libc/gen/Lint___sigsetjmp14.c b/lib/nbsd_libc/gen/Lint___sigsetjmp14.c new file mode 100644 index 000000000..7bd3da5b5 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint___sigsetjmp14.c @@ -0,0 +1,25 @@ +/* $NetBSD: Lint___sigsetjmp14.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +int +__sigsetjmp14(env, savemask) + sigjmp_buf env; + int savemask; +{ + return (0); +} + +/*ARGSUSED*/ +void +__siglongjmp14(env, val) + sigjmp_buf env; + int val; +{ +} diff --git a/lib/nbsd_libc/gen/Lint__setjmp.c b/lib/nbsd_libc/gen/Lint__setjmp.c new file mode 100644 index 000000000..f12a441bf --- /dev/null +++ b/lib/nbsd_libc/gen/Lint__setjmp.c @@ -0,0 +1,24 @@ +/* $NetBSD: Lint__setjmp.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +int +_setjmp(env) + jmp_buf env; +{ + return (0); +} + +/*ARGSUSED*/ +void +_longjmp(env, val) + jmp_buf env; + int val; +{ +} diff --git a/lib/nbsd_libc/gen/Lint_alloca.c b/lib/nbsd_libc/gen/Lint_alloca.c new file mode 100644 index 000000000..66624414f --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_alloca.c @@ -0,0 +1,16 @@ +/* $NetBSD: Lint_alloca.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +void * +alloca(size) + size_t size; +{ + return (0); +} diff --git a/lib/nbsd_libc/gen/Lint_bswap16.c b/lib/nbsd_libc/gen/Lint_bswap16.c new file mode 100644 index 000000000..2842e6f70 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_bswap16.c @@ -0,0 +1,13 @@ +/* $NetBSD: Lint_bswap16.c,v 1.3 2001/02/27 19:04:40 cgd Exp $ */ +/* Written by Manuel Bouyer. Public Domain */ + +#include + +/*ARGSUSED*/ +/*LINTED prototype mismatch */ +u_int16_t +bswap16(b16) + u_int16_t b16; +{ + return 0; +} diff --git a/lib/nbsd_libc/gen/Lint_bswap32.c b/lib/nbsd_libc/gen/Lint_bswap32.c new file mode 100644 index 000000000..3c7c4217d --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_bswap32.c @@ -0,0 +1,12 @@ +/* $NetBSD: Lint_bswap32.c,v 1.2 2001/02/27 19:04:40 cgd Exp $ */ +/* Written by Manuel Bouyer. Public Domain */ + +#include + +/*ARGSUSED*/ +u_int32_t +bswap32(b32) + u_int32_t b32; +{ + return 0; +} diff --git a/lib/nbsd_libc/gen/Lint_bswap64.c b/lib/nbsd_libc/gen/Lint_bswap64.c new file mode 100644 index 000000000..77286d77d --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_bswap64.c @@ -0,0 +1,12 @@ +/* $NetBSD: Lint_bswap64.c,v 1.2 2001/02/27 19:04:40 cgd Exp $ */ +/* Written by Manuel Bouyer. Public Domain */ + +#include + +/*ARGSUSED*/ +u_int64_t +bswap64(b64) + u_int64_t b64; +{ + return 0; +} diff --git a/lib/nbsd_libc/gen/Lint_fabs.c b/lib/nbsd_libc/gen/Lint_fabs.c new file mode 100644 index 000000000..fe28e2e92 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_fabs.c @@ -0,0 +1,16 @@ +/* $NetBSD: Lint_fabs.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +double +fabs(x) + double x; +{ + return (0.0); +} diff --git a/lib/nbsd_libc/gen/Lint_flt_rounds.c b/lib/nbsd_libc/gen/Lint_flt_rounds.c new file mode 100644 index 000000000..b0744ad64 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_flt_rounds.c @@ -0,0 +1,13 @@ +/* $NetBSD: Lint_flt_rounds.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +/*ARGSUSED*/ +int +__flt_rounds() +{ + return (0); +} diff --git a/lib/nbsd_libc/gen/Lint_fpgetmask.c b/lib/nbsd_libc/gen/Lint_fpgetmask.c new file mode 100644 index 000000000..8c6a12eb1 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_fpgetmask.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_fpgetmask.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +fp_except +fpgetmask() +{ + fp_except rv = { 0 }; + + return (rv); +} diff --git a/lib/nbsd_libc/gen/Lint_fpgetround.c b/lib/nbsd_libc/gen/Lint_fpgetround.c new file mode 100644 index 000000000..94f5f8c1b --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_fpgetround.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_fpgetround.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +fp_rnd +fpgetround() +{ + fp_rnd rv = { 0 }; + + return (rv); +} diff --git a/lib/nbsd_libc/gen/Lint_fpgetsticky.c b/lib/nbsd_libc/gen/Lint_fpgetsticky.c new file mode 100644 index 000000000..413898cf1 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_fpgetsticky.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_fpgetsticky.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +fp_except +fpgetsticky() +{ + fp_except rv = { 0 }; + + return (rv); +} diff --git a/lib/nbsd_libc/gen/Lint_fpsetmask.c b/lib/nbsd_libc/gen/Lint_fpsetmask.c new file mode 100644 index 000000000..f50fbba6c --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_fpsetmask.c @@ -0,0 +1,18 @@ +/* $NetBSD: Lint_fpsetmask.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +fp_except +fpsetmask(m) + fp_except m; +{ + fp_except rv = { 0 }; + + return (rv); +} diff --git a/lib/nbsd_libc/gen/Lint_fpsetround.c b/lib/nbsd_libc/gen/Lint_fpsetround.c new file mode 100644 index 000000000..9d7d216bf --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_fpsetround.c @@ -0,0 +1,18 @@ +/* $NetBSD: Lint_fpsetround.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +fp_rnd +fpsetround(r) + fp_rnd r; +{ + fp_rnd rv = { 0 }; + + return (rv); +} diff --git a/lib/nbsd_libc/gen/Lint_fpsetsticky.c b/lib/nbsd_libc/gen/Lint_fpsetsticky.c new file mode 100644 index 000000000..22fe09600 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_fpsetsticky.c @@ -0,0 +1,18 @@ +/* $NetBSD: Lint_fpsetsticky.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +fp_except +fpsetsticky(s) + fp_except s; +{ + fp_except rv = { 0 }; + + return (rv); +} diff --git a/lib/nbsd_libc/gen/Lint_frexp.c b/lib/nbsd_libc/gen/Lint_frexp.c new file mode 100644 index 000000000..3fb1c1472 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_frexp.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_frexp.c,v 1.2 1998/12/02 09:47:20 christos Exp $ */ + +/* + * This file placed in the public domain. + * Matthias Pfaller, December 6, 1997. + */ + +#include + +/*ARGSUSED*/ +double +frexp(value, eptr) + double value; + int *eptr; +{ + return(0); +} diff --git a/lib/nbsd_libc/gen/Lint_ldexp.c b/lib/nbsd_libc/gen/Lint_ldexp.c new file mode 100644 index 000000000..3cb80c04a --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_ldexp.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_ldexp.c,v 1.2 1998/12/02 09:47:20 christos Exp $ */ + +/* + * This file placed in the public domain. + * Matthias Pfaller, December 6, 1997. + */ + +#include + +/*ARGSUSED*/ +double +ldexp(val, exp) + double val; + int exp; +{ + return(0); +} diff --git a/lib/nbsd_libc/gen/Lint_modf.c b/lib/nbsd_libc/gen/Lint_modf.c new file mode 100644 index 000000000..1acf0ff7f --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_modf.c @@ -0,0 +1,16 @@ +/* $NetBSD: Lint_modf.c,v 1.2 2000/06/14 06:49:05 cgd Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include + +/*ARGSUSED*/ +double +modf(value, iptr) + double value, *iptr; +{ + return (0.0); +} diff --git a/lib/nbsd_libc/gen/Lint_resumecontext.c b/lib/nbsd_libc/gen/Lint_resumecontext.c new file mode 100644 index 000000000..529b96f54 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_resumecontext.c @@ -0,0 +1,14 @@ +/* $NetBSD: Lint_resumecontext.c,v 1.2 2003/01/18 11:38:46 thorpej Exp $ */ + +/* + * This file placed in the public domain. + * Klaus Klein, January 26, 1999. + */ + +#include + +/*ARGSUSED*/ +void +_resumecontext() +{ +} diff --git a/lib/nbsd_libc/gen/Lint_swapcontext.c b/lib/nbsd_libc/gen/Lint_swapcontext.c new file mode 100644 index 000000000..0e0b45e80 --- /dev/null +++ b/lib/nbsd_libc/gen/Lint_swapcontext.c @@ -0,0 +1,18 @@ +/* $NetBSD: Lint_swapcontext.c,v 1.2 2003/01/18 11:38:47 thorpej Exp $ */ + +/* + * This file placed in the public domain. + * Klaus Klein, November 29, 1998. + */ + +#include + +/*ARGSUSED*/ +int +swapcontext(oucp, ucp) + ucontext_t *oucp; + const ucontext_t *ucp; +{ + + return (0); +} diff --git a/lib/nbsd_libc/gen/Makefile.inc b/lib/nbsd_libc/gen/Makefile.inc new file mode 100644 index 000000000..1beaad029 --- /dev/null +++ b/lib/nbsd_libc/gen/Makefile.inc @@ -0,0 +1,169 @@ +# $NetBSD: Makefile.inc,v 1.173 2010/12/12 20:22:48 christos Exp $ +# from: @(#)Makefile.inc 8.6 (Berkeley) 5/4/95 + +# gen sources +.PATH: ${ARCHDIR}/gen ${.CURDIR}/gen + +SRCS+= _errno.c alarm.c alphasort.c arc4random.c assert.c basename.c clock.c \ + closedir.c closefrom.c confstr.c ctermid.c ctype_.c daemon.c \ + dehumanize_number.c devname.c dirname.c disklabel.c err.c errx.c \ + errlist.c errno.c execl.c execle.c execlp.c execv.c execvp.c \ + extattr.c fmtcheck.c fmtmsg.c fnmatch.c fstab.c ftok.c \ + fts.c ftw.c getbsize.c getcap.c getcwd.c \ + getdevmajor.c getdomainname.c getgrent.c \ + getgrouplist.c getgroupmembership.c gethostname.c \ + getloadavg.c getlogin.c getmntinfo.c \ + getnetgrent.c getpagesize.c \ + getpass.c getprogname.c getpwent.c getttyent.c \ + getusershell.c glob.c humanize_number.c initdir.c initgroups.c \ + isascii.c isatty.c isctype.c lockf.c nftw.c \ + nice.c nlist.c nlist_aout.c \ + nlist_coff.c nlist_ecoff.c nlist_elf32.c nlist_elf64.c opendir.c \ + pause.c popen.c psignal.c pthread_atfork.c ptree.c pwcache.c \ + pw_scan.c raise.c randomid.c rb.c readdir.c rewinddir.c \ + scandir.c seekdir.c setdomainname.c \ + sethostname.c setjmperr.c setmode.c setproctitle.c setprogname.c \ + shquote.c shquotev.c sighold.c sigignore.c siginterrupt.c \ + siglist.c signal.c signame.c sigrelse.c \ + sigset.c sigsetops.c sleep.c \ + stringlist.c sysconf.c sysctl.c sysctlbyname.c sysctlgetmibinfo.c \ + sysctlnametomib.c syslog.c telldir.c time.c \ + times.c toascii.c tolower_.c ttyname.c ttyslot.c \ + toupper_.c ualarm.c ulimit.c uname.c unvis.c usleep.c utime.c utmp.c \ + utmpx.c valloc.c vis.c wait.c wait3.c waitpid.c warn.c warnx.c \ + vwarn.c vwarnx.c verr.c verrx.c wordexp.c + +# indirect reference stubs, to be removed soon. +SRCS+= _err.c _errx.c \ + _sysconf.c _verr.c _verrx.c _vwarn.c _vwarnx.c _warn.c _warnx.c + +# uses alloca +COPTS.execvp.c = -Wno-stack-protector +COPTS.execl.c = -Wno-stack-protector +COPTS.execle.c = -Wno-stack-protector +COPTS.execlp.c = -Wno-stack-protector + +# machine-dependent gen sources +# m-d Makefile.inc must include sources for: +# _setjmp() bswap16() bswap32() bswap64() fabs() infinity +# isinf() setjmp() sigsetjmp() + +.include "${ARCHDIR}/gen/Makefile.inc" + +MAN+= alarm.3 arc4random.3 basename.3 bswap.3 clock.3 closefrom.3 confstr.3 \ + cpuset.3 ctermid.3 ctype.3 daemon.3 devname.3 directory.3 dirname.3 \ + endutxent.3 err.3 exec.3 extattr.3 \ + fmtcheck.3 fmtmsg.3 fnmatch.3 fpclassify.3 fpgetmask.3 \ + ftok.3 fts.3 ftw.3 \ + getbsize.3 cgetcap.3 getcwd.3 getdevmajor.3 \ + getdomainname.3 getdiskbyname.3 getfsent.3 \ + getgrent.3 getgrouplist.3 gethostname.3 getlastlogx.3 getloadavg.3 \ + getmntinfo.3 getnetgrent.3 getpagesize.3 getpass.3 \ + getprogname.3 getpwent.3 getttyent.3 getusershell.3 glob.3 \ + humanize_number.3 \ + initgroups.3 isalnum.3 isalpha.3 isascii.3 isblank.3 iscntrl.3 \ + isdigit.3 isfinite.3 isgraph.3 isgreater.3 isinf.3 islower.3 isnan.3 \ + isnormal.3 isprint.3 ispunct.3 isspace.3 isupper.3 isxdigit.3 \ + lockf.3 _lwp_makecontext.3 makecontext.3 \ + nice.3 nlist.3 \ + pause.3 popen.3 psignal.3 pwcache.3 pthread_atfork.3 \ + raise.3 randomid.3 realpath.3 scandir.3 setjmp.3 setmode.3 \ + setproctitle.3 shquote.3 sighold.3 sigignore.3 siginterrupt.3 \ + signal.3 signbit.3 sigrelse.3 sigset.3 sigsetops.3 sleep.3 \ + stringlist.3 sysconf.3 sysctl.3 syslog.3 time.3 times.3 \ + timezone.3 toascii.3 tolower.3 toupper.3 ttyname.3 \ + ualarm.3 ulimit.3 uname.3 unvis.3 usleep.3 utime.3 valloc.3 vis.3 \ + wordexp.3 + +MLINKS+=bswap.3 bswap16.3 bswap.3 bswap32.3 bswap.3 bswap64.3 +MLINKS+=cpuset.3 cpuset_create.3 cpuset.3 cpuset_destroy.3 \ + cpuset.3 cpuset_zero.3 cpuset.3 cpuset_set.3 cpuset.3 cpuset_clr.3 \ + cpuset.3 cpuset_isset.3 cpuset.3 cpuset_size.3 +MLINKS+=directory.3 closedir.3 directory.3 dirfd.3 directory.3 opendir.3 \ + directory.3 readdir.3 directory.3 readdir_r.3 directory.3 \ + rewinddir.3 directory.3 seekdir.3 directory.3 telldir.3 \ + directory.3 fdopendir.3 +MLINKS+=endutxent.3 getutxent.3 endutxent.3 getutxid.3 \ + endutxent.3 getutxline.3 endutxent.3 pututxline.3 \ + endutxent.3 setutxent.3 +MLINKS+=err.3 verr.3 err.3 errx.3 err.3 verrx.3 err.3 warn.3 err.3 vwarn.3 \ + err.3 warnx.3 err.3 vwarnx.3 +MLINKS+=exec.3 execl.3 exec.3 execle.3 exec.3 execlp.3 exec.3 execv.3 \ + exec.3 execvp.3 exec.3 exect.3 +MLINKS+=extattr.3 extattr_namespace_to_string.3 \ + extattr.3 extattr_string_to_namespace.3 +MLINKS+=fpgetmask.3 fpgetround.3 fpgetmask.3 fpgetsticky.3 \ + fpgetmask.3 fpsetmask.3 fpgetmask.3 fpsetround.3 \ + fpgetmask.3 fpsetsticky.3 +MLINKS+=fts.3 fts_open.3 fts.3 fts_read.3 fts.3 fts_children.3 \ + fts.3 fts_set.3 fts.3 fts_close.3 +MLINKS+=ftw.3 nftw.3 +MLINKS+=cgetcap.3 cgetclose.3 cgetcap.3 cgetent.3 \ + cgetcap.3 cgetfirst.3 cgetcap.3 cgetmatch.3 cgetcap.3 cgetnext.3 \ + cgetcap.3 cgetnum.3 cgetcap.3 cgetset.3 cgetcap.3 cgetstr.3 \ + cgetcap.3 cgetustr.3 +MLINKS+=getcwd.3 getwd.3 +MLINKS+=getdiskbyname.3 setdisktab.3 +MLINKS+=getdomainname.3 setdomainname.3 +MLINKS+=getfsent.3 endfsent.3 getfsent.3 getfsfile.3 getfsent.3 getfsspec.3 \ + getfsent.3 setfsent.3 +MLINKS+=getgrent.3 endgrent.3 getgrent.3 setgroupent.3 getgrent.3 getgrgid.3 \ + getgrent.3 getgrnam.3 getgrent.3 setgrent.3 +MLINKS+=getgrent.3 getgrgid_r.3 getgrent.3 getgrnam_r.3 getgrent.3 getgrent_r.3 +MLINKS+=getgrouplist.3 getgroupmembership.3 +MLINKS+=gethostname.3 sethostname.3 +MLINKS+=getlastlogx.3 getutmp.3 getlastlogx.3 getutmpx.3 +MLINKS+=getlastlogx.3 updlastlogx.3 getlastlogx.3 updwtmpx.3 +MLINKS+=getlastlogx.3 utmpxname.3 +MLINKS+=getnetgrent.3 endnetgrent.3 getnetgrent.3 innetgr.3 \ + getnetgrent.3 setnetgrent.3 +MLINKS+=getprogname.3 setprogname.3 +MLINKS+=getpwent.3 endpwent.3 getpwent.3 setpassent.3 getpwent.3 getpwnam.3 \ + getpwent.3 getpwuid.3 getpwent.3 setpwent.3 +MLINKS+=getpwent.3 getpwnam_r.3 getpwent.3 getpwuid_r.3 getpwent.3 getpwent_r.3 +# getpwent.3 setpwfile.3 - deprecated +MLINKS+=getttyent.3 endttyent.3 getttyent.3 getttynam.3 \ + getttyent.3 setttyent.3 getttyent.3 setttyentpath.3 +MLINKS+=getusershell.3 endusershell.3 getusershell.3 setusershell.3 +MLINKS+=glob.3 globfree.3 +MLINKS+=glob.3 glob_pattern_p.3 +MLINKS+=humanize_number.3 dehumanize_number.3 +MLINKS+=makecontext.3 swapcontext.3 +MLINKS+=popen.3 pclose.3 +MLINKS+=psignal.3 sys_siglist.3 psignal.3 sys_signame.3 psignal.3 psiginfo.3 +MLINKS+=pwcache.3 user_from_uid.3 pwcache.3 group_from_gid.3 +MLINKS+=pwcache.3 uid_from_user.3 pwcache.3 gid_from_group.3 +MLINKS+=pwcache.3 pwcache_userdb.3 pwcache.3 pwcache_groupdb.3 +MLINKS+=randomid.3 randomid_new.3 randomid.3 randomid_delete.3 +MLINKS+=scandir.3 alphasort.3 +MLINKS+=setjmp.3 _longjmp.3 setjmp.3 _setjmp.3 setjmp.3 longjmp.3 \ + setjmp.3 longjmperror.3 setjmp.3 sigsetjmp.3 setjmp.3 siglongjmp.3 +MLINKS+=setmode.3 getmode.3 +MLINKS+=shquote.3 shquotev.3 +MLINKS+=sigsetops.3 sigemptyset.3 sigsetops.3 sigfillset.3 \ + sigsetops.3 sigaddset.3 sigsetops.3 sigdelset.3 \ + sigsetops.3 sigismember.3 +MLINKS+=stringlist.3 sl_init.3 stringlist.3 sl_free.3 stringlist.3 sl_add.3 \ + stringlist.3 sl_find.3 +MLINKS+=sysctl.3 sysctlbyname.3 sysctl.3 sysctlgetmibinfo.3 \ + sysctl.3 sysctlnametomib.3 +MLINKS+=syslog.3 closelog.3 syslog.3 openlog.3 syslog.3 setlogmask.3 \ + syslog.3 vsyslog.3 +MLINKS+=syslog.3 closelog_r.3 syslog.3 openlog_r.3 syslog.3 setlogmask_r.3 \ + syslog.3 syslog_r.3 syslog.3 vsyslog_r.3 +MLINKS+=syslog.3 syslogp.3 syslog.3 syslogp_r.3 \ + syslog.3 vsyslogp.3 syslog.3 vsyslogp_r.3 +MLINKS+=ttyname.3 isatty.3 ttyname.3 ttyslot.3 ttyname.3 ttyname_r.3 +MLINKS+=unvis.3 strunvis.3 unvis.3 strunvisx.3 +MLINKS+=vis.3 svis.3 vis.3 strvis.3 vis.3 strvisx.3 vis.3 strsvis.3 \ + vis.3 strsvisx.3 + +CPPFLAGS.ctype_.c+= -I${LIBCDIR}/locale +CPPFLAGS.isctype.c+= -I${LIBCDIR}/locale +CPPFLAGS.tolower_.c+= -I${LIBCDIR}/locale +CPPFLAGS.toupper_.c+= -I${LIBCDIR}/locale + +errlist.c: errlist.awk ${NETBSDSRCDIR}/sys/sys/errno.h + ${TOOL_AWK} -f ${.ALLSRC} > ${.TARGET} + +CLEANFILES+= errlist.c diff --git a/lib/nbsd_libc/gen/_err.c b/lib/nbsd_libc/gen/_err.c new file mode 100644 index 000000000..ddeef6824 --- /dev/null +++ b/lib/nbsd_libc/gen/_err.c @@ -0,0 +1,30 @@ +/* $NetBSD: _err.c,v 1.11 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _err.c,v 1.11 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_err, err) +#else + +#include + +__dead void _verr(int eval, const char *, _BSD_VA_LIST_); + +__dead void +err(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _verr(eval, fmt, ap); + va_end(ap); +} +#endif diff --git a/lib/nbsd_libc/gen/_errno.c b/lib/nbsd_libc/gen/_errno.c new file mode 100644 index 000000000..257900d1e --- /dev/null +++ b/lib/nbsd_libc/gen/_errno.c @@ -0,0 +1,55 @@ +/* $NetBSD: _errno.c,v 1.13 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _errno.c,v 1.13 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "reentrant.h" +#include +#include + +#undef errno +extern int errno; + +int * +__errno(void) +{ +#ifdef _REENTRANT + if (__isthreaded == 0) + return &errno; + + return thr_errno(); +#else + return &errno; +#endif +} diff --git a/lib/nbsd_libc/gen/_errx.c b/lib/nbsd_libc/gen/_errx.c new file mode 100644 index 000000000..79b5b8034 --- /dev/null +++ b/lib/nbsd_libc/gen/_errx.c @@ -0,0 +1,30 @@ +/* $NetBSD: _errx.c,v 1.11 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _errx.c,v 1.11 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_errx, errx) +#else + +#include + +__dead void _verrx(int eval, const char *, _BSD_VA_LIST_); + +__dead void +errx(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _verrx(eval, fmt, ap); + va_end(ap); +} +#endif diff --git a/lib/nbsd_libc/gen/_lwp_makecontext.3 b/lib/nbsd_libc/gen/_lwp_makecontext.3 new file mode 100644 index 000000000..9994c98d1 --- /dev/null +++ b/lib/nbsd_libc/gen/_lwp_makecontext.3 @@ -0,0 +1,74 @@ +.\" $NetBSD: _lwp_makecontext.3,v 1.4 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2003 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Jason R. Thorpe of Wasabi Systems, Inc. +.\" +.\" 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 January 16, 2003 +.Dt _LWP_MAKECONTEXT 3 +.Os +.Sh NAME +.Nm _lwp_makecontext +.Nd create a new initial light-weight process execution context +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In lwp.h +.Ft void +.Fn _lwp_makecontext "ucontext_t *context" "void (*start_routine)(void *)" \ + "void *arg" "void *private" "caddr_t stack_base" "size_t stack_size" +.Sh DESCRIPTION +.Fn _lwp_makecontext +initializes the context structure pointed to by +.Fa context +in a manner suitable for using with +.Xr _lwp_create 2 . +The LWP will begin execution at the function specified by +.Fa start_routine +which will be passed a single argument +.Fa arg . +The LWP private data pointer will be set to +.Fa private . +The stack region for the new LWP is specified by the +.Fa stack_base +and +.Fa stack_size +arguments. +.Pp +The signal mask in the context structure is not initialized by +.Fn _lwp_makecontext . +.Sh SEE ALSO +.Xr _lwp_create 2 , +.Xr _lwp_getprivate 2 +.Sh HISTORY +The +.Fn _lwp_create +system call first appeared in +.Nx 2.0 . +.Sh BUGS +The LWP private data pointer is not initialized by the current +implementation of +.Fn _lwp_makecontext . diff --git a/lib/nbsd_libc/gen/_sysconf.c b/lib/nbsd_libc/gen/_sysconf.c new file mode 100644 index 000000000..b4738c19c --- /dev/null +++ b/lib/nbsd_libc/gen/_sysconf.c @@ -0,0 +1,46 @@ +/* $NetBSD: _sysconf.c,v 1.5 2009/10/21 01:07:45 snj Exp $ */ + +/* + * Copyright (c) 1996 Christos Zoulas. 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _sysconf.c,v 1.5 2009/10/21 01:07:45 snj Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(__sysconf, sysconf) +#else + +#include +long __sysconf __P((int)); /* XXX */ + +long +sysconf(name) + int name; +{ + + return __sysconf(name); +} +#endif diff --git a/lib/nbsd_libc/gen/_verr.c b/lib/nbsd_libc/gen/_verr.c new file mode 100644 index 000000000..1baab5164 --- /dev/null +++ b/lib/nbsd_libc/gen/_verr.c @@ -0,0 +1,25 @@ +/* $NetBSD: _verr.c,v 1.10 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _verr.c,v 1.10 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_verr, verr) +#else + +__dead void _verr(int, const char *, _BSD_VA_LIST_); + +void +verr(int eval, const char *fmt, _BSD_VA_LIST_ ap) +{ + _verr(eval, fmt, ap); +} + +#endif diff --git a/lib/nbsd_libc/gen/_verrx.c b/lib/nbsd_libc/gen/_verrx.c new file mode 100644 index 000000000..0c161afab --- /dev/null +++ b/lib/nbsd_libc/gen/_verrx.c @@ -0,0 +1,25 @@ +/* $NetBSD: _verrx.c,v 1.10 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _verrx.c,v 1.10 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_verrx, verrx) +#else + +__dead void _verrx(int, const char *, _BSD_VA_LIST_); + +void +verrx(int eval, const char *fmt, _BSD_VA_LIST_ ap) +{ + _verrx(eval, fmt, ap); +} + +#endif diff --git a/lib/nbsd_libc/gen/_vwarn.c b/lib/nbsd_libc/gen/_vwarn.c new file mode 100644 index 000000000..76cef1c60 --- /dev/null +++ b/lib/nbsd_libc/gen/_vwarn.c @@ -0,0 +1,25 @@ +/* $NetBSD: _vwarn.c,v 1.10 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _vwarn.c,v 1.10 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_vwarn, vwarn) +#else + +void _vwarn(const char *, _BSD_VA_LIST_); + +void +vwarn(const char *fmt, _BSD_VA_LIST_ ap) +{ + _vwarn(fmt, ap); +} + +#endif diff --git a/lib/nbsd_libc/gen/_vwarnx.c b/lib/nbsd_libc/gen/_vwarnx.c new file mode 100644 index 000000000..37cdeaae3 --- /dev/null +++ b/lib/nbsd_libc/gen/_vwarnx.c @@ -0,0 +1,25 @@ +/* $NetBSD: _vwarnx.c,v 1.10 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _vwarnx.c,v 1.10 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_vwarnx, vwarnx) +#else + +void _vwarnx(const char *, _BSD_VA_LIST_); + +void +vwarnx(const char *fmt, _BSD_VA_LIST_ ap) +{ + _vwarnx(fmt, ap); +} + +#endif diff --git a/lib/nbsd_libc/gen/_warn.c b/lib/nbsd_libc/gen/_warn.c new file mode 100644 index 000000000..7fbed081e --- /dev/null +++ b/lib/nbsd_libc/gen/_warn.c @@ -0,0 +1,30 @@ +/* $NetBSD: _warn.c,v 1.10 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _warn.c,v 1.10 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_warn, warn) +#else + +#include + +void _vwarn(const char *, _BSD_VA_LIST_); + +void +warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vwarn(eval, fmt, ap); + va_end(ap); +} +#endif diff --git a/lib/nbsd_libc/gen/_warnx.c b/lib/nbsd_libc/gen/_warnx.c new file mode 100644 index 000000000..a5fec8ebb --- /dev/null +++ b/lib/nbsd_libc/gen/_warnx.c @@ -0,0 +1,31 @@ +/* $NetBSD: _warnx.c,v 1.10 2005/09/13 01:44:09 christos Exp $ */ + +/* + * J.T. Conklin, December 12, 1994 + * Public Domain + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _warnx.c,v 1.10 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_warnx, warnx) +#else + +#include + +void _vwarnx(const char *, _BSD_VA_LIST_); + +void +warnx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vwarnx(eval, fmt, ap); + va_end(ap); +} + +#endif diff --git a/lib/nbsd_libc/gen/alarm.3 b/lib/nbsd_libc/gen/alarm.3 new file mode 100644 index 000000000..4eba88342 --- /dev/null +++ b/lib/nbsd_libc/gen/alarm.3 @@ -0,0 +1,98 @@ +.\" $NetBSD: alarm.3,v 1.22 2003/08/07 16:42:45 agc Exp $ +.\" +.\" Copyright (c) 1980, 1991, 1993, 1994 +.\" 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. +.\" +.\" @(#)alarm.3 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 19, 1994 +.Dt ALARM 3 +.Os +.Sh NAME +.Nm alarm +.Nd set signal timer alarm +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft unsigned int +.Fn alarm "unsigned int seconds" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by +.Xr setitimer 2 . +.Ef +.Pp +The +.Fn alarm +function sets a timer to deliver the signal +.Dv SIGALRM +to the calling process +.Ar seconds +after the call to +.Fn alarm . +If an alarm has already been set with +.Fn alarm +but has not been delivered, another call to +.Fn alarm +will supersede the prior call. +The request +.Fn alarm "0" +voids the current +alarm and the signal SIGALRM will not be delivered. +The maximum number of +.Ar seconds +allowed +is 2147483647. +.Pp +The return value of +.Fn alarm +is the amount of time left on the timer from a previous call to +.Fn alarm . +If no alarm is currently set, the return value is 0. +If there is an error setting the timer, +.Fn alarm +returns ((unsigned int) -1). +.Sh SEE ALSO +.Xr setitimer 2 , +.Xr sigaction 2 , +.Xr sigsuspend 2 , +.Xr signal 3 , +.Xr sigvec 3 , +.Xr sleep 3 , +.Xr ualarm 3 , +.Xr usleep 3 +.Sh STANDARDS +The +.Fn alarm +function conforms to +.St -p1003.1-90 . +.Sh HISTORY +An +.Fn alarm +function appeared in +.At v7 . diff --git a/lib/nbsd_libc/gen/alarm.c b/lib/nbsd_libc/gen/alarm.c new file mode 100644 index 000000000..5ad22c10c --- /dev/null +++ b/lib/nbsd_libc/gen/alarm.c @@ -0,0 +1,67 @@ +/* $NetBSD: alarm.c,v 1.10 2003/08/07 16:42:45 agc Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)alarm.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: alarm.c,v 1.10 2003/08/07 16:42:45 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Backwards compatible alarm. + */ +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(alarm,_alarm) +#endif + +unsigned int +alarm(secs) + unsigned int secs; +{ + struct itimerval it, oitv; + struct itimerval *itp = ⁢ + + timerclear(&itp->it_interval); + itp->it_value.tv_sec = secs; + itp->it_value.tv_usec = 0; + if (setitimer(ITIMER_REAL, itp, &oitv) == -1) + return (unsigned int) -1; + if (oitv.it_value.tv_usec) + oitv.it_value.tv_sec++; + return (unsigned int)oitv.it_value.tv_sec; +} diff --git a/lib/nbsd_libc/gen/alphasort.c b/lib/nbsd_libc/gen/alphasort.c new file mode 100644 index 000000000..c8310d425 --- /dev/null +++ b/lib/nbsd_libc/gen/alphasort.c @@ -0,0 +1,59 @@ +/* $NetBSD: alphasort.c,v 1.2 2009/03/01 19:59:55 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94"; +#else +__RCSID("$NetBSD: alphasort.c,v 1.2 2009/03/01 19:59:55 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +/* + * Alphabetic order comparison routine for those who want it. + */ +int +alphasort(const void *d1, const void *d2) +{ + + _DIAGASSERT(d1 != NULL); + _DIAGASSERT(d2 != NULL); + + return (strcmp((*(const struct dirent *const *)d1)->d_name, + (*(const struct dirent *const *)d2)->d_name)); +} diff --git a/lib/nbsd_libc/gen/arc4random.3 b/lib/nbsd_libc/gen/arc4random.3 new file mode 100644 index 000000000..702ad788d --- /dev/null +++ b/lib/nbsd_libc/gen/arc4random.3 @@ -0,0 +1,113 @@ +.\" $NetBSD: arc4random.3,v 1.7 2005/12/26 19:40:14 perry Exp $ +.\" $OpenBSD: arc4random.3,v 1.17 2000/12/21 14:07:41 aaron Exp $ +.\" +.\" Copyright 1997 Niels Provos +.\" 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 by Niels Provos. +.\" 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. +.\" +.\" Manual page, using -mandoc macros +.\" +.Dd April 15, 1997 +.Dt ARC4RANDOM 3 +.Os +.Sh NAME +.Nm arc4random , +.Nm arc4random_stir , +.Nm arc4random_addrandom +.Nd arc4 random number generator +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft uint32_t +.Fn arc4random "void" +.Ft void +.Fn arc4random_stir "void" +.Ft void +.Fn arc4random_addrandom "u_char *dat" "int datlen" +.Sh DESCRIPTION +The +.Fn arc4random +function provides a high quality 32-bit pseudo-random +number very quickly. +.Fn arc4random +seeds itself on a regular basis from the kernel strong random number +subsystem described in +.Xr rnd 4 . +On each call, an ARC4 generator is used to generate a new result. +The +.Fn arc4random +function uses the ARC4 cipher key stream generator, +which uses 8*8 8 bit S-Boxes. +The S-Boxes can be in about (2**1700) states. +.Pp +.Fn arc4random +fits into a middle ground not covered by other subsystems such as +the strong, slow, and resource expensive random +devices described in +.Xr rnd 4 +versus the fast but poor quality interfaces described in +.Xr rand 3 , +.Xr random 3 , +and +.Xr drand48 3 . +.Pp +The +.Fn arc4random_stir +function reads data from +.Pa /dev/urandom +and uses it to permute the S-Boxes via +.Fn arc4random_addrandom . +.Pp +There is no need to call +.Fn arc4random_stir +before using +.Fn arc4random , +since +.Fn arc4random +automatically initializes itself. +.Sh SEE ALSO +.Xr rand 3 , +.Xr rand48 3 , +.Xr random 3 +.Sh HISTORY +An algorithm called +.Pa RC4 +was designed by RSA Data Security, Inc. +It was considered a trade secret, but not trademarked. +Because it was a trade secret, it obviously could not be patented. +A clone of this was posted anonymously to USENET and confirmed to +be equivalent by several sources who had access to the original cipher. +Because of the trade secret situation, RSA Data Security, Inc. can do +nothing about the release of the ARC4 algorithm. +Since +.Pa RC4 +used to be a trade secret, the cipher is now referred to as +.Pa ARC4 . +.Pp +These functions first appeared in +.Ox 2.1 . diff --git a/lib/nbsd_libc/gen/arc4random.c b/lib/nbsd_libc/gen/arc4random.c new file mode 100644 index 000000000..1b9f2229d --- /dev/null +++ b/lib/nbsd_libc/gen/arc4random.c @@ -0,0 +1,217 @@ +/* $NetBSD: arc4random.c,v 1.9 2005/12/24 21:11:16 perry Exp $ */ +/* $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */ + +/* + * Arc4 random number generator for OpenBSD. + * Copyright 1996 David Mazieres . + * + * Modification and redistribution in source and binary forms is + * permitted provided that due credit is given to the author and the + * OpenBSD project by leaving this copyright notice intact. + */ + +/* + * This code is derived from section 17.1 of Applied Cryptography, + * second edition, which describes a stream cipher allegedly + * compatible with RSA Labs "RC4" cipher (the actual description of + * which is a trade secret). The same algorithm is used as a stream + * cipher called "arcfour" in Tatu Ylonen's ssh package. + * + * Here the stream cipher has been modified always to include the time + * when initializing the state. That makes it impossible to + * regenerate the same random sequence twice, so this can't be used + * for encryption, but will generate good random numbers. + * + * RC4 is a registered trademark of RSA Laboratories. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: arc4random.c,v 1.9 2005/12/24 21:11:16 perry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(arc4random,_arc4random) +#endif + +struct arc4_stream { + u_int8_t i; + u_int8_t j; + u_int8_t s[256]; +}; + +static int rs_initialized; +static struct arc4_stream rs; + +static inline void arc4_init(struct arc4_stream *); +static inline void arc4_addrandom(struct arc4_stream *, u_char *, int); +static void arc4_stir(struct arc4_stream *); +static inline u_int8_t arc4_getbyte(struct arc4_stream *); +static inline u_int32_t arc4_getword(struct arc4_stream *); + +static inline void +arc4_init(as) + struct arc4_stream *as; +{ + int n; + + for (n = 0; n < 256; n++) + as->s[n] = n; + as->i = 0; + as->j = 0; +} + +static inline void +arc4_addrandom(as, dat, datlen) + struct arc4_stream *as; + u_char *dat; + int datlen; +{ + int n; + u_int8_t si; + + as->i--; + for (n = 0; n < 256; n++) { + as->i = (as->i + 1); + si = as->s[as->i]; + as->j = (as->j + si + dat[n % datlen]); + as->s[as->i] = as->s[as->j]; + as->s[as->j] = si; + } + as->j = as->i; +} + +static void +arc4_stir(as) + struct arc4_stream *as; +{ + int fd; + struct { + struct timeval tv; + u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; + } rdat; + int n; + + gettimeofday(&rdat.tv, NULL); + fd = open("/dev/urandom", O_RDONLY); + if (fd != -1) { + read(fd, rdat.rnd, sizeof(rdat.rnd)); + close(fd); + } +#ifdef KERN_URND + else { + int mib[2]; + u_int i; + size_t len; + + /* Device could not be opened, we might be chrooted, take + * randomness from sysctl. */ + + mib[0] = CTL_KERN; + mib[1] = KERN_URND; + + for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i++) { + len = sizeof(u_int); + if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) + break; + } + } +#endif + /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take + * whatever was on the stack... */ + + arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); + + /* + * Throw away the first N words of output, as suggested in the + * paper "Weaknesses in the Key Scheduling Algorithm of RC4" + * by Fluher, Mantin, and Shamir. (N = 256 in our case.) + */ + for (n = 0; n < 256 * 4; n++) + arc4_getbyte(as); +} + +static inline u_int8_t +arc4_getbyte(as) + struct arc4_stream *as; +{ + u_int8_t si, sj; + + as->i = (as->i + 1); + si = as->s[as->i]; + as->j = (as->j + si); + sj = as->s[as->j]; + as->s[as->i] = sj; + as->s[as->j] = si; + return (as->s[(si + sj) & 0xff]); +} + +static inline u_int32_t +arc4_getword(as) + struct arc4_stream *as; +{ + u_int32_t val; + val = arc4_getbyte(as) << 24; + val |= arc4_getbyte(as) << 16; + val |= arc4_getbyte(as) << 8; + val |= arc4_getbyte(as); + return val; +} + +void +arc4random_stir() +{ + if (!rs_initialized) { + arc4_init(&rs); + rs_initialized = 1; + } + arc4_stir(&rs); +} + +void +arc4random_addrandom(dat, datlen) + u_char *dat; + int datlen; +{ + if (!rs_initialized) + arc4random_stir(); + arc4_addrandom(&rs, dat, datlen); +} + +u_int32_t +arc4random() +{ + if (!rs_initialized) + arc4random_stir(); + return arc4_getword(&rs); +} + +#if 0 +/*-------- Test code for i386 --------*/ +#include +#include +int +main(int argc, char **argv) +{ + const int iter = 1000000; + int i; + pctrval v; + + v = rdtsc(); + for (i = 0; i < iter; i++) + arc4random(); + v = rdtsc() - v; + v /= iter; + + printf("%qd cycles\n", v); +} +#endif diff --git a/lib/nbsd_libc/gen/assert.c b/lib/nbsd_libc/gen/assert.c new file mode 100644 index 000000000..7cbf555de --- /dev/null +++ b/lib/nbsd_libc/gen/assert.c @@ -0,0 +1,141 @@ +/* $NetBSD: assert.c,v 1.16 2005/02/09 21:35:46 kleink 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)assert.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: assert.c,v 1.16 2005/02/09 21:35:46 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include + +void +__assert13(file, line, function, failedexpr) + const char *file, *function, *failedexpr; + int line; +{ + + (void)fprintf(stderr, + "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n", + failedexpr, file, line, + function ? ", function \"" : "", + function ? function : "", + function ? "\"" : ""); + abort(); + /* NOTREACHED */ +} + +void +__assert(file, line, failedexpr) + const char *file, *failedexpr; + int line; +{ + + __assert13(file, line, NULL, failedexpr); + /* NOTREACHED */ +} + + +enum { + DIAGASSERT_ABORT = 1<<0, + DIAGASSERT_STDERR = 1<<1, + DIAGASSERT_SYSLOG = 1<<2 +}; + +static int diagassert_flags = -1; + +void +__diagassert13(file, line, function, failedexpr) + const char *file, *function, *failedexpr; + int line; +{ + char buf[1024]; + + if (diagassert_flags == -1) { + char *p; + + diagassert_flags = DIAGASSERT_SYSLOG; + + for (p = getenv("LIBC_DIAGASSERT"); p && *p; p++) { + switch (*p) { + case 'a': + diagassert_flags |= DIAGASSERT_ABORT; + break; + case 'A': + diagassert_flags &= ~DIAGASSERT_ABORT; + break; + case 'e': + diagassert_flags |= DIAGASSERT_STDERR; + break; + case 'E': + diagassert_flags &= ~DIAGASSERT_STDERR; + break; + case 'l': + diagassert_flags |= DIAGASSERT_SYSLOG; + break; + case 'L': + diagassert_flags &= ~DIAGASSERT_SYSLOG; + break; + } + } + } + + snprintf(buf, sizeof(buf), + "assertion \"%s\" failed: file \"%s\", line %d%s%s%s", + failedexpr, file, line, + function ? ", function \"" : "", + function ? function : "", + function ? "\"" : ""); + if (diagassert_flags & DIAGASSERT_STDERR) + (void)fprintf(stderr, "%s: %s\n", getprogname(), buf); + if (diagassert_flags & DIAGASSERT_SYSLOG) + syslog(LOG_DEBUG | LOG_USER, "%s", buf); + if (diagassert_flags & DIAGASSERT_ABORT) + abort(); +} + +void +__diagassert(file, line, failedexpr) + const char *file, *failedexpr; + int line; +{ + + __diagassert13(file, line, NULL, failedexpr); +} diff --git a/lib/nbsd_libc/gen/basename.3 b/lib/nbsd_libc/gen/basename.3 new file mode 100644 index 000000000..7fe1e96d1 --- /dev/null +++ b/lib/nbsd_libc/gen/basename.3 @@ -0,0 +1,95 @@ +.\" $NetBSD: basename.3,v 1.14 2008/05/10 22:39:40 christos Exp $ +.\" +.\" Copyright (c) 1997, 2002 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Klaus Klein and Jason R. Thorpe. +.\" +.\" 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 May 10, 2008 +.Dt BASENAME 3 +.Os +.Sh NAME +.Nm basename +.Nd return the last component of a pathname +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In libgen.h +.Ft char * +.Fn basename "char *path" +.Sh DESCRIPTION +The +.Fn basename +function takes the pathname pointed to by +.Ar path +and returns a pointer to the final component of the pathname, +deleting any trailing +.Sq / +characters. +.Pp +If +.Ar path +consists entirely of +.Sq / +characters, +.Fn basename +returns a pointer to the string +.Dq / . +.Pp +If +.Ar path +is a null pointer or points to an empty string, +.Fn basename +returns a pointer to the string +.Dq \&. . +.Sh RETURN VALUES +The +.Fn basename +function returns a pointer to the final component of +.Ar path . +.Sh SEE ALSO +.Xr basename 1 , +.Xr dirname 3 +.Sh STANDARDS +.Bl -bullet -compact +.It +.St -xpg4.2 +.It +.St -p1003.1-2001 +.El +.Sh BUGS +If the length of the result is longer than +.Dv PATH_MAX +bytes +.Pq including the terminating nul , +the result will be truncated. +.Pp +The +.Fn basename +function returns a pointer to static storage that may be overwritten +by subsequent calls to +.Fn basename . +This is not strictly a bug; it is explicitly allowed by +.St -p1003.1-2001 . diff --git a/lib/nbsd_libc/gen/basename.c b/lib/nbsd_libc/gen/basename.c new file mode 100644 index 000000000..be5fe8f79 --- /dev/null +++ b/lib/nbsd_libc/gen/basename.c @@ -0,0 +1,85 @@ +/* $NetBSD: basename.c,v 1.9 2009/11/24 13:34:20 tnozaki Exp $ */ + +/*- + * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Klaus Klein and Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: basename.c,v 1.9 2009/11/24 13:34:20 tnozaki Exp $"); +#endif /* !LIBC_SCCS && !lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(basename,_basename) +#endif + +#if !HAVE_BASENAME +char * +basename(char *path) +{ + static char result[PATH_MAX]; + const char *p, *lastp; + size_t len; + + /* + * If `path' is a null pointer or points to an empty string, + * return a pointer to the string ".". + */ + if ((path == NULL) || (*path == '\0')) { + result[0] = '.'; + result[1] = '\0'; + + return (result); + } + + /* Strip trailing slashes, if any. */ + lastp = path + strlen(path) - 1; + while (lastp != path && *lastp == '/') + lastp--; + + /* Now find the beginning of this (final) component. */ + p = lastp; + while (p != path && *(p - 1) != '/') + p--; + + /* ...and copy the result into the result buffer. */ + len = (lastp - p) + 1 /* last char */; + if (len > (PATH_MAX - 1)) + len = PATH_MAX - 1; + + memcpy(result, p, len); + result[len] = '\0'; + + return (result); +} +#endif diff --git a/lib/nbsd_libc/gen/bswap.3 b/lib/nbsd_libc/gen/bswap.3 new file mode 100644 index 000000000..cb8d03f0e --- /dev/null +++ b/lib/nbsd_libc/gen/bswap.3 @@ -0,0 +1,54 @@ +.\" $NetBSD: bswap.3,v 1.11 2009/10/22 21:50:01 bouyer Exp $ +.\" +.\" Copyright (c) 1998 Manuel Bouyer. +.\" +.\" 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 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 March 17, 1998 +.Dt BSWAP 3 +.Os +.Sh NAME +.Nm bswap16 , +.Nm bswap32 , +.Nm bswap64 +.Nd byte-order swapping functions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In machine/bswap.h +.Ft uint16_t +.Fn bswap16 "uint16_t" +.Ft uint32_t +.Fn bswap32 "uint32_t" +.Ft uint64_t +.Fn bswap64 "uint64_t" +.Sh DESCRIPTION +The +.Fn bswap16 , +.Fn bswap32 , +and +.Fn bswap64 +functions return the value of their argument with the bytes inverted. +They can be used to convert 16, 32 or 64 bits integers from little to big +endian, or vice-versa. +.Sh SEE ALSO +.Xr byteorder 3 diff --git a/lib/nbsd_libc/gen/cgetcap.3 b/lib/nbsd_libc/gen/cgetcap.3 new file mode 100644 index 000000000..1a43ccc13 --- /dev/null +++ b/lib/nbsd_libc/gen/cgetcap.3 @@ -0,0 +1,623 @@ +.\" $NetBSD: cgetcap.3,v 1.6 2009/08/19 15:43:02 joerg 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 +.\" Casey Leedom of Lawrence Livermore National Laboratory. +.\" +.\" 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. +.\" +.\" @(#)getcap.3 8.4 (Berkeley) 5/13/94 +.\" +.Dd February 1, 2008 +.Dt CGETCAP 3 +.Os +.Sh NAME +.Nm cgetent , +.Nm cgetset , +.Nm cgetmatch , +.Nm cgetcap , +.Nm cgetnum , +.Nm cgetstr , +.Nm cgetustr , +.Nm cgetfirst , +.Nm cgetnext , +.Nm cgetclose , +.Nm cexpandtc +.Nd capability database access routines +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft int +.Fn cgetent "char **buf" "const char * const *db_array" "const char *name" +.Ft int +.Fn cgetset "const char *ent" +.Ft int +.Fn cgetmatch "const char *buf" "const char *name" +.Ft char * +.Fn cgetcap "char *buf" "const char *cap" "int type" +.Ft int +.Fn cgetnum "char *buf" "const char *cap" "long *num" +.Ft int +.Fn cgetstr "char *buf" "const char *cap" "char **str" +.Ft int +.Fn cgetustr "char *buf" "const char *cap" "char **str" +.Ft int +.Fn cgetfirst "char **buf" "const char * const *db_array" +.Ft int +.Fn cgetnext "char **buf" "const char * const *db_array" +.Ft int +.Fn cgetclose "void" +.Ft void +.Fn csetexpandtc "int expandtc" +.Sh DESCRIPTION +.Fn cgetent +extracts the capability +.Fa name +from the database specified by the +.Dv NULL +terminated file array +.Fa db_array +and returns a pointer to a +.Xr malloc 3 Ap d +copy of it in +.Fa buf . +.Fn cgetent +will first look for files ending in +.Pa .db +(see +.Xr cap_mkdb 1 ) +before accessing the +.Tn ASCII +file. +.Pp +.Fa buf +must be retained through all subsequent calls to +.Fn cgetmatch , +.Fn cgetcap , +.Fn cgetnum , +.Fn cgetstr , +and +.Fn cgetustr , +but may then be +.Xr free 3 Ap d . +.Pp +On success 0 is returned, 1 if the returned record contains an unresolved +.Qq tc +expansion, \-1 if the requested record couldn't be found, \-2 if +a system error was encountered (couldn't open/read a file, etc.) +also setting +.Va errno , +and \-3 if a potential reference loop is detected (see +.Qq tc=name +comments below). +.Pp +.Fn cgetset +enables the addition of a character buffer containing a single capability +record entry to the capability database. +Conceptually, the entry is added as the first +.Dq file +in the database, and +is therefore searched first on the call to +.Fn cgetent . +The entry is passed in +.Fa ent . +If +.Fa ent +is +.Dv NULL , +the current entry is removed from the database. +.Pp +.Fn cgetset +must precede the database traversal. +It must be called before the +.Fn cgetent +call. +If a sequential access is being performed (see below), it must be called +before the first sequential access call +.Po +.Fn cgetfirst +or +.Fn cgetnext +.Pc , +or be directly preceded by a +.Fn cgetclose +call. +On success 0 is returned and \-1 on failure. +.Pp +.Fn cgetmatch +will return 0 if +.Fa name +is one of the names of the capability record +.Fa buf , +\-1 if not. +.Pp +.Fn cgetcap +searches the capability record +.Fa buf +for the capability +.Fa cap +with type +.Fa type . +A +.Fa type +is specified using any single character. +If a colon +.Pq Sq \&: +is used, an untyped capability will be searched +for (see below for explanation of types). +A pointer to the value of +.Fa cap +in +.Fa buf +is returned on success, +.Dv NULL +if the requested capability couldn't be found. +The end of the capability value is signaled by a +.Sq \&: +or +.Tn ASCII +.Dv NUL +(see below for capability database syntax). +.Pp +.Fn cgetnum +retrieves the value of the numeric capability +.Fa cap +from the capability record pointed to by +.Fa buf . +The numeric value is returned in the +.Ft long +pointed to by +.Fa num . +0 is returned on success, +\-1 if the requested numeric capability couldn't be found. +.Pp +.Fn cgetstr +retrieves the value of the string capability +.Fa cap +from the capability record pointed to by +.Fa buf . +A pointer to a decoded, +.Dv NUL +terminated, +.Xr malloc 3 Ap d +copy of the string is returned in the +.Ft char * +pointed to by +.Fa str . +The number of characters in the decoded string not including the trailing +.Dv NUL +is returned on success, \-1 if the requested string capability couldn't +be found, \-2 if a system error was encountered (storage allocation +failure). +.Pp +.Fn cgetustr +is identical to +.Fn cgetstr +except that it does not expand special characters, but rather returns each +character of the capability string literally. +.Pp +.Fn cgetfirst , +.Fn cgetnext , +comprise a function group that provides for sequential access of the +.Dv NULL +pointer terminated array of file names, +.Fa db_array . +.Fn cgetfirst +returns the first record in the database and resets the access +to the first record. +.Fn cgetnext +returns the next record in the database with respect to the +record returned by the previous +.Fn cgetfirst +or +.Fn cgetnext +call. +If there is no such previous call, +the first record in the database is returned. +Each record is returned in a +.Xr malloc 3 Ap d +copy pointed to by +.Fa buf . +.Qq tc +expansion is done (see +.Qq tc=name +comments below). +.Pp +Upon completion of the database 0 is returned, 1 is returned upon successful +return of record with possibly more remaining (we haven't reached the end of +the database yet), 2 is returned if the record contains an unresolved +.Qq tc +expansion, \-1 is returned if an system error occurred, and \-2 +is returned if a potential reference loop is detected (see +.Qq tc=name +comments below). +Upon completion of database (0 return) the database is closed. +.Pp +.Fn cgetclose +closes the sequential access and frees any memory and file descriptors +being used. +Note that it does not erase the buffer pushed by a call to +.Fn cgetset . +.Sh CAPABILITY DATABASE SYNTAX +Capability databases are normally +.Tn ASCII +and may be edited with standard text editors. +Blank lines and lines beginning with a +.Sq # +are comments and are ignored. +Lines ending with a +.Sq \|\e +indicate that the next line is a continuation +of the current line; the +.Sq \|\e +and following newline are ignored. +Long lines are usually continued onto several physical +lines by ending each line except the last with a +.Sq \|\e . +.Pp +Capability databases consist of a series of records, one per logical line. +Each record contains a variable number of +.So \&: Sc Ns -separated +fields (capabilities). +Empty fields consisting entirely of white space +characters (spaces and tabs) are ignored. +.Pp +The first capability of each record specifies its names, separated by +.Sq | +characters. +These names are used to reference records in the database. +By convention, the last name is usually a comment and is not intended as +a lookup tag. +For example, the +.Em vt100 +record from the +.Pa termcap +database begins: +.Pp +.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:" +.Pp +giving four names that can be used to access the record. +.Pp +The remaining non-empty capabilities describe a set of (name, value) +bindings, consisting of a name optionally followed by a typed value: +.Bl -column "nameTvalue" +.It name Ta "typeless [boolean] capability" +.Em name No "is present [true]" +.It name Ns Em \&T Ns value Ta capability +.Pq Em name , \&T +has value +.Em value +.It name@ Ta "no capability" Em name No exists +.It name Ns Em T Ns \&@ Ta capability +.Pq Em name , T +does not exist +.El +.Pp +Names consist of one or more characters. +Names may contain any character except +.Sq \&: , +but it's usually best +to restrict them to the printable characters and avoid use of +graphics like +.Sq # , +.Sq = , +.Sq % , +.Sq @ , +etc. +.Pp +Types are single characters used to separate capability names from +their associated typed values. +Types may be any character except a +.Sq \&: . +Typically, graphics like +.Sq # , +.Sq = , +.Sq % , +etc. are used. +Values may be any number of characters and may contain any character except +.Sq \&: . +.Sh CAPABILITY DATABASE SEMANTICS +Capability records describe a set of (name, value) bindings. +Names may have multiple values bound to them. +Different values for a name are distinguished by their +.Fa types . +.Fn cgetcap +will return a pointer to a value of a name given the capability name and +the type of the value. +.Pp +The types +.Sq # +and +.Sq = +are conventionally used to denote numeric and +string typed values, but no restriction on those types is enforced. +The functions +.Fn cgetnum +and +.Fn cgetstr +can be used to implement the traditional syntax and semantics of +.Sq # +and +.Sq = . +Typeless capabilities are typically used to denote boolean objects with +presence or absence indicating truth and false values respectively. +This interpretation is conveniently represented by: +.Pp +.Dl "(getcap(buf, name, ':') != NULL)" +.Pp +A special capability, +.Qq tc=name , +is used to indicate that the record specified by +.Fa name +should be substituted for the +.Qq tc +capability. +.Qq tc +capabilities may interpolate records which also contain +.Qq tc +capabilities and more than one +.Qq tc +capability may be used in a record. +A +.Qq tc +expansion scope (i.e. where the argument is searched for) contains the +file in which the +.Qq tc +is declared and all subsequent files in the file array. +.Pp +.Fn csetexpandtc +can be used to control if +.Qq tc +expansion is performed or not. +.Pp +When a database is searched for a capability record, the first matching +record in the search is returned. +When a record is scanned for a capability, the first matching +capability is returned; the capability +.Qq :nameT@: +will hide any following definition of a value of type +.Em T +for +.Fa name ; +and the capability +.Qq :name@: +will prevent any following values of +.Fa name +from being seen. +.Pp +These features combined with +.Qq tc +capabilities can be used to generate variations of other databases and +records by either adding new capabilities, overriding definitions with new +definitions, or hiding following definitions via +.Sq @ +capabilities. +.Sh EXAMPLES +.Bd -unfilled -offset indent +example\||\|an example of binding multiple values to names:\e + :foo%bar:foo^blah:foo@:\e + :abc%xyz:abc^frap:abc$@:\e + :tc=more: +.Ed +.Pp +The capability +.Em foo +has two values bound to it +.Po +.Em bar +of type +.Sq % +and +.Em blah +of type +.Sq ^ +.Pc +and any other value bindings are hidden. +The capability +.Em abc +also has two values bound but only a value of type +.Sq $ +is prevented from +being defined in the capability record more. +.Pp +.Bd -unfilled -offset indent +file1: + new\||\|new_record\||\|a modification of "old":\e + :fript=bar:who-cares@:tc=old:blah:tc=extensions: +file2: + old\||\|old_record\||\|an old database record:\e + :fript=foo:who-cares:glork#200: +.Ed +.Pp +The records are extracted by calling +.Fn cgetent +with +.Fa file1 +preceding +.Fa file2 . +In the capability record +.Em new +in +.Fa file1 , +.Qq fript=bar +overrides the definition of +.Qq fript=foo +interpolated from the capability record +.Em old +in +.Fa file2 , +.Qq who-cares@ +prevents the definition of any who-cares definitions in +.Em old +from being seen, +.Qq glork#200 +is inherited from +.Em old , +and +.Em blah +and anything defined by the record extensions is added to those +definitions in +.Em old . +Note that the position of the +.Qq fript=bar +and +.Qq who-cares@ +definitions before +.Qq tc=old +is important here. +If they were after, the definitions in +.Em old +would take precedence. +.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS +Two types are predefined by +.Fn cgetnum +and +.Fn cgetstr : +.Bl -column "nameXnumber" +.It Em name Ns \&# Ns Em number Ta numeric +capability +.Em name +has value +.Em number +.It Em name Ns = Ns Em string Ta "string capability" +.Em name +has value +.Em string +.It Em name Ns \&#@ Ns Ta "the numeric capability" +.Em name +does not exist +.It Em name Ns \&=@ Ns Ta "the string capability" +.Em name +does not exist +.El +.Pp +Numeric capability values may be given in one of three numeric bases. +If the number starts with either +.Ql 0x +or +.Ql 0X +it is interpreted as a hexadecimal number (both upper and lower case a-f +may be used to denote the extended hexadecimal digits). +Otherwise, if the number starts with a +.Ql 0 +it is interpreted as an octal number. +Otherwise the number is interpreted as a decimal number. +.Pp +String capability values may contain any character. +Non-printable +.Dv ASCII +codes, new lines, and colons may be conveniently represented by the use +of escape sequences: +.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)" +.It ^X ('\fIX\fP' \*[Am] 037) control-\fIX\fP +.It \e\|b, \e\|B (ASCII 010) backspace +.It \e\|t, \e\|T (ASCII 011) tab +.It \e\|n, \e\|N (ASCII 012) line feed (newline) +.It \e\|f, \e\|F (ASCII 014) form feed +.It \e\|r, \e\|R (ASCII 015) carriage return +.It \e\|e, \e\|E (ASCII 027) escape +.It \e\|c, \e\|C (:) colon +.It \e\|\e (\e\|) back slash +.It \e\|^ (^) caret +.It \e\|\fInnn\fP (ASCII octal \fInnn\fP) +.El +.Pp +A +.Sq \|\e +followed by up to three octal digits directly specifies +the numeric code for a character. +The use of +.Tn ASCII +.Dv NUL Ns s , +while easily +encoded, causes all sorts of problems and must be used with care since +.Dv NUL Ns s +are typically used to denote the end of strings; many applications +use +.Sq \e\|200 +to represent a +.Dv NUL . +.Sh DIAGNOSTICS +.Fn cgetent , +.Fn cgetset , +.Fn cgetmatch , +.Fn cgetnum , +.Fn cgetstr , +.Fn cgetustr , +.Fn cgetfirst , +and +.Fn cgetnext +return a value greater than or equal to 0 on success and a value less +than 0 on failure. +.Fn cgetcap +returns a character pointer on success and a +.Dv NULL +on failure. +.Pp +.Fn cgetclose , +.Fn cgetent , +.Fn cgetfirst , +and +.Fn cgetnext +may fail and set +.Va errno +for any of the errors specified for the library functions: +.Xr fopen 3 , +.Xr fclose 3 , +.Xr open 2 , +and +.Xr close 2 . +.Pp +.Fn cgetent , +.Fn cgetset , +.Fn cgetstr , +and +.Fn cgetustr +may fail and set +.Va errno +as follows: +.Bl -tag -width Er +.It Bq Er ENOMEM +No memory to allocate. +.El +.Sh SEE ALSO +.Xr cap_mkdb 1 , +.Xr malloc 3 +.Sh BUGS +Colons +.Pq Sq \&: +can't be used in names, types, or values. +.Pp +There are no checks for +.Qq tc=name +loops in +.Fn cgetent . +.Pp +The buffer added to the database by a call to +.Fn cgetset +is not unique to the database but is rather prepended to any database used. diff --git a/lib/nbsd_libc/gen/clock.3 b/lib/nbsd_libc/gen/clock.3 new file mode 100644 index 000000000..678e7ac9d --- /dev/null +++ b/lib/nbsd_libc/gen/clock.3 @@ -0,0 +1,66 @@ +.\" $NetBSD: clock.3,v 1.10 2003/08/07 16:42:46 agc Exp $ +.\" +.\" 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. +.\" +.\" @(#)clock.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt CLOCK 3 +.Os +.Sh NAME +.Nm clock +.Nd determine processor time used +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In time.h +.Ft clock_t +.Fn clock void +.Sh DESCRIPTION +The +.Fn clock +function +determines the amount of processor time used since the invocation of the +calling process, measured in +.Dv CLOCKS_PER_SEC Ns s . +.Sh RETURN VALUES +The +.Fn clock +function returns the amount of time used unless an error occurs, in which +case the return value is \-1. +.Sh SEE ALSO +.Xr getrusage 2 +.Sh STANDARDS +The +.Fn clock +function conforms to +.St -ansiC . diff --git a/lib/nbsd_libc/gen/clock.c b/lib/nbsd_libc/gen/clock.c new file mode 100644 index 000000000..a50e9d430 --- /dev/null +++ b/lib/nbsd_libc/gen/clock.c @@ -0,0 +1,69 @@ +/* $NetBSD: clock.c,v 1.10 2009/01/11 02:46:27 christos Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)clock.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: clock.c,v 1.10 2009/01/11 02:46:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include + +/* + * This code is all rather silly because the kernel counts actual + * execution time (to usec accuracy) then splits it into user, system and + * interrupt based on when clock ticks happen. getrusage apportions the + * time based on the number of ticks, and here we are trying to generate + * a number which was, traditionally, the number of ticks! + * + * Due to the way the time is apportioned, this code (and indeed getrusage + * itself) are not guaranteed monotonic. + */ + +clock_t +clock(void) +{ + struct rusage ru; + clock_t hz = CLOCKS_PER_SEC; + + if (getrusage(RUSAGE_SELF, &ru)) + return ((clock_t) -1); + return (clock_t)((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) * hz + + (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec + 50) + / 100 * hz / 10000); +} diff --git a/lib/nbsd_libc/gen/closedir.c b/lib/nbsd_libc/gen/closedir.c new file mode 100644 index 000000000..432019792 --- /dev/null +++ b/lib/nbsd_libc/gen/closedir.c @@ -0,0 +1,85 @@ +/* $NetBSD: closedir.c,v 1.16 2010/09/26 02:26:59 yamt Exp $ */ + +/* + * Copyright (c) 1983, 1993 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)closedir.c 8.1 (Berkeley) 6/10/93"; +#else +__RCSID("$NetBSD: closedir.c,v 1.16 2010/09/26 02:26:59 yamt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include "extern.h" +#include + +#include +#include +#include +#include +#include + +#include "dirent_private.h" + +#ifdef __weak_alias +__weak_alias(closedir,_closedir) +#endif + +/* + * close a directory. + */ +int +closedir(DIR *dirp) +{ + int fd; + + _DIAGASSERT(dirp != NULL); + +#ifdef _REENTRANT + if (__isthreaded) + mutex_lock((mutex_t *)dirp->dd_lock); +#endif + fd = dirp->dd_fd; + dirp->dd_fd = -1; + _finidir(dirp); + +#ifdef _REENTRANT + if (__isthreaded) { + mutex_unlock((mutex_t *)dirp->dd_lock); + mutex_destroy((mutex_t *)dirp->dd_lock); + free(dirp->dd_lock); + } +#endif + free((void *)dirp); + return(close(fd)); +} diff --git a/lib/nbsd_libc/gen/closefrom.3 b/lib/nbsd_libc/gen/closefrom.3 new file mode 100644 index 000000000..82368a094 --- /dev/null +++ b/lib/nbsd_libc/gen/closefrom.3 @@ -0,0 +1,61 @@ +.\" $NetBSD: closefrom.3,v 1.5 2004/06/01 16:12:51 kleink Exp $ +.\" $OpenBSD: closefrom.2,v 1.2 2004/01/12 20:52:09 jmc Exp $ +.\" +.\" Copyright (c) 2004 Ted Unangst. 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 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. +.Dd June 1, 2004 +.Dt CLOSEFROM 3 +.Os +.Sh NAME +.Nm closefrom +.Nd delete many descriptors +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn closefrom "int fd" +.Sh DESCRIPTION +The +.Fn closefrom +call deletes all descriptors numbered +.Fa fd +and higher from the per-process file descriptor table. +It is effectively the same as calling +.Xr close 2 +on each descriptor. +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. +Otherwise, a value of \-1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn closefrom +will fail if: +.Bl -tag -width Er +.It Bq Er EBADF +.Fa fd +is invalid. +.It Bq Er EINTR +An interrupt was received. +.El +.Sh SEE ALSO +.Xr close 2 diff --git a/lib/nbsd_libc/gen/closefrom.c b/lib/nbsd_libc/gen/closefrom.c new file mode 100644 index 000000000..4d84c0997 --- /dev/null +++ b/lib/nbsd_libc/gen/closefrom.c @@ -0,0 +1,45 @@ +/* $NetBSD: closefrom.c,v 1.2 2004/06/22 13:49:43 atatat Exp $ */ + +/* + * Copyright (C) 2004 WIDE Project. + * 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 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 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 PROJECT 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: closefrom.c,v 1.2 2004/06/22 13:49:43 atatat Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +int +closefrom(int fd) +{ + + return (fcntl(fd, F_CLOSEM)); +} diff --git a/lib/nbsd_libc/gen/confstr.3 b/lib/nbsd_libc/gen/confstr.3 new file mode 100644 index 000000000..17c5a5e57 --- /dev/null +++ b/lib/nbsd_libc/gen/confstr.3 @@ -0,0 +1,134 @@ +.\" $NetBSD: confstr.3,v 1.20 2010/04/22 08:00:34 jruoho Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)confstr.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd April 22, 2010 +.Dt CONFSTR 3 +.Os +.Sh NAME +.Nm confstr +.Nd get string-valued configurable variables +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft size_t +.Fn confstr "int name" "char *buf" "size_t len" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by +.Xr sysctl 3 . +.Ef +.Pp +The +.Fn confstr +function provides a method for applications to get configuration +defined string values. +.Pp +The +.Fa name +argument specifies the system variable to be queried. +Symbolic constants for each name value are found in the +.In unistd.h +header. +The +.Fa len +argument specifies the size of the buffer referenced by the +argument +.Fa buf . +If +.Fa len +is non-zero, +.Fa buf +is a non-null pointer, and +.Fa name +has a value, up to +.Fa len +\- 1 bytes of the value are copied into the buffer +.Fa buf . +The copied value is always null terminated. +.Pp +The available values are as follows: +.Pp +.Bl -tag -width "123456" +.It Li _CS_PATH +Return a value for the +.Ev PATH +environment variable that finds all the standard utilities. +.El +.Sh RETURN VALUES +If the call to +.Nm confstr +is not successful, 0 is returned and +.Va errno +is set appropriately. +Otherwise, if the variable does not have a configuration defined value, +0 is returned and +.Va errno +is not modified. +Otherwise, the buffer size needed to hold the entire configuration-defined +value is returned. +If this size is greater than the argument +.Fa len , +the string in +.Fa buf +was truncated. +.Sh ERRORS +The +.Nm confstr +function may fail and set +.Va error +for any of the errors specified for the library functions +.Xr malloc 3 +and +.Xr sysctl 3 . +.Pp +In addition, the following errors may be reported: +.Bl -tag -width Er +.It Bq Er EINVAL +The value of the +.Fa name +argument is invalid. +.El +.Sh SEE ALSO +.Xr sysctl 3 +.Sh STANDARDS +The +.Nm confstr +function conforms to +.St -p1003.2-92 . +.Sh HISTORY +The +.Nm confstr +function first appeared in +.Bx 4.4 . +.Sh BUGS +The standards require us to return 0 both on errors, and when the value +is not set. diff --git a/lib/nbsd_libc/gen/confstr.c b/lib/nbsd_libc/gen/confstr.c new file mode 100644 index 000000000..4641e9463 --- /dev/null +++ b/lib/nbsd_libc/gen/confstr.c @@ -0,0 +1,98 @@ +/* $NetBSD: confstr.c,v 1.12 2006/12/03 00:39:19 christos Exp $ */ + +/*- + * Copyright (c) 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: confstr.c,v 1.12 2006/12/03 00:39:19 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(confstr,_confstr) +#endif + +size_t +confstr(name, buf, len) + int name; + char *buf; + size_t len; +{ + size_t tlen; + int mib[2], sverrno; + char *p; + + /* + * POSIX 1003.2 requires errors to return 0 -- + * that is *really* useful. + */ + switch (name) { + case _CS_PATH: + mib[0] = CTL_USER; + mib[1] = USER_CS_PATH; + if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1) + return 0; + if (len != 0 && buf != NULL) { + if ((p = malloc(tlen)) == NULL) + return 0; + if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) { + sverrno = errno; + free(p); + errno = sverrno; + return 0; + } + /* + * POSIX 1003.2 requires partial return of + * the string -- that is even more useful. + */ + (void)strncpy(buf, p, len - 1); + buf[len - 1] = '\0'; + free(p); + } + return tlen + 1; + default: + errno = EINVAL; + return 0; + } + /* NOTREACHED */ +} diff --git a/lib/nbsd_libc/gen/cpuset.3 b/lib/nbsd_libc/gen/cpuset.3 new file mode 100644 index 000000000..e27ef046b --- /dev/null +++ b/lib/nbsd_libc/gen/cpuset.3 @@ -0,0 +1,118 @@ +.\" $NetBSD: cpuset.3,v 1.3 2008/09/12 13:39:04 rmind Exp $ +.\" +.\" Copyright (c) 2008 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Mindaugas Rasiukevicius . +.\" +.\" 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 September 12, 2008 +.Dt CPUSET 3 +.Os +.Sh NAME +.Nm cpuset_create , +.Nm cpuset_destroy , +.Nm cpuset_zero , +.Nm cpuset_set , +.Nm cpuset_clr , +.Nm cpuset_isset , +.Nm cpuset_size +.Nd dynamic CPU sets +.Sh SYNOPSIS +.In sched.h +.Ft cpuset_t +.Fn cpuset_create "void" +.Ft void +.Fn cpuset_destroy "cpuset_t *set" +.Ft void +.Fn cpuset_zero "cpuset_t *set" +.Ft int +.Fn cpuset_set "cpuid_t cpu" "cpuset_t *set" +.Ft int +.Fn cpuset_clr "cpuid_t cpu" "cpuset_t *set" +.Ft int +.Fn cpuset_isset "cpuid_t cpu" "const cpuset_t *set" +.Ft size_t +.Fn cpuset_size "const cpuset_t *set" +.Sh DESCRIPTION +This section describes the functions used to create, set, use and destroy +the dynamic CPU sets. +.Pp +This API can be used with the POSIX threads, see +.Xr pthread 3 +and +.Xr affinity 3 . +.Pp +The ID of the primary CPU in the system is 0. +.Sh FUNCTIONS +.Bl -tag -width compact +.It Fn cpuset_create +Allocates and initializes a clean CPU-set. +Returns the pointer to the CPU-set, or +.Dv NULL +on failure. +.It Fn cpuset_destroy set +Destroy the CPU-set specified by +.Fa set . +.It Fn cpuset_zero set +Makes the CPU-set specified by +.Fa set +clean, that is, memory is initialized to zero bytes, and none of +the CPUs set. +.It Fn cpuset_set cpu set +Sets the CPU specified by +.Fa cpu +in +.Fa set . +Returns zero on success, and \-1 if +.Fa cpu +is invalid. +.It Fn cpuset_clr cpu set +Clears the CPU specified by +.Fa cpu +in the CPU-set +.Fa set . +Returns zero on success, and \-1 if +.Fa cpu +is invalid. +.It Fn cpuset_isset cpu set +Checks if CPU specified by +.Fa cpu +is set in the CPU-set +.Fa set . +Returns the positive number if set, zero if not set, and \-1 if +.Fa cpu +is invalid. +.It Fn cpuset_size set +Returns the size in bytes of CPU-set specified by +.Fa set . +.El +.Sh SEE ALSO +.Xr affinity 3 , +.Xr pset 3 , +.Xr sched 3 , +.Xr schedctl 8 +.Sh HISTORY +The dynamic CPU sets appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/gen/ctermid.3 b/lib/nbsd_libc/gen/ctermid.3 new file mode 100644 index 000000000..b167392b2 --- /dev/null +++ b/lib/nbsd_libc/gen/ctermid.3 @@ -0,0 +1,90 @@ +.\" $NetBSD: ctermid.3,v 1.11 2010/03/22 19:30:53 joerg 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. +.\" +.\" @(#)ctermid.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt CTERMID 3 +.Os +.Sh NAME +.Nm ctermid +.Nd generate terminal pathname +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdio.h +.Ft char * +.Fn ctermid "char *buf" +.Sh DESCRIPTION +The +.Fn ctermid +function generates a string, that, when used as a pathname, refers to +the current controlling terminal of the calling process. +.Pp +If +.Ar buf +is the +.Dv NULL +pointer, a pointer to a static area is returned. +Otherwise, the pathname is copied into the memory referenced by +.Ar buf . +The argument +.Ar buf +is assumed to point to an array at least +.Dv L_ctermid +bytes long (as defined in the include file +.In stdio.h ) . +.Pp +The current implementation simply returns +.Ql /dev/tty . +.Sh RETURN VALUES +Upon successful completion, a +.Pf non- Dv NULL +pointer is returned. +Otherwise, a +.Dv NULL +pointer is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +The current implementation detects no error conditions. +.Sh SEE ALSO +.Xr ttyname 3 +.Sh STANDARDS +The +.Fn ctermid +function conforms to +.St -p1003.1-90 . +.Sh BUGS +By default the +.Fn ctermid +function writes all information to an internal static object. +Subsequent calls to +.Fn ctermid +will modify the same object. diff --git a/lib/nbsd_libc/gen/ctermid.c b/lib/nbsd_libc/gen/ctermid.c new file mode 100644 index 000000000..f0db511d6 --- /dev/null +++ b/lib/nbsd_libc/gen/ctermid.c @@ -0,0 +1,61 @@ +/* $NetBSD: ctermid.c,v 1.10 2003/08/07 16:42:46 agc 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ctermid.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: ctermid.c,v 1.10 2003/08/07 16:42:46 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(ctermid,_ctermid) +#endif + +char * +ctermid(s) + char *s; +{ + static char def[] = _PATH_TTY; + + if (s) { + (void)strncpy(s, def, L_ctermid); + return (s); + } + return (def); +} diff --git a/lib/nbsd_libc/gen/ctype.3 b/lib/nbsd_libc/gen/ctype.3 new file mode 100644 index 000000000..dbe8d96ee --- /dev/null +++ b/lib/nbsd_libc/gen/ctype.3 @@ -0,0 +1,154 @@ +.\" $NetBSD: ctype.3,v 1.21 2010/05/06 11:46:11 jruoho Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)ctype.3 6.5 (Berkeley) 4/19/91 +.\" +.Dd May 6, 2010 +.Dt CTYPE 3 +.Os +.Sh NAME +.Nm isalpha , +.Nm isupper , +.Nm islower , +.Nm isdigit , +.Nm isxdigit , +.Nm isalnum , +.Nm isspace , +.Nm ispunct , +.Nm isprint , +.Nm isgraph , +.Nm iscntrl , +.Nm isblank , +.Nm toupper , +.Nm tolower , +.Nd character classification and mapping functions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Fn isalpha "int c" +.Fn isupper "int c" +.Fn islower "int c" +.Fn isdigit "int c" +.Fn isxdigit "int c" +.Fn isalnum "int c" +.Fn isspace "int c" +.Fn ispunct "int c" +.Fn isprint "int c" +.Fn isgraph "int c" +.Fn iscntrl "int c" +.Fn isblank "int c" +.Fn toupper "int c" +.Fn tolower "int c" +.Sh DESCRIPTION +The above functions perform character tests and conversions on the integer +.Ar c . +.Pp +See the specific manual pages for information about the +test or conversion performed by each function. +.Sh EXAMPLES +To print an upper-case version of a string to stdout, +the following code can be used: +.Bd -literal -offset indent +const char *s = "xyz"; + +while (*s != '\e0') { + putchar(toupper((int)(unsigned char)*s)); + s++; +} +.Ed +.Sh SEE ALSO +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +These functions, with the exception of +.Fn isblank , +conform to +.St -ansiC . +All described functions, including +.Fn isblank , +also conform to +.St -p1003.1-2001 . +.Sh CAVEATS +The first argument of these functions is of type +.Vt int , +but only a very restricted subset of values are actually valid. +The argument must either be the value of the macro +.Dv EOF +(which has a negative value), +or must be a non-negative value within the range representable as +.Vt unsigned char . +Passing invalid values leads to undefined behavior. +.Pp +Values of type +.Vt int +that were returned by +.Xr getc 3 , +.Xr fgetc 3 , +and similar functions or macros +are already in the correct range, and may be safely passed to these +.Nm ctype +functions without any casts. +.Pp +Values of type +.Vt char +or +.Vt signed char +must first be cast to +.Vt unsigned char , +to ensure that the values are within the correct range. +The result should then be cast to +.Vt int +to avoid warnings from some compilers. +Casting a negative-valued +.Vt char +or +.Vt signed char +directly to +.Vt int +will produce a negative-valued +.Vt int , +which will be outside the range of allowed values +(unless it happens to be equal to +.Dv EOF , +but even that would not give the desired result). diff --git a/lib/nbsd_libc/gen/ctype_.c b/lib/nbsd_libc/gen/ctype_.c new file mode 100644 index 000000000..931cfb492 --- /dev/null +++ b/lib/nbsd_libc/gen/ctype_.c @@ -0,0 +1,83 @@ +/* $NetBSD: ctype_.c,v 1.19 2010/12/14 02:28:57 joerg Exp $ */ + +/* + * Copyright (c) 1989 The Regents of the University of California. + * All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +/*static char *sccsid = "from: @(#)ctype_.c 5.6 (Berkeley) 6/1/90";*/ +#else +__RCSID("$NetBSD: ctype_.c,v 1.19 2010/12/14 02:28:57 joerg Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include "ctype_local.h" + +#if EOF != -1 +#error "EOF != -1" +#endif + +#define _U _CTYPE_U +#define _L _CTYPE_L +#define _N _CTYPE_N +#define _S _CTYPE_S +#define _P _CTYPE_P +#define _C _CTYPE_C +#define _X _CTYPE_X +#define _B _CTYPE_B + +const unsigned char _C_ctype_[1 + _CTYPE_NUM_CHARS] = { + 0, + _C, _C, _C, _C, _C, _C, _C, _C, + _C, _C|_S, _C|_S, _C|_S, _C|_S, _C|_S, _C, _C, + _C, _C, _C, _C, _C, _C, _C, _C, + _C, _C, _C, _C, _C, _C, _C, _C, + _S|_B, _P, _P, _P, _P, _P, _P, _P, + _P, _P, _P, _P, _P, _P, _P, _P, + _N, _N, _N, _N, _N, _N, _N, _N, + _N, _N, _P, _P, _P, _P, _P, _P, + _P, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U, + _U, _U, _U, _U, _U, _U, _U, _U, + _U, _U, _U, _U, _U, _U, _U, _U, + _U, _U, _U, _P, _P, _P, _P, _P, + _P, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L, + _L, _L, _L, _L, _L, _L, _L, _L, + _L, _L, _L, _L, _L, _L, _L, _L, + _L, _L, _L, _P, _P, _P, _P, _C +}; + +const unsigned char *_ctype_ = &_C_ctype_[0]; diff --git a/lib/nbsd_libc/gen/daemon.3 b/lib/nbsd_libc/gen/daemon.3 new file mode 100644 index 000000000..47f3f5790 --- /dev/null +++ b/lib/nbsd_libc/gen/daemon.3 @@ -0,0 +1,114 @@ +.\" $NetBSD: daemon.3,v 1.15 2003/08/07 16:42:46 agc Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)daemon.3 8.1 (Berkeley) 6/9/93 +.\" +.Dd September 3, 1999 +.Dt DAEMON 3 +.Os +.Sh NAME +.Nm daemon +.Nd run in the background +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft int +.Fn daemon "int nochdir" "int noclose" +.Sh DESCRIPTION +The +.Fn daemon +function is for programs wishing to detach themselves from the +controlling terminal and run in the background as system daemons. +.Pp +Unless the argument +.Fa nochdir +is non-zero, +.Fn daemon +changes the current working directory to the root +.Pq Pa / . +.Pp +Unless the argument +.Fa noclose +is non-zero, +.Fn daemon +will redirect standard input, standard output and standard error +to +.Pa /dev/null . +.Sh RETURN VALUES +On return 0 indicates success with \-1 indicating error. +.Sh ERRORS +The function +.Fn daemon +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr fork 2 +and +.Xr setsid 2 . +.Sh SEE ALSO +.Xr fork 2 , +.Xr setsid 2 +.Sh HISTORY +The +.Fn daemon +function first appeared in +.Bx 4.4 . +.Sh CAVEATS +Unless the +.Ar noclose +argument is non-zero, +.Fn daemon +will close the first three file descriptors and redirect them to +.Pa /dev/null . +Normally, these correspond to standard input, standard output and +standard error. +However, if any of those file descriptors refer to something else they +will still be closed, resulting in incorrect behavior of the calling program. +This can happen if any of standard input, standard output or standard error +have been closed before the program was run. +Programs using +.Fn daemon +should therefore make sure to either call +.Fn daemon +before opening any files or sockets or, alternately, verifying that +any file descriptors obtained have a value greater than 2. +.Sh BUGS +.Fn daemon +uses +.Fn fork +as part of its tty detachment mechanism. +Consequently the process id changes when +.Fn daemon +is invoked. +Processes employing +.Fn daemon +can not be reliably waited upon until +.Fn daemon +has been invoked. diff --git a/lib/nbsd_libc/gen/daemon.c b/lib/nbsd_libc/gen/daemon.c new file mode 100644 index 000000000..032149dc3 --- /dev/null +++ b/lib/nbsd_libc/gen/daemon.c @@ -0,0 +1,80 @@ +/* $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)daemon.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(daemon,_daemon) +#endif + +int +daemon(nochdir, noclose) + int nochdir, noclose; +{ + int fd; + + switch (fork()) { + case -1: + return (-1); + case 0: + break; + default: + _exit(0); + } + + if (setsid() == -1) + return (-1); + + if (!nochdir) + (void)chdir("/"); + + if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { + (void)dup2(fd, STDIN_FILENO); + (void)dup2(fd, STDOUT_FILENO); + (void)dup2(fd, STDERR_FILENO); + if (fd > STDERR_FILENO) + (void)close(fd); + } + return (0); +} diff --git a/lib/nbsd_libc/gen/dehumanize_number.c b/lib/nbsd_libc/gen/dehumanize_number.c new file mode 100644 index 000000000..89afead87 --- /dev/null +++ b/lib/nbsd_libc/gen/dehumanize_number.c @@ -0,0 +1,115 @@ +/* $NetBSD: dehumanize_number.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ + +/* + * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Julio M. Merino Vidal, developed as part of Google's Summer of Code + * 2005 program. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: dehumanize_number.c,v 1.3 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +/* + * Converts the number given in 'str', which may be given in a humanized + * form (as described in humanize_number(3), but with some limitations), + * to an int64_t without units. + * In case of success, 0 is returned and *size holds the value. + * Otherwise, -1 is returned and *size is untouched. + * + * TODO: Internationalization, SI units. + */ +int +dehumanize_number(const char *str, int64_t *size) +{ + char *ep, unit; + const char *delimit; + long multiplier; + long long tmp, tmp2; + size_t len; + + len = strlen(str); + if (len == 0) { + errno = EINVAL; + return -1; + } + + multiplier = 1; + + unit = str[len - 1]; + if (isalpha((unsigned char)unit)) { + switch (tolower((unsigned char)unit)) { + case 'b': + multiplier = 1; + break; + + case 'k': + multiplier = 1024; + break; + + case 'm': + multiplier = 1024 * 1024; + break; + + case 'g': + multiplier = 1024 * 1024 * 1024; + break; + + default: + errno = EINVAL; + return -1; /* Invalid suffix. */ + } + + delimit = &str[len - 1]; + } else + delimit = NULL; + + errno = 0; + tmp = strtoll(str, &ep, 10); + if (str[0] == '\0' || (ep != delimit && *ep != '\0')) + return -1; /* Not a number. */ + else if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN)) + return -1; /* Out of range. */ + + tmp2 = tmp * multiplier; + tmp2 = tmp2 / multiplier; + if (tmp != tmp2) { + errno = ERANGE; + return -1; /* Out of range. */ + } + *size = tmp * multiplier; + + return 0; +} diff --git a/lib/nbsd_libc/gen/devname.3 b/lib/nbsd_libc/gen/devname.3 new file mode 100644 index 000000000..1c00fc5b5 --- /dev/null +++ b/lib/nbsd_libc/gen/devname.3 @@ -0,0 +1,81 @@ +.\" $NetBSD: devname.3,v 1.11 2003/08/07 16:42:46 agc Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)devname.3 8.2 (Berkeley) 4/29/95 +.\" +.Dd April 29, 1995 +.Dt DEVNAME 3 +.Os +.Sh NAME +.Nm devname +.Nd get device name +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.In sys/stat.h +.Ft char * +.Fn devname "dev_t dev" "mode_t type" +.Sh DESCRIPTION +The +.Fn devname +function returns a pointer to the name of the block or character +device in +.Dq Pa /dev +with a device number of +.Fa dev , +and a file type matching the one encoded in +.Fa type +which must be one of S_IFBLK or S_IFCHR. +The device name is cached so that multiple calls with the same +.Fa dev +and +.Fa type +do not require additional queries of the device database file. +If no device matches the specified values, or no information is +available, +.Dv NULL +is returned. +.Pp +The traditional display for applications when no device is +found is the string +.Dq ?? . +.Sh FILES +.Bl -tag -width /var/run/dev.db -compact +.It Pa /var/run/dev.db +Device database file. +.El +.Sh SEE ALSO +.Xr stat 2 , +.Xr dev_mkdb 8 +.Sh HISTORY +The +.Nm devname +function call appeared in +.Bx 4.4 . diff --git a/lib/nbsd_libc/gen/devname.c b/lib/nbsd_libc/gen/devname.c new file mode 100644 index 000000000..e11d11f06 --- /dev/null +++ b/lib/nbsd_libc/gen/devname.c @@ -0,0 +1,198 @@ +/* $NetBSD: devname.c,v 1.21 2010/03/23 20:28:59 drochner Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Simon Burge. + * + * 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. + */ + +/*- + * Copyright (c) 1992 Keith Muller. + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Keith Muller of the University of California, San Diego. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)devname.c 8.2 (Berkeley) 4/29/95"; +#else +__RCSID("$NetBSD: devname.c,v 1.21 2010/03/23 20:28:59 drochner Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define DEV_SZ 317 /* show be prime for best results */ +#define VALID 1 /* entry and devname are valid */ +#define INVALID 2 /* entry valid, devname NOT valid */ + +typedef struct devc { + int valid; /* entry valid? */ + dev_t dev; /* cached device */ + mode_t type; /* cached file type */ + char name[NAME_MAX]; /* device name */ +} DEVC; + +char * +devname(dev, type) + dev_t dev; + mode_t type; +{ + struct { + mode_t type; + dev_t dev; + } bkey; + struct { + mode_t type; + int32_t dev; + } obkey; + static DB *db; + static int failure; + DBT data, key; + DEVC *ptr, **pptr; + static DEVC **devtb = NULL; + static devmajor_t pts; + static int pts_valid = 0; + + if (!db && !failure && + !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) { + warn("warning: %s", _PATH_DEVDB); + failure = 1; + } + /* initialise dev cache */ + if (!failure && devtb == NULL) { + devtb = calloc(DEV_SZ, sizeof(DEVC *)); + if (devtb == NULL) + failure= 1; + } + if (failure) + return (NULL); + + /* see if we have this dev/type cached */ + pptr = devtb + (size_t)((dev + type) % DEV_SZ); + ptr = *pptr; + + if (ptr && ptr->valid > 0 && ptr->dev == dev && ptr->type == type) { + if (ptr->valid == VALID) + return (ptr->name); + return (NULL); + } + + if (ptr == NULL) + *pptr = ptr = malloc(sizeof(DEVC)); + + /* + * Keys are a mode_t followed by a dev_t. The former is the type of + * the file (mode & S_IFMT), the latter is the st_rdev field. Be + * sure to clear any padding that may be found in bkey. + */ + (void)memset(&bkey, 0, sizeof(bkey)); + bkey.dev = dev; + bkey.type = type; + key.data = &bkey; + key.size = sizeof(bkey); + if ((db->get)(db, &key, &data, 0) == 0) { +found_it: + if (ptr == NULL) + return (char *)data.data; + ptr->dev = dev; + ptr->type = type; + strncpy(ptr->name, (char *)data.data, NAME_MAX); + ptr->name[NAME_MAX - 1] = '\0'; + ptr->valid = VALID; + } else { + /* Look for a 32 bit dev_t. */ + (void)memset(&obkey, 0, sizeof(obkey)); + obkey.dev = (int32_t)(uint32_t)dev; + obkey.type = type; + key.data = &obkey; + key.size = sizeof(obkey); + if ((db->get)(db, &key, &data, 0) == 0) + goto found_it; + + if (ptr == NULL) + return (NULL); + ptr->valid = INVALID; + if (type == S_IFCHR) { + if (!pts_valid) { + pts = getdevmajor("pts", S_IFCHR); + pts_valid = 1; + } + if (pts != NODEVMAJOR && major(dev) == pts) { + (void)snprintf(ptr->name, sizeof(ptr->name), + "%s%d", _PATH_DEV_PTS + + sizeof(_PATH_DEV) - 1, + minor(dev)); + ptr->valid = VALID; + } + } + ptr->dev = dev; + ptr->type = type; + } + if (ptr->valid == VALID) + return (ptr->name); + else + return (NULL); +} diff --git a/lib/nbsd_libc/gen/directory.3 b/lib/nbsd_libc/gen/directory.3 new file mode 100644 index 000000000..432cf60ae --- /dev/null +++ b/lib/nbsd_libc/gen/directory.3 @@ -0,0 +1,395 @@ +.\" $NetBSD: directory.3,v 1.36 2010/12/17 19:20:42 njoly Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)directory.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 2010 +.Dt DIRECTORY 3 +.Os +.Sh NAME +.Nm fdopendir , +.Nm opendir , +.Nm readdir , +.Nm readdir_r , +.Nm telldir , +.Nm seekdir , +.Nm rewinddir , +.Nm closedir , +.Nm dirfd +.Nd directory operations +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In dirent.h +.Ft DIR * +.Fn opendir "const char *filename" +.Ft DIR * +.Fn fdopendir "int fd" +.Ft struct dirent * +.Fn readdir "DIR *dirp" +.Ft int +.Fn readdir_r "DIR * restrict dirp" "struct dirent * restrict entry" "struct dirent ** restrict result" +.Ft long +.Fn telldir "DIR *dirp" +.Ft void +.Fn seekdir "DIR *dirp" "long loc" +.Ft void +.Fn rewinddir "DIR *dirp" +.Ft int +.Fn closedir "DIR *dirp" +.Ft int +.Fn dirfd "DIR *dirp" +.Sh DESCRIPTION +The type +.Vt DIR +represents a directory stream; +an ordered sequence of all directory entries in a particular directory. +The purpose of the +.Vt DIR +structure is similar to that of the +.Vt FILE +structure maintained by the +.Xr stdio 3 +library functions. +.Sh FUNCTIONS +The following standard directory operations are defined. +.Bl -tag -width XXX +.It Fn opendir "filename" +The +.Fn opendir +function opens the directory named by +.Fa filename +and associates a directory stream with it. +The directory stream is positioned at the first entry. +Upon successful completion, a pointer to +.Vt DIR +type is returned. +Otherwise, +.Fn opendir +returns +.Dv NULL . +.It Fn fdopendir "fd" +The +.Fn fdopendir +function associates a directory stream with the directory file descriptor +.Fa fd . +The file offset associated with +.Fa fd +at the time of the call determines which entries are returned. +.Pp +Upon failure, +.Fn fdopendir +returns +.Dv NULL . +Otherwise the file descriptor is under the control of the system, +and if any attempt is made to close the file descriptor, +or to modify the state of the associated description, +other than by means of +.Fn closedir , +.Fn readdir , +.Fn readdir_r , +.Fn rewinddir , +the behavior is undefined. +The file descriptor can be closed by calling +.Fn closedir . +.It Fn readdir "dirp" +The +.Fn readdir +function returns a pointer to the directory entry at the current position +in the directory stream specified by +.Fa dirp , +and positions the directory stream at the next entry. +It returns +.Dv NULL +upon reaching the end of the directory or detecting an invalid +.Fn seekdir +operation. +The returned structure is described in +.Xr dirent 3 . +.Pp +The returned pointer to the +.Em dirent +structure points to data which may be overwritten by another call to +.Fn readdir +on the same directory stream. +This data is not however overwritten by another call to +.Fn readdir +on a different directory stream. +.It Fn readdir_r "dirp" "entry" "result" +The +.Fn readdir_r +function +provides the same functionality as +.Fn readdir , +but the caller must provide a directory +.Fa entry +buffer to store the results in. +If the read succeeds, +.Fa result +is pointed at the +.Fa entry ; +upon reaching the end of the directory +.Fa result +is set to +.Dv NULL . +The +.Fn readdir_r +function +returns 0 on success or an error number to indicate failure. +.Pp +Like +.Fn readdir , +the +.Fn readdir_r +function may buffer several directory entries per actual read operation. +Both functions mark for update the +.Em st_atime +field (see +.Xr stat 2 ) +of the directory each time the directory is actually read. +.It Fn telldir "dirp" +The +.Fn telldir +function returns the current location associated +with the directory stream specified by +.Fa dirp . +.Pp +If the most recent operation on the particular directory stream was a +.Fn seekdir , +the directory position returned from +.Fn telldir +is the same as +.Fa loc +supplied as an argument to the +.Fn seekdir +call. +.It Fn seekdir "dirp" "loc" +The +.Fn seekdir +function sets the position of the next +.Fn readdir +operation on the directory stream specified by +.Fa dirp . +The value of +.Fa loc +should come from a previous call to +.Fn telldir +using the same directory stream. +.Pp +The new position reverts to the one associated +with the directory stream when the +.Fn telldir +operation was performed. +Values returned by +.Fn telldir +are good only for the lifetime of the +.Vt DIR +pointer, +.Fa dirp , +from which they are derived. +If the directory is closed and then reopened, the +.Fn telldir +value cannot be re-used. +.It Fn rewinddir "dirp" +The +.Fn rewinddir +function resets the position of the named directory +stream to the beginning of the directory. +It also causes the directory stream to refer to the +current state of the corresponding directory, as if a call to +.Fn opendir +would have been made. +.Pp +If +.Fa dirp +does not refer to a valid directory stream, the behavior is undefined. +.It Fn closedir "dirp" +The +.Fn closedir +function closes the directory stream +and frees the structure associated with the +.Fa dirp +pointer, +returning 0 on success and \-1 on failure. +.It Fn dirfd "dirp" +The +.Fn dirfd +function returns the integer file descriptor +associated with the directory stream specified by +.Fa dirp . +Upon failure, +.Fn dirfd +returns \-1. +The returned file descriptor should be closed by +.Fn closedir +instead of +.Xr close 2 . +.Pp +The rationale of +.Fn dirfd +is to provide a mechanism by which a file descriptor +can be obtained for the use of the +.Xr fchdir 2 +function. +.El +.Sh EXAMPLES +Sample code which searches a directory for entry +.Dq name +is: +.Bd -literal -offset indent +len = strlen(name); +dirp = opendir("."); +if (dirp != NULL) { + while ((dp = readdir(dirp)) != NULL) + if (dp-\*[Gt]d_namlen == len \*[Am]\*[Am] + !strcmp(dp-\*[Gt]d_name, name)) { + (void)closedir(dirp); + return (FOUND); + } + (void)closedir(dirp); +} +return (NOT_FOUND); +.Ed +.Sh COMPATIBILITY +The described directory operations have traditionally been problematic +in terms of portability. +A good example is the semantics around +.Sq \&. +(dot) and +.Sq \&.. +(dot-dot). +Based on historical implementations, +the rules about file descriptors apply to directory streams as well. +The +.St -p1003.1-2008 +standard does not however any more mandate that directory streams +are necessarily implemented by using file descriptors. +.Pp +The following additional remarks can be noted from the +.St -p1003.1-2008 +standard. +.Bl -bullet -offset 2n +.It +If the type +.Vt DIR +is implemented using a file descriptor, +like in +.Nx , +applications should be able to open only +.Dv OPEN_MAX +files and directories. +Otherwise the limit is left as unspecified. +.It +When a file descriptor is used to implement the directory stream, the +.Fn closedir +function behaves as if the +.Dv FD_CLOEXEC +had been set for the file descriptor. +In another words, it is mandatory that +.Fn closedir +deallocates the file descriptor. +.It +If directory streams are not implemented by using file descriptors, +functions such as +.Fn dirfd +may fail with +.Er ENOTSUP . +.It +If a file is removed from or added to the directory +after the most recent call to +.Fn opendir +or +.Fn rewinddir , +it is unspecified whether a subsequent call to +.Fn readdir +returns an entry for that file. +.It +When using the function +.Fn seekdir , +note that if the value of +.Fa loc +was not obtained from an earlier call to +.Fn telldir , +or if a call to +.Fn rewinddir +occurred between the calls to +.Fn telldir +and +.Fn seekdir , +any subsequent call to +.Fn readdir +is unspecified, possibly resulting undefined behavior. +.It +After a call to +.Xr fork 2 , +either the parent or child (but not both) can continue processing the +directory stream using +.Fn readdir , +.Fn rewinddir , +or +.Fn seekdir . +However, if both the parent and child processes use these functions, +the result is undefined. +.El +.Sh ERRORS +.\" +.\" XXX: The errors should be enumerated. +.\" +All described functions may set +.Vt errno +to indicate the error. +.Sh SEE ALSO +.Xr close 2 , +.Xr lseek 2 , +.Xr open 2 , +.Xr read 2 , +.Xr dirent 3 +.Sh STANDARDS +The +.Fn opendir , +.Fn readdir , +.Fn rewinddir +and +.Fn closedir +functions conform to +.St -p1003.1-90 . +The other functions conform to +.St -p1003.1-2008 . +.Sh HISTORY +The +.Fn opendir , +.Fn readdir , +.Fn telldir , +.Fn seekdir , +.Fn rewinddir , +.Fn closedir , +and +.Fn dirfd +functions appeared in +.Bx 4.2 . diff --git a/lib/nbsd_libc/gen/dirent_private.h b/lib/nbsd_libc/gen/dirent_private.h new file mode 100644 index 000000000..77d2d56fc --- /dev/null +++ b/lib/nbsd_libc/gen/dirent_private.h @@ -0,0 +1,24 @@ +/* $NetBSD: dirent_private.h,v 1.4 2010/09/26 02:26:59 yamt Exp $ */ + +/* + * One struct _dirpos is malloced to describe the current directory + * position each time telldir is called. It records the current magic + * cookie returned by getdents and the offset within the buffer associated + * with that return value. + */ +struct dirpos { + struct dirpos *dp_next; /* next structure in list */ + off_t dp_seek; /* magic cookie returned by getdents */ + long dp_loc; /* offset of entry in buffer */ +}; + +struct _dirdesc; +void _seekdir_unlocked(struct _dirdesc *, long); +long _telldir_unlocked(struct _dirdesc *); +int _initdir(DIR *, int, const char *); +void _finidir(DIR *); +#ifndef __LIBC12_SOURCE__ +struct dirent; +struct dirent *_readdir_unlocked(struct _dirdesc *, int) + __RENAME(___readdir_unlocked50); +#endif diff --git a/lib/nbsd_libc/gen/dirname.3 b/lib/nbsd_libc/gen/dirname.3 new file mode 100644 index 000000000..216631833 --- /dev/null +++ b/lib/nbsd_libc/gen/dirname.3 @@ -0,0 +1,98 @@ +.\" $NetBSD: dirname.3,v 1.14 2008/05/10 22:39:40 christos Exp $ +.\" +.\" Copyright (c) 1997, 2002 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Klaus Klein and Jason R. Thorpe. +.\" +.\" 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 May 10, 2008 +.Dt DIRNAME 3 +.Os +.Sh NAME +.Nm dirname +.Nd report the parent directory name of a file pathname +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In libgen.h +.Ft char * +.Fn dirname "char *path" +.Sh DESCRIPTION +The +.Fn dirname +function takes a pointer to a character string that contains a pathname, +.Ar path , +and returns a pointer to a string that is a pathname of the parent directory of +.Ar path . +Trailing +.Sq / +characters in +.Ar path +are not counted as part of the path. +.Pp +If +.Ar path +does not contain a +.Sq / , +then +.Fn dirname +returns a pointer to the string +.Dq \&. . +.Pp +If +.Ar path +is a null pointer or points to an empty string, +.Fn dirname +returns a pointer to the string +.Dq \&. . +.Sh RETURN VALUES +The +.Fn dirname +function returns a pointer to a string that is the parent directory of +.Ar path . +.Sh SEE ALSO +.Xr dirname 1 , +.Xr basename 3 +.Sh STANDARDS +.Bl -bullet -compact +.It +.St -xpg4.2 +.It +.St -p1003.1-2001 +.El +.Sh BUGS +If the length of the result is longer than +.Dv PATH_MAX +bytes +.Pq including the terminating nul , +the result will be truncated. +.Pp +The +.Fn dirname +function returns a pointer to static storage that may be overwritten +by subsequent calls to +.Fn dirname . +This is not strictly a bug; it is explicitly allowed by +.St -p1003.1-2001 . diff --git a/lib/nbsd_libc/gen/dirname.c b/lib/nbsd_libc/gen/dirname.c new file mode 100644 index 000000000..ef2cb04f2 --- /dev/null +++ b/lib/nbsd_libc/gen/dirname.c @@ -0,0 +1,93 @@ +/* $NetBSD: dirname.c,v 1.11 2009/11/24 13:34:20 tnozaki Exp $ */ + +/*- + * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Klaus Klein and Jason R. Thorpe. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: dirname.c,v 1.11 2009/11/24 13:34:20 tnozaki Exp $"); +#endif /* !LIBC_SCCS && !lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(dirname,_dirname) +#endif + +#if !HAVE_DIRNAME +char * +dirname(char *path) +{ + static char result[PATH_MAX]; + const char *lastp; + size_t len; + + /* + * If `path' is a null pointer or points to an empty string, + * return a pointer to the string ".". + */ + if ((path == NULL) || (*path == '\0')) + goto singledot; + + + /* Strip trailing slashes, if any. */ + lastp = path + strlen(path) - 1; + while (lastp != path && *lastp == '/') + lastp--; + + /* Terminate path at the last occurence of '/'. */ + do { + if (*lastp == '/') { + /* Strip trailing slashes, if any. */ + while (lastp != path && *lastp == '/') + lastp--; + + /* ...and copy the result into the result buffer. */ + len = (lastp - path) + 1 /* last char */; + if (len > (PATH_MAX - 1)) + len = PATH_MAX - 1; + + memcpy(result, path, len); + result[len] = '\0'; + + return (result); + } + } while (--lastp >= path); + + /* No /'s found, return a pointer to the string ".". */ +singledot: + result[0] = '.'; + result[1] = '\0'; + + return (result); +} +#endif diff --git a/lib/nbsd_libc/gen/disklabel.c b/lib/nbsd_libc/gen/disklabel.c new file mode 100644 index 000000000..94a118443 --- /dev/null +++ b/lib/nbsd_libc/gen/disklabel.c @@ -0,0 +1,242 @@ +/* $NetBSD: disklabel.c,v 1.34 2006/03/19 02:17:16 christos Exp $ */ + +/* + * Copyright (c) 1983, 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; +#else +__RCSID("$NetBSD: disklabel.c,v 1.34 2006/03/19 02:17:16 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#define DKTYPENAMES +#define FSTYPENAMES +#include +#include + +#if HAVE_NBTOOL_CONFIG_H +#include +#include +#else +#include +#include +#endif /* HAVE_NBTOOL_CONFIG_H */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getdiskbyname,_getdiskbyname) +#endif + +#if 0 +static void error __P((int)); +#endif +static int gettype __P((char *, const char *const *)); + +static const char *db_array[2] = { _PATH_DISKTAB, 0 }; + +int +setdisktab(name) + const char *name; +{ + if (!name || !*name) + return -1; + + db_array[0] = name; + return 0; +} + + +struct disklabel * +getdiskbyname(name) + const char *name; +{ + static struct disklabel disk; + struct disklabel *dp = &disk; + struct partition *pp; + char *buf; + char *cp, *cq; /* can't be */ + char p, max, psize[3], pbsize[3], + pfsize[3], poffset[3], ptype[3]; + u_int32_t *dx; + long f; + + _DIAGASSERT(name != NULL); + + if (cgetent(&buf, db_array, name) < 0) + return NULL; + + memset(&disk, 0, sizeof(disk)); + /* + * typename + */ + cq = dp->d_typename; + cp = buf; + while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && + (*cq = *cp) && *cq != '|' && *cq != ':') + cq++, cp++; + *cq = '\0'; + /* + * boot name (optional) xxboot, bootxx + */ + cgetstr(buf, "b0", &dp->d_boot0); + cgetstr(buf, "b1", &dp->d_boot1); + + if (cgetstr(buf, "ty", &cq) >= 0) { + if (strcmp(cq, "removable") == 0) + dp->d_flags |= D_REMOVABLE; + else if (strcmp(cq, "simulated") == 0) + dp->d_flags |= D_RAMDISK; + free(cq); + } + if (cgetcap(buf, "sf", ':') != NULL) + dp->d_flags |= D_BADSECT; + +#define getnumdflt(field, dname, dflt) \ + (field) = ((cgetnum(buf, dname, &f) == -1) ? (dflt) : (u_int32_t) f) +#define getnum(field, dname) \ + if (cgetnum(buf, dname, &f) != -1) field = (u_int32_t)f + + getnumdflt(dp->d_secsize, "se", DEV_BSIZE); + getnum(dp->d_ntracks, "nt"); + getnum(dp->d_nsectors, "ns"); + getnum(dp->d_ncylinders, "nc"); + + if (cgetstr(buf, "dt", &cq) >= 0) { + dp->d_type = gettype(cq, dktypenames); + free(cq); + } else + getnumdflt(dp->d_type, "dt", 0); + getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); + getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); + getnumdflt(dp->d_rpm, "rm", 3600); + getnumdflt(dp->d_interleave, "il", 1); + getnumdflt(dp->d_trackskew, "sk", 0); + getnumdflt(dp->d_cylskew, "cs", 0); + getnumdflt(dp->d_headswitch, "hs", 0); + getnumdflt(dp->d_trkseek, "ts", 0); + getnumdflt(dp->d_bbsize, "bs", BBSIZE); + getnumdflt(dp->d_sbsize, "sb", SBLOCKSIZE); + strcpy(psize, "px"); /* XXX: strcpy is safe */ + strcpy(pbsize, "bx"); /* XXX: strcpy is safe */ + strcpy(pfsize, "fx"); /* XXX: strcpy is safe */ + strcpy(poffset, "ox"); /* XXX: strcpy is safe */ + strcpy(ptype, "tx"); /* XXX: strcpy is safe */ + max = 'a' - 1; + pp = &dp->d_partitions[0]; + for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { + long ff; + + psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; + if (cgetnum(buf, psize, &ff) == -1) + pp->p_size = 0; + else { + pp->p_size = (u_int32_t)ff; + getnum(pp->p_offset, poffset); + getnumdflt(pp->p_fsize, pfsize, 0); + if (pp->p_fsize) { + long bsize; + + if (cgetnum(buf, pbsize, &bsize) == -1) + pp->p_frag = 8; + else + pp->p_frag = + (u_int8_t)(bsize / pp->p_fsize); + } + getnumdflt(pp->p_fstype, ptype, 0); + if (pp->p_fstype == 0) + if (cgetstr(buf, ptype, &cq) >= 0) { + pp->p_fstype = gettype(cq, fstypenames); + free(cq); + } + max = p; + } + } + dp->d_npartitions = max + 1 - 'a'; + strcpy(psize, "dx"); /* XXX: strcpy is safe */ + dx = dp->d_drivedata; + for (p = '0'; p < '0' + NDDATA; p++, dx++) { + psize[1] = p; + getnumdflt(*dx, psize, 0); + } + dp->d_magic = DISKMAGIC; + dp->d_magic2 = DISKMAGIC; + free(buf); + return (dp); +} + +static int +gettype(t, names) + char *t; + const char *const *names; +{ + const char *const *nm; + + _DIAGASSERT(t != NULL); + _DIAGASSERT(names != NULL); + + for (nm = names; *nm; nm++) + if (strcasecmp(t, *nm) == 0) + return (nm - names); + if (isdigit((unsigned char) *t)) + return (atoi(t)); + return (0); +} + +#if 0 +static void +error(err) + int err; +{ + char *p; + + (void)write(STDERR_FILENO, "disktab: ", 9); + (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1); + (void)write(STDERR_FILENO, ": ", 2); + p = strerror(err); + (void)write(STDERR_FILENO, p, strlen(p)); + (void)write(STDERR_FILENO, "\n", 1); +} +#endif diff --git a/lib/nbsd_libc/gen/endutxent.3 b/lib/nbsd_libc/gen/endutxent.3 new file mode 100644 index 000000000..06a443345 --- /dev/null +++ b/lib/nbsd_libc/gen/endutxent.3 @@ -0,0 +1,202 @@ +.\" $NetBSD: endutxent.3,v 1.5 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2002 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by 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. +.\" +.Dd September 26, 2002 +.Dt ENDUTXENT 3 +.Os +.Sh NAME +.Nm endutxent , +.Nm getutxent , +.Nm getutxid , +.Nm getutxline , +.Nm pututxline , +.Nm setutxent +.Nd user accounting database functions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In utmpx.h +.Ft void +.Fn endutxent void +.Ft struct utmpx * +.Fn getutxent void +.Ft struct utmpx * +.Fn getutxid "const struct utmpx *" +.Ft struct utmpx * +.Fn getutxline "const struct utmpx *" +.Ft struct utmpx * +.Fn pututxline "const struct utmpx *" +.Ft void +.Fn setutxent void +.Sh DESCRIPTION +These functions provide access to the +.Xr utmpx 5 +user accounting database. +.Pp +.Fn getutxent +reads the next entry from the database; +if the database was not yet open, it also opens it. +.Fn setutxent +resets the database, so that the next +.Fn getutxent +call will get the first entry. +.Fn endutxent +closes the database. +.Pp +.Fn getutxid +returns the next entry of the type specified in its argument's +.Va ut_type +field, or +.Dv NULL +if none is found. +.Fn getutxline +returns the next +.Dv LOGIN_PROCESS +or +.Dv USER_PROCESS +entry which has the same name as specified in the +.Va ut_line +field, or +.Dv NULL +if no match is found. +.Pp +.Fn pututxline +adds the argument +.Xr utmpx 5 +entry line to the accounting database, replacing a previous entry for +the same user if it exists. +.Ss The utmpx structure +The +.Nm utmpx +structure has the following definition: +.Pp +.Bd -literal +struct utmpx { + char ut_name[_UTX_USERSIZE]; /* login name */ + char ut_id[_UTX_IDSIZE]; /* inittab id */ + char ut_line[_UTX_LINESIZE]; /* tty name */ + char ut_host[_UTX_HOSTSIZE]; /* host name */ + uint16_t ut_session; /* session id used for windowing */ + uint16_t ut_type; /* type of this entry */ + pid_t ut_pid; /* process id creating the entry */ + struct { + uint16_t e_termination; /* process termination signal */ + uint16_t e_exit; /* process exit status */ + } ut_exit; + struct sockaddr_storage ut_ss; /* address where entry was made from */ + struct timeval ut_tv; /* time entry was created */ + uint32_t ut_pad[10]; /* reserved for future use */ +}; +.Ed +.Pp +Valid entries for +.Fa ut_type +are: +.Bl -tag -width LOGIN_PROCESSXX -compact -offset indent +.It Dv BOOT_TIME +Time of a system boot. +.It Dv DEAD_PROCESS +A session leader exited. +.It Dv EMPTY +No valid user accounting information. +.It Dv INIT_PROCESS +A process spawned by +.Xr init 8 . +.It Dv LOGIN_PROCESS +The session leader of a logged-in user. +.It Dv NEW_TIME +Time after system clock change. +.It Dv OLD_TIME +Time before system clock change. +.It Dv RUN_LVL +Run level. +Provided for compatibility, not used on +.Nx . +.It Dv USER_PROCESS +A user process. +.El +.Sh RETURN VALUES +.Fn getutxent +returns the next entry, or +.Dv NULL +on failure (end of database or problems reading from the database). +.Fn getutxid +and +.Fn getutxline +return the matching structure on success, or +.Dv NULL +if no match was found. +.Fn pututxline +returns the structure that was successfully written, or +.Dv NULL . +.Sh SEE ALSO +.Xr logwtmpx 3 , +.Xr utmpx 5 +.Sh STANDARDS +The +.Fn endutxent , +.Fn getutxent , +.Fn getutxid , +.Fn getutxline , +.Fn pututxline , +.Fn setutxent +all conform to +.St -p1003.1-2001 +(XSI extension), and previously to +.St -xpg4.2 . +The fields +.Fa ut_user , +.Fa ut_id , +.Fa ut_line , +.Fa ut_pid , +.Fa ut_type , +and +.Fa ut_tv +conform to +.St -p1003.1-2001 +(XSI extension), and previously to +.St -xpg4.2 . +.\" .Fa ut_host , +.\" .Fa ut_session , +.\" .Fa ut_exit , +.\" and +.\" .Fa ut_ss +.\" are from +.\" SVR3/4? +.\" .Dv RUN_LVL +.\" is for compatibility with +.\" what exactly? +.\" .Sh HISTORY +.\" The +.\" .Nm utmpx , +.\" .Nm wtmpx , +.\" and +.\" .Nm lastlogx +.\" files first appeared in +.\" SVR3? 4? diff --git a/lib/nbsd_libc/gen/err.3 b/lib/nbsd_libc/gen/err.3 new file mode 100644 index 000000000..92f2629d9 --- /dev/null +++ b/lib/nbsd_libc/gen/err.3 @@ -0,0 +1,174 @@ +.\" $NetBSD: err.3,v 1.20 2010/03/22 19:30:53 joerg Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)err.3 8.1 (Berkeley) 6/9/93 +.\" +.Dd March 21, 2001 +.Dt ERR 3 +.Os +.Sh NAME +.Nm err , +.Nm verr , +.Nm errx , +.Nm verrx , +.Nm warn , +.Nm vwarn , +.Nm warnx , +.Nm vwarnx +.Nd formatted error messages +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In err.h +.Ft void +.Fn err "int status" "const char *fmt" "..." +.Ft void +.Fn verr "int status" "const char *fmt" "va_list args" +.Ft void +.Fn errx "int status" "const char *fmt" "..." +.Ft void +.Fn verrx "int status" "const char *fmt" "va_list args" +.Ft void +.Fn warn "const char *fmt" "..." +.Ft void +.Fn vwarn "const char *fmt" "va_list args" +.Ft void +.Fn warnx "const char *fmt" "..." +.Ft void +.Fn vwarnx "const char *fmt" "va_list args" +.Sh DESCRIPTION +The +.Fn err +and +.Fn warn +family of functions display a formatted error message on the standard +error output. +In all cases, the last component of the program name, a colon character, +and a space are output. +If the +.Fa fmt +argument is not +.Dv NULL , +the formatted error message is output. +In the case of the +.Fn err , +.Fn verr , +.Fn warn , +and +.Fn vwarn +functions, the error message string affiliated with the current value of +the global variable +.Va errno +is output next, preceded by a colon character and a space if +.Fa fmt +is not +.Dv NULL . +In all cases, the output is followed by a newline character. +The +.Fn errx , +.Fn verrx , +.Fn warnx , +and +.Fn vwarnx +functions will not output this error message string. +.Pp +The +.Fn err , +.Fn verr , +.Fn errx , +and +.Fn verrx +functions do not return, but instead cause the program to terminate +with the status value given by the argument +.Fa status . +It is often appropriate to use the value +.Dv EXIT_FAILURE , +defined in +.In stdlib.h , +as the +.Fa status +argument given to these functions. +.Sh EXAMPLES +Display the current +.Va errno +information string and terminate with status indicating failure: +.Bd -literal -offset indent +if ((p = malloc(size)) == NULL) + err(EXIT_FAILURE, NULL); +if ((fd = open(file_name, O_RDONLY, 0)) == -1) + err(EXIT_FAILURE, "%s", file_name); +.Ed +.Pp +Display an error message and terminate with status indicating failure: +.Bd -literal -offset indent +if (tm.tm_hour \*[Lt] START_TIME) + errx(EXIT_FAILURE, "too early, wait until %s", + start_time_string); +.Ed +.Pp +Warn of an error: +.Bd -literal -offset indent +if ((fd = open(raw_device, O_RDONLY, 0)) == -1) + warnx("%s: %s: trying the block device", + raw_device, strerror(errno)); +if ((fd = open(block_device, O_RDONLY, 0)) == -1) + warn("%s", block_device); +.Ed +.Sh SEE ALSO +.Xr exit 3 , +.Xr getprogname 3 , +.Xr strerror 3 +.Sh HISTORY +The +.Fn err +and +.Fn warn +functions first appeared in +.Bx 4.4 . +.Sh CAVEATS +It is important never to pass a string with user-supplied data as a +format without using +.Ql %s . +An attacker can put format specifiers in the string to mangle your stack, +leading to a possible security hole. +This holds true even if you have built the string +.Dq by hand +using a function like +.Fn snprintf , +as the resulting string may still contain user-supplied conversion specifiers +for later interpolation by the +.Fn err +and +.Fn warn +functions. +.Pp +Always be sure to use the proper secure idiom: +.Bd -literal -offset indent +err(1, "%s", string); +.Ed diff --git a/lib/nbsd_libc/gen/err.c b/lib/nbsd_libc/gen/err.c new file mode 100644 index 000000000..9bd01b070 --- /dev/null +++ b/lib/nbsd_libc/gen/err.c @@ -0,0 +1,63 @@ +/* $NetBSD: err.c,v 1.26 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: err.c,v 1.26 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(err, _err) +#endif + +#if !HAVE_ERR_H +__dead void +err(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verr(eval, fmt, ap); + va_end(ap); +} +#endif diff --git a/lib/nbsd_libc/gen/errlist.awk b/lib/nbsd_libc/gen/errlist.awk new file mode 100644 index 000000000..81c43fc3c --- /dev/null +++ b/lib/nbsd_libc/gen/errlist.awk @@ -0,0 +1,113 @@ +#! /usr/bin/awk -f +# $NetBSD: errlist.awk,v 1.4 2010/12/16 22:52:32 joerg Exp $ +# +# Copyright (c) 2010 The NetBSD Foundation, Inc. +# All rights reserved. +# +# This code is derived from software contributed to The NetBSD Foundation +# by Christos Zoulas. +# +# 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 by the NetBSD +# Foundation, Inc. and its contributors. +# 4. Neither the name of The NetBSD Foundation 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 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. +# +function tabs(desc) { + l = length(desc) + 3; + if (concat) + l++ + if (l < 16) + return "\t\t\t\t"; + else if (l < 24) + return "\t\t\t"; + else if (l < 32) + return "\t\t"; + else if (l < 40) + return "\t"; + else + return ""; +} +function perror(name, number, desc) +{ + if (!concat) { + printf("\t\"%s\",%s/* %d - %s */\n", desc, tabs(desc), number, name); + } else { + offsets[number] = offset; + offset += length(desc) + 1; + printf("\t\"%s\\0\"%s/* %d - %s */\n", desc, tabs(desc), number, name); + } +} +BEGIN { + printf("/* Automatically generated file; do not edit */\n"); + printf("#include \n"); + printf("__RCSID(\"$NetBSD: errlist.awk,v 1.4 2010/12/16 22:52:32 joerg Exp $\");\n"); + printf("#include \n"); + if (!concat) { + printf("static const char *const errlist[] = {\n"); + } else { + printf("static const char concat_errlist[] = {\n"); + offset = 0; + } + perror("ENOERROR", 0, "Undefined error: 0"); + errno = 1; +} +/^#define/ { + name = $2; + if (name == "ELAST") + next; + number = $3; + if (number < 0 || number == "EAGAIN") + next; + desc = $0; + i1 = index(desc, "/*") + 3; + l = length(desc); + desc = substr(desc, i1, l - i1 - 2); + if (number != errno) { + printf("error number mismatch %d != %d\n", number, errno) > "/dev/stderr"; + exit(1); + } + perror(name, number, desc); + errno++; +} +END { + printf("};\n\n"); + if (!concat) { + printf("const int sys_nerr = sizeof(errlist) / sizeof(errlist[0]);\n"); + printf("const char * const *sys_errlist = errlist;\n"); + } else { + printf("static const int concat_nerr = %d;\n", errno); + printf("static const unsigned short concat_offset[] = {\n"); + offsets[errno++] = offset; + for (j = 0; j < errno; j++) { + printf("\t%d,\n", offsets[j]); + } + printf("};\n"); + if (offset > 65535) { + printf("Total errlist size doesn't fit into 16bit\n") > "/dev/stderr"; + exit(1); + } + } +} diff --git a/lib/nbsd_libc/gen/errno.c b/lib/nbsd_libc/gen/errno.c new file mode 100644 index 000000000..ca8b2acf8 --- /dev/null +++ b/lib/nbsd_libc/gen/errno.c @@ -0,0 +1,8 @@ +/* $NetBSD: errno.c,v 1.5 2005/06/12 05:21:27 lukem Exp $ */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: errno.c,v 1.5 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +int errno; diff --git a/lib/nbsd_libc/gen/errx.c b/lib/nbsd_libc/gen/errx.c new file mode 100644 index 000000000..004fdd6b1 --- /dev/null +++ b/lib/nbsd_libc/gen/errx.c @@ -0,0 +1,63 @@ +/* $NetBSD: errx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: errx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(errx, _errx) +#endif + +#if !HAVE_ERR_H +__dead void +errx(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verrx(eval, fmt, ap); + va_end(ap); +} +#endif diff --git a/lib/nbsd_libc/gen/exec.3 b/lib/nbsd_libc/gen/exec.3 new file mode 100644 index 000000000..ec529ba7d --- /dev/null +++ b/lib/nbsd_libc/gen/exec.3 @@ -0,0 +1,262 @@ +.\" $NetBSD: exec.3,v 1.21 2010/03/22 19:30:53 joerg Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)exec.3 8.3 (Berkeley) 1/24/94 +.\" +.Dd May 6, 2005 +.Dt EXEC 3 +.Os +.Sh NAME +.Nm execl , +.Nm execlp , +.Nm execle , +.Nm exect , +.Nm execv , +.Nm execvp +.Nd execute a file +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Vt extern char **environ; +.Ft int +.Fn execl "const char *path" "const char *arg" ... +.Ft int +.Fn execlp "const char *file" "const char *arg" ... +.Ft int +.Fn execle "const char *path" "const char *arg" ... "char *const envp[]" +.Ft int +.Fn exect "const char *path" "char *const argv[]" "char *const envp[]" +.Ft int +.Fn execv "const char *path" "char *const argv[]" +.Ft int +.Fn execvp "const char *file" "char *const argv[]" +.Sh DESCRIPTION +The +.Nm exec +family of functions replaces the current process image with a +new process image. +The functions described in this manual page are front-ends for the function +.Xr execve 2 . +(See the manual page for +.Xr execve 2 +for detailed information about the replacement of the current process. +The +.Xr script 7 +manual page provides detailed information about the execution of +interpreter scripts.) +.Pp +The initial argument for these functions is the pathname of a file which +is to be executed. +.Pp +The +.Fa "const char *arg" +and subsequent ellipses in the +.Fn execl , +.Fn execlp , +and +.Fn execle +functions can be thought of as +.Em arg0 , +.Em arg1 , +\&..., +.Em argn . +Together they describe a list of one or more pointers to null-terminated +strings that represent the argument list available to the executed program. +The first argument, by convention, should point to the file name associated +with the file being executed. +The list of arguments +.Em must +be terminated by a +.Dv NULL +pointer. +.Pp +The +.Fn exect , +.Fn execv , +and +.Fn execvp +functions provide an array of pointers to null-terminated strings that +represent the argument list available to the new program. +The first argument, by convention, should point to the file name associated +with the file being executed. +The array of pointers +.Sy must +be terminated by a +.Dv NULL +pointer. +.Pp +The +.Fn execle +and +.Fn exect +functions also specify the environment of the executed process by following +the +.Dv NULL +pointer that terminates the list of arguments in the parameter list +or the pointer to the argv array with an additional parameter. +This additional parameter is an array of pointers to null-terminated strings +and +.Em must +be terminated by a +.Dv NULL +pointer. +The other functions take the environment for the new process image from the +external variable +.Va environ +in the current process. +.Pp +Some of these functions have special semantics. +.Pp +The functions +.Fn execlp +and +.Fn execvp +will duplicate the actions of the shell in searching for an executable file +if the specified file name does not contain a slash +.Dq Li \&/ +character. +The search path is the path specified in the environment by the +.Ev PATH +variable. +If this variable isn't specified, +.Va _PATH_DEFPATH +from +.In paths.h +is used instead, its value being: +.Pa /usr/bin:/bin:/usr/pkg/bin:/usr/local/bin . +In addition, certain errors are treated specially. +.Pp +If permission is denied for a file (the attempted +.Xr execve 2 +returned +.Er EACCES ) , +these functions will continue searching the rest of +the search path. +If no other file is found, however, they will return with the global variable +.Va errno +set to +.Er EACCES . +.Pp +If the header of a file isn't recognized (the attempted +.Xr execve 2 +returned +.Er ENOEXEC ) , +these functions will execute the shell with the path of +the file as its first argument. +(If this attempt fails, no further searching is done.) +.Pp +If the file is currently busy (the attempted +.Xr execve 2 +returned +.Er ETXTBUSY ) , +these functions will sleep for several seconds, +periodically re-attempting to execute the file. +.Pp +The function +.Fn exect +executes a file with the program tracing facilities enabled (see +.Xr ptrace 2 ) . +.Sh RETURN VALUES +If any of the +.Nm exec +functions returns, an error will have occurred. +The return value is \-1, and the global variable +.Va errno +will be set to indicate the error. +.Sh FILES +.Bl -tag -width /bin/sh -compact +.It Pa /bin/sh +The shell. +.El +.Sh ERRORS +.Fn execl , +.Fn execle , +.Fn execlp +and +.Fn execvp +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr execve 2 +and +.Xr malloc 3 . +.Pp +.Fn exect +and +.Fn execv +may fail and set +.Va errno +for any of the errors specified for the library function +.Xr execve 2 . +.Sh SEE ALSO +.Xr sh 1 , +.Xr execve 2 , +.Xr fork 2 , +.Xr ptrace 2 , +.Xr environ 7 , +.Xr script 7 +.Sh COMPATIBILITY +Historically, the default path for the +.Fn execlp +and +.Fn execvp +functions was +.Dq Pa :/bin:/usr/bin . +This was changed to improve security and behaviour. +.Pp +The behavior of +.Fn execlp +and +.Fn execvp +when errors occur while attempting to execute the file is historic +practice, but has not traditionally been documented and is not specified +by the +.Tn POSIX +standard. +.Pp +Traditionally, the functions +.Fn execlp +and +.Fn execvp +ignored all errors except for the ones described above and +.Er ENOMEM +and +.Er E2BIG , +upon which they returned. +They now return if any error other than the ones described above occurs. +.Sh STANDARDS +.Fn execl , +.Fn execv , +.Fn execle , +.Fn execlp +and +.Fn execvp +conform to +.St -p1003.1-90 . diff --git a/lib/nbsd_libc/gen/execl.c b/lib/nbsd_libc/gen/execl.c new file mode 100644 index 000000000..9de075cb7 --- /dev/null +++ b/lib/nbsd_libc/gen/execl.c @@ -0,0 +1,86 @@ +/* $NetBSD: execl.c,v 1.16 2008/01/09 11:26:03 simonb Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: execl.c,v 1.16 2008/01/09 11:26:03 simonb Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include "reentrant.h" + +#ifdef __weak_alias +__weak_alias(execl,_execl) +#endif + + +extern char **environ; + +int +execl(const char *name, const char *arg, ...) +{ + int r; +#if defined(__i386__) || defined(__m68k__) + r = execve(name, __UNCONST(&arg), environ); + return r; +#else + va_list ap; + char **argv; + int i; + + va_start(ap, arg); + for (i = 2; va_arg(ap, char *) != NULL; i++) + continue; + va_end(ap); + + if ((argv = alloca(i * sizeof (char *))) == NULL) { + errno = ENOMEM; + return -1; + } + + va_start(ap, arg); + argv[0] = __UNCONST(arg); + for (i = 1; (argv[i] = va_arg(ap, char *)) != NULL; i++) + continue; + va_end(ap); + + r = execve(name, argv, environ); + return r; +#endif +} diff --git a/lib/nbsd_libc/gen/execle.c b/lib/nbsd_libc/gen/execle.c new file mode 100644 index 000000000..1d9a4a462 --- /dev/null +++ b/lib/nbsd_libc/gen/execle.c @@ -0,0 +1,85 @@ +/* $NetBSD: execle.c,v 1.11 2008/01/09 11:26:03 simonb Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: execle.c,v 1.11 2008/01/09 11:26:03 simonb Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(execle,_execle) +#endif + +int +execle(const char *name, const char *arg, ...) +{ +#if defined(__i386__) || defined(__m68k__) + va_list ap; + char **envp; + + va_start(ap, arg); + while ((va_arg(ap, char *)) != NULL) + continue; + envp = va_arg(ap, char **); + va_end(ap); + + return execve(name, __UNCONST(&arg), envp); +#else + va_list ap; + char **argv, **envp; + int i; + + va_start(ap, arg); + for (i = 2; va_arg(ap, char *) != NULL; i++) + continue; + va_end(ap); + + argv = alloca(i * sizeof (char *)); + + va_start(ap, arg); + argv[0] = __UNCONST(arg); + for (i = 1; (argv[i] = va_arg(ap, char *)) != NULL; i++) + continue; + envp = va_arg(ap, char **); + va_end(ap); + + return execve(name, argv, envp); +#endif +} diff --git a/lib/nbsd_libc/gen/execlp.c b/lib/nbsd_libc/gen/execlp.c new file mode 100644 index 000000000..8b84cd1bf --- /dev/null +++ b/lib/nbsd_libc/gen/execlp.c @@ -0,0 +1,75 @@ +/* $NetBSD: execlp.c,v 1.11 2008/01/09 11:26:03 simonb Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: execlp.c,v 1.11 2008/01/09 11:26:03 simonb Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(execlp,_execlp) +#endif + +int +execlp(const char *name, const char *arg, ...) +{ +#if defined(__i386__) || defined(__m68k__) + return execvp(name, __UNCONST(&arg)); +#else + va_list ap; + char **argv; + int i; + + va_start(ap, arg); + for (i = 2; va_arg(ap, char *) != NULL; i++) + continue; + va_end(ap); + + argv = alloca(i * sizeof (char *)); + + va_start(ap, arg); + argv[0] = __UNCONST(arg); + for (i = 1; (argv[i] = va_arg(ap, char *)) != NULL; i++) + continue; + va_end(ap); + + return execvp(name, argv); +#endif +} diff --git a/lib/nbsd_libc/gen/execv.c b/lib/nbsd_libc/gen/execv.c new file mode 100644 index 000000000..2bfc80069 --- /dev/null +++ b/lib/nbsd_libc/gen/execv.c @@ -0,0 +1,57 @@ +/* $NetBSD: execv.c,v 1.9 2005/11/29 13:30:49 christos Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: execv.c,v 1.9 2005/11/29 13:30:49 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include "reentrant.h" + +#ifdef __weak_alias +__weak_alias(execv,_execv) +#endif + +extern char **environ; + +int +execv(name, argv) + const char *name; + char * const *argv; +{ + return execve(name, argv, environ); +} diff --git a/lib/nbsd_libc/gen/execvp.c b/lib/nbsd_libc/gen/execvp.c new file mode 100644 index 000000000..4a4a3cfea --- /dev/null +++ b/lib/nbsd_libc/gen/execvp.c @@ -0,0 +1,155 @@ +/* $NetBSD: execvp.c,v 1.30 2007/07/20 12:41:07 yamt Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: execvp.c,v 1.30 2007/07/20 12:41:07 yamt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "reentrant.h" + +#ifdef __weak_alias +__weak_alias(execvp,_execvp) +#endif + +extern char **environ; + +int +execvp(const char *name, char * const *argv) +{ + const char **memp; + int cnt; + size_t lp, ln; + int eacces = 0; + unsigned int etxtbsy = 0; + char buf[PATH_MAX]; + const char *bp, *path, *p; + + _DIAGASSERT(name != NULL); + + /* "" is not a valid filename; check this before traversing PATH. */ + if (name[0] == '\0') { + errno = ENOENT; + goto done; + } + ln = strlen(name); + /* If it's an absolute or relative path name, it's easy. */ + if (strchr(name, '/')) { + bp = name; + path = ""; + goto retry; + } + bp = buf; + + /* Get the path we're searching. */ + if (!(path = getenv("PATH"))) + path = _PATH_DEFPATH; + + do { + /* Find the end of this path element. */ + for (p = path; *path != 0 && *path != ':'; path++) + continue; + /* + * It's a SHELL path -- double, leading and trailing colons + * mean the current directory. + */ + if (p == path) { + p = "."; + lp = 1; + } else + lp = path - p; + + /* + * If the path is too long complain. This is a possible + * security issue; given a way to make the path too long + * the user may execute the wrong program. + */ + if (lp + ln + 2 > sizeof(buf)) { + (void)write(STDERR_FILENO, "execvp: ", 8); + (void)write(STDERR_FILENO, p, lp); + (void)write(STDERR_FILENO, ": path too long\n", 16); + continue; + } + memcpy(buf, p, lp); + buf[lp] = '/'; + memcpy(buf + lp + 1, name, ln); + buf[lp + ln + 1] = '\0'; + +retry: (void)execve(bp, argv, environ); + switch (errno) { + case EACCES: + eacces = 1; + break; + case ENOTDIR: + case ENOENT: + break; + case ENOEXEC: + for (cnt = 0; argv[cnt] != NULL; ++cnt) + continue; + /* + * we can't use malloc here because, if we are doing + * vfork+exec, it leaks memory in the parent. + */ + if ((memp = alloca((cnt + 2) * sizeof(*memp))) == NULL) + goto done; + memp[0] = _PATH_BSHELL; + memp[1] = bp; + (void)memcpy(&memp[2], &argv[1], cnt * sizeof(*memp)); + (void)execve(_PATH_BSHELL, __UNCONST(memp), environ); + goto done; + case ETXTBSY: + if (etxtbsy < 3) + (void)sleep(++etxtbsy); + goto retry; + default: + goto done; + } + } while (*path++ == ':'); /* Otherwise, *path was NUL */ + if (eacces) + errno = EACCES; + else if (!errno) + errno = ENOENT; +done: + return (-1); +} diff --git a/lib/nbsd_libc/gen/extattr.3 b/lib/nbsd_libc/gen/extattr.3 new file mode 100644 index 000000000..3c6a97c08 --- /dev/null +++ b/lib/nbsd_libc/gen/extattr.3 @@ -0,0 +1,99 @@ +.\" $NetBSD: extattr.3,v 1.3 2005/01/02 18:25:09 wiz Exp $ +.\" +.\" Copyright (c) 2001 Dima Dorfman +.\" 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 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. +.\" +.\" FreeBSD: src/lib/libc/posix1e/extattr.3,v 1.5 2002/12/12 17:25:53 ru Exp +.\" +.Dd January 2, 2004 +.Dt EXTATTR 3 +.Os +.Sh NAME +.Nm extattr_namespace_to_string , +.Nm extattr_string_to_namespace +.Nd convert an extended attribute namespace identifier to a string and vice versa +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/extattr.h +.Ft int +.Fn extattr_namespace_to_string "int attrnamespace" "char **string" +.Ft int +.Fn extattr_string_to_namespace "const char *string" "int *attrnamespace" +.Sh DESCRIPTION +The +.Fn extattr_namespace_to_string +function converts a VFS extended attribute identifier to a human-readable +string. +The +.Fn extattr_string_to_namespace +converts a human-readable string representing a namespace to a +namespace identifier. +Although a file system may implement arbitrary namespaces, +these functions only support the +.Dv EXTATTR_NAMESPACE_USER +.Pq Dq user +and +.Dv EXTATTR_NAMESPACE_SYSTEM +.Pq Dq system +namespaces, +which are defined in +.Xr extattr 9 . +.Pp +These functions are meant to be used in error reporting and other +interactive tasks. +For example, +instead of printing the integer identifying an extended attribute in +an error message, +a program might use +.Fn extattr_namespace_to_string +to obtain a human-readable representation. +Likewise, +instead of requiring a user to enter the integer representing a namespace, +an interactive program might ask for a name and use +.Fn extattr_string_to_namespace +to get the desired identifier. +.Sh RETURN VALUES +If any of the calls are unsuccessful, the value \-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 requested namespace could not be identified. +.El +.Sh SEE ALSO +.Xr getextattr 1 , +.Xr extattr_get_file 2 , +.Xr extattr 9 +.Sh HISTORY +Extended attribute support was developed as part of the +.Tn TrustedBSD +Project, and introduced in +.Fx 5.0 +and +.Nx 3.0 . +It was developed to support security extensions requiring additional labels +to be associated with each file or directory. diff --git a/lib/nbsd_libc/gen/extattr.c b/lib/nbsd_libc/gen/extattr.c new file mode 100644 index 000000000..90c9c5c2c --- /dev/null +++ b/lib/nbsd_libc/gen/extattr.c @@ -0,0 +1,87 @@ +/* $NetBSD: extattr.c,v 1.2 2005/02/09 21:35:46 kleink Exp $ */ + +/*- + * Copyright (c) 2001 Robert N. M. Watson + * 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 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. + */ + +/* + * TrustedBSD: Utility functions for extended attributes. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: extattr.c,v 1.2 2005/02/09 21:35:46 kleink Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include + +int +extattr_namespace_to_string(int attrnamespace, char **string) +{ + + switch(attrnamespace) { + case EXTATTR_NAMESPACE_USER: + if (string != NULL) { + if ((*string = + strdup(EXTATTR_NAMESPACE_USER_STRING)) == NULL) + return (-1); + } + return (0); + + case EXTATTR_NAMESPACE_SYSTEM: + if (string != NULL) + if ((*string = + strdup(EXTATTR_NAMESPACE_SYSTEM_STRING)) == NULL) + return (-1); + return (0); + + default: + errno = EINVAL; + return (-1); + } +} + +int +extattr_string_to_namespace(const char *string, int *attrnamespace) +{ + + if (strcmp(string, EXTATTR_NAMESPACE_USER_STRING) == 0) { + if (attrnamespace != NULL) + *attrnamespace = EXTATTR_NAMESPACE_USER; + return (0); + } else if (strcmp(string, EXTATTR_NAMESPACE_SYSTEM_STRING) == 0) { + if (attrnamespace != NULL) + *attrnamespace = EXTATTR_NAMESPACE_SYSTEM; + return (0); + } else { + errno = EINVAL; + return (-1); + } +} diff --git a/lib/nbsd_libc/gen/fabs_ieee754.c b/lib/nbsd_libc/gen/fabs_ieee754.c new file mode 100644 index 000000000..006004b92 --- /dev/null +++ b/lib/nbsd_libc/gen/fabs_ieee754.c @@ -0,0 +1,54 @@ +/* $NetBSD: fabs_ieee754.c,v 1.2 2010/09/10 16:32:35 matt Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fabs_ieee754.c,v 1.2 2010/09/10 16:32:35 matt Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 fabs - return the absolute value of the argument + * IEEE 754 double-precision version + */ +double +fabs(double x) +{ + union ieee_double_u u; + + u.dblu_d = x; + + u.dblu_dbl.dbl_sign = 0; + + return u.dblu_d; +} diff --git a/lib/nbsd_libc/gen/fmtcheck.3 b/lib/nbsd_libc/gen/fmtcheck.3 new file mode 100644 index 000000000..4c3c3b574 --- /dev/null +++ b/lib/nbsd_libc/gen/fmtcheck.3 @@ -0,0 +1,97 @@ +.\" $NetBSD: fmtcheck.3,v 1.7 2009/03/09 19:24:26 joerg Exp $ +.\" +.\" Copyright (c) 2000 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This file was contributed to The NetBSD Foundation by Allen Briggs. +.\" +.\" 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 October 17, 2000 +.Dt FMTCHECK 3 +.Os +.Sh NAME +.Nm fmtcheck +.Nd sanitizes user-supplied printf(3)-style format string +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdio.h +.Ft const char * +.Fn fmtcheck "const char *fmt_suspect" "const char *fmt_default" +.Sh DESCRIPTION +The +.Nm +function scans +.Fa fmt_suspect +and +.Fa fmt_default +to determine if +.Fa fmt_suspect +will consume the same argument types as +.Fa fmt_default +and to ensure that +.Fa fmt_suspect +is a valid format string. +.Pp +The +.Xr printf 3 +family of functions can not verify the types of arguments that they are +passed at run-time. +In some cases, like +.Xr catgets 3 , +it is useful or necessary to use a user-supplied format string with no +guarantee that the format string matches the specified parameters. +.Pp +The +.Nm +function was designed to be used in these cases, as in: +.Bd -literal -offset indent +printf(fmtcheck(user_format, standard_format), arg1, arg2); +.Ed +.Pp +In the check, field widths, fillers, precisions, etc. are ignored (unless +the field width or precision is an asterisk +.Ql * +instead of a digit string). +Also, any text other than the format specifiers is completely ignored. +.Pp +Note that the formats may be quite different as long as they accept the +same parameters. +For example, "%p %o %30s %#llx %-10.*e %n" is +compatible with "This number %lu %d%% and string %s has %qd numbers +and %.*g floats (%n)." +However, "%o" is not equivalent to "%lx" because +the first requires an integer and the second requires a long. +.Sh RETURN VALUES +If +.Fa fmt_suspect +is a valid format and consumes the same argument types as +.Fa fmt_default , +then the +.Nm +function will return +.Fa fmt_suspect . +Otherwise, it will return +.Fa fmt_default . +.Sh SEE ALSO +.Xr printf 3 diff --git a/lib/nbsd_libc/gen/fmtcheck.c b/lib/nbsd_libc/gen/fmtcheck.c new file mode 100644 index 000000000..7c779bea4 --- /dev/null +++ b/lib/nbsd_libc/gen/fmtcheck.c @@ -0,0 +1,243 @@ +/* $NetBSD: fmtcheck.c,v 1.8 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code was contributed to The NetBSD Foundation by Allen Briggs. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fmtcheck.c,v 1.8 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include "namespace.h" + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fmtcheck,__fmtcheck) +#endif + +enum __e_fmtcheck_types { + FMTCHECK_START, + FMTCHECK_SHORT, + FMTCHECK_INT, + FMTCHECK_LONG, + FMTCHECK_QUAD, + FMTCHECK_SHORTPOINTER, + FMTCHECK_INTPOINTER, + FMTCHECK_LONGPOINTER, + FMTCHECK_QUADPOINTER, + FMTCHECK_DOUBLE, + FMTCHECK_LONGDOUBLE, + FMTCHECK_STRING, + FMTCHECK_WIDTH, + FMTCHECK_PRECISION, + FMTCHECK_DONE, + FMTCHECK_UNKNOWN +}; +typedef enum __e_fmtcheck_types EFT; + +#define RETURN(pf,f,r) do { \ + *(pf) = (f); \ + return r; \ + } /*NOTREACHED*/ /*CONSTCOND*/ while (0) + +static EFT +get_next_format_from_precision(const char **pf) +{ + int sh, lg, quad, longdouble; + const char *f; + + sh = lg = quad = longdouble = 0; + + f = *pf; + switch (*f) { + case 'h': + f++; + sh = 1; + break; + case 'l': + f++; + if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN); + if (*f == 'l') { + f++; + quad = 1; + } else { + lg = 1; + } + break; + case 'q': + f++; + quad = 1; + break; + case 'L': + f++; + longdouble = 1; + break; + default: + break; + } + if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN); + if (strchr("diouxX", *f)) { + if (longdouble) + RETURN(pf,f,FMTCHECK_UNKNOWN); + if (lg) + RETURN(pf,f,FMTCHECK_LONG); + if (quad) + RETURN(pf,f,FMTCHECK_QUAD); + RETURN(pf,f,FMTCHECK_INT); + } + if (*f == 'n') { + if (longdouble) + RETURN(pf,f,FMTCHECK_UNKNOWN); + if (sh) + RETURN(pf,f,FMTCHECK_SHORTPOINTER); + if (lg) + RETURN(pf,f,FMTCHECK_LONGPOINTER); + if (quad) + RETURN(pf,f,FMTCHECK_QUADPOINTER); + RETURN(pf,f,FMTCHECK_INTPOINTER); + } + if (strchr("DOU", *f)) { + if (sh + lg + quad + longdouble) + RETURN(pf,f,FMTCHECK_UNKNOWN); + RETURN(pf,f,FMTCHECK_LONG); + } + if (strchr("eEfg", *f)) { + if (longdouble) + RETURN(pf,f,FMTCHECK_LONGDOUBLE); + if (sh + lg + quad) + RETURN(pf,f,FMTCHECK_UNKNOWN); + RETURN(pf,f,FMTCHECK_DOUBLE); + } + if (*f == 'c') { + if (sh + lg + quad + longdouble) + RETURN(pf,f,FMTCHECK_UNKNOWN); + RETURN(pf,f,FMTCHECK_INT); + } + if (*f == 's') { + if (sh + lg + quad + longdouble) + RETURN(pf,f,FMTCHECK_UNKNOWN); + RETURN(pf,f,FMTCHECK_STRING); + } + if (*f == 'p') { + if (sh + lg + quad + longdouble) + RETURN(pf,f,FMTCHECK_UNKNOWN); + RETURN(pf,f,FMTCHECK_LONG); + } + RETURN(pf,f,FMTCHECK_UNKNOWN); + /*NOTREACHED*/ +} + +static EFT +get_next_format_from_width(const char **pf) +{ + const char *f; + + f = *pf; + if (*f == '.') { + f++; + if (*f == '*') { + RETURN(pf,f,FMTCHECK_PRECISION); + } + /* eat any precision (empty is allowed) */ + while (isdigit((unsigned char)*f)) f++; + if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN); + } + RETURN(pf,f,get_next_format_from_precision(pf)); + /*NOTREACHED*/ +} + +static EFT +get_next_format(const char **pf, EFT eft) +{ + int infmt; + const char *f; + + if (eft == FMTCHECK_WIDTH) { + (*pf)++; + return get_next_format_from_width(pf); + } else if (eft == FMTCHECK_PRECISION) { + (*pf)++; + return get_next_format_from_precision(pf); + } + + f = *pf; + infmt = 0; + while (!infmt) { + f = strchr(f, '%'); + if (f == NULL) + RETURN(pf,f,FMTCHECK_DONE); + f++; + if (!*f) + RETURN(pf,f,FMTCHECK_UNKNOWN); + if (*f != '%') + infmt = 1; + else + f++; + } + + /* Eat any of the flags */ + while (*f && (strchr("#0- +", *f))) + f++; + + if (*f == '*') { + RETURN(pf,f,FMTCHECK_WIDTH); + } + /* eat any width */ + while (isdigit((unsigned char)*f)) f++; + if (!*f) { + RETURN(pf,f,FMTCHECK_UNKNOWN); + } + + RETURN(pf,f,get_next_format_from_width(pf)); + /*NOTREACHED*/ +} + +const char * +fmtcheck(const char *f1, const char *f2) +{ + const char *f1p, *f2p; + EFT f1t, f2t; + + if (!f1) return f2; + + f1p = f1; + f1t = FMTCHECK_START; + f2p = f2; + f2t = FMTCHECK_START; + while ((f1t = get_next_format(&f1p, f1t)) != FMTCHECK_DONE) { + if (f1t == FMTCHECK_UNKNOWN) + return f2; + f2t = get_next_format(&f2p, f2t); + if (f1t != f2t) + return f2; + } + return f1; +} diff --git a/lib/nbsd_libc/gen/fmtmsg.3 b/lib/nbsd_libc/gen/fmtmsg.3 new file mode 100644 index 000000000..3b3fb646c --- /dev/null +++ b/lib/nbsd_libc/gen/fmtmsg.3 @@ -0,0 +1,220 @@ +.\" $NetBSD: fmtmsg.3,v 1.6 2008/04/30 13:10:50 martin 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 September 10, 1999 +.Dt FMTMSG 3 +.Os +.Sh NAME +.Nm fmtmsg +.Nd format and display a message +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In fmtmsg.h +.Ft int +.Fn fmtmsg "long classification" "const char *label" "int severity" "const char *text" "const char *action" "const char *tag" +.Sh DESCRIPTION +The +.Fn fmtmsg +function can be used to display messages in the specified format. +Messages may be written either to standard error, to the console, or both. +.Pp +A formatted message consists of up to five components specified in +.Fa label , +.Fa severity , +.Fa text , +.Fa action +and +.Fa tag . +Further information such as the origin of the message, the recoverability +from the condition causing the message and where to display the message +is specified in +.Fa classification . +.Ss classification +The +.Fa classification +argument consists of a major classification and several sub-classifications. +It has no effect on the content of the message displayed. +With the exception of the display sub-classification, only a single identifier +may be specified for each (sub-)classification. +The following classifications +are available: +.Bl -tag -width MessageXSourceXSub-classificationsXX +.It Major Classifications +The source of the condition. +Available identifiers are: +.Dv MM_HARD +(hardware), +.Dv MM_SOFT +(software), and +.Dv MM_FIRM +(firmware). +.It Message Source Sub-classifications +The type of software detecting the condition. +Available identifiers are: +.Dv MM_APPL +(application), +.Dv MM_UTIL +(utility), and +.Dv MM_OPSYS +(operating system). +.It Display Sub-classifications +The displays the formatted messages is to be written to. +Available identifiers are: +.Dv MM_PRINT +(standard error stream) and +.Dv MM_CONSOLE +(system console). +.It Status Sub-classifications +The capability of the calling software to recover from the condition. +Available identifiers are: +.Dv MM_RECOVER +(recoverable) and +.Dv MM_NRECOV +(non-recoverable). +.El +.Pp +If no +.Fa classification +is to be supplied, +.Dv MM_NULLMC +must be specified. +.Ss label +The +.Fa label +argument identifies the source of the message. +It consists of two fields separated by a colon (:). +The first field is up to 10 characters, the second is up to 14 characters. +.Pp +If no +.Fa label +is to be supplied, +.Dv MM_NULLLBL +must be specified. +.Ss severity +The seriousness of the condition causing the message. +The following +.Fa severity +levels are available: +.Bl -tag -width MM_WARNINGXX +.It Dv MM_HALT +The software has encountered a severe fault and is halting. +.It Dv MM_ERROR +The software has encountered a fault. +.It Dv MM_WARNING +The software has encountered an unusual non-fault condition. +.It Dv MM_INFO +The software informs about a non-error condition. +.El +.Pp +If no +.Fa severity +level is to be supplied, +.Dv MM_NOSEV +must be specified. +.Ss text +The description of the condition the software encountered. +The character +string is not limited to a specific size. +.Pp +If no +.Fa text +is to be supplied, +.Dv MM_NOTXT +must be specified. +.Ss action +The first step to be taken to recover from the condition the software +encountered; it will be preceded by the prefix +.Dq TO FIX: . +The character string is not limited to a specific size. +.Pp +If no +.Fa action +is to be supplied, +.Dv MM_NOACT +must be specified. +.Ss tag +The on-line documentation which provides further information about the +condition and the message, such as +.Dq Xr fmtmsg 3 . +The character string is not limited to a specific size. +.Pp +If no +.Fa tag +is to be supplied, +.Dv MM_NOTAG +must be specified. +.Pp +Further effect on the formatting of the message as displayed on the +standard error stream (but not on the system console!) may be taken by +setting the +.Ev MSGVERB +environment variable, which selects the subset of message components +to be printed. +It consists of a colon-separated list of the optional keywords +.Fa label , +.Fa severity , +.Fa text , +.Fa action , +and +.Fa tag , +which correspond to the arguments to +.Fn fmtmsg +with the same names. +If +.Ev MSGVERB +is either not set or malformed (containing empty or unknown keywords), +its content is ignored an all message components will be selected. +.Pp +Note that displaying a message on the system console may fail due to +inappropriate privileges or a non-permissive file mode of the console device. +.Sh RETURN VALUES +The +.Fn fmtmsg +function returns one of the following values: +.Bl -tag -width MM_NOTOKXXX +.It Dv MM_OK +The function succeeded. +.It Dv MM_NOTOK +The function failed completely. +.It Dv MM_NOMSG +The function was unable to generate a message on standard error, +but otherwise succeeded. +.It Dv MM_NOCOM +The function was unable to generate a message on the console, +but otherwise succeeded. +.El +.Sh SEE ALSO +.Xr printf 3 , +.Xr syslog 3 +.Sh STANDARDS +The +.Fn fmtmsg +function conforms to +.St -xsh5 . diff --git a/lib/nbsd_libc/gen/fmtmsg.c b/lib/nbsd_libc/gen/fmtmsg.c new file mode 100644 index 000000000..9ca13ace3 --- /dev/null +++ b/lib/nbsd_libc/gen/fmtmsg.c @@ -0,0 +1,246 @@ +/* $NetBSD: fmtmsg.c,v 1.4 2008/04/28 20:22:59 martin 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fmtmsg.c,v 1.4 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include + +static unsigned int msgverb __P((const char *)); +static const char * severity2str __P((int)); +static int writeit __P((FILE *, unsigned int, const char *, + const char *, const char *, const char *, + const char *)); + +#define MM_VERBLABEL 0x01U +#define MM_VERBSEVERITY 0x02U +#define MM_VERBTEXT 0x04U +#define MM_VERBACTION 0x08U +#define MM_VERBTAG 0x10U +#define MM_VERBALL \ + (MM_VERBLABEL | MM_VERBSEVERITY | MM_VERBTEXT | MM_VERBACTION | \ + MM_VERBTAG) + +static const struct keyword { + size_t len; /* strlen(keyword) */ + const char * const keyword; +} keywords[] = { + { 5, "label" }, /* log2(MM_VERBLABEL) */ + { 8, "severity" }, /* ... */ + { 4, "text" }, + { 6, "action" }, + { 3, "tag" } /* log2(MM_VERBTAG) */ +}; + +static const size_t nkeywords = sizeof (keywords) / sizeof (keywords[0]); + +/* + * Convert a colon-separated list of known keywords to a set of MM_VERB* + * flags, defaulting to `all' if not set, empty, or in presence of unknown + * keywords. + */ +static unsigned int +msgverb(str) + const char *str; +{ + u_int i; + unsigned int result; + + if (str == NULL) + return (MM_VERBALL); + + result = 0; + while (*str != '\0') { + for (i = 0; i < nkeywords; i++) { + if (memcmp(str, keywords[i].keyword, keywords[i].len) + == 0 && + (*(str + keywords[i].len) == ':' || + *(str + keywords[i].len) == '\0')) + break; + } + if (i == nkeywords) { + result = MM_VERBALL; + break; + } + + result |= (1 << i); + if (*(str += keywords[i].len) == ':') + str++; /* Advance */ + } + if (result == 0) + result = MM_VERBALL; + + return (result); +} + +static const char * const severities[] = { + "", /* MM_NONE */ + "HALT", + "ERROR", + "WARNING", + "INFO" +}; + +static const size_t nseverities = sizeof (severities) / sizeof (severities[0]); + +/* + * Returns the string representation associated with the numerical severity + * value, defaulting to NULL for an unknown value. + */ +static const char * +severity2str(severity) + int severity; +{ + const char *result; + + if (severity >= 0 && + (u_int) severity < nseverities) + result = severities[severity]; + else + result = NULL; + + return (result); +} + +/* + * Format and write the message to the given stream, selecting those + * components displayed from msgverb, returning the number of characters + * written, or a negative value in case of an error. + */ +static int +writeit(stream, which, label, sevstr, text, action, tag) + FILE *stream; + unsigned int which; + const char *label; + const char *sevstr; + const char *text; + const char *action; + const char *tag; +{ + int nwritten; + + nwritten = fprintf(stream, "%s%s%s%s%s%s%s%s%s%s%s", + ((which & MM_VERBLABEL) && label != MM_NULLLBL) ? + label : "", + ((which & MM_VERBLABEL) && label != MM_NULLLBL) ? + ": " : "", + (which & MM_VERBSEVERITY) ? + sevstr : "", + (which & MM_VERBSEVERITY) ? + ": " : "", + ((which & MM_VERBTEXT) && text != MM_NULLTXT) ? + text : "", + ((which & MM_VERBLABEL) && label != MM_NULLLBL) || + ((which & MM_VERBSEVERITY)) || + ((which & MM_VERBTEXT) && text != MM_NULLTXT) ? + "\n" : "", + ((which & MM_VERBACTION) && action != MM_NULLACT) ? + "TO FIX: " : "", + ((which & MM_VERBACTION) && action != MM_NULLACT) ? + action : "", + ((which & MM_VERBACTION) && label != MM_NULLACT) ? + " " : "", + ((which & MM_VERBTAG) && tag != MM_NULLTAG) ? + tag : "", + ((which & MM_VERBACTION) && action != MM_NULLACT) || + ((which & MM_VERBTAG) && tag != MM_NULLTAG) ? + "\n" : ""); + + return (nwritten); +} + +int +fmtmsg(classification, label, severity, text, action, tag) + long classification; + const char *label; + int severity; + const char *text; + const char *action; + const char *tag; +{ + FILE *console; + const char *p, *sevstr; + int result; + + /* Validate label constraints, if not null. */ + if (label != MM_NULLLBL) { + /* + * Two fields, separated by a colon. The first field is up to + * 10 bytes, the second is up to 14 bytes. + */ + p = strchr(label, ':'); + if (p == NULL || p - label > 10 || strlen(p + 1) > 14) + return (MM_NOTOK); + } + /* Validate severity argument. */ + if ((sevstr = severity2str(severity)) == NULL) + return (MM_NOTOK); + + /* + * Fact in search for a better place: XSH5 does not define any + * functionality for `classification' bits other than the display + * subclassification. + */ + + result = 0; + + if (classification & MM_PRINT) { + if (writeit(stderr, msgverb(getenv("MSGVERB")), + label, sevstr, text, action, tag) < 0) + result |= MM_NOMSG; + } + /* Similar to MM_PRINT but ignoring $MSGVERB. */ + if (classification & MM_CONSOLE) { + if ((console = fopen(_PATH_CONSOLE, "w")) != NULL) { + if (writeit(console, MM_VERBALL, + label, sevstr, text, action, tag) < 0) + result |= MM_NOCON; + /* + * Ignore result: does not constitute ``generate a + * console message.'' + */ + (void)fclose(console); + } else { + result |= MM_NOCON; + } + } + + if (result == (MM_NOMSG | MM_NOCON)) + result = MM_NOTOK; + + return (result == 0 ? MM_OK : result); +} diff --git a/lib/nbsd_libc/gen/fnmatch.3 b/lib/nbsd_libc/gen/fnmatch.3 new file mode 100644 index 000000000..e14688691 --- /dev/null +++ b/lib/nbsd_libc/gen/fnmatch.3 @@ -0,0 +1,140 @@ +.\" $NetBSD: fnmatch.3,v 1.22 2010/11/30 21:03:07 jruoho Exp $ +.\" +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Guido van Rossum. +.\" 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. +.\" +.\" @(#)fnmatch.3 8.3 (Berkeley) 4/28/95 +.\" +.Dd November 30, 2010 +.Dt FNMATCH 3 +.Os +.Sh NAME +.Nm fnmatch +.Nd match filename or pathname using shell glob rules +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In fnmatch.h +.Ft int +.Fn fnmatch "const char *pattern" "const char *string" "int flags" +.Sh DESCRIPTION +The +.Fn fnmatch +function +matches patterns according to the globbing rules used by the shell. +It checks the string specified by the +.Fa string +argument to see if it matches the pattern specified by the +.Fa pattern +argument. +.Pp +The +.Fa flags +argument modifies the interpretation of +.Fa pattern +and +.Fa string . +The value of +.Fa flags +is the bitwise inclusive +.Tn OR +of any of the following +constants, which are defined in the include file +.Pa fnmatch.h . +.Bl -tag -width FNM_LEADING_DIRXX +.It Dv FNM_NOESCAPE +Normally, every occurrence of a backslash +.Pq Ql \e +followed by a character in +.Fa pattern +is replaced by that character. +This is done to negate any special meaning for the character. +If the +.Dv FNM_NOESCAPE +flag is set, a backslash character is treated as an ordinary character. +.It Dv FNM_PATHNAME +Slash characters in +.Fa string +must be explicitly matched by slashes in +.Fa pattern . +If this flag is not set, then slashes are treated as regular characters. +.It Dv FNM_PERIOD +Leading periods in strings match periods in patterns. +The definition of ``leading'' is related to the specification of +.Dv FNM_PATHNAME . +A period is always ``leading'' if it is the first character in +.Ar string . +Additionally, if +.Dv FNM_PATHNAME +is set, +a period is ``leading'' if it immediately follows a slash. +.It Dv FNM_LEADING_DIR +Ignore +.Dq /* +rest after successful +.Fa pattern +matching. +.It Dv FNM_CASEFOLD +The pattern is matched in a case-insensitive fashion. +.El +.Sh RETURN VALUES +The +.Fn fnmatch +function returns zero if +.Fa string +matches the pattern specified by +.Fa pattern , +otherwise, it returns the value +.Dv FNM_NOMATCH . +.Sh SEE ALSO +.Xr sh 1 , +.Xr glob 3 , +.Xr regex 3 , +.Xr glob 7 +.Sh STANDARDS +The +.Fn fnmatch +function conforms to +.St -p1003.2-92 . +The +.Dv FNM_CASEFOLD +flag is a +.Nx +extension. +.Sh HISTORY +The +.Fn fnmatch +function first appeared in +.Bx 4.4 . +.Sh BUGS +The pattern +.Ql * +matches the empty string, even if +.Dv FNM_PATHNAME +is specified. diff --git a/lib/nbsd_libc/gen/fnmatch.c b/lib/nbsd_libc/gen/fnmatch.c new file mode 100644 index 000000000..556cae7bc --- /dev/null +++ b/lib/nbsd_libc/gen/fnmatch.c @@ -0,0 +1,204 @@ +/* $NetBSD: fnmatch.c,v 1.21 2005/12/24 21:11:16 perry Exp $ */ + +/* + * Copyright (c) 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Guido van Rossum. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; +#else +__RCSID("$NetBSD: fnmatch.c,v 1.21 2005/12/24 21:11:16 perry Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. + * Compares a filename or pathname to a pattern. + */ + +#include "namespace.h" + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(fnmatch,_fnmatch) +#endif + +#define EOS '\0' + +static const char *rangematch __P((const char *, int, int)); + +static inline int +foldcase(int ch, int flags) +{ + + if ((flags & FNM_CASEFOLD) != 0 && isupper(ch)) + return (tolower(ch)); + return (ch); +} + +#define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags)) + +int +fnmatch(pattern, string, flags) + const char *pattern, *string; + int flags; +{ + const char *stringstart; + char c, test; + + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(string != NULL); + + for (stringstart = string;;) + switch (c = FOLDCASE(*pattern++, flags)) { + case EOS: + if ((flags & FNM_LEADING_DIR) && *string == '/') + return (0); + return (*string == EOS ? 0 : FNM_NOMATCH); + case '?': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && (flags & FNM_PATHNAME)) + return (FNM_NOMATCH); + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + ++string; + break; + case '*': + c = FOLDCASE(*pattern, flags); + /* Collapse multiple stars. */ + while (c == '*') + c = FOLDCASE(*++pattern, flags); + + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + + /* Optimize for pattern with * at end or before /. */ + if (c == EOS) { + if (flags & FNM_PATHNAME) + return ((flags & FNM_LEADING_DIR) || + strchr(string, '/') == NULL ? + 0 : FNM_NOMATCH); + else + return (0); + } else if (c == '/' && flags & FNM_PATHNAME) { + if ((string = strchr(string, '/')) == NULL) + return (FNM_NOMATCH); + break; + } + + /* General case, use recursion. */ + while ((test = FOLDCASE(*string, flags)) != EOS) { + if (!fnmatch(pattern, string, + flags & ~FNM_PERIOD)) + return (0); + if (test == '/' && flags & FNM_PATHNAME) + break; + ++string; + } + return (FNM_NOMATCH); + case '[': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && flags & FNM_PATHNAME) + return (FNM_NOMATCH); + if ((pattern = + rangematch(pattern, FOLDCASE(*string, flags), + flags)) == NULL) + return (FNM_NOMATCH); + ++string; + break; + case '\\': + if (!(flags & FNM_NOESCAPE)) { + if ((c = FOLDCASE(*pattern++, flags)) == EOS) { + c = '\\'; + --pattern; + } + } + /* FALLTHROUGH */ + default: + if (c != FOLDCASE(*string++, flags)) + return (FNM_NOMATCH); + break; + } + /* NOTREACHED */ +} + +static const char * +rangematch(pattern, test, flags) + const char *pattern; + int test, flags; +{ + int negate, ok; + char c, c2; + + _DIAGASSERT(pattern != NULL); + + /* + * A bracket expression starting with an unquoted circumflex + * character produces unspecified results (IEEE 1003.2-1992, + * 3.13.2). This implementation treats it like '!', for + * consistency with the regular expression syntax. + * J.T. Conklin (conklin@ngai.kaleida.com) + */ + if ((negate = (*pattern == '!' || *pattern == '^')) != 0) + ++pattern; + + for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']';) { + if (c == '\\' && !(flags & FNM_NOESCAPE)) + c = FOLDCASE(*pattern++, flags); + if (c == EOS) + return (NULL); + if (*pattern == '-' + && (c2 = FOLDCASE(*(pattern+1), flags)) != EOS && + c2 != ']') { + pattern += 2; + if (c2 == '\\' && !(flags & FNM_NOESCAPE)) + c2 = FOLDCASE(*pattern++, flags); + if (c2 == EOS) + return (NULL); + if (c <= test && test <= c2) + ok = 1; + } else if (c == test) + ok = 1; + } + return (ok == negate ? NULL : pattern); +} diff --git a/lib/nbsd_libc/gen/fpclassify.3 b/lib/nbsd_libc/gen/fpclassify.3 new file mode 100644 index 000000000..c8dcc1ba5 --- /dev/null +++ b/lib/nbsd_libc/gen/fpclassify.3 @@ -0,0 +1,94 @@ +.\" $NetBSD: fpclassify.3,v 1.3 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2003 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 January 14, 2004 +.Dt FPCLASSIFY 3 +.Os +.Sh NAME +.Nm fpclassify +.Nd classify real floating type +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In math.h +.Ft int +.Fn fpclassify "real-floating x" +.Sh DESCRIPTION +The +.Fn fpclassify +macro performs classification of its argument +.Fa x . +An argument represented in a format wider than its semantic type is +converted to its semantic type first. +The classification is then based on the type of the argument. +.Ss IEEE 754 +.Bl -tag -width "FP_SUBNORMALXXX" -compact -offset indent +.It Dv FP_INFINITE +infinity, either positive or negative +.It Dv FP_NAN +not-a-number +.Pq Dq NaN +.It Dv FP_NORMAL +normal +.It Dv FP_SUBNORMAL +subnormal +.It Dv FP_ZERO +zero +.El +.Ss VAX +.Bl -tag -width "FP_DIRTYZEROXXX" -compact -offset indent +.It Dv FP_ROP +reserved operand +.Pq Dq ROP +.It Dv FP_DIRTYZERO +dirty zero +.It Dv FP_NORMAL +finite +.It Dv FP_ZERO +true zero +.El +.Sh RETURN VALUES +The +.Fn fpclassify +macro returns the value of the number classification macro appropriate +to its argument +.Fa x +as described above. +.Sh ERRORS +No errors are defined. +.Sh SEE ALSO +.Xr isfinite 3 , +.Xr isnormal 3 , +.Xr math 3 , +.Xr signbit 3 +.Sh STANDARDS +The +.Fn fpclassify +macro conforms to +.St -isoC-99 . diff --git a/lib/nbsd_libc/gen/fpclassifyd_ieee754.c b/lib/nbsd_libc/gen/fpclassifyd_ieee754.c new file mode 100644 index 000000000..ecce33194 --- /dev/null +++ b/lib/nbsd_libc/gen/fpclassifyd_ieee754.c @@ -0,0 +1,64 @@ +/* $NetBSD: fpclassifyd_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyd_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * IEEE 754 double-precision version + */ +int +__fpclassifyd(double x) +{ + union ieee_double_u u; + + u.dblu_d = x; + + if (u.dblu_dbl.dbl_exp == 0) { + if (u.dblu_dbl.dbl_frach == 0 && u.dblu_dbl.dbl_fracl == 0) + return FP_ZERO; + else + return FP_SUBNORMAL; + } else if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN) { + if (u.dblu_dbl.dbl_frach == 0 && u.dblu_dbl.dbl_fracl == 0) + return FP_INFINITE; + else + return FP_NAN; + } + + return FP_NORMAL; +} diff --git a/lib/nbsd_libc/gen/fpclassifyf_ieee754.c b/lib/nbsd_libc/gen/fpclassifyf_ieee754.c new file mode 100644 index 000000000..2e522d5f1 --- /dev/null +++ b/lib/nbsd_libc/gen/fpclassifyf_ieee754.c @@ -0,0 +1,64 @@ +/* $NetBSD: fpclassifyf_ieee754.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyf_ieee754.c,v 1.3 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.1 fpclassify - classify real floating type + * IEEE 754 single-precision version + */ +int +__fpclassifyf(float x) +{ + union ieee_single_u u; + + u.sngu_f = x; + + if (u.sngu_sng.sng_exp == 0) { + if (u.sngu_sng.sng_frac == 0) + return FP_ZERO; + else + return FP_SUBNORMAL; + } else if (u.sngu_sng.sng_exp == SNG_EXP_INFNAN) { + if (u.sngu_sng.sng_frac == 0) + return FP_INFINITE; + else + return FP_NAN; + } + + return FP_NORMAL; +} diff --git a/lib/nbsd_libc/gen/fpclassifyl_ieee754.c b/lib/nbsd_libc/gen/fpclassifyl_ieee754.c new file mode 100644 index 000000000..f523573c8 --- /dev/null +++ b/lib/nbsd_libc/gen/fpclassifyl_ieee754.c @@ -0,0 +1,68 @@ +/* $NetBSD: fpclassifyl_ieee754.c,v 1.1 2011/01/17 23:53:03 matt Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fpclassifyl_ieee754.c,v 1.1 2011/01/17 23:53:03 matt Exp $"); +#endif + +#include +#include + +#ifdef __HAVE_LONG_DOUBLE +/* + * 7.12.3.1 fpclassify - classify real floating type + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__fpclassifyl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + if (u.extu_ext.ext_exp == 0) { + if (u.extu_ext.ext_frach == 0 && u.extu_ext.ext_frachm == 0 + && u.extu_ext.ext_fraclm == 0 && u.extu_ext.ext_fracl == 0) + return FP_ZERO; + else + return FP_SUBNORMAL; + } else if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) { + if (u.extu_ext.ext_frach == 0 && u.extu_ext.ext_frachm == 0 + && u.extu_ext.ext_fraclm == 0 && u.extu_ext.ext_fracl == 0) + return FP_INFINITE; + else + return FP_NAN; + } + + return FP_NORMAL; +} +#endif /* __HAVE_LONG_DOUBLE */ diff --git a/lib/nbsd_libc/gen/fpgetmask.3 b/lib/nbsd_libc/gen/fpgetmask.3 new file mode 100644 index 000000000..04b27ff1f --- /dev/null +++ b/lib/nbsd_libc/gen/fpgetmask.3 @@ -0,0 +1,140 @@ +.\" $NetBSD: fpgetmask.3,v 1.10 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 1999 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Ross Harvey. +.\" +.\" 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 April 4, 2004 +.Dt FPGETMASK 3 +.Os +.Sh NAME +.Nm fpgetmask , +.Nm fpgetround , +.Nm fpgetsticky , +.Nm fpsetmask , +.Nm fpsetround , +.Nm fpsetsticky +.Nd IEEE FP mode control +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ieeefp.h +.Ft fp_except +.Fn fpgetmask void +.Ft fp_rnd +.Fn fpgetround void +.Ft fp_except +.Fn fpgetsticky void +.Ft fp_except +.Fn fpsetmask fp_except\ mask +.Ft fp_rnd +.Fn fpsetround fp_rnd\ rnd_dir +.Ft fp_except +.Fn fpsetsticky fp_except\ sticky +.Sh DESCRIPTION +A rounding mode is one of +.Dv FP_RZ , FP_RM , FP_RN , +or +.Dv FP_RP , +for rounding towards zero, rounding +.Pq Em Minus infinity +down, rounding to +.Em nearest , +and rounding +.Pq Em Plus infinity +up. +The default mode is +.Dv FP_RN . +.Pp +An +.Ft fp_except +value is a bitmask specifying an exception type and containing any of +the values listed below. +.Bl -column -offset indent FP_X_UFLxx +.It Dv FP_X_INV Ta Invalid\ Operation +.It Dv FP_X_DZ Ta Division\ by\ zero +.It Dv FP_X_OFL Ta Overflow +.It Dv FP_X_UFL Ta Underflow +.It Dv FP_X_IMP Ta Imprecision (inexact) +.It Dv FP_X_IOV Ta Integer\ Overflow +.El +.Pp +The +.Fn fpsetmask +function will set the current exception mask, i.e., it will cause +future operations with the specified result status to raise the +.Dv SIGFPE +exception. +The +.Fn fpgetmask +function will return the current exception mask. +.Pp +The +.Fn fpsetround +function will cause future operations to use the specified dynamic +rounding mode. +The +.Fn fpgetround +function will return the current rounding mode. +.Bl -tag -width Note:x +.It Em Note : +On some architectures, instructions can optionally specify static +rounding modes and exception enables that will supersede the specified +dynamic mode. +On other architectures, these features may not be fully supported. +.El +.Pp +A +.Dq sticky +status word may be maintained in which a bit is set every time an +exceptional floating point condition is encountered, whether or not a +.Dv SIGFPE +is generated. +The +.Fn fpsetsticky +function will set or clear the specified exception history bits. +The +.Fn fpgetsticky +function will return the exception history bits. +.Sh RETURN VALUES +The +.Fn fpgetround +and +.Fn fpsetround +functions return the +.Pq previous +rounding mode. +The +.Fn fpgetmask , +.Fn fpsetmask , +.Fn fpgetsticky , +and +.Fn fpsetsticky +functions return the +.Pq previous +exception mask and exception history bits. +.Sh SEE ALSO +.Xr sigaction 2 diff --git a/lib/nbsd_libc/gen/fstab.c b/lib/nbsd_libc/gen/fstab.c new file mode 100644 index 000000000..bd8305e59 --- /dev/null +++ b/lib/nbsd_libc/gen/fstab.c @@ -0,0 +1,233 @@ +/* $NetBSD: fstab.c,v 1.28 2006/08/12 23:49:54 christos Exp $ */ + +/* + * Copyright (c) 1980, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: fstab.c,v 1.28 2006/08/12 23:49:54 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(endfsent,_endfsent) +__weak_alias(getfsent,_getfsent) +__weak_alias(getfsfile,_getfsfile) +__weak_alias(getfsspec,_getfsspec) +__weak_alias(setfsent,_setfsent) +#endif + +static FILE *_fs_fp; +static size_t _fs_lineno = 0; +static const char *_fs_file = _PATH_FSTAB; +static struct fstab _fs_fstab; + +static int fstabscan __P((void)); + +static char *nextfld(char **, const char *); +static int fstabscan(void); + + +static char * +nextfld(char **str, const char *sep) +{ + char *ret; + + _DIAGASSERT(str != NULL); + _DIAGASSERT(sep != NULL); + + while ((ret = stresep(str, sep, '\\')) != NULL && *ret == '\0') + continue; + return ret; +} + + +static int +fstabscan(void) +{ + char *cp, *lp, *sp; +#define MAXLINELENGTH 1024 + static char line[MAXLINELENGTH]; + char subline[MAXLINELENGTH]; + static const char sep[] = ":\n"; + static const char ws[] = " \t\n"; + static const char *fstab_type[] = { + FSTAB_RW, FSTAB_RQ, FSTAB_RO, FSTAB_SW, FSTAB_DP, FSTAB_XX, NULL + }; + + (void)memset(&_fs_fstab, 0, sizeof(_fs_fstab)); + for (;;) { + if (!(lp = fgets(line, sizeof(line), _fs_fp))) + return 0; + _fs_lineno++; +/* OLD_STYLE_FSTAB */ + if (!strpbrk(lp, " \t")) { + _fs_fstab.fs_spec = nextfld(&lp, sep); + if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') + continue; + _fs_fstab.fs_file = nextfld(&lp, sep); + _fs_fstab.fs_type = nextfld(&lp, sep); + if (_fs_fstab.fs_type) { + if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) + continue; + _fs_fstab.fs_mntops = _fs_fstab.fs_type; + _fs_fstab.fs_vfstype = + __UNCONST( + strcmp(_fs_fstab.fs_type, FSTAB_SW) ? + "ufs" : "swap"); + if ((cp = nextfld(&lp, sep)) != NULL) { + _fs_fstab.fs_freq = atoi(cp); + if ((cp = nextfld(&lp, sep)) != NULL) { + _fs_fstab.fs_passno = atoi(cp); + return 1; + } + } + } + goto bad; + } +/* OLD_STYLE_FSTAB */ + _fs_fstab.fs_spec = nextfld(&lp, ws); + if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') + continue; + _fs_fstab.fs_file = nextfld(&lp, ws); + _fs_fstab.fs_vfstype = nextfld(&lp, ws); + _fs_fstab.fs_mntops = nextfld(&lp, ws); + if (_fs_fstab.fs_mntops == NULL) + goto bad; + _fs_fstab.fs_freq = 0; + _fs_fstab.fs_passno = 0; + if ((cp = nextfld(&lp, ws)) != NULL) { + _fs_fstab.fs_freq = atoi(cp); + if ((cp = nextfld(&lp, ws)) != NULL) + _fs_fstab.fs_passno = atoi(cp); + } + + /* subline truncated iff line truncated */ + (void)strlcpy(subline, _fs_fstab.fs_mntops, sizeof(subline)); + sp = subline; + + while ((cp = nextfld(&sp, ",")) != NULL) { + const char **tp; + + if (strlen(cp) != 2) + continue; + + for (tp = fstab_type; *tp; tp++) + if (strcmp(cp, *tp) == 0) { + _fs_fstab.fs_type = __UNCONST(*tp); + break; + } + if (*tp) + break; + } + if (_fs_fstab.fs_type == NULL) + goto bad; + if (strcmp(_fs_fstab.fs_type, FSTAB_XX) == 0) + continue; + if (cp != NULL) + return 1; + +bad: + warnx("%s, %lu: Missing fields", _fs_file, (u_long)_fs_lineno); + } + /* NOTREACHED */ +} + +struct fstab * +getfsent(void) +{ + if ((!_fs_fp && !setfsent()) || !fstabscan()) + return NULL; + return &_fs_fstab; +} + +struct fstab * +getfsspec(const char *name) +{ + + _DIAGASSERT(name != NULL); + + if (setfsent()) + while (fstabscan()) + if (!strcmp(_fs_fstab.fs_spec, name)) + return &_fs_fstab; + return NULL; +} + +struct fstab * +getfsfile(const char *name) +{ + + _DIAGASSERT(name != NULL); + + if (setfsent()) + while (fstabscan()) + if (!strcmp(_fs_fstab.fs_file, name)) + return &_fs_fstab; + return NULL; +} + +int +setfsent(void) +{ + _fs_lineno = 0; + if (_fs_fp) { + rewind(_fs_fp); + return 1; + } + if ((_fs_fp = fopen(_PATH_FSTAB, "r")) == NULL) { + warn("Cannot open `%s'", _PATH_FSTAB); + return 0; + } + return 1; +} + +void +endfsent(void) +{ + if (_fs_fp) { + (void)fclose(_fs_fp); + _fs_fp = NULL; + } +} diff --git a/lib/nbsd_libc/gen/ftok.3 b/lib/nbsd_libc/gen/ftok.3 new file mode 100644 index 000000000..8bab7b4a3 --- /dev/null +++ b/lib/nbsd_libc/gen/ftok.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: ftok.3,v 1.16 2010/04/27 15:26:59 jruoho Exp $ +.\" +.\" Copyright (c) 1994 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 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 April 27, 2010 +.Dt FTOK 3 +.Os +.Sh NAME +.Nm ftok +.Nd create IPC identifier from path name +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/ipc.h +.Ft key_t +.Fn ftok "const char *path" "int id" +.Sh DESCRIPTION +The +.Fn ftok +function attempts to create a unique key suitable for use with the +.Xr msgget 2 , +.Xr semget 2 +and +.Xr shmget 2 +functions given the +.Fa path +of an existing file and a user-selectable +.Fa id . +.Pp +The specified +.Fa path +must specify an existing file that is accessible to the calling process +or the call will fail. +Also, note that links to files will return the same key, given the same +.Fa id . +.Pp +Only the 8 least significant bits of +.Fa id +are used in the key generation; the rest of the bits are ignored. +.Sh RETURN VALUES +The +.Fn ftok +function will return ((key_t)-1) if +.Fa path +does not exist or if it cannot be accessed by the calling process. +.Sh SEE ALSO +.Xr msgget 2 , +.Xr semget 2 , +.Xr shmget 2 +.Sh STANDARDS +The +.Fn ftok +function conforms to +.St -p1003.1 . +.Sh HISTORY +The +.Fn ftok +function originated with System V and is typically used by programs +that use the System V IPC routines. +.Sh AUTHORS +.An Thorsten Lockert Aq tholo@sigmasoft.com +.Sh BUGS +The returned key is computed based on the device and inode of the +specified +.Fa path +in combination with the given +.Fa id . +Thus it is quite possible for the routine to return duplicate keys +given that those fields are not 8- and 16-bit quantities like they +were on System V based systems where this library routine's ancestor +were originally created. diff --git a/lib/nbsd_libc/gen/ftok.c b/lib/nbsd_libc/gen/ftok.c new file mode 100644 index 000000000..d50acc63c --- /dev/null +++ b/lib/nbsd_libc/gen/ftok.c @@ -0,0 +1,59 @@ +/* $NetBSD: ftok.c,v 1.10 2004/09/15 19:45:17 hubertf Exp $ */ + +/* + * Copyright (c) 1994 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ftok.c,v 1.10 2004/09/15 19:45:17 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include + +#ifdef __weak_alias +__weak_alias(ftok,_ftok) +#endif + +key_t +ftok(path, id) + const char *path; + int id; +{ + struct stat st; + + _DIAGASSERT(path != NULL); + + if (stat(path, &st) < 0) + return (key_t)-1; + + return (key_t) + (id << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff)); +} diff --git a/lib/nbsd_libc/gen/fts.3 b/lib/nbsd_libc/gen/fts.3 new file mode 100644 index 000000000..596c0654d --- /dev/null +++ b/lib/nbsd_libc/gen/fts.3 @@ -0,0 +1,773 @@ +.\" $NetBSD: fts.3,v 1.29 2010/04/29 17:39:03 jruoho Exp $ +.\" +.\" Copyright (c) 1989, 1991, 1993, 1994 +.\" 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. +.\" +.\" @(#)fts.3 8.5 (Berkeley) 4/16/94 +.\" +.Dd April 29, 2010 +.Dt FTS 3 +.Os +.Sh NAME +.Nm fts , +.Nm fts_open , +.Nm fts_read , +.Nm fts_children , +.Nm fts_set , +.Nm fts_close +.Nd traverse a file hierarchy +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/stat.h +.In fts.h +.Ft FTS * +.Fo fts_open +.Fa "char * const *path_argv" +.Fa "int options" +.Fa "int (*compar)(const FTSENT **, const FTSENT **)" +.Fc +.Ft FTSENT * +.Fn fts_read "FTS *ftsp" +.Ft FTSENT * +.Fn fts_children "FTS *ftsp" "int options" +.Ft int +.Fn fts_set "FTS *ftsp" "FTSENT *f" "int options" +.Ft int +.Fn fts_close "FTS *ftsp" +.Sh DESCRIPTION +The +.Nm +functions are provided for traversing +.Ux +file hierarchies. +A simple overview is that the +.Fn fts_open +function returns a +.Dq handle +on a file hierarchy, which is then supplied to +the other +.Nm +functions. +The function +.Fn fts_read +returns a pointer to a structure describing one of the files in the file +hierarchy. +The function +.Fn fts_children +returns a pointer to a linked list of structures, each of which describes +one of the files contained in a directory in the hierarchy. +In general, directories are visited two distinguishable times; in pre-order +(before any of their descendants are visited) and in post-order (after all +of their descendants have been visited). +Files are visited once. +It is possible to walk the hierarchy +.Dq logically +(ignoring symbolic links) +or physically (visiting symbolic links), order the walk of the hierarchy or +prune and/or re-visit portions of the hierarchy. +.Pp +Two structures are defined (and typedef'd) in the include file +.In fts.h . +The first is +.Fa FTS , +the structure that represents the file hierarchy itself. +The second is +.Fa FTSENT , +the structure that represents a file in the file +hierarchy. +Normally, an +.Fa FTSENT +structure is returned for every file in the file +hierarchy. +In this manual page, +.Dq file +and +.Dq Fa FTSENT No structure +are generally +interchangeable. +The +.Fa FTSENT +structure contains at least the following fields, which are +described in greater detail below: +.Bd -literal +typedef struct _ftsent { + u_short fts_info; /* flags for FTSENT structure */ + char *fts_accpath; /* access path */ + char *fts_path; /* root path */ + short fts_pathlen; /* strlen(fts_path) */ + char *fts_name; /* file name */ + short fts_namelen; /* strlen(fts_name) */ + short fts_level; /* depth (\-1 to N) */ + int fts_errno; /* file errno */ + long fts_number; /* local numeric value */ + void *fts_pointer; /* local address value */ + struct ftsent *fts_parent; /* parent directory */ + struct ftsent *fts_link; /* next file structure */ + struct ftsent *fts_cycle; /* cycle structure */ + struct stat *fts_statp; /* stat(2) information */ +} FTSENT; +.Ed +.Pp +These fields are defined as follows: +.Bl -tag -width "fts_namelen" +.It Fa fts_info +One of the following flags describing the returned +.Fa FTSENT +structure and +the file it represents. +With the exception of directories without errors +.Pq Dv FTS_D , +all of these +entries are terminal, that is, they will not be revisited, nor will any +of their descendants be visited. +.Bl -tag -width FTS_DEFAULT +.It Dv FTS_D +A directory being visited in pre-order. +.It Dv FTS_DC +A directory that causes a cycle in the tree. +(The +.Fa fts_cycle +field of the +.Fa FTSENT +structure will be filled in as well). +.It Dv FTS_DEFAULT +Any +.Fa FTSENT +structure that represents a file type not explicitly described +by one of the other +.Fa fts_info +values. +.It Dv FTS_DNR +A directory which cannot be read. +This is an error return, and the +.Fa fts_errno +field will be set to indicate what caused the error. +.It Dv FTS_DOT +A file named +.Ql \&. +or +.Ql .. +which was not specified as a file name to +.Fn fts_open +(see +.Dv FTS_SEEDOT ) . +.It Dv FTS_DP +A directory being visited in post-order. +The contents of the +.Fa FTSENT +structure will be unchanged from when +it was returned in pre-order, i.e., with the +.Fa fts_info +field set to +.Dv FTS_D . +.It Dv FTS_ERR +This is an error return, and the +.Fa fts_errno +field will be set to indicate what caused the error. +.It Dv FTS_F +A regular file. +.It Dv FTS_NS +A file for which no +.Xr stat 2 +information was available. +The contents of the +.Fa fts_statp +field are undefined. +This is an error return, and the +.Fa fts_errno +field will be set to indicate what caused the error. +.It Dv FTS_NSOK +A file for which no +.Xr stat 2 +information was requested. +The contents of the +.Fa fts_statp +field are undefined. +.It Dv FTS_SL +A symbolic link. +.It Dv FTS_SLNONE +A symbolic link with a non-existent target. +The contents of the +.Fa fts_statp +field reference the file characteristic information for the symbolic link +itself. +.It Dv FTS_W +A whiteout object. +.El +.It Fa fts_accpath +A path for accessing the file from the current directory. +.It Fa fts_path +The path for the file relative to the root of the traversal. +This path contains the path specified to +.Fn fts_open +as a prefix. +.It Fa fts_pathlen +The length of the string referenced by +.Fa fts_path . +.It Fa fts_name +The name of the file. +.It Fa fts_namelen +The length of the string referenced by +.Fa fts_name . +.It Fa fts_level +The depth of the traversal, numbered from \-1 to N, where this file +was found. +The +.Fa FTSENT +structure representing the parent of the starting point (or root) +of the traversal is numbered \-1, and the +.Fa FTSENT +structure for the root +itself is numbered 0. +.It Fa fts_errno +Upon return of a +.Fa FTSENT +structure from the +.Fn fts_children +or +.Fn fts_read +functions, with its +.Fa fts_info +field set to +.Dv FTS_DNR , +.Dv FTS_ERR +or +.Dv FTS_NS , +the +.Fa fts_errno +field contains the value of the external variable +.Va errno +specifying the cause of the error. +Otherwise, the contents of the +.Fa fts_errno +field are undefined. +.It Fa fts_number +This field is provided for the use of the application program and is +not modified by the +.Nm +functions. +It is initialized to 0. +.It Fa fts_pointer +This field is provided for the use of the application program and is +not modified by the +.Nm +functions. +It is initialized to +.Dv NULL . +.It Fa fts_parent +A pointer to the +.Fa FTSENT +structure referencing the file in the hierarchy +immediately above the current file, i.e., the directory of which this +file is a member. +A parent structure for the initial entry point is provided as well, +however, only the +.Fa fts_level , +.Fa fts_number +and +.Fa fts_pointer +fields are guaranteed to be initialized. +.It Fa fts_link +Upon return from the +.Fn fts_children +function, the +.Fa fts_link +field points to the next structure in the +.Dv NULL Ns -terminated +linked list of directory members. +Otherwise, the contents of the +.Fa fts_link +field are undefined. +.It Fa fts_cycle +If a directory causes a cycle in the hierarchy (see +.Dv FTS_DC ) , +either because +of a hard link between two directories, or a symbolic link pointing to a +directory, the +.Fa fts_cycle +field of the structure will point to the +.Fa FTSENT +structure in the hierarchy that references the same file as the current +.Fa FTSENT +structure. +Otherwise, the contents of the +.Fa fts_cycle +field are undefined. +.It Fa fts_statp +A pointer to +.Xr stat 2 +information for the file. +.El +.Pp +A single buffer is used for all of the paths of all of the files in the +file hierarchy. +Therefore, the +.Fa fts_path +and +.Fa fts_accpath +fields are guaranteed to be +.Dv NULL Ns -terminated +.Em only +for the file most recently returned by +.Fn fts_read . +To use these fields to reference any files represented by other +.Fa FTSENT +structures will require that the path buffer be modified using the +information contained in that +.Fa FTSENT +structure's +.Fa fts_pathlen +field. +Any such modifications should be undone before further calls to +.Fn fts_read +are attempted. +The +.Fa fts_name +field is always +.Dv NULL Ns -terminated . +.Sh FTS_OPEN +The +.Fn fts_open +function takes a pointer to an array of character pointers naming one +or more paths which make up a logical file hierarchy to be traversed. +The array must be terminated by a +.Dv NULL +pointer. +.Pp +There are +a number of options, at least one of which (either +.Dv FTS_LOGICAL +or +.Dv FTS_PHYSICAL ) +must be specified. +The options are selected by +.Em or Ns 'ing +the following values: +.Bl -tag -width "FTS_PHYSICAL" +.It Dv FTS_COMFOLLOW +This option causes any symbolic link specified as a root path to be +followed immediately whether or not +.Dv FTS_LOGICAL +is also specified. +.It Dv FTS_LOGICAL +This option causes the +.Nm +routines to return +.Fa FTSENT +structures for the targets of symbolic links +instead of the symbolic links themselves. +If this option is set, the only symbolic links for which +.Fa FTSENT +structures +are returned to the application are those referencing non-existent files. +Either +.Dv FTS_LOGICAL +or +.Dv FTS_PHYSICAL +.Em must +be provided to the +.Fn fts_open +function. +.It Dv FTS_NOCHDIR +As a performance optimization, the +.Nm +functions change directories as they walk the file hierarchy. +This has the side-effect that an application cannot rely on being +in any particular directory during the traversal. +The +.Dv FTS_NOCHDIR +option turns off this optimization, and the +.Nm +functions will not change the current directory. +Note that applications should not themselves change their current directory +and try to access files unless +.Dv FTS_NOCHDIR +is specified and absolute +pathnames were provided as arguments to +.Fn fts_open . +.It Dv FTS_NOSTAT +By default, returned +.Fa FTSENT +structures reference file characteristic information (the +.Fa statp +field) for each file visited. +This option relaxes that requirement as a performance optimization, +allowing the +.Nm +functions to set the +.Fa fts_info +field to +.Dv FTS_NSOK +and leave the contents of the +.Fa statp +field undefined. +.It Dv FTS_PHYSICAL +This option causes the +.Nm +routines to return +.Fa FTSENT +structures for symbolic links themselves instead +of the target files they point to. +If this option is set, +.Fa FTSENT +structures for all symbolic links in the +hierarchy are returned to the application. +Either +.Dv FTS_LOGICAL +or +.Dv FTS_PHYSICAL +.Em must +be provided to the +.Fn fts_open +function. +.It Dv FTS_SEEDOT +By default, unless they are specified as path arguments to +.Fn fts_open , +any files named +.Ql \&. +or +.Ql .. +encountered in the file hierarchy are ignored. +This option causes the +.Nm +routines to return +.Fa FTSENT +structures for them. +.It Dv FTS_WHITEOUT +Return whiteout entries, which are normally hidden. +.It Dv FTS_XDEV +This option prevents +.Nm +from descending into directories that have a different device number +than the file from which the descent began. +.El +.Pp +The argument +.Fn compar +specifies a user-defined function which may be used to order the traversal +of the hierarchy. +It +takes two pointers to pointers to +.Fa FTSENT +structures as arguments and +should return a negative value, zero, or a positive value to indicate +if the file referenced by its first argument comes before, in any order +with respect to, or after, the file referenced by its second argument. +The +.Fa fts_accpath , +.Fa fts_path +and +.Fa fts_pathlen +fields of the +.Fa FTSENT +structures may +.Em never +be used in this comparison. +If the +.Fa fts_info +field is set to +.Dv FTS_NS +or +.Dv FTS_NSOK , +the +.Fa fts_statp +field may not either. +If the +.Fn compar +argument is +.Dv NULL , +the directory traversal order is in the order listed in +.Fa path_argv +for the root paths, and in the order listed in the directory for +everything else. +.Sh FTS_READ +The +.Fn fts_read +function returns a pointer to an +.Fa FTSENT +structure describing a file in +the hierarchy. +Directories (that are readable and do not cause cycles) are visited at +least twice, once in pre-order and once in post-order. +All other files are visited at least once. +(Hard links between directories that do not cause cycles or symbolic +links to symbolic links may cause files to be visited more than once, +or directories more than twice.) +.Pp +If all the members of the hierarchy have been returned, +.Fn fts_read +returns +.Dv NULL +and sets the external variable +.Va errno +to 0. +If an error unrelated to a file in the hierarchy occurs, +.Fn fts_read +returns +.Dv NULL +and sets +.Va errno +appropriately. +If an error related to a returned file occurs, a pointer to an +.Fa FTSENT +structure is returned, and +.Va errno +may or may not have been set (see +.Fa fts_info ) . +.Pp +The +.Fa FTSENT +structures returned by +.Fn fts_read +may be overwritten after a call to +.Fn fts_close +on the same file hierarchy stream, or, after a call to +.Fn fts_read +on the same file hierarchy stream unless they represent a file of type +directory, in which case they will not be overwritten until after a call to +.Fn fts_read +after the +.Fa FTSENT +structure has been returned by the function +.Fn fts_read +in post-order. +.Sh FTS_CHILDREN +The +.Fn fts_children +function returns a pointer to an +.Fa FTSENT +structure describing the first entry in a +.Dv NULL Ns -terminated +linked list of the files in the directory represented by the +.Fa FTSENT +structure most recently returned by +.Fn fts_read . +The list is linked through the +.Fa fts_link +field of the +.Fa FTSENT +structure, and is ordered by the user-specified comparison function, if any. +Repeated calls to +.Fn fts_children +will recreate this linked list. +.Pp +As a special case, if +.Fn fts_read +has not yet been called for a hierarchy, +.Fn fts_children +will return a pointer to the files in the logical directory specified to +.Fn fts_open , +i.e., the arguments specified to +.Fn fts_open . +Otherwise, if the +.Fa FTSENT +structure most recently returned by +.Fn fts_read +is not a directory being visited in pre-order, +or the directory does not contain any files, +.Fn fts_children +returns +.Dv NULL +and sets +.Va errno +to zero. +If an error occurs, +.Fn fts_children +returns +.Dv NULL +and sets +.Va errno +appropriately. +.Pp +The +.Fa FTSENT +structures returned by +.Fn fts_children +may be overwritten after a call to +.Fn fts_children , +.Fn fts_close +or +.Fn fts_read +on the same file hierarchy stream. +.Pp +.Em Option +may be set to the following value: +.Bl -tag -width FTS_NAMEONLY +.It Dv FTS_NAMEONLY +Only the names of the files are needed. +The contents of all the fields in the returned linked list of structures +are undefined with the exception of the +.Fa fts_name +and +.Fa fts_namelen +fields. +.El +.Sh FTS_SET +The function +.Fn fts_set +allows the user application to determine further processing for the +file +.Fa f +of the stream +.Fa ftsp . +The +.Fn fts_set +function +returns 0 on success, and \-1 if an error occurs. +.Em Option +must be set to one of the following values: +.Bl -tag -width FTS_PHYSICAL +.It Dv FTS_AGAIN +Re-visit the file; any file type may be re-visited. +The next call to +.Fn fts_read +will return the referenced file. +The +.Fa fts_stat +and +.Fa fts_info +fields of the structure will be reinitialized at that time, +but no other fields will have been changed. +This option is meaningful only for the most recently returned +file from +.Fn fts_read . +Normal use is for post-order directory visits, where it causes the +directory to be re-visited (in both pre and post-order) as well as all +of its descendants. +.It Dv FTS_FOLLOW +The referenced file must be a symbolic link. +If the referenced file is the one most recently returned by +.Fn fts_read , +the next call to +.Fn fts_read +returns the file with the +.Fa fts_info +and +.Fa fts_statp +fields reinitialized to reflect the target of the symbolic link instead +of the symbolic link itself. +If the file is one of those most recently returned by +.Fn fts_children , +the +.Fa fts_info +and +.Fa fts_statp +fields of the structure, when returned by +.Fn fts_read , +will reflect the target of the symbolic link instead of the symbolic link +itself. +In either case, if the target of the symbolic link does not exist the +fields of the returned structure will be unchanged and the +.Fa fts_info +field will be set to +.Dv FTS_SLNONE . +.Pp +If the target of the link is a directory, the pre-order return, followed +by the return of all of its descendants, followed by a post-order return, +is done. +.It Dv FTS_SKIP +No descendants of this file are visited. +The file may be one of those most recently returned by either +.Fn fts_children +or +.Fn fts_read . +.El +.Sh FTS_CLOSE +The +.Fn fts_close +function closes a file hierarchy stream +.Fa ftsp +and restores the current directory to the directory from which +.Fn fts_open +was called to open +.Fa ftsp . +The +.Fn fts_close +function +returns 0 on success, and \-1 if an error occurs. +.Sh ERRORS +The function +.Fn fts_open +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr open 2 +and +.Xr malloc 3 . +.Pp +The function +.Fn fts_close +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr chdir 2 +and +.Xr close 2 . +.Pp +The functions +.Fn fts_read +and +.Fn fts_children +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr chdir 2 , +.Xr malloc 3 , +.Xr opendir 3 , +.Xr readdir 3 +and +.Xr stat 2 . +.Pp +In addition, +.Fn fts_children , +.Fn fts_open +and +.Fn fts_set +may fail and set +.Va errno +as follows: +.Bl -tag -width Er +.It Bq Er EINVAL +The options were invalid. +.El +.Sh SEE ALSO +.Xr find 1 , +.Xr chdir 2 , +.Xr stat 2 , +.Xr qsort 3 , +.Xr symlink 7 +.Sh STANDARDS +The +.Nm +utility was expected to be included in the +.St -p1003.1-88 +revision. +But twenty years later, it still was not included in the +.St -p1003.1-2008 +revision. diff --git a/lib/nbsd_libc/gen/fts.c b/lib/nbsd_libc/gen/fts.c new file mode 100644 index 000000000..1c3bc733e --- /dev/null +++ b/lib/nbsd_libc/gen/fts.c @@ -0,0 +1,1225 @@ +/* $NetBSD: fts.c,v 1.40 2009/11/02 17:17:34 stacktic Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; +#else +__RCSID("$NetBSD: fts.c,v 1.40 2009/11/02 17:17:34 stacktic Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#if ! HAVE_NBTOOL_CONFIG_H +#define HAVE_STRUCT_DIRENT_D_NAMLEN +#endif + +static FTSENT *fts_alloc(FTS *, const char *, size_t); +static FTSENT *fts_build(FTS *, int); +static void fts_free(FTSENT *); +static void fts_lfree(FTSENT *); +static void fts_load(FTS *, FTSENT *); +static size_t fts_maxarglen(char * const *); +static size_t fts_pow2(size_t); +static int fts_palloc(FTS *, size_t); +static void fts_padjust(FTS *, FTSENT *); +static FTSENT *fts_sort(FTS *, FTSENT *, size_t); +static unsigned short fts_stat(FTS *, FTSENT *, int); +static int fts_safe_changedir(const FTS *, const FTSENT *, int, + const char *); + +#if defined(ALIGNBYTES) && defined(ALIGN) +#define FTS_ALLOC_ALIGNED 1 +#else +#undef FTS_ALLOC_ALIGNED +#endif + +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) + +#define CLR(opt) (sp->fts_options &= ~(opt)) +#define ISSET(opt) (sp->fts_options & (opt)) +#define SET(opt) (sp->fts_options |= (opt)) + +#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) + +/* fts_build flags */ +#define BCHILD 1 /* fts_children */ +#define BNAMES 2 /* fts_children, names only */ +#define BREAD 3 /* fts_read */ + +#ifndef DTF_HIDEW +#undef FTS_WHITEOUT +#endif + +FTS * +fts_open(char * const *argv, int options, + int (*compar)(const FTSENT **, const FTSENT **)) +{ + FTS *sp; + FTSENT *p, *root; + size_t nitems; + FTSENT *parent, *tmp = NULL; /* pacify gcc */ + size_t len; + + _DIAGASSERT(argv != NULL); + + /* Options check. */ + if (options & ~FTS_OPTIONMASK) { + errno = EINVAL; + return (NULL); + } + + /* Allocate/initialize the stream */ + if ((sp = malloc((unsigned int)sizeof(FTS))) == NULL) + return (NULL); + memset(sp, 0, sizeof(FTS)); + sp->fts_compar = compar; + sp->fts_options = options; + + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ + if (ISSET(FTS_LOGICAL)) + SET(FTS_NOCHDIR); + + /* + * Start out with 1K of path space, and enough, in any case, + * to hold the user's paths. + */ + if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) + goto mem1; + + /* Allocate/initialize root's parent. */ + if ((parent = fts_alloc(sp, "", 0)) == NULL) + goto mem2; + parent->fts_level = FTS_ROOTPARENTLEVEL; + + /* Allocate/initialize root(s). */ + for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { + /* Don't allow zero-length paths. */ + if ((len = strlen(*argv)) == 0) { + errno = ENOENT; + goto mem3; + } + + if ((p = fts_alloc(sp, *argv, len)) == NULL) + goto mem3; + p->fts_level = FTS_ROOTLEVEL; + p->fts_parent = parent; + p->fts_accpath = p->fts_name; + p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); + + /* Command-line "." and ".." are real directories. */ + if (p->fts_info == FTS_DOT) + p->fts_info = FTS_D; + + /* + * If comparison routine supplied, traverse in sorted + * order; otherwise traverse in the order specified. + */ + if (compar) { + p->fts_link = root; + root = p; + } else { + p->fts_link = NULL; + if (root == NULL) + tmp = root = p; + else { + tmp->fts_link = p; + tmp = p; + } + } + } + if (compar && nitems > 1) + root = fts_sort(sp, root, nitems); + + /* + * Allocate a dummy pointer and make fts_read think that we've just + * finished the node before the root(s); set p->fts_info to FTS_INIT + * so that everything about the "current" node is ignored. + */ + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) + goto mem3; + sp->fts_cur->fts_link = root; + sp->fts_cur->fts_info = FTS_INIT; + + /* + * If using chdir(2), grab a file descriptor pointing to dot to insure + * that we can get back here; this could be avoided for some paths, + * but almost certainly not worth the effort. Slashes, symbolic links, + * and ".." are all fairly nasty problems. Note, if we can't get the + * descriptor we run anyway, just more slowly. + */ + if (!ISSET(FTS_NOCHDIR)) { + if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1) + SET(FTS_NOCHDIR); + else if (fcntl(sp->fts_rfd, F_SETFD, FD_CLOEXEC) == -1) { + close(sp->fts_rfd); + SET(FTS_NOCHDIR); + } + } + + if (nitems == 0) + fts_free(parent); + + return (sp); + +mem3: fts_lfree(root); + fts_free(parent); +mem2: free(sp->fts_path); +mem1: free(sp); + return (NULL); +} + +static void +fts_load(FTS *sp, FTSENT *p) +{ + size_t len; + char *cp; + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(p != NULL); + + /* + * Load the stream structure for the next traversal. Since we don't + * actually enter the directory until after the preorder visit, set + * the fts_accpath field specially so the chdir gets done to the right + * place and the user can access the first node. From fts_open it's + * known that the path will fit. + */ + len = p->fts_pathlen = p->fts_namelen; + memmove(sp->fts_path, p->fts_name, len + 1); + if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { + len = strlen(++cp); + memmove(p->fts_name, cp, len + 1); + p->fts_namelen = len; + } + p->fts_accpath = p->fts_path = sp->fts_path; + sp->fts_dev = p->fts_dev; +} + +int +fts_close(FTS *sp) +{ + FTSENT *freep, *p; + int saved_errno = 0; + + _DIAGASSERT(sp != NULL); + + /* + * This still works if we haven't read anything -- the dummy structure + * points to the root list, so we step through to the end of the root + * list which has a valid parent pointer. + */ + if (sp->fts_cur) { + if (sp->fts_cur->fts_flags & FTS_SYMFOLLOW) + (void)close(sp->fts_cur->fts_symfd); + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { + freep = p; + p = p->fts_link ? p->fts_link : p->fts_parent; + fts_free(freep); + } + fts_free(p); + } + + /* Free up child linked list, sort array, path buffer. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + if (sp->fts_array) + free(sp->fts_array); + free(sp->fts_path); + + /* Return to original directory, save errno if necessary. */ + if (!ISSET(FTS_NOCHDIR)) { + if (fchdir(sp->fts_rfd) == -1) + saved_errno = errno; + (void)close(sp->fts_rfd); + } + + /* Free up the stream pointer. */ + free(sp); + if (saved_errno) { + errno = saved_errno; + return -1; + } + + return 0; +} + +#if !defined(__FTS_COMPAT_TAILINGSLASH) + +/* + * Special case of "/" at the end of the path so that slashes aren't + * appended which would cause paths to be written as "....//foo". + */ +#define NAPPEND(p) \ + (p->fts_path[p->fts_pathlen - 1] == '/' \ + ? p->fts_pathlen - 1 : p->fts_pathlen) + +#else /* !defined(__FTS_COMPAT_TAILINGSLASH) */ + +/* + * compatibility with the old behaviour. + * + * Special case a root of "/" so that slashes aren't appended which would + * cause paths to be written as "//foo". + */ + +#define NAPPEND(p) \ + (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \ + p->fts_path[0] == '/' ? 0 : p->fts_pathlen) + +#endif /* !defined(__FTS_COMPAT_TAILINGSLASH) */ + +FTSENT * +fts_read(FTS *sp) +{ + FTSENT *p, *tmp; + int instr; + char *t; + int saved_errno; + + _DIAGASSERT(sp != NULL); + + /* If finished or unrecoverable error, return NULL. */ + if (sp->fts_cur == NULL || ISSET(FTS_STOP)) + return (NULL); + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* Save and zero out user instructions. */ + instr = p->fts_instr; + p->fts_instr = FTS_NOINSTR; + + /* Any type of file may be re-visited; re-stat and re-turn. */ + if (instr == FTS_AGAIN) { + p->fts_info = fts_stat(sp, p, 0); + return (p); + } + + /* + * Following a symlink -- SLNONE test allows application to see + * SLNONE and recover. If indirecting through a symlink, have + * keep a pointer to current location. If unable to get that + * pointer, follow fails. + */ + if (instr == FTS_FOLLOW && + (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + close(p->fts_symfd); + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + return (p); + } + + /* Directory in pre-order. */ + if (p->fts_info == FTS_D) { + /* If skipped or crossed mount point, do post-order visit. */ + if (instr == FTS_SKIP || + (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { + if (p->fts_flags & FTS_SYMFOLLOW) + (void)close(p->fts_symfd); + if (sp->fts_child) { + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + p->fts_info = FTS_DP; + return (p); + } + + /* Rebuild if only read the names and now traversing. */ + if (sp->fts_child && ISSET(FTS_NAMEONLY)) { + CLR(FTS_NAMEONLY); + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + + /* + * Cd to the subdirectory. + * + * If have already read and now fail to chdir, whack the list + * to make the names come out right, and set the parent errno + * so the application will eventually get an error condition. + * Set the FTS_DONTCHDIR flag so that when we logically change + * directories back to the parent we don't do a chdir. + * + * If haven't read do so. If the read fails, fts_build sets + * FTS_STOP or the fts_info field of the node. + */ + if (sp->fts_child) { + if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { + p->fts_errno = errno; + p->fts_flags |= FTS_DONTCHDIR; + for (p = sp->fts_child; p; p = p->fts_link) + p->fts_accpath = + p->fts_parent->fts_accpath; + } + } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { + if (ISSET(FTS_STOP)) + return (NULL); + return (p); + } + p = sp->fts_child; + sp->fts_child = NULL; + goto name; + } + + /* Move to the next node on this level. */ +next: tmp = p; + if ((p = p->fts_link) != NULL) { + fts_free(tmp); + + /* + * If reached the top, return to the original directory, and + * load the paths for the next root. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + fts_load(sp, p); + return (sp->fts_cur = p); + } + + /* + * User may have called fts_set on the node. If skipped, + * ignore. If followed, get a file descriptor so we can + * get back if necessary. + */ + if (p->fts_instr == FTS_SKIP) + goto next; + if (p->fts_instr == FTS_FOLLOW) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = + open(".", O_RDONLY, 0)) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + close(p->fts_symfd); + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + p->fts_instr = FTS_NOINSTR; + } + +name: t = sp->fts_path + NAPPEND(p->fts_parent); + *t++ = '/'; + memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1)); + return (sp->fts_cur = p); + } + + /* Move up to the parent node. */ + p = tmp->fts_parent; + fts_free(tmp); + + if (p->fts_level == FTS_ROOTPARENTLEVEL) { + /* + * Done; free everything up and set errno to 0 so the user + * can distinguish between error and EOF. + */ + fts_free(p); + errno = 0; + return (sp->fts_cur = NULL); + } + + /* Nul terminate the pathname. */ + sp->fts_path[p->fts_pathlen] = '\0'; + + /* + * Return to the parent directory. If at a root node or came through + * a symlink, go back through the file descriptor. Otherwise, cd up + * one directory. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + } else if (p->fts_flags & FTS_SYMFOLLOW) { + if (FCHDIR(sp, p->fts_symfd)) { + saved_errno = errno; + (void)close(p->fts_symfd); + errno = saved_errno; + SET(FTS_STOP); + return (NULL); + } + (void)close(p->fts_symfd); + } else if (!(p->fts_flags & FTS_DONTCHDIR) && + fts_safe_changedir(sp, p->fts_parent, -1, "..")) { + SET(FTS_STOP); + return (NULL); + } + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; + return (sp->fts_cur = p); +} + +/* + * Fts_set takes the stream as an argument although it's not used in this + * implementation; it would be necessary if anyone wanted to add global + * semantics to fts using fts_set. An error return is allowed for similar + * reasons. + */ +/* ARGSUSED */ +int +fts_set(FTS *sp, FTSENT *p, int instr) +{ + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(p != NULL); + + if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && + instr != FTS_NOINSTR && instr != FTS_SKIP) { + errno = EINVAL; + return (1); + } + p->fts_instr = instr; + return (0); +} + +FTSENT * +fts_children(FTS *sp, int instr) +{ + FTSENT *p; + int fd; + + _DIAGASSERT(sp != NULL); + + if (instr && instr != FTS_NAMEONLY) { + errno = EINVAL; + return (NULL); + } + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* + * Errno set to 0 so user can distinguish empty directory from + * an error. + */ + errno = 0; + + /* Fatal errors stop here. */ + if (ISSET(FTS_STOP)) + return (NULL); + + /* Return logical hierarchy of user's arguments. */ + if (p->fts_info == FTS_INIT) + return (p->fts_link); + + /* + * If not a directory being visited in pre-order, stop here. Could + * allow FTS_DNR, assuming the user has fixed the problem, but the + * same effect is available with FTS_AGAIN. + */ + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) + return (NULL); + + /* Free up any previous child list. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + + if (instr == FTS_NAMEONLY) { + SET(FTS_NAMEONLY); + instr = BNAMES; + } else + instr = BCHILD; + + /* + * If using chdir on a relative path and called BEFORE fts_read does + * its chdir to the root of a traversal, we can lose -- we need to + * chdir into the subdirectory, and we don't know where the current + * directory is, so we can't get back so that the upcoming chdir by + * fts_read will work. + */ + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || + ISSET(FTS_NOCHDIR)) + return (sp->fts_child = fts_build(sp, instr)); + + if ((fd = open(".", O_RDONLY, 0)) == -1) + return (sp->fts_child = NULL); + sp->fts_child = fts_build(sp, instr); + if (fchdir(fd)) { + (void)close(fd); + return (NULL); + } + (void)close(fd); + return (sp->fts_child); +} + +/* + * This is the tricky part -- do not casually change *anything* in here. The + * idea is to build the linked list of entries that are used by fts_children + * and fts_read. There are lots of special cases. + * + * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is + * set and it's a physical walk (so that symbolic links can't be directories), + * we can do things quickly. First, if it's a 4.4BSD file system, the type + * of the file is in the directory entry. Otherwise, we assume that the number + * of subdirectories in a node is equal to the number of links to the parent. + * The former skips all stat calls. The latter skips stat calls in any leaf + * directories and for any files after the subdirectories in the directory have + * been found, cutting the stat calls by about 2/3. + */ +static FTSENT * +fts_build(FTS *sp, int type) +{ + struct dirent *dp; + FTSENT *p, *head; + size_t nitems; + FTSENT *cur, *tail; + DIR *dirp; + void *oldaddr; + size_t dnamlen; + int cderrno, descend, level, nlinks, saved_errno, nostat, doadjust; + size_t len, maxlen; +#ifdef FTS_WHITEOUT + int oflag; +#endif + char *cp = NULL; /* pacify gcc */ + + _DIAGASSERT(sp != NULL); + + /* Set current node pointer. */ + cur = sp->fts_cur; + + /* + * Open the directory for reading. If this fails, we're done. + * If being called from fts_read, set the fts_info field. + */ +#ifdef FTS_WHITEOUT + if (ISSET(FTS_WHITEOUT)) + oflag = DTF_NODUP|DTF_REWIND; + else + oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; +#else +#define __opendir2(path, flag) opendir(path) +#endif + if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { + if (type == BREAD) { + cur->fts_info = FTS_DNR; + cur->fts_errno = errno; + } + return (NULL); + } + + /* + * Nlinks is the number of possible entries of type directory in the + * directory if we're cheating on stat calls, 0 if we're not doing + * any stat calls at all, -1 if we're doing stats on everything. + */ + if (type == BNAMES) { + nlinks = 0; + nostat = 1; + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); + nostat = 1; + } else { + nlinks = -1; + nostat = 0; + } + +#ifdef notdef + (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); + (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", + ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); +#endif + /* + * If we're going to need to stat anything or we want to descend + * and stay in the directory, chdir. If this fails we keep going, + * but set a flag so we don't chdir after the post-order visit. + * We won't be able to stat anything, but we can still return the + * names themselves. Note, that since fts_read won't be able to + * chdir into the directory, it will have to return different path + * names than before, i.e. "a/b" instead of "b". Since the node + * has already been visited in pre-order, have to wait until the + * post-order visit to return the error. There is a special case + * here, if there was nothing to stat then it's not an error to + * not be able to stat. This is all fairly nasty. If a program + * needed sorted entries or stat information, they had better be + * checking FTS_NS on the returned nodes. + */ + cderrno = 0; + if (nlinks || type == BREAD) { + if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) { + if (nlinks && type == BREAD) + cur->fts_errno = errno; + cur->fts_flags |= FTS_DONTCHDIR; + descend = 0; + cderrno = errno; + } else + descend = 1; + } else + descend = 0; + + /* + * Figure out the max file name length that can be stored in the + * current path -- the inner loop allocates more path as necessary. + * We really wouldn't have to do the maxlen calculations here, we + * could do them in fts_read before returning the path, but it's a + * lot easier here since the length is part of the dirent structure. + * + * If not changing directories set a pointer so that can just append + * each new name into the path. + */ + len = NAPPEND(cur); + if (ISSET(FTS_NOCHDIR)) { + cp = sp->fts_path + len; + *cp++ = '/'; + } + len++; + maxlen = sp->fts_pathlen - len; + +#if defined(__FTS_COMPAT_LEVEL) + if (cur->fts_level == SHRT_MAX) { + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = ENAMETOOLONG; + return (NULL); + } +#endif + + level = cur->fts_level + 1; + + /* Read the directory, attaching each entry to the `link' pointer. */ + doadjust = 0; + for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) { + + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) + continue; + +#if defined(HAVE_STRUCT_DIRENT_D_NAMLEN) + dnamlen = dp->d_namlen; +#else + dnamlen = strlen(dp->d_name); +#endif + if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL) + goto mem1; + if (dnamlen >= maxlen) { /* include space for NUL */ + oldaddr = sp->fts_path; + if (fts_palloc(sp, dnamlen + len + 1)) { + /* + * No more memory for path or structures. Save + * errno, free up the current structure and the + * structures already allocated. + */ +mem1: saved_errno = errno; + if (p) + fts_free(p); + fts_lfree(head); + (void)closedir(dirp); + errno = saved_errno; + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + return (NULL); + } + /* Did realloc() change the pointer? */ + if (oldaddr != sp->fts_path) { + doadjust = 1; + if (ISSET(FTS_NOCHDIR)) + cp = sp->fts_path + len; + } + maxlen = sp->fts_pathlen - len; + } + +#if defined(__FTS_COMPAT_LENGTH) + if (len + dnamlen >= USHRT_MAX) { + /* + * In an FTSENT, fts_pathlen is an unsigned short + * so it is possible to wraparound here. + * If we do, free up the current structure and the + * structures already allocated, then error out + * with ENAMETOOLONG. + */ + fts_free(p); + fts_lfree(head); + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = ENAMETOOLONG; + return (NULL); + } +#endif + p->fts_level = level; + p->fts_pathlen = len + dnamlen; + p->fts_parent = sp->fts_cur; + +#ifdef FTS_WHITEOUT + if (dp->d_type == DT_WHT) + p->fts_flags |= FTS_ISW; +#endif + + if (cderrno) { + if (nlinks) { + p->fts_info = FTS_NS; + p->fts_errno = cderrno; + } else + p->fts_info = FTS_NSOK; + p->fts_accpath = cur->fts_accpath; + } else if (nlinks == 0 +#ifdef DT_DIR + || (nostat && + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) +#endif + ) { + p->fts_accpath = + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; + p->fts_info = FTS_NSOK; + } else { + /* Build a file name for fts_stat to stat. */ + if (ISSET(FTS_NOCHDIR)) { + p->fts_accpath = p->fts_path; + memmove(cp, p->fts_name, + (size_t)(p->fts_namelen + 1)); + } else + p->fts_accpath = p->fts_name; + /* Stat it. */ + p->fts_info = fts_stat(sp, p, 0); + + /* Decrement link count if applicable. */ + if (nlinks > 0 && (p->fts_info == FTS_D || + p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) + --nlinks; + } + + /* We walk in directory order so "ls -f" doesn't get upset. */ + p->fts_link = NULL; + if (head == NULL) + head = tail = p; + else { + tail->fts_link = p; + tail = p; + } + ++nitems; + } + (void)closedir(dirp); + + /* + * If had to realloc the path, adjust the addresses for the rest + * of the tree. + */ + if (doadjust) + fts_padjust(sp, head); + + /* + * If not changing directories, reset the path back to original + * state. + */ + if (ISSET(FTS_NOCHDIR)) { + if (len == sp->fts_pathlen || nitems == 0) + --cp; + *cp = '\0'; + } + + /* + * If descended after called from fts_children or after called from + * fts_read and nothing found, get back. At the root level we use + * the saved fd; if one of fts_open()'s arguments is a relative path + * to an empty directory, we wind up here with no other way back. If + * can't get back, we're done. + */ + if (descend && (type == BCHILD || !nitems) && + (cur->fts_level == FTS_ROOTLEVEL ? + FCHDIR(sp, sp->fts_rfd) : + fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + return (NULL); + } + + /* If didn't find anything, return NULL. */ + if (!nitems) { + if (type == BREAD) + cur->fts_info = FTS_DP; + return (NULL); + } + + /* Sort the entries. */ + if (sp->fts_compar && nitems > 1) + head = fts_sort(sp, head, nitems); + return (head); +} + +static unsigned short +fts_stat(FTS *sp, FTSENT *p, int follow) +{ + FTSENT *t; + dev_t dev; + __fts_ino_t ino; + __fts_stat_t *sbp, sb; + int saved_errno; + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(p != NULL); + + /* If user needs stat info, stat buffer already allocated. */ + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; + +#ifdef FTS_WHITEOUT + /* check for whiteout */ + if (p->fts_flags & FTS_ISW) { + if (sbp != &sb) { + memset(sbp, '\0', sizeof (*sbp)); + sbp->st_mode = S_IFWHT; + } + return (FTS_W); + } +#endif + + /* + * If doing a logical walk, or application requested FTS_FOLLOW, do + * a stat(2). If that fails, check for a non-existent symlink. If + * fail, set the errno from the stat call. + */ + if (ISSET(FTS_LOGICAL) || follow) { + if (stat(p->fts_accpath, sbp)) { + saved_errno = errno; + if (!lstat(p->fts_accpath, sbp)) { + errno = 0; + return (FTS_SLNONE); + } + p->fts_errno = saved_errno; + goto err; + } + } else if (lstat(p->fts_accpath, sbp)) { + p->fts_errno = errno; +err: memset(sbp, 0, sizeof(*sbp)); + return (FTS_NS); + } + + if (S_ISDIR(sbp->st_mode)) { + /* + * Set the device/inode. Used to find cycles and check for + * crossing mount points. Also remember the link count, used + * in fts_build to limit the number of stat calls. It is + * understood that these fields are only referenced if fts_info + * is set to FTS_D. + */ + dev = p->fts_dev = sbp->st_dev; + ino = p->fts_ino = sbp->st_ino; + p->fts_nlink = sbp->st_nlink; + + if (ISDOT(p->fts_name)) + return (FTS_DOT); + + /* + * Cycle detection is done by brute force when the directory + * is first encountered. If the tree gets deep enough or the + * number of symbolic links to directories is high enough, + * something faster might be worthwhile. + */ + for (t = p->fts_parent; + t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) + if (ino == t->fts_ino && dev == t->fts_dev) { + p->fts_cycle = t; + return (FTS_DC); + } + return (FTS_D); + } + if (S_ISLNK(sbp->st_mode)) + return (FTS_SL); + if (S_ISREG(sbp->st_mode)) + return (FTS_F); + return (FTS_DEFAULT); +} + +static FTSENT * +fts_sort(FTS *sp, FTSENT *head, size_t nitems) +{ + FTSENT **ap, *p; + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(head != NULL); + + /* + * Construct an array of pointers to the structures and call qsort(3). + * Reassemble the array in the order returned by qsort. If unable to + * sort for memory reasons, return the directory entries in their + * current order. Allocate enough space for the current needs plus + * 40 so don't realloc one entry at a time. + */ + if (nitems > sp->fts_nitems) { + FTSENT **new; + + new = realloc(sp->fts_array, sizeof(FTSENT *) * (nitems + 40)); + if (new == 0) + return (head); + sp->fts_array = new; + sp->fts_nitems = nitems + 40; + } + for (ap = sp->fts_array, p = head; p; p = p->fts_link) + *ap++ = p; + qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), + (int (*)(const void *, const void *))sp->fts_compar); + for (head = *(ap = sp->fts_array); --nitems; ++ap) + ap[0]->fts_link = ap[1]; + ap[0]->fts_link = NULL; + return (head); +} + +static FTSENT * +fts_alloc(FTS *sp, const char *name, size_t namelen) +{ + FTSENT *p; +#if defined(FTS_ALLOC_ALIGNED) + size_t len; +#endif + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(name != NULL); + +#if defined(FTS_ALLOC_ALIGNED) + /* + * The file name is a variable length array and no stat structure is + * necessary if the user has set the nostat bit. Allocate the FTSENT + * structure, the file name and the stat structure in one chunk, but + * be careful that the stat structure is reasonably aligned. Since the + * fts_name field is declared to be of size 1, the fts_name pointer is + * namelen + 2 before the first possible address of the stat structure. + */ + len = sizeof(FTSENT) + namelen; + if (!ISSET(FTS_NOSTAT)) + len += sizeof(*(p->fts_statp)) + ALIGNBYTES; + if ((p = malloc(len)) == NULL) + return (NULL); + + if (!ISSET(FTS_NOSTAT)) + p->fts_statp = (__fts_stat_t *)ALIGN( + (unsigned long)(p->fts_name + namelen + 2)); +#else + if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL) + return (NULL); + + if (!ISSET(FTS_NOSTAT)) + if ((p->fts_statp = malloc(sizeof(*(p->fts_statp)))) == NULL) { + free(p); + return (NULL); + } +#endif + + if (ISSET(FTS_NOSTAT)) + p->fts_statp = NULL; + + /* Copy the name plus the trailing NULL. */ + memmove(p->fts_name, name, namelen + 1); + + p->fts_namelen = namelen; + p->fts_path = sp->fts_path; + p->fts_errno = 0; + p->fts_flags = 0; + p->fts_instr = FTS_NOINSTR; + p->fts_number = 0; + p->fts_pointer = NULL; + return (p); +} + +static void +fts_free(FTSENT *p) +{ +#if !defined(FTS_ALLOC_ALIGNED) + if (p->fts_statp) + free(p->fts_statp); +#endif + free(p); +} + +static void +fts_lfree(FTSENT *head) +{ + FTSENT *p; + + /* XXX: head may be NULL ? */ + + /* Free a linked list of structures. */ + while ((p = head) != NULL) { + head = head->fts_link; + fts_free(p); + } +} + +static size_t +fts_pow2(size_t x) +{ + + x--; + x |= x>>1; + x |= x>>2; + x |= x>>4; + x |= x>>8; + x |= x>>16; +#if LONG_BIT > 32 + x |= x>>32; +#endif +#if LONG_BIT > 64 + x |= x>>64; +#endif + x++; + return (x); +} + +/* + * Allow essentially unlimited paths; find, rm, ls should all work on any tree. + * Most systems will allow creation of paths much longer than MAXPATHLEN, even + * though the kernel won't resolve them. Round up the new size to a power of 2, + * so we don't realloc the path 2 bytes at a time. + */ +static int +fts_palloc(FTS *sp, size_t size) +{ + char *new; + + _DIAGASSERT(sp != NULL); + +#ifdef __FTS_COMPAT_LENGTH + /* Protect against fts_pathlen overflow. */ + if (size > USHRT_MAX + 1) { + errno = ENAMETOOLONG; + return (1); + } +#endif + size = fts_pow2(size); + new = realloc(sp->fts_path, size); + if (new == 0) + return (1); + sp->fts_path = new; + sp->fts_pathlen = size; + return (0); +} + +/* + * When the path is realloc'd, have to fix all of the pointers in structures + * already returned. + */ +static void +fts_padjust(FTS *sp, FTSENT *head) +{ + FTSENT *p; + char *addr; + + _DIAGASSERT(sp != NULL); + +#define ADJUST(p) do { \ + if ((p)->fts_accpath != (p)->fts_name) \ + (p)->fts_accpath = \ + addr + ((p)->fts_accpath - (p)->fts_path); \ + (p)->fts_path = addr; \ +} while (/*CONSTCOND*/0) + + addr = sp->fts_path; + + /* Adjust the current set of children. */ + for (p = sp->fts_child; p; p = p->fts_link) + ADJUST(p); + + /* Adjust the rest of the tree, including the current level. */ + for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { + ADJUST(p); + p = p->fts_link ? p->fts_link : p->fts_parent; + } +} + +static size_t +fts_maxarglen(char * const *argv) +{ + size_t len, max; + + _DIAGASSERT(argv != NULL); + + for (max = 0; *argv; ++argv) + if ((len = strlen(*argv)) > max) + max = len; + return (max + 1); +} + +/* + * Change to dir specified by fd or p->fts_accpath without getting + * tricked by someone changing the world out from underneath us. + * Assumes p->fts_dev and p->fts_ino are filled in. + */ +static int +fts_safe_changedir(const FTS *sp, const FTSENT *p, int fd, const char *path) +{ + int oldfd = fd, ret = -1; + __fts_stat_t sb; + + if (ISSET(FTS_NOCHDIR)) + return 0; + + if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1) + return -1; + + if (fstat(fd, &sb) == -1) + goto bail; + + if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) { + errno = ENOENT; + goto bail; + } + + ret = fchdir(fd); + +bail: + if (oldfd < 0) { + int save_errno = errno; + (void)close(fd); + errno = save_errno; + } + return ret; +} diff --git a/lib/nbsd_libc/gen/ftw.3 b/lib/nbsd_libc/gen/ftw.3 new file mode 100644 index 000000000..4343823ac --- /dev/null +++ b/lib/nbsd_libc/gen/ftw.3 @@ -0,0 +1,214 @@ +.\" $NetBSD: ftw.3,v 1.5 2010/04/30 04:39:16 jruoho Exp $ +.\" +.\" From OpenBSD: ftw.3,v 1.4 2003/10/30 18:52:58 jmc Exp +.\" +.\" Copyright (c) 2003 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 April 30, 2010 +.Dt FTW 3 +.Os +.Sh NAME +.Nm ftw, nftw +.Nd traverse (walk) a file tree +.Sh SYNOPSIS +.In ftw.h +.Ft int +.Fo ftw +.Fa "const char *path" +.Fa "int (*fn)(const char *, const struct stat *, int)" +.Fa "int maxfds" +.Fc +.Ft int +.Fo nftw +.Fa "const char *path" +.Fa "int (*fn)(const\ char\ *, const\ struct\ stat\ *, int, struct\ FTW\ *)" +.Fa "int maxfds" +.Fa "int flags" +.Fc +.Sh DESCRIPTION +.Bf -symbolic +These functions are provided for compatibility with legacy code. +New code should use the +.Xr fts 3 +functions. +.Ef +.Pp +The +.Fn ftw +and +.Fn nftw +functions traverse (walk) the directory hierarchy rooted in +.Fa path . +For each object in the hierarchy, these functions call the function +pointed to by +.Fa fn . +The +.Fn ftw +function passes this function a pointer to a NUL-terminated string containing +the name of the object, a pointer to a stat structure corresponding to the +object, and an integer flag. +The +.Fn nftw +function passes the aforementioned arguments plus a pointer to a +.Dv FTW +structure as defined by +.In ftw.h +(shown below): +.Bd -literal +struct FTW { + int base; /* offset of basename into pathname */ + int level; /* directory depth relative to starting point */ +}; +.Ed +.Pp +Possible values for the flag passed to +.Fa fn +are: +.Bl -tag -width FTW_DNR +.It Dv FTW_F +A regular file. +.It Dv FTW_D +A directory being visited in pre-order. +.It Dv FTW_DNR +A directory which cannot be read. +The directory will not be descended into. +.It Dv FTW_DP +A directory being visited in post-order +.Pq Fn nftw No only . +.It Dv FTW_NS +A file for which no +.Xr stat 2 +information was available. +The contents of the stat structure are undefined. +.It Dv FTW_SL +A symbolic link. +.It Dv FTW_SLN +A symbolic link with a non-existent target +.Pq Fn nftw No only . +.El +.Pp +The +.Fn ftw +function traverses the tree in pre-order. +That is, it processes the directory before the directory's contents. +.Pp +The +.Fa maxfds +argument specifies the maximum number of file descriptors +to keep open while traversing the tree. +It has no effect in this implementation. +.Pp +The +.Fn nftw +function has an additional +.Fa flags +argument with the following possible values: +.Bl -tag -width FTW_MOUNT +.It Dv FTW_PHYS +Physical walk, don't follow symbolic links. +.It Dv FTW_MOUNT +The walk will not cross a mount point. +.It FTW_DEPTH +Process directories in post-order. +Contents of a directory are visited before the directory itself. +By default, +.Fn nftw +traverses the tree in pre-order. +.It FTW_CHDIR +Change to a directory before reading it. +By default, +.Fn nftw +will change its starting directory. +The current working directory will be restored to its original value before +.Fn nftw +returns. +.El +.Sh RETURN VALUES +If the tree was traversed successfully, the +.Fn ftw +and +.Fn nftw +functions return 0. +If the function pointed to by +.Fa fn +returns a non-zero value, +.Fn ftw +and +.Fn nftw +will stop processing the tree and return the value from +.Fa fn . +Both functions return \-1 if an error is detected. +.Sh ERRORS +The +.Fn ftw +and +.Fn nftw +functions may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr close 2 , +.Xr open 2 , +.Xr stat 2 , +.Xr malloc 3 , +.Xr opendir 3 , +and +.Xr readdir 3 . +If the +.Dv FGTW_CHDIR +flag is set, the +.Fn nftw +function may fail and set +.Va errno +for any of the errors specified for +.Xr chdir 2 . +In addition, either function may fail and set +.Va errno +as follows: +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Fa maxfds +argument is less than 1 or greater than +.Dv OPEN_MAX . +.El +.Sh SEE ALSO +.Xr chdir 2 , +.Xr close 2 , +.Xr open 2 , +.Xr stat 2 , +.Xr fts 3 , +.Xr malloc 3 , +.Xr opendir 3 , +.Xr readdir 3 +.Sh STANDARDS +The +.Fn ftw +and +.Fn nftw +functions conform to +.St -p1003.1-2001 . +The +.St -p1003.1-2008 +revision marked the function +.Fn ftw +as obsolete. +.Sh BUGS +The +.Fa maxfds +argument is currently ignored. diff --git a/lib/nbsd_libc/gen/ftw.c b/lib/nbsd_libc/gen/ftw.c new file mode 100644 index 000000000..a7f6bbdb6 --- /dev/null +++ b/lib/nbsd_libc/gen/ftw.c @@ -0,0 +1,98 @@ +/* $NetBSD: ftw.c,v 1.1 2005/12/30 23:07:32 agc Exp $ */ + +/* From OpenBSD: ftw.c,v 1.2 2003/07/21 21:15:32 millert Exp */ + +/* + * Copyright (c) 2003 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 + +#ifndef lint +__RCSID("$NetBSD: ftw.c,v 1.1 2005/12/30 23:07:32 agc Exp $"); +#endif + +#include +#include +#include +#include +#include +#include + +int +ftw(const char *path, int (*fn)(const char *, const struct stat *, int), + int nfds) +{ + /* LINTED */ + char * const paths[2] = { __UNCONST(path), NULL }; + FTSENT *cur; + FTS *ftsp; + int fnflag, error, sverrno; + + /* XXX - nfds is currently unused */ + if (nfds < 1 || nfds > OPEN_MAX) { + errno = EINVAL; + return (-1); + } + + ftsp = fts_open(paths, FTS_COMFOLLOW | FTS_NOCHDIR, NULL); + if (ftsp == NULL) + return (-1); + error = 0; + while ((cur = fts_read(ftsp)) != NULL) { + switch (cur->fts_info) { + case FTS_D: + fnflag = FTW_D; + break; + case FTS_DNR: + fnflag = FTW_DNR; + break; + case FTS_DP: + /* we only visit in preorder */ + continue; + case FTS_F: + case FTS_DEFAULT: + fnflag = FTW_F; + break; + case FTS_NS: + case FTS_NSOK: + case FTS_SLNONE: + fnflag = FTW_NS; + break; + case FTS_SL: + fnflag = FTW_SL; + break; + case FTS_DC: + errno = ELOOP; + /* FALLTHROUGH */ + default: + error = -1; + goto done; + } + error = fn(cur->fts_path, cur->fts_statp, fnflag); + if (error != 0) + break; + } +done: + sverrno = errno; + if (fts_close(ftsp) != 0 && error == 0) + error = -1; + else + errno = sverrno; + return (error); +} diff --git a/lib/nbsd_libc/gen/getbsize.3 b/lib/nbsd_libc/gen/getbsize.3 new file mode 100644 index 000000000..b3c100208 --- /dev/null +++ b/lib/nbsd_libc/gen/getbsize.3 @@ -0,0 +1,90 @@ +.\" $NetBSD: getbsize.3,v 1.9 2003/08/07 16:42:48 agc Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)getbsize.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd May 30, 2003 +.Dt GETBSIZE 3 +.Os +.Sh NAME +.Nm getbsize +.Nd get user block size +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft char * +.Fn getbsize "int *headerlenp" "long *blocksizep" +.Sh DESCRIPTION +The +.Nm getbsize +function determines the user's preferred block size based on the value of the +.Dq BLOCKSIZE +environment variable; see +.Xr environ 7 +for details on its use and format. +.Pp +The +.Nm getbsize +function returns a pointer to a +.Dv NUL +terminated string describing +the block size, something like +.Dq 1K-blocks . +If the +.Fa headerlenp +parameter is not +.Dv NULL +the memory referenced by +.Fa headerlenp +is filled in with the length of the string (not including the +terminating +.Dv NUL ) . +If the +.Fa blocksizep +parameter is not +.Dv NULL +the memory referenced by +.Fa blocksizep +is filled in with block size, in bytes. +.Pp +If the user's block size is unreasonable, a warning message is +written to standard error and the returned information reflects +a block size of 512 bytes. +.Sh SEE ALSO +.Xr df 1 , +.Xr du 1 , +.Xr ls 1 , +.Xr systat 1 , +.Xr environ 7 +.Sh HISTORY +The +.Nm getbsize +function first appeared in +.Bx 4.4 . diff --git a/lib/nbsd_libc/gen/getbsize.c b/lib/nbsd_libc/gen/getbsize.c new file mode 100644 index 000000000..c5e4c33cc --- /dev/null +++ b/lib/nbsd_libc/gen/getbsize.c @@ -0,0 +1,120 @@ +/* $NetBSD: getbsize.c,v 1.16 2005/11/29 03:11:59 christos Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getbsize.c,v 1.16 2005/11/29 03:11:59 christos Exp $"); +#endif +#endif /* not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getbsize,_getbsize) +#endif + +char * +getbsize(headerlenp, blocksizep) + int *headerlenp; + long *blocksizep; +{ + static char header[20]; + long n, max, mul, blocksize; + char *ep, *p; + const char *form; + +#define KB (1024L) +#define MB (1024L * 1024L) +#define GB (1024L * 1024L * 1024L) +#define MAXB GB /* No tera, peta, nor exa. */ + form = ""; + if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') { + if ((n = strtol(p, &ep, 10)) < 0) + goto underflow; + if (n == 0) + n = 1; + if (*ep && ep[1]) + goto fmterr; + switch (*ep) { + case 'G': case 'g': + form = "G"; + max = MAXB / GB; + mul = GB; + break; + case 'K': case 'k': + form = "K"; + max = MAXB / KB; + mul = KB; + break; + case 'M': case 'm': + form = "M"; + max = MAXB / MB; + mul = MB; + break; + case '\0': + max = MAXB; + mul = 1; + break; + default: +fmterr: warnx("%s: unknown blocksize", p); + n = 512; + mul = 1; + max = 0; + break; + } + if (n > max) { + warnx("maximum blocksize is %ldG", MAXB / GB); + n = max; + } + if ((blocksize = n * mul) < 512) { +underflow: warnx("%s: minimum blocksize is 512", p); + form = ""; + blocksize = n = 512; + } + } else + blocksize = n = 512; + + if (headerlenp) + *headerlenp = + snprintf(header, sizeof(header), "%ld%s-blocks", n, form); + if (blocksizep) + *blocksizep = blocksize; + return (header); +} diff --git a/lib/nbsd_libc/gen/getcap.c b/lib/nbsd_libc/gen/getcap.c new file mode 100644 index 000000000..f17873e59 --- /dev/null +++ b/lib/nbsd_libc/gen/getcap.c @@ -0,0 +1,1213 @@ +/* $NetBSD: getcap.c,v 1.48 2008/02/02 20:56:46 christos 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 + * Casey Leedom of Lawrence Livermore National Laboratory. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; +#else +__RCSID("$NetBSD: getcap.c,v 1.48 2008/02/02 20:56:46 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifndef SMALL +#include "namespace.h" +#endif +#include +#include + +#include +#include +#ifndef SMALL +#include +#endif +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(cgetcap,_cgetcap) +__weak_alias(cgetclose,_cgetclose) +__weak_alias(cgetent,_cgetent) +__weak_alias(cgetfirst,_cgetfirst) +__weak_alias(cgetmatch,_cgetmatch) +__weak_alias(cgetnext,_cgetnext) +__weak_alias(cgetnum,_cgetnum) +__weak_alias(cgetset,_cgetset) +__weak_alias(cgetstr,_cgetstr) +__weak_alias(cgetustr,_cgetustr) +__weak_alias(csetexpandtc,_csetexpandtc) +#endif + +#define BFRAG 1024 +#define BSIZE 1024 +#define ESC ('[' & 037) /* ASCII ESC */ +#define MAX_RECURSION 32 /* maximum getent recursion */ +#define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */ + +#define RECOK (char)0 +#define TCERR (char)1 +#define SHADOW (char)2 + +static size_t topreclen; /* toprec length */ +static char *toprec; /* Additional record specified by cgetset() */ +static int gottoprec; /* Flag indicating retrieval of toprecord */ +static int expandtc = 1; /* flag to expand tc= or not */ + +#ifndef SMALL +static int cdbget(DB *, char **, const char *); +#endif +static int getent(char **, size_t *, const char * const *, int, + const char *, int, char *); +static int nfcmp(char *, char *); + +/* + * Cgetset() allows the addition of a user specified buffer to be added + * to the database array, in effect "pushing" the buffer on top of the + * virtual database. 0 is returned on success, -1 on failure. + */ +int +cgetset(const char *ent) +{ + const char *source, *check; + char *dest; + + if (ent == NULL) { + if (toprec != NULL) + free(toprec); + toprec = NULL; + topreclen = 0; + return 0; + } + topreclen = strlen(ent); + if ((toprec = malloc(topreclen + 1)) == NULL) { + errno = ENOMEM; + return -1; + } + gottoprec = 0; + + source = ent; + dest = toprec; + while (*source != '\0') { /* Strip whitespace */ + *dest++ = *source++; /* Do not check first field */ + while (*source == ':') { + check = source + 1; + while (*check && (isspace((unsigned char)*check) || + (*check=='\\' && isspace((unsigned char)check[1])))) + ++check; + if (*check == ':') + source = check; + else + break; + + } + } + *dest = 0; + + return 0; +} + +/* + * Cgetcap searches the capability record buf for the capability cap with + * type `type'. A pointer to the value of cap is returned on success, NULL + * if the requested capability couldn't be found. + * + * Specifying a type of ':' means that nothing should follow cap (:cap:). + * In this case a pointer to the terminating ':' or NUL will be returned if + * cap is found. + * + * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator) + * return NULL. + */ +char * +cgetcap(buf, cap, type) + char *buf; + const char *cap; + int type; +{ + char *bp; + const char *cp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + + bp = buf; + for (;;) { + /* + * Skip past the current capability field - it's either the + * name field if this is the first time through the loop, or + * the remainder of a field whose name failed to match cap. + */ + for (;;) + if (*bp == '\0') + return NULL; + else if (*bp++ == ':') + break; + + /* + * Try to match (cap, type) in buf. + */ + for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++) + continue; + if (*cp != '\0') + continue; + if (*bp == '@') + return NULL; + if (type == ':') { + if (*bp != '\0' && *bp != ':') + continue; + return bp; + } + if (*bp != type) + continue; + bp++; + return *bp == '@' ? NULL : bp; + } + /* NOTREACHED */ +} + +/* + * Cgetent extracts the capability record name from the NULL terminated file + * array db_array and returns a pointer to a malloc'd copy of it in buf. + * Buf must be retained through all subsequent calls to cgetcap, cgetnum, + * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success, + * -1 if the requested record couldn't be found, -2 if a system error was + * encountered (couldn't open/read a file, etc.), and -3 if a potential + * reference loop is detected. + */ +/* coverity[+alloc : arg-*0] */ +int +cgetent(char **buf, const char * const *db_array, const char *name) +{ + size_t dummy; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + _DIAGASSERT(name != NULL); + + return getent(buf, &dummy, db_array, -1, name, 0, NULL); +} + +void +csetexpandtc(int etc) +{ + expandtc = etc; +} + +/* + * Getent implements the functions of cgetent. If fd is non-negative, + * *db_array has already been opened and fd is the open file descriptor. We + * do this to save time and avoid using up file descriptors for tc= + * recursions. + * + * Getent returns the same success/failure codes as cgetent. On success, a + * pointer to a malloc'ed capability record with all tc= capabilities fully + * expanded and its length (not including trailing ASCII NUL) are left in + * *cap and *len. + * + * Basic algorithm: + * + Allocate memory incrementally as needed in chunks of size BFRAG + * for capability buffer. + * + Recurse for each tc=name and interpolate result. Stop when all + * names interpolated, a name can't be found, or depth exceeds + * MAX_RECURSION. + */ +/* coverity[+alloc : arg-*0] */ +static int +getent(char **cap, size_t *len, const char * const *db_array, int fd, + const char *name, int depth, char *nfield) +{ +#ifndef SMALL + DB *capdbp; + char pbuf[MAXPATHLEN]; + char *cbuf; + int retval; + size_t clen; +#endif + char *record, *newrecord; + char *r_end, *rp; /* pacify gcc */ + const char * const *db_p; + int myfd, eof, foundit; + int tc_not_resolved; + + _DIAGASSERT(cap != NULL); + _DIAGASSERT(len != NULL); + _DIAGASSERT(db_array != NULL); + /* fd may be -1 */ + _DIAGASSERT(name != NULL); + /* nfield may be NULL */ + + myfd = 0; + rp = NULL; + + /* + * Return with ``loop detected'' error if we've recursed more than + * MAX_RECURSION times. + */ + if (depth > MAX_RECURSION) + return -3; + + /* + * Check if we have a top record from cgetset(). + */ + if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) { + if ((record = malloc(topreclen + BFRAG)) == NULL) { + errno = ENOMEM; + return -2; + } + (void)strcpy(record, toprec); /* XXX: strcpy is safe */ + db_p = db_array; + rp = record + topreclen + 1; + r_end = rp + BFRAG; + goto tc_exp; + } + /* + * Allocate first chunk of memory. + */ + if ((record = malloc(BFRAG)) == NULL) { + errno = ENOMEM; + return -2; + } + r_end = record + BFRAG; + foundit = 0; + /* + * Loop through database array until finding the record. + */ + + for (db_p = db_array; *db_p != NULL; db_p++) { + eof = 0; + + /* + * Open database if not already open. + */ + + if (fd >= 0) { + (void)lseek(fd, (off_t)0, SEEK_SET); + } else { +#ifndef SMALL + (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p); + if (expandtc && + (capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0)) + != NULL) { + free(record); + retval = cdbget(capdbp, &record, name); + if (retval < 0) { + /* no record available */ + (void)capdbp->close(capdbp); + return retval; + } + /* save the data; close frees it */ + clen = strlen(record); + if ((cbuf = malloc(clen + 1)) == NULL) { + (void)capdbp->close(capdbp); + errno = ENOMEM; + return -2; + } + memmove(cbuf, record, clen + 1); + if (capdbp->close(capdbp) < 0) { + int serrno = errno; + + free(cbuf); + errno = serrno; + return -2; + } + *len = clen; + *cap = cbuf; + return retval; + } else +#endif + { + fd = open(*db_p, O_RDONLY, 0); + if (fd < 0) { + /* No error on unfound file. */ + continue; + } + myfd = 1; + } + } + /* + * Find the requested capability record ... + */ + { + char buf[BUFSIZ]; + char *b_end, *bp, *cp; + int c, slash; + + /* + * Loop invariants: + * There is always room for one more character in record. + * R_end always points just past end of record. + * Rp always points just past last character in record. + * B_end always points just past last character in buf. + * Bp always points at next character in buf. + * Cp remembers where the last colon was. + */ + b_end = buf; + bp = buf; + cp = NULL; + slash = 0; + for (;;) { + /* + * Read in a line implementing (\, newline) + * line continuation. + */ + rp = record; + for (;;) { + if (bp >= b_end) { + int n; + + n = read(fd, buf, sizeof(buf)); + if (n <= 0) { + if (myfd) + (void)close(fd); + if (n < 0) { + int serrno = errno; + + free(record); + errno = serrno; + return -2; + } else { + fd = -1; + eof = 1; + break; + } + } + b_end = buf+n; + bp = buf; + } + + c = *bp++; + if (c == '\n') { + if (slash) { + slash = 0; + rp--; + continue; + } else + break; + } + if (slash) { + slash = 0; + cp = 0; + } + if (c == ':') { + /* + * If the field was `empty' (i.e. + * contained only white space), back up + * to the colon (eliminating the + * field). + */ + if (cp != NULL) + rp = cp; + else + cp = rp; + } else if (c == '\\') { + slash = 1; + } else if (c != ' ' && c != '\t') { + /* + * Forget where the colon was, as this + * is not an empty field. + */ + cp = 0; + } + *rp++ = c; + + /* + * Enforce loop invariant: if no room + * left in record buffer, try to get + * some more. + */ + if (rp >= r_end) { + u_int pos; + size_t newsize; + + pos = rp - record; + newsize = r_end - record + BFRAG; + newrecord = realloc(record, newsize); + if (newrecord == NULL) { + free(record); + if (myfd) + (void)close(fd); + errno = ENOMEM; + return -2; + } + record = newrecord; + r_end = record + newsize; + rp = record + pos; + } + } + /* Eliminate any white space after the last colon. */ + if (cp) + rp = cp + 1; + /* Loop invariant lets us do this. */ + *rp++ = '\0'; + + /* + * If encountered eof check next file. + */ + if (eof) + break; + + /* + * Toss blank lines and comments. + */ + if (*record == '\0' || *record == '#') + continue; + + /* + * See if this is the record we want ... + */ + if (cgetmatch(record, name) == 0) + if (nfield == NULL || !nfcmp(nfield, record)) { + foundit = 1; + break; /* found it! */ + } + } + } + if (foundit) + break; + } + + if (!foundit) + return -1; + + /* + * Got the capability record, but now we have to expand all tc=name + * references in it ... + */ +tc_exp: + tc_not_resolved = 0; + if (expandtc) { + char *newicap, *s; + size_t ilen, newilen; + int diff, iret, tclen; + char *icap, *scan, *tc, *tcstart, *tcend; + + /* + * Loop invariants: + * There is room for one more character in record. + * R_end points just past end of record. + * Rp points just past last character in record. + * Scan points at remainder of record that needs to be + * scanned for tc=name constructs. + */ + scan = record; + for (;;) { + if ((tc = cgetcap(scan, "tc", '=')) == NULL) + break; + + /* + * Find end of tc=name and stomp on the trailing `:' + * (if present) so we can use it to call ourselves. + */ + s = tc; + for (;;) + if (*s == '\0') + break; + else + if (*s++ == ':') { + *(s - 1) = '\0'; + break; + } + tcstart = tc - 3; + tclen = s - tcstart; + tcend = s; + + iret = getent(&icap, &ilen, db_p, fd, tc, depth+1, + NULL); + newicap = icap; /* Put into a register. */ + newilen = ilen; + if (iret != 0) { + /* an error */ + if (iret < -1) { + if (myfd) + (void)close(fd); + free(record); + return iret; + } + if (iret == 1) + tc_not_resolved = 1; + /* couldn't resolve tc */ + if (iret == -1) { + *(s - 1) = ':'; + scan = s - 1; + tc_not_resolved = 1; + continue; + + } + } + /* not interested in name field of tc'ed record */ + s = newicap; + for (;;) + if (*s == '\0') + break; + else if (*s++ == ':') + break; + newilen -= s - newicap; + newicap = s; + + /* make sure interpolated record is `:'-terminated */ + s += newilen; + if (*(s - 1) != ':') { + *s = ':'; /* overwrite NUL with : */ + newilen++; + } + + /* + * Make sure there's enough room to insert the + * new record. + */ + diff = newilen - tclen; + if (diff >= r_end - rp) { + u_int pos, tcpos, tcposend; + size_t newsize; + + pos = rp - record; + newsize = r_end - record + diff + BFRAG; + tcpos = tcstart - record; + tcposend = tcend - record; + newrecord = realloc(record, newsize); + if (newrecord == NULL) { + free(record); + if (myfd) + (void)close(fd); + free(icap); + errno = ENOMEM; + return -2; + } + record = newrecord; + r_end = record + newsize; + rp = record + pos; + tcstart = record + tcpos; + tcend = record + tcposend; + } + + /* + * Insert tc'ed record into our record. + */ + s = tcstart + newilen; + memmove(s, tcend, (size_t)(rp - tcend)); + memmove(tcstart, newicap, newilen); + rp += diff; + free(icap); + + /* + * Start scan on `:' so next cgetcap works properly + * (cgetcap always skips first field). + */ + scan = s - 1; + } + + } + /* + * Close file (if we opened it), give back any extra memory, and + * return capability, length and success. + */ + if (myfd) + (void)close(fd); + *len = rp - record - 1; /* don't count NUL */ + if (r_end > rp) { + if ((newrecord = + realloc(record, (size_t)(rp - record))) == NULL) { + free(record); + errno = ENOMEM; + return -2; + } + record = newrecord; + } + + *cap = record; + if (tc_not_resolved) + return 1; + return 0; +} + +#ifndef SMALL +static int +cdbget(DB *capdbp, char **bp, const char *name) +{ + DBT key; + DBT data; + + _DIAGASSERT(capdbp != NULL); + _DIAGASSERT(bp != NULL); + _DIAGASSERT(name != NULL); + + key.data = __UNCONST(name); + key.size = strlen(name); + + for (;;) { + /* Get the reference. */ + switch(capdbp->get(capdbp, &key, &data, 0)) { + case -1: + return -2; + case 1: + return -1; + } + + /* If not an index to another record, leave. */ + if (((char *)data.data)[0] != SHADOW) + break; + + key.data = (char *)data.data + 1; + key.size = data.size - 1; + } + + *bp = (char *)data.data + 1; + return ((char *)(data.data))[0] == TCERR ? 1 : 0; +} +#endif + +/* + * Cgetmatch will return 0 if name is one of the names of the capability + * record buf, -1 if not. + */ +int +cgetmatch(const char *buf, const char *name) +{ + const char *np, *bp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(name != NULL); + + /* + * Start search at beginning of record. + */ + bp = buf; + for (;;) { + /* + * Try to match a record name. + */ + np = name; + for (;;) + if (*np == '\0') { + if (*bp == '|' || *bp == ':' || *bp == '\0') + return 0; + else + break; + } else if (*bp++ != *np++) + break; + + /* + * Match failed, skip to next name in record. + */ + if (bp > buf) + bp--; /* a '|' or ':' may have stopped the match */ + else + return -1; + for (;;) + if (*bp == '\0' || *bp == ':') + return -1; /* match failed totally */ + else if (*bp++ == '|') + break; /* found next name */ + } +} + +int +cgetfirst(char **buf, const char * const *db_array) +{ + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + + (void)cgetclose(); + return cgetnext(buf, db_array); +} + +static FILE *pfp; +static int slash; +static const char * const *dbp; + +int +cgetclose(void) +{ + if (pfp != NULL) { + (void)fclose(pfp); + pfp = NULL; + } + dbp = NULL; + gottoprec = 0; + slash = 0; + return 0; +} + +/* + * Cgetnext() gets either the first or next entry in the logical database + * specified by db_array. It returns 0 upon completion of the database, 1 + * upon returning an entry with more remaining, and -1 if an error occurs. + */ +/* coverity[+alloc : arg-*0] */ +int +cgetnext(char **bp, const char * const *db_array) +{ + size_t len = 0; + int status, done; + char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; + size_t dummy; + + _DIAGASSERT(bp != NULL); + _DIAGASSERT(db_array != NULL); + + if (dbp == NULL) + dbp = db_array; + + if (pfp == NULL && (pfp = fopen(*dbp, "r")) == NULL) { + (void)cgetclose(); + return -1; + } + for (;;) { + if (toprec != NULL && !gottoprec) { + gottoprec = 1; + line = toprec; + } else { + line = fgetln(pfp, &len); + if (line == NULL) { + if (pfp == NULL) + return -1; + if (ferror(pfp)) { + (void)cgetclose(); + return -1; + } else { + (void)fclose(pfp); + pfp = NULL; + if (*++dbp == NULL) { + (void)cgetclose(); + return 0; + } else if ((pfp = + fopen(*dbp, "r")) == NULL) { + (void)cgetclose(); + return -1; + } else + continue; + } + } else + line[len - 1] = '\0'; + if (len == 1) { + slash = 0; + continue; + } + if (isspace((unsigned char)*line) || + *line == ':' || *line == '#' || slash) { + if (line[len - 2] == '\\') + slash = 1; + else + slash = 0; + continue; + } + if (line[len - 2] == '\\') + slash = 1; + else + slash = 0; + } + + + /* + * Line points to a name line. + */ + if (len > sizeof(nbuf)) + return -1; + done = 0; + np = nbuf; + for (;;) { + for (cp = line; *cp != '\0'; cp++) { + if (*cp == ':') { + *np++ = ':'; + done = 1; + break; + } + if (*cp == '\\') + break; + *np++ = *cp; + } + if (done) { + *np = '\0'; + break; + } else { /* name field extends beyond the line */ + line = fgetln(pfp, &len); + if (line == NULL && pfp) { + if (ferror(pfp)) { + (void)cgetclose(); + return -1; + } + (void)fclose(pfp); + pfp = NULL; + *np = '\0'; + break; + } else + line[len - 1] = '\0'; + } + } + if (len > sizeof(buf)) + return -1; + rp = buf; + for (cp = nbuf; *cp != '\0'; cp++) + if (*cp == '|' || *cp == ':') + break; + else + *rp++ = *cp; + + *rp = '\0'; + /* + * XXX + * Last argument of getent here should be nbuf if we want true + * sequential access in the case of duplicates. + * With NULL, getent will return the first entry found + * rather than the duplicate entry record. This is a + * matter of semantics that should be resolved. + */ + status = getent(bp, &dummy, db_array, -1, buf, 0, NULL); + if (status == -2 || status == -3) + (void)cgetclose(); + + return status + 1; + } + /* NOTREACHED */ +} + +/* + * Cgetstr retrieves the value of the string capability cap from the + * capability record pointed to by buf. A pointer to a decoded, NUL + * terminated, malloc'd copy of the string is returned in the char * + * pointed to by str. The length of the string not including the trailing + * NUL is returned on success, -1 if the requested string capability + * couldn't be found, -2 if a system error was encountered (storage + * allocation failure). + */ +int +cgetstr(char *buf, const char *cap, char **str) +{ + u_int m_room; + const char *bp; + char *mp; + int len; + char *mem, *newmem; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); + + /* + * Find string capability cap + */ + bp = cgetcap(buf, cap, '='); + if (bp == NULL) + return -1; + + /* + * Conversion / storage allocation loop ... Allocate memory in + * chunks SFRAG in size. + */ + if ((mem = malloc(SFRAG)) == NULL) { + errno = ENOMEM; + return -2; /* couldn't even allocate the first fragment */ + } + m_room = SFRAG; + mp = mem; + + while (*bp != ':' && *bp != '\0') { + /* + * Loop invariants: + * There is always room for one more character in mem. + * Mp always points just past last character in mem. + * Bp always points at next character in buf. + */ + if (*bp == '^') { + bp++; + if (*bp == ':' || *bp == '\0') + break; /* drop unfinished escape */ + *mp++ = *bp++ & 037; + } else if (*bp == '\\') { + bp++; + if (*bp == ':' || *bp == '\0') + break; /* drop unfinished escape */ + if ('0' <= *bp && *bp <= '7') { + int n, i; + + n = 0; + i = 3; /* maximum of three octal digits */ + do { + n = n * 8 + (*bp++ - '0'); + } while (--i && '0' <= *bp && *bp <= '7'); + *mp++ = n; + } + else switch (*bp++) { + case 'b': case 'B': + *mp++ = '\b'; + break; + case 't': case 'T': + *mp++ = '\t'; + break; + case 'n': case 'N': + *mp++ = '\n'; + break; + case 'f': case 'F': + *mp++ = '\f'; + break; + case 'r': case 'R': + *mp++ = '\r'; + break; + case 'e': case 'E': + *mp++ = ESC; + break; + case 'c': case 'C': + *mp++ = ':'; + break; + default: + /* + * Catches '\', '^', and + * everything else. + */ + *mp++ = *(bp-1); + break; + } + } else + *mp++ = *bp++; + m_room--; + + /* + * Enforce loop invariant: if no room left in current + * buffer, try to get some more. + */ + if (m_room == 0) { + size_t size = mp - mem; + + if ((newmem = realloc(mem, size + SFRAG)) == NULL) { + free(mem); + return -2; + } + mem = newmem; + m_room = SFRAG; + mp = mem + size; + } + } + *mp++ = '\0'; /* loop invariant let's us do this */ + m_room--; + len = mp - mem - 1; + + /* + * Give back any extra memory and return value and success. + */ + if (m_room != 0) { + if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { + free(mem); + return -2; + } + mem = newmem; + } + *str = mem; + return len; +} + +/* + * Cgetustr retrieves the value of the string capability cap from the + * capability record pointed to by buf. The difference between cgetustr() + * and cgetstr() is that cgetustr does not decode escapes but rather treats + * all characters literally. A pointer to a NUL terminated malloc'd + * copy of the string is returned in the char pointed to by str. The + * length of the string not including the trailing NUL is returned on success, + * -1 if the requested string capability couldn't be found, -2 if a system + * error was encountered (storage allocation failure). + */ +int +cgetustr(char *buf, const char *cap, char **str) +{ + u_int m_room; + const char *bp; + char *mp; + int len; + char *mem, *newmem; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); + + /* + * Find string capability cap + */ + if ((bp = cgetcap(buf, cap, '=')) == NULL) + return -1; + + /* + * Conversion / storage allocation loop ... Allocate memory in + * chunks SFRAG in size. + */ + if ((mem = malloc(SFRAG)) == NULL) { + errno = ENOMEM; + return -2; /* couldn't even allocate the first fragment */ + } + m_room = SFRAG; + mp = mem; + + while (*bp != ':' && *bp != '\0') { + /* + * Loop invariants: + * There is always room for one more character in mem. + * Mp always points just past last character in mem. + * Bp always points at next character in buf. + */ + *mp++ = *bp++; + m_room--; + + /* + * Enforce loop invariant: if no room left in current + * buffer, try to get some more. + */ + if (m_room == 0) { + size_t size = mp - mem; + + if ((newmem = realloc(mem, size + SFRAG)) == NULL) { + free(mem); + return -2; + } + mem = newmem; + m_room = SFRAG; + mp = mem + size; + } + } + *mp++ = '\0'; /* loop invariant let's us do this */ + m_room--; + len = mp - mem - 1; + + /* + * Give back any extra memory and return value and success. + */ + if (m_room != 0) { + if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { + free(mem); + return -2; + } + mem = newmem; + } + *str = mem; + return len; +} + +/* + * Cgetnum retrieves the value of the numeric capability cap from the + * capability record pointed to by buf. The numeric value is returned in + * the long pointed to by num. 0 is returned on success, -1 if the requested + * numeric capability couldn't be found. + */ +int +cgetnum(char *buf, const char *cap, long *num) +{ + long n; + int base, digit; + const char *bp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(num != NULL); + + /* + * Find numeric capability cap + */ + bp = cgetcap(buf, cap, '#'); + if (bp == NULL) + return -1; + + /* + * Look at value and determine numeric base: + * 0x... or 0X... hexadecimal, + * else 0... octal, + * else decimal. + */ + if (*bp == '0') { + bp++; + if (*bp == 'x' || *bp == 'X') { + bp++; + base = 16; + } else + base = 8; + } else + base = 10; + + /* + * Conversion loop ... + */ + n = 0; + for (;;) { + if ('0' <= *bp && *bp <= '9') + digit = *bp - '0'; + else if ('a' <= *bp && *bp <= 'f') + digit = 10 + *bp - 'a'; + else if ('A' <= *bp && *bp <= 'F') + digit = 10 + *bp - 'A'; + else + break; + + if (digit >= base) + break; + + n = n * base + digit; + bp++; + } + + /* + * Return value and success. + */ + *num = n; + return 0; +} + + +/* + * Compare name field of record. + */ +static int +nfcmp(char *nf, char *rec) +{ + char *cp, tmp; + int ret; + + _DIAGASSERT(nf != NULL); + _DIAGASSERT(rec != NULL); + + for (cp = rec; *cp != ':'; cp++) + continue; + + tmp = *(cp + 1); + *(cp + 1) = '\0'; + ret = strcmp(nf, rec); + *(cp + 1) = tmp; + + return ret; +} diff --git a/lib/nbsd_libc/gen/getcwd.3 b/lib/nbsd_libc/gen/getcwd.3 new file mode 100644 index 000000000..712d5a94a --- /dev/null +++ b/lib/nbsd_libc/gen/getcwd.3 @@ -0,0 +1,172 @@ +.\" $NetBSD: getcwd.3,v 1.18 2010/04/29 06:54:26 jruoho Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)getcwd.3 8.2 (Berkeley) 12/11/93 +.\" +.Dd April 29, 2010 +.Dt GETCWD 3 +.Os +.Sh NAME +.Nm getcwd , +.Nm getwd +.Nd get working directory pathname +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft char * +.Fn getcwd "char *buf" "size_t size" +.Ft char * +.Fn getwd "char *buf" +.Sh DESCRIPTION +The +.Fn getcwd +function copies the absolute pathname of the current working directory +into the memory referenced by +.Fa buf +and returns a pointer to +.Fa buf . +The +.Fa size +argument is the size, in bytes, of the array referenced by +.Fa buf . +.Pp +If +.Fa buf +is +.Dv NULL , +space is allocated as necessary to store the pathname. +This space may later be +.Xr free 3 Ns 'd . +.Pp +The function +.Fn getwd +is a compatibility routine which calls +.Fn getcwd +with its +.Fa buf +argument and a size of +.Dv MAXPATHLEN +(as defined in the include +file +.In sys/param.h ) . +Obviously, +.Fa buf +should be at least +.Dv MAXPATHLEN +bytes in length. +.Pp +These routines have traditionally been used by programs to save the +name of a working directory for the purpose of returning to it. +A much faster and less error-prone method of accomplishing this is to +open the current directory +.Pq Ql \&. +and use the +.Xr fchdir 2 +function to return. +.Sh RETURN VALUES +Upon successful completion, a pointer to the pathname is returned. +Otherwise a +.Dv NULL +pointer is returned and the global variable +.Va errno +is set to indicate the error. +In addition, +.Fn getwd +copies the error message associated with +.Va errno +into the memory referenced by +.Fa buf . +.Sh ERRORS +The +.Fn getcwd +function +will fail if: +.Bl -tag -width Er +.It Bq Er EACCES +Read or search permission was denied for a component of the pathname. +.It Bq Er EINVAL +The +.Fa size +argument is zero. +.It Bq Er ENOENT +A component of the pathname no longer exists. +.It Bq Er ENOMEM +Insufficient memory is available. +.It Bq Er ERANGE +The +.Fa size +argument is greater than zero but smaller than the length of the pathname +plus 1. +.El +.Sh SEE ALSO +.Xr chdir 2 , +.Xr fchdir 2 , +.Xr malloc 3 , +.Xr strerror 3 +.Sh STANDARDS +The +.Fn getwd +and +.Fn getcwd +functions conform to +.St -p1003.1-90 . +The +.St -p1003.1-2004 +revision marked +.Fn getwd +as legacy and recommended the use of +.Fn getcwd +instead. +The +.St -p1003.1-2008 +revision removed +.Fn getwd +from the specification. +.Pp +The ability to specify a +.Dv NULL +pointer and have +.Fn getcwd +allocate memory as necessary is an extension. +.Sh HISTORY +The +.Fn getwd +function appeared in +.Bx 4.0 . +.Sh SECURITY CONSIDERATIONS +As +.Fn getwd +does not know the length of the supplied buffer, it is possible +for a long (but valid) path to overflow the buffer and provide +a means for an attacker to exploit the caller. +.Fn getcwd +should be used in place of +.Fn getwd +(the latter is only provided for compatibility purposes). diff --git a/lib/nbsd_libc/gen/getcwd.c b/lib/nbsd_libc/gen/getcwd.c new file mode 100644 index 000000000..ed12f714d --- /dev/null +++ b/lib/nbsd_libc/gen/getcwd.c @@ -0,0 +1,248 @@ +/* $NetBSD: getcwd.c,v 1.47 2011/01/20 02:57:00 christos Exp $ */ + +/* + * Copyright (c) 1989, 1991, 1993, 1995 + * 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; +#else +__RCSID("$NetBSD: getcwd.c,v 1.47 2011/01/20 02:57:00 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include + +#include "extern.h" + +#ifdef __weak_alias +__weak_alias(getcwd,_sys_getcwd) +__weak_alias(_getcwd,_sys_getcwd) +__weak_alias(realpath,_realpath) + +#if defined(_FORTIFY_SOURCE) && !defined(__lint__) +#undef getcwd +#define getcwd _sys_getcwd +#endif + +#endif + +/* + * char *realpath(const char *path, char resolved[MAXPATHLEN]); + * + * 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; + int idx = 0, n, nlnk = 0; + const char *q; + char *p, wbuf[2][MAXPATHLEN]; + size_t len; + + _DIAGASSERT(resolved != NULL); + + /* POSIX sez we must test for this */ + if (path == NULL) { + errno = EINVAL; + return NULL; + } + + /* + * Build real path one by one with paying an attention to ., + * .. and symbolic link. + */ + + /* + * `p' is where we'll put a new component with prepending + * a delimiter. + */ + p = resolved; + + if (*path == 0) { + *p = 0; + errno = ENOENT; + return (NULL); + } + + /* If relative path, start from current working directory. */ + if (*path != '/') { + /* check for resolved pointer to appease coverity */ + if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) { + p[0] = '.'; + p[1] = 0; + return (NULL); + } + len = strlen(resolved); + if (len > 1) + p += len; + } + +loop: + /* Skip any slash. */ + while (*path == '/') + path++; + + if (*path == 0) { + if (p == resolved) + *p++ = '/'; + *p = 0; + return (resolved); + } + + /* Find the end of this component. */ + q = path; + do + q++; + while (*q != '/' && *q != 0); + + /* Test . or .. */ + if (path[0] == '.') { + if (q - path == 1) { + path = q; + goto loop; + } + if (path[1] == '.' && q - path == 2) { + /* Trim the last component. */ + if (p != resolved) + while (*--p != '/') + ; + path = q; + goto loop; + } + } + + /* Append this component. */ + if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) { + errno = ENAMETOOLONG; + if (p == resolved) + *p++ = '/'; + *p = 0; + return (NULL); + } + p[0] = '/'; + memcpy(&p[1], path, + /* LINTED We know q > path. */ + q - path); + p[1 + q - path] = 0; + + /* + * If this component is a symlink, toss it and prepend link + * target to unresolved path. + */ + if (lstat(resolved, &sb) == -1) { + return (NULL); + } + if (S_ISLNK(sb.st_mode)) { + if (nlnk++ >= MAXSYMLINKS) { + errno = ELOOP; + return (NULL); + } + n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1); + if (n < 0) + return (NULL); + if (n == 0) { + errno = ENOENT; + return (NULL); + } + + /* Append unresolved path to link target and switch to it. */ + if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) { + errno = ENAMETOOLONG; + return (NULL); + } + memcpy(&wbuf[idx][n], q, len + 1); + path = wbuf[idx]; + idx ^= 1; + + /* If absolute symlink, start from root. */ + if (*path == '/') + p = resolved; + goto loop; + } + if (*q == '/' && !S_ISDIR(sb.st_mode)) { + errno = ENOTDIR; + return (NULL); + } + + /* Advance both resolved and unresolved path. */ + p += 1 + q - path; + path = q; + goto loop; +} + +char * +getcwd(char *pt, size_t size) +{ + char *npt; + + /* + * If a buffer is specified, the size has to be non-zero. + */ + if (pt != NULL) { + if (size == 0) { + /* __getcwd(pt, 0) results ERANGE. */ + errno = EINVAL; + return (NULL); + } + if (__getcwd(pt, size) >= 0) + return (pt); + return (NULL); + } + + /* + * If no buffer specified by the user, allocate one as necessary. + */ + size = 1024 >> 1; + do { + if ((npt = realloc(pt, size <<= 1)) == NULL) + break; + pt = npt; + if (__getcwd(pt, size) >= 0) + return (pt); + } while (size <= MAXPATHLEN * 4 && errno == ERANGE); + + free(pt); + return (NULL); +} diff --git a/lib/nbsd_libc/gen/getdevmajor.3 b/lib/nbsd_libc/gen/getdevmajor.3 new file mode 100644 index 000000000..97db9b0e5 --- /dev/null +++ b/lib/nbsd_libc/gen/getdevmajor.3 @@ -0,0 +1,118 @@ +.\" $NetBSD: getdevmajor.3,v 1.5 2009/03/24 22:34:54 drochner Exp $ +.\" +.\" Copyright (c) 2004 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Andrew Brown. +.\" +.\" 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. +.\" +.Dd January 20, 2009 +.Dt GETDEVMAJOR 3 +.Os +.Sh NAME +.Nm getdevmajor +.Nd get block or character device major number +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.In sys/stat.h +.Ft devmajor_t +.Fn getdevmajor "const char *name" "mode_t type" +.Sh DESCRIPTION +The +.Fn getdevmajor +function returns the major device number of the block or character +device specified by +.Ar name +and a file type matching the one encoded in +.Fa type +which must be one of +.Dv S_IFBLK +or +.Dv S_IFCHR . +.Sh RETURN VALUES +If no device matches the specified values, no information is +available, or an error occurs, +.Dv NODEVMAJOR +is returned and +.Va errno +is set to indicate the error. +.Sh EXAMPLES +To retrieve the major number for +.Xr pty 4 +slave devices (aka pts devices): +.Bd -literal -offset indent +#include \*[Lt]stdlib.h\*[Gt] +#include \*[Lt]sys/stat.h\*[Gt] +.sp +devmajor_t pts; +pts = getdevmajor("pts", S_IFCHR); +.Ed +.Pp +To retrieve the major numbers for the block and character +.Xr wd 4 +devices: +.Bd -literal -offset indent +#include \*[Lt]stdlib.h\*[Gt] +#include \*[Lt]sys/stat.h\*[Gt] +.sp +devmajor_t c, b; +c = getdevmajor("wd", S_IFCHR); +b = getdevmajor("wd", S_IFBLK); +.Ed +.Sh ERRORS +The +.Fn getdevmajor +function may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr malloc 3 +and +.Xr sysctlbyname 3 . +In addition, the following errors may be reported: +.Bl -tag -width Er +.It Bq Er EINVAL +The value of the +.Fa type +argument is not +.Dv S_IFCHR +or +.Dv S_IFBLK . +.It Bq Er ENOENT +The named device is not found. +.El +.Sh SEE ALSO +.Xr stat 2 , +.Xr devname 3 , +.Xr malloc 3 , +.Xr sysctlbyname 3 +.Sh HISTORY +The +.Fn getdevmajor +function call appeared in +.Nx 3.0 . diff --git a/lib/nbsd_libc/gen/getdevmajor.c b/lib/nbsd_libc/gen/getdevmajor.c new file mode 100644 index 000000000..b946694ff --- /dev/null +++ b/lib/nbsd_libc/gen/getdevmajor.c @@ -0,0 +1,110 @@ +/* $NetBSD: getdevmajor.c,v 1.5 2009/01/20 20:08:12 drochner Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Brown. + * + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getdevmajor.c,v 1.5 2009/01/20 20:08:12 drochner Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getdevmajor,_getdevmajor) +#endif + +/* + * XXX temporary alias because getdevmajor() was renamed + * in -current for some time + */ +dev_t __getdevmajor50(const char *, mode_t); +dev_t +__getdevmajor50(const char *name, mode_t type) +{ + + return (dev_t)getdevmajor(name, type); +} + +devmajor_t +getdevmajor(const char *name, mode_t type) +{ + struct kinfo_drivers kd[200], *kdp = &kd[0]; + int rc, i; + size_t sz = sizeof(kd); + devmajor_t n = NODEVMAJOR; + + if (type != S_IFCHR && type != S_IFBLK) { + errno = EINVAL; + return n; + } + + do { + rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0); + if (rc == -1) { + if (errno != ENOMEM) + goto out; + if (kdp != &kd[0]) + free(kdp); + if ((kdp = malloc(sz)) == NULL) + return n; + } + } while (rc == -1); + + rc = sz / sizeof(*kdp); + + for (i = 0; i < rc; i++) { + if (strcmp(name, kdp[i].d_name) == 0) { + if (type == S_IFCHR) + n = kdp[i].d_cmajor; + else + n = kdp[i].d_bmajor; + break; + } + } + if (i >= rc) + errno = ENOENT; + + out: + if (kdp != &kd[0]) + free(kdp); + + return n; +} diff --git a/lib/nbsd_libc/gen/getdiskbyname.3 b/lib/nbsd_libc/gen/getdiskbyname.3 new file mode 100644 index 000000000..93c01e428 --- /dev/null +++ b/lib/nbsd_libc/gen/getdiskbyname.3 @@ -0,0 +1,105 @@ +.\" $NetBSD: getdiskbyname.3,v 1.13 2009/08/19 15:47:39 joerg Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)getdiskbyname.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt GETDISKBYNAME 3 +.Os +.Sh NAME +.Nm getdiskbyname , +.Nm setdisktab +.Nd get generic disk description by its name +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/disklabel.h +.In disktab.h +.Ft int +.Fn setdisktab "char *name" +.Ft struct disklabel * +.Fn getdiskbyname "const char *name" +.Sh DESCRIPTION +The +.Fn getdiskbyname +function +takes a disk name (e.g. +.Ql rm03 ) +and returns a prototype disk label +describing its geometry information and the standard +disk partition tables. +All information is obtained from the +.Xr disktab 5 +file. +.Pp +The +.Fn setdisktab +function changes the default +.Sy disktab +file name from +.Pa /etc/disktab +.Pq aka Dv _PATH_DISKTAB +to +.Fa name . +.Sh RETURN VALUES +.Fn getdiskbyname +returns a null pointer if the entry is not found in the current +.Pa disktab +file. +.Pp +.Fn setdisktab +returns 0 on success and \-1 if +.Fa name +is a null pointer or points to an empty string. +.Sh FILES +.Bl -tag -width /etc/disktab -compact +.It Pa /etc/disktab +the default database of disk types. +.El +.Sh SEE ALSO +.Xr disklabel 5 , +.Xr disktab 5 , +.Xr disklabel 8 +.Sh HISTORY +The +.Fn getdiskbyname +function appeared in +.Bx 4.3 . +.Pp +The +.Fn setdisktab +function appeared in +.Nx 1.4 . +.Sh BUGS +The +.Fn getdiskbyname +function leaves its results in an internal static object and returns a +pointer to that object. +Subsequent calls will modify the same object. diff --git a/lib/nbsd_libc/gen/getdomainname.3 b/lib/nbsd_libc/gen/getdomainname.3 new file mode 100644 index 000000000..a6f488878 --- /dev/null +++ b/lib/nbsd_libc/gen/getdomainname.3 @@ -0,0 +1,99 @@ +.\" $NetBSD: getdomainname.3,v 1.15 2003/08/07 16:42:49 agc Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)gethostname.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd May 6, 1994 +.Dt GETDOMAINNAME 3 +.Os +.Sh NAME +.Nm getdomainname , +.Nm setdomainname +.Nd get/set domain name of current host +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn getdomainname "char *name" "size_t namelen" +.Ft int +.Fn setdomainname "const char *name" "size_t namelen" +.Sh DESCRIPTION +.Fn getdomainname +returns the standard domain name for the current processor, as +previously set by +.Fn setdomainname . +The parameter +.Fa namelen +specifies the size of the +.Fa name +array. +The returned name is null-terminated unless insufficient +space is provided. +.Pp +.Fn setdomainname +sets the domain name of the host machine to be +.Fa name , +which has length +.Fa namelen . +This call is restricted to the super-user and +is normally used only when the system is bootstrapped. +.Sh RETURN VALUES +If the call succeeds a value of 0 is returned. +If the call fails, a value of \-1 is returned and an error code is +placed in the global location +.Va errno . +.Sh ERRORS +The following errors may be returned by these calls: +.Bl -tag -width Er +.It Bq Er EFAULT +The +.Fa name +or +.Fa namelen +parameter gave an +invalid address. +.It Bq Er EPERM +The caller tried to set the domain name and was not the super-user. +.El +.Sh SEE ALSO +.Xr gethostid 3 , +.Xr gethostname 3 , +.Xr sysctl 3 +.Sh HISTORY +The +.Nm +function call appeared in +.Bx 4.2 . +.Sh BUGS +Domain names are limited to +.Dv MAXHOSTNAMELEN +(from +.Ao Pa sys/param.h Ac ) +characters including null-termination, currently 256. diff --git a/lib/nbsd_libc/gen/getdomainname.c b/lib/nbsd_libc/gen/getdomainname.c new file mode 100644 index 000000000..0832218f9 --- /dev/null +++ b/lib/nbsd_libc/gen/getdomainname.c @@ -0,0 +1,77 @@ +/* $NetBSD: getdomainname.c,v 1.12 2003/08/07 16:42:49 agc Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)gethostname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getdomainname.c,v 1.12 2003/08/07 16:42:49 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getdomainname,_getdomainname) +#endif + +int +getdomainname(name, namelen) + char *name; + size_t namelen; +{ + int mib[2]; + size_t size; + int olderrno; + + _DIAGASSERT(name != NULL); + + mib[0] = CTL_KERN; + mib[1] = KERN_DOMAINNAME; + size = namelen; + olderrno = errno; + if (sysctl(mib, 2, name, &size, NULL, 0) == -1) { + if (errno == ENOMEM) { + errno = olderrno; + return (0); + } + return (-1); + } + + return (0); +} diff --git a/lib/nbsd_libc/gen/getfsent.3 b/lib/nbsd_libc/gen/getfsent.3 new file mode 100644 index 000000000..685a6c754 --- /dev/null +++ b/lib/nbsd_libc/gen/getfsent.3 @@ -0,0 +1,148 @@ +.\" $NetBSD: getfsent.3,v 1.11 2010/03/22 19:30:53 joerg Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)getfsent.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt GETFSENT 3 +.Os +.Sh NAME +.Nm getfsent , +.Nm getfsspec , +.Nm getfsfile , +.Nm setfsent , +.Nm endfsent +.Nd get file system descriptor file entry +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In fstab.h +.Ft struct fstab * +.Fn getfsent void +.Ft struct fstab * +.Fn getfsspec "const char *spec" +.Ft struct fstab * +.Fn getfsfile "const char *file" +.Ft int +.Fn setfsent void +.Ft void +.Fn endfsent void +.Sh DESCRIPTION +The +.Fn getfsent , +.Fn getfsspec , +and +.Fn getfsfile +functions +each return a pointer to an object with the following structure +containing the broken-out fields of a line in the file system +description file, +.In fstab.h . +.Bd -literal -offset indent +struct fstab { + char *fs_spec; /* block special device name */ + char *fs_file; /* file system path prefix */ + char *fs_vfstype; /* type of file system */ + char *fs_mntops; /* comma separated mount options */ + char *fs_type; /* rw, ro, sw, or xx */ + int fs_freq; /* dump frequency, in days */ + int fs_passno; /* pass number on parallel dump */ +}; +.Ed +.Pp +The fields have meanings described in +.Xr fstab 5 . +.Pp +The +.Fn setfsent +function +opens the file (closing any previously opened file) or rewinds it +if it is already open. +.Pp +The +.Fn endfsent +function +closes the file. +.Pp +The +.Fn getfsspec +and +.Fn getfsfile +functions +search the entire file (opening it if necessary) for a matching special +file name or file system file name. +.Pp +For programs wishing to read the entire database, +.Fn getfsent +reads the next entry (opening the file if necessary). +.Pp +All entries in the file with a type field equivalent to +.Dv FSTAB_XX +are ignored. +.Sh RETURN VALUES +The +.Fn getfsent , +.Fn getfsspec , +and +.Fn getfsfile +functions +return a null pointer (0) on +.Dv EOF +or error. +The +.Fn setfsent +function +returns 0 on failure, 1 on success. +The +.Fn endfsent +function +returns nothing. +.Sh FILES +.Bl -tag -width /etc/fstab -compact +.It Pa /etc/fstab +.El +.Sh SEE ALSO +.Xr fstab 5 +.Sh HISTORY +The +.Fn getfsent +function appeared in +.Bx 4.0 ; +the +.Fn endfsent , +.Fn getfsfile , +.Fn getfsspec , +and +.Fn setfsent +functions appeared in +.Bx 4.3 . +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. diff --git a/lib/nbsd_libc/gen/getgrent.3 b/lib/nbsd_libc/gen/getgrent.3 new file mode 100644 index 000000000..2dea4e351 --- /dev/null +++ b/lib/nbsd_libc/gen/getgrent.3 @@ -0,0 +1,344 @@ +.\" $NetBSD: getgrent.3,v 1.30 2010/03/22 19:30:53 joerg 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. +.\" +.\" @(#)getgrent.3 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 30, 2008 +.Dt GETGRENT 3 +.Os +.Sh NAME +.Nm getgrent , +.Nm getgrent_r , +.Nm getgrgid , +.Nm getgrgid_r , +.Nm getgrnam , +.Nm getgrnam_r , +.Nm setgroupent , +.\" .Nm setgrfile , +.Nm setgrent , +.Nm endgrent +.Nd group database operations +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In grp.h +.Ft struct group * +.Fn getgrent void +.Ft int +.Fo getgrent_r +.Fa "struct group *grp" +.Fa "char *buffer" +.Fa "size_t buflen" +.Fa "struct group **result" +.Fc +.Ft struct group * +.Fn getgrgid "gid_t gid" +.Ft int +.Fo getgrgid_r +.Fa "gid_t gid" +.Fa "struct group *grp" +.Fa "char *buffer" +.Fa "size_t buflen" +.Fa "struct group **result" +.Fc +.Ft struct group * +.Fn getgrnam "const char *name" +.Ft int +.Fo getgrnam_r +.Fa "const char *name" +.Fa "struct group *grp" +.Fa "char *buffer" +.Fa "size_t buflen" +.Fa "struct group **result" +.Fc +.Ft int +.Fn setgroupent "int stayopen" +.\" .Ft void +.\" .Fn setgrfile "const char *name" +.Ft void +.Fn setgrent void +.Ft void +.Fn endgrent void +.Sh DESCRIPTION +These functions operate on the group database which is described in +.Xr group 5 . +Each line of the database is defined by the structure +.Ar group +found in the include +file +.In grp.h : +.Bd -literal -offset indent +struct group { + char *gr_name; /* group name */ + char *gr_passwd; /* group password */ + gid_t gr_gid; /* group id */ + char **gr_mem; /* group members */ +}; +.Ed +.Pp +The functions +.Fn getgrnam +and +.Fn getgrgid +search the group database for the given group name pointed to by +.Ar name +or the group id pointed to by +.Ar gid , +respectively, returning the first one encountered. +Identical group names or group ids may result in undefined behavior. +.Pp +The +.Fn getgrent +function sequentially reads the group database and is intended for programs +that wish to step through the complete list of groups. +.Pp +All three functions will open the group file for reading, if necessary. +.Pp +The functions +.Fn getgrnam_r , +.Fn getgrgid_r , +and +.Fn getgrent_r +act like their non re-entrant counterparts +respectively, updating the contents of +.Ar grp +and storing a pointer to that in +.Ar result , +and returning +.Dv 0 . +Storage used by +.Ar grp +is allocated from +.Ar buffer , +which is +.Ar buflen +bytes in size. +If the requested entry cannot be found, +.Ar result +will point to +.Dv NULL +and +.Dv 0 +will be returned. +If an error occurs, +a non-zero error number will be returned and +.Ar result +will point to +.Dv NULL . +Calling +.Fn getgrent_r +from multiple threads will result in each thread reading a disjoint portion +of the group database. +.Pp +The +.Fn setgroupent +function opens the file, or rewinds it if it is already open. +If +.Fa stayopen +is non-zero, file descriptors are left open, significantly speeding +functions subsequent calls. +This functionality is unnecessary for +.Fn getgrent +as it doesn't close its file descriptors by default. +It should also be noted that it is dangerous for long-running +programs to use this functionality as the group file may be updated. +.Pp +The +.Fn setgrent +function is equivalent to +.Fn setgroupent +with an argument of zero. +.Pp +The +.Fn endgrent +function closes any open files. +.Sh RETURN VALUES +The functions +.Fn getgrgid , +.Fn getgrnam , +and +.Fn getgrent +return a valid pointer to a group structure on success +and a +.Dv NULL +pointer if the entry was not found or an error occured. +If an error occured, the global variable +.Dv errno +is set to indicate the nature of the failure. +.Pp +The functions +.Fn getgrgid_r , +.Fn getgrnam_r , +and +.Fn getgrent_r +return +.Dv 0 +on success or entry not found, and non-zero on failure, setting the global +variable +.Dv errno +to indicate the nature of the failure. +.Pp +The +.Fn setgroupent +function returns the value 1 if successful, otherwise the value +0 is returned, setting the global variable +.Dv errno +to indicate the nature of the failure. +.Pp +The +.Fn endgrent +and +.Fn setgrent +functions have no return value. +.Sh ERRORS +The following error codes may be set in +.Va errno +for +.Nm getgrent , +.Nm getgrent_r , +.Nm getgrnam , +.Nm getgrnam_r , +.Nm getgrgid , +.Nm getgrgid_r , +and +.Nm setgroupent : +.Bl -tag -width Er +.It Bq Er EIO +An I/O error has occurred. +.It Bq Er EINTR +A signal was caught during the database search. +.It Bq Er EMFILE +The limit on open files for this process has been reached. +.It Bq Er ENFILE +The system limit on open files has been reached. +.El +.Pp +The following error code may be set in +.Va errno +for +.Nm getgrent_r , +.Nm getgrnam_r , +and +.Nm getgrgid_r : +.Bl -tag -width Er +.It Bq Er ERANGE +The resulting +.Ft struct group +does not fit in the space defined by +.Dv buffer +and +.Dv buflen +.El +.Pp +Other +.Dv errno +values may be set depending on the specific database backends. +.Sh FILES +.Bl -tag -width /etc/group -compact +.It Pa /etc/group +group database file +.El +.Sh SEE ALSO +.Xr getpwent 3 , +.Xr group 5 , +.Xr nsswitch.conf 5 +.Sh STANDARDS +The +.Fn getgrgid +and +.Fn getgrnam +functions conform to +.St -p1003.1-90 . +The +.Fn getgrgid_r +and +.Fn getgrnam_r +functions conform to +.St -p1003.1c-95 . +The +.Fn endgrent , +.Fn getgrent , +and +.Fn setgrent +functions conform to +.St -xpg4.2 +and +.St -p1003.1-2004 +(XSI extension). +.Sh HISTORY +The functions +.Fn endgrent , +.Fn getgrent , +.Fn getgrgid , +.Fn getgrnam , +and +.Fn setgrent +appeared in +.At v7 . +The functions +.Fn setgrfile +and +.Fn setgroupent +appeared in +.Bx 4.3 Reno . +The functions +.Fn getgrgid_r +and +.Fn getgrnam_r +appeared in +.Nx 3.0 . +.Sh COMPATIBILITY +The historic function +.Fn setgrfile , +which allowed the specification of alternative group databases, has +been deprecated and is no longer available. +.Sh BUGS +The functions +.Fn getgrent , +.Fn getgrgid , +.Fn getgrnam , +.Fn setgroupent +and +.Fn setgrent +leave their results in an internal static object and return +a pointer to that object. +Subsequent calls to the same function will modify the same object. +.Pp +The functions +.Fn getgrent , +.Fn endgrent , +.Fn setgroupent , +and +.Fn setgrent +are fairly useless in a networked environment and should be +avoided, if possible. +.Fn getgrent +makes no attempt to suppress duplicate information if multiple +sources are specified in +.Xr nsswitch.conf 5 diff --git a/lib/nbsd_libc/gen/getgrent.c b/lib/nbsd_libc/gen/getgrent.c new file mode 100644 index 000000000..9e2b6957f --- /dev/null +++ b/lib/nbsd_libc/gen/getgrent.c @@ -0,0 +1,1935 @@ +/* $NetBSD: getgrent.c,v 1.62 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999-2000, 2004-2005 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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. + */ + +/* + * Copyright (c) 1989, 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. + */ + +/* + * Portions Copyright (c) 1994, Jason Downs. 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 BY THE AUTHOR(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 AUTHOR(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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94"; +#else +__RCSID("$NetBSD: getgrent.c,v 1.62 2008/04/28 20:22:59 martin Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HESIOD +#include +#endif + +#ifdef YP +#include +#include +#include +#endif + +#include "gr_private.h" + +#ifdef __weak_alias +__weak_alias(endgrent,_endgrent) +__weak_alias(getgrent,_getgrent) +__weak_alias(getgrent_r,_getgrent_r) +__weak_alias(getgrgid,_getgrgid) +__weak_alias(getgrgid_r,_getgrgid_r) +__weak_alias(getgrnam,_getgrnam) +__weak_alias(getgrnam_r,_getgrnam_r) +__weak_alias(setgrent,_setgrent) +__weak_alias(setgroupent,_setgroupent) +#endif + +#ifdef _REENTRANT +mutex_t __grmutex = MUTEX_INITIALIZER; +#endif + +/* + * _gr_memfrombuf + * Obtain want bytes from buffer (of size buflen) and return a pointer + * to the available memory after adjusting buffer/buflen. + * Returns NULL if there is insufficient space. + */ +static char * +_gr_memfrombuf(size_t want, char **buffer, size_t *buflen) +{ + char *rv; + + if (want > *buflen) { + errno = ERANGE; + return NULL; + } + rv = *buffer; + *buffer += want; + *buflen -= want; + return rv; +} + +/* + * _gr_parse + * Parses entry as a line per group(5) (without the trailing \n) + * and fills in grp with corresponding values; memory for strings + * and arrays will be allocated from buf (of size buflen). + * Returns 1 if parsed successfully, 0 on parse failure. + */ +static int +_gr_parse(const char *entry, struct group *grp, char *buf, size_t buflen) +{ + unsigned long id; + const char *bp; + char *ep; + size_t count; + int memc; + + _DIAGASSERT(entry != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buf != NULL); + +#define COPYTOBUF(to) \ + do { \ + (to) = _gr_memfrombuf(count+1, &buf, &buflen); \ + if ((to) == NULL) \ + return 0; \ + memmove((to), entry, count); \ + to[count] = '\0'; \ + } while (0) /* LINTED */ + +#if 0 + if (*entry == '+') /* fail on compat `+' token */ + return 0; +#endif + + count = strcspn(entry, ":"); /* parse gr_name */ + if (entry[count] == '\0') + return 0; + COPYTOBUF(grp->gr_name); + entry += count + 1; + + count = strcspn(entry, ":"); /* parse gr_passwd */ + if (entry[count] == '\0') + return 0; + COPYTOBUF(grp->gr_passwd); + entry += count + 1; + + count = strcspn(entry, ":"); /* parse gr_gid */ + if (entry[count] == '\0') + return 0; + id = strtoul(entry, &ep, 10); + if (id > GID_MAX || *ep != ':') + return 0; + grp->gr_gid = (gid_t)id; + entry += count + 1; + + memc = 1; /* for final NULL */ + if (*entry != '\0') + memc++; /* for first item */ + for (bp = entry; *bp != '\0'; bp++) { + if (*bp == ',') + memc++; + } + /* grab ALIGNed char **gr_mem from buf */ + ep = _gr_memfrombuf(memc * sizeof(char *) + ALIGNBYTES, &buf, &buflen); + grp->gr_mem = (char **)ALIGN(ep); + if (grp->gr_mem == NULL) + return 0; + + for (memc = 0; *entry != '\0'; memc++) { + count = strcspn(entry, ","); /* parse member */ + COPYTOBUF(grp->gr_mem[memc]); + entry += count; + if (*entry == ',') + entry++; + } + +#undef COPYTOBUF + + grp->gr_mem[memc] = NULL; + return 1; +} + +/* + * _gr_copy + * Copy the contents of fromgrp to grp; memory for strings + * and arrays will be allocated from buf (of size buflen). + * Returns 1 if copied successfully, 0 on copy failure. + * NOTE: fromgrp must not use buf for its own pointers. + */ +static int +_gr_copy(struct group *fromgrp, struct group *grp, char *buf, size_t buflen) +{ + char *ep; + int memc; + + _DIAGASSERT(fromgrp != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buf != NULL); + +#define COPYSTR(to, from) \ + do { \ + size_t count = strlen((from)); \ + (to) = _gr_memfrombuf(count+1, &buf, &buflen); \ + if ((to) == NULL) \ + return 0; \ + memmove((to), (from), count); \ + to[count] = '\0'; \ + } while (0) /* LINTED */ + + COPYSTR(grp->gr_name, fromgrp->gr_name); + COPYSTR(grp->gr_passwd, fromgrp->gr_passwd); + grp->gr_gid = fromgrp->gr_gid; + + for (memc = 0; fromgrp->gr_mem[memc]; memc++) + continue; + memc++; /* for final NULL */ + + /* grab ALIGNed char **gr_mem from buf */ + ep = _gr_memfrombuf(memc * sizeof(char *) + ALIGNBYTES, &buf, &buflen); + grp->gr_mem = (char **)ALIGN(ep); + if (grp->gr_mem == NULL) + return 0; + + for (memc = 0; fromgrp->gr_mem[memc]; memc++) { + COPYSTR(grp->gr_mem[memc], fromgrp->gr_mem[memc]); + } + +#undef COPYSTR + + grp->gr_mem[memc] = NULL; + return 1; +} + + /* + * files methods + */ + +int +__grstart_files(struct __grstate_files *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->fp == NULL) { + state->fp = fopen(_PATH_GROUP, "r"); + if (state->fp == NULL) + return NS_UNAVAIL; + } else { + rewind(state->fp); + } + return NS_SUCCESS; +} + +int +__grend_files(struct __grstate_files *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->fp) { + (void) fclose(state->fp); + state->fp = NULL; + } + return NS_SUCCESS; +} + +/* + * __grscan_files + * Scan state->fp for the next desired entry. + * If search is zero, return the next entry. + * If search is non-zero, look for a specific name (if name != NULL), + * or a specific gid (if name == NULL). + * Sets *retval to the errno if the result is not NS_SUCCESS + * or NS_NOTFOUND. + */ +int +__grscan_files(int *retval, struct group *grp, char *buffer, size_t buflen, + struct __grstate_files *state, int search, const char *name, gid_t gid) +{ + int rv; + char filebuf[_GETGR_R_SIZE_MAX], *ep; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + /* name is NULL to indicate searching for gid */ + + *retval = 0; + + if (state->fp == NULL) { /* only start if file not open yet */ + rv = __grstart_files(state); + if (rv != NS_SUCCESS) + goto filesgrscan_out; + } + + rv = NS_NOTFOUND; + + /* scan line by line */ + while (fgets(filebuf, sizeof(filebuf), state->fp) != NULL) { + ep = strchr(filebuf, '\n'); + if (ep == NULL) { /* skip lines that are too big */ + int ch; + + while ((ch = getc(state->fp)) != '\n' && ch != EOF) + continue; + continue; + } + *ep = '\0'; /* clear trailing \n */ + + if (filebuf[0] == '+') /* skip compat line */ + continue; + + /* validate line */ + if (! _gr_parse(filebuf, grp, buffer, buflen)) { + continue; /* skip bad lines */ + } + if (! search) { /* just want this one */ + rv = NS_SUCCESS; + break; + } + /* want specific */ + if ((name && strcmp(name, grp->gr_name) == 0) || + (!name && gid == grp->gr_gid)) { + rv = NS_SUCCESS; + break; + } + } + + filesgrscan_out: + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + return rv; +} + + +static struct __grstate_files _files_state; + /* storage for non _r functions */ +static struct group _files_group; +static char _files_groupbuf[_GETGR_R_SIZE_MAX]; + +/*ARGSUSED*/ +static int +_files_setgrent(void *nsrv, void *nscb, va_list ap) +{ + + _files_state.stayopen = 0; + return __grstart_files(&_files_state); +} + +/*ARGSUSED*/ +static int +_files_setgroupent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + _files_state.stayopen = stayopen; + rv = __grstart_files(&_files_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_files_endgrent(void *nsrv, void *nscb, va_list ap) +{ + + _files_state.stayopen = 0; + return __grend_files(&_files_state); +} + +/*ARGSUSED*/ +static int +_files_getgrent(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grscan_files(&rerror, &_files_group, + _files_groupbuf, sizeof(_files_groupbuf), + &_files_state, 0, NULL, 0); + if (rv == NS_SUCCESS) + *retval = &_files_group; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getgrent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + rv = __grscan_files(retval, grp, buffer, buflen, + &_files_state, 0, NULL, 0); + if (rv == NS_SUCCESS) + *result = grp; + else + *result = NULL; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getgrgid(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + gid_t gid = va_arg(ap, gid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_files(&_files_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_files(&rerror, &_files_group, + _files_groupbuf, sizeof(_files_groupbuf), + &_files_state, 1, NULL, gid); + if (!_files_state.stayopen) + __grend_files(&_files_state); + if (rv == NS_SUCCESS) + *retval = &_files_group; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getgrgid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + gid_t gid = va_arg(ap, gid_t); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_files state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_files(retval, grp, buffer, buflen, &state, 1, NULL, gid); + __grend_files(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getgrnam(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_files(&_files_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_files(&rerror, &_files_group, + _files_groupbuf, sizeof(_files_groupbuf), + &_files_state, 1, name, 0); + if (!_files_state.stayopen) + __grend_files(&_files_state); + if (rv == NS_SUCCESS) + *retval = &_files_group; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getgrnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_files state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_files(retval, grp, buffer, buflen, &state, 1, name, 0); + __grend_files(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + + +#ifdef HESIOD + /* + * dns methods + */ + +int +__grstart_dns(struct __grstate_dns *state) +{ + + _DIAGASSERT(state != NULL); + + state->num = 0; + if (state->context == NULL) { /* setup Hesiod */ + if (hesiod_init(&state->context) == -1) + return NS_UNAVAIL; + } + + return NS_SUCCESS; +} + +int +__grend_dns(struct __grstate_dns *state) +{ + + _DIAGASSERT(state != NULL); + + state->num = 0; + if (state->context) { + hesiod_end(state->context); + state->context = NULL; + } + return NS_SUCCESS; +} + +/* + * __grscan_dns + * Search Hesiod for the next desired entry. + * If search is zero, return the next entry. + * If search is non-zero, look for a specific name (if name != NULL), + * or a specific gid (if name == NULL). + */ +int +__grscan_dns(int *retval, struct group *grp, char *buffer, size_t buflen, + struct __grstate_dns *state, int search, const char *name, gid_t gid) +{ + const char **curzone; + char **hp, *ep; + int rv; + + static const char *zones_gid_group[] = { + "gid", + "group", + NULL + }; + + static const char *zones_group[] = { + "group", + NULL + }; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + /* name is NULL to indicate searching for gid */ + + *retval = 0; + + if (state->context == NULL) { /* only start if Hesiod not setup */ + rv = __grstart_dns(state); + if (rv != NS_SUCCESS) + return rv; + } + + next_dns_entry: + hp = NULL; + rv = NS_NOTFOUND; + + if (! search) { /* find next entry */ + if (state->num == -1) /* exhausted search */ + return NS_NOTFOUND; + /* find group-NNN */ + snprintf(buffer, buflen, "group-%u", state->num); + state->num++; + curzone = zones_group; + } else if (name) { /* find group name */ + snprintf(buffer, buflen, "%s", name); + curzone = zones_group; + } else { /* find gid */ + snprintf(buffer, buflen, "%u", (unsigned int)gid); + curzone = zones_gid_group; + } + + for (; *curzone; curzone++) { /* search zones */ + hp = hesiod_resolve(state->context, buffer, *curzone); + if (hp != NULL) + break; + if (errno != ENOENT) { + rv = NS_UNAVAIL; + goto dnsgrscan_out; + } + } + if (*curzone == NULL) { + if (! search) + state->num = -1; + goto dnsgrscan_out; + } + + if ((ep = strchr(hp[0], '\n')) != NULL) + *ep = '\0'; /* clear trailing \n */ + if (_gr_parse(hp[0], grp, buffer, buflen)) { /* validate line */ + if (! search) { /* just want this one */ + rv = NS_SUCCESS; + } else if ((name && strcmp(name, grp->gr_name) == 0) || + (!name && gid == grp->gr_gid)) { /* want specific */ + rv = NS_SUCCESS; + } + } else { /* dodgy entry */ + if (!search) { /* try again if ! searching */ + hesiod_free_list(state->context, hp); + goto next_dns_entry; + } + } + + dnsgrscan_out: + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + if (hp) + hesiod_free_list(state->context, hp); + return rv; +} + +static struct __grstate_dns _dns_state; + /* storage for non _r functions */ +static struct group _dns_group; +static char _dns_groupbuf[_GETGR_R_SIZE_MAX]; + +/*ARGSUSED*/ +static int +_dns_setgrent(void *nsrv, void *nscb, va_list ap) +{ + + _dns_state.stayopen = 0; + return __grstart_dns(&_dns_state); +} + +/*ARGSUSED*/ +static int +_dns_setgroupent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + _dns_state.stayopen = stayopen; + rv = __grstart_dns(&_dns_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_dns_endgrent(void *nsrv, void *nscb, va_list ap) +{ + + _dns_state.stayopen = 0; + return __grend_dns(&_dns_state); +} + +/*ARGSUSED*/ +static int +_dns_getgrent(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grscan_dns(&rerror, &_dns_group, + _dns_groupbuf, sizeof(_dns_groupbuf), &_dns_state, 0, NULL, 0); + if (rv == NS_SUCCESS) + *retval = &_dns_group; + return rv; +} + +/*ARGSUSED*/ +static int +_dns_getgrent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + rv = __grscan_dns(retval, grp, buffer, buflen, + &_dns_state, 0, NULL, 0); + if (rv == NS_SUCCESS) + *result = grp; + else + *result = NULL; + return rv; +} +/*ARGSUSED*/ +static int +_dns_getgrgid(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + gid_t gid = va_arg(ap, gid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_dns(&_dns_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_dns(&rerror, &_dns_group, + _dns_groupbuf, sizeof(_dns_groupbuf), &_dns_state, 1, NULL, gid); + if (!_dns_state.stayopen) + __grend_dns(&_dns_state); + if (rv == NS_SUCCESS) + *retval = &_dns_group; + return rv; +} + +/*ARGSUSED*/ +static int +_dns_getgrgid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + gid_t gid = va_arg(ap, gid_t); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_dns state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_dns(retval, grp, buffer, buflen, &state, 1, NULL, gid); + __grend_dns(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + +/*ARGSUSED*/ +static int +_dns_getgrnam(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_dns(&_dns_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_dns(&rerror, &_dns_group, + _dns_groupbuf, sizeof(_dns_groupbuf), &_dns_state, 1, name, 0); + if (!_dns_state.stayopen) + __grend_dns(&_dns_state); + if (rv == NS_SUCCESS) + *retval = &_dns_group; + return rv; +} + +/*ARGSUSED*/ +static int +_dns_getgrnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_dns state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_dns(retval, grp, buffer, buflen, &state, 1, name, 0); + __grend_dns(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + +#endif /* HESIOD */ + + +#ifdef YP + /* + * nis methods + */ + +int +__grstart_nis(struct __grstate_nis *state) +{ + + _DIAGASSERT(state != NULL); + + state->done = 0; + if (state->current) { + free(state->current); + state->current = NULL; + } + if (state->domain == NULL) { /* setup NIS */ + switch (yp_get_default_domain(&state->domain)) { + case 0: + break; + case YPERR_RESRC: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + return NS_SUCCESS; +} + +int +__grend_nis(struct __grstate_nis *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->domain) { + state->domain = NULL; + } + state->done = 0; + if (state->current) { + free(state->current); + state->current = NULL; + } + return NS_SUCCESS; +} + +/* + * __grscan_nis + * Search NIS for the next desired entry. + * If search is zero, return the next entry. + * If search is non-zero, look for a specific name (if name != NULL), + * or a specific gid (if name == NULL). + */ +int +__grscan_nis(int *retval, struct group *grp, char *buffer, size_t buflen, + struct __grstate_nis *state, int search, const char *name, gid_t gid) +{ + const char *map; + char *key, *data; + int nisr, rv, keylen, datalen; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + /* name is NULL to indicate searching for gid */ + + *retval = 0; + + if (state->domain == NULL) { /* only start if NIS not setup */ + rv = __grstart_nis(state); + if (rv != NS_SUCCESS) + return rv; + } + + next_nis_entry: + key = NULL; + data = NULL; + rv = NS_SUCCESS; + + if (! search) { /* find next entry */ + if (state->done) /* exhausted search */ + return NS_NOTFOUND; + map = "group.byname"; + if (state->current) { /* already searching */ + nisr = yp_next(state->domain, map, + state->current, state->currentlen, + &key, &keylen, &data, &datalen); + free(state->current); + state->current = NULL; + switch (nisr) { + case 0: + state->current = key; + state->currentlen = keylen; + key = NULL; + break; + case YPERR_NOMORE: + rv = NS_NOTFOUND; + state->done = 1; + break; + default: + rv = NS_UNAVAIL; + break; + } + } else { /* new search */ + if (yp_first(state->domain, map, + &state->current, &state->currentlen, + &data, &datalen)) { + rv = NS_UNAVAIL; + } + } + } else { /* search for specific item */ + if (name) { /* find group name */ + snprintf(buffer, buflen, "%s", name); + map = "group.byname"; + } else { /* find gid */ + snprintf(buffer, buflen, "%u", (unsigned int)gid); + map = "group.bygid"; + } + nisr = yp_match(state->domain, map, buffer, (int)strlen(buffer), + &data, &datalen); + switch (nisr) { + case 0: + break; + case YPERR_KEY: + rv = NS_NOTFOUND; + break; + default: + rv = NS_UNAVAIL; + break; + } + } + if (rv == NS_SUCCESS) { /* validate data */ + data[datalen] = '\0'; /* clear trailing \n */ + if (_gr_parse(data, grp, buffer, buflen)) { + if (! search) { /* just want this one */ + rv = NS_SUCCESS; + } else if ((name && strcmp(name, grp->gr_name) == 0) || + (!name && gid == grp->gr_gid)) { + /* want specific */ + rv = NS_SUCCESS; + } + } else { /* dodgy entry */ + if (!search) { /* try again if ! searching */ + free(data); + goto next_nis_entry; + } + } + } + + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + if (key) + free(key); + if (data) + free(data); + return rv; +} + +static struct __grstate_nis _nis_state; + /* storage for non _r functions */ +static struct group _nis_group; +static char _nis_groupbuf[_GETGR_R_SIZE_MAX]; + +/*ARGSUSED*/ +static int +_nis_setgrent(void *nsrv, void *nscb, va_list ap) +{ + + _nis_state.stayopen = 0; + return __grstart_nis(&_nis_state); +} + +/*ARGSUSED*/ +static int +_nis_setgroupent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + _nis_state.stayopen = stayopen; + rv = __grstart_nis(&_nis_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_nis_endgrent(void *nsrv, void *nscb, va_list ap) +{ + + return __grend_nis(&_nis_state); +} + +/*ARGSUSED*/ +static int +_nis_getgrent(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grscan_nis(&rerror, &_nis_group, + _nis_groupbuf, sizeof(_nis_groupbuf), &_nis_state, 0, NULL, 0); + if (rv == NS_SUCCESS) + *retval = &_nis_group; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getgrent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + rv = __grscan_nis(retval, grp, buffer, buflen, + &_nis_state, 0, NULL, 0); + if (rv == NS_SUCCESS) + *result = grp; + else + *result = NULL; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getgrgid(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + gid_t gid = va_arg(ap, gid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_nis(&_nis_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_nis(&rerror, &_nis_group, + _nis_groupbuf, sizeof(_nis_groupbuf), &_nis_state, 1, NULL, gid); + if (!_nis_state.stayopen) + __grend_nis(&_nis_state); + if (rv == NS_SUCCESS) + *retval = &_nis_group; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getgrgid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + gid_t gid = va_arg(ap, gid_t); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_nis state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_nis(retval, grp, buffer, buflen, &state, 1, NULL, gid); + __grend_nis(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getgrnam(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_nis(&_nis_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_nis(&rerror, &_nis_group, + _nis_groupbuf, sizeof(_nis_groupbuf), &_nis_state, 1, name, 0); + if (!_nis_state.stayopen) + __grend_nis(&_nis_state); + if (rv == NS_SUCCESS) + *retval = &_nis_group; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getgrnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_nis state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_nis(retval, grp, buffer, buflen, &state, 1, name, 0); + __grend_nis(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + +#endif /* YP */ + + +#ifdef _GROUP_COMPAT + /* + * compat methods + */ + +int +__grstart_compat(struct __grstate_compat *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->fp == NULL) { + state->fp = fopen(_PATH_GROUP, "r"); + if (state->fp == NULL) + return NS_UNAVAIL; + } else { + rewind(state->fp); + } + return NS_SUCCESS; +} + +int +__grend_compat(struct __grstate_compat *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->name) { + free(state->name); + state->name = NULL; + } + if (state->fp) { + (void) fclose(state->fp); + state->fp = NULL; + } + return NS_SUCCESS; +} + + +/* + * __grbad_compat + * log an error if "files" or "compat" is specified in + * group_compat database + */ +/*ARGSUSED*/ +int +__grbad_compat(void *nsrv, void *nscb, va_list ap) +{ + static int warned; + + _DIAGASSERT(nsrv != NULL); + _DIAGASSERT(nscb != NULL); + + if (!warned) { + syslog(LOG_ERR, + "nsswitch.conf group_compat database can't use '%s'", + (const char *)nscb); + } + warned = 1; + return NS_UNAVAIL; +} + +/* + * __grscan_compat + * Scan state->fp for the next desired entry. + * If search is zero, return the next entry. + * If search is non-zero, look for a specific name (if name != NULL), + * or a specific gid (if name == NULL). + * Sets *retval to the errno if the result is not NS_SUCCESS or + * NS_NOTFOUND. + * + * searchfunc is invoked when a compat "+" lookup is required; + * searchcookie is passed as the first argument to searchfunc, + * the second argument is the group result. + * This should return NS_NOTFOUND when "no more groups" from compat src. + * If searchfunc is NULL then nsdispatch of getgrent is used. + * This is primarily intended for getgroupmembership(3)'s compat backend. + */ +int +__grscan_compat(int *retval, struct group *grp, char *buffer, size_t buflen, + struct __grstate_compat *state, int search, const char *name, gid_t gid, + int (*searchfunc)(void *, struct group **), void *searchcookie) +{ + int rv; + char filebuf[_GETGR_R_SIZE_MAX], *ep; + + static const ns_dtab compatentdtab[] = { + NS_FILES_CB(__grbad_compat, "files") + NS_DNS_CB(_dns_getgrent_r, NULL) + NS_NIS_CB(_nis_getgrent_r, NULL) + NS_COMPAT_CB(__grbad_compat, "compat") + NS_NULL_CB + }; + static const ns_dtab compatgiddtab[] = { + NS_FILES_CB(__grbad_compat, "files") + NS_DNS_CB(_dns_getgrgid_r, NULL) + NS_NIS_CB(_nis_getgrgid_r, NULL) + NS_COMPAT_CB(__grbad_compat, "compat") + NS_NULL_CB + }; + static const ns_dtab compatnamdtab[] = { + NS_FILES_CB(__grbad_compat, "files") + NS_DNS_CB(_dns_getgrnam_r, NULL) + NS_NIS_CB(_nis_getgrnam_r, NULL) + NS_COMPAT_CB(__grbad_compat, "compat") + NS_NULL_CB + }; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + /* name is NULL to indicate searching for gid */ + + *retval = 0; + + if (state->fp == NULL) { /* only start if file not open yet */ + rv = __grstart_compat(state); + if (rv != NS_SUCCESS) + goto compatgrscan_out; + } + rv = NS_NOTFOUND; + + for (;;) { /* loop through file */ + if (state->name != NULL) { + /* processing compat entry */ + int crv, cretval; + struct group cgrp, *cgrpres; + + if (state->name[0]) { /* specific +group: */ + crv = nsdispatch(NULL, compatnamdtab, + NSDB_GROUP_COMPAT, "getgrnam_r", + __nsdefaultnis, + &cretval, state->name, + &cgrp, filebuf, sizeof(filebuf), &cgrpres); + free(state->name); /* (only check 1 grp) */ + state->name = NULL; + } else if (!search) { /* any group */ + if (searchfunc) { + crv = searchfunc(searchcookie, + &cgrpres); + } else { + crv = nsdispatch(NULL, compatentdtab, + NSDB_GROUP_COMPAT, "getgrent_r", + __nsdefaultnis, + &cretval, &cgrp, filebuf, + sizeof(filebuf), &cgrpres); + } + } else if (name) { /* specific group */ + crv = nsdispatch(NULL, compatnamdtab, + NSDB_GROUP_COMPAT, "getgrnam_r", + __nsdefaultnis, + &cretval, name, + &cgrp, filebuf, sizeof(filebuf), &cgrpres); + } else { /* specific gid */ + crv = nsdispatch(NULL, compatgiddtab, + NSDB_GROUP_COMPAT, "getgrgid_r", + __nsdefaultnis, + &cretval, gid, + &cgrp, filebuf, sizeof(filebuf), &cgrpres); + } + if (crv != NS_SUCCESS) { /* not found */ + free(state->name); + state->name = NULL; + continue; /* try next line */ + } + if (!_gr_copy(cgrpres, grp, buffer, buflen)) { + rv = NS_UNAVAIL; + break; + } + goto compatgrscan_cmpgrp; /* skip to grp test */ + } + + /* get next file line */ + if (fgets(filebuf, sizeof(filebuf), state->fp) == NULL) + break; + + ep = strchr(filebuf, '\n'); + if (ep == NULL) { /* skip lines that are too big */ + int ch; + + while ((ch = getc(state->fp)) != '\n' && ch != EOF) + continue; + continue; + } + *ep = '\0'; /* clear trailing \n */ + + if (filebuf[0] == '+') { /* parse compat line */ + if (state->name) + free(state->name); + state->name = NULL; + switch(filebuf[1]) { + case ':': + case '\0': + state->name = strdup(""); + break; + default: + ep = strchr(filebuf + 1, ':'); + if (ep == NULL) + break; + *ep = '\0'; + state->name = strdup(filebuf + 1); + break; + } + if (state->name == NULL) { + rv = NS_UNAVAIL; + break; + } + continue; + } + + /* validate line */ + if (! _gr_parse(filebuf, grp, buffer, buflen)) { + continue; /* skip bad lines */ + } + + compatgrscan_cmpgrp: + if (! search) { /* just want this one */ + rv = NS_SUCCESS; + break; + } + /* want specific */ + if ((name && strcmp(name, grp->gr_name) == 0) || + (!name && gid == grp->gr_gid)) { + rv = NS_SUCCESS; + break; + } + + } + + compatgrscan_out: + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + return rv; +} + +static struct __grstate_compat _compat_state; + /* storage for non _r functions */ +static struct group _compat_group; +static char _compat_groupbuf[_GETGR_R_SIZE_MAX]; + +/*ARGSUSED*/ +static int +_compat_setgrent(void *nsrv, void *nscb, va_list ap) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(__grbad_compat, "files") + NS_DNS_CB(_dns_setgrent, NULL) + NS_NIS_CB(_nis_setgrent, NULL) + NS_COMPAT_CB(__grbad_compat, "compat") + NS_NULL_CB + }; + + /* force group_compat setgrent() */ + (void) nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "setgrent", + __nsdefaultnis_forceall); + + /* reset state, keep fp open */ + _compat_state.stayopen = 0; + return __grstart_compat(&_compat_state); +} + +/*ARGSUSED*/ +static int +_compat_setgroupent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + static const ns_dtab dtab[] = { + NS_FILES_CB(__grbad_compat, "files") + NS_DNS_CB(_dns_setgroupent, NULL) + NS_NIS_CB(_nis_setgroupent, NULL) + NS_COMPAT_CB(__grbad_compat, "compat") + NS_NULL_CB + }; + + /* force group_compat setgroupent() */ + (void) nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "setgroupent", + __nsdefaultnis_forceall, &rv, stayopen); + + _compat_state.stayopen = stayopen; + rv = __grstart_compat(&_compat_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_compat_endgrent(void *nsrv, void *nscb, va_list ap) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(__grbad_compat, "files") + NS_DNS_CB(_dns_endgrent, NULL) + NS_NIS_CB(_nis_endgrent, NULL) + NS_COMPAT_CB(__grbad_compat, "compat") + NS_NULL_CB + }; + + /* force group_compat endgrent() */ + (void) nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "endgrent", + __nsdefaultnis_forceall); + + /* reset state, close fp */ + _compat_state.stayopen = 0; + return __grend_compat(&_compat_state); +} + +/*ARGSUSED*/ +static int +_compat_getgrent(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grscan_compat(&rerror, &_compat_group, + _compat_groupbuf, sizeof(_compat_groupbuf), + &_compat_state, 0, NULL, 0, NULL, NULL); + if (rv == NS_SUCCESS) + *retval = &_compat_group; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getgrent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + rv = __grscan_compat(retval, grp, buffer, buflen, + &_compat_state, 0, NULL, 0, NULL, NULL); + if (rv == NS_SUCCESS) + *result = grp; + else + *result = NULL; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getgrgid(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + gid_t gid = va_arg(ap, gid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_compat(&_compat_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_compat(&rerror, &_compat_group, + _compat_groupbuf, sizeof(_compat_groupbuf), + &_compat_state, 1, NULL, gid, NULL, NULL); + if (!_compat_state.stayopen) + __grend_compat(&_compat_state); + if (rv == NS_SUCCESS) + *retval = &_compat_group; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getgrgid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + gid_t gid = va_arg(ap, gid_t); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_compat state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_compat(retval, grp, buffer, buflen, &state, + 1, NULL, gid, NULL, NULL); + __grend_compat(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getgrnam(void *nsrv, void *nscb, va_list ap) +{ + struct group **retval = va_arg(ap, struct group **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = __grstart_compat(&_compat_state); + if (rv != NS_SUCCESS) + return rv; + rv = __grscan_compat(&rerror, &_compat_group, + _compat_groupbuf, sizeof(_compat_groupbuf), + &_compat_state, 1, name, 0, NULL, NULL); + if (!_compat_state.stayopen) + __grend_compat(&_compat_state); + if (rv == NS_SUCCESS) + *retval = &_compat_group; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getgrnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct group *grp = va_arg(ap, struct group *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct group **result = va_arg(ap, struct group **); + + struct __grstate_compat state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = __grscan_compat(retval, grp, buffer, buflen, &state, + 1, name, 0, NULL, NULL); + __grend_compat(&state); + if (rv == NS_SUCCESS) + *result = grp; + return rv; +} + +#endif /* _GROUP_COMPAT */ + + + /* + * public functions + */ + +struct group * +getgrent(void) +{ + int rv; + struct group *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getgrent, NULL) + NS_DNS_CB(_dns_getgrent, NULL) + NS_NIS_CB(_nis_getgrent, NULL) + NS_COMPAT_CB(_compat_getgrent, NULL) + NS_NULL_CB + }; + + mutex_lock(&__grmutex); + rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrent", __nsdefaultcompat, + &retval); + mutex_unlock(&__grmutex); + return (rv == NS_SUCCESS) ? retval : NULL; +} + +int +getgrent_r(struct group *grp, char *buffer, size_t buflen, + struct group **result) +{ + int rv, retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getgrent_r, NULL) + NS_DNS_CB(_dns_getgrent_r, NULL) + NS_NIS_CB(_nis_getgrent_r, NULL) + NS_COMPAT_CB(_compat_getgrent_r, NULL) + NS_NULL_CB + }; + + mutex_lock(&__grmutex); + rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrent_r", __nsdefaultcompat, + &retval, grp, buffer, buflen, result); + mutex_unlock(&__grmutex); + switch (rv) { + case NS_SUCCESS: + case NS_NOTFOUND: + return 0; + default: + return retval; + } +} + + +struct group * +getgrgid(gid_t gid) +{ + int rv; + struct group *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getgrgid, NULL) + NS_DNS_CB(_dns_getgrgid, NULL) + NS_NIS_CB(_nis_getgrgid, NULL) + NS_COMPAT_CB(_compat_getgrgid, NULL) + NS_NULL_CB + }; + + mutex_lock(&__grmutex); + rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrgid", __nsdefaultcompat, + &retval, gid); + mutex_unlock(&__grmutex); + return (rv == NS_SUCCESS) ? retval : NULL; +} + +int +getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t buflen, + struct group **result) +{ + int rv, retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getgrgid_r, NULL) + NS_DNS_CB(_dns_getgrgid_r, NULL) + NS_NIS_CB(_nis_getgrgid_r, NULL) + NS_COMPAT_CB(_compat_getgrgid_r, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + retval = 0; + mutex_lock(&__grmutex); + rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrgid_r", __nsdefaultcompat, + &retval, gid, grp, buffer, buflen, result); + mutex_unlock(&__grmutex); + switch (rv) { + case NS_SUCCESS: + case NS_NOTFOUND: + return 0; + default: + return retval; + } +} + +struct group * +getgrnam(const char *name) +{ + int rv; + struct group *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getgrnam, NULL) + NS_DNS_CB(_dns_getgrnam, NULL) + NS_NIS_CB(_nis_getgrnam, NULL) + NS_COMPAT_CB(_compat_getgrnam, NULL) + NS_NULL_CB + }; + + mutex_lock(&__grmutex); + rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrnam", __nsdefaultcompat, + &retval, name); + mutex_unlock(&__grmutex); + return (rv == NS_SUCCESS) ? retval : NULL; +} + +int +getgrnam_r(const char *name, struct group *grp, char *buffer, size_t buflen, + struct group **result) +{ + int rv, retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getgrnam_r, NULL) + NS_DNS_CB(_dns_getgrnam_r, NULL) + NS_NIS_CB(_nis_getgrnam_r, NULL) + NS_COMPAT_CB(_compat_getgrnam_r, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(grp != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + retval = 0; + mutex_lock(&__grmutex); + rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrnam_r", __nsdefaultcompat, + &retval, name, grp, buffer, buflen, result); + mutex_unlock(&__grmutex); + switch (rv) { + case NS_SUCCESS: + case NS_NOTFOUND: + return 0; + default: + return retval; + } +} + +void +endgrent(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_endgrent, NULL) + NS_DNS_CB(_dns_endgrent, NULL) + NS_NIS_CB(_nis_endgrent, NULL) + NS_COMPAT_CB(_compat_endgrent, NULL) + NS_NULL_CB + }; + + mutex_lock(&__grmutex); + /* force all endgrent() methods */ + (void) nsdispatch(NULL, dtab, NSDB_GROUP, "endgrent", + __nsdefaultcompat_forceall); + mutex_unlock(&__grmutex); +} + +int +setgroupent(int stayopen) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_setgroupent, NULL) + NS_DNS_CB(_dns_setgroupent, NULL) + NS_NIS_CB(_nis_setgroupent, NULL) + NS_COMPAT_CB(_compat_setgroupent, NULL) + NS_NULL_CB + }; + int rv, retval; + + mutex_lock(&__grmutex); + /* force all setgroupent() methods */ + rv = nsdispatch(NULL, dtab, NSDB_GROUP, "setgroupent", + __nsdefaultcompat_forceall, &retval, stayopen); + mutex_unlock(&__grmutex); + return (rv == NS_SUCCESS) ? retval : 0; +} + +void +setgrent(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_setgrent, NULL) + NS_DNS_CB(_dns_setgrent, NULL) + NS_NIS_CB(_nis_setgrent, NULL) + NS_COMPAT_CB(_compat_setgrent, NULL) + NS_NULL_CB + }; + + mutex_lock(&__grmutex); + /* force all setgrent() methods */ + (void) nsdispatch(NULL, dtab, NSDB_GROUP, "setgrent", + __nsdefaultcompat_forceall); + mutex_unlock(&__grmutex); +} diff --git a/lib/nbsd_libc/gen/getgrouplist.3 b/lib/nbsd_libc/gen/getgrouplist.3 new file mode 100644 index 000000000..7638ab027 --- /dev/null +++ b/lib/nbsd_libc/gen/getgrouplist.3 @@ -0,0 +1,152 @@ +.\" $NetBSD: getgrouplist.3,v 1.14 2008/05/02 18:11:04 martin Exp $ +.\" +.\" Copyright (c) 2005 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Luke Mewburn. +.\" +.\" 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. +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)getgrouplist.3 8.1 (Berkeley) 6/9/93 +.\" +.Dd January 6, 2005 +.Dt GETGROUPLIST 3 +.Os +.Sh NAME +.Nm getgrouplist , +.Nm getgroupmembership , +.Nd calculate group access list +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn getgrouplist "const char *name" "gid_t basegid" "gid_t *groups" "int *ngroups" +.Ft int +.Fn getgroupmembership "const char *name" "gid_t basegid" "gid_t *groups" "int maxgrp" "int *ngroups" +.Sh DESCRIPTION +The +.Fn getgrouplist +and +.Fn getgroupmembership +functions read through the group database and calculate +the group access list for the user specified in +.Fa name . +The +.Fa basegid +is automatically included in the groups list. +Typically this value is given as +the group number from the password database. +.Pp +The resulting group list is returned in the integer array pointed to by +.Fa groups . +.Pp +For +.Fn getgrouplist , +the caller specifies the size of the +.Fa groups +array in the integer pointed to by +.Fa ngroups . +.Pp +For +.Fn getgroupmembership , +the caller specifies the size of the +.Fa groups +array in +.Fa maxgrp . +.Pp +The actual number of groups found is returned in +.Fa ngroups . +.Pp +Duplicate group ids will be suppressed from the result. +.Sh RETURN VALUES +The +.Fn getgrouplist +and +.Fn getgroupmembership +functions +return 0 if successful, and +return \-1 if the size of the group list is too small to +hold all the user's groups. +In the latter case, the +.Fa groups +array will be filled with as many groups as will fit and +.Fa ngroups +will contain the total number of groups found. +.Sh FILES +.Bl -tag -width /etc/group -compact +.It Pa /etc/group +group membership list +.El +.Sh SEE ALSO +.Xr setgroups 2 , +.Xr initgroups 3 , +.Xr group 5 +.Sh HISTORY +The +.Fn getgrouplist +function first appeared in +.Bx 4.4 . +The +.Fn getgroupmembership +function first appeared in +.Nx 3.0 +to address an API deficiency in +.Fn getgrouplist . +.Sh BUGS +The +.Fn getgrouplist +function +uses the routines based on +.Xr getgrent 3 . +If the invoking program uses any of these routines, +the group structure will +be overwritten in the call to +.Fn getgrouplist . diff --git a/lib/nbsd_libc/gen/getgrouplist.c b/lib/nbsd_libc/gen/getgrouplist.c new file mode 100644 index 000000000..cd152ec7d --- /dev/null +++ b/lib/nbsd_libc/gen/getgrouplist.c @@ -0,0 +1,70 @@ +/* $NetBSD: getgrouplist.c,v 1.22 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2004-2005 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getgrouplist.c 8.2 (Berkeley) 12/8/94"; +#else +__RCSID("$NetBSD: getgrouplist.c,v 1.22 2008/04/28 20:22:59 martin Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * calculate group access list + */ + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getgrouplist,_getgrouplist) +#endif + +int +getgrouplist(const char *uname, gid_t agroup, gid_t *groups, int *grpcnt) +{ + int rv, groupc; + + _DIAGASSERT(uname != NULL); + /* groups may be NULL if just sizing when invoked with *grpcnt = 0 */ + _DIAGASSERT(grpcnt != NULL); + + groupc = 0; + rv = getgroupmembership(uname, agroup, groups, *grpcnt, &groupc); + *grpcnt = groupc; /* set groupc to the actual # of groups */ + return rv; +} diff --git a/lib/nbsd_libc/gen/getgroupmembership.c b/lib/nbsd_libc/gen/getgroupmembership.c new file mode 100644 index 000000000..78687c0a5 --- /dev/null +++ b/lib/nbsd_libc/gen/getgroupmembership.c @@ -0,0 +1,405 @@ +/* $NetBSD: getgroupmembership.c,v 1.4 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2004-2005 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getgroupmembership.c,v 1.4 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +/* + * calculate group access list + */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HESIOD +#include +#endif + +#include "gr_private.h" + +#ifdef __weak_alias +__weak_alias(getgroupmembership,_getgroupmembership) +#endif + +/* + * __gr_addgid + * Add gid to the groups array (of maxgrp size) at the position + * indicated by *groupc, unless it already exists or *groupc is + * past &groups[maxgrp]. + * Returns 1 upon success (including duplicate suppression), 0 otherwise. + */ +static int +__gr_addgid(gid_t gid, gid_t *groups, int maxgrp, int *groupc) +{ + int ret, dupc; + + _DIAGASSERT(groupc != NULL); + _DIAGASSERT(groups != NULL); + + /* skip duplicates */ + for (dupc = 0; dupc < MIN(maxgrp, *groupc); dupc++) { + if (groups[dupc] == gid) + return 1; + } + + ret = 1; + if (*groupc < maxgrp) /* add this gid */ + groups[*groupc] = gid; + else + ret = 0; + (*groupc)++; + return ret; +} + + +/*ARGSUSED*/ +static int +_files_getgroupmembership(void *retval, void *cb_data, va_list ap) +{ + int *result = va_arg(ap, int *); + const char *uname = va_arg(ap, const char *); + gid_t agroup = va_arg(ap, gid_t); + gid_t *groups = va_arg(ap, gid_t *); + int maxgrp = va_arg(ap, int); + int *groupc = va_arg(ap, int *); + + struct __grstate_files state; + struct group grp; + char grpbuf[_GETGR_R_SIZE_MAX]; + int rv, i; + + _DIAGASSERT(result != NULL); + _DIAGASSERT(uname != NULL); + /* groups may be NULL if just sizing when invoked with maxgrp = 0 */ + _DIAGASSERT(groupc != NULL); + + /* install primary group */ + (void) __gr_addgid(agroup, groups, maxgrp, groupc); + + memset(&state, 0, sizeof(state)); + while (__grscan_files(&rv, &grp, grpbuf, sizeof(grpbuf), &state, + 0, NULL, 0) == NS_SUCCESS) { + /* scan members */ + for (i = 0; grp.gr_mem[i]; i++) { + if (strcmp(grp.gr_mem[i], uname) != 0) + continue; + if (! __gr_addgid(grp.gr_gid, groups, maxgrp, groupc)) + *result = -1; + break; + } + } + __grend_files(&state); + return NS_NOTFOUND; +} + + +#ifdef HESIOD + +/*ARGSUSED*/ +static int +_dns_getgroupmembership(void *retval, void *cb_data, va_list ap) +{ + int *result = va_arg(ap, int *); + const char *uname = va_arg(ap, const char *); + gid_t agroup = va_arg(ap, gid_t); + gid_t *groups = va_arg(ap, gid_t *); + int maxgrp = va_arg(ap, int); + int *groupc = va_arg(ap, int *); + + struct __grstate_dns state; + struct group grp; + char grpbuf[_GETGR_R_SIZE_MAX]; + unsigned long id; + void *context; + char **hp, *cp, *ep; + int rv, i; + + _DIAGASSERT(result != NULL); + _DIAGASSERT(uname != NULL); + /* groups may be NULL if just sizing when invoked with maxgrp = 0 */ + _DIAGASSERT(groupc != NULL); + + /* install primary group */ + (void) __gr_addgid(agroup, groups, maxgrp, groupc); + + hp = NULL; + rv = NS_NOTFOUND; + + if (hesiod_init(&context) == -1) /* setup hesiod */ + return NS_UNAVAIL; + + hp = hesiod_resolve(context, uname, "grplist"); /* find grplist */ + if (hp == NULL) { + if (errno != ENOENT) { /* wasn't "not found"*/ + rv = NS_UNAVAIL; + goto dnsgroupmembers_out; + } + /* grplist not found, fallback to _dns_grscan */ + memset(&state, 0, sizeof(state)); + while (__grscan_dns(&rv, &grp, grpbuf, sizeof(grpbuf), &state, + 0, NULL, 0) == NS_SUCCESS) { + /* scan members */ + for (i = 0; grp.gr_mem[i]; i++) { + if (strcmp(grp.gr_mem[i], uname) != 0) + continue; + if (! __gr_addgid(grp.gr_gid, groups, maxgrp, + groupc)) + *result = -1; + break; + } + } + __grend_dns(&state); + rv = NS_NOTFOUND; + goto dnsgroupmembers_out; + } + + if ((ep = strchr(hp[0], '\n')) != NULL) + *ep = '\0'; /* clear trailing \n */ + + for (cp = hp[0]; *cp != '\0'; ) { /* parse grplist */ + if ((cp = strchr(cp, ':')) == NULL) /* skip grpname */ + break; + cp++; + id = strtoul(cp, &ep, 10); /* parse gid */ + if (id > GID_MAX || (*ep != ':' && *ep != '\0')) { + rv = NS_UNAVAIL; + goto dnsgroupmembers_out; + } + cp = ep; + if (*cp == ':') + cp++; + + /* add gid */ + if (! __gr_addgid((gid_t)id, groups, maxgrp, groupc)) + *result = -1; + } + + rv = NS_NOTFOUND; + + dnsgroupmembers_out: + if (hp) + hesiod_free_list(context, hp); + hesiod_end(context); + return rv; +} + +#endif /* HESIOD */ + + +#ifdef YP + +/*ARGSUSED*/ +static int +_nis_getgroupmembership(void *retval, void *cb_data, va_list ap) +{ + int *result = va_arg(ap, int *); + const char *uname = va_arg(ap, const char *); + gid_t agroup = va_arg(ap, gid_t); + gid_t *groups = va_arg(ap, gid_t *); + int maxgrp = va_arg(ap, int); + int *groupc = va_arg(ap, int *); + + struct __grstate_nis state; + struct group grp; + char grpbuf[_GETGR_R_SIZE_MAX]; + int rv, i; + + _DIAGASSERT(result != NULL); + _DIAGASSERT(uname != NULL); + /* groups may be NULL if just sizing when invoked with maxgrp = 0 */ + _DIAGASSERT(groupc != NULL); + + /* install primary group */ + (void) __gr_addgid(agroup, groups, maxgrp, groupc); + + memset(&state, 0, sizeof(state)); + while (__grscan_nis(&rv, &grp, grpbuf, sizeof(grpbuf), &state, + 0, NULL, 0) == NS_SUCCESS) { + /* scan members */ + for (i = 0; grp.gr_mem[i]; i++) { + if (strcmp(grp.gr_mem[i], uname) != 0) + continue; + if (! __gr_addgid(grp.gr_gid, groups, maxgrp, groupc)) + *result = -1; + break; + } + } + __grend_nis(&state); + + return NS_NOTFOUND; +} + +#endif /* YP */ + + +#ifdef _GROUP_COMPAT + +struct __compatggm { + const char *uname; /* user to search for */ + gid_t *groups; + gid_t agroup; + int maxgrp; + int *groupc; +}; + +static int +_compat_ggm_search(void *cookie, struct group **groupres) +{ + struct __compatggm *cp; + int rerror, crv; + + static const ns_dtab dtab[] = { + NS_FILES_CB(__grbad_compat, "files") + NS_DNS_CB(_dns_getgroupmembership, NULL) + NS_NIS_CB(_nis_getgroupmembership, NULL) + NS_COMPAT_CB(__grbad_compat, "compat") + NS_NULL_CB + }; + + *groupres = NULL; /* we don't care about this */ + cp = (struct __compatggm *)cookie; + + crv = nsdispatch(NULL, dtab, + NSDB_GROUP_COMPAT, "getgroupmembership", + __nsdefaultnis, + &rerror, cp->uname, cp->agroup, cp->groups, cp->maxgrp, cp->groupc); + + if (crv == NS_SUCCESS) + crv = NS_NOTFOUND; /* indicate "no more +: entries" */ + + return crv; +} + +/* ARGSUSED */ +static int +_compat_getgroupmembership(void *retval, void *cb_data, va_list ap) +{ + int *result = va_arg(ap, int *); + const char *uname = va_arg(ap, const char *); + gid_t agroup = va_arg(ap, gid_t); + gid_t *groups = va_arg(ap, gid_t *); + int maxgrp = va_arg(ap, int); + int *groupc = va_arg(ap, int *); + + struct __grstate_compat state; + struct __compatggm ggmstate; + struct group grp; + char grpbuf[_GETGR_R_SIZE_MAX]; + int rv, i; + + _DIAGASSERT(result != NULL); + _DIAGASSERT(uname != NULL); + /* groups may be NULL if just sizing when invoked with maxgrp = 0 */ + _DIAGASSERT(groupc != NULL); + + /* install primary group */ + (void) __gr_addgid(agroup, groups, maxgrp, groupc); + + memset(&state, 0, sizeof(state)); + memset(&ggmstate, 0, sizeof(ggmstate)); + ggmstate.uname = uname; + ggmstate.groups = groups; + ggmstate.agroup = agroup; + ggmstate.maxgrp = maxgrp; + ggmstate.groupc = groupc; + + while (__grscan_compat(&rv, &grp, grpbuf, sizeof(grpbuf), &state, + 0, NULL, 0, _compat_ggm_search, &ggmstate) + == NS_SUCCESS) { + /* scan members */ + for (i = 0; grp.gr_mem[i]; i++) { + if (strcmp(grp.gr_mem[i], uname) != 0) + continue; + if (! __gr_addgid(grp.gr_gid, groups, maxgrp, groupc)) + *result = -1; + break; + } + } + + __grend_compat(&state); + return NS_NOTFOUND; +} + +#endif /* _GROUP_COMPAT */ + + +int +getgroupmembership(const char *uname, gid_t agroup, + gid_t *groups, int maxgrp, int *groupc) +{ + int rerror; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getgroupmembership, NULL) + NS_DNS_CB(_dns_getgroupmembership, NULL) + NS_NIS_CB(_nis_getgroupmembership, NULL) + NS_COMPAT_CB(_compat_getgroupmembership, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(uname != NULL); + /* groups may be NULL if just sizing when invoked with maxgrp = 0 */ + _DIAGASSERT(groupc != NULL); + + *groupc = 0; + + mutex_lock(&__grmutex); + /* + * Call each backend. + * For compatibility with getgrent(3) semantics, + * a backend should return NS_NOTFOUND even upon + * completion, to allow result merging to occur. + */ + (void) nsdispatch(NULL, dtab, NSDB_GROUP, "getgroupmembership", + __nsdefaultcompat, + &rerror, uname, agroup, groups, maxgrp, groupc); + mutex_unlock(&__grmutex); + + if (*groupc > maxgrp) /* too many groups found */ + return -1; + else + return 0; +} diff --git a/lib/nbsd_libc/gen/gethostname.3 b/lib/nbsd_libc/gen/gethostname.3 new file mode 100644 index 000000000..23b5be267 --- /dev/null +++ b/lib/nbsd_libc/gen/gethostname.3 @@ -0,0 +1,101 @@ +.\" $NetBSD: gethostname.3,v 1.17 2006/02/07 18:59:56 wiz Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)gethostname.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd October 14, 2005 +.Dt GETHOSTNAME 3 +.Os +.Sh NAME +.Nm gethostname , +.Nm sethostname +.Nd get/set name of current host +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn gethostname "char *name" "size_t namelen" +.Ft int +.Fn sethostname "const char *name" "size_t namelen" +.Sh DESCRIPTION +.Fn gethostname +returns the standard host name for the current processor, as +previously set by +.Fn sethostname . +The parameter +.Fa namelen +specifies the size of the +.Fa name +array. +The returned name is null-terminated unless insufficient +space is provided. +.Pp +.Fn sethostname +sets the name of the host machine to be +.Fa name , +which has length +.Fa namelen . +This call is restricted to the super-user and +is normally used only when the system is bootstrapped. +.Sh RETURN VALUES +If the call succeeds a value of 0 is returned. +If the call fails, a value of \-1 is returned and an error code is +placed in the global location +.Va errno . +.Sh ERRORS +If the +.Fn gethostname +or +.Fn sethostname +functions fail, +they will set +.Va errno +for any of the errors specified for the routine +.Xr sysctl 3 . +.Sh SEE ALSO +.Xr gethostid 3 , +.Xr sysctl 3 , +.Xr sysctl 8 +.Sh STANDARDS +The +.Fn gethostname +function conforms to +.St -xpg4.2 . +.Sh HISTORY +The +.Nm +function call appeared in +.Bx 4.2 . +.Sh BUGS +Host names are limited to +.Dv MAXHOSTNAMELEN +(from +.Ao Pa sys/param.h Ac ) +characters including null-termination, currently 256. diff --git a/lib/nbsd_libc/gen/gethostname.c b/lib/nbsd_libc/gen/gethostname.c new file mode 100644 index 000000000..eba55d1ca --- /dev/null +++ b/lib/nbsd_libc/gen/gethostname.c @@ -0,0 +1,70 @@ +/* $NetBSD: gethostname.c,v 1.12 2005/10/14 23:53:41 christos Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)gethostname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: gethostname.c,v 1.12 2005/10/14 23:53:41 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(gethostname,_gethostname) +#endif + +int +gethostname(name, namelen) + char *name; + size_t namelen; +{ + int mib[2]; + size_t size; + + _DIAGASSERT(name != NULL); + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTNAME; + size = namelen; + if (sysctl(mib, 2, name, &size, NULL, 0) == -1) + return (-1); + + return (0); +} diff --git a/lib/nbsd_libc/gen/getlastlogx.3 b/lib/nbsd_libc/gen/getlastlogx.3 new file mode 100644 index 000000000..cb7404c31 --- /dev/null +++ b/lib/nbsd_libc/gen/getlastlogx.3 @@ -0,0 +1,173 @@ +.\" $NetBSD: getlastlogx.3,v 1.2 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2003 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by 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. +.\" +.Dd August 26, 2003 +.Dt GETLASTLOGX 3 +.Os +.Sh NAME +.Nm getlastlogx , +.Nm getutmp , +.Nm getutmpx , +.Nm updlastlogx , +.Nm updwtmpx , +.Nm utmpxname +.Nd user accounting database functions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In utmpx.h +.Ft struct lastlogx * +.Fn getlastlogx "const char *fname" "uid_t uid" "struct lastlogx *ll" +.Ft void +.Fn getutmp "const struct utmpx *ux" "struct utmp *u" +.Ft void +.Fn getutmpx "const struct utmp *u" "struct utmpx *ux" +.Ft int +.Fn updlastlogx "const char *fname" "uid_t uid" "struct lastlogx *ll" +.Ft int +.Fn updwtmpx "const char *file" "const struct utmpx *utx" +.Ft int +.Fn utmpxname "const char *fname" +.Sh DESCRIPTION +The +.Fn getlastlogx +function looks up the entry for the user with user id +.Fa uid +in the +.Xr lastlogx 5 +file given by +.Fa fname +and returns it in +.Fa \&ll . +If the provided +.Fa \&ll +is +.Dv NULL , +the necessary space will be allocated by +.Fn getlastlogx +and should be +.Fn free Ns d +by the caller. +.Pp +The +.Fn getutmp +function fills out the entries in the struct utmp +.Fa u +with the data provided in the struct utmpx +.Fa ux . +.Fn getutmpx +does the opposite, filling out the entries in the struct utmpx +.Fa ux +with the data provided in the struct utmp +.Fa u , +and initializing all the unknown fields to 0. +The sole exception is the +.Fa ut_type +field, which will be initialized to +.Dv USER_PROCESS . +.Pp +The +.Fn updlastlogx +function tries to update the information for the user with the user id +.Fa uid +in the +.Xr lastlogx 5 +file given by +.Fa fname +with the data supplied in +.Fa \&ll . +A +.Ft struct lastlogx +is defined like this: +.Bd -literal +struct lastlogx { + struct timeval ll_tv; /* time entry was created */ + char ll_line[_UTX_LINESIZE]; /* tty name */ + char ll_host[_UTX_HOSTSIZE]; /* host name */ + struct sockaddr_storage ll_ss; /* address where entry was made from */ +}; +.Ed +All the fields should be filled out by the caller. +.Pp +The +.Fn updwtmpx +function updates the +.Xr wtmpx 5 +file +.Fa file +with the +.Xr utmpx 5 +entry +.Fa utx . +.Pp +The +.Fn utmpxname +function sets the default +.Xr utmpx 5 +database file name to +.Fa fname . +.Sh RETURN VALUES +.Fn getlastlogx +returns the found entry on success, or +.Dv NULL +if it could not open the database, could not find an entry matching +.Fa uid +in there, or could not allocate the necessary space (in case +.Fa \&ll +was +.Dv NULL ) . +.Pp +.Fn utmpxname +returns 1 on success, or 0 if the supplied file name was too long or +did not end with +.Sq x . +.Pp +.Fn updlastlogx +and +.Fn updwtmpx +return 0 on success, or \-1 in case the database or file respectively +could not be opened or the data not written into it. +.Sh SEE ALSO +.Xr endutxent 3 , +.Xr loginx 3 , +.Xr utmpx 5 +.Sh HISTORY +The functions +.Fn getutmp , +.Fn getutmpx , +.Fn updwtmpx , +and +.Fn utmpxname +first appeared in +.Tn Solaris . +.Nm getlastlogx +and +.Nm updlastlogx +first appeared in +.Nx 2.0 . diff --git a/lib/nbsd_libc/gen/getloadavg.3 b/lib/nbsd_libc/gen/getloadavg.3 new file mode 100644 index 000000000..954992a97 --- /dev/null +++ b/lib/nbsd_libc/gen/getloadavg.3 @@ -0,0 +1,66 @@ +.\" $NetBSD: getloadavg.3,v 1.14 2003/08/07 16:42:50 agc 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. +.\" +.\" @(#)getloadavg.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt GETLOADAVG 3 +.Os +.Sh NAME +.Nm getloadavg +.Nd get system load averages +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft int +.Fn getloadavg "double loadavg[]" "int nelem" +.Sh DESCRIPTION +The +.Fn getloadavg +function returns the number of processes in the system run queue +averaged over various periods of time. +Up to +.Fa nelem +samples are retrieved and assigned to successive elements of +.Fa loadavg Ns Bq . +The system imposes a maximum of 3 samples, representing averages +over the last 1, 5, and 15 minutes, respectively. +.Sh DIAGNOSTICS +If the load average was unobtainable, \-1 is returned; otherwise, +the number of samples actually retrieved is returned. +.Sh SEE ALSO +.Xr uptime 1 , +.Xr kvm_getloadavg 3 , +.Xr sysctl 3 +.Sh HISTORY +The +.Fn getloadavg +function appeared in +.Bx 4.3 Reno . diff --git a/lib/nbsd_libc/gen/getloadavg.c b/lib/nbsd_libc/gen/getloadavg.c new file mode 100644 index 000000000..870bb3139 --- /dev/null +++ b/lib/nbsd_libc/gen/getloadavg.c @@ -0,0 +1,84 @@ +/* $NetBSD: getloadavg.c,v 1.13 2003/08/07 16:42:50 agc Exp $ */ + +/*- + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getloadavg.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getloadavg.c,v 1.13 2003/08/07 16:42:50 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getloadavg,_getloadavg) +#endif + +/* + * getloadavg() -- Get system load averages. + * + * Put `nelem' samples into `loadavg' array. + * Return number of samples retrieved, or -1 on error. + */ +int +getloadavg(loadavg, nelem) + double loadavg[]; + int nelem; +{ + struct loadavg loadinfo; + int i, mib[2]; + size_t size; + + _DIAGASSERT(loadavg != NULL); + _DIAGASSERT(nelem >= 0); + + mib[0] = CTL_VM; + mib[1] = VM_LOADAVG; + size = sizeof(loadinfo); + if (sysctl(mib, 2, &loadinfo, &size, NULL, 0) < 0) + return (-1); + + nelem = MIN((size_t) nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t)); + for (i = 0; i < nelem; i++) + loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale; + return (nelem); +} diff --git a/lib/nbsd_libc/gen/getlogin.c b/lib/nbsd_libc/gen/getlogin.c new file mode 100644 index 000000000..b0abc58d4 --- /dev/null +++ b/lib/nbsd_libc/gen/getlogin.c @@ -0,0 +1,146 @@ +/* $NetBSD: getlogin.c,v 1.15 2009/01/11 02:46:27 christos Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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 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. + */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getlogin.c,v 1.15 2009/01/11 02:46:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include "reentrant.h" +#include "extern.h" + +#ifdef __weak_alias +__weak_alias(getlogin,_getlogin) +__weak_alias(getlogin_r,_getlogin_r) +__weak_alias(setlogin,_setlogin) +#endif + +int __logname_valid; /* known to setlogin() */ +static char logname[MAXLOGNAME + 1]; + +#ifdef _REENTRANT +static mutex_t logname_mutex = MUTEX_INITIALIZER; +#endif + +char * +getlogin(void) +{ + char *rv; + + mutex_lock(&logname_mutex); + if (__logname_valid == 0) { + if (__getlogin(logname, sizeof(logname) - 1) < 0) { + mutex_unlock(&logname_mutex); + return ((char *)NULL); + } + __logname_valid = 1; + } + rv = (*logname ? logname : (char *)NULL); + mutex_unlock(&logname_mutex); + + return rv; +} + +int +getlogin_r(char *name, size_t namelen) +{ + size_t len; + int rv; + + mutex_lock(&logname_mutex); + if (__logname_valid == 0) { + if (__getlogin(logname, sizeof(logname) - 1) < 0) { + rv = errno; + mutex_unlock(&logname_mutex); + return (rv); + } + __logname_valid = 1; + } + len = strlen(logname) + 1; + if (len > namelen) { + rv = ERANGE; + } else { + strncpy(name, logname, len); + rv = 0; + } + mutex_unlock(&logname_mutex); + + return (rv); +} + +int +setlogin(const char *name) +{ + int retval; + + retval = __setlogin(name); + __logname_valid = 0; + + return (retval); +} diff --git a/lib/nbsd_libc/gen/getmntinfo.3 b/lib/nbsd_libc/gen/getmntinfo.3 new file mode 100644 index 000000000..1b2227b99 --- /dev/null +++ b/lib/nbsd_libc/gen/getmntinfo.3 @@ -0,0 +1,115 @@ +.\" $NetBSD: getmntinfo.3,v 1.15 2004/05/05 08:48:36 jdolecek 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. +.\" +.\" @(#)getmntinfo.3 8.1 (Berkeley) 6/9/93 +.\" +.Dd April 14, 2004 +.Dt GETMNTINFO 3 +.Os +.Sh NAME +.Nm getmntinfo +.Nd get information about mounted file systems +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/statvfs.h +.Ft int +.Fn getmntinfo "struct statvfs **mntbufp" "int flags" +.Sh DESCRIPTION +The +.Fn getmntinfo +function returns an array of +.Em statvfs +structures describing each currently mounted file system (see +.Xr statvfs 2 ) . +.Pp +The +.Fn getmntinfo +function +passes its +.Fa flags +parameter transparently to +.Xr getvfsstat 2 . +.Sh RETURN VALUES +On successful completion, +.Fn getmntinfo +returns a count of the number of elements in the array. +The pointer to the array is stored into +.Fa mntbufp . +.Pp +If an error occurs, zero is returned and the external variable +.Va errno +is set to indicate the error. +Although the pointer +.Fa mntbufp +will be unmodified, any information previously returned by +.Fn getmntinfo +will be lost. +.Sh ERRORS +The +.Fn getmntinfo +function +may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr getvfsstat 2 +or +.Xr malloc 3 . +.Sh SEE ALSO +.Xr getvfsstat 2 , +.Xr mount 2 , +.Xr statvfs 2 , +.Xr mount 8 +.Sh HISTORY +The +.Fn getmntinfo +function first appeared in +.Bx 4.4 . +It was converted from using +.Xr getfsstat 2 +to +.Xr getvfsstat 2 +in +.Nx 3.0 . +.Sh BUGS +The +.Fn getmntinfo +function writes the array of structures to an internal static object +and returns +a pointer to that object. +Subsequent calls to +.Fn getmntinfo +will modify the same object. +.Pp +The memory allocated by +.Fn getmntinfo +cannot be +.Xr free 3 Ns 'd +by the application. diff --git a/lib/nbsd_libc/gen/getmntinfo.c b/lib/nbsd_libc/gen/getmntinfo.c new file mode 100644 index 000000000..62eca752b --- /dev/null +++ b/lib/nbsd_libc/gen/getmntinfo.c @@ -0,0 +1,81 @@ +/* $NetBSD: getmntinfo.c,v 1.16 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getmntinfo.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getmntinfo.c,v 1.16 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include + +/* + * Return information about mounted filesystems. + */ +int +getmntinfo(mntbufp, flags) + struct statvfs **mntbufp; + int flags; +{ + static struct statvfs *mntbuf; + static int mntsize; + static size_t bufsize; + + _DIAGASSERT(mntbufp != NULL); + + if (mntsize <= 0 && + (mntsize = getvfsstat(NULL, (size_t)0, MNT_NOWAIT)) == -1) + return (0); + if (bufsize > 0 && + (mntsize = getvfsstat(mntbuf, bufsize, flags)) == -1) + return (0); + while (bufsize <= mntsize * sizeof(struct statvfs)) { + if (mntbuf) + free(mntbuf); + bufsize = (mntsize + 1) * sizeof(struct statvfs); + if ((mntbuf = malloc(bufsize)) == NULL) + return (0); + if ((mntsize = getvfsstat(mntbuf, bufsize, flags)) == -1) + return (0); + } + *mntbufp = mntbuf; + return (mntsize); +} diff --git a/lib/nbsd_libc/gen/getnetgrent.3 b/lib/nbsd_libc/gen/getnetgrent.3 new file mode 100644 index 000000000..7e456937e --- /dev/null +++ b/lib/nbsd_libc/gen/getnetgrent.3 @@ -0,0 +1,124 @@ +.\" $NetBSD: getnetgrent.3,v 1.12 2003/08/07 16:42:50 agc 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. +.\" +.\" @(#)getnetgrent.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd January 16, 1999 +.Dt GETNETGRENT 3 +.Os +.Sh NAME +.Nm getnetgrent , +.Nm innetgr , +.Nm setnetgrent , +.Nm endnetgrent +.Nd netgroup database operations +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In netgroup.h +.Ft int +.Fn getnetgrent "const char **host" "const char **user" "const char **domain" +.Ft int +.Fn innetgr "const char *netgroup" "const char *host" "const char *user" "const char *domain" +.Ft void +.Fn setnetgrent "const char *netgroup" +.Ft void +.Fn endnetgrent void +.Sh DESCRIPTION +These functions operate on the netgroup database file which is described in +.Xr netgroup 5 . +.Pp +The database defines a set of netgroups, each made up of one or more triples: +.Bd -literal -offset indent +(host, user, domain) +.Ed +.Pp +that defines a combination of host, user and domain. +Any of the three fields may be specified as ``wildcards'' that match any +string. +.Pp +The function +.Fn getnetgrent +sets the three pointer arguments to the strings of the next member of the +current netgroup. +If any of the string pointers are +.Dv NULL +that field is considered a wildcard. +.Pp +The functions +.Fn setnetgrent +and +.Fn endnetgrent +set the current netgroup and terminate the current netgroup respectively. +If +.Fn setnetgrent +is called with a different netgroup than the previous call, an implicit +.Fn endnetgrent +is implied. +.Fn setnetgrent +also sets the offset to the first member of the netgroup. +.Pp +The function +.Fn innetgr +searches for a match of all fields within the specified group. +If any of the +.Sy host , +.Sy user , +or +.Sy domain +arguments are +.Dv NULL +those fields will match any string value in the netgroup member. +.Sh RETURN VALUES +The function +.Fn getnetgrent +returns 0 for ``no more netgroup members'' and 1 otherwise. +The function +.Fn innetgr +returns 1 for a successful match and 0 otherwise. +The functions +.Fn setnetgrent +and +.Fn endnetgrent +have no return value. +.Sh FILES +.Bl -tag -width /etc/netgroup -compact +.It Pa /etc/netgroup +netgroup database file +.El +.Sh SEE ALSO +.Xr netgroup 5 , +.Xr nsswitch.conf 5 +.Sh BUGS +The function +.Fn getnetgrent +returns pointers to dynamically allocated data areas that are free'd when +the function +.Fn endnetgrent +is called. diff --git a/lib/nbsd_libc/gen/getnetgrent.c b/lib/nbsd_libc/gen/getnetgrent.c new file mode 100644 index 000000000..80eb129f3 --- /dev/null +++ b/lib/nbsd_libc/gen/getnetgrent.c @@ -0,0 +1,991 @@ +/* $NetBSD: getnetgrent.c,v 1.41 2009/10/21 01:07:45 snj Exp $ */ + +/* + * Copyright (c) 1994 Christos Zoulas + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getnetgrent.c,v 1.41 2009/10/21 01:07:45 snj Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include +#define _NETGROUP_PRIVATE +#include +#include +#include +#include +#include +#include +#include + +#ifdef YP +#include +#include +#include +#endif + +#ifdef __weak_alias +__weak_alias(endnetgrent,_endnetgrent) +__weak_alias(getnetgrent,_getnetgrent) +__weak_alias(innetgr,_innetgr) +__weak_alias(setnetgrent,_setnetgrent) +#endif + +#define _NG_STAR(s) (((s) == NULL || *(s) == '\0') ? _ngstar : s) +#define _NG_EMPTY(s) ((s) == NULL ? "" : s) +#define _NG_ISSPACE(p) (isspace((unsigned char) (p)) || (p) == '\n') + +static const char _ngstar[] = "*"; +static struct netgroup *_nghead = NULL; +static struct netgroup *_nglist = NULL; +static DB *_ng_db; + +static int getstring(char **, int, __aconst char **); +static struct netgroup *getnetgroup(char **); +static int lookup(char *, char **, int); +static int addgroup(StringList *, char *); +static int in_check(const char *, const char *, const char *, + struct netgroup *); +static int in_find(StringList *, char *, const char *, const char *, + const char *); +static char *in_lookup1(const char *, const char *, int); +static int in_lookup(const char *, const char *, const char *, int); + +#ifdef NSSRC_FILES +static const ns_src default_files_nis[] = { + { NSSRC_FILES, NS_SUCCESS | NS_NOTFOUND }, +#ifdef YP + { NSSRC_NIS, NS_SUCCESS }, +#endif + { 0, 0 }, +}; +#endif + +/* + * getstring(): Get a string delimited by the character, skipping leading and + * trailing blanks and advancing the pointer + */ +static int +getstring(char **pp, int del, char __aconst **str) +{ + size_t len; + char *sp, *ep, *dp; + + _DIAGASSERT(pp != NULL); + _DIAGASSERT(str != NULL); + + /* skip leading blanks */ + for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++) + continue; + + /* accumulate till delimiter or space */ + for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++) + continue; + + /* hunt for the delimiter */ + for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++) + continue; + + if (*dp != del) { + *str = NULL; + return 0; + } + + *pp = ++dp; + + len = (ep - sp) + 1; + if (len > 1) { + dp = malloc(len); + if (dp == NULL) + return 0; + (void)memcpy(dp, sp, len); + dp[len - 1] = '\0'; + } else + dp = NULL; + + *str = dp; + return 1; +} + + +/* + * getnetgroup(): Parse a netgroup, and advance the pointer + */ +static struct netgroup * +getnetgroup(pp) + char **pp; +{ + struct netgroup *ng; + + _DIAGASSERT(pp != NULL); + _DIAGASSERT(*pp != NULL); + + ng = malloc(sizeof(struct netgroup)); + if (ng == NULL) + return NULL; + + (*pp)++; /* skip '(' */ + if (!getstring(pp, ',', &ng->ng_host)) + goto badhost; + + if (!getstring(pp, ',', &ng->ng_user)) + goto baduser; + + if (!getstring(pp, ')', &ng->ng_domain)) + goto baddomain; + +#ifdef DEBUG_NG + { + char buf[1024]; + (void) fprintf(stderr, "netgroup %s\n", + _ng_print(buf, sizeof(buf), ng)); + } +#endif + return ng; + +baddomain: + if (ng->ng_user) + free(ng->ng_user); +baduser: + if (ng->ng_host) + free(ng->ng_host); +badhost: + free(ng); + return NULL; +} + +void +_ng_cycle(const char *grp, const StringList *sl) +{ + size_t i; + warnx("netgroup: Cycle in group `%s'", grp); + (void)fprintf(stderr, "groups: "); + for (i = 0; i < sl->sl_cur; i++) + (void)fprintf(stderr, "%s ", sl->sl_str[i]); + (void)fprintf(stderr, "\n"); +} + +static int _local_lookup(void *, void *, va_list); + +/*ARGSUSED*/ +static int +_local_lookup(void *rv, void *cb_data, va_list ap) +{ + char *name = va_arg(ap, char *); + char **line = va_arg(ap, char **); + int bywhat = va_arg(ap, int); + + DBT key, data; + size_t len; + char *ks; + int r; + + if (_ng_db == NULL) + return NS_UNAVAIL; + + len = strlen(name) + 2; + ks = malloc(len); + if (ks == NULL) + return NS_UNAVAIL; + + ks[0] = bywhat; + (void)memcpy(&ks[1], name, len - 1); + + key.data = (u_char *)ks; + key.size = len; + + r = (*_ng_db->get)(_ng_db, &key, &data, 0); + free(ks); + switch (r) { + case 0: + break; + case 1: + return NS_NOTFOUND; + case -1: + /* XXX: call endnetgrent() here ? */ + return NS_UNAVAIL; + } + + *line = strdup(data.data); + if (*line == NULL) + return NS_UNAVAIL; + return NS_SUCCESS; +} + +#ifdef YP +static int _nis_lookup(void *, void *, va_list); + +/*ARGSUSED*/ +static int +_nis_lookup(void *rv, void *cb_data, va_list ap) +{ + char *name = va_arg(ap, char *); + char **line = va_arg(ap, char **); + int bywhat = va_arg(ap, int); + + static char *__ypdomain; + int i; + const char *map = NULL; + + if(__ypdomain == NULL) { + switch (yp_get_default_domain(&__ypdomain)) { + case 0: + break; + case YPERR_RESRC: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + + switch (bywhat) { + case _NG_KEYBYNAME: + map = "netgroup"; + break; + + case _NG_KEYBYUSER: + map = "netgroup.byuser"; + break; + + case _NG_KEYBYHOST: + map = "netgroup.byhost"; + break; + + default: + abort(); + } + + *line = NULL; + switch (yp_match(__ypdomain, map, name, (int)strlen(name), line, &i)) { + case 0: + return NS_SUCCESS; + case YPERR_KEY: + if (*line) + free(*line); + return NS_NOTFOUND; + default: + if (*line) + free(*line); + return NS_UNAVAIL; + } + /* NOTREACHED */ +} +#endif + +#ifdef NSSRC_FILES +/* + * lookup(): Find the given key in the database or yp, and return its value + * in *line; returns 1 if key was found, 0 otherwise + */ +static int +lookup(char *name, char **line, int bywhat) +{ + int r; + static const ns_dtab dtab[] = { + NS_FILES_CB(_local_lookup, NULL) + NS_NIS_CB(_nis_lookup, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(line != NULL); + + r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "lookup", default_files_nis, + name, line, bywhat); + return (r == NS_SUCCESS) ? 1 : 0; +} +#else +static int +_local_lookupv(int *rv, void *cbdata, ...) +{ + int e; + va_list ap; + va_start(ap, cbdata); + e = _local_lookup(rv, cbdata, ap); + va_end(ap); + return e; +} + +static int +lookup(name, line, bywhat) + char *name; + char **line; + int bywhat; +{ + return _local_lookupv(NULL, NULL, name, line, bywhat) == NS_SUCCESS; +} +#endif + +/* + * _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE: + * line was empty or a comment _NG_GROUP: line had a netgroup definition, + * returned in ng _NG_NAME: line had a netgroup name, returned in name + * + * Public since used by netgroup_mkdb + */ +int +_ng_parse(char **p, char **name, struct netgroup **ng) +{ + + _DIAGASSERT(p != NULL); + _DIAGASSERT(*p != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(ng != NULL); + + while (**p) { + if (**p == '#') + /* comment */ + return _NG_NONE; + + while (**p && _NG_ISSPACE(**p)) + /* skipblank */ + (*p)++; + + if (**p == '(') { + if ((*ng = getnetgroup(p)) == NULL) + return _NG_ERROR; + return _NG_GROUP; + } else { + char *np; + size_t i; + + for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++) + continue; + if (np != *p) { + i = (*p - np) + 1; + *name = malloc(i); + if (*name == NULL) + return _NG_ERROR; + (void)memcpy(*name, np, i); + (*name)[i - 1] = '\0'; + return _NG_NAME; + } + } + } + return _NG_NONE; +} + + +/* + * addgroup(): Recursively add all the members of the netgroup to this group. + * returns 0 upon failure, nonzero upon success. + * grp is not a valid pointer after return (either free(3)ed or allocated + * to a stringlist). in either case, it shouldn't be used again. + */ +static int +addgroup(StringList *sl, char *grp) +{ + char *line, *p; + struct netgroup *ng; + char *name; + + _DIAGASSERT(sl != NULL); + _DIAGASSERT(grp != NULL); + +#ifdef DEBUG_NG + (void)fprintf(stderr, "addgroup(%s)\n", grp); +#endif + /* check for cycles */ + if (sl_find(sl, grp) != NULL) { + _ng_cycle(grp, sl); + free(grp); + return 0; + } + if (sl_add(sl, grp) == -1) { + free(grp); + return 0; + } + + /* Lookup this netgroup */ + line = NULL; + if (!lookup(grp, &line, _NG_KEYBYNAME)) { + if (line) + free(line); + return 0; + } + + p = line; + + for (;;) { + switch (_ng_parse(&p, &name, &ng)) { + case _NG_NONE: + /* Done with the line */ + free(line); + return 1; + + case _NG_GROUP: + /* new netgroup */ + /* add to the list */ + ng->ng_next = _nglist; + _nglist = ng; + break; + + case _NG_NAME: + /* netgroup name */ + if (!addgroup(sl, name)) + return 0; + break; + + case _NG_ERROR: + return 0; + + default: + abort(); + } + } +} + + +/* + * in_check(): Compare the spec with the netgroup + */ +static int +in_check(const char *host, const char *user, const char *domain, + struct netgroup *ng) +{ + + /* host may be NULL */ + /* user may be NULL */ + /* domain may be NULL */ + _DIAGASSERT(ng != NULL); + + if ((host != NULL) && (ng->ng_host != NULL) + && strcmp(ng->ng_host, host) != 0) + return 0; + + if ((user != NULL) && (ng->ng_user != NULL) + && strcmp(ng->ng_user, user) != 0) + return 0; + + if ((domain != NULL) && (ng->ng_domain != NULL) + && strcmp(ng->ng_domain, domain) != 0) + return 0; + + return 1; +} + + +/* + * in_find(): Find a match for the host, user, domain spec. + * grp is not a valid pointer after return (either free(3)ed or allocated + * to a stringlist). in either case, it shouldn't be used again. + */ +static int +in_find(StringList *sl, char *grp, const char *host, const char *user, + const char *domain) +{ + char *line, *p; + int i; + struct netgroup *ng; + char *name; + + _DIAGASSERT(sl != NULL); + _DIAGASSERT(grp != NULL); + /* host may be NULL */ + /* user may be NULL */ + /* domain may be NULL */ + +#ifdef DEBUG_NG + (void)fprintf(stderr, "in_find(%s)\n", grp); +#endif + /* check for cycles */ + if (sl_find(sl, grp) != NULL) { + _ng_cycle(grp, sl); + free(grp); + return 0; + } + if (sl_add(sl, grp) == -1) { + free(grp); + return 0; + } + + /* Lookup this netgroup */ + line = NULL; + if (!lookup(grp, &line, _NG_KEYBYNAME)) { + if (line) + free(line); + return 0; + } + + p = line; + + for (;;) { + switch (_ng_parse(&p, &name, &ng)) { + case _NG_NONE: + /* Done with the line */ + free(line); + return 0; + + case _NG_GROUP: + /* new netgroup */ + i = in_check(host, user, domain, ng); + if (ng->ng_host != NULL) + free(ng->ng_host); + if (ng->ng_user != NULL) + free(ng->ng_user); + if (ng->ng_domain != NULL) + free(ng->ng_domain); + free(ng); + if (i) { + free(line); + return 1; + } + break; + + case _NG_NAME: + /* netgroup name */ + if (in_find(sl, name, host, user, domain)) { + free(line); + return 1; + } + break; + + case _NG_ERROR: + free(line); + return 0; + + default: + abort(); + } + } +} + +/* + * _ng_makekey(): Make a key from the two names given. The key is of the form + * . Names strings are replaced with * if they are empty; + * Returns NULL if there's a problem. + */ +char * +_ng_makekey(const char *s1, const char *s2, size_t len) +{ + char *buf; + + /* s1 may be NULL */ + /* s2 may be NULL */ + + buf = malloc(len); + if (buf != NULL) + (void)snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2)); + return buf; +} + +void +_ng_print(char *buf, size_t len, const struct netgroup *ng) +{ + _DIAGASSERT(buf != NULL); + _DIAGASSERT(ng != NULL); + + (void)snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host), + _NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain)); +} + + +/* + * in_lookup1(): Fast lookup for a key in the appropriate map + */ +static char * +in_lookup1(const char *key, const char *domain, int map) +{ + char *line; + size_t len; + char *ptr; + int res; + + /* key may be NULL */ + /* domain may be NULL */ + + len = (key ? strlen(key) : 1) + (domain ? strlen(domain) : 1) + 2; + ptr = _ng_makekey(key, domain, len); + if (ptr == NULL) + return NULL; + res = lookup(ptr, &line, map); + free(ptr); + return res ? line : NULL; +} + + +/* + * in_lookup(): Fast lookup for a key in the appropriate map + */ +static int +in_lookup(const char *group, const char *key, const char *domain, int map) +{ + size_t len; + char *ptr, *line; + + _DIAGASSERT(group != NULL); + /* key may be NULL */ + /* domain may be NULL */ + + if (domain != NULL) { + /* Domain specified; look in "group.domain" and "*.domain" */ + if ((line = in_lookup1(key, domain, map)) == NULL) + line = in_lookup1(NULL, domain, map); + } else + line = NULL; + + if (line == NULL) { + /* + * domain not specified or domain lookup failed; look in + * "group.*" and "*.*" + */ + if (((line = in_lookup1(key, NULL, map)) == NULL) && + ((line = in_lookup1(NULL, NULL, map)) == NULL)) + return 0; + } + + len = strlen(group); + + for (ptr = line; (ptr = strstr(ptr, group)) != NULL;) + /* Make sure we did not find a substring */ + if ((ptr != line && ptr[-1] != ',') || + (ptr[len] != '\0' && strchr("\n\t ,", ptr[len]) == NULL)) + ptr++; + else { + free(line); + return 1; + } + + free(line); + return 0; +} + +/*ARGSUSED*/ +static int +_local_endnetgrent(void *rv, void *cb_data, va_list ap) +{ + for (_nglist = _nghead; _nglist != NULL; _nglist = _nghead) { + _nghead = _nglist->ng_next; + if (_nglist->ng_host != NULL) + free(_nglist->ng_host); + if (_nglist->ng_user != NULL) + free(_nglist->ng_user); + if (_nglist->ng_domain != NULL) + free(_nglist->ng_domain); + free(_nglist); + } + + if (_ng_db) { + (void)(*_ng_db->close)(_ng_db); + _ng_db = NULL; + } + + return NS_SUCCESS; +} + +/*ARGSUSED*/ +static int +_local_setnetgrent(void *rv, void *cb_data, va_list ap) +{ + const char *ng = va_arg(ap, const char *); + StringList *sl; + char *ng_copy; + + _DIAGASSERT(ng != NULL); + + sl = sl_init(); + if (sl == NULL) + return NS_TRYAGAIN; + + /* Cleanup any previous storage */ + if (_nghead != NULL) + endnetgrent(); + + if (_ng_db == NULL) + _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL); + + ng_copy = strdup(ng); + if (ng_copy != NULL) + addgroup(sl, ng_copy); + _nghead = _nglist; + sl_free(sl, 1); + + return NS_SUCCESS; +} + +/*ARGSUSED*/ +static int +_local_getnetgrent(void *rv, void *cb_data, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char **host = va_arg(ap, const char **); + const char **user = va_arg(ap, const char **); + const char **domain = va_arg(ap, const char **); + + _DIAGASSERT(host != NULL); + _DIAGASSERT(user != NULL); + _DIAGASSERT(domain != NULL); + + *retval = 0; + + if (_nglist == NULL) + return NS_TRYAGAIN; + + *host = _nglist->ng_host; + *user = _nglist->ng_user; + *domain = _nglist->ng_domain; + + _nglist = _nglist->ng_next; + + *retval = 1; + + return NS_SUCCESS; +} + +/*ARGSUSED*/ +static int +_local_innetgr(void *rv, void *cb_data, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *grp = va_arg(ap, const char *); + const char *host = va_arg(ap, const char *); + const char *user = va_arg(ap, const char *); + const char *domain = va_arg(ap, const char *); + + int found; + StringList *sl; + char *grcpy; + + _DIAGASSERT(grp != NULL); + /* host may be NULL */ + /* user may be NULL */ + /* domain may be NULL */ + + if (_ng_db == NULL) + _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL); + + /* Try the fast lookup first */ + if (host != NULL && user == NULL) { + if (in_lookup(grp, host, domain, _NG_KEYBYHOST)) { + *retval = 1; + return NS_SUCCESS; + } + } else if (host == NULL && user != NULL) { + if (in_lookup(grp, user, domain, _NG_KEYBYUSER)) { + *retval = 1; + return NS_SUCCESS; + } + } + /* If a domainname is given, we would have found a match */ + if (domain != NULL) { + *retval = 0; + return NS_SUCCESS; + } + + /* Too bad need the slow recursive way */ + sl = sl_init(); + if (sl == NULL) { + *retval = 0; + return NS_SUCCESS; + } + if ((grcpy = strdup(grp)) == NULL) { + sl_free(sl, 1); + *retval = 0; + return NS_SUCCESS; + } + found = in_find(sl, grcpy, host, user, domain); + sl_free(sl, 1); + + *retval = found; + return NS_SUCCESS; +} + +#ifdef YP + +/*ARGSUSED*/ +static int +_nis_endnetgrent(void *rv, void *cb_data, va_list ap) +{ + return _local_endnetgrent(rv, cb_data, ap); +} + +/*ARGSUSED*/ +static int +_nis_setnetgrent(void *rv, void *cb_data, va_list ap) +{ + return _local_setnetgrent(rv, cb_data, ap); +} + +/*ARGSUSED*/ +static int +_nis_getnetgrent(void *rv, void *cb_data, va_list ap) +{ + return _local_getnetgrent(rv, cb_data, ap); +} + +/*ARGSUSED*/ +static int +_nis_innetgr(void *rv, void *cb_data, va_list ap) +{ + return _local_innetgr(rv, cb_data, ap); +} + +#endif + + +#ifdef NSSRC_FILES +void +endnetgrent(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_local_endnetgrent, NULL) + NS_NIS_CB(_nis_endnetgrent, NULL) + NS_NULL_CB + }; + + (void) nsdispatch(NULL, dtab, NSDB_NETGROUP, "endnetgrent", + __nsdefaultcompat); +} +#else +static int +_local_endnetgrentv(int *rv, void *cbdata, ...) +{ + int e; + va_list ap; + va_start(ap, cbdata); + e = _local_endnetgrent(rv, cbdata, ap); + va_end(ap); + return e; +} + +void +endnetgrent(void) +{ + (void)_local_endnetgrentv(NULL, NULL, NULL); +} +#endif + +#ifdef NSSRC_FILES +void +setnetgrent(const char *ng) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_local_setnetgrent, NULL) + NS_NIS_CB(_nis_setnetgrent, NULL) + NS_NULL_CB + }; + + (void) nsdispatch(NULL, dtab, NSDB_NETGROUP, "setnetgrent", + __nsdefaultnis, ng); +} +#else +static int +_local_setnetgrentv(int *rv, void *cbdata, ...) +{ + int e; + va_list ap; + va_start(ap, cbdata); + e = _local_setnetgrent(rv, cbdata, ap); + va_end(ap); + return e; +} + +void +setnetgrent(const char *ng) +{ + (void) _local_setnetgrentv(NULL, NULL,ng); +} + +#endif + +#ifdef NSSRC_FILES +int +getnetgrent(const char **host, const char **user, const char **domain) +{ + int r, retval; + static const ns_dtab dtab[] = { + NS_FILES_CB(_local_getnetgrent, NULL) + NS_NIS_CB(_nis_getnetgrent, NULL) + NS_NULL_CB + }; + + r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "getnetgrent", + __nsdefaultnis, &retval, host, user, domain); + + return (r == NS_SUCCESS) ? retval : 0; +} +#else +static int +_local_getnetgrentv(int *rv, void *cbdata, ...) +{ + int e; + va_list ap; + va_start(ap, cbdata); + e = _local_getnetgrent(rv, cbdata, ap); + va_end(ap); + return e; +} + +int +getnetgrent(const char **host, const char **user, const char **domain) +{ + return _local_getnetgrentv(NULL, NULL, host, user, domain) == NS_SUCCESS; +} +#endif + +#ifdef NSSRC_FILES +int +innetgr(const char *grp, const char *host, const char *user, + const char *domain) +{ + int r, retval; + static const ns_dtab dtab[] = { + NS_FILES_CB(_local_innetgr, NULL) + NS_NIS_CB(_nis_innetgr, NULL) + NS_NULL_CB + }; + + r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "innetgr", + __nsdefaultnis, &retval, grp, host, user, domain); + + return (r == NS_SUCCESS) ? retval : 0; +} +#else +static int +_local_innetgrv(int *rv, void *cbdata, ...) +{ + int e; + va_list ap; + va_start(ap, cbdata); + e = _local_innetgr(rv, cbdata, ap); + va_end(ap); + return e; +} + +int +innetgr(const char *grp, const char *host, const char *user, + const char *domain) +{ + return _local_innetgrv(NULL, NULL, grp, host, user, domain) == NS_SUCCESS; +} +#endif diff --git a/lib/nbsd_libc/gen/getpagesize.3 b/lib/nbsd_libc/gen/getpagesize.3 new file mode 100644 index 000000000..51b68e664 --- /dev/null +++ b/lib/nbsd_libc/gen/getpagesize.3 @@ -0,0 +1,66 @@ +.\" $NetBSD: getpagesize.3,v 1.12 2003/08/07 16:42:50 agc Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)getpagesize.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt GETPAGESIZE 3 +.Os +.Sh NAME +.Nm getpagesize +.Nd get system page size +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn getpagesize void +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by +.Xr sysconf 3 . +.Ef +.Pp +.Fn getpagesize +returns the number of bytes in a page. +Page granularity is the granularity of many of the memory management calls. +.Pp +The page size is a +.Em system +page size and may not be the same as the underlying +hardware page size. +.Sh SEE ALSO +.Xr pagesize 1 , +.Xr sbrk 2 , +.Xr sysconf 3 +.Sh HISTORY +The +.Nm +function call appeared in +.Bx 4.2 . diff --git a/lib/nbsd_libc/gen/getpagesize.c b/lib/nbsd_libc/gen/getpagesize.c new file mode 100644 index 000000000..515339d06 --- /dev/null +++ b/lib/nbsd_libc/gen/getpagesize.c @@ -0,0 +1,68 @@ +/* $NetBSD: getpagesize.c,v 1.10 2009/12/14 01:04:46 matt Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getpagesize.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getpagesize.c,v 1.10 2009/12/14 01:04:46 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getpagesize,_getpagesize) +#endif + +int +getpagesize() +{ + static int pagsz; + + if (pagsz == 0) { + int mib[2]; + size_t size; + + mib[0] = CTL_HW; + mib[1] = HW_PAGESIZE; + size = sizeof pagsz; + if (sysctl(mib, 2, &pagsz, &size, NULL, 0) == -1) + return (-1); + _DIAGASSERT(pagsz); + } + return (pagsz); +} diff --git a/lib/nbsd_libc/gen/getpass.3 b/lib/nbsd_libc/gen/getpass.3 new file mode 100644 index 000000000..64220f0a9 --- /dev/null +++ b/lib/nbsd_libc/gen/getpass.3 @@ -0,0 +1,96 @@ +.\" $NetBSD: getpass.3,v 1.13 2010/05/06 11:09:39 jruoho 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. +.\" +.\" @(#)getpass.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd May 6, 2010 +.Dt GETPASS 3 +.Os +.Sh NAME +.Nm getpass +.Nd get a password +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In pwd.h +.In unistd.h +.Ft char * +.Fn getpass "const char *prompt" +.Sh DESCRIPTION +The +.Fn getpass +function displays a prompt to, and reads in a password from, +.Pa /dev/tty . +If this file is not accessible, +.Fn getpass +displays the prompt on the standard error output and reads from the standard +input. +.Pp +The password may be up to _PASSWORD_LEN (currently 128) +characters in length. +Any additional +characters and the terminating newline character are discarded. +.Pp +.Fn getpass +turns off character echoing while reading the password. +.Sh RETURN VALUES +.Fn getpass +returns a pointer to the null terminated password. +.Sh FILES +.Bl -tag -width /dev/tty -compact +.It Pa /dev/tty +.El +.Sh SEE ALSO +.Xr crypt 3 +.Sh STANDARDS +The +.Fn getpass +function appeared in +.St -susv2 , +but it was already marked as legacy. +The function was removed in the +.St -p1003.1-2001 +standard. +.Sh HISTORY +A +.Fn getpass +function appeared in +.At v7 . +.Sh BUGS +The +.Fn getpass +function leaves its result in an internal static object and returns +a pointer to that object. +Subsequent calls to +.Fn getpass +will modify the same object. +.Sh SECURITY CONSIDERATIONS +The calling process should zero the password as soon as possible to +avoid leaving the cleartext password visible in the process's address +space. diff --git a/lib/nbsd_libc/gen/getpass.c b/lib/nbsd_libc/gen/getpass.c new file mode 100644 index 000000000..ee4a8961b --- /dev/null +++ b/lib/nbsd_libc/gen/getpass.c @@ -0,0 +1,107 @@ +/* $NetBSD: getpass.c,v 1.16 2008/01/01 21:22:55 christos Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getpass.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getpass.c,v 1.16 2008/01/01 21:22:55 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(getpass,_getpass) +#endif + +char * +getpass(prompt) + const char *prompt; +{ + struct termios term; + int ch; + char *p; + FILE *fp, *outfp; + int echo; + static char buf[_PASSWORD_LEN + 1]; + sigset_t oset, nset; + + _DIAGASSERT(prompt != NULL); + + /* + * note - blocking signals isn't necessarily the + * right thing, but we leave it for now. + */ + sigemptyset(&nset); + sigaddset(&nset, SIGINT); + sigaddset(&nset, SIGTSTP); + (void)sigprocmask(SIG_BLOCK, &nset, &oset); + + /* + * read and write to /dev/tty if possible; else read from + * stdin and write to stderr. + */ + if ((outfp = fp = fopen(_PATH_TTY, "w+")) == NULL) { + outfp = stderr; + fp = stdin; + } + (void)tcgetattr(fileno(fp), &term); + if ((echo = (term.c_lflag & ECHO)) != 0) { + term.c_lflag &= ~ECHO; + (void)tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &term); + } + if (prompt != NULL) + (void)fputs(prompt, outfp); + rewind(outfp); /* implied flush */ + for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';) + if (p < buf + _PASSWORD_LEN) + *p++ = ch; + *p = '\0'; + (void)write(fileno(outfp), "\n", 1); + if (echo) { + term.c_lflag |= ECHO; + (void)tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &term); + } + if (fp != stdin) + (void)fclose(fp); + (void)sigprocmask(SIG_SETMASK, &oset, NULL); + return(buf); +} diff --git a/lib/nbsd_libc/gen/getprogname.3 b/lib/nbsd_libc/gen/getprogname.3 new file mode 100644 index 000000000..878d6c6da --- /dev/null +++ b/lib/nbsd_libc/gen/getprogname.3 @@ -0,0 +1,116 @@ +.\" $NetBSD: getprogname.3,v 1.7 2008/03/29 16:51:40 dholland 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. +.\" +.\" <> +.\" +.Dd March 29, 2008 +.Dt GETPROGNAME 3 +.Os +.Sh NAME +.Nm getprogname , +.Nm setprogname +.Nd get/set the name of the current program +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft const char * +.Fn getprogname "void" +.Ft void +.Fn setprogname "const char *name" +.Sh DESCRIPTION +These utility functions get and set the current program's name +as used by various error-reporting functions. +.Pp +.Fn getprogname +returns the name of the current program. +This function is typically useful when generating error messages +or other diagnostic output. +If the program name has not been set, +.Fn getprogname +will return +.Dv NULL . +.Pp +.Fn setprogname +sets the name of the current program to be the last pathname +component of the +.Fa name +argument. +It should be invoked at the start of the program, using the +.Fa argv[0] +passed into the program's +.Fn main +function. +A pointer into the string pointed to by the +.Fa name +argument is kept as the program name. +Therefore, the string pointed to by +.Fa name +should not be modified during the rest of the program's operation. +.Pp +A program's name can only be set once, and in +.Nx +that is actually +done by program start-up code that is run before +.Fn main +is called. +Therefore, in +.Nx , +calling +.Fn setprogname +from +.Fn main +has no effect. +However, it does serve to increase the portability of the program: +on other operating systems, +.Fn getprogname +and +.Fn setprogname +may be implemented by a portability library, and a call to +.Fn setprogname +allows that library to know the program name without +modifications to that system's program start-up code. +.Sh SEE ALSO +.Xr err 3 , +.Xr setproctitle 3 +.Sh HISTORY +The +.Nm getprogname +and +.Nm setprogname +function calls appeared in +.Nx 1.6 . +.Sh RESTRICTIONS +The string returned by +.Fn getprogname +is supplied by the invoking process and should not be trusted by +setuid or setgid programs. diff --git a/lib/nbsd_libc/gen/getprogname.c b/lib/nbsd_libc/gen/getprogname.c new file mode 100644 index 000000000..417d3349a --- /dev/null +++ b/lib/nbsd_libc/gen/getprogname.c @@ -0,0 +1,57 @@ +/* $NetBSD: getprogname.c,v 1.3 2003/07/26 19:24:42 salo 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getprogname.c,v 1.3 2003/07/26 19:24:42 salo Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#ifdef __weak_alias +__weak_alias(getprogname,_getprogname) +#endif + +extern const char *__progname; + +const char * +getprogname(void) +{ + + return (__progname); +} diff --git a/lib/nbsd_libc/gen/getpwent.3 b/lib/nbsd_libc/gen/getpwent.3 new file mode 100644 index 000000000..db97f378a --- /dev/null +++ b/lib/nbsd_libc/gen/getpwent.3 @@ -0,0 +1,370 @@ +.\" $NetBSD: getpwent.3,v 1.37 2010/03/22 19:30:53 joerg 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. +.\" +.\" @(#)getpwent.3 8.2 (Berkeley) 12/11/93 +.\" +.Dd April 30, 2008 +.Dt GETPWENT 3 +.Os +.Sh NAME +.Nm getpwent , +.Nm getpwent_r , +.Nm getpwnam , +.Nm getpwnam_r , +.Nm getpwuid , +.Nm getpwuid_r , +.Nm setpassent , +.Nm setpwent , +.Nm endpwent +.Nd password database operations +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In pwd.h +.Ft struct passwd * +.Fn getpwent void +.Ft int +.Fo getpwent_r +.Fa "struct passwd *pw" +.Fa "char *buffer" +.Fa "size_t buflen" +.Fa "struct passwd **result" +.Fc +.Ft struct passwd * +.Fn getpwnam "const char *name" +.Ft int +.Fo getpwnam_r +.Fa "const char *name" +.Fa "struct passwd *pw" +.Fa "char *buffer" +.Fa "size_t buflen" +.Fa "struct passwd **result" +.Fc +.Ft struct passwd * +.Fn getpwuid "uid_t uid" +.Ft int +.Fo getpwuid_r +.Fa "uid_t uid" +.Fa "struct passwd *pw" +.Fa "char *buffer" +.Fa "size_t buflen" +.Fa "struct passwd **result" +.Fc +.Ft int +.Fn setpassent "int stayopen" +.Ft void +.Fn setpwent void +.Ft void +.Fn endpwent void +.Sh DESCRIPTION +These functions +operate on the password database +which is described +in +.Xr passwd 5 . +Each entry in the database is defined by the structure +.Ar passwd +found in the include +file +.In pwd.h : +.Bd -literal -offset indent +struct passwd { + char *pw_name; /* user name */ + char *pw_passwd; /* encrypted password */ + uid_t pw_uid; /* user uid */ + gid_t pw_gid; /* user gid */ + time_t pw_change; /* password change time */ + char *pw_class; /* user login class */ + char *pw_gecos; /* general information */ + char *pw_dir; /* home directory */ + char *pw_shell; /* default shell */ + time_t pw_expire; /* account expiration */ +}; +.Ed +.Pp +The functions +.Fn getpwnam +and +.Fn getpwuid +search the password database for the given user name pointed to by +.Ar name +or user id pointed to by +.Ar uid +respectively, always returning the first one encountered. +Identical user names or user ids may result in undefined behavior. +.Pp +The +.Fn getpwent +function +sequentially reads the password database and is intended for programs +that wish to process the complete list of users. +.Pp +The functions +.Fn getpwnam_r , +.Fn getpwuid_r , +and +.Fn getpwent_r +act like their non re-entrant counterparts, updating the contents of +.Ar pw +and storing a pointer to that in +.Ar result , +and returning +.Dv 0 . +Storage used by +.Ar pw +is allocated from +.Ar buffer , +which is +.Ar buflen +bytes in size. +If the requested entry cannot be found, +.Ar result +will point to +.Dv NULL +and +.Dv 0 +will be returned. +If an error occurs, +a non-zero error number will be returned and +.Ar result +will point to +.Dv NULL . +Calling +.Fn getpwent_r +from multiple threads will result in each thread reading a disjoint portion +of the password database. +.Pp +The +.Fn setpassent +function +accomplishes two purposes. +First, it causes +.Fn getpwent +to +.Dq rewind +to the beginning of the database. +Additionally, if +.Fa stayopen +is non-zero, file descriptors are left open, significantly speeding +up subsequent accesses for all of the functions. +(This latter functionality is unnecessary for +.Fn getpwent +as it doesn't close its file descriptors by default.) +.Pp +It is dangerous for long-running programs to keep the file descriptors +open as the database will become out of date if it is updated while the +program is running. +.Pp +The +.Fn setpwent +function +is equivalent to +.Fn setpassent +with an argument of zero. +.Pp +The +.Fn endpwent +function +closes any open files. +.Pp +These functions have been written to +.Dq shadow +the password file, e.g. allow only certain programs to have access +to the encrypted password. +If the process which calls them has an effective uid of 0, the encrypted +password will be returned, otherwise, the password field of the returned +structure will point to the string +.Ql * . +.Sh RETURN VALUES +The functions +.Fn getpwent , +.Fn getpwnam , +and +.Fn getpwuid , +return a valid pointer to a passwd structure on success +and a +.Dv NULL +pointer if the entry was not found or an error occured. +If an error occured, the global variable +.Dv errno +is set to indicate the nature of the failure. +The +.Fn setpassent +function returns 0 on failure, setting the global variable +.Dv errno +to indicate the nature of the failure, and 1 on success. +The +.Fn endpwent +and +.Fn setpwent +functions +have no return value. +The functions +.Fn getpwnam_r , +.Fn getpwuid_r , +and +.Fn getpwent_r +return +.Dv 0 +on success or entry not found, and non-zero on failure, setting the global +variable +.Dv errno +to indicate the nature of the failure. +.Sh ERRORS +The following error codes may be set in +.Va errno +for +.Nm getpwent , +.Nm getpwent_r , +.Nm getpwnam , +.Nm getpwnam_r , +.Nm getpwuid , +.Nm getpwuid_r , +and +.Nm setpassent : +.Bl -tag -width Er +.It Bq Er EIO +An I/O error has occurred. +.It Bq Er EINTR +A signal was caught during the database search. +.It Bq Er EMFILE +The limit on open files for this process has been reached. +.It Bq Er ENFILE +The system limit on open files has been reached. +.El +.Pp +The following error code may be set in +.Va errno +for +.Nm getpwent_r , +.Nm getpwnam_r , +and +.Nm getpwuid_r : +.Bl -tag -width Er +.It Bq Er ERANGE +The resulting +.Ft struct passwd +does not fit in the space defined by +.Dv buffer +and +.Dv buflen +.El +.Pp +Other +.Dv errno +values may be set depending on the specific database backends. +.Sh FILES +.Bl -tag -width /etc/master.passwd -compact +.It Pa /etc/pwd.db +The insecure password database file +.It Pa /etc/spwd.db +The secure password database file +.It Pa /etc/master.passwd +The current password file +.It Pa /etc/passwd +A Version 7 format password file +.El +.Sh SEE ALSO +.Xr getlogin 2 , +.Xr getgrent 3 , +.Xr nsswitch.conf 5 , +.Xr passwd 5 , +.Xr passwd.conf 5 , +.Xr pwd_mkdb 8 , +.Xr vipw 8 +.Sh STANDARDS +The +.Fn getpwnam +and +.Fn getpwuid , +functions conform to +.St -p1003.1-90 . +The +.Fn getpwnam_r +and +.Fn getpwuid_r +functions conform to +.St -p1003.1c-95 . +The +.Fn endpwent , +.Fn getpwent , +and +.Fn setpwent +functions conform to +.St -xpg4.2 +and +.St -p1003.1-2004 +(XSI extension). +.Sh HISTORY +The +.Nm getpwent , +.Nm getpwnam , +.Nm getpwuid , +.Nm setpwent , +and +.Nm endpwent +functions appeared in +.At v7 . +The +.Nm setpassent +function appeared in +.Bx 4.3 Reno . +The functions +.Fn getpwnam_r +and +.Fn getpwuid_r +appeared in +.Nx 3.0 . +.Sh BUGS +The functions +.Fn getpwent , +.Fn getpwnam , +and +.Fn getpwuid , +leave their results in an internal static object and return +a pointer to that object. +Subsequent calls to any of these functions will modify the same object. +.Pp +The functions +.Fn getpwent , +.Fn endpwent , +.Fn setpassent , +and +.Fn setpwent +are fairly useless in a networked environment and should be +avoided, if possible. +.Fn getpwent +makes no attempt to suppress duplicate information if multiple +sources are specified in +.Xr nsswitch.conf 5 . +.Sh COMPATIBILITY +The historic function +.Fn setpwfile +which allowed the specification of alternative password databases, +has been deprecated and is no longer available. diff --git a/lib/nbsd_libc/gen/getpwent.c b/lib/nbsd_libc/gen/getpwent.c new file mode 100644 index 000000000..c49f5e2de --- /dev/null +++ b/lib/nbsd_libc/gen/getpwent.c @@ -0,0 +1,2579 @@ +/* $NetBSD: getpwent.c,v 1.77 2010/03/23 20:28:59 drochner Exp $ */ + +/*- + * Copyright (c) 1997-2000, 2004-2005 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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. + */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Portions Copyright (c) 1994, 1995, Jason Downs. 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 BY THE AUTHOR(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 AUTHOR(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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getpwent.c 8.2 (Berkeley) 4/27/95"; +#else +__RCSID("$NetBSD: getpwent.c,v 1.77 2010/03/23 20:28:59 drochner Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HESIOD +#include +#endif + +#ifdef YP +#include +#include +#include +#include +#endif + +#include "pw_private.h" + +#define _PASSWD_COMPAT /* "passwd" defaults to compat, so always provide it */ + +#ifdef __weak_alias +__weak_alias(endpwent,_endpwent) +__weak_alias(setpassent,_setpassent) +__weak_alias(setpwent,_setpwent) +#endif + +#ifdef _REENTRANT +static mutex_t _pwmutex = MUTEX_INITIALIZER; +#endif + +const char __yp_token[] = "__YP!"; /* Let pwd_mkdb pull this in. */ + + +/* + * The pwd.db lookup techniques and data extraction code here must be kept + * in sync with that in `pwd_mkdb'. + */ + +#if defined(YP) || defined(HESIOD) +/* + * _pw_parse + * Parses entry using pw_scan(3) (without the trailing \n) + * after copying to buf, and fills in pw with corresponding values. + * If old is non-zero, entry is in _PASSWORD_OLDFMT. + * Returns 1 if parsed successfully, 0 on parse failure. + */ +static int +_pw_parse(const char *entry, struct passwd *pw, char *buf, size_t buflen, + int old) +{ + int flags; + + _DIAGASSERT(entry != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buf != NULL); + + if (strlcpy(buf, entry, buflen) >= buflen) + return 0; + flags = _PASSWORD_NOWARN; + if (old) + flags |= _PASSWORD_OLDFMT; + return __pw_scan(buf, pw, &flags); +} +#endif /* YP || HESIOD */ + +/* + * _pw_opendb + * if *db is NULL, dbopen(3) /etc/spwd.db or /etc/pwd.db (depending + * upon permissions, etc) + */ +static int +_pw_opendb(DB **db, int *version) +{ + static int warned; + DBT key; + DBT value; + + const char *dbfile = NULL; + + _DIAGASSERT(db != NULL); + _DIAGASSERT(version != NULL); + if (*db != NULL) /* open *db */ + return NS_SUCCESS; + + if (geteuid() == 0) { + dbfile = _PATH_SMP_DB; + *db = dbopen(dbfile, O_RDONLY, 0, DB_HASH, NULL); + } + if (*db == NULL) { + dbfile = _PATH_MP_DB; + *db = dbopen(dbfile, O_RDONLY, 0, DB_HASH, NULL); + } + if (*db == NULL) { + if (!warned) { + int serrno = errno; + syslog(LOG_ERR, "%s: %m", dbfile); + errno = serrno; + } + warned = 1; + return NS_UNAVAIL; + } + key.data = __UNCONST("VERSION"); + key.size = strlen((char *)key.data) + 1; + switch ((*(*db)->get)(*db, &key, &value, 0)) { + case 0: + if (sizeof(*version) != value.size) + return NS_UNAVAIL; + (void)memcpy(version, value.data, value.size); + break; /* found */ + case 1: + *version = 0; /* not found */ + break; + case -1: + return NS_UNAVAIL; /* error in db routines */ + default: + abort(); + } + return NS_SUCCESS; +} + +/* + * _pw_getkey + * Lookup key in *db, filling in pw + * with the result, allocating memory from buffer (size buflen). + * (The caller may point key.data to buffer on entry; the contents + * of key.data will be invalid on exit.) + */ +static int +_pw_getkey(DB *db, DBT *key, + struct passwd *pw, char *buffer, size_t buflen, int *pwflags, + int version) +{ + char *p, *t; + DBT data; + + _DIAGASSERT(db != NULL); + _DIAGASSERT(key != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + /* pwflags may be NULL (if we don't care about them */ + + if (db == NULL) /* this shouldn't happen */ + return NS_UNAVAIL; + + switch ((db->get)(db, key, &data, 0)) { + case 0: + break; /* found */ + case 1: + return NS_NOTFOUND; /* not found */ + case -1: + return NS_UNAVAIL; /* error in db routines */ + default: + abort(); + } + + p = (char *)data.data; + if (data.size > buflen) { + errno = ERANGE; + return NS_UNAVAIL; + } + + /* + * THE DECODING BELOW MUST MATCH THAT IN pwd_mkdb. + */ + t = buffer; +#define MACRO(a) do { a } while (/*CONSTCOND*/0) +#define EXPAND(e) MACRO(e = t; while ((*t++ = *p++));) +#define SCALAR(v) MACRO(memmove(&(v), p, sizeof v); p += sizeof v;) + EXPAND(pw->pw_name); + EXPAND(pw->pw_passwd); + SCALAR(pw->pw_uid); + SCALAR(pw->pw_gid); + if (version == 0) { + int32_t tmp; + SCALAR(tmp); + pw->pw_change = tmp; + } else + SCALAR(pw->pw_change); + EXPAND(pw->pw_class); + EXPAND(pw->pw_gecos); + EXPAND(pw->pw_dir); + EXPAND(pw->pw_shell); + if (version == 0) { + int32_t tmp; + SCALAR(tmp); + pw->pw_expire = tmp; + } else + SCALAR(pw->pw_expire); + if (pwflags) { + /* See if there's any data left. If so, read in flags. */ + if (data.size > (size_t) (p - (char *)data.data)) { + SCALAR(*pwflags); + } else { /* default */ + *pwflags = _PASSWORD_NOUID|_PASSWORD_NOGID; + } + } + + return NS_SUCCESS; +} + +/* + * _pw_memfrombuf + * Obtain want bytes from buffer (of size buflen) and return a pointer + * to the available memory after adjusting buffer/buflen. + * Returns NULL if there is insufficient space. + */ +static char * +_pw_memfrombuf(size_t want, char **buffer, size_t *buflen) +{ + char *rv; + + if (want > *buflen) { + errno = ERANGE; + return NULL; + } + rv = *buffer; + *buffer += want; + *buflen -= want; + return rv; +} + +/* + * _pw_copy + * Copy the contents of frompw to pw; memory for strings + * and arrays will be allocated from buf (of size buflen). + * If proto != NULL, use various fields in proto in preference to frompw. + * Returns 1 if copied successfully, 0 on copy failure. + * NOTE: frompw must not use buf for its own pointers. + */ +static int +_pw_copy(const struct passwd *frompw, struct passwd *pw, + char *buf, size_t buflen, const struct passwd *protopw, int protoflags) +{ + size_t count; + int useproto; + + _DIAGASSERT(frompw != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buf != NULL); + /* protopw may be NULL */ + + useproto = protopw && protopw->pw_name; + +#define COPYSTR(to, from) \ + do { \ + count = strlen((from)); \ + (to) = _pw_memfrombuf(count+1, &buf, &buflen); \ + if ((to) == NULL) \ + return 0; \ + memmove((to), (from), count); \ + to[count] = '\0'; \ + } while (0) /* LINTED */ + +#define COPYFIELD(field) COPYSTR(pw->field, frompw->field) + +#define COPYPROTOFIELD(field) COPYSTR(pw->field, \ + (useproto && *protopw->field ? protopw->field : frompw->field)) + + COPYFIELD(pw_name); + +#ifdef PW_OVERRIDE_PASSWD + COPYPROTOFIELD(pw_passwd); +#else + COPYFIELD(pw_passwd); +#endif + + if (useproto && !(protoflags & _PASSWORD_NOUID)) + pw->pw_uid = protopw->pw_uid; + else + pw->pw_uid = frompw->pw_uid; + + if (useproto && !(protoflags & _PASSWORD_NOGID)) + pw->pw_gid = protopw->pw_gid; + else + pw->pw_gid = frompw->pw_gid; + + pw->pw_change = frompw->pw_change; + COPYFIELD(pw_class); + COPYPROTOFIELD(pw_gecos); + COPYPROTOFIELD(pw_dir); + COPYPROTOFIELD(pw_shell); + +#undef COPYSTR +#undef COPYFIELD +#undef COPYPROTOFIELD + + return 1; +} + + + /* + * files methods + */ + + /* state shared between files methods */ +struct files_state { + int stayopen; /* see getpassent(3) */ + DB *db; /* passwd file handle */ + int keynum; /* key counter, -1 if no more */ + int version; +}; + +static struct files_state _files_state; + /* storage for non _r functions */ +static struct passwd _files_passwd; +static char _files_passwdbuf[_GETPW_R_SIZE_MAX]; + +static int +_files_start(struct files_state *state) +{ + int rv; + + _DIAGASSERT(state != NULL); + + state->keynum = 0; + rv = _pw_opendb(&state->db, &state->version); + if (rv != NS_SUCCESS) + return rv; + return NS_SUCCESS; +} + +static int +_files_end(struct files_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->keynum = 0; + if (state->db) { + (void)(state->db->close)(state->db); + state->db = NULL; + } + return NS_SUCCESS; +} + +/* + * _files_pwscan + * Search state->db for the next desired entry. + * If search is _PW_KEYBYNUM, look for state->keynum. + * If search is _PW_KEYBYNAME, look for name. + * If search is _PW_KEYBYUID, look for uid. + * Sets *retval to the errno if the result is not NS_SUCCESS + * or NS_NOTFOUND. + */ +static int +_files_pwscan(int *retval, struct passwd *pw, char *buffer, size_t buflen, + struct files_state *state, int search, const char *name, uid_t uid) +{ + const void *from; + size_t fromlen; + DBT key; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + /* name is NULL to indicate searching for uid */ + + *retval = 0; + + if (state->db == NULL) { /* only start if file not open yet */ + rv = _files_start(state); + if (rv != NS_SUCCESS) + goto filespwscan_out; + } + + for (;;) { /* search for a match */ + switch (search) { + case _PW_KEYBYNUM: + if (state->keynum == -1) + return NS_NOTFOUND; /* no more records */ + state->keynum++; + from = &state->keynum; + fromlen = sizeof(state->keynum); + break; + case _PW_KEYBYNAME: + from = name; + fromlen = strlen(name); + break; + case _PW_KEYBYUID: + from = &uid; + fromlen = sizeof(uid); + break; + default: + abort(); + } + + if (buflen <= fromlen) { /* buffer too small */ + *retval = ERANGE; + return NS_UNAVAIL; + } + buffer[0] = search; /* setup key */ + memmove(buffer + 1, from, fromlen); + key.size = fromlen + 1; + key.data = (u_char *)buffer; + + /* search for key */ + rv = _pw_getkey(state->db, &key, pw, buffer, buflen, NULL, + state->version); + if (rv != NS_SUCCESS) /* no match */ + break; + if (pw->pw_name[0] == '+' || pw->pw_name[0] == '-') { + /* if a compat line */ + if (search == _PW_KEYBYNUM) + continue; /* read next if pwent */ + rv = NS_NOTFOUND; /* don't match if pw{nam,uid} */ + break; + } + break; + } + + if (rv == NS_NOTFOUND && search == _PW_KEYBYNUM) + state->keynum = -1; /* flag `no more records' */ + + if (rv == NS_SUCCESS) { + if ((search == _PW_KEYBYUID && pw->pw_uid != uid) || + (search == _PW_KEYBYNAME && strcmp(pw->pw_name, name) != 0)) + rv = NS_NOTFOUND; + } + + filespwscan_out: + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + return rv; +} + +/*ARGSUSED*/ +static int +_files_setpwent(void *nsrv, void *nscb, va_list ap) +{ + + _files_state.stayopen = 0; + return _files_start(&_files_state); +} + +/*ARGSUSED*/ +static int +_files_setpassent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + _files_state.stayopen = stayopen; + rv = _files_start(&_files_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_files_endpwent(void *nsrv, void *nscb, va_list ap) +{ + + _files_state.stayopen = 0; + return _files_end(&_files_state); +} + +/*ARGSUSED*/ +static int +_files_getpwent(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _files_pwscan(&rerror, &_files_passwd, + _files_passwdbuf, sizeof(_files_passwdbuf), + &_files_state, _PW_KEYBYNUM, NULL, 0); + if (rv == NS_SUCCESS) + *retval = &_files_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getpwent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + rv = _files_pwscan(retval, pw, buffer, buflen, &_files_state, + _PW_KEYBYNUM, NULL, 0); + if (rv == NS_SUCCESS) + *result = pw; + else + *result = NULL; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getpwnam(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _files_start(&_files_state); + if (rv != NS_SUCCESS) + return rv; + rv = _files_pwscan(&rerror, &_files_passwd, + _files_passwdbuf, sizeof(_files_passwdbuf), + &_files_state, _PW_KEYBYNAME, name, 0); + if (!_files_state.stayopen) + _files_end(&_files_state); + if (rv == NS_SUCCESS) + *retval = &_files_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getpwnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct files_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = _files_pwscan(retval, pw, buffer, buflen, &state, + _PW_KEYBYNAME, name, 0); + _files_end(&state); + if (rv == NS_SUCCESS) + *result = pw; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getpwuid(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + uid_t uid = va_arg(ap, uid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _files_start(&_files_state); + if (rv != NS_SUCCESS) + return rv; + rv = _files_pwscan(&rerror, &_files_passwd, + _files_passwdbuf, sizeof(_files_passwdbuf), + &_files_state, _PW_KEYBYUID, NULL, uid); + if (!_files_state.stayopen) + _files_end(&_files_state); + if (rv == NS_SUCCESS) + *retval = &_files_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_files_getpwuid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + uid_t uid = va_arg(ap, uid_t); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct files_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = _files_pwscan(retval, pw, buffer, buflen, &state, + _PW_KEYBYUID, NULL, uid); + _files_end(&state); + if (rv == NS_SUCCESS) + *result = pw; + return rv; +} + + +#ifdef HESIOD + /* + * dns methods + */ + + /* state shared between dns methods */ +struct dns_state { + int stayopen; /* see getpassent(3) */ + void *context; /* Hesiod context */ + int num; /* passwd index, -1 if no more */ +}; + +static struct dns_state _dns_state; + /* storage for non _r functions */ +static struct passwd _dns_passwd; +static char _dns_passwdbuf[_GETPW_R_SIZE_MAX]; + +static int +_dns_start(struct dns_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->num = 0; + if (state->context == NULL) { /* setup Hesiod */ + if (hesiod_init(&state->context) == -1) + return NS_UNAVAIL; + } + + return NS_SUCCESS; +} + +static int +_dns_end(struct dns_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->num = 0; + if (state->context) { + hesiod_end(state->context); + state->context = NULL; + } + return NS_SUCCESS; +} + +/* + * _dns_pwscan + * Look for the Hesiod name provided in buffer in the NULL-terminated + * list of zones, + * and decode into pw/buffer/buflen. + */ +static int +_dns_pwscan(int *retval, struct passwd *pw, char *buffer, size_t buflen, + struct dns_state *state, const char **zones) +{ + const char **curzone; + char **hp, *ep; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + _DIAGASSERT(zones != NULL); + + *retval = 0; + + if (state->context == NULL) { /* only start if Hesiod not setup */ + rv = _dns_start(state); + if (rv != NS_SUCCESS) + return rv; + } + + hp = NULL; + rv = NS_NOTFOUND; + + for (curzone = zones; *curzone; curzone++) { /* search zones */ + hp = hesiod_resolve(state->context, buffer, *curzone); + if (hp != NULL) + break; + if (errno != ENOENT) { + rv = NS_UNAVAIL; + goto dnspwscan_out; + } + } + if (*curzone == NULL) + goto dnspwscan_out; + + if ((ep = strchr(hp[0], '\n')) != NULL) + *ep = '\0'; /* clear trailing \n */ + if (_pw_parse(hp[0], pw, buffer, buflen, 1)) /* validate line */ + rv = NS_SUCCESS; + else + rv = NS_UNAVAIL; + + dnspwscan_out: + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + if (hp) + hesiod_free_list(state->context, hp); + return rv; +} + +/*ARGSUSED*/ +static int +_dns_setpwent(void *nsrv, void *nscb, va_list ap) +{ + + _dns_state.stayopen = 0; + return _dns_start(&_dns_state); +} + +/*ARGSUSED*/ +static int +_dns_setpassent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + _dns_state.stayopen = stayopen; + rv = _dns_start(&_dns_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_dns_endpwent(void *nsrv, void *nscb, va_list ap) +{ + + _dns_state.stayopen = 0; + return _dns_end(&_dns_state); +} + +/*ARGSUSED*/ +static int +_dns_getpwent(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + + char **hp, *ep; + int rv; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + + if (_dns_state.num == -1) /* exhausted search */ + return NS_NOTFOUND; + + if (_dns_state.context == NULL) { + /* only start if Hesiod not setup */ + rv = _dns_start(&_dns_state); + if (rv != NS_SUCCESS) + return rv; + } + + next_dns_entry: + hp = NULL; + rv = NS_NOTFOUND; + + /* find passwd-NNN */ + snprintf(_dns_passwdbuf, sizeof(_dns_passwdbuf), + "passwd-%u", _dns_state.num); + _dns_state.num++; + + hp = hesiod_resolve(_dns_state.context, _dns_passwdbuf, "passwd"); + if (hp == NULL) { + if (errno == ENOENT) + _dns_state.num = -1; + else + rv = NS_UNAVAIL; + } else { + if ((ep = strchr(hp[0], '\n')) != NULL) + *ep = '\0'; /* clear trailing \n */ + /* validate line */ + if (_pw_parse(hp[0], &_dns_passwd, + _dns_passwdbuf, sizeof(_dns_passwdbuf), 1)) + rv = NS_SUCCESS; + else { /* dodgy entry, try again */ + hesiod_free_list(_dns_state.context, hp); + goto next_dns_entry; + } + } + + if (hp) + hesiod_free_list(_dns_state.context, hp); + if (rv == NS_SUCCESS) + *retval = &_dns_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_dns_getpwent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + char **hp, *ep; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *retval = 0; + + if (_dns_state.num == -1) /* exhausted search */ + return NS_NOTFOUND; + + if (_dns_state.context == NULL) { + /* only start if Hesiod not setup */ + rv = _dns_start(&_dns_state); + if (rv != NS_SUCCESS) + return rv; + } + + next_dns_entry: + hp = NULL; + rv = NS_NOTFOUND; + + /* find passwd-NNN */ + snprintf(buffer, buflen, "passwd-%u", _dns_state.num); + _dns_state.num++; + + hp = hesiod_resolve(_dns_state.context, buffer, "passwd"); + if (hp == NULL) { + if (errno == ENOENT) + _dns_state.num = -1; + else + rv = NS_UNAVAIL; + } else { + if ((ep = strchr(hp[0], '\n')) != NULL) + *ep = '\0'; /* clear trailing \n */ + /* validate line */ + if (_pw_parse(hp[0], pw, buffer, buflen, 1)) + rv = NS_SUCCESS; + else { /* dodgy entry, try again */ + hesiod_free_list(_dns_state.context, hp); + goto next_dns_entry; + } + } + + if (hp) + hesiod_free_list(_dns_state.context, hp); + if (rv == NS_SUCCESS) + *result = pw; + else + *result = NULL; + return rv; +} + +static const char *_dns_uid_zones[] = { + "uid", + "passwd", + NULL +}; + +/*ARGSUSED*/ +static int +_dns_getpwuid(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + uid_t uid = va_arg(ap, uid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _dns_start(&_dns_state); + if (rv != NS_SUCCESS) + return rv; + snprintf(_dns_passwdbuf, sizeof(_dns_passwdbuf), + "%u", (unsigned int)uid); + rv = _dns_pwscan(&rerror, &_dns_passwd, + _dns_passwdbuf, sizeof(_dns_passwdbuf), + &_dns_state, _dns_uid_zones); + if (!_dns_state.stayopen) + _dns_end(&_dns_state); + if (rv == NS_SUCCESS && uid == _dns_passwd.pw_uid) + *retval = &_dns_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_dns_getpwuid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + uid_t uid = va_arg(ap, uid_t); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct dns_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + snprintf(buffer, buflen, "%u", (unsigned int)uid); + rv = _dns_pwscan(retval, pw, buffer, buflen, &state, _dns_uid_zones); + _dns_end(&state); + if (rv != NS_SUCCESS) + return rv; + if (uid == pw->pw_uid) { + *result = pw; + return NS_SUCCESS; + } else + return NS_NOTFOUND; +} + +static const char *_dns_nam_zones[] = { + "passwd", + NULL +}; + +/*ARGSUSED*/ +static int +_dns_getpwnam(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _dns_start(&_dns_state); + if (rv != NS_SUCCESS) + return rv; + snprintf(_dns_passwdbuf, sizeof(_dns_passwdbuf), "%s", name); + rv = _dns_pwscan(&rerror, &_dns_passwd, + _dns_passwdbuf, sizeof(_dns_passwdbuf), + &_dns_state, _dns_nam_zones); + if (!_dns_state.stayopen) + _dns_end(&_dns_state); + if (rv == NS_SUCCESS && strcmp(name, _dns_passwd.pw_name) == 0) + *retval = &_dns_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_dns_getpwnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct dns_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + snprintf(buffer, buflen, "%s", name); + rv = _dns_pwscan(retval, pw, buffer, buflen, &state, _dns_nam_zones); + _dns_end(&state); + if (rv != NS_SUCCESS) + return rv; + if (strcmp(name, pw->pw_name) == 0) { + *result = pw; + return NS_SUCCESS; + } else + return NS_NOTFOUND; +} + +#endif /* HESIOD */ + + +#ifdef YP + /* + * nis methods + */ + /* state shared between nis methods */ +struct nis_state { + int stayopen; /* see getpassent(3) */ + char *domain; /* NIS domain */ + int done; /* non-zero if search exhausted */ + char *current; /* current first/next match */ + int currentlen; /* length of _nis_current */ + enum { /* shadow map type */ + NISMAP_UNKNOWN, /* unknown ... */ + NISMAP_NONE, /* none: use "passwd.by*" */ + NISMAP_ADJUNCT, /* pw_passwd from "passwd.adjunct.*" */ + NISMAP_MASTER /* all from "master.passwd.by*" */ + } maptype; +}; + +static struct nis_state _nis_state; + /* storage for non _r functions */ +static struct passwd _nis_passwd; +static char _nis_passwdbuf[_GETPW_R_SIZE_MAX]; + + /* macros for deciding which NIS maps to use. */ +#define PASSWD_BYNAME(x) ((x)->maptype == NISMAP_MASTER \ + ? "master.passwd.byname" : "passwd.byname") +#define PASSWD_BYUID(x) ((x)->maptype == NISMAP_MASTER \ + ? "master.passwd.byuid" : "passwd.byuid") + +static int +_nis_start(struct nis_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->done = 0; + if (state->current) { + free(state->current); + state->current = NULL; + } + if (state->domain == NULL) { /* setup NIS */ + switch (yp_get_default_domain(&state->domain)) { + case 0: + break; + case YPERR_RESRC: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + + /* determine where to get pw_passwd from */ + if (state->maptype == NISMAP_UNKNOWN) { + int r, order; + + state->maptype = NISMAP_NONE; /* default to no adjunct */ + if (geteuid() != 0) /* non-root can't use adjunct */ + return NS_SUCCESS; + + /* look for "master.passwd.*" */ + r = yp_order(state->domain, "master.passwd.byname", &order); + if (r == 0) { + state->maptype = NISMAP_MASTER; + return NS_SUCCESS; + } + + /* master.passwd doesn't exist, try passwd.adjunct */ + if (r == YPERR_MAP) { + r = yp_order(state->domain, "passwd.adjunct.byname", + &order); + if (r == 0) + state->maptype = NISMAP_ADJUNCT; + } + } + return NS_SUCCESS; +} + +static int +_nis_end(struct nis_state *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->domain) + state->domain = NULL; + state->done = 0; + if (state->current) + free(state->current); + state->current = NULL; + state->maptype = NISMAP_UNKNOWN; + return NS_SUCCESS; +} + +/* + * nis_parse + * wrapper to _pw_parse that obtains the real password from the + * "passwd.adjunct.byname" NIS map if the maptype is NISMAP_ADJUNCT. + */ +static int +_nis_parse(const char *entry, struct passwd *pw, char *buf, size_t buflen, + struct nis_state *state) +{ + size_t elen; + + _DIAGASSERT(entry != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buf != NULL); + _DIAGASSERT(state != NULL); + + elen = strlen(entry); + if (elen >= buflen) + return 0; + if (! _pw_parse(entry, pw, buf, buflen, + !(state->maptype == NISMAP_MASTER))) + return 0; + + if ((state->maptype == NISMAP_ADJUNCT) && + (strstr(pw->pw_passwd, "##") != NULL)) { + char *data; + int datalen; + + if (yp_match(state->domain, "passwd.adjunct.byname", + pw->pw_name, (int)strlen(pw->pw_name), + &data, &datalen) == 0) { + char *bp, *ep; + /* skip name to get password */ + ep = data; + if ((bp = strsep(&ep, ":")) != NULL && + (bp = strsep(&ep, ":")) != NULL) { + /* store new pw_passwd after entry */ + strlcpy(buf + elen, bp, buflen - elen); + pw->pw_passwd = &buf[elen]; + } + free(data); + } + } + + return 1; +} + + +/* + * _nis_pwscan + * Look for the yp key provided in buffer from map, + * and decode into pw/buffer/buflen. + */ +static int +_nis_pwscan(int *retval, struct passwd *pw, char *buffer, size_t buflen, + struct nis_state *state, const char *map) +{ + char *data; + int nisr, rv, datalen; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + _DIAGASSERT(map != NULL); + + *retval = 0; + + if (state->domain == NULL) { /* only start if NIS not setup */ + rv = _nis_start(state); + if (rv != NS_SUCCESS) + return rv; + } + + data = NULL; + rv = NS_NOTFOUND; + + /* search map */ + nisr = yp_match(state->domain, map, buffer, (int)strlen(buffer), + &data, &datalen); + switch (nisr) { + case 0: + data[datalen] = '\0'; /* clear trailing \n */ + if (_nis_parse(data, pw, buffer, buflen, state)) + rv = NS_SUCCESS; /* validate line */ + else + rv = NS_UNAVAIL; + break; + case YPERR_KEY: + break; + default: + rv = NS_UNAVAIL; + break; + } + + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + if (data) + free(data); + return rv; +} + +/*ARGSUSED*/ +static int +_nis_setpwent(void *nsrv, void *nscb, va_list ap) +{ + + _nis_state.stayopen = 0; + return _nis_start(&_nis_state); +} + +/*ARGSUSED*/ +static int +_nis_setpassent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + _nis_state.stayopen = stayopen; + rv = _nis_start(&_nis_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_nis_endpwent(void *nsrv, void *nscb, va_list ap) +{ + + return _nis_end(&_nis_state); +} + + +/*ARGSUSED*/ +static int +_nis_getpwent(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + + char *key, *data; + int keylen, datalen, rv, nisr; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + + if (_nis_state.done) /* exhausted search */ + return NS_NOTFOUND; + if (_nis_state.domain == NULL) { + /* only start if NIS not setup */ + rv = _nis_start(&_nis_state); + if (rv != NS_SUCCESS) + return rv; + } + + next_nis_entry: + key = NULL; + data = NULL; + rv = NS_NOTFOUND; + + if (_nis_state.current) { /* already searching */ + nisr = yp_next(_nis_state.domain, PASSWD_BYNAME(&_nis_state), + _nis_state.current, _nis_state.currentlen, + &key, &keylen, &data, &datalen); + free(_nis_state.current); + _nis_state.current = NULL; + switch (nisr) { + case 0: + _nis_state.current = key; + _nis_state.currentlen = keylen; + key = NULL; + break; + case YPERR_NOMORE: + _nis_state.done = 1; + goto nisent_out; + default: + rv = NS_UNAVAIL; + goto nisent_out; + } + } else { /* new search */ + if (yp_first(_nis_state.domain, PASSWD_BYNAME(&_nis_state), + &_nis_state.current, &_nis_state.currentlen, + &data, &datalen)) { + rv = NS_UNAVAIL; + goto nisent_out; + } + } + + data[datalen] = '\0'; /* clear trailing \n */ + /* validate line */ + if (_nis_parse(data, &_nis_passwd, + _nis_passwdbuf, sizeof(_nis_passwdbuf), &_nis_state)) + rv = NS_SUCCESS; + else { /* dodgy entry, try again */ + free(data); + goto next_nis_entry; + } + + nisent_out: + if (key) + free(key); + if (data) + free(data); + if (rv == NS_SUCCESS) + *retval = &_nis_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getpwent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + char *key, *data; + int keylen, datalen, rv, nisr; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *retval = 0; + + if (_nis_state.done) /* exhausted search */ + return NS_NOTFOUND; + if (_nis_state.domain == NULL) { + /* only start if NIS not setup */ + rv = _nis_start(&_nis_state); + if (rv != NS_SUCCESS) + return rv; + } + + next_nis_entry: + key = NULL; + data = NULL; + rv = NS_NOTFOUND; + + if (_nis_state.current) { /* already searching */ + nisr = yp_next(_nis_state.domain, PASSWD_BYNAME(&_nis_state), + _nis_state.current, _nis_state.currentlen, + &key, &keylen, &data, &datalen); + free(_nis_state.current); + _nis_state.current = NULL; + switch (nisr) { + case 0: + _nis_state.current = key; + _nis_state.currentlen = keylen; + key = NULL; + break; + case YPERR_NOMORE: + _nis_state.done = 1; + goto nisent_out; + default: + rv = NS_UNAVAIL; + goto nisent_out; + } + } else { /* new search */ + if (yp_first(_nis_state.domain, PASSWD_BYNAME(&_nis_state), + &_nis_state.current, &_nis_state.currentlen, + &data, &datalen)) { + rv = NS_UNAVAIL; + goto nisent_out; + } + } + + data[datalen] = '\0'; /* clear trailing \n */ + /* validate line */ + if (_nis_parse(data, pw, buffer, buflen, &_nis_state)) + rv = NS_SUCCESS; + else { /* dodgy entry, try again */ + if (key) + free(key); + free(data); + goto next_nis_entry; + } + + nisent_out: + if (key) + free(key); + if (data) + free(data); + if (rv == NS_SUCCESS) + *result = pw; + else + *result = NULL; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getpwuid(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + uid_t uid = va_arg(ap, uid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _nis_start(&_nis_state); + if (rv != NS_SUCCESS) + return rv; + snprintf(_nis_passwdbuf, sizeof(_nis_passwdbuf), "%u", (unsigned int)uid); + rv = _nis_pwscan(&rerror, &_nis_passwd, + _nis_passwdbuf, sizeof(_nis_passwdbuf), + &_nis_state, PASSWD_BYUID(&_nis_state)); + if (!_nis_state.stayopen) + _nis_end(&_nis_state); + if (rv == NS_SUCCESS && uid == _nis_passwd.pw_uid) + *retval = &_nis_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getpwuid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + uid_t uid = va_arg(ap, uid_t); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct nis_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = _nis_start(&state); + if (rv != NS_SUCCESS) + return rv; + snprintf(buffer, buflen, "%u", (unsigned int)uid); + rv = _nis_pwscan(retval, pw, buffer, buflen, + &state, PASSWD_BYUID(&state)); + _nis_end(&state); + if (rv != NS_SUCCESS) + return rv; + if (uid == pw->pw_uid) { + *result = pw; + return NS_SUCCESS; + } else + return NS_NOTFOUND; +} + +/*ARGSUSED*/ +static int +_nis_getpwnam(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _nis_start(&_nis_state); + if (rv != NS_SUCCESS) + return rv; + snprintf(_nis_passwdbuf, sizeof(_nis_passwdbuf), "%s", name); + rv = _nis_pwscan(&rerror, &_nis_passwd, + _nis_passwdbuf, sizeof(_nis_passwdbuf), + &_nis_state, PASSWD_BYNAME(&_nis_state)); + if (!_nis_state.stayopen) + _nis_end(&_nis_state); + if (rv == NS_SUCCESS && strcmp(name, _nis_passwd.pw_name) == 0) + *retval = &_nis_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_nis_getpwnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct nis_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + snprintf(buffer, buflen, "%s", name); + memset(&state, 0, sizeof(state)); + rv = _nis_start(&state); + if (rv != NS_SUCCESS) + return rv; + rv = _nis_pwscan(retval, pw, buffer, buflen, + &state, PASSWD_BYNAME(&state)); + _nis_end(&state); + if (rv != NS_SUCCESS) + return rv; + if (strcmp(name, pw->pw_name) == 0) { + *result = pw; + return NS_SUCCESS; + } else + return NS_NOTFOUND; +} + +#endif /* YP */ + + +#ifdef _PASSWD_COMPAT + /* + * compat methods + */ + + /* state shared between compat methods */ + +struct compat_state { + int stayopen; /* see getpassent(3) */ + DB *db; /* passwd DB */ + int keynum; /* key counter, -1 if no more */ + enum { /* current compat mode */ + COMPAT_NOTOKEN = 0, /* no compat token present */ + COMPAT_NONE, /* parsing normal pwd.db line */ + COMPAT_FULL, /* parsing `+' entries */ + COMPAT_USER, /* parsing `+name' entries */ + COMPAT_NETGROUP /* parsing `+@netgroup' entries */ + } mode; + char *user; /* COMPAT_USER "+name" */ + DB *exclude; /* compat exclude DB */ + struct passwd proto; /* proto passwd entry */ + char protobuf[_GETPW_R_SIZE_MAX]; + /* buffer for proto ptrs */ + int protoflags; /* proto passwd flags */ + int version; +}; + +static struct compat_state _compat_state; + /* storage for non _r functions */ +static struct passwd _compat_passwd; +static char _compat_passwdbuf[_GETPW_R_SIZE_MAX]; + +static int +_compat_start(struct compat_state *state) +{ + int rv; + + _DIAGASSERT(state != NULL); + + state->keynum = 0; + if (state->db == NULL) { /* not open yet */ + DBT key, data; + DBT pkey, pdata; + char bf[MAXLOGNAME]; + + rv = _pw_opendb(&state->db, &state->version); + if (rv != NS_SUCCESS) + return rv; + + state->mode = COMPAT_NOTOKEN; + + /* + * Determine if the "compat" token is present in pwd.db; + * either "__YP!" or PW_KEYBYNAME+"+". + * Only works if pwd_mkdb installs the token. + */ + key.data = (u_char *)__UNCONST(__yp_token); + key.size = strlen(__yp_token); + + bf[0] = _PW_KEYBYNAME; /* Pre-token database support. */ + bf[1] = '+'; + pkey.data = (u_char *)bf; + pkey.size = 2; + + if ((state->db->get)(state->db, &key, &data, 0) == 0 + || (state->db->get)(state->db, &pkey, &pdata, 0) == 0) + state->mode = COMPAT_NONE; + } + return NS_SUCCESS; +} + +static int +_compat_end(struct compat_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->keynum = 0; + if (state->db) { + (void)(state->db->close)(state->db); + state->db = NULL; + } + state->mode = COMPAT_NOTOKEN; + if (state->user) + free(state->user); + state->user = NULL; + if (state->exclude != NULL) + (void)(state->exclude->close)(state->exclude); + state->exclude = NULL; + state->proto.pw_name = NULL; + state->protoflags = 0; + return NS_SUCCESS; +} + +/* + * _compat_add_exclude + * add the name to the exclude list in state->exclude. + */ +static int +_compat_add_exclude(struct compat_state *state, const char *name) +{ + DBT key, data; + + _DIAGASSERT(state != NULL); + _DIAGASSERT(name != NULL); + + /* initialize the exclusion table if needed */ + if (state->exclude == NULL) { + state->exclude = dbopen(NULL, O_RDWR, 600, DB_HASH, NULL); + if (state->exclude == NULL) + return 0; + } + + key.size = strlen(name); /* set up the key */ + key.data = (u_char *)__UNCONST(name); + + data.data = NULL; /* data is nothing */ + data.size = 0; + + /* store it */ + if ((state->exclude->put)(state->exclude, &key, &data, 0) == -1) + return 0; + + return 1; +} + +/* + * _compat_is_excluded + * test if a name is on the compat mode exclude list + */ +static int +_compat_is_excluded(struct compat_state *state, const char *name) +{ + DBT key, data; + + _DIAGASSERT(state != NULL); + _DIAGASSERT(name != NULL); + + if (state->exclude == NULL) + return 0; /* nothing excluded */ + + key.size = strlen(name); /* set up the key */ + key.data = (u_char *)__UNCONST(name); + + if ((state->exclude->get)(state->exclude, &key, &data, 0) == 0) + return 1; /* is excluded */ + + return 0; +} + + +/* + * _passwdcompat_bad + * log an error if "files" or "compat" is specified in + * passwd_compat database + */ +/*ARGSUSED*/ +static int +_passwdcompat_bad(void *nsrv, void *nscb, va_list ap) +{ + static int warned; + + _DIAGASSERT(nsrv != NULL); + _DIAGASSERT(nscb != NULL); + + if (!warned) { + syslog(LOG_ERR, + "nsswitch.conf passwd_compat database can't use '%s'", + (char *)nscb); + } + warned = 1; + return NS_UNAVAIL; +} + +/* + * _passwdcompat_setpassent + * Call setpassent for all passwd_compat sources. + */ +static int +_passwdcompat_setpassent(int stayopen) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_passwdcompat_bad, "files") + NS_DNS_CB(_dns_setpassent, NULL) + NS_NIS_CB(_nis_setpassent, NULL) + NS_COMPAT_CB(_passwdcompat_bad, "compat") + NS_NULL_CB + }; + + int rv, result; + + rv = nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "setpassent", + __nsdefaultnis_forceall, &result, stayopen); + return rv; +} + +/* + * _passwdcompat_endpwent + * Call endpwent for all passwd_compat sources. + */ +static int +_passwdcompat_endpwent(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_passwdcompat_bad, "files") + NS_DNS_CB(_dns_endpwent, NULL) + NS_NIS_CB(_nis_endpwent, NULL) + NS_COMPAT_CB(_passwdcompat_bad, "compat") + NS_NULL_CB + }; + + return nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "endpwent", + __nsdefaultnis_forceall); +} + +/* + * _passwdcompat_pwscan + * When a name lookup in compat mode is required (e.g., `+name', or a + * name in `+@netgroup'), look it up in the 'passwd_compat' nsswitch + * database. + * Fail if passwd_compat contains files or compat. + */ +static int +_passwdcompat_pwscan(struct passwd *pw, char *buffer, size_t buflen, + int search, const char *name, uid_t uid) +{ + static const ns_dtab compatentdtab[] = { + NS_FILES_CB(_passwdcompat_bad, "files") + NS_DNS_CB(_dns_getpwent_r, NULL) + NS_NIS_CB(_nis_getpwent_r, NULL) + NS_COMPAT_CB(_passwdcompat_bad, "compat") + NS_NULL_CB + }; + static const ns_dtab compatuiddtab[] = { + NS_FILES_CB(_passwdcompat_bad, "files") + NS_DNS_CB(_dns_getpwuid_r, NULL) + NS_NIS_CB(_nis_getpwuid_r, NULL) + NS_COMPAT_CB(_passwdcompat_bad, "compat") + NS_NULL_CB + }; + static const ns_dtab compatnamdtab[] = { + NS_FILES_CB(_passwdcompat_bad, "files") + NS_DNS_CB(_dns_getpwnam_r, NULL) + NS_NIS_CB(_nis_getpwnam_r, NULL) + NS_COMPAT_CB(_passwdcompat_bad, "compat") + NS_NULL_CB + }; + + int rv, crv; + struct passwd *cpw; + + switch (search) { + case _PW_KEYBYNUM: + rv = nsdispatch(NULL, compatentdtab, + NSDB_PASSWD_COMPAT, "getpwent_r", __nsdefaultnis, + &crv, pw, buffer, buflen, &cpw); + break; + case _PW_KEYBYNAME: + _DIAGASSERT(name != NULL); + rv = nsdispatch(NULL, compatnamdtab, + NSDB_PASSWD_COMPAT, "getpwnam_r", __nsdefaultnis, + &crv, name, pw, buffer, buflen, &cpw); + break; + case _PW_KEYBYUID: + rv = nsdispatch(NULL, compatuiddtab, + NSDB_PASSWD_COMPAT, "getpwuid_r", __nsdefaultnis, + &crv, uid, pw, buffer, buflen, &cpw); + break; + default: + abort(); + /*NOTREACHED*/ + } + return rv; +} + +/* + * _compat_pwscan + * Search state->db for the next desired entry. + * If search is _PW_KEYBYNUM, look for state->keynum. + * If search is _PW_KEYBYNAME, look for name. + * If search is _PW_KEYBYUID, look for uid. + * Sets *retval to the errno if the result is not NS_SUCCESS + * or NS_NOTFOUND. + */ +static int +_compat_pwscan(int *retval, struct passwd *pw, char *buffer, size_t buflen, + struct compat_state *state, int search, const char *name, uid_t uid) +{ + DBT key; + int rv, r, pwflags; + const char *user, *host, *dom; + const void *from; + size_t fromlen; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(state != NULL); + /* name may be NULL */ + + *retval = 0; + + if (state->db == NULL) { + rv = _compat_start(state); + if (rv != NS_SUCCESS) + return rv; + } + if (buflen <= 1) { /* buffer too small */ + *retval = ERANGE; + return NS_UNAVAIL; + } + + for (;;) { /* loop over pwd.db */ + rv = NS_NOTFOUND; + if (state->mode != COMPAT_NOTOKEN && + state->mode != COMPAT_NONE) { + /* doing a compat lookup */ + struct passwd cpw; + char cbuf[_GETPW_R_SIZE_MAX]; + + switch (state->mode) { + + case COMPAT_FULL: + /* get next user or lookup by key */ + rv = _passwdcompat_pwscan(&cpw, + cbuf, sizeof(cbuf), search, name, uid); + if (rv != NS_SUCCESS) + state->mode = COMPAT_NONE; + break; + + case COMPAT_NETGROUP: +/* XXXREENTRANT: getnetgrent is not thread safe */ + /* get next user from netgroup */ + r = getnetgrent(&host, &user, &dom); + if (r == 0) { /* end of group */ + endnetgrent(); + state->mode = COMPAT_NONE; + break; + } + if (!user || !*user) + break; + rv = _passwdcompat_pwscan(&cpw, + cbuf, sizeof(cbuf), + _PW_KEYBYNAME, user, 0); + break; + + case COMPAT_USER: + /* get specific user */ + if (state->user == NULL) { + state->mode = COMPAT_NONE; + break; + } + rv = _passwdcompat_pwscan(&cpw, + cbuf, sizeof(cbuf), + _PW_KEYBYNAME, state->user, 0); + free(state->user); + state->user = NULL; + state->mode = COMPAT_NONE; + break; + + case COMPAT_NOTOKEN: + case COMPAT_NONE: + abort(); + + } + if (rv != NS_SUCCESS) /* if not matched, next loop */ + continue; + + /* copy cpw to pw, applying prototype */ + if (! _pw_copy(&cpw, pw, buffer, buflen, + &state->proto, state->protoflags)) { + rv = NS_UNAVAIL; + break; + } + + if (_compat_is_excluded(state, pw->pw_name)) + continue; /* excluded; next loop */ + + if ((search == _PW_KEYBYNAME + && strcmp(pw->pw_name, name) != 0) + || (search == _PW_KEYBYUID && pw->pw_uid != uid)) { + continue; /* not specific; next loop */ + } + + break; /* exit loop if found */ + } else { /* not a compat line */ + state->proto.pw_name = NULL; + /* clear prototype */ + } + + if (state->mode == COMPAT_NOTOKEN) { + /* no compat token; do direct lookup */ + switch (search) { + case _PW_KEYBYNUM: + if (state->keynum == -1) /* no more records */ + return NS_NOTFOUND; + state->keynum++; + from = &state->keynum; + fromlen = sizeof(state->keynum); + break; + case _PW_KEYBYNAME: + from = name; + fromlen = strlen(name); + break; + case _PW_KEYBYUID: + from = &uid; + fromlen = sizeof(uid); + break; + default: + abort(); + } + buffer[0] = search; + } else { + /* compat token; do line by line */ + if (state->keynum == -1) /* no more records */ + return NS_NOTFOUND; + state->keynum++; + from = &state->keynum; + fromlen = sizeof(state->keynum); + buffer[0] = _PW_KEYBYNUM; + } + + if (buflen <= fromlen) { /* buffer too small */ + *retval = ERANGE; + return NS_UNAVAIL; + } + memmove(buffer + 1, from, fromlen); /* setup key */ + key.size = fromlen + 1; + key.data = (u_char *)buffer; + + rv = _pw_getkey(state->db, &key, pw, buffer, buflen, &pwflags, + state->version); + if (rv != NS_SUCCESS) /* stop on error */ + break; + + if (state->mode == COMPAT_NOTOKEN) + break; /* stop if no compat token */ + + if (pw->pw_name[0] == '+') { + /* compat inclusion */ + switch(pw->pw_name[1]) { + case '\0': /* `+' */ + state->mode = COMPAT_FULL; + /* reset passwd_compat search */ +/* XXXREENTRANT: setpassent is not thread safe ? */ + (void) _passwdcompat_setpassent(0); + break; + case '@': /* `+@netgroup' */ + state->mode = COMPAT_NETGROUP; + /* reset netgroup search */ +/* XXXREENTRANT: setnetgrent is not thread safe */ + setnetgrent(pw->pw_name + 2); + break; + default: /* `+name' */ + state->mode = COMPAT_USER; + if (state->user) + free(state->user); + state->user = strdup(pw->pw_name + 1); + break; + } + /* save the prototype */ + state->protoflags = pwflags; + if (! _pw_copy(pw, &state->proto, state->protobuf, + sizeof(state->protobuf), NULL, 0)) { + rv = NS_UNAVAIL; + break; + } + continue; /* loop again after inclusion */ + } else if (pw->pw_name[0] == '-') { + /* compat exclusion */ + rv = NS_SUCCESS; + switch(pw->pw_name[1]) { + case '\0': /* `-' */ + break; + case '@': /* `-@netgroup' */ +/* XXXREENTRANT: {set,get,end}netgrent is not thread safe */ + setnetgrent(pw->pw_name + 2); + while (getnetgrent(&host, &user, &dom)) { + if (!user || !*user) + continue; + if (! _compat_add_exclude(state,user)) { + rv = NS_UNAVAIL; + break; + } + } + endnetgrent(); + break; + default: /* `-name' */ + if (! _compat_add_exclude(state, + pw->pw_name + 1)) { + rv = NS_UNAVAIL; + } + break; + } + if (rv != NS_SUCCESS) /* exclusion failure */ + break; + continue; /* loop again after exclusion */ + } + if (search == _PW_KEYBYNUM || + (search == _PW_KEYBYUID && pw->pw_uid == uid) || + (search == _PW_KEYBYNAME && strcmp(pw->pw_name, name) == 0)) + break; /* token mode match found */ + } + + if (rv == NS_NOTFOUND && + (search == _PW_KEYBYNUM || state->mode != COMPAT_NOTOKEN)) + state->keynum = -1; /* flag `no more records' */ + + if (rv == NS_SUCCESS) { + if ((search == _PW_KEYBYNAME && strcmp(pw->pw_name, name) != 0) + || (search == _PW_KEYBYUID && pw->pw_uid != uid)) + rv = NS_NOTFOUND; + } + + if (rv != NS_SUCCESS && rv != NS_NOTFOUND) + *retval = errno; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_setpwent(void *nsrv, void *nscb, va_list ap) +{ + + /* force passwd_compat setpwent() */ + (void) _passwdcompat_setpassent(0); + + /* reset state, keep db open */ + _compat_state.stayopen = 0; + return _compat_start(&_compat_state); +} + +/*ARGSUSED*/ +static int +_compat_setpassent(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + int stayopen = va_arg(ap, int); + + int rv; + + /* force passwd_compat setpassent() */ + (void) _passwdcompat_setpassent(stayopen); + + _compat_state.stayopen = stayopen; + rv = _compat_start(&_compat_state); + *retval = (rv == NS_SUCCESS); + return rv; +} + +/*ARGSUSED*/ +static int +_compat_endpwent(void *nsrv, void *nscb, va_list ap) +{ + + /* force passwd_compat endpwent() */ + (void) _passwdcompat_endpwent(); + + /* reset state, close db */ + _compat_state.stayopen = 0; + return _compat_end(&_compat_state); +} + + +/*ARGSUSED*/ +static int +_compat_getpwent(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _compat_pwscan(&rerror, &_compat_passwd, + _compat_passwdbuf, sizeof(_compat_passwdbuf), + &_compat_state, _PW_KEYBYNUM, NULL, 0); + if (rv == NS_SUCCESS) + *retval = &_compat_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getpwent_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + rv = _compat_pwscan(retval, pw, buffer, buflen, &_compat_state, + _PW_KEYBYNUM, NULL, 0); + if (rv == NS_SUCCESS) + *result = pw; + else + *result = NULL; + return rv; +} + + +/*ARGSUSED*/ +static int +_compat_getpwnam(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + const char *name = va_arg(ap, const char *); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _compat_start(&_compat_state); + if (rv != NS_SUCCESS) + return rv; + rv = _compat_pwscan(&rerror, &_compat_passwd, + _compat_passwdbuf, sizeof(_compat_passwdbuf), + &_compat_state, _PW_KEYBYNAME, name, 0); + if (!_compat_state.stayopen) + _compat_end(&_compat_state); + if (rv == NS_SUCCESS) + *retval = &_compat_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getpwnam_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + const char *name = va_arg(ap, const char *); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct compat_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = _compat_pwscan(retval, pw, buffer, buflen, &state, + _PW_KEYBYNAME, name, 0); + _compat_end(&state); + if (rv == NS_SUCCESS) + *result = pw; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getpwuid(void *nsrv, void *nscb, va_list ap) +{ + struct passwd **retval = va_arg(ap, struct passwd **); + uid_t uid = va_arg(ap, uid_t); + + int rv, rerror; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + rv = _compat_start(&_compat_state); + if (rv != NS_SUCCESS) + return rv; + rv = _compat_pwscan(&rerror, &_compat_passwd, + _compat_passwdbuf, sizeof(_compat_passwdbuf), + &_compat_state, _PW_KEYBYUID, NULL, uid); + if (!_compat_state.stayopen) + _compat_end(&_compat_state); + if (rv == NS_SUCCESS) + *retval = &_compat_passwd; + return rv; +} + +/*ARGSUSED*/ +static int +_compat_getpwuid_r(void *nsrv, void *nscb, va_list ap) +{ + int *retval = va_arg(ap, int *); + uid_t uid = va_arg(ap, uid_t); + struct passwd *pw = va_arg(ap, struct passwd *); + char *buffer = va_arg(ap, char *); + size_t buflen = va_arg(ap, size_t); + struct passwd **result = va_arg(ap, struct passwd **); + + struct compat_state state; + int rv; + + _DIAGASSERT(retval != NULL); + _DIAGASSERT(pw != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + memset(&state, 0, sizeof(state)); + rv = _compat_pwscan(retval, pw, buffer, buflen, &state, + _PW_KEYBYUID, NULL, uid); + _compat_end(&state); + if (rv == NS_SUCCESS) + *result = pw; + return rv; +} + +#endif /* _PASSWD_COMPAT */ + + + /* + * public functions + */ + +struct passwd * +getpwent(void) +{ + int r; + struct passwd *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getpwent, NULL) + NS_DNS_CB(_dns_getpwent, NULL) + NS_NIS_CB(_nis_getpwent, NULL) + NS_COMPAT_CB(_compat_getpwent, NULL) + NS_NULL_CB + }; + + mutex_lock(&_pwmutex); + r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwent", __nsdefaultcompat, + &retval); + mutex_unlock(&_pwmutex); + return (r == NS_SUCCESS) ? retval : NULL; +} + +int +getpwent_r(struct passwd *pwd, char *buffer, size_t buflen, + struct passwd **result) +{ + int r, retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getpwent_r, NULL) + NS_DNS_CB(_dns_getpwent_r, NULL) + NS_NIS_CB(_nis_getpwent_r, NULL) + NS_COMPAT_CB(_compat_getpwent_r, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(pwd != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + retval = 0; + mutex_lock(&_pwmutex); + r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwent_r", __nsdefaultcompat, + &retval, pwd, buffer, buflen, result); + mutex_unlock(&_pwmutex); + switch (r) { + case NS_SUCCESS: + case NS_NOTFOUND: + return 0; + default: + return retval; + } +} + + +struct passwd * +getpwnam(const char *name) +{ + int rv; + struct passwd *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getpwnam, NULL) + NS_DNS_CB(_dns_getpwnam, NULL) + NS_NIS_CB(_nis_getpwnam, NULL) + NS_COMPAT_CB(_compat_getpwnam, NULL) + NS_NULL_CB + }; + + mutex_lock(&_pwmutex); + rv = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwnam", __nsdefaultcompat, + &retval, name); + mutex_unlock(&_pwmutex); + return (rv == NS_SUCCESS) ? retval : NULL; +} + +int +getpwnam_r(const char *name, struct passwd *pwd, char *buffer, size_t buflen, + struct passwd **result) +{ + int r, retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getpwnam_r, NULL) + NS_DNS_CB(_dns_getpwnam_r, NULL) + NS_NIS_CB(_nis_getpwnam_r, NULL) + NS_COMPAT_CB(_compat_getpwnam_r, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(pwd != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + retval = 0; + mutex_lock(&_pwmutex); + r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwnam_r", __nsdefaultcompat, + &retval, name, pwd, buffer, buflen, result); + mutex_unlock(&_pwmutex); + switch (r) { + case NS_SUCCESS: + case NS_NOTFOUND: + return 0; + default: + return retval; + } +} + +struct passwd * +getpwuid(uid_t uid) +{ + int rv; + struct passwd *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getpwuid, NULL) + NS_DNS_CB(_dns_getpwuid, NULL) + NS_NIS_CB(_nis_getpwuid, NULL) + NS_COMPAT_CB(_compat_getpwuid, NULL) + NS_NULL_CB + }; + + mutex_lock(&_pwmutex); + rv = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwuid", __nsdefaultcompat, + &retval, uid); + mutex_unlock(&_pwmutex); + return (rv == NS_SUCCESS) ? retval : NULL; +} + +int +getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t buflen, + struct passwd **result) +{ + int r, retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getpwuid_r, NULL) + NS_DNS_CB(_dns_getpwuid_r, NULL) + NS_NIS_CB(_nis_getpwuid_r, NULL) + NS_COMPAT_CB(_compat_getpwuid_r, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(pwd != NULL); + _DIAGASSERT(buffer != NULL); + _DIAGASSERT(result != NULL); + + *result = NULL; + retval = 0; + mutex_lock(&_pwmutex); + r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwuid_r", __nsdefaultcompat, + &retval, uid, pwd, buffer, buflen, result); + mutex_unlock(&_pwmutex); + switch (r) { + case NS_SUCCESS: + case NS_NOTFOUND: + return 0; + default: + return retval; + } +} + +void +endpwent(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_endpwent, NULL) + NS_DNS_CB(_dns_endpwent, NULL) + NS_NIS_CB(_nis_endpwent, NULL) + NS_COMPAT_CB(_compat_endpwent, NULL) + NS_NULL_CB + }; + + mutex_lock(&_pwmutex); + /* force all endpwent() methods */ + (void) nsdispatch(NULL, dtab, NSDB_PASSWD, "endpwent", + __nsdefaultcompat_forceall); + mutex_unlock(&_pwmutex); +} + +/*ARGSUSED*/ +int +setpassent(int stayopen) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_setpassent, NULL) + NS_DNS_CB(_dns_setpassent, NULL) + NS_NIS_CB(_nis_setpassent, NULL) + NS_COMPAT_CB(_compat_setpassent, NULL) + NS_NULL_CB + }; + int rv, retval; + + mutex_lock(&_pwmutex); + /* force all setpassent() methods */ + rv = nsdispatch(NULL, dtab, NSDB_PASSWD, "setpassent", + __nsdefaultcompat_forceall, &retval, stayopen); + mutex_unlock(&_pwmutex); + return (rv == NS_SUCCESS) ? retval : 0; +} + +void +setpwent(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_setpwent, NULL) + NS_DNS_CB(_dns_setpwent, NULL) + NS_NIS_CB(_nis_setpwent, NULL) + NS_COMPAT_CB(_compat_setpwent, NULL) + NS_NULL_CB + }; + + mutex_lock(&_pwmutex); + /* force all setpwent() methods */ + (void) nsdispatch(NULL, dtab, NSDB_PASSWD, "setpwent", + __nsdefaultcompat_forceall); + mutex_unlock(&_pwmutex); +} diff --git a/lib/nbsd_libc/gen/getttyent.3 b/lib/nbsd_libc/gen/getttyent.3 new file mode 100644 index 000000000..e0757bad9 --- /dev/null +++ b/lib/nbsd_libc/gen/getttyent.3 @@ -0,0 +1,219 @@ +.\" $NetBSD: getttyent.3,v 1.18 2006/04/23 16:46:32 wiz 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. +.\" +.\" @(#)getttyent.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd April 18, 2006 +.Dt GETTTYENT 3 +.Os +.Sh NAME +.Nm getttyent , +.Nm getttynam , +.Nm setttyent , +.Nm setttyentpath , +.Nm endttyent +.Nd get ttys file entry +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ttyent.h +.Ft struct ttyent * +.Fn getttyent +.Ft struct ttyent * +.Fn getttynam "char *name" +.Ft int +.Fn setttyent void +.Ft int +.Fn setttyentpath "const char *path" +.Ft int +.Fn endttyent void +.Sh DESCRIPTION +The +.Fn getttyent , +and +.Fn getttynam +functions +each return a pointer to an object, with the following structure, +containing the broken-out fields of a line from the tty description +file. +.Bd -literal +struct ttyent { + char *ty_name; /* terminal device name */ + char *ty_getty; /* command to execute */ + char *ty_type; /* terminal type */ +#define TTY_ON 0x01 /* enable logins */ +#define TTY_SECURE 0x02 /* allow uid of 0 to login */ +#define TTY_LOCAL 0x04 /* set 'CLOCAL' on open (dev. specific) */ +#define TTY_RTSCTS 0x08 /* set 'CRTSCTS' on open (dev. specific) */ +#define TTY_SOFTCAR 0x10 /* ignore hardware carrier (dev. spec.) */ +#define TTY_MDMBUF 0x20 /* set 'MDMBUF' on open (dev. specific) */ +#define TTY_DTRCTS 0x40 /* set 'CDTRCTS' on open (dev. specific) */ + int ty_status; /* flag values */ + char *ty_window; /* command for window manager */ + char *ty_comment; /* comment field */ + char *ty_class; /* category of tty usage */ +}; +.Ed +.Pp +The fields are as follows: +.Bl -tag -width ty_comment +.It Fa ty_name +The name of the character-special file. +.It Fa ty_getty +The name of the command invoked by +.Xr init 8 +to initialize tty line characteristics. +.It Fa ty_type +The name of the default terminal type connected to this tty line. +.It Fa ty_status +A mask of bit fields which indicate various actions allowed on this +tty line. +The possible flags are as follows: +.Bl -tag -width TTY_SOFTCAR +.It Dv TTY_ON +Enables logins (i.e., +.Xr init 8 +will start the command referenced by +.Fa ty_getty +on this entry). +.It Dv TTY_SECURE +Allow users with a uid of 0 to login on this terminal. +.It Dv TTY_LOCAL +If the terminal port's driver supports it, cause the line +to be treated as ``local.'' +.It Dv TTY_MDMBUF +If the terminal port's driver supports it, use +DTR/DCD hardware flow control on the line by default. +.It Dv TTY_RTSCTS +If the terminal port's driver supports it, use +full-duplex RTS/CTS hardware flow control on the line +by default. +.It Dv TTY_SOFTCAR +If the terminal port's driver supports it, ignore hardware +carrier on the line. +.El +.It Fa ty_window +The command to execute for a window system associated with the line. +.It Fa ty_comment +Any trailing comment field, with any leading hash marks (``#'') or +whitespace removed. +.It Fa ty_class +A key indexing into a termcap-style database (/etc/ttyclasses) +of attributes for this class of tty. +No attributes are currently defined or used, +so there are currently no functions to retrieve them. +.El +.Pp +If any of the fields pointing to character strings are unspecified, +they are returned as null pointers. +The field +.Fa ty_status +will be zero if no flag values are specified. +.Pp +See +.Xr ttys 5 +for a more complete discussion of the meaning and usage of the +fields. +.Pp +The +.Fn getttyent +function +reads the next line from the ttys file, opening the file if necessary. +The +.Fn setttyent +function +rewinds the file if open, or opens the file if it is unopened. +The +.Fn setttyentpath +function +is equivalent to +.Fn setttyent +but accepts an additional argument to read the ttys information from +an alternate file instead of the default location +.Pq defined in Dv _PATH_TTYS . +The +.Fn endttyent +function +closes any open files. +.Pp +The +.Fn getttynam +function +searches from the beginning of the file until a matching +.Fa name +is found +(or until +.Dv EOF +is encountered). +.Sh RETURN VALUES +The routines +.Fn getttyent +and +.Fn getttynam +return a null pointer on +.Dv EOF +or error. +The +.Fn setttyent +and +.Fn setttyentpath +functions +and +.Fn endttyent +return 0 on failure and 1 on success. +.Sh FILES +.Bl -tag -width /etc/ttys -compact +.It Pa /etc/ttys +.El +.Sh SEE ALSO +.Xr login 1 , +.Xr ttyslot 3 , +.Xr gettytab 5 , +.Xr termcap 5 , +.Xr ttys 5 , +.Xr getty 8 , +.Xr init 8 , +.Xr ttyflags 8 +.Sh HISTORY +The +.Fn getttyent , +.Fn getttynam , +.Fn setttyent , +and +.Fn endttyent +functions appeared in +.Bx 4.3 . +The +.Fn setttyentpath +function appeared in +.Nx 4.0 . +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. diff --git a/lib/nbsd_libc/gen/getttyent.c b/lib/nbsd_libc/gen/getttyent.c new file mode 100644 index 000000000..f3f3e2af2 --- /dev/null +++ b/lib/nbsd_libc/gen/getttyent.c @@ -0,0 +1,248 @@ +/* $NetBSD: getttyent.c,v 1.23 2006/04/17 23:29:21 salo Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getttyent.c,v 1.23 2006/04/17 23:29:21 salo Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(endttyent,_endttyent) +__weak_alias(getttyent,_getttyent) +__weak_alias(getttynam,_getttynam) +__weak_alias(setttyent,_setttyent) +__weak_alias(setttyentpath,_setttyentpath) +#endif + +static FILE *tf; +static size_t lineno = 0; +static char *skip(char *, char *); +static char *value(char *); + +struct ttyent * +getttynam(const char *tty) +{ + struct ttyent *t; + + _DIAGASSERT(tty != NULL); + + setttyent(); + while ((t = getttyent()) != NULL) + if (!strcmp(tty, t->ty_name)) + break; + endttyent(); + return (t); +} + +struct ttyent * +getttyent(void) +{ + static struct ttyent tty; + int c; + char *p; + size_t len; + static char *line = NULL; + char zapchar; + + if (line) + free(line); + + if (!tf && !setttyent()) + return NULL; + + for (;;) { + errno = 0; + line = fparseln(tf, &len, &lineno, NULL, FPARSELN_UNESCALL); + if (line == NULL) { + if (errno != 0) + warn(__func__); + return NULL; + } + for (p = line; *p && isspace((unsigned char)*p); p++) + continue; + if (*p && *p != '#') + break; + free(line); + } + + tty.ty_name = p; + p = skip(p, &zapchar); + if (*(tty.ty_getty = p) == '\0') + tty.ty_getty = tty.ty_type = NULL; + else { + p = skip(p, &zapchar); + if (*(tty.ty_type = p) == '\0') + tty.ty_type = NULL; + else + p = skip(p, &zapchar); + } + tty.ty_status = 0; + tty.ty_window = NULL; + tty.ty_class = NULL; + +#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0') +#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' + for (; *p; p = skip(p, &zapchar)) { + if (scmp(_TTYS_OFF)) + tty.ty_status &= ~TTY_ON; + else if (scmp(_TTYS_ON)) + tty.ty_status |= TTY_ON; + else if (scmp(_TTYS_SECURE)) + tty.ty_status |= TTY_SECURE; + else if (scmp(_TTYS_LOCAL)) + tty.ty_status |= TTY_LOCAL; + else if (scmp(_TTYS_RTSCTS)) + tty.ty_status |= TTY_RTSCTS; + else if (scmp(_TTYS_DTRCTS)) + tty.ty_status |= TTY_DTRCTS; + else if (scmp(_TTYS_SOFTCAR)) + tty.ty_status |= TTY_SOFTCAR; + else if (scmp(_TTYS_MDMBUF)) + tty.ty_status |= TTY_MDMBUF; + else if (vcmp(_TTYS_WINDOW)) + tty.ty_window = value(p); + else if (vcmp(_TTYS_CLASS)) + tty.ty_class = value(p); + else + warnx("%s: %s, %lu: unknown option `%s'", + __func__, _PATH_TTYS, (unsigned long)lineno, p); + } + + if (zapchar == '#' || *p == '#') + while ((c = *++p) == ' ' || c == '\t') + continue; + tty.ty_comment = p; + if (*p == '\0') + tty.ty_comment = NULL; + if ((p = strchr(p, '\n')) != NULL) + *p = '\0'; + return &tty; +} + +#define QUOTED 1 + +/* + * Skip over the current field, removing quotes, and return a pointer to + * the next field. + */ +static char * +skip(char *p, char *zapchar) +{ + char *t; + int c, q; + + _DIAGASSERT(p != NULL); + *zapchar = '\0'; + + for (q = 0, t = p; (c = *p) != '\0'; p++) { + if (c == '"') { + q ^= QUOTED; /* obscure, but nice */ + continue; + } + if (q == QUOTED && *p == '\\' && *(p+1) == '"') + p++; + *t++ = *p; + if (q == QUOTED) + continue; + if (c == '#') { + *zapchar = c; + *p = '\0'; + *--t = '\0'; + return p; + } + if (c == '\t' || c == ' ' || c == '\n') { + *zapchar = c; + *p++ = '\0'; + while ((c = *p) == '\t' || c == ' ' || c == '\n') + p++; + *--t = '\0'; + return p; + } + } + if (t != p) + *t = '\0'; + return p; +} + +static char * +value(char *p) +{ + + _DIAGASSERT(p != NULL); + + return (p = strchr(p, '=')) != NULL ? ++p : NULL; +} + +int +setttyentpath(const char *path) +{ + lineno = 0; + if (tf) { + rewind(tf); + return 1; + } else if ((tf = fopen(path, "r")) != NULL) + return 1; + return 0; +} + +int +setttyent(void) +{ + return setttyentpath(_PATH_TTYS); +} + +int +endttyent(void) +{ + int rval; + + if (tf) { + rval = !(fclose(tf) == EOF); + tf = NULL; + return rval; + } + return 1; +} diff --git a/lib/nbsd_libc/gen/getusershell.3 b/lib/nbsd_libc/gen/getusershell.3 new file mode 100644 index 000000000..1b36738ee --- /dev/null +++ b/lib/nbsd_libc/gen/getusershell.3 @@ -0,0 +1,98 @@ +.\" $NetBSD: getusershell.3,v 1.10 2004/11/23 04:38:19 lukem Exp $ +.\" +.\" Copyright (c) 1985, 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. +.\" +.\" @(#)getusershell.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd November 23, 2004 +.Dt GETUSERSHELL 3 +.Os +.Sh NAME +.Nm getusershell , +.Nm setusershell , +.Nm endusershell +.Nd get valid user shells +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft char * +.Fn getusershell void +.Ft void +.Fn setusershell void +.Ft void +.Fn endusershell void +.Sh DESCRIPTION +The +.Fn getusershell +function +returns a pointer to a valid user shell as defined by the +system manager in the shells database as described in +.Xr shells 5 . +If the shells database is not available, +.Fn getusershell +behaves as if +.Pa /bin/sh +and +.Pa /bin/csh +were listed. +.Pp +The +.Fn getusershell +function +reads the next +line (opening the file if necessary); +.Fn setusershell +rewinds the file; +.Fn endusershell +closes it. +.Sh FILES +.Bl -tag -width /etc/shells -compact +.It Pa /etc/shells +.El +.Sh DIAGNOSTICS +The routine +.Fn getusershell +returns a null pointer (0) on +.Dv EOF . +.Sh SEE ALSO +.Xr nsswitch.conf 5 , +.Xr shells 5 +.Sh HISTORY +The +.Fn getusershell +function appeared in +.Bx 4.3 . +.Sh BUGS +The +.Fn getusershell +function leaves its result in an internal static object and returns +a pointer to that object. +Subsequent calls to +.Fn getusershell +will modify the same object. diff --git a/lib/nbsd_libc/gen/getusershell.c b/lib/nbsd_libc/gen/getusershell.c new file mode 100644 index 000000000..a9c7c241b --- /dev/null +++ b/lib/nbsd_libc/gen/getusershell.c @@ -0,0 +1,546 @@ +/* $NetBSD: getusershell.c,v 1.27 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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. + */ + +/* + * Copyright (c) 1985, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getusershell.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getusershell.c,v 1.27 2008/04/28 20:22:59 martin Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HESIOD +#include +#endif +#ifdef YP +#include +#include +#include +#endif + +#ifdef __weak_alias +__weak_alias(endusershell,_endusershell) +__weak_alias(getusershell,_getusershell) +__weak_alias(setusershell,_setusershell) +#endif + +/* + * Local shells should NOT be added here. + * They should be added in /etc/shells. + */ +static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; + +#ifdef _REENTRANT +static mutex_t __shellmutex = MUTEX_INITIALIZER; +#endif + +static char curshell[MAXPATHLEN + 2]; + +static const char *const *curokshell = okshells; +static int shellsfound = 0; + + /* + * files methods + */ + + /* state shared between files methods */ +struct files_state { + FILE *fp; +}; + +static struct files_state _files_state; + + +static int +_files_start(struct files_state *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->fp == NULL) { + state->fp = fopen(_PATH_SHELLS, "r"); + if (state->fp == NULL) + return NS_UNAVAIL; + } else { + rewind(state->fp); + } + return NS_SUCCESS; +} + +static int +_files_end(struct files_state *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->fp) { + (void) fclose(state->fp); + state->fp = NULL; + } + return NS_SUCCESS; +} + +/*ARGSUSED*/ +static int +_files_setusershell(void *nsrv, void *nscb, va_list ap) +{ + + return _files_start(&_files_state); +} + +/*ARGSUSED*/ +static int +_files_endusershell(void *nsrv, void *nscb, va_list ap) +{ + + return _files_end(&_files_state); +} + +/*ARGSUSED*/ +static int +_files_getusershell(void *nsrv, void *nscb, va_list ap) +{ + char **retval = va_arg(ap, char **); + + char *sp, *cp; + int rv; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + if (_files_state.fp == NULL) { /* only start if file not open yet */ + rv = _files_start(&_files_state); + if (rv != NS_SUCCESS) + return rv; + } + + while (fgets(curshell, sizeof(curshell) - 1, _files_state.fp) != NULL) { + sp = cp = curshell; + while (*cp != '#' && *cp != '/' && *cp != '\0') + cp++; + if (*cp == '#' || *cp == '\0') + continue; + sp = cp; + while (!isspace((unsigned char) *cp) && *cp != '#' + && *cp != '\0') + cp++; + *cp++ = '\0'; + *retval = sp; + return NS_SUCCESS; + } + + return NS_NOTFOUND; +} + + +#ifdef HESIOD + /* + * dns methods + */ + + /* state shared between dns methods */ +struct dns_state { + void *context; /* Hesiod context */ + int num; /* shell index, -1 if no more */ +}; + +static struct dns_state _dns_state; + +static int +_dns_start(struct dns_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->num = 0; + if (state->context == NULL) { /* setup Hesiod */ + if (hesiod_init(&state->context) == -1) + return NS_UNAVAIL; + } + + return NS_SUCCESS; +} + +static int +_dns_end(struct dns_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->num = 0; + if (state->context) { + hesiod_end(state->context); + state->context = NULL; + } + return NS_SUCCESS; +} + +/*ARGSUSED*/ +static int +_dns_setusershell(void *nsrv, void *nscb, va_list ap) +{ + + return _dns_start(&_dns_state); +} + +/*ARGSUSED*/ +static int +_dns_endusershell(void *nsrv, void *nscb, va_list ap) +{ + + return _dns_end(&_dns_state); +} + +/*ARGSUSED*/ +static int +_dns_getusershell(void *nsrv, void *nscb, va_list ap) +{ + char **retval = va_arg(ap, char **); + + char shellname[] = "shells-NNNNNNNNNN"; + char **hp, *ep; + int rv; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + + if (_dns_state.num == -1) /* exhausted search */ + return NS_NOTFOUND; + + if (_dns_state.context == NULL) { + /* only start if Hesiod not setup */ + rv = _dns_start(&_dns_state); + if (rv != NS_SUCCESS) + return rv; + } + + hp = NULL; + rv = NS_NOTFOUND; + + /* find shells-NNN */ + snprintf(shellname, sizeof(shellname), "shells-%d", _dns_state.num); + _dns_state.num++; + + hp = hesiod_resolve(_dns_state.context, shellname, "shells"); + if (hp == NULL) { + if (errno == ENOENT) + rv = NS_NOTFOUND; + else + rv = NS_UNAVAIL; + } else { + if ((ep = strchr(hp[0], '\n')) != NULL) + *ep = '\0'; /* clear trailing \n */ + /* only use first result */ + strlcpy(curshell, hp[0], sizeof(curshell)); + *retval = curshell; + rv = NS_SUCCESS; + } + + if (hp) + hesiod_free_list(_dns_state.context, hp); + if (rv != NS_SUCCESS) + _dns_state.num = -1; /* any failure halts search */ + return rv; +} + +#endif /* HESIOD */ + + +#ifdef YP + /* + * nis methods + */ + /* state shared between nis methods */ +struct nis_state { + char *domain; /* NIS domain */ + int done; /* non-zero if search exhausted */ + char *current; /* current first/next match */ + int currentlen; /* length of _nis_current */ +}; + +static struct nis_state _nis_state; + +static int +_nis_start(struct nis_state *state) +{ + + _DIAGASSERT(state != NULL); + + state->done = 0; + if (state->current) { + free(state->current); + state->current = NULL; + } + if (state->domain == NULL) { /* setup NIS */ + switch (yp_get_default_domain(&state->domain)) { + case 0: + break; + case YPERR_RESRC: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + return NS_SUCCESS; +} + +static int +_nis_end(struct nis_state *state) +{ + + _DIAGASSERT(state != NULL); + + if (state->domain) + state->domain = NULL; + state->done = 0; + if (state->current) + free(state->current); + state->current = NULL; + return NS_SUCCESS; +} + +/*ARGSUSED*/ +static int +_nis_setusershell(void *nsrv, void *nscb, va_list ap) +{ + + return _nis_start(&_nis_state); +} + +/*ARGSUSED*/ +static int +_nis_endusershell(void *nsrv, void *nscb, va_list ap) +{ + + return _nis_end(&_nis_state); +} + +/*ARGSUSED*/ +static int +_nis_getusershell(void *nsrv, void *nscb, va_list ap) +{ + char **retval = va_arg(ap, char **); + + char *key, *data; + int keylen, datalen, rv, nisr; + + _DIAGASSERT(retval != NULL); + + *retval = NULL; + + if (_nis_state.done) /* exhausted search */ + return NS_NOTFOUND; + if (_nis_state.domain == NULL) { + /* only start if NIS not setup */ + rv = _nis_start(&_nis_state); + if (rv != NS_SUCCESS) + return rv; + } + + key = NULL; + data = NULL; + rv = NS_NOTFOUND; + + if (_nis_state.current) { /* already searching */ + nisr = yp_next(_nis_state.domain, "shells", + _nis_state.current, _nis_state.currentlen, + &key, &keylen, &data, &datalen); + free(_nis_state.current); + _nis_state.current = NULL; + switch (nisr) { + case 0: + _nis_state.current = key; + _nis_state.currentlen = keylen; + key = NULL; + break; + case YPERR_NOMORE: + rv = NS_NOTFOUND; + goto nisent_out; + default: + rv = NS_UNAVAIL; + goto nisent_out; + } + } else { /* new search */ + if (yp_first(_nis_state.domain, "shells", + &_nis_state.current, &_nis_state.currentlen, + &data, &datalen)) { + rv = NS_UNAVAIL; + goto nisent_out; + } + } + + data[datalen] = '\0'; /* clear trailing \n */ + strlcpy(curshell, data, sizeof(curshell)); + *retval = curshell; + rv = NS_SUCCESS; + + nisent_out: + if (key) + free(key); + if (data) + free(data); + if (rv != NS_SUCCESS) /* any failure halts search */ + _nis_state.done = 1; + return rv; +} + +#endif /* YP */ + + + /* + * public functions + */ + +void +endusershell(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_endusershell, NULL) + NS_DNS_CB(_dns_endusershell, NULL) + NS_NIS_CB(_nis_endusershell, NULL) + NS_NULL_CB + }; + + mutex_lock(&__shellmutex); + + curokshell = okshells; /* reset okshells fallback state */ + shellsfound = 0; + + /* force all endusershell() methods */ + (void) nsdispatch(NULL, dtab, NSDB_SHELLS, "endusershell", + __nsdefaultfiles_forceall); + mutex_unlock(&__shellmutex); +} + +__aconst char * +getusershell(void) +{ + int rv; + __aconst char *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getusershell, NULL) + NS_DNS_CB(_dns_getusershell, NULL) + NS_NIS_CB(_nis_getusershell, NULL) + NS_NULL_CB + }; + + mutex_lock(&__shellmutex); + + retval = NULL; + do { + rv = nsdispatch(NULL, dtab, NSDB_SHELLS, "getusershell", + __nsdefaultsrc, &retval); + /* loop until failure or non-blank result */ + } while (rv == NS_SUCCESS && retval[0] == '\0'); + + if (rv == NS_SUCCESS) { + shellsfound++; + } else if (shellsfound == 0) { /* no shells; fall back to okshells */ + if (curokshell != NULL) { + retval = __UNCONST(*curokshell); + curokshell++; + rv = NS_SUCCESS; + } + } + + mutex_unlock(&__shellmutex); + return (rv == NS_SUCCESS) ? retval : NULL; +} + +void +setusershell(void) +{ + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_setusershell, NULL) + NS_DNS_CB(_dns_setusershell, NULL) + NS_NIS_CB(_nis_setusershell, NULL) + NS_NULL_CB + }; + + mutex_lock(&__shellmutex); + + curokshell = okshells; /* reset okshells fallback state */ + shellsfound = 0; + + /* force all setusershell() methods */ + (void) nsdispatch(NULL, dtab, NSDB_SHELLS, "setusershell", + __nsdefaultfiles_forceall); + mutex_unlock(&__shellmutex); +} diff --git a/lib/nbsd_libc/gen/glob.3 b/lib/nbsd_libc/gen/glob.3 new file mode 100644 index 000000000..7d8e47a65 --- /dev/null +++ b/lib/nbsd_libc/gen/glob.3 @@ -0,0 +1,527 @@ +.\" $NetBSD: glob.3,v 1.39 2010/11/30 21:03:07 jruoho Exp $ +.\" +.\" Copyright (c) 1989, 1991, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Guido van Rossum. +.\" 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. +.\" +.\" @(#)glob.3 8.3 (Berkeley) 4/16/94 +.\" +.Dd November 30, 2010 +.Dt GLOB 3 +.Os +.Sh NAME +.Nm glob , +.Nm globfree , +.Nm glob_pattern_p +.Nd generate pathnames matching a pattern +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In glob.h +.Ft int +.Fn glob "const char * restrict pattern" "int flags" "const int (*errfunc)(const char *, int)" "glob_t * restrict pglob" +.Ft void +.Fn globfree "glob_t *pglob" +.Ft int +.Fn glob_pattern_p "const char *pattern" "int quote" +.Sh DESCRIPTION +The +.Fn glob +function +is a pathname generator that implements the rules for file name pattern +matching used by the shell. +.Pp +The include file +.Pa glob.h +defines the structure type +.Fa glob_t , +which contains at least the following fields: +.Bd -literal +typedef struct { + size_t gl_pathc; /* count of total paths so far */ + size_t gl_matchc; /* count of paths matching pattern */ + size_t gl_offs; /* reserved at beginning of gl_pathv */ + int gl_flags; /* returned flags */ + char **gl_pathv; /* list of paths matching pattern */ +} glob_t; +.Ed +.Pp +The argument +.Fa pattern +is a pointer to a pathname pattern to be expanded. +The +.Fn glob +argument +matches all accessible pathnames against the pattern and creates +a list of the pathnames that match. +In order to have access to a pathname, +.Fn glob +requires search permission on every component of a path except the last +and read permission on each directory of any filename component of +.Fa pattern +that contains any of the special characters +.Ql * , +.Ql \&? +or +.Ql \&[ . +.Pp +The +.Fn glob +argument +stores the number of matched pathnames into the +.Fa gl_pathc +field, and a pointer to a list of pointers to pathnames into the +.Fa gl_pathv +field. +The first pointer after the last pathname is +.Dv NULL . +If the pattern does not match any pathnames, the returned number of +matched paths is set to zero. +.Pp +It is the caller's responsibility to create the structure pointed to by +.Fa pglob . +The +.Fn glob +function allocates other space as needed, including the memory pointed +to by +.Fa gl_pathv . +.Pp +The argument +.Fa flags +is used to modify the behavior of +.Fn glob . +The value of +.Fa flags +is the bitwise inclusive +.Tn OR +of any of the following +values defined in +.Pa glob.h : +.Bl -tag -width GLOB_ALTDIRFUNC +.It Dv GLOB_APPEND +Append pathnames generated to the ones from a previous call (or calls) +to +.Fn glob . +The value of +.Fa gl_pathc +will be the total matches found by this call and the previous call(s). +The pathnames are appended to, not merged with the pathnames returned by +the previous call(s). +Between calls, the caller must not change the setting of the +.Dv GLOB_DOOFFS +flag, nor change the value of +.Fa gl_offs +when +.Dv GLOB_DOOFFS +is set, nor (obviously) call +.Fn globfree +for +.Fa pglob . +.It Dv GLOB_DOOFFS +Make use of the +.Fa gl_offs +field. +If this flag is set, +.Fa gl_offs +is used to specify how many +.Dv NULL +pointers to prepend to the beginning +of the +.Fa gl_pathv +field. +In other words, +.Fa gl_pathv +will point to +.Fa gl_offs +.Dv NULL +pointers, +followed by +.Fa gl_pathc +pathname pointers, followed by a +.Dv NULL +pointer. +.It Dv GLOB_ERR +Causes +.Fn glob +to return when it encounters a directory that it cannot open or read. +Ordinarily, +.Fn glob +continues to find matches. +.It Dv GLOB_MARK +Each pathname that is a directory that matches +.Fa pattern +has a slash +appended. +.It Dv GLOB_NOCHECK +If +.Fa pattern +does not match any pathname, then +.Fn glob +returns a list +consisting of only +.Fa pattern , +with the number of total pathnames set to 1, and the number of matched +pathnames set to 0. +.It Dv GLOB_NOSORT +By default, the pathnames are sorted in ascending +.Tn ASCII +order; +this flag prevents that sorting (speeding up +.Fn glob ) . +.El +.Pp +The following values may also be included in +.Fa flags , +however, they are non-standard extensions to +.St -p1003.2 . +.Bl -tag -width GLOB_ALTDIRFUNC +.It Dv GLOB_ALTDIRFUNC +The following additional fields in the pglob structure have been +initialized with alternate functions for glob to use to open, read, +and close directories and to get stat information on names found +in those directories. +.Bd -literal + void *(*gl_opendir)(const char * name); + struct dirent *(*gl_readdir)(void *); + void (*gl_closedir)(void *); + int (*gl_lstat)(const char *name, struct stat *st); + int (*gl_stat)(const char *name, struct stat *st); +.Ed +.Pp +This extension is provided to allow programs such as +.Xr restore 8 +to provide globbing from directories stored on tape. +.It Dv GLOB_BRACE +Pre-process the pattern string to expand +.Ql {pat,pat,...} +strings like +.Xr csh 1 . +The pattern +.Ql {} +is left unexpanded for historical reasons +.Po +.Xr csh 1 +does the same thing to ease typing of +.Xr find 1 +patterns +.Pc . +.It Dv GLOB_MAGCHAR +Set by the +.Fn glob +function if the pattern included globbing characters. +See the description of the usage of the +.Fa gl_matchc +structure member for more details. +.It Dv GLOB_NOMAGIC +Is the same as +.Dv GLOB_NOCHECK +but it only appends the +.Fa pattern +if it does not contain any of the special characters ``*'', ``?'' or ``[''. +.Dv GLOB_NOMAGIC +is provided to simplify implementing the historic +.Xr csh 1 +globbing behavior and should probably not be used anywhere else. +.It Dv GLOB_NOESCAPE +Disable the use of the backslash +.Pq Ql \e +character for quoting. +.It Dv GLOB_TILDE +Expand patterns that start with +.Ql ~ +to user name home directories. +.It Dv GLOB_LIMIT +Limit the amount of memory used to store matched strings to +.Li 64K , +the number of +.Xr stat 2 +calls to 128, and the number of +.Xr readdir 3 +calls to 16K. +This option should be set for programs that can be coerced to a denial of +service attack via patterns that expand to a very large number of matches, +such as a long string of +.Li */../*/.. +.It Dv GLOB_PERIOD +Allow metacharacters to match a leading period in a filename. +.It Dv GLOB_NO_DOTDIRS +Hide +.Sq Li \&. +and +.Sq Li \&.. +from metacharacter matches, regardless of whether +.Dv GLOB_PERIOD +is set and whether the pattern component begins with a literal period. +.Dv GLOB_STAR +Indicates that two adjacent +.Li * +characters will do a recursive match in all subdirs, without following +symbolic links and three adjacent +.Li * +characters will also follow symbolic links. +.El +.Pp +If, during the search, a directory is encountered that cannot be opened +or read and +.Fa errfunc +is +.Pf non- Dv NULL , +.Fn glob +calls +.Fa (*errfunc)(path, errno) . +This may be unintuitive: a pattern like +.Ql */Makefile +will try to +.Xr stat 2 +.Ql foo/Makefile +even if +.Ql foo +is not a directory, resulting in a +call to +.Fa errfunc . +The error routine can suppress this action by testing for +.Dv ENOENT +and +.Dv ENOTDIR ; +however, the +.Dv GLOB_ERR +flag will still cause an immediate +return when this happens. +.Pp +If +.Fa errfunc +returns non-zero, +.Fn glob +stops the scan and returns +.Dv GLOB_ABORTED +after setting +.Fa gl_pathc +and +.Fa gl_pathv +to reflect any paths already matched. +This also happens if an error is encountered and +.Dv GLOB_ERR +is set in +.Fa flags , +regardless of the return value of +.Fa errfunc , +if called. +If +.Dv GLOB_ERR +is not set and either +.Fa errfunc +is +.Dv NULL +or +.Fa errfunc +returns zero, the error is ignored. +.Pp +The +.Fn globfree +function frees any space associated with +.Fa pglob +from a previous call(s) to +.Fn glob . +.Pp +The +.Fn glob_pattern_p +returns +.Dv 1 +if the +.Fa pattern +has any special characters that +.Fn glob +will interpret and +.Dv 0 +otherwise. +If the +.Fa quote +argument is non-zero, then backslash quoted characters are ignored. +.Pp +The historical +.Dv GLOB_QUOTE +flag is no longer supported. +Per +.St -p1003.2-92 , +backslash escaping of special characters is the default behaviour; +it may be disabled by specifying the +.Dv GLOB_NOESCAPE +flag. +.Sh RETURN VALUES +On successful completion, +.Fn glob +returns zero. +In addition the fields of +.Fa pglob +contain the values described below: +.Bl -tag -width GLOB_NOCHECK +.It Fa gl_pathc +contains the total number of matched pathnames so far. +This includes other matches from previous invocations of +.Fn glob +if +.Dv GLOB_APPEND +was specified. +.It Fa gl_matchc +contains the number of matched pathnames in the current invocation of +.Fn glob . +.It Fa gl_flags +contains a copy of the +.Fa flags +parameter with the bit +.Dv GLOB_MAGCHAR +set if +.Fa pattern +contained any of the special characters ``*'', ``?'' or ``['', cleared +if not. +.It Fa gl_pathv +contains a pointer to a +.Dv NULL Ns -terminated +list of matched pathnames. +However, if +.Fa gl_pathc +is zero, the contents of +.Fa gl_pathv +are undefined. +.El +.Pp +If +.Fn glob +terminates due to an error, it sets +.Va errno +and returns one of the following non-zero constants, which are defined +in the include file +.In glob.h : +.Bl -tag -width GLOB_ABORTEDXXX +.It Dv GLOB_ABORTED +The scan was stopped because an error was encountered and either +.Dv GLOB_ERR +was set or +.Fa (*errfunc)() +returned non-zero. +.It Dv GLOB_NOMATCH +The pattern does not match any existing pathname, and +.Dv GLOB_NOCHECK +was not set in +.Dv flags . +.It Dv GLOB_NOSPACE +An attempt to allocate memory failed, or if +.Va errno +was 0 +.Li GLOB_LIMIT +was specified in the flags and +.Li ARG_MAX +patterns were matched. +.El +.Pp +The historical +.Dv GLOB_ABEND +return constant is no longer supported. +Portable applications should use the +.Dv GLOB_ABORTED +constant instead. +.Pp +The arguments +.Fa pglob\-\*[Gt]gl_pathc +and +.Fa pglob\-\*[Gt]gl_pathv +are still set as specified above. +.Sh ENVIRONMENT +.Bl -tag -width HOME -compact +.It Ev HOME +If defined, used as the home directory of the current user in +tilde expansions. +.El +.Sh EXAMPLES +A rough equivalent of +.Ql "ls -l *.c *.h" +can be obtained with the +following code: +.Bd -literal -offset indent +glob_t g; + +g.gl_offs = 2; +glob("*.c", GLOB_DOOFFS, NULL, \*[Am]g); +glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, \*[Am]g); +g.gl_pathv[0] = "ls"; +g.gl_pathv[1] = "-l"; +execvp("ls", g.gl_pathv); +.Ed +.Sh SEE ALSO +.Xr sh 1 , +.Xr fnmatch 3 , +.Xr regexp 3 , +.Xr glob 7 +.Sh STANDARDS +The +.Fn glob +function is expected to be +.St -p1003.2 +compatible with the exception +that the flags +.Dv GLOB_ALTDIRFUNC , +.Dv GLOB_BRACE , +.Dv GLOB_MAGCHAR , +.Dv GLOB_NOMAGIC , +.Dv GLOB_TILDE , +and +.Dv GLOB_LIMIT +and the fields +.Fa gl_matchc +and +.Fa gl_flags +should not be used by applications striving for strict +.Tn POSIX +conformance. +.Sh HISTORY +The +.Fn glob +and +.Fn globfree +functions first appeared in +.Bx 4.4 . +The +.Fn glob_pattern_p +function is modelled after the one found in glibc. +.Sh BUGS +Patterns longer than +.Dv MAXPATHLEN +may cause unchecked errors. +.Pp +The +.Fn glob +function may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr stat 2 , +.Xr closedir 3 , +.Xr opendir 3 , +.Xr readdir 3 , +.Xr malloc 3 , +and +.Xr free 3 . diff --git a/lib/nbsd_libc/gen/glob.c b/lib/nbsd_libc/gen/glob.c new file mode 100644 index 000000000..c1083f90b --- /dev/null +++ b/lib/nbsd_libc/gen/glob.c @@ -0,0 +1,1170 @@ +/* $NetBSD: glob.c,v 1.28 2011/01/21 23:30:31 christos 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 + * Guido van Rossum. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; +#else +__RCSID("$NetBSD: glob.c,v 1.28 2011/01/21 23:30:31 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * glob(3) -- a superset of the one defined in POSIX 1003.2. + * + * The [!...] convention to negate a range is supported (SysV, Posix, ksh). + * + * Optional extra services, controlled by flags not defined by POSIX: + * + * GLOB_MAGCHAR: + * Set in gl_flags if pattern contained a globbing character. + * GLOB_NOMAGIC: + * Same as GLOB_NOCHECK, but it will only append pattern if it did + * not contain any magic characters. [Used in csh style globbing] + * GLOB_ALTDIRFUNC: + * Use alternately specified directory access functions. + * GLOB_TILDE: + * expand ~user/foo to the /home/dir/of/user/foo + * GLOB_BRACE: + * expand {1,2}{a,b} to 1a 1b 2a 2b + * GLOB_PERIOD: + * allow metacharacters to match leading dots in filenames. + * GLOB_NO_DOTDIRS: + * . and .. are hidden from wildcards, even if GLOB_PERIOD is set. + * gl_matchc: + * Number of matches in the current invocation of glob. + */ + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_NBTOOL_CONFIG_H +#define NO_GETPW_R +#endif + +#define GLOB_LIMIT_STRING 65536 /* number of readdirs */ +#define GLOB_LIMIT_STAT 128 /* number of stat system calls */ +#define GLOB_LIMIT_READDIR 16384 /* total buffer size of path strings */ +#define GLOB_LIMIT_PATH 1024 /* number of path elements */ +#define GLOB_LIMIT_BRACE 128 /* Number of brace calls */ + +struct glob_limit { + size_t l_string; + size_t l_stat; + size_t l_readdir; + size_t l_brace; +}; + +/* + * XXX: For NetBSD 1.4.x compatibility. (kill me l8r) + */ +#ifndef _DIAGASSERT +#define _DIAGASSERT(a) +#endif + +#define DOLLAR '$' +#define DOT '.' +#define EOS '\0' +#define LBRACKET '[' +#define NOT '!' +#define QUESTION '?' +#define QUOTE '\\' +#define RANGE '-' +#define RBRACKET ']' +#define SEP '/' +#define STAR '*' +#define TILDE '~' +#define UNDERSCORE '_' +#define LBRACE '{' +#define RBRACE '}' +#define SLASH '/' +#define COMMA ',' + +#ifndef USE_8BIT_CHARS + +#define M_QUOTE 0x8000 +#define M_PROTECT 0x4000 +#define M_MASK 0xffff +#define M_ASCII 0x00ff + +typedef u_short Char; + +#else + +#define M_QUOTE (Char)0x80 +#define M_PROTECT (Char)0x40 +#define M_MASK (Char)0xff +#define M_ASCII (Char)0x7f + +typedef char Char; + +#endif + + +#define CHAR(c) ((Char)((c)&M_ASCII)) +#define META(c) ((Char)((c)|M_QUOTE)) +#define M_ALL META('*') +#define M_END META(']') +#define M_NOT META('!') +#define M_ONE META('?') +#define M_RNG META('-') +#define M_SET META('[') +#define ismeta(c) (((c)&M_QUOTE) != 0) + + +static int compare(const void *, const void *); +static int g_Ctoc(const Char *, char *, size_t); +static int g_lstat(Char *, __gl_stat_t *, glob_t *); +static DIR *g_opendir(Char *, glob_t *); +static Char *g_strchr(const Char *, int); +static int g_stat(Char *, __gl_stat_t *, glob_t *); +static int glob0(const Char *, glob_t *, struct glob_limit *); +static int glob1(Char *, glob_t *, struct glob_limit *); +static int glob2(Char *, Char *, Char *, const Char *, glob_t *, + struct glob_limit *); +static int glob3(Char *, Char *, Char *, const Char *, const Char *, + const Char *, glob_t *, struct glob_limit *); +static int globextend(const Char *, glob_t *, struct glob_limit *); +static const Char *globtilde(const Char *, Char *, size_t, glob_t *); +static int globexp1(const Char *, glob_t *, struct glob_limit *); +static int globexp2(const Char *, const Char *, glob_t *, int *, + struct glob_limit *); +static int match(const Char *, const Char *, const Char *); +#ifdef DEBUG +static void qprintf(const char *, Char *); +#endif + +int +glob(const char *pattern, int flags, int (*errfunc)(const char *, int), + glob_t *pglob) +{ + const u_char *patnext; + int c; + Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; + struct glob_limit limit = { 0, 0, 0, 0 }; + + _DIAGASSERT(pattern != NULL); + + patnext = (const u_char *) pattern; + if (!(flags & GLOB_APPEND)) { + pglob->gl_pathc = 0; + pglob->gl_pathv = NULL; + if (!(flags & GLOB_DOOFFS)) + pglob->gl_offs = 0; + } + pglob->gl_flags = flags & ~GLOB_MAGCHAR; + pglob->gl_errfunc = errfunc; + pglob->gl_matchc = 0; + + bufnext = patbuf; + bufend = bufnext + MAXPATHLEN; + if (flags & GLOB_NOESCAPE) { + while (bufnext < bufend && (c = *patnext++) != EOS) + *bufnext++ = c; + } else { + /* Protect the quoted characters. */ + while (bufnext < bufend && (c = *patnext++) != EOS) + if (c == QUOTE) { + if ((c = *patnext++) == EOS) { + c = QUOTE; + --patnext; + } + *bufnext++ = c | M_PROTECT; + } + else + *bufnext++ = c; + } + *bufnext = EOS; + + if (flags & GLOB_BRACE) + return globexp1(patbuf, pglob, &limit); + else + return glob0(patbuf, pglob, &limit); +} + +/* + * Expand recursively a glob {} pattern. When there is no more expansion + * invoke the standard globbing routine to glob the rest of the magic + * characters + */ +static int +globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit) +{ + const Char* ptr = pattern; + int rv; + + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(pglob != NULL); + + if ((pglob->gl_flags & GLOB_LIMIT) && + limit->l_brace++ >= GLOB_LIMIT_BRACE) { + errno = 0; + return GLOB_NOSPACE; + } + + /* Protect a single {}, for find(1), like csh */ + if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) + return glob0(pattern, pglob, limit); + + while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL) + if (!globexp2(ptr, pattern, pglob, &rv, limit)) + return rv; + + return glob0(pattern, pglob, limit); +} + + +/* + * Recursive brace globbing helper. Tries to expand a single brace. + * If it succeeds then it invokes globexp1 with the new pattern. + * If it fails then it tries to glob the rest of the pattern and returns. + */ +static int +globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, + struct glob_limit *limit) +{ + int i; + Char *lm, *ls; + const Char *pe, *pm, *pl; + Char patbuf[MAXPATHLEN + 1]; + + _DIAGASSERT(ptr != NULL); + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(pglob != NULL); + _DIAGASSERT(rv != NULL); + + /* copy part up to the brace */ + for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) + continue; + ls = lm; + + /* Find the balanced brace */ + for (i = 0, pe = ++ptr; *pe; pe++) + if (*pe == LBRACKET) { + /* Ignore everything between [] */ + for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) + continue; + if (*pe == EOS) { + /* + * We could not find a matching RBRACKET. + * Ignore and just look for RBRACE + */ + pe = pm; + } + } + else if (*pe == LBRACE) + i++; + else if (*pe == RBRACE) { + if (i == 0) + break; + i--; + } + + /* Non matching braces; just glob the pattern */ + if (i != 0 || *pe == EOS) { + /* + * we use `pattern', not `patbuf' here so that that + * unbalanced braces are passed to the match + */ + *rv = glob0(pattern, pglob, limit); + return 0; + } + + for (i = 0, pl = pm = ptr; pm <= pe; pm++) { + switch (*pm) { + case LBRACKET: + /* Ignore everything between [] */ + for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) + continue; + if (*pm == EOS) { + /* + * We could not find a matching RBRACKET. + * Ignore and just look for RBRACE + */ + pm = pl; + } + break; + + case LBRACE: + i++; + break; + + case RBRACE: + if (i) { + i--; + break; + } + /* FALLTHROUGH */ + case COMMA: + if (i && *pm == COMMA) + break; + else { + /* Append the current string */ + for (lm = ls; (pl < pm); *lm++ = *pl++) + continue; + /* + * Append the rest of the pattern after the + * closing brace + */ + for (pl = pe + 1; (*lm++ = *pl++) != EOS;) + continue; + + /* Expand the current pattern */ +#ifdef DEBUG + qprintf("globexp2", patbuf); +#endif + *rv = globexp1(patbuf, pglob, limit); + + /* move after the comma, to the next string */ + pl = pm + 1; + } + break; + + default: + break; + } + } + *rv = 0; + return 0; +} + + + +/* + * expand tilde from the passwd file. + */ +static const Char * +globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob) +{ + struct passwd *pwd; + const char *h; + const Char *p; + Char *b; + char *d; + Char *pend = &patbuf[patsize / sizeof(Char)]; +#ifndef NO_GETPW_R + struct passwd pwres; + char pwbuf[1024]; +#endif + + pend--; + + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(patbuf != NULL); + _DIAGASSERT(pglob != NULL); + + if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) + return pattern; + + /* Copy up to the end of the string or / */ + for (p = pattern + 1, d = (char *)(void *)patbuf; + d < (char *)(void *)pend && *p && *p != SLASH; + *d++ = *p++) + continue; + + if (d == (char *)(void *)pend) + return NULL; + + *d = EOS; + d = (char *)(void *)patbuf; + + if (*d == EOS) { + /* + * handle a plain ~ or ~/ by expanding $HOME + * first and then trying the password file + */ + if ((h = getenv("HOME")) == NULL) { +#ifdef NO_GETPW_R + if ((pwd = getpwuid(getuid())) == NULL) +#else + if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf), + &pwd) != 0 || pwd == NULL) +#endif + return pattern; + else + h = pwd->pw_dir; + } + } + else { + /* + * Expand a ~user + */ +#ifdef NO_GETPW_R + if ((pwd = getpwnam(d)) == NULL) +#else + if (getpwnam_r(d, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 || + pwd == NULL) +#endif + return pattern; + else + h = pwd->pw_dir; + } + + /* Copy the home directory */ + for (b = patbuf; b < pend && *h; *b++ = *h++) + continue; + + if (b == pend) + return NULL; + + /* Append the rest of the pattern */ + while (b < pend && (*b++ = *p++) != EOS) + continue; + + if (b == pend) + return NULL; + + return patbuf; +} + + +/* + * The main glob() routine: compiles the pattern (optionally processing + * quotes), calls glob1() to do the real pattern matching, and finally + * sorts the list (unless unsorted operation is requested). Returns 0 + * if things went well, nonzero if errors occurred. It is not an error + * to find no matches. + */ +static int +glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit) +{ + const Char *qpatnext; + int c, error; + __gl_size_t oldpathc; + Char *bufnext, patbuf[MAXPATHLEN+1]; + + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(pglob != NULL); + + if ((qpatnext = globtilde(pattern, patbuf, sizeof(patbuf), + pglob)) == NULL) + return GLOB_ABEND; + oldpathc = pglob->gl_pathc; + bufnext = patbuf; + + /* We don't need to check for buffer overflow any more. */ + while ((c = *qpatnext++) != EOS) { + switch (c) { + case LBRACKET: + c = *qpatnext; + if (c == NOT) + ++qpatnext; + if (*qpatnext == EOS || + g_strchr(qpatnext+1, RBRACKET) == NULL) { + *bufnext++ = LBRACKET; + if (c == NOT) + --qpatnext; + break; + } + *bufnext++ = M_SET; + if (c == NOT) + *bufnext++ = M_NOT; + c = *qpatnext++; + do { + *bufnext++ = CHAR(c); + if (*qpatnext == RANGE && + (c = qpatnext[1]) != RBRACKET) { + *bufnext++ = M_RNG; + *bufnext++ = CHAR(c); + qpatnext += 2; + } + } while ((c = *qpatnext++) != RBRACKET); + pglob->gl_flags |= GLOB_MAGCHAR; + *bufnext++ = M_END; + break; + case QUESTION: + pglob->gl_flags |= GLOB_MAGCHAR; + *bufnext++ = M_ONE; + break; + case STAR: + pglob->gl_flags |= GLOB_MAGCHAR; + /* collapse adjacent stars to one [or three if globstar] + * to avoid exponential behavior + */ + if (bufnext == patbuf || bufnext[-1] != M_ALL || + ((pglob->gl_flags & GLOB_STAR) != 0 && + (bufnext - 1 == patbuf || bufnext[-2] != M_ALL || + bufnext - 2 == patbuf || bufnext[-3] != M_ALL))) + *bufnext++ = M_ALL; + break; + default: + *bufnext++ = CHAR(c); + break; + } + } + *bufnext = EOS; +#ifdef DEBUG + qprintf("glob0", patbuf); +#endif + + if ((error = glob1(patbuf, pglob, limit)) != 0) + return error; + + if (pglob->gl_pathc == oldpathc) { + /* + * If there was no match we are going to append the pattern + * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was + * specified and the pattern did not contain any magic + * characters GLOB_NOMAGIC is there just for compatibility + * with csh. + */ + if ((pglob->gl_flags & GLOB_NOCHECK) || + ((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR)) + == GLOB_NOMAGIC)) { + return globextend(pattern, pglob, limit); + } else { + return GLOB_NOMATCH; + } + } else if (!(pglob->gl_flags & GLOB_NOSORT)) { + qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, + (size_t)pglob->gl_pathc - oldpathc, sizeof(char *), + compare); + } + + return 0; +} + +static int +compare(const void *p, const void *q) +{ + + _DIAGASSERT(p != NULL); + _DIAGASSERT(q != NULL); + + return strcoll(*(const char * const *)p, *(const char * const *)q); +} + +static int +glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit) +{ + Char pathbuf[MAXPATHLEN+1]; + + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(pglob != NULL); + + /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ + if (*pattern == EOS) + return 0; + /* + * we save one character so that we can use ptr >= limit, + * in the general case when we are appending non nul chars only. + */ + return glob2(pathbuf, pathbuf, + pathbuf + (sizeof(pathbuf) / sizeof(*pathbuf)) - 1, pattern, + pglob, limit); +} + +/* + * The functions glob2 and glob3 are mutually recursive; there is one level + * of recursion for each segment in the pattern that contains one or more + * meta characters. + */ +static int +glob2(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, + glob_t *pglob, struct glob_limit *limit) +{ + __gl_stat_t sb; + const Char *p; + Char *q; + int anymeta; + Char *pend; + ptrdiff_t diff; + + _DIAGASSERT(pathbuf != NULL); + _DIAGASSERT(pathend != NULL); + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(pglob != NULL); + +#ifdef DEBUG + qprintf("glob2", pathbuf); +#endif + /* + * Loop over pattern segments until end of pattern or until + * segment with meta character found. + */ + for (anymeta = 0;;) { + if (*pattern == EOS) { /* End of pattern? */ + *pathend = EOS; + if (g_lstat(pathbuf, &sb, pglob)) + return 0; + + if ((pglob->gl_flags & GLOB_LIMIT) && + limit->l_stat++ >= GLOB_LIMIT_STAT) { + errno = 0; + *pathend++ = SEP; + *pathend = EOS; +printf("stat limit\n"); + return GLOB_NOSPACE; + } + if (((pglob->gl_flags & GLOB_MARK) && + pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) || + (S_ISLNK(sb.st_mode) && + (g_stat(pathbuf, &sb, pglob) == 0) && + S_ISDIR(sb.st_mode)))) { + if (pathend >= pathlim) + return GLOB_ABORTED; + *pathend++ = SEP; + *pathend = EOS; + } + ++pglob->gl_matchc; + return globextend(pathbuf, pglob, limit); + } + + /* Find end of next segment, copy tentatively to pathend. */ + q = pathend; + p = pattern; + while (*p != EOS && *p != SEP) { + if (ismeta(*p)) + anymeta = 1; + if (q >= pathlim) + return GLOB_ABORTED; + *q++ = *p++; + } + + /* + * No expansion, or path ends in slash-dot shash-dot-dot, + * do next segment. + */ + if (pglob->gl_flags & GLOB_PERIOD) { + for (pend = pathend; pend > pathbuf && pend[-1] == '/'; + pend--) + continue; + diff = pend - pathbuf; + } else { + /* XXX: GCC */ + diff = 0; + pend = pathend; + } + + if ((!anymeta) || + ((pglob->gl_flags & GLOB_PERIOD) && + (diff >= 1 && pend[-1] == DOT) && + (diff >= 2 && (pend[-2] == SLASH || pend[-2] == DOT)) && + (diff < 3 || pend[-3] == SLASH))) { + pathend = q; + pattern = p; + while (*pattern == SEP) { + if (pathend >= pathlim) + return GLOB_ABORTED; + *pathend++ = *pattern++; + } + } else /* Need expansion, recurse. */ + return glob3(pathbuf, pathend, pathlim, pattern, p, + pattern, pglob, limit); + } + /* NOTREACHED */ +} + +static int +glob3(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, + const Char *restpattern, const Char *pglobstar, glob_t *pglob, + struct glob_limit *limit) +{ + struct dirent *dp; + DIR *dirp; + __gl_stat_t sbuf; + int error; + char buf[MAXPATHLEN]; + int globstar = 0; + int chase_symlinks = 0; + const Char *termstar = NULL; + + /* + * The readdirfunc declaration can't be prototyped, because it is + * assigned, below, to two functions which are prototyped in glob.h + * and dirent.h as taking pointers to differently typed opaque + * structures. + */ + struct dirent *(*readdirfunc)(void *); + + _DIAGASSERT(pathbuf != NULL); + _DIAGASSERT(pathend != NULL); + _DIAGASSERT(pattern != NULL); + _DIAGASSERT(restpattern != NULL); + _DIAGASSERT(pglob != NULL); + + *pathend = EOS; + errno = 0; + + while (pglobstar < restpattern) { + if ((pglobstar[0] & M_MASK) == M_ALL && + (pglobstar[1] & M_MASK) == M_ALL) { + globstar = 1; + chase_symlinks = (pglobstar[2] & M_MASK) == M_ALL; + termstar = pglobstar + (2 + chase_symlinks); + break; + } + pglobstar++; + } + + if (globstar) { + error = pglobstar == pattern && termstar == restpattern ? + *restpattern == EOS ? + glob2(pathbuf, pathend, pathlim, restpattern - 1, pglob, + limit) : + glob2(pathbuf, pathend, pathlim, restpattern + 1, pglob, + limit) : + glob3(pathbuf, pathend, pathlim, pattern, restpattern, + termstar, pglob, limit); + if (error) + return error; + *pathend = EOS; + } + + if (*pathbuf && (g_lstat(pathbuf, &sbuf, pglob) || + !S_ISDIR(sbuf.st_mode) +#ifdef S_IFLINK + && ((globstar && !chase_symlinks) || !S_ISLNK(sbuf.st_mode)) +#endif + )) + return 0; + + if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { + if (pglob->gl_errfunc) { + if (g_Ctoc(pathbuf, buf, sizeof(buf))) + return GLOB_ABORTED; + if (pglob->gl_errfunc(buf, errno) || + pglob->gl_flags & GLOB_ERR) + return GLOB_ABORTED; + } + /* + * Posix/XOpen: glob should return when it encounters a + * directory that it cannot open or read + * XXX: Should we ignore ENOTDIR and ENOENT though? + * I think that Posix had in mind EPERM... + */ + if (pglob->gl_flags & GLOB_ERR) + return GLOB_ABORTED; + + return 0; + } + + error = 0; + + /* Search directory for matching names. */ + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + readdirfunc = pglob->gl_readdir; + else + readdirfunc = (struct dirent *(*)__P((void *))) readdir; + while ((dp = (*readdirfunc)(dirp)) != NULL) { + u_char *sc; + Char *dc; + + if ((pglob->gl_flags & GLOB_LIMIT) && + limit->l_readdir++ >= GLOB_LIMIT_READDIR) { + errno = 0; + *pathend++ = SEP; + *pathend = EOS; + return GLOB_NOSPACE; + } + + /* + * Initial DOT must be matched literally, unless we have + * GLOB_PERIOD set. + */ + if ((pglob->gl_flags & GLOB_PERIOD) == 0) + if (dp->d_name[0] == DOT && *pattern != DOT) + continue; + /* + * If GLOB_NO_DOTDIRS is set, . and .. vanish. + */ + if ((pglob->gl_flags & GLOB_NO_DOTDIRS) && + (dp->d_name[0] == DOT) && + ((dp->d_name[1] == EOS) || + ((dp->d_name[1] == DOT) && (dp->d_name[2] == EOS)))) + continue; + /* + * The resulting string contains EOS, so we can + * use the pathlim character, if it is the nul + */ + for (sc = (u_char *) dp->d_name, dc = pathend; + dc <= pathlim && (*dc++ = *sc++) != EOS;) + continue; + + /* + * Have we filled the buffer without seeing EOS? + */ + if (dc > pathlim && *pathlim != EOS) { + /* + * Abort when requested by caller, otherwise + * reset pathend back to last SEP and continue + * with next dir entry. + */ + if (pglob->gl_flags & GLOB_ERR) { + error = GLOB_ABORTED; + break; + } + else { + *pathend = EOS; + continue; + } + } + + if (globstar) { +#ifdef S_IFLNK + if (!chase_symlinks && + (g_lstat(pathbuf, &sbuf, pglob) || + S_ISLNK(sbuf.st_mode))) + continue; +#endif + + if (!match(pathend, pattern, termstar)) + continue; + + if (--dc < pathlim - 2) + *dc++ = SEP; + *dc = EOS; + error = glob2(pathbuf, dc, pathlim, pglobstar, + pglob, limit); + if (error) + break; + *pathend = EOS; + } else { + if (!match(pathend, pattern, restpattern)) { + *pathend = EOS; + continue; + } + error = glob2(pathbuf, --dc, pathlim, restpattern, + pglob, limit); + if (error) + break; + } + } + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + (*pglob->gl_closedir)(dirp); + else + closedir(dirp); + + /* + * Again Posix X/Open issue with regards to error handling. + */ + if ((error || errno) && (pglob->gl_flags & GLOB_ERR)) + return GLOB_ABORTED; + + return error; +} + + +/* + * Extend the gl_pathv member of a glob_t structure to accommodate a new item, + * add the new item, and update gl_pathc. + * + * This assumes the BSD realloc, which only copies the block when its size + * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic + * behavior. + * + * Return 0 if new item added, error code if memory couldn't be allocated. + * + * Invariant of the glob_t structure: + * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and + * gl_pathv points to (gl_offs + gl_pathc + 1) items. + */ +static int +globextend(const Char *path, glob_t *pglob, struct glob_limit *limit) +{ + char **pathv; + size_t i, newsize, len; + char *copy; + const Char *p; + + _DIAGASSERT(path != NULL); + _DIAGASSERT(pglob != NULL); + + newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); + if ((pglob->gl_flags & GLOB_LIMIT) && + newsize > GLOB_LIMIT_PATH * sizeof(*pathv)) + goto nospace; + pathv = pglob->gl_pathv ? realloc(pglob->gl_pathv, newsize) : + malloc(newsize); + if (pathv == NULL) + return GLOB_NOSPACE; + + if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { + /* first time around -- clear initial gl_offs items */ + pathv += pglob->gl_offs; + for (i = pglob->gl_offs + 1; --i > 0; ) + *--pathv = NULL; + } + pglob->gl_pathv = pathv; + + for (p = path; *p++;) + continue; + len = (size_t)(p - path); + limit->l_string += len; + if ((copy = malloc(len)) != NULL) { + if (g_Ctoc(path, copy, len)) { + free(copy); + return GLOB_ABORTED; + } + pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; + } + pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; + + if ((pglob->gl_flags & GLOB_LIMIT) && + (newsize + limit->l_string) >= GLOB_LIMIT_STRING) + goto nospace; + + return copy == NULL ? GLOB_NOSPACE : 0; +nospace: + errno = 0; + return GLOB_NOSPACE; +} + + +/* + * pattern matching function for filenames. Each occurrence of the * + * pattern causes a recursion level. + */ +static int +match(const Char *name, const Char *pat, const Char *patend) +{ + int ok, negate_range; + Char c, k; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(pat != NULL); + _DIAGASSERT(patend != NULL); + + while (pat < patend) { + c = *pat++; + switch (c & M_MASK) { + case M_ALL: + while (pat < patend && (*pat & M_MASK) == M_ALL) + pat++; /* eat consecutive '*' */ + if (pat == patend) + return 1; + for (; !match(name, pat, patend); name++) + if (*name == EOS) + return 0; + return 1; + case M_ONE: + if (*name++ == EOS) + return 0; + break; + case M_SET: + ok = 0; + if ((k = *name++) == EOS) + return 0; + if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) + ++pat; + while (((c = *pat++) & M_MASK) != M_END) + if ((*pat & M_MASK) == M_RNG) { + if (c <= k && k <= pat[1]) + ok = 1; + pat += 2; + } else if (c == k) + ok = 1; + if (ok == negate_range) + return 0; + break; + default: + if (*name++ != c) + return 0; + break; + } + } + return *name == EOS; +} + +/* Free allocated data belonging to a glob_t structure. */ +void +globfree(glob_t *pglob) +{ + size_t i; + char **pp; + + _DIAGASSERT(pglob != NULL); + + if (pglob->gl_pathv != NULL) { + pp = pglob->gl_pathv + pglob->gl_offs; + for (i = pglob->gl_pathc; i--; ++pp) + if (*pp) + free(*pp); + free(pglob->gl_pathv); + pglob->gl_pathv = NULL; + pglob->gl_pathc = 0; + } +} + +#ifndef __LIBC12_SOURCE__ +int +glob_pattern_p(const char *pattern, int quote) +{ + int range = 0; + + for (; *pattern; pattern++) + switch (*pattern) { + case QUESTION: + case STAR: + return 1; + + case QUOTE: + if (quote && pattern[1] != EOS) + ++pattern; + break; + + case LBRACKET: + range = 1; + break; + + case RBRACKET: + if (range) + return 1; + break; + default: + break; + } + + return 0; +} +#endif + +static DIR * +g_opendir(Char *str, glob_t *pglob) +{ + char buf[MAXPATHLEN]; + + _DIAGASSERT(str != NULL); + _DIAGASSERT(pglob != NULL); + + if (!*str) + (void)strlcpy(buf, ".", sizeof(buf)); + else { + if (g_Ctoc(str, buf, sizeof(buf))) + return NULL; + } + + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return (*pglob->gl_opendir)(buf); + + return opendir(buf); +} + +static int +g_lstat(Char *fn, __gl_stat_t *sb, glob_t *pglob) +{ + char buf[MAXPATHLEN]; + + _DIAGASSERT(fn != NULL); + _DIAGASSERT(sb != NULL); + _DIAGASSERT(pglob != NULL); + + if (g_Ctoc(fn, buf, sizeof(buf))) + return -1; + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return (*pglob->gl_lstat)(buf, sb); + return lstat(buf, sb); +} + +static int +g_stat(Char *fn, __gl_stat_t *sb, glob_t *pglob) +{ + char buf[MAXPATHLEN]; + + _DIAGASSERT(fn != NULL); + _DIAGASSERT(sb != NULL); + _DIAGASSERT(pglob != NULL); + + if (g_Ctoc(fn, buf, sizeof(buf))) + return -1; + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return (*pglob->gl_stat)(buf, sb); + return stat(buf, sb); +} + +static Char * +g_strchr(const Char *str, int ch) +{ + + _DIAGASSERT(str != NULL); + + do { + if (*str == ch) + return __UNCONST(str); + } while (*str++); + return NULL; +} + +static int +g_Ctoc(const Char *str, char *buf, size_t len) +{ + char *dc; + + _DIAGASSERT(str != NULL); + _DIAGASSERT(buf != NULL); + + if (len == 0) + return 1; + + for (dc = buf; len && (*dc++ = *str++) != EOS; len--) + continue; + + return len == 0; +} + +#ifdef DEBUG +static void +qprintf(const char *str, Char *s) +{ + Char *p; + + _DIAGASSERT(str != NULL); + _DIAGASSERT(s != NULL); + + (void)printf("%s:\n", str); + for (p = s; *p; p++) + (void)printf("%c", CHAR(*p)); + (void)printf("\n"); + for (p = s; *p; p++) + (void)printf("%c", *p & M_PROTECT ? '"' : ' '); + (void)printf("\n"); + for (p = s; *p; p++) + (void)printf("%c", ismeta(*p) ? '_' : ' '); + (void)printf("\n"); +} +#endif diff --git a/lib/nbsd_libc/gen/gr_private.h b/lib/nbsd_libc/gen/gr_private.h new file mode 100644 index 000000000..560ab9265 --- /dev/null +++ b/lib/nbsd_libc/gen/gr_private.h @@ -0,0 +1,112 @@ +/* $NetBSD: gr_private.h,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2004-2005 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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. + */ + +/* + * Structures and functions used by various group(5) public functions + * and their back-end implementations. + * These are subject to change without notice and should not be used + * outside of libc (even by third-party nss_*.so modules implementing + * group(5) back-ends). + */ + +#define _GROUP_COMPAT /* "group" defaults to compat, so always provide it */ + + + /* + * mutex to serialize the public group(5) functions use of the + * back-end implementations, which may not be reentrant. + */ +extern mutex_t __grmutex; + + /* + * files methods + */ +struct __grstate_files { /* state shared between files methods */ + int stayopen; /* see getgroupent(3) */ + FILE *fp; /* groups file handle */ +}; + +extern int __grstart_files(struct __grstate_files *); +extern int __grend_files(struct __grstate_files *); +extern int __grscan_files(int *, struct group *, char *, size_t, + struct __grstate_files *, int, const char *, gid_t); + + /* + * dns methods + */ +struct __grstate_dns { /* state shared between dns methods */ + int stayopen; /* see getgroupent(3) */ + void *context; /* Hesiod context */ + int num; /* group index, -1 if no more */ +}; + +extern int __grstart_dns(struct __grstate_dns *); +extern int __grend_dns(struct __grstate_dns *state); +extern int __grscan_dns(int *, struct group *, char *, size_t, + struct __grstate_dns *, int, const char *, gid_t); + + /* + * nis methods + */ +struct __grstate_nis { /* state shared between nis methods */ + int stayopen; /* see getgroupent(3) */ + char *domain; /* NIS domain */ + int done; /* non-zero if search exhausted */ + char *current; /* current first/next match */ + int currentlen; /* length of _nis_current */ +}; + +extern int __grstart_nis(struct __grstate_nis *); +extern int __grend_nis(struct __grstate_nis *); +extern int __grscan_nis(int *, struct group *, char *, size_t, + struct __grstate_nis *, int, const char *, gid_t); + + /* + * compat methods + */ +struct __grstate_compat { /* state shared between compat methods */ + int stayopen; /* see getgroupent(3) */ + FILE *fp; /* file handle */ +/* + * XXX: convert name to a separate compatstate enum and grow name as necessary + * instead of using strdup & free for each + line + */ + char *name; /* NULL if reading file, */ + /* "" if compat "+", */ + /* name if compat "+name" */ +}; + +extern int __grbad_compat(void *nsrv, void *nscb, va_list ap); +extern int __grstart_compat(struct __grstate_compat *); +extern int __grend_compat(struct __grstate_compat *); +extern int __grscan_compat(int *, struct group *, char *, size_t, + struct __grstate_compat *, int, const char *, gid_t, + int (*)(void *, struct group **), void *); diff --git a/lib/nbsd_libc/gen/humanize_number.3 b/lib/nbsd_libc/gen/humanize_number.3 new file mode 100644 index 000000000..0179636ab --- /dev/null +++ b/lib/nbsd_libc/gen/humanize_number.3 @@ -0,0 +1,168 @@ +.\" $NetBSD: humanize_number.3,v 1.9 2011/01/14 10:08:57 jruoho Exp $ +.\" +.\" Copyright (c) 1999, 2002, 2008 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Luke Mewburn and by Tomas Svensson. +.\" +.\" 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 January 14, 2011 +.Dt HUMANIZE_NUMBER 3 +.Os +.Sh NAME +.Nm dehumanize_number , +.Nm humanize_number +.Nd format a number into a human readable form and viceversa +.Sh SYNOPSIS +.In stdlib.h +.Ft int +.Fn dehumanize_number "const char *str" "int64_t *result" +.Ft int +.Fn humanize_number "char *buf" "size_t len" "int64_t number" "const char *suffix" "int scale" "int flags" +.Sh DESCRIPTION +The +.Fn humanize_number +function formats the signed 64 bit quantity given in +.Fa number +into +.Fa buffer . +A space and then +.Fa suffix +is appended to the end. +.Fa buffer +must be at least +.Fa len +bytes long. +.Pp +If the formatted number (including +.Fa suffix ) +would be too long to fit into +.Fa buffer , +then divide +.Fa number +by 1024 until it will. +In this case, prefix +.Fa suffix +with the appropriate SI designator. +.Pp +The prefixes are: +.Bl -column "Prefix" "Description" "Multiplier" -offset indent +.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier" +.It k kilo 1024 +.It M mega 1048576 +.It G giga 1073741824 +.It T tera 1099511627776 +.It P peta 1125899906842624 +.It E exa 1152921504606846976 +.El +.Pp +.Fa len +must be at least 4 plus the length of +.Fa suffix , +in order to ensure a useful result is generated into +.Fa buffer . +To use a specific prefix, specify this as +.Fa scale +(Multiplier = 1024 ^ scale). +This can not be combined with any of the +.Fa scale +flags below. +.Pp +The following flags may be passed in +.Pa scale : +.Bl -tag -width Dv -offset indent +.It Dv HN_AUTOSCALE +Format the buffer using the lowest multiplier possible. +.It Dv HN_GETSCALE +Return the prefix index number (the number of times +.Fa number +must be divided to fit) instead of formatting it to the buffer. +.El +.Pp +The following flags may be passed in +.Pa flags : +.Bl -tag -width Dv -offset indent +.It Dv HN_DECIMAL +If the final result is less than 10, display it using one digit. +.It Dv HN_NOSPACE +Do not put a space between +.Fa number +and the prefix. +.It Dv HN_B +Use 'B' (bytes) as prefix if the original result does not have a prefix. +.It Dv HN_DIVISOR_1000 +Divide +.Fa number +with 1000 instead of 1024. +.El +.Pp +The +.Fn dehumanize_number +function parses the string representing an integral value given in +.Fa str +and stores the numerical value in the integer pointed to by +.Fa result . +The provided string may hold one of the suffixes, which will be interpreted +and used to scale up its accompanying numerical value. +.Sh RETURN VALUES +.Fn humanize_number +returns the number of characters stored in +.Fa buffer +(excluding the terminating NUL) upon success, or \-1 upon failure. +If +.Dv HN_GETSCALE +is specified, the prefix index number will be returned instead. +.Pp +.Fn dehumanize_number +returns 0 if the string was parsed correctly. +A \-1 is returned to indicate failure and an error code is stored in +.Va errno . +.Sh ERRORS +.Fn dehumanize_number +will fail and no number will be stored in +.Fa result +if: +.Bl -tag -width Er +.It Bq Er EINVAL +The string in +.Fa str +was empty or carried an unknown suffix. +.It Bq Er ERANGE +The string in +.Fa str +represented a number that does not fit in +.Fa result . +.El +.Sh SEE ALSO +.Xr strsuftoll 3 , +.Xr orders 7 , +.Xr humanize_number 9 +.Sh HISTORY +.Fn humanize_number +first appeared in +.Nx 2.0 . +.Pp +.Fn dehumanize_number +first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/gen/humanize_number.c b/lib/nbsd_libc/gen/humanize_number.c new file mode 100644 index 000000000..9166b0677 --- /dev/null +++ b/lib/nbsd_libc/gen/humanize_number.c @@ -0,0 +1,147 @@ +/* $NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $ */ + +/* + * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +int +humanize_number(char *buf, size_t len, int64_t bytes, + const char *suffix, int scale, int flags) +{ + const char *prefixes, *sep; + int b, i, r, maxscale, s1, s2, sign; + int64_t divisor, max; + size_t baselen; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(suffix != NULL); + _DIAGASSERT(scale >= 0); + + if (flags & HN_DIVISOR_1000) { + /* SI for decimal multiplies */ + divisor = 1000; + if (flags & HN_B) + prefixes = "B\0k\0M\0G\0T\0P\0E"; + else + prefixes = "\0\0k\0M\0G\0T\0P\0E"; + } else { + /* + * binary multiplies + * XXX IEC 60027-2 recommends Ki, Mi, Gi... + */ + divisor = 1024; + if (flags & HN_B) + prefixes = "B\0K\0M\0G\0T\0P\0E"; + else + prefixes = "\0\0K\0M\0G\0T\0P\0E"; + } + +#define SCALE2PREFIX(scale) (&prefixes[(scale) << 1]) + maxscale = 7; + + if (scale >= maxscale && + (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0) + return (-1); + + if (buf == NULL || suffix == NULL) + return (-1); + + if (len > 0) + buf[0] = '\0'; + if (bytes < 0) { + sign = -1; + bytes *= -100; + baselen = 3; /* sign, digit, prefix */ + } else { + sign = 1; + bytes *= 100; + baselen = 2; /* digit, prefix */ + } + if (flags & HN_NOSPACE) + sep = ""; + else { + sep = " "; + baselen++; + } + baselen += strlen(suffix); + + /* Check if enough room for `x y' + suffix + `\0' */ + if (len < baselen + 1) + return (-1); + + if (scale & (HN_AUTOSCALE | HN_GETSCALE)) { + /* See if there is additional columns can be used. */ + for (max = 100, i = len - baselen; i-- > 0;) + max *= 10; + + /* + * Divide the number until it fits the given column. + * If there will be an overflow by the rounding below, + * divide once more. + */ + for (i = 0; bytes >= max - 50 && i < maxscale; i++) + bytes /= divisor; + + if (scale & HN_GETSCALE) + return (i); + } else + for (i = 0; i < scale && i < maxscale; i++) + bytes /= divisor; + + /* If a value <= 9.9 after rounding and ... */ + if (bytes < 995 && i > 0 && flags & HN_DECIMAL) { + /* baselen + \0 + .N */ + if (len < baselen + 1 + 2) + return (-1); + b = ((int)bytes + 5) / 10; + s1 = b / 10; + s2 = b % 10; + r = snprintf(buf, len, "%d%s%d%s%s%s", + sign * s1, localeconv()->decimal_point, s2, + sep, SCALE2PREFIX(i), suffix); + } else + r = snprintf(buf, len, "%" PRId64 "%s%s%s", + sign * ((bytes + 50) / 100), + sep, SCALE2PREFIX(i), suffix); + + return (r); +} diff --git a/lib/nbsd_libc/gen/infinity_ieee754.c b/lib/nbsd_libc/gen/infinity_ieee754.c new file mode 100644 index 000000000..56a540c69 --- /dev/null +++ b/lib/nbsd_libc/gen/infinity_ieee754.c @@ -0,0 +1,20 @@ +/* $NetBSD: infinity_ieee754.c,v 1.3 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * IEEE-compatible infinity.c -- public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinity_ieee754.c,v 1.3 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +const union __double_u __infinity = +#if BYTE_ORDER == BIG_ENDIAN + { { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 } }; +#else + { { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f } }; +#endif diff --git a/lib/nbsd_libc/gen/infinityf_ieee754.c b/lib/nbsd_libc/gen/infinityf_ieee754.c new file mode 100644 index 000000000..fedbcf36d --- /dev/null +++ b/lib/nbsd_libc/gen/infinityf_ieee754.c @@ -0,0 +1,20 @@ +/* $NetBSD: infinityf_ieee754.c,v 1.2 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * IEEE-compatible infinityf.c -- public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinityf_ieee754.c,v 1.2 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +const union __float_u __infinityf = +#if BYTE_ORDER == BIG_ENDIAN + { { 0x7f, 0x80, 0, 0 } }; +#else + { { 0, 0, 0x80, 0x7f } }; +#endif diff --git a/lib/nbsd_libc/gen/infinityl_dbl_ieee754.c b/lib/nbsd_libc/gen/infinityl_dbl_ieee754.c new file mode 100644 index 000000000..3b77bdfe8 --- /dev/null +++ b/lib/nbsd_libc/gen/infinityl_dbl_ieee754.c @@ -0,0 +1,21 @@ +/* $NetBSD: infinityl_dbl_ieee754.c,v 1.1 2003/10/25 22:31:20 kleink Exp $ */ + +/* + * IEEE-compatible infinityl.c -- public domain. + * For platforms where long double == double. + */ + +#include +#include +#include + +#if LDBL_MANT_DIG != DBL_MANT_DIG +#error double / long double mismatch +#endif + +const union __long_double_u __infinityl = +#if BYTE_ORDER == BIG_ENDIAN + { { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 } }; +#else + { { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f } }; +#endif diff --git a/lib/nbsd_libc/gen/infinityl_ieee754.c b/lib/nbsd_libc/gen/infinityl_ieee754.c new file mode 100644 index 000000000..7506faac0 --- /dev/null +++ b/lib/nbsd_libc/gen/infinityl_ieee754.c @@ -0,0 +1,39 @@ +/* $NetBSD: infinityl_ieee754.c,v 1.1 2011/01/17 23:53:03 matt Exp $ */ + +/* + * IEEE-compatible infinityl.c for 64-bit or 128-bit long double format. + * This is public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: infinityl_ieee754.c,v 1.1 2011/01/17 23:53:03 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#ifdef __HAVE_LONG_DOUBLE +#define LDBL_EXPBITS EXT_EXPBITS +#define LDBL_EXP_INFNAN EXT_EXP_INFNAN +#else +#define LDBL_EXPBITS DBL_EXPBITS +#define LDBL_EXP_INFNAN DBL_EXP_INFNAN +#endif + +#define EXP_INFNAN (LDBL_EXP_INFNAN << (31 - LDBL_EXPBITS)) + +const union __long_double_u __infinityl = { { +#if BYTE_ORDER == BIG_ENDIAN + [0] = (EXP_INFNAN >> 24) & 0x7f, + [1] = (EXP_INFNAN >> 16) & 0xff, + [2] = (EXP_INFNAN >> 8) & 0xff, + [3] = (EXP_INFNAN >> 0) & 0xff, +#else + [sizeof(long double)-4] = (EXP_INFNAN >> 0) & 0xff, + [sizeof(long double)-3] = (EXP_INFNAN >> 8) & 0xff, + [sizeof(long double)-2] = (EXP_INFNAN >> 16) & 0xff, + [sizeof(long double)-1] = (EXP_INFNAN >> 24) & 0x7f, +#endif +} }; diff --git a/lib/nbsd_libc/gen/initdir.c b/lib/nbsd_libc/gen/initdir.c new file mode 100644 index 000000000..a4b9e1e75 --- /dev/null +++ b/lib/nbsd_libc/gen/initdir.c @@ -0,0 +1,274 @@ +/* $NetBSD: initdir.c,v 1.1 2010/09/26 02:26:59 yamt Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: initdir.c,v 1.1 2010/09/26 02:26:59 yamt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include "extern.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "dirent_private.h" + +#define MAXITERATIONS 100 + +int +_initdir(DIR *dirp, int fd, const char *name) +{ + int flags = dirp->dd_flags; + int pagesz; + int incr; + + /* + * If the machine's page size is an exact multiple of DIRBLKSIZ, + * use a buffer that is cluster boundary aligned. + * Hopefully this can be a big win someday by allowing page trades + * to user space to be done by getdents() + */ + if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0) + incr = pagesz; + else + incr = DIRBLKSIZ; + + if ((flags & DTF_REWIND) && name == NULL) { + return EINVAL; + } + if ((flags & __DTF_READALL) != 0) { + size_t len; + size_t space; + char *buf, *nbuf; + char *ddptr; + char *ddeptr; + int n; + struct dirent **dpv; + int i; + + /* + * The strategy here for directories on top of a union stack + * is to read all the directory entries into a buffer, sort + * the buffer, and remove duplicate entries by setting the + * inode number to zero. + * + * For directories on an NFS mounted filesystem, we try + * to get a consistent snapshot by trying until we have + * successfully read all of the directory without errors + * (i.e. 'bad cookie' errors from the server because + * the directory was modified). These errors should not + * happen often, but need to be dealt with. + */ + i = 0; +retry: + len = 0; + space = 0; + buf = 0; + ddptr = 0; + + do { + /* + * Always make at least DIRBLKSIZ bytes + * available to getdents + */ + if (space < DIRBLKSIZ) { + space += incr; + len += incr; + nbuf = realloc(buf, len); + if (nbuf == NULL) { + dirp->dd_buf = buf; + return errno; + } + buf = nbuf; + ddptr = buf + (len - space); + } + + dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR); + n = getdents(fd, ddptr, space); + /* + * For NFS: EINVAL means a bad cookie error + * from the server. Keep trying to get a + * consistent view, in this case this means + * starting all over again. + */ + if (n == -1 && errno == EINVAL && + (flags & __DTF_RETRY_ON_BADCOOKIE) != 0) { + free(buf); + lseek(fd, (off_t)0, SEEK_SET); + if (++i > MAXITERATIONS) + return EINVAL; + goto retry; + } + if (n > 0) { + ddptr += n; + space -= n; + } + } while (n > 0); + + ddeptr = ddptr; + + /* + * Re-open the directory. + * This has the effect of rewinding back to the + * top of the union stack and is needed by + * programs which plan to fchdir to a descriptor + * which has also been read -- see fts.c. + */ + if (flags & DTF_REWIND) { + (void) close(fd); + if ((fd = open(name, O_RDONLY)) == -1 || + fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { + dirp->dd_buf = buf; + return errno; + } + } + + /* + * There is now a buffer full of (possibly) duplicate + * names. + */ + dirp->dd_buf = buf; + + /* + * Go round this loop twice... + * + * Scan through the buffer, counting entries. + * On the second pass, save pointers to each one. + * Then sort the pointers and remove duplicate names. + */ + if ((flags & DTF_NODUP) != 0) { + for (dpv = 0;;) { + for (n = 0, ddptr = buf; ddptr < ddeptr;) { + struct dirent *dp; + + dp = (struct dirent *)(void *)ddptr; + if ((long)dp & _DIRENT_ALIGN(dp)) + break; + /* + * d_reclen is unsigned, + * so no need to compare <= 0 + */ + if (dp->d_reclen > (ddeptr + 1 - ddptr)) + break; + ddptr += dp->d_reclen; + if (dp->d_fileno) { + if (dpv) + dpv[n] = dp; + n++; + } + } + + if (dpv) { + struct dirent *xp; + + /* + * This sort must be stable. + */ + mergesort(dpv, (size_t)n, sizeof(*dpv), + alphasort); + + dpv[n] = NULL; + xp = NULL; + + /* + * Scan through the buffer in sort + * order, zapping the inode number + * of any duplicate names. + */ + for (n = 0; dpv[n]; n++) { + struct dirent *dp = dpv[n]; + + if ((xp == NULL) || + strcmp(dp->d_name, + xp->d_name)) + xp = dp; + else + dp->d_fileno = 0; + if (dp->d_type == DT_WHT && + (flags & DTF_HIDEW)) + dp->d_fileno = 0; + } + + free(dpv); + break; + } else { + dpv = malloc((n + 1) * + sizeof(struct dirent *)); + if (dpv == NULL) + break; + } + } + } + + dirp->dd_len = len; + dirp->dd_size = ddptr - dirp->dd_buf; + } else { + dirp->dd_len = incr; + dirp->dd_buf = malloc((size_t)dirp->dd_len); + if (dirp->dd_buf == NULL) + return errno; + dirp->dd_seek = 0; + flags &= ~DTF_REWIND; + } + dirp->dd_loc = 0; + dirp->dd_fd = fd; + dirp->dd_flags = flags; + /* + * Set up seek point for rewinddir. + */ + (void)_telldir_unlocked(dirp); + return 0; +} + +void +_finidir(DIR *dirp) +{ + struct dirpos *poslist; + + free(dirp->dd_buf); + + /* free seekdir/telldir storage */ + for (poslist = dirp->dd_internal; poslist; ) { + struct dirpos *nextpos = poslist->dp_next; + free(poslist); + poslist = nextpos; + } + dirp->dd_internal = NULL; +} diff --git a/lib/nbsd_libc/gen/initgroups.3 b/lib/nbsd_libc/gen/initgroups.3 new file mode 100644 index 000000000..56e52a4f1 --- /dev/null +++ b/lib/nbsd_libc/gen/initgroups.3 @@ -0,0 +1,91 @@ +.\" $NetBSD: initgroups.3,v 1.15 2003/08/07 16:42:51 agc Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)initgroups.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd August 10, 2002 +.Dt INITGROUPS 3 +.Os +.Sh NAME +.Nm initgroups +.Nd initialize supplementary group IDs +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn initgroups "const char *name" "gid_t basegid" +.Sh DESCRIPTION +The +.Fn initgroups +function +uses the +.Xr getgrouplist 3 +function to calculate the supplementary group IDs for the user +specified in +.Fa name . +This group list is then set up for the current process using +.Xr setgroups 2 . +The +.Fa basegid +is automatically included in the group list. +Typically this value is given as +the group number from the password file. +.Pp +If the groups database lists more than +.Ev NGROUPS +groups for +.Fa name +(including one for +.Fa basegid ) , +the later groups are ignored. +.Sh RETURN VALUES +The +.Fn initgroups +function +returns \-1 if it was not invoked by the super-user. +.Sh SEE ALSO +.Xr setgroups 2 , +.Xr getgrouplist 3 +.Sh HISTORY +The +.Fn initgroups +function appeared in +.Bx 4.2 . +.Sh BUGS +The +.Fn getgrouplist +function called by +.Fn initgroups +uses the routines based on +.Xr getgrent 3 . +If the invoking program uses any of these routines, +the group structure will +be overwritten in the call to +.Fn initgroups . diff --git a/lib/nbsd_libc/gen/initgroups.c b/lib/nbsd_libc/gen/initgroups.c new file mode 100644 index 000000000..e1c293b00 --- /dev/null +++ b/lib/nbsd_libc/gen/initgroups.c @@ -0,0 +1,84 @@ +/* $NetBSD: initgroups.c,v 1.21 2003/08/07 16:42:51 agc Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)initgroups.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: initgroups.c,v 1.21 2003/08/07 16:42:51 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(initgroups,_initgroups) +#endif + +int +initgroups(uname, agroup) + const char *uname; + gid_t agroup; +{ + gid_t groups_list[NGROUPS]; + int ngroups; + gid_t *groups = groups_list; + int rval; + + _DIAGASSERT(uname != NULL); + + ngroups = NGROUPS; + if (getgrouplist(uname, agroup, groups, &ngroups) == -1) { + int maxgroups = ngroups; + groups = calloc((size_t)maxgroups, sizeof *groups); + if (groups == NULL) + return -1; + if (getgrouplist(uname, agroup, groups, &ngroups) == -1) + ngroups = maxgroups; + } + rval = setgroups(ngroups, groups); + if (rval == -1 && errno == EINVAL) { + int ng = (int)sysconf(_SC_NGROUPS_MAX); + if (ng > 0 && ng < ngroups) + rval = setgroups(ng, groups); + } + if (groups != groups_list) + free(groups); + return rval; +} diff --git a/lib/nbsd_libc/gen/isalnum.3 b/lib/nbsd_libc/gen/isalnum.3 new file mode 100644 index 000000000..53d6ec08f --- /dev/null +++ b/lib/nbsd_libc/gen/isalnum.3 @@ -0,0 +1,97 @@ +.\" $NetBSD: isalnum.3,v 1.15 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isalnum.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISALNUM 3 +.Os +.Sh NAME +.Nm isalnum +.Nd alphanumeric character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isalnum "int c" +.Sh DESCRIPTION +The +.Fn isalnum +function tests for any character for which +.Xr isalpha 3 +or +.Xr isdigit 3 +is true. +.Sh RETURN VALUES +The +.Fn isalnum +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isalnum +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isalnum +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isalpha.3 b/lib/nbsd_libc/gen/isalpha.3 new file mode 100644 index 000000000..51c9022ae --- /dev/null +++ b/lib/nbsd_libc/gen/isalpha.3 @@ -0,0 +1,114 @@ +.\" $NetBSD: isalpha.3,v 1.14 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isalpha.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISALPHA 3 +.Os +.Sh NAME +.Nm isalpha +.Nd alphabetic character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isalpha "int c" +.Sh DESCRIPTION +The +.Fn isalpha +function tests for any character for which +.Xr isupper 3 +or +.Xr islower 3 +is true and +.\" , or any of an implementation-defined set of characters +for which none of +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr ispunct 3 , +or +.Xr isspace 3 +is true. +In the +.Em ``C'' +locale, +.Fn isalpha +returns true only for the characters for which +.Xr isupper 3 +or +.Xr islower 3 +is true. +.Sh RETURN VALUES +The +.Fn isalpha +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isalpha +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isalpha +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isascii.3 b/lib/nbsd_libc/gen/isascii.3 new file mode 100644 index 000000000..ecb5830dc --- /dev/null +++ b/lib/nbsd_libc/gen/isascii.3 @@ -0,0 +1,85 @@ +.\" $NetBSD: isascii.3,v 1.18 2010/04/30 04:46:18 jruoho Exp $ +.\" +.\" Copyright (c) 1989, 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. +.\" +.\" @(#)isascii.3 5.1 (Berkeley) 5/2/91 +.\" +.Dd April 30, 2010 +.Dt ISASCII 3 +.Os +.Sh NAME +.Nm isascii +.Nd test for ASCII character +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isascii "int c" +.Sh DESCRIPTION +The +.Fn isascii +function tests for an +.Tn ASCII +character, which is any character with a value in the +range from 0 to 127, inclusive. +.Pp +The +.Fn isascii +is defined on all integer values. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isascii +function conforms to +.St -xpg4 +and +.St -p1003.1-2001 . +The +.St -p1003.1-2008 +revision however marked it as obsolete, noting that +.Fn isascii +cannot be used portably in a localized application. diff --git a/lib/nbsd_libc/gen/isascii.c b/lib/nbsd_libc/gen/isascii.c new file mode 100644 index 000000000..b441a3298 --- /dev/null +++ b/lib/nbsd_libc/gen/isascii.c @@ -0,0 +1,52 @@ +/* $NetBSD: isascii.c,v 1.3 2010/06/01 13:52:08 tnozaki Exp $ */ + +/* + * Copyright (c) 1989 The Regents of the University of California. + * All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * 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. + * + * from: @(#)isctype.c 5.2 (Berkeley) 6/1/90 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isascii.c,v 1.3 2010/06/01 13:52:08 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define _CTYPE_NOINLINE +#include + +int +isascii(c) + int c; +{ + return ((unsigned)(c) <= 0177); +} diff --git a/lib/nbsd_libc/gen/isatty.c b/lib/nbsd_libc/gen/isatty.c new file mode 100644 index 000000000..2bc3106c7 --- /dev/null +++ b/lib/nbsd_libc/gen/isatty.c @@ -0,0 +1,59 @@ +/* $NetBSD: isatty.c,v 1.12 2003/08/07 16:42:51 agc Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isatty.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isatty.c,v 1.12 2003/08/07 16:42:51 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(isatty,_isatty) +#endif + +int +isatty(fd) + int fd; +{ + struct termios t; + + return(tcgetattr(fd, &t) != -1); +} diff --git a/lib/nbsd_libc/gen/isblank.3 b/lib/nbsd_libc/gen/isblank.3 new file mode 100644 index 000000000..59a5ca167 --- /dev/null +++ b/lib/nbsd_libc/gen/isblank.3 @@ -0,0 +1,102 @@ +.\" $NetBSD: isblank.3,v 1.13 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isspace.3 5.3 (Berkeley) 7/31/91 +.\" +.Dd April 17, 2008 +.Dt ISBLANK 3 +.Os +.Sh NAME +.Nm isblank +.Nd blank-space character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isblank "int c" +.Sh DESCRIPTION +The +.Fn isblank +function tests for the standard blank-space characters. +The standard blank-space characters are the following: +.Pp +.Bl -tag -width xxxxx -offset indent -compact +.It Sq \0 +Space character. +.It Li \et +Horizontal tab. +.El +.Pp +In the +.Em ``C'' +locale, +.Fn isblank +returns true only for the standard blank-space characters. +.Sh RETURN VALUES +The +.Fn isblank +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh CAVEATS +The argument to +.Fn isblank +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/iscntrl.3 b/lib/nbsd_libc/gen/iscntrl.3 new file mode 100644 index 000000000..efea8f8b8 --- /dev/null +++ b/lib/nbsd_libc/gen/iscntrl.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: iscntrl.3,v 1.13 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)iscntrl.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISCNTRL 3 +.Os +.Sh NAME +.Nm iscntrl +.Nd control character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn iscntrl "int c" +.Sh DESCRIPTION +The +.Fn iscntrl +function tests for any control character. +.Sh RETURN VALUES +The +.Fn iscntrl +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn iscntrl +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn iscntrl +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isctype.c b/lib/nbsd_libc/gen/isctype.c new file mode 100644 index 000000000..acab4ebde --- /dev/null +++ b/lib/nbsd_libc/gen/isctype.c @@ -0,0 +1,99 @@ +/* $NetBSD: isctype.c,v 1.21 2010/12/14 02:28:57 joerg Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isctype.c,v 1.21 2010/12/14 02:28:57 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#define _CTYPE_NOINLINE +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#if EOF != -1 +#error "EOF != -1" +#endif + +#include "setlocale_local.h" + +#define _CTYPE_TAB(table, i) ((_current_cache()->table + 1)[i]) + +#define _ISCTYPE_FUNC(name, bit) \ +int \ +is##name(int c) \ +{ \ + return (int)(_CTYPE_TAB(ctype_tab, c) & (bit)); \ +} + +_ISCTYPE_FUNC(alnum, _CTYPE_U|_CTYPE_L|_CTYPE_N ) +_ISCTYPE_FUNC(alpha, _CTYPE_U|_CTYPE_L ) +_ISCTYPE_FUNC(cntrl, _CTYPE_C ) +_ISCTYPE_FUNC(digit, _CTYPE_N ) +_ISCTYPE_FUNC(graph, _CTYPE_P|_CTYPE_U|_CTYPE_L|_CTYPE_N ) +_ISCTYPE_FUNC(lower, _CTYPE_L ) +_ISCTYPE_FUNC(print, _CTYPE_P|_CTYPE_U|_CTYPE_L|_CTYPE_N|_CTYPE_B) +_ISCTYPE_FUNC(punct, _CTYPE_P ) +_ISCTYPE_FUNC(space, _CTYPE_S ) +_ISCTYPE_FUNC(upper, _CTYPE_U ) +_ISCTYPE_FUNC(xdigit, _CTYPE_N|_CTYPE_X ) + +int +isblank(int c) +{ + /* XXX: FIXME */ + return c == ' ' || c == '\t'; +} + +int +toupper(int c) +{ + return (int)_CTYPE_TAB(toupper_tab, c); +} + +int +tolower(int c) +{ + return (int)_CTYPE_TAB(tolower_tab, c); +} + +int +_toupper(int c) +{ + return (c - 'a' + 'A'); +} + +int +_tolower(int c) +{ + return (c - 'A' + 'a'); +} diff --git a/lib/nbsd_libc/gen/isdigit.3 b/lib/nbsd_libc/gen/isdigit.3 new file mode 100644 index 000000000..80e297c37 --- /dev/null +++ b/lib/nbsd_libc/gen/isdigit.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: isdigit.3,v 1.14 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isdigit.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISDIGIT 3 +.Os +.Sh NAME +.Nm isdigit +.Nd decimal-digit character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isdigit "int c" +.Sh DESCRIPTION +The +.Fn isdigit +function tests for any decimal-digit character. +.Sh RETURN VALUES +The +.Fn isdigit +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isdigit +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isdigit +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isfinite.3 b/lib/nbsd_libc/gen/isfinite.3 new file mode 100644 index 000000000..2d159a690 --- /dev/null +++ b/lib/nbsd_libc/gen/isfinite.3 @@ -0,0 +1,77 @@ +.\" $NetBSD: isfinite.3,v 1.3 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2003 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 October 29, 2003 +.Dt ISFINITE 3 +.Os +.Sh NAME +.Nm isfinite +.Nd test for finite value +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In math.h +.Ft int +.Fn isfinite "real-floating x" +.Sh DESCRIPTION +The +.Fn isfinite +determines whether its argument +.Fa x +has a finite value. +An argument represented in a format wider than its semantic type is +converted to its semantic type first. +The determination is then based on the type of the argument. +.Ss IEEE 754 +It is determined whether the value of +.Fa x +is zero, subnormal, or normal, and neither infinite nor NaN. +.Ss VAX +It is determined whether the value of +.Fa x +is true zero or finite, and neither dirty zero nor ROP. +.Sh RETURN VALUES +The +.Fn isfinite +macro returns a non-zero value if the value of +.Fa x +is finite. +Otherwise 0 is returned. +.Sh ERRORS +No errors are defined. +.Sh SEE ALSO +.Xr fpclassify 3 , +.Xr isnormal 3 , +.Xr math 3 , +.Xr signbit 3 +.Sh STANDARDS +The +.Fn isfinite +macro conforms to +.St -isoC-99 . diff --git a/lib/nbsd_libc/gen/isfinited_ieee754.c b/lib/nbsd_libc/gen/isfinited_ieee754.c new file mode 100644 index 000000000..7cdf6eb78 --- /dev/null +++ b/lib/nbsd_libc/gen/isfinited_ieee754.c @@ -0,0 +1,55 @@ +/* $NetBSD: isfinited_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinited_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * IEEE 754 double-precision version + */ +int +__isfinited(double x) +{ + union ieee_double_u u; + + u.dblu_d = x; + + if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/gen/isfinitef_ieee754.c b/lib/nbsd_libc/gen/isfinitef_ieee754.c new file mode 100644 index 000000000..6014fb723 --- /dev/null +++ b/lib/nbsd_libc/gen/isfinitef_ieee754.c @@ -0,0 +1,55 @@ +/* $NetBSD: isfinitef_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinitef_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * IEEE 754 single-precision version + */ +int +__isfinitef(float x) +{ + union ieee_single_u u; + + u.sngu_f = x; + + if (u.sngu_sng.sng_exp == SNG_EXP_INFNAN) + return 0; + + return 1; +} diff --git a/lib/nbsd_libc/gen/isfinitel_ieee754.c b/lib/nbsd_libc/gen/isfinitel_ieee754.c new file mode 100644 index 000000000..d95bfb4c6 --- /dev/null +++ b/lib/nbsd_libc/gen/isfinitel_ieee754.c @@ -0,0 +1,57 @@ +/* $NetBSD: isfinitel_ieee754.c,v 1.1 2011/01/17 23:53:03 matt Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: isfinitel_ieee754.c,v 1.1 2011/01/17 23:53:03 matt Exp $"); +#endif + +#include +#include + +#ifdef __HAVE_LONG_DOUBLE +/* + * 7.12.3.2 isfinite - determine whether an argument has finite value + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__isfinitel(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + if (u.extu_ext.ext_exp == EXT_EXP_INFNAN) + return 0; + + return 1; +} +#endif /* __HAVE_LONG_DOUBLE */ diff --git a/lib/nbsd_libc/gen/isgraph.3 b/lib/nbsd_libc/gen/isgraph.3 new file mode 100644 index 000000000..083e82c4b --- /dev/null +++ b/lib/nbsd_libc/gen/isgraph.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: isgraph.3,v 1.14 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isgraph.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISGRAPH 3 +.Os +.Sh NAME +.Nm isgraph +.Nd printing character test (space character exclusive) +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isgraph "int c" +.Sh DESCRIPTION +The +.Fn isgraph +function tests for any printing character except space ('\ '). +.Sh RETURN VALUES +The +.Fn isgraph +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isgraph +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isgraph +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isgreater.3 b/lib/nbsd_libc/gen/isgreater.3 new file mode 100644 index 000000000..e6fed54a4 --- /dev/null +++ b/lib/nbsd_libc/gen/isgreater.3 @@ -0,0 +1,104 @@ +.\" $NetBSD: isgreater.3,v 1.1 2007/02/22 22:08:20 drochner Exp $ +.\" +.\" Copyright (c) 2003 David Schultz +.\" 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 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. +.\" +.\" FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/isgreater.3,v 1.3 2005/02/06 03:23:31 das Exp +.\" +.Dd February 12, 2003 +.Dt ISGREATER 3 +.Os +.Sh NAME +.Nm isgreater , isgreaterequal , isless , islessequal , +.Nm islessgreater , isunordered +.Nd "compare two floating-point numbers" +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In math.h +.Ft int +.Fn isgreater "real-floating x" "real-floating y" +.Ft int +.Fn isgreaterequal "real-floating x" "real-floating y" +.Ft int +.Fn isless "real-floating x" "real-floating y" +.Ft int +.Fn islessequal "real-floating x" "real-floating y" +.Ft int +.Fn islessgreater "real-floating x" "real-floating y" +.Ft int +.Fn isunordered "real-floating x" "real-floating y" +.Sh DESCRIPTION +Each of the macros +.Fn isgreater , +.Fn isgreaterequal , +.Fn isless , +.Fn islessequal , +and +.Fn islessgreater +take arguments +.Fa x +and +.Fa y +and return a non-zero value if and only if its nominal +relation on +.Fa x +and +.Fa y +is true. +These macros always return zero if either +argument is not a number (NaN), but unlike the corresponding C +operators, they never raise a floating point exception. +.Pp +The +.Fn isunordered +macro takes arguments +.Fa x +and +.Fa y +and returns non-zero if and only if neither +.Fa x +nor +.Fa y +are NaNs. +For any pair of floating-point values, one +of the relationships (less, greater, equal, unordered) holds. +.Sh SEE ALSO +.Xr fpclassify 3 , +.Xr math 3 , +.Xr signbit 3 +.Sh STANDARDS +The +.Fn isgreater , +.Fn isgreaterequal , +.Fn isless , +.Fn islessequal , +.Fn islessgreater , +and +.Fn isunordered +macros conform to +.St -isoC-99 . +.Sh HISTORY +The relational macros described above first appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/gen/isinf.3 b/lib/nbsd_libc/gen/isinf.3 new file mode 100644 index 000000000..713e0ca70 --- /dev/null +++ b/lib/nbsd_libc/gen/isinf.3 @@ -0,0 +1,84 @@ +.\" $NetBSD: isinf.3,v 1.12 2004/03/04 23:47:56 wiz Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)isinf.3 8.2 (Berkeley) 1/29/94 +.\" +.Dd March 5, 2004 +.Dt ISINF 3 +.Os +.Sh NAME +.Nm isinf +.Nd test for infinity +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In math.h +.Ft int +.Fn isinf "real-floating x" +.Sh DESCRIPTION +The +.Fn isinf +macro determines whether its argument +.Fa x +is an infinity (positive or negative). +An argument represented in a format wider than its semantic type is +converted to its semantic type first. +The determination is then based on the type of the argument. +.Ss IEEE 754 +It is determined whether the value of +.Fa x +is an infinity. +.Ss VAX +Infinities are not supported. +.Sh RETURN VALUES +The +.Fn isinf +macro returns a non-zero value if the value of +.Fa x +is an infinity. +Otherwise 0 is returned. +.Sh SEE ALSO +.Xr fpclassify 3 , +.Xr isfinite 3 , +.Xr isinff 3 , +.Xr isnan 3 , +.Xr isnanf 3 , +.Xr isnormal 3 , +.Xr math 3 , +.Xr signbit 3 +.Rs +.%T "IEEE Standard for Binary Floating-Point Arithmetic" +.%Q ANSI +.%R Std 754-1985 +.Re +.Sh STANDARDS +The +.Fn isinf +macro conforms to +.St -isoC-99 . diff --git a/lib/nbsd_libc/gen/isinfd_ieee754.c b/lib/nbsd_libc/gen/isinfd_ieee754.c new file mode 100644 index 000000000..18899b740 --- /dev/null +++ b/lib/nbsd_libc/gen/isinfd_ieee754.c @@ -0,0 +1,68 @@ +/* $NetBSD: isinfd_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinfd_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* libc.so.12 ABI compatbility */ +#ifdef __weak_alias +__weak_alias(isinf,__isinfd) +#endif + +/* + * 7.12.3.3 isinf - test for infinity + * IEEE 754 double-precision version + */ +int +__isinfd(double x) +{ + union ieee_double_u u; + + u.dblu_d = x; + + return (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN && + (u.dblu_dbl.dbl_frach == 0 && u.dblu_dbl.dbl_fracl == 0)); +} diff --git a/lib/nbsd_libc/gen/isinff_ieee754.c b/lib/nbsd_libc/gen/isinff_ieee754.c new file mode 100644 index 000000000..62f4e0f45 --- /dev/null +++ b/lib/nbsd_libc/gen/isinff_ieee754.c @@ -0,0 +1,63 @@ +/* $NetBSD: isinff_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinff_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.3 isinf - test for infinity + * IEEE 754 single-precision version + */ +int +__isinff(float x) +{ + union ieee_single_u u; + + u.sngu_f = x; + + return (u.sngu_sng.sng_exp == SNG_EXP_INFNAN && + u.sngu_sng.sng_frac == 0); +} diff --git a/lib/nbsd_libc/gen/isinfl_ieee754.c b/lib/nbsd_libc/gen/isinfl_ieee754.c new file mode 100644 index 000000000..9c99d3d2f --- /dev/null +++ b/lib/nbsd_libc/gen/isinfl_ieee754.c @@ -0,0 +1,66 @@ +/* $NetBSD: isinfl_ieee754.c,v 1.4 2011/01/17 23:53:03 matt Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isinfl_ieee754.c,v 1.4 2011/01/17 23:53:03 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#ifdef __HAVE_LONG_DOUBLE +/* + * 7.12.3.3 isinf - test for infinity + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__isinfl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return u.extu_ext.ext_exp == EXT_EXP_INFNAN + && u.extu_ext.ext_frach == 0 && u.extu_ext.ext_frachm == 0 + && u.extu_ext.ext_fraclm == 0 && u.extu_ext.ext_fracl == 0; +} +#endif /* __HAVE_LONG_DOUBLE */ diff --git a/lib/nbsd_libc/gen/islower.3 b/lib/nbsd_libc/gen/islower.3 new file mode 100644 index 000000000..1322e36e0 --- /dev/null +++ b/lib/nbsd_libc/gen/islower.3 @@ -0,0 +1,107 @@ +.\" $NetBSD: islower.3,v 1.14 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)islower.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISLOWER 3 +.Os +.Sh NAME +.Nm islower +.Nd lower-case character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn islower "int c" +.Sh DESCRIPTION +The +.Fn islower +function tests for any lower-case letter +.\" or any of an +.\" implementation-defined set of characters +for which none of +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr ispunct 3 , +or +.Xr isspace 3 +is true. +In the +.Em ``C'' +locale, +.Fn islower +returns true only for the characters defined as lower-case letters. +.Sh RETURN VALUES +The +.Fn islower +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn islower +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn islower +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isnan.3 b/lib/nbsd_libc/gen/isnan.3 new file mode 100644 index 000000000..42442147c --- /dev/null +++ b/lib/nbsd_libc/gen/isnan.3 @@ -0,0 +1,86 @@ +.\" $NetBSD: isnan.3,v 1.2 2004/03/04 23:49:31 wiz Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" From: @(#)isinf.3 8.2 (Berkeley) 1/29/94 +.\" from: NetBSD: isinf.3,v 1.10 2003/08/07 16:42:52 agc Exp +.\" +.Dd March 5, 2004 +.Dt ISNAN 3 +.Os +.Sh NAME +.Nm isnan +.Nd test for not-a-number +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In math.h +.Ft int +.Fn isnan "real-floating x" +.Sh DESCRIPTION +The +.Fn isnan +macro determines whether its argument +.Fa x +is not-a-number +.Pq Dq NaN . +An argument represented in a format wider than its semantic type is +converted to its semantic type first. +The determination is then based on the type of the argument. +.Ss IEEE 754 +It is determined whether the value of +.Fa x +is a NaN. +.Ss VAX +NaNs are not supported. +.Sh RETURN VALUES +The +.Fn isnan +macro returns a non-zero value if the value of +.Fa x +is a NaN. +Otherwise 0 is returned. +.Sh SEE ALSO +.Xr fpclassify 3 , +.Xr isfinite 3 , +.Xr isinf 3 , +.Xr isinff 3 , +.Xr isnanf 3 , +.Xr isnormal 3 , +.Xr math 3 , +.Xr signbit 3 +.Rs +.%T "IEEE Standard for Binary Floating-Point Arithmetic" +.%Q ANSI +.%R Std 754-1985 +.Re +.Sh STANDARDS +The +.Fn isnan +macro conforms to +.St -isoC-99 . diff --git a/lib/nbsd_libc/gen/isnand_ieee754.c b/lib/nbsd_libc/gen/isnand_ieee754.c new file mode 100644 index 000000000..0ce4bd936 --- /dev/null +++ b/lib/nbsd_libc/gen/isnand_ieee754.c @@ -0,0 +1,68 @@ +/* $NetBSD: isnand_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnand_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* libc.so.12 ABI compatbility */ +#ifdef __weak_alias +__weak_alias(isnan,__isnand) +#endif + +/* + * 7.12.3.4 isnan - test for a NaN + * IEEE 754 double-precision version + */ +int +__isnand(double x) +{ + union ieee_double_u u; + + u.dblu_d = x; + + return (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN && + (u.dblu_dbl.dbl_frach != 0 || u.dblu_dbl.dbl_fracl != 0)); +} diff --git a/lib/nbsd_libc/gen/isnanf_ieee754.c b/lib/nbsd_libc/gen/isnanf_ieee754.c new file mode 100644 index 000000000..ee3b94dec --- /dev/null +++ b/lib/nbsd_libc/gen/isnanf_ieee754.c @@ -0,0 +1,63 @@ +/* $NetBSD: isnanf_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnanf_ieee754.c,v 1.1 2004/03/04 23:42:39 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * 7.12.3.4 isnan - test for a NaN + * IEEE 754 single-precision version + */ +int +__isnanf(float x) +{ + union ieee_single_u u; + + u.sngu_f = x; + + return (u.sngu_sng.sng_exp == SNG_EXP_INFNAN && + u.sngu_sng.sng_frac != 0); +} diff --git a/lib/nbsd_libc/gen/isnanl_ieee754.c b/lib/nbsd_libc/gen/isnanl_ieee754.c new file mode 100644 index 000000000..d74a54b2a --- /dev/null +++ b/lib/nbsd_libc/gen/isnanl_ieee754.c @@ -0,0 +1,66 @@ +/* $NetBSD: isnanl_ieee754.c,v 1.6 2011/01/17 23:53:03 matt Exp $ */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + * + * from: Header: isinf.c,v 1.1 91/07/08 19:03:34 torek Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)isinf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: isnanl_ieee754.c,v 1.6 2011/01/17 23:53:03 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#ifdef __HAVE_LONG_DOUBLE +/* + * 7.12.3.4 isnan - test for a NaN + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__isnanl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return u.extu_ext.ext_exp == EXT_EXP_INFNAN + && (u.extu_ext.ext_frach != 0 || u.extu_ext.ext_frachm != 0 + || u.extu_ext.ext_fraclm != 0 || u.extu_ext.ext_fracl != 0); +} +#endif /* __HAVE_LONG_DOUBLE */ diff --git a/lib/nbsd_libc/gen/isnormal.3 b/lib/nbsd_libc/gen/isnormal.3 new file mode 100644 index 000000000..7ccda97fc --- /dev/null +++ b/lib/nbsd_libc/gen/isnormal.3 @@ -0,0 +1,77 @@ +.\" $NetBSD: isnormal.3,v 1.3 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2003 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 October 29, 2003 +.Dt ISNORMAL 3 +.Os +.Sh NAME +.Nm isnormal +.Nd test for normal value +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In math.h +.Ft int +.Fn isnormal "real-floating x" +.Sh DESCRIPTION +The +.Fn isnormal +macro determines whether its argument +.Fa x +has a normal value. +An argument represented in a format wider than its semantic type is +converted to its semantic type first. +The determination is then based on the type of the argument. +.Ss IEEE 754 +It is determined whether the value of +.Fa x +is normal, and neither zero, subnormal, infinite nor NaN. +.Ss VAX +It is determined whether the value of +.Fa x +is finite, and neither true zero, dirty zero nor ROP. +.Sh RETURN VALUES +The +.Fn isnormal +macro returns a non-zero value if the value of +.Fa x +is finite. +Otherwise 0 is returned. +.Sh ERRORS +No errors are defined. +.Sh SEE ALSO +.Xr fpclassify 3 , +.Xr isfinite 3 , +.Xr math 3 , +.Xr signbit 3 +.Sh STANDARDS +The +.Fn isnormal +macro conforms to +.St -isoC-99 . diff --git a/lib/nbsd_libc/gen/isprint.3 b/lib/nbsd_libc/gen/isprint.3 new file mode 100644 index 000000000..e2091097c --- /dev/null +++ b/lib/nbsd_libc/gen/isprint.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: isprint.3,v 1.13 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isprint.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISPRINT 3 +.Os +.Sh NAME +.Nm isprint +.Nd printing character test (space character inclusive) +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isprint "int c" +.Sh DESCRIPTION +The +.Fn isprint +function tests for any printing character including space (' '). +.Sh RETURN VALUES +The +.Fn isprint +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isprint +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isprint +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/ispunct.3 b/lib/nbsd_libc/gen/ispunct.3 new file mode 100644 index 000000000..8d32ac67e --- /dev/null +++ b/lib/nbsd_libc/gen/ispunct.3 @@ -0,0 +1,96 @@ +.\" $NetBSD: ispunct.3,v 1.13 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)ispunct.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISPUNCT 3 +.Os +.Sh NAME +.Nm ispunct +.Nd punctuation character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn ispunct "int c" +.Sh DESCRIPTION +The +.Fn ispunct +function tests for any printing character except space (' ') or a +character for which +.Xr isalnum 3 +is true. +.Sh RETURN VALUES +The +.Fn ispunct +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn ispunct +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn ispunct +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isspace.3 b/lib/nbsd_libc/gen/isspace.3 new file mode 100644 index 000000000..bb1dcbbcb --- /dev/null +++ b/lib/nbsd_libc/gen/isspace.3 @@ -0,0 +1,120 @@ +.\" $NetBSD: isspace.3,v 1.14 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isspace.3 5.3 (Berkeley) 7/31/91 +.\" +.Dd April 17, 2008 +.Dt ISSPACE 3 +.Os +.Sh NAME +.Nm isspace +.Nd white-space character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isspace "int c" +.Sh DESCRIPTION +The +.Fn isspace +function tests for the standard white-space characters +.\" or for any +.\" of an implementation-defined set of characters +for which +.Xr isalnum 3 +is false. +The standard white-space characters are the following: +.Pp +.Bl -tag -width xxxxx -offset indent -compact +.It Sq \0 +Space character. +.It Li \ef +Form feed. +.It Li \en +New-line. +.It Li \er +Carriage return. +.It Li \et +Horizontal tab. +.It Li \ev +And vertical tab. +.El +.Pp +In the +.Em ``C'' +locale, +.Fn isspace +returns true only for the standard white-space characters. +.Sh RETURN VALUES +The +.Fn isspace +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isspace +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isspace +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isupper.3 b/lib/nbsd_libc/gen/isupper.3 new file mode 100644 index 000000000..d969eca56 --- /dev/null +++ b/lib/nbsd_libc/gen/isupper.3 @@ -0,0 +1,105 @@ +.\" $NetBSD: isupper.3,v 1.14 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isupper.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISUPPER 3 +.Os +.Sh NAME +.Nm isupper +.Nd upper-case character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isupper "int c" +.Sh DESCRIPTION +The +.Fn isupper +function tests for any upper-case letter or any of an +implementation-defined set of characters for which none of +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr ispunct 3 , +or +.Xr isspace 3 +is true. +In the +.Em ``C'' +locale, +.Fn isupper +returns true only for the characters defined as upper-case letters. +.Sh RETURN VALUES +The +.Fn isupper +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isupper +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isupper +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/isxdigit.3 b/lib/nbsd_libc/gen/isxdigit.3 new file mode 100644 index 000000000..7777af032 --- /dev/null +++ b/lib/nbsd_libc/gen/isxdigit.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: isxdigit.3,v 1.13 2008/04/17 16:25:36 apb Exp $ +.\" +.\" 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. +.\" +.\" @(#)isxdigit.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt ISXDIGIT 3 +.Os +.Sh NAME +.Nm isxdigit +.Nd hexadecimal-digit character test +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn isxdigit "int c" +.Sh DESCRIPTION +The +.Fn isxdigit +function tests for any hexadecimal-digit character. +.Sh RETURN VALUES +The +.Fn isxdigit +function returns zero if the character tests false and +returns non-zero if the character tests true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn isxdigit +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn isxdigit +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/ldexp_ieee754.c b/lib/nbsd_libc/gen/ldexp_ieee754.c new file mode 100644 index 000000000..cc8149a6a --- /dev/null +++ b/lib/nbsd_libc/gen/ldexp_ieee754.c @@ -0,0 +1,142 @@ +/* $NetBSD: ldexp_ieee754.c,v 1.5 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles M. Hannum. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ldexp_ieee754.c,v 1.5 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +/* + * Multiply the given value by 2^expon. + */ +double +ldexp(double val, int expon) +{ + int oldexp, newexp; + union ieee_double_u u, mul; + + u.dblu_d = val; + oldexp = u.dblu_dbl.dbl_exp; + + /* + * If input is zero, Inf or NaN, just return it. + */ + if (u.dblu_d == 0.0 || oldexp == DBL_EXP_INFNAN) + return (val); + + if (oldexp == 0) { + /* + * u.v is denormal. We must adjust it so that the exponent + * arithmetic below will work. + */ + if (expon <= DBL_EXP_BIAS) { + /* + * Optimization: if the scaling can be done in a single + * multiply, or underflows, just do it now. + */ + if (expon <= -DBL_FRACBITS) { + errno = ERANGE; + return (val < 0.0 ? -0.0 : 0.0); + } + mul.dblu_d = 0.0; + mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS; + u.dblu_d *= mul.dblu_d; + if (u.dblu_d == 0.0) { + errno = ERANGE; + return (val < 0.0 ? -0.0 : 0.0); + } + return (u.dblu_d); + } else { + /* + * We know that expon is very large, and therefore the + * result cannot be denormal (though it may be Inf). + * Shift u.v by just enough to make it normal. + */ + mul.dblu_d = 0.0; + mul.dblu_dbl.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS; + u.dblu_d *= mul.dblu_d; + expon -= DBL_FRACBITS; + oldexp = u.dblu_dbl.dbl_exp; + } + } + + /* + * u.v is now normalized and oldexp has been adjusted if necessary. + * Calculate the new exponent and check for underflow and overflow. + */ + newexp = oldexp + expon; + + if (newexp <= 0) { + /* + * The output number is either denormal or underflows (see + * comments in machine/ieee.h). + */ + if (newexp <= -DBL_FRACBITS) { + errno = ERANGE; + return (val < 0.0 ? -0.0 : 0.0); + } + /* + * Denormalize the result. We do this with a multiply. If + * expon is very large, it won't fit in a double, so we have + * to adjust the exponent first. This is safe because we know + * that u.v is normal at this point. + */ + if (expon <= -DBL_EXP_BIAS) { + u.dblu_dbl.dbl_exp = 1; + expon += oldexp - 1; + } + mul.dblu_d = 0.0; + mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS; + u.dblu_d *= mul.dblu_d; + return (u.dblu_d); + } else if (newexp >= DBL_EXP_INFNAN) { + /* + * The result overflowed; return +/-Inf. + */ + u.dblu_dbl.dbl_exp = DBL_EXP_INFNAN; + u.dblu_dbl.dbl_frach = 0; + u.dblu_dbl.dbl_fracl = 0; + errno = ERANGE; + return (u.dblu_d); + } else { + /* + * The result is normal; just replace the old exponent with the + * new one. + */ + u.dblu_dbl.dbl_exp = newexp; + return (u.dblu_d); + } +} diff --git a/lib/nbsd_libc/gen/lockf.3 b/lib/nbsd_libc/gen/lockf.3 new file mode 100644 index 000000000..293b8ace8 --- /dev/null +++ b/lib/nbsd_libc/gen/lockf.3 @@ -0,0 +1,253 @@ +.\" $NetBSD: lockf.3,v 1.10 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 1997 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Klaus Klein and S.P. Zeidler. +.\" +.\" 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 December 19, 1997 +.Dt LOCKF 3 +.Os +.Sh NAME +.Nm lockf +.Nd record locking on files +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn lockf "int filedes" "int function" "off_t size" +.Sh DESCRIPTION +The +.Fn lockf +function allows sections of a file to be locked with advisory-mode locks. +Calls to +.Fn lockf +from other processes which attempt to lock the locked file section will +either return an error value or block until the section becomes unlocked. +All the locks for a process are removed when the process terminates. +.Pp +The argument +.Fa filedes +is an open file descriptor. +The file descriptor must have been opened either for write-only +.Dv ( O_WRONLY ) +or read/write +.Dv ( O_RDWR ) +operation. +.Pp +The +.Fa function +argument is a control value which specifies the action to be taken. +The permissible values for +.Fa function +are as follows: +.Bl -tag -width F_ULOCKXX -compact -offset indent +.It Sy Function +.Sy Description +.It Dv F_ULOCK +unlock locked sections +.It Dv F_LOCK +lock a section for exclusive use +.It Dv F_TLOCK +test and lock a section for exclusive use +.It Dv F_TEST +test a section for locks by other processes +.El +.Pp +.Dv F_ULOCK +removes locks from a section of the file; +.Dv F_LOCK +and +.Dv F_TLOCK +both lock a section of a file if the section is available; +.Dv F_TEST +detects if a lock by another process is present on the specified section. +.Pp +The +.Fa size +argument is the number of contiguous bytes to be locked or +unlocked. +The section to be locked or unlocked starts at the current +offset in the file and extends forward for a positive size or backward +for a negative size (the preceding bytes up to but not including the +current offset). +However, it is not permitted to lock a section that +starts or extends before the beginning of the file. +If +.Fa size +is 0, the section from the current offset through the largest possible +file offset is locked (that is, from the current offset through the +present or any future end-of-file). +.Pp +The sections locked with +.Dv F_LOCK +or +.Dv F_TLOCK +may, in whole or in part, contain or be contained by a previously +locked section for the same process. +When this occurs, or if adjacent +locked sections would occur, the sections are combined into a single +locked section. +If the request would cause the number of locks to +exceed a system-imposed limit, the request will fail. +.Pp +.Dv F_LOCK +and +.Dv F_TLOCK +requests differ only by the action taken if the section is not +available. +.Dv F_LOCK +blocks the calling process until the section is available. +.Dv F_TLOCK +makes the function fail if the section is already locked by another +process. +.Pp +File locks are released on first close by the locking process of any +file descriptor for the file. +.Pp +.Dv F_ULOCK +requests release (wholly or in part) one or more locked sections +controlled by the process. +Locked sections will be unlocked starting +at the current file offset through +.Fa size +bytes or to the end of file if size is 0. +When all of a locked section +is not released (that is, when the beginning or end of the area to be +unlocked falls within a locked section), the remaining portions of +that section are still locked by the process. +Releasing the center +portion of a locked section will cause the remaining locked beginning +and end portions to become two separate locked sections. +If the +request would cause the number of locks in the system to exceed a +system-imposed limit, the request will fail. +.Pp +An +.Dv F_ULOCK +request in which size is non-zero and the offset of the last byte of +the requested section is the maximum value for an object of type +off_t, when the process has an existing lock in which size is 0 and +which includes the last byte of the requested section, will be treated +as a request to unlock from the start of the requested section with a +size equal to 0. +Otherwise an +.Dv F_ULOCK +request will attempt to unlock only the requested section. +.Pp +A potential for deadlock occurs if a process controlling a locked +region is put to sleep by attempting to lock the locked region of +another process. +This implementation detects that sleeping until a +locked region is unlocked would cause a deadlock and fails with an +.Er EDEADLK +error. +.Pp +.Fn lockf , +.Xr fcntl 2 +and +.Xr flock 2 +locks may be safely used concurrently. +.Pp +Blocking on a section is interrupted by any signal. +.Sh RETURN VALUES +If successful, the +.Fn lockf +function returns 0. +Otherwise, it returns \-1, sets +.Va errno +to indicate an error, and existing locks are not changed. +.Sh ERRORS +.Fn lockf +will fail if: +.Bl -tag -width Er +.It Bq Er EAGAIN +The argument +.Fa function +is +.Dv F_TLOCK +or +.Dv F_TEST +and the section is already locked by another process. +.It Bq Er EBADF +The argument +.Fa filedes +is not a valid open file descriptor. +.Pp +The argument +.Fa function +is +.Dv F_LOCK +or +.Dv F_TLOCK , +and +.Fa filedes +is not a valid file descriptor open for writing. +.It Bq Er EDEADLK +The argument +.Fa function +is +.Dv F_LOCK +and a deadlock is detected. +.It Bq Er EINTR +The argument +.Fa function +is F_LOCK +and +.Fn lockf +was interrupted by the delivery of a signal. +.It Bq Er EINVAL +The argument +.Fa function +is not one of +.Dv F_ULOCK , +.Dv F_LOCK , +.Dv F_TLOCK +or +.Dv F_TEST . +.Pp +The argument +.Fa filedes +refers to a file that does not support locking. +.It Bq Er ENOLCK +The argument +.Fa function +is +.Dv F_ULOCK , +.Dv F_LOCK +or +.Dv F_TLOCK , +and satisfying the lock or unlock request would result in the number +of locked regions in the system exceeding a system-imposed limit. +.El +.Sh SEE ALSO +.Xr fcntl 2 , +.Xr flock 2 +.Sh STANDARDS +The +.Fn lockf +function conforms to +.St -xpg4.2 . diff --git a/lib/nbsd_libc/gen/lockf.c b/lib/nbsd_libc/gen/lockf.c new file mode 100644 index 000000000..c892e9f5d --- /dev/null +++ b/lib/nbsd_libc/gen/lockf.c @@ -0,0 +1,89 @@ +/* $NetBSD: lockf.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1997 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: lockf.c,v 1.3 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(lockf,_lockf) +#endif + + +int +lockf(filedes, function, size) + int filedes; + int function; + off_t size; +{ + struct flock fl; + int cmd; + + fl.l_start = 0; + fl.l_len = size; + fl.l_whence = SEEK_CUR; + + switch (function) { + case F_ULOCK: + cmd = F_SETLK; + fl.l_type = F_UNLCK; + break; + case F_LOCK: + cmd = F_SETLKW; + fl.l_type = F_WRLCK; + break; + case F_TLOCK: + cmd = F_SETLK; + fl.l_type = F_WRLCK; + break; + case F_TEST: + fl.l_type = F_WRLCK; + if (fcntl(filedes, F_GETLK, &fl) == -1) + return (-1); + if (fl.l_type == F_UNLCK || fl.l_pid == getpid()) + return (0); + errno = EAGAIN; + return (-1); + /* NOTREACHED */ + default: + errno = EINVAL; + return (-1); + /* NOTREACHED */ + } + + return (fcntl(filedes, cmd, &fl)); +} diff --git a/lib/nbsd_libc/gen/makecontext.3 b/lib/nbsd_libc/gen/makecontext.3 new file mode 100644 index 000000000..00b798c8a --- /dev/null +++ b/lib/nbsd_libc/gen/makecontext.3 @@ -0,0 +1,175 @@ +.\" $NetBSD: makecontext.3,v 1.9 2010/04/29 06:07:35 jruoho Exp $ +.\" +.\" Copyright (c) 2001, 2009 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 April 29, 2010 +.Dt MAKECONTEXT 3 +.Os +.Sh NAME +.Nm makecontext , +.Nm swapcontext +.Nd manipulate user contexts +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ucontext.h +.Ft void +.Fn makecontext "ucontext_t *ucp" "void (*func)()" "int argc" ... +.Ft int +.Fn swapcontext "ucontext_t * restrict oucp" "ucontext_t * restrict ucp" +.Sh DESCRIPTION +The +.Fn makecontext +function modifies the object pointed to by +.Fa ucp , +which has been initialized using +.Xr getcontext 2 . +When this context is resumed using +.Fn swapcontext +or +.Xr setcontext 2 , +program execution continues as if +.Fa func +had been called with the arguments specified after +.Fa argc +in the call of +.Fn makecontext . +The value of +.Fa argc +must be equal to the number of integer arguments following it, +and must be equal to the number of integer arguments expected by +.Fa func ; +otherwise, the behavior is undefined. +.Pp +Before being modified using +.Fn makecontext , +a stack must be allocated for the context (in the +.Fa uc_stack +member), and a context to resume after +.Fa func +has returned must be determined (pointed to by the +.Fa uc_link +member); +otherwise, the behavior is undefined. +If +.Fa uc_link +is a null pointer, then the context is the main context, +and the process will exit with an exit status of 0 upon return. +.Pp +The +.Fn swapcontext +function saves the current context in the object pointed to by +.Fa oucp , +sets the current context to that specified in the object pointed to by +.Fa ucp , +and resumes execution. +When a context saved by +.Fn swapcontext +is restored using +.Xr setcontext 2 , +execution will resume as if the corresponding invocation of +.Fn swapcontext +had just returned (successfully). +.Sh RETURN VALUES +The +.Fn makecontext +function returns no value. +.Pp +On success, +.Fn swapcontext +returns a value of 0, +Otherwise, \-1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn swapcontext +function will fail if: +.Bl -tag -width Er +.It Bq Er EFAULT +The +.Fa oucp +argument or the +.Fa ucp +argument points to an invalid address. +.It Bq Er EINVAL +The contents of the datum pointed to by +.Fa ucp +are invalid. +.El +.Sh SEE ALSO +.Xr _exit 2 , +.Xr getcontext 2 , +.Xr setcontext 2 , +.Xr ucontext 2 +.Sh STANDARDS +The +.Fn makecontext +and +.Fn swapcontext +functions conform to +.St -xsh5 +and +.St -p1003.1-2001 . +.Pp +The +.St -p1003.1-2004 +revision marked the functions +.Fn makecontext +and +.Fn swapcontext +as obsolete, citing portability issues and recommending the use of +.Tn POSIX +threads instead. +The +.St -p1003.1-2008 +revision removed the functions from the specification. +.Pp +.Bf -symbolic +The standard does not clearly define the type of integer arguments +passed to +.Fa func +via +.Fn makecontext ; +portable applications should not rely on the implementation detail that +it may be possible to pass pointer arguments to functions. +.Ef +This may be clarified in a future revision of the standard. +.Sh HISTORY +The +.Fn makecontext +and +.Fn swapcontext +functions first appeared in +.At V.4 . +.Sh CAVEATS +Due to limitations in the current pthread implementation, +.Nm +should not be used in programs which link against the +.Xr pthread 3 +library (whether threads are used or not). diff --git a/lib/nbsd_libc/gen/modf_ieee754.c b/lib/nbsd_libc/gen/modf_ieee754.c new file mode 100644 index 000000000..2132036b3 --- /dev/null +++ b/lib/nbsd_libc/gen/modf_ieee754.c @@ -0,0 +1,102 @@ +/* $NetBSD: modf_ieee754.c,v 1.3 2010/01/27 14:10:41 drochner Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include +#include +#include +#include + +/* + * double modf(double val, double *iptr) + * returns: f and i such that |f| < 1.0, (f + i) = val, and + * sign(f) == sign(i) == sign(val). + * + * Beware signedness when doing subtraction, and also operand size! + */ +double +modf(double val, double *iptr) +{ + union ieee_double_u u, v; + u_int64_t frac; + + /* + * If input is +/-Inf or NaN, return +/-0 or NaN. + */ + u.dblu_d = val; + if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN) { + *iptr = u.dblu_d; + return (0.0 / u.dblu_d); + } + + /* + * If input can't have a fractional part, return + * (appropriately signed) zero, and make i be the input. + */ + if ((int)u.dblu_dbl.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) { + *iptr = u.dblu_d; + v.dblu_d = 0.0; + v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign; + return (v.dblu_d); + } + + /* + * If |input| < 1.0, return it, and set i to the appropriately + * signed zero. + */ + if (u.dblu_dbl.dbl_exp < DBL_EXP_BIAS) { + v.dblu_d = 0.0; + v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign; + *iptr = v.dblu_d; + return (u.dblu_d); + } + + /* + * There can be a fractional part of the input. + * If you look at the math involved for a few seconds, it's + * plain to see that the integral part is the input, with the + * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed, + * the fractional part is the part with the rest of the + * bits zeroed. Just zeroing the high bits to get the + * fractional part would yield a fraction in need of + * normalization. Therefore, we take the easy way out, and + * just use subtraction to get the fractional part. + */ + v.dblu_d = u.dblu_d; + /* Zero the low bits of the fraction, the sleazy way. */ + frac = ((u_int64_t)v.dblu_dbl.dbl_frach << 32) + v.dblu_dbl.dbl_fracl; + frac >>= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS); + frac <<= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS); + v.dblu_dbl.dbl_fracl = frac & 0xffffffff; + v.dblu_dbl.dbl_frach = frac >> 32; + *iptr = v.dblu_d; + + u.dblu_d -= v.dblu_d; + u.dblu_dbl.dbl_sign = v.dblu_dbl.dbl_sign; + return (u.dblu_d); +} diff --git a/lib/nbsd_libc/gen/nftw.c b/lib/nbsd_libc/gen/nftw.c new file mode 100644 index 000000000..0e5134261 --- /dev/null +++ b/lib/nbsd_libc/gen/nftw.c @@ -0,0 +1,114 @@ +/* $NetBSD */ + +/* From OpenBSD: nftw.c,v 1.2 2003/07/21 21:15:32 millert Exp */ + +/* + * Copyright (c) 2003 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 + +#ifndef lint +__RCSID("$NetBSD: nftw.c,v 1.1 2005/12/30 23:07:32 agc Exp $"); +#endif + +#include +#include +#include +#include +#include +#include + +int +nftw(const char *path, int (*fn)(const char *, const struct stat *, int, + struct FTW *), int nfds, int ftwflags) +{ + /* LINTED */ + char * const paths[2] = { __UNCONST(path), NULL }; + struct FTW f; + FTSENT *cur; + FTS *ftsp; + int ftsflags, fnflag, error, postorder, sverrno; + + /* XXX - nfds is currently unused */ + if (nfds < 1 || nfds > OPEN_MAX) { + errno = EINVAL; + return (-1); + } + + ftsflags = FTS_COMFOLLOW; + if (!(ftwflags & FTW_CHDIR)) + ftsflags |= FTS_NOCHDIR; + if (ftwflags & FTW_MOUNT) + ftsflags |= FTS_XDEV; + if (ftwflags & FTW_PHYS) + ftsflags |= FTS_PHYSICAL; + postorder = (ftwflags & FTW_DEPTH) != 0; + ftsp = fts_open(paths, ftsflags, NULL); + if (ftsp == NULL) + return (-1); + error = 0; + while ((cur = fts_read(ftsp)) != NULL) { + switch (cur->fts_info) { + case FTS_D: + if (postorder) + continue; + fnflag = FTW_D; + break; + case FTS_DNR: + fnflag = FTW_DNR; + break; + case FTS_DP: + if (!postorder) + continue; + fnflag = FTW_DP; + break; + case FTS_F: + case FTS_DEFAULT: + fnflag = FTW_F; + break; + case FTS_NS: + case FTS_NSOK: + fnflag = FTW_NS; + break; + case FTS_SL: + fnflag = FTW_SL; + break; + case FTS_SLNONE: + fnflag = FTW_SLN; + break; + case FTS_DC: + errno = ELOOP; + /* FALLTHROUGH */ + default: + error = -1; + goto done; + } + f.base = cur->fts_pathlen - cur->fts_namelen; + f.level = cur->fts_level; + error = fn(cur->fts_path, cur->fts_statp, fnflag, &f); + if (error != 0) + break; + } +done: + sverrno = errno; + (void) fts_close(ftsp); + errno = sverrno; + return (error); +} diff --git a/lib/nbsd_libc/gen/nice.3 b/lib/nbsd_libc/gen/nice.3 new file mode 100644 index 000000000..e59f12327 --- /dev/null +++ b/lib/nbsd_libc/gen/nice.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: nice.3,v 1.13 2003/08/07 16:42:53 agc Exp $ +.\" +.\" Copyright (c) 1980, 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. +.\" +.\" @(#)nice.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd February 16, 1998 +.Dt NICE 3 +.Os +.Sh NAME +.Nm nice +.Nd set program scheduling priority +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn nice "int incr" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by +.Xr setpriority 2 . +.Ef +.Pp +The +.Fn nice +function obtains the scheduling priority of the process +from the system and sets it to the priority value specified in +.Fa incr . +The priority is a value in the range -20 to 20. +The default priority is 0; lower priorities cause more favorable scheduling. +Only the super-user may lower priorities. +.Pp +Children inherit the priority of their parent processes via +.Xr fork 2 . +.Sh RETURN VALUES +Upon successful completion, +.Fn nice +returns the new nice value minus +.Dv NZERO . +Otherwise, \-1 is returned, the process' nice value is not changed, and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn nice +function will fail if: +.Bl -tag -width Er +.It Bq Er EPERM +The +.Fa incr +argument is negative and the caller is not the super-user. +.El +.Sh SEE ALSO +.Xr nice 1 , +.Xr fork 2 , +.Xr setpriority 2 , +.Xr renice 8 +.Sh STANDARDS +The +.Fn nice +function conforms to +.St -xpg4.2 . +.Sh HISTORY +A +.Fn nice +syscall appeared in +.At v6 . diff --git a/lib/nbsd_libc/gen/nice.c b/lib/nbsd_libc/gen/nice.c new file mode 100644 index 000000000..907653181 --- /dev/null +++ b/lib/nbsd_libc/gen/nice.c @@ -0,0 +1,68 @@ +/* $NetBSD: nice.c,v 1.12 2003/08/07 16:42:53 agc Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)nice.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: nice.c,v 1.12 2003/08/07 16:42:53 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(nice,_nice) +#endif + +/* + * Backwards compatible nice. + */ +int +nice(incr) + int incr; +{ + int prio; + + errno = 0; + prio = getpriority(PRIO_PROCESS, 0); + if (prio == -1 && errno) + return (-1); + if (setpriority(PRIO_PROCESS, 0, prio + incr) != 0) + return (-1); + return (getpriority(PRIO_PROCESS, 0)); +} diff --git a/lib/nbsd_libc/gen/nlist.3 b/lib/nbsd_libc/gen/nlist.3 new file mode 100644 index 000000000..2a9f9917a --- /dev/null +++ b/lib/nbsd_libc/gen/nlist.3 @@ -0,0 +1,79 @@ +.\" $NetBSD: nlist.3,v 1.11 2003/08/07 16:42:53 agc Exp $ +.\" +.\" Copyright (c) 1980, 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. +.\" +.\" @(#)nlist.3 8.3 (Berkeley) 4/19/94 +.\" +.Dd April 19, 1994 +.Dt NLIST 3 +.Os +.Sh NAME +.Nm nlist +.Nd retrieve symbol table name list from an executable file +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In nlist.h +.Ft int +.Fn nlist "const char *filename" "struct nlist *nl" +.Sh DESCRIPTION +The +.Fn nlist +function +retrieves name list entries from the symbol table of an +executable file. +(See +.Xr a.out 5 . ) +The argument +.Fa \&nl +is set to reference the +beginning of the list. +The list is preened of binary and invalid data; +if an entry in the +name list is valid, the +.Fa n_type +and +.Fa n_value +for the entry are copied into the list +referenced by +.Fa \&nl . +No other data is copied. +The last entry in the list is always +.Dv NULL . +.Sh RETURN VALUES +The number of invalid entries is returned if successful; otherwise, +if the file +.Fa filename +does not exist or is not executable, the returned value is \-1. +.Sh SEE ALSO +.Xr a.out 5 +.Sh HISTORY +A +.Fn nlist +function appeared in +.At v6 . diff --git a/lib/nbsd_libc/gen/nlist.c b/lib/nbsd_libc/gen/nlist.c new file mode 100644 index 000000000..5689be1dc --- /dev/null +++ b/lib/nbsd_libc/gen/nlist.c @@ -0,0 +1,147 @@ +/* $NetBSD: nlist.c,v 1.22 2009/08/20 08:30:04 he Exp $ */ + +/* + * Copyright (c) 1989, 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. + */ + +/* + * Copyright (c) 1996 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 by the University of + * California, Berkeley and its contributors. + * 4. 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: nlist.c,v 1.22 2009/08/20 08:30:04 he Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#if 0 +#ifdef __weak_alias +__weak_alias(nlist,_nlist) +#endif +#endif + +#include "nlist_private.h" + +static const struct { + int (*fdnlist) __P((int, struct nlist *)); +} fdnlist_fmts[] = { +#ifdef NLIST_AOUT + { __fdnlist_aout }, +#endif +#ifdef NLIST_COFF + { __fdnlist_coff }, +#endif +#ifdef NLIST_ECOFF + { __fdnlist_ecoff }, +#endif +#ifdef NLIST_ELF32 + { __fdnlist_elf32 }, +#endif +#ifdef NLIST_ELF64 + { __fdnlist_elf64 }, +#endif +}; + +int +nlist(name, list) + const char *name; + struct nlist *list; +{ + int fd, n; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(list != NULL); + + fd = open(name, O_RDONLY, 0); + if (fd < 0) + return (-1); + n = __fdnlist(fd, list); + (void)close(fd); + return (n); +} + +int +__fdnlist(fd, list) + int fd; + struct nlist *list; +{ + size_t i; + int rv; + + _DIAGASSERT(fd != -1); + _DIAGASSERT(list != NULL); + + for (i = 0; i < sizeof(fdnlist_fmts) / sizeof(fdnlist_fmts[0]); i++) + if ((rv = (*fdnlist_fmts[i].fdnlist)(fd, list)) != -1) + return (rv); + return (-1); +} diff --git a/lib/nbsd_libc/gen/nlist_aout.c b/lib/nbsd_libc/gen/nlist_aout.c new file mode 100644 index 000000000..e51fd88b3 --- /dev/null +++ b/lib/nbsd_libc/gen/nlist_aout.c @@ -0,0 +1,195 @@ +/* $NetBSD: nlist_aout.c,v 1.22 2009/08/20 11:08:59 martin Exp $ */ + +/* + * Copyright (c) 1989, 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. + */ + +/* + * Copyright (c) 1996 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 by the University of + * California, Berkeley and its contributors. + * 4. 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: nlist_aout.c,v 1.22 2009/08/20 11:08:59 martin Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +struct nlist; +#include "nlist_private.h" + +#ifdef NLIST_AOUT +#include +#include + +int +__fdnlist_aout(fd, list) + int fd; + struct nlist *list; +{ + struct nlist *p, *s; + char *strtab; + off_t stroff, symoff; + int nent; + size_t strsize, symsize, cc; + struct nlist nbuf[1024]; + struct exec exec; + struct stat st; + char *scoreboard, *scored; + + _DIAGASSERT(fd != -1); + _DIAGASSERT(list != NULL); + + if (pread(fd, &exec, sizeof(exec), (off_t)0) != sizeof(exec) || + N_BADMAG(exec) || fstat(fd, &st) < 0) + return (-1); + + symoff = N_SYMOFF(exec); + symsize = (size_t)exec.a_syms; + stroff = symoff + symsize; + + /* Check for files too large to mmap. */ + if ((uintmax_t)(st.st_size - stroff) > (uintmax_t)SIZE_T_MAX) { + errno = EFBIG; + return (-1); + } + /* + * Map string table into our address space. This gives us + * an easy way to randomly access all the strings, without + * making the memory allocation permanent as with malloc/free + * (i.e., munmap will return it to the system). + */ + strsize = (size_t)(st.st_size - stroff); + strtab = mmap(NULL, strsize, PROT_READ, MAP_PRIVATE|MAP_FILE, + fd, stroff); + if (strtab == (char *)-1) + return (-1); + /* + * clean out any left-over information for all valid entries. + * Type and value defined to be 0 if not found; historical + * versions cleared other and desc as well. Also figure out + * the largest string length so don't read any more of the + * string table than we have to. + * + * XXX clearing anything other than n_type and n_value violates + * the semantics given in the man page. + */ + nent = 0; + for (p = list; !ISLAST(p); ++p) { + p->n_type = 0; + p->n_other = 0; + p->n_desc = 0; + p->n_value = 0; + ++nent; + } + if (lseek(fd, symoff, SEEK_SET) == -1) + return (-1); +#if defined(__SSP__) || defined(__SSP_ALL__) + scoreboard = malloc((size_t)nent); +#else + scoreboard = alloca((size_t)nent); +#endif + if (scoreboard == NULL) + return (-1); + (void)memset(scoreboard, 0, (size_t)nent); + + while (symsize > 0) { + cc = MIN(symsize, sizeof(nbuf)); + if (read(fd, nbuf, cc) != (ssize_t) cc) + break; + symsize -= cc; + for (s = nbuf; cc > 0; ++s, cc -= sizeof(*s)) { + long soff = s->n_un.n_strx; + + if (soff == 0 || (s->n_type & N_STAB) != 0) + continue; + for (p = list, scored = scoreboard; !ISLAST(p); + p++, scored++) + if (*scored == 0 && + !strcmp(&strtab[(size_t)soff], + p->n_un.n_name)) { + p->n_value = s->n_value; + p->n_type = s->n_type; + p->n_desc = s->n_desc; + p->n_other = s->n_other; + *scored = 1; + if (--nent <= 0) + break; + } + } + } + munmap(strtab, strsize); +#if defined(__SSP__) || defined(__SSP_ALL__) + free(scoreboard); +#endif + return (nent); +} +#endif /* NLIST_AOUT */ diff --git a/lib/nbsd_libc/gen/nlist_coff.c b/lib/nbsd_libc/gen/nlist_coff.c new file mode 100644 index 000000000..1ef347542 --- /dev/null +++ b/lib/nbsd_libc/gen/nlist_coff.c @@ -0,0 +1,199 @@ +/* $NetBSD: nlist_coff.c,v 1.8 2009/08/21 08:42:02 he Exp $ */ + +/* + * Copyright (c) 1996 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nlist_coff.c,v 1.8 2009/08/21 08:42:02 he Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "nlist_private.h" +#ifdef NLIST_COFF +#include +#endif + +#ifdef NLIST_COFF +#define BAD do { rv = -1; goto out; } while (/*CONSTCOND*/0) +#define BADUNMAP do { rv = -1; goto unmap; } while (/*CONSTCOND*/0) + +#define ES_LEN 18 +struct coff_extsym { + union { + char u_name[8]; + struct { + int u_zero; + int u_offset; + } s; + } u; + int32_t es_value; + int16_t es_scnum; + int16_t es_type; + int8_t es_class; + int8_t es_numaux; +}; +#define es_name u.u_name +#define es_zero u.s.u_zero +#define es_offset u.s.u_offset + +int +__fdnlist_coff(fd, list) + int fd; + struct nlist *list; +{ + struct nlist *p; + struct coff_filehdr *filehdrp; + struct stat st; + char *mappedfile; + size_t mappedsize; + u_long symoff, extstroff; + int rv, nent; + long i, nesyms; + + _DIAGASSERT(fd != -1); + _DIAGASSERT(list != NULL); + + rv = -1; + + /* + * If we can't fstat() the file, something bad is going on. + */ + if (fstat(fd, &st) < 0) + BAD; + + /* + * Map the file in its entirety. + */ + if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) { + errno = EFBIG; + BAD; + } + mappedsize = st.st_size; + mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE, + fd, 0); + if (mappedfile == (char *)-1) + BAD; + + /* + * Make sure we can access the executable's header + * directly, and make sure we recognize the executable + * as an COFF binary. + */ + if (mappedsize < sizeof (struct coff_filehdr)) + BADUNMAP; + filehdrp = (struct coff_filehdr *)&mappedfile[0]; + + if (COFF_BADMAG(filehdrp)) + BADUNMAP; + + /* + * Find the symbol list. + */ + symoff = filehdrp->f_symptr; + nesyms = filehdrp->f_nsyms; + + if (symoff + ES_LEN * nesyms > mappedsize) + BADUNMAP; + extstroff = symoff + ES_LEN * nesyms; + + nent = 0; + for (p = list; !ISLAST(p); ++p) { + p->n_type = 0; + p->n_other = 0; + p->n_desc = 0; + p->n_value = 0; + ++nent; + } + + for (i = 0; i < nesyms; i++) { + char *symtabname; + const char *nlistname; + struct coff_extsym esym; + char name[10]; + + memcpy(&esym, &mappedfile[symoff + ES_LEN * i], ES_LEN); + if (esym.es_numaux != 0) { + i += esym.es_numaux; /* XXX Skip aux entry */ + continue; + } + + if (esym.es_zero != 0) { + memcpy(name, esym.es_name, 8); + name[8] = 0; + symtabname = name; + } else if (esym.es_offset != 0) + symtabname = &mappedfile[extstroff + esym.es_offset]; + else + continue; + + for (p = list; !ISLAST(p); p++) { + nlistname = N_NAME(p); + if (!strcmp(symtabname, nlistname)) { + /* + * Translate (roughly) from COFF to nlist + */ + p->n_value = esym.es_value; + p->n_type = N_EXT; /* XXX */ + p->n_desc = 0; /* XXX */ + p->n_other = 0; /* XXX */ + + if (--nent <= 0) + goto done; + break; /* into next run of outer loop */ + } + } + } + +done: + rv = nent; +unmap: + munmap(mappedfile, mappedsize); +out: + return (rv); +} + +#endif /* NLIST_COFF */ diff --git a/lib/nbsd_libc/gen/nlist_ecoff.c b/lib/nbsd_libc/gen/nlist_ecoff.c new file mode 100644 index 000000000..8555590a2 --- /dev/null +++ b/lib/nbsd_libc/gen/nlist_ecoff.c @@ -0,0 +1,189 @@ +/* $NetBSD: nlist_ecoff.c,v 1.18 2009/08/21 08:42:02 he Exp $ */ + +/* + * Copyright (c) 1996 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nlist_ecoff.c,v 1.18 2009/08/21 08:42:02 he Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "nlist_private.h" +#ifdef NLIST_ECOFF +#include +#endif + +#ifdef NLIST_ECOFF +#define check(off, size) ((off < 0) || (off + size > mappedsize)) +#define BAD do { rv = -1; goto out; } while (/*CONSTCOND*/0) +#define BADUNMAP do { rv = -1; goto unmap; } while (/*CONSTCOND*/0) + +int +__fdnlist_ecoff(fd, list) + int fd; + struct nlist *list; +{ + struct nlist *p; + struct ecoff_exechdr *exechdrp; + struct ecoff_symhdr *symhdrp; + struct ecoff_extsym *esyms; + struct stat st; + char *mappedfile; + size_t mappedsize; + u_long symhdroff, extstroff; + u_int symhdrsize; + int rv, nent; + long i, nesyms; + + _DIAGASSERT(fd != -1); + _DIAGASSERT(list != NULL); + + rv = -1; + + /* + * If we can't fstat() the file, something bad is going on. + */ + if (fstat(fd, &st) < 0) + BAD; + + /* + * Map the file in its entirety. + */ + if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) { + errno = EFBIG; + BAD; + } + mappedsize = st.st_size; + mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE, + fd, 0); + if (mappedfile == (char *)-1) + BAD; + + /* + * Make sure we can access the executable's header + * directly, and make sure the recognize the executable + * as an ECOFF binary. + */ + if (check(0, sizeof *exechdrp)) + BADUNMAP; + exechdrp = (struct ecoff_exechdr *)&mappedfile[0]; + + if (ECOFF_BADMAG(exechdrp)) + BADUNMAP; + + /* + * Find the symbol list. + */ + symhdroff = exechdrp->f.f_symptr; + symhdrsize = exechdrp->f.f_nsyms; + + if ((symhdroff + sizeof *symhdrp) > mappedsize || + sizeof *symhdrp != symhdrsize) + BADUNMAP; + symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff]; + + nesyms = symhdrp->esymMax; + if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms)) + BADUNMAP; + esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset]; + extstroff = symhdrp->cbSsExtOffset; + + /* + * Clean out any left-over information for all valid entries. + * Type and value are defined to be 0 if not found; historical + * versions cleared other and desc as well. + * + * XXX Clearing anything other than n_type and n_value violates + * the semantics given in the man page. + */ + nent = 0; + for (p = list; !ISLAST(p); ++p) { + p->n_type = 0; + p->n_other = 0; + p->n_desc = 0; + p->n_value = 0; + ++nent; + } + + for (i = 0; i < nesyms; i++) { + for (p = list; !ISLAST(p); p++) { + const char *nlistname; + char *symtabname; + + /* This may be incorrect */ + nlistname = N_NAME(p); + if (*nlistname == '_') + nlistname++; + + symtabname = + &mappedfile[extstroff + esyms[i].es_strindex]; + + if (!strcmp(symtabname, nlistname)) { + /* + * Translate (roughly) from ECOFF to nlist + */ + p->n_value = esyms[i].es_value; + p->n_type = N_EXT; /* XXX */ + p->n_desc = 0; /* XXX */ + p->n_other = 0; /* XXX */ + + if (--nent <= 0) + goto done; + break; /* into next run of outer loop */ + } + } + } + +done: + rv = nent; +unmap: + munmap(mappedfile, mappedsize); +out: + return (rv); +} + +#endif /* NLIST_ECOFF */ diff --git a/lib/nbsd_libc/gen/nlist_elf32.c b/lib/nbsd_libc/gen/nlist_elf32.c new file mode 100644 index 000000000..7658f31a6 --- /dev/null +++ b/lib/nbsd_libc/gen/nlist_elf32.c @@ -0,0 +1,309 @@ +/* $NetBSD: nlist_elf32.c,v 1.32 2010/08/28 21:30:02 joerg Exp $ */ + +/* + * Copyright (c) 1996 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nlist_elf32.c,v 1.32 2010/08/28 21:30:02 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +/* If not included by nlist_elf64.c, ELFSIZE won't be defined. */ +#ifndef ELFSIZE +#define ELFSIZE 32 +#endif + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "nlist_private.h" +#if defined(NLIST_ELF32) || defined(NLIST_ELF64) +#include +#endif + +#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \ + (defined(NLIST_ELF64) && (ELFSIZE == 64)) + +/* No need to check for off < 0 because it is unsigned */ +#define check(off, size) (off + size > mappedsize) +#define BAD goto out +#define BADUNMAP goto unmap + +int +ELFNAMEEND(__fdnlist)(fd, list) + int fd; + struct nlist *list; +{ + struct stat st; + struct nlist *p; + char *mappedfile, *strtab; + size_t mappedsize; + Elf_Ehdr *ehdrp, ehdr; + Elf_Shdr *shdrp, *symshdrp, *symstrshdrp; + Elf_Sym *symp; + Elf_Off shdr_off; + Elf_Word shdr_size; +#if (ELFSIZE == 32) + Elf32_Half nshdr; +#elif (ELFSIZE == 64) + Elf64_Word nshdr; +#endif + size_t i, nsyms; + int rv, nent; + + _DIAGASSERT(fd != -1); + _DIAGASSERT(list != NULL); + + rv = -1; + + symshdrp = symstrshdrp = NULL; + + /* + * If we can't fstat() the file, something bad is going on. + */ + if (fstat(fd, &st) < 0) + BAD; + + /* + * Map the file in its entirety. + */ + if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) { + errno = EFBIG; + BAD; + } + + /* + * Read the elf header of the file. + */ + if ((ssize_t)(i = pread(fd, &ehdr, sizeof(Elf_Ehdr), (off_t)0)) == -1) + BAD; + + /* + * Check that the elf header is correct. + */ + if (i != sizeof(Elf_Ehdr)) + BAD; + if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 || + ehdr.e_ident[EI_CLASS] != ELFCLASS) + BAD; + + switch (ehdr.e_machine) { + ELFDEFNNAME(MACHDEP_ID_CASES) + + default: + BAD; + } + + if (S_ISCHR(st.st_mode)) { + const char *nlistname; + struct ksyms_gsymbol kg; + Elf_Sym sym; + + /* + * Character device; assume /dev/ksyms. + */ + nent = 0; + for (p = list; !ISLAST(p); ++p) { + + p->n_other = 0; + p->n_desc = 0; + nlistname = N_NAME(p); + if (*nlistname == '_') + nlistname++; + + kg.kg_name = nlistname; + kg.kg_sym = &sym; + if (ioctl(fd, KIOCGSYMBOL, &kg) == 0) { + p->n_value = sym.st_value; + switch (ELF_ST_TYPE(sym.st_info)) { + case STT_NOTYPE: + p->n_type = N_UNDF; + break; + case STT_OBJECT: + p->n_type = N_DATA; + break; + case STT_FUNC: + p->n_type = N_TEXT; + break; + case STT_FILE: + p->n_type = N_FN; + break; + default: + p->n_type = 0; + /* catch other enumerations for gcc */ + break; + } + if (ELF_ST_BIND(sym.st_info) != STB_LOCAL) + p->n_type |= N_EXT; + } else { + nent++; + p->n_value = 0; + p->n_type = 0; + } + } + return nent; + } + + mappedsize = (size_t)st.st_size; + mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE, + fd, (off_t)0); + if (mappedfile == (char *)-1) + BAD; + + /* + * Make sure we can access the executable's header + * directly, and make sure the recognize the executable + * as an ELF binary. + */ + if (check(0, sizeof *ehdrp)) + BADUNMAP; + ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0]; + + /* + * Find the symbol list and string table. + */ + nshdr = ehdrp->e_shnum; + shdr_off = ehdrp->e_shoff; + shdr_size = ehdrp->e_shentsize * nshdr; + + if (check(shdr_off, shdr_size) || + (sizeof *shdrp != ehdrp->e_shentsize)) + BADUNMAP; + shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off]; + + for (i = 0; i < nshdr; i++) { + if (shdrp[i].sh_type == SHT_SYMTAB) { + symshdrp = &shdrp[i]; + symstrshdrp = &shdrp[shdrp[i].sh_link]; + } + } + + /* Make sure we're not stripped. */ + if (symshdrp == NULL || symshdrp->sh_offset == 0) + BADUNMAP; + + /* Make sure the symbols and strings are safely mapped. */ + if (check(symshdrp->sh_offset, symshdrp->sh_size)) + BADUNMAP; + if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size)) + BADUNMAP; + + symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset]; + nsyms = symshdrp->sh_size / sizeof(*symp); + strtab = &mappedfile[symstrshdrp->sh_offset]; + + /* + * Clean out any left-over information for all valid entries. + * Type and value are defined to be 0 if not found; historical + * versions cleared other and desc as well. + * + * XXX Clearing anything other than n_type and n_value violates + * the semantics given in the man page. + */ + nent = 0; + for (p = list; !ISLAST(p); ++p) { + p->n_type = 0; + p->n_other = 0; + p->n_desc = 0; + p->n_value = 0; + ++nent; + } + + for (i = 0; i < nsyms; i++) { + for (p = list; !ISLAST(p); ++p) { + const char *nlistname; + char *symtabname; + + /* This may be incorrect */ + nlistname = N_NAME(p); + if (*nlistname == '_') + nlistname++; + + symtabname = &strtab[symp[i].st_name]; + + if (!strcmp(symtabname, nlistname)) { + /* + * Translate (roughly) from ELF to nlist + */ + p->n_value = symp[i].st_value; + switch (ELF_ST_TYPE(symp[i].st_info)) { + case STT_NOTYPE: + p->n_type = N_UNDF; + break; + case STT_OBJECT: + p->n_type = N_DATA; + break; + case STT_FUNC: + p->n_type = N_TEXT; + break; + case STT_FILE: + p->n_type = N_FN; + break; + default: + /* catch other enumerations for gcc */ + break; + } + if (ELF_ST_BIND(symp[i].st_info) != STB_LOCAL) + p->n_type |= N_EXT; + p->n_desc = 0; /* XXX */ + p->n_other = 0; /* XXX */ + + if (--nent <= 0) + goto done; + break; /* into next run of outer loop */ + } + } + } + +done: + rv = nent; +unmap: + munmap(mappedfile, mappedsize); +out: + return (rv); +} + +#endif diff --git a/lib/nbsd_libc/gen/nlist_elf64.c b/lib/nbsd_libc/gen/nlist_elf64.c new file mode 100644 index 000000000..9f9cff26b --- /dev/null +++ b/lib/nbsd_libc/gen/nlist_elf64.c @@ -0,0 +1,44 @@ +/* $NetBSD: nlist_elf64.c,v 1.6 2003/07/26 19:24:43 salo Exp $ */ + +/* + * Copyright (c) 1996 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nlist_elf64.c,v 1.6 2003/07/26 19:24:43 salo Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define ELFSIZE 64 + +#include "nlist_elf32.c" diff --git a/lib/nbsd_libc/gen/nlist_private.h b/lib/nbsd_libc/gen/nlist_private.h new file mode 100644 index 000000000..485fc4de6 --- /dev/null +++ b/lib/nbsd_libc/gen/nlist_private.h @@ -0,0 +1,90 @@ +/* $NetBSD: nlist_private.h,v 1.21 2011/01/17 23:32:31 matt Exp $ */ + +/* + * Copyright (c) 1996 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. + * + * <> + */ + +#if defined(__alpha__) +# define NLIST_ECOFF +# define NLIST_ELF64 +#elif defined(__x86_64__) +# define NLIST_ELF64 +# define NLIST_ELF32 +#elif defined(__mips__) +# define NLIST_ECOFF +# define NLIST_ELF32 +# ifndef __mips_o32 +# define NLIST_ELF64 +# endif +#elif defined(__arm__) || defined(__i386__) || defined (__m68k__) || \ + defined(__powerpc__) || defined(__vax__) +# define NLIST_AOUT +# define NLIST_ELF32 +#elif defined(__sparc__) +# define NLIST_AOUT +# define NLIST_ELF32 +# define NLIST_ELF64 +#elif defined(__SH5__) +# define NLIST_ELF32 +# define NLIST_ELF64 +#elif defined(__sh__) +# define NLIST_COFF +# define NLIST_ELF32 +#elif defined(__hppa__) +# define NLIST_ELF32 +#else +# define NLIST_AOUT +/* #define NLIST_ECOFF */ +/* #define NLIST_ELF32 */ +/* #define NLIST_ELF64 */ +#endif + +#define ISLAST(p) (N_NAME(p) == 0 || N_NAME(p)[0] == 0) + +struct nlist; + +#ifdef NLIST_AOUT +int __fdnlist_aout __P((int, struct nlist *)); +#endif +#ifdef NLIST_COFF +int __fdnlist_coff __P((int, struct nlist *)); +#endif +#ifdef NLIST_ECOFF +int __fdnlist_ecoff __P((int, struct nlist *)); +#endif +#ifdef NLIST_ELF32 +int __fdnlist_elf32 __P((int, struct nlist *)); +#endif +#ifdef NLIST_ELF64 +int __fdnlist_elf64 __P((int, struct nlist *)); +#endif diff --git a/lib/nbsd_libc/gen/opendir.c b/lib/nbsd_libc/gen/opendir.c new file mode 100644 index 000000000..7bb5997b3 --- /dev/null +++ b/lib/nbsd_libc/gen/opendir.c @@ -0,0 +1,164 @@ +/* $NetBSD: opendir.c,v 1.37 2010/09/26 02:26:59 yamt Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94"; +#else +__RCSID("$NetBSD: opendir.c,v 1.37 2010/09/26 02:26:59 yamt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include "extern.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "dirent_private.h" + +static DIR *__opendir_common(int, const char *, int); + +__weak_alias(fdopendir,_fdopendir) + +/* + * Open a directory. + */ +DIR * +opendir(const char *name) +{ + + _DIAGASSERT(name != NULL); + + return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); +} + +DIR * +__opendir2(const char *name, int flags) +{ + int fd; + + if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) + return NULL; + return __opendir_common(fd, name, flags); +} + +#ifndef __LIBC12_SOURCE__ +DIR * +_fdopendir(int fd) +{ + + return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP); +} +#endif + +static DIR * +__opendir_common(int fd, const char *name, int flags) +{ + DIR *dirp = NULL; + int serrno; + struct stat sb; + struct statvfs sfb; + int error; + + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + goto error; + if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) { + errno = ENOTDIR; + goto error; + } + if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) + goto error; + dirp->dd_buf = NULL; + dirp->dd_internal = NULL; +#ifdef _REENTRANT + if (__isthreaded) { + if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL) + goto error; + mutex_init((mutex_t *)dirp->dd_lock, NULL); + } +#endif + + /* + * Tweak flags for the underlying filesystem. + */ + + if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0) + goto error; + if ((flags & DTF_NODUP) != 0) { + if (!strncmp(sfb.f_fstypename, MOUNT_UNION, + sizeof(sfb.f_fstypename)) || + (sfb.f_flag & MNT_UNION) != 0) { + flags |= __DTF_READALL; + } else { + flags &= ~DTF_NODUP; + } + } + if (!strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename))) { + flags |= __DTF_READALL | __DTF_RETRY_ON_BADCOOKIE; + } + + dirp->dd_flags = flags; + error = _initdir(dirp, fd, name); + if (error) { + errno = error; + goto error; + } + + return (dirp); +error: + serrno = errno; + if (dirp != NULL) { +#ifdef _REENTRANT + if (__isthreaded) { + mutex_destroy((mutex_t *)dirp->dd_lock); + free(dirp->dd_lock); + } +#endif + free(dirp->dd_buf); + } + free(dirp); + if (fd != -1) + (void)close(fd); + errno = serrno; + return NULL; +} diff --git a/lib/nbsd_libc/gen/pause.3 b/lib/nbsd_libc/gen/pause.3 new file mode 100644 index 000000000..413ffbb9a --- /dev/null +++ b/lib/nbsd_libc/gen/pause.3 @@ -0,0 +1,92 @@ +.\" $NetBSD: pause.3,v 1.15 2003/08/07 16:42:55 agc Exp $ +.\" +.\" Copyright (c) 1980, 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. +.\" +.\" @(#)pause.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt PAUSE 3 +.Os +.Sh NAME +.Nm pause +.Nd stop until signal +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn pause void +.Sh DESCRIPTION +.Bf -symbolic +Pause is made obsolete by +.Xr sigsuspend 2 . +.Ef +.Pp +The +.Fn pause +function +forces a process to pause until +a signal is received from either the +.Xr kill 2 +function +or an interval timer. +(See +.Xr setitimer 2 . ) +Upon termination of a signal handler started during a +.Fn pause , +the +.Fn pause +call will return. +.Sh RETURN VALUES +Always returns \-1. +.Sh ERRORS +The +.Fn pause +function +always returns: +.Bl -tag -width Er +.It Bq Er EINTR +The call was interrupted. +.El +.Sh SEE ALSO +.Xr kill 2 , +.Xr poll 2 , +.Xr select 2 , +.Xr setitimer 2 , +.Xr sigsuspend 2 +.Sh STANDARDS +The +.Fn pause +function conforms to +.St -p1003.1-90 . +.Sh HISTORY +A +.Fn pause +syscall +appeared in +.At v6 . diff --git a/lib/nbsd_libc/gen/pause.c b/lib/nbsd_libc/gen/pause.c new file mode 100644 index 000000000..f11547a49 --- /dev/null +++ b/lib/nbsd_libc/gen/pause.c @@ -0,0 +1,61 @@ +/* $NetBSD: pause.c,v 1.10 2003/08/07 16:42:55 agc Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)pause.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: pause.c,v 1.10 2003/08/07 16:42:55 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(pause,_pause) +#endif + +/* + * Backwards compatible pause. + */ +int +pause() +{ + sigset_t omask; + + if (sigprocmask(SIG_BLOCK, NULL, &omask) == -1) + return -1; + + return sigsuspend(&omask); +} diff --git a/lib/nbsd_libc/gen/popen.3 b/lib/nbsd_libc/gen/popen.3 new file mode 100644 index 000000000..d5cf7bb28 --- /dev/null +++ b/lib/nbsd_libc/gen/popen.3 @@ -0,0 +1,202 @@ +.\" $NetBSD: popen.3,v 1.16 2007/08/02 23:45:10 wiz Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)popen.3 8.2 (Berkeley) 5/3/95 +.\" +.Dd August 2, 2007 +.Dt POPEN 3 +.Os +.Sh NAME +.Nm popen , +.Nm pclose +.Nd process +.Tn I/O +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdio.h +.Ft FILE * +.Fn popen "const char *command" "const char *type" +.Ft int +.Fn pclose "FILE *stream" +.Sh DESCRIPTION +The +.Fn popen +function +.Dq opens +a process by creating an IPC connection, +forking, +and invoking the shell. +Historically, +.Nm popen +was implemented with a unidirectional pipe; +hence many implementations of +.Nm popen +only allow the +.Fa type +argument to specify reading or writing, not both. +Since +.Nm popen +is now implemented using sockets, the +.Fa type +may request a bidirectional data flow. +The +.Fa type +argument is a pointer to a null-terminated string +which must be +.Ql r +for reading, +.Ql w +for writing, or +.Ql r+ +for reading and writing. +.Pp +The +.Fa command +argument is a pointer to a null-terminated string +containing a shell command line. +This command is passed to +.Pa /bin/sh +using the +.Fl c +flag; interpretation, if any, is performed by the shell. +.Pp +The return value from +.Fn popen +is a normal standard +.Tn I/O +stream in all respects +save that it must be closed with +.Fn pclose +rather than +.Fn fclose . +Writing to such a stream +writes to the standard input of the command; +the command's standard output is the same as that of the process that called +.Fn popen , +unless this is altered by the command itself. +Conversely, reading from a +.Dq popened +stream reads the command's standard output, and +the command's standard input is the same as that of the process that called +.Fn popen . +.Pp +Note that output +.Fn popen +streams are fully buffered by default. +.Pp +The +.Fn pclose +function waits for the associated process to terminate +and returns the exit status of the command +as returned by +.Fn wait4 . +.Sh RETURN VALUES +The +.Fn popen +function returns +.Dv NULL +if the +.Xr fork 2 , +.Xr pipe 2 , +or +.Xr socketpair 2 +calls fail, +or if it cannot allocate memory. +.Pp +The +.Fn pclose +function +returns \-1 if +.Fa stream +is not associated with a +.Dq popened +command, if +.Fa stream +has already been +.Dq pclosed , +or if +.Xr wait4 2 +returns an error. +.Sh ERRORS +The +.Fn popen +function does not reliably set +.Va errno . +.Sh SEE ALSO +.Xr sh 1 , +.Xr fork 2 , +.Xr pipe 2 , +.Xr socketpair 2 , +.Xr wait4 2 , +.Xr fclose 3 , +.Xr fflush 3 , +.Xr fopen 3 , +.Xr shquote 3 , +.Xr stdio 3 , +.Xr system 3 +.Sh STANDARDS +The +.Fn popen +and +.Fn pclose +functions conform to +.St -p1003.2-92 . +.Sh HISTORY +A +.Fn popen +and a +.Fn pclose +function appeared in +.At v7 . +.Sh BUGS +Since the standard input of a command opened for reading +shares its seek offset with the process that called +.Fn popen , +if the original process has done a buffered read, +the command's input position may not be as expected. +Similarly, the output from a command opened for writing +may become intermingled with that of the original process. +The latter can be avoided by calling +.Xr fflush 3 +before +.Fn popen . +.Pp +Failure to execute the shell +is indistinguishable from the shell's failure to execute command, +or an immediate exit of the command. +The only hint is an exit status of 127. +.Pp +The +.Fn popen +argument +always calls +.Xr sh 1 , +never calls +.Xr csh 1 . diff --git a/lib/nbsd_libc/gen/popen.c b/lib/nbsd_libc/gen/popen.c new file mode 100644 index 000000000..edd85d5d0 --- /dev/null +++ b/lib/nbsd_libc/gen/popen.c @@ -0,0 +1,226 @@ +/* $NetBSD: popen.c,v 1.30 2010/11/14 18:11:42 tron Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software written by Ken Arnold and + * published in UNIX Review, Vol. 6, No. 8. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95"; +#else +__RCSID("$NetBSD: popen.c,v 1.30 2010/11/14 18:11:42 tron Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "env.h" +#include "reentrant.h" + +#ifdef __weak_alias +__weak_alias(popen,_popen) +__weak_alias(pclose,_pclose) +#endif + +static struct pid { + struct pid *next; + FILE *fp; +#ifdef _REENTRANT + int fd; +#endif + pid_t pid; +} *pidlist; + +#ifdef _REENTRANT +static rwlock_t pidlist_lock = RWLOCK_INITIALIZER; +#endif + +FILE * +popen(const char *command, const char *type) +{ + struct pid *cur, *old; + FILE *iop; + const char * volatile xtype = type; + int pdes[2], pid, serrno; + volatile int twoway; + + _DIAGASSERT(command != NULL); + _DIAGASSERT(xtype != NULL); + + if (strchr(xtype, '+')) { + twoway = 1; + type = "r+"; + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pdes) < 0) + return (NULL); + } else { + twoway = 0; + if ((*xtype != 'r' && *xtype != 'w') || xtype[1] || + (pipe(pdes) < 0)) { + errno = EINVAL; + return (NULL); + } + } + + if ((cur = malloc(sizeof(struct pid))) == NULL) { + (void)close(pdes[0]); + (void)close(pdes[1]); + errno = ENOMEM; + return (NULL); + } + + (void)rwlock_rdlock(&pidlist_lock); + (void)__readlockenv(); + switch (pid = vfork()) { + case -1: /* Error. */ + serrno = errno; + (void)__unlockenv(); + (void)rwlock_unlock(&pidlist_lock); + free(cur); + (void)close(pdes[0]); + (void)close(pdes[1]); + errno = serrno; + return (NULL); + /* NOTREACHED */ + case 0: /* Child. */ + /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams + from previous popen() calls that remain open in the + parent process are closed in the new child process. */ + for (old = pidlist; old; old = old->next) +#ifdef _REENTRANT + close(old->fd); /* don't allow a flush */ +#else + close(fileno(old->fp)); /* don't allow a flush */ +#endif + + if (*xtype == 'r') { + (void)close(pdes[0]); + if (pdes[1] != STDOUT_FILENO) { + (void)dup2(pdes[1], STDOUT_FILENO); + (void)close(pdes[1]); + } + if (twoway) + (void)dup2(STDOUT_FILENO, STDIN_FILENO); + } else { + (void)close(pdes[1]); + if (pdes[0] != STDIN_FILENO) { + (void)dup2(pdes[0], STDIN_FILENO); + (void)close(pdes[0]); + } + } + + execl(_PATH_BSHELL, "sh", "-c", command, NULL); + _exit(127); + /* NOTREACHED */ + } + (void)__unlockenv(); + + /* Parent; assume fdopen can't fail. */ + if (*xtype == 'r') { + iop = fdopen(pdes[0], xtype); +#ifdef _REENTRANT + cur->fd = pdes[0]; +#endif + (void)close(pdes[1]); + } else { + iop = fdopen(pdes[1], xtype); +#ifdef _REENTRANT + cur->fd = pdes[1]; +#endif + (void)close(pdes[0]); + } + + /* Link into list of file descriptors. */ + cur->fp = iop; + cur->pid = pid; + cur->next = pidlist; + pidlist = cur; + (void)rwlock_unlock(&pidlist_lock); + + return (iop); +} + +/* + * pclose -- + * Pclose returns -1 if stream is not associated with a `popened' command, + * if already `pclosed', or waitpid returns an error. + */ +int +pclose(iop) + FILE *iop; +{ + struct pid *cur, *last; + int pstat; + pid_t pid; + + _DIAGASSERT(iop != NULL); + + rwlock_wrlock(&pidlist_lock); + + /* Find the appropriate file pointer. */ + for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) + if (cur->fp == iop) + break; + if (cur == NULL) { + (void)rwlock_unlock(&pidlist_lock); + return (-1); + } + + (void)fclose(iop); + + /* Remove the entry from the linked list. */ + if (last == NULL) + pidlist = cur->next; + else + last->next = cur->next; + + (void)rwlock_unlock(&pidlist_lock); + + do { + pid = waitpid(cur->pid, &pstat, 0); + } while (pid == -1 && errno == EINTR); + + free(cur); + + return (pid == -1 ? -1 : pstat); +} diff --git a/lib/nbsd_libc/gen/psignal.3 b/lib/nbsd_libc/gen/psignal.3 new file mode 100644 index 000000000..05e5922fc --- /dev/null +++ b/lib/nbsd_libc/gen/psignal.3 @@ -0,0 +1,105 @@ +.\" $NetBSD: psignal.3,v 1.17 2010/08/27 08:38:41 christos Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)psignal.3 8.2 (Berkeley) 2/27/95 +.\" +.Dd August 27, 2010 +.Dt PSIGNAL 3 +.Os +.Sh NAME +.Nm psignal , +.Nm psiginfo , +.Nm sys_siglist , +.Nm sys_signame +.Nd system signal messages +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft void +.Fn psignal "int sig" "const char *s" +.Fn psiginfo "const siginfo_t *si" "const char *s" +.Vt extern const char * const sys_siglist[]; +.Vt extern const char * const sys_signame[]; +.Sh DESCRIPTION +The +.Fn psignal +function locates the descriptive message +string for the given signal number +.Fa sig +and writes it to the standard error. +.Pp +If the argument +.Fa s +is +.Pf non- Dv NULL +it is written to the standard error file descriptor +prior to the message string, +immediately followed by a colon and a space. +If the signal number is not recognized +.Pq Xr sigaction 2 , +the string +.Dq "Unknown signal" +is produced. +.Pp +The +.Fn psiginfo +function produces the same output as the +.Fn psignal +function, only it uses the signal number information from the +.Fa si +argument. +.Pp +The message strings can be accessed directly using the external array +.Va sys_siglist , +indexed by recognized signal numbers. +The external array +.Va sys_signame +is used similarly and contains short, upper-case abbreviations for signals +which are useful for recognizing signal names in user input. +The defined variable +.Dv NSIG +contains a count of the strings in +.Va sys_siglist +and +.Va sys_signame . +.Sh SEE ALSO +.Xr sigaction 2 , +.Xr perror 3 , +.Xr setlocale 3 , +.Xr strsignal 3 +.Sh HISTORY +The +.Fn psignal +function appeared in +.Bx 4.2 . +The +.Fn psiginfo +function appeared in +.Nx 6.0 . diff --git a/lib/nbsd_libc/gen/psignal.c b/lib/nbsd_libc/gen/psignal.c new file mode 100644 index 000000000..c2363519c --- /dev/null +++ b/lib/nbsd_libc/gen/psignal.c @@ -0,0 +1,85 @@ +/* $NetBSD: psignal.c,v 1.22 2010/08/27 08:38:41 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)psignal.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: psignal.c,v 1.22 2010/08/27 08:38:41 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#include +#include +#include +#include + +#include "extern.h" + +#ifdef __weak_alias +__weak_alias(psignal,_psignal) +#endif + +void +psignal(int sig, const char *s) +{ + struct iovec *v; + struct iovec iov[4]; + char buf[NL_TEXTMAX]; + + v = iov; + if (s && *s) { + v->iov_base = __UNCONST(s); + v->iov_len = strlen(s); + v++; + v->iov_base = __UNCONST(": "); + v->iov_len = 2; + v++; + } + v->iov_base = __UNCONST(__strsignal((int)sig, buf, sizeof(buf))); + v->iov_len = strlen(v->iov_base); + v++; + v->iov_base = __UNCONST("\n"); + v->iov_len = 1; + (void)writev(STDERR_FILENO, iov, (v - iov) + 1); +} + +void +psiginfo(const siginfo_t *si, const char *s) +{ + psignal(si->si_signo, s); +} diff --git a/lib/nbsd_libc/gen/pthread_atfork.3 b/lib/nbsd_libc/gen/pthread_atfork.3 new file mode 100644 index 000000000..76c9f08f9 --- /dev/null +++ b/lib/nbsd_libc/gen/pthread_atfork.3 @@ -0,0 +1,120 @@ +.\" $NetBSD: pthread_atfork.3,v 1.5 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2003 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Nathan J. Williams. +.\" +.\" 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 February 12, 2003 +.Dt PTHREAD_ATFORK 3 +.Os +.Sh NAME +.Nm pthread_atfork +.Nd register handlers to be called when process forks +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In pthread.h +.Ft int +.Fn pthread_atfork "void (*prepare)(void)" "void (*parent)(void)" "void (*child)(void)" +.Sh DESCRIPTION +The +.Fn pthread_atfork +function registers the provided handler functions to be called when the +.Xr fork 2 +function is called. +Each of the three handlers is called at a different place in the +.Xr fork 2 +sequence. +The +.Ar prepare +handler is called in the parent process before the fork happens, the +.Ar parent +handler is called in the parent process after the fork has happened, and the +.Ar child +handler is called in the child process after the fork has happened. +The +.Ar parent +and +.Ar child +handlers are called in the order in which they were registered, while the +.Ar prepare +handlers are called in reverse of the order in which they were registered. +.Pp +Any of the handlers given may be +.Dv NULL . +.Pp +The intended use of +.Fn pthread_atfork +is to provide a consistent state to a child process from a multithreaded parent +process where locks may be acquired and released asynchronously with respect to the +.Xr fork 2 +call. +Each subsystem with locks that are used in a child process should register +handlers with +.Fn pthread_atfork +that acquires those locks in the +.Ar prepare +handler and releases them in the +.Ar parent +handler. +.Sh RETURN VALUES +The +.Fn pthread_atfork +function returns 0 on success and an error number on failure. +.Sh ERRORS +The following error code may be returned: +.Bl -tag -width Er +.It Bq Er ENOMEM +Insufficient memory exists to register the fork handlers. +.El +.Sh SEE ALSO +.Xr fork 2 +.Sh STANDARDS +The +.Fn pthread_atfork +function conforms to +.St -p1003.1c-95 . +.Sh HISTORY +The +.Fn pthread_atfork +function first appeared in +.Nx 2.0 . +.Sh CAVEATS +After calling +.Xr fork 2 +from a multithreaded process, it is only safe to call +async-signal-safe functions until calling one of the +.Xr exec 3 +functions. +The +.Fn pthread_* +functions are not async-signal-safe, so it is not safe to use such functions +in the +.Ar child +handler. +.Sh BUGS +There is no way to unregister a handler registered with +.Fn pthread_atfork . diff --git a/lib/nbsd_libc/gen/pthread_atfork.c b/lib/nbsd_libc/gen/pthread_atfork.c new file mode 100644 index 000000000..4f7f5608c --- /dev/null +++ b/lib/nbsd_libc/gen/pthread_atfork.c @@ -0,0 +1,203 @@ +/* $NetBSD: pthread_atfork.c,v 1.8 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nathan J. Williams. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: pthread_atfork.c,v 1.8 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include "reentrant.h" + +#ifdef __weak_alias +__weak_alias(pthread_atfork, _pthread_atfork) +__weak_alias(fork, _fork) +#endif /* __weak_alias */ + +pid_t __fork __P((void)); /* XXX */ + +struct atfork_callback { + SIMPLEQ_ENTRY(atfork_callback) next; + void (*fn)(void); +}; + +/* + * Hypothetically, we could protect the queues with a rwlock which is + * write-locked by pthread_atfork() and read-locked by fork(), but + * since the intended use of the functions is obtaining locks to hold + * across the fork, forking is going to be serialized anyway. + */ +static struct atfork_callback atfork_builtin; +static mutex_t atfork_lock = MUTEX_INITIALIZER; +SIMPLEQ_HEAD(atfork_callback_q, atfork_callback); + +static struct atfork_callback_q prepareq = SIMPLEQ_HEAD_INITIALIZER(prepareq); +static struct atfork_callback_q parentq = SIMPLEQ_HEAD_INITIALIZER(parentq); +static struct atfork_callback_q childq = SIMPLEQ_HEAD_INITIALIZER(childq); + +static struct atfork_callback * +af_alloc(void) +{ + + if (atfork_builtin.fn == NULL) + return &atfork_builtin; + + return malloc(sizeof(atfork_builtin)); +} + +static void +af_free(struct atfork_callback *af) +{ + + if (af != &atfork_builtin) + free(af); +} + +int +pthread_atfork(void (*prepare)(void), void (*parent)(void), + void (*child)(void)) +{ + struct atfork_callback *newprepare, *newparent, *newchild; + + newprepare = newparent = newchild = NULL; + + mutex_lock(&atfork_lock); + if (prepare != NULL) { + newprepare = af_alloc(); + if (newprepare == NULL) { + mutex_unlock(&atfork_lock); + return ENOMEM; + } + newprepare->fn = prepare; + } + + if (parent != NULL) { + newparent = af_alloc(); + if (newparent == NULL) { + if (newprepare != NULL) + af_free(newprepare); + mutex_unlock(&atfork_lock); + return ENOMEM; + } + newparent->fn = parent; + } + + if (child != NULL) { + newchild = af_alloc(); + if (newchild == NULL) { + if (newprepare != NULL) + af_free(newprepare); + if (newparent != NULL) + af_free(newparent); + mutex_unlock(&atfork_lock); + return ENOMEM; + } + newchild->fn = child; + } + + /* + * The order in which the functions are called is specified as + * LIFO for the prepare handler and FIFO for the others; insert + * at the head and tail as appropriate so that SIMPLEQ_FOREACH() + * produces the right order. + */ + if (prepare) + SIMPLEQ_INSERT_HEAD(&prepareq, newprepare, next); + if (parent) + SIMPLEQ_INSERT_TAIL(&parentq, newparent, next); + if (child) + SIMPLEQ_INSERT_TAIL(&childq, newchild, next); + mutex_unlock(&atfork_lock); + + return 0; +} + +pid_t +fork(void) +{ + struct atfork_callback *iter; + pid_t ret; + + mutex_lock(&atfork_lock); + SIMPLEQ_FOREACH(iter, &prepareq, next) + (*iter->fn)(); + + ret = __fork(); + + if (ret != 0) { + /* + * We are the parent. It doesn't matter here whether + * the fork call succeeded or failed. + */ + SIMPLEQ_FOREACH(iter, &parentq, next) + (*iter->fn)(); + mutex_unlock(&atfork_lock); + } else { + /* We are the child */ + SIMPLEQ_FOREACH(iter, &childq, next) + (*iter->fn)(); + /* + * Note: We are explicitly *not* unlocking + * atfork_lock. Unlocking atfork_lock is problematic, + * because if any threads in the parent blocked on it + * between the initial lock and the fork() syscall, + * unlocking in the child will try to schedule + * threads, and either the internal mutex interlock or + * the runqueue spinlock could have been held at the + * moment of fork(). Since the other threads do not + * exist in this process, the spinlock will never be + * unlocked, and we would wedge. + * Instead, we reinitialize atfork_lock, since we know + * that the state of the atfork lists is consistent here, + * and that there are no other threads to be affected by + * the forcible cleaning of the queue. + * This permits double-forking to work, although + * it requires knowing that it's "safe" to initialize + * a locked mutex in this context. + * + * The problem exists for users of this interface, + * too, since the intented use of pthread_atfork() is + * to acquire locks across the fork call to ensure + * that the child sees consistent state. There's not + * much that can usefully be done in a child handler, + * and conventional wisdom discourages using them, but + * they're part of the interface, so here we are... + */ + mutex_init(&atfork_lock, NULL); + } + + return ret; +} diff --git a/lib/nbsd_libc/gen/pw_private.h b/lib/nbsd_libc/gen/pw_private.h new file mode 100644 index 000000000..646fb2d5c --- /dev/null +++ b/lib/nbsd_libc/gen/pw_private.h @@ -0,0 +1,8 @@ +/* $NetBSD: pw_private.h,v 1.2 2003/07/26 19:24:43 salo Exp $ */ + +/* + * Written by Jason R. Thorpe , June 26, 1998. + * Public domain. + */ + +int __pw_scan __P((char *bp, struct passwd *pw, int *flags)); diff --git a/lib/nbsd_libc/gen/pw_scan.c b/lib/nbsd_libc/gen/pw_scan.c new file mode 100644 index 000000000..5bb8a105a --- /dev/null +++ b/lib/nbsd_libc/gen/pw_scan.c @@ -0,0 +1,240 @@ +/* $NetBSD: pw_scan.c,v 1.22 2009/01/29 10:41:39 enami Exp $ */ + +/* + * Copyright (c) 1987, 1993, 1994, 1995 + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#include "compat_pwd.h" + +#else +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: pw_scan.c,v 1.22 2009/01/29 10:41:39 enami Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(_LIBC) +#include "namespace.h" +#endif +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _LIBC +#include "pw_private.h" +#endif +#endif /* ! HAVE_NBTOOL_CONFIG_H */ + +static int +gettime(long long *res, const char *p, int *flags, int dowarn, int flag) +{ + long long l; + char *ep; + const char *vp; + + if (*p == '\0') { + *flags |= flag; + *res = 0; + return 1; + } + l = strtoll(p, &ep, 0); + if (p == ep || *ep != '\0') { + vp = "Invalid number"; + goto done; + } + if (errno == ERANGE && (l == LLONG_MAX || l == LLONG_MIN)) { + vp = strerror(errno); + goto done; + } + + *res = l; + return 1; +done: + if (dowarn) { + warnx("%s `%s' for %s time", vp, p, + flag == _PASSWORD_NOEXP ? "expiration" : "change"); + } + return 0; + +} + +static int +getid(unsigned long *res, const char *p, int *flags, int dowarn, int flag) +{ + unsigned long ul; + char *ep; + + if (*p == '\0') { + *flags |= flag; + *res = 0; + return 1; + } + ul = strtoul(p, &ep, 0); + if (p == ep || *ep != '\0') { + ep = __UNCONST("Invalid number"); + goto done; + } + if (errno == ERANGE && ul == ULONG_MAX) { + ep = strerror(errno); + goto done; + } + if (ul > *res) { + ep = strerror(ERANGE); + goto done; + } + + *res = ul; + return 1; +done: + if (dowarn) + warnx("%s %s `%s'", ep, + flag == _PASSWORD_NOUID ? "uid" : "gid", p); + return 0; + +} + +int +#ifdef _LIBC +__pw_scan(char *bp, struct passwd *pw, int *flags) +#else +pw_scan( char *bp, struct passwd *pw, int *flags) +#endif +{ + unsigned long id; + long long ti; + int root, inflags; + int dowarn; + const char *p, *sh; + + _DIAGASSERT(bp != NULL); + _DIAGASSERT(pw != NULL); + + if (flags) { + inflags = *flags; + *flags = 0; + } else { + inflags = 0; + flags = &inflags; + } + dowarn = !(inflags & _PASSWORD_NOWARN); + + if (!(pw->pw_name = strsep(&bp, ":"))) /* login */ + goto fmt; + if (strlen(pw->pw_name) > (LOGIN_NAME_MAX - 1)) { + if (dowarn) + warnx("username too long, `%s' > %d", pw->pw_name, + LOGIN_NAME_MAX - 1); + return 0; + } + + root = !strcmp(pw->pw_name, "root"); + + if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */ + goto fmt; + + if (!(p = strsep(&bp, ":"))) /* uid */ + goto fmt; + + id = UID_MAX; + if (!getid(&id, p, flags, dowarn, _PASSWORD_NOUID)) + return 0; + + if (root && id) { + if (dowarn) + warnx("root uid should be 0"); + return 0; + } + + pw->pw_uid = (uid_t)id; + + if (!(p = strsep(&bp, ":"))) /* gid */ + goto fmt; + + id = GID_MAX; + if (!getid(&id, p, flags, dowarn, _PASSWORD_NOGID)) + return 0; + + pw->pw_gid = (gid_t)id; + + if (inflags & _PASSWORD_OLDFMT) { + pw->pw_class = __UNCONST(""); + pw->pw_change = 0; + pw->pw_expire = 0; + *flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP); + } else { + pw->pw_class = strsep(&bp, ":"); /* class */ + if (!(p = strsep(&bp, ":"))) /* change */ + goto fmt; + if (!gettime(&ti, p, flags, dowarn, _PASSWORD_NOCHG)) + return 0; + pw->pw_change = ti; + + if (!(p = strsep(&bp, ":"))) /* expire */ + goto fmt; + if (!gettime(&ti, p, flags, dowarn, _PASSWORD_NOEXP)) + return 0; + pw->pw_expire = ti; + } + + pw->pw_gecos = strsep(&bp, ":"); /* gecos */ + pw->pw_dir = strsep(&bp, ":"); /* directory */ + if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */ + goto fmt; + +#if ! HAVE_NBTOOL_CONFIG_H + p = pw->pw_shell; + if (root && *p) /* empty == /bin/sh */ + for (setusershell();;) { + if (!(sh = getusershell())) { + if (dowarn) + warnx("warning, unknown root shell"); + break; + } + if (!strcmp(p, sh)) + break; + } +#endif + + if ((p = strsep(&bp, ":")) != NULL) { /* too many */ +fmt: + if (dowarn) + warnx("corrupted entry"); + return 0; + } + + return 1; +} diff --git a/lib/nbsd_libc/gen/pwcache.3 b/lib/nbsd_libc/gen/pwcache.3 new file mode 100644 index 000000000..c0b679af9 --- /dev/null +++ b/lib/nbsd_libc/gen/pwcache.3 @@ -0,0 +1,220 @@ +.\" $NetBSD: pwcache.3,v 1.17 2008/05/02 18:11:04 martin 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. +.\" +.\" +.\" Copyright (c) 2002 The NetBSD Foundation, Inc. +.\" 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 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. +.\" +.\" +.\" @(#)pwcache.3 8.1 (Berkeley) 6/9/93 +.\" +.Dd January 24, 2002 +.Dt PWCACHE 3 +.Os +.Sh NAME +.Nm pwcache , +.Nm user_from_uid , +.Nm group_from_gid +.Nd cache password and group entries +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In pwd.h +.Ft const char * +.Fn user_from_uid "uid_t uid" "int nouser" +.Ft int +.Fn uid_from_user "const char *name" "uid_t *uid" +.Ft int +.Fn pwcache_userdb "int (*setpassent)(int)" "void (*endpwent)(void)" "struct passwd * (*getpwnam)(const char *)" "struct passwd * (*getpwuid)(uid_t)" +.In grp.h +.Ft const char * +.Fn group_from_gid "gid_t gid" "int nogroup" +.Ft int +.Fn gid_from_group "const char *name" "gid_t *gid" +.Ft int +.Fn pwcache_groupdb "int (*setgroupent)(int)" "void (*endgrent)(void)" "struct group * (*getgrnam)(const char *)" "struct group * (*getgrgid)(gid_t)" +.Sh DESCRIPTION +The +.Fn user_from_uid +function returns the user name associated with the argument +.Fa uid . +The user name is cached so that multiple calls with the same +.Fa uid +do not require additional calls to +.Xr getpwuid 3 . +If there is no user associated with the +.Fa uid , +a pointer is returned +to a string representation of the +.Fa uid , +unless the argument +.Fa nouser +is non-zero, in which case a +.Dv NULL +pointer is returned. +.Pp +The +.Fn group_from_gid +function returns the group name associated with the argument +.Fa gid . +The group name is cached so that multiple calls with the same +.Fa gid +do not require additional calls to +.Xr getgrgid 3 . +If there is no group associated with the +.Fa gid , +a pointer is returned +to a string representation of the +.Fa gid , +unless the argument +.Fa nogroup +is non-zero, in which case a +.Dv NULL +pointer is returned. +.Pp +The +.Fn uid_from_user +function returns the uid associated with the argument +.Fa name . +The uid is cached so that multiple calls with the same +.Fa name +do not require additional calls to +.Xr getpwnam 3 . +If there is no uid associated with the +.Fa name , +the +.Fn uid_from_user +function returns \-1; otherwise it stores the uid at the location pointed to by +.Fa uid +and returns 0. +.Pp +The +.Fn gid_from_group +function returns the gid associated with the argument +.Fa name . +The gid is cached so that multiple calls with the same +.Fa name +do not require additional calls to +.Xr getgrnam 3 . +If there is no gid associated with the +.Fa name , +the +.Fn gid_from_group +function returns \-1; otherwise it stores the gid at the location pointed to by +.Fa gid +and returns 0. +.Pp +The +.Fn pwcache_userdb +function changes the user database access routines which +.Fn user_from_uid +and +.Fn uid_from_user +call to search for users. +The caches are flushed and the existing +.Fn endpwent +method is called before switching to the new routines. +.Fa getpwnam +and +.Fa getpwuid +must be provided, and +.Fa setpassent +and +.Fa endpwent +may be +.Dv NULL +pointers. +.Pp +The +.Fn pwcache_groupdb +function changes the group database access routines which +.Fn group_from_gid +and +.Fn gid_from_group +call to search for groups. +The caches are flushed and the existing +.Fn endgrent +method is called before switching to the new routines. +.Fa getgrnam +and +.Fa getgrgid +must be provided, and +.Fa setgroupent +and +.Fa endgrent +may be +.Dv NULL +pointers. +.Sh SEE ALSO +.Xr getgrgid 3 , +.Xr getgrnam 3 , +.Xr getpwnam 3 , +.Xr getpwuid 3 +.Sh HISTORY +The +.Fn user_from_uid +and +.Fn group_from_gid +functions first appeared in +.Bx 4.4 . +.Pp +The +.Fn uid_from_user +and +.Fn gid_from_group +functions first appeared in +.Nx 1.4 . +.Pp +The +.Fn pwcache_userdb +and +.Fn pwcache_groupdb +functions first appeared in +.Nx 1.6 . diff --git a/lib/nbsd_libc/gen/pwcache.c b/lib/nbsd_libc/gen/pwcache.c new file mode 100644 index 000000000..2caf96a51 --- /dev/null +++ b/lib/nbsd_libc/gen/pwcache.c @@ -0,0 +1,643 @@ +/* $NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $ */ + +/*- + * Copyright (c) 1992 Keith Muller. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Keith Muller of the University of California, San Diego. + * + * 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. + */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * 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 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +/* + * XXX Undefine the renames of these functions so that we don't + * XXX rename the versions found in the host's by mistake! + */ +#undef group_from_gid +#undef user_from_uid +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#if HAVE_NBTOOL_CONFIG_H +/* XXX Now, re-apply the renaming that we undid above. */ +#define group_from_gid __nbcompat_group_from_gid +#define user_from_uid __nbcompat_user_from_uid +#endif + +#ifdef __weak_alias +__weak_alias(user_from_uid,_user_from_uid) +__weak_alias(group_from_gid,_group_from_gid) +__weak_alias(pwcache_groupdb,_pwcache_groupdb) +#endif + +#if !HAVE_PWCACHE_USERDB || HAVE_NBTOOL_CONFIG_H +#include "pwcache.h" + +/* + * routines that control user, group, uid and gid caches (for the archive + * member print routine). + * IMPORTANT: + * these routines cache BOTH hits and misses, a major performance improvement + */ + +/* + * function pointers to various name lookup routines. + * these may be changed as necessary. + */ +static int (*_pwcache_setgroupent)(int) = setgroupent; +static void (*_pwcache_endgrent)(void) = endgrent; +static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam; +static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid; +static int (*_pwcache_setpassent)(int) = setpassent; +static void (*_pwcache_endpwent)(void) = endpwent; +static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam; +static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid; + +/* + * internal state + */ +static int pwopn; /* is password file open */ +static int gropn; /* is group file open */ +static UIDC **uidtb; /* uid to name cache */ +static GIDC **gidtb; /* gid to name cache */ +static UIDC **usrtb; /* user name to uid cache */ +static GIDC **grptb; /* group name to gid cache */ + +static int uidtb_fail; /* uidtb_start() failed ? */ +static int gidtb_fail; /* gidtb_start() failed ? */ +static int usrtb_fail; /* usrtb_start() failed ? */ +static int grptb_fail; /* grptb_start() failed ? */ + + +static u_int st_hash(const char *, size_t, int); +static int uidtb_start(void); +static int gidtb_start(void); +static int usrtb_start(void); +static int grptb_start(void); + + +static u_int +st_hash(const char *name, size_t len, int tabsz) +{ + u_int key = 0; + + _DIAGASSERT(name != NULL); + + while (len--) { + key += *name++; + key = (key << 8) | (key >> 24); + } + + return (key % tabsz); +} + +/* + * uidtb_start + * creates an an empty uidtb + * Return: + * 0 if ok, -1 otherwise + */ +static int +uidtb_start(void) +{ + + if (uidtb != NULL) + return (0); + if (uidtb_fail) + return (-1); + if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) { + ++uidtb_fail; + return (-1); + } + return (0); +} + +/* + * gidtb_start + * creates an an empty gidtb + * Return: + * 0 if ok, -1 otherwise + */ +static int +gidtb_start(void) +{ + + if (gidtb != NULL) + return (0); + if (gidtb_fail) + return (-1); + if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) { + ++gidtb_fail; + return (-1); + } + return (0); +} + +/* + * usrtb_start + * creates an an empty usrtb + * Return: + * 0 if ok, -1 otherwise + */ +static int +usrtb_start(void) +{ + + if (usrtb != NULL) + return (0); + if (usrtb_fail) + return (-1); + if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) { + ++usrtb_fail; + return (-1); + } + return (0); +} + +/* + * grptb_start + * creates an an empty grptb + * Return: + * 0 if ok, -1 otherwise + */ +static int +grptb_start(void) +{ + + if (grptb != NULL) + return (0); + if (grptb_fail) + return (-1); + if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) { + ++grptb_fail; + return (-1); + } + return (0); +} + +/* + * user_from_uid() + * caches the name (if any) for the uid. If noname clear, we always + * return the stored name (if valid or invalid match). + * We use a simple hash table. + * Return + * Pointer to stored name (or a empty string) + */ +const char * +user_from_uid(uid_t uid, int noname) +{ + struct passwd *pw; + UIDC *ptr, **pptr; + + if ((uidtb == NULL) && (uidtb_start() < 0)) + return (NULL); + + /* + * see if we have this uid cached + */ + pptr = uidtb + (uid % UID_SZ); + ptr = *pptr; + + if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) { + /* + * have an entry for this uid + */ + if (!noname || (ptr->valid == VALID)) + return (ptr->name); + return (NULL); + } + + /* + * No entry for this uid, we will add it + */ + if (!pwopn) { + if (_pwcache_setpassent != NULL) + (*_pwcache_setpassent)(1); + ++pwopn; + } + + if (ptr == NULL) + *pptr = ptr = (UIDC *)malloc(sizeof(UIDC)); + + if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) { + /* + * no match for this uid in the local password file + * a string that is the uid in numeric format + */ + if (ptr == NULL) + return (NULL); + ptr->uid = uid; + (void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid); + ptr->valid = INVALID; + if (noname) + return (NULL); + } else { + /* + * there is an entry for this uid in the password file + */ + if (ptr == NULL) + return (pw->pw_name); + ptr->uid = uid; + (void)strlcpy(ptr->name, pw->pw_name, UNMLEN); + ptr->valid = VALID; + } + return (ptr->name); +} + +/* + * group_from_gid() + * caches the name (if any) for the gid. If noname clear, we always + * return the stored name (if valid or invalid match). + * We use a simple hash table. + * Return + * Pointer to stored name (or a empty string) + */ +const char * +group_from_gid(gid_t gid, int noname) +{ + struct group *gr; + GIDC *ptr, **pptr; + + if ((gidtb == NULL) && (gidtb_start() < 0)) + return (NULL); + + /* + * see if we have this gid cached + */ + pptr = gidtb + (gid % GID_SZ); + ptr = *pptr; + + if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { + /* + * have an entry for this gid + */ + if (!noname || (ptr->valid == VALID)) + return (ptr->name); + return (NULL); + } + + /* + * No entry for this gid, we will add it + */ + if (!gropn) { + if (_pwcache_setgroupent != NULL) + (*_pwcache_setgroupent)(1); + ++gropn; + } + + if (ptr == NULL) + *pptr = ptr = (GIDC *)malloc(sizeof(GIDC)); + + if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) { + /* + * no match for this gid in the local group file, put in + * a string that is the gid in numberic format + */ + if (ptr == NULL) + return (NULL); + ptr->gid = gid; + (void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid); + ptr->valid = INVALID; + if (noname) + return (NULL); + } else { + /* + * there is an entry for this group in the group file + */ + if (ptr == NULL) + return (gr->gr_name); + ptr->gid = gid; + (void)strlcpy(ptr->name, gr->gr_name, GNMLEN); + ptr->valid = VALID; + } + return (ptr->name); +} + +/* + * uid_from_user() + * caches the uid for a given user name. We use a simple hash table. + * Return + * the uid (if any) for a user name, or a -1 if no match can be found + */ +int +uid_from_user(const char *name, uid_t *uid) +{ + struct passwd *pw; + UIDC *ptr, **pptr; + size_t namelen; + + /* + * return -1 for mangled names + */ + if (name == NULL || ((namelen = strlen(name)) == 0)) + return (-1); + if ((usrtb == NULL) && (usrtb_start() < 0)) + return (-1); + + /* + * look up in hash table, if found and valid return the uid, + * if found and invalid, return a -1 + */ + pptr = usrtb + st_hash(name, namelen, UNM_SZ); + ptr = *pptr; + + if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { + if (ptr->valid == INVALID) + return (-1); + *uid = ptr->uid; + return (0); + } + + if (!pwopn) { + if (_pwcache_setpassent != NULL) + (*_pwcache_setpassent)(1); + ++pwopn; + } + + if (ptr == NULL) + *pptr = ptr = (UIDC *)malloc(sizeof(UIDC)); + + /* + * no match, look it up, if no match store it as an invalid entry, + * or store the matching uid + */ + if (ptr == NULL) { + if ((pw = (*_pwcache_getpwnam)(name)) == NULL) + return (-1); + *uid = pw->pw_uid; + return (0); + } + (void)strlcpy(ptr->name, name, UNMLEN); + if ((pw = (*_pwcache_getpwnam)(name)) == NULL) { + ptr->valid = INVALID; + return (-1); + } + ptr->valid = VALID; + *uid = ptr->uid = pw->pw_uid; + return (0); +} + +/* + * gid_from_group() + * caches the gid for a given group name. We use a simple hash table. + * Return + * the gid (if any) for a group name, or a -1 if no match can be found + */ +int +gid_from_group(const char *name, gid_t *gid) +{ + struct group *gr; + GIDC *ptr, **pptr; + size_t namelen; + + /* + * return -1 for mangled names + */ + if (name == NULL || ((namelen = strlen(name)) == 0)) + return (-1); + if ((grptb == NULL) && (grptb_start() < 0)) + return (-1); + + /* + * look up in hash table, if found and valid return the uid, + * if found and invalid, return a -1 + */ + pptr = grptb + st_hash(name, namelen, GID_SZ); + ptr = *pptr; + + if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { + if (ptr->valid == INVALID) + return (-1); + *gid = ptr->gid; + return (0); + } + + if (!gropn) { + if (_pwcache_setgroupent != NULL) + (*_pwcache_setgroupent)(1); + ++gropn; + } + + if (ptr == NULL) + *pptr = ptr = (GIDC *)malloc(sizeof(GIDC)); + + /* + * no match, look it up, if no match store it as an invalid entry, + * or store the matching gid + */ + if (ptr == NULL) { + if ((gr = (*_pwcache_getgrnam)(name)) == NULL) + return (-1); + *gid = gr->gr_gid; + return (0); + } + + (void)strlcpy(ptr->name, name, GNMLEN); + if ((gr = (*_pwcache_getgrnam)(name)) == NULL) { + ptr->valid = INVALID; + return (-1); + } + ptr->valid = VALID; + *gid = ptr->gid = gr->gr_gid; + return (0); +} + +#define FLUSHTB(arr, len, fail) \ + do { \ + if (arr != NULL) { \ + for (i = 0; i < len; i++) \ + if (arr[i] != NULL) \ + free(arr[i]); \ + arr = NULL; \ + } \ + fail = 0; \ + } while (/* CONSTCOND */0); + +int +pwcache_userdb( + int (*a_setpassent)(int), + void (*a_endpwent)(void), + struct passwd * (*a_getpwnam)(const char *), + struct passwd * (*a_getpwuid)(uid_t)) +{ + int i; + + /* a_setpassent and a_endpwent may be NULL */ + if (a_getpwnam == NULL || a_getpwuid == NULL) + return (-1); + + if (_pwcache_endpwent != NULL) + (*_pwcache_endpwent)(); + FLUSHTB(uidtb, UID_SZ, uidtb_fail); + FLUSHTB(usrtb, UNM_SZ, usrtb_fail); + pwopn = 0; + _pwcache_setpassent = a_setpassent; + _pwcache_endpwent = a_endpwent; + _pwcache_getpwnam = a_getpwnam; + _pwcache_getpwuid = a_getpwuid; + + return (0); +} + +int +pwcache_groupdb( + int (*a_setgroupent)(int), + void (*a_endgrent)(void), + struct group * (*a_getgrnam)(const char *), + struct group * (*a_getgrgid)(gid_t)) +{ + int i; + + /* a_setgroupent and a_endgrent may be NULL */ + if (a_getgrnam == NULL || a_getgrgid == NULL) + return (-1); + + if (_pwcache_endgrent != NULL) + (*_pwcache_endgrent)(); + FLUSHTB(gidtb, GID_SZ, gidtb_fail); + FLUSHTB(grptb, GNM_SZ, grptb_fail); + gropn = 0; + _pwcache_setgroupent = a_setgroupent; + _pwcache_endgrent = a_endgrent; + _pwcache_getgrnam = a_getgrnam; + _pwcache_getgrgid = a_getgrgid; + + return (0); +} + + +#ifdef TEST_PWCACHE + +struct passwd * +test_getpwnam(const char *name) +{ + static struct passwd foo; + + memset(&foo, 0, sizeof(foo)); + if (strcmp(name, "toor") == 0) { + foo.pw_uid = 666; + return &foo; + } + return (getpwnam(name)); +} + +int +main(int argc, char *argv[]) +{ + uid_t u; + int r, i; + + printf("pass 1 (default userdb)\n"); + for (i = 1; i < argc; i++) { + printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n", + i, pwopn, usrtb_fail, usrtb); + r = uid_from_user(argv[i], &u); + if (r == -1) + printf(" uid_from_user %s: failed\n", argv[i]); + else + printf(" uid_from_user %s: %d\n", argv[i], u); + } + printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n", + pwopn, usrtb_fail, usrtb); + + puts(""); + printf("pass 2 (replacement userdb)\n"); + printf("pwcache_userdb returned %d\n", + pwcache_userdb(setpassent, test_getpwnam, getpwuid)); + printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb); + + for (i = 1; i < argc; i++) { + printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n", + i, pwopn, usrtb_fail, usrtb); + u = -1; + r = uid_from_user(argv[i], &u); + if (r == -1) + printf(" uid_from_user %s: failed\n", argv[i]); + else + printf(" uid_from_user %s: %d\n", argv[i], u); + } + printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n", + pwopn, usrtb_fail, usrtb); + + puts(""); + printf("pass 3 (null pointers)\n"); + printf("pwcache_userdb returned %d\n", + pwcache_userdb(NULL, NULL, NULL)); + + return (0); +} +#endif /* TEST_PWCACHE */ +#endif /* !HAVE_PWCACHE_USERDB */ diff --git a/lib/nbsd_libc/gen/pwcache.h b/lib/nbsd_libc/gen/pwcache.h new file mode 100644 index 000000000..3bd651b60 --- /dev/null +++ b/lib/nbsd_libc/gen/pwcache.h @@ -0,0 +1,72 @@ +/* $NetBSD: pwcache.h,v 1.5 2003/11/10 08:51:51 wiz Exp $ */ + +/*- + * Copyright (c) 1992 Keith Muller. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Keith Muller of the University of California, San Diego. + * + * 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. + * + * @(#)cache.h 8.1 (Berkeley) 5/31/93 + */ + +/* + * Constants and data structures used to implement group and password file + * caches. Traditional passwd/group cache routines perform quite poorly with + * archives. The chances of hitting a valid lookup with an archive is quite a + * bit worse than with files already resident on the file system. These misses + * create a MAJOR performance cost. To address this problem, these routines + * cache both hits and misses. + * + * NOTE: name lengths must be as large as those stored in ANY PROTOCOL and + * as stored in the passwd and group files. CACHE SIZES MUST BE PRIME + */ +#define UNMLEN 32 /* >= user name found in any protocol */ +#define GNMLEN 32 /* >= group name found in any protocol */ +#define UID_SZ 317 /* size of uid to user_name cache */ +#define UNM_SZ 317 /* size of user_name to uid cache */ +#define GID_SZ 251 /* size of gid to group_name cache */ +#define GNM_SZ 251 /* size of group_name to gid cache */ +#define VALID 1 /* entry and name are valid */ +#define INVALID 2 /* entry valid, name NOT valid */ + +/* + * Node structures used in the user, group, uid, and gid caches. + */ + +typedef struct uidc { + int valid; /* is this a valid or a miss entry */ + char name[UNMLEN]; /* uid name */ + uid_t uid; /* cached uid */ +} UIDC; + +typedef struct gidc { + int valid; /* is this a valid or a miss entry */ + char name[GNMLEN]; /* gid name */ + gid_t gid; /* cached gid */ +} GIDC; diff --git a/lib/nbsd_libc/gen/raise.3 b/lib/nbsd_libc/gen/raise.3 new file mode 100644 index 000000000..1b3b81463 --- /dev/null +++ b/lib/nbsd_libc/gen/raise.3 @@ -0,0 +1,79 @@ +.\" $NetBSD: raise.3,v 1.9 2009/01/30 23:52:28 wiz Exp $ +.\" +.\" 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. +.\" +.\" @(#)raise.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd January 29, 2009 +.Dt RAISE 3 +.Os +.Sh NAME +.Nm raise +.Nd send a signal to the current thread +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn raise "int sig" +.Sh DESCRIPTION +The +.Fn raise +function sends the signal +.Fa sig +to the current thread. +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. +Otherwise, a value of \-1 is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn raise +function +may fail and set +.Va errno +for any of the errors specified for the +library functions +.Xr _lwp_self 2 +and +.Xr _lwp_kill 2 . +.Sh SEE ALSO +.Xr kill 2 +.Sh STANDARDS +The +.Fn raise +function +conforms to +.St -ansiC +and +.St -susv2 . diff --git a/lib/nbsd_libc/gen/raise.c b/lib/nbsd_libc/gen/raise.c new file mode 100644 index 000000000..2c77d665c --- /dev/null +++ b/lib/nbsd_libc/gen/raise.c @@ -0,0 +1,49 @@ +/* $NetBSD: raise.c,v 1.8 2009/01/29 23:52:21 rmind 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)raise.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: raise.c,v 1.8 2009/01/29 23:52:21 rmind Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +int +raise(int s) +{ + + return _lwp_kill(_lwp_self(), s); +} diff --git a/lib/nbsd_libc/gen/randomid.3 b/lib/nbsd_libc/gen/randomid.3 new file mode 100644 index 000000000..dbb31cef6 --- /dev/null +++ b/lib/nbsd_libc/gen/randomid.3 @@ -0,0 +1,148 @@ +.\" $NetBSD: randomid.3,v 1.7 2006/01/05 19:45:29 rpaulo Exp $ +.\" +.\" Copyright (C) 2006 The NetBSD Foundation +.\" All rights reserved. +.\" +.\" Copyright (C) 2003 WIDE Project. +.\" 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 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 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 PROJECT 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 January 5, 2006 +.Dt RANDOMID 3 +.Os +.Sh NAME +.Nm randomid +.Nm randomid_new , +.Nm randomid_delete , +.Nd provide pseudo-random data stream without repetitions +.Sh SYNOPSIS +.In sys/types.h +.In randomid.h +.Ft uint32_t +.Fn randomid "randomid_t ctx" +.Ft randomid_t +.Fn randomid_new "int bits" "long timeo" +.Ft void +.Fn randomid_delete "randomid_t ctx" +.Sh DESCRIPTION +The +.Fn randomid +function provides pseudo-random data stream, +which is guaranteed not to generate the same number twice during +a certain duration. +.Fa ctx +is the context which holds internal state for the random number generator. +.Pp +To initialize a context, +.Fa randomid_new +is used. +.Fa bits +specifies the bitwidth of the value generated by +.Fn randomid . +Currently 32, 20, and 16 are supported. +.Fa timeo +specifies the reinitialization interval in seconds. +.Fa timeo +has to be bigger than +.Dv RANDOMID_TIMEO_MIN . +.Fa randomid_new +returns a dynamically-allocated memory region allocated by +.Xr malloc 3 . +.Pp +.Fn randomid_delete +will +.Xr free 3 +the internal state +.Fa ctx . +.Pp +The same number may appear after two reinitialization events of the internal state, +.Fa ctx . +Reinitialization happens when the random number generator cycle is exhausted, +or +.Fa timeo +seconds have passed since the last reinitialization. +For instance, +.Fa ctx +configured to generate 16 bit data stream will reinitialize its internal state +every 30000 calls to +.Fn randomid +.Po +or after +.Fa timeo +seconds +.Pc , +therefore the same data will not appear until after 30000 calls to +.Fn randomid +.Po +or after +.Fa timeo +seconds +.Pc . +.Pp +The internal state, +.Fa ctx , +determines the data stream generated by +.Fn randomid . +.Fa ctx +must be allocated per data stream +.Pq such as a specific data field . +It must not be shared among multiple data streams with different usage. +.\" +.Sh EXAMPLES +.Bd -literal -offset indent +#include \*[Lt]stdio.h\*[Gt] +#include \*[Lt]sys/types.h\*[Gt] +#include \*[Lt]randomid.h\*[Gt] + +uint32_t +genid(void) +{ + static randomid_t ctx = NULL; + + if (!ctx) + ctx = randomid_new(16, (long)3600); + return randomid(ctx); +} +.Ed +.\" +.Sh ERRORS +.Fn randomid_new +returns +.Dv NULL +on error and sets the external variable +.Va errno . +.\" +.Sh SEE ALSO +.Xr arc4random 3 +.\" +.Sh HISTORY +The pseudo-random data stream generator was designed by Niels Provos for +.Ox +IPv4 fragment ID generation. +.Fn randomid +is a generalized version of the generator, reworked by Jun-ichiro itojun Hagino, +and was introduced in +.Nx 2.0 . diff --git a/lib/nbsd_libc/gen/randomid.c b/lib/nbsd_libc/gen/randomid.c new file mode 100644 index 000000000..4eb9a3b32 --- /dev/null +++ b/lib/nbsd_libc/gen/randomid.c @@ -0,0 +1,324 @@ +/* $NetBSD: randomid.c,v 1.13 2009/01/11 02:46:27 christos Exp $ */ +/* $KAME: ip6_id.c,v 1.8 2003/09/06 13:41:06 itojun Exp $ */ +/* $OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $ */ + +/* + * Copyright (C) 2003 WIDE Project. + * 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 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 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 PROJECT 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. + */ + +/* + * Copyright 1998 Niels Provos + * All rights reserved. + * + * Theo de Raadt came up with the idea of using + * such a mathematical system to generate more random (yet non-repeating) + * ids to solve the resolver/named problem. But Niels designed the + * actual system based on the constraints. + * + * 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 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. + */ + +/* + * seed = random (bits - 1) bit + * n = prime, g0 = generator to n, + * j = random so that gcd(j,n-1) == 1 + * g = g0^j mod n will be a generator again. + * + * X[0] = random seed. + * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator + * with a = 7^(even random) mod m, + * b = random with gcd(b,m) == 1 + * m = constant and a maximal period of m-1. + * + * The transaction id is determined by: + * id[n] = seed xor (g^X[n] mod n) + * + * Effectivly the id is restricted to the lower (bits - 1) bits, thus + * yielding two different cycles by toggling the msb on and off. + * This avoids reuse issues caused by reseeding. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: randomid.c,v 1.13 2009/01/11 02:46:27 christos Exp $"); +#endif + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(randomid,_randomid) +__weak_alias(randomid_new,_randomid_new) +__weak_alias(randomid_delete,_randomid_delete) +#endif + +struct randomconf { + const int rc_bits; /* resulting bits */ + const u_int32_t rc_max; /* Uniq cycle, avoid blackjack prediction */ + const u_int32_t rc_gen; /* Starting generator */ + const u_int32_t rc_n; /* ru_n: prime, ru_n - 1: product of pfacts[] */ + const u_int32_t rc_agen; /* determine ru_a as ru_agen^(2*rand) */ + const u_int32_t rc_m; /* ru_m = 2^x*3^y */ + const u_int32_t rc_pfacts[4]; /* factors of ru_n */ + const int rc_skip; /* skip values */ +}; + +struct randomid_ctx { + struct randomconf *ru_conf; +#define ru_bits ru_conf->rc_bits +#define ru_max ru_conf->rc_max +#define ru_gen ru_conf->rc_gen +#define ru_n ru_conf->rc_n +#define ru_agen ru_conf->rc_agen +#define ru_m ru_conf->rc_m +#define ru_pfacts ru_conf->rc_pfacts +#define ru_skip ru_conf->rc_skip + long ru_out; /* Time after wich will be reseeded */ + u_int32_t ru_counter; + u_int32_t ru_msb; + + u_int32_t ru_x; + u_int32_t ru_seed, ru_seed2; + u_int32_t ru_a, ru_b; + u_int32_t ru_g; + time_t ru_reseed; +}; + +static struct randomconf randomconf[] = { + { + 32, /* resulting bits */ + 1000000000, /* Uniq cycle, avoid blackjack prediction */ + 2, /* Starting generator */ + 2147483629, /* RU_N-1 = 2^2*3^2*59652323 */ + 7, /* determine ru_a as RU_AGEN^(2*rand) */ + 1836660096, /* RU_M = 2^7*3^15 - don't change */ + { 2, 3, 59652323, 0 }, /* factors of ru_n */ + 3, /* skip values */ + }, + { + 20, /* resulting bits */ + 200000, /* Uniq cycle, avoid blackjack prediction */ + 2, /* Starting generator */ + 524269, /* RU_N-1 = 2^2*3^2*14563 */ + 7, /* determine ru_a as RU_AGEN^(2*rand) */ + 279936, /* RU_M = 2^7*3^7 - don't change */ + { 2, 3, 14563, 0 }, /* factors of ru_n */ + 3, /* skip values */ + }, + { + 16, /* resulting bits */ + 30000, /* Uniq cycle, avoid blackjack prediction */ + 2, /* Starting generator */ + 32749, /* RU_N-1 = 2^2*3*2729 */ + 7, /* determine ru_a as RU_AGEN^(2*rand) */ + 31104, /* RU_M = 2^7*3^5 - don't change */ + { 2, 3, 2729, 0 }, /* factors of ru_n */ + 0, /* skip values */ + }, + { + .rc_bits = -1, /* termination */ + }, +}; + +static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t); +static void initid(struct randomid_ctx *); + +struct randomid_ctx *randomid_new(int, long); +void randomid_delete(struct randomid_ctx *); +u_int32_t randomid(struct randomid_ctx *); + +/* + * Do a fast modular exponation, returned value will be in the range + * of 0 - (mod-1) + */ + +static u_int32_t +pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod) +{ + u_int64_t s, t, u; + + s = 1; + t = gen; + u = expo; + + while (u) { + if (u & 1) + s = (s * t) % mod; + u >>= 1; + t = (t * t) % mod; + } + return ((u_int32_t)s & UINT32_MAX); +} + +/* + * Initalizes the seed and chooses a suitable generator. Also toggles + * the msb flag. The msb flag is used to generate two distinct + * cycles of random numbers and thus avoiding reuse of ids. + * + * This function is called from id_randomid() when needed, an + * application does not have to worry about it. + */ +static void +initid(struct randomid_ctx *p) +{ + u_int32_t j, i; + int noprime = 1; + struct timeval tv; + + p->ru_x = arc4random() % p->ru_m; + + /* (bits - 1) bits of random seed */ + p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1)); + p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1)); + + /* Determine the LCG we use */ + p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1; + p->ru_a = pmod(p->ru_agen, + (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m); + while (p->ru_b % 3 == 0) + p->ru_b += 2; + + j = arc4random() % p->ru_n; + + /* + * Do a fast gcd(j, RU_N - 1), so we can find a j with + * gcd(j, RU_N - 1) == 1, giving a new generator for + * RU_GEN^j mod RU_N + */ + while (noprime) { + for (i = 0; p->ru_pfacts[i] > 0; i++) + if (j % p->ru_pfacts[i] == 0) + break; + + if (p->ru_pfacts[i] == 0) + noprime = 0; + else + j = (j + 1) % p->ru_n; + } + + p->ru_g = pmod(p->ru_gen, j, p->ru_n); + p->ru_counter = 0; + + gettimeofday(&tv, NULL); + p->ru_reseed = tv.tv_sec + p->ru_out; + p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1)); +} + +struct randomid_ctx * +randomid_new(int bits, long timeo) +{ + struct randomconf *conf; + struct randomid_ctx *ctx; + + if (timeo < RANDOMID_TIMEO_MIN) { + errno = EINVAL; + return (NULL); + } + + for (conf = randomconf; conf->rc_bits > 0; conf++) { + if (bits == conf->rc_bits) + break; + } + + /* unsupported bits */ + if (bits != conf->rc_bits) { + errno = ENOTSUP; + return (NULL); + } + + ctx = malloc(sizeof(*ctx)); + if (!ctx) + return (NULL); + + memset(ctx, 0, sizeof(*ctx)); + ctx->ru_conf = conf; + ctx->ru_out = timeo; + + return (ctx); +} + +void +randomid_delete(struct randomid_ctx *ctx) +{ + + memset(ctx, 0, sizeof(*ctx)); + free(ctx); +} + +u_int32_t +randomid(struct randomid_ctx *p) +{ + int i, n; + struct timeval tv; + + gettimeofday(&tv, NULL); + if (p->ru_counter >= p->ru_max || tv.tv_sec > p->ru_reseed) + initid(p); + + /* Skip a random number of ids */ + if (p->ru_skip) { + n = arc4random() & p->ru_skip; + if (p->ru_counter + n >= p->ru_max) + initid(p); + } else + n = 0; + + for (i = 0; i <= n; i++) { + /* Linear Congruential Generator */ + p->ru_x = (u_int32_t)(((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m); + } + + p->ru_counter += i; + + return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) | + p->ru_msb; +} diff --git a/lib/nbsd_libc/gen/readdir.c b/lib/nbsd_libc/gen/readdir.c new file mode 100644 index 000000000..13c3eb034 --- /dev/null +++ b/lib/nbsd_libc/gen/readdir.c @@ -0,0 +1,143 @@ +/* $NetBSD: readdir.c,v 1.25 2010/09/16 02:38:50 yamt Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)readdir.c 8.3 (Berkeley) 9/29/94"; +#else +__RCSID("$NetBSD: readdir.c,v 1.25 2010/09/16 02:38:50 yamt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include "extern.h" +#include + +#include +#include +#include +#include + +#include "dirent_private.h" + +/* + * get next entry in a directory. + */ +struct dirent * +_readdir_unlocked(DIR *dirp, int skipdeleted) +{ + struct dirent *dp; + + for (;;) { + if (dirp->dd_loc >= dirp->dd_size) { + if (dirp->dd_flags & __DTF_READALL) + return (NULL); + dirp->dd_loc = 0; + } + if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) { + dirp->dd_seek = lseek(dirp->dd_fd, (off_t)0, SEEK_CUR); + dirp->dd_size = getdents(dirp->dd_fd, + dirp->dd_buf, (size_t)dirp->dd_len); + if (dirp->dd_size <= 0) + return (NULL); + } + dp = (struct dirent *) + (void *)(dirp->dd_buf + (size_t)dirp->dd_loc); + if ((intptr_t)dp & _DIRENT_ALIGN(dp))/* bogus pointer check */ + return (NULL); + /* d_reclen is unsigned; no need to compare it <= 0 */ + if (dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) + return (NULL); + dirp->dd_loc += dp->d_reclen; + if (dp->d_ino == 0 && skipdeleted) + continue; + if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW)) + continue; + return (dp); + } +} + +struct dirent * +readdir(dirp) + DIR *dirp; +{ + struct dirent *dp; + +#ifdef _REENTRANT + if (__isthreaded) { + mutex_lock((mutex_t *)dirp->dd_lock); + dp = _readdir_unlocked(dirp, 1); + mutex_unlock((mutex_t *)dirp->dd_lock); + } + else +#endif + dp = _readdir_unlocked(dirp, 1); + return (dp); +} + +int +readdir_r(dirp, entry, result) + DIR *dirp; + struct dirent *entry; + struct dirent **result; +{ + struct dirent *dp; + int saved_errno; + + saved_errno = errno; + errno = 0; +#ifdef _REENTRANT + if (__isthreaded) { + mutex_lock((mutex_t *)dirp->dd_lock); + if ((dp = _readdir_unlocked(dirp, 1)) != NULL) + memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp)); + mutex_unlock((mutex_t *)dirp->dd_lock); + } + else +#endif + if ((dp = _readdir_unlocked(dirp, 1)) != NULL) + memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp)); + + if (errno != 0) { + if (dp == NULL) + return (errno); + } else + errno = saved_errno; + + if (dp != NULL) + *result = entry; + else + *result = NULL; + + return (0); +} diff --git a/lib/nbsd_libc/gen/realpath.3 b/lib/nbsd_libc/gen/realpath.3 new file mode 100644 index 000000000..37cff784b --- /dev/null +++ b/lib/nbsd_libc/gen/realpath.3 @@ -0,0 +1,120 @@ +.\" $NetBSD: realpath.3,v 1.12 2005/08/13 19:53:53 elad Exp $ +.\" +.\" 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. +.\" +.\" from: @(#)realpath.3 8.2 (Berkeley) 2/16/94 +.\" +.Dd August 13, 2005 +.Dt REALPATH 3 +.Os +.Sh NAME +.Nm realpath +.Nd returns the canonicalized absolute pathname +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/param.h +.In stdlib.h +.Ft "char *" +.Fn realpath "const char *pathname" "char resolvedname[MAXPATHLEN]" +.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 resolvedname . +The +.Fa resolvedname +argument +.Em must +refer to a buffer capable of storing at least +.Dv MAXPATHLEN +characters. +.Pp +The +.Fn realpath +function will resolve both absolute and relative paths +and return the absolute pathname corresponding to +.Fa pathname . +.Sh RETURN VALUES +The +.Fn realpath +function returns +.Fa resolvedname +on success. +If an error occurs, +.Fn realpath +returns +.Dv NULL , +and +.Fa resolvedname +contains the pathname which caused the problem. +.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 chdir 2 , +.Xr close 2 , +.Xr fchdir 2 , +.Xr lstat 2 , +.Xr open 2 , +.Xr readlink 2 +and +.Xr getcwd 3 . +.Sh SEE ALSO +.Xr getcwd 3 +.Sh HISTORY +The +.Fn realpath +function call first appeared in +.Bx 4.4 . +.Sh BUGS +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 resolvedname +when given a relative +.Fa pathname . diff --git a/lib/nbsd_libc/gen/rewinddir.c b/lib/nbsd_libc/gen/rewinddir.c new file mode 100644 index 000000000..a16685247 --- /dev/null +++ b/lib/nbsd_libc/gen/rewinddir.c @@ -0,0 +1,74 @@ +/* $NetBSD: rewinddir.c,v 1.13 2010/09/26 02:26:59 yamt 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)rewinddir.c 8.1 (Berkeley) 6/8/93"; +#else +__RCSID("$NetBSD: rewinddir.c,v 1.13 2010/09/26 02:26:59 yamt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include "extern.h" +#include + +#include +#include + +#include "dirent_private.h" + +#ifdef __weak_alias +__weak_alias(rewinddir,_rewinddir) +#endif + +void +rewinddir(DIR *dirp) +{ + int fd; + +#ifdef _REENTRANT + if (__isthreaded) { + mutex_lock((mutex_t *)dirp->dd_lock); + } +#endif + fd = dirp->dd_fd; + _finidir(dirp); + dirp->dd_seek = lseek(fd, (off_t)0, SEEK_SET); + _initdir(dirp, fd, NULL); +#ifdef _REENTRANT + if (__isthreaded) { + mutex_unlock((mutex_t *)dirp->dd_lock); + } +#endif +} diff --git a/lib/nbsd_libc/gen/scandir.3 b/lib/nbsd_libc/gen/scandir.3 new file mode 100644 index 000000000..01d88b2c9 --- /dev/null +++ b/lib/nbsd_libc/gen/scandir.3 @@ -0,0 +1,113 @@ +.\" $NetBSD: scandir.3,v 1.14 2010/12/17 19:20:42 njoly Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)scandir.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd April 29, 2010 +.Dt SCANDIR 3 +.Os +.Sh NAME +.Nm scandir , +.Nm alphasort +.Nd scan a directory +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In dirent.h +.Ft int +.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp" "int \*(lp*compar\*(rp\*(lpconst void *, const void *\*(rp" +.Ft int +.Fn alphasort "const void *d1" "const void *d2" +.Sh DESCRIPTION +The +.Fn scandir +function +reads the directory +.Fa dirname +and builds an array of pointers to directory +entries using +.Xr malloc 3 . +It returns the number of entries in the array. +A pointer to the array of directory entries is stored in the location +referenced by +.Fa namelist . +.Pp +The +.Fa select +parameter is a pointer to a user supplied subroutine which is called by +.Fn scandir +to select which entries are to be included in the array. +The select routine is passed a +pointer to a directory entry and should return a non-zero +value if the directory entry is to be included in the array. +If +.Fa select +is null, then all the directory entries will be included. +.Pp +The +.Fa compar +parameter is a pointer to a user supplied subroutine which is passed to +.Xr qsort 3 +to sort the completed array. +If this pointer is null, the array is not sorted. +.Pp +The +.Fn alphasort +function +is a routine which can be used for the +.Fa compar +parameter to sort the array alphabetically. +.Pp +The memory allocated for the array can be deallocated with +.Xr free 3 , +by freeing each pointer in the array and then the array itself. +.Sh DIAGNOSTICS +Returns \-1 if the directory cannot be opened for reading or if +.Xr malloc 3 +cannot allocate enough memory to hold all the data structures. +.Sh SEE ALSO +.Xr directory 3 , +.Xr dirent 3 , +.Xr malloc 3 , +.Xr qsort 3 +.Sh STANDARDS +The +.Fn scandir +and +.Fn alphasort +functions conform to +.St -p1003.1-2008 . +.Sh HISTORY +The +.Fn scandir +and +.Fn alphasort +functions appeared in +.Bx 4.2 . diff --git a/lib/nbsd_libc/gen/scandir.c b/lib/nbsd_libc/gen/scandir.c new file mode 100644 index 000000000..d89442936 --- /dev/null +++ b/lib/nbsd_libc/gen/scandir.c @@ -0,0 +1,151 @@ +/* $NetBSD: scandir.c,v 1.26 2007/06/09 23:57:25 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94"; +#else +__RCSID("$NetBSD: scandir.c,v 1.26 2007/06/09 23:57:25 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Scan the directory dirname calling selectfn to make a list of selected + * directory entries then sort using qsort and compare routine dcomp. + * Returns the number of entries and a pointer to a list of pointers to + * struct dirent (through namelist). Returns -1 if there were any errors. + */ + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include + +/* + * Compute an estimate of the number of entries in a directory based on + * the file size. Returns the estimated number of entries or 0 on failure. + */ +static size_t +dirsize(int fd, size_t olen) +{ + struct stat stb; + size_t nlen; + + if (fstat(fd, &stb) == -1) + return 0; + /* + * Estimate the array size by taking the size of the directory file + * and dividing it by a multiple of the minimum size entry. + */ + nlen = (size_t)(stb.st_size / _DIRENT_MINSIZE((struct dirent *)0)); + /* + * If the size turns up 0, switch to an alternate strategy and use the + * file size as the number of entries like ZFS returns. If that turns + * out to be 0 too return a minimum of 10 entries, plus the old length. + */ + if (nlen == 0) + nlen = (size_t)(stb.st_size ? stb.st_size : 10); + return olen + nlen; +} + +int +scandir(const char *dirname, struct dirent ***namelist, + int (*selectfn)(const struct dirent *), + int (*dcomp)(const void *, const void *)) +{ + struct dirent *d, *p, **names, **newnames; + size_t nitems, arraysz; + DIR *dirp; + + _DIAGASSERT(dirname != NULL); + _DIAGASSERT(namelist != NULL); + + if ((dirp = opendir(dirname)) == NULL) + return -1; + + if ((arraysz = dirsize(dirp->dd_fd, 0)) == 0) + goto bad; + + names = malloc(arraysz * sizeof(*names)); + if (names == NULL) + goto bad; + + nitems = 0; + while ((d = readdir(dirp)) != NULL) { + if (selectfn != NULL && !(*selectfn)(d)) + continue; /* just selected names */ + + /* + * Check to make sure the array has space left and + * realloc the maximum size. + */ + if (nitems >= arraysz) { + if ((arraysz = dirsize(dirp->dd_fd, arraysz)) == 0) + goto bad2; + newnames = realloc(names, arraysz * sizeof(*names)); + if (newnames == NULL) + goto bad2; + names = newnames; + } + + /* + * Make a minimum size copy of the data + */ + p = malloc((size_t)_DIRENT_SIZE(d)); + if (p == NULL) + goto bad2; + p->d_fileno = d->d_fileno; + p->d_reclen = d->d_reclen; + p->d_type = d->d_type; + p->d_namlen = d->d_namlen; + (void)memmove(p->d_name, d->d_name, (size_t)(p->d_namlen + 1)); + names[nitems++] = p; + } + (void)closedir(dirp); + if (nitems && dcomp != NULL) + qsort(names, nitems, sizeof(*names), dcomp); + *namelist = names; + return nitems; + +bad2: + while (nitems-- > 0) + free(names[nitems]); + free(names); +bad: + (void)closedir(dirp); + return -1; +} diff --git a/lib/nbsd_libc/gen/seekdir.c b/lib/nbsd_libc/gen/seekdir.c new file mode 100644 index 000000000..e70eba635 --- /dev/null +++ b/lib/nbsd_libc/gen/seekdir.c @@ -0,0 +1,70 @@ +/* $NetBSD: seekdir.c,v 1.14 2006/05/17 20:36:50 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)seekdir.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: seekdir.c,v 1.14 2006/05/17 20:36:50 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include "extern.h" +#include + +#include + +#include "dirent_private.h" + +#ifdef __weak_alias +__weak_alias(seekdir,_seekdir) +#endif + +/* + * Seek to an entry in a directory. + * _seekdir_unlocked is in telldir.c so that it can share opaque data structures. + */ +void +seekdir(DIR *dirp, long loc) +{ + +#ifdef _REENTRANT + if (__isthreaded) { + mutex_lock((mutex_t *)dirp->dd_lock); + _seekdir_unlocked(dirp, loc); + mutex_unlock((mutex_t *)dirp->dd_lock); + } else +#endif + _seekdir_unlocked(dirp, loc); +} diff --git a/lib/nbsd_libc/gen/setdomainname.c b/lib/nbsd_libc/gen/setdomainname.c new file mode 100644 index 000000000..8ec82bb46 --- /dev/null +++ b/lib/nbsd_libc/gen/setdomainname.c @@ -0,0 +1,67 @@ +/* $NetBSD: setdomainname.c,v 1.12 2003/08/07 16:42:56 agc Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sethostname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: setdomainname.c,v 1.12 2003/08/07 16:42:56 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(setdomainname,_setdomainname) +#endif + +int +setdomainname(name, namelen) + const char *name; + size_t namelen; +{ + int mib[2]; + + _DIAGASSERT(name != NULL); + + mib[0] = CTL_KERN; + mib[1] = KERN_DOMAINNAME; + if (sysctl(mib, 2, NULL, NULL, name, namelen) == -1) + return (-1); + return (0); +} diff --git a/lib/nbsd_libc/gen/sethostname.c b/lib/nbsd_libc/gen/sethostname.c new file mode 100644 index 000000000..9ff569fd4 --- /dev/null +++ b/lib/nbsd_libc/gen/sethostname.c @@ -0,0 +1,67 @@ +/* $NetBSD: sethostname.c,v 1.12 2003/08/07 16:42:56 agc Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sethostname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: sethostname.c,v 1.12 2003/08/07 16:42:56 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(sethostname,_sethostname) +#endif + +int +sethostname(name, namelen) + const char *name; + size_t namelen; +{ + int mib[2]; + + _DIAGASSERT(name != NULL); + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTNAME; + if (sysctl(mib, 2, NULL, NULL, name, namelen) == -1) + return (-1); + return (0); +} diff --git a/lib/nbsd_libc/gen/setjmp.3 b/lib/nbsd_libc/gen/setjmp.3 new file mode 100644 index 000000000..2c4b38961 --- /dev/null +++ b/lib/nbsd_libc/gen/setjmp.3 @@ -0,0 +1,229 @@ +.\" $NetBSD: setjmp.3,v 1.17 2008/05/31 16:15:07 enami Exp $ +.\" +.\" 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. +.\" +.\" @(#)setjmp.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 1, 2008 +.Dt SETJMP 3 +.Os +.Sh NAME +.Nm sigsetjmp , +.Nm siglongjmp , +.Nm setjmp , +.Nm longjmp , +.Nm _setjmp , +.Nm _longjmp , +.Nm longjmperror +.Nd non-local jumps +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In setjmp.h +.Ft int +.Fn sigsetjmp "sigjmp_buf env" "int savemask" +.Ft void +.Fn siglongjmp "sigjmp_buf env" "int val" +.Ft int +.Fn setjmp "jmp_buf env" +.Ft void +.Fn longjmp "jmp_buf env" "int val" +.Ft int +.Fn _setjmp "jmp_buf env" +.Ft void +.Fn _longjmp "jmp_buf env" "int val" +.Ft void +.Fn longjmperror void +.Sh DESCRIPTION +The +.Fn sigsetjmp , +.Fn setjmp , +and +.Fn _setjmp +functions save their calling environment in +.Fa env . +Each of these functions returns 0. +.Pp +The corresponding +.Fn longjmp +functions restore the environment saved by the most recent +invocation of the respective +.Fn setjmp +function. +They then return so that program execution continues as if the corresponding +invocation of the +.Fn setjmp +call had just returned the value specified by +.Fa val , +instead of 0. +.Pp +Pairs of calls may be intermixed, i.e., both +.Fn sigsetjmp +and +.Fn siglongjmp +as well as +.Fn setjmp +and +.Fn longjmp +combinations may be used in the same program. +However, individual calls may not, e.g., the +.Fa env +argument to +.Fn setjmp +may not be passed to +.Fn siglongjmp . +.Pp +The +.Fn longjmp +routines may not be called after the routine which called the +.Fn setjmp +routines returns. +.Pp +All accessible objects have values as of the time +.Fn longjmp +routine was called, except that the values of objects of automatic storage +invocation duration that do not have the +.Li volatile +type and have been changed between the +.Fn setjmp +invocation and +.Fn longjmp +call are indeterminate. +.Pp +The +.Fn setjmp Ns / Ns Fn longjmp +function pairs save and restore the signal mask while +.Fn _setjmp Ns / Ns Fn _longjmp +function pairs save and restore only the register set and the stack. +(See +.Fn sigprocmask 2 . ) +.Pp +The +.Fn sigsetjmp Ns / Ns Fn siglongjmp +function pairs save and restore the signal mask if the argument +.Fa savemask +is non-zero. +Otherwise, only the register set and the stack are saved. +.Pp +In other words, +.Fn setjmp Ns / Ns Fn longjmp +are functionally equivalent to +.Fn sigsetjmp Ns / Ns Fn siglongjmp +when +.Fn sigsetjmp +is called with a non-zero +.Fa savemask +argument. +Conversely, +.Fn _setjmp Ns / Ns Fn _longjmp +are functionally equivalent to +.Fn sigsetjmp Ns / Ns Fn siglongjmp +when +.Fn sigsetjmp +is called with a zero-value +.Fa savemask . +.Pp +The +.Fn sigsetjmp Ns / Ns Fn siglongjmp +interfaces are preferred for maximum portability. +.Sh ERRORS +If the contents of the +.Fa env +are corrupted or correspond to an environment that has already returned, +the +.Fn longjmp +routine calls the routine +.Xr longjmperror 3 . +If +.Fn longjmperror +returns, the program is aborted (see +.Xr abort 3 ) . +The default version of +.Fn longjmperror +prints the message +.Dq Li longjmp botch +to standard error and returns. +User programs wishing to exit more gracefully should write their own +versions of +.Fn longjmperror . +.Sh SEE ALSO +.Xr sigaction 2 , +.Xr sigaltstack 2 , +.Xr sigprocmask 2 , +.Xr pthread_sigmask 3 , +.Xr signal 3 +.Sh STANDARDS +The +.Fn setjmp +and +.Fn longjmp +functions conform to +.St -ansiC . +The +.Fn sigsetjmp +and +.Fn siglongjmp +functions conform to +.St -p1003.1-90 . +.Sh CAVEATS +Historically, on +.At V , +the +.Fn setjmp Ns / Ns Fn longjmp +functions have been equivalent to the +.Bx +.Fn _setjmp Ns / Ns Fn _longjmp +functions and do not restore the signal mask. +Because of this discrepancy, the +.Fn sigsetjmp Ns / Ns Fn siglongjmp +interfaces should be used if portability is desired. +.Pp +Use of +.Fn longjmp +or +.Fn siglongjmp +from inside a signal handler is not as easy as it might seem. +Generally speaking, all possible code paths between the +.Fn setjmp +and +.Fn longjmp +must be signal race safe. +Furthermore, the code paths must not do resource management +(such as +.Xr open 2 +or +.Xr close 2 ) +without blocking the signal in question, or resources might +be mismanaged. +Obviously this makes +.Fn longjmp +much less useful than previously thought. diff --git a/lib/nbsd_libc/gen/setjmperr.c b/lib/nbsd_libc/gen/setjmperr.c new file mode 100644 index 000000000..f47eb8780 --- /dev/null +++ b/lib/nbsd_libc/gen/setjmperr.c @@ -0,0 +1,56 @@ +/* $NetBSD: setjmperr.c,v 1.7 2003/08/07 16:42:56 agc Exp $ */ + +/* + * Copyright (c) 1980, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)setjmperr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: setjmperr.c,v 1.7 2003/08/07 16:42:56 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * This routine is called from longjmp() when an error occurs. + * Programs that wish to exit gracefully from this error may + * write their own versions. + * If this routine returns, the program is aborted. + */ + +#include +#include + +void +longjmperror() +{ +#define ERRMSG "longjmp botch.\n" + (void)write(STDERR_FILENO, ERRMSG, sizeof(ERRMSG) - 1); +} diff --git a/lib/nbsd_libc/gen/setmode.3 b/lib/nbsd_libc/gen/setmode.3 new file mode 100644 index 000000000..9c131d25e --- /dev/null +++ b/lib/nbsd_libc/gen/setmode.3 @@ -0,0 +1,150 @@ +.\" $NetBSD: setmode.3,v 1.21 2009/01/11 02:46:27 christos 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. +.\" +.\" @(#)setmode.3 8.2 (Berkeley) 4/28/95 +.\" +.Dd January 4, 2009 +.Dt SETMODE 3 +.Os +.Sh NAME +.Nm getmode , +.Nm setmode +.Nd modify mode bits +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft void * +.Fn setmode "const char *mode_str" +.Ft mode_t +.Fn getmode "const void *set" "mode_t mode" +.Sh DESCRIPTION +The +.Fn setmode +function accepts a string representation of a file mode change, +compiles it to binary form, and returns an abstract representation +that may be passed to +.Fn getmode . +The string may be an numeric (octal) or symbolic string of the form +accepted by +.Xr chmod 1 , +and may represent either an exact mode to set or a change to make to +the existing mode. +.Pp +The +.Fn getmode +function +adjusts the file permission bits given by +.Fa mode +according to the compiled change representation +.Fa set , +and returns the adjusted mode. +While only the permission bits are altered, other parts of the file +mode, particularly the type, may be examined. +.Pp +Because some of the possible symbolic values are defined relative to +the file creation mask, +.Fn setmode +may call +.Xr umask 2 , +temporarily changing the mask. +If this occurs, the file creation mask will be restored before +.Fn setmode +returns. +If the calling program changes the value of its file creation mask +after calling +.Fn setmode , +.Fn setmode +must be called again to recompile the mode string if +.Fn getmode +is to modify future file modes correctly. +.Pp +If the mode passed to +.Fn setmode +is invalid, +.Fn setmode +returns +.Dv NULL . +.Sh EXAMPLES +The effects of the shell command +.Ql "chmod a+x myscript.sh" +can be duplicated as follows: +.Bd -literal -offset indent +const char *file = "myscript.sh"; +struct stat st; +mode_t newmode; + +stat(file, \*[Am]st); +newmode = getmode(setmode("a+x"), st.st_mode); +chmod(file, newmode); +.Ed +.Sh ERRORS +The +.Fn setmode +function +may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr malloc 3 +or +.Xr strtol 3 . +In addition, +.Fn setmode +will fail and set +.Va errno +to: +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Fa mode +argument does not represent a valid mode. +.El +.Sh SEE ALSO +.Xr chmod 1 , +.Xr stat 2 , +.Xr umask 2 , +.Xr malloc 3 +.Sh HISTORY +The +.Fn getmode +and +.Fn setmode +functions first appeared in +.Bx 4.4 . +.Sh BUGS +Each call to +.Nm setmode +allocates a small amount of memory that there is no correct way to +free. +.Pp +The type of +.Fa set +should really be some opaque struct type used only by these functions +rather than +.Ft void * . diff --git a/lib/nbsd_libc/gen/setmode.c b/lib/nbsd_libc/gen/setmode.c new file mode 100644 index 000000000..83dd752b9 --- /dev/null +++ b/lib/nbsd_libc/gen/setmode.c @@ -0,0 +1,492 @@ +/* $NetBSD: setmode.c,v 1.31 2005/10/01 20:08:01 christos Exp $ */ + +/* + * Copyright (c) 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Dave Borman at Cray Research, Inc. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94"; +#else +__RCSID("$NetBSD: setmode.c,v 1.31 2005/10/01 20:08:01 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifdef SETMODE_DEBUG +#include +#endif + +#ifdef __weak_alias +__weak_alias(getmode,_getmode) +__weak_alias(setmode,_setmode) +#endif + +#define SET_LEN 6 /* initial # of bitcmd struct to malloc */ +#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ + +typedef struct bitcmd { + char cmd; + char cmd2; + mode_t bits; +} BITCMD; + +#define CMD2_CLR 0x01 +#define CMD2_SET 0x02 +#define CMD2_GBITS 0x04 +#define CMD2_OBITS 0x08 +#define CMD2_UBITS 0x10 + +static BITCMD *addcmd __P((BITCMD *, mode_t, mode_t, mode_t, mode_t)); +static void compress_mode __P((BITCMD *)); +#ifdef SETMODE_DEBUG +static void dumpmode __P((BITCMD *)); +#endif + +/* + * Given the old mode and an array of bitcmd structures, apply the operations + * described in the bitcmd structures to the old mode, and return the new mode. + * Note that there is no '=' command; a strict assignment is just a '-' (clear + * bits) followed by a '+' (set bits). + */ +mode_t +getmode(bbox, omode) + const void *bbox; + mode_t omode; +{ + const BITCMD *set; + mode_t clrval, newmode, value; + + _DIAGASSERT(bbox != NULL); + + set = (const BITCMD *)bbox; + newmode = omode; + for (value = 0;; set++) + switch(set->cmd) { + /* + * When copying the user, group or other bits around, we "know" + * where the bits are in the mode so that we can do shifts to + * copy them around. If we don't use shifts, it gets real + * grundgy with lots of single bit checks and bit sets. + */ + case 'u': + value = (newmode & S_IRWXU) >> 6; + goto common; + + case 'g': + value = (newmode & S_IRWXG) >> 3; + goto common; + + case 'o': + value = newmode & S_IRWXO; +common: if (set->cmd2 & CMD2_CLR) { + clrval = + (set->cmd2 & CMD2_SET) ? S_IRWXO : value; + if (set->cmd2 & CMD2_UBITS) + newmode &= ~((clrval<<6) & set->bits); + if (set->cmd2 & CMD2_GBITS) + newmode &= ~((clrval<<3) & set->bits); + if (set->cmd2 & CMD2_OBITS) + newmode &= ~(clrval & set->bits); + } + if (set->cmd2 & CMD2_SET) { + if (set->cmd2 & CMD2_UBITS) + newmode |= (value<<6) & set->bits; + if (set->cmd2 & CMD2_GBITS) + newmode |= (value<<3) & set->bits; + if (set->cmd2 & CMD2_OBITS) + newmode |= value & set->bits; + } + break; + + case '+': + newmode |= set->bits; + break; + + case '-': + newmode &= ~set->bits; + break; + + case 'X': + if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) + newmode |= set->bits; + break; + + case '\0': + default: +#ifdef SETMODE_DEBUG + (void)printf("getmode:%04o -> %04o\n", omode, newmode); +#endif + return (newmode); + } +} + +#define ADDCMD(a, b, c, d) do { \ + if (set >= endset) { \ + BITCMD *newset; \ + setlen += SET_LEN_INCR; \ + newset = realloc(saveset, sizeof(BITCMD) * setlen); \ + if (newset == NULL) \ + goto out; \ + set = newset + (set - saveset); \ + saveset = newset; \ + endset = newset + (setlen - 2); \ + } \ + set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)); \ +} while (/*CONSTCOND*/0) + +#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) + +void * +setmode(p) + const char *p; +{ + int serrno; + char op, *ep; + BITCMD *set, *saveset, *endset; + sigset_t signset, sigoset; + mode_t mask, perm, permXbits, who; + long lval; + int equalopdone = 0; /* pacify gcc */ + int setlen; + + if (!*p) { + errno = EINVAL; + return NULL; + } + + /* + * Get a copy of the mask for the permissions that are mask relative. + * Flip the bits, we want what's not set. Since it's possible that + * the caller is opening files inside a signal handler, protect them + * as best we can. + */ + sigfillset(&signset); + (void)sigprocmask(SIG_BLOCK, &signset, &sigoset); + (void)umask(mask = umask(0)); + mask = ~mask; + (void)sigprocmask(SIG_SETMASK, &sigoset, NULL); + + setlen = SET_LEN + 2; + + if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL) + return (NULL); + saveset = set; + endset = set + (setlen - 2); + + /* + * If an absolute number, get it and return; disallow non-octal digits + * or illegal bits. + */ + if (isdigit((unsigned char)*p)) { + errno = 0; + lval = strtol(p, &ep, 8); + if (*ep) { + errno = EINVAL; + goto out; + } + if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) + goto out; + if (lval & ~(STANDARD_BITS|S_ISTXT)) { + errno = EINVAL; + goto out; + } + perm = (mode_t)lval; + ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); + set->cmd = 0; + return (saveset); + } + + /* + * Build list of structures to set/clear/copy bits as described by + * each clause of the symbolic mode. + */ + for (;;) { + /* First, find out which bits might be modified. */ + for (who = 0;; ++p) { + switch (*p) { + case 'a': + who |= STANDARD_BITS; + break; + case 'u': + who |= S_ISUID|S_IRWXU; + break; + case 'g': + who |= S_ISGID|S_IRWXG; + break; + case 'o': + who |= S_IRWXO; + break; + default: + goto getop; + } + } + +getop: if ((op = *p++) != '+' && op != '-' && op != '=') { + errno = EINVAL; + goto out; + } + if (op == '=') + equalopdone = 0; + + who &= ~S_ISTXT; + for (perm = 0, permXbits = 0;; ++p) { + switch (*p) { + case 'r': + perm |= S_IRUSR|S_IRGRP|S_IROTH; + break; + case 's': + /* + * If specific bits where requested and + * only "other" bits ignore set-id. + */ + if (who == 0 || (who & ~S_IRWXO)) + perm |= S_ISUID|S_ISGID; + break; + case 't': + /* + * If specific bits where requested and + * only "other" bits ignore set-id. + */ + if (who == 0 || (who & ~S_IRWXO)) { + who |= S_ISTXT; + perm |= S_ISTXT; + } + break; + case 'w': + perm |= S_IWUSR|S_IWGRP|S_IWOTH; + break; + case 'X': + permXbits = S_IXUSR|S_IXGRP|S_IXOTH; + break; + case 'x': + perm |= S_IXUSR|S_IXGRP|S_IXOTH; + break; + case 'u': + case 'g': + case 'o': + /* + * When ever we hit 'u', 'g', or 'o', we have + * to flush out any partial mode that we have, + * and then do the copying of the mode bits. + */ + if (perm) { + ADDCMD(op, who, perm, mask); + perm = 0; + } + if (op == '=') + equalopdone = 1; + if (op == '+' && permXbits) { + ADDCMD('X', who, permXbits, mask); + permXbits = 0; + } + ADDCMD(*p, who, op, mask); + break; + + default: + /* + * Add any permissions that we haven't already + * done. + */ + if (perm || (op == '=' && !equalopdone)) { + if (op == '=') + equalopdone = 1; + ADDCMD(op, who, perm, mask); + perm = 0; + } + if (permXbits) { + ADDCMD('X', who, permXbits, mask); + permXbits = 0; + } + goto apply; + } + } + +apply: if (!*p) + break; + if (*p != ',') + goto getop; + ++p; + } + set->cmd = 0; +#ifdef SETMODE_DEBUG + (void)printf("Before compress_mode()\n"); + dumpmode(saveset); +#endif + compress_mode(saveset); +#ifdef SETMODE_DEBUG + (void)printf("After compress_mode()\n"); + dumpmode(saveset); +#endif + return (saveset); +out: + serrno = errno; + free(saveset); + errno = serrno; + return NULL; +} + +static BITCMD * +addcmd(set, op, who, oparg, mask) + BITCMD *set; + mode_t oparg, who, op, mask; +{ + + _DIAGASSERT(set != NULL); + + switch (op) { + case '=': + set->cmd = '-'; + set->bits = who ? who : STANDARD_BITS; + set++; + + op = '+'; + /* FALLTHROUGH */ + case '+': + case '-': + case 'X': + set->cmd = op; + set->bits = (who ? who : mask) & oparg; + break; + + case 'u': + case 'g': + case 'o': + set->cmd = op; + if (who) { + set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | + ((who & S_IRGRP) ? CMD2_GBITS : 0) | + ((who & S_IROTH) ? CMD2_OBITS : 0); + set->bits = (mode_t)~0; + } else { + set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; + set->bits = mask; + } + + if (oparg == '+') + set->cmd2 |= CMD2_SET; + else if (oparg == '-') + set->cmd2 |= CMD2_CLR; + else if (oparg == '=') + set->cmd2 |= CMD2_SET|CMD2_CLR; + break; + } + return (set + 1); +} + +#ifdef SETMODE_DEBUG +static void +dumpmode(set) + BITCMD *set; +{ + + _DIAGASSERT(set != NULL); + + for (; set->cmd; ++set) + (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", + set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", + set->cmd2 & CMD2_CLR ? " CLR" : "", + set->cmd2 & CMD2_SET ? " SET" : "", + set->cmd2 & CMD2_UBITS ? " UBITS" : "", + set->cmd2 & CMD2_GBITS ? " GBITS" : "", + set->cmd2 & CMD2_OBITS ? " OBITS" : ""); +} +#endif + +/* + * Given an array of bitcmd structures, compress by compacting consecutive + * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', + * 'g' and 'o' commands continue to be separate. They could probably be + * compacted, but it's not worth the effort. + */ +static void +compress_mode(set) + BITCMD *set; +{ + BITCMD *nset; + int setbits, clrbits, Xbits, op; + + _DIAGASSERT(set != NULL); + + for (nset = set;;) { + /* Copy over any 'u', 'g' and 'o' commands. */ + while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { + *set++ = *nset++; + if (!op) + return; + } + + for (setbits = clrbits = Xbits = 0;; nset++) { + if ((op = nset->cmd) == '-') { + clrbits |= nset->bits; + setbits &= ~nset->bits; + Xbits &= ~nset->bits; + } else if (op == '+') { + setbits |= nset->bits; + clrbits &= ~nset->bits; + Xbits &= ~nset->bits; + } else if (op == 'X') + Xbits |= nset->bits & ~setbits; + else + break; + } + if (clrbits) { + set->cmd = '-'; + set->cmd2 = 0; + set->bits = clrbits; + set++; + } + if (setbits) { + set->cmd = '+'; + set->cmd2 = 0; + set->bits = setbits; + set++; + } + if (Xbits) { + set->cmd = 'X'; + set->cmd2 = 0; + set->bits = Xbits; + set++; + } + } +} diff --git a/lib/nbsd_libc/gen/setproctitle.3 b/lib/nbsd_libc/gen/setproctitle.3 new file mode 100644 index 000000000..8d32011f7 --- /dev/null +++ b/lib/nbsd_libc/gen/setproctitle.3 @@ -0,0 +1,99 @@ +.\" $NetBSD: setproctitle.3,v 1.18 2003/07/26 19:24:44 salo Exp $ +.\" +.\" Copyright (c) 1994, 1995 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. +.\" +.\" <> +.\" +.Dd April 13, 1994 +.Dt SETPROCTITLE 3 +.Os +.Sh NAME +.Nm setproctitle +.Nd set process title +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft void +.Fn setproctitle "const char *fmt" "..." +.Sh DESCRIPTION +The +.Fn setproctitle +function sets the invoking process's title. +The process title is set to the last component of the program +name, followed by a colon and the formatted string specified +by +.Va fmt . +If +.Va fmt +is +.Dv NULL , +the colon and formatted string are omitted. +The length of a process title is limited to 2048 bytes. +.Sh EXAMPLES +Set the process title to the program name, with no further information: +.Bd -literal -offset indent +setproctitle(NULL); +.Ed +.Pp +Set the process title to the program name, an informational string, +and the process id: +.Bd -literal -offset indent +setproctitle("foo! (%d)", getpid()); +.Ed +.Sh SEE ALSO +.Xr ps 1 , +.Xr w 1 , +.Xr getprogname 3 , +.Xr printf 3 +.Sh HISTORY +The +.Fn setproctitle +function first appeared in +.Nx 1.0 . +.Sh CAVEATS +It is important never to pass a string with user-supplied data as a +format without using +.Ql %s . +An attacker can put format specifiers in the string to mangle your stack, +leading to a possible security hole. +This holds true even if you have built the string +.Dq by hand +using a function like +.Fn snprintf , +as the resulting string may still contain user-supplied conversion specifiers +for later interpolation by +.Fn setproctitle . +.Pp +Always be sure to use the proper secure idiom: +.Bd -literal -offset indent +setproctitle("%s", string); +.Ed diff --git a/lib/nbsd_libc/gen/setproctitle.c b/lib/nbsd_libc/gen/setproctitle.c new file mode 100644 index 000000000..513892264 --- /dev/null +++ b/lib/nbsd_libc/gen/setproctitle.c @@ -0,0 +1,98 @@ +/* $NetBSD: setproctitle.c,v 1.22 2008/01/03 04:26:27 christos Exp $ */ + +/* + * Copyright (c) 1994, 1995 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: setproctitle.c,v 1.22 2008/01/03 04:26:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(setproctitle,_setproctitle) +#endif + +#define MAX_PROCTITLE 2048 + +/* + * For compatibility with old versions of crt0 that didn't define __ps_strings, + * define it as a common here. + */ +struct ps_strings *__ps_strings; + +void +setproctitle(const char *fmt, ...) +{ + static char buf[MAX_PROCTITLE], *bufp; + const char *pname = getprogname(); + + if (fmt != NULL) { + int len = snprintf(buf, sizeof(buf), "%s: ", pname); + if (len >= 0) { + va_list ap; + + va_start(ap, fmt); + (void)vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); + va_end(ap); + } + } else + (void)snprintf(buf, sizeof(buf), "%s", pname); + + bufp = buf; + +#ifdef USRSTACK + /* + * For compatibility with old versions of crt0 and old kernels, set + * __ps_strings to a default value if it's null. + * But only if USRSTACK is defined. It might not be defined if + * user-level code can not assume it's a constant (i.e. m68k). + */ + if (__ps_strings == 0) + __ps_strings = PS_STRINGS; +#endif /* USRSTACK */ + + if (__ps_strings != 0) { + __ps_strings->ps_nargvstr = 1; + __ps_strings->ps_argvstr = &bufp; + } +} diff --git a/lib/nbsd_libc/gen/setprogname.c b/lib/nbsd_libc/gen/setprogname.c new file mode 100644 index 000000000..489952371 --- /dev/null +++ b/lib/nbsd_libc/gen/setprogname.c @@ -0,0 +1,65 @@ +/* $NetBSD: setprogname.c,v 1.3 2003/07/26 19:24:44 salo 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: setprogname.c,v 1.3 2003/07/26 19:24:44 salo Exp $"); +#endif /* LIBC_SCCS and not lint */ + +/* In NetBSD, the program name is set by crt0. It can't be overridden. */ +#undef REALLY_SET_PROGNAME + +#include + +#ifdef REALLY_SET_PROGNAME +#include + +extern const char *__progname; +#endif + +/*ARGSUSED*/ +void +setprogname(const char *progname) +{ + +#ifdef REALLY_SET_PROGNAME + __progname = strrchr(progname, '/'); + if (__progname == NULL) + __progname = progname; + else + __progname++; +#endif +} diff --git a/lib/nbsd_libc/gen/shquote.3 b/lib/nbsd_libc/gen/shquote.3 new file mode 100644 index 000000000..026467fcf --- /dev/null +++ b/lib/nbsd_libc/gen/shquote.3 @@ -0,0 +1,244 @@ +.\" $NetBSD: shquote.3,v 1.9 2008/09/07 08:55:46 apb 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. +.\" +.\" <> +.\" +.Dd September 7, 2008 +.Dt SHQUOTE 3 +.Os +.Sh NAME +.Nm shquote , +.Nm shquotev +.Nd quote argument strings for use with the shell +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft size_t +.Fn shquote "const char *arg" "char *buf" "size_t bufsize" +.Ft size_t +.Fn shquotev "int argc" "char * const *argv" "char *buf" "size_t bufsize" +.Sh DESCRIPTION +The +.Fn shquote +and +.Fn shquotev +functions copy strings and transform the copies by adding shell +escape and quoting characters. +They are used to encapsulate +arguments to be included in command strings passed to the +.Fn system +and +.Fn popen +functions, so that the arguments will have the correct values +after being evaluated by the shell. +.Pp +The exact method of quoting and escaping may vary, and is intended +to match the conventions of the shell used by +.Fn system +and +.Fn popen . +It may not match the conventions used by other shells. +In this implementation, the following +transformation is applied to each input string: +.Bl -bullet -width indent +.It +it is surrounded by single quotes +.Pq ' , +.It +any single quotes in the input are escaped by replacing them with +the four-character sequence: +.Li '\e'' , +and +.It +extraneous pairs of single quotes (caused by multiple adjacent single +quotes in the input string, or by single quotes at the beginning or +end of the input string) are elided. +.El +.Pp +The +.Fn shquote +function transforms the string specified by its +.Fa arg +argument, and places the result into the memory pointed to by +.Fa buf . +.Pp +The +.Fn shquotev +function transforms each of the +.Fa argc +strings specified by the array +.Fa argv +independently. +The transformed strings are placed in the memory pointed to by +.Fa buf , +separated by spaces. +It does not modify the pointer array specified by +.Fa argv +or the strings pointed to by the pointers in the array. +.Pp +Both functions write up to +.Fa bufsize +- 1 characters of output into the buffer pointed to by +.Fa buf , +then add a +.Li NUL +character to terminate the output string. +If +.Fa bufsize +is given as zero, the +.Fa buf +parameter is ignored and no output is written. +.Sh RETURN VALUES +The +.Fn shquote +and +.Fn shquotev +functions return the number of characters necessary to hold the +result from operating on their input strings, +not including the terminating +.Li NUL . +That is, they return the length of the string that would have +been written to the output buffer, if it were large enough. +If an error occurs during processing, the value ((size_t)\-1) +is returned and +.Va errno +is set appropriately. +.Sh EXAMPLES +The following code fragment demonstrates how you might use +.Fn shquotev +to construct a command string to be used with +.Fn system . +The command uses an environment variable (which will be expanded by +the shell) to determine the actual program to run. +Note that the environment variable may be expanded by +the shell into multiple words. +The first word of the expansion will be used by the shell +as the name of the program to run, +and the rest will be passed as arguments to the program. +.Bd -literal -offset indent +char **argv, c, *cmd; +size_t cmdlen, len, qlen; +int argc; + +\&... + +/* + * Size buffer to hold the command string, and allocate it. + * Buffer of length one given to snprintf() for portability. + */ +cmdlen = snprintf(\*[Am]c, 1, "${PROG-%s} ", PROG_DEFAULT); +qlen = shquotev(argc, argv, NULL, 0); +if (qlen == (size_t)-1) { + \&... +} +cmdlen += qlen + 1; +cmd = malloc(cmdlen); +if (cmd == NULL) { + \&... +} + +/* Create the command string. */ +len = snprintf(cmd, cmdlen, "${PROG-%s} ", PROG_DEFAULT); +qlen = shquotev(argc, argv, cmd + len, cmdlen - len); +if (qlen == (size_t)-1) { + /* Should not ever happen. */ + \&... +} +len += qlen; + +/* "cmd" can now be passed to system(). */ +.Ed +.Pp +The following example shows how you would implement the same +functionality using the +.Fn shquote +function directly. +.Bd -literal -offset indent +char **argv, c, *cmd; +size_t cmdlen, len, qlen; +int argc, i; + +\&... + +/* + * Size buffer to hold the command string, and allocate it. + * Buffer of length one given to snprintf() for portability. + */ +cmdlen = snprintf(\*[Am]c, 1, "${PROG-%s} ", PROG_DEFAULT); +for (i = 0; i \*[Lt] argc; i++) { + qlen = shquote(argv[i], NULL, 0); + if (qlen == (size_t)-1) { + \&... + } + cmdlen += qlen + 1; +} +cmd = malloc(cmdlen); +if (cmd == NULL) { + \&... +} + +/* Start the command string with the env var reference. */ +len = snprintf(cmd, cmdlen, "${PROG-%s} ", PROG_DEFAULT); + +/* Quote all of the arguments when copying them. */ +for (i = 0; i \*[Lt] argc; i++) { + qlen = shquote(argv[i], cmd + len, cmdlen - len); + if (qlen == (size_t)-1) { + /* Should not ever happen. */ + \&... + } + len += qlen; + cmd[len++] = ' '; +} +cmd[--len] = '\e0'; + +/* "cmd" can now be passed to system(). */ +.Ed +.Sh SEE ALSO +.Xr sh 1 , +.Xr popen 3 , +.Xr system 3 +.Sh BUGS +This implementation does not currently handle strings containing multibyte +characters properly. +To address this issue, +.Pa /bin/sh +.Po +the shell used by +.Fn system +and +.Fn popen +.Pc +must first be fixed to handle multibyte characters. +When that has been done, +these functions can have multibyte character support enabled. diff --git a/lib/nbsd_libc/gen/shquote.c b/lib/nbsd_libc/gen/shquote.c new file mode 100644 index 000000000..e72837f5f --- /dev/null +++ b/lib/nbsd_libc/gen/shquote.c @@ -0,0 +1,189 @@ +/* $NetBSD: shquote.c,v 1.8 2006/03/19 02:33:02 christos 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: shquote.c,v 1.8 2006/03/19 02:33:02 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +/* + * Define SHQUOTE_USE_MULTIBYTE if you want shquote() to handle multibyte + * characters using mbrtowc(). + * + * Please DO NOT rip this #ifdef out of the code. It's also here to help + * portability. + */ +#undef SHQUOTE_USE_MULTIBYTE + +#include "namespace.h" +#include +#include +#ifdef SHQUOTE_USE_MULTIBYTE +#include +#include +#include +#endif + +#ifdef __weak_alias +__weak_alias(shquote,_shquote) +#endif + +/* + * shquote(): + * + * Requotes arguments so that they'll be interpreted properly by the + * shell (/bin/sh). + * + * Wraps single quotes around the string, and replaces single quotes + * in the string with the sequence: + * '\'' + * + * Returns the number of characters required to hold the resulting quoted + * argument. + * + * The buffer supplied is filled in and NUL-terminated. If 'bufsize' + * indicates that the buffer is too short to hold the output string, the + * first (bufsize - 1) bytes of quoted argument are filled in and the + * buffer is NUL-terminated. + * + * Changes could be made to optimize the length of strings output by this + * function: + * + * * if there are no metacharacters or whitespace in the input, + * the output could be the input string. + */ + +#ifdef SHQUOTE_USE_MULTIBYTE + +#define XLATE_OUTCH(x) wcrtomb(outch, (x), &mbso) +#define XLATE_INCH() \ + do { \ + n = mbrtowc(&c, arg, MB_CUR_MAX, &mbsi); \ + } while (/*LINTED const cond*/0) + +#else + +#define XLATE_OUTCH(x) (outch[0] = (x), 1) +#define XLATE_INCH() \ + do { \ + n = ((c = *arg) != '\0') ? 1 : 0; \ + } while (/*LINTED const cond*/0) + +#endif + +#define PUT(x) \ + do { \ + outchlen = XLATE_OUTCH(x); \ + if (outchlen == (size_t)-1) \ + goto bad; \ + rv += outchlen; \ + if (bufsize != 0) { \ + if (bufsize < outchlen || \ + (bufsize == outchlen && \ + outch[outchlen - 1] != '\0')) { \ + *buf = '\0'; \ + bufsize = 0; \ + } else { \ + memcpy(buf, outch, outchlen); \ + buf += outchlen; \ + bufsize -= outchlen; \ + } \ + } \ + } while (/*LINTED const cond*/0) + +size_t +shquote(const char *arg, char *buf, size_t bufsize) +{ +#ifdef SHQUOTE_USE_MULTIBYTE + char outch[MB_LEN_MAX]; + mbstate_t mbsi, mbso; + wchar_t c, lastc; + size_t outchlen; +#else + char outch[1]; + char c, lastc; + size_t outchlen; +#endif + size_t rv; + int n; + + rv = 0; + lastc = 0; +#ifdef SHQUOTE_USE_MULTIBYTE + memset(&mbsi, 0, sizeof mbsi); + memset(&mbso, 0, sizeof mbso); +#endif + + if (*arg != '\'') + PUT('\''); + for (;;) { + XLATE_INCH(); + if (n <= 0) + break; + arg += n; + lastc = c; + + if (c == '\'') { + if (rv != 0) + PUT('\''); + PUT('\\'); + PUT('\''); + for (;;) { + XLATE_INCH(); + if (n <= 0 || c != '\'') + break; + PUT('\\'); + PUT('\''); + arg += n; + } + if (n > 0) + PUT('\''); + } else + PUT(c); + } + if (lastc != '\'') + PUT('\''); + + /* Put multibyte or NUL terminator, but don't count the NUL. */ + PUT('\0'); + rv--; + + return rv; + +bad: + /* A multibyte character encoding or decoding error occurred. */ + return (size_t)-1; +} diff --git a/lib/nbsd_libc/gen/shquotev.c b/lib/nbsd_libc/gen/shquotev.c new file mode 100644 index 000000000..4953f47ff --- /dev/null +++ b/lib/nbsd_libc/gen/shquotev.c @@ -0,0 +1,86 @@ +/* $NetBSD: shquotev.c,v 1.5 2005/06/12 05:21:27 lukem 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. + * + * <> + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: shquotev.c,v 1.5 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +/* + * shquotev(): + * + * Apply shquote() to a set of strings, separating the results by spaces. + */ + +size_t +shquotev(int argc, char * const * argv, char *buf, size_t bufsize) +{ + size_t rv, callrv; + int i; + + rv = 0; + + if (argc == 0) { + if (bufsize != 0) + *buf = '\0'; + return rv; + } + + for (i = 0; i < argc; i++) { + callrv = shquote(argv[i], buf, bufsize); + if (callrv == (size_t)-1) + goto bad; + rv += callrv; + buf += callrv; + bufsize = (bufsize > callrv) ? (bufsize - callrv) : 0; + + if (i < (argc - 1)) { + rv++; + if (bufsize > 1) { + *buf++ = ' '; + bufsize--; + } + } + } + + return rv; + +bad: + return (size_t)-1; +} diff --git a/lib/nbsd_libc/gen/sighold.3 b/lib/nbsd_libc/gen/sighold.3 new file mode 100644 index 000000000..b29e46bfa --- /dev/null +++ b/lib/nbsd_libc/gen/sighold.3 @@ -0,0 +1,80 @@ +.\" $NetBSD: sighold.3,v 1.5 2010/04/30 04:39:16 jruoho Exp $ +.\" +.\" Copyright (c) 2003 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 April 30, 2010 +.Dt SIGHOLD 3 +.Os +.Sh NAME +.Nm sighold +.Nd manipulate current signal mask +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn sighold "int sig" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by +.Xr sigprocmask 2 . +.Ef +.Pp +The +.Fn sighold +function adds the signal +.Fa sig +to the calling process' signal mask. +.Sh RETURN VALUES +If successful, the +.Fn sighold +function returns 0. +Otherwise \-1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn sighold +function will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +The argument +.Fa sig +is not a valid signal number. +.El +.Sh SEE ALSO +.Xr sigprocmask 2 , +.Xr sigrelse 3 +.Sh STANDARDS +The +.Fn sighold +function conforms to +.St -p1003.1-2001 . +It was however marked as obsolete in the +.St -p1003.1-2008 +revision of the standard. diff --git a/lib/nbsd_libc/gen/sighold.c b/lib/nbsd_libc/gen/sighold.c new file mode 100644 index 000000000..7fa70c983 --- /dev/null +++ b/lib/nbsd_libc/gen/sighold.c @@ -0,0 +1,51 @@ +/* $NetBSD: sighold.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sighold.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include + +int +sighold(int sig) +{ + sigset_t set; + + sigemptyset(&set); + if (sigaddset(&set, sig) != 0) + return (-1); + + return (sigprocmask(SIG_BLOCK, &set, NULL)); +} diff --git a/lib/nbsd_libc/gen/sigignore.3 b/lib/nbsd_libc/gen/sigignore.3 new file mode 100644 index 000000000..41ed44e83 --- /dev/null +++ b/lib/nbsd_libc/gen/sigignore.3 @@ -0,0 +1,85 @@ +.\" $NetBSD: sigignore.3,v 1.6 2010/04/30 06:48:20 wiz Exp $ +.\" +.\" Copyright (c) 2003 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 April 30, 2010 +.Dt SIGIGNORE 3 +.Os +.Sh NAME +.Nm sigignore +.Nd manipulate signal dispositions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn sigignore "int sig" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by +.Xr sigaction 2 . +.Ef +.Pp +The +.Fn sigignore +function sets the disposition of the signal +.Fa sig +to +.Dv SIG_IGN . +.Sh RETURN VALUES +If successful, the +.Fn sigignore +function returns 0. +Otherwise \-1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn sigignore +function will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +The argument +.Fa sig +is not a valid signal number; +or an attempt is made to ignore a signal that cannot be ignored, +such as +.Dv SIGKILL +or +.Dv SIGSTOP . +.El +.Sh SEE ALSO +.Xr sigaction 2 +.Sh STANDARDS +The +.Fn sigignore +function conforms to +.St -p1003.1-2001 . +It was however marked as obsolete in the +.St -p1003.1-2008 +revision of the standard. diff --git a/lib/nbsd_libc/gen/sigignore.c b/lib/nbsd_libc/gen/sigignore.c new file mode 100644 index 000000000..e9d73221b --- /dev/null +++ b/lib/nbsd_libc/gen/sigignore.c @@ -0,0 +1,52 @@ +/* $NetBSD: sigignore.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sigignore.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include + +int +sigignore(int sig) +{ + struct sigaction sa; + + sa.sa_handler = SIG_IGN; + if (sigemptyset(&sa.sa_mask) != 0) + return (-1); + sa.sa_flags = 0; + + return (sigaction(sig, &sa, NULL)); +} diff --git a/lib/nbsd_libc/gen/siginterrupt.3 b/lib/nbsd_libc/gen/siginterrupt.3 new file mode 100644 index 000000000..f03283316 --- /dev/null +++ b/lib/nbsd_libc/gen/siginterrupt.3 @@ -0,0 +1,105 @@ +.\" $NetBSD: siginterrupt.3,v 1.11 2003/08/07 16:42:56 agc Exp $ +.\" +.\" Copyright (c) 1985, 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. +.\" +.\" @(#)siginterrupt.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt SIGINTERRUPT 3 +.Os +.Sh NAME +.Nm siginterrupt +.Nd allow signals to interrupt system calls +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn siginterrupt "int sig" "int flag" +.Sh DESCRIPTION +The +.Fn siginterrupt +function +is used to change the system call restart +behavior when a system call is interrupted by the specified signal. +If the flag is false (0), then system calls will be restarted if +they are interrupted by the specified signal +and no data has been transferred yet. +System call restart is the default behavior on +.Bx 4.2 . +.Pp +If the flag is true (1), +then restarting of system calls is disabled. +If a system call is interrupted by the specified signal +and no data has been transferred, +the system call will return \-1 with the global variable +.Va errno +set to +.Dv EINTR . +Interrupted system calls that have started transferring +data will return the amount of data actually transferred. +System call interrupt is the signal behavior found on +.Bx 4.1 +and +.At V +systems. +.Pp +Note that the new +.Bx 4.2 +signal handling semantics are not +altered in any other way. +Most notably, signal handlers always remain installed until +explicitly changed by a subsequent +.Xr sigaction 2 +call, and the signal mask operates as documented in +.Xr sigaction 2 . +Programs may switch between restartable and interruptible +system call operation as often as desired in the execution of a program. +.Pp +Issuing a +.Fn siginterrupt 3 +call during the execution of a signal handler will cause +the new action to take place on the next signal to be caught. +.Sh NOTES +This library routine uses an extension of the +.Xr sigaction 2 +system call that is not available in +.Bx 4.2 , +hence it should not be used if backward compatibility is needed. +.Sh RETURN VALUES +A 0 value indicates that the call succeeded. +A \-1 value indicates that an invalid signal number has been supplied. +.Sh SEE ALSO +.Xr sigaction 2 , +.Xr sigprocmask 2 , +.Xr sigsuspend 2 +.Sh HISTORY +The +.Fn siginterrupt +function appeared in +.Bx 4.3 . diff --git a/lib/nbsd_libc/gen/siginterrupt.c b/lib/nbsd_libc/gen/siginterrupt.c new file mode 100644 index 000000000..56e98b851 --- /dev/null +++ b/lib/nbsd_libc/gen/siginterrupt.c @@ -0,0 +1,70 @@ +/* $NetBSD: siginterrupt.c,v 1.12 2003/08/07 16:42:56 agc Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)siginterrupt.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: siginterrupt.c,v 1.12 2003/08/07 16:42:56 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#ifdef __weak_alias +__weak_alias(siginterrupt,_siginterrupt) +#endif + +extern sigset_t __sigintr; +/* + * Set signal state to prevent restart of system calls + * after an instance of the indicated signal. + */ +int +siginterrupt(sig, flag) + int sig, flag; +{ + struct sigaction sa; + int ret; + + if ((ret = sigaction(sig, (struct sigaction *)0, &sa)) < 0) + return (ret); + if (flag) { + sigaddset(&__sigintr, sig); + sa.sa_flags &= ~SA_RESTART; + } else { + sigdelset(&__sigintr, sig); + sa.sa_flags |= SA_RESTART; + } + return (sigaction(sig, &sa, (struct sigaction *)0)); +} diff --git a/lib/nbsd_libc/gen/siglist.c b/lib/nbsd_libc/gen/siglist.c new file mode 100644 index 000000000..3b2e464b5 --- /dev/null +++ b/lib/nbsd_libc/gen/siglist.c @@ -0,0 +1,112 @@ +/* $NetBSD: siglist.c,v 1.17 2007/01/17 23:24:22 hubertf Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)siglist.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: siglist.c,v 1.17 2007/01/17 23:24:22 hubertf Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include + +static const char *const __siglist14[] = { + "Signal 0", /* 0 */ + "Hangup", /* 1 SIGHUP */ + "Interrupt", /* 2 SIGINT */ + "Quit", /* 3 SIGQUIT */ + "Illegal instruction", /* 4 SIGILL */ + "Trace/BPT trap", /* 5 SIGTRAP */ + "Abort trap", /* 6 SIGABRT */ + "EMT trap", /* 7 SIGEMT */ + "Floating point exception", /* 8 SIGFPE */ + "Killed", /* 9 SIGKILL */ + "Bus error", /* 10 SIGBUS */ + "Segmentation fault", /* 11 SIGSEGV */ + "Bad system call", /* 12 SIGSYS */ + "Broken pipe", /* 13 SIGPIPE */ + "Alarm clock", /* 14 SIGALRM */ + "Terminated", /* 15 SIGTERM */ + "Urgent I/O condition", /* 16 SIGURG */ + "Suspended (signal)", /* 17 SIGSTOP */ + "Suspended", /* 18 SIGTSTP */ + "Continued", /* 19 SIGCONT */ + "Child exited", /* 20 SIGCHLD */ + "Stopped (tty input)", /* 21 SIGTTIN */ + "Stopped (tty output)", /* 22 SIGTTOU */ + "I/O possible", /* 23 SIGIO */ + "Cputime limit exceeded", /* 24 SIGXCPU */ + "Filesize limit exceeded", /* 25 SIGXFSZ */ + "Virtual timer expired", /* 26 SIGVTALRM */ + "Profiling timer expired", /* 27 SIGPROF */ + "Window size changes", /* 28 SIGWINCH */ + "Information request", /* 29 SIGINFO */ + "User defined signal 1", /* 30 SIGUSR1 */ + "User defined signal 2", /* 31 SIGUSR2 */ + "Power fail/restart", /* 32 SIGPWR */ + "Real time signal 0", /* 33 SIGRTMIN + 0 */ + "Real time signal 1", /* 34 SIGRTMIN + 1 */ + "Real time signal 2", /* 35 SIGRTMIN + 2 */ + "Real time signal 3", /* 36 SIGRTMIN + 3 */ + "Real time signal 4", /* 37 SIGRTMIN + 4 */ + "Real time signal 5", /* 38 SIGRTMIN + 5 */ + "Real time signal 6", /* 39 SIGRTMIN + 6 */ + "Real time signal 7", /* 40 SIGRTMIN + 7 */ + "Real time signal 8", /* 41 SIGRTMIN + 8 */ + "Real time signal 9", /* 42 SIGRTMIN + 9 */ + "Real time signal 10", /* 43 SIGRTMIN + 10 */ + "Real time signal 11", /* 44 SIGRTMIN + 11 */ + "Real time signal 12", /* 45 SIGRTMIN + 12 */ + "Real time signal 13", /* 46 SIGRTMIN + 13 */ + "Real time signal 14", /* 47 SIGRTMIN + 14 */ + "Real time signal 15", /* 48 SIGRTMIN + 15 */ + "Real time signal 16", /* 49 SIGRTMIN + 16 */ + "Real time signal 17", /* 50 SIGRTMIN + 17 */ + "Real time signal 18", /* 51 SIGRTMIN + 18 */ + "Real time signal 19", /* 52 SIGRTMIN + 19 */ + "Real time signal 20", /* 53 SIGRTMIN + 20 */ + "Real time signal 21", /* 54 SIGRTMIN + 21 */ + "Real time signal 22", /* 55 SIGRTMIN + 22 */ + "Real time signal 23", /* 56 SIGRTMIN + 23 */ + "Real time signal 24", /* 57 SIGRTMIN + 24 */ + "Real time signal 25", /* 58 SIGRTMIN + 25 */ + "Real time signal 26", /* 59 SIGRTMIN + 26 */ + "Real time signal 27", /* 60 SIGRTMIN + 27 */ + "Real time signal 28", /* 61 SIGRTMIN + 28 */ + "Real time signal 29", /* 62 SIGRTMIN + 29 */ + "Real time signal 30", /* 63 SIGRTMIN + 30 */ +}; + +const int __sys_nsig14 = sizeof(__siglist14) / sizeof(__siglist14[0]); + +const char * const *__sys_siglist14 = __siglist14; diff --git a/lib/nbsd_libc/gen/signal.3 b/lib/nbsd_libc/gen/signal.3 new file mode 100644 index 000000000..e554195a3 --- /dev/null +++ b/lib/nbsd_libc/gen/signal.3 @@ -0,0 +1,191 @@ +.\" $NetBSD: signal.3,v 1.24 2004/06/13 19:17:06 lha Exp $ +.\" +.\" Copyright (c) 1980, 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. +.\" +.\" @(#)signal.3 8.3 (Berkeley) 4/19/94 +.\" +.Dd June 11, 2004 +.Dt SIGNAL 3 +.Os +.Sh NAME +.Nm signal +.Nd simplified software signal facilities +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.\" The following is Quite Ugly, but syntactically correct. Don't try to +.\" fix it. +.Ft void \*(lp* +.Fn signal "int sig" "void \*(lp*func\*(rp\*(lpint\*(rp\*(rp\*(rp\*(lpint" +.Sh DESCRIPTION +This +.Fn signal +facility +is a simplified interface to the more general +.Xr sigaction 2 +facility. +.Pp +Signals allow the manipulation of a process from outside its +domain as well as allowing the process to manipulate itself or +copies of itself (children). +There are two general types of signals: +those that cause termination of a process and those that do not. +Signals which cause termination of a program might result from +an irrecoverable error or might be the result of a user at a terminal +typing the `interrupt' character. +Signals are used when a process is stopped because it wishes to access +its control terminal while in the background (see +.Xr tty 4 ) . +Signals are optionally generated +when a process resumes after being stopped, +when the status of child processes changes, +or when input is ready at the control terminal. +Most signals result in the termination of the process receiving them +if no action +is taken; some signals instead cause the process receiving them +to be stopped, or are simply discarded if the process has not +requested otherwise. +Except for the +.Dv SIGKILL +and +.Dv SIGSTOP +signals, the +.Fn signal +function allows for a signal to be caught, to be ignored, or to generate +an interrupt. +See +.Xr signal 7 +for comprehensive list of supported signals. +.Pp +The +.Fa func +procedure allows a user to choose the action upon receipt of a signal. +To set the default action of the signal to occur as listed above, +.Fa func +should be +.Dv SIG_DFL . +A +.Dv SIG_DFL +resets the default action. +To ignore the signal +.Fa func +should be +.Dv SIG_IGN . +This will cause subsequent instances of the signal to be ignored +and pending instances to be discarded. +If +.Dv SIG_IGN +is not used, +further occurrences of the signal are +automatically blocked and +.Fa func +is called. +.Pp +The handled signal is unblocked when the +function returns and +the process continues from where it left off when the signal occurred. +.Bf -symbolic +Unlike previous signal facilities, the handler +func() remains installed after a signal has been delivered. +.Ef +.Pp +For some system calls, if a signal is caught while the call is +executing and the call is prematurely terminated, +the call is automatically restarted. +(The handler is installed using the +.Dv SA_RESTART +flag with +.Xr sigaction 2 ) . +The affected system calls include +.Xr read 2 , +.Xr write 2 , +.Xr sendto 2 , +.Xr recvfrom 2 , +.Xr sendmsg 2 +and +.Xr recvmsg 2 +on a communications channel or a low speed device +and during a +.Xr ioctl 2 +or +.Xr wait 2 . +However, calls that have already committed are not restarted, +but instead return a partial success (for example, a short read count). +.Pp +When a process which has installed signal handlers forks, +the child process inherits the signals. +All caught signals may be reset to their default action by a call +to the +.Xr execve 2 +function; +ignored signals remain ignored. +.Pp +Only functions that are async-signal-safe can safely be used in signal +handlers, see +.Xr signal 7 +for a complete list. +.Sh RETURN VALUES +The previous action is returned on a successful call. +Otherwise, +.Dv SIG_ERR +is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn signal +will fail and no action will take place if one of the following occur: +.Bl -tag -width Er +.It Bq Er EINVAL +Specified +.Em sig +is not a valid signal number. +.It Bq Er EINVAL +An attempt is made to ignore or supply a handler for +.Dv SIGKILL +or +.Dv SIGSTOP . +.El +.Sh SEE ALSO +.Xr kill 1 , +.Xr kill 2 , +.Xr ptrace 2 , +.Xr sigaction 2 , +.Xr sigaltstack 2 , +.Xr sigprocmask 2 , +.Xr sigsuspend 2 , +.Xr psignal 3 , +.Xr setjmp 3 , +.Xr strsignal 3 , +.Xr tty 4 , +.Xr signal 7 +.Sh HISTORY +This +.Fn signal +facility appeared in +.Bx 4.0 . diff --git a/lib/nbsd_libc/gen/signal.c b/lib/nbsd_libc/gen/signal.c new file mode 100644 index 000000000..09f6e57c0 --- /dev/null +++ b/lib/nbsd_libc/gen/signal.c @@ -0,0 +1,68 @@ +/* $NetBSD: signal.c,v 1.12 2003/08/07 16:42:56 agc Exp $ */ + +/* + * Copyright (c) 1985, 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)signal.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: signal.c,v 1.12 2003/08/07 16:42:56 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Almost backwards compatible signal. + */ +#include "namespace.h" +#include + +#ifdef __weak_alias +__weak_alias(signal,_signal) +#endif + +sigset_t __sigintr; /* shared with siginterrupt */ + +sig_t +signal(s, a) + int s; + sig_t a; +{ + struct sigaction sa, osa; + + sa.sa_handler = a; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + if (!sigismember(&__sigintr, s)) + sa.sa_flags |= SA_RESTART; + if (sigaction(s, &sa, &osa) < 0) + return (SIG_ERR); + return (osa.sa_handler); +} diff --git a/lib/nbsd_libc/gen/signame.c b/lib/nbsd_libc/gen/signame.c new file mode 100644 index 000000000..a5f137d78 --- /dev/null +++ b/lib/nbsd_libc/gen/signame.c @@ -0,0 +1,111 @@ +/* $NetBSD: signame.c,v 1.13 2005/09/13 01:44:09 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "from: @(#)siglist.c 5.6 (Berkeley) 2/23/91";*/ +#else +__RCSID("$NetBSD: signame.c,v 1.13 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +static const char *const __signame14[] = { + "Signal 0", /* 0 */ + "HUP", /* 1 SIGHUP */ + "INT", /* 2 SIGINT */ + "QUIT", /* 3 SIGQUIT */ + "ILL", /* 4 SIGILL */ + "TRAP", /* 5 SIGTRAP */ + "ABRT", /* 6 SIGABRT */ + "EMT", /* 7 SIGEMT */ + "FPE", /* 8 SIGFPE */ + "KILL", /* 9 SIGKILL */ + "BUS", /* 10 SIGBUS */ + "SEGV", /* 11 SIGSEGV */ + "SYS", /* 12 SIGSYS */ + "PIPE", /* 13 SIGPIPE */ + "ALRM", /* 14 SIGALRM */ + "TERM", /* 15 SIGTERM */ + "URG", /* 16 SIGURG */ + "STOP", /* 17 SIGSTOP */ + "TSTP", /* 18 SIGTSTP */ + "CONT", /* 19 SIGCONT */ + "CHLD", /* 20 SIGCHLD */ + "TTIN", /* 21 SIGTTIN */ + "TTOU", /* 22 SIGTTOU */ + "IO", /* 23 SIGIO */ + "XCPU", /* 24 SIGXCPU */ + "XFSZ", /* 25 SIGXFSZ */ + "VTALRM", /* 26 SIGVTALRM */ + "PROF", /* 27 SIGPROF */ + "WINCH", /* 28 SIGWINCH */ + "INFO", /* 29 SIGINFO */ + "USR1", /* 30 SIGUSR1 */ + "USR2", /* 31 SIGUSR2 */ + "PWR", /* 32 SIGPWR */ + "RT0", /* 33 SIGRTMIN + 0 */ + "RT1", /* 34 SIGRTMIN + 1 */ + "RT2", /* 35 SIGRTMIN + 2 */ + "RT3", /* 36 SIGRTMIN + 3 */ + "RT4", /* 37 SIGRTMIN + 4 */ + "RT5", /* 38 SIGRTMIN + 5 */ + "RT6", /* 39 SIGRTMIN + 6 */ + "RT7", /* 40 SIGRTMIN + 7 */ + "RT8", /* 41 SIGRTMIN + 8 */ + "RT9", /* 42 SIGRTMIN + 9 */ + "RT10", /* 43 SIGRTMIN + 10 */ + "RT11", /* 44 SIGRTMIN + 11 */ + "RT12", /* 45 SIGRTMIN + 12 */ + "RT13", /* 46 SIGRTMIN + 13 */ + "RT14", /* 47 SIGRTMIN + 14 */ + "RT15", /* 48 SIGRTMIN + 15 */ + "RT16", /* 49 SIGRTMIN + 16 */ + "RT17", /* 50 SIGRTMIN + 17 */ + "RT18", /* 51 SIGRTMIN + 18 */ + "RT19", /* 52 SIGRTMIN + 19 */ + "RT20", /* 53 SIGRTMIN + 20 */ + "RT21", /* 54 SIGRTMIN + 21 */ + "RT22", /* 55 SIGRTMIN + 22 */ + "RT23", /* 56 SIGRTMIN + 23 */ + "RT24", /* 57 SIGRTMIN + 24 */ + "RT25", /* 58 SIGRTMIN + 25 */ + "RT26", /* 59 SIGRTMIN + 26 */ + "RT27", /* 60 SIGRTMIN + 27 */ + "RT28", /* 61 SIGRTMIN + 28 */ + "RT29", /* 62 SIGRTMIN + 29 */ + "RT30", /* 63 SIGRTMIN + 30 */ +}; + +const char * const *__sys_signame14 = __signame14; diff --git a/lib/nbsd_libc/gen/signbit.3 b/lib/nbsd_libc/gen/signbit.3 new file mode 100644 index 000000000..71e3eb599 --- /dev/null +++ b/lib/nbsd_libc/gen/signbit.3 @@ -0,0 +1,75 @@ +.\" $NetBSD: signbit.3,v 1.3 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2003 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 October 29, 2003 +.Dt SIGNBIT 3 +.Os +.Sh NAME +.Nm signbit +.Nd test sign +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In math.h +.Ft int +.Fn signbit "real-floating x" +.Sh DESCRIPTION +The +.Fn signbit +macro determines whether the sign of its argument value +.Fa x +is negative. +An argument represented in a format wider than its semantic type is +converted to its semantic type first. +The determination is then based on the type of the argument. +.Ss IEEE754 +The sign is determined for all values, including infinities, zeroes, +and NaNs +.Ss VAX +The sign is determined for finites, true zeros, and dirty zeroes; +for ROPs the sign is reported negative. +.Sh RETURN VALUES +The +.Fn signbit +macro returns a non-zero value if the sign of its value +.Fa x +is negative. +Otherwise 0 is returned. +.Sh ERRORS +No errors are defined. +.Sh SEE ALSO +.Xr fpclassify 3 , +.Xr isfinite 3 , +.Xr isnormal 3 , +.Xr math 3 +.Sh STANDARDS +The +.Fn signbit +macro conforms to +.St -isoC-99 . diff --git a/lib/nbsd_libc/gen/signbitd_ieee754.c b/lib/nbsd_libc/gen/signbitd_ieee754.c new file mode 100644 index 000000000..9ea2f169f --- /dev/null +++ b/lib/nbsd_libc/gen/signbitd_ieee754.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitd_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: signbitd_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * IEEE 754 double-precision version + */ +int +__signbitd(double x) +{ + union ieee_double_u u; + + u.dblu_d = x; + + return (u.dblu_dbl.dbl_sign == 1); +} diff --git a/lib/nbsd_libc/gen/signbitf_ieee754.c b/lib/nbsd_libc/gen/signbitf_ieee754.c new file mode 100644 index 000000000..df63bd7bc --- /dev/null +++ b/lib/nbsd_libc/gen/signbitf_ieee754.c @@ -0,0 +1,52 @@ +/* $NetBSD: signbitf_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: signbitf_ieee754.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include +#include + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * IEEE 754 single-precision version + */ +int +__signbitf(float x) +{ + union ieee_single_u u; + + u.sngu_f = x; + + return (u.sngu_sng.sng_sign == 1); +} diff --git a/lib/nbsd_libc/gen/signbitl_ieee754.c b/lib/nbsd_libc/gen/signbitl_ieee754.c new file mode 100644 index 000000000..7e8b1535e --- /dev/null +++ b/lib/nbsd_libc/gen/signbitl_ieee754.c @@ -0,0 +1,56 @@ +/* $NetBSD: signbitl_ieee754.c,v 1.1 2011/01/17 23:53:03 matt Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD"); +#endif + +#include +#include + +#ifdef __HAVE_LONG_DOUBLE + +/* + * 7.12.3.6 signbit - determine whether the sign of an argument is negative + * IEEE 754 compatible 128-bit extended-precision version + */ +int +__signbitl(long double x) +{ + union ieee_ext_u u; + + u.extu_ld = x; + + return (u.extu_ext.ext_sign == 1); +} + +#endif /* __HAVE_LONG_DOUBLE */ diff --git a/lib/nbsd_libc/gen/sigrelse.3 b/lib/nbsd_libc/gen/sigrelse.3 new file mode 100644 index 000000000..f70cc49e0 --- /dev/null +++ b/lib/nbsd_libc/gen/sigrelse.3 @@ -0,0 +1,80 @@ +.\" $NetBSD: sigrelse.3,v 1.4 2010/04/30 04:39:16 jruoho Exp $ +.\" +.\" Copyright (c) 2003 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 April 30, 2010 +.Dt SIGRELSE 3 +.Os +.Sh NAME +.Nm sigrelse +.Nd manipulate current signal mask +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn sigrelse "int sig" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by +.Xr sigprocmask 2 . +.Ef +.Pp +The +.Fn sigrelse +function removes the signal +.Fa sig +from the calling process' signal mask. +.Sh RETURN VALUES +If successful, the +.Fn sigrelse +function returns 0. +Otherwise \-1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn sigrelse +function will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +The argument +.Fa sig +is not a valid signal number. +.El +.Sh SEE ALSO +.Xr sigprocmask 2 , +.Xr sighold 3 +.Sh STANDARDS +The +.Fn sigrelse +function conforms to +.St -p1003.1-2001 . +It was however marked as obsolete in the +.St -p1003.1-2008 +revision of the standard. diff --git a/lib/nbsd_libc/gen/sigrelse.c b/lib/nbsd_libc/gen/sigrelse.c new file mode 100644 index 000000000..6a6643efc --- /dev/null +++ b/lib/nbsd_libc/gen/sigrelse.c @@ -0,0 +1,51 @@ +/* $NetBSD: sigrelse.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sigrelse.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include + +int +sigrelse(int sig) +{ + sigset_t set; + + sigemptyset(&set); + if (sigaddset(&set, sig) != 0) + return (-1); + + return (sigprocmask(SIG_UNBLOCK, &set, NULL)); +} diff --git a/lib/nbsd_libc/gen/sigset.3 b/lib/nbsd_libc/gen/sigset.3 new file mode 100644 index 000000000..c5da28189 --- /dev/null +++ b/lib/nbsd_libc/gen/sigset.3 @@ -0,0 +1,125 @@ +.\" $NetBSD: sigset.3,v 1.8 2010/04/30 06:48:20 wiz Exp $ +.\" +.\" Copyright (c) 2003 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 April 30, 2010 +.Dt SIGSET 3 +.Os +.Sh NAME +.Nm sigset +.Nd manipulate signal dispositions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.\" The following is Quite Ugly, but syntactically correct. Don't try to +.\" fix it. +.Ft void \*(lp* +.Fn sigset "int sig" "void \*(lp*disp\*(rp\*(lpint\*(rp\*(rp\*(rp\*(lpint" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by +.Xr sigaction 2 +and +.Xr sigprocmask 2 . +.Ef +.Pp +The +.Fn sigset +function manipulates the disposition of the signal +.Fa sig . +The new disposition is given in +.Fa disp . +.Pp +If +.Fa disp +is one of +.Dv SIG_DFL , +.Dv SIG_IGN , +or the address of a handler function, +the disposition of +.Fa sig +is changed accordingly, and +.Fa sig +is removed from the process' signal mask. +Also, if +.Fa disp +is the address of a handler function, +.Fa sig +will be added to the process' signal mask during execution of the handler. +.Pp +If +.Fa disp +is equal to +.Dv SIG_HOLD , +.Fa sig +is added to the calling process' signal mask and the disposition of +.Fa sig +remains unchanged. +.Sh RETURN VALUES +If successful, the +.Fn sigset +function returns +.Dv SIG_HOLD +if +.Fa sig +had been blocked, +and the previous disposition of +.Fa sig +if it had not been blocked. +Otherwise +.Dv SIG_ERR +is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn sigset +function will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +The argument +.Fa sig +is not a valid signal number; +or an attempt is made to ignore a signal that cannot be ignored, +such as +.Dv SIGKILL +or +.Dv SIGSTOP . +.El +.Sh SEE ALSO +.Xr sigaction 2 , +.Xr sigprocmask 2 +.Sh STANDARDS +The +.Fn sigset +function conforms to +.St -p1003.1-2001 . +It was however marked as obsolete in the +.St -p1003.1-2008 +revision of the standard. diff --git a/lib/nbsd_libc/gen/sigset.c b/lib/nbsd_libc/gen/sigset.c new file mode 100644 index 000000000..466f41da1 --- /dev/null +++ b/lib/nbsd_libc/gen/sigset.c @@ -0,0 +1,95 @@ +/* $NetBSD: sigset.c,v 1.2 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 2003 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sigset.c,v 1.2 2008/04/28 20:22:59 martin Exp $"); +#endif + +#include "namespace.h" +#include +#include +#include + +sig_t +sigset(int sig, void (*disp)(int)) +{ + sigset_t set, oset; + struct sigaction sa, osa; + + osa.sa_handler = SIG_ERR; + + if (disp == SIG_HOLD) { + /* Add sig to current signal mask. */ + if (sigemptyset(&set) != 0) + goto out; + if (sigaddset(&set, sig) != 0) + goto out; + if (sigprocmask(SIG_BLOCK, &set, &oset) != 0) + goto out; + + if (sigismember(&oset, sig)) { + /* Had been masked before, return SIG_HOLD. */ + osa.sa_handler = SIG_HOLD; + } else { + /* Return previous disposition. */ + (void)sigaction(sig, NULL, &osa); + } + } else if (disp == SIG_ERR) { + errno = EINVAL; + } else { + /* Set up and install new disposition. */ + sa.sa_handler = disp; + if (sigemptyset(&sa.sa_mask) != 0) + goto out; + sa.sa_flags = 0; + + if (sigaction(sig, &sa, &osa) != 0) { + osa.sa_handler = SIG_ERR; + goto out; + } + + /* Delete sig from current signal mask. */ + if (sigemptyset(&set) != 0) + return (SIG_ERR); + if (sigaddset(&set, sig) != 0) + return (SIG_ERR); + if (sigprocmask(SIG_UNBLOCK, &set, &oset) != 0) + return (SIG_ERR); + + /* If had been masked before, return SIG_HOLD. */ + if (sigismember(&oset, sig)) + osa.sa_handler = SIG_HOLD; + } + + out: + return (osa.sa_handler); +} diff --git a/lib/nbsd_libc/gen/sigsetops.3 b/lib/nbsd_libc/gen/sigsetops.3 new file mode 100644 index 000000000..e97e130da --- /dev/null +++ b/lib/nbsd_libc/gen/sigsetops.3 @@ -0,0 +1,124 @@ +.\" $NetBSD: sigsetops.3,v 1.14 2003/08/07 16:42:57 agc Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)sigsetops.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt SIGSETOPS 3 +.Os +.Sh NAME +.Nm sigemptyset , +.Nm sigfillset , +.Nm sigaddset , +.Nm sigdelset , +.Nm sigismember +.Nd manipulate signal sets +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In signal.h +.Ft int +.Fn sigemptyset "sigset_t *set" +.Ft int +.Fn sigfillset "sigset_t *set" +.Ft int +.Fn sigaddset "sigset_t *set" "int signo" +.Ft int +.Fn sigdelset "sigset_t *set" "int signo" +.Ft int +.Fn sigismember "sigset_t *set" "int signo" +.Sh DESCRIPTION +These functions manipulate signal sets stored in a +.Fa sigset_t . +Either +.Fn sigemptyset +or +.Fn sigfillset +must be called for every object of type +.Fa sigset_t +before any other use of the object. +.Pp +The +.Fn sigemptyset +function initializes a signal set to be empty. +.Pp +The +.Fn sigfillset +function initializes a signal set to contain all signals. +.Pp +The +.Fn sigaddset +function adds the specified signal +.Fa signo +to the signal set. +.Pp +The +.Fn sigdelset +function deletes the specified signal +.Fa signo +from the signal set. +.Pp +The +.Fn sigismember +function returns whether a specified signal +.Fa signo +is contained in the signal set. +.Pp +.Fn sigemptyset +and +.Fn sigfillset +are provided as macros, but actual functions are available +if their names are undefined (with #undef +.Em name ) . +.Sh RETURN VALUES +The +.Fn sigismember +function returns 1 +if the signal is a member of the set, +a 0 otherwise. +The other functions return 0 upon success. +A \-1 return value +indicates an error occurred and the global variable +.Va errno +is set to indicate the reason. +.Sh ERRORS +These functions could fail if one of the following occurs: +.Bl -tag -width Er +.It Bq Er EINVAL +.Fa signo +has an invalid value. +.El +.Sh SEE ALSO +.Xr kill 2 , +.Xr sigaction 2 , +.Xr sigsuspend 2 , +.Xr signal 7 +.Sh STANDARDS +These functions conform to +.St -p1003.1-90 . diff --git a/lib/nbsd_libc/gen/sigsetops.c b/lib/nbsd_libc/gen/sigsetops.c new file mode 100644 index 000000000..5b227355a --- /dev/null +++ b/lib/nbsd_libc/gen/sigsetops.c @@ -0,0 +1,46 @@ +/* $NetBSD: sigsetops.c,v 1.16 2010/07/31 00:04:43 joerg Exp $ */ + +/*- + * Copyright (c) 1989, 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. + * + * @(#)sigsetops.c 8.1 (Berkeley) 6/4/93 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sigsetops.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: sigsetops.c,v 1.16 2010/07/31 00:04:43 joerg Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#define __SIGSETOPS_BODY + +#include +#include diff --git a/lib/nbsd_libc/gen/sleep.3 b/lib/nbsd_libc/gen/sleep.3 new file mode 100644 index 000000000..9cf8e19a3 --- /dev/null +++ b/lib/nbsd_libc/gen/sleep.3 @@ -0,0 +1,77 @@ +.\" $NetBSD: sleep.3,v 1.15 2003/08/07 16:42:57 agc Exp $ +.\" +.\" Copyright (c) 1986, 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. +.\" +.\" @(#)sleep.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt SLEEP 3 +.Os +.Sh NAME +.Nm sleep +.Nd suspend process execution for interval of seconds +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft unsigned int +.Fn sleep "unsigned int seconds" +.Sh DESCRIPTION +The +.Fn sleep +function suspends execution of the calling process until either the +number of seconds specified by +.Fa seconds +have elapsed or a signal is delivered to the calling process and its +action is to invoke a signal-catching function or to terminate the +process. +The suspension time may be longer than requested due to the +scheduling of other activity by the system. +.Sh RETURN VALUES +If the +.Fn sleep +function returns because the requested time has elapsed, the value +returned will be zero. +If the +.Fn sleep +function returns due to the delivery of a signal, the value returned +will be the unslept amount (the request time minus the time actually +slept) in seconds. +.Sh SEE ALSO +.Xr nanosleep 2 , +.Xr usleep 3 +.Sh STANDARDS +The +.Fn sleep +function conforms to +.St -p1003.1-90 . +.Sh HISTORY +A +.Fn sleep +function appeared in +.At v7 . diff --git a/lib/nbsd_libc/gen/sleep.c b/lib/nbsd_libc/gen/sleep.c new file mode 100644 index 000000000..a6b3cc9b7 --- /dev/null +++ b/lib/nbsd_libc/gen/sleep.c @@ -0,0 +1,57 @@ +/* $NetBSD: sleep.c,v 1.22 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sleep.c,v 1.22 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(sleep,_sleep) +#endif + +unsigned int +sleep(seconds) + unsigned int seconds; +{ + struct timespec rqt, rmt; + + rqt.tv_sec = seconds; + rqt.tv_nsec = 0; + + nanosleep(&rqt, &rmt); + + return (unsigned int)rmt.tv_sec; +} diff --git a/lib/nbsd_libc/gen/stringlist.3 b/lib/nbsd_libc/gen/stringlist.3 new file mode 100644 index 000000000..2eb83fc6a --- /dev/null +++ b/lib/nbsd_libc/gen/stringlist.3 @@ -0,0 +1,144 @@ +.\" $NetBSD: stringlist.3,v 1.15 2010/05/06 09:46:49 jruoho Exp $ +.\" +.\" Copyright (c) 1997, 1999 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This file was contributed to The NetBSD Foundation by Luke Mewburn. +.\" +.\" 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 May 6, 2010 +.Dt STRINGLIST 3 +.Os +.Sh NAME +.Nm stringlist , +.Nm sl_init , +.Nm sl_add , +.Nm sl_free , +.Nm sl_find , +.Nm sl_delete +.Nd stringlist manipulation functions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stringlist.h +.Ft StringList * +.Fn sl_init +.Ft int +.Fn sl_add "StringList *sl" "char *item" +.Ft void +.Fn sl_free "StringList *sl" "int freeall" +.Ft char * +.Fn sl_find "StringList *sl" "const char *item" +.Ft int +.Fn sl_delete "StringList *sl" "const char *item" "int freeit" +.Sh DESCRIPTION +The +.Nm +functions manipulate stringlists, which are lists of +strings that extend automatically if necessary. +.Pp +The +.Ar StringList +structure has the following definition: +.Bd -literal -offset indent +typedef struct _stringlist { + char **sl_str; + size_t sl_max; + size_t sl_cur; +} StringList; +.Ed +.Pp +where: +.Bl -tag -width "sl_str" -offset indent +.It Ar sl_str +is a pointer to the base of the array containing the list, +.It Ar sl_max +is the size of +.Ar sl_str , +and +.It Ar sl_cur +is the offset in +.Ar sl_str +of the current element. +.El +.Pp +The following stringlist manipulation functions are available: +.Bl -tag -width "sl_delete()" -offset 2n +.It Fn sl_init +Create a stringlist. +Returns a pointer to a +.Ar StringList , +or +.Dv NULL +in case of failure. +.It Fn sl_free +Releases memory occupied by +.Ar sl +and the +.Ar sl-\*[Gt]sl_str +array. +If +.Ar freeall +is non-zero, then each of the items within +.Ar sl-\*[Gt]sl_str +is released as well. +.It Fn sl_add +Add +.Ar item +to +.Ar sl-\*[Gt]sl_str +at +.Ar sl-\*[Gt]sl_cur , +extending the size of +.Ar sl-\*[Gt]sl_str . +Returns zero upon success, \-1 upon failure. +.It Fn sl_find +Find +.Ar item +in +.Ar sl , +returning +.Dv NULL +if it's not found. +.It Fn sl_delete +Remove +.Ar item +from the list. +If +.Ar freeit +is non-zero, the string is freed. +Returns +.Dv 0 +if the name is found +and +.Dv \-1 +if the name is not found. +.El +.Sh SEE ALSO +.Xr free 3 , +.Xr malloc 3 +.Sh HISTORY +The +.Nm +functions appeared in +.Nx 1.3 . diff --git a/lib/nbsd_libc/gen/stringlist.c b/lib/nbsd_libc/gen/stringlist.c new file mode 100644 index 000000000..0be588c21 --- /dev/null +++ b/lib/nbsd_libc/gen/stringlist.c @@ -0,0 +1,156 @@ +/* $NetBSD: stringlist.c,v 1.13 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: stringlist.c,v 1.13 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(sl_add,_sl_add) +__weak_alias(sl_find,_sl_find) +__weak_alias(sl_free,_sl_free) +__weak_alias(sl_init,_sl_init) +__weak_alias(sl_delete,_sl_delete) +#endif + +#define _SL_CHUNKSIZE 20 + +/* + * sl_init(): Initialize a string list + */ +StringList * +sl_init(void) +{ + StringList *sl; + + sl = malloc(sizeof(StringList)); + if (sl == NULL) + return NULL; + + sl->sl_cur = 0; + sl->sl_max = _SL_CHUNKSIZE; + sl->sl_str = malloc(sl->sl_max * sizeof(char *)); + if (sl->sl_str == NULL) { + free(sl); + sl = NULL; + } + return sl; +} + + +/* + * sl_add(): Add an item to the string list + */ +int +sl_add(StringList *sl, char *name) +{ + + _DIAGASSERT(sl != NULL); + + if (sl->sl_cur == sl->sl_max - 1) { + char **new; + + new = realloc(sl->sl_str, + (sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *)); + if (new == NULL) + return -1; + sl->sl_max += _SL_CHUNKSIZE; + sl->sl_str = new; + } + sl->sl_str[sl->sl_cur++] = name; + return 0; +} + + +/* + * sl_free(): Free a stringlist + */ +void +sl_free(StringList *sl, int all) +{ + size_t i; + + if (sl == NULL) + return; + if (sl->sl_str) { + if (all) + for (i = 0; i < sl->sl_cur; i++) + free(sl->sl_str[i]); + free(sl->sl_str); + } + free(sl); +} + + +/* + * sl_find(): Find a name in the string list + */ +char * +sl_find(StringList *sl, const char *name) +{ + size_t i; + + _DIAGASSERT(sl != NULL); + + for (i = 0; i < sl->sl_cur; i++) + if (strcmp(sl->sl_str[i], name) == 0) + return sl->sl_str[i]; + + return NULL; +} + +int +sl_delete(StringList *sl, const char *name, int all) +{ + size_t i, j; + + for (i = 0; i < sl->sl_cur; i++) + if (strcmp(sl->sl_str[i], name) == 0) { + if (all) + free(sl->sl_str[i]); + for (j = i + 1; j < sl->sl_cur; j++) + sl->sl_str[j - 1] = sl->sl_str[j]; + sl->sl_str[--sl->sl_cur] = NULL; + return 0; + } + return -1; +} + diff --git a/lib/nbsd_libc/gen/sysconf.3 b/lib/nbsd_libc/gen/sysconf.3 new file mode 100644 index 000000000..34235f898 --- /dev/null +++ b/lib/nbsd_libc/gen/sysconf.3 @@ -0,0 +1,329 @@ +.\" $NetBSD: sysconf.3,v 1.37 2010/03/22 19:30:54 joerg Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)sysconf.3 8.3 (Berkeley) 4/19/94 +.\" +.Dd August 6, 2008 +.Dt SYSCONF 3 +.Os +.Sh NAME +.Nm sysconf +.Nd get configurable system variables +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft long +.Fn sysconf "int name" +.Sh DESCRIPTION +This interface is defined by +.St -p1003.1-88 . +A far more complete interface is available using +.Xr sysctl 3 . +.Pp +The +.Fn sysconf +function provides a method for applications to determine the current +value of a configurable system limit or option variable. +The +.Fa name +argument specifies the system variable to be queried. +Symbolic constants for each name value are found in the include file +.In unistd.h . +.Pp +The available values are as follows: +.Bl -tag -width "123456" +.It Li _SC_ARG_MAX +The maximum bytes of argument to +.Xr execve 2 . +.It Li _SC_ATEXIT_MAX +The maxmimum number of functions that may be registered with +.Xr atexit 3 . +.It Li _SC_BARRIERS +The version of +.St -p1003.1 +and its +Barriers +option to which the system attempts to conform, +otherwise \-1. +.It Li _SC_CLOCK_SELECTION +Return the +.Tn POSIX +version the implementation of the Clock Selection Option +on this system conforms to, +or \-1 if unavailable. +.It Li _SC_CHILD_MAX +The maximum number of simultaneous processes per user id. +.It Li _SC_CLK_TCK +The number of clock ticks per second. +.It Li _SC_FSYNC +Return 1 if the File Synchronization Option is available on this system, +otherwise \-1. +.It Li _SC_HOST_NAME_MAX +The maximum size of a hostname, including NULL. +.It Li _SC_IOV_MAX +The maximum number of +.Va iovec +structures that a process has available for use with +.Xr preadv 2 , +.Xr pwritev 2 , +.Xr readv 2 , +.Xr recvmsg 2 , +.Xr sendmsg 2 +or +.Xr writev 2 . +.It Li _SC_JOB_CONTROL +Return 1 if job control is available on this system, otherwise \-1. +.It Li _SC_LOGIN_NAME_MAX +Returns the size of the storage required for a login name, in bytes, +including the terminating NUL. +.It Li _SC_MAPPED_FILES +Return 1 if the Memory Mapped Files Option is available on this system, +otherwise \-1. +.It Li _SC_MEMLOCK +Return 1 if the Process Memory Locking Option is available on this system, +otherwise \-1. +.It Li _SC_MEMLOCK_RANGE +Return 1 if the Range Memory Locking Option is available on this system, +otherwise \-1. +.It Li _SC_MEMORY_PROTECTION +Return 1 if the Memory Protection Option is available on this system, +otherwise \-1. +.It Li _SC_MONOTONIC_CLOCK +Return the +.Tn POSIX +version the implementation of the Monotonic Clock Option +on this system conforms to, +or \-1 if unavailable. +.It Li _SC_NGROUPS_MAX +The maximum number of supplemental groups. +.It Li _SC_OPEN_MAX +The maximum number of open files per process. +.It Li _SC_PAGESIZE +The size of a system page in bytes. +.It Li _SC_PASS_MAX +The maximum length of the password, not counting NULL. +.It Li _SC_READER_WRITER_LOCKS +The version of +.St -p1003.1 +and its +Read-Write Locks +option to which the system attempts to conform, +otherwise \-1. +.It Li _SC_REGEXP +Return 1 if +.Tn POSIX +regular expressions are available on this system, otherwise \-1. +.It Li _SC_SEMAPHORES +The version of +.St -p1003.1 +and its +Semaphores +option to which the system attempts to conform, +otherwise \-1. +.Pp +Availability of the +Semaphores +option depends on the +.Li P1003_1B_SEMAPHORE +kernel option. +.It Li _SC_SHELL +Return 1 if +.Tn POSIX +shell is available on this system, otherwise \-1. +.It Li _SC_SPIN_LOCKS +The version of +.St -p1003.1 +and its +Spin Locks +option to which the system attempts to conform, +otherwise \-1. +.It Li _SC_STREAM_MAX +The minimum maximum number of streams that a process may have open +at any one time. +.It Li _SC_SYMLOOP_MAX +The maximum number of symbolic links that may be expanded in a path name. +.It Li _SC_SYNCHRONIZED_IO +Return 1 if the Synchronized I/O Option is available on this system, +otherwise \-1. +.It Li _SC_THREADS +The version of +.St -p1003.1 +and its +Threads +option to which the system attempts to conform, +otherwise \-1. +.It Li _SC_TIMERS +The version of +.St -p1003.1 +and its +Timers +option to which the system attempts to conform, +otherwise \-1. +.It Li _SC_TZNAME_MAX +The minimum maximum number of types supported for the name of a +timezone. +.It Li _SC_SAVED_IDS +Returns 1 if saved set-group and saved set-user ID is available, +otherwise \-1. +.It Li _SC_VERSION +The version of ISO/IEC 9945 (POSIX 1003.1) with which the system +attempts to comply. +.It Li _SC_XOPEN_SHM +Return 1 if the +.St -xpg4.2 +Shared Memory +option is available on this system, +otherwise \-1. +.Pp +Availability of the +Shared Memory +option depends on the +.Li SYSVSHM +kernel option. +.It Li _SC_BC_BASE_MAX +The maximum ibase/obase values in the +.Xr bc 1 +utility. +.It Li _SC_BC_DIM_MAX +The maximum array size in the +.Xr bc 1 +utility. +.It Li _SC_BC_SCALE_MAX +The maximum scale value in the +.Xr bc 1 +utility. +.It Li _SC_BC_STRING_MAX +The maximum string length in the +.Xr bc 1 +utility. +.It Li _SC_COLL_WEIGHTS_MAX +The maximum number of weights that can be assigned to any entry of +the LC_COLLATE order keyword in the locale definition file. +.It Li _SC_EXPR_NEST_MAX +The maximum number of expressions that can be nested within +parenthesis by the +.Xr expr 1 +utility. +.It Li _SC_LINE_MAX +The maximum length in bytes of a text-processing utility's input +line. +.It Li _SC_RE_DUP_MAX +The maximum number of repeated occurrences of a regular expression +permitted when using interval notation. +.It Li _SC_2_VERSION +The version of POSIX 1003.2 with which the system attempts to comply. +.It Li _SC_2_C_BIND +Return 1 if the system's C-language development facilities support the +C-Language Bindings Option, otherwise \-1. +.It Li _SC_2_C_DEV +Return 1 if the system supports the C-Language Development Utilities Option, +otherwise \-1. +.It Li _SC_2_CHAR_TERM +Return 1 if the system supports at least one terminal type capable of +all operations described in POSIX 1003.2, otherwise \-1. +.It Li _SC_2_FORT_DEV +Return 1 if the system supports the FORTRAN Development Utilities Option, +otherwise \-1. +.It Li _SC_2_FORT_RUN +Return 1 if the system supports the FORTRAN Runtime Utilities Option, +otherwise \-1. +.It Li _SC_2_LOCALEDEF +Return 1 if the system supports the creation of locales, otherwise \-1. +.It Li _SC_2_SW_DEV +Return 1 if the system supports the Software Development Utilities Option, +otherwise \-1. +.It Li _SC_2_UPE +Return 1 if the system supports the User Portability Utilities Option, +otherwise \-1. +.It Li _SC_GETGR_R_SIZE_MAX +The minimum size of the +.Fa buffer +passed to +.Xr getgrgid_r 3 +and +.Xr getgrnam_r 3 . +.It Li _SC_GETPW_R_SIZE_MAX +The minimum size of the +.Fa buffer +passed to +.Xr getpwnam_r 3 +and +.Xr getpwuid_r 3 . +.It Li _SC_NPROCESSORS_CONF +The number of processors configured. +.It Li _SC_NPROCESSORS_ONLN +The number of processors online (capable of running processes). +.El +.Sh RETURN VALUES +If the call to +.Nm sysconf +is not successful, \-1 is returned and +.Va errno +is set appropriately. +Otherwise, if the variable is associated with functionality that is not +supported, \-1 is returned and +.Va errno +is not modified. +Otherwise, the current variable value is returned. +.Sh ERRORS +The +.Fn sysconf +function may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr sysctl 3 . +In addition, the following error may be reported: +.Bl -tag -width Er +.It Bq Er EINVAL +The value of the +.Fa name +argument is invalid. +.El +.Sh SEE ALSO +.Xr sysctl 3 +.Sh STANDARDS +The +.Fn sysconf +function conforms to +.St -p1003.1-90 . +The constants +.Li _SC_NPROCESSORS_CONF +and +.Li _SC_NPROCESSORS_ONLN +are not part of the standard, but are provided by many systems. +.Sh HISTORY +The +.Nm sysconf +function first appeared in +.Bx 4.4 . +.Sh BUGS +The value for _SC_STREAM_MAX is a minimum maximum, and required to be +the same as ANSI C's FOPEN_MAX, so the returned value is a ridiculously +small and misleading number. diff --git a/lib/nbsd_libc/gen/sysconf.c b/lib/nbsd_libc/gen/sysconf.c new file mode 100644 index 000000000..48e47a8e5 --- /dev/null +++ b/lib/nbsd_libc/gen/sysconf.c @@ -0,0 +1,413 @@ +/* $NetBSD: sysconf.c,v 1.33 2008/08/06 17:17:04 matt Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Sean Eric Fagan of Cygnus Support. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94"; +#else +__RCSID("$NetBSD: sysconf.c,v 1.33 2008/08/06 17:17:04 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(sysconf,__sysconf) +#endif + +/* + * sysconf -- + * get configurable system variables. + * + * XXX + * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values + * not change during the lifetime of the calling process. This would seem + * to require that any change to system limits kill all running processes. + * A workaround might be to cache the values when they are first retrieved + * and then simply return the cached value on subsequent calls. This is + * less useful than returning up-to-date values, however. + */ +long +sysconf(int name) +{ + struct rlimit rl; + size_t len; + int mib[CTL_MAXNAME], value; + unsigned int mib_len; + struct clockinfo tmpclock; + static int clk_tck; + + len = sizeof(value); + + /* Default length of the MIB */ + mib_len = 2; + + switch (name) { + +/* 1003.1 */ + case _SC_ARG_MAX: + mib[0] = CTL_KERN; + mib[1] = KERN_ARGMAX; + break; + case _SC_CHILD_MAX: + return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : (long)rl.rlim_cur); + case _O_SC_CLK_TCK: + /* + * For applications compiled when CLK_TCK was a compile-time + * constant. + */ + return 100; + case _SC_CLK_TCK: + /* + * Has to be handled specially because it returns a + * struct clockinfo instead of an integer. Also, since + * this might be called often by some things that + * don't grok CLK_TCK can be a macro expanding to a + * function, cache the value. + */ + if (clk_tck == 0) { + mib[0] = CTL_KERN; + mib[1] = KERN_CLOCKRATE; + len = sizeof(struct clockinfo); + clk_tck = sysctl(mib, 2, &tmpclock, &len, NULL, 0) + == -1 ? -1 : tmpclock.hz; + } + return(clk_tck); + case _SC_JOB_CONTROL: + mib[0] = CTL_KERN; + mib[1] = KERN_JOB_CONTROL; + goto yesno; + case _SC_NGROUPS_MAX: + mib[0] = CTL_KERN; + mib[1] = KERN_NGROUPS; + break; + case _SC_OPEN_MAX: + return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : (long)rl.rlim_cur); + case _SC_STREAM_MAX: + mib[0] = CTL_USER; + mib[1] = USER_STREAM_MAX; + break; + case _SC_TZNAME_MAX: + mib[0] = CTL_USER; + mib[1] = USER_TZNAME_MAX; + break; + case _SC_SAVED_IDS: + mib[0] = CTL_KERN; + mib[1] = KERN_SAVED_IDS; + goto yesno; + case _SC_VERSION: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX1; + break; + +/* 1003.1b */ + case _SC_PAGESIZE: + return _getpagesize(); + case _SC_FSYNC: + mib[0] = CTL_KERN; + mib[1] = KERN_FSYNC; + goto yesno; + case _SC_SYNCHRONIZED_IO: + mib[0] = CTL_KERN; + mib[1] = KERN_SYNCHRONIZED_IO; + goto yesno; + case _SC_MAPPED_FILES: + mib[0] = CTL_KERN; + mib[1] = KERN_MAPPED_FILES; + goto yesno; + case _SC_MEMLOCK: + mib[0] = CTL_KERN; + mib[1] = KERN_MEMLOCK; + goto yesno; + case _SC_MEMLOCK_RANGE: + mib[0] = CTL_KERN; + mib[1] = KERN_MEMLOCK_RANGE; + goto yesno; + case _SC_MEMORY_PROTECTION: + mib[0] = CTL_KERN; + mib[1] = KERN_MEMORY_PROTECTION; + goto yesno; + case _SC_MONOTONIC_CLOCK: + mib[0] = CTL_KERN; + mib[1] = KERN_MONOTONIC_CLOCK; + goto yesno; + case _SC_SEMAPHORES: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX_SEMAPHORES; + goto yesno; + case _SC_TIMERS: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX_TIMERS; + goto yesno; + +/* 1003.1c */ + case _SC_LOGIN_NAME_MAX: + mib[0] = CTL_KERN; + mib[1] = KERN_LOGIN_NAME_MAX; + break; + case _SC_THREADS: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX_THREADS; + goto yesno; + +/* 1003.1j */ + case _SC_BARRIERS: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX_BARRIERS; + goto yesno; + case _SC_SPIN_LOCKS: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX_SPIN_LOCKS; + goto yesno; + /* Historical; Threads option in 1003.1-2001 */ + case _SC_READER_WRITER_LOCKS: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX_READER_WRITER_LOCKS; + goto yesno; + +/* 1003.2 */ + case _SC_BC_BASE_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_BASE_MAX; + break; + case _SC_BC_DIM_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_DIM_MAX; + break; + case _SC_BC_SCALE_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_SCALE_MAX; + break; + case _SC_BC_STRING_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_STRING_MAX; + break; + case _SC_COLL_WEIGHTS_MAX: + mib[0] = CTL_USER; + mib[1] = USER_COLL_WEIGHTS_MAX; + break; + case _SC_EXPR_NEST_MAX: + mib[0] = CTL_USER; + mib[1] = USER_EXPR_NEST_MAX; + break; + case _SC_LINE_MAX: + mib[0] = CTL_USER; + mib[1] = USER_LINE_MAX; + break; + case _SC_RE_DUP_MAX: + mib[0] = CTL_USER; + mib[1] = USER_RE_DUP_MAX; + break; + case _SC_2_VERSION: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_VERSION; + break; + case _SC_2_C_BIND: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_C_BIND; + goto yesno; + case _SC_2_C_DEV: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_C_DEV; + goto yesno; + case _SC_2_CHAR_TERM: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_CHAR_TERM; + goto yesno; + case _SC_2_FORT_DEV: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_FORT_DEV; + goto yesno; + case _SC_2_FORT_RUN: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_FORT_RUN; + goto yesno; + case _SC_2_LOCALEDEF: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_LOCALEDEF; + goto yesno; + case _SC_2_SW_DEV: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_SW_DEV; + goto yesno; + case _SC_2_UPE: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_UPE; + goto yesno; + +/* XPG 4.2 */ + case _SC_IOV_MAX: + mib[0] = CTL_KERN; + mib[1] = KERN_IOV_MAX; + break; + case _SC_XOPEN_SHM: + mib[0] = CTL_KERN; + mib[1] = KERN_SYSVIPC; + mib[2] = KERN_SYSVIPC_SHM; + mib_len = 3; + goto yesno; + +/* 1003.1-2001, XSI Option Group */ + case _SC_AIO_LISTIO_MAX: + if (sysctlgetmibinfo("kern.aio_listio_max", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + break; + case _SC_AIO_MAX: + if (sysctlgetmibinfo("kern.aio_max", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + break; + case _SC_ASYNCHRONOUS_IO: + if (sysctlgetmibinfo("kern.posix_aio", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + goto yesno; + case _SC_MESSAGE_PASSING: + if (sysctlgetmibinfo("kern.posix_msg", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + goto yesno; + case _SC_MQ_OPEN_MAX: + if (sysctlgetmibinfo("kern.mqueue.mq_open_max", &mib[0], + &mib_len, NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + break; + case _SC_MQ_PRIO_MAX: + if (sysctlgetmibinfo("kern.mqueue.mq_prio_max", &mib[0], + &mib_len, NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + break; + case _SC_PRIORITY_SCHEDULING: + if (sysctlgetmibinfo("kern.posix_sched", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + goto yesno; + case _SC_ATEXIT_MAX: + mib[0] = CTL_USER; + mib[1] = USER_ATEXIT_MAX; + break; + +/* 1003.1-2001, TSF */ + case _SC_GETGR_R_SIZE_MAX: + return _GETGR_R_SIZE_MAX; + case _SC_GETPW_R_SIZE_MAX: + return _GETPW_R_SIZE_MAX; + +/* Unsorted */ + case _SC_HOST_NAME_MAX: + return MAXHOSTNAMELEN; + case _SC_PASS_MAX: + return _PASSWORD_LEN; + case _SC_REGEXP: + return _POSIX_REGEXP; + case _SC_SHELL: + return _POSIX_SHELL; + case _SC_SYMLOOP_MAX: + return MAXSYMLINKS; + +yesno: if (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1) + return (-1); + if (value == 0) + return (-1); + return (value); + +/* Extensions */ + case _SC_NPROCESSORS_CONF: + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + break; + case _SC_NPROCESSORS_ONLN: + mib[0] = CTL_HW; + mib[1] = HW_NCPUONLINE; + break; + +/* Native */ + case _SC_SCHED_RT_TS: + if (sysctlgetmibinfo("kern.sched.rtts", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + break; + case _SC_SCHED_PRI_MIN: + if (sysctlgetmibinfo("kern.sched.pri_min", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + break; + case _SC_SCHED_PRI_MAX: + if (sysctlgetmibinfo("kern.sched.pri_max", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) + return -1; + break; + case _SC_THREAD_DESTRUCTOR_ITERATIONS: + return _POSIX_THREAD_DESTRUCTOR_ITERATIONS; + case _SC_THREAD_KEYS_MAX: + return _POSIX_THREAD_KEYS_MAX; + case _SC_THREAD_STACK_MIN: + return _getpagesize(); + case _SC_THREAD_THREADS_MAX: + if (sysctlgetmibinfo("kern.maxproc", &mib[0], &mib_len, + NULL, NULL, NULL, SYSCTL_VERSION)) /* XXX */ + return -1; + goto yesno; + case _SC_THREAD_ATTR_STACKADDR: + return _POSIX_THREAD_ATTR_STACKADDR; + case _SC_THREAD_ATTR_STACKSIZE: + return _POSIX_THREAD_ATTR_STACKSIZE; + case _SC_THREAD_SAFE_FUNCTIONS: + return _POSIX_THREAD_SAFE_FUNCTIONS; + case _SC_THREAD_PRIORITY_SCHEDULING: + case _SC_THREAD_PRIO_INHERIT: + case _SC_THREAD_PRIO_PROTECT: + case _SC_THREAD_PROCESS_SHARED: + return -1; + case _SC_TTY_NAME_MAX: + return pathconf(_PATH_DEV, _PC_NAME_MAX); + default: + errno = EINVAL; + return (-1); + } + return (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1 ? -1 : value); +} diff --git a/lib/nbsd_libc/gen/sysctl.3 b/lib/nbsd_libc/gen/sysctl.3 new file mode 100644 index 000000000..79da5557d --- /dev/null +++ b/lib/nbsd_libc/gen/sysctl.3 @@ -0,0 +1,724 @@ +.\" $NetBSD: sysctl.3,v 1.200 2010/03/22 19:30:54 joerg Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)sysctl.3 8.4 (Berkeley) 5/9/95 +.\" +.Dd September 26, 2009 +.Dt SYSCTL 3 +.Os +.Sh NAME +.Nm sysctl , +.Nm sysctlbyname , +.Nm sysctlgetmibinfo , +.Nm sysctlnametomib +.Nd get or set system information +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/param.h +.In sys/sysctl.h +.Ft int +.Fn sysctl "const int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" \ +"const void *newp" "size_t newlen" +.Ft int +.Fn sysctlbyname "const char *sname" "void *oldp" "size_t *oldlenp" \ +"const void *newp" "size_t newlen" +.Ft int +.Fn sysctlgetmibinfo "const char *sname" "int *name" "u_int *namelenp" \ +"char *cname" "size_t *csz" "struct sysctlnode **rnode" "int v" +.Ft int +.Fn sysctlnametomib "const char *sname" "int *name" "size_t *namelenp" +.Sh DESCRIPTION +The +.Nm +function retrieves system information and allows processes with +appropriate privileges to set system information. +The information available from +.Nm +consists of integers, strings, and tables. +Information may be retrieved and set from the command interface +using the +.Xr sysctl 8 +utility. +.Pp +Unless explicitly noted below, +.Nm +returns a consistent snapshot of the data requested. +Consistency is obtained by locking the destination +buffer into memory so that the data may be copied out without blocking. +Calls to +.Nm +are serialized to avoid deadlock. +.Pp +The state is described using a ``Management Information Base'' (MIB) +style name, listed in +.Fa name , +which is a +.Fa namelen +length array of integers. +.Pp +The +.Fn sysctlbyname +function accepts a string representation of a MIB entry and internally +maps it to the appropriate numeric MIB representation. +Its semantics are otherwise no different from +.Fn sysctl . +.Pp +The information is copied into the buffer specified by +.Fa oldp . +The size of the buffer is given by the location specified by +.Fa oldlenp +before the call, +and that location gives the amount of data copied after a successful call. +If the amount of data available is greater +than the size of the buffer supplied, +the call supplies as much data as fits in the buffer provided +and returns with the error code ENOMEM. +If the old value is not desired, +.Fa oldp +and +.Fa oldlenp +should be set to +.Dv NULL . +.Pp +The size of the available data can be determined by calling +.Nm +with a +.Dv NULL +parameter for +.Fa oldp . +The size of the available data will be returned in the location pointed to by +.Fa oldlenp . +For some operations, the amount of space may change often. +For these operations, +the system attempts to round up so that the returned size is +large enough for a call to return the data shortly thereafter. +.Pp +To set a new value, +.Fa newp +is set to point to a buffer of length +.Fa newlen +from which the requested value is to be taken. +If a new value is not to be set, +.Fa newp +should be set to +.Dv NULL +and +.Fa newlen +set to 0. +.Pp +The +.Fn sysctlnametomib +function can be used to map the string representation of a MIB entry +to the numeric version. +The +.Fa name +argument should point to an array of integers large enough to hold the +MIB, and +.Fa namelenp +should indicate the number of integer slots available. +Following a successful translation, the size_t indicated by +.Fa namelenp +will be changed to show the number of slots consumed. +.Pp +The +.Fn sysctlgetmibinfo +function performs name translation similar to +.Fn sysctlnametomib , +but also canonicalizes the name (or returns the first erroneous token +from the string being parsed) into the space indicated by +.Fa cname +and +.Fa csz . +.Fa csz +should indicate the size of the buffer pointed to by +.Fa cname +and on return, will indicate the size of the returned string including +the trailing +.Sq nul +character. +.Pp +The +.Fa rnode +and +.Fa v +arguments to +.Fn sysctlgetmibinfo +are used to provide a tree for it to parse into, and to get back +either a pointer to, or a copy of, the terminal node. +If +.Fa rnode +is +.Dv NULL , +.Fn sysctlgetmibinfo +uses its own internal tree for parsing, and checks it against the +kernel at each call, to make sure that the name-to-number mapping is +kept up to date. +The +.Fa v +argument is ignored in this case. +If +.Fa rnode +is not +.Dv NULL +but the pointer it references is, on a successful return, +.Fa rnode +will be adjusted to point to a copy of the terminal node. +The +.Fa v +argument indicates which version of the +.Nm +node structure the caller wants. +The application must later +.Fn free +this copy. +If neither +.Fa rnode +nor the pointer it references are +.Dv NULL , +the pointer is used as the address of a tree over which the parsing is +done. +In this last case, the tree is not checked against the kernel, no +refreshing of the mappings is performed, and the value given by +.Fa v +must agree with the version indicated by the tree. +It is recommended that applications always use +.Dv SYSCTL_VERSION +as the value for +.Fa v , +as defined in the include file +.Pa sys/sysctl.h . +.Pp +The numeric and text names of sysctl variables are described in +.Xr sysctl 7 . +The numeric names are defined as preprocessor macros. +The top level names are defined with a CTL_ prefix in +.In sys/sysctl.h . +The next and subsequent levels down have different prefixes for each +subtree. +.Pp +For example, the following retrieves the maximum number of processes allowed +in the system - the +.Li kern.maxproc +variable: +.Bd -literal -offset indent -compact +int mib[2], maxproc; +size_t len; +.sp +mib[0] = CTL_KERN; +mib[1] = KERN_MAXPROC; +len = sizeof(maxproc); +sysctl(mib, 2, \*[Am]maxproc, \*[Am]len, NULL, 0); +.Ed +.sp +To retrieve the standard search path for the system utilities - +.Li user.cs_path : +.Bd -literal -offset indent -compact +int mib[2]; +size_t len; +char *p; +.sp +mib[0] = CTL_USER; +mib[1] = USER_CS_PATH; +sysctl(mib, 2, NULL, \*[Am]len, NULL, 0); +p = malloc(len); +sysctl(mib, 2, p, \*[Am]len, NULL, 0); +.Ed +.Sh DYNAMIC OPERATIONS +Several meta-identifiers are provided to perform operations on the +.Nm +tree itself, or support alternate means of accessing the data +instrumented by the +.Nm +tree. +.Bl -column CTLXCREATESYMXXX +.It Sy Name Description +.It CTL\_QUERY Retrieve a mapping of names to numbers below a given node +.It CTL\_CREATE Create a new node +.It CTL\_CREATESYM Create a new node by its kernel symbol +.It CTL\_DESTROY Destroy a node +.It CTL\_DESCRIBE Retrieve node descriptions +.El +.Pp +The core interface to all of these meta-functions is the structure +that the kernel uses to describe the tree internally, as defined in +.In sys/sysctl.h +as: +.Pp +.Bd -literal +struct sysctlnode { + uint32_t sysctl_flags; /* flags and type */ + int32_t sysctl_num; /* mib number */ + char sysctl_name[SYSCTL_NAMELEN]; /* node name */ + uint32_t sysctl_ver; /* node's version vs. rest of tree */ + uint32_t __rsvd; + union { + struct { + uint32_t suc_csize; /* size of child node array */ + uint32_t suc_clen; /* number of valid children */ + struct sysctlnode* suc_child; /* array of child nodes */ + } scu_child; + struct { + void *sud_data; /* pointer to external data */ + size_t sud_offset; /* offset to data */ + } scu_data; + int32_t scu_alias; /* node this node refers to */ + int32_t scu_idata; /* immediate "int" data */ + u_quad_t scu_qdata; /* immediate "u_quad_t" data */ + } sysctl_un; + size_t _sysctl_size; /* size of instrumented data */ + sysctlfn _sysctl_func; /* access helper function */ + struct sysctlnode *sysctl_parent; /* parent of this node */ + const char *sysctl_desc; /* description of node */ +}; + +#define sysctl_csize sysctl_un.scu_child.suc_csize +#define sysctl_clen sysctl_un.scu_child.suc_clen +#define sysctl_child sysctl_un.scu_child.suc_child +#define sysctl_data sysctl_un.scu_data.sud_data +#define sysctl_offset sysctl_un.scu_data.sud_offset +#define sysctl_alias sysctl_un.scu_alias +#define sysctl_idata sysctl_un.scu_idata +#define sysctl_qdata sysctl_un.scu_qdata +.Ed +.Pp +Querying the tree to discover the name to number mapping permits +dynamic discovery of all the data that the tree currently has +instrumented. +For example, to discover all the nodes below the +CTL_VFS node: +.Pp +.Bd -literal -offset indent -compact +struct sysctlnode query, vfs[128]; +int mib[2]; +size_t len; +.sp +mib[0] = CTL_VFS; +mib[1] = CTL_QUERY; +memset(\*[Am]query, 0, sizeof(query)); +query.sysctl_flags = SYSCTL_VERSION; +len = sizeof(vfs); +sysctl(mib, 2, \*[Am]vfs[0], \*[Am]len, \*[Am]query, sizeof(query)); +.Ed +.Pp +Note that a reference to an empty node with +.Fa sysctl_flags +set to +.Dv SYSCTL_VERSION +is passed to sysctl in order to indicate the version that the program +is using. +All dynamic operations passing nodes into sysctl require that the +version be explicitly specified. +.Pp +Creation and destruction of nodes works by constructing part of a new +node description (or a description of the existing node) and invoking +CTL_CREATE (or CTL_CREATESYM) or CTL_DESTROY at the parent of the new +node, with a pointer to the new node passed via the +.Fa new +and +.Fa newlen +arguments. +If valid values for +.Fa old +and +.Fa oldlenp +are passed, a copy of the new node once in the tree will be returned. +If the create operation fails because a node with the same name or MIB +number exists, a copy of the conflicting node will be returned. +.Pp +The minimum requirements for creating a node are setting the +.Fa sysctl_flags +to indicate the new node's type, +.Fa sysctl_num +to either the new node's number (or CTL_CREATE or CTL_CREATESYM if a +dynamically allocated MIB number is acceptable), +.Fa sysctl_size +to the size of the data to be instrumented (which must agree with the +given type), and +.Fa sysctl_name +must be set to the new node's name. +Nodes that are not of type +.Dq node +must also have some description of the data to be instrumented, which +will vary depending on what is to be instrumented. +.Pp +If existing kernel data is to be covered by this new node, its address +should be given in +.Fa sysctl_data +or, if CTL_CREATESYM is used, +.Fa sysctl_data +should be set to a string containing its name from the kernel's symbol +table. +If new data is to be instrumented and an initial value is available, +the new integer or quad type data should be placed into either +.Fa sysctl_idata +or +.Fa sysctl_qdata , +respectively, along with the SYSCTL_IMMEDIATE flag being set, or +.Fa sysctl_data +should be set to point to a copy of the new data, and the +SYSCTL_OWNDATA flag must be set. +This latter method is the only way that new string and struct type +nodes can be initialized. +Invalid kernel addresses are accepted, but any attempt to access those +nodes will return an error. +.Pp +The +.Fa sysctl_csize , +.Fa sysctl_clen , +.Fa sysctl_child , +.Fa sysctl_parent , +and +.Fa sysctl_alias +members are used by the kernel to link the tree together and must be +.Dv NULL +or 0. +Nodes created in this manner cannot have helper functions, so +.Fa sysctl_func +must also be +.Dv NULL . +If the +.Fa sysctl_ver +member is non-zero, it must match either the version of the parent or +the version at the root of the MIB or an error is returned. +This can be used to ensure that nodes are only added or removed from a +known state of the tree. +Note: It may not be possible to determine the version at the root +of the tree. +.Pp +This example creates a new subtree and adds a node to it that controls the +.Fa audiodebug +kernel variable, thereby making it tunable at at any time, without +needing to use +.Xr ddb 4 +or +.Xr kvm 3 +to alter the kernel's memory directly. +.Pp +.Bd -literal -offset indent -compact +struct sysctlnode node; +int mib[2]; +size_t len; +.sp +mib[0] = CTL_CREATE; /* create at top-level */ +len = sizeof(node); +memset(\*[Am]node, 0, len); +node.sysctl_flags = SYSCTL_VERSION|CTLFLAG_READWRITE|CTLTYPE_NODE; +snprintf(node.sysctl_name, sizeof(node.sysctl_name), "local"); +node.sysctl_num = CTL_CREATE; /* request dynamic MIB number */ +sysctl(\*[Am]mib[0], 1, \*[Am]node, \*[Am]len, \*[Am]node, len); +.sp +mib[0] = node.sysctl_num; /* use new MIB number */ +mib[1] = CTL_CREATESYM; /* create at second level */ +len = sizeof(node); +memset(\*[Am]node, 0, len); +node.sysctl_flags = SYSCTL_VERSION|CTLFLAG_READWRITE|CTLTYPE_INT; +snprintf(node.sysctl_name, sizeof(node.sysctl_name), "audiodebug"); +node.sysctl_num = CTL_CREATE; +node.sysctl_data = "audiodebug"; /* kernel symbol to be used */ +sysctl(\*[Am]mib[0], 2, NULL, NULL, \*[Am]node, len); +.Ed +.Pp +The process for deleting nodes is similar, but less data needs to +be supplied. +Only the +.Fa sysctl_num +field +needs to be filled in; almost all other fields must be left blank. +The +.Fa sysctl_name +and/or +.Fa sysctl_ver +fields can be filled in with the name and version of the existing node +as additional checks on what will be deleted. +If all the given data fail to match any node, nothing will be deleted. +If valid values for +.Fa old +and +.Fa oldlenp +are supplied and a node is deleted, a copy of what was in the MIB tree +will be returned. +.Pp +This sample code shows the deletion of the two nodes created in the +above example: +.Pp +.Bd -literal -offset indent -compact +int mib[2]; +.sp +len = sizeof(node); +memset(\*[Am]node, 0, len); +node.sysctl_flags = SYSCTL_VERSION; +.sp +mib[0] = 3214; /* assumed number for "local" */ +mib[1] = CTL_DESTROY; +node.sysctl_num = 3215; /* assumed number for "audiodebug" */ +sysctl(\*[Am]mib[0], 2, NULL, NULL, \*[Am]node, len); +.sp +mib[0] = CTL_DESTROY; +node.sysctl_num = 3214; /* now deleting "local" */ +sysctl(\*[Am]mib[0], 1, NULL, NULL, \*[Am]node, len); +.Ed +.Pp +Descriptions of each of the nodes can also be retrieved, if they are +available. +Descriptions can be retrieved in bulk at each level or on a per-node +basis. +The layout of the buffer into which the descriptions are returned is a +series of variable length structures, each of which describes its own +size. +The length indicated includes the terminating +.Sq nul +character. +Nodes that have no description or where the description is not +available are indicated by an empty string. +The +.Fa descr_ver +will match the +.Fa sysctl_ver +value for a given node, so that descriptions for nodes whose number +have been recycled can be detected and ignored or discarded. +.Pp +.Bd -literal +struct sysctldesc { + int32_t descr_num; /* mib number of node */ + uint32_t descr_ver; /* version of node */ + uint32_t descr_len; /* length of description string */ + char descr_str[1]; /* not really 1...see above */ +}; +.Ed +.Pp +The +.Fn NEXT_DESCR +macro can be used to skip to the next description in the retrieved +list. +.Pp +.Bd -literal -offset indent -compact +struct sysctlnode desc; +struct sysctldesc *d; +char buf[1024]; +int mib[2]; +size_t len; +.sp +/* retrieve kern-level descriptions */ +mib[0] = CTL_KERN; +mib[1] = CTL_DESCRIBE; +d = (struct sysctldesc *)\*[Am]buf[0]; +len = sizeof(buf); +sysctl(mib, 2, d, \*[Am]len, NULL, 0); +while ((caddr_t)d \*[Lt] (caddr_t)\*[Am]buf[len]) { + printf("node %d: %.*s\\n", d-\*[Gt]descr_num, d-\*[Gt]descr_len, + d-\*[Gt]descr_str); + d = NEXT_DESCR(d); +} +.sp +/* retrieve description for kern.securelevel */ +memset(\*[Am]desc, 0, sizeof(desc)); +desc.sysctl_flags = SYSCTL_VERSION; +desc.sysctl_num = KERN_SECURELEVEL; +d = (struct sysctldesc *)\*[Am]buf[0]; +len = sizeof(buf); +sysctl(mib, 2, d, \*[Am]len, \*[Am]desc, sizeof(desc)); +printf("kern.securelevel: %.*s\\n", d-\*[Gt]descr_len, d-\*[Gt]descr_str); +.Ed +.Pp +Descriptions can also be set as follows, subject to the following rules: +.Pp +.Bl -bullet -compact +.It +The kernel securelevel is at zero or lower +.It +The caller has super-user privileges +.It +The node does not currently have a description +.It +The node is not marked as +.Dq permanent +.El +.Pp +.Bd -literal -offset indent -compact +struct sysctlnode desc; +int mib[2]; +.sp +/* presuming the given top-level node was just added... */ +mib[0] = 3214; /* mib numbers taken from previous examples */ +mib[1] = CTL_DESCRIBE; +memset(\*[Am]desc, 0, sizeof(desc)); +desc.sysctl_flags = SYSCTL_VERSION; +desc.sysctl_num = 3215; +desc.sysctl_desc = "audio debug control knob"; +sysctl(mib, 2, NULL, NULL, \*[Am]desc, sizeof(desc)); +.Ed +.Pp +Upon successfully setting a description, the new description will be +returned in the space indicated by the +.Fa oldp +and +.Fa oldlenp +arguments. +.Pp +The +.Fa sysctl_flags +field in the struct sysctlnode contains the sysctl version, node type +information, and a number of flags. +The macros +.Fn SYSCTL_VERS , +.Fn SYSCTL_TYPE , +and +.Fn SYSCTL_FLAGS +can be used to access the different fields. +Valid flags are: +.Bl -column CTLFLAGXPERMANENTXXX +.It Sy Name Description +.It CTLFLAG\_READONLY Node is read-only +.It CTLFLAG\_READWRITE Node is writable by the superuser +.It CTLFLAG\_ANYWRITE Node is writable by anyone +.It CTLFLAG\_PRIVATE Node is readable only by the superuser +.It CTLFLAG\_PERMANENT Node cannot be removed (cannot be set by +processes) +.It CTLFLAG\_OWNDATA Node owns data and does not instrument +existing data +.It CTLFLAG\_IMMEDIATE Node contains instrumented data and does not +instrument existing data +.It CTLFLAG\_HEX Node's contents should be displayed in a hexadecimal +form +.It CTLFLAG\_ROOT Node is the root of a tree (cannot be set at +any time) +.It CTLFLAG\_ANYNUMBER Node matches any MIB number (cannot be set by +processes) +.It CTLFLAG\_HIDDEN Node not displayed by default +.It CTLFLAG\_ALIAS Node refers to a sibling node (cannot be set +by processes) +.It CTLFLAG\_OWNDESC Node owns its own description string space +.El +.Sh RETURN VALUES +If the call to +.Nm +is successful, 0 is returned. +Otherwise \-1 is returned and +.Va errno +is set appropriately. +.Sh FILES +.Bl -tag -width \*[Lt]netinet6/udp6Xvar.h\*[Gt] -compact +.It Aq Pa sys/sysctl.h +definitions for top level identifiers, second level kernel and hardware +identifiers, and user level identifiers +.It Aq Pa sys/socket.h +definitions for second level network identifiers +.It Aq Pa sys/gmon.h +definitions for third level profiling identifiers +.It Aq Pa uvm/uvm_param.h +definitions for second level virtual memory identifiers +.It Aq Pa netinet/in.h +definitions for third level IPv4/v6 identifiers and +fourth level IPv4/v6 identifiers +.It Aq Pa netinet/icmp_var.h +definitions for fourth level ICMP identifiers +.It Aq Pa netinet/icmp6.h +definitions for fourth level ICMPv6 identifiers +.It Aq Pa netinet/tcp_var.h +definitions for fourth level TCP identifiers +.It Aq Pa netinet/udp_var.h +definitions for fourth level UDP identifiers +.It Aq Pa netinet6/udp6_var.h +definitions for fourth level IPv6 UDP identifiers +.It Aq Pa netinet6/ipsec.h +definitions for fourth level IPsec identifiers +.It Aq Pa netkey/key_var.h +definitions for third level PF_KEY identifiers +.It Aq Pa machine/cpu.h +definitions for second level machdep identifiers +.El +.Sh ERRORS +The following errors may be reported: +.Bl -tag -width Er +.It Bq Er EFAULT +The buffer +.Fa name , +.Fa oldp , +.Fa newp , +or length pointer +.Fa oldlenp +contains an invalid address, or the requested value is temporarily +unavailable. +.It Bq Er EINVAL +The +.Fa name +array is zero or greater than CTL_MAXNAME. +.It Bq Er EINVAL +A non-null +.Fa newp +is given and its specified length in +.Fa newlen +is too large or too small, or the given value is not acceptable for +the given node. +.It Bq Er EISDIR +The +.Fa name +array specifies an intermediate rather than terminal name. +.It Bq Er ENOENT +The +.Fa name +array specifies a node that does not exist in the tree. +.It Bq Er ENOENT +An attempt was made to destroy a node that does not exist, or to +create or destroy a node below a node that does not exist. +.It Bq Er ENOMEM +The length pointed to by +.Fa oldlenp +is too short to hold the requested value. +.It Bq Er ENOTDIR +The +.Fa name +array specifies a node below a node that addresses data. +.It Bq Er ENOTEMPTY +An attempt was made to destroy a node that still has children. +.It Bq Er EOPNOTSUPP +The +.Fa name +array specifies a value that is unknown or a meta-operation was +attempted that the requested node does not support. +.It Bq Er EPERM +An attempt is made to set a read-only value. +.It Bq Er EPERM +A process without appropriate privilege attempts to set a value or to +create or destroy a node. +.It Bq Er EPERM +An attempt to change a value protected by the current kernel security +level is made. +.El +.Sh SEE ALSO +.Xr sysctl 7 , +.Xr sysctl 8 , +.Xr secmodel_securelevel 9 +.\" .Xr sysctl 9 +.Sh HISTORY +The +.Nm +function first appeared in +.Bx 4.4 . diff --git a/lib/nbsd_libc/gen/sysctl.c b/lib/nbsd_libc/gen/sysctl.c new file mode 100644 index 000000000..6af568586 --- /dev/null +++ b/lib/nbsd_libc/gen/sysctl.c @@ -0,0 +1,403 @@ +/* $NetBSD: sysctl.c,v 1.30 2008/08/27 08:56:49 christos Exp $ */ + +/*- + * Copyright (c) 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sysctl.c 8.2 (Berkeley) 1/4/94"; +#else +__RCSID("$NetBSD: sysctl.c,v 1.30 2008/08/27 08:56:49 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#define __COMPAT_SYSCTL +#include + +#include +#include +#include +#include +#include +#include "extern.h" + +#ifdef __weak_alias +__weak_alias(sysctl,_sysctl) +#endif + +/* + * handles requests off the user subtree + */ +static int user_sysctl(const int *, u_int, void *, size_t *, + const void *, size_t); + +/* + * copies out individual nodes taking target version into account + */ +static size_t __cvt_node_out(uint, const struct sysctlnode *, void **, + size_t *); + +#include + +int +sysctl(name, namelen, oldp, oldlenp, newp, newlen) + const int *name; + unsigned int namelen; + void *oldp; + const void *newp; + size_t *oldlenp, newlen; +{ + size_t oldlen, savelen; + int error; + + if (name[0] != CTL_USER) + return (__sysctl(name, namelen, oldp, oldlenp, + newp, newlen)); + + oldlen = (oldlenp == NULL) ? 0 : *oldlenp; + savelen = oldlen; + error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen); + + if (error != 0) { + errno = error; + return (-1); + } + + if (oldlenp != NULL) { + *oldlenp = oldlen; + if (oldp != NULL && oldlen > savelen) { + errno = ENOMEM; + return (-1); + } + } + + return (0); +} + +static int +user_sysctl(name, namelen, oldp, oldlenp, newp, newlen) + const int *name; + unsigned int namelen; + void *oldp; + const void *newp; + size_t *oldlenp, newlen; +{ +#define _INT(s, n, v, d) { \ + .sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT| \ + CTLTYPE_INT|SYSCTL_VERSION, \ + sysc_init_field(_sysctl_size, sizeof(int)), \ + .sysctl_name = (s), \ + .sysctl_num = (n), \ + .sysctl_un = { .scu_idata = (v), }, \ + sysc_init_field(_sysctl_desc, (d)), \ + } + + /* + * the nodes under the "user" node + */ + static const struct sysctlnode sysctl_usermib[] = { +#if defined(lint) + /* + * lint doesn't like my initializers + */ + 0 +#else /* !lint */ + { + .sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT| + CTLTYPE_STRING, + sysc_init_field(_sysctl_size, sizeof(_PATH_STDPATH)), + .sysctl_name = "cs_path", + .sysctl_num = USER_CS_PATH, + /* + * XXX these nasty initializers (and the one in + * the _INT() macro) can go away once all ports + * are using gcc3, and become + * + * .sysctl_data = _PATH_STDPATH, + * .sysctl_desc = NULL, + */ + .sysctl_un = { .scu_data = { + sysc_init_field(_sud_data, + __UNCONST(_PATH_STDPATH)), + }, }, + sysc_init_field(_sysctl_desc, + "A value for the PATH environment variable " + "that finds all the standard utilities"), + }, + _INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX, + "The maximum ibase/obase values in the bc(1) utility"), + _INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX, + "The maximum array size in the bc(1) utility"), + _INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX, + "The maximum scale value in the bc(1) utility"), + _INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX, + "The maximum string length in the bc(1) utility"), + _INT("coll_weights_max", USER_COLL_WEIGHTS_MAX, + COLL_WEIGHTS_MAX, "The maximum number of weights that can " + "be assigned to any entry of the LC_COLLATE order keyword " + "in the locale definition file"), + _INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX, + "The maximum number of expressions that can be nested " + "within parenthesis by the expr(1) utility"), + _INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length " + "in bytes of a text-processing utility's input line"), + _INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum " + "number of repeated occurrences of a regular expression " + "permitted when using interval notation"), + _INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION, + "The version of POSIX 1003.2 with which the system " + "attempts to comply"), +#ifdef _POSIX2_C_BIND + _INT("posix2_c_bind", USER_POSIX2_C_BIND, 1, + "Whether the system's C-language development facilities " + "support the C-Language Bindings Option"), +#else + _INT("posix2_c_bind", USER_POSIX2_C_BIND, 0, + "Whether the system's C-language development facilities " + "support the C-Language Bindings Option"), +#endif +#ifdef POSIX2_C_DEV + _INT("posix2_c_dev", USER_POSIX2_C_DEV, 1, + "Whether the system supports the C-Language Development " + "Utilities Option"), +#else + _INT("posix2_c_dev", USER_POSIX2_C_DEV, 0, + "Whether the system supports the C-Language Development " + "Utilities Option"), +#endif +#ifdef POSIX2_CHAR_TERM + _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1, + "Whether the system supports at least one terminal type " + "capable of all operations described in POSIX 1003.2"), +#else + _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0, + "Whether the system supports at least one terminal type " + "capable of all operations described in POSIX 1003.2"), +#endif +#ifdef POSIX2_FORT_DEV + _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1, + "Whether the system supports the FORTRAN Development " + "Utilities Option"), +#else + _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0, + "Whether the system supports the FORTRAN Development " + "Utilities Option"), +#endif +#ifdef POSIX2_FORT_RUN + _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1, + "Whether the system supports the FORTRAN Runtime " + "Utilities Option"), +#else + _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0, + "Whether the system supports the FORTRAN Runtime " + "Utilities Option"), +#endif +#ifdef POSIX2_LOCALEDEF + _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1, + "Whether the system supports the creation of locales"), +#else + _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0, + "Whether the system supports the creation of locales"), +#endif +#ifdef POSIX2_SW_DEV + _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1, + "Whether the system supports the Software Development " + "Utilities Option"), +#else + _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0, + "Whether the system supports the Software Development " + "Utilities Option"), +#endif +#ifdef POSIX2_UPE + _INT("posix2_upe", USER_POSIX2_UPE, 1, + "Whether the system supports the User Portability " + "Utilities Option"), +#else + _INT("posix2_upe", USER_POSIX2_UPE, 0, + "Whether the system supports the User Portability " + "Utilities Option"), +#endif + _INT("stream_max", USER_STREAM_MAX, FOPEN_MAX, + "The minimum maximum number of streams that a process " + "may have open at any one time"), + _INT("tzname_max", USER_TZNAME_MAX, NAME_MAX, + "The minimum maximum number of types supported for the " + "name of a timezone"), + _INT("atexit_max", USER_ATEXIT_MAX, -1, + "The maximum number of functions that may be registered " + "with atexit(3)"), +#endif /* !lint */ + }; +#undef _INT + + static const int clen = sizeof(sysctl_usermib) / + sizeof(sysctl_usermib[0]); + + const struct sysctlnode *node; + int ni; + size_t l, sz; + + /* + * none of these nodes are writable and they're all terminal (for now) + */ + if (namelen != 1) + return (EINVAL); + + l = *oldlenp; + if (name[0] == CTL_QUERY) { + uint v; + node = newp; + if (node == NULL) + return (EINVAL); + else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 && + newlen == sizeof(struct sysctlnode)) + v = SYSCTL_VERS_1; + else + return (EINVAL); + + sz = 0; + for (ni = 0; ni < clen; ni++) + sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l); + *oldlenp = sz; + return (0); + } + + if (name[0] == CTL_DESCRIBE) { + /* + * XXX make sure this is larger than the largest + * "user" description + */ + char buf[192]; + struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp; + size_t d; + + node = newp; + if (node != NULL && + (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 || + newlen != sizeof(struct sysctlnode))) + return (EINVAL); + + sz = 0; + for (ni = 0; ni < clen; ni++) { + memset(&buf[0], 0, sizeof(buf)); + if (node != NULL && + node->sysctl_num != sysctl_usermib[ni].sysctl_num) + continue; + d1->descr_num = sysctl_usermib[ni].sysctl_num; + d1->descr_ver = sysctl_usermib[ni].sysctl_ver; + if (sysctl_usermib[ni].sysctl_desc == NULL) + d1->descr_len = 1; + else { + (void)strlcpy(d1->descr_str, + sysctl_usermib[ni].sysctl_desc, + sizeof(buf) - sizeof(*d1)); + d1->descr_len = strlen(d1->descr_str) + 1; + } + d = (size_t)__sysc_desc_adv(NULL, d1->descr_len); + if (d2 != NULL) + memcpy(d2, d1, d); + sz += d; + if (node != NULL) + break; + } + *oldlenp = sz; + if (sz == 0 && node != NULL) + return (ENOENT); + return (0); + + } + + /* + * none of these nodes are writable + */ + if (newp != NULL || newlen != 0) + return (EPERM); + + node = &sysctl_usermib[0]; + for (ni = 0; ni < clen; ni++) + if (name[0] == node[ni].sysctl_num) + break; + if (ni == clen) + return (EOPNOTSUPP); + + node = &node[ni]; + if (node->sysctl_flags & CTLFLAG_IMMEDIATE) { + switch (SYSCTL_TYPE(node->sysctl_flags)) { + case CTLTYPE_INT: + newp = &node->sysctl_idata; + break; + case CTLTYPE_QUAD: + newp = &node->sysctl_qdata; + break; + default: + return (EINVAL); + } + } + else + newp = node->sysctl_data; + + l = MIN(l, node->sysctl_size); + if (oldp != NULL) + memcpy(oldp, newp, l); + *oldlenp = node->sysctl_size; + + return (0); +} + +static size_t +__cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l) +{ + const void *src = n; + size_t sz; + + switch (v) { +#if (SYSCTL_VERSION != SYSCTL_VERS_1) +#error __cvt_node_out: no support for SYSCTL_VERSION +#endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ + + case SYSCTL_VERSION: + sz = sizeof(struct sysctlnode); + break; + + default: + sz = 0; + break; + } + + if (sz > 0 && *o != NULL && *l >= sz) { + memcpy(*o, src, sz); + *o = sz + (caddr_t)*o; + *l -= sz; + } + + return(sz); +} diff --git a/lib/nbsd_libc/gen/sysctlbyname.c b/lib/nbsd_libc/gen/sysctlbyname.c new file mode 100644 index 000000000..0a6be7f32 --- /dev/null +++ b/lib/nbsd_libc/gen/sysctlbyname.c @@ -0,0 +1,67 @@ +/* $NetBSD: sysctlbyname.c,v 1.7 2010/12/13 23:10:13 pooka Exp $ */ + +/*- + * Copyright (c) 2003,2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Brown. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sysctlbyname.c,v 1.7 2010/12/13 23:10:13 pooka Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef RUMP_ACTION +#include "namespace.h" +#endif +#include +#include + +#ifdef RUMP_ACTION +#include +#define sysctl(a,b,c,d,e,f) rump_sys___sysctl(a,b,c,d,e,f) +#else +#ifdef __weak_alias +__weak_alias(sysctlbyname,_sysctlbyname) +#endif +#endif + +/* + * trivial sysctlbyname() function for the "lazy". + */ +int +sysctlbyname(const char *gname, void *oldp, size_t *oldlenp, + const void *newp, size_t newlen) +{ + int name[CTL_MAXNAME], rc; + u_int namelen; + + rc = sysctlgetmibinfo(gname, &name[0], &namelen, NULL, NULL, NULL, + SYSCTL_VERSION); + if (rc == 0) + rc = sysctl(&name[0], namelen, oldp, oldlenp, newp, newlen); + return (rc); +} diff --git a/lib/nbsd_libc/gen/sysctlgetmibinfo.c b/lib/nbsd_libc/gen/sysctlgetmibinfo.c new file mode 100644 index 000000000..10d5c077f --- /dev/null +++ b/lib/nbsd_libc/gen/sysctlgetmibinfo.c @@ -0,0 +1,608 @@ +/* $NetBSD: sysctlgetmibinfo.c,v 1.9 2010/12/13 23:10:13 pooka Exp $ */ + +/*- + * Copyright (c) 2003,2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Brown. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sysctlgetmibinfo.c,v 1.9 2010/12/13 23:10:13 pooka Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef RUMP_ACTION +#include "namespace.h" +#ifdef _REENTRANT +#include "reentrant.h" +#endif /* _REENTRANT */ +#endif /* RUMP_ACTION */ +#include +#include + +#include +#include +#include +#include + +#ifdef RUMP_ACTION +#include +#define sysctl(a,b,c,d,e,f) rump_sys___sysctl(a,b,c,d,e,f) +#else +#ifdef __weak_alias +__weak_alias(__learn_tree,___learn_tree) +__weak_alias(sysctlgetmibinfo,_sysctlgetmibinfo) +#endif +#endif + +/* + * the place where we attach stuff we learn on the fly, not + * necessarily used. + */ +static struct sysctlnode sysctl_mibroot = { +#if defined(lint) + /* + * lint doesn't like my initializers + */ + 0 +#else /* !lint */ + .sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE, + sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)), + .sysctl_name = "(root)", +#endif /* !lint */ +}; + +/* + * routines to handle learning and cleanup + */ +static int compar(const void *, const void *); +static void free_children(struct sysctlnode *); +static void relearnhead(void); + +/* + * specifically not static since sysctl(8) "borrows" it. + */ +int __learn_tree(int *, u_int, struct sysctlnode *); + +/* + * for ordering nodes -- a query may or may not be given them in + * numeric order + */ +static int +compar(const void *a, const void *b) +{ + + return (((const struct sysctlnode *)a)->sysctl_num - + ((const struct sysctlnode *)b)->sysctl_num); +} + +/* + * recursively nukes a branch or an entire tree from the given node + */ +static void +free_children(struct sysctlnode *rnode) +{ + struct sysctlnode *node; + + if (rnode == NULL || + SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE || + rnode->sysctl_child == NULL) + return; + + for (node = rnode->sysctl_child; + node < &rnode->sysctl_child[rnode->sysctl_clen]; + node++) { + free_children(node); + } + free(rnode->sysctl_child); + rnode->sysctl_child = NULL; +} + +/* + * verifies that the head of the tree in the kernel is the same as the + * head of the tree we already got, integrating new stuff and removing + * old stuff, if it's not. + */ +static void +relearnhead(void) +{ + struct sysctlnode *h, *i, *o, qnode; + size_t si, so; + int rc, name, nlen, olen, ni, oi; + uint32_t t; + + /* + * if there's nothing there, there's no need to expend any + * effort + */ + if (sysctl_mibroot.sysctl_child == NULL) + return; + + /* + * attempt to pull out the head of the tree, starting with the + * size we have now, and looping if we need more (or less) + * space + */ + si = 0; + so = sysctl_mibroot.sysctl_clen * sizeof(struct sysctlnode); + name = CTL_QUERY; + memset(&qnode, 0, sizeof(qnode)); + qnode.sysctl_flags = SYSCTL_VERSION; + do { + si = so; + h = malloc(si); + rc = sysctl(&name, 1, h, &so, &qnode, sizeof(qnode)); + if (rc == -1 && errno != ENOMEM) + return; + if (si < so) + free(h); + } while (si < so); + + /* + * order the new copy of the head + */ + nlen = so / sizeof(struct sysctlnode); + qsort(h, (size_t)nlen, sizeof(struct sysctlnode), compar); + + /* + * verify that everything is the same. if it is, we don't + * need to do any more work here. + */ + olen = sysctl_mibroot.sysctl_clen; + rc = (nlen == olen) ? 0 : 1; + o = sysctl_mibroot.sysctl_child; + for (ni = 0; rc == 0 && ni < nlen; ni++) { + if (h[ni].sysctl_num != o[ni].sysctl_num || + h[ni].sysctl_ver != o[ni].sysctl_ver) + rc = 1; + } + if (rc == 0) { + free(h); + return; + } + + /* + * something changed. h will become the new head, and we need + * pull over any subtrees we already have if they're the same + * version. + */ + i = h; + ni = oi = 0; + while (ni < nlen && oi < olen) { + /* + * something was inserted or deleted + */ + if (SYSCTL_TYPE(i[ni].sysctl_flags) == CTLTYPE_NODE) + i[ni].sysctl_child = NULL; + if (i[ni].sysctl_num != o[oi].sysctl_num) { + if (i[ni].sysctl_num < o[oi].sysctl_num) { + ni++; + } + else { + free_children(&o[oi]); + oi++; + } + continue; + } + + /* + * same number, but different version, so throw away + * any accumulated children + */ + if (i[ni].sysctl_ver != o[oi].sysctl_ver) + free_children(&o[oi]); + + /* + * this node is the same, but we only need to + * move subtrees. + */ + else if (SYSCTL_TYPE(i[ni].sysctl_flags) == CTLTYPE_NODE) { + /* + * move subtree to new parent + */ + i[ni].sysctl_clen = o[oi].sysctl_clen; + i[ni].sysctl_csize = o[oi].sysctl_csize; + i[ni].sysctl_child = o[oi].sysctl_child; + /* + * reparent inherited subtree + */ + for (t = 0; + i[ni].sysctl_child != NULL && + t < i[ni].sysctl_clen; + t++) + i[ni].sysctl_child[t].sysctl_parent = &i[ni]; + } + ni++; + oi++; + } + + /* + * left over new nodes need to have empty subtrees cleared + */ + while (ni < nlen) { + if (SYSCTL_TYPE(i[ni].sysctl_flags) == CTLTYPE_NODE) + i[ni].sysctl_child = NULL; + ni++; + } + + /* + * left over old nodes need to be cleaned out + */ + while (oi < olen) { + free_children(&o[oi]); + oi++; + } + + /* + * pop new head in + */ + sysctl_mibroot.sysctl_clen = nlen; + sysctl_mibroot.sysctl_csize = nlen; + sysctl_mibroot.sysctl_child = h; + free(o); +} + +/* + * sucks in the children at a given level and attaches it to the tree. + */ +int +__learn_tree(int *name, u_int namelen, struct sysctlnode *pnode) +{ + struct sysctlnode qnode; + uint32_t rc; + size_t sz; + + if (pnode == NULL) + pnode = &sysctl_mibroot; + if (SYSCTL_TYPE(pnode->sysctl_flags) != CTLTYPE_NODE) { + errno = EINVAL; + return (-1); + } + if (pnode->sysctl_child != NULL) + return (0); + + if (pnode->sysctl_clen == 0) + sz = SYSCTL_DEFSIZE * sizeof(struct sysctlnode); + else + sz = pnode->sysctl_clen * sizeof(struct sysctlnode); + pnode->sysctl_child = malloc(sz); + if (pnode->sysctl_child == NULL) + return (-1); + + name[namelen] = CTL_QUERY; + pnode->sysctl_clen = 0; + pnode->sysctl_csize = 0; + memset(&qnode, 0, sizeof(qnode)); + qnode.sysctl_flags = SYSCTL_VERSION; + rc = sysctl(name, namelen + 1, pnode->sysctl_child, &sz, + &qnode, sizeof(qnode)); + if (sz == 0) { + free(pnode->sysctl_child); + pnode->sysctl_child = NULL; + return (rc); + } + if (rc) { + free(pnode->sysctl_child); + pnode->sysctl_child = NULL; + if ((sz % sizeof(struct sysctlnode)) != 0) + errno = EINVAL; + if (errno != ENOMEM) + return (rc); + } + + if (pnode->sysctl_child == NULL) { + pnode->sysctl_child = malloc(sz); + if (pnode->sysctl_child == NULL) + return (-1); + + rc = sysctl(name, namelen + 1, pnode->sysctl_child, &sz, + &qnode, sizeof(qnode)); + if (rc) { + free(pnode->sysctl_child); + pnode->sysctl_child = NULL; + return (rc); + } + } + + /* + * how many did we get? + */ + pnode->sysctl_clen = sz / sizeof(struct sysctlnode); + pnode->sysctl_csize = sz / sizeof(struct sysctlnode); + if (pnode->sysctl_clen * sizeof(struct sysctlnode) != sz) { + free(pnode->sysctl_child); + pnode->sysctl_child = NULL; + errno = EINVAL; + return (-1); + } + + /* + * you know, the kernel doesn't really keep them in any + * particular order...just like entries in a directory + */ + qsort(pnode->sysctl_child, pnode->sysctl_clen, + sizeof(struct sysctlnode), compar); + + /* + * rearrange parent<->child linkage + */ + for (rc = 0; rc < pnode->sysctl_clen; rc++) { + pnode->sysctl_child[rc].sysctl_parent = pnode; + if (SYSCTL_TYPE(pnode->sysctl_child[rc].sysctl_flags) == + CTLTYPE_NODE) { + /* + * these nodes may have children, but we + * haven't discovered that yet. + */ + pnode->sysctl_child[rc].sysctl_child = NULL; + } + pnode->sysctl_child[rc].sysctl_desc = NULL; + } + + return (0); +} + +/* + * that's "given name" as a string, the integer form of the name fit + * to be passed to sysctl(), "canonicalized name" (optional), and a + * pointer to the length of the integer form. oh, and then a pointer + * to the node, in case you (the caller) care. you can leave them all + * NULL except for gname, though that might be rather pointless, + * unless all you wanna do is verify that a given name is acceptable. + * + * returns either 0 (everything was fine) or -1 and sets errno + * accordingly. if errno is set to EAGAIN, we detected a change to + * the mib while parsing, and you should try again. in the case of an + * invalid node name, cname will be set to contain the offending name. + */ +#ifdef _REENTRANT +static mutex_t sysctl_mutex = MUTEX_INITIALIZER; +static int sysctlgetmibinfo_unlocked(const char *, int *, u_int *, char *, + size_t *, struct sysctlnode **, int); +#endif /* __REENTRANT */ + +int +sysctlgetmibinfo(const char *gname, int *iname, u_int *namelenp, + char *cname, size_t *csz, struct sysctlnode **rnode, int v) +#ifdef _REENTRANT +{ + int rc; + + mutex_lock(&sysctl_mutex); + rc = sysctlgetmibinfo_unlocked(gname, iname, namelenp, cname, csz, + rnode, v); + mutex_unlock(&sysctl_mutex); + + return (rc); +} + +static int +sysctlgetmibinfo_unlocked(const char *gname, int *iname, u_int *namelenp, + char *cname, size_t *csz, struct sysctlnode **rnode, + int v) +#endif /* _REENTRANT */ +{ + struct sysctlnode *pnode, *node; + int name[CTL_MAXNAME], n, haven; + u_int ni, nl; + intmax_t q; + char sep[2], token[SYSCTL_NAMELEN], + pname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME]; + const char *piece, *dot; + char *t; + size_t l; + + if (rnode != NULL) { + if (*rnode == NULL) { + /* XXX later deal with dealing back a sub version */ + if (v != SYSCTL_VERSION) + return (EINVAL); + + pnode = &sysctl_mibroot; + } + else { + /* this is just someone being silly */ + if (SYSCTL_VERS((*rnode)->sysctl_flags) != (uint32_t)v) + return (EINVAL); + + /* XXX later deal with other people's trees */ + if (SYSCTL_VERS((*rnode)->sysctl_flags) != + SYSCTL_VERSION) + return (EINVAL); + + pnode = *rnode; + } + } + else + pnode = &sysctl_mibroot; + + if (pnode == &sysctl_mibroot) + relearnhead(); + + nl = ni = 0; + token[0] = '\0'; + pname[0] = '\0'; + node = NULL; + + /* + * default to using '.' as the separator, but allow '/' as + * well, and then allow a leading separator + */ + if ((dot = strpbrk(gname, "./")) == NULL) + sep[0] = '.'; + else + sep[0] = dot[0]; + sep[1] = '\0'; + if (gname[0] == sep[0]) { + strlcat(pname, sep, sizeof(pname)); + gname++; + } + +#define COPY_OUT_DATA(t, c, cs, nlp, l) do { \ + if ((c) != NULL && (cs) != NULL) \ + *(cs) = strlcpy((c), (t), *(cs)); \ + else if ((cs) != NULL) \ + *(cs) = strlen(t) + 1; \ + if ((nlp) != NULL) \ + *(nlp) = (l); \ + } while (/*CONSTCOND*/0) + + piece = gname; + while (piece != NULL && *piece != '\0') { + /* + * what was i looking for? + */ + dot = strchr(piece, sep[0]); + if (dot == NULL) { + l = strlcpy(token, piece, sizeof(token)); + if (l > sizeof(token)) { + COPY_OUT_DATA(piece, cname, csz, namelenp, nl); + errno = ENAMETOOLONG; + return (-1); + } + } + else if (dot - piece > (intptr_t)(sizeof(token) - 1)) { + COPY_OUT_DATA(token, cname, csz, namelenp, nl); + errno = ENAMETOOLONG; + return (-1); + } + else { + strncpy(token, piece, (size_t)(dot - piece)); + token[dot - piece] = '\0'; + } + + /* + * i wonder if this "token" is an integer? + */ + errno = 0; + q = strtoimax(token, &t, 0); + n = (int)q; + if (errno != 0 || *t != '\0') + haven = 0; + else if (q < INT_MIN || q > UINT_MAX) + haven = 0; + else + haven = 1; + + /* + * make sure i have something to look at + */ + if (SYSCTL_TYPE(pnode->sysctl_flags) != CTLTYPE_NODE) { + if (haven && nl > 0) { + strlcat(pname, sep, sizeof(pname)); + goto just_numbers; + } + COPY_OUT_DATA(token, cname, csz, namelenp, nl); + errno = ENOTDIR; + return (-1); + } + if (pnode->sysctl_child == NULL) { + if (__learn_tree(name, nl, pnode) == -1) { + COPY_OUT_DATA(token, cname, csz, namelenp, nl); + return (-1); + } + } + node = pnode->sysctl_child; + if (node == NULL) { + COPY_OUT_DATA(token, cname, csz, namelenp, nl); + errno = ENOENT; + return (-1); + } + + /* + * now...is it there? + */ + for (ni = 0; ni < pnode->sysctl_clen; ni++) + if ((haven && ((n == node[ni].sysctl_num) || + (node[ni].sysctl_flags & CTLFLAG_ANYNUMBER))) || + strcmp(token, node[ni].sysctl_name) == 0) + break; + if (ni >= pnode->sysctl_clen) { + COPY_OUT_DATA(token, cname, csz, namelenp, nl); + errno = ENOENT; + return (-1); + } + + /* + * ah...it is. + */ + pnode = &node[ni]; + if (nl > 0) + strlcat(pname, sep, sizeof(pname)); + if (haven && n != pnode->sysctl_num) { + just_numbers: + strlcat(pname, token, sizeof(pname)); + name[nl] = n; + } + else { + strlcat(pname, pnode->sysctl_name, sizeof(pname)); + name[nl] = pnode->sysctl_num; + } + piece = (dot != NULL) ? dot + 1 : NULL; + nl++; + if (nl == CTL_MAXNAME) { + COPY_OUT_DATA(token, cname, csz, namelenp, nl); + errno = ERANGE; + return (-1); + } + } + + if (nl == 0) { + if (namelenp != NULL) + *namelenp = 0; + errno = EINVAL; + return (-1); + } + + COPY_OUT_DATA(pname, cname, csz, namelenp, nl); + if (iname != NULL && namelenp != NULL) + memcpy(iname, &name[0], MIN(nl, *namelenp) * sizeof(int)); + if (namelenp != NULL) + *namelenp = nl; + if (rnode != NULL) { + if (*rnode != NULL) + /* + * they gave us a private tree to work in, so + * we give back a pointer into that private + * tree + */ + *rnode = pnode; + else { + /* + * they gave us a place to put the node data, + * so give them a copy + */ + *rnode = malloc(sizeof(struct sysctlnode)); + if (*rnode != NULL) { + **rnode = *pnode; + (*rnode)->sysctl_child = NULL; + (*rnode)->sysctl_parent = NULL; + } + } + } + + return (0); +} diff --git a/lib/nbsd_libc/gen/sysctlnametomib.c b/lib/nbsd_libc/gen/sysctlnametomib.c new file mode 100644 index 000000000..92376e1ff --- /dev/null +++ b/lib/nbsd_libc/gen/sysctlnametomib.c @@ -0,0 +1,70 @@ +/* $NetBSD: sysctlnametomib.c,v 1.6 2010/12/13 23:10:13 pooka Exp $ */ + +/*- + * Copyright (c) 2003,2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Brown. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sysctlnametomib.c,v 1.6 2010/12/13 23:10:13 pooka Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef RUMP_ACTION +#include "namespace.h" +#endif +#include +#include + +#ifdef RUMP_ACTION +#include +#define sysctl(a,b,c,d,e,f) rump_sys___sysctl(a,b,c,d,e,f) +#else +#ifdef __weak_alias +__weak_alias(sysctlnametomib,_sysctlnametomib) +#endif +#endif /* RUMP_ACTION */ + +/* + * freebsd compatible sysctlnametomib() function, implemented as an + * extremely thin wrapper around sysctlgetmibinfo(). i think the use + * of size_t as the third argument is erroneous, but what can we do + * about that? + */ +int +sysctlnametomib(const char *gname, int *iname, size_t *namelenp) +{ + u_int unamelen; + int rc; + + unamelen = *namelenp; + rc = sysctlgetmibinfo(gname, iname, &unamelen, NULL, NULL, NULL, + SYSCTL_VERSION); + *namelenp = unamelen; + + return (rc); +} diff --git a/lib/nbsd_libc/gen/syslog.3 b/lib/nbsd_libc/gen/syslog.3 new file mode 100644 index 000000000..278b0616d --- /dev/null +++ b/lib/nbsd_libc/gen/syslog.3 @@ -0,0 +1,517 @@ +.\" $NetBSD: syslog.3,v 1.28 2010/05/13 18:04:58 jruoho Exp $ +.\" $OpenBSD: syslog.3,v 1.25 2005/07/22 03:16:58 jaredy Exp $ +.\" +.\" Copyright (c) 1985, 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. +.\" +.\" @(#)syslog.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd May 3, 2010 +.Dt SYSLOG 3 +.Os +.Sh NAME +.Nm syslog , +.Nm syslog_r , +.Nm vsyslog , +.Nm vsyslog_r , +.Nm syslogp , +.Nm syslogp_r , +.Nm vsyslogp , +.Nm vsyslogp_r , +.Nm openlog , +.Nm openlog_r , +.Nm closelog , +.Nm closelog_r , +.Nm setlogmask , +.Nm setlogmask_r +.Nd control system log +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In syslog.h +.Ft void +.Fn syslog "int priority" "const char *message" "..." +.Ft void +.Fn syslog_r "int priority" "struct syslog_data *data" "const char *message" "..." +.Ft void +.Fn syslogp "int priority" "const char *msgid" "const char *sdfmt" "const char *message" "..." +.Ft void +.Fn syslogp_r "int priority" "struct syslog_data *data" "const char *msgid" "const char *sdfmt" "const char *message" "..." +.\" .Ft void +.\" .Fn syslog_ss "int priority" "struct syslog_data *data" "const char *message" "..." +.Ft void +.Fn openlog "const char *ident" "int logopt" "int facility" +.Ft void +.Fn openlog_r "const char *ident" "int logopt" "int facility" "struct syslog_data *data" +.Ft void +.Fn closelog void +.Ft void +.Fn closelog_r "struct syslog_data *data" +.Ft int +.Fn setlogmask "int maskpri" +.Ft int +.Fn setlogmask_r "int maskpri" "struct syslog_data *data" +.In stdarg.h +.Ft void +.Fn vsyslog "int priority" "const char *message" "va_list args" +.Ft void +.Fn vsyslog_r "int priority" "struct syslog_data *data" "const char *message" "va_list args" +.Ft void +.Fn vsyslogp "int priority" "const char *msgid" "const char *sdfmt" "const char *message" "va_list args" +.Ft void +.Fn vsyslogp_r "int priority" "struct syslog_data *data" "const char *msgid" "const char *sdfmt" "const char *message" "va_list args" +.\" .Ft void +.\" .Fn vsyslog_ss "int priority" "struct syslog_data *data" "const char *message" "va_list args" +.Sh DESCRIPTION +The +.Fn syslog +function +writes +.Fa message +to the system message logger. +The message is then written to the system console, log files, +logged-in users, or forwarded to other machines as appropriate (see +.Xr syslogd 8 ) . +.Pp +The message is identical to a +.Xr printf 3 +format string, except that +.Ql %m +is replaced by the current error +message. +(As denoted by the global variable +.Va errno ; +see +.Xr strerror 3 . ) +A trailing newline is added if none is present. +.\" shouldn't the newline statement be removed? +.\" when logging through a socket a newline is +.\" not added nor is it required. -- ms +.Pp +The +.Fn syslog_r +function is a multithread-safe version of the +.Fn syslog +function. +It takes a pointer to a +.Fa syslog_data +structure which is used to store +information. +This parameter must be initialized before +.Fn syslog_r +is called. +The +.Dv SYSLOG_DATA_INIT +constant is used for this purpose. +The +.Fa syslog_data +structure and the +.Dv SYSLOG_DATA_INIT +constant are defined as: +.Bd -literal -offset indent +struct syslog_data { + int log_file; + int connected; + int opened; + int log_stat; + const char *log_tag; + int log_fac; + int log_mask; +}; + +#define SYSLOG_DATA_INIT { \e + .log_file = -1, \e + .log_fac = LOG_USER, \e + .log_mask = 0xff, \e +} +.Ed +.Pp +The structure is composed of the following elements: +.Bl -tag -width connected -offset indent +.It Va log_file +contains the file descriptor of the file where the message is logged +.It Va connected +indicates if connect has been done +.It Va opened +indicates if +.Fn openlog_r +has been called +.It Va log_stat +status bits, set by +.Fn openlog_r +.It Va log_tag +string to tag the entry with +.It Va log_fac +facility code +.It Va log_mask +mask of priorities to be logged +.El +.\" .Pp +.\" The +.\" .Fn syslog_ss +.\" is the async-signal-safe version of +.\" .Fn syslog_r +.\" and is also multithread-safe. +.\" It has the following limitations: +.\" .Bl -enum -offset indent +.\" .It +.\" The format string cannot contain multi-byte character sequences. +.\" .It +.\" Floating point formats are not supported and print +.\" .Dq UNK . +.\" .It +.\" The time of the event is not sent to +.\" .Xr syslogd 8 . +.\" .It +.\" The error string in the %m format is not printed symbolically but as +.\" .Dq Error %d . +.\" .El +.\" .Pp +.\" For more information about async-signal-safe functions and signal handlers, see +.\" .Xr signal 7 . +.Pp +The +.Fn vsyslog +function +is an alternative form in which the arguments have already been captured +using the variable-length argument facilities of +.Xr varargs 3 . +.Pp +The +.Fn syslogp +variants take additional arguments which correspond to new fields in the +syslog-protocol message format. +All three arguments are evaluated as +.Xr printf 3 +format strings and any of them can be +.Dv NULL . +This enables applications to use message IDs, structured data, and UTF-8 encoded +content in messages. +.Pp +The message is tagged with +.Fa priority . +Priorities are encoded as a +.Fa facility +and a +.Em level . +The facility describes the part of the system +generating the message. +The level is selected from the following +.Em ordered +(high to low) list: +.Bl -tag -width LOG_AUTHPRIV +.It Dv LOG_EMERG +A panic condition. +This is normally broadcast to all users. +.It Dv LOG_ALERT +A condition that should be corrected immediately, such as a corrupted +system database. +.It Dv LOG_CRIT +Critical conditions, e.g., hard device errors. +.It Dv LOG_ERR +Errors. +.It Dv LOG_WARNING +Warning messages. +.It Dv LOG_NOTICE +Conditions that are not error conditions, +but should possibly be handled specially. +.It Dv LOG_INFO +Informational messages. +.It Dv LOG_DEBUG +Messages that contain information +normally of use only when debugging a program. +.El +.Pp +The +.Fn vsyslog_r +is used the same way as +.Fn vsyslog +except that it takes an additional pointer to a +.Fa syslog_data +structure. +It is a multithread-safe version of the +.Fn vsyslog +function described above. +.\" The +.\" .Fn vsyslog_ss +.\" is the async-signal-safe version of +.\" .Fn vsyslog_r , +.\" is also multithread-safe, and has the same limitations as +.\" .Fn syslog_ss . +.Pp +The +.Fn openlog +function +provides for more specialized processing of the messages sent +by +.Fn syslog +and +.Fn vsyslog . +The parameter +.Fa ident +is a string that will be prepended to every message. +The +.Fa logopt +argument +is a bit field specifying logging options, which is formed by +.Tn OR Ns 'ing +one or more of the following values: +.Bl -tag -width LOG_AUTHPRIV +.It Dv LOG_CONS +If +.Fn syslog +cannot pass the message to +.Xr syslogd 8 +it will attempt to write the message to the console +.Pq Dq Pa /dev/console . +.It Dv LOG_NDELAY +Open the connection to +.Xr syslogd 8 +immediately. +Normally the open is delayed until the first message is logged. +Useful for programs that need to manage the order in which file +descriptors are allocated. +.It Dv LOG_PERROR +Write the message to standard error output as well to the system log. +.It Dv LOG_PID +Log the process id with each message: useful for identifying +instantiations of daemons. +(This PID is placed within brackets +between the ident and the message.) +.El +.Pp +The +.Fa facility +parameter encodes a default facility to be assigned to all messages +that do not have an explicit facility encoded: +.Bl -tag -width LOG_AUTHPRIV +.It Dv LOG_AUTH +The authorization system: +.Xr login 1 , +.Xr su 1 , +.Xr getty 8 , +etc. +.It Dv LOG_AUTHPRIV +The same as +.Dv LOG_AUTH , +but logged to a file readable only by +selected individuals. +.It Dv LOG_CRON +The cron daemon: +.Xr cron 8 . +.It Dv LOG_DAEMON +System daemons, such as +.Xr routed 8 , +that are not provided for explicitly by other facilities. +.It Dv LOG_FTP +The file transfer protocol daemon: +.Xr ftpd 8 . +.It Dv LOG_KERN +Messages generated by the kernel. +These cannot be generated by any user processes. +.It Dv LOG_LPR +The line printer spooling system: +.Xr lpr 1 , +.Xr lpc 8 , +.Xr lpd 8 , +etc. +.It Dv LOG_MAIL +The mail system. +.It Dv LOG_NEWS +The network news system. +.It Dv LOG_SYSLOG +Messages generated internally by +.Xr syslogd 8 . +.It Dv LOG_USER +Messages generated by random user processes. +This is the default facility identifier if none is specified. +.It Dv LOG_UUCP +The uucp system. +.It Dv LOG_LOCAL0 +Reserved for local use. +Similarly for +.Dv LOG_LOCAL1 +through +.Dv LOG_LOCAL7 . +.El +.Pp +The +.Fn openlog_r +function is the multithread-safe version of the +.Fn openlog +function. +It takes an additional pointer to a +.Fa syslog_data +structure. +This function must be used in conjunction with the other +multithread-safe functions. +.Pp +The +.Fn closelog +function +can be used to close the log file. +.Pp +The +.Fn closelog_r +does the same thing as +.Xr closelog 3 +but in a multithread-safe way and takes an additional +pointer to a +.Fa syslog_data +structure. +.Pp +The +.Fn setlogmask +function +sets the log priority mask to +.Fa maskpri +and returns the previous mask. +Calls to +.Fn syslog +with a priority not set in +.Fa maskpri +are rejected. +The mask for an individual priority +.Fa pri +is calculated by the macro +.Fn LOG_MASK pri ; +the mask for all priorities up to and including +.Fa toppri +is given by the macro +.Fn LOG_UPTO toppri . +The default allows all priorities to be logged. +.Pp +The +.Fn setlogmask_r +function is the multithread-safe version of +.Fn setlogmask . +It takes an additional pointer to a +.Fa syslog_data +structure. +.Sh RETURN VALUES +The routines +.Fn closelog , +.Fn closelog_r , +.Fn openlog , +.Fn openlog_r , +.Fn syslog , +.Fn syslog_r , +.Fn vsyslog , +.Fn vsyslog_r , +.Fn syslogp , +.Fn syslogp_r , +.Fn vsyslogp , +and +.Fn vsyslogp_r +return no value. +.Pp +The routines +.Fn setlogmask +and +.Fn setlogmask_r +always return the previous log mask level. +.Sh EXAMPLES +.Bd -literal -offset indent -compact +syslog(LOG_ALERT, "who: internal error 23"); + +openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); + +setlogmask(LOG_UPTO(LOG_ERR)); + +syslog(LOG_INFO, "Connection from host %d", CallingHost); + +syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m"); + +syslogp(LOG_INFO|LOG_LOCAL2, NULL, NULL, "foobar error: %m"); + +syslogp(LOG_INFO, "ID%d", "[meta language=\e"en-US\e"]", + "event: %s", 42, EventDescription); +.Ed +.Pp +For the multithread-safe functions: +.Bd -literal -offset indent +struct syslog_data sdata = SYSLOG_DATA_INIT; + +syslog_r(LOG_INFO|LOG_LOCAL2, \*[Am]sdata, "foobar error: %m"); +.Ed +.Sh SEE ALSO +.Xr logger 1 , +.Xr syslogd 8 +.Rs +.%R RFC +.%N 3164 +.%D August 2001 +.%T The BSD syslog Protocol +.Re +.Rs +.%R Internet-Draft +.%N draft-ietf-syslog-protocol-23 +.%D September 2007 +.%T The syslog Protocol +.Re +.Sh HISTORY +These non-multithread-safe functions appeared in +.Bx 4.2 . +The multithread-safe functions appeared in +.Ox 3.1 +and then in +.Nx 4.0 . +The async-signal-safe functions appeared in +.Nx 4.0 . +The syslog-protocol functions appeared in +.Nx 5.0 . +.Sh CAVEATS +It is important never to pass a string with user-supplied data as a +format without using +.Ql %s . +An attacker can put format specifiers in the string to mangle your stack, +leading to a possible security hole. +This holds true even if you have built the string +.Dq by hand +using a function like +.Fn snprintf , +as the resulting string may still contain user-supplied conversion specifiers +for later interpolation by +.Fn syslog . +.Pp +Always be sure to use the proper secure idiom: +.Bd -literal -offset indent +syslog(priority, "%s", string); +.Ed +.Pp +With +.Fn syslogp +the caller is responsible to use the right formatting for the message fields. +A +.Fa msgid +must only contain up to 32 ASCII characters. +A +.Fa sdfmt +has strict rules for paranthesis and character quoting. +If the +.Fa msgfmt +contains UTF-8 characters, then it has to start with a Byte Order Mark. diff --git a/lib/nbsd_libc/gen/syslog.c b/lib/nbsd_libc/gen/syslog.c new file mode 100644 index 000000000..6f961087e --- /dev/null +++ b/lib/nbsd_libc/gen/syslog.c @@ -0,0 +1,571 @@ +/* $NetBSD: syslog.c,v 1.48 2010/05/13 22:40:14 christos Exp $ */ + +/* + * Copyright (c) 1983, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95"; +#else +__RCSID("$NetBSD: syslog.c,v 1.48 2010/05/13 22:40:14 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "reentrant.h" +#include "extern.h" + +#ifdef __weak_alias +__weak_alias(closelog,_closelog) +__weak_alias(openlog,_openlog) +__weak_alias(setlogmask,_setlogmask) +__weak_alias(syslog,_syslog) +__weak_alias(vsyslog,_vsyslog) +__weak_alias(syslogp,_syslogp) +__weak_alias(vsyslogp,_vsyslogp) + +__weak_alias(closelog_r,_closelog_r) +__weak_alias(openlog_r,_openlog_r) +__weak_alias(setlogmask_r,_setlogmask_r) +__weak_alias(syslog_r,_syslog_r) +__weak_alias(vsyslog_r,_vsyslog_r) +__weak_alias(syslog_ss,_syslog_ss) +__weak_alias(vsyslog_ss,_vsyslog_ss) +__weak_alias(syslogp_r,_syslogp_r) +__weak_alias(vsyslogp_r,_vsyslogp_r) +__weak_alias(syslogp_ss,_syslogp_ss) +__weak_alias(vsyslogp_ss,_vsyslogp_ss) +#endif + +static struct syslog_data sdata = SYSLOG_DATA_INIT; + +static void openlog_unlocked_r(const char *, int, int, + struct syslog_data *); +static void disconnectlog_r(struct syslog_data *); +static void connectlog_r(struct syslog_data *); + +#define LOG_SIGNAL_SAFE (int)0x80000000 + + +#ifdef _REENTRANT +static mutex_t syslog_mutex = MUTEX_INITIALIZER; +#endif + +static char hostname[MAXHOSTNAMELEN]; + +/* + * syslog, vsyslog -- + * print message on log file; output is intended for syslogd(8). + */ +void +syslog(int pri, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vsyslog(pri, fmt, ap); + va_end(ap); +} + +void +vsyslog(int pri, const char *fmt, va_list ap) +{ + vsyslog_r(pri, &sdata, fmt, ap); +} + +/* + * syslogp, vsyslogp -- + * like syslog but take additional arguments for MSGID and SD + */ +void +syslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, ...) +{ + va_list ap; + + va_start(ap, msgfmt); + vsyslogp(pri, msgid, sdfmt, msgfmt, ap); + va_end(ap); +} + +void +vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, va_list ap) +{ + vsyslogp_r(pri, &sdata, msgid, sdfmt, msgfmt, ap); +} + +void +openlog(const char *ident, int logstat, int logfac) +{ + openlog_r(ident, logstat, logfac, &sdata); +} + +void +closelog(void) +{ + closelog_r(&sdata); +} + +/* setlogmask -- set the log mask level */ +int +setlogmask(int pmask) +{ + return setlogmask_r(pmask, &sdata); +} + +/* Reentrant version of syslog, i.e. syslog_r() */ + +void +syslog_r(int pri, struct syslog_data *data, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vsyslog_r(pri, data, fmt, ap); + va_end(ap); +} + +void +syslogp_r(int pri, struct syslog_data *data, const char *msgid, + const char *sdfmt, const char *msgfmt, ...) +{ + va_list ap; + + va_start(ap, msgfmt); + vsyslogp_r(pri, data, msgid, sdfmt, msgfmt, ap); + va_end(ap); +} + +void +syslog_ss(int pri, struct syslog_data *data, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap); + va_end(ap); +} + +void +syslogp_ss(int pri, struct syslog_data *data, const char *msgid, + const char *sdfmt, const char *msgfmt, ...) +{ + va_list ap; + + va_start(ap, msgfmt); + vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap); + va_end(ap); +} + +void +vsyslog_ss(int pri, struct syslog_data *data, const char *fmt, va_list ap) +{ + vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap); +} + +void +vsyslogp_ss(int pri, struct syslog_data *data, const char *msgid, + const char *sdfmt, const char *msgfmt, va_list ap) +{ + vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap); +} + + +void +vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap) +{ + vsyslogp_r(pri, data, NULL, NULL, fmt, ap); +} + +void +vsyslogp_r(int pri, struct syslog_data *data, const char *msgid, + const char *sdfmt, const char *msgfmt, va_list ap) +{ + static const char BRCOSP[] = "]: "; + static const char CRLF[] = "\r\n"; + size_t cnt, prlen, tries; + char ch, *p, *t; + struct timeval tv; + struct tm tmnow; + time_t now; + int fd, saved_errno; +#define TBUF_LEN 2048 +#define FMT_LEN 1024 +#define MAXTRIES 10 + char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN], fmt_cat[FMT_LEN] = ""; + size_t tbuf_left, fmt_left, msgsdlen; + char *fmt = fmt_cat; + int signal_safe = pri & LOG_SIGNAL_SAFE; + struct iovec iov[7]; /* prog + [ + pid + ]: + fmt + crlf */ + int opened, iovcnt; + + pri &= ~LOG_SIGNAL_SAFE; + +#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID + /* Check for invalid bits. */ + if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { + syslog_r(INTERNALLOG | signal_safe, data, + "syslog_r: unknown facility/priority: %x", pri); + pri &= LOG_PRIMASK|LOG_FACMASK; + } + + /* Check priority against setlogmask values. */ + if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask)) + return; + + saved_errno = errno; + + /* Set default facility if none specified. */ + if ((pri & LOG_FACMASK) == 0) + pri |= data->log_fac; + + /* Build the message. */ + p = tbuf; + tbuf_left = TBUF_LEN; + +#define DEC() \ + do { \ + if (prlen >= tbuf_left) \ + prlen = tbuf_left - 1; \ + p += prlen; \ + tbuf_left -= prlen; \ + } while (/*CONSTCOND*/0) + + prlen = snprintf_ss(p, tbuf_left, "<%d>1 ", pri); + DEC(); + + if (!signal_safe && (gettimeofday(&tv, NULL) != -1)) { + /* strftime() implies tzset(), localtime_r() doesn't. */ + tzset(); + now = (time_t) tv.tv_sec; + localtime_r(&now, &tmnow); + + prlen = strftime(p, tbuf_left, "%FT%T", &tmnow); + DEC(); + prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec); + DEC(); + prlen = strftime(p, tbuf_left-1, "%z", &tmnow); + /* strftime gives eg. "+0200", but we need "+02:00" */ + if (prlen == 5) { + p[prlen+1] = p[prlen]; + p[prlen] = p[prlen-1]; + p[prlen-1] = p[prlen-2]; + p[prlen-2] = ':'; + prlen += 1; + } + } else { + prlen = snprintf_ss(p, tbuf_left, "-"); + + /* if gmtime_r() was signal-safe we could output the UTC-time: + gmtime_r(&now, &tmnow); + prlen = strftime(p, tbuf_left, "%FT%TZ", &tmnow); + */ + } + DEC(); + prlen = snprintf_ss(p, tbuf_left, " %s ", hostname); + DEC(); + + if (data->log_tag == NULL) + data->log_tag = getprogname(); + + prlen = snprintf_ss(p, tbuf_left, "%s ", + data->log_tag ? data->log_tag : "-"); + if (data->log_stat & (LOG_PERROR|LOG_CONS)) { + iovcnt = 0; + iov[iovcnt].iov_base = p; + iov[iovcnt].iov_len = prlen - 1; + iovcnt++; + } + DEC(); + + if (data->log_stat & LOG_PID) { + prlen = snprintf_ss(p, tbuf_left, "%d ", getpid()); + if (data->log_stat & (LOG_PERROR|LOG_CONS)) { + iov[iovcnt].iov_base = __UNCONST("["); + iov[iovcnt].iov_len = 1; + iovcnt++; + iov[iovcnt].iov_base = p; + iov[iovcnt].iov_len = prlen - 1; + iovcnt++; + iov[iovcnt].iov_base = __UNCONST(BRCOSP); + iov[iovcnt].iov_len = 3; + iovcnt++; + } + } else { + prlen = snprintf_ss(p, tbuf_left, "- "); + if (data->log_stat & (LOG_PERROR|LOG_CONS)) { + iov[iovcnt].iov_base = __UNCONST(BRCOSP + 1); + iov[iovcnt].iov_len = 2; + iovcnt++; + } + } + DEC(); + + /* + * concat the format strings, then use one vsnprintf() + */ + if (msgid != NULL && *msgid != '\0') { + strlcat(fmt_cat, msgid, FMT_LEN); + strlcat(fmt_cat, " ", FMT_LEN); + } else + strlcat(fmt_cat, "- ", FMT_LEN); + + if (sdfmt != NULL && *sdfmt != '\0') { + strlcat(fmt_cat, sdfmt, FMT_LEN); + } else + strlcat(fmt_cat, "-", FMT_LEN); + + if (data->log_stat & (LOG_PERROR|LOG_CONS)) + msgsdlen = strlen(fmt_cat) + 1; + else + msgsdlen = 0; /* XXX: GCC */ + + if (msgfmt != NULL && *msgfmt != '\0') { + strlcat(fmt_cat, " ", FMT_LEN); + strlcat(fmt_cat, msgfmt, FMT_LEN); + } + + /* + * We wouldn't need this mess if printf handled %m, or if + * strerror() had been invented before syslog(). + */ + for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) { + if (ch == '%' && fmt[1] == 'm') { + char ebuf[128]; + ++fmt; + if (signal_safe || + strerror_r(saved_errno, ebuf, sizeof(ebuf))) + prlen = snprintf_ss(t, fmt_left, "Error %d", + saved_errno); + else + prlen = snprintf_ss(t, fmt_left, "%s", ebuf); + if (prlen >= fmt_left) + prlen = fmt_left - 1; + t += prlen; + fmt_left -= prlen; + } else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) { + *t++ = '%'; + *t++ = '%'; + fmt++; + fmt_left -= 2; + } else { + if (fmt_left > 1) { + *t++ = ch; + fmt_left--; + } + } + } + *t = '\0'; + + if (signal_safe) + prlen = vsnprintf_ss(p, tbuf_left, fmt_cpy, ap); + else + prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap); + + if (data->log_stat & (LOG_PERROR|LOG_CONS)) { + iov[iovcnt].iov_base = p + msgsdlen; + iov[iovcnt].iov_len = prlen - msgsdlen; + iovcnt++; + } + + DEC(); + cnt = p - tbuf; + + /* Output to stderr if requested. */ + if (data->log_stat & LOG_PERROR) { + iov[iovcnt].iov_base = __UNCONST(CRLF + 1); + iov[iovcnt].iov_len = 1; + (void)writev(STDERR_FILENO, iov, iovcnt + 1); + } + + /* Get connected, output the message to the local logger. */ + if (data == &sdata) + mutex_lock(&syslog_mutex); + opened = !data->opened; + if (opened) + openlog_unlocked_r(data->log_tag, data->log_stat, 0, data); + connectlog_r(data); + + /* + * If the send() failed, there are two likely scenarios: + * 1) syslogd was restarted + * 2) /dev/log is out of socket buffer space + * We attempt to reconnect to /dev/log to take care of + * case #1 and keep send()ing data to cover case #2 + * to give syslogd a chance to empty its socket buffer. + */ + for (tries = 0; tries < MAXTRIES; tries++) { + if (send(data->log_file, tbuf, cnt, 0) != -1) + break; + if (errno != ENOBUFS) { + disconnectlog_r(data); + connectlog_r(data); + } else + (void)usleep(1); + } + + /* + * Output the message to the console; try not to block + * as a blocking console should not stop other processes. + * Make sure the error reported is the one from the syslogd failure. + */ + if (tries == MAXTRIES && (data->log_stat & LOG_CONS) && + (fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) { + iov[iovcnt].iov_base = __UNCONST(CRLF); + iov[iovcnt].iov_len = 2; + (void)writev(fd, iov, iovcnt + 1); + (void)close(fd); + } + + if (data == &sdata) + mutex_unlock(&syslog_mutex); + + if (data != &sdata && opened) { + /* preserve log tag */ + const char *ident = data->log_tag; + closelog_r(data); + data->log_tag = ident; + } +} + +static void +disconnectlog_r(struct syslog_data *data) +{ + /* + * If the user closed the FD and opened another in the same slot, + * that's their problem. They should close it before calling on + * system services. + */ + if (data->log_file != -1) { + (void)close(data->log_file); + data->log_file = -1; + } + data->connected = 0; /* retry connect */ +} + +static void +connectlog_r(struct syslog_data *data) +{ + /* AF_UNIX address of local logger */ + static const struct sockaddr_un sun = { + .sun_family = AF_LOCAL, + .sun_len = sizeof(sun), + .sun_path = _PATH_LOG, + }; + + if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) { + if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) + return; + (void)fcntl(data->log_file, F_SETFD, FD_CLOEXEC); + data->connected = 0; + } + if (!data->connected) { + if (connect(data->log_file, + (const struct sockaddr *)(const void *)&sun, + sizeof(sun)) == -1) { + (void)close(data->log_file); + data->log_file = -1; + } else + data->connected = 1; + } +} + +static void +openlog_unlocked_r(const char *ident, int logstat, int logfac, + struct syslog_data *data) +{ + if (ident != NULL) + data->log_tag = ident; + data->log_stat = logstat; + if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) + data->log_fac = logfac; + + if (data->log_stat & LOG_NDELAY) /* open immediately */ + connectlog_r(data); + + /* We could cache this, but then it might change */ + if (gethostname(hostname, sizeof(hostname)) == -1 + || hostname[0] == '\0') { + /* can this really happen? */ + hostname[0] = '-'; + hostname[1] = '\0'; + } + data->opened = 1; +} + +void +openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data) +{ + if (data == &sdata) + mutex_lock(&syslog_mutex); + openlog_unlocked_r(ident, logstat, logfac, data); + if (data == &sdata) + mutex_unlock(&syslog_mutex); +} + +void +closelog_r(struct syslog_data *data) +{ + if (data == &sdata) + mutex_lock(&syslog_mutex); + (void)close(data->log_file); + data->log_file = -1; + data->connected = 0; + data->log_tag = NULL; + if (data == &sdata) + mutex_unlock(&syslog_mutex); +} + +int +setlogmask_r(int pmask, struct syslog_data *data) +{ + int omask; + + omask = data->log_mask; + if (pmask != 0) + data->log_mask = pmask; + return omask; +} diff --git a/lib/nbsd_libc/gen/telldir.c b/lib/nbsd_libc/gen/telldir.c new file mode 100644 index 000000000..f631642ac --- /dev/null +++ b/lib/nbsd_libc/gen/telldir.c @@ -0,0 +1,122 @@ +/* $NetBSD: telldir.c,v 1.19 2008/05/04 18:53:26 tonnerre Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)telldir.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: telldir.c,v 1.19 2008/05/04 18:53:26 tonnerre Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" +#include "extern.h" +#include + +#include +#include +#include +#include + +#include "dirent_private.h" + +#ifdef __weak_alias +__weak_alias(telldir,_telldir) +#endif + +long +telldir(DIR *dirp) +{ + long rv; +#ifdef _REENTRANT + if (__isthreaded) { + mutex_lock((mutex_t *)dirp->dd_lock); + rv = (intptr_t)_telldir_unlocked(dirp); + mutex_unlock((mutex_t *)dirp->dd_lock); + } else +#endif + rv = (intptr_t)_telldir_unlocked(dirp); + return rv; +} + +/* + * return a pointer into a directory + */ +long +_telldir_unlocked(DIR *dirp) +{ + struct dirpos *lp; + + for (lp = dirp->dd_internal; lp; lp = lp->dp_next) + if (lp->dp_seek == dirp->dd_seek && + lp->dp_loc == dirp->dd_loc) + return (intptr_t)lp; + + if ((lp = malloc(sizeof(*lp))) == NULL) + return (-1); + + lp->dp_seek = dirp->dd_seek; + lp->dp_loc = dirp->dd_loc; + lp->dp_next = dirp->dd_internal; + dirp->dd_internal = lp; + + return (intptr_t)lp; +} + +/* + * seek to an entry in a directory. + * Only values returned by "telldir" should be passed to seekdir. + */ +void +_seekdir_unlocked(DIR *dirp, long loc) +{ + struct dirpos *lp; + + _DIAGASSERT(dirp != NULL); + + for (lp = dirp->dd_internal; lp; lp = lp->dp_next) + if ((intptr_t)lp == loc) + break; + + if (lp == NULL) + return; + + if (lp->dp_loc == dirp->dd_loc && lp->dp_seek == dirp->dd_seek) + return; + + dirp->dd_seek = lseek(dirp->dd_fd, lp->dp_seek, SEEK_SET); + dirp->dd_loc = 0; + while (dirp->dd_loc < lp->dp_loc) + if (_readdir_unlocked(dirp, 0) == NULL) + break; +} diff --git a/lib/nbsd_libc/gen/time.3 b/lib/nbsd_libc/gen/time.3 new file mode 100644 index 000000000..5c44f95ae --- /dev/null +++ b/lib/nbsd_libc/gen/time.3 @@ -0,0 +1,112 @@ +.\" $NetBSD: time.3,v 1.15 2010/04/24 01:35:50 dholland Exp $ +.\" +.\" Copyright (c) 1989, 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. +.\" +.\" @(#)time.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd April 23, 2010 +.Dt TIME 3 +.Os +.Sh NAME +.Nm time +.Nd get time of day +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In time.h +.Ft time_t +.Fn time "time_t *tloc" +.Sh DESCRIPTION +The +.Fn time +function +returns the value of time in seconds since 0 hours, 0 minutes, +0 seconds, January 1, 1970, Coordinated Universal Time. +.Pp +A copy of the time value may be saved to the area indicated by the +pointer +.Fa tloc . +If +.Fa tloc +is a +.Dv NULL +pointer, no value is stored. +.Pp +Upon successful completion, +.Fn time +returns the value of time. +Otherwise a value of +.Po +.Po Fa time_t Pc \-1 +.Pc +is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +The following error codes may be set in +.Va errno : +.Bl -tag -width Er +.It Bq Er EFAULT +An argument address referenced invalid memory. +.El +.Sh SEE ALSO +.Xr gettimeofday 2 , +.Xr ctime 3 +.Sh STANDARDS +The +.Fn time +function conforms to +.St -p1003.1-90 . +.Sh HISTORY +A +.Fn time +function appeared in +.At v2 . +It returned a 32-bit value measuring sixtieths of a second, leading to +rollover every 2.26 years. +In +.At v6 , +the precision of +.Fn time +was changed to seconds, allowing 135.6 years between rollovers. +.Pp +In +.Nx 6.0 +the +.Vt time_t +type was changed to be 64 bits wide, including on 32-bit machines, +making rollover a concern for the far distant future only. +Note however that any code making the incorrect assumption that +.Vt time_t +is the same as +.Vt long +will fail on 32-bit machines in 2038. diff --git a/lib/nbsd_libc/gen/time.c b/lib/nbsd_libc/gen/time.c new file mode 100644 index 000000000..e0df22f85 --- /dev/null +++ b/lib/nbsd_libc/gen/time.c @@ -0,0 +1,57 @@ +/* $NetBSD: time.c,v 1.11 2009/01/11 02:46:27 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: time.c,v 1.11 2009/01/11 02:46:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include + +time_t +time(time_t *t) +{ + struct timeval tt; + + if (gettimeofday(&tt, NULL) == -1) + return (time_t)-1; + if (t != NULL) + *t = tt.tv_sec; + return tt.tv_sec; +} diff --git a/lib/nbsd_libc/gen/times.3 b/lib/nbsd_libc/gen/times.3 new file mode 100644 index 000000000..f14d9eb2d --- /dev/null +++ b/lib/nbsd_libc/gen/times.3 @@ -0,0 +1,151 @@ +.\" $NetBSD: times.3,v 1.15 2003/08/07 16:42:58 agc 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. +.\" +.\" @(#)times.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt TIMES 3 +.Os +.Sh NAME +.Nm times +.Nd process times +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/times.h +.Ft clock_t +.Fn times "struct tms *tp" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by +.Xr getrusage 2 +and +.Xr gettimeofday 2 . +.Ef +.Pp +The +.Fn times +function returns the value of time in clock ticks since 0 hours, 0 +minutes, 0 seconds, January 1, 1970, Coordinated Universal Time (UTC). +.Pp +The number of clock ticks per second may be determined by calling +.Xr sysconf 3 +with the +.Dv _SC_CLK_TCK +request. +It is generally (but not always) between 60 and 1024. +.Pp +Note that at the common rate of 100 ticks per second on many +.Nx +ports, and with a 32-bit unsigned clock_t, this value first wrapped in 1971. +.Pp +The +.Fn times +call also fills in the structure pointed to by +.Fa tp +with time-accounting information. +.Pp +The +.Fa tms +structure is defined as follows: +.Bd -literal -offset indent +typedef struct { + clock_t tms_utime; + clock_t tms_stime; + clock_t tms_cutime; + clock_t tms_cstime; +} +.Ed +.Pp +The elements of this structure are defined as follows: +.Bl -tag -width tms_cutime +.It Fa tms_utime +The +.Tn CPU +time charged for the execution of user instructions. +.It Fa tms_stime +The +.Tn CPU +time charged for execution by the system on behalf of +the process. +.It Fa tms_cutime +The sum of the +.Fa tms_utime s +and +.Fa tms_cutime s +of the child processes. +.It Fa tms_cstime +The sum of the +.Fa tms_stime Ns s +and +.Fa tms_cstime Ns s +of the child processes. +.El +.Pp +All times are measured in clock ticks, as defined above. +Note that at 100 ticks per second, +and with a 32-bit unsigned clock_t, +the values wrap after 497 days. +.Pp +The times of a terminated child process are included in the +.Fa tms_cutime +and +.Fa tms_cstime +elements of the parent when one of the +.Xr wait 2 +functions returns the process ID of the terminated child to the parent. +If an error occurs, +.Fn times +returns the value +.Pq (clock_t)\-1 , +and sets +.Va errno +to indicate the error. +.Sh ERRORS +The +.Fn times +function +may fail and set the global variable +.Va errno +for any of the errors specified for the library +routines +.Xr getrusage 2 +and +.Xr gettimeofday 2 . +.Sh SEE ALSO +.Xr time 1 , +.Xr getrusage 2 , +.Xr gettimeofday 2 , +.Xr wait 2 , +.Xr sysconf 3 +.Sh STANDARDS +The +.Fn times +function conforms to +.St -p1003.1-90 . diff --git a/lib/nbsd_libc/gen/times.c b/lib/nbsd_libc/gen/times.c new file mode 100644 index 000000000..55b05a18b --- /dev/null +++ b/lib/nbsd_libc/gen/times.c @@ -0,0 +1,92 @@ +/* $NetBSD: times.c,v 1.15 2009/01/11 02:46:27 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)times.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: times.c,v 1.15 2009/01/11 02:46:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include + +/* + * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000, + * but this would overflow if we switch to nanosec. + */ +#define CONVTCK(r) \ + (clock_t)(r.tv_sec * clk_tck + r.tv_usec / (1000000 / (uint)clk_tck)) + +#ifndef __times_rusage +#define __times_rusage struct rusage +#endif +#ifndef __times_timeval +#define __times_timeval struct timeval +#endif + +clock_t +times(struct tms *tp) +{ + __times_rusage ru; + __times_timeval t; + static clock_t clk_tck; + + _DIAGASSERT(tp != NULL); + + /* + * we use a local copy of CLK_TCK because it expands to a + * moderately expensive function call. + */ + if (clk_tck == 0) + clk_tck = (clock_t)CLK_TCK; + + if (getrusage(RUSAGE_SELF, &ru) < 0) + return ((clock_t)-1); + tp->tms_utime = CONVTCK(ru.ru_utime); + tp->tms_stime = CONVTCK(ru.ru_stime); + if (getrusage(RUSAGE_CHILDREN, &ru) < 0) + return ((clock_t)-1); + tp->tms_cutime = CONVTCK(ru.ru_utime); + tp->tms_cstime = CONVTCK(ru.ru_stime); + if (gettimeofday(&t, (struct timezone *)0)) + return ((clock_t)-1); + return ((clock_t)(CONVTCK(t))); +} diff --git a/lib/nbsd_libc/gen/timezone.3 b/lib/nbsd_libc/gen/timezone.3 new file mode 100644 index 000000000..6a202dad8 --- /dev/null +++ b/lib/nbsd_libc/gen/timezone.3 @@ -0,0 +1,75 @@ +.\" $NetBSD: timezone.3,v 1.12 2003/08/07 16:42:58 agc Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)timezone.3 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 19, 1994 +.Dt TIMEZONE 3 +.Os +.Sh NAME +.Nm timezone +.Nd return the timezone abbreviation +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.Ft char * +.Fn timezone "int zone" "int dst" +.Sh DESCRIPTION +.Bf -symbolic +.\" This interface is available from the compatibility library, libcompat; +This interface is available for compatibility only and will disappear in a +future software release; +it is impossible to reliably map timezone's arguments to a time zone +abbreviation. +See +.Xr ctime 3 ; +see +.Xr tzset 3 +for the new definition of this interface. +.Ef +.Pp +The +.Fn timezone +function returns a pointer to a time zone abbreviation for the specified +.Ar zone +and +.Ar dst +values. +.Ar Zone +is the number of minutes west of GMT and +.Ar dst +is non-zero if daylight savings time is in effect. +.Sh SEE ALSO +.Xr ctime 3 , +.Xr tzset 3 +.Sh HISTORY +A +.Fn timezone +function appeared in +.At v7 . diff --git a/lib/nbsd_libc/gen/toascii.3 b/lib/nbsd_libc/gen/toascii.3 new file mode 100644 index 000000000..e2c0e0f62 --- /dev/null +++ b/lib/nbsd_libc/gen/toascii.3 @@ -0,0 +1,83 @@ +.\" $NetBSD: toascii.3,v 1.13 2010/04/30 04:46:18 jruoho Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)toascii.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd April 30, 2010 +.Dt TOASCII 3 +.Os +.Sh NAME +.Nm toascii +.Nd convert a byte to 7-bit ASCII +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn toascii "int c" +.Sh DESCRIPTION +The +.Fn toascii +function returns the argument with all but the lower 7 bits cleared. +.Sh RETURN VALUES +The +.Fn toascii +function always returns a valid ASCII character. +The result is a non-negative integer in the +range from 0 to 127, inclusive. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr tolower 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn toascii +function conforms to +.St -xpg4 +and +.St -p1003.1-2001 . +The +.St -p1003.1-2008 +revision however marked it as obsolete, noting that +.Fn toascii +cannot be used portably in a localized application. diff --git a/lib/nbsd_libc/gen/toascii.c b/lib/nbsd_libc/gen/toascii.c new file mode 100644 index 000000000..7bddde11b --- /dev/null +++ b/lib/nbsd_libc/gen/toascii.c @@ -0,0 +1,52 @@ +/* $NetBSD: toascii.c,v 1.2 2003/08/07 16:42:58 agc Exp $ */ + +/* + * Copyright (c) 1989 The Regents of the University of California. + * All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * 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. + * + * from: @(#)isctype.c 5.2 (Berkeley) 6/1/90 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: toascii.c,v 1.2 2003/08/07 16:42:58 agc Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include + +#undef toascii +int +toascii(c) + int c; +{ + return ((c) & 0177); +} diff --git a/lib/nbsd_libc/gen/tolower.3 b/lib/nbsd_libc/gen/tolower.3 new file mode 100644 index 000000000..253a55e2a --- /dev/null +++ b/lib/nbsd_libc/gen/tolower.3 @@ -0,0 +1,102 @@ +.\" $NetBSD: tolower.3,v 1.14 2008/04/17 16:25:36 apb Exp $ +.\" +.\" Copyright (c) 1989, 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. +.\" +.\" @(#)tolower.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt TOLOWER 3 +.Os +.Sh NAME +.Nm tolower +.Nd upper case to lower case letter conversion +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn tolower "int c" +.Sh DESCRIPTION +The +.Fn tolower +function converts an upper-case letter to the corresponding lower-case +letter. +.Sh RETURN VALUES +If the argument is an upper-case letter, the +.Fn tolower +function returns the corresponding lower-case letter if there is +one; otherwise the argument is returned unchanged. +.\" In the +.\" .Em ``C'' +.\" locale, +.\" .Fn tolower +.\" maps only the characters for which +.\" .Xr isupper +.\" is true to the corresponding characters for which +.\" .Xr islower +.\" is true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr toupper 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn tolower +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn tolower +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/tolower_.c b/lib/nbsd_libc/gen/tolower_.c new file mode 100644 index 000000000..edc6ee3a6 --- /dev/null +++ b/lib/nbsd_libc/gen/tolower_.c @@ -0,0 +1,57 @@ +/* $NetBSD: tolower_.c,v 1.13 2010/06/01 13:52:08 tnozaki Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_RCS) && !defined(lint) +__RCSID("$NetBSD: tolower_.c,v 1.13 2010/06/01 13:52:08 tnozaki Exp $"); +#endif /* LIBC_RCS and not lint */ + +#include +#include +#include "ctype_local.h" + +#if EOF != -1 +#error "EOF != -1" +#endif + +const short _C_tolower_[1 + _CTYPE_NUM_CHARS] = { + EOF, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff +}; + +const short *_tolower_tab_ = &_C_tolower_[0]; diff --git a/lib/nbsd_libc/gen/toupper.3 b/lib/nbsd_libc/gen/toupper.3 new file mode 100644 index 000000000..b927a5460 --- /dev/null +++ b/lib/nbsd_libc/gen/toupper.3 @@ -0,0 +1,101 @@ +.\" $NetBSD: toupper.3,v 1.17 2008/04/17 16:25:36 apb Exp $ +.\" +.\" Copyright (c) 1989, 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. +.\" +.\" @(#)toupper.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd April 17, 2008 +.Dt TOUPPER 3 +.Os +.Sh NAME +.Nm toupper +.Nd lower case to upper case letter conversion +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ctype.h +.Ft int +.Fn toupper "int c" +.Sh DESCRIPTION +The +.Fn toupper +function converts a lower-case letter to the corresponding +upper-case letter. +.Sh RETURN VALUES +If the argument is a lower-case letter, the +.Fn toupper +function returns the corresponding upper-case letter if there is +one; otherwise the argument is returned unchanged. +.\" In the +.\" .Em ``C'' +.\" locale, +.\" .Fn toupper +.\" maps only the characters for which +.\" .Xr islower +.\" is true to the corresponding characters for which +.\" .Xr isupper +.\" is true. +.Sh SEE ALSO +.Xr ctype 3 , +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isascii 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 , +.Xr stdio 3 , +.Xr toascii 3 , +.Xr ascii 7 +.Sh STANDARDS +The +.Fn toupper +function conforms to +.St -ansiC . +.Sh CAVEATS +The argument to +.Fn toupper +must be +.Dv EOF +or representable as an +.Vt unsigned char ; +otherwise, the behavior is undefined. +See the +.Sx CAVEATS +section of +.Xr ctype 3 +for more details. diff --git a/lib/nbsd_libc/gen/toupper_.c b/lib/nbsd_libc/gen/toupper_.c new file mode 100644 index 000000000..864f7e172 --- /dev/null +++ b/lib/nbsd_libc/gen/toupper_.c @@ -0,0 +1,57 @@ +/* $NetBSD: toupper_.c,v 1.13 2010/06/01 13:52:08 tnozaki Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_RCS) && !defined(lint) +__RCSID("$NetBSD: toupper_.c,v 1.13 2010/06/01 13:52:08 tnozaki Exp $"); +#endif /* LIBC_RCS and not lint */ + +#include +#include +#include "ctype_local.h" + +#if EOF != -1 +#error "EOF != -1" +#endif + +const short _C_toupper_[1 + _CTYPE_NUM_CHARS] = { + EOF, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff +}; + +const short *_toupper_tab_ = &_C_toupper_[0]; diff --git a/lib/nbsd_libc/gen/ttyname.3 b/lib/nbsd_libc/gen/ttyname.3 new file mode 100644 index 000000000..a9c094122 --- /dev/null +++ b/lib/nbsd_libc/gen/ttyname.3 @@ -0,0 +1,204 @@ +.\" $NetBSD: ttyname.3,v 1.21 2008/06/25 11:47:29 ad Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)ttyname.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 25, 2008 +.Dt TTYNAME 3 +.Os +.Sh NAME +.Nm ttyname , +.Nm ttyname_r , +.Nm isatty , +.Nm ttyslot +.Nd get name of associated terminal (tty) from file descriptor +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft char * +.Fn ttyname "int fd" +.Ft int +.Fn ttyname_r "int fd" "char *buf" "size_t len" +.Ft int +.Fn isatty "int fd" +.In stdlib.h +.Ft int +.Fn ttyslot +.Sh DESCRIPTION +These functions operate on the system file descriptors for terminal +type devices. +These descriptors are not related to the standard +.Tn I/O +.Dv FILE +typedef, but refer to the special device files found in +.Pa /dev +and named +.Pa /dev/tty Ns Em xx +and for which an entry exists in the initialization file +.Pa /etc/ttys +(see +.Xr ttys 5 ) , +or for pseudo-terminal devices created in ptyfs and named +.Pa /dev/pts/ Ns Em n . +.Pp +The +.Fn isatty +function +determines if the file descriptor +.Fa fd +refers to a valid terminal type device. +.Pp +The +.Fn ttyname +function gets the related device name of a file descriptor for +which +.Fn isatty +is true. +The +.Fn ttyname_r +is the reentrant version of the above, and it places the results in +.Fa buf . +If there is not enough space to place the results (indicated by +.Fa len ) , +then it returns an error. +.Pp +The +.Fn ttyslot +function +fetches the current process' control terminal number from the +.Xr ttys 5 +file entry. +If the terminal is a pseudo-terminal, and there is no special entry +in the +.Xr ttys 5 +file for it, the slot number returned is 1 + (last slot number) + +minor(tty). +This will return a consistent and unique number for each pseudo-terminal +device without requiring one to enumerate all of them in +.Xr ttys 5 . +.Sh IMPLEMENTATION NOTES +As an optimisation, these functions attempt to obtain information about +all devices from the +.Pa /var/run/dev.db +database, if it exists. +If the database exists but is out of date, then these functions +may produce incorrect results. +The database should be updated using the +.Xr dev_mkdb 8 +command. +.Sh RETURN VALUES +The +.Fn ttyname +function returns the NUL-terminated name if the device is found and +.Fn isatty +is true; otherwise +a +.Dv NULL +pointer is returned and +.Va errno +is set to indicate the error. +.Pp +The +.Fn ttyname_r +functions returns 0 on success and an error code on failure. +.Pp +The +.Fn isatty +function returns 1 if +.Fa fd +is associated with a terminal device; otherwise it returns 0 and +.Va errno +is set to indicate the error. +.Pp +The +.Fn ttyslot +function +returns the unit number of the device file if found; otherwise +the value zero is returned. +.Sh FILES +.Bl -tag -width /etc/ttys -compact +.It Pa /dev/\(** +.It Pa /etc/ttys +.El +.Sh ERRORS +The +.Fn ttyname , +.Fn ttyname_r , +and +.Fn isatty +functions will fail if: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa fd +argument is not a valid file descriptor. +.It Bq Er ENOTTY +The +.Fa fd +argument does not refer to a terminal device. +.El +.Pp +The +.Fn ttyname_r +function will also fail if: +.Bl -tag -width Er +.It Bq Er ERANGE +The buffer provided is not large enough to fit the result. +.It Bq Er ENOENT +The terminal device is not found. +This can happen if the device node has been removed after it was opened. +.El +.Sh SEE ALSO +.Xr ioctl 2 , +.Xr ttys 5 , +.Xr dev_mkdb 8 +.Sh STANDARDS +The +.Fn ttyname +and +.Fn isatty +functions conform to +.St -p1003.1-90 . +.Sh HISTORY +.Fn isatty , +.Fn ttyname , +and +.Fn ttyslot +functions appeared in +.At v7 . +.\" Use of the .Pa /var/run/dev.db file was added in ???. +.Sh BUGS +The +.Fn ttyname +function leaves its result in an internal static object and returns +a pointer to that object. +Subsequent calls to +.Fn ttyname +will modify the same object. diff --git a/lib/nbsd_libc/gen/ttyname.c b/lib/nbsd_libc/gen/ttyname.c new file mode 100644 index 000000000..6002c1e2f --- /dev/null +++ b/lib/nbsd_libc/gen/ttyname.c @@ -0,0 +1,177 @@ +/* $NetBSD: ttyname.c,v 1.24 2008/06/25 11:47:29 ad Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ttyname.c 8.2 (Berkeley) 1/27/94"; +#else +__RCSID("$NetBSD: ttyname.c,v 1.24 2008/06/25 11:47:29 ad Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(ttyname,_ttyname) +__weak_alias(ttyname_r,_ttyname_r) +#endif + +static int oldttyname(const struct stat *, char *, size_t); + +int +ttyname_r(int fd, char *buf, size_t len) +{ + struct stat sb; + struct termios ttyb; + DB *db; + DBT data, key; + struct { + mode_t type; + dev_t dev; + } bkey; + struct ptmget ptm; +#define DEVSZ (sizeof(_PATH_DEV) - 1) + + _DIAGASSERT(fd != -1); + + if (len <= DEVSZ) { + return ERANGE; + } + + /* If it is a pty, deal with it quickly */ + if (ioctl(fd, TIOCPTSNAME, &ptm) != -1) { + if (strlcpy(buf, ptm.sn, len) >= len) { + return ERANGE; + } + return 0; + } + /* Must be a terminal. */ + if (tcgetattr(fd, &ttyb) == -1) + return errno; + + /* Must be a character device. */ + if (fstat(fd, &sb)) + return errno; + if (!S_ISCHR(sb.st_mode)) + return ENOTTY; + + (void)memcpy(buf, _PATH_DEV, DEVSZ); + if ((db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)) != NULL) { + (void)memset(&bkey, 0, sizeof(bkey)); + bkey.type = S_IFCHR; + bkey.dev = sb.st_rdev; + key.data = &bkey; + key.size = sizeof(bkey); + if (!(db->get)(db, &key, &data, 0)) { + if (len - DEVSZ <= data.size) { + return ERANGE; + } + (void)memcpy(buf + DEVSZ, data.data, data.size); + (void)(db->close)(db); + return 0; + } + (void)(db->close)(db); + } + if (oldttyname(&sb, buf, len) == -1) + return errno; + return 0; +} + +static int +oldttyname(const struct stat *sb, char *buf, size_t len) +{ + struct dirent *dirp; + DIR *dp; + struct stat dsb; + size_t dlen; + + _DIAGASSERT(sb != NULL); + + if ((dp = opendir(_PATH_DEV)) == NULL) + return -1; + + while ((dirp = readdir(dp)) != NULL) { + if (dirp->d_fileno != sb->st_ino) + continue; + dlen = dirp->d_namlen + 1; + if (len - DEVSZ <= dlen) { + /* + * XXX: we return an error if *any* entry does not + * fit + */ + errno = ERANGE; + (void)closedir(dp); + return -1; + } + (void)memcpy(buf + DEVSZ, dirp->d_name, dlen); + if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev || + sb->st_ino != dsb.st_ino) + continue; + (void)closedir(dp); + return 0; + } + (void)closedir(dp); + /* + * XXX: Documented by TOG to return EBADF or ENOTTY only; neither are + * applicable here. + */ + errno = ENOENT; + return -1; +} + +char * +ttyname(int fd) +{ + static char buf[MAXPATHLEN]; + int rv; + + rv = ttyname_r(fd, buf, sizeof(buf)); + if (rv != 0) { + errno = rv; + return NULL; + } + return buf; +} diff --git a/lib/nbsd_libc/gen/ttyslot.c b/lib/nbsd_libc/gen/ttyslot.c new file mode 100644 index 000000000..33ac4540a --- /dev/null +++ b/lib/nbsd_libc/gen/ttyslot.c @@ -0,0 +1,96 @@ +/* $NetBSD: ttyslot.c,v 1.13 2009/01/11 02:46:27 christos Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ttyslot.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: ttyslot.c,v 1.13 2009/01/11 02:46:27 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(ttyslot,_ttyslot) +#endif + +int +ttyslot(void) +{ + struct ttyent *ttyp; + int slot = 0, ispty = 0; + char *p; + int cnt; + char *name; + struct ptmget ptm; + + setttyent(); + for (cnt = 0; cnt < 3; ++cnt) { + if (ioctl(cnt, TIOCPTSNAME, &ptm) != -1) { + ispty = 1; + name = ptm.sn; + } else if ((name = ttyname(cnt)) != NULL) { + ispty = 0; + } else + continue; + + if ((p = strstr(name, "/pts/")) != NULL) + ++p; + else if ((p = strrchr(name, '/')) != NULL) + ++p; + else + p = name; + + for (slot = 1; (ttyp = getttyent()) != NULL; ++slot) + if (!strcmp(ttyp->ty_name, p)) { + endttyent(); + return slot; + } + break; + } + endttyent(); + if (ispty) { + struct stat st; + if (fstat(cnt, &st) == -1) + return 0; + return slot + (int)minor(st.st_rdev) + 1; + } + return 0; +} diff --git a/lib/nbsd_libc/gen/ualarm.3 b/lib/nbsd_libc/gen/ualarm.3 new file mode 100644 index 000000000..7e5b7d357 --- /dev/null +++ b/lib/nbsd_libc/gen/ualarm.3 @@ -0,0 +1,108 @@ +.\" $NetBSD: ualarm.3,v 1.18 2010/04/29 06:07:35 jruoho Exp $ +.\" +.\" Copyright (c) 1986, 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. +.\" +.\" @(#)ualarm.3 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 29, 2010 +.Dt UALARM 3 +.Os +.Sh NAME +.Nm ualarm +.Nd schedule signal after specified time +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft useconds_t +.Fn ualarm "useconds_t microseconds" "useconds_t interval" +.Sh DESCRIPTION +.Bf -symbolic +This is a simplified interface to +.Xr setitimer 2 . +.Ef +.Pp +The +.Fn ualarm +function +waits a count of +.Ar microseconds +before asserting the terminating signal +.Dv SIGALRM . +System activity or time used in processing the call may cause a slight +delay. +.Pp +If the +.Fa interval +argument is non-zero, the +.Dv SIGALRM +signal will be sent +to the process every +.Fa interval +microseconds after the timer expires (e.g. after +.Fa microseconds +microseconds have passed). +.Sh RETURN VALUES +When the signal has successfully been caught, +.Fn ualarm +returns the amount of time left on the clock. +The maximum number of +.Ar microseconds +allowed +is 2147483647. +If there is an error setting the timer, +.Fn ualarm +returns ((useconds_t) -1). +.Sh SEE ALSO +.Xr getitimer 2 , +.Xr setitimer 2 , +.Xr sigaction 2 , +.Xr sigsuspend 2 , +.Xr alarm 3 , +.Xr signal 3 , +.Xr sigvec 3 , +.Xr sleep 3 , +.Xr usleep 3 +.Sh STANDARDS +The +.Fn ualarm +functions conforms to +.St -xpg4.2 +and +.St -p1003.1-2001 . +The latter standard marked +.Fn ualarm +as obsolescent and a later revision, +.St -p1003.1-2008 , +removed the specification of +.Fn ualarm . +.Sh HISTORY +The +.Fn ualarm +function appeared in +.Bx 4.3 . diff --git a/lib/nbsd_libc/gen/ualarm.c b/lib/nbsd_libc/gen/ualarm.c new file mode 100644 index 000000000..2da867050 --- /dev/null +++ b/lib/nbsd_libc/gen/ualarm.c @@ -0,0 +1,74 @@ +/* $NetBSD: ualarm.c,v 1.10 2003/08/07 16:42:58 agc Exp $ */ + +/* + * Copyright (c) 1985, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)ualarm.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: ualarm.c,v 1.10 2003/08/07 16:42:58 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(ualarm,_ualarm) +#endif + +#define USPS 1000000 /* # of microseconds in a second */ + +/* + * Generate a SIGALRM signal in ``usecs'' microseconds. + * If ``reload'' is non-zero, keep generating SIGALRM + * every ``reload'' microseconds after the first signal. + */ +useconds_t +ualarm(usecs, reload) + useconds_t usecs; + useconds_t reload; +{ + struct itimerval new, old; + + new.it_interval.tv_usec = reload % USPS; + new.it_interval.tv_sec = reload / USPS; + + new.it_value.tv_usec = usecs % USPS; + new.it_value.tv_sec = usecs / USPS; + + if (setitimer(ITIMER_REAL, &new, &old) == 0) + return (useconds_t)(old.it_value.tv_sec * USPS + + old.it_value.tv_usec); + + return (useconds_t)-1; +} diff --git a/lib/nbsd_libc/gen/ulimit.3 b/lib/nbsd_libc/gen/ulimit.3 new file mode 100644 index 000000000..5312050cf --- /dev/null +++ b/lib/nbsd_libc/gen/ulimit.3 @@ -0,0 +1,119 @@ +.\" $NetBSD: ulimit.3,v 1.9 2010/04/30 05:09:23 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 April 30, 2010 +.Dt ULIMIT 3 +.Os +.Sh NAME +.Nm ulimit +.Nd get and set process limits +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In ulimit.h +.Ft long int +.Fn ulimit "int cmd" ... +.Sh DESCRIPTION +The +.Fn ulimit +function provides a method to query or alter resource limits of the calling +process. +The method to be performed is specified by the +.Fa cmd +argument; possible values are: +.Bl -tag -width UL_GETFSIZEXX +.It Li UL_GETFSIZE +Return the soft file size limit of the process. +The value returned is in units of 512-byte blocks. +If the result cannot be represented in an object of type +.Fa long int , +the result is unspecified. +.It Li UL_SETFSIZE +Set the hard and soft file size limits of the process to the value of the +second argument passed, which is in units of 512-byte blocks, and which is +expected to be of type +.Fa long int . +The new file size limit of the process is returned. +Any process may decrease the limit, but raising it is only permitted if +the caller is the super-user. +.El +.Pp +If successful, the +.Fn ulimit +function will not change the setting of +.Va errno . +.Sh RETURN VALUES +If successful, the +.Fn ulimit +function returns the value of the requested limit. +Otherwise, it returns \-1, sets +.Va errno +to indicate an error, and the limit is not changed. +Therefore, to detect an error condition applications should set +.Va errno +to 0, call +.Fn ulimit , +and check if \-1 is returned and +.Va errno +is non-zero. +.Sh ERRORS +The +.Fn ulimit +function will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Fa cmd +argument is not valid. +.It Bq Er EPERM +It was attempted to increase a limit, and the caller is not the super-user. +.El +.Sh SEE ALSO +.Xr getrlimit 2 , +.Xr setrlimit 2 +.Sh STANDARDS +The +.Fn ulimit +function conforms to +.St -xsh5 +and +.St -p1003.1-2001 . +It was marked as obsolete in the +.St -p1003.1-2008 +revision, which recommended the use of +.Xr getrlimit 2 +and +.Xr setrlimit 2 +instead, noting that because +.Fn ulimit +uses the type +.Vt long +rather than +.Vt rlim_t , +it may not be sufficient for file sizes on many current systems. diff --git a/lib/nbsd_libc/gen/ulimit.c b/lib/nbsd_libc/gen/ulimit.c new file mode 100644 index 000000000..ba61a22f9 --- /dev/null +++ b/lib/nbsd_libc/gen/ulimit.c @@ -0,0 +1,70 @@ +/* $NetBSD: ulimit.c,v 1.3 2008/04/28 20:22:59 martin 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ulimit.c,v 1.3 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +long int +ulimit(int cmd, ...) +{ + va_list ap; + struct rlimit rlimit; + long int new_limit, result; + + va_start(ap, cmd); + + result = -1L; + switch (cmd) { + case UL_GETFSIZE: + if (getrlimit(RLIMIT_FSIZE, &rlimit) == 0) + result = (long int)(rlimit.rlim_cur / 512); + break; + case UL_SETFSIZE: + new_limit = va_arg(ap, long int); + rlimit.rlim_cur = rlimit.rlim_max = (rlim_t)new_limit * 512; + if (setrlimit(RLIMIT_FSIZE, &rlimit) == 0) + result = new_limit; + break; + default: + errno = EINVAL; + } + + va_end(ap); + + return (result); +} diff --git a/lib/nbsd_libc/gen/uname.3 b/lib/nbsd_libc/gen/uname.3 new file mode 100644 index 000000000..33a2c0c4d --- /dev/null +++ b/lib/nbsd_libc/gen/uname.3 @@ -0,0 +1,93 @@ +.\" $NetBSD: uname.3,v 1.11 2010/04/27 15:16:28 jruoho Exp $ +.\" +.\" Copyright (c) 1994 +.\" 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. +.\" +.\" @(#)uname.3 8.1 (Berkeley) 1/4/94 +.\" +.Dd April 27, 2010 +.Dt UNAME 3 +.Os +.Sh NAME +.Nm uname +.Nd get system identification +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/utsname.h +.Ft int +.Fn uname "struct utsname *name" +.Sh DESCRIPTION +The +.Fn uname +function stores nul-terminated strings of information identifying +the current system into the structure referenced by +.Fa name . +.Pp +The +.Li utsname +structure is defined in the +.In sys/utsname.h +header file, and contains the following members: +.Bl -tag -width nodenameXXXX -offset indent +.It Va sysname +Name of the operating system implementation. +.It Va nodename +Network name of this machine. +.It Va release +Release level of the operating system. +.It Va version +Version level of the operating system. +.It Va machine +Machine hardware platform. +.El +.Sh RETURN VALUES +If +.Nm uname +is successful, 0 is returned, otherwise, \-1 is returned and +.Va errno +is set appropriately. +.Sh ERRORS +The +.Fn uname +function may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr sysctl 3 . +.Sh SEE ALSO +.Xr uname 1 , +.Xr sysctl 3 +.Sh STANDARDS +The +.Fn uname +function conforms to +.St -p1003.1-90 . +.Sh HISTORY +The +.Nm uname +function first appeared in +.Bx 4.4 . diff --git a/lib/nbsd_libc/gen/uname.c b/lib/nbsd_libc/gen/uname.c new file mode 100644 index 000000000..4aa704b82 --- /dev/null +++ b/lib/nbsd_libc/gen/uname.c @@ -0,0 +1,106 @@ +/* $NetBSD: uname.c,v 1.10 2007/01/15 22:26:35 cbiere Exp $ */ + +/*- + * Copyright (c) 1994 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)uname.c 8.1 (Berkeley) 1/4/94"; +#else +__RCSID("$NetBSD: uname.c,v 1.10 2007/01/15 22:26:35 cbiere Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include + +#ifdef __weak_alias +__weak_alias(uname,_uname) +#endif + +int +uname(name) + struct utsname *name; +{ + int mib[2]; + size_t len; + char *p; + + _DIAGASSERT(name != NULL); + + mib[0] = CTL_KERN; + mib[1] = KERN_OSTYPE; + len = sizeof(name->sysname); + if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) + goto error; + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTNAME; + len = sizeof(name->nodename); + if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) + goto error; + + mib[0] = CTL_KERN; + mib[1] = KERN_OSRELEASE; + len = sizeof(name->release); + if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) + goto error; + + mib[0] = CTL_KERN; + mib[1] = KERN_VERSION; + len = sizeof(name->version); + if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) + goto error; + + /* The version may have newlines in it, turn them into spaces. */ + for (p = name->version; len--; ++p) { + if (*p == '\n' || *p == '\t') { + if (len > 1) + *p = ' '; + else + *p = '\0'; + } + } + + mib[0] = CTL_HW; + mib[1] = HW_MACHINE; + len = sizeof(name->machine); + if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) + goto error; + return (0); + +error: + return (-1); +} diff --git a/lib/nbsd_libc/gen/unvis.3 b/lib/nbsd_libc/gen/unvis.3 new file mode 100644 index 000000000..3f6fdc550 --- /dev/null +++ b/lib/nbsd_libc/gen/unvis.3 @@ -0,0 +1,208 @@ +.\" $NetBSD: unvis.3,v 1.20 2010/11/28 01:28:21 wiz 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. +.\" +.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93 +.\" +.Dd November 27, 2010 +.Dt UNVIS 3 +.Os +.Sh NAME +.Nm unvis , +.Nm strunvis +.Nd decode a visual representation of characters +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In vis.h +.Ft int +.Fn unvis "char *cp" "int c" "int *astate" "int flag" +.Ft int +.Fn strunvis "char *dst" "const char *src" +.Ft int +.Fn strunvisx "char *dst" "const char *src" "int flag" +.Sh DESCRIPTION +The +.Fn unvis , +.Fn strunvis +and +.Fn strunvisx +functions +are used to decode a visual representation of characters, as produced +by the +.Xr vis 3 +function, back into +the original form. +.Pp +The +.Fn unvis +function is called with successive characters in +.Ar c +until a valid sequence is recognized, at which time the decoded +character is available at the character pointed to by +.Ar cp . +.Pp +The +.Fn strunvis +function decodes the characters pointed to by +.Ar src +into the buffer pointed to by +.Ar dst . +The +.Fn strunvis +function simply copies +.Ar src +to +.Ar dst , +decoding any escape sequences along the way, +and returns the number of characters placed into +.Ar dst , +or \-1 if an +invalid escape sequence was detected. +The size of +.Ar dst +should be equal to the size of +.Ar src +(that is, no expansion takes place during decoding). +.Pp +The +.Fn strunvisx +function does the same as the +.Fn strunvis +function, +but it allows you to add a flag that specifies the style the string +.Ar src +is encoded with. +Currently, the supported flags are: +.Dv VIS_HTTPSTYLE +and +.Dv VIS_MIMESTYLE . +.Pp +The +.Fn unvis +function implements a state machine that can be used to decode an +arbitrary stream of bytes. +All state associated with the bytes being decoded is stored outside the +.Fn unvis +function (that is, a pointer to the state is passed in), so +calls decoding different streams can be freely intermixed. +To start decoding a stream of bytes, first initialize an integer to zero. +Call +.Fn unvis +with each successive byte, along with a pointer +to this integer, and a pointer to a destination character. +The +.Fn unvis +function has several return codes that must be handled properly. +They are: +.Bl -tag -width UNVIS_VALIDPUSH +.It Li \&0 (zero) +Another character is necessary; nothing has been recognized yet. +.It Dv UNVIS_VALID +A valid character has been recognized and is available at the location +pointed to by cp. +.It Dv UNVIS_VALIDPUSH +A valid character has been recognized and is available at the location +pointed to by cp; however, the character currently passed in should +be passed in again. +.It Dv UNVIS_NOCHAR +A valid sequence was detected, but no character was produced. +This return code is necessary to indicate a logical break between characters. +.It Dv UNVIS_SYNBAD +An invalid escape sequence was detected, or the decoder is in an unknown state. +The decoder is placed into the starting state. +.El +.Pp +When all bytes in the stream have been processed, call +.Fn unvis +one more time with flag set to +.Dv UNVIS_END +to extract any remaining character (the character passed in is ignored). +.Pp +The +.Ar flag +argument is also used to specify the encoding style of the source. +If set to +.Dv VIS_HTTPSTYLE +or +.Dv VIS_HTTP1808 , +.Fn unvis +will decode URI strings as specified in RFC 1808. +If set to +.Dv VIS_HTTP1866 , +.Fn unvis +will decode URI strings as specified in RFC 1866. +If set to +.Dv VIS_MIMESTYLE , +.Fn unvis +will decode MIME Quoted-Printable strings as specified in RFC 2045. +If set to +.Dv VIS_NOESCAPE , +.Fn unvis +will not decode \e quoted characters. +.Pp +The following code fragment illustrates a proper use of +.Fn unvis . +.Bd -literal -offset indent +int state = 0; +char out; + +while ((ch = getchar()) != EOF) { +again: + switch(unvis(\*[Am]out, ch, \*[Am]state, 0)) { + case 0: + case UNVIS_NOCHAR: + break; + case UNVIS_VALID: + (void)putchar(out); + break; + case UNVIS_VALIDPUSH: + (void)putchar(out); + goto again; + case UNVIS_SYNBAD: + errx(EXIT_FAILURE, "Bad character sequence!"); + } +} +if (unvis(\*[Am]out, '\e0', \*[Am]state, UNVIS_END) == UNVIS_VALID) + (void)putchar(out); +.Ed +.Sh SEE ALSO +.Xr unvis 1 , +.Xr vis 1 , +.Xr vis 3 +.Rs +.%A R. Fielding +.%T Relative Uniform Resource Locators +.%O RFC1808 +.Re +.Sh HISTORY +The +.Fn unvis +function +first appeared in +.Bx 4.4 . diff --git a/lib/nbsd_libc/gen/unvis.c b/lib/nbsd_libc/gen/unvis.c new file mode 100644 index 000000000..13bc869fc --- /dev/null +++ b/lib/nbsd_libc/gen/unvis.c @@ -0,0 +1,514 @@ +/* $NetBSD: unvis.c,v 1.32 2010/11/27 21:22:11 christos Exp $ */ + +/*- + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: unvis.c,v 1.32 2010/11/27 21:22:11 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(strunvis,_strunvis) +#endif + +#if !HAVE_VIS +/* + * decode driven by state machine + */ +#define S_GROUND 0 /* haven't seen escape char */ +#define S_START 1 /* start decoding special sequence */ +#define S_META 2 /* metachar started (M) */ +#define S_META1 3 /* metachar more, regular char (-) */ +#define S_CTRL 4 /* control char started (^) */ +#define S_OCTAL2 5 /* octal digit 2 */ +#define S_OCTAL3 6 /* octal digit 3 */ +#define S_HEX1 7 /* http hex digit */ +#define S_HEX2 8 /* http hex digit 2 */ +#define S_MIME1 9 /* mime hex digit 1 */ +#define S_MIME2 10 /* mime hex digit 2 */ +#define S_EATCRNL 11 /* mime eating CRNL */ +#define S_AMP 12 /* seen & */ +#define S_NUMBER 13 /* collecting number */ +#define S_STRING 14 /* collecting string */ + +#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') +#define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10)) +#define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10)) + +/* + * RFC 1866 + */ +static const struct nv { + const char *name; + uint8_t value; +} nv[] = { + { "AElig", 198 }, /* capital AE diphthong (ligature) */ + { "Aacute", 193 }, /* capital A, acute accent */ + { "Acirc", 194 }, /* capital A, circumflex accent */ + { "Agrave", 192 }, /* capital A, grave accent */ + { "Aring", 197 }, /* capital A, ring */ + { "Atilde", 195 }, /* capital A, tilde */ + { "Auml", 196 }, /* capital A, dieresis or umlaut mark */ + { "Ccedil", 199 }, /* capital C, cedilla */ + { "ETH", 208 }, /* capital Eth, Icelandic */ + { "Eacute", 201 }, /* capital E, acute accent */ + { "Ecirc", 202 }, /* capital E, circumflex accent */ + { "Egrave", 200 }, /* capital E, grave accent */ + { "Euml", 203 }, /* capital E, dieresis or umlaut mark */ + { "Iacute", 205 }, /* capital I, acute accent */ + { "Icirc", 206 }, /* capital I, circumflex accent */ + { "Igrave", 204 }, /* capital I, grave accent */ + { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */ + { "Ntilde", 209 }, /* capital N, tilde */ + { "Oacute", 211 }, /* capital O, acute accent */ + { "Ocirc", 212 }, /* capital O, circumflex accent */ + { "Ograve", 210 }, /* capital O, grave accent */ + { "Oslash", 216 }, /* capital O, slash */ + { "Otilde", 213 }, /* capital O, tilde */ + { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */ + { "THORN", 222 }, /* capital THORN, Icelandic */ + { "Uacute", 218 }, /* capital U, acute accent */ + { "Ucirc", 219 }, /* capital U, circumflex accent */ + { "Ugrave", 217 }, /* capital U, grave accent */ + { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */ + { "Yacute", 221 }, /* capital Y, acute accent */ + { "aacute", 225 }, /* small a, acute accent */ + { "acirc", 226 }, /* small a, circumflex accent */ + { "acute", 180 }, /* acute accent */ + { "aelig", 230 }, /* small ae diphthong (ligature) */ + { "agrave", 224 }, /* small a, grave accent */ + { "amp", 38 }, /* ampersand */ + { "aring", 229 }, /* small a, ring */ + { "atilde", 227 }, /* small a, tilde */ + { "auml", 228 }, /* small a, dieresis or umlaut mark */ + { "brvbar", 166 }, /* broken (vertical) bar */ + { "ccedil", 231 }, /* small c, cedilla */ + { "cedil", 184 }, /* cedilla */ + { "cent", 162 }, /* cent sign */ + { "copy", 169 }, /* copyright sign */ + { "curren", 164 }, /* general currency sign */ + { "deg", 176 }, /* degree sign */ + { "divide", 247 }, /* divide sign */ + { "eacute", 233 }, /* small e, acute accent */ + { "ecirc", 234 }, /* small e, circumflex accent */ + { "egrave", 232 }, /* small e, grave accent */ + { "eth", 240 }, /* small eth, Icelandic */ + { "euml", 235 }, /* small e, dieresis or umlaut mark */ + { "frac12", 189 }, /* fraction one-half */ + { "frac14", 188 }, /* fraction one-quarter */ + { "frac34", 190 }, /* fraction three-quarters */ + { "gt", 62 }, /* greater than */ + { "iacute", 237 }, /* small i, acute accent */ + { "icirc", 238 }, /* small i, circumflex accent */ + { "iexcl", 161 }, /* inverted exclamation mark */ + { "igrave", 236 }, /* small i, grave accent */ + { "iquest", 191 }, /* inverted question mark */ + { "iuml", 239 }, /* small i, dieresis or umlaut mark */ + { "laquo", 171 }, /* angle quotation mark, left */ + { "lt", 60 }, /* less than */ + { "macr", 175 }, /* macron */ + { "micro", 181 }, /* micro sign */ + { "middot", 183 }, /* middle dot */ + { "nbsp", 160 }, /* no-break space */ + { "not", 172 }, /* not sign */ + { "ntilde", 241 }, /* small n, tilde */ + { "oacute", 243 }, /* small o, acute accent */ + { "ocirc", 244 }, /* small o, circumflex accent */ + { "ograve", 242 }, /* small o, grave accent */ + { "ordf", 170 }, /* ordinal indicator, feminine */ + { "ordm", 186 }, /* ordinal indicator, masculine */ + { "oslash", 248 }, /* small o, slash */ + { "otilde", 245 }, /* small o, tilde */ + { "ouml", 246 }, /* small o, dieresis or umlaut mark */ + { "para", 182 }, /* pilcrow (paragraph sign) */ + { "plusmn", 177 }, /* plus-or-minus sign */ + { "pound", 163 }, /* pound sterling sign */ + { "quot", 34 }, /* double quote */ + { "raquo", 187 }, /* angle quotation mark, right */ + { "reg", 174 }, /* registered sign */ + { "sect", 167 }, /* section sign */ + { "shy", 173 }, /* soft hyphen */ + { "sup1", 185 }, /* superscript one */ + { "sup2", 178 }, /* superscript two */ + { "sup3", 179 }, /* superscript three */ + { "szlig", 223 }, /* small sharp s, German (sz ligature) */ + { "thorn", 254 }, /* small thorn, Icelandic */ + { "times", 215 }, /* multiply sign */ + { "uacute", 250 }, /* small u, acute accent */ + { "ucirc", 251 }, /* small u, circumflex accent */ + { "ugrave", 249 }, /* small u, grave accent */ + { "uml", 168 }, /* umlaut (dieresis) */ + { "uuml", 252 }, /* small u, dieresis or umlaut mark */ + { "yacute", 253 }, /* small y, acute accent */ + { "yen", 165 }, /* yen sign */ + { "yuml", 255 }, /* small y, dieresis or umlaut mark */ +}; + +/* + * unvis - decode characters previously encoded by vis + */ +int +unvis(char *cp, int c, int *astate, int flag) +{ + unsigned char uc = (unsigned char)c; + unsigned char st, ia, is, lc; + +/* + * Bottom 8 bits of astate hold the state machine state. + * Top 8 bits hold the current character in the http 1866 nv string decoding + */ +#define GS(a) ((a) & 0xff) +#define SS(a, b) (((uint32_t)(a) << 24) | (b)) +#define GI(a) ((uint32_t)(a) >> 24) + + _DIAGASSERT(cp != NULL); + _DIAGASSERT(astate != NULL); + st = GS(*astate); + + if (flag & UNVIS_END) { + switch (st) { + case S_OCTAL2: + case S_OCTAL3: + case S_HEX2: + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case S_GROUND: + return UNVIS_NOCHAR; + default: + return UNVIS_SYNBAD; + } + } + + switch (st) { + + case S_GROUND: + *cp = 0; + if ((flag & VIS_NOESCAPE) == 0 && c == '\\') { + *astate = SS(0, S_START); + return UNVIS_NOCHAR; + } + if ((flag & VIS_HTTP1808) && c == '%') { + *astate = SS(0, S_HEX1); + return UNVIS_NOCHAR; + } + if ((flag & VIS_HTTP1866) && c == '&') { + *astate = SS(0, S_AMP); + return UNVIS_NOCHAR; + } + if ((flag & VIS_MIMESTYLE) && c == '=') { + *astate = SS(0, S_MIME1); + return UNVIS_NOCHAR; + } + *cp = c; + return UNVIS_VALID; + + case S_START: + switch(c) { + case '\\': + *cp = c; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + *cp = (c - '0'); + *astate = SS(0, S_OCTAL2); + return UNVIS_NOCHAR; + case 'M': + *cp = (char)0200; + *astate = SS(0, S_META); + return UNVIS_NOCHAR; + case '^': + *astate = SS(0, S_CTRL); + return UNVIS_NOCHAR; + case 'n': + *cp = '\n'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 'r': + *cp = '\r'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 'b': + *cp = '\b'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 'a': + *cp = '\007'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 'v': + *cp = '\v'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 't': + *cp = '\t'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 'f': + *cp = '\f'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 's': + *cp = ' '; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case 'E': + *cp = '\033'; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + case '\n': + /* + * hidden newline + */ + *astate = SS(0, S_GROUND); + return UNVIS_NOCHAR; + case '$': + /* + * hidden marker + */ + *astate = SS(0, S_GROUND); + return UNVIS_NOCHAR; + } + goto bad; + + case S_META: + if (c == '-') + *astate = SS(0, S_META1); + else if (c == '^') + *astate = SS(0, S_CTRL); + else + goto bad; + return UNVIS_NOCHAR; + + case S_META1: + *astate = SS(0, S_GROUND); + *cp |= c; + return UNVIS_VALID; + + case S_CTRL: + if (c == '?') + *cp |= 0177; + else + *cp |= c & 037; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + + case S_OCTAL2: /* second possible octal digit */ + if (isoctal(uc)) { + /* + * yes - and maybe a third + */ + *cp = (*cp << 3) + (c - '0'); + *astate = SS(0, S_OCTAL3); + return UNVIS_NOCHAR; + } + /* + * no - done with current sequence, push back passed char + */ + *astate = SS(0, S_GROUND); + return UNVIS_VALIDPUSH; + + case S_OCTAL3: /* third possible octal digit */ + *astate = SS(0, S_GROUND); + if (isoctal(uc)) { + *cp = (*cp << 3) + (c - '0'); + return UNVIS_VALID; + } + /* + * we were done, push back passed char + */ + return UNVIS_VALIDPUSH; + + case S_HEX1: + if (isxdigit(uc)) { + *cp = xtod(uc); + *astate = SS(0, S_HEX2); + return UNVIS_NOCHAR; + } + /* + * no - done with current sequence, push back passed char + */ + *astate = SS(0, S_GROUND); + return UNVIS_VALIDPUSH; + + case S_HEX2: + *astate = S_GROUND; + if (isxdigit(uc)) { + *cp = xtod(uc) | (*cp << 4); + return UNVIS_VALID; + } + return UNVIS_VALIDPUSH; + + case S_MIME1: + if (uc == '\n' || uc == '\r') { + *astate = SS(0, S_EATCRNL); + return UNVIS_NOCHAR; + } + if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) { + *cp = XTOD(uc); + *astate = SS(0, S_MIME2); + return UNVIS_NOCHAR; + } + goto bad; + + case S_MIME2: + if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) { + *astate = SS(0, S_GROUND); + *cp = XTOD(uc) | (*cp << 4); + return UNVIS_VALID; + } + goto bad; + + case S_EATCRNL: + switch (uc) { + case '\r': + case '\n': + return UNVIS_NOCHAR; + case '=': + *astate = SS(0, S_MIME1); + return UNVIS_NOCHAR; + default: + *cp = uc; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + } + + case S_AMP: + *cp = 0; + if (uc == '#') { + *astate = SS(0, S_NUMBER); + return UNVIS_NOCHAR; + } + *astate = SS(0, S_STRING); + /*FALLTHROUGH*/ + + case S_STRING: + ia = *cp; /* index in the array */ + is = GI(*astate); /* index in the string */ + lc = is == 0 ? 0 : nv[ia].name[is - 1]; /* last character */ + + if (uc == ';') + uc = '\0'; + + for (; ia < __arraycount(nv); ia++) { + if (is != 0 && nv[ia].name[is - 1] != lc) + goto bad; + if (nv[ia].name[is] == uc) + break; + } + + if (*cp == __arraycount(nv)) + goto bad; + + if (uc != 0) { + *cp = ia; + *astate = SS(is + 1, S_STRING); + return UNVIS_NOCHAR; + } + + *cp = nv[ia].value; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + + case S_NUMBER: + if (uc == ';') + return UNVIS_VALID; + if (!isdigit(uc)) + goto bad; + *cp += (*cp * 10) + uc - '0'; + return UNVIS_NOCHAR; + + default: + bad: + /* + * decoder in unknown state - (probably uninitialized) + */ + *astate = SS(0, S_GROUND); + return UNVIS_SYNBAD; + } +} + +/* + * strunvis - decode src into dst + * + * Number of chars decoded into dst is returned, -1 on error. + * Dst is null terminated. + */ + +int +strunvisx(char *dst, const char *src, int flag) +{ + char c; + char *start = dst; + int state = 0; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + while ((c = *src++) != '\0') { + again: + switch (unvis(dst, c, &state, flag)) { + case UNVIS_VALID: + dst++; + break; + case UNVIS_VALIDPUSH: + dst++; + goto again; + case 0: + case UNVIS_NOCHAR: + break; + default: + return (-1); + } + } + if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) + dst++; + *dst = '\0'; + return (int)(dst - start); +} + +int +strunvis(char *dst, const char *src) +{ + return strunvisx(dst, src, 0); +} +#endif diff --git a/lib/nbsd_libc/gen/usleep.3 b/lib/nbsd_libc/gen/usleep.3 new file mode 100644 index 000000000..12ae2e2d7 --- /dev/null +++ b/lib/nbsd_libc/gen/usleep.3 @@ -0,0 +1,104 @@ +.\" $NetBSD: usleep.3,v 1.19 2010/04/29 17:29:56 jruoho Exp $ +.\" +.\" Copyright (c) 1986, 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. +.\" +.\" @(#)usleep.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd April 29, 2010 +.Dt USLEEP 3 +.Os +.Sh NAME +.Nm usleep +.Nd suspend execution for interval of microseconds +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn usleep "useconds_t microseconds" +.Sh DESCRIPTION +The +.Fn usleep +function +suspends execution of the calling process +until either the number of microseconds specified by +.Fa microseconds +have elapsed or a signal is delivered to the calling process and its +action is to invoke a signal catching function or to terminate the +process. +The suspension time may be longer than requested due to the +scheduling of other activity by the system. +.Pp +The +.Fa microseconds +argument must be less than 1,000,000. +If the value of +.Fa microseconds +is 0, then the call has no effect. +.Sh RETURN VALUES +On successful completion, +.Fn usleep +returns 0. +Otherwise, it returns \-1 and sets +.Va errno +to indicate the error. +.Sh ERRORS +The +.Fn usleep +function may fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Fa microseconds +interval specified 1,000,000 or more microseconds. +.El +.Sh SEE ALSO +.Xr nanosleep 2 , +.Xr sleep 3 +.Sh STANDARDS +The +.Fn usleep +function conforms to +.St -xpg4.2 . +It later appeared in the +.Tn POSIX +standard, but in +.St -p1003.1-2004 +it was marked as legacy and the use of +.Xr nanosleep 2 +was recommended instead. +The +.St -p1003.1-2008 +revision removed +.Fn usleep +from the specification. +.Sh HISTORY +The +.Fn usleep +function appeared in +.Bx 4.3 . diff --git a/lib/nbsd_libc/gen/usleep.c b/lib/nbsd_libc/gen/usleep.c new file mode 100644 index 000000000..8722d5da0 --- /dev/null +++ b/lib/nbsd_libc/gen/usleep.c @@ -0,0 +1,66 @@ +/* $NetBSD: usleep.c,v 1.19 2008/04/28 20:22:59 martin Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: usleep.c,v 1.19 2008/04/28 20:22:59 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(usleep,_usleep) +#endif + +int +usleep(useconds) + useconds_t useconds; +{ + struct timespec ts; + + if (useconds == 0) + return (0); + + if (useconds >= 1000000) { + errno = EINVAL; + return (-1); + } + + ts.tv_sec = 0; + ts.tv_nsec = useconds * 1000; + + nanosleep(&ts, NULL); + + return (0); +} diff --git a/lib/nbsd_libc/gen/utime.3 b/lib/nbsd_libc/gen/utime.3 new file mode 100644 index 000000000..2f2eb6ea2 --- /dev/null +++ b/lib/nbsd_libc/gen/utime.3 @@ -0,0 +1,145 @@ +.\" $NetBSD: utime.3,v 1.21 2010/04/29 17:07:00 jruoho Exp $ +.\" +.\" Copyright (c) 1980, 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. +.\" +.\" @(#)utime.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd April 29, 2010 +.Dt UTIME 3 +.Os +.Sh NAME +.Nm utime +.Nd set file times +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In utime.h +.Ft int +.Fn utime "const char *file" "const struct utimbuf *timep" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by +.Xr utimes 2 . +.Ef +.Pp +The +.Fn utime +function sets the access and modification times of the named file. +.Pp +If +.Fa timep +is +.Dv NULL , +the access and modification times are set to the current time. +The calling process must be the owner of the file or have permission to +write the file. +.Pp +If +.Fa timep +is +.Pf non- Dv NULL , +.Fa time +is assumed to be a pointer to a utimbuf structure, as defined in +.In utime.h : +.Bd -literal -offset indent +struct utimbuf { + time_t actime; /* Access time */ + time_t modtime; /* Modification time */ +}; +.Ed +.Pp +The access time is set to the value of the actime member, and the +modification time is set to the value of the modtime member. +The times are measured in seconds since 0 hours, 0 minutes, 0 +seconds, January 1, 1970 Coordinated Universal Time (UTC). +The calling process must be the owner of the file or be the super-user. +.Pp +In either case, the inode-change-time of the file is set to the current +time. +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. +Otherwise, a value of \-1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn utime +will fail if: +.Bl -tag -width Er +.It Bq Er EACCES +Search permission is denied for a component of the path prefix; +or the +.Fa times +argument is +.Dv NULL +and the effective user ID of the process does not +match the owner of the file, and is not the super-user, and write +access is denied. +.It Bq Er EFAULT +.Fa file +or +.Fa times +points outside the process's allocated address space. +.It Bq Er EINVAL +The pathname contains a character with the high-order bit set. +.It Bq Er EIO +An I/O error occurred while reading or writing the affected inode. +.It Bq Er ELOOP +Too many symbolic links were encountered in translating the pathname. +.It Bq Er ENAMETOOLONG +A component of a pathname exceeded 255 characters, +or an entire path name exceeded 1023 characters. +.It Bq Er ENOENT +The named file does not exist. +.It Bq Er ENOTDIR +A component of the path prefix is not a directory. +.It Bq Er EPERM +The +.Fa times +argument is not +.Dv NULL +and the calling process's effective user ID +does not match the owner of the file and is not the super-user. +.It Bq Er EROFS +The file system containing the file is mounted read-only. +.El +.Sh SEE ALSO +.Xr stat 2 , +.Xr utimes 2 +.Sh STANDARDS +The +.Fn utime +function conforms to +.St -p1003.1-90 . +It was however marked as legacy in the +.St -p1003.1-2008 +revision of the standard. +.Sh HISTORY +A +.Fn utime +function appeared in +.At v7 . diff --git a/lib/nbsd_libc/gen/utime.c b/lib/nbsd_libc/gen/utime.c new file mode 100644 index 000000000..46534b87a --- /dev/null +++ b/lib/nbsd_libc/gen/utime.c @@ -0,0 +1,67 @@ +/* $NetBSD: utime.c,v 1.13 2010/03/23 20:28:59 drochner 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)utime.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: utime.c,v 1.13 2010/03/23 20:28:59 drochner Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include + +int +utime(path, times) + const char *path; + const struct utimbuf *times; +{ + struct timeval tv[2], *tvp; + + _DIAGASSERT(path != NULL); + + if (times == (struct utimbuf *) NULL) + tvp = NULL; + else { + tv[0].tv_sec = times->actime; + tv[1].tv_sec = times->modtime; + tv[0].tv_usec = tv[1].tv_usec = 0; + tvp = tv; + } + return (utimes(path, tvp)); +} diff --git a/lib/nbsd_libc/gen/utmp.c b/lib/nbsd_libc/gen/utmp.c new file mode 100644 index 000000000..b83a3c0c7 --- /dev/null +++ b/lib/nbsd_libc/gen/utmp.c @@ -0,0 +1,106 @@ +/* $NetBSD: utmp.c,v 1.9 2009/02/05 23:52:55 lukem Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: utmp.c,v 1.9 2009/02/05 23:52:55 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +static struct utmp utmp; +static FILE *ut; +static char utfile[MAXPATHLEN] = _PATH_UTMP; + +void +setutent(void) +{ + if (ut == NULL) + return; + (void)fseeko(ut, (off_t)0, SEEK_SET); +} + +struct utmp * +getutent(void) +{ + if (ut == NULL) { + struct stat st; + off_t numentries; + if ((ut = fopen(utfile, "r")) == NULL) + return NULL; + if (fstat(fileno(ut), &st) == -1) + goto out; + /* + * If we have a an old version utmp file bail. + */ + numentries = st.st_size / sizeof(utmp); + if ((off_t)(numentries * sizeof(utmp)) != st.st_size) + goto out; + } + if (fread(&utmp, sizeof(utmp), 1, ut) == 1) + return &utmp; +out: + (void)fclose(ut); + return NULL; +} + +void +endutent(void) +{ + if (ut != NULL) { + (void)fclose(ut); + ut = NULL; + } +} + +int +utmpname(const char *fname) +{ + size_t len = strlen(fname); + + if (len >= sizeof(utfile)) + return 0; + + /* must not end in x! */ + if (fname[len - 1] == 'x') + return 0; + + (void)strlcpy(utfile, fname, sizeof(utfile)); + endutent(); + return 1; +} diff --git a/lib/nbsd_libc/gen/utmpx.c b/lib/nbsd_libc/gen/utmpx.c new file mode 100644 index 000000000..779d56512 --- /dev/null +++ b/lib/nbsd_libc/gen/utmpx.c @@ -0,0 +1,481 @@ +/* $NetBSD: utmpx.c,v 1.26 2009/01/11 02:46:27 christos Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: utmpx.c,v 1.26 2009/01/11 02:46:27 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static FILE *fp; +static int readonly = 0; +static int version = 1; +static struct utmpx ut; +static char utfile[MAXPATHLEN] = _PATH_UTMPX; + +static struct utmpx *utmp_update(const struct utmpx *); + +static const char vers[] = "utmpx-2.00"; + +struct otimeval { + long tv_sec; + long tv_usec; +}; + +static void +old2new(struct utmpx *utx) +{ + struct otimeval otv; + struct timeval *tv = &utx->ut_tv; + (void)memcpy(&otv, tv, sizeof(otv)); + tv->tv_sec = otv.tv_sec; + tv->tv_usec = otv.tv_usec; +} + +static void +new2old(struct utmpx *utx) +{ + struct timeval tv; + struct otimeval *otv = (void *)&utx->ut_tv; + (void)memcpy(&tv, otv, sizeof(tv)); + otv->tv_sec = (long)tv.tv_sec; + otv->tv_usec = (long)tv.tv_usec; +} + +void +setutxent() +{ + + (void)memset(&ut, 0, sizeof(ut)); + if (fp == NULL) + return; + (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET); +} + + +void +endutxent() +{ + + (void)memset(&ut, 0, sizeof(ut)); + if (fp != NULL) { + (void)fclose(fp); + fp = NULL; + readonly = 0; + } +} + + +struct utmpx * +getutxent() +{ + + if (fp == NULL) { + struct stat st; + + if ((fp = fopen(utfile, "r+")) == NULL) + if ((fp = fopen(utfile, "w+")) == NULL) { + if ((fp = fopen(utfile, "r")) == NULL) + goto fail; + else + readonly = 1; + } + + + /* get file size in order to check if new file */ + if (fstat(fileno(fp), &st) == -1) + goto failclose; + + if (st.st_size == 0) { + /* new file, add signature record */ + (void)memset(&ut, 0, sizeof(ut)); + ut.ut_type = SIGNATURE; + (void)memcpy(ut.ut_user, vers, sizeof(vers)); + if (fwrite(&ut, sizeof(ut), 1, fp) != 1) + goto failclose; + } else { + /* old file, read signature record */ + if (fread(&ut, sizeof(ut), 1, fp) != 1) + goto failclose; + if (memcmp(ut.ut_user, vers, 5) != 0 || + ut.ut_type != SIGNATURE) + goto failclose; + } + version = ut.ut_user[6] - '0'; + } + + if (fread(&ut, sizeof(ut), 1, fp) != 1) + goto fail; + if (version == 1) + old2new(&ut); + + return &ut; +failclose: + (void)fclose(fp); +fail: + (void)memset(&ut, 0, sizeof(ut)); + return NULL; +} + + +struct utmpx * +getutxid(const struct utmpx *utx) +{ + + _DIAGASSERT(utx != NULL); + + if (utx->ut_type == EMPTY) + return NULL; + + do { + if (ut.ut_type == EMPTY) + continue; + switch (utx->ut_type) { + case EMPTY: + return NULL; + case RUN_LVL: + case BOOT_TIME: + case OLD_TIME: + case NEW_TIME: + if (ut.ut_type == utx->ut_type) + return &ut; + break; + case INIT_PROCESS: + case LOGIN_PROCESS: + case USER_PROCESS: + case DEAD_PROCESS: + switch (ut.ut_type) { + case INIT_PROCESS: + case LOGIN_PROCESS: + case USER_PROCESS: + case DEAD_PROCESS: + if (memcmp(ut.ut_id, utx->ut_id, + sizeof(ut.ut_id)) == 0) + return &ut; + break; + default: + break; + } + break; + default: + return NULL; + } + } while (getutxent() != NULL); + return NULL; +} + + +struct utmpx * +getutxline(const struct utmpx *utx) +{ + + _DIAGASSERT(utx != NULL); + + do { + switch (ut.ut_type) { + case EMPTY: + break; + case LOGIN_PROCESS: + case USER_PROCESS: + if (strncmp(ut.ut_line, utx->ut_line, + sizeof(ut.ut_line)) == 0) + return &ut; + break; + default: + break; + } + } while (getutxent() != NULL); + return NULL; +} + + +struct utmpx * +pututxline(const struct utmpx *utx) +{ + struct utmpx temp, *u = NULL; + int gotlock = 0; + + _DIAGASSERT(utx != NULL); + + if (utx == NULL) + return NULL; + + if (strcmp(_PATH_UTMPX, utfile) == 0) + if ((fp != NULL && readonly) || (fp == NULL && geteuid() != 0)) + return utmp_update(utx); + + + (void)memcpy(&temp, utx, sizeof(temp)); + + if (fp == NULL) { + (void)getutxent(); + if (fp == NULL || readonly) + return NULL; + } + + if (getutxid(&temp) == NULL) { + setutxent(); + if (getutxid(&temp) == NULL) { + if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1) + return NULL; + gotlock++; + if (fseeko(fp, (off_t)0, SEEK_END) == -1) + goto fail; + } + } + + if (!gotlock) { + /* we are not appending */ + if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1) + return NULL; + } + + if (version == 1) + new2old(&temp); + if (fwrite(&temp, sizeof (temp), 1, fp) != 1) + goto fail; + + if (fflush(fp) == -1) + goto fail; + + u = memcpy(&ut, &temp, sizeof(ut)); +fail: + if (gotlock) { + if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1) + return NULL; + } + return u; +} + + +static struct utmpx * +utmp_update(const struct utmpx *utx) +{ + char buf[sizeof(*utx) * 4 + 1]; + pid_t pid; + int status; + + _DIAGASSERT(utx != NULL); + + (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx), + VIS_WHITE); + switch (pid = fork()) { + case 0: + (void)execl(_PATH_UTMP_UPDATE, + strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL); + _exit(1); + /*NOTREACHED*/ + case -1: + return NULL; + default: + if (waitpid(pid, &status, 0) == -1) + return NULL; + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) + return memcpy(&ut, utx, sizeof(ut)); + return NULL; + } + +} + +/* + * The following are extensions and not part of the X/Open spec. + */ +int +updwtmpx(const char *file, const struct utmpx *utx) +{ + int fd; + int saved_errno; + + _DIAGASSERT(file != NULL); + _DIAGASSERT(utx != NULL); + + fd = open(file, O_WRONLY|O_APPEND|O_SHLOCK); + + if (fd == -1) { + if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1) + return -1; + (void)memset(&ut, 0, sizeof(ut)); + ut.ut_type = SIGNATURE; + (void)memcpy(ut.ut_user, vers, sizeof(vers)); + if (write(fd, &ut, sizeof(ut)) == -1) + goto failed; + } + if (write(fd, utx, sizeof(*utx)) == -1) + goto failed; + if (close(fd) == -1) + return -1; + return 0; + + failed: + saved_errno = errno; + (void) close(fd); + errno = saved_errno; + return -1; +} + + +int +utmpxname(const char *fname) +{ + size_t len; + + _DIAGASSERT(fname != NULL); + + len = strlen(fname); + + if (len >= sizeof(utfile)) + return 0; + + /* must end in x! */ + if (fname[len - 1] != 'x') + return 0; + + (void)strlcpy(utfile, fname, sizeof(utfile)); + endutxent(); + return 1; +} + + +void +getutmp(const struct utmpx *ux, struct utmp *u) +{ + + _DIAGASSERT(ux != NULL); + _DIAGASSERT(u != NULL); + + (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name)); + (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line)); + (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host)); + u->ut_time = ux->ut_tv.tv_sec; +} + +void +getutmpx(const struct utmp *u, struct utmpx *ux) +{ + + _DIAGASSERT(ux != NULL); + _DIAGASSERT(u != NULL); + + (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name)); + (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line)); + (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host)); + ux->ut_tv.tv_sec = u->ut_time; + ux->ut_tv.tv_usec = 0; + (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss)); + ux->ut_pid = 0; + ux->ut_type = USER_PROCESS; + ux->ut_session = 0; + ux->ut_exit.e_termination = 0; + ux->ut_exit.e_exit = 0; +} + +struct lastlogx * +getlastlogx(const char *fname, uid_t uid, struct lastlogx *ll) +{ + DBT key, data; + DB *db; + + _DIAGASSERT(fname != NULL); + _DIAGASSERT(ll != NULL); + + db = dbopen(fname, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL); + + if (db == NULL) + return NULL; + + key.data = &uid; + key.size = sizeof(uid); + + if ((db->get)(db, &key, &data, 0) != 0) + goto error; + + if (data.size != sizeof(*ll)) { + errno = EFTYPE; + goto error; + } + + if (ll == NULL) + if ((ll = malloc(sizeof(*ll))) == NULL) + goto done; + + (void)memcpy(ll, data.data, sizeof(*ll)); + goto done; +error: + ll = NULL; +done: + (db->close)(db); + return ll; +} + +int +updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll) +{ + DBT key, data; + int error = 0; + DB *db; + + _DIAGASSERT(fname != NULL); + _DIAGASSERT(ll != NULL); + + db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0644, DB_HASH, NULL); + + if (db == NULL) + return -1; + + key.data = &uid; + key.size = sizeof(uid); + data.data = ll; + data.size = sizeof(*ll); + if ((db->put)(db, &key, &data, 0) != 0) + error = -1; + + (db->close)(db); + return error; +} diff --git a/lib/nbsd_libc/gen/valloc.3 b/lib/nbsd_libc/gen/valloc.3 new file mode 100644 index 000000000..2c49a0de7 --- /dev/null +++ b/lib/nbsd_libc/gen/valloc.3 @@ -0,0 +1,77 @@ +.\" $NetBSD: valloc.3,v 1.14 2010/05/08 11:22:58 wiz Exp $ +.\" +.\" Copyright (c) 1980, 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. +.\" +.\" @(#)valloc.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd May 6, 2010 +.Dt VALLOC 3 +.Os +.Sh NAME +.Nm valloc +.Nd aligned memory allocation function +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft void * +.Fn valloc "size_t size" +.Sh DESCRIPTION +.Bf -symbolic +The +.Fn valloc +function is obsoleted by the current version of +.Xr malloc 3 , +which aligns page-sized and larger allocations. +.Ef +.Pp +The +.Fn valloc +function +allocates +.Fa size +bytes aligned on a page boundary. +It is implemented by calling +.Xr malloc 3 +with a slightly larger request, saving the true beginning of the block +allocated, and returning a properly aligned pointer. +.Sh RETURN VALUES +The +.Fn valloc +function returns +a pointer to the allocated space if successful; otherwise +a null pointer is returned +.Sh HISTORY +The +.Fn valloc +function appeared in +.Bx 3.0 . +.Sh BUGS +A +.Fn vfree +function has not been implemented. diff --git a/lib/nbsd_libc/gen/valloc.c b/lib/nbsd_libc/gen/valloc.c new file mode 100644 index 000000000..e92b73b5e --- /dev/null +++ b/lib/nbsd_libc/gen/valloc.c @@ -0,0 +1,58 @@ +/* $NetBSD: valloc.c,v 1.10 2003/08/07 16:42:59 agc Exp $ */ + +/* + * Copyright (c) 1980, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)valloc.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: valloc.c,v 1.10 2003/08/07 16:42:59 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(valloc,_valloc) +#endif + +void * +valloc(i) + size_t i; +{ + long valsiz = getpagesize(), j; + void *cp = malloc((size_t)(i + (valsiz-1))); + + j = ((long)cp + (valsiz-1)) &~ (valsiz-1); + return ((void *)j); +} diff --git a/lib/nbsd_libc/gen/verr.c b/lib/nbsd_libc/gen/verr.c new file mode 100644 index 000000000..d78e32ea0 --- /dev/null +++ b/lib/nbsd_libc/gen/verr.c @@ -0,0 +1,72 @@ +/* $NetBSD: verr.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: verr.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(verr, _verr) +#endif + +#if !HAVE_ERR_H +__dead void +verr(int eval, const char *fmt, _BSD_VA_LIST_ ap) +{ + int sverrno; + + sverrno = errno; + (void)fprintf(stderr, "%s: ", getprogname()); + if (fmt != NULL) { + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, ": "); + } + (void)fprintf(stderr, "%s\n", strerror(sverrno)); + exit(eval); +} +#endif diff --git a/lib/nbsd_libc/gen/verrx.c b/lib/nbsd_libc/gen/verrx.c new file mode 100644 index 000000000..951b8629c --- /dev/null +++ b/lib/nbsd_libc/gen/verrx.c @@ -0,0 +1,65 @@ +/* $NetBSD: verrx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: verrx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(verrx, _verrx) +#endif + +#if !HAVE_ERR_H +__dead void +verrx(int eval, const char *fmt, _BSD_VA_LIST_ ap) +{ + (void)fprintf(stderr, "%s: ", getprogname()); + if (fmt != NULL) + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, "\n"); + exit(eval); +} +#endif diff --git a/lib/nbsd_libc/gen/vis.3 b/lib/nbsd_libc/gen/vis.3 new file mode 100644 index 000000000..68c7b84e1 --- /dev/null +++ b/lib/nbsd_libc/gen/vis.3 @@ -0,0 +1,354 @@ +.\" $NetBSD: vis.3,v 1.23 2009/02/10 23:06:31 christos 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. +.\" +.\" @(#)vis.3 8.1 (Berkeley) 6/9/93 +.\" +.Dd February 10, 2009 +.Dt VIS 3 +.Os +.Sh NAME +.Nm vis , +.Nm strvis , +.Nm strvisx , +.Nm svis , +.Nm strsvis , +.Nm strsvisx +.Nd visually encode characters +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In vis.h +.Ft char * +.Fn vis "char *dst" "int c" "int flag" "int nextc" +.Ft int +.Fn strvis "char *dst" "const char *src" "int flag" +.Ft int +.Fn strvisx "char *dst" "const char *src" "size_t len" "int flag" +.Ft char * +.Fn svis "char *dst" "int c" "int flag" "int nextc" "const char *extra" +.Ft int +.Fn strsvis "char *dst" "const char *src" "int flag" "const char *extra" +.Ft int +.Fn strsvisx "char *dst" "const char *src" "size_t len" "int flag" "const char *extra" +.Sh DESCRIPTION +The +.Fn vis +function +copies into +.Fa dst +a string which represents the character +.Fa c . +If +.Fa c +needs no encoding, it is copied in unaltered. +The string is null terminated, and a pointer to the end of the string is +returned. +The maximum length of any encoding is four +characters (not including the trailing +.Dv NUL ) ; +thus, when +encoding a set of characters into a buffer, the size of the buffer should +be four times the number of characters encoded, plus one for the trailing +.Dv NUL . +The flag parameter is used for altering the default range of +characters considered for encoding and for altering the visual +representation. +The additional character, +.Fa nextc , +is only used when selecting the +.Dv VIS_CSTYLE +encoding format (explained below). +.Pp +The +.Fn strvis +and +.Fn strvisx +functions copy into +.Fa dst +a visual representation of +the string +.Fa src . +The +.Fn strvis +function encodes characters from +.Fa src +up to the +first +.Dv NUL . +The +.Fn strvisx +function encodes exactly +.Fa len +characters from +.Fa src +(this +is useful for encoding a block of data that may contain +.Dv NUL Ns 's ) . +Both forms +.Dv NUL +terminate +.Fa dst . +The size of +.Fa dst +must be four times the number +of characters encoded from +.Fa src +(plus one for the +.Dv NUL ) . +Both +forms return the number of characters in dst (not including +the trailing +.Dv NUL ) . +.Pp +The functions +.Fn svis , +.Fn strsvis , +and +.Fn strsvisx +correspond to +.Fn vis , +.Fn strvis , +and +.Fn strvisx +but have an additional argument +.Fa extra , +pointing to a +.Dv NUL +terminated list of characters. +These characters will be copied encoded or backslash-escaped into +.Fa dst . +These functions are useful e.g. to remove the special meaning +of certain characters to shells. +.Pp +The encoding is a unique, invertible representation composed entirely of +graphic characters; it can be decoded back into the original form using +the +.Xr unvis 3 +or +.Xr strunvis 3 +functions. +.Pp +There are two parameters that can be controlled: the range of +characters that are encoded (applies only to +.Fn vis , +.Fn strvis , +and +.Fn strvisx ) , +and the type of representation used. +By default, all non-graphic characters, +except space, tab, and newline are encoded. +(See +.Xr isgraph 3 . ) +The following flags +alter this: +.Bl -tag -width VIS_WHITEX +.It Dv VIS_SP +Also encode space. +.It Dv VIS_TAB +Also encode tab. +.It Dv VIS_NL +Also encode newline. +.It Dv VIS_WHITE +Synonym for +.Dv VIS_SP +\&| +.Dv VIS_TAB +\&| +.Dv VIS_NL . +.It Dv VIS_SAFE +Only encode "unsafe" characters. +Unsafe means control characters which may cause common terminals to perform +unexpected functions. +Currently this form allows space, tab, newline, backspace, bell, and +return - in addition to all graphic characters - unencoded. +.El +.Pp +(The above flags have no effect for +.Fn svis , +.Fn strsvis , +and +.Fn strsvisx . +When using these functions, place all graphic characters to be +encoded in an array pointed to by +.Fa extra . +In general, the backslash character should be included in this array, see the +warning on the use of the +.Dv VIS_NOSLASH +flag below). +.Pp +There are four forms of encoding. +All forms use the backslash character +.Ql \e +to introduce a special +sequence; two backslashes are used to represent a real backslash, +except +.Dv VIS_HTTPSTYLE +that uses +.Ql % , +or +.Dv VIS_MIMESTYLE +that uses +.Ql = . +These are the visual formats: +.Bl -tag -width VIS_CSTYLE +.It (default) +Use an +.Ql M +to represent meta characters (characters with the 8th +bit set), and use caret +.Ql ^ +to represent control characters see +.Pf ( Xr iscntrl 3 ) . +The following formats are used: +.Bl -tag -width xxxxx +.It Dv \e^C +Represents the control character +.Ql C . +Spans characters +.Ql \e000 +through +.Ql \e037 , +and +.Ql \e177 +(as +.Ql \e^? ) . +.It Dv \eM-C +Represents character +.Ql C +with the 8th bit set. +Spans characters +.Ql \e241 +through +.Ql \e376 . +.It Dv \eM^C +Represents control character +.Ql C +with the 8th bit set. +Spans characters +.Ql \e200 +through +.Ql \e237 , +and +.Ql \e377 +(as +.Ql \eM^? ) . +.It Dv \e040 +Represents +.Tn ASCII +space. +.It Dv \e240 +Represents Meta-space. +.El +.Pp +.It Dv VIS_CSTYLE +Use C-style backslash sequences to represent standard non-printable +characters. +The following sequences are used to represent the indicated characters: +.Bd -unfilled -offset indent +.Li \ea Tn - BEL No (007) +.Li \eb Tn - BS No (010) +.Li \ef Tn - NP No (014) +.Li \en Tn - NL No (012) +.Li \er Tn - CR No (015) +.Li \es Tn - SP No (040) +.Li \et Tn - HT No (011) +.Li \ev Tn - VT No (013) +.Li \e0 Tn - NUL No (000) +.Ed +.Pp +When using this format, the nextc parameter is looked at to determine +if a +.Dv NUL +character can be encoded as +.Ql \e0 +instead of +.Ql \e000 . +If +.Fa nextc +is an octal digit, the latter representation is used to +avoid ambiguity. +.It Dv VIS_OCTAL +Use a three digit octal sequence. +The form is +.Ql \eddd +where +.Em d +represents an octal digit. +.It Dv VIS_HTTPSTYLE +Use URI encoding as described in RFC 1738. +The form is +.Ql %xx +where +.Em x +represents a lower case hexadecimal digit. +.It Dv VIS_MIMESTYLE +Use MIME Quoted-Printable encoding as described in RFC 2045, only don't +break lines and don't handle CRLF. +The form is: +.Ql %XX +where +.Em X +represents an upper case hexadecimal digit. +.El +.Pp +There is one additional flag, +.Dv VIS_NOSLASH , +which inhibits the +doubling of backslashes and the backslash before the default +format (that is, control characters are represented by +.Ql ^C +and +meta characters as +.Ql M-C ) . +With this flag set, the encoding is +ambiguous and non-invertible. +.Sh SEE ALSO +.Xr unvis 1 , +.Xr vis 1 , +.Xr unvis 3 +.Rs +.%A T. Berners-Lee +.%T Uniform Resource Locators (URL) +.%O RFC1738 +.Re +.Sh HISTORY +The +.Fa vis , +.Fa strvis , +and +.Fa strvisx +functions first appeared in +.Bx 4.4 . +The +.Fa svis , +.Fa strsvis , +and +.Fa strsvisx +functions appeared in +.Nx 1.5 . diff --git a/lib/nbsd_libc/gen/vis.c b/lib/nbsd_libc/gen/vis.c new file mode 100644 index 000000000..477309623 --- /dev/null +++ b/lib/nbsd_libc/gen/vis.c @@ -0,0 +1,423 @@ +/* $NetBSD: vis.c,v 1.41 2009/11/23 10:08:47 plunky Exp $ */ + +/*- + * Copyright (c) 1989, 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. + */ + +/*- + * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc. + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: vis.c,v 1.41 2009/11/23 10:08:47 plunky Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(strsvis,_strsvis) +__weak_alias(strsvisx,_strsvisx) +__weak_alias(strvis,_strvis) +__weak_alias(strvisx,_strvisx) +__weak_alias(svis,_svis) +__weak_alias(vis,_vis) +#endif + +#if !HAVE_VIS || !HAVE_SVIS +#include +#include +#include +#include + +static char *do_svis(char *, int, int, int, const char *); + +#undef BELL +#define BELL '\a' + +#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') +#define iswhite(c) (c == ' ' || c == '\t' || c == '\n') +#define issafe(c) (c == '\b' || c == BELL || c == '\r') +#define xtoa(c) "0123456789abcdef"[c] +#define XTOA(c) "0123456789ABCDEF"[c] + +#define MAXEXTRAS 5 + +#define MAKEEXTRALIST(flag, extra, orig_str) \ +do { \ + const char *orig = orig_str; \ + const char *o = orig; \ + char *e; \ + while (*o++) \ + continue; \ + extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \ + if (!extra) break; \ + for (o = orig, e = extra; (*e++ = *o++) != '\0';) \ + continue; \ + e--; \ + if (flag & VIS_SP) *e++ = ' '; \ + if (flag & VIS_TAB) *e++ = '\t'; \ + if (flag & VIS_NL) *e++ = '\n'; \ + if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \ + *e = '\0'; \ +} while (/*CONSTCOND*/0) + +/* + * This is do_hvis, for HTTP style (RFC 1808) + */ +static char * +do_hvis(char *dst, int c, int flag, int nextc, const char *extra) +{ + + if ((isascii(c) && isalnum(c)) + /* safe */ + || c == '$' || c == '-' || c == '_' || c == '.' || c == '+' + /* extra */ + || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')' + || c == ',') { + dst = do_svis(dst, c, flag, nextc, extra); + } else { + *dst++ = '%'; + *dst++ = xtoa(((unsigned int)c >> 4) & 0xf); + *dst++ = xtoa((unsigned int)c & 0xf); + } + + return dst; +} + +/* + * This is do_mvis, for Quoted-Printable MIME (RFC 2045) + * NB: No handling of long lines or CRLF. + */ +static char * +do_mvis(char *dst, int c, int flag, int nextc, const char *extra) +{ + if ((c != '\n') && + /* Space at the end of the line */ + ((isspace(c) && (nextc == '\r' || nextc == '\n')) || + /* Out of range */ + (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) || + /* Specific char to be escaped */ + strchr("#$@[\\]^`{|}~", c) != NULL)) { + *dst++ = '='; + *dst++ = XTOA(((unsigned int)c >> 4) & 0xf); + *dst++ = XTOA((unsigned int)c & 0xf); + } else { + dst = do_svis(dst, c, flag, nextc, extra); + } + return dst; +} + +/* + * This is do_vis, the central code of vis. + * dst: Pointer to the destination buffer + * c: Character to encode + * flag: Flag word + * nextc: The character following 'c' + * extra: Pointer to the list of extra characters to be + * backslash-protected. + */ +static char * +do_svis(char *dst, int c, int flag, int nextc, const char *extra) +{ + int isextra; + isextra = strchr(extra, c) != NULL; + if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) || + ((flag & VIS_SAFE) && issafe(c)))) { + *dst++ = c; + return dst; + } + if (flag & VIS_CSTYLE) { + switch (c) { + case '\n': + *dst++ = '\\'; *dst++ = 'n'; + return dst; + case '\r': + *dst++ = '\\'; *dst++ = 'r'; + return dst; + case '\b': + *dst++ = '\\'; *dst++ = 'b'; + return dst; + case BELL: + *dst++ = '\\'; *dst++ = 'a'; + return dst; + case '\v': + *dst++ = '\\'; *dst++ = 'v'; + return dst; + case '\t': + *dst++ = '\\'; *dst++ = 't'; + return dst; + case '\f': + *dst++ = '\\'; *dst++ = 'f'; + return dst; + case ' ': + *dst++ = '\\'; *dst++ = 's'; + return dst; + case '\0': + *dst++ = '\\'; *dst++ = '0'; + if (isoctal(nextc)) { + *dst++ = '0'; + *dst++ = '0'; + } + return dst; + default: + if (isgraph(c)) { + *dst++ = '\\'; *dst++ = c; + return dst; + } + } + } + if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { + *dst++ = '\\'; + *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; + *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; + *dst++ = (c & 07) + '0'; + } else { + if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; + if (c & 0200) { + c &= 0177; *dst++ = 'M'; + } + if (iscntrl(c)) { + *dst++ = '^'; + if (c == 0177) + *dst++ = '?'; + else + *dst++ = c + '@'; + } else { + *dst++ = '-'; *dst++ = c; + } + } + return dst; +} + +typedef char *(*visfun_t)(char *, int, int, int, const char *); + +/* + * Return the appropriate encoding function depending on the flags given. + */ +static visfun_t +getvisfun(int flag) +{ + if (flag & VIS_HTTPSTYLE) + return do_hvis; + if (flag & VIS_MIMESTYLE) + return do_mvis; + return do_svis; +} + +/* + * svis - visually encode characters, also encoding the characters + * pointed to by `extra' + */ +char * +svis(char *dst, int c, int flag, int nextc, const char *extra) +{ + char *nextra = NULL; + visfun_t f; + + _DIAGASSERT(dst != NULL); + _DIAGASSERT(extra != NULL); + MAKEEXTRALIST(flag, nextra, extra); + if (!nextra) { + *dst = '\0'; /* can't create nextra, return "" */ + return dst; + } + f = getvisfun(flag); + dst = (*f)(dst, c, flag, nextc, nextra); + free(nextra); + *dst = '\0'; + return dst; +} + + +/* + * strsvis, strsvisx - visually encode characters from src into dst + * + * Extra is a pointer to a \0-terminated list of characters to + * be encoded, too. These functions are useful e. g. to + * encode strings in such a way so that they are not interpreted + * by a shell. + * + * Dst must be 4 times the size of src to account for possible + * expansion. The length of dst, not including the trailing NULL, + * is returned. + * + * Strsvisx encodes exactly len bytes from src into dst. + * This is useful for encoding a block of data. + */ +int +strsvis(char *dst, const char *csrc, int flag, const char *extra) +{ + int c; + char *start; + char *nextra = NULL; + const unsigned char *src = (const unsigned char *)csrc; + visfun_t f; + + _DIAGASSERT(dst != NULL); + _DIAGASSERT(src != NULL); + _DIAGASSERT(extra != NULL); + MAKEEXTRALIST(flag, nextra, extra); + if (!nextra) { + *dst = '\0'; /* can't create nextra, return "" */ + return 0; + } + f = getvisfun(flag); + for (start = dst; (c = *src++) != '\0'; /* empty */) + dst = (*f)(dst, c, flag, *src, nextra); + free(nextra); + *dst = '\0'; + return (int)(dst - start); +} + + +int +strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra) +{ + unsigned char c; + char *start; + char *nextra = NULL; + const unsigned char *src = (const unsigned char *)csrc; + visfun_t f; + + _DIAGASSERT(dst != NULL); + _DIAGASSERT(src != NULL); + _DIAGASSERT(extra != NULL); + MAKEEXTRALIST(flag, nextra, extra); + if (! nextra) { + *dst = '\0'; /* can't create nextra, return "" */ + return 0; + } + + f = getvisfun(flag); + for (start = dst; len > 0; len--) { + c = *src++; + dst = (*f)(dst, c, flag, len > 1 ? *src : '\0', nextra); + } + free(nextra); + *dst = '\0'; + return (int)(dst - start); +} +#endif + +#if !HAVE_VIS +/* + * vis - visually encode characters + */ +char * +vis(char *dst, int c, int flag, int nextc) +{ + char *extra = NULL; + unsigned char uc = (unsigned char)c; + visfun_t f; + + _DIAGASSERT(dst != NULL); + + MAKEEXTRALIST(flag, extra, ""); + if (! extra) { + *dst = '\0'; /* can't create extra, return "" */ + return dst; + } + f = getvisfun(flag); + dst = (*f)(dst, uc, flag, nextc, extra); + free(extra); + *dst = '\0'; + return dst; +} + + +/* + * strvis, strvisx - visually encode characters from src into dst + * + * Dst must be 4 times the size of src to account for possible + * expansion. The length of dst, not including the trailing NULL, + * is returned. + * + * Strvisx encodes exactly len bytes from src into dst. + * This is useful for encoding a block of data. + */ +int +strvis(char *dst, const char *src, int flag) +{ + char *extra = NULL; + int rv; + + MAKEEXTRALIST(flag, extra, ""); + if (!extra) { + *dst = '\0'; /* can't create extra, return "" */ + return 0; + } + rv = strsvis(dst, src, flag, extra); + free(extra); + return rv; +} + + +int +strvisx(char *dst, const char *src, size_t len, int flag) +{ + char *extra = NULL; + int rv; + + MAKEEXTRALIST(flag, extra, ""); + if (!extra) { + *dst = '\0'; /* can't create extra, return "" */ + return 0; + } + rv = strsvisx(dst, src, len, flag, extra); + free(extra); + return rv; +} +#endif diff --git a/lib/nbsd_libc/gen/vwarn.c b/lib/nbsd_libc/gen/vwarn.c new file mode 100644 index 000000000..802efdc62 --- /dev/null +++ b/lib/nbsd_libc/gen/vwarn.c @@ -0,0 +1,71 @@ +/* $NetBSD: vwarn.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: vwarn.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(vwarn, _vwarn) +#endif + +#if !HAVE_ERR_H +void +vwarn(const char *fmt, _BSD_VA_LIST_ ap) +{ + int sverrno; + + sverrno = errno; + (void)fprintf(stderr, "%s: ", getprogname()); + if (fmt != NULL) { + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, ": "); + } + (void)fprintf(stderr, "%s\n", strerror(sverrno)); +} +#endif diff --git a/lib/nbsd_libc/gen/vwarnx.c b/lib/nbsd_libc/gen/vwarnx.c new file mode 100644 index 000000000..60f849d9a --- /dev/null +++ b/lib/nbsd_libc/gen/vwarnx.c @@ -0,0 +1,64 @@ +/* $NetBSD: vwarnx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: vwarnx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(vwarnx, _vwarnx) +#endif + +#if !HAVE_ERR_H +void +vwarnx(const char *fmt, _BSD_VA_LIST_ ap) +{ + (void)fprintf(stderr, "%s: ", getprogname()); + if (fmt != NULL) + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, "\n"); +} +#endif diff --git a/lib/nbsd_libc/gen/wait.c b/lib/nbsd_libc/gen/wait.c new file mode 100644 index 000000000..2877c4de6 --- /dev/null +++ b/lib/nbsd_libc/gen/wait.c @@ -0,0 +1,57 @@ +/* $NetBSD: wait.c,v 1.8 2003/08/07 16:43:00 agc Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)wait.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: wait.c,v 1.8 2003/08/07 16:43:00 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(wait,_wait) +#endif + + +pid_t +wait(istat) + int *istat; +{ + return (wait4(WAIT_ANY, istat, 0, (struct rusage *)0)); +} diff --git a/lib/nbsd_libc/gen/wait3.c b/lib/nbsd_libc/gen/wait3.c new file mode 100644 index 000000000..3152552aa --- /dev/null +++ b/lib/nbsd_libc/gen/wait3.c @@ -0,0 +1,54 @@ +/* $NetBSD: wait3.c,v 1.9 2010/03/23 20:28:59 drochner Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)wait3.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: wait3.c,v 1.9 2010/03/23 20:28:59 drochner Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +pid_t +wait3(istat, options, rup) + int *istat; + int options; + struct rusage *rup; +{ + return (wait4(WAIT_ANY, istat, options, rup)); +} diff --git a/lib/nbsd_libc/gen/waitpid.c b/lib/nbsd_libc/gen/waitpid.c new file mode 100644 index 000000000..46a831793 --- /dev/null +++ b/lib/nbsd_libc/gen/waitpid.c @@ -0,0 +1,55 @@ +/* $NetBSD: waitpid.c,v 1.9 2003/08/07 16:43:01 agc Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)waitpid.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: waitpid.c,v 1.9 2003/08/07 16:43:01 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(waitpid,_waitpid) +#endif + +pid_t +waitpid(pid_t pid, int *istat, int options) +{ + return (wait4(pid, istat, options, (struct rusage *)0)); +} diff --git a/lib/nbsd_libc/gen/warn.c b/lib/nbsd_libc/gen/warn.c new file mode 100644 index 000000000..2f784662e --- /dev/null +++ b/lib/nbsd_libc/gen/warn.c @@ -0,0 +1,63 @@ +/* $NetBSD: warn.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: warn.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(warn, _warn) +#endif + +#if !HAVE_ERR_H +void +warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); +} +#endif diff --git a/lib/nbsd_libc/gen/warnx.c b/lib/nbsd_libc/gen/warnx.c new file mode 100644 index 000000000..e703c5332 --- /dev/null +++ b/lib/nbsd_libc/gen/warnx.c @@ -0,0 +1,63 @@ +/* $NetBSD: warnx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $ */ + +/*- + * Copyright (c) 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: warnx.c,v 1.14 2007/06/18 14:13:54 ginsbach Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(warnx, _warnx) +#endif + +#if !HAVE_ERR_H +void +warnx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} +#endif diff --git a/lib/nbsd_libc/gen/wordexp.3 b/lib/nbsd_libc/gen/wordexp.3 new file mode 100644 index 000000000..62eae0a97 --- /dev/null +++ b/lib/nbsd_libc/gen/wordexp.3 @@ -0,0 +1,226 @@ +.\" $NetBSD: wordexp.3,v 1.2 2006/04/24 20:27:34 jld Exp $ +.\" +.\" Copyright (c) 2002 Tim J. Robbins +.\" 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 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. +.\" +.\" $FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/wordexp.3,v 1.6 2003/09/08 19:57:14 ru Exp $ +.\" +.Dd July 13, 2004 +.Dt WORDEXP 3 +.Os +.Sh NAME +.Nm wordexp +.Nd "perform shell-style word expansions" +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wordexp.h +.Ft int +.Fn wordexp "const char * restrict words" "wordexp_t * restrict pwordexp" "int flags" +.Ft void +.Fn wordfree "wordexp_t *pwordexp" +.Sh DESCRIPTION +The +.Fn wordexp +function performs shell-style word expansion on +.Fa words +and places the list of expanded words into the structure pointed to by +.Fa pwordexp . +.Pp +The +.Fa flags +argument is the bitwise inclusive OR of any of the following constants: +.Bl -tag -width ".Dv WRDE_SHOWERR" +.It Dv WRDE_APPEND +Append the words to those generated by a previous call to +.Fn wordexp . +.It Dv WRDE_DOOFFS +As many +.Dv NULL +pointers as are specified by the +.Va we_offs +member of +.Fa we +are added to the front of +.Va we_wordv . +.It Dv WRDE_NOCMD +Disallow command substitution in +.Fa words . +See the note in +.Sx BUGS +before using this. +.It Dv WRDE_REUSE +The +.Fa we +argument was passed to a previous successful call to +.Fn wordexp +but has not been passed to +.Fn wordfree . +The implementation may reuse the space allocated to it. +.It Dv WRDE_SHOWERR +Do not redirect shell error messages to +.Pa /dev/null . +.It Dv WRDE_UNDEF +Report error on an attempt to expand an undefined shell variable. +.El +.Pp +The structure type +.Nm wordexp_t +includes the following members: +.Bd -literal -offset indent +size_t we_wordc +char **we_wordv +size_t we_offs +.Ed +.Pp +The +.Fa we_wordc +member is the count of generated words. +.Pp +The +.Fa we_wordv +member points to a list of pointers to expanded words. +.Pp +The +.Fa we_offs +member is the number of slots to reserve at the beginning of the +.Fa we_wordv +member. +.Pp +It is the caller's responsibility to allocate the storage pointed to by +.Fa pwordexp . +The +.Fn wordexp +function allocates other space as needed, including memory +pointed to by the +.Fa we_wordv +member. +.Pp +The +.Fn wordfree +function frees the memory allocated by +.Fn wordexp . +.Sh IMPLEMENTATION NOTES +The +.Fn wordexp +function is implemented as a wrapper around the undocumented +.Ic wordexp +shell built-in command. +.Sh RETURN VALUES +The +.Fn wordexp +function returns zero if successful, otherwise it returns one of the following +error codes: +.Bl -tag -width ".Dv WRDE_NOSPACE" +.It Dv WRDE_BADCHAR +The +.Fa words +argument contains one of the following unquoted characters: +.Aq newline , +.Ql | , +.Ql \*[Am] , +.Ql \&; , +.Ql \*[Lt] , +.Ql \*[Gt] , +.Ql \&( , +.Ql \&) , +.Ql { , +.Ql } . +.It Dv WRDE_BADVAL +An attempt was made to expand an undefined shell variable and +.Dv WRDE_UNDEF +is set in +.Fa flags . +.It Dv WRDE_CMDSUB +An attempt was made to use command substitution and +.Dv WRDE_NOCMD +is set in +.Fa flags . +.It Dv WRDE_NOSPACE +Not enough memory to store the result. +.It Dv WRDE_SYNTAX +Shell syntax error in +.Fa words . +.It Dv WRDE_ERRNO +An internal error occured and +.Va errno +is set to indicate the error. +.El +.Pp +The +.Fn wordfree +function returns no value. +.Sh ENVIRONMENT +.Bl -tag -width ".Ev IFS" +.It Ev IFS +Field separator. +.El +.Sh EXAMPLES +Invoke the editor on all +.Pa .c +files in the current directory +and +.Pa /etc/motd +(error checking omitted): +.Bd -literal -offset indent +wordexp_t we; + +wordexp("${EDITOR:-vi} *.c /etc/motd", \*[Am]we, 0); +execvp(we-\*[Gt]we_wordv[0], we-\*[Gt]we_wordv); +.Ed +.Sh DIAGNOSTICS +Diagnostic messages from the shell are written to the standard error output +if +.Dv WRDE_SHOWERR +is set in +.Fa flags . +.Sh SEE ALSO +.Xr sh 1 , +.Xr fnmatch 3 , +.Xr glob 3 , +.Xr popen 3 , +.Xr system 3 +.Sh STANDARDS +The +.Fn wordexp +and +.Fn wordfree +functions conform to +.St -p1003.1-2001 . +Their first release was in +.St -p1003.2-92 . +The return value +.Dv WRDE_ERRNO +is an extension. +.Sh BUGS +Do not pass untrusted user data to +.Fn wordexp , +regardless of whether the +.Dv WRDE_NOCMD +flag is set. +The +.Fn wordexp +function attempts to detect input that would cause commands to be +executed before passing it to the shell +but it does not use the same parser so it may be fooled. diff --git a/lib/nbsd_libc/gen/wordexp.c b/lib/nbsd_libc/gen/wordexp.c new file mode 100644 index 000000000..a05db38eb --- /dev/null +++ b/lib/nbsd_libc/gen/wordexp.c @@ -0,0 +1,346 @@ +/* $NetBSD: wordexp.c,v 1.3 2009/02/12 04:10:52 lukem Exp $ */ + +/*- + * Copyright (c) 2002 Tim J. Robbins. + * 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 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 "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +__FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/wordexp.c,v 1.5 2004/04/09 11:32:32 tjr Exp $"); +#else +__RCSID("$NetBSD: wordexp.c,v 1.3 2009/02/12 04:10:52 lukem Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +static int we_askshell(const char *, wordexp_t *, int); +static int we_check(const char *, int); + +/* + * wordexp -- + * Perform shell word expansion on `words' and place the resulting list + * of words in `we'. See wordexp(3). + * + */ +int +wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags) +{ + int error; + + _DIAGASSERT(we != NULL); + _DIAGASSERT(words != NULL); + if (flags & WRDE_REUSE) + wordfree(we); + if ((flags & WRDE_APPEND) == 0) { + we->we_wordc = 0; + we->we_wordv = NULL; + we->we_strings = NULL; + we->we_nbytes = 0; + } + if ((error = we_check(words, flags)) != 0) { + wordfree(we); + return (error); + } + if ((error = we_askshell(words, we, flags)) != 0) { + wordfree(we); + return (error); + } + return (0); +} + +/* + * we_askshell -- + * Use the `wordexp' /bin/sh builtin function to do most of the work + * in expanding the word string. This function is complicated by + * memory management. + */ +static int +we_askshell(const char *words, wordexp_t *we, int flags) +{ + int pdes[2]; /* Pipe to child */ + size_t nwords, nbytes; /* Number of words, bytes from child */ + int i; /* Handy integer */ + unsigned int ui; /* For array iteration */ + size_t sofs; /* Offset into we->we_strings */ + size_t vofs; /* Offset into we->we_wordv */ + pid_t pid; /* Process ID of child */ + int status; /* Child exit status */ + const char *ifs; /* IFS env. var. */ + char *np, *p; /* Handy pointers */ + char *nstrings; /* Temporary for realloc() */ + char **nwv; /* Temporary for realloc() */ + FILE *fp; /* Stream to read pipe */ + extern char **environ; + char *cmd; + + if ((ifs = getenv("IFS")) == NULL) + ifs = " \t\n"; + if (asprintf(&cmd, "wordexp%c%s\n", *ifs, words) < 0) + return (WRDE_NOSPACE); + if (pipe(pdes) < 0) { + free(cmd); + return (WRDE_ERRNO); + } + if ((fp = fdopen(pdes[0], "r")) == NULL) { + free(cmd); + return (WRDE_ERRNO); + } + if ((pid = fork()) < 0) { + free(cmd); + fclose(fp); + close(pdes[1]); + return (WRDE_ERRNO); + } + else if (pid == 0) { + /* + * We are the child; just get /bin/sh to run the wordexp + * builtin on `words'. + */ + int devnull; + + close(pdes[0]); + if (pdes[1] != STDOUT_FILENO) { + if (dup2(pdes[1], STDOUT_FILENO) < 0) + _exit(1); + close(pdes[1]); + } + if ((flags & WRDE_SHOWERR) == 0) { + if ((devnull = open(_PATH_DEVNULL, O_RDWR, 0666)) < 0) + _exit(1); + if (dup2(devnull, STDERR_FILENO) < 0) + _exit(1); + close(devnull); + } + execle(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u", + "-c", cmd, (char *)NULL, environ); + _exit(1); + } + + /* + * We are the parent; read the output of the shell wordexp function, + * which is a decimal word count, an null, a decimal byte count, + * (not including terminating null bytes), a null and then followed + * by the expanded words separated by nulls. + */ + free(cmd); + close(pdes[1]); + /* read the word count */ + nwords = 0; + while ((i = getc(fp)) != EOF) { + if (i == '\0') + break; + nwords *= 10; + nwords += (i - '0'); + } + /* read the byte count */ + nbytes = 0; + while ((i = getc(fp)) != EOF) { + if (i == '\0') + break; + nbytes *= 10; + nbytes += (i - '0'); + } + if (i == EOF) { + fclose(fp); + waitpid(pid, &status, 0); + return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); + } + nbytes += nwords; + + /* + * Allocate or reallocate (when flags & WRDE_APPEND) the word vector + * and string storage buffers for the expanded words we're about to + * read from the child. + */ + sofs = we->we_nbytes; + vofs = we->we_wordc; + if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND)) + vofs += we->we_offs; + we->we_wordc += nwords; + we->we_nbytes += nbytes; + if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 + + (flags & WRDE_DOOFFS ? we->we_offs : 0)) * + sizeof(char *))) == NULL) { + fclose(fp); + waitpid(pid, &status, 0); + return (WRDE_NOSPACE); + } + we->we_wordv = nwv; + if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) { + fclose(fp); + waitpid(pid, &status, 0); + return (WRDE_NOSPACE); + } + for (ui = 0; ui < vofs; ui++) + if (we->we_wordv[ui] != NULL) + we->we_wordv[ui] += nstrings - we->we_strings; + we->we_strings = nstrings; + + if (fread(we->we_strings + sofs, sizeof(char), nbytes, fp) != nbytes) { + fclose(fp); + waitpid(pid, &status, 0); + return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); + } + + if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || + WEXITSTATUS(status) != 0) { + fclose(fp); + return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); + } + fclose(fp); + + /* + * Break the null-terminated expanded word strings out into + * the vector. + */ + if (vofs == 0 && flags & WRDE_DOOFFS) + while (vofs < we->we_offs) + we->we_wordv[vofs++] = NULL; + p = we->we_strings + sofs; + while (nwords-- != 0) { + we->we_wordv[vofs++] = p; + if ((np = memchr(p, '\0', nbytes)) == NULL) + return (WRDE_NOSPACE); /* XXX */ + nbytes -= np - p + 1; + p = np + 1; + } + we->we_wordv[vofs] = NULL; + + return (0); +} + +/* + * we_check -- + * Check that the string contains none of the following unquoted + * special characters: |&;<>(){} + * or command substitutions when WRDE_NOCMD is set in flags. + */ +static int +we_check(const char *words, int flags) +{ + char c; + int dquote, level, quote, squote; + + quote = squote = dquote = 0; + while ((c = *words++) != '\0') { + switch (c) { + case '\\': + quote ^= 1; + continue; + case '\'': + if (quote + dquote == 0) + squote ^= 1; + break; + case '"': + if (quote + squote == 0) + dquote ^= 1; + break; + case '`': + if (quote + squote == 0 && flags & WRDE_NOCMD) + return (WRDE_CMDSUB); + while ((c = *words++) != '\0' && c != '`') + if (c == '\\' && (c = *words++) == '\0') + break; + if (c == '\0') + return (WRDE_SYNTAX); + break; + case '|': case '&': case ';': case '<': case '>': + case '{': case '}': case '(': case ')': case '\n': + if (quote + squote + dquote == 0) + return (WRDE_BADCHAR); + break; + case '$': + if ((c = *words++) == '\0') + break; + else if (quote + squote == 0 && c == '(') { + if (flags & WRDE_NOCMD && *words != '(') + return (WRDE_CMDSUB); + level = 1; + while ((c = *words++) != '\0') { + if (c == '\\') { + if ((c = *words++) == '\0') + break; + } else if (c == '(') + level++; + else if (c == ')' && --level == 0) + break; + } + if (c == '\0' || level != 0) + return (WRDE_SYNTAX); + } else if (quote + squote == 0 && c == '{') { + level = 1; + while ((c = *words++) != '\0') { + if (c == '\\') { + if ((c = *words++) == '\0') + break; + } else if (c == '{') + level++; + else if (c == '}' && --level == 0) + break; + } + if (c == '\0' || level != 0) + return (WRDE_SYNTAX); + } else + c = *--words; + break; + default: + break; + } + quote = 0; + } + if (quote + squote + dquote != 0) + return (WRDE_SYNTAX); + + return (0); +} + +/* + * wordfree -- + * Free the result of wordexp(). See wordexp(3). + * + */ +void +wordfree(wordexp_t *we) +{ + _DIAGASSERT(we != NULL); + free(we->we_wordv); + free(we->we_strings); + we->we_wordv = NULL; + we->we_strings = NULL; + we->we_nbytes = 0; + we->we_wordc = 0; +} diff --git a/lib/nbsd_libc/gmon/Makefile.inc b/lib/nbsd_libc/gmon/Makefile.inc new file mode 100644 index 000000000..f735d56ba --- /dev/null +++ b/lib/nbsd_libc/gmon/Makefile.inc @@ -0,0 +1,28 @@ +# $NetBSD: Makefile.inc,v 1.9 2009/12/17 06:54:51 mrg Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +# gmon sources +.PATH: ${.CURDIR}/gmon ${ARCHDIR}/gmon + +.-include "${ARCHDIR}/gmon/Makefile.inc" + +SRCS+= gmon.c mcount.c +MAN+= moncontrol.3 +MLINKS+=moncontrol.3 monstartup.3 + +.if (${MACHINE_ARCH} == "mipseb") || (${MACHINE_ARCH} == "mipsel") || \ + (${MACHINE_ARCH} == "mips64eb") || (${MACHINE_ARCH} == "mips64el") +# Turn off as(1) warnings on MIPS, since warnings are fatal with WARNS>0 +# and mcount.c causes warnings from as(1). +# mcount.c should be fixed and this test removed. +# +COPTS.mcount.c+=-Wa,--no-warn +.endif + + +# mcount and gmon cannot be compiled with profiling +mcount.po: mcount.o + cp mcount.o mcount.po + +gmon.po: gmon.o + cp gmon.o gmon.po diff --git a/lib/nbsd_libc/gmon/gmon.c b/lib/nbsd_libc/gmon/gmon.c new file mode 100644 index 000000000..01d017c49 --- /dev/null +++ b/lib/nbsd_libc/gmon/gmon.c @@ -0,0 +1,544 @@ +/* $NetBSD: gmon.c,v 1.33 2011/01/05 00:03:52 wiz Exp $ */ + +/* + * Copyright (c) 2003, 2004 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Nathan J. Williams for Wasabi Systems, Inc. + * + * 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 by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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. + */ + +/*- + * Copyright (c) 1983, 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 +#if !defined(lint) && defined(LIBC_SCCS) +#if 0 +static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: gmon.c,v 1.33 2011/01/05 00:03:52 wiz Exp $"); +#endif +#endif + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include "extern.h" +#include "reentrant.h" + +struct gmonparam _gmonparam = { .state = GMON_PROF_OFF }; + +#ifdef _REENTRANT +struct gmonparam *_gmonfree; +struct gmonparam *_gmoninuse; +mutex_t _gmonlock = MUTEX_INITIALIZER; +thread_key_t _gmonkey; +struct gmonparam _gmondummy; +#endif + +static u_int s_scale; +/* see profil(2) where this is describe (incorrectly) */ +#define SCALE_1_TO_1 0x10000L + +void moncontrol(int); +void monstartup(u_long, u_long); +void _mcleanup(void); +static int hertz(void); + +#ifdef _REENTRANT +static void _m_gmon_destructor(void *); +struct gmonparam *_m_gmon_alloc(void) + __attribute__((__no_instrument_function__)); +static void _m_gmon_merge(void); +static void _m_gmon_merge_two(struct gmonparam *, struct gmonparam *); +#endif + +void +monstartup(u_long lowpc, u_long highpc) +{ + u_long o; + char *cp; + struct gmonparam *p = &_gmonparam; + + /* + * round lowpc and highpc to multiples of the density we're using + * so the rest of the scaling (here and in gprof) stays in ints. + */ + p->lowpc = rounddown(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); + p->highpc = roundup(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); + p->textsize = p->highpc - p->lowpc; + p->kcountsize = p->textsize / HISTFRACTION; + p->hashfraction = HASHFRACTION; + p->fromssize = p->textsize / p->hashfraction; + p->tolimit = p->textsize * ARCDENSITY / 100; + if (p->tolimit < MINARCS) + p->tolimit = MINARCS; + else if (p->tolimit > MAXARCS) + p->tolimit = MAXARCS; + p->tossize = p->tolimit * sizeof(struct tostruct); + + cp = sbrk((intptr_t)(p->kcountsize + p->fromssize + p->tossize)); + if (cp == (char *)-1) { + warnx("%s: out of memory", __func__); + return; + } +#ifdef notdef + (void)memset(cp, 0, p->kcountsize + p->fromssize + p->tossize); +#endif + p->tos = (struct tostruct *)(void *)cp; + cp += (size_t)p->tossize; + p->kcount = (u_short *)(void *)cp; + cp += (size_t)p->kcountsize; + p->froms = (u_short *)(void *)cp; + + __minbrk = sbrk((intptr_t)0); + p->tos[0].link = 0; + + o = p->highpc - p->lowpc; + if (p->kcountsize < o) { +#ifndef notdef + s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; +#else /* avoid floating point */ + u_long quot = o / p->kcountsize; + + if (quot >= 0x10000) + s_scale = 1; + else if (quot >= 0x100) + s_scale = 0x10000 / quot; + else if (o >= 0x800000) + s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); + else + s_scale = 0x1000000 / ((o << 8) / p->kcountsize); +#endif + } else + s_scale = SCALE_1_TO_1; + +#ifdef _REENTRANT + _gmondummy.state = GMON_PROF_BUSY; + thr_keycreate(&_gmonkey, _m_gmon_destructor); +#endif + moncontrol(1); +} + +#ifdef _REENTRANT +static void +_m_gmon_destructor(void *arg) +{ + struct gmonparam *p = arg, *q, **prev; + + if (p == &_gmondummy) + return; + + thr_setspecific(_gmonkey, &_gmondummy); + + mutex_lock(&_gmonlock); + /* XXX eww, linear list traversal. */ + for (q = _gmoninuse, prev = &_gmoninuse; + q != NULL; + prev = (struct gmonparam **)(void *)&q->kcount, /* XXX */ + q = (struct gmonparam *)(void *)q->kcount) { + if (q == p) + *prev = (struct gmonparam *)(void *)q->kcount; + } + p->kcount = (u_short *)(void *)_gmonfree; + _gmonfree = p; + mutex_unlock(&_gmonlock); + + thr_setspecific(_gmonkey, NULL); +} + +struct gmonparam * +_m_gmon_alloc(void) +{ + struct gmonparam *p; + char *cp; + + mutex_lock(&_gmonlock); + if (_gmonfree != NULL) { + p = _gmonfree; + _gmonfree = (struct gmonparam *)(void *)p->kcount; + p->kcount = (u_short *)(void *)_gmoninuse; + _gmoninuse = p; + } else { + mutex_unlock(&_gmonlock); + cp = mmap(NULL, + (size_t)(sizeof (struct gmonparam) + + _gmonparam.fromssize + _gmonparam.tossize), + PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0LL); + p = (void *)cp; + *p = _gmonparam; + p->kcount = NULL; + cp += sizeof (struct gmonparam); + memset(cp, 0, (size_t)(p->fromssize + p->tossize)); + p->froms = (u_short *)(void *)cp; + p->tos = (struct tostruct *)(void *)(cp + p->fromssize); + mutex_lock(&_gmonlock); + p->kcount = (u_short *)(void *)_gmoninuse; + _gmoninuse = p; + } + mutex_unlock(&_gmonlock); + thr_setspecific(_gmonkey, p); + + return p; +} + +static void +_m_gmon_merge_two(struct gmonparam *p, struct gmonparam *q) +{ + u_long fromindex; + u_short *frompcindex, qtoindex, toindex; + u_long selfpc; + u_long endfrom; + long count; + struct tostruct *top; + + endfrom = (q->fromssize / sizeof(*q->froms)); + for (fromindex = 0; fromindex < endfrom; fromindex++) { + if (q->froms[fromindex] == 0) + continue; + for (qtoindex = q->froms[fromindex]; qtoindex != 0; + qtoindex = q->tos[qtoindex].link) { + selfpc = q->tos[qtoindex].selfpc; + count = q->tos[qtoindex].count; + /* cribbed from mcount */ + frompcindex = &p->froms[fromindex]; + toindex = *frompcindex; + if (toindex == 0) { + /* + * first time traversing this arc + */ + toindex = ++p->tos[0].link; + if (toindex >= p->tolimit) + /* halt further profiling */ + goto overflow; + + *frompcindex = (u_short)toindex; + top = &p->tos[(size_t)toindex]; + top->selfpc = selfpc; + top->count = count; + top->link = 0; + goto done; + } + top = &p->tos[(size_t)toindex]; + if (top->selfpc == selfpc) { + /* + * arc at front of chain; usual case. + */ + top->count+= count; + goto done; + } + /* + * have to go looking down chain for it. + * top points to what we are looking at, + * we know it is not at the head of the chain. + */ + for (; /* goto done */; ) { + if (top->link == 0) { + /* + * top is end of the chain and + * none of the chain had + * top->selfpc == selfpc. so + * we allocate a new tostruct + * and link it to the head of + * the chain. + */ + toindex = ++p->tos[0].link; + if (toindex >= p->tolimit) + goto overflow; + + top = &p->tos[(size_t)toindex]; + top->selfpc = selfpc; + top->count = count; + top->link = *frompcindex; + *frompcindex = (u_short)toindex; + goto done; + } + /* + * otherwise, check the next arc on the chain. + */ + top = &p->tos[top->link]; + if (top->selfpc == selfpc) { + /* + * there it is. + * add to its count. + */ + top->count += count; + goto done; + } + + } + + done: ; + } + + } + overflow: ; + +} + +static void +_m_gmon_merge(void) +{ + struct gmonparam *q; + + mutex_lock(&_gmonlock); + + for (q = _gmonfree; q != NULL; + q = (struct gmonparam *)(void *)q->kcount) + _m_gmon_merge_two(&_gmonparam, q); + + for (q = _gmoninuse; q != NULL; + q = (struct gmonparam *)(void *)q->kcount) { + q->state = GMON_PROF_OFF; + _m_gmon_merge_two(&_gmonparam, q); + } + + mutex_unlock(&_gmonlock); +} +#endif + +void +_mcleanup(void) +{ + int fd; + int fromindex; + int endfrom; + u_long frompc; + int toindex; + struct rawarc rawarc; + struct gmonparam *p = &_gmonparam; + struct gmonhdr gmonhdr, *hdr; + struct clockinfo clockinfo; + int mib[2]; + size_t size; + char *profdir; + const char *proffile; + char buf[PATH_MAX]; +#ifdef DEBUG + int logfd, len; + char buf2[200]; +#endif + + /* + * We disallow writing to the profiling file, if we are a + * set{u,g}id program and our effective {u,g}id does not match + * our real one. + */ + if (issetugid() && (geteuid() != getuid() || getegid() != getgid())) { + warnx("%s: Profiling of set{u,g}id binaries is not" + " allowed", __func__); + return; + } + + if (p->state == GMON_PROF_ERROR) + warnx("%s: tos overflow", __func__); + + size = sizeof(clockinfo); + mib[0] = CTL_KERN; + mib[1] = KERN_CLOCKRATE; + if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { + /* + * Best guess + */ + clockinfo.profhz = hertz(); + } else if (clockinfo.profhz == 0) { + if (clockinfo.hz != 0) + clockinfo.profhz = clockinfo.hz; + else + clockinfo.profhz = hertz(); + } + + moncontrol(0); + + if ((profdir = getenv("PROFDIR")) != NULL) { + /* If PROFDIR contains a null value, no profiling + output is produced */ + if (*profdir == '\0') + return; + + if (snprintf(buf, sizeof buf, "%s/%d.%s", + profdir, getpid(), getprogname()) >= (int)(sizeof buf)) { + warnx("%s: internal buffer overflow, PROFDIR too long", + __func__); + return; + } + + proffile = buf; + } else { + proffile = "gmon.out"; + } + + fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0666); + if (fd < 0) { + warn("%s: Cannot open `%s'", __func__, proffile); + return; + } +#ifdef DEBUG + logfd = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); + if (logfd < 0) { + warn("%s: Cannot open `%s'", __func__, "gmon.log"); + (void)close(fd); + return; + } + len = snprintf(buf2, sizeof buf2, "[mcleanup1] kcount %p ssiz %lu\n", + p->kcount, p->kcountsize); + (void)write(logfd, buf2, (size_t)len); +#endif +#ifdef _REENTRANT + _m_gmon_merge(); +#endif + hdr = (struct gmonhdr *)&gmonhdr; + hdr->lpc = p->lowpc; + hdr->hpc = p->highpc; + hdr->ncnt = (int)(p->kcountsize + sizeof(gmonhdr)); + hdr->version = GMONVERSION; + hdr->profrate = clockinfo.profhz; + (void)write(fd, hdr, sizeof *hdr); + (void)write(fd, p->kcount, (size_t)p->kcountsize); + endfrom = (int)(p->fromssize / sizeof(*p->froms)); + for (fromindex = 0; fromindex < endfrom; fromindex++) { + if (p->froms[fromindex] == 0) + continue; + + frompc = p->lowpc; + frompc += fromindex * p->hashfraction * sizeof(*p->froms); + for (toindex = p->froms[fromindex]; toindex != 0; + toindex = p->tos[toindex].link) { +#ifdef DEBUG + len = snprintf(buf2, sizeof buf2, + "[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" , + (u_long)frompc, (u_long)p->tos[toindex].selfpc, + (u_long)p->tos[toindex].count); + (void)write(logfd, buf2, (size_t)len); +#endif + rawarc.raw_frompc = frompc; + rawarc.raw_selfpc = p->tos[toindex].selfpc; + rawarc.raw_count = p->tos[toindex].count; + (void)write(fd, &rawarc, sizeof rawarc); + } + } + (void)close(fd); +#ifdef DEBUG + (void)close(logfd); +#endif +} + +/* + * Control profiling + * profiling is what mcount checks to see if + * all the data structures are ready. + */ +void +moncontrol(int mode) +{ + struct gmonparam *p = &_gmonparam; + + if (mode) { + /* start */ + profil((char *)(void *)p->kcount, (size_t)p->kcountsize, + p->lowpc, s_scale); + p->state = GMON_PROF_ON; + } else { + /* stop */ + profil(NULL, 0, (u_long)0, 0); + p->state = GMON_PROF_OFF; + } +} + +/* + * discover the tick frequency of the machine + * if something goes wrong, we return 0, an impossible hertz. + */ +static int +hertz(void) +{ + struct itimerspec tim; + timer_t t; + int rv = 0; + + tim.it_interval.tv_sec = 0; + tim.it_interval.tv_nsec = 1; + tim.it_value.tv_sec = 0; + tim.it_value.tv_nsec = 0; + + if (timer_create(CLOCK_REALTIME, NULL, &t) == -1) + return 0; + + if (timer_settime(t, 0, &tim, NULL) == -1) + goto out; + + if (timer_gettime(t, &tim) == -1) + goto out; + + if (tim.it_interval.tv_nsec < 2) + goto out; + + rv = (int)(1000000000LL / tim.it_interval.tv_nsec); +out: + (void)timer_delete(t); + return rv; +} diff --git a/lib/nbsd_libc/gmon/moncontrol.3 b/lib/nbsd_libc/gmon/moncontrol.3 new file mode 100644 index 000000000..bc3dc3257 --- /dev/null +++ b/lib/nbsd_libc/gmon/moncontrol.3 @@ -0,0 +1,115 @@ +.\" $NetBSD: moncontrol.3,v 1.10 2009/04/11 15:33:27 joerg Exp $ +.\" +.\" Copyright (c) 1980, 1991, 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. +.\" +.\" @(#)moncontrol.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt MONCONTROL 3 +.Os +.Sh NAME +.Nm moncontrol , +.Nm monstartup +.Nd control execution profile +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.Fn moncontrol "int mode" +.Fn monstartup "u_long *lowpc" "u_long *highpc" +.Sh DESCRIPTION +An executable program compiled using the +.Fl pg +option to +.Xr cc 1 +automatically includes calls to collect statistics for the +.Xr gprof 1 +call-graph execution profiler. +In typical operation, profiling begins at program startup +and ends when the program calls exit. +When the program exits, the profiling data are written to the file +.Em gmon.out , +then +.Xr gprof 1 +can be used to examine the results. +.Pp +.Fn moncontrol +selectively controls profiling within a program. +When the program starts, profiling begins. +To stop the collection of histogram ticks and call counts use +.Fn moncontrol 0 ; +to resume the collection of histogram ticks and call counts use +.Fn moncontrol 1 . +This feature allows the cost of particular operations to be measured. +Note that an output file will be produced on program exit +regardless of the state of +.Fn moncontrol . +.Pp +Programs that are not loaded with +.Fl pg +may selectively collect profiling statistics by calling +.Fn monstartup +with the range of addresses to be profiled. +.Fa lowpc +and +.Fa highpc +specify the address range that is to be sampled; +the lowest address sampled is that of +.Fa lowpc +and the highest is just below +.Fa highpc . +Only functions in that range that have been compiled with the +.Fl pg +option to +.Xr cc 1 +will appear in the call graph part of the output; +however, all functions in that address range will +have their execution time measured. +Profiling begins on return from +.Fn monstartup . +.Sh ENVIRONMENT +.Bl -tag -width "PROFDIR" +.It Ev PROFDIR +Directory to place the output file(s) in. +When this is set, instead of writing the profiling output to +.Pa gmon.out , +a filename is generated from the process id and name of the program +(e.g., +.Pa 123.a.out ) . +If you are profiling a program that forks, +or otherwise creates multiple copies, +setting this is the only reasonable way to get all profiling data. +.El +.Sh FILES +.Bl -tag -width ".Pa gmon.out" -compact +.It Pa gmon.out +execution data file +.El +.Sh SEE ALSO +.Xr cc 1 , +.Xr gprof 1 , +.Xr profil 2 diff --git a/lib/nbsd_libc/hash/Makefile.inc b/lib/nbsd_libc/hash/Makefile.inc new file mode 100644 index 000000000..3b5879abf --- /dev/null +++ b/lib/nbsd_libc/hash/Makefile.inc @@ -0,0 +1,11 @@ +# $NetBSD: Makefile.inc,v 1.11 2006/10/27 18:29:21 drochner Exp $ +# $OpenBSD: Makefile.inc,v 1.5 1997/07/17 06:02:42 millert Exp $ + +# hash functions +.PATH: ${ARCHDIR}/hash ${.CURDIR}/hash + +.include "${.CURDIR}/hash/md2/Makefile.inc" +.include "${.CURDIR}/hash/rmd160/Makefile.inc" +.include "${.CURDIR}/hash/sha1/Makefile.inc" +.include "${.CURDIR}/hash/sha2/Makefile.inc" + diff --git a/lib/nbsd_libc/hash/hashhl.c b/lib/nbsd_libc/hash/hashhl.c new file mode 100644 index 000000000..2816407a7 --- /dev/null +++ b/lib/nbsd_libc/hash/hashhl.c @@ -0,0 +1,145 @@ +/* $NetBSD: hashhl.c,v 1.4 2010/01/17 23:10:20 wiz Exp $ */ + +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + */ + +/* + * Modified September 24, 2005 by Elad Efrat + * Modified April 29, 1997 by Jason R. Thorpe + */ + +#ifdef HASH_ALGORITHM + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +/* + * Do all the name mangling before we include "namespace.h" + */ +#define CONCAT(x,y) __CONCAT(x,y) + +#ifndef HASH_FNPREFIX +#define HASH_FNPREFIX HASH_ALGORITHM +#endif /* !HASH_FNPREFIX */ + +#define FNPREFIX(x) CONCAT(HASH_FNPREFIX,x) +#define HASH_CTX CONCAT(HASH_ALGORITHM,_CTX) +#define HASH_LEN CONCAT(HASH_ALGORITHM,_DIGEST_LENGTH) +#define HASH_STRLEN CONCAT(HASH_ALGORITHM,_DIGEST_STRING_LENGTH) + +#if !defined(_KERNEL) && defined(__weak_alias) && !defined(HAVE_NBTOOL_CONFIG_H) +#define WA(a,b) __weak_alias(a,b) +WA(FNPREFIX(End),CONCAT(_,FNPREFIX(End))) +WA(FNPREFIX(FileChunk),CONCAT(_,FNPREFIX(FileChunk))) +WA(FNPREFIX(File),CONCAT(_,FNPREFIX(File))) +WA(FNPREFIX(Data),CONCAT(_,FNPREFIX(Data))) +#undef WA +#endif + +#include "namespace.h" +#include HASH_INCLUDE + +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifndef MIN +#define MIN(x,y) ((x)<(y)?(x):(y)) +#endif /* !MIN */ + + +char * +FNPREFIX(End)(HASH_CTX *ctx, char *buf) +{ + int i; + unsigned char digest[HASH_LEN]; + static const char hex[]="0123456789abcdef"; + + _DIAGASSERT(ctx != 0); + + if (buf == NULL) + buf = malloc((size_t)HASH_STRLEN); + if (buf == NULL) + return (NULL); + + FNPREFIX(Final)(digest, ctx); + + for (i = 0; i < HASH_LEN; i++) { + buf[i+i] = hex[(u_int32_t)digest[i] >> 4]; + buf[i+i+1] = hex[digest[i] & 0x0f]; + } + + buf[i+i] = '\0'; + return (buf); +} + +char * +FNPREFIX(FileChunk)(const char *filename, char *buf, off_t off, off_t len) +{ + struct stat sb; + u_char buffer[BUFSIZ]; + HASH_CTX ctx; + int fd, save_errno; + ssize_t nr; + + FNPREFIX(Init)(&ctx); + + if ((fd = open(filename, O_RDONLY)) < 0) + return (NULL); + if (len == 0) { + if (fstat(fd, &sb) == -1) { + close(fd); + return (NULL); + } + len = sb.st_size; + } + if (off > 0 && lseek(fd, off, SEEK_SET) < 0) { + close(fd); + return (NULL); + } + + while ((nr = read(fd, buffer, (size_t) MIN((off_t)sizeof(buffer), len))) + > 0) { + FNPREFIX(Update)(&ctx, buffer, (unsigned int)nr); + if (len > 0 && (len -= nr) == 0) + break; + } + + save_errno = errno; + close(fd); + errno = save_errno; + return (nr < 0 ? NULL : FNPREFIX(End)(&ctx, buf)); +} + +char * +FNPREFIX(File)(const char *filename, char *buf) +{ + return (FNPREFIX(FileChunk)(filename, buf, (off_t)0, (off_t)0)); +} + +char * +FNPREFIX(Data)(const unsigned char *data, size_t len, char *buf) +{ + HASH_CTX ctx; + + _DIAGASSERT(data != 0); + + FNPREFIX(Init)(&ctx); + FNPREFIX(Update)(&ctx, data, (unsigned int)len); + return (FNPREFIX(End)(&ctx, buf)); +} + +#endif /* HASH_ALGORITHM */ diff --git a/lib/nbsd_libc/hash/md2/Makefile.inc b/lib/nbsd_libc/hash/md2/Makefile.inc new file mode 100644 index 000000000..8e1adf531 --- /dev/null +++ b/lib/nbsd_libc/hash/md2/Makefile.inc @@ -0,0 +1,12 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/24 20:51:14 elad Exp $ + +# hash functions +.PATH: ${.CURDIR}/hash/md2 + +SRCS+= md2.c md2hl.c + +MAN+= md2.3 + +MLINKS+=md2.3 MD2Init.3 md2.3 MD2Update.3 md2.3 MD2Final.3 +MLINKS+=md2.3 MD2End.3 md2.3 MD2File.3 md2.3 MD2Data.3 +MLINKS+=md2.3 MD2Transform.3 md2.3 MD2FileChunk.3 diff --git a/lib/nbsd_libc/hash/md2/md2.3 b/lib/nbsd_libc/hash/md2/md2.3 new file mode 100644 index 000000000..e6f819cf9 --- /dev/null +++ b/lib/nbsd_libc/hash/md2/md2.3 @@ -0,0 +1,123 @@ +.\" $NetBSD: md2.3,v 1.1 2005/09/24 20:51:14 elad Exp $ +.\" +.\" ---------------------------------------------------------------------------- +.\" "THE BEER-WARE LICENSE" (Revision 42): +.\" wrote this file. As long as you retain this notice you +.\" can do whatever you want with this stuff. If we meet some day, and you think +.\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp +.\" ---------------------------------------------------------------------------- +.\" +.\" from FreeBSD Id: mdX.3,v 1.7 1996/10/22 16:28:56 phk Exp +.\" +.Dd September 24, 2005 +.Dt MD2 3 +.Os +.Sh NAME +.Nm MD2Init , +.Nm MD2Update , +.Nm MD2Final , +.Nm MD2End , +.Nm MD2File , +.Nm MD2Data +.Nd calculate the RSA Data Security, Inc., +.Dq MD2 +message digest +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In mdX.h +.Ft void +.Fn MD2Init "MD2_CTX *context" +.Ft void +.Fn MD2Update "MD2_CTX *context" "const unsigned char *data" "unsigned int len" +.Ft void +.Fn MD2Final "unsigned char digest[16]" "MD2_CTX *context" +.Ft "char *" +.Fn MD2End "MD2_CTX *context" "char *buf" +.Ft "char *" +.Fn MD2File "const char *filename" "char *buf" +.Ft "char *" +.Fn MD2Data "const unsigned char *data" "unsigned int len" "char *buf" +.Sh DESCRIPTION +The MD2 functions calculate a 128-bit cryptographic checksum (digest) +for any number of input bytes. +A cryptographic checksum is a one-way +hash-function, that is, you cannot find (except by exhaustive search) +the input corresponding to a particular output. +This net result is +a ``fingerprint'' of the input-data, which doesn't disclose the actual +input. +.Pp +The MD2 routines should not be used for any security-related purpose. +.Pp +The +.Fn MD2Init , +.Fn MD2Update , +and +.Fn MD2Final +functions are the core functions. +Allocate an MD2_CTX, initialize it with +.Fn MD2Init , +run over the data with +.Fn MD2Update , +and finally extract the result using +.Fn MD2Final . +.Pp +.Fn MD2End +is a wrapper for +.Fn MD2Final +which converts the return value to a 33-character +(including the terminating '\e0') +.Tn ASCII +string which represents the 128 bits in hexadecimal. +.Pp +.Fn MD2File +calculates the digest of a file, and uses +.Fn MD2End +to return the result. +If the file cannot be opened, a null pointer is returned. +.Fn MD2Data +calculates the digest of a chunk of data in memory, and uses +.Fn MD2End +to return the result. +.Pp +When using +.Fn MD2End , +.Fn MD2File , +or +.Fn MD2Data , +the +.Ar buf +argument can be a null pointer, in which case the returned string +is allocated with +.Xr malloc 3 +and subsequently must be explicitly deallocated using +.Xr free 3 +after use. +If the +.Ar buf +argument is non-null it must point to at least 33 characters of buffer space. +.Sh SEE ALSO +.Xr md2 3 , +.Rs +.%A B. Kaliski +.%T The MD2 Message-Digest Algorithm +.%O RFC 1319 +.Re +.Rs +.%A RSA Laboratories +.%T Frequently Asked Questions About today's Cryptography +.Re +.Sh HISTORY +These functions appeared in +.Nx 1.3 . +.Sh AUTHORS +The original MD2 routines were developed by +.Tn RSA +Data Security, Inc., and published in the above references. +This code is a public domain implementation by Andrew Brown. +.Sh BUGS +No method is known to exist which finds two files having the same hash value, +nor to find a file with a specific hash value. +There is on the other hand no guarantee that such a method doesn't exist. diff --git a/lib/nbsd_libc/hash/md2/md2.c b/lib/nbsd_libc/hash/md2/md2.c new file mode 100644 index 000000000..8e6367cf6 --- /dev/null +++ b/lib/nbsd_libc/hash/md2/md2.c @@ -0,0 +1,191 @@ +/* $NetBSD: md2.c,v 1.5 2008/04/28 20:23:00 martin Exp $ */ + +/* + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Brown. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: md2.c,v 1.5 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include + +#include +#include +#include + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#if !HAVE_MD2_H + +/* cut-n-pasted from rfc1319 */ +static unsigned char S[256] = { + 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, + 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, + 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, + 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, + 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, + 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, + 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, + 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, + 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, + 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, + 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, + 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, + 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, + 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, + 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, + 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, + 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, + 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 +}; + +/* cut-n-pasted from rfc1319 */ +static const unsigned char *pad[] = { + (const unsigned char *)"", + (const unsigned char *)"\001", + (const unsigned char *)"\002\002", + (const unsigned char *)"\003\003\003", + (const unsigned char *)"\004\004\004\004", + (const unsigned char *)"\005\005\005\005\005", + (const unsigned char *)"\006\006\006\006\006\006", + (const unsigned char *)"\007\007\007\007\007\007\007", + (const unsigned char *)"\010\010\010\010\010\010\010\010", + (const unsigned char *)"\011\011\011\011\011\011\011\011\011", + (const unsigned char *)"\012\012\012\012\012\012\012\012\012\012", + (const unsigned char *)"\013\013\013\013\013\013\013\013\013\013\013", + (const unsigned char *) + "\014\014\014\014\014\014\014\014\014\014\014\014", + (const unsigned char *) + "\015\015\015\015\015\015\015\015\015\015\015\015\015", + (const unsigned char *) + "\016\016\016\016\016\016\016\016\016\016\016\016\016\016", + (const unsigned char *) + "\017\017\017\017\017\017\017\017\017\017\017\017\017\017\017", + (const unsigned char *) + "\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020" +}; + +/* + * XXX This should not be visible, but due to an accident, it is + * XXX so it must remain so. + */ +/*static*/ void MD2Transform __P((MD2_CTX *)); + +#ifdef __weak_alias +__weak_alias(MD2Init,_MD2Init) +__weak_alias(MD2Update,_MD2Update) +__weak_alias(MD2Final,_MD2Final) +__weak_alias(MD2Transform,_MD2Transform) +#endif + +void +MD2Init(context) + MD2_CTX *context; +{ + _DIAGASSERT(context != 0); + + context->i = 16; + memset(&context->C[0], 0, sizeof(context->C)); + memset(&context->X[0], 0, sizeof(context->X)); +} + +void +MD2Update(context, input, inputLen) + MD2_CTX *context; + const unsigned char *input; + unsigned int inputLen; +{ + unsigned int idx, piece; + + _DIAGASSERT(context != 0); + _DIAGASSERT(input != 0); + + for (idx = 0; idx < inputLen; idx += piece) { + piece = 32 - context->i; + if ((inputLen - idx) < piece) + piece = inputLen - idx; + memcpy(&context->X[context->i], &input[idx], (size_t)piece); + if ((context->i += piece) == 32) + MD2Transform(context); /* resets i */ + } +} + +void +MD2Final(digest, context) + unsigned char digest[16]; /* message digest */ + MD2_CTX *context; /* context */ +{ + unsigned int padlen; + + _DIAGASSERT(digest != 0); + _DIAGASSERT(context != 0); + + /* padlen should be 1..16 */ + padlen = 32 - context->i; + + /* add padding */ + MD2Update(context, pad[padlen], padlen); + + /* add checksum */ + MD2Update(context, &context->C[0], (unsigned int) sizeof(context->C)); + + /* copy out final digest */ + memcpy(digest, &context->X[0], (size_t)16); + + /* reset the context */ + MD2Init(context); +} + +/*static*/ void +MD2Transform(context) + MD2_CTX *context; +{ + u_int32_t l, j, k, t; + + /* set block "3" and update "checksum" */ + for (l = context->C[15], j = 0; j < 16; j++) { + context->X[32 + j] = context->X[j] ^ context->X[16 + j]; + l = context->C[j] ^= S[context->X[16 + j] ^ l]; + } + + /* mangle input block */ + for (t = j = 0; j < 18; t = (t + j) % 256, j++) + for (k = 0; k < 48; k++) + t = context->X[k] = (context->X[k] ^ S[t]); + + /* reset input pointer */ + context->i = 16; +} + +#endif /* !HAVE_MD2_H */ diff --git a/lib/nbsd_libc/hash/md2/md2hl.c b/lib/nbsd_libc/hash/md2/md2hl.c new file mode 100644 index 000000000..64eb2bef9 --- /dev/null +++ b/lib/nbsd_libc/hash/md2/md2hl.c @@ -0,0 +1,17 @@ +/* $NetBSD: md2hl.c,v 1.4 2008/04/13 02:04:31 dholland Exp $ */ + +/* + * Derived from code written by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: md2hl.c,v 1.4 2008/04/13 02:04:31 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM MD2 +#define HASH_INCLUDE + +#include "../hashhl.c" diff --git a/lib/nbsd_libc/hash/rmd160/Makefile.inc b/lib/nbsd_libc/hash/rmd160/Makefile.inc new file mode 100644 index 000000000..0caf265e0 --- /dev/null +++ b/lib/nbsd_libc/hash/rmd160/Makefile.inc @@ -0,0 +1,13 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/24 19:04:52 elad Exp $ + +# hash functions +.PATH: ${.CURDIR}/hash/rmd160 + +SRCS+= rmd160.c rmd160hl.c + +MAN+= rmd160.3 + +MLINKS+=rmd160.3 RMD160Init.3 rmd160.3 RMD160Update.3 +MLINKS+=rmd160.3 RMD160Final.3 rmd160.3 RMD160Transform.3 +MLINKS+=rmd160.3 RMD160End.3 rmd160.3 RMD160File.3 +MLINKS+=rmd160.3 RMD160Data.3 diff --git a/lib/nbsd_libc/hash/rmd160/rmd160.3 b/lib/nbsd_libc/hash/rmd160/rmd160.3 new file mode 100644 index 000000000..f9d64e09e --- /dev/null +++ b/lib/nbsd_libc/hash/rmd160/rmd160.3 @@ -0,0 +1,225 @@ +.\" $NetBSD: rmd160.3,v 1.3 2010/04/05 21:26:30 joerg Exp $ +.\" $OpenBSD: rmd160.3,v 1.12 2000/04/18 03:01:29 aaron Exp $ +.\" +.\" Copyright (c) 1997 Todd C. Miller +.\" 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 not be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" +.\" 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. +.\" +.\" See http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html +.\" for detailed information about RIPEMD-160. +.\" +.Dd July 16, 1997 +.Dt RMD160 3 +.Os +.Sh NAME +.Nm RMD160Init , +.Nm RMD160Update , +.Nm RMD160Final , +.Nm RMD160Transform , +.Nm RMD160End , +.Nm RMD160File , +.Nm RMD160Data +.Nd calculate the ``RIPEMD-160'' message digest +.Sh SYNOPSIS +.In sys/types.h +.In rmd160.h +.Ft void +.Fn RMD160Init "RMD160_CTX *context" +.Ft void +.Fn RMD160Update "RMD160_CTX *context" "const u_char *data" "u_int nbytes" +.Ft void +.Fn RMD160Final "u_char digest[20]" "RMD160_CTX *context" +.Ft void +.Fn RMD160Transform "uint32_t state[5]" "const uint32_t block[16]" +.Ft "char *" +.Fn RMD160End "RMD160_CTX *context" "char *buf" +.Ft "char *" +.Fn RMD160File "char *filename" "char *buf" +.Ft "char *" +.Fn RMD160Data "u_char *data" "size_t len" "char *buf" +.Sh DESCRIPTION +The RMD160 functions implement the 160-bit RIPE message digest hash algorithm +(RMD-160). +RMD-160 is used to generate a condensed representation +of a message called a message digest. +The algorithm takes a +message less than 2^64 bits as input and produces a 160-bit digest +suitable for use as a digital signature. +.Pp +The RMD160 functions are considered to be more secure than the +.Xr md4 3 +and +.Xr md5 3 +functions and at least as secure as the +.Xr sha1 3 +function. +All share a similar interface. +.Pp +The +.Fn RMD160Init +function initializes a RMD160_CTX +.Ar context +for use with +.Fn RMD160Update , +and +.Fn RMD160Final . +The +.Fn RMD160Update +function adds +.Ar data +of length +.Ar nbytes +to the RMD160_CTX specified by +.Ar context . +.Fn RMD160Final +is called when all data has been added via +.Fn RMD160Update +and stores a message digest in the +.Ar digest +parameter. +When a null pointer is passed to +.Fn RMD160Final +as first argument only the final padding will be applied and the +current context can still be used with +.Fn RMD160Update . +.Pp +The +.Fn RMD160Transform +function is used by +.Fn RMD160Update +to hash 512-bit blocks and forms the core of the algorithm. +Most programs should use the interface provided by +.Fn RMD160Init , +.Fn RMD160Update +and +.Fn RMD160Final +instead of calling +.Fn RMD160Transform +directly. +.Pp +The +.Fn RMD160End +function is a front end for +.Fn RMD160Final +which converts the digest into an +.Tn ASCII +representation of the 160 bit digest in hexadecimal. +.Pp +The +.Fn RMD160File +function calculates the digest for a file and returns the result via +.Fn RMD160End . +If +.Fn RMD160File +is unable to open the file a NULL pointer is returned. +.Pp +The +.Fn RMD160Data +function +calculates the digest of an arbitrary string and returns the result via +.Fn RMD160End . +.Pp +For each of the +.Fn RMD160End , +.Fn RMD160File , +and +.Fn RMD160Data +functions the +.Ar buf +parameter should either be a string of at least 41 characters in +size or a NULL pointer. +In the latter case, space will be dynamically allocated via +.Xr malloc 3 +and should be freed using +.Xr free 3 +when it is no longer needed. +.Sh EXAMPLES +The follow code fragment will calculate the digest for +the string "abc" which is ``0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc''. +.Bd -literal -offset indent +RMD160_CTX rmd; +u_char results[20]; +char *buf; +int n; + +buf = "abc"; +n = strlen(buf); +RMD160Init(\*[Am]rmd); +RMD160Update(\*[Am]rmd, (u_char *)buf, n); +RMD160Final(results, \*[Am]rmd); + +/* Print the digest as one long hex value */ +printf("0x"); +for (n = 0; n \*[Lt] 20; n++) + printf("%02x", results[n]); +putchar('\en'); +.Ed +.Pp +Alternately, the helper functions could be used in the following way: +.Bd -literal -offset indent +RMD160_CTX rmd; +u_char output[41]; +char *buf = "abc"; + +printf("0x%s\en", RMD160Data(buf, strlen(buf), output)); +.Ed +.Sh SEE ALSO +.Xr rmd160 1 , +.Xr md4 3 , +.Xr md5 3 , +.Xr sha1 3 +.Pp +.Rs +.%A H. Dobbertin, A. Bosselaers, B. Preneel +.%T RIPEMD-160, a strengthened version of RIPEMD +.Re +.Rs +.%T Information technology - Security techniques - Hash-functions - Part 3: Dedicated hash-functions +.%O ISO/IEC 10118-3 +.Re +.Rs +.%A H. Dobbertin, A. Bosselaers, B. Preneel +.%T The RIPEMD-160 cryptographic hash function +.%J Dr. Dobb's Journal +.%V Vol. 22, No. 1 +.%D January 1997 +.%P pp. 24-28 +.Re +.Sh HISTORY +The RMD-160 functions appeared in +.Ox 2.1 . +.Sh AUTHORS +This implementation of RMD-160 was written by Antoon Bosselaers. +.Pp +The +.Fn RMD160End , +.Fn RMD160File , +and +.Fn RMD160Data +helper functions are derived from code written by Poul-Henning Kamp. +.Sh BUGS +If a message digest is to be copied to a multi-byte type (ie: +an array of five 32-bit integers) it will be necessary to +perform byte swapping on little endian machines such as the i386, alpha, +and VAX. diff --git a/lib/nbsd_libc/hash/rmd160/rmd160hl.c b/lib/nbsd_libc/hash/rmd160/rmd160hl.c new file mode 100644 index 000000000..85cc8373c --- /dev/null +++ b/lib/nbsd_libc/hash/rmd160/rmd160hl.c @@ -0,0 +1,17 @@ +/* $NetBSD: rmd160hl.c,v 1.6 2008/04/13 02:04:32 dholland Exp $ */ + +/* + * Derived from code written by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: rmd160hl.c,v 1.6 2008/04/13 02:04:32 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM RMD160 +#define HASH_INCLUDE + +#include "../hashhl.c" diff --git a/lib/nbsd_libc/hash/sha1/Makefile.inc b/lib/nbsd_libc/hash/sha1/Makefile.inc new file mode 100644 index 000000000..99c35cf29 --- /dev/null +++ b/lib/nbsd_libc/hash/sha1/Makefile.inc @@ -0,0 +1,12 @@ +# $NetBSD: Makefile.inc,v 1.2 2006/10/27 18:29:21 drochner Exp $ + +# hash functions +.PATH: ${.CURDIR}/hash/sha1 + +SRCS+= sha1.c sha1hl.c + +MAN+= sha1.3 + +MLINKS+=sha1.3 SHA1Init.3 sha1.3 SHA1Update.3 sha1.3 SHA1Final.3 +MLINKS+=sha1.3 SHA1End.3 sha2.3 SHA1File.3 sha1.3 SHA1Data.3 +MLINKS+=sha1.3 SHA1Transform.3 sha1.3 SHA1FileChunk.3 diff --git a/lib/nbsd_libc/hash/sha1/sha1.3 b/lib/nbsd_libc/hash/sha1/sha1.3 new file mode 100644 index 000000000..762fd03c5 --- /dev/null +++ b/lib/nbsd_libc/hash/sha1/sha1.3 @@ -0,0 +1,203 @@ +.\" $NetBSD: sha1.3,v 1.6 2010/04/05 21:27:01 joerg Exp $ +.\" $OpenBSD: sha1.3,v 1.9 1998/03/07 22:18:12 millert Exp $ +.\" +.\" Copyright (c) 1997, 2004 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. +.\" +.\" See http://csrc.nist.gov/fips/fip180-1.txt for the detailed standard +.\" +.Dd July 10, 1997 +.Dt SHA1 3 +.Os +.Sh NAME +.Nm SHA1Init , +.Nm SHA1Update , +.Nm SHA1Final , +.Nm SHA1Transform , +.Nm SHA1End , +.Nm SHA1File , +.Nm SHA1Data +.Nd calculate the NIST Secure Hash Algorithm +.Sh SYNOPSIS +.In sys/types.h +.In sha1.h +.Ft void +.Fn SHA1Init "SHA1_CTX *context" +.Ft void +.Fn SHA1Update "SHA1_CTX *context" "const uint8_t *data" "u_int len" +.Ft void +.Fn SHA1Final "uint8_t digest[20]" "SHA1_CTX *context" +.Ft void +.Fn SHA1Transform "uint32_t state[5]" "uint8_t buffer[64]" +.Ft "char *" +.Fn SHA1End "SHA1_CTX *context" "char *buf" +.Ft "char *" +.Fn SHA1File "char *filename" "char *buf" +.Ft "char *" +.Fn SHA1Data "uint8_t *data" "size_t len" "char *buf" +.Sh DESCRIPTION +The SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1), +FIPS PUB 180-1. +SHA-1 is used to generate a condensed representation +of a message called a message digest. +The algorithm takes a +message less than 2^64 bits as input and produces a 160-bit digest +suitable for use as a digital signature. +.Pp +The SHA1 functions are considered to be more secure than the +.Xr md4 3 +and +.Xr md5 3 +functions with which they share a similar interface. +.Pp +The +.Fn SHA1Init +function initializes a SHA1_CTX +.Ar context +for use with +.Fn SHA1Update , +and +.Fn SHA1Final . +The +.Fn SHA1Update +function adds +.Ar data +of length +.Ar len +to the SHA1_CTX specified by +.Ar context . +.Fn SHA1Final +is called when all data has been added via +.Fn SHA1Update +and stores a message digest in the +.Ar digest +parameter. +When a null pointer is passed to +.Fn SHA1Final +as first argument only the final padding will be applied and the +current context can still be used with +.Fn SHA1Update . +.Pp +The +.Fn SHA1Transform +function is used by +.Fn SHA1Update +to hash 512-bit blocks and forms the core of the algorithm. +Most programs should use the interface provided by +.Fn SHA1Init , +.Fn SHA1Update +and +.Fn SHA1Final +instead of calling +.Fn SHA1Transform +directly. +.Pp +The +.Fn SHA1End +function is a front end for +.Fn SHA1Final +which converts the digest into an +.Tn ASCII +representation of the 160 bit digest in hexadecimal. +.Pp +The +.Fn SHA1File +function calculates the digest for a file and returns the result via +.Fn SHA1End . +If +.Fn SHA1File +is unable to open the file a NULL pointer is returned. +.Pp +The +.Fn SHA1Data +function +calculates the digest of an arbitrary string and returns the result via +.Fn SHA1End . +.Pp +For each of the +.Fn SHA1End , +.Fn SHA1File , +and +.Fn SHA1Data +functions the +.Ar buf +parameter should either be a string of at least 41 characters in +size or a NULL pointer. +In the latter case, space will be dynamically +allocated via +.Xr malloc 3 +and should be freed using +.Xr free 3 +when it is no longer needed. +.Sh EXAMPLES +The follow code fragment will calculate the digest for +the string "abc" which is ``0xa9993e36476816aba3e25717850c26c9cd0d89d''. +.Bd -literal -offset indent +SHA1_CTX sha; +uint8_t results[20]; +char *buf; +int n; + +buf = "abc"; +n = strlen(buf); +SHA1Init(\*[Am]sha); +SHA1Update(\*[Am]sha, (uint8_t *)buf, n); +SHA1Final(results, \*[Am]sha); + +/* Print the digest as one long hex value */ +printf("0x"); +for (n = 0; n \*[Lt] 20; n++) + printf("%02x", results[n]); +putchar('\en'); +.Ed +.Pp +Alternately, the helper functions could be used in the following way: +.Bd -literal -offset indent +SHA1_CTX sha; +uint8_t output[41]; +char *buf = "abc"; + +printf("0x%s", SHA1Data(buf, strlen(buf), output)); +.Ed +.Sh SEE ALSO +.\" .Xr sha1 1 , +.Xr md5 1 , +.Xr md4 3 , +.Xr md5 3 +.Pp +.Rs +.%A J. Burrows +.%T The Secure Hash Standard +.%O FIPS PUB 180-1 +.Re +.Sh HISTORY +The SHA-1 functions appeared in +.Nx 1.4 . +.Sh AUTHORS +This implementation of SHA-1 was written by Steve Reid. +.Pp +The +.Fn SHA1End , +.Fn SHA1File , +and +.Fn SHA1Data +helper functions are derived from code written by Poul-Henning Kamp. +.Sh BUGS +This implementation of SHA-1 has not been validated by NIST +and as such is not in official compliance with the standard. +.Pp +If a message digest is to be copied to a multi-byte type (ie: +an array of five 32-bit integers) it will be necessary to +perform byte swapping on little endian machines such as the i386, alpha, +and VAX. diff --git a/lib/nbsd_libc/hash/sha1/sha1hl.c b/lib/nbsd_libc/hash/sha1/sha1hl.c new file mode 100644 index 000000000..a73f4df82 --- /dev/null +++ b/lib/nbsd_libc/hash/sha1/sha1hl.c @@ -0,0 +1,17 @@ +/* $NetBSD: sha1hl.c,v 1.4 2008/04/13 02:04:32 dholland Exp $ */ + +/* + * Derived from code written by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha1hl.c,v 1.4 2008/04/13 02:04:32 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM SHA1 +#define HASH_INCLUDE + +#include "../hashhl.c" diff --git a/lib/nbsd_libc/hash/sha2/Makefile.inc b/lib/nbsd_libc/hash/sha2/Makefile.inc new file mode 100644 index 000000000..4d13c2fdc --- /dev/null +++ b/lib/nbsd_libc/hash/sha2/Makefile.inc @@ -0,0 +1,21 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/05/26 08:04:11 joerg Exp $ + +# hash functions +.PATH: ${.CURDIR}/hash/sha2 + +SRCS+= sha2.c sha224hl.c sha256hl.c sha384hl.c sha512hl.c + +MAN+= sha2.3 + +MLINKS+=sha2.3 SHA224_Init.3 sha2.3 SHA224_Update.3 sha2.3 SHA224_Final.3 +MLINKS+=sha2.3 SHA224_End.3 sha2.3 SHA224_File.3 sha2.3 SHA224_Data.3 +MLINKS+=sha2.3 SHA224_Transform.3 sha2.3 SHA224_FileChunk.3 +MLINKS+=sha2.3 SHA256_Init.3 sha2.3 SHA256_Update.3 sha2.3 SHA256_Final.3 +MLINKS+=sha2.3 SHA256_End.3 sha2.3 SHA256_File.3 sha2.3 SHA256_Data.3 +MLINKS+=sha2.3 SHA256_Transform.3 sha2.3 SHA256_FileChunk.3 +MLINKS+=sha2.3 SHA384_Init.3 sha2.3 SHA384_Update.3 sha2.3 SHA384_Final.3 +MLINKS+=sha2.3 SHA384_End.3 sha2.3 SHA384_File.3 sha2.3 SHA384_Data.3 +MLINKS+=sha2.3 SHA384_Transform.3 sha2.3 SHA384_FileChunk.3 +MLINKS+=sha2.3 SHA512_Init.3 sha2.3 SHA512_Update.3 sha2.3 SHA512_Final.3 +MLINKS+=sha2.3 SHA512_End.3 sha2.3 SHA512_File.3 sha2.3 SHA512_Data.3 +MLINKS+=sha2.3 SHA512_Transform.3 sha2.3 SHA512_FileChunk.3 diff --git a/lib/nbsd_libc/hash/sha2/sha2.3 b/lib/nbsd_libc/hash/sha2/sha2.3 new file mode 100644 index 000000000..8192cf57e --- /dev/null +++ b/lib/nbsd_libc/hash/sha2/sha2.3 @@ -0,0 +1,304 @@ +.\" $NetBSD: sha2.3,v 1.5 2009/05/26 08:04:12 joerg Exp $ +.\" $OpenBSD: sha2.3,v 1.11 2004/06/22 01:57:29 jfb Exp $ +.\" +.\" Copyright (c) 2003, 2004 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. +.\" +.\" See http://www.nist.gov/sha/ for the detailed standard +.\" +.Dd May 20, 2009 +.Dt SHA2 3 +.Os +.Sh NAME +.Nm SHA256_Init , +.Nm SHA256_Update , +.Nm SHA256_Pad , +.Nm SHA256_Final , +.Nm SHA256_Transform , +.Nm SHA256_End , +.Nm SHA256_File , +.Nm SHA256_FileChunk , +.Nm SHA256_Data +.Nd calculate the NIST Secure Hash Standard (version 2) +.Sh SYNOPSIS +.In sys/types.h +.In sha2.h +.Ft void +.Fn SHA224_Init "SHA224_CTX *context" +.Ft void +.Fn SHA224_Update "SHA224_CTX *context" "const uint8_t *data" "size_t len" +.Ft void +.Fn SHA224_Pad "SHA224_CTX *context" +.Ft void +.Fn SHA224_Final "uint8_t digest[SHA224_DIGEST_LENGTH]" "SHA224_CTX *context" +.Ft void +.Fn SHA224_Transform "uint32_t state[8]" "const uint8_t buffer[SHA224_BLOCK_LENGTH]" +.Ft "char *" +.Fn SHA224_End "SHA224_CTX *context" "char *buf" +.Ft "char *" +.Fn SHA224_File "const char *filename" "char *buf" +.Ft "char *" +.Fn SHA224_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" +.Fn SHA224_Data "uint8_t *data" "size_t len" "char *buf" +.Ft void +.Fn SHA256_Init "SHA256_CTX *context" +.Ft void +.Fn SHA256_Update "SHA256_CTX *context" "const uint8_t *data" "size_t len" +.Ft void +.Fn SHA256_Pad "SHA256_CTX *context" +.Ft void +.Fn SHA256_Final "uint8_t digest[SHA256_DIGEST_LENGTH]" "SHA256_CTX *context" +.Ft void +.Fn SHA256_Transform "uint32_t state[8]" "const uint8_t buffer[SHA256_BLOCK_LENGTH]" +.Ft "char *" +.Fn SHA256_End "SHA256_CTX *context" "char *buf" +.Ft "char *" +.Fn SHA256_File "const char *filename" "char *buf" +.Ft "char *" +.Fn SHA256_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" +.Fn SHA256_Data "uint8_t *data" "size_t len" "char *buf" +.Ft void +.Fn SHA384_Init "SHA384_CTX *context" +.Ft void +.Fn SHA384_Update "SHA384_CTX *context" "const uint8_t *data" "size_t len" +.Ft void +.Fn SHA384_Pad "SHA384_CTX *context" +.Ft void +.Fn SHA384_Final "uint8_t digest[SHA384_DIGEST_LENGTH]" "SHA384_CTX *context" +.Ft void +.Fn SHA384_Transform "uint64_t state[8]" "const uint8_t buffer[SHA384_BLOCK_LENGTH]" +.Ft "char *" +.Fn SHA384_End "SHA384_CTX *context" "char *buf" +.Ft "char *" +.Fn SHA384_File "char *filename" "char *buf" +.Ft "char *" +.Fn SHA384_FileChunk "char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" +.Fn SHA384_Data "uint8_t *data" "size_t len" "char *buf" +.Ft void +.Fn SHA512_Init "SHA512_CTX *context" +.Ft void +.Fn SHA512_Update "SHA512_CTX *context" "const uint8_t *data" "size_t len" +.Ft void +.Fn SHA512_Pad "SHA512_CTX *context" +.Ft void +.Fn SHA512_Final "uint8_t digest[SHA512_DIGEST_LENGTH]" "SHA512_CTX *context" +.Ft void +.Fn SHA512_Transform "uint64_t state[8]" "const uint8_t buffer[SHA512_BLOCK_LENGTH]" +.Ft "char *" +.Fn SHA512_End "SHA512_CTX *context" "char *buf" +.Ft "char *" +.Fn SHA512_File "char *filename" "char *buf" +.Ft "char *" +.Fn SHA512_FileChunk "char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" +.Fn SHA512_Data "uint8_t *data" "size_t len" "char *buf" +.Sh DESCRIPTION +The SHA2 functions implement the NIST Secure Hash Standard, +FIPS PUB 180-2. +The SHA2 functions are used to generate a condensed representation of a +message called a message digest, suitable for use as a digital signature. +There are four families of functions, with names corresponding to +the number of bits in the resulting message digest. +The SHA-224 and SHA-256 functions are limited to processing a message of less +than 2^64 bits as input. +The SHA-384 and SHA-512 functions can process a message of at most 2^128 - 1 +bits as input. +.Pp +The SHA2 functions are considered to be more secure than the +.Xr sha1 3 +functions with which they share a similar interface. +The 224, 256, 384, and 512-bit versions of SHA2 share the same interface. +For brevity, only the 256-bit variants are described below. +.Pp +The +.Fn SHA256_Init +function initializes a SHA256_CTX +.Ar context +for use with +.Fn SHA256_Update , +and +.Fn SHA256_Final . +The +.Fn SHA256_Update +function adds +.Ar data +of length +.Ar len +to the SHA256_CTX specified by +.Ar context . +.Fn SHA256_Final +is called when all data has been added via +.Fn SHA256_Update +and stores a message digest in the +.Ar digest +parameter. +.Pp +The +.Fn SHA256_Pad +function can be used to apply padding to the message digest as in +.Fn SHA256_Final , +but the current context can still be used with +.Fn SHA256_Update . +.Pp +The +.Fn SHA256_Transform +function is used by +.Fn SHA256_Update +to hash 512-bit blocks and forms the core of the algorithm. +Most programs should use the interface provided by +.Fn SHA256_Init , +.Fn SHA256_Update , +and +.Fn SHA256_Final +instead of calling +.Fn SHA256_Transform +directly. +.Pp +The +.Fn SHA256_End +function is a front end for +.Fn SHA256_Final +which converts the digest into an +.Tn ASCII +representation of the digest in hexadecimal. +.Pp +The +.Fn SHA256_File +function calculates the digest for a file and returns the result via +.Fn SHA256_End . +If +.Fn SHA256_File +is unable to open the file, a +.Dv NULL +pointer is returned. +.Pp +.Fn SHA256_FileChunk +behaves like +.Fn SHA256_File +but calculates the digest only for that portion of the file starting at +.Fa offset +and continuing for +.Fa length +bytes or until end of file is reached, whichever comes first. +A zero +.Fa length +can be specified to read until end of file. +A negative +.Fa length +or +.Fa offset +will be ignored. +.Pp +The +.Fn SHA256_Data +function +calculates the digest of an arbitrary string and returns the result via +.Fn SHA256_End . +.Pp +For each of the +.Fn SHA256_End , +.Fn SHA256_File , +.Fn SHA256_FileChunk , +and +.Fn SHA256_Data +functions the +.Ar buf +parameter should either be a string large enough to hold the resulting digest +(e.g., +.Ev SHA224_DIGEST_STRING_LENGTH , +.Ev SHA256_DIGEST_STRING_LENGTH , +.Ev SHA384_DIGEST_STRING_LENGTH , +or +.Ev SHA512_DIGEST_STRING_LENGTH , +depending on the function being used) +or a +.Dv NULL +pointer. +In the latter case, space will be dynamically allocated via +.Xr malloc 3 +and should be freed using +.Xr free 3 +when it is no longer needed. +.Sh EXAMPLES +The following code fragment will calculate the SHA-256 digest for the string +.Qq abc , +which is +.Dq 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad . +.Bd -literal -offset indent +SHA256_CTX ctx; +uint8_t results[SHA256_DIGEST_LENGTH]; +char *buf; +int n; + +buf = "abc"; +n = strlen(buf); +SHA256_Init(\*[Am]ctx); +SHA256_Update(\*[Am]ctx, (uint8_t *)buf, n); +SHA256_Final(results, \*[Am]ctx); + +/* Print the digest as one long hex value */ +printf("0x"); +for (n = 0; n \*[Lt] SHA256_DIGEST_LENGTH; n++) + printf("%02x", results[n]); +putchar('\en'); +.Ed +.Pp +Alternately, the helper functions could be used in the following way: +.Bd -literal -offset indent +SHA256_CTX ctx; +uint8_t output[SHA256_DIGEST_STRING_LENGTH]; +char *buf = "abc"; + +printf("0x%s\en", SHA256_Data(buf, strlen(buf), output)); +.Ed +.Sh SEE ALSO +.Xr cksum 1 , +.Xr md4 3 , +.Xr md5 3 , +.Xr rmd160 3 , +.Xr sha1 3 +.Rs +.%T Secure Hash Standard +.%O FIPS PUB 180-2 +.Re +.Sh HISTORY +The SHA2 functions appeared in +.Ox 3.4 +and +.Nx 3.0 . +.Sh AUTHORS +This implementation of the SHA functions was written by Aaron D. Gifford. +.Pp +The +.Fn SHA256_End , +.Fn SHA256_File , +.Fn SHA256_FileChunk , +and +.Fn SHA256_Data +helper functions are derived from code written by Poul-Henning Kamp. +.Sh CAVEATS +This implementation of the Secure Hash Standard has not been validated by +NIST and as such is not in official compliance with the standard. +.Pp +If a message digest is to be copied to a multi-byte type (i.e.: +an array of five 32-bit integers) it will be necessary to +perform byte swapping on little endian machines such as the i386, alpha, +and vax. diff --git a/lib/nbsd_libc/hash/sha2/sha224hl.c b/lib/nbsd_libc/hash/sha2/sha224hl.c new file mode 100644 index 000000000..52f4bbf41 --- /dev/null +++ b/lib/nbsd_libc/hash/sha2/sha224hl.c @@ -0,0 +1,16 @@ +/* $NetBSD */ + +/* + * Derived from code written by Jason R. Thorpe , + * May 20, 2009. + * Public domain. + */ + +#include +__RCSID("$NetBSD: sha224hl.c,v 1.1 2009/05/26 08:04:12 joerg Exp $"); + +#define HASH_ALGORITHM SHA224 +#define HASH_FNPREFIX SHA224_ +#define HASH_INCLUDE + +#include "../hashhl.c" diff --git a/lib/nbsd_libc/hash/sha2/sha256hl.c b/lib/nbsd_libc/hash/sha2/sha256hl.c new file mode 100644 index 000000000..960f4daad --- /dev/null +++ b/lib/nbsd_libc/hash/sha2/sha256hl.c @@ -0,0 +1,18 @@ +/* $NetBSD: sha256hl.c,v 1.8 2008/04/13 02:04:32 dholland Exp $ */ + +/* + * Derived from code written by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha256hl.c,v 1.8 2008/04/13 02:04:32 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM SHA256 +#define HASH_FNPREFIX SHA256_ +#define HASH_INCLUDE + +#include "../hashhl.c" diff --git a/lib/nbsd_libc/hash/sha2/sha384hl.c b/lib/nbsd_libc/hash/sha2/sha384hl.c new file mode 100644 index 000000000..c503d8b7a --- /dev/null +++ b/lib/nbsd_libc/hash/sha2/sha384hl.c @@ -0,0 +1,18 @@ +/* $NetBSD: sha384hl.c,v 1.8 2008/04/13 02:04:32 dholland Exp $ */ + +/* + * Derived from code written by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha384hl.c,v 1.8 2008/04/13 02:04:32 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM SHA384 +#define HASH_FNPREFIX SHA384_ +#define HASH_INCLUDE + +#include "../hashhl.c" diff --git a/lib/nbsd_libc/hash/sha2/sha512hl.c b/lib/nbsd_libc/hash/sha2/sha512hl.c new file mode 100644 index 000000000..bc46b16fa --- /dev/null +++ b/lib/nbsd_libc/hash/sha2/sha512hl.c @@ -0,0 +1,18 @@ +/* $NetBSD: sha512hl.c,v 1.8 2008/04/13 02:04:32 dholland Exp $ */ + +/* + * Derived from code written by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha512hl.c,v 1.8 2008/04/13 02:04:32 dholland Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM SHA512 +#define HASH_FNPREFIX SHA512_ +#define HASH_INCLUDE + +#include "../hashhl.c" diff --git a/lib/nbsd_libc/iconv/Makefile.inc b/lib/nbsd_libc/iconv/Makefile.inc new file mode 100644 index 000000000..723bf382b --- /dev/null +++ b/lib/nbsd_libc/iconv/Makefile.inc @@ -0,0 +1,13 @@ +# $NetBSD: Makefile.inc,v 1.4 2009/01/11 02:46:28 christos Exp $ + +.PATH: ${ARCHDIR}/iconv ${.CURDIR}/iconv + +SRCS+= iconv.c +MAN+= iconv.3 +MLINKS+=iconv.3 iconv_open.3 iconv.3 iconv_close.3 + +.if ${CITRUS} == "yes" +CPPFLAGS.iconv.c+= -DHAVE_CITRUS -I${LIBCDIR}/citrus +.else +CPPFLAGS.iconv.c+= -UHAVE_CITRUS +.endif diff --git a/lib/nbsd_libc/iconv/iconv.3 b/lib/nbsd_libc/iconv/iconv.3 new file mode 100644 index 000000000..b43934a9b --- /dev/null +++ b/lib/nbsd_libc/iconv/iconv.3 @@ -0,0 +1,244 @@ +.\" $NetBSD: iconv.3,v 1.14 2010/05/05 22:07:58 wiz Exp $ +.\" +.\" Copyright (c)2003 Citrus Project, +.\" 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 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. +.\" +.Dd May 5, 2010 +.Dt ICONV 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm iconv_open , +.Nm iconv_close , +.Nm iconv +.Nd codeset conversion functions +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In iconv.h +.Ft iconv_t +.Fn iconv_open "const char *dstname" "const char *srcname" +.Ft int +.Fn iconv_close "iconv_t cd" +.Ft size_t +.Fn iconv "iconv_t cd" "const char ** restrict src" "size_t * restrict srcleft" "char ** restrict dst" "size_t * restrict dstleft" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn iconv_open +function opens a converter from the codeset +.Fa srcname +to the codeset +.Fa dstname +and returns its descriptor. +.Pp +The +.Fn iconv_close +function closes the specified converter +.Fa cd . +.Pp +The +.Fn iconv +function converts the string in the buffer +.Fa *src +of length +.Fa *srcleft +bytes and stores the converted string in the buffer +.Fa *dst +of size +.Fa *dstleft +bytes. +After calling +.Fn iconv , +the values pointed to by +.Fa src , +.Fa srcleft , +.Fa dst , +and +.Fa dstleft +are updated as follows: +.Bl -tag -width 01234567 -offset indent +.It Fa *src +Pointer to the byte just after the last character fetched. +.It Fa *srcleft +Number of remaining bytes in the source buffer. +.It Fa *dst +Pointer to the byte just after the last character stored. +.It Fa *dstleft +Number of remainder bytes in the destination buffer. +.El +.Pp +If the string pointed to by +.Fa *src +contains a byte sequence which is not a valid character in the source +codeset, the conversion stops just after the last successful conversion. +If the output buffer is too small to store the converted +character, the conversion also stops in the same way. +In these cases, the values pointed to by +.Fa src , +.Fa srcleft , +.Fa dst , +and +.Fa dstleft +are updated to the state just after the last successful conversion. +.Pp +If the string pointed to by +.Fa *src +contains a character which is valid under the source codeset but +can not be converted to the destination codeset, +the character is replaced by an +.Dq invalid character +which depends on the destination codeset, e.g., +.Sq \&? , +and the conversion is continued. +.Fn iconv +returns the number of such +.Dq invalid conversions . +.Pp +If the source and/or destination codesets are stateful, +.Fn iconv +places these into their initial state. +There are two special cases of +.Fn iconv : +.Bl -enum -offset indent +.It +If both +.Fa dst +and +.Fa *dst +are +.No non- Ns Dv NULL , +.Fn iconv +stores the shift sequence for the destination switching to the initial state +in the buffer pointed to by +.Fa *dst . +The buffer size is specified by the value pointed to by +.Fa dstleft +as above. +.Fn iconv +will fail if the buffer is too small to store the shift sequence. +.It +On the other hand, +.Fa dst +or +.Fa *dst +may be +.Dv NULL . +In this case, the shift sequence for the destination switching +to the initial state is discarded. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +Upon successful completion of +.Fn iconv_open , +it returns a conversion descriptor. +Otherwise, +.Fn iconv_open +returns (iconv_t)\-1 and sets +.Va errno +to indicate the error. +.Pp +Upon successful completion of +.Fn iconv_close , +it returns 0. +Otherwise, +.Fn iconv_close +returns \-1 and sets errno to indicate the error. +.Pp +Upon successful completion of +.Fn iconv , +it returns the number of +.Dq invalid +conversions. +Otherwise, +.Fn iconv +returns (size_t)\-1 and sets errno to indicate the error. +.\" ---------------------------------------------------------------------- +.Sh ERRORS +The +.Fn iconv_open +function may cause an error in the following cases: +.Bl -tag -width Er +.It Bq Er EINVAL +There is no converter specified by +.Fa srcname +and +.Fa dstname . +.It Bq Er ENOMEM +Memory is exhausted. +.El +.Pp +The +.Fn iconv_close +function may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EBADF +The conversion descriptor specified by +.Fa cd +is invalid. +.El +.Pp +The +.Fn iconv +function may cause an error in the following cases: +.Bl -tag -width Er +.It Bq Er E2BIG +The output buffer pointed to by +.Fa *dst +is too small to store the result string. +.It Bq Er EBADF +The conversion descriptor specified by +.Fa cd +is invalid. +.It Bq Er EILSEQ +The string pointed to by +.Fa *src +contains a byte sequence which does not describe a valid character of +the source codeset. +.It Bq Er EINVAL +The string pointed to by +.Fa *src +terminates with an incomplete character or shift sequence. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr iconv 1 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +.Fn iconv_open , +.Fn iconv_close , +and +.Fn iconv +conform to +.St -p1003.1-2001 . +.\" ---------------------------------------------------------------------- +.Sh BUGS +If +.Fn iconv +is aborted due to the occurrence of some error, +the +.Dq invalid conversion +count mentioned above is unfortunately lost. diff --git a/lib/nbsd_libc/iconv/iconv.c b/lib/nbsd_libc/iconv/iconv.c new file mode 100644 index 000000000..5fd542e3b --- /dev/null +++ b/lib/nbsd_libc/iconv/iconv.c @@ -0,0 +1,188 @@ +/* $NetBSD: iconv.c,v 1.12 2009/08/05 15:17:02 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: iconv.c,v 1.12 2009/08/05 15:17:02 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include + +#ifdef __weak_alias +__weak_alias(iconv, _iconv) +__weak_alias(iconv_open, _iconv_open) +__weak_alias(iconv_close, _iconv_close) +#endif + +#ifdef HAVE_CITRUS +#include +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_esdb.h" +#include "citrus_hash.h" +#include "citrus_iconv.h" + +#define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1) + + +iconv_t +iconv_open(const char *out, const char *in) +{ + int ret; + struct _citrus_iconv *handle; + + ret = _citrus_iconv_open(&handle, _PATH_ICONV, in, out); + if (ret) { + errno = ret == ENOENT? EINVAL : ret; + return ((iconv_t)-1); + } + + return ((iconv_t)(void *)handle); +} + +int +iconv_close(iconv_t handle) +{ + if (ISBADF(handle)) { + errno = EBADF; + return (-1); + } + + _citrus_iconv_close((struct _citrus_iconv *)(void *)handle); + + return (0); +} + +size_t +iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout) +{ + int err; + size_t ret; + + if (ISBADF(handle)) { + errno = EBADF; + return ((size_t)-1); + } + + err = _citrus_iconv_convert( + (struct _citrus_iconv *)(void *)handle, in, szin, out, szout, + 0, &ret); + if (err) { + errno = err; + ret = (size_t)-1; + } + + return (ret); +} + +size_t +__iconv(iconv_t handle, const char **in, size_t *szin, char **out, + size_t *szout, u_int32_t flags, size_t *invalids) +{ + int err; + size_t ret; + + if (ISBADF(handle)) { + errno = EBADF; + return ((size_t)-1); + } + + err = _citrus_iconv_convert( + (struct _citrus_iconv *)(void *)handle, in, szin, out, szout, + flags, &ret); + if (invalids) + *invalids = ret; + if (err) { + errno = err; + ret = (size_t)-1; + } + + return (ret); +} + +int +__iconv_get_list(char ***rlist, size_t *rsz) +{ + int ret; + + ret = _citrus_esdb_get_list(rlist, rsz); + if (ret) { + errno = ret; + return -1; + } + + return 0; +} + +void +__iconv_free_list(char **list, size_t sz) +{ + _citrus_esdb_free_list(list, sz); +} + +#else +iconv_t +/*ARGSUSED*/ +iconv_open(const char *in, const char *out) +{ + errno = EINVAL; + return ((iconv_t)-1); +} +int +/*ARGSUSED*/ +iconv_close(iconv_t handle) +{ + errno = EBADF; + return (-1); +} +size_t +/*ARGSUSED*/ +iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout) +{ + errno = EBADF; + return ((size_t)-1); +} +int +/*ARGSUSED*/ +__iconv_get_list(char ***rlist, size_t *rsz) +{ + errno = EINVAL; + return -1; +} +void +/*ARGSUSED*/ +__iconv_free_list(char **list, size_t sz) +{ +} +#endif diff --git a/lib/nbsd_libc/include/env.h b/lib/nbsd_libc/include/env.h new file mode 100644 index 000000000..20b6bf95a --- /dev/null +++ b/lib/nbsd_libc/include/env.h @@ -0,0 +1,65 @@ +/* $NetBSD: env.h,v 1.2 2010/11/14 22:04:36 tron Exp $ */ + +/*- + * Copyright (c) 2010 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matthias Scheler. + * + * 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 "reentrant.h" + +extern ssize_t __getenvslot(const char *name, size_t l_name, bool allocate); +extern char *__findenvvar(const char *name, size_t l_name); + +#ifdef _REENTRANT +extern bool __readlockenv(void); +extern bool __writelockenv(void); +extern bool __unlockenv(void); +#else +static __inline bool +__readlockenv(void) +{ + return true; +} + +static __inline bool +__writelockenv(void) +{ + return true; +} + +static __inline bool +__unlocklockenv(void) +{ + return true; +} +#endif + +extern char **environ; diff --git a/lib/nbsd_libc/include/extern.h b/lib/nbsd_libc/include/extern.h new file mode 100644 index 000000000..5bd012aa7 --- /dev/null +++ b/lib/nbsd_libc/include/extern.h @@ -0,0 +1,68 @@ +/* $NetBSD: extern.h,v 1.17 2009/10/21 01:07:45 snj Exp $ */ + +/* + * Copyright (c) 1997 Christos Zoulas. 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 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. + */ + +__BEGIN_DECLS +extern char *__minbrk; +int __getcwd(char *, size_t); +int __getlogin(char *, size_t); +int __setlogin(const char *); +void _resumecontext(void); +const char *__strerror(int , char *, size_t); +const char *__strsignal(int , char *, size_t); +char *__dtoa(double, int, int, int *, int *, char **); +void __freedtoa(char *); +int __sysctl(const int *, unsigned int, void *, size_t *, const void *, size_t); + +struct sigaction; +int __sigaction_sigtramp(int, const struct sigaction *, + struct sigaction *, const void *, int); + +#ifdef WIDE_DOUBLE +char *__hdtoa(double, const char *, int, int *, int *, char **); +char *__hldtoa(long double, const char *, int, int *, int *, char **); +char *__ldtoa(long double *, int, int, int *, int *, char **); +#endif + +struct syslog_data; +void syslog_ss(int, struct syslog_data *, const char *, ...) + __attribute__((__format__(__printf__,3,4))); +void vsyslog_ss(int, struct syslog_data *, const char *, _BSD_VA_LIST_); +void vsyslog_ss(int, struct syslog_data *, const char *, _BSD_VA_LIST_) + __attribute__((__format__(__printf__,3,0))); +void syslogp_ss(int, struct syslog_data *, const char *, const char *, + const char *, ...) __attribute__((__format__(__printf__,5,0))); +void vsyslogp_ss(int, struct syslog_data *, const char *, const char *, + const char *, _BSD_VA_LIST_) __attribute__((__format__(__printf__,5,0))); + +int snprintf_ss(char * __restrict, size_t, const char * __restrict, ...) + __attribute__((__format__(__printf__, 3, 4))); +int vsnprintf_ss(char * __restrict, size_t, const char * __restrict, + _BSD_VA_LIST_) __attribute__((__format__(__printf__, 3, 0))); + +void _malloc_prefork(void); +void _malloc_postfork(void); + +__END_DECLS diff --git a/lib/nbsd_libc/include/fd_setsize.h b/lib/nbsd_libc/include/fd_setsize.h new file mode 100644 index 000000000..235b1ad1c --- /dev/null +++ b/lib/nbsd_libc/include/fd_setsize.h @@ -0,0 +1,9 @@ +#ifndef _FD_SETSIZE_H +#define _FD_SETSIZE_H + +/* + * If you need a bigger FD_SETSIZE, this is NOT the place to set it. + * This file is a fallback for BIND ports which don't specify their own. + */ + +#endif /* _FD_SETSIZE_H */ diff --git a/lib/nbsd_libc/include/isc/assertions.h b/lib/nbsd_libc/include/isc/assertions.h new file mode 100644 index 000000000..7db7361d0 --- /dev/null +++ b/lib/nbsd_libc/include/isc/assertions.h @@ -0,0 +1,132 @@ +/* $NetBSD: assertions.h,v 1.5 2009/04/12 17:07:16 christos Exp $ */ + +/* + * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") + * Copyright (C) 1997-2001 Internet Software Consortium. + * + * Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +/* + * Id: assertions.h,v 1.5 2008/11/14 02:36:51 marka Exp + */ + +#ifndef ASSERTIONS_H +#define ASSERTIONS_H 1 + +typedef enum { + assert_require, assert_ensure, assert_insist, assert_invariant +} assertion_type; + +typedef void (*assertion_failure_callback)(const char *, int, assertion_type, + const char *, int); + +/* coverity[+kill] */ +extern assertion_failure_callback __assertion_failed; +void set_assertion_failure_callback(assertion_failure_callback f); +const char *assertion_type_to_text(assertion_type type); + +#if defined(CHECK_ALL) || defined(__COVERITY__) +#define CHECK_REQUIRE 1 +#define CHECK_ENSURE 1 +#define CHECK_INSIST 1 +#define CHECK_INVARIANT 1 +#endif + +#if defined(CHECK_NONE) && !defined(__COVERITY__) +#define CHECK_REQUIRE 0 +#define CHECK_ENSURE 0 +#define CHECK_INSIST 0 +#define CHECK_INVARIANT 0 +#endif + +#ifdef _DIAGNOSTIC +#ifndef CHECK_REQUIRE +#define CHECK_REQUIRE 1 +#endif + +#ifndef CHECK_ENSURE +#define CHECK_ENSURE 1 +#endif + +#ifndef CHECK_INSIST +#define CHECK_INSIST 1 +#endif + +#ifndef CHECK_INVARIANT +#define CHECK_INVARIANT 1 +#endif +#endif /* _DIAGNOSTIC */ + +#if CHECK_REQUIRE != 0 +#define REQUIRE(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_require, \ + #cond, 0), 0))) +#define REQUIRE_ERR(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_require, \ + #cond, 1), 0))) +#else +#define REQUIRE(cond) ((void) (cond)) +#define REQUIRE_ERR(cond) ((void) (cond)) +#endif /* CHECK_REQUIRE */ + +#if CHECK_ENSURE != 0 +#define ENSURE(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \ + #cond, 0), 0))) +#define ENSURE_ERR(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \ + #cond, 1), 0))) +#else +#define ENSURE(cond) ((void) (cond)) +#define ENSURE_ERR(cond) ((void) (cond)) +#endif /* CHECK_ENSURE */ + +#if CHECK_INSIST != 0 +#define INSIST(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \ + #cond, 0), 0))) +#define INSIST_ERR(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \ + #cond, 1), 0))) +#else +#if !defined(__lint__) +#define INSIST(cond) ((void) (cond)) +#define INSIST_ERR(cond) ((void) (cond)) +#else /* !__lint__ */ +#define INSIST(cond) +#define INSIST_ERR(cond) +#endif /* !__lint__ */ +#endif /* CHECK_INSIST */ + +#if CHECK_INVARIANT != 0 +#define INVARIANT(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \ + #cond, 0), 0))) +#define INVARIANT_ERR(cond) \ + ((void) ((cond) || \ + ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \ + #cond, 1), 0))) +#else +#define INVARIANT(cond) ((void) (cond)) +#define INVARIANT_ERR(cond) ((void) (cond)) +#endif /* CHECK_INVARIANT */ +#endif /* ASSERTIONS_H */ +/*! \file */ diff --git a/lib/nbsd_libc/include/isc/dst.h b/lib/nbsd_libc/include/isc/dst.h new file mode 100644 index 000000000..5537e3d73 --- /dev/null +++ b/lib/nbsd_libc/include/isc/dst.h @@ -0,0 +1,170 @@ +/* $NetBSD: dst.h,v 1.1.1.4 2009/04/12 16:35:44 christos Exp $ */ + +#ifndef DST_H +#define DST_H + +#ifndef HAS_DST_KEY +typedef struct dst_key { + char *dk_key_name; /*%< name of the key */ + int dk_key_size; /*%< this is the size of the key in bits */ + int dk_proto; /*%< what protocols this key can be used for */ + int dk_alg; /*%< algorithm number from key record */ + u_int32_t dk_flags; /*%< and the flags of the public key */ + u_int16_t dk_id; /*%< identifier of the key */ +} DST_KEY; +#endif /* HAS_DST_KEY */ +/* + * do not taint namespace + */ +#define dst_bsafe_init __dst_bsafe_init +#define dst_buffer_to_key __dst_buffer_to_key +#define dst_check_algorithm __dst_check_algorithm +#define dst_compare_keys __dst_compare_keys +#define dst_cylink_init __dst_cylink_init +#define dst_dnskey_to_key __dst_dnskey_to_key +#define dst_eay_dss_init __dst_eay_dss_init +#define dst_free_key __dst_free_key +#define dst_generate_key __dst_generate_key +#define dst_hmac_md5_init __dst_hmac_md5_init +#define dst_init __dst_init +#define dst_key_to_buffer __dst_key_to_buffer +#define dst_key_to_dnskey __dst_key_to_dnskey +#define dst_read_key __dst_read_key +#define dst_rsaref_init __dst_rsaref_init +#define dst_s_build_filename __dst_s_build_filename +#define dst_s_calculate_bits __dst_s_calculate_bits +#define dst_s_conv_bignum_b64_to_u8 __dst_s_conv_bignum_b64_to_u8 +#define dst_s_conv_bignum_u8_to_b64 __dst_s_conv_bignum_u8_to_b64 +#define dst_s_dns_key_id __dst_s_dns_key_id +#define dst_s_dump __dst_s_dump +#define dst_s_filename_length __dst_s_filename_length +#define dst_s_fopen __dst_s_fopen +#define dst_s_get_int16 __dst_s_get_int16 +#define dst_s_get_int32 __dst_s_get_int32 +#define dst_s_id_calc __dst_s_id_calc +#define dst_s_put_int16 __dst_s_put_int16 +#define dst_s_put_int32 __dst_s_put_int32 +#define dst_s_quick_random __dst_s_quick_random +#define dst_s_quick_random_set __dst_s_quick_random_set +#define dst_s_random __dst_s_random +#define dst_s_semi_random __dst_s_semi_random +#define dst_s_verify_str __dst_s_verify_str +#define dst_sig_size __dst_sig_size +#define dst_sign_data __dst_sign_data +#define dst_verify_data __dst_verify_data +#define dst_write_key __dst_write_key + +/* + * DST Crypto API defintions + */ +void dst_init(void); +int dst_check_algorithm(const int); + + +int dst_sign_data(const int, /*!< specifies INIT/UPDATE/FINAL/ALL */ + DST_KEY *, /*!< the key to use */ + void **, /*!< pointer to state structure */ + const u_char *, /*!< data to be signed */ + const int, /*!< length of input data */ + u_char *, /*!< buffer to write signature to */ + const int); /*!< size of output buffer */ +int dst_verify_data(const int, /*!< specifies INIT/UPDATE/FINAL/ALL */ + DST_KEY *, /*!< the key to use */ + void **, /*!< pointer to state structure */ + const u_char *, /*!< data to be verified */ + const int, /*!< length of input data */ + const u_char *, /*!< buffer containing signature */ + const int); /*!< length of signature */ +DST_KEY *dst_read_key(const char *, /*!< name of key */ + const u_int16_t, /*!< key tag identifier */ + const int, /*!< key algorithm */ + const int); /*!< Private/PublicKey wanted */ +int dst_write_key(const DST_KEY *, /*!< key to write out */ + const int); /*!< Public/Private */ +DST_KEY *dst_dnskey_to_key(const char *, /*!< KEY record name */ + const u_char *, /*!< KEY RDATA */ + const int); /*!< size of input buffer */ +int dst_key_to_dnskey(const DST_KEY *, /*!< key to translate */ + u_char *, /*!< output buffer */ + const int); /*!< size of out_storage */ +DST_KEY *dst_buffer_to_key(const char *, /*!< name of the key */ + const int, /*!< algorithm */ + const int, /*!< dns flags */ + const int, /*!< dns protocol */ + const u_char *, /*!< key in dns wire fmt */ + const int); /*!< size of key */ +int dst_key_to_buffer(DST_KEY *, u_char *, int); + +DST_KEY *dst_generate_key(const char *, /*!< name of new key */ + const int, /*!< key algorithm to generate */ + const int, /*!< size of new key */ + const int, /*!< alg dependent parameter */ + const int, /*!< key DNS flags */ + const int); /*!< key DNS protocol */ +DST_KEY *dst_free_key(DST_KEY *); +int dst_compare_keys(const DST_KEY *, const DST_KEY *); + +int dst_sig_size(DST_KEY *); + + +/* support for dns key tags/ids */ +u_int16_t dst_s_dns_key_id(const u_char *, const int); +u_int16_t dst_s_id_calc(const u_char *, const int); + +/* Used by callers as well as by the library. */ +#define RAW_KEY_SIZE 8192 /*%< large enough to store any key */ +/* DST_API control flags */ +/* These are used used in functions dst_sign_data and dst_verify_data */ +#define SIG_MODE_INIT 1 /*%< initialize digest */ +#define SIG_MODE_UPDATE 2 /*%< add data to digest */ +#define SIG_MODE_FINAL 4 /*%< generate/verify signature */ +#define SIG_MODE_ALL (SIG_MODE_INIT|SIG_MODE_UPDATE|SIG_MODE_FINAL) + +/* Flags for dst_read_private_key() */ +#define DST_FORCE_READ 0x1000000 +#define DST_CAN_SIGN 0x010F +#define DST_NO_AUTHEN 0x8000 +#define DST_EXTEND_FLAG 0x1000 +#define DST_STANDARD 0 +#define DST_PRIVATE 0x2000000 +#define DST_PUBLIC 0x4000000 +#define DST_RAND_SEMI 1 +#define DST_RAND_STD 2 +#define DST_RAND_KEY 3 +#define DST_RAND_DSS 4 + + +/* DST algorithm codes */ +#define KEY_RSA 1 +#define KEY_DH 2 +#define KEY_DSA 3 +#define KEY_PRIVATE 254 +#define KEY_EXPAND 255 +#define KEY_HMAC_MD5 157 +#define KEY_HMAC_SHA1 158 +#define UNKNOWN_KEYALG 0 +#define DST_MAX_ALGS KEY_HMAC_SHA1 + +/* DST constants to locations in KEY record changes in new KEY record */ +#define DST_FLAGS_SIZE 2 +#define DST_KEY_PROT 2 +#define DST_KEY_ALG 3 +#define DST_EXT_FLAG 4 +#define DST_KEY_START 4 + +#ifndef SIGN_F_NOKEY +#define SIGN_F_NOKEY 0xC000 +#endif + +/* error codes from dst routines */ +#define SIGN_INIT_FAILURE (-23) +#define SIGN_UPDATE_FAILURE (-24) +#define SIGN_FINAL_FAILURE (-25) +#define VERIFY_INIT_FAILURE (-26) +#define VERIFY_UPDATE_FAILURE (-27) +#define VERIFY_FINAL_FAILURE (-28) +#define MISSING_KEY_OR_SIGNATURE (-30) +#define UNSUPPORTED_KEYALG (-31) + +#endif /* DST_H */ +/*! \file */ diff --git a/lib/nbsd_libc/include/isc/eventlib.h b/lib/nbsd_libc/include/isc/eventlib.h new file mode 100644 index 000000000..4e9cc2e40 --- /dev/null +++ b/lib/nbsd_libc/include/isc/eventlib.h @@ -0,0 +1,206 @@ +/* $NetBSD: eventlib.h,v 1.3 2009/04/12 17:07:16 christos Exp $ */ + +/* + * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") + * Copyright (C) 1995-1999, 2001, 2003 Internet Software Consortium. + * + * Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +/* eventlib.h - exported interfaces for eventlib + * vix 09sep95 [initial] + * + * Id: eventlib.h,v 1.7 2008/11/14 02:36:51 marka Exp + */ + +#ifndef _EVENTLIB_H +#define _EVENTLIB_H + +#include +#include +#include +#include + +#ifndef __P +# define __EVENTLIB_P_DEFINED +# ifdef __STDC__ +# define __P(x) x +# else +# define __P(x) () +# endif +#endif + +/* In the absence of branded types... */ +typedef struct { void *opaque; } evConnID; +typedef struct { void *opaque; } evFileID; +typedef struct { void *opaque; } evStreamID; +typedef struct { void *opaque; } evTimerID; +typedef struct { void *opaque; } evWaitID; +typedef struct { void *opaque; } evContext; +typedef struct { void *opaque; } evEvent; + +#define evInitID(id) ((id)->opaque = NULL) +#define evTestID(id) ((id).opaque != NULL) + +typedef void (*evConnFunc)__P((evContext, void *, int, const void *, int, + const void *, int)); +typedef void (*evFileFunc)__P((evContext, void *, int, int)); +typedef void (*evStreamFunc)__P((evContext, void *, int, int)); +typedef void (*evTimerFunc)__P((evContext, void *, + struct timespec, struct timespec)); +typedef void (*evWaitFunc)__P((evContext, void *, const void *)); + +typedef struct { unsigned char mask[256/8]; } evByteMask; +#define EV_BYTEMASK_BYTE(b) ((b) / 8) +#define EV_BYTEMASK_MASK(b) (1 << ((b) % 8)) +#define EV_BYTEMASK_SET(bm, b) \ + ((bm).mask[EV_BYTEMASK_BYTE(b)] |= EV_BYTEMASK_MASK(b)) +#define EV_BYTEMASK_CLR(bm, b) \ + ((bm).mask[EV_BYTEMASK_BYTE(b)] &= ~EV_BYTEMASK_MASK(b)) +#define EV_BYTEMASK_TST(bm, b) \ + ((bm).mask[EV_BYTEMASK_BYTE(b)] & EV_BYTEMASK_MASK(b)) + +#define EV_POLL 1 +#define EV_WAIT 2 +#define EV_NULL 4 + +#define EV_READ 1 +#define EV_WRITE 2 +#define EV_EXCEPT 4 + +#define EV_WASNONBLOCKING 8 /* Internal library use. */ + +/* eventlib.c */ +#define evCreate __evCreate +#define evSetDebug __evSetDebug +#define evDestroy __evDestroy +#define evGetNext __evGetNext +#define evDispatch __evDispatch +#define evDrop __evDrop +#define evMainLoop __evMainLoop +#define evHighestFD __evHighestFD +#define evGetOption __evGetOption +#define evSetOption __evSetOption + +int evCreate __P((evContext *)); +void evSetDebug __P((evContext, int, FILE *)); +int evDestroy __P((evContext)); +int evGetNext __P((evContext, evEvent *, int)); +int evDispatch __P((evContext, evEvent)); +void evDrop __P((evContext, evEvent)); +int evMainLoop __P((evContext)); +int evHighestFD __P((evContext)); +int evGetOption __P((evContext *, const char *, int *)); +int evSetOption __P((evContext *, const char *, int)); + +/* ev_connects.c */ +#define evListen __evListen +#define evConnect __evConnect +#define evCancelConn __evCancelConn +#define evHold __evHold +#define evUnhold __evUnhold +#define evTryAccept __evTryAccept + +int evListen __P((evContext, int, int, evConnFunc, void *, evConnID *)); +int evConnect __P((evContext, int, const void *, int, + evConnFunc, void *, evConnID *)); +int evCancelConn __P((evContext, evConnID)); +int evHold __P((evContext, evConnID)); +int evUnhold __P((evContext, evConnID)); +int evTryAccept __P((evContext, evConnID, int *)); + +/* ev_files.c */ +#define evSelectFD __evSelectFD +#define evDeselectFD __evDeselectFD + +int evSelectFD __P((evContext, int, int, evFileFunc, void *, evFileID *)); +int evDeselectFD __P((evContext, evFileID)); + +/* ev_streams.c */ +#define evConsIovec __evConsIovec +#define evWrite __evWrite +#define evRead __evRead +#define evTimeRW __evTimeRW +#define evUntimeRW __evUntimeRW +#define evCancelRW __evCancelRW + +struct iovec evConsIovec __P((void *, size_t)); +int evWrite __P((evContext, int, const struct iovec *, int, + evStreamFunc func, void *, evStreamID *)); +int evRead __P((evContext, int, const struct iovec *, int, + evStreamFunc func, void *, evStreamID *)); +int evTimeRW __P((evContext, evStreamID, evTimerID timer)); +int evUntimeRW __P((evContext, evStreamID)); +int evCancelRW __P((evContext, evStreamID)); + +/* ev_timers.c */ +#define evConsTime __evConsTime +#define evAddTime __evAddTime +#define evSubTime __evSubTime +#define evCmpTime __evCmpTime +#define evTimeSpec __evTimeSpec +#define evTimeVal __evTimeVal + +#define evNowTime __evNowTime +#define evUTCTime __evUTCTime +#define evLastEventTime __evLastEventTime +#define evSetTimer __evSetTimer +#define evClearTimer __evClearTimer +#define evConfigTimer __evConfigTimer +#define evResetTimer __evResetTimer +#define evSetIdleTimer __evSetIdleTimer +#define evClearIdleTimer __evClearIdleTimer +#define evResetIdleTimer __evResetIdleTimer +#define evTouchIdleTimer __evTouchIdleTimer + +struct timespec evConsTime __P((time_t sec, long nsec)); +struct timespec evAddTime __P((struct timespec, struct timespec)); +struct timespec evSubTime __P((struct timespec, struct timespec)); +struct timespec evNowTime __P((void)); +struct timespec evUTCTime __P((void)); +struct timespec evLastEventTime __P((evContext)); +struct timespec evTimeSpec __P((struct timeval)); +struct timeval evTimeVal __P((struct timespec)); +int evCmpTime __P((struct timespec, struct timespec)); +int evSetTimer __P((evContext, evTimerFunc, void *, struct timespec, + struct timespec, evTimerID *)); +int evClearTimer __P((evContext, evTimerID)); +int evConfigTimer __P((evContext, evTimerID, const char *param, + int value)); +int evResetTimer __P((evContext, evTimerID, evTimerFunc, void *, + struct timespec, struct timespec)); +int evSetIdleTimer __P((evContext, evTimerFunc, void *, struct timespec, + evTimerID *)); +int evClearIdleTimer __P((evContext, evTimerID)); +int evResetIdleTimer __P((evContext, evTimerID, evTimerFunc, void *, + struct timespec)); +int evTouchIdleTimer __P((evContext, evTimerID)); + +/* ev_waits.c */ +#define evWaitFor __evWaitFor +#define evDo __evDo +#define evUnwait __evUnwait +#define evDefer __evDefer + +int evWaitFor __P((evContext, const void *, evWaitFunc, void *, evWaitID *)); +int evDo __P((evContext, const void *)); +int evUnwait __P((evContext, evWaitID)); +int evDefer __P((evContext, evWaitFunc, void *)); + +#ifdef __EVENTLIB_P_DEFINED +# undef __P +#endif + +#endif /*_EVENTLIB_H*/ + +/*! \file */ diff --git a/lib/nbsd_libc/include/isc/heap.h b/lib/nbsd_libc/include/isc/heap.h new file mode 100644 index 000000000..30cec98f2 --- /dev/null +++ b/lib/nbsd_libc/include/isc/heap.h @@ -0,0 +1,51 @@ +/* $NetBSD: heap.h,v 1.1.1.4 2009/04/12 16:35:44 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1997,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +typedef int (*heap_higher_priority_func)(void *, void *); +typedef void (*heap_index_func)(void *, int); +typedef void (*heap_for_each_func)(void *, void *); + +typedef struct heap_context { + int array_size; + int array_size_increment; + int heap_size; + void **heap; + heap_higher_priority_func higher_priority; + heap_index_func index; +} *heap_context; + +#define heap_new __heap_new +#define heap_free __heap_free +#define heap_insert __heap_insert +#define heap_delete __heap_delete +#define heap_increased __heap_increased +#define heap_decreased __heap_decreased +#define heap_element __heap_element +#define heap_for_each __heap_for_each + +heap_context heap_new(heap_higher_priority_func, heap_index_func, int); +int heap_free(heap_context); +int heap_insert(heap_context, void *); +int heap_delete(heap_context, int); +int heap_increased(heap_context, int); +int heap_decreased(heap_context, int); +void * heap_element(heap_context, int); +int heap_for_each(heap_context, heap_for_each_func, void *); + +/*! \file */ diff --git a/lib/nbsd_libc/include/isc/list.h b/lib/nbsd_libc/include/isc/list.h new file mode 100644 index 000000000..46f2e79be --- /dev/null +++ b/lib/nbsd_libc/include/isc/list.h @@ -0,0 +1,120 @@ +/* $NetBSD: list.h,v 1.5 2009/04/12 17:07:16 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1997,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +#ifndef LIST_H +#define LIST_H 1 +#include + +#define LIST(type) struct { type *head, *tail; } +#define INIT_LIST(list) \ + do { (list).head = NULL; (list).tail = NULL; } while (/*CONSTCOND*/0) + +#define LINK(type) struct { type *prev, *next; } +#define INIT_LINK_TYPE(elt, link, type) \ + do { \ + (elt)->link.prev = (type *)(-1); \ + (elt)->link.next = (type *)(-1); \ + } while (/*CONSTCOND*/0) +#define INIT_LINK(elt, link) \ + INIT_LINK_TYPE(elt, link, void) +#define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1) && \ + (void *)((elt)->link.next) != (void *)(-1)) + +#define HEAD(list) ((list).head) +#define TAIL(list) ((list).tail) +#define EMPTY(list) ((list).head == NULL) + +#define PREPEND(list, elt, link) \ + do { \ + INSIST(!LINKED(elt, link));\ + if ((list).head != NULL) \ + (list).head->link.prev = (elt); \ + else \ + (list).tail = (elt); \ + (elt)->link.prev = NULL; \ + (elt)->link.next = (list).head; \ + (list).head = (elt); \ + } while (/*CONSTCOND*/0) + +#define APPEND(list, elt, link) \ + do { \ + INSIST(!LINKED(elt, link));\ + if ((list).tail != NULL) \ + (list).tail->link.next = (elt); \ + else \ + (list).head = (elt); \ + (elt)->link.prev = (list).tail; \ + (elt)->link.next = NULL; \ + (list).tail = (elt); \ + } while (/*CONSTCOND*/0) + +#define UNLINK_TYPE(list, elt, link, type) \ + do { \ + INSIST(LINKED(elt, link));\ + if ((elt)->link.next != NULL) \ + (elt)->link.next->link.prev = (elt)->link.prev; \ + else { \ + INSIST((list).tail == (elt)); \ + (list).tail = (elt)->link.prev; \ + } \ + if ((elt)->link.prev != NULL) \ + (elt)->link.prev->link.next = (elt)->link.next; \ + else { \ + INSIST((list).head == (elt)); \ + (list).head = (elt)->link.next; \ + } \ + INIT_LINK_TYPE(elt, link, type); \ + } while (/*CONSTCOND*/0) +#define UNLINK(list, elt, link) \ + UNLINK_TYPE(list, elt, link, void) + +#define PREV(elt, link) ((elt)->link.prev) +#define NEXT(elt, link) ((elt)->link.next) + +#define INSERT_BEFORE(list, before, elt, link) \ + do { \ + INSIST(!LINKED(elt, link));\ + if ((before)->link.prev == NULL) \ + PREPEND(list, elt, link); \ + else { \ + (elt)->link.prev = (before)->link.prev; \ + (before)->link.prev = (elt); \ + (elt)->link.prev->link.next = (elt); \ + (elt)->link.next = (before); \ + } \ + } while (/*CONSTCOND*/0) + +#define INSERT_AFTER(list, after, elt, link) \ + do { \ + INSIST(!LINKED(elt, link));\ + if ((after)->link.next == NULL) \ + APPEND(list, elt, link); \ + else { \ + (elt)->link.next = (after)->link.next; \ + (after)->link.next = (elt); \ + (elt)->link.next->link.prev = (elt); \ + (elt)->link.prev = (after); \ + } \ + } while (/*CONSTCOND*/0) + +#define ENQUEUE(list, elt, link) APPEND(list, elt, link) +#define DEQUEUE(list, elt, link) UNLINK(list, elt, link) + +#endif /* LIST_H */ +/*! \file */ diff --git a/lib/nbsd_libc/include/isc/memcluster.h b/lib/nbsd_libc/include/isc/memcluster.h new file mode 100644 index 000000000..3c1b489cb --- /dev/null +++ b/lib/nbsd_libc/include/isc/memcluster.h @@ -0,0 +1,52 @@ +/* $NetBSD: memcluster.h,v 1.1.1.4 2009/04/12 16:35:44 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1997,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +#ifndef MEMCLUSTER_H +#define MEMCLUSTER_H + +#include + +#define meminit __meminit +#ifdef MEMCLUSTER_DEBUG +#define memget(s) __memget_debug(s, __FILE__, __LINE__) +#define memput(p, s) __memput_debug(p, s, __FILE__, __LINE__) +#else /*MEMCLUSTER_DEBUG*/ +#ifdef MEMCLUSTER_RECORD +#define memget(s) __memget_record(s, __FILE__, __LINE__) +#define memput(p, s) __memput_record(p, s, __FILE__, __LINE__) +#else /*MEMCLUSTER_RECORD*/ +#define memget __memget +#define memput __memput +#endif /*MEMCLUSTER_RECORD*/ +#endif /*MEMCLUSTER_DEBUG*/ +#define memstats __memstats +#define memactive __memactive + +int meminit(size_t, size_t); +void * __memget(size_t); +void __memput(void *, size_t); +void * __memget_debug(size_t, const char *, int); +void __memput_debug(void *, size_t, const char *, int); +void * __memget_record(size_t, const char *, int); +void __memput_record(void *, size_t, const char *, int); +void memstats(FILE *); +int memactive(void); + +#endif /* MEMCLUSTER_H */ +/*! \file */ diff --git a/lib/nbsd_libc/include/namespace.h b/lib/nbsd_libc/include/namespace.h new file mode 100644 index 000000000..c4bb6bfc0 --- /dev/null +++ b/lib/nbsd_libc/include/namespace.h @@ -0,0 +1,851 @@ +/* $NetBSD: namespace.h,v 1.146 2010/12/16 18:38:06 christos Exp $ */ + +/*- + * Copyright (c) 1997-2004 The NetBSD Foundation, Inc. + * 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 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. + */ + +#ifndef _NAMESPACE_H_ +#define _NAMESPACE_H_ + +#include + +#ifndef __lint__ +#define aio_suspend _aio_suspend +#define brk _brk +#define catclose _catclose +#define catgets _catgets +#define catopen _catopen +#define daylight _daylight +#define difftime _difftime +#define err _err +#define errx _errx +#ifdef _REENTRANT +#define fileno _fileno +#endif /* _REENTRANT */ +#define fork _fork +#define fseeko _fseeko +#define ftello _ftello +#define getcontext _getcontext +#define getenv_r _getenv_r +#define imaxabs _imaxabs +#define imaxdiv _imaxdiv +#define inet_aton _inet_aton +#define inet_pton _inet_pton +#define pipe _pipe +#define sbrk _sbrk +#define strerror_r _strerror_r +#define strlcat _strlcat +#define strlcpy _strlcpy +#define strtof _strtof +#define strtoimax _strtoimax +#define strtold _strtold +#define strtoll _strtoll +#define strtoull _strtoull +#define strtoumax _strtoumax +#define sys_errlist _sys_errlist +#define sys_nerr _sys_nerr +#define sys_siglist _sys_siglist +#define sys_nsig _sys_nsig +#define sysconf __sysconf +#define verr _verr +#define verrx _verrx +#define vwarn _vwarn +#define vwarnx _vwarnx +#define warn _warn +#define warnx _warnx + +#ifdef __weak_alias +#define MD2Data _MD2Data +#define MD2End _MD2End +#define MD2FileChunk _MD2FileChunk +#define MD2File _MD2File +#define MD2Final _MD2Final +#define MD2Init _MD2Init +#define MD2Transform _MD2Transform +#define MD2Update _MD2Update +#define MD4Data _MD4Data +#define MD4End _MD4End +#define MD4FileChunk _MD4FileChunk +#define MD4File _MD4File +#define MD4Final _MD4Final +#define MD4Init _MD4Init +#define MD4Transform _MD4Transform +#define MD4Update _MD4Update +#define MD5Data _MD5Data +#define MD5End _MD5End +#define MD5FileChunk _MD5FileChunk +#define MD5File _MD5File +#define MD5Final _MD5Final +#define MD5Init _MD5Init +#define MD5Transform _MD5Transform +#define MD5Update _MD5Update +#define RMD160Data _RMD160Data +#define RMD160End _RMD160End +#define RMD160FileChunk _RMD160FileChunk +#define RMD160File _RMD160File +#define RMD160Final _RMD160Final +#define RMD160Init _RMD160Init +#define RMD160Transform _RMD160Transform +#define RMD160Update _RMD160Update +#define SHA1Data _SHA1Data +#define SHA1End _SHA1End +#define SHA1FileChunk _SHA1FileChunk +#define SHA1File _SHA1File +#define SHA1Final _SHA1Final +#define SHA1Init _SHA1Init +#define SHA1Transform _SHA1Transform +#define SHA1Update _SHA1Update +#define SHA224_Data _SHA224_Data +#define SHA224_End _SHA224_End +#define SHA224_FileChunk _SHA224_FileChunk +#define SHA224_File _SHA224_File +#define SHA224_Final _SHA224_Final +#define SHA224_Init _SHA224_Init +#define SHA224_Transform _SHA224_Transform +#define SHA224_Update _SHA224_Update +#define SHA256_Data _SHA256_Data +#define SHA256_End _SHA256_End +#define SHA256_FileChunk _SHA256_FileChunk +#define SHA256_File _SHA256_File +#define SHA256_Final _SHA256_Final +#define SHA256_Init _SHA256_Init +#define SHA256_Transform _SHA256_Transform +#define SHA256_Update _SHA256_Update +#define SHA384_Data _SHA384_Data +#define SHA384_End _SHA384_End +#define SHA384_FileChunk _SHA384_FileChunk +#define SHA384_File _SHA384_File +#define SHA384_Final _SHA384_Final +#define SHA384_Init _SHA384_Init +#define SHA384_Transform _SHA384_Transform +#define SHA384_Update _SHA384_Update +#define SHA512_Data _SHA512_Data +#define SHA512_End _SHA512_End +#define SHA512_FileChunk _SHA512_FileChunk +#define SHA512_File _SHA512_File +#define SHA512_Final _SHA512_Final +#define SHA512_Init _SHA512_Init +#define SHA512_Transform _SHA512_Transform +#define SHA512_Update _SHA512_Update +#define a64l _a64l +#define adjtime _adjtime +#define alarm _alarm +#define alphasort _alphasort +#define arc4random _arc4random +#define asctime_r _asctime_r +#define asprintf _asprintf +#define atoll _atoll +#define authnone_create _authnone_create +#define authunix_create _authunix_create +#define authunix_create_default _authunix_create_default +#define basename _basename +#define bindresvport _bindresvport +#define bindresvport_sa _bindresvport_sa +#define bm_comp _bm_comp +#define bm_exec _bm_exec +#define bm_free _bm_free +#define callrpc _callrpc +#define cdbr_close _cdbr_close +#define cdbr_find _cdbr_find +#define cdbr_get _cdbr_get +#define cdbr_open _cdbr_open +#define cdbw_close _cdbw_close +#define cdbw_open _cdbw_open +#define cdbw_put _cdbw_put +#define cdbw_put_data _cdbw_put_data +#define cdbw_put_key _cdbw_put_key +#define cdbw_output _cdbw_output +#define cfgetispeed _cfgetispeed +#define cfgetospeed _cfgetospeed +#define cfmakeraw _cfmakeraw +#define cfsetispeed _cfsetispeed +#define cfsetospeed _cfsetospeed +#define cfsetspeed _cfsetspeed +#define cgetcap _cgetcap +#define cgetclose _cgetclose +#define cgetent _cgetent +#define cgetfirst _cgetfirst +#define cgetmatch _cgetmatch +#define cgetnext _cgetnext +#define cgetnum _cgetnum +#define cgetset _cgetset +#define cgetstr _cgetstr +#define cgetustr _cgetustr +#define clnt_broadcast _clnt_broadcast +#define clnt_create _clnt_create +#define clnt_create_vers _clnt_create_vers +#define clnt_dg_create _clnt_dg_create +#define clnt_pcreateerror _clnt_pcreateerror +#define clnt_perrno _clnt_perrno +#define clnt_perror _clnt_perror +#define clnt_raw_create _clnt_raw_create +#define clnt_tli_create _clnt_tli_create +#define clnt_tp_create _clnt_tp_create +#define clnt_spcreateerror _clnt_spcreateerror +#define clnt_sperrno _clnt_sperrno +#define clnt_sperror _clnt_sperror +#define clnt_vc_create _clnt_vc_create +#define clntraw_create _clntraw_create +#define clnttcp_create _clnttcp_create +#define clntudp_bufcreate _clntudp_bufcreate +#define clntudp_create _clntudp_create +#define clock_gettime _clock_gettime +#define clock_getres _clock_getres +#define clock_settime _clock_settime +#define closedir _closedir +#define closelog _closelog +#define closelog_r _closelog_r +#define confstr _confstr +#define csetexpandtc _csetexpandtc +#define ctermid _ctermid +#define ctime_r _ctime_r +#define ctime_rz _ctime_rz +#define daemon _daemon +#define dbopen _dbopen +#define devname _devname +#define difftime _difftime +#define dirname _dirname +#define dn_expand _dn_expand +#define drand48 _drand48 +#define endfsent _endfsent +#define endgrent _endgrent +#define endhostent _endhostent +#define endnetconfig _endnetconfig +#define endnetent _endnetent +#define endnetgrent _endnetgrent +#define endnetpath _endnetpath +#define endprotoent _endprotoent +#define endprotoent_r _endprotoent_r +#define endpwent _endpwent +#define endrpcent _endrpcent +#define endservent _endservent +#define endservent_r _endservent_r +#define endttyent _endttyent +#define endusershell _endusershell +#define erand48 _erand48 +#define ether_aton _ether_aton +#define ether_hostton _ether_hostton +#define ether_line _ether_line +#define ether_ntoa _ether_ntoa +#define ether_ntohost _ether_ntohost +#define execl _execl +#define execle _execle +#define execlp _execlp +#define execv _execv +#define execvp _execvp +#define fdopen _fdopen +#define fgetln _fgetln +#define fgetwln _fgetwln +#define fhstatvfs _fhstatvfs +#define flockfile _flockfile +#define ftrylockfile _ftrylockfile +#define funlockfile _funlockfile +#define fnmatch _fnmatch +#define fparseln _fparseln +#define fpgetmask _fpgetmask +#define fpgetround _fpgetround +#define fpgetsticky _fpgetsticky +#define fpsetmask _fpsetmask +#define fpsetround _fpsetround +#define fpsetsticky _fpsetsticky +#define freenetconfigent _freenetconfigent +#define freeaddrinfo _freeaddrinfo +#define freeifaddrs _freeifaddrs +#define fstatvfs _fstatvfs +#define ftok _ftok +#define ftruncate _ftruncate +#define fts_children _fts_children +#define fts_close _fts_close +#define fts_open _fts_open +#define fts_read _fts_read +#define fts_set _fts_set +#define gai_strerror _gai_strerror +#define get_myaddress _get_myaddress +#define getaddrinfo _getaddrinfo +#define getbsize _getbsize +#define getcwd _getcwd +#define getdelim _getdelim +#define getdevmajor _getdevmajor +#define getdiskbyname _getdiskbyname +#define getdomainname _getdomainname +#define getfsent _getfsent +#define getfsfile _getfsfile +#define getfsspec _getfsspec +#define getgrent _getgrent +#define getgrent_r _getgrent_r +#define getgrgid _getgrgid +#define getgrgid_r _getgrgid_r +#define getgrnam _getgrnam +#define getgrnam_r _getgrnam_r +#define getgrouplist _getgrouplist +#define getgroupmembership _getgroupmembership +#define gethostbyaddr _gethostbyaddr +#define gethostbyname _gethostbyname +#define gethostent _gethostent +#define gethostname _gethostname +#define getifaddrs _getifaddrs +#define getline _getline +#define getloadavg _getloadavg +#define getlogin _getlogin +#define getlogin_r _getlogin_r +#define getmntinfo _getmntinfo +#define getmode _getmode +#define getnameinfo _getnameinfo +#define getnetbyaddr _getnetbyaddr +#define getnetbyname _getnetbyname +#define getnetconfig _getnetconfig +#define getnetconfigent _getnetconfigent +#define getnetent _getnetent +#define getnetgrent _getnetgrent +#define getnetpath _getnetpath +#define getopt _getopt +#define getopt_long _getopt_long +#define getpagesize _getpagesize +#define getpass _getpass +#define getprogname _getprogname +#define getprotobyname _getprotobyname +#define getprotobyname_r _getprotobyname_r +#define getprotobynumber _getprotobynumber +#define getprotobynumber_r _getprotobynumber_r +#define getprotoent _getprotoent +#define getprotoent_r _getprotoent_r +#define getpwent _getpwent +#define getpwent_r _getpwent_r +#define getpwnam _getpwnam +#define getpwnam_r _getpwnam_r +#define getpwuid _getpwuid +#define getpwuid_r _getpwuid_r +#define getrpcbyname _getrpcbyname +#define getrpcbyname_r _getrpcbyname_r +#define getrpcbynumber _getrpcbynumber +#define getrpcbynumber_r _getrpcbynumber_r +#define getrpcent _getrpcent +#define getrpcent_r _getrpcent_r +#define getrpcport _getrpcport +#define getservbyname _getservbyname +#define getservbyname_r _getservbyname_r +#define getservbyport _getservbyport +#define getservbyport_r _getservbyport_r +#define getservent _getservent +#define getservent_r _getservent_r +#define getsubopt _getsubopt +#define getttyent _getttyent +#define getttynam _getttynam +#define getusershell _getusershell +#define glob _glob +#define globfree _globfree +#define gmtime_r _gmtime_r +#define group_from_gid _group_from_gid +#define heapsort _heapsort +#define herror _herror +#define hes_error _hes_error +#define hes_free _hes_free +#define hes_init _hes_init +#define hes_resolve _hes_resolve +#define hes_to_bind _hes_to_bind +#define hesiod_end _hesiod_end +#define hesiod_free_list _hesiod_free_list +#define hesiod_init _hesiod_init +#define hesiod_resolve _hesiod_resolve +#define hesiod_to_bind _hesiod_to_bind +#define iconv _iconv +#define iconv_open _iconv_open +#define iconv_close _iconv_close +#define if_freenameindex _if_freenameindex +#define if_indextoname _if_indextoname +#define if_nameindex _if_nameindex +#define if_nametoindex _if_nametoindex +#define in6addr_any _in6addr_any +#define in6addr_linklocal_allnodes _in6addr_linklocal_allnodes +#define in6addr_linklocal_allrouters _in6addr_linklocal_allrouters +#define in6addr_loopback _in6addr_loopback +#define in6addr_nodelocal_allnodes _in6addr_nodelocal_allnodes +#define inet6_option_alloc _inet6_option_alloc +#define inet6_option_append _inet6_option_append +#define inet6_option_find _inet6_option_find +#define inet6_option_init _inet6_option_init +#define inet6_option_next _inet6_option_next +#define inet6_option_space _inet6_option_space +#define inet6_opt_init _inet6_opt_init +#define inet6_opt_append _inet6_opt_append +#define inet6_opt_finish _inet6_opt_finish +#define inet6_opt_set_val _inet6_opt_set_val +#define inet6_opt_next _inet6_opt_next +#define inet6_opt_find _inet6_opt_find +#define inet6_opt_get_val _inet6_opt_get_val +#define inet6_rthdr_add _inet6_rthdr_add +#define inet6_rthdr_getaddr _inet6_rthdr_getaddr +#define inet6_rthdr_getflags _inet6_rthdr_getflags +#define inet6_rthdr_init _inet6_rthdr_init +#define inet6_rthdr_lasthop _inet6_rthdr_lasthop +#define inet6_rthdr_segments _inet6_rthdr_segments +#define inet6_rthdr_space _inet6_rthdr_space +#define inet6_rth_space _inet6_rth_space +#define inet6_rth_init _inet6_rth_init +#define inet6_rth_add _inet6_rth_add +#define inet6_rth_reverse _inet6_rth_reverse +#define inet6_rth_segments _inet6_rth_segments +#define inet6_rth_getaddr _inet6_rth_getaddr +#define inet_cidr_ntop _inet_cidr_ntop +#define inet_cidr_pton _inet_cidr_pton +#define inet_lnaof _inet_lnaof +#define inet_makeaddr _inet_makeaddr +#define inet_net_ntop _inet_net_ntop +#define inet_net_pton _inet_net_pton +#define inet_neta _inet_neta +#define inet_netof _inet_netof +#define inet_network _inet_network +#define inet_nsap_addr _inet_nsap_addr +#define inet_nsap_ntoa _inet_nsap_ntoa +#define inet_ntoa _inet_ntoa +#define inet_ntop _inet_ntop +#define initgroups _initgroups +#define initstate _initstate +#define innetgr _innetgr +#define isatty _isatty +#define jrand48 _jrand48 +#define kill _kill +#define l64a _l64a +#define l64a_r _l64a_r +#define lcong48 _lcong48 +#define llabs _llabs +#define lldiv _lldiv +#define localtime_r _localtime_r +#define localtime_rz _localtime_rz +#define lockf _lockf +#define lrand48 _lrand48 +#define lseek _lseek +#define mergesort _mergesort +#define mi_vector_hash _mi_vector_hash +#define mkstemp _mkstemp +#define mktime_z _mktime_z +#define mmap _mmap +#define mpool_close _mpool_close +#define mpool_filter _mpool_filter +#define mpool_get _mpool_get +#define mpool_new _mpool_new +#define mpool_open _mpool_open +#define mpool_put _mpool_put +#define mpool_sync _mpool_sync +#define mq_timedreceive _mq_timedreceive +#define mq_timedsend _mq_timedsend +#define mrand48 _mrand48 +#define nc_perror _nc_perror +#define nc_sperror _nc_sperror +#define nanosleep _nanosleep +#define nice _nice +#if 0 +#define nlist _nlist +#endif +#define nrand48 _nrand48 +#define ntp_adjtime _ntp_adjtime +#define nsdispatch _nsdispatch +#define offtime _offtime +#define opendir _opendir +#define fdopendir _fdopendir +#define openlog _openlog +#define openlog_r _openlog_r +#define pause _pause +#define pclose _pclose +#define pmap_getmaps _pmap_getmaps +#define pmap_getport _pmap_getport +#define pmap_rmtcall _pmap_rmtcall +#define pmap_set _pmap_set +#define pmap_unset _pmap_unset +#define pollts _pollts +#define popen _popen +#define posix2time _posix2time +#define posix2time_z _posix2time_z +#define pread _pread +#define pselect _pselect +#define psignal _psignal +#define pthread_atfork _pthread_atfork +#define ptree_init ptree_init +#define ptree_insert_node ptree_insert_node +#define ptree_insert_mask_node ptree_insert_mask_node +#define ptree_find_filtered_node ptree_find_filtered_node +#define ptree_remove_node ptree_remove_node +#define ptree_iterate ptree_iterate +#define putenv _putenv +#define pwcache_groupdb _pwcache_groupdb +#define pwcache_userdb _pwcache_userdb +#define pwrite _pwrite +#define qabs _qabs +#define qdiv _qdiv +#define radixsort _radixsort +#define random _random +#define randomid _randomid +#define randomid_new _randomid_new +#define randomid_delete _randomid_delete +#define read _read +#define readdir _readdir +#define readdir_r _readdir_r +#define readlink _readlink +#define realpath _realpath +#define regcomp _regcomp +#define regerror _regerror +#define regexec _regexec +#define regfree _regfree +#define registerrpc _registerrpc +#define res_init _res_init +#define res_mkquery _res_mkquery +#define res_query _res_query +#define res_search _res_search +#define rewinddir _rewinddir +#define rpc_broadcast _rpc_broadcast +#define rpc_broadcast_exp _rpc_broadcast_exp +#define rpc_call _rpc_call +#define rpc_control _rpc_control +#define rpc_reg _rpc_reg +#define rpcb_getmaps _rpcb_getmaps +#define rpcb_gettime _rpcb_gettime +#define rpcb_rmtcall _rpcb_rmtcall +#define rpcb_set _rpcb_set +#define rpcb_taddr2uaddr _rpcb_taddr2uaddr +#define rpcb_uaddr2taddr _rpcb_uaddr2taddr +#define rpcb_unset _rpcb_unset +#define scandir _scandir +#define seed48 _seed48 +#define seekdir _seekdir +#define select _select +#define send _send +#define setdomainname _setdomainname +#define setenv _setenv +#define setfsent _setfsent +#define setgrent _setgrent +#define setgroupent _setgroupent +#define sethostent _sethostent +#define sethostname _sethostname +#define setlogin _setlogin +#define setlogmask _setlogmask +#define setlogmask_r _setlogmask_r +#define setmode _setmode +#define setnetconfig _setnetconfig +#define setnetent _setnetent +#define setnetgrent _setnetgrent +#define setpassent _setpassent +#define setnetpath _setnetpath +#define setproctitle _setproctitle +#define setprotoent _setprotoent +#define setprotoent_r _setprotoent_r +#define setpwent _setpwent +#define setrpcent _setrpcent +#define setservent _setservent +#define setservent_r _setservent_r +#define setstate _setstate +#define setttyent _setttyent +#define setttyentpath _setttyentpath +#define settimeofday _settimeofday +#define setusershell _setusershell +#define shm_open _shm_open +#define shm_unlink _shm_unlink +#define shquote _shquote +#define siginterrupt _siginterrupt +#define signal _signal +#define sigtimedwait _sigtimedwait +#define sl_add _sl_add +#define sl_create _sl_create +#define sl_delete _sl_delete +#define sl_find _sl_find +#define sl_free _sl_free +#define sl_init _sl_init +#define sleep _sleep +#ifndef snprintf +#define snprintf _snprintf +#endif +#define snprintf_ss _snprintf_ss +#define sradixsort _sradixsort +#define srand48 _srand48 +#define srandom _srandom +#define statvfs(a, b) _statvfs(a, b) +#define strcasecmp _strcasecmp +#define strdup _strdup +#define stresep _stresep +#define strftime_z _strftime_z +#define strndup _strndup +#define strncasecmp _strncasecmp +#define strptime _strptime +#define strsep _strsep +#define strsignal _strsignal +#define strsuftoll _strsuftoll +#define strsuftollx _strsuftollx +#define strsvis _strsvis +#define strsvisx _strsvisx +#define strtok_r _strtok_r +#define strunvis _strunvis +#define strvis _strvis +#define strvisx _strvisx +#define svc_auth_reg _svc_auth_reg +#define svc_create _svc_create +#define svc_dg_create _svc_dg_create +#define svc_exit _svc_exit +#define svc_fd_create _svc_fd_create +#define svc_getreq _svc_getreq +#define svc_getreqset _svc_getreqset +#define svc_getreq_common _svc_getreq_common +#define svc_raw_create _svc_raw_create +#define svc_register _svc_register +#define svc_reg _svc_reg +#define svc_run _svc_run +#define svc_sendreply _svc_sendreply +#define svc_tli_create _svc_tli_create +#define svc_tp_create _svc_tp_create +#define svc_unregister _svc_unregister +#define svc_unreg _svc_unreg +#define svc_vc_create _svc_vc_create +#define svcerr_auth _svcerr_auth +#define svcerr_decode _svcerr_decode +#define svcerr_noproc _svcerr_noproc +#define svcerr_noprog _svcerr_noprog +#define svcerr_progvers _svcerr_progvers +#define svcerr_systemerr _svcerr_systemerr +#define svcerr_weakauth _svcerr_weakauth +#define svcfd_create _svcfd_create +#define svcraw_create _svcraw_create +#define svctcp_create _svctcp_create +#define svcudp_bufcreate _svcudp_bufcreate +#define svcudp_create _svcudp_create +#define svcudp_enablecache _svcudp_enablecache +#define svis _svis +#define sysarch _sys_sysarch +#define sysctl _sysctl +#define sysctlbyname _sysctlbyname +#define sysctlgetmibinfo _sysctlgetmibinfo +#define sysctlnametomib _sysctlnametomib +#define syslog _syslog +#define syslog_r _syslog_r +#define syslog_ss _syslog_ss +#define syslogp _syslogp +#define syslogp_r _syslogp_r +#define syslogp_ss _syslogp_ss +#define taddr2uaddr _taddr2uaddr +#define tcdrain _tcdrain +#define tcflow _tcflow +#define tcflush _tcflush +#define tcgetattr _tcgetattr +#define tcgetpgrp _tcgetpgrp +#define tcgetsid _tcgetsid +#define tcsendbreak _tcsendbreak +#define tcsetattr _tcsetattr +#define tcsetpgrp _tcsetpgrp +#define telldir _telldir +#define time _time +#define time2posix _time2posix +#define timegm _timegm +#define timelocal _timelocal +#define timeoff _timeoff +#define times _times +#define ttyname _ttyname +#define ttyname_r _ttyname_r +#define ttyslot _ttyslot +#define tzname _tzname +#define tzset _tzset +#define tzsetwall _tzsetwall +#define uaddr2taddr _uaddr2taddr +#define ualarm _ualarm +#define uname _uname +#define unsetenv _unsetenv +#define unvis _unvis +#define user_from_uid _user_from_uid +#define usleep _usleep +#define utime _utime +#define uuid_create_nil _uuid_create_nil +#define uuid_is_nil _uuid_is_nil +#define valloc _valloc +#define vdprintf _vdprintf +#define vis _vis +#define vdprintf _vdprintf +#ifndef vsnprintf +#define vsnprintf _vsnprintf +#endif +#define vsnprintf_ss _vsnprintf_ss +#define vsyslog _vsyslog +#define vsyslog_r _vsyslog_r +#define vsyslog_ss _vsyslog_ss +#define vsyslogp _vsyslogp +#define vsyslogp_r _vsyslogp_r +#define vsyslogp_ss _vsyslogp_ss +#define wait _wait +#define wait3 _wait3 +#define wait4 _wait4 +#define waitpid _waitpid +#define wcscasecmp _wcscasecmp +#define wcsdup _wcsdup +#define wcsncasecmp _wcsncasecmp +#define wcstof _wcstof +#define wcstod _wcstod +#define wcstold _wcstold +#define wcwidth _wcwidth +#define xdr_accepted_reply _xdr_accepted_reply +#define xdr_array _xdr_array +#define xdr_authunix_parms _xdr_authunix_parms +#define xdr_bool _xdr_bool +#define xdr_bytes _xdr_bytes +#define xdr_callhdr _xdr_callhdr +#define xdr_callmsg _xdr_callmsg +#define xdr_char _xdr_char +#define xdr_datum _xdr_datum +#define xdr_des_block _xdr_des_block +#define xdr_domainname _xdr_domainname +#define xdr_double _xdr_double +#define xdr_enum _xdr_enum +#define xdr_float _xdr_float +#define xdr_free _xdr_free +#define xdr_hyper _xdr_hyper +#define xdr_int _xdr_int +#define xdr_int16_t _xdr_int16_t +#define xdr_int32_t _xdr_int32_t +#define xdr_int64_t _xdr_int64_t +#define xdr_long _xdr_long +#define xdr_longlong_t _xdr_longlong_t +#define xdr_mapname _xdr_mapname +#define xdr_netbuf _xdr_netbuf +#define xdr_netobj _xdr_netobj +#define xdr_opaque _xdr_opaque +#define xdr_opaque_auth _xdr_opaque_auth +#define xdr_peername _xdr_peername +#define xdr_pmap _xdr_pmap +#define xdr_pmaplist _xdr_pmaplist +#define xdr_pointer _xdr_pointer +#define xdr_reference _xdr_reference +#define xdr_rejected_reply _xdr_rejected_reply +#define xdr_replymsg _xdr_replymsg +#define xdr_rmtcall_args _xdr_rmtcall_args +#define xdr_rmtcallres _xdr_rmtcallres +#define xdr_rpcb _xdr_rpcb +#define xdr_rpcb_entry _xdr_rpcb_entry +#define xdr_rpcb_entry_list_ptr _xdr_rpcb_entry_list_ptr +#define xdr_rpcb_rmtcallargs _xdr_rpcb_rmtcallargs +#define xdr_rpcb_rmtcallres _xdr_rpcb_rmtcallres +#define xdr_rpcb_stat _xdr_rpcb_stat +#define xdr_rpcb_stat_byvers _xdr_rpcb_stat_byvers +#define xdr_rpcblist _xdr_rpcblist +#define xdr_rpcblist_ptr _xdr_rpcblist_ptr +#define xdr_rpcbs_addrlist _xdr_rpcbs_addrlist +#define xdr_rpcbs_addrlist_ptr _xdr_rpcbs_addrlist_ptr +#define xdr_rpcbs_proc _xdr_rpcbs_proc +#define xdr_rpcbs_rmtcalllist _xdr_rpcbs_rmtcalllist +#define xdr_rpcbs_rmtcalllist_ptr _xdr_rpcbs_rmtcalllist_ptr +#define xdr_rpcbs _xdr_rpcbs +#define xdr_rpcbs _xdr_rpcbs +#define xdr_short _xdr_short +#define xdr_string _xdr_string +#define xdr_u_char _xdr_u_char +#define xdr_u_hyper _xdr_u_hyper +#define xdr_u_int _xdr_u_int +#define xdr_u_int16_t _xdr_u_int16_t +#define xdr_u_int32_t _xdr_u_int32_t +#define xdr_u_int64_t _xdr_u_int64_t +#define xdr_u_long _xdr_u_long +#define xdr_u_longlong_t _xdr_u_longlong_t +#define xdr_u_short _xdr_u_short +#define xdr_union _xdr_union +#define xdr_vector _xdr_vector +#define xdr_void _xdr_void +#define xdr_wrapstring _xdr_wrapstring +#define xdr_yp_inaddr _xdr_yp_inaddr +#define xdr_ypall _xdr_ypall +#define xdr_ypbind_resp _xdr_ypbind_resp +#define xdr_ypbind_setdom _xdr_ypbind_setdom +#define xdr_ypdomain_wrap_string _xdr_ypdomain_wrap_string +#define xdr_ypmap_parms _xdr_ypmap_parms +#define xdr_ypmap_wrap_string _xdr_ypmap_wrap_string +#define xdr_ypmaplist _xdr_ypmaplist +#define xdr_ypowner_wrap_string _xdr_ypowner_wrap_string +#define xdr_yppushresp_xfr _xdr_yppushresp_xfr +#define xdr_ypreq_key _xdr_ypreq_key +#define xdr_ypreq_nokey _xdr_ypreq_nokey +#define xdr_ypreq_xfr _xdr_ypreq_xfr +#define xdr_ypresp_key_val _xdr_ypresp_key_val +#define xdr_ypresp_maplist _xdr_ypresp_maplist +#define xdr_ypresp_master _xdr_ypresp_master +#define xdr_ypresp_order _xdr_ypresp_order +#define xdr_ypresp_val _xdr_ypresp_val +#define xdrmem_create _xdrmem_create +#define xdrrec_create _xdrrec_create +#define xdrrec_endofrecord _xdrrec_endofrecord +#define xdrrec_eof _xdrrec_eof +#define xdrrec_skiprecord _xdrrec_skiprecord +#define xdrstdio_create _xdrstdio_create +#define xprt_register _xprt_register +#define xprt_unregister _xprt_unregister +#define yp_all _yp_all +#define yp_bind _yp_bind +#define yp_first _yp_first +#define yp_get_default_domain _yp_get_default_domain +#define yp_maplist _yp_maplist +#define yp_master _yp_master +#define yp_match _yp_match +#define yp_next _yp_next +#define yp_order _yp_order +#define yp_unbind _yp_unbind +#define yperr_string _yperr_string +#define ypprot_err _ypprot_err +#define dlopen __dlopen +#define dlclose __dlclose +#define dlsym __dlsym +#define dlerror __dlerror +#define dladdr __dladdr +#define fmtcheck __fmtcheck + +/* RB trees */ +#define rb_tree_init _rb_tree_init +#define rb_tree_find_node _rb_tree_find_node +#define rb_tree_find_node_geq _rb_tree_find_node_geq +#define rb_tree_find_node_leq _rb_tree_find_node_leq +#define rb_tree_insert_node _rb_tree_insert_node +#define rb_tree_remove_node _rb_tree_remove_node +#define rb_tree_iterate _rb_tree_iterate +#ifdef RBDEBUG +#define rb_tree_check _rb_tree_check +#define rb_tree_depths _rb_tree_depths +#endif + +/* rpc locks */ +#define authdes_lock __rpc_authdes_lock +#define authnone_lock __rpc_authnone_lock +#define authsvc_lock __rpc_authsvc_lock +#define clnt_fd_lock __rpc_clnt_fd_lock +#define clntraw_lock __rpc_clntraw_lock +#define dname_lock __rpc_dname_lock +#define dupreq_lock __rpc_dupreq_lock +#define keyserv_lock __rpc_keyserv_lock +#define libnsl_trace_lock __rpc_libnsl_trace_lock +#define loopnconf_lock __rpc_loopnconf_lock +#define ops_lock __rpc_ops_lock +#define portnum_lock __rpc_portnum_lock +#define proglst_lock __rpc_proglst_lock +#define rpcbaddr_cache_lock __rpc_rpcbaddr_cache_lock +#define rpcsoc_lock __rpc_rpcsoc_lock +#define svc_fd_lock __rpc_svc_fd_lock +#define svc_lock __rpc_svc_lock +#define svcraw_lock __rpc_svcraw_lock +#define xprtlist_lock __rpc_xprtlist_lock + +#define __learn_tree ___learn_tree +#endif /* __weak_alias */ +#endif /* !__lint__ */ + +#endif /* _NAMESPACE_H_ */ diff --git a/lib/nbsd_libc/include/pathnames.h b/lib/nbsd_libc/include/pathnames.h new file mode 100644 index 000000000..5705c6838 --- /dev/null +++ b/lib/nbsd_libc/include/pathnames.h @@ -0,0 +1,33 @@ +/* $NetBSD: pathnames.h,v 1.6 2008/05/29 14:51:25 mrg Exp $ */ + +/* + * Copyright (c) 1997 Matthew R. Green + * 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 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. + */ + +#ifdef RESCUEDIR +#define _PATH_BIN_RCMD RESCUEDIR "/rcmd" +#else +#define _PATH_BIN_RCMD "/bin/rcmd" +#endif diff --git a/lib/nbsd_libc/include/port_after.h b/lib/nbsd_libc/include/port_after.h new file mode 100644 index 000000000..e69de29bb diff --git a/lib/nbsd_libc/include/port_before.h b/lib/nbsd_libc/include/port_before.h new file mode 100644 index 000000000..36217ac54 --- /dev/null +++ b/lib/nbsd_libc/include/port_before.h @@ -0,0 +1,11 @@ +#include "namespace.h" +#include +#define ISC_FORMAT_PRINTF(a,b) __attribute__((__format__(__printf__,a,b))) +#define ISC_SOCKLEN_T socklen_t +#define DE_CONST(c,v) v = ((c) ? \ + strchr((const void *)(c), *(const char *)(const void *)(c)) : NULL) +#ifndef lint +#define UNUSED(a) (void)&a +#else +#define UNUSED(a) a = a +#endif diff --git a/lib/nbsd_libc/include/reentrant.h b/lib/nbsd_libc/include/reentrant.h new file mode 100644 index 000000000..fcfe2f9f9 --- /dev/null +++ b/lib/nbsd_libc/include/reentrant.h @@ -0,0 +1,266 @@ +/* $NetBSD: reentrant.h,v 1.14 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 2003 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin, by Nathan J. Williams, and by Jason R. Thorpe. + * + * 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. + */ + +/* + * Requirements: + * + * 1. The thread safe mechanism should be lightweight so the library can + * be used by non-threaded applications without unreasonable overhead. + * + * 2. There should be no dependency on a thread engine for non-threaded + * applications. + * + * 3. There should be no dependency on any particular thread engine. + * + * 4. The library should be able to be compiled without support for thread + * safety. + * + * + * Rationale: + * + * One approach for thread safety is to provide discrete versions of the + * library: one thread safe, the other not. The disadvantage of this is + * that libc is rather large, and two copies of a library which are 99%+ + * identical is not an efficent use of resources. + * + * Another approach is to provide a single thread safe library. However, + * it should not add significant run time or code size overhead to non- + * threaded applications. + * + * Since the NetBSD C library is used in other projects, it should be + * easy to replace the mutual exclusion primitives with ones provided by + * another system. Similarly, it should also be easy to remove all + * support for thread safety completely if the target environment does + * not support threads. + * + * + * Implementation Details: + * + * The thread primitives used by the library (mutex_t, mutex_lock, etc.) + * are macros which expand to the cooresponding primitives provided by + * the thread engine or to nothing. The latter is used so that code is + * not unreasonably cluttered with #ifdefs when all thread safe support + * is removed. + * + * The thread macros can be directly mapped to the mutex primitives from + * pthreads, however it should be reasonably easy to wrap another mutex + * implementation so it presents a similar interface. + * + * The thread functions operate by dispatching to symbols which are, by + * default, weak-aliased to no-op functions in thread-stub/thread-stub.c + * (some uses of thread operations are conditional on __isthreaded, but + * not all of them are). + * + * When the thread library is linked in, it provides strong-alias versions + * of those symbols which dispatch to its own real thread operations. + * + */ + +#ifdef _REENTRANT + +/* + * Abstract thread interface for thread-safe libraries. These routines + * will use stubs in libc if the application is not linked against the + * pthread library, and the real function in the pthread library if it + * is. + */ + +#include +#include + +#define mutex_t pthread_mutex_t +#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER + +#define mutexattr_t pthread_mutexattr_t + +#define MUTEX_TYPE_NORMAL PTHREAD_MUTEX_NORMAL +#define MUTEX_TYPE_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK +#define MUTEX_TYPE_RECURSIVE PTHREAD_MUTEX_RECURSIVE + +#define cond_t pthread_cond_t +#define COND_INITIALIZER PTHREAD_COND_INITIALIZER + +#define condattr_t pthread_condattr_t + +#define rwlock_t pthread_rwlock_t +#define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER + +#define rwlockattr_t pthread_rwlockattr_t + +#define thread_key_t pthread_key_t + +#define thr_t pthread_t + +#define thrattr_t pthread_attr_t + +#define once_t pthread_once_t +#define ONCE_INITIALIZER PTHREAD_ONCE_INIT + +#ifndef __LIBC_THREAD_STUBS + +__BEGIN_DECLS +int __libc_mutex_init(mutex_t *, const mutexattr_t *); +int __libc_mutex_lock(mutex_t *); +int __libc_mutex_trylock(mutex_t *); +int __libc_mutex_unlock(mutex_t *); +int __libc_mutex_destroy(mutex_t *); + +int __libc_mutexattr_init(mutexattr_t *); +int __libc_mutexattr_settype(mutexattr_t *, int); +int __libc_mutexattr_destroy(mutexattr_t *); +__END_DECLS + +#define mutex_init(m, a) __libc_mutex_init((m), (a)) +#define mutex_lock(m) __libc_mutex_lock((m)) +#define mutex_trylock(m) __libc_mutex_trylock((m)) +#define mutex_unlock(m) __libc_mutex_unlock((m)) +#define mutex_destroy(m) __libc_mutex_destroy((m)) + +#define mutexattr_init(ma) __libc_mutexattr_init((ma)) +#define mutexattr_settype(ma, t) __libc_mutexattr_settype((ma), (t)) +#define mutexattr_destroy(ma) __libc_mutexattr_destroy((ma)) + +__BEGIN_DECLS +int __libc_cond_init(cond_t *, const condattr_t *); +int __libc_cond_signal(cond_t *); +int __libc_cond_broadcast(cond_t *); +int __libc_cond_wait(cond_t *, mutex_t *); +#ifndef __LIBC12_SOURCE__ +int __libc_cond_timedwait(cond_t *, mutex_t *, const struct timespec *); +#endif +int __libc_cond_destroy(cond_t *); +__END_DECLS + +#define cond_init(c, t, a) __libc_cond_init((c), (a)) +#define cond_signal(c) __libc_cond_signal((c)) +#define cond_broadcast(c) __libc_cond_broadcast((c)) +#define cond_wait(c, m) __libc_cond_wait((c), (m)) +#define cond_timedwait(c, m, t) __libc_cond_timedwait((c), (m), (t)) +#define cond_destroy(c) __libc_cond_destroy((c)) + +__BEGIN_DECLS +int __libc_rwlock_init(rwlock_t *, const rwlockattr_t *); +int __libc_rwlock_rdlock(rwlock_t *); +int __libc_rwlock_wrlock(rwlock_t *); +int __libc_rwlock_tryrdlock(rwlock_t *); +int __libc_rwlock_trywrlock(rwlock_t *); +int __libc_rwlock_unlock(rwlock_t *); +int __libc_rwlock_destroy(rwlock_t *); +__END_DECLS + +#define rwlock_init(l, a) __libc_rwlock_init((l), (a)) +#define rwlock_rdlock(l) __libc_rwlock_rdlock((l)) +#define rwlock_wrlock(l) __libc_rwlock_wrlock((l)) +#define rwlock_tryrdlock(l) __libc_rwlock_tryrdlock((l)) +#define rwlock_trywrlock(l) __libc_rwlock_trywrlock((l)) +#define rwlock_unlock(l) __libc_rwlock_unlock((l)) +#define rwlock_destroy(l) __libc_rwlock_destroy((l)) + +__BEGIN_DECLS +int __libc_thr_keycreate(thread_key_t *, void (*)(void *)); +int __libc_thr_setspecific(thread_key_t, const void *); +void *__libc_thr_getspecific(thread_key_t); +int __libc_thr_keydelete(thread_key_t); +__END_DECLS + +#define thr_keycreate(k, d) __libc_thr_keycreate((k), (d)) +#define thr_setspecific(k, p) __libc_thr_setspecific((k), (p)) +#define thr_getspecific(k) __libc_thr_getspecific((k)) +#define thr_keydelete(k) __libc_thr_keydelete((k)) + +__BEGIN_DECLS +int __libc_thr_once(once_t *, void (*)(void)); +int __libc_thr_sigsetmask(int, const sigset_t *, sigset_t *); +thr_t __libc_thr_self(void); +int __libc_thr_yield(void); +void __libc_thr_create(thr_t *, const thrattr_t *, + void *(*)(void *), void *); +void __libc_thr_exit(void *) __attribute__((__noreturn__)); +int *__libc_thr_errno(void); +int __libc_thr_setcancelstate(int, int *); +unsigned int __libc_thr_curcpu(void); + +extern int __isthreaded; +__END_DECLS + +#define thr_once(o, f) __libc_thr_once((o), (f)) +#define thr_sigsetmask(f, n, o) __libc_thr_sigsetmask((f), (n), (o)) +#define thr_self() __libc_thr_self() +#define thr_yield() __libc_thr_yield() +#define thr_create(tp, ta, f, a) __libc_thr_create((tp), (ta), (f), (a)) +#define thr_exit(v) __libc_thr_exit((v)) +#define thr_errno() __libc_thr_errno() +#define thr_enabled() (__isthreaded) +#define thr_setcancelstate(n, o) __libc_thr_setcancelstate((n),(o)) +#define thr_curcpu() __libc_thr_curcpu() +#endif /* __LIBC_THREAD_STUBS */ + +#define FLOCKFILE(fp) __flockfile_internal(fp, 1) +#define FUNLOCKFILE(fp) __funlockfile_internal(fp, 1) + +#else /* _REENTRANT */ + +#define mutex_init(m, a) +#define mutex_lock(m) +#define mutex_trylock(m) +#define mutex_unlock(m) +#define mutex_destroy(m) + +#define cond_init(c, t, a) +#define cond_signal(c) +#define cond_broadcast(c) +#define cond_wait(c, m) +#define cond_timedwait(c, m, t) +#define cond_destroy(c) + +#define rwlock_init(l, a) +#define rwlock_rdlock(l) +#define rwlock_wrlock(l) +#define rwlock_tryrdlock(l) +#define rwlock_trywrlock(l) +#define rwlock_unlock(l) +#define rwlock_destroy(l) + +#define thr_keycreate(k, d) +#define thr_setspecific(k, p) +#define thr_getspecific(k) +#define thr_keydelete(k) + +#define thr_once(o, f) +#define thr_sigsetmask(f, n, o) +#define thr_self() +#define thr_errno() +#define thr_curcpu() ((unsigned int)0) + +#define FLOCKFILE(fp) +#define FUNLOCKFILE(fp) + +#endif /* _REENTRANT */ diff --git a/lib/nbsd_libc/include/resolv_mt.h b/lib/nbsd_libc/include/resolv_mt.h new file mode 100644 index 000000000..73a8dcc18 --- /dev/null +++ b/lib/nbsd_libc/include/resolv_mt.h @@ -0,0 +1,49 @@ +/* $NetBSD: resolv_mt.h,v 1.1.1.3 2009/04/12 16:35:44 christos Exp $ */ + +#ifndef _RESOLV_MT_H +#define _RESOLV_MT_H + +#include +#include +#include +#include + +/* Access functions for the libresolv private interface */ + +int __res_enable_mt(void); +int __res_disable_mt(void); + +/* Per-thread context */ + +typedef struct { +int no_hosts_fallback_private; +int retry_save; +int retry_private; +char inet_nsap_ntoa_tmpbuf[255*3]; +char sym_ntos_unname[20]; +char sym_ntop_unname[20]; +char p_option_nbuf[40]; +char p_time_nbuf[40]; +char precsize_ntoa_retbuf[sizeof "90000000.00"]; +char loc_ntoa_tmpbuf[sizeof +"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"]; +char p_secstodate_output[15]; +} mtctxres_t; + +/* Thread-specific data (TSD) */ + +mtctxres_t *___mtctxres(void); +#define mtctxres (___mtctxres()) + +/* Various static data that should be TSD */ + +#define sym_ntos_unname (mtctxres->sym_ntos_unname) +#define sym_ntop_unname (mtctxres->sym_ntop_unname) +#define inet_nsap_ntoa_tmpbuf (mtctxres->inet_nsap_ntoa_tmpbuf) +#define p_option_nbuf (mtctxres->p_option_nbuf) +#define p_time_nbuf (mtctxres->p_time_nbuf) +#define precsize_ntoa_retbuf (mtctxres->precsize_ntoa_retbuf) +#define loc_ntoa_tmpbuf (mtctxres->loc_ntoa_tmpbuf) +#define p_secstodate_output (mtctxres->p_secstodate_output) + +#endif /* _RESOLV_MT_H */ diff --git a/lib/nbsd_libc/inet/Makefile.inc b/lib/nbsd_libc/inet/Makefile.inc new file mode 100644 index 000000000..5d5ce205d --- /dev/null +++ b/lib/nbsd_libc/inet/Makefile.inc @@ -0,0 +1,9 @@ +# $NetBSD: Makefile.inc,v 1.1 2004/05/20 23:13:02 christos Exp $ + +# net sources +.PATH: ${.CURDIR}/inet + +SRCS+= _inet_aton.c _inet_pton.c inet_addr.c inet_cidr_ntop.c \ + inet_cidr_pton.c inet_lnaof.c inet_makeaddr.c \ + inet_net_ntop.c inet_net_pton.c inet_neta.c inet_netof.c \ + inet_network.c inet_ntoa.c inet_ntop.c inet_pton.c nsap_addr.c diff --git a/lib/nbsd_libc/inet/_inet_aton.c b/lib/nbsd_libc/inet/_inet_aton.c new file mode 100644 index 000000000..0837a6744 --- /dev/null +++ b/lib/nbsd_libc/inet/_inet_aton.c @@ -0,0 +1,29 @@ +/* $NetBSD: _inet_aton.c,v 1.4 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Written by Klaus Klein, September 14, 1999. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _inet_aton.c,v 1.4 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_inet_aton, inet_aton) +#else + +#include +#include +#include + +int _inet_aton(const char *, struct in_addr *); + +int +inet_aton(const char *cp, struct in_addr *addr) +{ + + return _inet_aton(cp, addr); +} +#endif diff --git a/lib/nbsd_libc/inet/_inet_pton.c b/lib/nbsd_libc/inet/_inet_pton.c new file mode 100644 index 000000000..7970272e5 --- /dev/null +++ b/lib/nbsd_libc/inet/_inet_pton.c @@ -0,0 +1,29 @@ +/* $NetBSD: _inet_pton.c,v 1.4 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Written by Klaus Klein, September 14, 1999. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _inet_pton.c,v 1.4 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_inet_pton, inet_pton) +#else + +#include +#include +#include + +int _inet_pton(int, const char *, void *); + +int +inet_pton(int af, const char *src, void *dst) +{ + + return _inet_pton(af, src, dst); +} +#endif diff --git a/lib/nbsd_libc/inet/inet.3 b/lib/nbsd_libc/inet/inet.3 new file mode 100644 index 000000000..30f42d41f --- /dev/null +++ b/lib/nbsd_libc/inet/inet.3 @@ -0,0 +1,362 @@ +.\" $NetBSD: inet.3,v 1.1 2004/05/20 23:13:02 christos Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)inet.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 30, 2003 +.Dt INET 3 +.Os +.Sh NAME +.Nm inet_addr , +.Nm inet_aton , +.Nm inet_lnaof , +.Nm inet_makeaddr , +.Nm inet_netof , +.Nm inet_network , +.Nm inet_ntoa , +.Nm inet_ntop , +.Nm inet_pton , +.Nm addr , +.Nm ntoa , +.Nm network +.Nd Internet address manipulation routines +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In arpa/inet.h +.Ft in_addr_t +.Fn inet_addr "const char *cp" +.Ft int +.Fn inet_aton "const char *cp" "struct in_addr *addr" +.Ft in_addr_t +.Fn inet_lnaof "struct in_addr in" +.Ft struct in_addr +.Fn inet_makeaddr "in_addr_t net" "in_addr_t lna" +.Ft in_addr_t +.Fn inet_netof "struct in_addr in" +.Ft in_addr_t +.Fn inet_network "const char *cp" +.Ft char * +.Fn inet_ntoa "struct in_addr in" +.Ft const char * +.Fn inet_ntop "int af" "const void * restrict src" "char * restrict dst" "socklen_t size" +.Ft int +.Fn inet_pton "int af" "const char * restrict src" "void * restrict dst" +.Sh DESCRIPTION +The routines +.Fn inet_aton , +.Fn inet_addr +and +.Fn inet_network +interpret character strings representing +numbers expressed in the Internet standard +.Qq dotted quad +notation. +.Pp +The +.Fn inet_pton +function converts a presentation format address (that is, printable form +as held in a character string) to network format (usually a +.Ft struct in_addr +or some other internal binary representation, in network byte order). +It returns 1 if the address was valid for the specified address family, or +0 if the address wasn't parsable in the specified address family, or -1 +if some system error occurred (in which case +.Va errno +will have been set). +This function is presently valid for +.Dv AF_INET +and +.Dv AF_INET6 . +.Pp +The +.Fn inet_aton +routine interprets the specified character string as an Internet address, +placing the address into the structure provided. +It returns 1 if the string was successfully interpreted, +or 0 if the string is invalid. +.Pp +The +.Fn inet_addr +and +.Fn inet_network +functions return numbers suitable for use +as Internet addresses and Internet network +numbers, respectively. +.Pp +The function +.Fn inet_ntop +converts an address from network format (usually a +.Ft struct in_addr +or some other binary form, in network byte order) to presentation format +(suitable for external display purposes). +It returns NULL if a system error occurs (in which case, +.Va errno +will have been set), or it returns a pointer to the destination string. +.Pp +The routine +.Fn inet_ntoa +takes an Internet address and returns an +.Tn ASCII +string representing the address in +.Qq dotted quad +notation. +.Pp +The routine +.Fn inet_makeaddr +takes an Internet network number and a local network address (both in +host order) and constructs an Internet address from it. +Note that to convert only a single value to a +.Ft struct in_addr +form that value should be passed as the first parameter and +.Ql 0L +should be given for the second parameter. +.Pp +The routines +.Fn inet_netof +and +.Fn inet_lnaof +break apart Internet host addresses, returning the network number and +local network address part, respectively (both in host order). +.Pp +All Internet addresses are returned in network +order (bytes ordered from left to right). +All network numbers and local address parts are +returned as machine format integer values. +.Sh INTERNET ADDRESSES (IP VERSION 4) +Values specified using the +.Qq dotted quad +notation take one +of the following forms: +.Bd -literal -offset indent +a.b.c.d +a.b.c +a.b +a +.Ed +.Pp +When four parts are specified, each is interpreted +as a byte of data and assigned, from left to right, +to the four bytes of an Internet address. +Note that when an Internet address is viewed as a 32-bit +integer quantity on a system that uses little-endian +byte order (e.g. +.Tn Intel i386, i486 +and +.Tn Pentium +processors) the bytes referred to above appear as +.Dq Li d.c.b.a . +That is, little-endian bytes are ordered from right to left. +.Pp +When a three part address is specified, the last +part is interpreted as a 16-bit quantity and placed +in the right-most two bytes of the network address. +This makes the three part address format convenient +for specifying Class B network addresses as +.Dq Li 128.net.host . +.Pp +When a two part address is supplied, the last part +is interpreted as a 24-bit quantity and placed in +the right most three bytes of the network address. +This makes the two part address format convenient +for specifying Class A network addresses as +.Dq Li net.host . +.Pp +When only one part is given, the value is stored +directly in the network address without any byte +rearrangement. +.Pp +All numbers supplied as +.Dq parts +in a +.Qq dotted quad +notation +may be decimal, octal, or hexadecimal, as specified +in the C language (i.e., a leading 0x or 0X implies +hexadecimal; otherwise, a leading 0 implies octal; +otherwise, the number is interpreted as decimal). +.Sh INTERNET ADDRESSES (IP VERSION 6) +In order to support scoped IPv6 addresses, +the use of +.Xr getaddrinfo 3 +and +.Xr getnameinfo 3 +is recommended rather than the functions presented here. +.Pp +The presentation format of an IPv6 address is given in RFC 2373: +.Pp +There are three conventional forms for representing IPv6 addresses as +text strings: +.Bl -enum +.It +The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the +hexadecimal values of the eight 16-bit pieces of the address. +Examples: +.Bd -literal -offset indent +FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 +1080:0:0:0:8:800:200C:417A +.Ed +.Pp +Note that it is not necessary to write the leading zeros in an +individual field, but there must be at least one numeral in +every field (except for the case described in 2). +.It +Due to the method of allocating certain styles of IPv6 +addresses, it will be common for addresses to contain long +strings of zero bits. +In order to make writing addresses +containing zero bits easier, a special syntax is available to +compress the zeros. +The use of ``::'' indicates multiple groups of 16-bits of zeros. +The ``::'' can only appear once in an address. +The ``::'' can also be used to compress the leading +and/or trailing zeros in an address. +.Pp +For example the following addresses: +.Bd -literal -offset indent +1080:0:0:0:8:800:200C:417A a unicast address +FF01:0:0:0:0:0:0:43 a multicast address +0:0:0:0:0:0:0:1 the loopback address +0:0:0:0:0:0:0:0 the unspecified addresses +.Ed +.Pp +may be represented as: +.Bd -literal -offset indent +1080::8:800:200C:417A a unicast address +FF01::43 a multicast address +::1 the loopback address +:: the unspecified addresses +.Ed +.It +An alternative form that is sometimes more convenient when +dealing with a mixed environment of IPv4 and IPv6 nodes is +x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values +of the six high-order 16-bit pieces of the address, and the 'd's +are the decimal values of the four low-order 8-bit pieces of the +address (standard IPv4 representation). +Examples: +.Bd -literal -offset indent +0:0:0:0:0:0:13.1.68.3 +0:0:0:0:0:FFFF:129.144.52.38 +.Ed +.Pp +or in compressed form: +.Bd -literal -offset indent +::13.1.68.3 +::FFFF:129.144.52.38 +.Ed +.El +.Sh DIAGNOSTICS +The constant +.Dv INADDR_NONE +is returned by +.Fn inet_addr +and +.Fn inet_network +for malformed requests. +.Sh SEE ALSO +.Xr byteorder 3 , +.Xr gethostbyname 3 , +.Xr getnetent 3 , +.Xr inet_net 3 , +.Xr hosts 5 , +.Xr networks 5 +.Rs +.%R RFC 2373 +.%D July 1998 +.%T "IP Version 6 Addressing Architecture" +.Re +.Rs +.%R RFC 3493 +.%D February 2003 +.%T "Basic Socket Interface Extensions for IPv6" +.Re +.Sh STANDARDS +The +.Nm inet_ntop +and +.Nm inet_pton +functions conform to +.St -p1003.1-2001 . +Note that +.Nm inet_pton +does not accept 1-, 2-, or 3-part dotted addresses; all four parts +must be specified. +This is a narrower input set than that accepted by +.Nm inet_aton . +.Sh HISTORY +The +.Nm inet_addr , +.Nm inet_network , +.Nm inet_makeaddr , +.Nm inet_lnaof +and +.Nm inet_netof +functions appeared in +.Bx 4.2 . +They were changed to use +.Va in_addr_t +in place of +.Va unsigned long +in +.Nx 2.0 . +The +.Nm inet_aton +and +.Nm inet_ntoa +functions appeared in +.Bx 4.3 . +The +.Nm inet_pton +and +.Nm inet_ntop +functions appeared in BIND 4.9.4 and thence +.Nx 1.3 ; +they were also in +.St -xns5.2 . +.Sh BUGS +The value +.Dv INADDR_NONE +(0xffffffff) is a valid broadcast address, but +.Fn inet_addr +cannot return that value without indicating failure. +The newer +.Fn inet_aton +function does not share this problem. +.Pp +The problem of host byte ordering versus network byte ordering is +confusing. +.Pp +The string returned by +.Fn inet_ntoa +resides in a static memory area. +.Pp +.Fn inet_addr +should return a +.Fa "struct in_addr" . diff --git a/lib/nbsd_libc/inet/inet6_option_space.3 b/lib/nbsd_libc/inet/inet6_option_space.3 new file mode 100644 index 000000000..c6407ae3f --- /dev/null +++ b/lib/nbsd_libc/inet/inet6_option_space.3 @@ -0,0 +1,448 @@ +.\" $NetBSD: inet6_option_space.3,v 1.3 2010/03/22 19:30:54 joerg Exp $ +.\" $KAME: inet6_option_space.3,v 1.7 2000/05/17 14:32:13 itojun Exp $ +.\" +.\" Copyright (c) 1983, 1987, 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. +.\" +.Dd December 10, 1999 +.Dt INET6_OPTION_SPACE 3 +.Os +.\" +.Sh NAME +.Nm inet6_option_space , +.Nm inet6_option_init , +.Nm inet6_option_append , +.Nm inet6_option_alloc , +.Nm inet6_option_next , +.Nm inet6_option_find +.Nd IPv6 Hop-by-Hop and Destination Options manipulation +.\" +.Sh SYNOPSIS +.In netinet/in.h +.Ft "int" +.Fn inet6_option_space "int nbytes" +.Ft "int" +.Fn inet6_option_init "void *bp" "struct cmsghdr **cmsgp" "int type" +.Ft "int" +.Fn inet6_option_append "struct cmsghdr *cmsg" "const uint8_t *typep" "int multx" "int plusy" +.Ft "uint8_t *" +.Fn inet6_option_alloc "struct cmsghdr *cmsg" "int datalen" "int multx" "int plusy" +.Ft "int" +.Fn inet6_option_next "const struct cmsghdr *cmsg" "uint8_t **tptrp" +.Ft "int" +.Fn inet6_option_find "const struct cmsghdr *cmsg" "uint8_t **tptrp" "int type" +.\" +.Sh DESCRIPTION +.\" +Building and parsing the Hop-by-Hop and Destination options is +complicated due to alignment constraints, padding and +ancillary data manipulation. +RFC 2292 defines a set of functions to help the application. +The function prototypes for +these functions are all in the +.In netinet/in.h +header. +.\" +.Ss inet6_option_space +.Fn inet6_option_space +returns the number of bytes required to hold an option when it is stored as +ancillary data, including the +.Li cmsghdr +structure at the beginning, +and any padding at the end +.Po +to make its size a multiple of 8 bytes +.Pc . +The argument is the size of the structure defining the option, +which must include any pad bytes at the beginning +.Po +the value +.Li y +in the alignment term +.Dq Li xn + y +.Pc , +the type byte, the length byte, and the option data. +.Pp +Note: If multiple options are stored in a single ancillary data +object, which is the recommended technique, this function +overestimates the amount of space required by the size of +.Li N-1 +.Li cmsghdr +structures, +where +.Li N +is the number of options to be stored in the object. +This is of little consequence, since it is assumed that most +Hop-by-Hop option headers and Destination option headers carry only +one option +.Pq appendix B of [RFC 2460] . +.\" +.Ss inet6_option_init +.Fn inet6_option_init +is called once per ancillary data object that will +contain either Hop-by-Hop or Destination options. +It returns +.Li 0 +on success or +.Li -1 +on an error. +.Pp +.Fa bp +is a pointer to previously allocated space that will contain the +ancillary data object. +It must be large enough to contain all the +individual options to be added by later calls to +.Fn inet6_option_append +and +.Fn inet6_option_alloc . +.Pp +.Fa cmsgp +is a pointer to a pointer to a +.Li cmsghdr +structure. +.Fa *cmsgp +is initialized by this function to point to the +.Li cmsghdr +structure constructed by this function in the buffer pointed to by +.Fa bp . +.Pp +.Fa type +is either +.Dv IPV6_HOPOPTS +or +.Dv IPV6_DSTOPTS . +This +.Fa type +is stored in the +.Li cmsg_type +member of the +.Li cmsghdr +structure pointed to by +.Fa *cmsgp . +.\" +.Ss inet6_option_append +This function appends a Hop-by-Hop option or a Destination option +into an ancillary data object that has been initialized by +.Fn inet6_option_init . +This function returns +.Li 0 +if it succeeds or +.Li -1 +on an error. +.Pp +.Fa cmsg +is a pointer to the +.Li cmsghdr +structure that must have been +initialized by +.Fn inet6_option_init . +.Pp +.Fa typep +is a pointer to the 8-bit option type. +It is assumed that this +field is immediately followed by the 8-bit option data length field, +which is then followed immediately by the option data. +The caller +initializes these three fields +.Pq the type-length-value, or TLV +before calling this function. +.Pp +The option type must have a value from +.Li 2 +to +.Li 255 , +inclusive. +.Po +.Li 0 +and +.Li 1 +are reserved for the +.Li Pad1 +and +.Li PadN +options, respectively. +.Pc +.Pp +The option data length must have a value between +.Li 0 +and +.Li 255 , +inclusive, and is the length of the option data that follows. +.Pp +.Fa multx +is the value +.Li x +in the alignment term +.Dq Li xn + y . +It must have a value of +.Li 1 , +.Li 2 , +.Li 4 , +or +.Li 8 . +.Pp +.Fa plusy +is the value +.Li y +in the alignment term +.Dq Li xn + y . +It must have a value between +.Li 0 +and +.Li 7 , +inclusive. +.\" +.Ss inet6_option_alloc +This function appends a Hop-by-Hop option or a Destination option +into an ancillary data object that has been initialized by +.Fn inet6_option_init . +This function returns a pointer to the 8-bit +option type field that starts the option on success, or +.Dv NULL +on an error. +.Pp +The difference between this function and +.Fn inet6_option_append +is that the latter copies the contents of a previously built option into +the ancillary data object while the current function returns a +pointer to the space in the data object where the option's TLV must +then be built by the caller. +.Pp +.Fa cmsg +is a pointer to the +.Li cmsghdr +structure that must have been +initialized by +.Fn inet6_option_init . +.Pp +.Fa datalen +is the value of the option data length byte for this option. +This value is required as an argument to allow the function to +determine if padding must be appended at the end of the option. +.Po +The +.Fn inet6_option_append +function does not need a data length argument +since the option data length must already be stored by the caller. +.Pc +.Pp +.Fa multx +is the value +.Li x +in the alignment term +.Dq Li xn + y . +It must have a value of +.Li 1 , +.Li 2 , +.Li 4 , +or +.Li 8 . +.Pp +.Fa plusy +is the value +.Li y +in the alignment term +.Dq Li xn + y . +It must have a value between +.Li 0 +and +.Li 7 , +inclusive. +.\" +.Ss inet6_option_next +This function processes the next Hop-by-Hop option or Destination +option in an ancillary data object. +If another option remains to be +processed, the return value of the function is +.Li 0 +and +.Fa *tptrp +points to +the 8-bit option type field +.Po +which is followed by the 8-bit option +data length, followed by the option data +.Pc . +If no more options remain +to be processed, the return value is +.Li -1 +and +.Fa *tptrp +is +.Dv NULL . +If an error occurs, the return value is +.Li -1 +and +.Fa *tptrp +is not +.Dv NULL . +.Pp +.Fa cmsg +is a pointer to +.Li cmsghdr +structure of which +.Li cmsg_level +equals +.Dv IPPROTO_IPV6 +and +.Li cmsg_type +equals either +.Dv IPV6_HOPOPTS +or +.Dv IPV6_DSTOPTS . +.Pp +.Fa tptrp +is a pointer to a pointer to an 8-bit byte and +.Fa *tptrp +is used +by the function to remember its place in the ancillary data object +each time the function is called. +The first time this function is +called for a given ancillary data object, +.Fa *tptrp +must be set to +.Dv NULL . +.Pp +Each time this function returns success, +.Fa *tptrp +points to the 8-bit +option type field for the next option to be processed. +.\" +.Ss inet6_option_find +This function is similar to the previously described +.Fn inet6_option_next +function, except this function lets the caller +specify the option type to be searched for, instead of always +returning the next option in the ancillary data object. +.Fa cmsg +is a +pointer to +.Li cmsghdr +structure of which +.Li cmsg_level +equals +.Dv IPPROTO_IPV6 +and +.Li cmsg_type +equals either +.Dv IPV6_HOPOPTS +or +.Dv IPV6_DSTOPTS . +.Pp +.Fa tptrp +is a pointer to a pointer to an 8-bit byte and +.Fa *tptrp +is used +by the function to remember its place in the ancillary data object +each time the function is called. +The first time this function is +called for a given ancillary data object, +.Fa *tptrp +must be set to +.Dv NULL . +.Pa +This function starts searching for an option of the specified type +beginning after the value of +.Fa *tptrp . +If an option of the specified +type is located, this function returns +.Li 0 +and +.Fa *tptrp +points to the 8- +bit option type field for the option of the specified type. +If an +option of the specified type is not located, the return value is +.Li -1 +and +.Fa *tptrp +is +.Dv NULL . +If an error occurs, the return value is +.Li -1 +and +.Fa *tptrp +is not +.Dv NULL . +.\" +.Sh EXAMPLES +RFC 2292 gives comprehensive examples in chapter 6. +.\" +.Sh DIAGNOSTICS +.Fn inet6_option_init +and +.Fn inet6_option_append +return +.Li 0 +on success or +.Li -1 +on an error. +.Pp +.Fn inet6_option_alloc +returns +.Dv NULL +on an error. +.Pp +On errors, +.Fn inet6_option_next +and +.Fn inet6_option_find +return +.Li -1 +setting +.Fa *tptrp +to non +.Dv NULL +value. +.\" +.Sh SEE ALSO +.Rs +.%A W. Stevens +.%A M. Thomas +.%T "Advanced Sockets API for IPv6" +.%N RFC 2292 +.%D February 1998 +.Re +.Rs +.%A S. Deering +.%A R. Hinden +.%T "Internet Protocol, Version 6 (IPv6) Specification" +.%N RFC 2460 +.%D December 1998 +.Re +.\" +.Sh STANDARDS +The functions +are documented in +.Dq Advanced Sockets API for IPv6 +.Pq RFC 2292 . +.\" +.Sh HISTORY +The implementation first appeared in KAME advanced networking kit. +.\" +.Sh BUGS +The text was shamelessly copied from RFC 2292. diff --git a/lib/nbsd_libc/inet/inet6_rthdr_space.3 b/lib/nbsd_libc/inet/inet6_rthdr_space.3 new file mode 100644 index 000000000..50f852cc4 --- /dev/null +++ b/lib/nbsd_libc/inet/inet6_rthdr_space.3 @@ -0,0 +1,321 @@ +.\" $NetBSD: inet6_rthdr_space.3,v 1.2 2010/03/22 19:30:54 joerg Exp $ +.\" $KAME: inet6_rthdr_space.3,v 1.8 2000/05/17 14:30:15 itojun Exp $ +.\" +.\" Copyright (c) 1983, 1987, 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. +.\" +.Dd December 10, 1999 +.Dt INET6_RTHDR_SPACE 3 +.Os +.\" +.Sh NAME +.Nm inet6_rthdr_space , +.Nm inet6_rthdr_init , +.Nm inet6_rthdr_add , +.Nm inet6_rthdr_lasthop , +.Nm inet6_rthdr_reverse , +.Nm inet6_rthdr_segments , +.Nm inet6_rthdr_getaddr , +.Nm inet6_rthdr_getflags +.Nd IPv6 Routing Header Options manipulation +.\" +.Sh SYNOPSIS +.In netinet/in.h +.Ft size_t +.Fn inet6_rthdr_space "int type" "int segments" +.Ft "struct cmsghdr *" +.Fn inet6_rthdr_init "void *bp" "int type" +.Ft int +.Fn inet6_rthdr_add "struct cmsghdr *cmsg" "const struct in6_addr *addr" "unsigned int flags" +.Ft int +.Fn inet6_rthdr_lasthop "struct cmsghdr *cmsg" "unsigned int flags" +.Ft int +.Fn inet6_rthdr_reverse "const struct cmsghdr *in" "struct cmsghdr *out" +.Ft int +.Fn inet6_rthdr_segments "const struct cmsghdr *cmsg" +.Ft "struct in6_addr *" +.Fn inet6_rthdr_getaddr "struct cmsghdr *cmsg" "int index" +.Ft int +.Fn inet6_rthdr_getflags "const struct cmsghdr *cmsg" "int index" +.\" +.Sh DESCRIPTION +RFC 2292 IPv6 advanced API defines eight +functions that the application calls to build and examine a Routing +header. +Four functions build a Routing header: +.Bl -hang +.It Fn inet6_rthdr_space +return #bytes required for ancillary data +.It Fn inet6_rthdr_init +initialize ancillary data for Routing header +.It Fn inet6_rthdr_add +add IPv6 address \*[Am] flags to Routing header +.It Fn inet6_rthdr_lasthop +specify the flags for the final hop +.El +.Pp +Four functions deal with a returned Routing header: +.Bl -hang +.It Fn inet6_rthdr_reverse +reverse a Routing header +.It Fn inet6_rthdr_segments +return #segments in a Routing header +.It Fn inet6_rthdr_getaddr +fetch one address from a Routing header +.It Fn inet6_rthdr_getflags +fetch one flag from a Routing header +.El +.Pp +The function prototypes for these functions are all in the +.In netinet/in.h +header. +.\" +.Ss inet6_rthdr_space +This function returns the number of bytes required to hold a Routing +header of the specified +.Fa type +containing the specified number of +.Fa segments +.Pq addresses . +For an IPv6 Type 0 Routing header, the number +of segments must be between 1 and 23, inclusive. +The return value +includes the size of the cmsghdr structure that precedes the Routing +header, and any required padding. +.Pp +If the return value is 0, then either the type of the Routing header +is not supported by this implementation or the number of segments is +invalid for this type of Routing header. +.Pp +Note: This function returns the size but does not allocate the space +required for the ancillary data. +This allows an application to +allocate a larger buffer, if other ancillary data objects are +desired, since all the ancillary data objects must be specified to +.Xr sendmsg 2 +as a single +.Li msg_control +buffer. +.\" +.Ss inet6_rthdr_init +This function initializes the buffer pointed to by +.Fa bp +to contain a +.Li cmsghdr +structure followed by a Routing header of the specified +.Fa type . +The +.Li cmsg_len +member of the +.Li cmsghdr +structure is initialized to the +size of the structure plus the amount of space required by the +Routing header. +The +.Li cmsg_level +and +.Li cmsg_type +members are also initialized as required. +.Pp +The caller must allocate the buffer and its size can be determined by +calling +.Fn inet6_rthdr_space . +.Pp +Upon success the return value is the pointer to the +.Li cmsghdr +structure, and this is then used as the first argument to the next +two functions. +Upon an error the return value is +.Dv NULL . +.\" +.Ss inet6_rthdr_add +This function adds the address pointed to by +.Fa addr +to the end of the +Routing header being constructed and sets the type of this hop to the +value of +.Fa flags . +For an IPv6 Type 0 Routing header, +.Fa flags +must be +either +.Dv IPV6_RTHDR_LOOSE +or +.Dv IPV6_RTHDR_STRICT . +.Pp +If successful, the +.Li cmsg_len +member of the +.Li cmsghdr +structure is +updated to account for the new address in the Routing header and the +return value of the function is 0. +Upon an error the return value of +the function is -1. +.\" +.Ss inet6_rthdr_lasthop +This function specifies the Strict/Loose flag for the final hop of a +Routing header. +For an IPv6 Type 0 Routing header, +.Fa flags +must be either +.Dv IPV6_RTHDR_LOOSE +or +.Dv IPV6_RTHDR_STRICT . +.Pp +The return value of the function is 0 upon success, or -1 upon an error. +.Pp +Notice that a Routing header specifying +.Li N +intermediate nodes requires +.Li N+1 +Strict/Loose flags. +This requires +.Li N +calls to +.Fn inet6_rthdr_add +followed by one call to +.Fn inet6_rthdr_lasthop . +.\" +.Ss inet6_rthdr_reverse +This function takes a Routing header that was received as ancillary +data +.Po +pointed to by the first argument, +.Fa in +.Pc +and writes a new Routing +header that sends datagrams along the reverse of that route. +Both +arguments are allowed to point to the same buffer +.Pq that is, the reversal can occur in place . +.Pp +The return value of the function is 0 on success, or -1 upon an +error. +.\" +.Ss inet6_rthdr_segments +This function returns the number of segments +.Pq addresses +contained in +the Routing header described by +.Fa cmsg . +On success the return value is +between 1 and 23, inclusive. +The return value of the function is -1 upon an error. +.\" +.Ss inet6_rthdr_getaddr +This function returns a pointer to the IPv6 address specified by +.Fa index +.Po +which must have a value between 1 and the value returned by +.Fn inet6_rthdr_segments +.Pc +in the Routing header described by +.Fa cmsg . +An +application should first call +.Fn inet6_rthdr_segments +to obtain the number of segments in the Routing header. +.Pp +Upon an error the return value of the function is +.Dv NULL . +.\" +.Ss inet6_rthdr_getflags +This function returns the flags value specified by +.Fa index +.Po +which must +have a value between 0 and the value returned by +.Fn inet6_rthdr_segments +.Pc +in the Routing header described by +.Fa cmsg . +For an IPv6 Type 0 Routing header the return value will be either +.Dv IPV6_RTHDR_LOOSE +or +.Dv IPV6_RTHDR_STRICT . +.Pp +Upon an error the return value of the function is -1. +.Pp +Note: Addresses are indexed starting at 1, and flags starting at 0, +to maintain consistency with the terminology and figures in RFC 2460. +.\" +.Sh EXAMPLES +RFC 2292 gives comprehensive examples in chapter 8. +.\" +.Sh DIAGNOSTICS +.Fn inet6_rthdr_space +returns 0 on errors. +.Pp +.Fn inet6_rthdr_add , +.Fn inet6_rthdr_lasthop +and +.Fn inet6_rthdr_reverse +return 0 on success, and returns -1 on error. +.Pp +.Fn inet6_rthdr_init +and +.Fn inet6_rthdr_getaddr +return +.Dv NULL +on error. +.Pp +.Fn inet6_rthdr_segments +and +.Fn inet6_rthdr_getflags +return -1 on error. +.\" +.Sh SEE ALSO +.Rs +.%A W. Stevens +.%A M. Thomas +.%T "Advanced Sockets API for IPv6" +.%N RFC 2292 +.%D February 1998 +.Re +.Rs +.%A S. Deering +.%A R. Hinden +.%T "Internet Protocol, Version 6 (IPv6) Specification" +.%N RFC 2460 +.%D December 1998 +.Re +.\" +.Sh STANDARDS +The functions +are documented in +.Dq Advanced Sockets API for IPv6 +.Pq RFC 2292 . +.\" +.Sh HISTORY +The implementation first appeared in KAME advanced networking kit. +.\" +.Sh BUGS +The text was shamelessly copied from RFC 2292. +.Pp +.Fn inet6_rthdr_reverse +is not implemented yet. diff --git a/lib/nbsd_libc/inet/inet_cidr_ntop.c b/lib/nbsd_libc/inet/inet_cidr_ntop.c new file mode 100644 index 000000000..bd8877227 --- /dev/null +++ b/lib/nbsd_libc/inet/inet_cidr_ntop.c @@ -0,0 +1,275 @@ +/* $NetBSD: inet_cidr_ntop.c,v 1.7 2009/04/12 17:07:16 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1998,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: inet_cidr_ntop.c,v 1.7 2006/10/11 02:18:18 marka Exp"; +#else +__RCSID("$NetBSD: inet_cidr_ntop.c,v 1.7 2009/04/12 17:07:16 christos Exp $"); +#endif +#endif + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_cidr_ntop,_inet_cidr_ntop) +#endif + +#ifdef SPRINTF_CHAR +# define SPRINTF(x) strlen(sprintf/**/x) +#else +# define SPRINTF(x) ((size_t)sprintf x) +#endif + +static char * +inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size); +static char * +inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size); + +/*% + * char * + * inet_cidr_ntop(af, src, bits, dst, size) + * convert network address from network to presentation format. + * "src"'s size is determined from its "af". + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * note: + * 192.5.5.1/28 has a nonzero host part, which means it isn't a network + * as called for by inet_net_ntop() but it can be a host address with + * an included netmask. + * author: + * Paul Vixie (ISC), October 1998 + */ +char * +inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size) { + switch (af) { + case AF_INET: + return (inet_cidr_ntop_ipv4(src, bits, dst, size)); + case AF_INET6: + return (inet_cidr_ntop_ipv6(src, bits, dst, size)); + default: + errno = EAFNOSUPPORT; + return (NULL); + } +} + +static int +decoct(const u_char *src, size_t bytes, char *dst, size_t size) { + char *odst = dst; + char *t; + size_t b; + + for (b = 1; b <= bytes; b++) { + if (size < sizeof "255.") + return (0); + t = dst; + dst += SPRINTF((dst, "%u", *src++)); + if (b != bytes) { + *dst++ = '.'; + *dst = '\0'; + } + size -= (size_t)(dst - t); + } + return (dst - odst); +} + +/*% + * static char * + * inet_cidr_ntop_ipv4(src, bits, dst, size) + * convert IPv4 network address from network to presentation format. + * "src"'s size is determined from its "af". + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * note: + * network byte order assumed. this means 192.5.5.240/28 has + * 0b11110000 in its fourth octet. + * author: + * Paul Vixie (ISC), October 1998 + */ +static char * +inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) { + char *odst = dst; + size_t len = 4; + size_t b; + size_t bytes; + + if ((bits < -1) || (bits > 32)) { + errno = EINVAL; + return (NULL); + } + + /* Find number of significant bytes in address. */ + if (bits == -1) + len = 4; + else + for (len = 1, b = 1 ; b < 4U; b++) + if (*(src + b)) + len = b + 1; + + /* Format whole octets plus nonzero trailing octets. */ + bytes = (((bits <= 0) ? 1 : bits) + 7) / 8; + if (len > bytes) + bytes = len; + b = decoct(src, bytes, dst, size); + if (b == 0U) + goto emsgsize; + dst += b; + size -= b; + + if (bits != -1) { + /* Format CIDR /width. */ + if (size < sizeof "/32") + goto emsgsize; + dst += SPRINTF((dst, "/%u", bits)); + } + + return (odst); + + emsgsize: + errno = EMSGSIZE; + return (NULL); +} + +static char * +inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) { + /* + * Note that int32_t and int16_t need only be "at least" large enough + * to contain a value of the specified size. On some systems, like + * Crays, there is no such thing as an integer variable with 16 bits. + * Keep this in mind if you think this function should have been coded + * to use pointer overlays. All the world's not a VAX. + */ + char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"]; + char *tp; + struct { int base, len; } best, cur; + u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; + int i; + + if ((bits < -1) || (bits > 128)) { + errno = EINVAL; + return (NULL); + } + + /* + * Preprocess: + * Copy the input (bytewise) array into a wordwise array. + * Find the longest run of 0x00's in src[] for :: shorthanding. + */ + memset(words, '\0', sizeof words); + for (i = 0; i < NS_IN6ADDRSZ; i++) + words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); + best.base = -1; + best.len = 0; + cur.base = -1; + cur.len = 0; + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + if (words[i] == 0) { + if (cur.base == -1) + cur.base = i, cur.len = 1; + else + cur.len++; + } else { + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + cur.base = -1; + } + } + } + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + } + if (best.base != -1 && best.len < 2) + best.base = -1; + + /* + * Format the result. + */ + tp = tmp; + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + /* Are we inside the best run of 0x00's? */ + if (best.base != -1 && i >= best.base && + i < (best.base + best.len)) { + if (i == best.base) + *tp++ = ':'; + continue; + } + /* Are we following an initial run of 0x00s or any real hex? */ + if (i != 0) + *tp++ = ':'; + /* Is this address an encapsulated IPv4? */ + if (i == 6 && best.base == 0 && (best.len == 6 || + (best.len == 7 && words[7] != 0x0001) || + (best.len == 5 && words[5] == 0xffff))) { + size_t n; + + if (src[15] || bits == -1 || bits > 120) + n = 4; + else if (src[14] || bits > 112) + n = 3; + else + n = 2; + n = decoct(src+12, n, tp, sizeof tmp - (tp - tmp)); + if (n == 0) { + errno = EMSGSIZE; + return (NULL); + } + tp += strlen(tp); + break; + } + tp += SPRINTF((tp, "%x", words[i])); + } + + /* Was it a trailing run of 0x00's? */ + if (best.base != -1 && (best.base + best.len) == + (NS_IN6ADDRSZ / NS_INT16SZ)) + *tp++ = ':'; + *tp = '\0'; + + if (bits != -1) + tp += SPRINTF((tp, "/%u", bits)); + + /* + * Check for overflow, copy, and we're done. + */ + if ((size_t)(tp - tmp) > size) { + errno = EMSGSIZE; + return (NULL); + } + strcpy(dst, tmp); + return (dst); +} + +/*! \file */ diff --git a/lib/nbsd_libc/inet/inet_cidr_pton.c b/lib/nbsd_libc/inet/inet_cidr_pton.c new file mode 100644 index 000000000..1ffbc9acf --- /dev/null +++ b/lib/nbsd_libc/inet/inet_cidr_pton.c @@ -0,0 +1,289 @@ +/* $NetBSD: inet_cidr_pton.c,v 1.6 2009/04/12 17:07:16 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1998,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp"; +#else +__RCSID("$NetBSD: inet_cidr_pton.c,v 1.6 2009/04/12 17:07:16 christos Exp $"); +#endif +#endif + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef SPRINTF_CHAR +# define SPRINTF(x) strlen(sprintf/**/x) +#else +# define SPRINTF(x) ((size_t)sprintf x) +#endif + +#ifdef __weak_alias +__weak_alias(inet_cidr_pton,_inet_cidr_pton) +#endif + +static int inet_cidr_pton_ipv4 __P((const char *src, u_char *dst, + int *bits, int ipv6)); +static int inet_cidr_pton_ipv6 __P((const char *src, u_char *dst, + int *bits)); + +static int getbits(const char *, int ipv6); + +/*% + * int + * inet_cidr_pton(af, src, dst, *bits) + * convert network address from presentation to network format. + * accepts inet_pton()'s input for this "af" plus trailing "/CIDR". + * "dst" is assumed large enough for its "af". "bits" is set to the + * /CIDR prefix length, which can have defaults (like /32 for IPv4). + * return: + * -1 if an error occurred (inspect errno; ENOENT means bad format). + * 0 if successful conversion occurred. + * note: + * 192.5.5.1/28 has a nonzero host part, which means it isn't a network + * as called for by inet_net_pton() but it can be a host address with + * an included netmask. + * author: + * Paul Vixie (ISC), October 1998 + */ +int +inet_cidr_pton(int af, const char *src, void *dst, int *bits) { + switch (af) { + case AF_INET: + return (inet_cidr_pton_ipv4(src, dst, bits, 0)); + case AF_INET6: + return (inet_cidr_pton_ipv6(src, dst, bits)); + default: + errno = EAFNOSUPPORT; + return (-1); + } +} + +static const char digits[] = "0123456789"; + +static int +inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) { + const u_char *odst = dst; + int n, ch, tmp, bits; + size_t size = 4; + + /* Get the mantissa. */ + while (ch = *src++, (isascii(ch) && isdigit(ch))) { + tmp = 0; + do { + n = strchr(digits, ch) - digits; + INSIST(n >= 0 && n <= 9); + tmp *= 10; + tmp += n; + if (tmp > 255) + goto enoent; + } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch)); + if (size-- == 0U) + goto emsgsize; + *dst++ = (u_char) tmp; + if (ch == '\0' || ch == '/') + break; + if (ch != '.') + goto enoent; + } + + /* Get the prefix length if any. */ + bits = -1; + if (ch == '/' && dst > odst) { + bits = getbits(src, ipv6); + if (bits == -2) + goto enoent; + } else if (ch != '\0') + goto enoent; + + /* Prefix length can default to /32 only if all four octets spec'd. */ + if (bits == -1) { + if (dst - odst == 4) + bits = ipv6 ? 128 : 32; + else + goto enoent; + } + + /* If nothing was written to the destination, we found no address. */ + if (dst == odst) + goto enoent; + + /* If prefix length overspecifies mantissa, life is bad. */ + if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst)) + goto enoent; + + /* Extend address to four octets. */ + while (size-- > 0U) + *dst++ = 0; + + *pbits = bits; + return (0); + + enoent: + errno = ENOENT; + return (-1); + + emsgsize: + errno = EMSGSIZE; + return (-1); +} + +static int +inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) { + static const char xdigits_l[] = "0123456789abcdef", + xdigits_u[] = "0123456789ABCDEF"; + u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; + const char *xdigits, *curtok; + int ch, saw_xdigit; + u_int val; + int bits; + + memset((tp = tmp), '\0', NS_IN6ADDRSZ); + endp = tp + NS_IN6ADDRSZ; + colonp = NULL; + /* Leading :: requires some special handling. */ + if (*src == ':') + if (*++src != ':') + return (0); + curtok = src; + saw_xdigit = 0; + val = 0; + bits = -1; + while ((ch = *src++) != '\0') { + const char *pch; + + if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) + pch = strchr((xdigits = xdigits_u), ch); + if (pch != NULL) { + val <<= 4; + val |= (pch - xdigits); + if (val > 0xffff) + return (0); + saw_xdigit = 1; + continue; + } + if (ch == ':') { + curtok = src; + if (!saw_xdigit) { + if (colonp) + return (0); + colonp = tp; + continue; + } else if (*src == '\0') { + return (0); + } + if (tp + NS_INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + saw_xdigit = 0; + val = 0; + continue; + } + if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && + inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) { + tp += NS_INADDRSZ; + saw_xdigit = 0; + break; /*%< '\\0' was seen by inet_pton4(). */ + } + if (ch == '/') { + bits = getbits(src, 1); + if (bits == -2) + goto enoent; + break; + } + goto enoent; + } + if (saw_xdigit) { + if (tp + NS_INT16SZ > endp) + goto emsgsize; + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + } + if (colonp != NULL) { + /* + * Since some memmove()'s erroneously fail to handle + * overlapping regions, we'll do the shift by hand. + */ + const int n = tp - colonp; + int i; + + if (tp == endp) + goto enoent; + for (i = 1; i <= n; i++) { + endp[- i] = colonp[n - i]; + colonp[n - i] = 0; + } + tp = endp; + } + + memcpy(dst, tmp, NS_IN6ADDRSZ); + + *pbits = bits; + return (0); + + enoent: + errno = ENOENT; + return (-1); + + emsgsize: + errno = EMSGSIZE; + return (-1); +} + +static int +getbits(const char *src, int ipv6) { + int bits = 0; + char *cp, ch; + + if (*src == '\0') /*%< syntax */ + return (-2); + do { + ch = *src++; + cp = strchr(digits, ch); + if (cp == NULL) /*%< syntax */ + return (-2); + bits *= 10; + bits += cp - digits; + if (bits == 0 && *src != '\0') /*%< no leading zeros */ + return (-2); + if (bits > (ipv6 ? 128 : 32)) /*%< range error */ + return (-2); + } while (*src != '\0'); + + return (bits); +} + +/*! \file */ diff --git a/lib/nbsd_libc/inet/inet_lnaof.c b/lib/nbsd_libc/inet/inet_lnaof.c new file mode 100644 index 000000000..3f66cbef7 --- /dev/null +++ b/lib/nbsd_libc/inet/inet_lnaof.c @@ -0,0 +1,66 @@ +/* $NetBSD: inet_lnaof.c,v 1.1 2004/05/20 23:13:02 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)inet_lnaof.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: inet_lnaof.c,v 1.1 2004/05/20 23:13:02 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet_lnaof,_inet_lnaof) +#endif + +/* + * Return the local network address portion of an + * internet address; handles class a/b/c network + * number formats. + */ +in_addr_t +inet_lnaof(struct in_addr in) +{ + in_addr_t i = ntohl(in.s_addr); + + if (IN_CLASSA(i)) + return ((i)&IN_CLASSA_HOST); + else if (IN_CLASSB(i)) + return ((i)&IN_CLASSB_HOST); + else + return ((i)&IN_CLASSC_HOST); +} diff --git a/lib/nbsd_libc/inet/inet_makeaddr.c b/lib/nbsd_libc/inet/inet_makeaddr.c new file mode 100644 index 000000000..0dfdd439b --- /dev/null +++ b/lib/nbsd_libc/inet/inet_makeaddr.c @@ -0,0 +1,70 @@ +/* $NetBSD: inet_makeaddr.c,v 1.1 2004/05/20 23:13:02 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)inet_makeaddr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: inet_makeaddr.c,v 1.1 2004/05/20 23:13:02 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet_makeaddr,_inet_makeaddr) +#endif + +/* + * Formulate an Internet address from network + host. Used in + * building addresses stored in the ifnet structure. + */ +struct in_addr +inet_makeaddr(in_addr_t net, in_addr_t host) +{ + in_addr_t addr; + struct in_addr ret; + + if (net < 128) + addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); + else if (net < 65536) + addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST); + else if (net < 16777216L) + addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST); + else + addr = net | host; + ret.s_addr = htonl(addr); + return ret; +} diff --git a/lib/nbsd_libc/inet/inet_net.3 b/lib/nbsd_libc/inet/inet_net.3 new file mode 100644 index 000000000..324955b6e --- /dev/null +++ b/lib/nbsd_libc/inet/inet_net.3 @@ -0,0 +1,154 @@ +.\" $NetBSD: inet_net.3,v 1.2 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 1997 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Luke Mewburn. +.\" +.\" 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 December 8, 2001 +.Dt INET_NET 3 +.Os +.Sh NAME +.Nm inet_net_ntop , +.Nm inet_net_pton +.Nd Internet network number manipulation routines +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/socket.h +.In netinet/in.h +.In arpa/inet.h +.Ft char * +.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size" +.Ft int +.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size" +.Sh DESCRIPTION +The +.Fn inet_net_ntop +function converts an Internet network number from network format (usually a +.Ft struct in_addr +or some other binary form, in network byte order) to CIDR presentation format +(suitable for external display purposes). +.Fa bits +is the number of bits in +.Fa src +that are the network number. +It returns NULL if a system error occurs (in which case, +.Va errno +will have been set), or it returns a pointer to the destination string. +.Pp +The +.Fn inet_net_pton +function converts a presentation format Internet network number (that is, +printable form as held in a character string) to network format (usually a +.Ft struct in_addr +or some other internal binary representation, in network byte order). +It returns the number of bits (either computed based on the class, or +specified with /CIDR), or -1 if a failure occurred +(in which case +.Va errno +will have been set. +It will be set to +.Er ENOENT +if the Internet network number was not valid). +.Pp +The currently supported values for +.Fa af +are +.Dv AF_INET +and +.Dv AF_INET6 . +.Fa size +is the size of the result buffer +.Fa dst . +.Sh NETWORK NUMBERS (IP VERSION 4) +Internet network numbers may be specified in one of the following forms: +.Bd -literal -offset indent +a.b.c.d/bits +a.b.c.d +a.b.c +a.b +a +.Ed +.Pp +When four parts are specified, each is interpreted +as a byte of data and assigned, from left to right, +to the four bytes of an Internet network number. Note +that when an Internet network number is viewed as a 32-bit +integer quantity on a system that uses little-endian +byte order (such as the +.Tn Intel 386, 486 +and +.Tn Pentium +processors) the bytes referred to above appear as +.Dq Li d.c.b.a . +That is, little-endian bytes are ordered from right to left. +.Pp +When a three part number is specified, the last +part is interpreted as a 16-bit quantity and placed +in the right-most two bytes of the Internet network number. +This makes the three part number format convenient +for specifying Class B network numbers as +.Dq Li 128.net.host . +.Pp +When a two part number is supplied, the last part +is interpreted as a 24-bit quantity and placed in +the right most three bytes of the Internet network number. +This makes the two part number format convenient +for specifying Class A network numbers as +.Dq Li net.host . +.Pp +When only one part is given, the value is stored +directly in the Internet network number without any byte +rearrangement. +.Pp +All numbers supplied as +.Dq parts +in a +.Ql \&. +notation +may be decimal, octal, or hexadecimal, as specified +in the C language (i.e., a leading 0x or 0X implies +hexadecimal; otherwise, a leading 0 implies octal; +otherwise, the number is interpreted as decimal). +.\" +.\" .Sh NETWORK NUMBERS (IP VERSION 6) +.\" XXX - document this! +.\" +.Sh SEE ALSO +.Xr byteorder 3 , +.Xr inet 3 , +.Xr networks 5 +.Sh HISTORY +The +.Nm inet_net_ntop +and +.Nm inet_net_pton +functions appeared in BIND 4.9.4 and thence +.Nx 1.3 . +Support for +.Dv AF_INET6 +appeared in +.Nx 1.6 . diff --git a/lib/nbsd_libc/inet/inet_net_ntop.c b/lib/nbsd_libc/inet/inet_net_ntop.c new file mode 100644 index 000000000..b5ef9ccfb --- /dev/null +++ b/lib/nbsd_libc/inet/inet_net_ntop.c @@ -0,0 +1,287 @@ +/* + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM 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 +#if defined(LIBC_SCCS) && !defined(lint) +#ifdef notdef +static const char rcsid[] = "Id: inet_net_ntop.c,v 1.1.2.1 2002/08/02 02:17:21 marka Exp "; +#else +__RCSID("$NetBSD: inet_net_ntop.c,v 1.2 2009/02/07 07:25:22 lukem Exp $"); +#endif +#endif + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_net_ntop,_inet_net_ntop) +#endif + +#ifdef SPRINTF_CHAR +# define SPRINTF(x) strlen(sprintf/**/x) +#else +# define SPRINTF(x) sprintf x +#endif + +static char * inet_net_ntop_ipv4 __P((const u_char *src, int bits, + char *dst, size_t size)); +static char * inet_net_ntop_ipv6 __P((const u_char *src, int bits, + char *dst, size_t size)); + +/* + * char * + * inet_net_ntop(af, src, bits, dst, size) + * convert network number from network to presentation format. + * generates CIDR style result always. + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * author: + * Paul Vixie (ISC), July 1996 + */ +char * +inet_net_ntop(af, src, bits, dst, size) + int af; + const void *src; + int bits; + char *dst; + size_t size; +{ + switch (af) { + case AF_INET: + return (inet_net_ntop_ipv4(src, bits, dst, size)); + case AF_INET6: + return (inet_net_ntop_ipv6(src, bits, dst, size)); + default: + errno = EAFNOSUPPORT; + return (NULL); + } +} + +/* + * static char * + * inet_net_ntop_ipv4(src, bits, dst, size) + * convert IPv4 network number from network to presentation format. + * generates CIDR style result always. + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * note: + * network byte order assumed. this means 192.5.5.240/28 has + * 0b11110000 in its fourth octet. + * author: + * Paul Vixie (ISC), July 1996 + */ +static char * +inet_net_ntop_ipv4(src, bits, dst, size) + const u_char *src; + int bits; + char *dst; + size_t size; +{ + char *odst = dst; + char *t; + u_int m; + int b; + + if (bits < 0 || bits > 32) { + errno = EINVAL; + return (NULL); + } + + if (bits == 0) { + if (size < sizeof "0") + goto emsgsize; + *dst++ = '0'; + size--; + *dst = '\0'; + } + + /* Format whole octets. */ + for (b = bits / 8; b > 0; b--) { + if (size <= sizeof "255.") + goto emsgsize; + t = dst; + dst += SPRINTF((dst, "%u", *src++)); + if (b > 1) { + *dst++ = '.'; + *dst = '\0'; + } + size -= (size_t)(dst - t); + } + + /* Format partial octet. */ + b = bits % 8; + if (b > 0) { + if (size <= sizeof ".255") + goto emsgsize; + t = dst; + if (dst != odst) + *dst++ = '.'; + m = ((1 << b) - 1) << (8 - b); + dst += SPRINTF((dst, "%u", *src & m)); + size -= (size_t)(dst - t); + } + + /* Format CIDR /width. */ + if (size <= sizeof "/32") + goto emsgsize; + dst += SPRINTF((dst, "/%u", bits)); + return (odst); + + emsgsize: + errno = EMSGSIZE; + return (NULL); +} + +/* + * static char * + * inet_net_ntop_ipv6(src, bits, fakebits, dst, size) + * convert IPv6 network number from network to presentation format. + * generates CIDR style result always. Picks the shortest representation + * unless the IP is really IPv4. + * always prints specified number of bits (bits). + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * note: + * network byte order assumed. this means 192.5.5.240/28 has + * 0x11110000 in its fourth octet. + * author: + * Vadim Kogan (UCB), June 2001 + * Original version (IPv4) by Paul Vixie (ISC), July 1996 + */ + +static char * +inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) { + u_int m; + int b; + size_t p; + size_t zero_s, zero_l, tmp_zero_s, tmp_zero_l; + size_t i; + int is_ipv4 = 0; + unsigned char inbuf[16]; + char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; + char *cp; + size_t words; + u_char *s; + + if (bits < 0 || bits > 128) { + errno = EINVAL; + return (NULL); + } + + cp = outbuf; + + if (bits == 0) { + *cp++ = ':'; + *cp++ = ':'; + *cp = '\0'; + } else { + /* Copy src to private buffer. Zero host part. */ + p = (bits + 7) / 8; + memcpy(inbuf, src, p); + memset(inbuf + p, 0, 16 - p); + b = bits % 8; + if (b != 0) { + m = ~0 << (8 - b); + inbuf[p-1] &= m; + } + + s = inbuf; + + /* how many words need to be displayed in output */ + words = (bits + 15) / 16; + if (words == 1) + words = 2; + + /* Find the longest substring of zero's */ + zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0; + for (i = 0; i < (words * 2); i += 2) { + if ((s[i] | s[i+1]) == 0) { + if (tmp_zero_l == 0) + tmp_zero_s = i / 2; + tmp_zero_l++; + } else { + if (tmp_zero_l && zero_l < tmp_zero_l) { + zero_s = tmp_zero_s; + zero_l = tmp_zero_l; + tmp_zero_l = 0; + } + } + } + + if (tmp_zero_l && zero_l < tmp_zero_l) { + zero_s = tmp_zero_s; + zero_l = tmp_zero_l; + } + + if (zero_l != words && zero_s == 0 && ((zero_l == 6) || + ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) || + ((zero_l == 7 && s[14] != 0 && s[15] != 1))))) + is_ipv4 = 1; + + /* Format whole words. */ + for (p = 0; p < words; p++) { + if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) { + /* Time to skip some zeros */ + if (p == zero_s) + *cp++ = ':'; + if (p == words - 1) + *cp++ = ':'; + s++; + s++; + continue; + } + + if (is_ipv4 && p > 5 ) { + *cp++ = (p == 6) ? ':' : '.'; + cp += SPRINTF((cp, "%u", *s++)); + /* we can potentially drop the last octet */ + if (p != 7 || bits > 120) { + *cp++ = '.'; + cp += SPRINTF((cp, "%u", *s++)); + } + } else { + if (cp != outbuf) + *cp++ = ':'; + cp += SPRINTF((cp, "%x", *s * 256 + s[1])); + s += 2; + } + } + } + /* Format CIDR /width. */ + (void)SPRINTF((cp, "/%u", bits)); + if (strlen(outbuf) + 1 > size) + goto emsgsize; + strcpy(dst, outbuf); + + return (dst); + +emsgsize: + errno = EMSGSIZE; + return (NULL); +} diff --git a/lib/nbsd_libc/inet/inet_net_pton.c b/lib/nbsd_libc/inet/inet_net_pton.c new file mode 100644 index 000000000..fda0cc3d1 --- /dev/null +++ b/lib/nbsd_libc/inet/inet_net_pton.c @@ -0,0 +1,412 @@ +/* + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: inet_net_pton.c,v 1.4.2.1 2002/08/02 02:17:21 marka Exp "; +#else +__RCSID("$NetBSD: inet_net_pton.c,v 1.1 2004/05/20 23:13:02 christos Exp $"); +#endif +#endif + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_net_pton,_inet_net_pton) +#endif + +#ifdef SPRINTF_CHAR +# define SPRINTF(x) strlen(sprintf/**/x) +#else +# define SPRINTF(x) ((size_t)sprintf x) +#endif + + +/* + * static int + * inet_net_pton_ipv4(src, dst, size) + * convert IPv4 network number from presentation to network format. + * accepts hex octets, hex strings, decimal octets, and /CIDR. + * "size" is in bytes and describes "dst". + * return: + * number of bits, either imputed classfully or specified with /CIDR, + * or -1 if some failure occurred (check errno). ENOENT means it was + * not an IPv4 network specification. + * note: + * network byte order assumed. this means 192.5.5.240/28 has + * 0b11110000 in its fourth octet. + * author: + * Paul Vixie (ISC), June 1996 + */ +static int +inet_net_pton_ipv4( const char *src, u_char *dst, size_t size) { + static const char xdigits[] = "0123456789abcdef"; + static const char digits[] = "0123456789"; + int n, ch, tmp = 0, dirty, bits; + const u_char *odst = dst; + + ch = *src++; + if (ch == '0' && (src[0] == 'x' || src[0] == 'X') + && isascii((u_char)(src[1])) + && isxdigit((u_char)(src[1]))) { + /* Hexadecimal: Eat nybble string. */ + if (size == 0) + goto emsgsize; + dirty = 0; + src++; /* skip x or X. */ + while ((ch = *src++) != '\0' && isascii((u_char)ch) + && isxdigit((u_char)ch)) { + if (isupper((u_char)ch)) + ch = tolower((u_char)ch); + n = strchr(xdigits, ch) - xdigits; + INSIST(n >= 0 && n <= 15); + if (dirty == 0) + tmp = n; + else + tmp = (tmp << 4) | n; + if (++dirty == 2) { + if (size-- == 0) + goto emsgsize; + *dst++ = (u_char) tmp; + dirty = 0; + } + } + if (dirty) { /* Odd trailing nybble? */ + if (size-- == 0) + goto emsgsize; + *dst++ = (u_char) (tmp << 4); + } + } else if (isascii((u_char)ch) && isdigit((u_char)ch)) { + /* Decimal: eat dotted digit string. */ + for (;;) { + tmp = 0; + do { + n = strchr(digits, ch) - digits; + INSIST(n >= 0 && n <= 9); + tmp *= 10; + tmp += n; + if (tmp > 255) + goto enoent; + } while ((ch = *src++) != '\0' && + isascii((u_char)ch) && isdigit((u_char)ch)); + if (size-- == 0) + goto emsgsize; + *dst++ = (u_char) tmp; + if (ch == '\0' || ch == '/') + break; + if (ch != '.') + goto enoent; + ch = *src++; + if (!isascii((u_char)ch) || !isdigit((u_char)ch)) + goto enoent; + } + } else + goto enoent; + + bits = -1; + if (ch == '/' && isascii((u_char)(src[0])) && + isdigit((u_char)(src[0])) && dst > odst) { + /* CIDR width specifier. Nothing can follow it. */ + ch = *src++; /* Skip over the /. */ + bits = 0; + do { + n = strchr(digits, ch) - digits; + INSIST(n >= 0 && n <= 9); + bits *= 10; + bits += n; + } while ((ch = *src++) != '\0' && isascii((u_char)ch) + && isdigit((u_char)ch)); + if (ch != '\0') + goto enoent; + if (bits > 32) + goto emsgsize; + } + + /* Firey death and destruction unless we prefetched EOS. */ + if (ch != '\0') + goto enoent; + + /* If nothing was written to the destination, we found no address. */ + if (dst == odst) + goto enoent; + /* If no CIDR spec was given, infer width from net class. */ + if (bits == -1) { + if (*odst >= 240) /* Class E */ + bits = 32; + else if (*odst >= 224) /* Class D */ + bits = 4; + else if (*odst >= 192) /* Class C */ + bits = 24; + else if (*odst >= 128) /* Class B */ + bits = 16; + else /* Class A */ + bits = 8; + /* If imputed mask is narrower than specified octets, widen. */ + if (bits >= 8 && bits < ((dst - odst) * 8)) + bits = (dst - odst) * 8; + } + /* Extend network to cover the actual mask. */ + while (bits > ((dst - odst) * 8)) { + if (size-- == 0) + goto emsgsize; + *dst++ = '\0'; + } + return (bits); + + enoent: + errno = ENOENT; + return (-1); + + emsgsize: + errno = EMSGSIZE; + return (-1); +} + +static int +getbits(const char *src, int *bitsp) { + static const char digits[] = "0123456789"; + int n; + int val; + char ch; + + val = 0; + n = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + pch = strchr(digits, ch); + if (pch != NULL) { + if (n++ != 0 && val == 0) /* no leading zeros */ + return (0); + val *= 10; + val += (pch - digits); + if (val > 128) /* range */ + return (0); + continue; + } + return (0); + } + if (n == 0) + return (0); + *bitsp = val; + return (1); +} + +static int +getv4(const char *src, u_char *dst, int *bitsp) { + static const char digits[] = "0123456789"; + u_char *odst = dst; + int n; + u_int val; + char ch; + + val = 0; + n = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + pch = strchr(digits, ch); + if (pch != NULL) { + if (n++ != 0 && val == 0) /* no leading zeros */ + return (0); + val *= 10; + val += (pch - digits); + if (val > 255) /* range */ + return (0); + continue; + } + if (ch == '.' || ch == '/') { + if (dst - odst > 3) /* too many octets? */ + return (0); + *dst++ = val; + if (ch == '/') + return (getbits(src, bitsp)); + val = 0; + n = 0; + continue; + } + return (0); + } + if (n == 0) + return (0); + if (dst - odst > 3) /* too many octets? */ + return (0); + *dst++ = val; + return (1); +} + +static int +inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) { + static const char xdigits_l[] = "0123456789abcdef", + xdigits_u[] = "0123456789ABCDEF"; + u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; + const char *xdigits, *curtok; + int ch, saw_xdigit; + u_int val; + int digits; + int bits; + size_t bytes; + int words; + int ipv4; + + memset((tp = tmp), '\0', NS_IN6ADDRSZ); + endp = tp + NS_IN6ADDRSZ; + colonp = NULL; + /* Leading :: requires some special handling. */ + if (*src == ':') + if (*++src != ':') + goto enoent; + curtok = src; + saw_xdigit = 0; + val = 0; + digits = 0; + bits = -1; + ipv4 = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) + pch = strchr((xdigits = xdigits_u), ch); + if (pch != NULL) { + val <<= 4; + val |= (pch - xdigits); + if (++digits > 4) + goto enoent; + saw_xdigit = 1; + continue; + } + if (ch == ':') { + curtok = src; + if (!saw_xdigit) { + if (colonp) + goto enoent; + colonp = tp; + continue; + } else if (*src == '\0') + goto enoent; + if (tp + NS_INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + saw_xdigit = 0; + digits = 0; + val = 0; + continue; + } + if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && + getv4(curtok, tp, &bits) > 0) { + tp += NS_INADDRSZ; + saw_xdigit = 0; + ipv4 = 1; + break; /* '\0' was seen by inet_pton4(). */ + } + if (ch == '/' && getbits(src, &bits) > 0) + break; + goto enoent; + } + if (saw_xdigit) { + if (tp + NS_INT16SZ > endp) + goto enoent; + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + } + if (bits == -1) + bits = 128; + + words = (bits + 15) / 16; + if (words < 2) + words = 2; + if (ipv4) + words = 8; + endp = tmp + 2 * words; + + if (colonp != NULL) { + /* + * Since some memmove()'s erroneously fail to handle + * overlapping regions, we'll do the shift by hand. + */ + const int n = tp - colonp; + int i; + + if (tp == endp) + goto enoent; + for (i = 1; i <= n; i++) { + endp[- i] = colonp[n - i]; + colonp[n - i] = 0; + } + tp = endp; + } + if (tp != endp) + goto enoent; + + bytes = (bits + 7) / 8; + if (bytes > size) + goto emsgsize; + memcpy(dst, tmp, bytes); + return (bits); + + enoent: + errno = ENOENT; + return (-1); + + emsgsize: + errno = EMSGSIZE; + return (-1); +} + +/* + * int + * inet_net_pton(af, src, dst, size) + * convert network number from presentation to network format. + * accepts hex octets, hex strings, decimal octets, and /CIDR. + * "size" is in bytes and describes "dst". + * return: + * number of bits, either imputed classfully or specified with /CIDR, + * or -1 if some failure occurred (check errno). ENOENT means it was + * not a valid network specification. + * author: + * Paul Vixie (ISC), June 1996 + */ +int +inet_net_pton(int af, const char *src, void *dst, size_t size) { + switch (af) { + case AF_INET: + return (inet_net_pton_ipv4(src, dst, size)); + case AF_INET6: + return (inet_net_pton_ipv6(src, dst, size)); + default: + errno = EAFNOSUPPORT; + return (-1); + } +} diff --git a/lib/nbsd_libc/inet/inet_neta.c b/lib/nbsd_libc/inet/inet_neta.c new file mode 100644 index 000000000..d0b90bcd6 --- /dev/null +++ b/lib/nbsd_libc/inet/inet_neta.c @@ -0,0 +1,98 @@ +/* $NetBSD: inet_neta.c,v 1.1 2004/05/20 23:13:02 christos Exp $ */ + +/* + * Copyright (c) 1996 by Internet Software Consortium. + * + * 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: inet_neta.c,v 8.2 1996/08/08 06:54:44 vixie Exp "; +#else +__RCSID("$NetBSD: inet_neta.c,v 1.1 2004/05/20 23:13:02 christos Exp $"); +#endif +#endif + +#include "namespace.h" +#include +#include +#include +#include + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet_neta,_inet_neta) +#endif + +/* + * char * + * inet_neta(src, dst, size) + * format a u_long network number into presentation format. + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * note: + * format of ``src'' is as for inet_network(). + * author: + * Paul Vixie (ISC), July 1996 + */ +char * +inet_neta(src, dst, size) + u_long src; + char *dst; + size_t size; +{ + char *odst = dst; + char *ep; + int advance; + + _DIAGASSERT(dst != NULL); + + if (src == 0x00000000) { + if (size < sizeof "0.0.0.0") + goto emsgsize; + strlcpy(dst, "0.0.0.0", size); + return dst; + } + ep = dst + size; + if (ep <= dst) + goto emsgsize; + while (src & 0xffffffff) { + u_char b = (u_char)((src & 0xff000000) >> 24); + + src <<= 8; + if (b || src) { + advance = snprintf(dst, (size_t)(ep - dst), "%u", b); + if (advance <= 0 || advance >= ep - dst) + goto emsgsize; + dst += advance; + if (src != 0L) { + if (dst + 1 >= ep) + goto emsgsize; + *dst++ = '.'; + *dst = '\0'; + } + } + } + return (odst); + + emsgsize: + errno = EMSGSIZE; + return (NULL); +} diff --git a/lib/nbsd_libc/inet/inet_netof.c b/lib/nbsd_libc/inet/inet_netof.c new file mode 100644 index 000000000..4758f3e38 --- /dev/null +++ b/lib/nbsd_libc/inet/inet_netof.c @@ -0,0 +1,65 @@ +/* $NetBSD: inet_netof.c,v 1.1 2004/05/20 23:13:02 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)inet_netof.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: inet_netof.c,v 1.1 2004/05/20 23:13:02 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet_netof,_inet_netof) +#endif + +/* + * Return the network number from an internet + * address; handles class a/b/c network #'s. + */ +in_addr_t +inet_netof(struct in_addr in) +{ + in_addr_t i = ntohl(in.s_addr); + + if (IN_CLASSA(i)) + return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); + else if (IN_CLASSB(i)) + return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); + else + return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); +} diff --git a/lib/nbsd_libc/inet/inet_network.c b/lib/nbsd_libc/inet/inet_network.c new file mode 100644 index 000000000..135675d58 --- /dev/null +++ b/lib/nbsd_libc/inet/inet_network.c @@ -0,0 +1,114 @@ +/* $NetBSD: inet_network.c,v 1.4 2008/01/20 04:56:08 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)inet_network.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: inet_network.c,v 1.4 2008/01/20 04:56:08 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include +#ifdef _DIAGNOSTIC +#include /* for NULL */ +#endif + +#ifdef __weak_alias +__weak_alias(inet_network,_inet_network) +#endif + +/* + * Internet network address interpretation routine. + * The library routines call this routine to interpret + * network numbers. + */ +in_addr_t +inet_network(const char *cp) +{ + in_addr_t val; + size_t i, n; + u_char c; + in_addr_t parts[4], *pp = parts; + int digit, base; + + _DIAGASSERT(cp != NULL); + +again: + val = 0; base = 10; digit = 0; + if (*cp == '0') + digit = 1, base = 8, cp++; + if (*cp == 'x' || *cp == 'X') + digit = 0, base = 16, cp++; + while ((c = *cp) != 0) { + if (isdigit(c)) { + if (base == 8 && (c == '8' || c == '9')) + return (INADDR_NONE); + val = (val * base) + (c - '0'); + cp++; + digit = 1; + continue; + } + if (base == 16 && isxdigit(c)) { + val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); + cp++; + digit = 1; + continue; + } + break; + } + if (!digit) + return (INADDR_NONE); + if (pp >= parts + 4 || val > 0xff) + return (INADDR_NONE); + if (*cp == '.') { + *pp++ = val, cp++; + goto again; + } + if (*cp && !isspace((u_char) *cp)) + return (INADDR_NONE); + *pp++ = val; + n = pp - parts; + if (n > 4) + return (INADDR_NONE); + for (val = 0, i = 0; i < n; i++) { + val <<= 8; + val |= parts[i] & 0xff; + } + return (val); +} diff --git a/lib/nbsd_libc/inet/inet_ntoa.c b/lib/nbsd_libc/inet/inet_ntoa.c new file mode 100644 index 000000000..df7670d0c --- /dev/null +++ b/lib/nbsd_libc/inet/inet_ntoa.c @@ -0,0 +1,64 @@ +/* $NetBSD: inet_ntoa.c,v 1.1 2004/05/20 23:13:02 christos Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)inet_ntoa.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: inet_ntoa.c,v 1.1 2004/05/20 23:13:02 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet_ntoa,_inet_ntoa) +#endif + +/* + * Convert network-format internet address + * to base 256 d.d.d.d representation. + */ +/*const*/ char * +inet_ntoa(struct in_addr in) { + static char ret[18]; + + strlcpy(ret, "[inet_ntoa error]", sizeof(ret)); + (void) inet_ntop(AF_INET, &in, ret, sizeof ret); + return (ret); +} diff --git a/lib/nbsd_libc/inet/inet_ntop.c b/lib/nbsd_libc/inet/inet_ntop.c new file mode 100644 index 000000000..ed3a95a22 --- /dev/null +++ b/lib/nbsd_libc/inet/inet_ntop.c @@ -0,0 +1,242 @@ +/* $NetBSD: inet_ntop.c,v 1.8 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp"; +#else +__RCSID("$NetBSD: inet_ntop.c,v 1.8 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_ntop,_inet_ntop) +#endif + +/*% + * WARNING: Don't even consider trying to compile this on a system where + * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. + */ + +static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size); +static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size); + +/* char * + * inet_ntop(af, src, dst, size) + * convert a network format address to presentation format. + * return: + * pointer to presentation format address (`dst'), or NULL (see errno). + * author: + * Paul Vixie, 1996. + */ +const char * +inet_ntop(af, src, dst, size) + int af; + const void *src; + char *dst; + socklen_t size; +{ + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + switch (af) { + case AF_INET: + return (inet_ntop4(src, dst, size)); + case AF_INET6: + return (inet_ntop6(src, dst, size)); + default: + errno = EAFNOSUPPORT; + return (NULL); + } + /* NOTREACHED */ +} + +/* const char * + * inet_ntop4(src, dst, size) + * format an IPv4 address, more or less like inet_ntoa() + * return: + * `dst' (as a const) + * notes: + * (1) uses no statics + * (2) takes a u_char* not an in_addr as input + * author: + * Paul Vixie, 1996. + */ +static const char * +inet_ntop4(src, dst, size) + const u_char *src; + char *dst; + socklen_t size; +{ + char tmp[sizeof "255.255.255.255"]; + int l; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u", + src[0], src[1], src[2], src[3]); + if (l <= 0 || (socklen_t) l >= size) { + errno = ENOSPC; + return (NULL); + } + strlcpy(dst, tmp, size); + return (dst); +} + +/* const char * + * inet_ntop6(src, dst, size) + * convert IPv6 binary address into presentation (printable) format + * author: + * Paul Vixie, 1996. + */ +static const char * +inet_ntop6(src, dst, size) + const u_char *src; + char *dst; + socklen_t size; +{ + /* + * Note that int32_t and int16_t need only be "at least" large enough + * to contain a value of the specified size. On some systems, like + * Crays, there is no such thing as an integer variable with 16 bits. + * Keep this in mind if you think this function should have been coded + * to use pointer overlays. All the world's not a VAX. + */ + char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; + char *tp, *ep; + struct { int base, len; } best, cur; + u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; + int i; + int advance; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + /* + * Preprocess: + * Copy the input (bytewise) array into a wordwise array. + * Find the longest run of 0x00's in src[] for :: shorthanding. + */ + memset(words, '\0', sizeof words); + for (i = 0; i < NS_IN6ADDRSZ; i++) + words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); + best.base = -1; + best.len = 0; + cur.base = -1; + cur.len = 0; + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + if (words[i] == 0) { + if (cur.base == -1) + cur.base = i, cur.len = 1; + else + cur.len++; + } else { + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + cur.base = -1; + } + } + } + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + } + if (best.base != -1 && best.len < 2) + best.base = -1; + + /* + * Format the result. + */ + tp = tmp; + ep = tmp + sizeof(tmp); + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + /* Are we inside the best run of 0x00's? */ + if (best.base != -1 && i >= best.base && + i < (best.base + best.len)) { + if (i == best.base) + *tp++ = ':'; + continue; + } + /* Are we following an initial run of 0x00s or any real hex? */ + if (i != 0) { + if (tp + 1 >= ep) + return (NULL); + *tp++ = ':'; + } + /* Is this address an encapsulated IPv4? */ + if (i == 6 && best.base == 0 && + (best.len == 6 || + (best.len == 7 && words[7] != 0x0001) || + (best.len == 5 && words[5] == 0xffff))) { + if (!inet_ntop4(src+12, tp, (socklen_t)(ep - tp))) + return (NULL); + tp += strlen(tp); + break; + } + advance = snprintf(tp, (size_t)(ep - tp), "%x", words[i]); + if (advance <= 0 || advance >= ep - tp) + return (NULL); + tp += advance; + } + /* Was it a trailing run of 0x00's? */ + if (best.base != -1 && (best.base + best.len) == + (NS_IN6ADDRSZ / NS_INT16SZ)) { + if (tp + 1 >= ep) + return (NULL); + *tp++ = ':'; + } + if (tp + 1 >= ep) + return (NULL); + *tp++ = '\0'; + + /* + * Check for overflow, copy, and we're done. + */ + if ((size_t)(tp - tmp) > size) { + errno = ENOSPC; + return (NULL); + } + strlcpy(dst, tmp, size); + return (dst); +} + +/*! \file */ diff --git a/lib/nbsd_libc/inet/inet_pton.c b/lib/nbsd_libc/inet/inet_pton.c new file mode 100644 index 000000000..17ae259bf --- /dev/null +++ b/lib/nbsd_libc/inet/inet_pton.c @@ -0,0 +1,309 @@ +/* $NetBSD: inet_pton.c,v 1.7 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: inet_pton.c,v 1.5 2005/07/28 06:51:47 marka Exp"; +#else +__RCSID("$NetBSD: inet_pton.c,v 1.7 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_pton,_inet_pton) +#endif + +/*% + * WARNING: Don't even consider trying to compile this on a system where + * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. + */ + +static int inet_pton4(const char *src, u_char *dst, int pton); +static int inet_pton6(const char *src, u_char *dst); + +/* int + * inet_pton(af, src, dst) + * convert from presentation format (which usually means ASCII printable) + * to network format (which is usually some kind of binary format). + * return: + * 1 if the address was valid for the specified address family + * 0 if the address wasn't valid (`dst' is untouched in this case) + * -1 if some other error occurred (`dst' is untouched in this case, too) + * author: + * Paul Vixie, 1996. + */ +int +inet_pton(int af, const char *src, void *dst) +{ + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + switch (af) { + case AF_INET: + return (inet_pton4(src, dst, 1)); + case AF_INET6: + return (inet_pton6(src, dst)); + default: + errno = EAFNOSUPPORT; + return (-1); + } + /* NOTREACHED */ +} + +/* int + * inet_pton4(src, dst, pton) + * when last arg is 0: inet_aton(). with hexadecimal, octal and shorthand. + * when last arg is 1: inet_pton(). decimal dotted-quad only. + * return: + * 1 if `src' is a valid input, else 0. + * notice: + * does not touch `dst' unless it's returning 1. + * author: + * Paul Vixie, 1996. + */ +static int +inet_pton4(const char *src, u_char *dst, int pton) +{ + u_int32_t val; + u_int digit, base; + int n; + unsigned char c; + u_int parts[4]; + register u_int *pp = parts; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + c = *src; + for (;;) { + /* + * Collect number up to ``.''. + * Values are specified as for C: + * 0x=hex, 0=octal, isdigit=decimal. + */ + if (!isdigit(c)) + return (0); + val = 0; base = 10; + if (c == '0') { + c = *++src; + if (c == 'x' || c == 'X') + base = 16, c = *++src; + else if (isdigit(c) && c != '9') + base = 8; + } + /* inet_pton() takes decimal only */ + if (pton && base != 10) + return (0); + for (;;) { + if (isdigit(c)) { + digit = c - '0'; + if (digit >= base) + break; + val = (val * base) + digit; + c = *++src; + } else if (base == 16 && isxdigit(c)) { + digit = c + 10 - (islower(c) ? 'a' : 'A'); + if (digit >= 16) + break; + val = (val << 4) | digit; + c = *++src; + } else + break; + } + if (c == '.') { + /* + * Internet format: + * a.b.c.d + * a.b.c (with c treated as 16 bits) + * a.b (with b treated as 24 bits) + * a (with a treated as 32 bits) + */ + if (pp >= parts + 3) + return (0); + *pp++ = val; + c = *++src; + } else + break; + } + /* + * Check for trailing characters. + */ + if (c != '\0' && !isspace(c)) + return (0); + /* + * Concoct the address according to + * the number of parts specified. + */ + n = pp - parts + 1; + /* inet_pton() takes dotted-quad only. it does not take shorthand. */ + if (pton && n != 4) + return (0); + switch (n) { + + case 0: + return (0); /* initial nondigit */ + + case 1: /* a -- 32 bits */ + break; + + case 2: /* a.b -- 8.24 bits */ + if (parts[0] > 0xff || val > 0xffffff) + return (0); + val |= parts[0] << 24; + break; + + case 3: /* a.b.c -- 8.8.16 bits */ + if ((parts[0] | parts[1]) > 0xff || val > 0xffff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if ((parts[0] | parts[1] | parts[2] | val) > 0xff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + if (dst) { + val = htonl(val); + memcpy(dst, &val, NS_INADDRSZ); + } + return (1); +} + +/* int + * inet_pton6(src, dst) + * convert presentation level address to network order binary form. + * return: + * 1 if `src' is a valid [RFC1884 2.2] address, else 0. + * notice: + * (1) does not touch `dst' unless it's returning 1. + * (2) :: in a full address is silently ignored. + * credit: + * inspired by Mark Andrews. + * author: + * Paul Vixie, 1996. + */ +static int +inet_pton6(const char *src, u_char *dst) +{ + static const char xdigits_l[] = "0123456789abcdef", + xdigits_u[] = "0123456789ABCDEF"; + u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; + const char *xdigits, *curtok; + int ch, seen_xdigits; + u_int val; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + memset((tp = tmp), '\0', NS_IN6ADDRSZ); + endp = tp + NS_IN6ADDRSZ; + colonp = NULL; + /* Leading :: requires some special handling. */ + if (*src == ':') + if (*++src != ':') + return (0); + curtok = src; + seen_xdigits = 0; + val = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) + pch = strchr((xdigits = xdigits_u), ch); + if (pch != NULL) { + val <<= 4; + val |= (pch - xdigits); + if (++seen_xdigits > 4) + return (0); + continue; + } + if (ch == ':') { + curtok = src; + if (!seen_xdigits) { + if (colonp) + return (0); + colonp = tp; + continue; + } else if (*src == '\0') + return (0); + if (tp + NS_INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + seen_xdigits = 0; + val = 0; + continue; + } + if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && + inet_pton4(curtok, tp, 1) > 0) { + tp += NS_INADDRSZ; + seen_xdigits = 0; + break; /*%< '\\0' was seen by inet_pton4(). */ + } + return (0); + } + if (seen_xdigits) { + if (tp + NS_INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + } + if (colonp != NULL) { + /* + * Since some memmove()'s erroneously fail to handle + * overlapping regions, we'll do the shift by hand. + */ + const int n = tp - colonp; + int i; + + if (tp == endp) + return (0); + for (i = 1; i <= n; i++) { + endp[- i] = colonp[n - i]; + colonp[n - i] = 0; + } + tp = endp; + } + if (tp != endp) + return (0); + memcpy(dst, tmp, NS_IN6ADDRSZ); + return (1); +} + +/*! \file */ diff --git a/lib/nbsd_libc/inet/nsap_addr.c b/lib/nbsd_libc/inet/nsap_addr.c new file mode 100644 index 000000000..8633205a9 --- /dev/null +++ b/lib/nbsd_libc/inet/nsap_addr.c @@ -0,0 +1,130 @@ +/* $NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp"; +#else +__RCSID("$NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_nsap_addr,_inet_nsap_addr) +__weak_alias(inet_nsap_ntoa,_inet_nsap_ntoa) +#endif + +static char +xtob(int c) { + return (c - (((c >= '0') && (c <= '9')) ? '0' : '7')); +} + +u_int +inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) { + u_char c, nib; + u_int len = 0; + + _DIAGASSERT(ascii != NULL); + _DIAGASSERT(binary != NULL); + + if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X')) + return (0); + ascii += 2; + + while ((c = *ascii++) != '\0' && len < (u_int)maxlen) { + if (c == '.' || c == '+' || c == '/') + continue; + if (!isascii(c)) + return (0); + if (islower(c)) + c = toupper(c); + if (isxdigit(c)) { + nib = xtob(c); + c = *ascii++; + if (c != '\0') { + c = toupper(c); + if (isxdigit(c)) { + *binary++ = (nib << 4) | xtob(c); + len++; + } else + return (0); + } + else + return (0); + } + else + return (0); + } + return (len); +} + +char * +inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) { + int nib; + int i; + char *tmpbuf = inet_nsap_ntoa_tmpbuf; + char *start; + + _DIAGASSERT(binary != NULL); + + if (ascii) + start = ascii; + else { + ascii = tmpbuf; + start = tmpbuf; + } + + *ascii++ = '0'; + *ascii++ = 'x'; + + if (binlen > 255) + binlen = 255; + + for (i = 0; i < binlen; i++) { + nib = (u_int32_t)*binary >> 4; + *ascii++ = nib + (nib < 10 ? '0' : '7'); + nib = *binary++ & 0x0f; + *ascii++ = nib + (nib < 10 ? '0' : '7'); + if (((i % 2) == 0 && (i + 1) < binlen)) + *ascii++ = '.'; + } + *ascii = '\0'; + return (start); +} + +/*! \file */ diff --git a/lib/nbsd_libc/isc/Makefile.inc b/lib/nbsd_libc/isc/Makefile.inc new file mode 100644 index 000000000..e7947d41a --- /dev/null +++ b/lib/nbsd_libc/isc/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2004/05/20 19:52:14 christos Exp $ + +# net sources +.PATH: ${.CURDIR}/isc + +SRCS+= assertions.c ev_timers.c ev_streams.c diff --git a/lib/nbsd_libc/isc/assertions.c b/lib/nbsd_libc/isc/assertions.c new file mode 100644 index 000000000..970231dd4 --- /dev/null +++ b/lib/nbsd_libc/isc/assertions.c @@ -0,0 +1,101 @@ +/* $NetBSD: assertions.c,v 1.6 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") + * Copyright (C) 1997, 1999, 2001 Internet Software Consortium. + * + * Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL ISC 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 +#if !defined(LINT) && !defined(CODECENTER) && !defined(lint) +#ifdef notdef +static const char rcsid[] = "Id: assertions.c,v 1.5 2008/11/14 02:36:51 marka Exp"; +#else +__RCSID("$NetBSD: assertions.c,v 1.6 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif + +#include "port_before.h" + +#include +#include +#include +#include + +#include + +#include "port_after.h" + +/* + * Forward. + */ + +static void default_assertion_failed(const char *, int, assertion_type, + const char *, int); + +/* + * Public. + */ + +assertion_failure_callback __assertion_failed = default_assertion_failed; + +void +set_assertion_failure_callback(assertion_failure_callback f) { + if (f == NULL) + __assertion_failed = default_assertion_failed; + else + __assertion_failed = f; +} + +const char * +assertion_type_to_text(assertion_type type) { + const char *result; + + switch (type) { + case assert_require: + result = "REQUIRE"; + break; + case assert_ensure: + result = "ENSURE"; + break; + case assert_insist: + result = "INSIST"; + break; + case assert_invariant: + result = "INVARIANT"; + break; + default: + result = NULL; + } + return (result); +} + +/* + * Private. + */ + +/* coverity[+kill] */ +static void +default_assertion_failed(const char *file, int line, assertion_type type, + const char *cond, int print_errno) +{ + fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n", + file, line, assertion_type_to_text(type), cond, + (print_errno) ? ": " : "", + (print_errno) ? strerror(errno) : ""); + abort(); + /* NOTREACHED */ +} + +/*! \file */ diff --git a/lib/nbsd_libc/isc/ev_streams.c b/lib/nbsd_libc/isc/ev_streams.c new file mode 100644 index 000000000..2804b9f5b --- /dev/null +++ b/lib/nbsd_libc/isc/ev_streams.c @@ -0,0 +1,319 @@ +/* $NetBSD: ev_streams.c,v 1.6 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +/* ev_streams.c - implement asynch stream file IO for the eventlib + * vix 04mar96 [initial] + */ + +#include +#if !defined(LINT) && !defined(CODECENTER) && !defined(lint) +#ifdef notdef +static const char rcsid[] = "Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp"; +#else +__RCSID("$NetBSD: ev_streams.c,v 1.6 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif + +#include "port_before.h" +#include "fd_setsize.h" + +#include +#include + +#include + +#include +#include +#include "eventlib_p.h" + +#include "port_after.h" + +#ifndef _LIBC +static int copyvec(evStream *str, const struct iovec *iov, int iocnt); +static void consume(evStream *str, size_t bytes); +static void done(evContext opaqueCtx, evStream *str); +static void writable(evContext opaqueCtx, void *uap, int fd, int evmask); +static void readable(evContext opaqueCtx, void *uap, int fd, int evmask); +#endif + +struct iovec +evConsIovec(void *buf, size_t cnt) { + struct iovec ret; + + memset(&ret, 0xf5, sizeof ret); + ret.iov_base = buf; + ret.iov_len = cnt; + return (ret); +} + +#ifndef _LIBC +int +evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, + evStreamFunc func, void *uap, evStreamID *id) +{ + evContext_p *ctx = opaqueCtx.opaque; + evStream *new; + int save; + + OKNEW(new); + new->func = func; + new->uap = uap; + new->fd = fd; + new->flags = 0; + if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0) + goto free; + if (copyvec(new, iov, iocnt) < 0) + goto free; + new->prevDone = NULL; + new->nextDone = NULL; + if (ctx->streams != NULL) + ctx->streams->prev = new; + new->prev = NULL; + new->next = ctx->streams; + ctx->streams = new; + if (id != NULL) + id->opaque = new; + return (0); + free: + save = errno; + FREE(new); + errno = save; + return (-1); +} + +int +evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, + evStreamFunc func, void *uap, evStreamID *id) +{ + evContext_p *ctx = opaqueCtx.opaque; + evStream *new; + int save; + + OKNEW(new); + new->func = func; + new->uap = uap; + new->fd = fd; + new->flags = 0; + if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0) + goto free; + if (copyvec(new, iov, iocnt) < 0) + goto free; + new->prevDone = NULL; + new->nextDone = NULL; + if (ctx->streams != NULL) + ctx->streams->prev = new; + new->prev = NULL; + new->next = ctx->streams; + ctx->streams = new; + if (id) + id->opaque = new; + return (0); + free: + save = errno; + FREE(new); + errno = save; + return (-1); +} + +int +evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ { + evStream *str = id.opaque; + + UNUSED(opaqueCtx); + + str->timer = timer; + str->flags |= EV_STR_TIMEROK; + return (0); +} + +int +evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ { + evStream *str = id.opaque; + + UNUSED(opaqueCtx); + + str->flags &= ~EV_STR_TIMEROK; + return (0); +} + +int +evCancelRW(evContext opaqueCtx, evStreamID id) { + evContext_p *ctx = opaqueCtx.opaque; + evStream *old = id.opaque; + + /* + * The streams list is doubly threaded. First, there's ctx->streams + * that's used by evDestroy() to find and cancel all streams. Second, + * there's ctx->strDone (head) and ctx->strLast (tail) which thread + * through the potentially smaller number of "IO completed" streams, + * used in evGetNext() to avoid scanning the entire list. + */ + + /* Unlink from ctx->streams. */ + if (old->prev != NULL) + old->prev->next = old->next; + else + ctx->streams = old->next; + if (old->next != NULL) + old->next->prev = old->prev; + + /* + * If 'old' is on the ctx->strDone list, remove it. Update + * ctx->strLast if necessary. + */ + if (old->prevDone == NULL && old->nextDone == NULL) { + /* + * Either 'old' is the only item on the done list, or it's + * not on the done list. If the former, then we unlink it + * from the list. If the latter, we leave the list alone. + */ + if (ctx->strDone == old) { + ctx->strDone = NULL; + ctx->strLast = NULL; + } + } else { + if (old->prevDone != NULL) + old->prevDone->nextDone = old->nextDone; + else + ctx->strDone = old->nextDone; + if (old->nextDone != NULL) + old->nextDone->prevDone = old->prevDone; + else + ctx->strLast = old->prevDone; + } + + /* Deallocate the stream. */ + if (old->file.opaque) + evDeselectFD(opaqueCtx, old->file); + memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount); + FREE(old); + return (0); +} + +/* Copy a scatter/gather vector and initialize a stream handler's IO. */ +static int +copyvec(evStream *str, const struct iovec *iov, int iocnt) { + int i; + + str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt); + if (str->iovOrig == NULL) { + errno = ENOMEM; + return (-1); + } + str->ioTotal = 0; + for (i = 0; i < iocnt; i++) { + str->iovOrig[i] = iov[i]; + str->ioTotal += iov[i].iov_len; + } + str->iovOrigCount = iocnt; + str->iovCur = str->iovOrig; + str->iovCurCount = str->iovOrigCount; + str->ioDone = 0; + return (0); +} + +/* Pull off or truncate lead iovec(s). */ +static void +consume(evStream *str, size_t bytes) { + while (bytes > 0U) { + if (bytes < (size_t)str->iovCur->iov_len) { + str->iovCur->iov_len -= bytes; + str->iovCur->iov_base = (void *) + ((u_char *)str->iovCur->iov_base + bytes); + str->ioDone += bytes; + bytes = 0; + } else { + bytes -= str->iovCur->iov_len; + str->ioDone += str->iovCur->iov_len; + str->iovCur++; + str->iovCurCount--; + } + } +} + +/* Add a stream to Done list and deselect the FD. */ +static void +done(evContext opaqueCtx, evStream *str) { + evContext_p *ctx = opaqueCtx.opaque; + + if (ctx->strLast != NULL) { + str->prevDone = ctx->strLast; + ctx->strLast->nextDone = str; + ctx->strLast = str; + } else { + INSIST(ctx->strDone == NULL); + ctx->strDone = ctx->strLast = str; + } + evDeselectFD(opaqueCtx, str->file); + str->file.opaque = NULL; + /* evDrop() will call evCancelRW() on us. */ +} + +/* Dribble out some bytes on the stream. (Called by evDispatch().) */ +static void +writable(evContext opaqueCtx, void *uap, int fd, int evmask) { + evStream *str = uap; + int bytes; + + UNUSED(evmask); + + bytes = writev(fd, str->iovCur, str->iovCurCount); + if (bytes > 0) { + if ((str->flags & EV_STR_TIMEROK) != 0) + evTouchIdleTimer(opaqueCtx, str->timer); + consume(str, bytes); + } else { + if (bytes < 0 && errno != EINTR) { + str->ioDone = -1; + str->ioErrno = errno; + } + } + if (str->ioDone == -1 || str->ioDone == str->ioTotal) + done(opaqueCtx, str); +} + +/* Scoop up some bytes from the stream. (Called by evDispatch().) */ +static void +readable(evContext opaqueCtx, void *uap, int fd, int evmask) { + evStream *str = uap; + int bytes; + + UNUSED(evmask); + + bytes = readv(fd, str->iovCur, str->iovCurCount); + if (bytes > 0) { + if ((str->flags & EV_STR_TIMEROK) != 0) + evTouchIdleTimer(opaqueCtx, str->timer); + consume(str, bytes); + } else { + if (bytes == 0) + str->ioDone = 0; + else { + if (errno != EINTR) { + str->ioDone = -1; + str->ioErrno = errno; + } + } + } + if (str->ioDone <= 0 || str->ioDone == str->ioTotal) + done(opaqueCtx, str); +} +#endif + +/*! \file */ diff --git a/lib/nbsd_libc/isc/ev_timers.c b/lib/nbsd_libc/isc/ev_timers.c new file mode 100644 index 000000000..9a6e730db --- /dev/null +++ b/lib/nbsd_libc/isc/ev_timers.c @@ -0,0 +1,518 @@ +/* $NetBSD: ev_timers.c,v 1.8 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1995-1999 by Internet Software Consortium + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +/* ev_timers.c - implement timers for the eventlib + * vix 09sep95 [initial] + */ + +#include +#if !defined(LINT) && !defined(CODECENTER) && !defined(lint) +#ifdef notdef +static const char rcsid[] = "Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp"; +#else +__RCSID("$NetBSD: ev_timers.c,v 1.8 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif + +/* Import. */ + +#include "port_before.h" +#include "fd_setsize.h" + +#include + +#include +#include +#include "eventlib_p.h" + +#include "port_after.h" + +/* Constants. */ + +#define MILLION 1000000 +#define BILLION 1000000000 + +/* Forward. */ + +#ifndef _LIBC +static int due_sooner(void *, void *); +static void set_index(void *, int); +static void free_timer(void *, void *); +static void print_timer(void *, void *); +static void idle_timeout(evContext, void *, struct timespec, struct timespec); + +/* Private type. */ + +typedef struct { + evTimerFunc func; + void * uap; + struct timespec lastTouched; + struct timespec max_idle; + evTimer * timer; +} idle_timer; +#endif + +/* Public. */ + +struct timespec +evConsTime(time_t sec, long nsec) { + struct timespec x; + + x.tv_sec = sec; + x.tv_nsec = nsec; + return (x); +} + +struct timespec +evAddTime(struct timespec addend1, struct timespec addend2) { + struct timespec x; + + x.tv_sec = addend1.tv_sec + addend2.tv_sec; + x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec; + if (x.tv_nsec >= BILLION) { + x.tv_sec++; + x.tv_nsec -= BILLION; + } + return (x); +} + +struct timespec +evSubTime(struct timespec minuend, struct timespec subtrahend) { + struct timespec x; + + x.tv_sec = minuend.tv_sec - subtrahend.tv_sec; + if (minuend.tv_nsec >= subtrahend.tv_nsec) + x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec; + else { + x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec; + x.tv_sec--; + } + return (x); +} + +int +evCmpTime(struct timespec a, struct timespec b) { +#define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0)); + time_t s = a.tv_sec - b.tv_sec; + long n; + + if (s != 0) + return SGN(s); + + n = a.tv_nsec - b.tv_nsec; + return SGN(n); +} + +struct timespec +evNowTime() { + struct timeval now; +#ifdef CLOCK_REALTIME + struct timespec tsnow; + int m = CLOCK_REALTIME; + +#ifdef CLOCK_MONOTONIC +#ifndef _LIBC + if (__evOptMonoTime) + m = CLOCK_MONOTONIC; +#endif +#endif + if (clock_gettime(m, &tsnow) == 0) + return (tsnow); +#endif + if (gettimeofday(&now, NULL) < 0) + return (evConsTime(0L, 0L)); + return (evTimeSpec(now)); +} + +struct timespec +evUTCTime(void) { + struct timeval now; +#ifdef CLOCK_REALTIME + struct timespec tsnow; + if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0) + return (tsnow); +#endif + if (gettimeofday(&now, NULL) < 0) + return (evConsTime(0L, 0L)); + return (evTimeSpec(now)); +} + +#ifndef _LIBC +struct timespec +evLastEventTime(evContext opaqueCtx) { + evContext_p *ctx = opaqueCtx.opaque; + + return (ctx->lastEventTime); +} +#endif + +struct timespec +evTimeSpec(struct timeval tv) { + struct timespec ts; + + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = tv.tv_usec * 1000; + return (ts); +} + +struct timeval +evTimeVal(struct timespec ts) { + struct timeval tv; + + tv.tv_sec = ts.tv_sec; + tv.tv_usec = ts.tv_nsec / 1000; + return (tv); +} + +#ifndef _LIBC +int +evSetTimer(evContext opaqueCtx, + evTimerFunc func, + void *uap, + struct timespec due, + struct timespec inter, + evTimerID *opaqueID +) { + evContext_p *ctx = opaqueCtx.opaque; + evTimer *id; + + evPrintf(ctx, 1, +"evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n", + ctx, func, uap, + (long)due.tv_sec, due.tv_nsec, + (long)inter.tv_sec, inter.tv_nsec); + +#ifdef __hpux + /* + * tv_sec and tv_nsec are unsigned. + */ + if (due.tv_nsec >= BILLION) + EV_ERR(EINVAL); + + if (inter.tv_nsec >= BILLION) + EV_ERR(EINVAL); +#else + if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION) + EV_ERR(EINVAL); + + if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION) + EV_ERR(EINVAL); +#endif + + /* due={0,0} is a magic cookie meaning "now." */ + if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L) + due = evNowTime(); + + /* Allocate and fill. */ + OKNEW(id); + id->func = func; + id->uap = uap; + id->due = due; + id->inter = inter; + + if (heap_insert(ctx->timers, id) < 0) + return (-1); + + /* Remember the ID if the caller provided us a place for it. */ + if (opaqueID) + opaqueID->opaque = id; + + if (ctx->debug > 7) { + evPrintf(ctx, 7, "timers after evSetTimer:\n"); + (void) heap_for_each(ctx->timers, print_timer, (void *)ctx); + } + + return (0); +} + +int +evClearTimer(evContext opaqueCtx, evTimerID id) { + evContext_p *ctx = opaqueCtx.opaque; + evTimer *del = id.opaque; + + if (ctx->cur != NULL && + ctx->cur->type == Timer && + ctx->cur->u.timer.this == del) { + evPrintf(ctx, 8, "deferring delete of timer (executing)\n"); + /* + * Setting the interval to zero ensures that evDrop() will + * clean up the timer. + */ + del->inter = evConsTime(0, 0); + return (0); + } + + if (heap_element(ctx->timers, del->index) != del) + EV_ERR(ENOENT); + + if (heap_delete(ctx->timers, del->index) < 0) + return (-1); + FREE(del); + + if (ctx->debug > 7) { + evPrintf(ctx, 7, "timers after evClearTimer:\n"); + (void) heap_for_each(ctx->timers, print_timer, (void *)ctx); + } + + return (0); +} + +int +evConfigTimer(evContext opaqueCtx, + evTimerID id, + const char *param, + int value +) { + evContext_p *ctx = opaqueCtx.opaque; + evTimer *timer = id.opaque; + int result=0; + + UNUSED(value); + + if (heap_element(ctx->timers, timer->index) != timer) + EV_ERR(ENOENT); + + if (strcmp(param, "rate") == 0) + timer->mode |= EV_TMR_RATE; + else if (strcmp(param, "interval") == 0) + timer->mode &= ~EV_TMR_RATE; + else + EV_ERR(EINVAL); + + return (result); +} + +int +evResetTimer(evContext opaqueCtx, + evTimerID id, + evTimerFunc func, + void *uap, + struct timespec due, + struct timespec inter +) { + evContext_p *ctx = opaqueCtx.opaque; + evTimer *timer = id.opaque; + struct timespec old_due; + int result=0; + + if (heap_element(ctx->timers, timer->index) != timer) + EV_ERR(ENOENT); + +#ifdef __hpux + /* + * tv_sec and tv_nsec are unsigned. + */ + if (due.tv_nsec >= BILLION) + EV_ERR(EINVAL); + + if (inter.tv_nsec >= BILLION) + EV_ERR(EINVAL); +#else + if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION) + EV_ERR(EINVAL); + + if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION) + EV_ERR(EINVAL); +#endif + + old_due = timer->due; + + timer->func = func; + timer->uap = uap; + timer->due = due; + timer->inter = inter; + + switch (evCmpTime(due, old_due)) { + case -1: + result = heap_increased(ctx->timers, timer->index); + break; + case 0: + result = 0; + break; + case 1: + result = heap_decreased(ctx->timers, timer->index); + break; + } + + if (ctx->debug > 7) { + evPrintf(ctx, 7, "timers after evResetTimer:\n"); + (void) heap_for_each(ctx->timers, print_timer, (void *)ctx); + } + + return (result); +} + +int +evSetIdleTimer(evContext opaqueCtx, + evTimerFunc func, + void *uap, + struct timespec max_idle, + evTimerID *opaqueID +) { + evContext_p *ctx = opaqueCtx.opaque; + idle_timer *tt; + + /* Allocate and fill. */ + OKNEW(tt); + tt->func = func; + tt->uap = uap; + tt->lastTouched = ctx->lastEventTime; + tt->max_idle = max_idle; + + if (evSetTimer(opaqueCtx, idle_timeout, tt, + evAddTime(ctx->lastEventTime, max_idle), + max_idle, opaqueID) < 0) { + FREE(tt); + return (-1); + } + + tt->timer = opaqueID->opaque; + + return (0); +} + +int +evClearIdleTimer(evContext opaqueCtx, evTimerID id) { + evTimer *del = id.opaque; + idle_timer *tt = del->uap; + + FREE(tt); + return (evClearTimer(opaqueCtx, id)); +} + +int +evResetIdleTimer(evContext opaqueCtx, + evTimerID opaqueID, + evTimerFunc func, + void *uap, + struct timespec max_idle +) { + evContext_p *ctx = opaqueCtx.opaque; + evTimer *timer = opaqueID.opaque; + idle_timer *tt = timer->uap; + + tt->func = func; + tt->uap = uap; + tt->lastTouched = ctx->lastEventTime; + tt->max_idle = max_idle; + + return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt, + evAddTime(ctx->lastEventTime, max_idle), + max_idle)); +} + +int +evTouchIdleTimer(evContext opaqueCtx, evTimerID id) { + evContext_p *ctx = opaqueCtx.opaque; + evTimer *t = id.opaque; + idle_timer *tt = t->uap; + + tt->lastTouched = ctx->lastEventTime; + + return (0); +} + +/* Public to the rest of eventlib. */ + +heap_context +evCreateTimers(const evContext_p *ctx) { + + UNUSED(ctx); + + return (heap_new(due_sooner, set_index, 2048)); +} + +void +evDestroyTimers(const evContext_p *ctx) { + (void) heap_for_each(ctx->timers, free_timer, NULL); + (void) heap_free(ctx->timers); +} + +/* Private. */ + +static int +due_sooner(void *a, void *b) { + evTimer *a_timer, *b_timer; + + a_timer = a; + b_timer = b; + return (evCmpTime(a_timer->due, b_timer->due) < 0); +} + +static void +set_index(void *what, int idx) { + evTimer *timer; + + timer = what; + timer->index = idx; +} + +static void +free_timer(void *what, void *uap) { + evTimer *t = what; + + UNUSED(uap); + + FREE(t); +} + +static void +print_timer(void *what, void *uap) { + evTimer *cur = what; + evContext_p *ctx = uap; + + cur = what; + evPrintf(ctx, 7, + " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n", + cur->func, cur->uap, + (long)cur->due.tv_sec, cur->due.tv_nsec, + (long)cur->inter.tv_sec, cur->inter.tv_nsec); +} + +static void +idle_timeout(evContext opaqueCtx, + void *uap, + struct timespec due, + struct timespec inter +) { + evContext_p *ctx = opaqueCtx.opaque; + idle_timer *this = uap; + struct timespec idle; + + UNUSED(due); + UNUSED(inter); + + idle = evSubTime(ctx->lastEventTime, this->lastTouched); + if (evCmpTime(idle, this->max_idle) >= 0) { + (this->func)(opaqueCtx, this->uap, this->timer->due, + this->max_idle); + /* + * Setting the interval to zero will cause the timer to + * be cleaned up in evDrop(). + */ + this->timer->inter = evConsTime(0L, 0L); + FREE(this); + } else { + /* evDrop() will reschedule the timer. */ + this->timer->inter = evSubTime(this->max_idle, idle); + } +} +#endif + +/*! \file */ diff --git a/lib/nbsd_libc/isc/eventlib_p.h b/lib/nbsd_libc/isc/eventlib_p.h new file mode 100644 index 000000000..4d6b78855 --- /dev/null +++ b/lib/nbsd_libc/isc/eventlib_p.h @@ -0,0 +1,283 @@ +/* $NetBSD: eventlib_p.h,v 1.3 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1995-1999 by Internet Software Consortium + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +/*! \file + * \brief private interfaces for eventlib + * \author vix 09sep95 [initial] + * + * Id: eventlib_p.h,v 1.9 2006/03/09 23:57:56 marka Exp + */ + +#ifndef _EVENTLIB_P_H +#define _EVENTLIB_P_H + +#include +#include +#include +#include +#include + +#define EVENTLIB_DEBUG 1 + +#include +#include +#include +#include +#include + +#include +#include +#include + +#define EV_MASK_ALL (EV_READ | EV_WRITE | EV_EXCEPT) +#define EV_ERR(e) return (errno = (e), -1) +#define OK(x) if ((x) < 0) EV_ERR(errno); else (void)NULL +#define OKFREE(x, y) if ((x) < 0) { FREE((y)); EV_ERR(errno); } \ + else (void)NULL + +#define NEW(p) if (((p) = memget(sizeof *(p))) != NULL) \ + FILL(p); \ + else \ + (void)NULL; +#define OKNEW(p) if (!((p) = memget(sizeof *(p)))) { \ + errno = ENOMEM; \ + return (-1); \ + } else \ + FILL(p) +#define FREE(p) memput((p), sizeof *(p)) + +#if EVENTLIB_DEBUG +#define FILL(p) memset((p), 0xF5, sizeof *(p)) +#else +#define FILL(p) +#endif + +#ifdef USE_POLL +#ifdef HAVE_STROPTS_H +#include +#endif +#include +#endif /* USE_POLL */ + +typedef struct evConn { + evConnFunc func; + void * uap; + int fd; + int flags; +#define EV_CONN_LISTEN 0x0001 /*%< Connection is a listener. */ +#define EV_CONN_SELECTED 0x0002 /*%< evSelectFD(conn->file). */ +#define EV_CONN_BLOCK 0x0004 /*%< Listener fd was blocking. */ + evFileID file; + struct evConn * prev; + struct evConn * next; +} evConn; + +typedef struct evAccept { + int fd; + union { + struct sockaddr sa; + struct sockaddr_in in; +#ifndef NO_SOCKADDR_UN + struct sockaddr_un un; +#endif + } la; + ISC_SOCKLEN_T lalen; + union { + struct sockaddr sa; + struct sockaddr_in in; +#ifndef NO_SOCKADDR_UN + struct sockaddr_un un; +#endif + } ra; + ISC_SOCKLEN_T ralen; + int ioErrno; + evConn * conn; + LINK(struct evAccept) link; +} evAccept; + +typedef struct evFile { + evFileFunc func; + void * uap; + int fd; + int eventmask; + int preemptive; + struct evFile * prev; + struct evFile * next; + struct evFile * fdprev; + struct evFile * fdnext; +} evFile; + +typedef struct evStream { + evStreamFunc func; + void * uap; + evFileID file; + evTimerID timer; + int flags; +#define EV_STR_TIMEROK 0x0001 /*%< IFF timer valid. */ + int fd; + struct iovec * iovOrig; + int iovOrigCount; + struct iovec * iovCur; + int iovCurCount; + int ioTotal; + int ioDone; + int ioErrno; + struct evStream *prevDone, *nextDone; + struct evStream *prev, *next; +} evStream; + +typedef struct evTimer { + evTimerFunc func; + void * uap; + struct timespec due, inter; + int index; + int mode; +#define EV_TMR_RATE 1 +} evTimer; + +typedef struct evWait { + evWaitFunc func; + void * uap; + const void * tag; + struct evWait * next; +} evWait; + +typedef struct evWaitList { + evWait * first; + evWait * last; + struct evWaitList * prev; + struct evWaitList * next; +} evWaitList; + +typedef struct evEvent_p { + enum { Accept, File, Stream, Timer, Wait, Free, Null } type; + union { + struct { evAccept *this; } accept; + struct { evFile *this; int eventmask; } file; + struct { evStream *this; } stream; + struct { evTimer *this; } timer; + struct { evWait *this; } wait; + struct { struct evEvent_p *next; } free; + struct { const void *placeholder; } null; + } u; +} evEvent_p; + +#ifdef USE_POLL +typedef struct { + void *ctx; /* pointer to the evContext_p */ + uint32_t type; /* READ, WRITE, EXCEPT, nonblk */ + uint32_t result; /* 1 => revents, 0 => events */ +} __evEmulMask; + +#define emulMaskInit(ctx, field, ev, lastnext) \ + ctx->field.ctx = ctx; \ + ctx->field.type = ev; \ + ctx->field.result = lastnext; + +extern short *__fd_eventfield(int fd, __evEmulMask *maskp); +extern short __poll_event(__evEmulMask *maskp); +extern void __fd_clr(int fd, __evEmulMask *maskp); +extern void __fd_set(int fd, __evEmulMask *maskp); + +#undef FD_ZERO +#define FD_ZERO(maskp) + +#undef FD_SET +#define FD_SET(fd, maskp) \ + __fd_set(fd, maskp) + +#undef FD_CLR +#define FD_CLR(fd, maskp) \ + __fd_clr(fd, maskp) + +#undef FD_ISSET +#define FD_ISSET(fd, maskp) \ + ((*__fd_eventfield(fd, maskp) & __poll_event(maskp)) != 0) + +#endif /* USE_POLL */ + +typedef struct { + /* Global. */ + const evEvent_p *cur; + /* Debugging. */ + int debug; + FILE *output; + /* Connections. */ + evConn *conns; + LIST(evAccept) accepts; + /* Files. */ + evFile *files, *fdNext; +#ifndef USE_POLL + fd_set rdLast, rdNext; + fd_set wrLast, wrNext; + fd_set exLast, exNext; + fd_set nonblockBefore; + int fdMax, fdCount, highestFD; + evFile *fdTable[FD_SETSIZE]; +#else + struct pollfd *pollfds; /* Allocated as needed */ + evFile **fdTable; /* Ditto */ + int maxnfds; /* # elements in above */ + int firstfd; /* First active fd */ + int fdMax; /* Last active fd */ + int fdCount; /* # fd:s with I/O */ + int highestFD; /* max fd allowed by OS */ + __evEmulMask rdLast, rdNext; + __evEmulMask wrLast, wrNext; + __evEmulMask exLast, exNext; + __evEmulMask nonblockBefore; +#endif /* USE_POLL */ +#ifdef EVENTLIB_TIME_CHECKS + struct timespec lastSelectTime; + int lastFdCount; +#endif + /* Streams. */ + evStream *streams; + evStream *strDone, *strLast; + /* Timers. */ + struct timespec lastEventTime; + heap_context timers; + /* Waits. */ + evWaitList *waitLists; + evWaitList waitDone; +} evContext_p; + +/* eventlib.c */ +#define evPrintf __evPrintf +void evPrintf(const evContext_p *ctx, int level, const char *fmt, ...) + ISC_FORMAT_PRINTF(3, 4); + +#ifdef USE_POLL +extern int evPollfdRealloc(evContext_p *ctx, int pollfd_chunk_size, int fd); +#endif /* USE_POLL */ + +/* ev_timers.c */ +#define evCreateTimers __evCreateTimers +heap_context evCreateTimers(const evContext_p *); +#define evDestroyTimers __evDestroyTimers +void evDestroyTimers(const evContext_p *); + +/* ev_waits.c */ +#define evFreeWait __evFreeWait +evWait *evFreeWait(evContext_p *ctx, evWait *old); + +/* Global options */ +extern int __evOptMonoTime; + +#endif /*_EVENTLIB_P_H*/ diff --git a/lib/nbsd_libc/libcincludes.mk b/lib/nbsd_libc/libcincludes.mk new file mode 100644 index 000000000..66afcc3b9 --- /dev/null +++ b/lib/nbsd_libc/libcincludes.mk @@ -0,0 +1,19 @@ +# $NetBSD: libcincludes.mk,v 1.1 2008/10/26 07:28:06 mrg Exp $ + +# Makefile fragment shared across several parts that want to look +# inside libc's include tree. + +.if defined(LIBC_MACHINE_ARCH) && \ + exists(${NETBSDSRCDIR}/lib/libc/arch/${LIBC_MACHINE_ARCH}/SYS.h) +ARCHSUBDIR= ${LIBC_MACHINE_ARCH} +.elif exists(${NETBSDSRCDIR}/lib/libc/arch/${MACHINE_ARCH}/SYS.h) +ARCHSUBDIR= ${MACHINE_ARCH} +.elif exists(${NETBSDSRCDIR}/lib/libc/arch/${MACHINE_CPU}/SYS.h) +ARCHSUBDIR= ${MACHINE_CPU} +.else +.BEGIN: + @echo no ARCHDIR for ${MACHINE_ARCH} nor ${MACHINE_CPU} + @false +.endif + +ARCHDIR= ${NETBSDSRCDIR}/lib/libc/arch/${ARCHSUBDIR} diff --git a/lib/nbsd_libc/locale/Makefile.inc b/lib/nbsd_libc/locale/Makefile.inc new file mode 100644 index 000000000..06fe8ada9 --- /dev/null +++ b/lib/nbsd_libc/locale/Makefile.inc @@ -0,0 +1,63 @@ +# from: @(#)Makefile.inc 5.1 (Berkeley) 2/18/91 +# $NetBSD: Makefile.inc,v 1.58 2010/06/19 13:26:52 tnozaki Exp $ + +# locale sources +.PATH: ${ARCHDIR}/locale ${.CURDIR}/locale + +SRCS+= _def_messages.c _def_monetary.c _def_numeric.c _def_time.c \ + setlocale.c __mb_cur_max.c \ + current_locale.c global_locale.c fix_grouping.c \ + localeconv.c nl_langinfo.c \ + generic_lc_all.c dummy_lc_collate.c \ + wcstol.c wcstoll.c wcstoimax.c wcstoul.c wcstoull.c wcstoumax.c \ + wcstod.c wcstof.c wcstold.c wcscoll.c wcsxfrm.c wcsftime.c + +.if (${CITRUS} == "yes") +# citrus multibyte locale support +# we have quirk for libc.a - see the last part of lib/libc/Makefile +CPPFLAGS+= -DWITH_RUNE -I${.CURDIR} +SRCS+= _wctrans.c _wctype.c rune.c runetable.c \ + multibyte_c90.c multibyte_amd1.c iswctype_mb.c +CPPFLAGS.rune.c+= -I${LIBCDIR}/citrus +CPPFLAGS.runetable.c+= -I${LIBCDIR}/citrus +CPPFLAGS.multibyte_c90.c+= -I${LIBCDIR}/citrus +CPPFLAGS.multibyte_amd1.c+= -I${LIBCDIR}/citrus +.else +# singlebyte locale - dummy +CPPFLAGS+= -UWITH_RUNE +SRCS+= aliasname.c bsdctype.c localeio.c \ + multibyte_sb.c iswctype_sb.c \ + localeio_lc_ctype.c localeio_lc_monetary.c \ + localeio_lc_numeric.c localeio_lc_time.c localeio_lc_messages.c +.endif + +MAN+= setlocale.3 nl_langinfo.3 + +MAN+= mbtowc.3 mbstowcs.3 wctomb.3 wcstombs.3 mblen.3 \ + +MAN+= btowc.3 mbrtowc.3 mbsrtowcs.3 \ + wctob.3 wcrtomb.3 wcsrtombs.3 \ + mbrlen.3 mbsinit.3 + +MAN+= iswalnum.3 wctype.3 iswctype.3 \ + towlower.3 wctrans.3 towctrans.3 \ + wcwidth.3 + +MAN+= wcstol.3 wcstod.3 \ + wcscoll.3 wcsxfrm.3 \ + wcsftime.3 + +MLINKS+=setlocale.3 localeconv.3 + +MLINKS+=iswalnum.3 iswalpha.3 iswalnum.3 iswblank.3 \ + iswalnum.3 iswcntrl.3 iswalnum.3 iswdigit.3 \ + iswalnum.3 iswgraph.3 iswalnum.3 iswlower.3 \ + iswalnum.3 iswprint.3 iswalnum.3 iswpunct.3 \ + iswalnum.3 iswspace.3 iswalnum.3 iswupper.3 \ + iswalnum.3 iswxdigit.3 + +MLINKS+=towlower.3 towupper.3 + +MLINKS+=wcstod.3 wcstof.3 wcstod.3 wcstold.3 +MLINKS+=wcstol.3 wcstoll.3 wcstol.3 wcstoimax.3 \ + wcstol.3 wcstoul.3 wcstol.3 wcstoull.3 wcstol.3 wcstoumax.3 diff --git a/lib/nbsd_libc/locale/__mb_cur_max.c b/lib/nbsd_libc/locale/__mb_cur_max.c new file mode 100644 index 000000000..08c8f1d49 --- /dev/null +++ b/lib/nbsd_libc/locale/__mb_cur_max.c @@ -0,0 +1,39 @@ +/* $NetBSD: __mb_cur_max.c,v 1.2 2001/01/25 01:25:06 itojun Exp $ */ + +/*- + * Copyright (c)1999 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __mb_cur_max.c,v 1.2 2001/01/25 01:25:06 itojun Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +size_t __mb_cur_max = 1; +size_t __mb_len_max_runtime = MB_LEN_MAX; + diff --git a/lib/nbsd_libc/locale/__wctoint.h b/lib/nbsd_libc/locale/__wctoint.h new file mode 100644 index 000000000..c5b695f12 --- /dev/null +++ b/lib/nbsd_libc/locale/__wctoint.h @@ -0,0 +1,79 @@ +/* $NetBSD: __wctoint.h,v 1.1 2001/09/28 11:25:37 yamt Exp $ */ + +/*- + * Copyright (c)2001 Citrus Project, + * 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 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. + * + * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/__wctoint.h,v 1.1 2001/09/21 13:52:32 yamt Exp $ + */ + + +__inline static int +__wctoint(wchar_t wc) +{ + int n; + + /* XXX I expect compiler to optimize this. :D */ + switch (wc) { + case L'0': n = 0; break; + case L'1': n = 1; break; + case L'2': n = 2; break; + case L'3': n = 3; break; + case L'4': n = 4; break; + case L'5': n = 5; break; + case L'6': n = 6; break; + case L'7': n = 7; break; + case L'8': n = 8; break; + case L'9': n = 9; break; + case L'A': case L'a': n = 10; break; + case L'B': case L'b': n = 11; break; + case L'C': case L'c': n = 12; break; + case L'D': case L'd': n = 13; break; + case L'E': case L'e': n = 14; break; + case L'F': case L'f': n = 15; break; + case L'G': case L'g': n = 16; break; + case L'H': case L'h': n = 17; break; + case L'I': case L'i': n = 18; break; + case L'J': case L'j': n = 19; break; + case L'K': case L'k': n = 20; break; + case L'L': case L'l': n = 21; break; + case L'M': case L'm': n = 22; break; + case L'N': case L'n': n = 23; break; + case L'O': case L'o': n = 24; break; + case L'P': case L'p': n = 25; break; + case L'Q': case L'q': n = 26; break; + case L'R': case L'r': n = 27; break; + case L'S': case L's': n = 28; break; + case L'T': case L't': n = 29; break; + case L'U': case L'u': n = 30; break; + case L'V': case L'v': n = 31; break; + case L'W': case L'w': n = 32; break; + case L'X': case L'x': n = 33; break; + case L'Y': case L'y': n = 34; break; + case L'Z': case L'z': n = 35; break; + default: n = -1; break; /* error */ + } + + return n; +} diff --git a/lib/nbsd_libc/locale/_def_messages.c b/lib/nbsd_libc/locale/_def_messages.c new file mode 100644 index 000000000..8ddaa034b --- /dev/null +++ b/lib/nbsd_libc/locale/_def_messages.c @@ -0,0 +1,24 @@ +/* $NetBSD: _def_messages.c,v 1.6 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _def_messages.c,v 1.6 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +const _MessagesLocale _DefaultMessagesLocale = +{ + "^[Yy]", + "^[Nn]", + "yes", + "no" +} ; + +const _MessagesLocale *_CurrentMessagesLocale = &_DefaultMessagesLocale; diff --git a/lib/nbsd_libc/locale/_def_monetary.c b/lib/nbsd_libc/locale/_def_monetary.c new file mode 100644 index 000000000..7bd133156 --- /dev/null +++ b/lib/nbsd_libc/locale/_def_monetary.c @@ -0,0 +1,42 @@ +/* $NetBSD: _def_monetary.c,v 1.8 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _def_monetary.c,v 1.8 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +const _MonetaryLocale _DefaultMonetaryLocale = +{ + "", + "", + "", + "", + "", + "", + "", + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX, + (char)CHAR_MAX +}; + +const _MonetaryLocale *_CurrentMonetaryLocale = &_DefaultMonetaryLocale; diff --git a/lib/nbsd_libc/locale/_def_numeric.c b/lib/nbsd_libc/locale/_def_numeric.c new file mode 100644 index 000000000..5dd29ac48 --- /dev/null +++ b/lib/nbsd_libc/locale/_def_numeric.c @@ -0,0 +1,23 @@ +/* $NetBSD: _def_numeric.c,v 1.6 2005/06/12 05:21:27 lukem Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _def_numeric.c,v 1.6 2005/06/12 05:21:27 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +const _NumericLocale _DefaultNumericLocale = +{ + ".", + "", + "" +}; + +const _NumericLocale *_CurrentNumericLocale = &_DefaultNumericLocale; diff --git a/lib/nbsd_libc/locale/_def_time.c b/lib/nbsd_libc/locale/_def_time.c new file mode 100644 index 000000000..bccb1b1dd --- /dev/null +++ b/lib/nbsd_libc/locale/_def_time.c @@ -0,0 +1,42 @@ +/* $NetBSD: _def_time.c,v 1.10 2008/05/17 03:49:54 ginsbach Exp $ */ + +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _def_time.c,v 1.10 2008/05/17 03:49:54 ginsbach Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +const _TimeLocale _DefaultTimeLocale = +{ + { + "Sun","Mon","Tue","Wed","Thu","Fri","Sat", + }, + { + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", + "Friday", "Saturday" + }, + { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }, + { + "January", "February", "March", "April", "May", "June", "July", + "August", "September", "October", "November", "December" + }, + { + "AM", "PM" + }, + "%a %b %e %H:%M:%S %Y", + "%m/%d/%y", + "%H:%M:%S", + "%I:%M:%S %p" +}; + +const _TimeLocale *_CurrentTimeLocale = &_DefaultTimeLocale; diff --git a/lib/nbsd_libc/locale/_wcstod.h b/lib/nbsd_libc/locale/_wcstod.h new file mode 100644 index 000000000..99a3f7e8f --- /dev/null +++ b/lib/nbsd_libc/locale/_wcstod.h @@ -0,0 +1,126 @@ +/* $NetBSD: _wcstod.h,v 1.2 2010/12/16 17:42:27 wiz Exp $ */ + +/*- + * Copyright (c) 2002 Tim J. Robbins + * 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 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. + * + * Original version ID: + * FreeBSD: /repoman/r/ncvs/src/lib/libc/locale/wcstod.c,v 1.4 2004/04/07 09:47:56 tjr Exp + * NetBSD: wcstod.c,v 1.8 2006/04/13 01:25:13 tnozaki Exp + */ + +/* + * function template for wcstof, wcstod, wcstold. + * + * parameters: + * _FUNCNAME : function name + * _RETURN_TYPE : return type + * _STRTOD_FUNC : real conversion function + */ +#ifndef __WCSTOD_H_ +#define __WCSTOD_H_ + +/* + * Convert a string to a double-precision number. + * + * This is the wide-character counterpart of strto{f,d,ld}(). So that + * we do not have to duplicate the code of strto{f,d,ld}() here, + * we convert the supplied wide-character string to multibyte and + * call strto{f,d,ld}() on the result. + * This assumes that the multibyte encoding is compatible with ASCII + * for at least the digits, radix character and letters. + */ +_RETURN_TYPE +_FUNCNAME(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr) +{ + const wchar_t *src, *start; + _RETURN_TYPE val; + char *buf, *end; + size_t bufsiz, len; + + _DIAGASSERT(nptr != NULL); + /* endptr may be null */ + + src = nptr; + while (iswspace((wint_t)*src) != 0) + ++src; + if (*src == L'\0') + goto no_convert; + + /* + * Convert the supplied numeric wide-char. string to multibyte. + * + * We could attempt to find the end of the numeric portion of the + * wide-char. string to avoid converting unneeded characters but + * choose not to bother; optimising the uncommon case where + * the input string contains a lot of text after the number + * duplicates a lot of strto{f,d,ld}()'s functionality and + * slows down the most common cases. + */ + start = src; + len = wcstombs(NULL, src, 0); + if (len == (size_t)-1) + /* errno = EILSEQ */ + goto no_convert; + + _DIAGASSERT(len > 0); + + bufsiz = len; + buf = (void *)malloc(bufsiz + 1); + if (buf == NULL) + /* errno = ENOMEM */ + goto no_convert; + + len = wcstombs(buf, src, bufsiz + 1); + + _DIAGASSERT(len == bufsiz); + _DIAGASSERT(buf[len] == '\0'); + + /* Let strto{f,d,ld}() do most of the work for us. */ + val = _STRTOD_FUNC(buf, &end); + if (buf == end) { + free(buf); + goto no_convert; + } + + /* + * We only know where the number ended in the _multibyte_ + * representation of the string. If the caller wants to know + * where it ended, count multibyte characters to find the + * corresponding position in the wide-char string. + */ + if (endptr != NULL) + /* XXX Assume each wide char is one byte. */ + *endptr = __UNCONST(start + (size_t)(end - buf)); + + free(buf); + + return val; + +no_convert: + if (endptr != NULL) + *endptr = __UNCONST(nptr); + return 0; +} +#endif /*__WCSTOD_H_*/ diff --git a/lib/nbsd_libc/locale/_wcstol.h b/lib/nbsd_libc/locale/_wcstol.h new file mode 100644 index 000000000..dd7a6a6c5 --- /dev/null +++ b/lib/nbsd_libc/locale/_wcstol.h @@ -0,0 +1,144 @@ +/* $NetBSD: _wcstol.h,v 1.3 2005/11/29 03:11:59 christos 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. + * + * Original version ID: + * @(#)strtol.c 8.1 (Berkeley) 6/4/93 + * NetBSD: wcstol.c,v 1.1 2001/09/27 16:30:36 yamt Exp + * Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstol.c,v 1.2 2001/09/21 16:11:41 yamt Exp + */ + +/* + * function template for wcstol, wcstoll and wcstoimax. + * + * parameters: + * _FUNCNAME : function name + * __INT : return type + * __INT_MIN : lower limit of the return type + * __INT_MAX : upper limit of the return type + */ + +__INT +_FUNCNAME(nptr, endptr, base) + const wchar_t *nptr; + wchar_t **endptr; + int base; +{ + const wchar_t *s; + __INT acc, cutoff; + wint_t wc; + int i; + int neg, any, cutlim; + + _DIAGASSERT(nptr != NULL); + /* endptr may be NULL */ + +#ifdef __GNUC__ + (void)&acc; (void)&cutoff; +#endif + + /* check base value */ + if (base && (base < 2 || base > 36)) { + 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 { + wc = (wchar_t) *s++; + } while (iswspace(wc)); + if (wc == L'-') { + neg = 1; + wc = *s++; + } else { + neg = 0; + if (wc == L'+') + wc = *s++; + } + if ((base == 0 || base == 16) && + wc == L'0' && (*s == L'x' || *s == L'X')) { + wc = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = wc == L'0' ? 8 : 10; + + /* + * See strtol for comments as to the logic used. + */ + cutoff = neg ? __INT_MIN : __INT_MAX; + cutlim = (int)(cutoff % base); + cutoff /= base; + if (neg) { + if (cutlim > 0) { + cutlim -= base; + cutoff += 1; + } + cutlim = -cutlim; + } + for (acc = 0, any = 0;; wc = (wchar_t) *s++) { + i = __wctoint(wc); + if (i == -1) + break; + if (i >= base) + break; + if (any < 0) + continue; + if (neg) { + if (acc < cutoff || (acc == cutoff && i > cutlim)) { + any = -1; + acc = __INT_MIN; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc -= i; + } + } else { + if (acc > cutoff || (acc == cutoff && i > cutlim)) { + any = -1; + acc = __INT_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc += i; + } + } + } + if (endptr != 0) + *endptr = __UNCONST(any ? s - 1 : nptr); + return (acc); +} diff --git a/lib/nbsd_libc/locale/_wcstoul.h b/lib/nbsd_libc/locale/_wcstoul.h new file mode 100644 index 000000000..a21a10b3d --- /dev/null +++ b/lib/nbsd_libc/locale/_wcstoul.h @@ -0,0 +1,120 @@ +/* $NetBSD: _wcstoul.h,v 1.3 2005/11/29 03:11:59 christos 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. + * + * Original version ID: + * @(#)strtoul.c 8.1 (Berkeley) 6/4/93 + * Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstoul.c,v 1.2 2001/09/21 16:11:41 yamt Exp + * NetBSD: wcstoul.c,v 1.1 2001/09/27 16:30:37 yamt Exp + */ + +/* + * function template for wcstoul, wcstoull and wcstoumax. + * + * parameters: + * _FUNCNAME : function name + * __UINT : return type + * __UINT_MAX : upper limit of the return type + */ + +__UINT +_FUNCNAME(nptr, endptr, base) + const wchar_t *nptr; + wchar_t **endptr; + int base; +{ + const wchar_t *s; + __UINT acc, cutoff; + wint_t wc; + int i; + int neg, any, cutlim; + + _DIAGASSERT(nptr != NULL); + /* endptr may be NULL */ + + if (base && (base < 2 || base > 36)) { + 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 { + wc = (wchar_t) *s++; + } while (iswspace(wc)); + if (wc == L'-') { + neg = 1; + wc = *s++; + } else { + neg = 0; + if (wc == L'+') + wc = *s++; + } + if ((base == 0 || base == 16) && + wc == L'0' && (*s == L'x' || *s == L'X')) { + wc = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = wc == L'0' ? 8 : 10; + + /* + * See strtoul for comments as to the logic used. + */ + cutoff = __UINT_MAX / (__UINT)base; + cutlim = (int)(__UINT_MAX % (__UINT)base); + for (acc = 0, any = 0;; wc = (wchar_t) *s++) { + i = __wctoint(wc); + if (i == (wint_t)-1) + break; + if (i >= base) + break; + if (any < 0) + continue; + if (acc > cutoff || (acc == cutoff && i > cutlim)) { + any = -1; + acc = __UINT_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= (__UINT)base; + acc += i; + } + } + if (neg && any > 0) + acc = -acc; + if (endptr != 0) + *endptr = __UNCONST(any ? s - 1 : nptr); + return (acc); +} diff --git a/lib/nbsd_libc/locale/_wctrans.c b/lib/nbsd_libc/locale/_wctrans.c new file mode 100644 index 000000000..c432c4ef6 --- /dev/null +++ b/lib/nbsd_libc/locale/_wctrans.c @@ -0,0 +1,106 @@ +/* $NetBSD: _wctrans.c,v 1.17 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _wctrans.c,v 1.17 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#include "runetype_local.h" +#include "_wctrans_local.h" + +/* + * _towctrans_ext: + * translate a character (extended part) + */ +wint_t +_towctrans_ext(wint_t c, struct _WCTransEntry const *te) +{ + __nbrune_t c0; + uint32_t x; + _RuneRange *rr; + _RuneEntry *base, *re; + + _DIAGASSERT(te != NULL); + + if (c == WEOF) + return (c); + + c0 = (__nbrune_t)c; /* XXX assumes wchar_t = int */ + rr = te->te_extmap; + base = rr->rr_rune_ranges; + for (x = rr->rr_nranges; x != 0; x >>= 1) { + re = base + (x >> 1); + if (re->re_min <= c0 && re->re_max >= c0) + return (re->re_map + c0 - re->re_min); + else if (c0 >= re->re_max) { + base = re + 1; + x--; + } + } + + return (c); +} diff --git a/lib/nbsd_libc/locale/_wctrans_local.h b/lib/nbsd_libc/locale/_wctrans_local.h new file mode 100644 index 000000000..d288ce767 --- /dev/null +++ b/lib/nbsd_libc/locale/_wctrans_local.h @@ -0,0 +1,60 @@ +/* $NetBSD: _wctrans_local.h,v 1.9 2010/06/02 15:47:25 tnozaki Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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. + */ + +#ifndef _WCTRANS_LOCAL_H_ +#define _WCTRANS_LOCAL_H_ + +__BEGIN_DECLS +wint_t _towctrans_ext(wint_t, _WCTransEntry const *); +__END_DECLS + +static __inline wint_t +_towctrans_priv(wint_t c, _WCTransEntry const *te) +{ + return (_RUNE_ISCACHED(c) + ? (wint_t)te->te_cached[(size_t)c] + : _towctrans_ext(c, te)); +} + +static __inline struct _WCTransEntry * +_wctrans_lower(_RuneLocale *rl) +{ + _DIAGASSERT(rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_name != NULL); + + return (&rl->rl_wctrans[_WCTRANS_INDEX_LOWER]); +} + +static __inline struct _WCTransEntry * +_wctrans_upper(_RuneLocale *rl) +{ + _DIAGASSERT(rl->rl_wctrans[_WCTRANS_INDEX_UPPER].te_name != NULL); + + return (&rl->rl_wctrans[_WCTRANS_INDEX_UPPER]); +} + +#endif /*_WCTRANS_LOCAL_H_*/ diff --git a/lib/nbsd_libc/locale/_wctype.c b/lib/nbsd_libc/locale/_wctype.c new file mode 100644 index 000000000..5e22ceb62 --- /dev/null +++ b/lib/nbsd_libc/locale/_wctype.c @@ -0,0 +1,112 @@ +/* $NetBSD: _wctype.c,v 1.9 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _wctype.c,v 1.9 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include "runetype_local.h" +#include "_wctrans_local.h" +#include "_wctype_local.h" + +_RuneType +_runetype_priv(_RuneLocale const *rl, wint_t wc) +{ + __nbrune_t wc0; + _RuneRange const *rr; + _RuneEntry *base, *re; + uint32_t x; + + _DIAGASSERT(rl != NULL); + + if (wc == WEOF) + return 0U; + if (_RUNE_ISCACHED(wc)) + return rl->rl_runetype[(size_t)wc]; + wc0 = (__nbrune_t)wc; + rr = &rl->rl_runetype_ext; + _DIAGASSERT(rr != NULL); + base = rr->rr_rune_ranges; + for (x = rr->rr_nranges; x != (uint32_t)0; x >>= 1) { + re = base + (x >> 1); + if (re->re_min <= wc0 && re->re_max >= wc0) { + if (re->re_rune_types) + return re->re_rune_types[wc0 - re->re_min]; + else + return re->re_map; + } else if (wc0 > re->re_max) { + base = re + 1; + --x; + } + } + return (_RuneType)0U; +} + +int +_iswctype_priv(_RuneLocale const *rl, + wint_t wc, _WCTypeEntry const *te) +{ + return !!(_runetype_priv(rl, wc) & te->te_mask); +} diff --git a/lib/nbsd_libc/locale/_wctype_local.h b/lib/nbsd_libc/locale/_wctype_local.h new file mode 100644 index 000000000..8c0322445 --- /dev/null +++ b/lib/nbsd_libc/locale/_wctype_local.h @@ -0,0 +1,37 @@ +/* $NetBSD: _wctype_local.h,v 1.3 2010/06/02 15:47:25 tnozaki Exp $ */ + +/*- + * Copyright (c) 2008 Citrus Project, + * 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 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. + */ + +#ifndef _WCTYPE_LOCAL_H_ +#define _WCTYPE_LOCAL_H_ + +__BEGIN_DECLS +_RuneType _runetype_priv(_RuneLocale const *, wint_t); +int _iswctype_priv(_RuneLocale const *, wint_t, _WCTypeEntry const *); +__END_DECLS + +#endif /*_WCTYPE_LOCAL_H_*/ diff --git a/lib/nbsd_libc/locale/aliasname.c b/lib/nbsd_libc/locale/aliasname.c new file mode 100644 index 000000000..8d3fc1136 --- /dev/null +++ b/lib/nbsd_libc/locale/aliasname.c @@ -0,0 +1,129 @@ +/* $NetBSD: aliasname.c,v 1.4 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c)2002 YAMAMOTO Takashi, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: aliasname.c,v 1.4 2009/01/11 02:46:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include "aliasname_local.h" + +__inline int __is_ws(char); + +__inline int __is_ws(char ch) +{ + + return (ch == ' ' || ch == '\t'); +} + +const char * +__unaliasname(const char *dbname, const char *alias, void *buf, size_t bufsize) +{ + FILE *fp = NULL; + const char *result = NULL; + size_t resultlen; + size_t aliaslen; + const char *p; + size_t len; + + _DIAGASSERT(dbname != NULL); + _DIAGASSERT(alias != NULL); + _DIAGASSERT(buf != NULL); + + fp = fopen(dbname, "r"); + if (fp == NULL) + goto quit; + + aliaslen = strlen(alias); + + while (/*CONSTCOND*/ 1) { + p = fgetln(fp, &len); + if (p == NULL) + goto quit; /* eof or error */ + + _DIAGASSERT(len != 0); + + /* ignore terminating NL */ + if (p[len - 1] == '\n') + len--; + + /* ignore null line and comment */ + if (len == 0 || p[0] == '#') + continue; + + if (aliaslen > len) + continue; + + if (memcmp(alias, p, aliaslen)) + continue; + + p += aliaslen; + len -= aliaslen; + + if (len == 0 || !__is_ws(*p)) + continue; + + /* entry was found here */ + break; + + /* NOTREACHED */ + } + + /* skip white spaces */ + do { + p++; + len--; + } while (len != 0 && __is_ws(*p)); + + if (len == 0) + goto quit; + + /* count length of result */ + resultlen = 0; + while (resultlen < len && !__is_ws(*p)) + resultlen++; + + /* check if space is enough */ + if (bufsize < resultlen + 1) + goto quit; + + memcpy(buf, p, resultlen); + ((char *)buf)[resultlen] = 0; + result = buf; + +quit: + if (fp) + fclose(fp); + + return result; +} diff --git a/lib/nbsd_libc/locale/aliasname_local.h b/lib/nbsd_libc/locale/aliasname_local.h new file mode 100644 index 000000000..bad619b7d --- /dev/null +++ b/lib/nbsd_libc/locale/aliasname_local.h @@ -0,0 +1,74 @@ +/* $NetBSD: aliasname_local.h,v 1.3 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c)2002 YAMAMOTO Takashi, + * 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 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. + */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _ALIASNAME_LOCAL_H_ +#define _ALIASNAME_LOCAL_H_ + +const char *__unaliasname(const char *, const char *, void *, size_t); + +static __inline int +__isforcemapping(const char *name) +{ + /* don't use strcasecmp, it owes locale. */ + return + name[0] == '/' && + (name[1] == 'F' || name[1] == 'f') && + (name[2] == 'O' || name[2] == 'o') && + (name[3] == 'R' || name[3] == 'r') && + (name[4] == 'C' || name[4] == 'c') && + (name[5] == 'E' || name[5] == 'e') && + name[6] == '\0'; +} + +#endif /*_ALIASNAME_LOCAL_H_*/ diff --git a/lib/nbsd_libc/locale/bsdctype.c b/lib/nbsd_libc/locale/bsdctype.c new file mode 100644 index 000000000..5e499370a --- /dev/null +++ b/lib/nbsd_libc/locale/bsdctype.c @@ -0,0 +1,178 @@ +/* $NetBSD: bsdctype.c,v 1.9 2010/06/20 02:23:15 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: bsdctype.c,v 1.9 2010/06/20 02:23:15 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bsdctype_local.h" +#include "runetype_misc.h" + +const _BSDCTypeLocale _DefaultBSDCTypeLocale = { + _C_ctype_, + _C_tolower_, + _C_toupper_ +}; + +const _BSDCTypeLocale *_CurrentBSDCTypeLocale = &_DefaultBSDCTypeLocale; + +typedef struct { + _BSDCTypeLocale bl; + unsigned char blp_ctype_tab [_CTYPE_NUM_CHARS + 1]; + short blp_tolower_tab[_CTYPE_NUM_CHARS + 1]; + short blp_toupper_tab[_CTYPE_NUM_CHARS + 1]; +} _BSDCTypeLocalePriv; + +static __inline void +_bsdctype_init_priv(_BSDCTypeLocalePriv *blp) +{ +#if _CTYPE_CACHE_SIZE != _CTYPE_NUM_CHARS + int i; + + for (i = _CTYPE_CACHE_SIZE; i < _CTYPE_NUM_CHARS; ++i) { + blp->blp_ctype_tab [i + 1] = 0; + blp->blp_tolower_tab[i + 1] = i; + blp->blp_toupper_tab[i + 1] = i; + } +#endif + blp->blp_ctype_tab [0] = 0; + blp->blp_tolower_tab[0] = EOF; + blp->blp_toupper_tab[0] = EOF; + blp->bl.bl_ctype_tab = &blp->blp_ctype_tab [0]; + blp->bl.bl_tolower_tab = &blp->blp_tolower_tab[0]; + blp->bl.bl_toupper_tab = &blp->blp_toupper_tab[0]; +} + +static __inline int +_bsdctype_read_file(const char * __restrict var, size_t lenvar, + _BSDCTypeLocalePriv * __restrict blp) +{ + const _FileBSDCTypeLocale *fbl; + uint32_t value; + int i; + + _DIAGASSERT(blp != NULL); + + if (lenvar < sizeof(*fbl)) + return EFTYPE; + fbl = (const _FileBSDCTypeLocale *)(const void *)var; + if (memcmp(&fbl->fbl_id[0], _CTYPE_ID, sizeof(fbl->fbl_id))) + return EFTYPE; + value = be32toh(fbl->fbl_rev); + if (value != _CTYPE_REV) + return EFTYPE; + value = be32toh(fbl->fbl_num_chars); + if (value != _CTYPE_CACHE_SIZE) + return EFTYPE; + for (i = 0; i < _CTYPE_CACHE_SIZE; ++i) { + blp->blp_ctype_tab [i + 1] = fbl->fbl_ctype_tab[i]; + blp->blp_tolower_tab[i + 1] = be16toh(fbl->fbl_tolower_tab[i]); + blp->blp_toupper_tab[i + 1] = be16toh(fbl->fbl_toupper_tab[i]); + } + return 0; +} + +static __inline int +_bsdctype_read_runetype(const char * __restrict var, size_t lenvar, + _BSDCTypeLocalePriv * __restrict blp) +{ + const _FileRuneLocale *frl; + int i; + + _DIAGASSERT(blp != NULL); + + if (lenvar < sizeof(*frl)) + return EFTYPE; + lenvar -= sizeof(*frl); + frl = (const _FileRuneLocale *)(const void *)var; + if (memcmp(_RUNECT10_MAGIC, &frl->frl_magic[0], sizeof(frl->frl_magic))) + return EFTYPE; + if (frl->frl_encoding[0] != 'N' || frl->frl_encoding[1] != 'O' || + frl->frl_encoding[2] != 'N' || frl->frl_encoding[3] != 'E' || + frl->frl_encoding[4] != '\0') /* XXX */ + return EFTYPE; + if (be32toh(frl->frl_runetype_ext.frr_nranges) != 0 || + be32toh(frl->frl_maplower_ext.frr_nranges) != 0 || + be32toh(frl->frl_mapupper_ext.frr_nranges) != 0) + return EFTYPE; + if (lenvar < be32toh((uint32_t)frl->frl_variable_len)) + return EFTYPE; + for (i = 0; i < _CTYPE_CACHE_SIZE; ++i) { + blp->blp_ctype_tab [i + 1] = (unsigned char) + _runetype_to_ctype((_RuneType) + be32toh(frl->frl_runetype[i])); + blp->blp_tolower_tab[i + 1] = (short) + be32toh((uint32_t)frl->frl_maplower[i]); + blp->blp_toupper_tab[i + 1] = (short) + be32toh((uint32_t)frl->frl_mapupper[i]); + } + return 0; +} + +int +_bsdctype_load(const char * __restrict var, size_t lenvar, + _BSDCTypeLocale ** __restrict pbl) +{ + int ret; + _BSDCTypeLocalePriv *blp; + + _DIAGASSERT(var != NULL || lenvar < 1); + _DIAGASSERT(pbl != NULL); + + if (lenvar < 1) + return EFTYPE; + blp = malloc(sizeof(*blp)); + if (blp == NULL) + return errno; + _bsdctype_init_priv(blp); + switch (*var) { + case 'B': + _bsdctype_read_file(var, lenvar, blp); + break; + case 'R': + _bsdctype_read_runetype(var, lenvar, blp); + break; + default: + ret = EFTYPE; + } + if (ret) + free(blp); + else + *pbl = &blp->bl; + return ret; +} diff --git a/lib/nbsd_libc/locale/bsdctype_file.h b/lib/nbsd_libc/locale/bsdctype_file.h new file mode 100644 index 000000000..c5effbf2f --- /dev/null +++ b/lib/nbsd_libc/locale/bsdctype_file.h @@ -0,0 +1,46 @@ +/* $NetBSD: bsdctype_file.h,v 1.1 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _BSDCTYPE_FILE_H_ +#define _BSDCTYPE_FILE_H_ + +#include "ctype_local.h" + +typedef struct { + char fbl_id[8]; + uint32_t fbl_rev; + uint32_t fbl_num_chars; + uint8_t fbl_ctype_tab [_CTYPE_CACHE_SIZE]; + int16_t fbl_tolower_tab[_CTYPE_CACHE_SIZE]; + int16_t fbl_toupper_tab[_CTYPE_CACHE_SIZE]; +} __packed _FileBSDCTypeLocale; + +#define _CTYPE_ID "BSDCTYPE" +#define _CTYPE_REV 2 + +#endif /*_BSDCTYPE_FILE_H_*/ diff --git a/lib/nbsd_libc/locale/bsdctype_local.h b/lib/nbsd_libc/locale/bsdctype_local.h new file mode 100644 index 000000000..112c0688f --- /dev/null +++ b/lib/nbsd_libc/locale/bsdctype_local.h @@ -0,0 +1,48 @@ +/* $NetBSD: bsdctype_local.h,v 1.2 2010/06/19 13:26:52 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _BSDCTYPE_LOCAL_H_ +#define _BSDCTYPE_LOCAL_H_ + +#include "bsdctype_file.h" + +typedef struct { + const unsigned char *bl_ctype_tab; + const short *bl_tolower_tab; + const short *bl_toupper_tab; +} _BSDCTypeLocale; + +extern const _BSDCTypeLocale _DefaultBSDCTypeLocale; +extern const _BSDCTypeLocale *_CurrentBSDCTypeLocale; + +__BEGIN_DECLS +int _bsdctype_load(const char * __restrict, size_t, + _BSDCTypeLocale ** __restrict); +__END_DECLS + +#endif /*_BSDCTYPE_LOCAL_H_*/ diff --git a/lib/nbsd_libc/locale/btowc.3 b/lib/nbsd_libc/locale/btowc.3 new file mode 100644 index 000000000..003531d50 --- /dev/null +++ b/lib/nbsd_libc/locale/btowc.3 @@ -0,0 +1,87 @@ +.\" $NetBSD: btowc.3,v 1.4 2004/01/24 16:58:54 wiz Exp $ +.\" +.\" Copyright (c)2003 Citrus Project, +.\" 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 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. +.\" +.Dd March 3, 2003 +.Dt BTOWC 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm btowc +.Nd convert a single byte character to a wide character +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wchar.h +.Ft wint_t +.Fn btowc "int c" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn btowc +function converts a single byte character +.Fa c +in the initial shift state of the current locale to a corresponding +wide character. +.Pp +The behaviour of +.Fn btowc +is affected by the +.Dv LC_CTYPE +category of the current locale. +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +The +.Fn btowc +function returns: +.Bl -tag -width 012345678901 +.It Dv WEOF +If +.Fa c +is +.Dv EOF +or if (unsigned char) +.Fa c +does not correspond to a valid single byte character representation. +.It (otherwise) +A wide character corresponding to +.Fa c . +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +No errors are defined. +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr mbrtowc 3 , +.Xr setlocale 3 , +.Xr wctob 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn btowc +function conforms to +.St -isoC-amd1 . diff --git a/lib/nbsd_libc/locale/ctype_local.h b/lib/nbsd_libc/locale/ctype_local.h new file mode 100644 index 000000000..b5a85d3d0 --- /dev/null +++ b/lib/nbsd_libc/locale/ctype_local.h @@ -0,0 +1,40 @@ +/* $NetBSD: ctype_local.h,v 1.4 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c) 2010 Citrus Project, + * 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 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. + */ +#ifndef _CTYPE_LOCAL_H_ +#define _CTYPE_LOCAL_H_ + +#include + +#define _CTYPE_NUM_CHARS (1 << CHAR_BIT) +#define _CTYPE_CACHE_SIZE (1 << 8) + +extern const unsigned char _C_ctype_[]; +extern const short _C_toupper_[]; +extern const short _C_tolower_[]; + +#endif /*_CTYPE_LOCAL_H_*/ diff --git a/lib/nbsd_libc/locale/current_locale.c b/lib/nbsd_libc/locale/current_locale.c new file mode 100644 index 000000000..f8bf1f663 --- /dev/null +++ b/lib/nbsd_libc/locale/current_locale.c @@ -0,0 +1,49 @@ +/* $NetBSD: current_locale.c,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: current_locale.c,v 1.2 2009/01/11 02:46:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include + +#include "setlocale_local.h" + +static struct _locale_impl_t *__current_locale = &_global_locale; + +struct _locale_impl_t ** +_current_locale() +{ + return &__current_locale; +} diff --git a/lib/nbsd_libc/locale/dummy_lc_collate.c b/lib/nbsd_libc/locale/dummy_lc_collate.c new file mode 100644 index 000000000..366160e6a --- /dev/null +++ b/lib/nbsd_libc/locale/dummy_lc_collate.c @@ -0,0 +1,54 @@ +/* $NetBSD: dummy_lc_collate.c,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: dummy_lc_collate.c,v 1.2 2009/01/11 02:46:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include + +#include "setlocale_local.h" + +/* + * macro required by dummy_lc_template.h + */ +#define _PREFIX(name) __CONCAT(_dummy_LC_COLLATE_,name) + +#define _CATEGORY_ID LC_COLLATE +#define _CATEGORY_NAME "LC_COLLATE" + +#include "dummy_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_dummy_LC_COLLATE_); diff --git a/lib/nbsd_libc/locale/dummy_lc_template.h b/lib/nbsd_libc/locale/dummy_lc_template.h new file mode 100644 index 000000000..154bf1fa2 --- /dev/null +++ b/lib/nbsd_libc/locale/dummy_lc_template.h @@ -0,0 +1,56 @@ +/* $NetBSD: dummy_lc_template.h,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _DUMMY_LC_TEMPLATE_H_ +#define _DUMMY_LC_TEMPLATE_H_ + +#include "generic_lc_template_decl.h" + +static const char * +_PREFIX(setlocale)(const char * __restrict name, + struct _locale_impl_t * __restrict locale) +{ + if (name != NULL) { + if (*name == '\0') + name = _get_locale_env(_CATEGORY_NAME); + if (strcmp(name, locale->part_name[(size_t)_CATEGORY_ID])) { + if (!strcmp(_C_LOCALE, name)) + name = _C_LOCALE; + else if (!strcmp(_POSIX_LOCALE, name)) + name = _POSIX_LOCALE; + else + return NULL; + locale->part_name[(size_t)_CATEGORY_ID] = name; + } + } + return locale->part_name[(size_t)_CATEGORY_ID]; +} + +#include "generic_lc_template.h" + +#endif /*_DUMMY_LC_TEMPLATE_H_*/ diff --git a/lib/nbsd_libc/locale/fix_grouping.c b/lib/nbsd_libc/locale/fix_grouping.c new file mode 100644 index 000000000..5796491c5 --- /dev/null +++ b/lib/nbsd_libc/locale/fix_grouping.c @@ -0,0 +1,109 @@ +/* $NetBSD: fix_grouping.c,v 1.5 2010/06/01 13:52:08 tnozaki Exp $ */ + +/* + * Copyright (c) 2001 Alexey Zelkin + * 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 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. + * + * Original version ID: + * FreeBSD: fix_grouping.c,v 1.8 2003/06/26 10:46:16 phantom Exp + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: fix_grouping.c,v 1.5 2010/06/01 13:52:08 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include "fix_grouping.h" + +#ifndef NBCHAR_MAX +#define NBCHAR_MAX CHAR_MAX +#endif + +#ifndef __UNCONST +#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a)) +#endif + +static const char nogrouping[] = { NBCHAR_MAX, '\0' }; + +/* don't use libc's isdigit, it owes locale. */ +#define isdigit(c) (c >= '0' && c <= '9') + +/* + * Internal helper used to convert grouping sequences from string + * representation into POSIX specified form, i.e. + * + * "3;3;-1" -> "\003\003\177\000" + */ + +const char * +__fix_locale_grouping_str(const char *str) +{ + char *src, *dst; + char n; + + if (str == NULL || *str == '\0') { + return nogrouping; + } + + for (src = __UNCONST(str), dst = __UNCONST(str); *src != '\0'; src++) { + + /* input string examples: "3;3", "3;2;-1" */ + if (*src == ';') + continue; + + if (*src == '-' && *(src+1) == '1') { + *dst++ = NBCHAR_MAX; + src++; + continue; + } + + if (!isdigit((unsigned char)*src)) { + /* broken grouping string */ + return nogrouping; + } + + /* assume all numbers <= 99 */ + n = *src - '0'; + if (isdigit((unsigned char)*(src+1))) { + src++; + n *= 10; + n += *src - '0'; + } + + *dst = n; + /* NOTE: assume all input started with "0" as 'no grouping' */ + if (*dst == '\0') + return (dst == __UNCONST(str)) ? nogrouping : str; + dst++; + } + *dst = '\0'; + return str; +} diff --git a/lib/nbsd_libc/locale/fix_grouping.h b/lib/nbsd_libc/locale/fix_grouping.h new file mode 100644 index 000000000..7da873372 --- /dev/null +++ b/lib/nbsd_libc/locale/fix_grouping.h @@ -0,0 +1,36 @@ +/* $NetBSD: fix_grouping.h,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c) 2008 Citrus Project, + * 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 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. + */ + +#ifndef _FIX_GROUPING_H_ +#define _FIX_GROUPING_H_ + +__BEGIN_DECLS +const char *__fix_locale_grouping_str(const char *); +__END_DECLS + +#endif /*_FIX_GROUPING_H_*/ diff --git a/lib/nbsd_libc/locale/generic_lc_all.c b/lib/nbsd_libc/locale/generic_lc_all.c new file mode 100644 index 000000000..3cf2597b0 --- /dev/null +++ b/lib/nbsd_libc/locale/generic_lc_all.c @@ -0,0 +1,124 @@ +/* $NetBSD: generic_lc_all.c,v 1.3 2009/10/04 21:05:18 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: generic_lc_all.c,v 1.3 2009/10/04 21:05:18 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include + +#include "setlocale_local.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_generic_LC_ALL_, name) + +#include "generic_lc_template_decl.h" + +const char * +_generic_LC_ALL_setlocale(const char * __restrict name, + struct _locale_impl_t * __restrict locale) +{ + _locale_category_t *l; + char head[_LOCALENAME_LEN_MAX * (_LC_LAST - 1)], *tail; + const char *tokens[_LC_LAST], *s, *t; + int load_locale_success, i, j; + + l = _find_category(1); + _DIAGASSERT(l != NULL); + load_locale_success = 0; + if (name != NULL) { + strlcpy(&head[0], name, sizeof(head)); + tokens[1] = &head[0]; + tail = strchr(tokens[1], '/'); + if (tail == NULL) { + for (i = 2; i < _LC_LAST; ++i) + tokens[i] = tokens[1]; + } else { + *tail++ = '\0'; + for (i = 2; i < _LC_LAST - 1; ++i) { + tokens[i] = (const char *)tail; + tail = strchr(tokens[i], '/'); + if (tail == NULL) + return NULL; + *tail++ = '\0'; + } + tokens[_LC_LAST - 1] = (const char *)tail; + tail = strchr(tokens[i], '/'); + if (tail != NULL) + return NULL; + } + if ((*l->setlocale)(tokens[1], locale) != NULL) + load_locale_success = 1; + } + s = (*l->setlocale)(NULL, locale); + _DIAGASSERT(s != NULL); + strlcpy(&locale->query[0], s, sizeof(locale->query)); + for (i = 2, j = 0; i < _LC_LAST; ++i) { + l = _find_category(i); + _DIAGASSERT(l != NULL); + if (name != NULL) { + if ((*l->setlocale)(tokens[i], locale) != NULL) + load_locale_success = 1; + } + t = (*l->setlocale)(NULL, locale); + _DIAGASSERT(t != NULL); + if (j == 0) { + if (!strcmp(s, t)) + continue; + for (j = 2; j < i; ++j) { + strlcat(&locale->query[0], "/", + sizeof(locale->query)); + strlcat(&locale->query[0], s, + sizeof(locale->query)); + } + } + strlcat(&locale->query[0], "/", sizeof(locale->query)); + strlcat(&locale->query[0], t, sizeof(locale->query)); + } + if (name != NULL && !load_locale_success) + return NULL; + return (const char *)&locale->query[0]; +} + +/* + * macro requrired by generic_lc_template.h + */ +#define _CATEGORY_ID LC_ALL + +#include "generic_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_generic_LC_ALL_); diff --git a/lib/nbsd_libc/locale/generic_lc_template.h b/lib/nbsd_libc/locale/generic_lc_template.h new file mode 100644 index 000000000..4278ed918 --- /dev/null +++ b/lib/nbsd_libc/locale/generic_lc_template.h @@ -0,0 +1,38 @@ +/* $NetBSD: generic_lc_template.h,v 1.3 2009/03/09 02:22:25 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _GENERIC_LC_TEMPLATE_H_ +#define _GENERIC_LC_TEMPLATE_H_ + +#define _LOCALE_CATEGORY_ENTRY(name) \ +const _locale_category_t name##desc = { \ + .category = _CATEGORY_ID, \ + .setlocale = &name##setlocale, \ +} + +#endif /*_GENERIC_LC_TEMPLATE_H_*/ diff --git a/lib/nbsd_libc/locale/generic_lc_template_decl.h b/lib/nbsd_libc/locale/generic_lc_template_decl.h new file mode 100644 index 000000000..2aed54c6d --- /dev/null +++ b/lib/nbsd_libc/locale/generic_lc_template_decl.h @@ -0,0 +1,35 @@ +/* $NetBSD: generic_lc_template_decl.h,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _GENERIC_LC_TEMPLATE_DECL_H_ +#define _GENERIC_LC_TEMPLATE_DECL_H_ + +static const char * _PREFIX(setlocale)(const char * __restrict, + struct _locale_impl_t * __restrict); + +#endif /*_GENERIC_LC_TEMPLATE_DECL_H_*/ diff --git a/lib/nbsd_libc/locale/global_locale.c b/lib/nbsd_libc/locale/global_locale.c new file mode 100644 index 000000000..34370bf4c --- /dev/null +++ b/lib/nbsd_libc/locale/global_locale.c @@ -0,0 +1,174 @@ +/* $NetBSD: global_locale.c,v 1.11 2010/06/19 13:26:52 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: global_locale.c,v 1.11 2010/06/19 13:26:52 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#ifdef WITH_RUNE +#include "runetype_local.h" +#else +#include "bsdctype_local.h" +#endif + +#include "setlocale_local.h" + +static struct lconv _global_ldata = { + .decimal_point = __UNCONST("."), + .thousands_sep = __UNCONST(""), + .grouping = __UNCONST(""), + .int_curr_symbol = __UNCONST(""), + .currency_symbol = __UNCONST(""), + .mon_decimal_point = __UNCONST(""), + .mon_thousands_sep = __UNCONST(""), + .mon_grouping = __UNCONST(""), + .positive_sign = __UNCONST(""), + .negative_sign = __UNCONST(""), + .int_frac_digits = CHAR_MAX, + .frac_digits = CHAR_MAX, + .p_cs_precedes = CHAR_MAX, + .p_sep_by_space = CHAR_MAX, + .n_cs_precedes = CHAR_MAX, + .n_sep_by_space = CHAR_MAX, + .p_sign_posn = CHAR_MAX, + .n_sign_posn = CHAR_MAX, + .int_p_cs_precedes = CHAR_MAX, + .int_n_cs_precedes = CHAR_MAX, + .int_p_sep_by_space = CHAR_MAX, + .int_n_sep_by_space = CHAR_MAX, + .int_p_sign_posn = CHAR_MAX, + .int_n_sign_posn = CHAR_MAX, +}; + +static const char *_global_items[(size_t)ALT_DIGITS + 1] = { + [(size_t)D_T_FMT ] = "%a %b %e %H:%M:%S %Y", + [(size_t)D_FMT ] = "%m/%d/%y", + [(size_t)T_FMT ] = "%H:%M:%S", + [(size_t)T_FMT_AMPM ] = "%I:%M:%S %p", + [(size_t)AM_STR ] = "AM", + [(size_t)PM_STR ] = "PM", + [(size_t)DAY_1 ] = "Sun", + [(size_t)DAY_2 ] = "Mon", + [(size_t)DAY_3 ] = "Tue", + [(size_t)DAY_4 ] = "Wed", + [(size_t)DAY_5 ] = "Thu", + [(size_t)DAY_6 ] = "Fri", + [(size_t)DAY_7 ] = "Sat", + [(size_t)ABDAY_1 ] = "Sunday", + [(size_t)ABDAY_2 ] = "Monday", + [(size_t)ABDAY_3 ] = "Tuesday", + [(size_t)ABDAY_4 ] = "Wednesday", + [(size_t)ABDAY_5 ] = "Thursday", + [(size_t)ABDAY_6 ] = "Friday", + [(size_t)ABDAY_7 ] = "Saturday", + [(size_t)MON_1 ] = "Jan", + [(size_t)MON_2 ] = "Feb", + [(size_t)MON_3 ] = "Mar", + [(size_t)MON_4 ] = "Apr", + [(size_t)MON_5 ] = "May", + [(size_t)MON_6 ] = "Jun", + [(size_t)MON_7 ] = "Jul", + [(size_t)MON_8 ] = "Aug", + [(size_t)MON_9 ] = "Sep", + [(size_t)MON_10 ] = "Oct", + [(size_t)MON_11 ] = "Nov", + [(size_t)MON_12 ] = "Dec", + [(size_t)ABMON_1 ] = "January", + [(size_t)ABMON_2 ] = "February", + [(size_t)ABMON_3 ] = "March", + [(size_t)ABMON_4 ] = "April", + [(size_t)ABMON_5 ] = "May", + [(size_t)ABMON_6 ] = "June", + [(size_t)ABMON_7 ] = "July", + [(size_t)ABMON_8 ] = "August", + [(size_t)ABMON_9 ] = "September", + [(size_t)ABMON_10 ] = "October", + [(size_t)ABMON_11 ] = "November", + [(size_t)ABMON_12 ] = "December", + [(size_t)RADIXCHAR ] = ".", + [(size_t)THOUSEP ] = "", + [(size_t)YESSTR ] = "yes", + [(size_t)YESEXPR ] = "^[Yy]", + [(size_t)NOSTR ] = "no", + [(size_t)NOEXPR ] = "^[Nn]", + [(size_t)CRNCYSTR ] = NULL, + [(size_t)CODESET ] = "646", + [(size_t)ERA ] = NULL, + [(size_t)ERA_D_FMT ] = NULL, + [(size_t)ERA_D_T_FMT] = NULL, + [(size_t)ERA_T_FMT ] = NULL, + [(size_t)ALT_DIGITS ] = NULL, +}; + +static struct _locale_cache_t _global_cache = { + .ctype_tab = (const unsigned char *)&_C_ctype_[0], + .tolower_tab = (const short *)&_C_tolower_[0], + .toupper_tab = (const short *)&_C_toupper_[0], + .mb_cur_max = (size_t)1, + .ldata = &_global_ldata, + .items = &_global_items[0], +}; + +struct _locale_impl_t _global_locale = { + .cache = &_global_cache, + .query = { _C_LOCALE }, + .part_name = { + [(size_t)LC_ALL ] = _C_LOCALE, + [(size_t)LC_COLLATE ] = _C_LOCALE, + [(size_t)LC_CTYPE ] = _C_LOCALE, + [(size_t)LC_MONETARY] = _C_LOCALE, + [(size_t)LC_NUMERIC ] = _C_LOCALE, + [(size_t)LC_TIME ] = _C_LOCALE, + [(size_t)LC_MESSAGES] = _C_LOCALE, + }, + .part_impl = { + [(size_t)LC_ALL ] = (_locale_part_t)NULL, + [(size_t)LC_COLLATE ] = (_locale_part_t)NULL, + [(size_t)LC_CTYPE ] = (_locale_part_t) +#ifdef WITH_RUNE + __UNCONST(&_DefaultRuneLocale), +#else + __UNCONST(&_DefaultBSDCTypeLocale), +#endif + [(size_t)LC_MONETARY] = (_locale_part_t) + __UNCONST(&_DefaultMonetaryLocale), + [(size_t)LC_NUMERIC ] = (_locale_part_t) + __UNCONST(&_DefaultNumericLocale), + [(size_t)LC_MESSAGES] = (_locale_part_t) + __UNCONST(&_DefaultMessagesLocale), + }, +}; diff --git a/lib/nbsd_libc/locale/iswalnum.3 b/lib/nbsd_libc/locale/iswalnum.3 new file mode 100644 index 000000000..d986995a1 --- /dev/null +++ b/lib/nbsd_libc/locale/iswalnum.3 @@ -0,0 +1,116 @@ +.\" $NetBSD: iswalnum.3,v 1.9 2004/01/24 16:58:54 wiz Exp $ +.\" +.\" 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. +.\" +.\" @(#)isalnum.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd December 22, 2000 +.Dt ISWALNUM 3 +.Os +.Sh NAME +.Nm iswalnum , +.Nm iswalpha , +.Nm iswblank , +.Nm iswcntrl , +.Nm iswdigit , +.Nm iswgraph , +.Nm iswlower , +.Nm iswprint , +.Nm iswpunct , +.Nm iswspace , +.Nm iswupper , +.Nm iswxdigit +.Nd wide character classification utilities +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wctype.h +.Ft int +.Fn iswalnum "wint_t wc" +.Ft int +.Fn iswalpha "wint_t wc" +.Ft int +.Fn iswblank "wint_t wc" +.Ft int +.Fn iswcntrl "wint_t wc" +.Ft int +.Fn iswdigit "wint_t wc" +.Ft int +.Fn iswgraph "wint_t wc" +.Ft int +.Fn iswlower "wint_t wc" +.Ft int +.Fn iswprint "wint_t wc" +.Ft int +.Fn iswpunct "wint_t wc" +.Ft int +.Fn iswspace "wint_t wc" +.Ft int +.Fn iswupper "wint_t wc" +.Ft int +.Fn iswxdigit "wint_t wc" +.Sh DESCRIPTION +The functions are character classification utility functions, +for use with wide characters +.Po +.Fa wchar_t +or +.Fa wint_t +.Pc . +See the description of singlebyte classification functions, like +.Xr isalnum 3 , +for details. +.Sh RETURN VALUES +The functions return zero if the character tests false and +return non-zero if the character tests true. +.Sh SEE ALSO +.Xr isalnum 3 , +.Xr isalpha 3 , +.Xr isblank 3 , +.Xr iscntrl 3 , +.Xr isdigit 3 , +.Xr isgraph 3 , +.Xr islower 3 , +.Xr isprint 3 , +.Xr ispunct 3 , +.Xr isspace 3 , +.Xr isupper 3 , +.Xr isxdigit 3 +.Sh STANDARDS +The functions conform to +.St -isoC-99 . +.Sh CAVEATS +The argument to these functions must be +.Dv WEOF +or a valid +.Fa wchar_t +value with the current locale; otherwise, the result is undefined. diff --git a/lib/nbsd_libc/locale/iswctype.3 b/lib/nbsd_libc/locale/iswctype.3 new file mode 100644 index 000000000..6225844ae --- /dev/null +++ b/lib/nbsd_libc/locale/iswctype.3 @@ -0,0 +1,99 @@ +.\" $NetBSD: iswctype.3,v 1.7 2007/05/21 15:20:40 tnozaki Exp $ +.\" +.\" Copyright (c)2003 Citrus Project, +.\" 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 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. +.\" +.Dd March 4, 2003 +.Dt ISWCTYPE 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm iswctype +.Nd test a character for character class identifier +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wctype.h +.Ft int +.Fn iswctype "wint_t wc" "wctype_t charclass" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn iswctype +function returns a boolean value that indicates whether a wide character +.Fa wc +is in +.Fa charclass . +.Pp +The behaviour of +.Fn iswctype +is undefined if the +.Fn iswctype +function is called with an invalid +.Fa charclass +(changes of +.Dv LC_CTYPE +category invalidate +.Fa charclass ) +or invalid wide character +.Fa wc . +.Pp +The behaviour of +.Fn iswctype +is affected by the +.Dv LC_CTYPE +category of the current locale. +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +The +.Fn iswctype +returns: +.Bl -tag -width 012345678901 +.It 0 +.Fa wc +is not in +.Fa charclass . +.It non-zero +.Fa wc +is in +.Fa charclass . +.El +.Pp +.\" ---------------------------------------------------------------------- +.Sh ERRORS +No errors are defined. +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr setlocale 3 , +.Xr towctrans 3 , +.Xr wctrans 3 , +.Xr wctype 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn iswctype +function conforms to +.St -isoC-amd1 . diff --git a/lib/nbsd_libc/locale/iswctype_mb.c b/lib/nbsd_libc/locale/iswctype_mb.c new file mode 100644 index 000000000..651ba1b78 --- /dev/null +++ b/lib/nbsd_libc/locale/iswctype_mb.c @@ -0,0 +1,184 @@ +/* $NetBSD: iswctype_mb.c,v 1.11 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: iswctype_mb.c,v 1.11 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "runetype_local.h" +#include "_wctype_local.h" +#include "_wctrans_local.h" + +#define _RUNE_LOCALE() ((_RuneLocale const *) \ + (*_current_locale())->part_impl[(size_t)LC_CTYPE]) + +#define _ISWCTYPE_FUNC(name, index) \ +int \ +isw##name(wint_t wc) \ +{ \ + _RuneLocale const *rl; \ + _WCTypeEntry const *te; \ + \ + rl = _RUNE_LOCALE(); \ + te = &rl->rl_wctype[index]; \ + return _iswctype_priv(rl, wc, te); \ +} +_ISWCTYPE_FUNC(alnum, _WCTYPE_INDEX_ALNUM) +_ISWCTYPE_FUNC(alpha, _WCTYPE_INDEX_ALPHA) +_ISWCTYPE_FUNC(blank, _WCTYPE_INDEX_BLANK) +_ISWCTYPE_FUNC(cntrl, _WCTYPE_INDEX_CNTRL) +_ISWCTYPE_FUNC(digit, _WCTYPE_INDEX_DIGIT) +_ISWCTYPE_FUNC(graph, _WCTYPE_INDEX_GRAPH) +_ISWCTYPE_FUNC(lower, _WCTYPE_INDEX_LOWER) +_ISWCTYPE_FUNC(print, _WCTYPE_INDEX_PRINT) +_ISWCTYPE_FUNC(punct, _WCTYPE_INDEX_PUNCT) +_ISWCTYPE_FUNC(space, _WCTYPE_INDEX_SPACE) +_ISWCTYPE_FUNC(upper, _WCTYPE_INDEX_UPPER) +_ISWCTYPE_FUNC(xdigit, _WCTYPE_INDEX_XDIGIT) + +#define _TOWCTRANS_FUNC(name, index) \ +wint_t \ +tow##name(wint_t wc) \ +{ \ + _RuneLocale const *rl; \ + _WCTransEntry const *te; \ + \ + rl = _RUNE_LOCALE(); \ + te = &rl->rl_wctrans[index]; \ + return _towctrans_priv(wc, te); \ +} +_TOWCTRANS_FUNC(upper, _WCTRANS_INDEX_UPPER) +_TOWCTRANS_FUNC(lower, _WCTRANS_INDEX_LOWER) + +wctype_t +wctype(const char *charclass) +{ + _RuneLocale const *rl; + size_t i; + + rl = _RUNE_LOCALE(); + for (i = 0; i < _WCTYPE_NINDEXES; ++i) { + if (!strcmp(rl->rl_wctype[i].te_name, charclass)) + return (wctype_t)__UNCONST(&rl->rl_wctype[i]); + } + return (wctype_t)NULL; +} + +wctrans_t +wctrans(const char *charmap) +{ + _RuneLocale const *rl; + size_t i; + + rl = _RUNE_LOCALE(); + for (i = 0; i < _WCTRANS_NINDEXES; ++i) { + _DIAGASSERT(rl->rl_wctrans[i].te_name != NULL); + if (!strcmp(rl->rl_wctrans[i].te_name, charmap)) + return (wctrans_t)__UNCONST(&rl->rl_wctype[i]); + } + return (wctrans_t)NULL; +} + +int +iswctype(wint_t wc, wctype_t charclass) +{ + _RuneLocale const *rl; + _WCTypeEntry const *te; + + if (charclass == NULL) { + errno = EINVAL; + return 0; + } + rl = _RUNE_LOCALE(); + te = (_WCTypeEntry const *)(void *)charclass; + return _iswctype_priv(rl, wc, te); +} + +wint_t +towctrans(wint_t wc, wctrans_t charmap) +{ + _WCTransEntry const *te; + + if (charmap == NULL) { + errno = EINVAL; + return wc; + } + te = (_WCTransEntry const *)(void *)charmap; + return _towctrans_priv(wc, te); +} + +__weak_alias(wcwidth,_wcwidth) + +int +wcwidth(wchar_t wc) +{ + _RuneLocale const *rl; + _RuneType x; + + if (wc == L'\0') + return 0; + rl = _RUNE_LOCALE(); + x = _runetype_priv(rl, wc); + if (x & _RUNETYPE_R) + return ((unsigned)x & _RUNETYPE_SWM) >> _RUNETYPE_SWS; + return -1; +} + +int +wcswidth(const wchar_t * __restrict ws, size_t wn) +{ + _RuneLocale const *rl; + _RuneType x; + int width; + + _DIAGASSERT(ws != NULL); + + rl = _RUNE_LOCALE(); + width = 0; + while (wn > 0 && *ws != L'\0') { + x = _runetype_priv(rl, *ws); + if ((x & _RUNETYPE_R) == 0) + return -1; + width += ((unsigned)x & _RUNETYPE_SWM) >> _RUNETYPE_SWS; + ++ws, --wn; + } + return width; +} diff --git a/lib/nbsd_libc/locale/iswctype_sb.c b/lib/nbsd_libc/locale/iswctype_sb.c new file mode 100644 index 000000000..00863ca85 --- /dev/null +++ b/lib/nbsd_libc/locale/iswctype_sb.c @@ -0,0 +1,199 @@ +/* $NetBSD: iswctype_sb.c,v 1.11 2010/06/01 18:00:28 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: iswctype_sb.c,v 1.11 2010/06/01 18:00:28 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#define _CTYPE_NOINLINE +#include +#include +#include +#include +#include +#include + +#define _ISWCTYPE_FUNC(name) \ +int \ +isw##name(wint_t wc) \ +{ \ + int c; \ + \ + c = (wc == WEOF) ? EOF : (unsigned char)wc; \ + return is##name(c); \ +} +_ISWCTYPE_FUNC(alnum) +_ISWCTYPE_FUNC(alpha) +_ISWCTYPE_FUNC(blank) +_ISWCTYPE_FUNC(cntrl) +_ISWCTYPE_FUNC(digit) +_ISWCTYPE_FUNC(graph) +_ISWCTYPE_FUNC(lower) +_ISWCTYPE_FUNC(print) +_ISWCTYPE_FUNC(punct) +_ISWCTYPE_FUNC(space) +_ISWCTYPE_FUNC(upper) +_ISWCTYPE_FUNC(xdigit) + +#define _TOWCTRANS_FUNC(name) \ +wint_t \ +tow##name(wint_t wc) \ +{ \ + int c; \ + c = (wc == WEOF) ? EOF : (unsigned char)wc; \ + return to##name(c); \ +} +_TOWCTRANS_FUNC(upper) +_TOWCTRANS_FUNC(lower) + +struct _wctype_priv_t { + const char *name; + int (*iswctype)(wint_t); +}; + +static const struct _wctype_priv_t _wctype_decl[] = { + { "alnum", &iswalnum }, + { "alpha", &iswalpha }, + { "blank", &iswblank }, + { "cntrl", &iswcntrl }, + { "digit", &iswdigit }, + { "graph", &iswgraph }, + { "lower", &iswlower }, + { "print", &iswprint }, + { "punct", &iswpunct }, + { "space", &iswspace }, + { "upper", &iswupper }, + { "xdigit", &iswxdigit }, +}; +static const size_t _wctype_decl_size = + sizeof(_wctype_decl) / sizeof(struct _wctype_priv_t); + +wctype_t +wctype(const char *charclass) +{ + size_t i; + + for (i = 0; i < _wctype_decl_size; ++i) { + if (!strcmp(charclass, _wctype_decl[i].name)) + return (wctype_t)__UNCONST(&_wctype_decl[i]); + } + return (wctype_t)NULL; +} + +struct _wctrans_priv_t { + const char *name; + wint_t (*towctrans)(wint_t); +}; + +static const struct _wctrans_priv_t _wctrans_decl[] = { + { "upper", &towupper }, + { "lower", &towlower }, +}; +static const size_t _wctrans_decl_size = + sizeof(_wctrans_decl) / sizeof(struct _wctrans_priv_t); + +wctrans_t +/*ARGSUSED*/ +wctrans(const char *charmap) +{ + size_t i; + + for (i = 0; i < _wctrans_decl_size; ++i) { + if (!strcmp(charmap, _wctrans_decl[i].name)) + return (wctrans_t)__UNCONST(&_wctrans_decl[i]); + } + return (wctrans_t)NULL; +} + +int +/*ARGSUSED*/ +iswctype(wint_t wc, wctype_t charclass) +{ + const struct _wctype_priv_t *p; + + p = (const struct _wctype_priv_t *)(void *)charclass; + if (p < &_wctype_decl[0] || p > &_wctype_decl[_wctype_decl_size - 1]) { + errno = EINVAL; + return 0; + } + return (*p->iswctype)(wc); +} + +wint_t +/*ARGSUSED*/ +towctrans(wint_t wc, wctrans_t charmap) +{ + const struct _wctrans_priv_t *p; + + p = (const struct _wctrans_priv_t *)(void *)charmap; + if (p < &_wctrans_decl[0] || p > &_wctrans_decl[_wctrans_decl_size - 1]) { + errno = EINVAL; + return wc; + } + return (*p->towctrans)(wc); +} + +__weak_alias(wcwidth,_wcwidth) + +int +wcwidth(wchar_t wc) +{ + int c; + + switch (wc) { + case L'\0': + return 0; + case WEOF: + c = EOF; + break; + default: + c = (unsigned char)wc; + } + if (isprint(c)) + return 1; + return -1; +} + +int +wcswidth(const wchar_t * __restrict ws, size_t wn) +{ + const wchar_t *pws; + int c; + + pws = ws; + while (wn > 0 && *ws != L'\0') { + c = (*ws == WEOF) ? EOF : (unsigned char)*ws; + if (!isprint(c)) + return -1; + ++ws, --wn; + } + return (int)(ws - pws); +} diff --git a/lib/nbsd_libc/locale/localeconv.c b/lib/nbsd_libc/locale/localeconv.c new file mode 100644 index 000000000..5b6f6dba8 --- /dev/null +++ b/lib/nbsd_libc/locale/localeconv.c @@ -0,0 +1,45 @@ +/* $NetBSD: localeconv.c,v 1.18 2010/05/22 13:15:59 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: localeconv.c,v 1.18 2010/05/22 13:15:59 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#define __SETLOCALE_SOURCE__ +#include + +#include "setlocale_local.h" + +struct lconv * +localeconv() +{ + return _current_cache()->ldata; +} diff --git a/lib/nbsd_libc/locale/localeio.c b/lib/nbsd_libc/locale/localeio.c new file mode 100644 index 000000000..ffd90150f --- /dev/null +++ b/lib/nbsd_libc/locale/localeio.c @@ -0,0 +1,177 @@ +/* $NetBSD: localeio.c,v 1.5 2010/06/19 13:26:52 tnozaki Exp $ */ +/* + * Copyright (c) 2008, The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Brian Ginsbach. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: localeio.c,v 1.5 2010/06/19 13:26:52 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "localeio.h" + +int +_localeio_map_file(const char * __restrict path, + void ** __restrict pvar, size_t * __restrict plenvar) +{ + int fd, ret; + struct stat st; + void *var; + size_t lenvar; + + _DIAGASSERT(path != NULL); + _DIAGASSERT(pvar != NULL); + _DIAGASSERT(plenvar != NULL); + + fd = open(path, O_RDONLY); + if (fd == -1) + return errno; + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || fstat(fd, &st) == 1) { + ret = errno; + goto error; + } + if (!S_ISREG(st.st_mode)) { + ret = EBADF; + goto error; + } + lenvar = (size_t)st.st_size; + if (lenvar < 1) { + ret = EFTYPE; + goto error; + } + var = mmap(NULL, lenvar, PROT_READ, + MAP_FILE|MAP_PRIVATE, fd, (off_t)0); + if (var == MAP_FAILED) { + ret = errno; + goto error; + } + *pvar = var; + *plenvar = lenvar; + return 0; +error: + return ret; +} + +void +_localeio_unmap_file(void *var, size_t lenvar) +{ + munmap(var, lenvar); +} + +int +__loadlocale(const char *name, size_t nstr, size_t nbytes, + size_t localesize, void *currentlocale) +{ + int fd, ret; + unsigned char **ap, *buf, *bp, *cp, *cbp, *ebp; + unsigned char ***locale; + struct stat st; + size_t i, bufsize; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(localesize != 0); + _DIAGASSERT(currentlocale != NULL); + + if ((fd = open(name, O_RDONLY)) == -1) + return ENOENT; + + if ((fstat(fd, &st) == -1) || !S_ISREG(st.st_mode) || + (st.st_size <= 0)) { + ret = EFTYPE; + goto error1; + } + + bufsize = localesize + (size_t)st.st_size; + if ((buf = malloc(bufsize)) == NULL) { + ret = ENOMEM; + goto error1; + } + + bp = buf + localesize; + if (read(fd, bp, (size_t)st.st_size) != st.st_size) { + ret = EFTYPE; + goto error2; + } + + ap = (unsigned char **)(void *)buf; + for (i = (size_t)0, ebp = buf + bufsize; i < nstr; i++) { + ap[i] = bp; + while (bp != ebp && *bp != '\n') + bp++; + if (bp == ebp) { + ret = EFTYPE; + goto error2; + } + *bp++ = '\0'; + } + + cp = buf + (sizeof(unsigned char *) * nstr); + for (i = 0, cbp = bp; i < nbytes; i++) { + int n; + + while (bp != ebp && *bp != '\n') + bp++; + if (bp == ebp) { + ret = EFTYPE; + goto error2; + } + /* ignore overflow/underflow and bad characters */ + n = (unsigned char)strtol((char *)cbp, NULL, 0); + cp[i] = (unsigned char)(n & CHAR_MAX); + cbp = bp; + } + + locale = currentlocale; + + *locale = (unsigned char **)(void *)buf; + (void)close(fd); + return 0; + +error2: + free(buf); + +error1: + (void)close(fd); + return ret; +} diff --git a/lib/nbsd_libc/locale/localeio.h b/lib/nbsd_libc/locale/localeio.h new file mode 100644 index 000000000..815729eb6 --- /dev/null +++ b/lib/nbsd_libc/locale/localeio.h @@ -0,0 +1,36 @@ +/* $NetBSD: localeio.h,v 1.4 2010/06/19 13:26:52 tnozaki Exp $ */ +/* + * Copyright (c) 2008, The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Brian Ginsbach. + * + * 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. + */ + +__BEGIN_DECLS +int _localeio_map_file(const char * __restrict, + void ** __restrict, size_t * __restrict); +void _localeio_unmap_file(void *, size_t); +int __loadlocale(const char *, size_t, size_t, size_t, void *); +__END_DECLS diff --git a/lib/nbsd_libc/locale/localeio_lc_ctype.c b/lib/nbsd_libc/locale/localeio_lc_ctype.c new file mode 100644 index 000000000..0916f10c5 --- /dev/null +++ b/lib/nbsd_libc/locale/localeio_lc_ctype.c @@ -0,0 +1,122 @@ +/* $NetBSD: localeio_lc_ctype.c,v 1.6 2010/06/19 13:26:52 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: localeio_lc_ctype.c,v 1.6 2010/06/19 13:26:52 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include + +#include "bsdctype_local.h" +#include "aliasname_local.h" +#include "localeio.h" + +#include "setlocale_local.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_localeio_LC_CTYPE_, name) + +/* + * macro required by nb_lc_template(_decl).h + */ +#define _CATEGORY_TYPE _BSDCTypeLocale + +#include "nb_lc_template_decl.h" + +static int +/*ARGSUSED*/ +_localeio_LC_CTYPE_create_impl(const char * __restrict root, + const char * __restrict name, _BSDCTypeLocale ** __restrict pdata) +{ + char path[PATH_MAX + 1]; + void *var; + size_t lenvar; + int ret; + + _DIAGASSERT(root != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(pdata != NULL); + + snprintf(path, sizeof(path), + "%s/%s/LC_CTYPE", root, name); + ret = _localeio_map_file(path, &var, &lenvar); + if (!ret) { + ret = _bsdctype_load((const char *)var, lenvar, pdata); + _localeio_unmap_file(var, lenvar); + } + return ret; +} + +static __inline void +_PREFIX(build_cache)(struct _locale_cache_t * __restrict cache, + _BSDCTypeLocale * __restrict data) +{ + _DIAGASSERT(cache != NULL); + _DIAGASSERT(data != NULL); + + cache->ctype_tab = data->bl_ctype_tab; + cache->tolower_tab = data->bl_tolower_tab; + cache->toupper_tab = data->bl_toupper_tab; + cache->mb_cur_max = (size_t)1; +} + +static __inline void +_PREFIX(fixup)(_BSDCTypeLocale *data) +{ + _DIAGASSERT(data != NULL); + + _ctype_ = data->bl_ctype_tab; + _tolower_tab_ = data->bl_tolower_tab; + _toupper_tab_ = data->bl_toupper_tab; +} + +/* + * macro required by nb_lc_template.h + */ +#define _CATEGORY_ID LC_CTYPE +#define _CATEGORY_NAME "LC_CTYPE" +#define _CATEGORY_DEFAULT _DefaultBSDCTypeLocale + +#include "nb_lc_template.h" +#include "generic_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_localeio_LC_CTYPE_); diff --git a/lib/nbsd_libc/locale/localeio_lc_messages.c b/lib/nbsd_libc/locale/localeio_lc_messages.c new file mode 100644 index 000000000..adb575432 --- /dev/null +++ b/lib/nbsd_libc/locale/localeio_lc_messages.c @@ -0,0 +1,83 @@ +/* $NetBSD: localeio_lc_messages.c,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/* + * Copyright (c) 2008, The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Brian Ginsbach. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: localeio_lc_messages.c,v 1.2 2009/01/11 02:46:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "aliasname_local.h" +#include "localeio.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_localeio_LC_MESSAGES_, name) + +#include "nb_lc_messages_misc.h" +#include "nb_lc_template_decl.h" + +#define NSTRINGS (sizeof(_MessagesLocale)/sizeof(const char **)) + +static int +_localeio_LC_MESSAGES_create_impl(const char * __restrict root, + const char * __restrict name, _MessagesLocale ** __restrict pdata) +{ + char path[PATH_MAX + 1]; + + _DIAGASSERT(root != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(pdata != NULL); + + snprintf(path, sizeof(path), + "%s/%s/LC_MESSAGES/SYS_LC_MESSAGES", root, name); + return __loadlocale(path, NSTRINGS, 0, sizeof(_MessagesLocale), + (void *)pdata); +} + +#include "nb_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_localeio_LC_MESSAGES_); diff --git a/lib/nbsd_libc/locale/localeio_lc_monetary.c b/lib/nbsd_libc/locale/localeio_lc_monetary.c new file mode 100644 index 000000000..ec5918f67 --- /dev/null +++ b/lib/nbsd_libc/locale/localeio_lc_monetary.c @@ -0,0 +1,95 @@ +/* $NetBSD: localeio_lc_monetary.c,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/* + * Copyright (c) 2008, The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Brian Ginsbach. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: localeio_lc_monetary.c,v 1.2 2009/01/11 02:46:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "aliasname_local.h" +#include "fix_grouping.h" +#include "localeio.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_localeio_LC_MONETARY_, name) + +#include "nb_lc_monetary_misc.h" +#include "nb_lc_template_decl.h" + +#define NSTRINGS \ + (offsetof(_MonetaryLocale, int_frac_digits)/sizeof(const char **)) +#define NCHARS \ + (offsetof(_MonetaryLocale, int_n_sign_posn) - \ + offsetof(_MonetaryLocale, int_frac_digits) + 1) + +static int +_localeio_LC_MONETARY_create_impl(const char * __restrict root, + const char * __restrict name, _MonetaryLocale ** __restrict pdata) +{ + char path[PATH_MAX + 1]; + int ret; + + _DIAGASSERT(root != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(pdata != NULL); + + snprintf(path, sizeof(path), + "%s/%s/LC_MONETARY", root, name); + ret = __loadlocale(path, NSTRINGS, NCHARS, sizeof(_MonetaryLocale), + (void *)pdata); + if (!ret) { + (*pdata)->mon_grouping = + __fix_locale_grouping_str((*pdata)->mon_grouping); + } + return ret; +} + +#include "nb_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_localeio_LC_MONETARY_); diff --git a/lib/nbsd_libc/locale/localeio_lc_numeric.c b/lib/nbsd_libc/locale/localeio_lc_numeric.c new file mode 100644 index 000000000..364589cc7 --- /dev/null +++ b/lib/nbsd_libc/locale/localeio_lc_numeric.c @@ -0,0 +1,90 @@ +/* $NetBSD: localeio_lc_numeric.c,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/* + * Copyright (c) 2008, The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Brian Ginsbach. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: localeio_lc_numeric.c,v 1.2 2009/01/11 02:46:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "aliasname_local.h" +#include "fix_grouping.h" +#include "localeio.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_localeio_LC_NUMERIC_, name) + +#include "nb_lc_numeric_misc.h" +#include "nb_lc_template_decl.h" + +#define NSTRINGS (sizeof(_NumericLocale)/sizeof(const char **)) + +static int +_localeio_LC_NUMERIC_create_impl(const char * __restrict root, + const char * __restrict name, _NumericLocale ** __restrict pdata) +{ + char path[PATH_MAX + 1]; + int ret; + + _DIAGASSERT(root != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(pdata != NULL); + + snprintf(path, sizeof(path), + "%s/%s/LC_NUMERIC", root, name); + ret = __loadlocale(path, NSTRINGS, 0, sizeof(_NumericLocale), + (void *)pdata); + if (!ret) { + (*pdata)->grouping = + __fix_locale_grouping_str((*pdata)->grouping); + } + return ret; +} + +#include "nb_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_localeio_LC_NUMERIC_); diff --git a/lib/nbsd_libc/locale/localeio_lc_time.c b/lib/nbsd_libc/locale/localeio_lc_time.c new file mode 100644 index 000000000..7da22da5c --- /dev/null +++ b/lib/nbsd_libc/locale/localeio_lc_time.c @@ -0,0 +1,84 @@ +/* $NetBSD: localeio_lc_time.c,v 1.2 2009/01/11 02:46:28 christos Exp $ */ + +/* + * Copyright (c) 2008, The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Brian Ginsbach. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: localeio_lc_time.c,v 1.2 2009/01/11 02:46:28 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "reentrant.h" +#include +#include +#include +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "aliasname_local.h" +#include "fix_grouping.h" +#include "localeio.h" + +/* + * macro required by all template headers + */ +#define _PREFIX(name) __CONCAT(_localeio_LC_TIME_, name) + +#include "nb_lc_time_misc.h" +#include "nb_lc_template_decl.h" + +#define NSTRINGS (sizeof(_TimeLocale)/sizeof(const char **)) + +static int +_localeio_LC_TIME_create_impl(const char * __restrict root, + const char * __restrict name, _TimeLocale ** __restrict pdata) +{ + char path[PATH_MAX + 1]; + + _DIAGASSERT(root != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(pdata != NULL); + + snprintf(path, sizeof(path), + "%s/%s/LC_TIME", root, name); + return __loadlocale(path, NSTRINGS, 0, sizeof(_TimeLocale), + (void *)pdata); +} + +#include "nb_lc_template.h" +_LOCALE_CATEGORY_ENTRY(_localeio_LC_TIME_); diff --git a/lib/nbsd_libc/locale/mblen.3 b/lib/nbsd_libc/locale/mblen.3 new file mode 100644 index 000000000..e40f9a434 --- /dev/null +++ b/lib/nbsd_libc/locale/mblen.3 @@ -0,0 +1,180 @@ +.\" $NetBSD: mblen.3,v 1.6 2006/10/14 07:51:01 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 3, 2002 +.Dt MBLEN 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm mblen +.Nd get number of bytes in a multibyte character +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In stdlib.h +.Ft int +.Fn mblen "const char *s" "size_t n" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn mblen +function usually determines the number of bytes in +a multibyte character pointed to by +.Fa s +and returns it. +This function shall only examine max n bytes of the array beginning from +.Fa s . +.Pp +In state-dependent encodings, +.Fa s +may point the special sequence bytes to change the shift-state. +Although such sequence bytes corresponds to no individual +wide-character code, +the +.Fn mblen +changes the own state by them and treats them +as if they are a part of the subsequent multibyte character. +.Pp +Unlike +.Xr mbrlen 3 , +the first +.Fa n +bytes pointed to by +.Fa s +need to form an entire multibyte character. +Otherwise, this function causes an error. +.Pp +.Fn mblen +is equivalent to the following call, except the internal state of the +.Xr mbtowc 3 +function is not affected: +.Bd -literal -offset indent +mbtowc(NULL, s, n); +.Ed +.Pp +Calling any other functions in +.Lb libc +never changes the internal +state of +.Fn mblen , +except for calling +.Xr setlocale 3 +with the +.Dv LC_CTYPE +category changed to that of the current locale. +Such +.Xr setlocale 3 +calls cause the internal state of this function to be indeterminate. +.Pp +The behaviour of +.Fn mblen +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +These are the special cases: +.Bl -tag -width 0123456789 +.It "s == NULL" +.Fn mblen +initializes its own internal state to an initial state, and +determines whether the current encoding is state-dependent. +This function returns 0 if the encoding is state-independent, +otherwise non-zero. +.It "n == 0" +In this case, +the first +.Fa n +bytes of the array pointed to by +.Fa s +never form a complete character. +Thus, +.Fn mblen +always fails. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +Normally, +.Fn mblen +returns: +.Bl -tag -width 0123456789 +.It "0" +.Fa s +points to a nul byte +.Pq Sq \e0 . +.It "positive" +The value returned is +a number of bytes for the valid multibyte character pointed to by +.Fa s . +There are no cases that this value is greater than +.Fa n +or the value of the +.Dv MB_CUR_MAX +macro. +.It "-1" +.Fa s +points to an invalid or incomplete multibyte character. +The +.Fn mblen +also sets +.Va errno +to indicate the error. +.El +.Pp +When +.Fa s +is equal to +.Dv NULL , +the +.Fn mblen +returns: +.Bl -tag -width 0123456789 +.It "0" +The current encoding is state-independent. +.It "non-zero" +The current encoding is state-dependent. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn mblen +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa s +points to an invalid or incomplete multibyte character. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr mbrlen 3 , +.Xr mbtowc 3 , +.Xr setlocale 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn mblen +function conforms to +.St -ansiC . diff --git a/lib/nbsd_libc/locale/mbrlen.3 b/lib/nbsd_libc/locale/mbrlen.3 new file mode 100644 index 000000000..34cd0e47f --- /dev/null +++ b/lib/nbsd_libc/locale/mbrlen.3 @@ -0,0 +1,206 @@ +.\" $NetBSD: mbrlen.3,v 1.9 2008/02/28 19:36:51 tnozaki Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 3, 2002 +.Dt MBRLEN 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm mbrlen +.Nd get number of bytes in a multibyte character (restartable) +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wchar.h +.Ft size_t +.Fn mbrlen "const char * restrict s" "size_t n" "mbstate_t * restrict ps" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn mbrlen +function usually determines the number of bytes in +a multibyte character pointed to by +.Fa s +and returns it. +This function shall only examine max n bytes of the array beginning from +.Fa s . +.Pp +.Fn mbrlen +is equivalent to the following call (except +.Fa ps +is evaluated only once): +.Bd -literal -offset indent +mbrtowc(NULL, s, n, (ps != NULL) ? ps : \*[Am]internal); +.Ed +.Pp +Here, +.Fa internal +is an internal state object. +.Pp +In state-dependent encodings, +.Fa s +may point to the special sequence bytes to change the shift-state. +Although such sequence bytes corresponds to no individual +wide-character code, these affect the conversion state object pointed +to by +.Fa ps , +and the +.Fn mbrlen +treats the special sequence bytes +as if these are a part of the subsequent multibyte character. +.Pp +Unlike +.Xr mblen 3 , +.Fn mbrlen +may accept the byte sequence when it is not a complete character +but possibly contains part of a valid character. +In this case, this function will accept all such bytes +and save them into the conversion state object pointed to by +.Fa ps . +They will be used on subsequent calls of this function to restart +the conversion suspended. +.Pp +The behaviour of +.Fn mbrlen +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +These are the special cases: +.Bl -tag -width 0123456789 +.It "s == NULL" +.Fn mbrlen +sets the conversion state object pointed to by +.Fa ps +to an initial state and always returns 0. +Unlike +.Xr mblen 3 , +the value returned does not indicate whether the current encoding of +the locale is state-dependent. +.Pp +In this case, +.Fn mbrlen +ignores +.Fa n . +.It "n == 0" +In this case, +the first +.Fa n +bytes of the array pointed to by +.Fa s +never form a complete character. +Thus, +.Fn mbrlen +always returns (size_t)-2. +.It "ps == NULL" +.Fn mbrlen +uses its own internal state object to keep the conversion state, +instead of +.Fa ps +mentioned in this manual page. +.Pp +Calling any other functions in +.Lb libc +never changes the internal +state of +.Fn mbrlen , +except for calling +.Xr setlocale 3 +with a changing +.Dv LC_CTYPE +category of the current locale. +Such +.Xr setlocale 3 +calls cause the internal state of this function to be indeterminate. +This internal state is initialized at startup time of the program. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +The +.Fn mbrlen +returns: +.Bl -tag -width 0123456789 +.It "0" +.Fa s +points to a nul byte +.Pq Sq \e0 . +.It "positive" +The value returned is +a number of bytes for the valid multibyte character pointed to by +.Fa s . +There are no cases that this value is greater than +.Fa n +or the value of the +.Dv MB_CUR_MAX +macro. +.It "(size_t)-2" +.Fa s +points to the byte sequence which possibly contains part of a valid +multibyte character, but which is incomplete. +When +.Fa n +is at least +.Dv MB_CUR_MAX , +this case can only occur if the array pointed to by +.Fa s +contains a redundant shift sequence. +.It "(size_t)-1" +.Fa s +points to an illegal byte sequence which does not form a valid multibyte +character. +In this case, +.Fn mbrtowc +sets +.Va errno +to indicate the error. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn mbrlen +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa s +points to an invalid multibyte character. +.It Bq Er EINVAL +.Fa ps +points to an invalid or uninitialized mbstate_t object. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr mblen 3 , +.Xr mbrtowc 3 , +.Xr setlocale 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn mbrlen +function conforms to +.St -isoC-amd1 . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/mbrtowc.3 b/lib/nbsd_libc/locale/mbrtowc.3 new file mode 100644 index 000000000..80f7a5979 --- /dev/null +++ b/lib/nbsd_libc/locale/mbrtowc.3 @@ -0,0 +1,196 @@ +.\" $NetBSD: mbrtowc.3,v 1.8 2006/10/16 09:10:29 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 4, 2002 +.Dt MBRTOWC 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm mbrtowc +.Nd converts a multibyte character to a wide character (restartable) +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wchar.h +.Ft size_t +.Fn mbrtowc "wchar_t * restrict pwc" "const char * restrict s" "size_t n" \ +"mbstate_t * restrict ps" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn mbrtowc +usually converts the multibyte character pointed to by +.Fa s +to a wide character, and stores the wide character +to the wchar_t object pointed to by +.Fa pwc +if +.Fa pwc +is +.Pf non- Dv NULL +and +.Fa s +points to a valid character. +The conversion happens in accordance with, and changes the conversion +state described in the mbstate_t object pointed to by +.Fa ps . +This function may examine at most +.Fa n +bytes of the array beginning from +.Fa s . +.Pp +If +.Fa s +points to a valid character and the character corresponds to a nul wide +character, then the +.Fn mbrtowc +places the mbstate_t object pointed to by +.Fa ps +to an initial conversion state. +.Pp +Unlike +.Xr mbtowc 3 , +the +.Fn mbrtowc +may accept the byte sequence pointed to by +.Fa s +not forming a complete multibyte character +but which may be part of a valid character. +In this case, this function will accept all such bytes +and save them into the conversion state object pointed to by +.Fa ps . +They will be used at subsequent calls of this function to restart +the conversion suspended. +.Pp +The behaviour of +.Fn mbrtowc +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +These are the special cases: +.Bl -tag -width 012345678901 +.It "s == NULL" +.Fn mbrtowc +sets the conversion state object pointed to by +.Fa ps +to an initial state and always returns 0. +Unlike +.Xr mbtowc 3 , +the value returned does not indicate whether the current encoding of +the locale is state-dependent. +.Pp +In this case, +.Fn mbrtowc +ignores +.Fa pwc +and +.Fa n , +and is equivalent to the following call: +.Bd -literal -offset indent +mbrtowc(NULL, "", 1, ps); +.Ed +.It "pwc == NULL" +The conversion from a multibyte character to a wide character has +taken place and the conversion state may be affected, but the resulting +wide character is discarded. +.It "ps == NULL" +.Fn mbrtowc +uses its own internal state object to keep the conversion state, +instead of +.Fa ps +mentioned in this manual page. +.Pp +Calling any other functions in +.Lb libc +never changes the internal state of +.Fn mbrtowc , +which is initialized at startup time of the program. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +In the usual cases, +.Fn mbrtowc +returns: +.Bl -tag -width 012345678901 +.It 0 +The next bytes pointed to by +.Fa s +form a nul character. +.It positive +If +.Fa s +points to a valid character, +.Fn mbrtowc +returns the number of bytes in the character. +.It (size_t)-2 +.Fa s +points to a byte sequence which possibly contains part of a valid +multibyte character, but which is incomplete. +When +.Fa n +is at least +.Dv MB_CUR_MAX , +this case can only occur if the array pointed to by +.Fa s +contains a redundant shift sequence. +.It (size_t)-1 +.Fa s +points to an illegal byte sequence which does not form a valid multibyte +character. +In this case, +.Fn mbrtowc +sets +.Va errno +to indicate the error. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn mbrtowc +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa s +points to an invalid or incomplete multibyte character. +.It Bq Er EINVAL +.Fa ps +points to an invalid or uninitialized mbstate_t object. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr mbrlen 3 , +.Xr mbtowc 3 , +.Xr setlocale 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn mbrtowc +function conforms to +.St -isoC-amd1 . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/mbsinit.3 b/lib/nbsd_libc/locale/mbsinit.3 new file mode 100644 index 000000000..63ef147b7 --- /dev/null +++ b/lib/nbsd_libc/locale/mbsinit.3 @@ -0,0 +1,74 @@ +.\" $NetBSD: mbsinit.3,v 1.6 2006/10/16 08:42:16 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 3, 2002 +.Dt MBSINIT 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm mbsinit +.Nd determines whether the state object is in the initial state +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wchar.h +.Ft int +.Fn mbsinit "const mbstate_t *ps" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn mbsinit +determines whether the state object pointed to by +.Fa ps +is the initial conversion state, or not. +.Pp +.Fa ps +may be a null pointer. +In this case, +.Fn mbsinit +will always return non-zero. +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +.Fn mbsinit +returns: +.Bl -tag -width 0123456789 +.It 0 +The current state is not the initial state. +.It non-zero +The current state is the initial state or +.Fa ps +is a null pointer. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +No errors are defined. +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn mbsinit +conforms to +.St -isoC-amd1 . diff --git a/lib/nbsd_libc/locale/mbsrtowcs.3 b/lib/nbsd_libc/locale/mbsrtowcs.3 new file mode 100644 index 000000000..a353420c4 --- /dev/null +++ b/lib/nbsd_libc/locale/mbsrtowcs.3 @@ -0,0 +1,181 @@ +.\" $NetBSD: mbsrtowcs.3,v 1.12 2010/12/16 17:42:27 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 4, 2002 +.Dt MBSRTOWCS 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm mbsrtowcs +.Nd converts a multibyte character string to a wide-character string \ +(restartable) +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wchar.h +.Ft size_t +.Fn mbsrtowcs "wchar_t * restrict pwcs" "const char ** restrict s" "size_t n" \ +"mbstate_t * restrict ps" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn mbsrtowcs +converts the multibyte character string indirectly pointed to by +.Fa s +to the corresponding wide-character string, and stores it in the +array pointed to by +.Fa pwcs . +The conversion stops due to the following reasons: +.Bl -bullet +.It +The conversion reaches a nul byte. +In this case, the nul byte is also converted. +.It +The +.Fn mbsrtowcs +has already stored +.Fa n +wide characters. +.It +The conversion encounters an invalid character. +.El +.Pp +Each character will be converted as if +.Xr mbrtowc 3 +is continuously called. +.Pp +After conversion, +if +.Fa pwcs +is not a null pointer, +the pointer object pointed to by +.Fa s +is a null pointer (if the conversion is stopped due to reaching a +nul byte) or the first byte of the character just after the last +character converted. +.Pp +If +.Fa pwcs +is not a null pointer and the conversion is stopped due to reaching +a nul byte, the +.Fn mbsrtowcs +places the state object pointed to by +.Fa ps +to an initial state after the conversion has taken place. +.Pp +The behaviour of +.Fn mbsrtowcs +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +These are the special cases: +.Bl -tag -width 012345678901 +.It "s == NULL || *s == NULL" +Undefined (may cause the program to crash). +.It "pwcs == NULL" +The conversion has taken place, but the resulting wide-character string +was discarded. +In this case, the pointer object pointed to by +.Fa s +is not modified and +.Fa n +is ignored. +.It "ps == NULL" +The +.Fn mbsrtowcs +uses its own internal state object to keep the conversion state, +instead of +.Fa ps +mentioned in this manual page. +.Pp +Calling any other functions in +.Lb libc +never changes the internal state of +.Fn mbsrtowcs , +which is initialized at startup time of the program. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +.Fn mbsrtowcs +returns: +.Bl -tag -width 012345678901 +.It 0 or positive +The value returned is the number of elements stored in the array +pointed to by +.Fa pwcs , +except for a terminating nul wide character (if any). +If +.Fa pwcs +is not +.Dv NULL +and the value returned is equal to +.Fa n , +the wide-character string pointed to by +.Fa pwcs +is not nul-terminated. +If +.Fa pwcs +is a null pointer, the value returned is the number of elements to contain +the whole string converted, except for a terminating nul wide character. +.It (size_t)-1 +The array indirectly pointed to by +.Fa s +contains a byte sequence forming invalid character. +In this case, +.Fn mbsrtowcs +sets +.Va errno +to indicate the error. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn mbsrtowcs +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +The pointer pointed to by +.Fa s +points to an invalid or incomplete multibyte character. +.It Bq Er EINVAL +.Fa ps +points to an invalid or uninitialized mbstate_t object. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr mbrtowc 3 , +.Xr mbstowcs 3 , +.Xr setlocale 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn mbsrtowcs +function conforms to +.St -isoC-amd1 . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/mbstowcs.3 b/lib/nbsd_libc/locale/mbstowcs.3 new file mode 100644 index 000000000..8d2dfea3c --- /dev/null +++ b/lib/nbsd_libc/locale/mbstowcs.3 @@ -0,0 +1,128 @@ +.\" $NetBSD: mbstowcs.3,v 1.11 2010/12/16 17:42:27 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 3, 2002 +.Dt MBSTOWCS 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm mbstowcs +.Nd converts a multibyte character string to a wide-character string +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In stdlib.h +.Ft size_t +.Fn mbstowcs "wchar_t * restrict pwcs" "const char * restrict s" "size_t n" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +.Fn mbstowcs +converts a nul-terminated multibyte character string pointed to by +.Fa s +to the corresponding wide-character string and stores it in the array +pointed to by +.Fa pwcs . +This function may modify the first at most +.Fa n +elements of the array pointed to by +.Fa pwcs . +Each character will be converted as if +.Xr mbtowc 3 +is continuously called, except the internal state of +.Xr mbtowc 3 +will not be affected. +.Pp +For state-dependent encoding, +.Fn mbstowcs +implies the multibyte character string pointed to by +.Fa s +always begins with an initial state. +.Pp +These are the special cases: +.Bl -tag -width 012345678901 +.It pwcs == NULL +.Fn mbstowcs +returns the number of elements to store the whole wide-character string +corresponding to the multibyte character string pointed to by +.Fa s . +In this case, +.Fa n +is ignored. +.It s == NULL +Undefined (may cause the program to crash). +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +.Fn mbstowcs +returns: +.Bl -tag -width 012345678901 +.It 0 or positive +Number of elements stored in the array pointed to by +.Fa pwcs . +There are no cases that the value returned is greater than +.Fa n +(unless +.Fa pwcs +is a null pointer) or the value of the +.Dv MB_CUR_MAX +macro. +If the return value is equal to +.Fa n , +the string pointed to by +.Fa pwcs +will not be nul-terminated. +.It (size_t)-1 +.Fa s +points to a string containing an invalid or incomplete multibyte character. +The +.Fn mbstowcs +also sets +.Va errno +to indicate the error. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn mbstowcs +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa s +points to a string containing an invalid or incomplete multibyte character. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr mbtowc 3 , +.Xr setlocale 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn mbstowcs +function conforms to +.St -ansiC . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/mbtowc.3 b/lib/nbsd_libc/locale/mbtowc.3 new file mode 100644 index 000000000..317fb3344 --- /dev/null +++ b/lib/nbsd_libc/locale/mbtowc.3 @@ -0,0 +1,182 @@ +.\" $NetBSD: mbtowc.3,v 1.8 2006/10/16 09:10:29 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 3, 2002 +.Dt MBTOWC 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm mbtowc +.Nd converts a multibyte character to a wide character +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In stdlib.h +.Ft int +.Fn mbtowc "wchar_t * restrict pwc" "const char * restrict s" "size_t n" +.Sh DESCRIPTION +.Fn mbtowc +usually converts the multibyte character pointed to by +.Fa s +to a wide character, and stores it in the wchar_t object pointed to by +.Fa pwc +if +.Fa pwc +is +.Pf non- Dv NULL +and +.Fa s +points to a valid character. +This function may inspect at most n bytes of the array beginning from +.Fa s . +.Pp +In state-dependent encodings, +.Fa s +may point to the special sequence bytes to change the shift-state. +Although such sequence bytes correspond to no individual +wide-character code, +.Fn mbtowc +changes its own state by the sequence bytes and treats them +as if they are a part of the subsequence multibyte character. +.Pp +Unlike +.Xr mbrtowc 3 , +the first +.Fa n +bytes pointed to by +.Fa s +need to form an entire multibyte character. +Otherwise, this function causes an error. +.Pp +Calling any other functions in +.Lb libc +never changes the internal state of +.Fn mbtowc , +except for calling +.Xr setlocale 3 +with changing the +.Dv LC_CTYPE +category of the current locale. +Such +.Xr setlocale 3 +call causes the internal state of this function to be indeterminate. +.Pp +The behaviour of +.Fn mbtowc +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +There are special cases: +.Bl -tag -width 012345678901 +.It s == NULL +.Fn mbtowc +initializes its own internal state to an initial state, and +determines whether the current encoding is state-dependent. +This function returns 0 if the encoding is state-independent, +otherwise non-zero. +In this case, +.Fa pwc +is completely ignored. +.It pwc == NULL +.Fn mbtowc +executes the conversion as if +.Fa pwc +is non-NULL, but a result of the conversion is discarded. +.It n == 0 +In this case, +the first +.Fa n +bytes of the array pointed to by +.Fa s +never form a complete character. +Thus, the +.Fn mbtowc +always fails. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +Normally, the +.Fn mbtowc +returns: +.Bl -tag -width 012345678901 +.It 0 +.Fa s +points to a nul byte +.Pq Sq \e0 . +.It positive +Number of bytes for the valid multibyte character pointed to by +.Fa s . +There are no cases that the value returned is greater than +the value of the +.Dv MB_CUR_MAX +macro. +.It -1 +.Fa s +points to an invalid or an incomplete multibyte character. +The +.Fn mbtowc +also sets +.Va errno +to indicate the error. +.El +.Pp +When +.Fa s +is equal to +.Dv NULL , +.Fn mbtowc +returns: +.Bl -tag -width 0123456789 +.It 0 +The current encoding is state-independent. +.It non-zero +The current encoding is state-dependent. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn mbtowc +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa s +points to an invalid or incomplete multibyte character. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr mblen 3 , +.Xr mbrtowc 3 , +.Xr setlocale 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn mbtowc +function conforms to +.St -ansiC . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/multibyte.h b/lib/nbsd_libc/locale/multibyte.h new file mode 100644 index 000000000..2b28aec06 --- /dev/null +++ b/lib/nbsd_libc/locale/multibyte.h @@ -0,0 +1,126 @@ +/* $NetBSD: multibyte.h,v 1.5 2009/01/11 02:46:28 christos Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * 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 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. + */ + +#ifndef _MULTIBYTE_H_ +#define _MULTIBYTE_H_ + +/* mbstate_t private */ + +#ifdef _BSD_MBSTATE_T_ +typedef _BSD_MBSTATE_T_ mbstate_t; +#undef _BSD_MBSTATE_T_ +#endif + +typedef struct _RuneStatePriv { + _RuneLocale *__runelocale; + char __private __attribute__((__aligned__)); +} _RuneStatePriv; + +typedef union _RuneState { + mbstate_t __pad; + struct _RuneStatePriv __priv; +#define rs_runelocale __priv.__runelocale +#define rs_private __priv.__private +} _RuneState; +#define _PRIVSIZE (sizeof(mbstate_t)-offsetof(_RuneStatePriv, __private)) + + +/* */ + +static __inline _citrus_ctype_t +_to_cur_ctype(void) +{ + return (_CurrentRuneLocale->rl_citrus_ctype); +} + +static __inline _RuneState * +_ps_to_runestate(mbstate_t *ps) +{ + return (_RuneState *)(void *)ps; +} + +static __inline _RuneState const * +_ps_to_runestate_const(mbstate_t const *ps) +{ + return (_RuneState const *)(void const *)ps; +} + +static __inline _RuneLocale * +_ps_to_runelocale(mbstate_t const *ps) +{ + return _ps_to_runestate_const(ps)->rs_runelocale; +} + +static __inline _citrus_ctype_t +_ps_to_ctype(mbstate_t const *ps) +{ + if (!ps) + return _to_cur_ctype(); + + _DIAGASSERT(_ps_to_runelocale(ps) != NULL); + + return _ps_to_runelocale(ps)->rl_citrus_ctype; +} + +static __inline void * +_ps_to_private(mbstate_t *ps) +{ + if (ps == NULL) + return NULL; + return (void *)&_ps_to_runestate(ps)->rs_private; +} + +static __inline void const * +_ps_to_private_const(mbstate_t const *ps) +{ + if (ps == NULL) + return NULL; + return (void const *)&_ps_to_runestate_const(ps)->rs_private; +} + +static __inline void +_init_ps(_RuneLocale *rl, mbstate_t *ps) +{ + size_t dum; + _ps_to_runestate(ps)->rs_runelocale = rl; + _citrus_ctype_mbrtowc(rl->rl_citrus_ctype, NULL, NULL, 0, + _ps_to_private(ps), &dum); +} + +static __inline void +_fixup_ps(_RuneLocale *rl, mbstate_t *ps, int forceinit) +{ + /* for future multi-locale facility */ + _DIAGASSERT(rl != NULL); + + if (ps != NULL && (_ps_to_runelocale(ps) == NULL || forceinit)) { + _init_ps(rl, ps); + } +} + +#endif /*_MULTIBYTE_H_*/ diff --git a/lib/nbsd_libc/locale/multibyte_amd1.c b/lib/nbsd_libc/locale/multibyte_amd1.c new file mode 100644 index 000000000..8c74ddfed --- /dev/null +++ b/lib/nbsd_libc/locale/multibyte_amd1.c @@ -0,0 +1,183 @@ +/* $NetBSD: multibyte_amd1.c,v 1.9 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2002, 2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: multibyte_amd1.c,v 1.9 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include + +#include "setlocale_local.h" + +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "runetype_local.h" +#include "multibyte.h" + +#define _RUNE_LOCALE() \ + ((_RuneLocale *)(*_current_locale())->part_impl[(size_t)LC_CTYPE]) + +#define _CITRUS_CTYPE() \ + (_RUNE_LOCALE()->rl_citrus_ctype) + +size_t +mbrlen(const char *s, size_t n, mbstate_t *ps) +{ + size_t ret; + int err0; + + _fixup_ps(_RUNE_LOCALE(), ps, s == NULL); + + err0 = _citrus_ctype_mbrlen(_ps_to_ctype(ps), s, n, + _ps_to_private(ps), &ret); + if (err0) + errno = err0; + + return ret; +} + +int +mbsinit(const mbstate_t *ps) +{ + int ret; + int err0; + _RuneLocale *rl; + + if (ps == NULL) + return 1; + + if (_ps_to_runelocale(ps) == NULL) + rl = _RUNE_LOCALE(); + else + rl = _ps_to_runelocale(ps); + + /* mbsinit should cause no error... */ + err0 = _citrus_ctype_mbsinit(rl->rl_citrus_ctype, + _ps_to_private_const(ps), &ret); + if (err0) + errno = err0; + + return ret; +} + +size_t +mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps) +{ + size_t ret; + int err0; + + _fixup_ps(_RUNE_LOCALE(), ps, s == NULL); + + err0 = _citrus_ctype_mbrtowc(_ps_to_ctype(ps), pwc, s, n, + _ps_to_private(ps), &ret); + if (err0) + errno = err0; + + return ret; +} + +size_t +mbsrtowcs(wchar_t *pwcs, const char **s, size_t n, mbstate_t *ps) +{ + size_t ret; + int err0; + + _fixup_ps(_RUNE_LOCALE(), ps, s == NULL); + + err0 = _citrus_ctype_mbsrtowcs(_ps_to_ctype(ps), pwcs, s, n, + _ps_to_private(ps), &ret); + if (err0) + errno = err0; + + return ret; +} + +size_t +wcrtomb(char *s, wchar_t wc, mbstate_t *ps) +{ + size_t ret; + int err0; + + _fixup_ps(_RUNE_LOCALE(), ps, s == NULL); + + err0 = _citrus_ctype_wcrtomb(_ps_to_ctype(ps), s, wc, + _ps_to_private(ps), &ret); + if (err0) + errno = err0; + + return ret; +} + +size_t +wcsrtombs(char *s, const wchar_t **ppwcs, size_t n, mbstate_t *ps) +{ + size_t ret; + int err0; + + _fixup_ps(_RUNE_LOCALE(), ps, s == NULL); + + err0 = _citrus_ctype_wcsrtombs(_ps_to_ctype(ps), s, ppwcs, n, + _ps_to_private(ps), &ret); + if (err0) + errno = err0; + + return ret; +} + +wint_t +btowc(int c) +{ + wint_t ret; + int err0; + + err0 = _citrus_ctype_btowc(_CITRUS_CTYPE(), c, &ret); + if (err0) + errno = err0; + + return ret; +} + +int +wctob(wint_t wc) +{ + int ret; + int err0; + + err0 = _citrus_ctype_wctob(_CITRUS_CTYPE(), wc, &ret); + if (err0) + errno = err0; + + return ret; +} diff --git a/lib/nbsd_libc/locale/multibyte_c90.c b/lib/nbsd_libc/locale/multibyte_c90.c new file mode 100644 index 000000000..6efa35bb9 --- /dev/null +++ b/lib/nbsd_libc/locale/multibyte_c90.c @@ -0,0 +1,118 @@ +/* $NetBSD: multibyte_c90.c,v 1.8 2010/06/13 04:14:57 tnozaki Exp $ */ + +/*- + * Copyright (c)2002, 2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: multibyte_c90.c,v 1.8 2010/06/13 04:14:57 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include +#include + +#include "setlocale_local.h" + +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "runetype_local.h" + +#define _RUNE_LOCALE() \ + ((_RuneLocale *)(*_current_locale())->part_impl[(size_t)LC_CTYPE]) + +#define _CITRUS_CTYPE() \ + (_RUNE_LOCALE()->rl_citrus_ctype) + +int +mblen(const char *s, size_t n) +{ + int ret; + int err0; + + err0 = _citrus_ctype_mblen(_CITRUS_CTYPE(), s, n, &ret); + if (err0) + errno = err0; + + return ret; +} + +size_t +mbstowcs(wchar_t *pwcs, const char *s, size_t n) +{ + size_t ret; + int err0; + + err0 = _citrus_ctype_mbstowcs(_CITRUS_CTYPE(), pwcs, s, n, &ret); + if (err0) + errno = err0; + + return ret; +} + +int +mbtowc(wchar_t *pw, const char *s, size_t n) +{ + int ret; + int err0; + + err0 = _citrus_ctype_mbtowc(_CITRUS_CTYPE(), pw, s, n, &ret); + if (err0) + errno = err0; + + return ret; +} + +size_t +wcstombs(char *s, const wchar_t *wcs, size_t n) +{ + size_t ret; + int err0; + + err0 = _citrus_ctype_wcstombs(_CITRUS_CTYPE(), s, wcs, n, &ret); + if (err0) + errno = err0; + + return ret; +} + +int +wctomb(char *s, wchar_t wc) +{ + int ret; + int err0; + + err0 = _citrus_ctype_wctomb(_CITRUS_CTYPE(), s, wc, &ret); + if (err0) + errno = err0; + + return ret; +} diff --git a/lib/nbsd_libc/locale/multibyte_sb.c b/lib/nbsd_libc/locale/multibyte_sb.c new file mode 100644 index 000000000..53b3fc417 --- /dev/null +++ b/lib/nbsd_libc/locale/multibyte_sb.c @@ -0,0 +1,265 @@ +/* $NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $ */ + +/* + * Copyright (c) 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char *sccsid = "from: @(#)multibyte.c 5.1 (Berkeley) 2/18/91"; +#else +__RCSID("$NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +/* + * Stub multibyte character functions. + * This cheezy implementation is fixed to the native single-byte + * character set. + */ + +/*ARGSUSED*/ +int +mbsinit(ps) + const mbstate_t *ps; +{ + + return 1; +} + +/*ARGSUSED*/ +size_t +mbrlen(s, n, ps) + const char *s; + size_t n; + mbstate_t *ps; +{ + + /* ps appears to be unused */ + + if (s == NULL || *s == '\0') + return 0; + if (n == 0) + return (size_t)-1; + return 1; +} + +int +mblen(s, n) + const char *s; + size_t n; +{ + + /* s may be NULL */ + + return mbrlen(s, n, NULL); +} + +/*ARGSUSED*/ +size_t +mbrtowc(pwc, s, n, ps) + wchar_t *pwc; + const char *s; + size_t n; + mbstate_t *ps; +{ + + /* pwc may be NULL */ + /* s may be NULL */ + /* ps appears to be unused */ + + if (s == NULL) + return 0; + if (n == 0) + return (size_t)-1; + if (pwc) + *pwc = (wchar_t) *s; + return (*s != '\0'); +} + +int +mbtowc(pwc, s, n) + wchar_t *pwc; + const char *s; + size_t n; +{ + + /* pwc may be NULL */ + /* s may be NULL */ + + return mbrtowc(pwc, s, n, NULL); +} + +/*ARGSUSED*/ +size_t +wcrtomb(s, wchar, ps) + char *s; + wchar_t wchar; + mbstate_t *ps; +{ + + /* s may be NULL */ + /* ps appears to be unused */ + + if (s == NULL) + return 0; + + *s = (char) wchar; + return 1; +} + +int +wctomb(s, wchar) + char *s; + wchar_t wchar; +{ + + /* s may be NULL */ + + return wcrtomb(s, wchar, NULL); +} + +/*ARGSUSED*/ +size_t +mbsrtowcs(pwcs, s, n, ps) + wchar_t *pwcs; + const char **s; + size_t n; + mbstate_t *ps; +{ + int count = 0; + + /* pwcs may be NULL */ + /* s may be NULL */ + /* ps appears to be unused */ + + if (!s || !*s) + return 0; + + if (n != 0) { + if (pwcs != NULL) { + do { + if ((*pwcs++ = (wchar_t) *(*s)++) == 0) + break; + count++; + } while (--n != 0); + } else { + do { + if (((wchar_t)*(*s)++) == 0) + break; + count++; + } while (--n != 0); + } + } + + return count; +} + +size_t +mbstowcs(pwcs, s, n) + wchar_t *pwcs; + const char *s; + size_t n; +{ + + /* pwcs may be NULL */ + /* s may be NULL */ + + return mbsrtowcs(pwcs, &s, n, NULL); +} + +/*ARGSUSED*/ +size_t +wcsrtombs(s, pwcs, n, ps) + char *s; + const wchar_t **pwcs; + size_t n; + mbstate_t *ps; +{ + int count = 0; + + /* s may be NULL */ + /* pwcs may be NULL */ + /* ps appears to be unused */ + + if (pwcs == NULL || *pwcs == NULL) + return (0); + + if (s == NULL) { + while (*(*pwcs)++ != 0) + count++; + return(count); + } + + if (n != 0) { + do { + if ((*s++ = (char) *(*pwcs)++) == 0) + break; + count++; + } while (--n != 0); + } + + return count; +} + +size_t +wcstombs(s, pwcs, n) + char *s; + const wchar_t *pwcs; + size_t n; +{ + + /* s may be NULL */ + /* pwcs may be NULL */ + + return wcsrtombs(s, &pwcs, n, NULL); +} + +wint_t +btowc(c) + int c; +{ + if (c == EOF || c & ~0xFF) + return WEOF; + return (wint_t)c; +} + +int +wctob(c) + wint_t c; +{ + if (c == WEOF || c & ~0xFF) + return EOF; + return (int)c; +} diff --git a/lib/nbsd_libc/locale/nb_lc_messages_misc.h b/lib/nbsd_libc/locale/nb_lc_messages_misc.h new file mode 100644 index 000000000..a2ddc82a8 --- /dev/null +++ b/lib/nbsd_libc/locale/nb_lc_messages_misc.h @@ -0,0 +1,66 @@ +/* $NetBSD: nb_lc_messages_misc.h,v 1.3 2010/03/27 15:25:22 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _NB_LC_MESSAGES_MISC_H_ +#define _NB_LC_MESSAGES_MISC_H_ + +/* + * macro required by nb_lc_template(_decl).h + */ +#define _CATEGORY_TYPE _MessagesLocale + +static __inline void +_PREFIX(build_cache)(struct _locale_cache_t * __restrict cache, + _MessagesLocale * __restrict data) +{ + _DIAGASSERT(cache != NULL); + _DIAGASSERT(cache->items != NULL); + _DIAGASSERT(data != NULL); + + cache->items[(size_t)YESSTR ] = data->yesstr; + cache->items[(size_t)YESEXPR] = data->yesexpr; + cache->items[(size_t)NOSTR ] = data->nostr; + cache->items[(size_t)NOEXPR ] = data->noexpr; +} + +static __inline void +_PREFIX(fixup)(_MessagesLocale *data) +{ + _DIAGASSERT(data != NULL); + + _CurrentMessagesLocale = data; +} + +/* + * macro required by nb_lc_template.h + */ +#define _CATEGORY_ID LC_MESSAGES +#define _CATEGORY_NAME "LC_MESSAGES" +#define _CATEGORY_DEFAULT _DefaultMessagesLocale + +#endif /*_NB_LC_MESSAGES_MISC_H_*/ diff --git a/lib/nbsd_libc/locale/nb_lc_monetary_misc.h b/lib/nbsd_libc/locale/nb_lc_monetary_misc.h new file mode 100644 index 000000000..3b13ad3cc --- /dev/null +++ b/lib/nbsd_libc/locale/nb_lc_monetary_misc.h @@ -0,0 +1,90 @@ +/* $NetBSD: nb_lc_monetary_misc.h,v 1.3 2010/03/27 15:25:22 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _NB_LC_MONETARY_MISC_H_ +#define _NB_LC_MONETARY_MISC_H_ + +/* + * macro required by nb_lc_template(_decl).h + */ +#define _CATEGORY_TYPE _MonetaryLocale + +static __inline void +_PREFIX(build_cache)(struct _locale_cache_t * __restrict cache, + _MonetaryLocale * __restrict data) +{ + struct lconv *ldata; + + _DIAGASSERT(cache != NULL); + _DIAGASSERT(cache->ldata != NULL); + _DIAGASSERT(cache->items != NULL); + _DIAGASSERT(data != NULL); + + ldata = cache->ldata; + ldata->int_curr_symbol = __UNCONST(data->int_curr_symbol); + ldata->currency_symbol = __UNCONST(data->currency_symbol); + ldata->mon_decimal_point = __UNCONST(data->mon_decimal_point); + ldata->mon_thousands_sep = __UNCONST(data->mon_thousands_sep); + ldata->mon_grouping = __UNCONST(data->mon_grouping); + ldata->positive_sign = __UNCONST(data->positive_sign); + ldata->negative_sign = __UNCONST(data->negative_sign); + + ldata->int_frac_digits = data->int_frac_digits; + ldata->frac_digits = data->frac_digits; + ldata->p_cs_precedes = data->p_cs_precedes; + ldata->p_sep_by_space = data->p_sep_by_space; + ldata->n_cs_precedes = data->n_cs_precedes; + ldata->n_sep_by_space = data->n_sep_by_space; + ldata->p_sign_posn = data->p_sign_posn; + ldata->n_sign_posn = data->n_sign_posn; + ldata->int_p_cs_precedes = data->int_p_cs_precedes; + ldata->int_n_cs_precedes = data->int_n_cs_precedes; + ldata->int_p_sep_by_space = data-> int_p_sep_by_space; + ldata->int_n_sep_by_space = data->int_n_sep_by_space; + ldata->int_p_sign_posn = data->int_p_sign_posn; + ldata->int_n_sign_posn = data->int_n_sign_posn; + + cache->items[(size_t)CRNCYSTR] = NULL; /* NOT IMPLEMENTED YET */ +} + +static __inline void +_PREFIX(fixup)(_MonetaryLocale *data) +{ + _DIAGASSERT(data != NULL); + + _CurrentMonetaryLocale = data; +} + +/* + * macro required by nb_lc_template.h + */ +#define _CATEGORY_ID LC_MONETARY +#define _CATEGORY_NAME "LC_MONETARY" +#define _CATEGORY_DEFAULT _DefaultMonetaryLocale + +#endif /*_RUNE_LC_MONETARY_MISC_H_*/ diff --git a/lib/nbsd_libc/locale/nb_lc_numeric_misc.h b/lib/nbsd_libc/locale/nb_lc_numeric_misc.h new file mode 100644 index 000000000..4336fc1b8 --- /dev/null +++ b/lib/nbsd_libc/locale/nb_lc_numeric_misc.h @@ -0,0 +1,72 @@ +/* $NetBSD: nb_lc_numeric_misc.h,v 1.3 2010/03/27 15:25:22 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _NB_LC_NUMERIC_MISC_H_ +#define _NB_LC_NUMERIC_MISC_H_ + +/* + * macro required by nb_lc_template(_decl).h + */ +#define _CATEGORY_TYPE _NumericLocale + +static __inline void +_PREFIX(build_cache)(struct _locale_cache_t * __restrict cache, + _NumericLocale * __restrict data) +{ + struct lconv *ldata; + + _DIAGASSERT(cache != NULL); + _DIAGASSERT(cache->ldata != NULL); + _DIAGASSERT(cache->items != NULL); + _DIAGASSERT(data != NULL); + + ldata = cache->ldata; + ldata->decimal_point = __UNCONST(data->decimal_point); + ldata->thousands_sep = __UNCONST(data->thousands_sep); + ldata->grouping = __UNCONST(data->grouping); + + cache->items[(size_t)RADIXCHAR] = data->decimal_point; + cache->items[(size_t)THOUSEP ] = data->thousands_sep; +} + +static __inline void +_PREFIX(fixup)(_NumericLocale *data) +{ + _DIAGASSERT(data != NULL); + + _CurrentNumericLocale = data; +} + +/* + * macro required by nb_lc_template.h + */ +#define _CATEGORY_ID LC_NUMERIC +#define _CATEGORY_NAME "LC_NUMERIC" +#define _CATEGORY_DEFAULT _DefaultNumericLocale + +#endif /*_NB_LC_NUMERIC_MISC_H_*/ diff --git a/lib/nbsd_libc/locale/nb_lc_template.h b/lib/nbsd_libc/locale/nb_lc_template.h new file mode 100644 index 000000000..8721dca5c --- /dev/null +++ b/lib/nbsd_libc/locale/nb_lc_template.h @@ -0,0 +1,248 @@ +/* $NetBSD: nb_lc_template.h,v 1.3 2010/05/22 13:15:59 tnozaki Exp $ */ + +/*- + * Copyright (c)1999, 2008 Citrus Project, + * 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 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. + */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Paul Kranenburg. + * + * 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. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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. + */ + +#ifndef _NB_LC_TEMPLATE_H_ +#define _NB_LC_TEMPLATE_H_ + +#define _nb_part_t _PREFIX(part_t) +#define _nb_part_cache _PREFIX(part_cache) +#define _nb_default_c _PREFIX(default_c) +#define _nb_default_posix _PREFIX(default_posix) +#define _nb_mutex _PREFIX(mutex) + +typedef struct _nb_part_t { + char name[_LOCALENAME_LEN_MAX]; + _CATEGORY_TYPE *impl; + SIMPLEQ_ENTRY(_nb_part_t) entry; +} _nb_part_t; + +static SIMPLEQ_HEAD(, _nb_part_t) _nb_part_cache = + SIMPLEQ_HEAD_INITIALIZER(_nb_part_cache); + +static const _nb_part_t _nb_default_c = { + _C_LOCALE, + __UNCONST(&_CATEGORY_DEFAULT), + { NULL }, +}; + +static const _nb_part_t _nb_default_posix = { + _POSIX_LOCALE, + __UNCONST(&_CATEGORY_DEFAULT), + { NULL }, +}; + +#ifdef _REENTRANT +static mutex_t _nb_mutex = MUTEX_INITIALIZER; +#endif + +static int +_PREFIX(load_sub)(const char * __restrict name, const char * __restrict real, + _nb_part_t ** __restrict part, int force) +{ + _nb_part_t *p, *q; + int ret; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(part != NULL); + + if (!strcmp(_C_LOCALE, name)) { + p = __UNCONST(&_nb_default_c); + } else if (!strcmp(_POSIX_LOCALE, name)) { + p = __UNCONST(&_nb_default_posix); + } else { + SIMPLEQ_FOREACH(p, &_nb_part_cache, entry) { + if (!strcmp((const char *)&p->name[0], name)) + goto found; + } + p = malloc(sizeof(*p)); + if (p == NULL) + return ENOMEM; + if (force) { + p->impl = __UNCONST(&_CATEGORY_DEFAULT); + } else { + _DIAGASSERT(_PathLocale != NULL); + ret = _PREFIX(create_impl)((const char *)_PathLocale, + name, &p->impl); + if (ret) { + free(p); + return ret; + } + } + strlcpy(&p->name[0], name, sizeof(p->name)); + SIMPLEQ_INSERT_TAIL(&_nb_part_cache, p, entry); + } +found: + if (real != NULL) { + q = malloc(sizeof(*q)); + if (q == NULL) + return ENOMEM; + strlcpy(&q->name[0], real, sizeof(p->name)); + q->impl = p->impl; + SIMPLEQ_INSERT_TAIL(&_nb_part_cache, q, entry); + p = q; + } + *part = p; + return 0; +} + +static __inline int +_PREFIX(load)(const char * __restrict name, + _nb_part_t ** __restrict part) +{ + int ret, force; + char path[PATH_MAX + 1], loccat[PATH_MAX + 1], buf[PATH_MAX + 1]; + const char *aliaspath, *alias; + +#define _LOAD_SUB_ALIAS(key) \ +do { \ + alias = __unaliasname(aliaspath, key, &buf[0], sizeof(buf)); \ + if (alias != NULL) { \ + ret = (force = !__isforcemapping(alias)) \ + ? _PREFIX(load_sub)(name, NULL, part, force) \ + : _PREFIX(load_sub)(alias, name, part, force); \ + _DIAGASSERT(!ret || !force); \ + goto done; \ + } \ +} while (/*CONSTCOND*/0) + + /* (1) non-aliased file */ + mutex_lock(&_nb_mutex); + ret = _PREFIX(load_sub)(name, NULL, part, 0); + if (ret != ENOENT) + goto done; + + /* (2) lookup locname/catname type alias */ + _DIAGASSERT(_PathLocale != NULL); + snprintf(&path[0], sizeof(path), + "%s/" _LOCALE_ALIAS_NAME, _PathLocale); + aliaspath = (const char *)&path[0]; + snprintf(&loccat[0], sizeof(loccat), + "%s/" _CATEGORY_NAME, name); + _LOAD_SUB_ALIAS((const char *)&loccat[0]); + + /* (3) lookup locname type alias */ + _LOAD_SUB_ALIAS(name); + +done: + mutex_unlock(&_nb_mutex); + return ret; +} + +static const char * +_PREFIX(setlocale)(const char * __restrict name, + struct _locale_impl_t * __restrict locale) +{ + _nb_part_t *part; + + /* name may be NULL */ + _DIAGASSERT(locale != NULL); + + if (name != NULL) { + if (*name == '\0') + name = _get_locale_env(_CATEGORY_NAME); + _DIAGASSERT(name != NULL); + _DIAGASSERT(locale->part_name[(size_t)_CATEGORY_ID] != NULL); + if (strcmp(name, locale->part_name[(size_t)_CATEGORY_ID])) { + if (_PREFIX(load)(name, &part)) + return NULL; + locale->part_name[(size_t)_CATEGORY_ID] + = &part->name[0]; + locale->part_impl[(size_t)_CATEGORY_ID] + = part->impl; + _PREFIX(build_cache)(locale->cache, part->impl); + if (locale == &_global_locale) + _PREFIX(fixup)(part->impl); + } + } + return locale->part_name[(size_t)_CATEGORY_ID]; +} + +#include "generic_lc_template.h" + +#endif /*_NB_LC_TEMPLATE_H_*/ diff --git a/lib/nbsd_libc/locale/nb_lc_template_decl.h b/lib/nbsd_libc/locale/nb_lc_template_decl.h new file mode 100644 index 000000000..f87ef0edb --- /dev/null +++ b/lib/nbsd_libc/locale/nb_lc_template_decl.h @@ -0,0 +1,45 @@ +/* $NetBSD: nb_lc_template_decl.h,v 1.2 2009/01/11 02:46:29 christos Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _NB_LC_TEMPLATE_DECL_H_ +#define _NB_LC_TEMPLATE_DECL_H_ + +#include "generic_lc_template_decl.h" + +static __inline int +_PREFIX(create_impl)(const char * __restrict, + const char * __restrict, _CATEGORY_TYPE ** __restrict); + +static __inline void +_PREFIX(build_cache)(struct _locale_cache_t * __restrict, + _CATEGORY_TYPE * __restrict); + +static __inline void +_PREFIX(fixup)(_CATEGORY_TYPE *); + +#endif /*_NB_LC_TEMPLATE_DECL_H_*/ diff --git a/lib/nbsd_libc/locale/nb_lc_time_misc.h b/lib/nbsd_libc/locale/nb_lc_time_misc.h new file mode 100644 index 000000000..2f426dd20 --- /dev/null +++ b/lib/nbsd_libc/locale/nb_lc_time_misc.h @@ -0,0 +1,92 @@ +/* $NetBSD: nb_lc_time_misc.h,v 1.3 2010/03/27 15:25:22 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _NB_LC_TIME_MISC_H_ +#define _NB_LC_TIME_MISC_H_ + +/* + * macro required by nb_lc_template(_decl).h + */ +#define _CATEGORY_TYPE _TimeLocale + +#define ABDAY_IDX(idx) ((size_t)idx - (size_t)ABDAY_1) +#define DAY_IDX(idx) ((size_t)idx - (size_t)DAY_1) +#define ABMON_IDX(idx) ((size_t)idx - (size_t)ABMON_1) +#define MON_IDX(idx) ((size_t)idx - (size_t)MON_1) +#define AM_PM_IDX(idx) ((size_t)idx - (size_t)AM_STR) + +static __inline void +/*ARGSUSED*/ +_PREFIX(build_cache)(struct _locale_cache_t * __restrict cache, + _TimeLocale * __restrict data) +{ + size_t i; + + _DIAGASSERT(cache != NULL); + _DIAGASSERT(cache->items != NULL); + _DIAGASSERT(data != NULL); + + for (i = (size_t)ABDAY_1; i <= ABDAY_7; ++i) + cache->items[i] = data->abday[ABDAY_IDX(i)]; + for (i = (size_t)DAY_1; i <= DAY_7; ++i) + cache->items[i] = data->day[DAY_IDX(i)]; + for (i = (size_t)ABMON_1; i <= ABMON_12; ++i) + cache->items[i] = data->abmon[ABMON_IDX(i)]; + for (i = (size_t)MON_1; i <= MON_12; ++i) + cache->items[i] = data->mon[MON_IDX(i)]; + for (i = (size_t)AM_STR; i <= PM_STR; ++i) + cache->items[i] = data->am_pm[AM_PM_IDX(i)]; + cache->items[(size_t)D_T_FMT ] = data->d_t_fmt; + cache->items[(size_t)D_FMT ] = data->d_fmt; + cache->items[(size_t)T_FMT ] = data->t_fmt; + cache->items[(size_t)T_FMT_AMPM ] = data->t_fmt_ampm; + + /* NOT IMPLEMENTED YET */ + cache->items[(size_t)ERA ] = NULL; + cache->items[(size_t)ERA_D_FMT ] = NULL; + cache->items[(size_t)ERA_D_T_FMT] = NULL; + cache->items[(size_t)ERA_T_FMT ] = NULL; + cache->items[(size_t)ALT_DIGITS ] = NULL; +} + +static __inline void +_PREFIX(fixup)(_TimeLocale *data) +{ + _DIAGASSERT(data != NULL); + + _CurrentTimeLocale = data; +} + +/* + * macro required by nb_lc_template.h + */ +#define _CATEGORY_ID LC_TIME +#define _CATEGORY_NAME "LC_TIME" +#define _CATEGORY_DEFAULT _DefaultTimeLocale + +#endif /*_NB_LC_TIME_MISC_H_*/ diff --git a/lib/nbsd_libc/locale/nl_langinfo.3 b/lib/nbsd_libc/locale/nl_langinfo.3 new file mode 100644 index 000000000..0f9b25d13 --- /dev/null +++ b/lib/nbsd_libc/locale/nl_langinfo.3 @@ -0,0 +1,148 @@ +.\" $NetBSD: nl_langinfo.3,v 1.19 2010/03/22 19:30:54 joerg Exp $ +.\" +.\" Written by J.T. Conklin . +.\" Public domain. +.\" +.Dd February 12, 2003 +.Dt NL_LANGINFO 3 +.Os +.Sh NAME +.Nm nl_langinfo +.Nd get locale information +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In langinfo.h +.Ft char * +.Fn nl_langinfo "nl_item item" +.Sh DESCRIPTION +The +.Fn nl_langinfo +function returns a pointer to a string containing information +set by the program's locale. +.Pp +The names and values of +.Fa item +are defined in +.In langinfo.h . +The entries under Category indicate in which +.Xr setlocale 3 +category each item is defined. +.Bl -column ERA_D_T_FMT LC_MESSAGES +.It Sy Constant Ta Sy Category Ta Sy Meaning +.It CODESET LC_CTYPE Codeset name +.It D_T_FMT LC_TIME String for formatting date and time +.It D_FMT LC_TIME Date format string +.It T_FMT LC_TIME Time format string +.It T_FMT_AMPM LC_TIME A.M. or P.M. time format string +.It AM_STR LC_TIME Ante-meridiem affix +.It PM_STR LC_TIME Post-meridiem affix +.It DAY_1 LC_TIME Name of the first day of the week (e.g.: Sunday) +.It DAY_2 LC_TIME Name of the second day of the week (e.g.: Monday) +.It DAY_3 LC_TIME Name of the third day of the week (e.g.: Tuesday) +.It DAY_4 LC_TIME Name of the fourth day of the week (e.g.: Wednesday) +.It DAY_5 LC_TIME Name of the fifth day of the week (e.g.: Thursday) +.It DAY_6 LC_TIME Name of the sixth day of the week (e.g.: Friday) +.It DAY_7 LC_TIME Name of the seventh day of the week (e.g.: Saturday) +.It ABDAY_1 LC_TIME Abbreviated name of the first day of the week +.It ABDAY_2 LC_TIME Abbreviated name of the second day of the week +.It ABDAY_3 LC_TIME Abbreviated name of the third day of the week +.It ABDAY_4 LC_TIME Abbreviated name of the fourth day of the week +.It ABDAY_5 LC_TIME Abbreviated name of the fifth day of the week +.It ABDAY_6 LC_TIME Abbreviated name of the sixth day of the week +.It ABDAY_7 LC_TIME Abbreviated name of the seventh day of the week +.It MON_1 LC_TIME Name of the first month of the year +.It MON_2 LC_TIME Name of the second month +.It MON_3 LC_TIME Name of the third month +.It MON_4 LC_TIME Name of the fourth month +.It MON_5 LC_TIME Name of the fifth month +.It MON_6 LC_TIME Name of the sixth month +.It MON_7 LC_TIME Name of the seventh month +.It MON_8 LC_TIME Name of the eighth month +.It MON_9 LC_TIME Name of the ninth month +.It MON_10 LC_TIME Name of the tenth month +.It MON_11 LC_TIME Name of the eleventh month +.It MON_12 LC_TIME Name of the twelfth month +.It ABMON_1 LC_TIME Abbreviated name of the first month +.It ABMON_2 LC_TIME Abbreviated name of the second month +.It ABMON_3 LC_TIME Abbreviated name of the third month +.It ABMON_4 LC_TIME Abbreviated name of the fourth month +.It ABMON_5 LC_TIME Abbreviated name of the fifth month +.It ABMON_6 LC_TIME Abbreviated name of the sixth month +.It ABMON_7 LC_TIME Abbreviated name of the seventh month +.It ABMON_8 LC_TIME Abbreviated name of the eighth month +.It ABMON_9 LC_TIME Abbreviated name of the ninth month +.It ABMON_10 LC_TIME Abbreviated name of the tenth month +.It ABMON_11 LC_TIME Abbreviated name of the eleventh month +.It ABMON_12 LC_TIME Abbreviated name of the twelfth month +.It ERA LC_TIME Era description segments +.It ERA_D_FMT LC_TIME Era date format string +.It ERA_D_T_FMT LC_TIME Era date and time format string +.It ERA_T_FMT LC_TIME Era time format string +.It ALT_DIGITS LC_TIME Alternative symbols for digits +.It RADIXCHAR LC_NUMERIC Radix character +.It THOUSEP LC_NUMERIC Separator for thousands +.It YESEXPR LC_MESSAGES Affirmative response expression +.It NOEXPR LC_MESSAGES Negative response expression +.\".It CRNCYSTR LC_MONETARY Local currency symbol +.El +.Sh RETURN VALUES +.Fn nl_langinfo +returns a pointer to an empty string if +.Fa item +is invalid. +.Sh EXAMPLES +The following example uses +.Fn nl_langinfo +to obtain the date and time format for the current locale: +.Pp +.Bd -literal -offset indent +#include \*[Lt]time.h\*[Gt] +#include \*[Lt]langinfo.h\*[Gt] +#include \*[Lt]locale.h\*[Gt] + +int main(void) +{ + char datestring[100]; + struct tm *tm; + time_t t; + char *ptr; + + t = time(NULL); + tm = localtime(\*[Am]t); + (void)setlocale(LC_ALL, ""); + ptr = nl_langinfo(D_T_FMT); + strftime(datestring, sizeof(datestring), ptr, tm); + printf("%s\en", datestring); + return (0); +} +.Ed +.\" .Pp +.\" The following example uses +.\" .Fn nl_langinfo +.\" to obtain the setting of the currency symbol for the current locale: +.\" .Pp +.\" .Bd +.\" #include \*[Lt]langinfo.h\*[Gt] +.\" #include \*[Lt]locale.h\*[Gt] +.\" int main(void) +.\" { +.\" char *ptr; +.\" (void)setlocale(LC_ALL, ""); +.\" ptr = nl_langinfo(CRNCYSTR); +.\" printf("%s", ptr); +.\" } +.\" .Ed +.Sh SEE ALSO +.Xr setlocale 3 , +.Xr nls 7 +.Sh STANDARDS +The +.Fn nl_langinfo +function conforms to +.St -p1003.1-2001 . +.Sh HISTORY +The +.Fn nl_langinfo +function appeared in +.Nx 1.0 . diff --git a/lib/nbsd_libc/locale/nl_langinfo.c b/lib/nbsd_libc/locale/nl_langinfo.c new file mode 100644 index 000000000..4f2dc6024 --- /dev/null +++ b/lib/nbsd_libc/locale/nl_langinfo.c @@ -0,0 +1,53 @@ +/* $NetBSD: nl_langinfo.c,v 1.15 2010/05/22 13:15:59 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nl_langinfo.c,v 1.15 2010/05/22 13:15:59 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#define __SETLOCALE_SOURCE__ +#include +#include + +#include "setlocale_local.h" + +char * +nl_langinfo(nl_item item) +{ + const char *s; + + s = NULL; + if (item >= D_T_FMT && item <= ALT_DIGITS) + s = _current_cache()->items[(size_t)item]; + if (s == NULL) + s = ""; + return __UNCONST(s); +} diff --git a/lib/nbsd_libc/locale/rune.c b/lib/nbsd_libc/locale/rune.c new file mode 100644 index 000000000..2d423fc7e --- /dev/null +++ b/lib/nbsd_libc/locale/rune.c @@ -0,0 +1,351 @@ +/* $NetBSD: rune.c,v 1.41 2010/11/30 15:25:05 tnozaki Exp $ */ + +/*- + * Copyright (c)2010 Citrus Project, + * 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 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 +#define __SETLOCALE_SOURCE__ +#include +#include +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +#include "citrus_module.h" +#include "citrus_ctype.h" + +#include "runetype_local.h" +#include "bsdctype_local.h" + +#include "multibyte.h" + +#include "_wctype_local.h" +#include "_wctrans_local.h" + +typedef struct { + _RuneLocale rl; + unsigned char rlp_ctype_tab [_CTYPE_NUM_CHARS + 1]; + short rlp_tolower_tab[_CTYPE_NUM_CHARS + 1]; + short rlp_toupper_tab[_CTYPE_NUM_CHARS + 1]; + char rlp_codeset[33]; /* XXX */ +} _RuneLocalePriv; + +static __inline void +_rune_wctype_init(_RuneLocale *rl) +{ + memcpy(&rl->rl_wctype, &_DefaultRuneLocale.rl_wctype, + sizeof(rl->rl_wctype)); +} + +static __inline void +_rune_wctrans_init(_RuneLocale *rl) +{ + rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_name = "tolower"; + rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_cached = &rl->rl_maplower[0]; + rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_extmap = &rl->rl_maplower_ext; + rl->rl_wctrans[_WCTRANS_INDEX_UPPER].te_name = "toupper"; + rl->rl_wctrans[_WCTRANS_INDEX_UPPER].te_cached = &rl->rl_mapupper[0]; + rl->rl_wctrans[_WCTRANS_INDEX_UPPER].te_extmap = &rl->rl_mapupper_ext; +} + +static __inline void +_rune_init_priv(_RuneLocalePriv *rlp) +{ +#if _CTYPE_CACHE_SIZE != _CTYPE_NUM_CHARS + int i; + + for (i = _CTYPE_CACHE_SIZE; i < _CTYPE_NUM_CHARS; ++i) { + rlp->rlp_ctype_tab [i + 1] = 0; + rlp->rlp_tolower_tab[i + 1] = i; + rlp->rlp_toupper_tab[i + 1] = i; + } +#endif + rlp->rlp_ctype_tab [0] = 0; + rlp->rlp_tolower_tab[0] = EOF; + rlp->rlp_toupper_tab[0] = EOF; + + rlp->rl.rl_ctype_tab = (const unsigned char *)&rlp->rlp_ctype_tab[0]; + rlp->rl.rl_tolower_tab = (const short *)&rlp->rlp_tolower_tab[0]; + rlp->rl.rl_toupper_tab = (const short *)&rlp->rlp_toupper_tab[0]; + rlp->rl.rl_codeset = (const char *)&rlp->rlp_codeset[0]; + + _rune_wctype_init(&rlp->rl); + _rune_wctrans_init(&rlp->rl); +} + +static __inline void +_rune_find_codeset(char *s, size_t n, + char *var, size_t *plenvar) +{ + size_t lenvar; + const char *endvar; + +#define _RUNE_CODESET_LEN (sizeof(_RUNE_CODESET)-1) + + lenvar = *plenvar; + for (/**/; lenvar > _RUNE_CODESET_LEN; ++var, --lenvar) { + if (!memcmp(var, _RUNE_CODESET, _RUNE_CODESET_LEN)) { + *var = '\0'; + *plenvar -= lenvar; + endvar = &var[_RUNE_CODESET_LEN]; + while (n-- > 1 && lenvar-- > _RUNE_CODESET_LEN) { + if (*endvar == ' ' || *endvar == '\t') + break; + *s++ = *endvar++; + } + break; + } + } + *s = '\0'; +} + +static __inline int +_rune_read_file(const char * __restrict var, size_t lenvar, + _RuneLocale ** __restrict prl) +{ + int ret, i; + const _FileRuneLocale *frl; + const _FileRuneEntry *fre; + const uint32_t *frune; + _RuneLocalePriv *rlp; + _RuneLocale *rl; + _RuneEntry *re; + uint32_t *rune; + uint32_t runetype_len, maplower_len, mapupper_len, variable_len; + size_t len, n; + + if (lenvar < sizeof(*frl)) + return EFTYPE; + lenvar -= sizeof(*frl); + frl = (const _FileRuneLocale *)(const void *)var; + if (memcmp(_RUNECT10_MAGIC, &frl->frl_magic[0], sizeof(frl->frl_magic))) + return EFTYPE; + + runetype_len = be32toh(frl->frl_runetype_ext.frr_nranges); + maplower_len = be32toh(frl->frl_maplower_ext.frr_nranges); + mapupper_len = be32toh(frl->frl_mapupper_ext.frr_nranges); + len = runetype_len + maplower_len + mapupper_len; + + fre = (const _FileRuneEntry *)(const void *)(frl + 1); + frune = (const uint32_t *)(const void *)(fre + len); + + variable_len = be32toh((uint32_t)frl->frl_variable_len); + + n = (len * sizeof(*fre)) + variable_len; + if (lenvar < n) + return EFTYPE; + lenvar -= n; + + n = sizeof(*rlp) + (len * sizeof(*re)) + lenvar; + rlp = (_RuneLocalePriv *)malloc(n); + if (rlp == NULL) + return ENOMEM; + _rune_init_priv(rlp); + + rl = &rlp->rl; + re = (_RuneEntry *)(void *)(rlp + 1); + rune = (uint32_t *)(void *)(re + len); + + for (i = 0; i < _CTYPE_CACHE_SIZE; ++i) { + rl->rl_runetype[i] = be32toh(frl->frl_runetype[i]); + rl->rl_maplower[i] = be32toh((uint32_t)frl->frl_maplower[i]); + rl->rl_mapupper[i] = be32toh((uint32_t)frl->frl_mapupper[i]); + } + +#define READ_RANGE(name) \ +do { \ + const _FileRuneEntry *end_fre; \ + const uint32_t *end_frune; \ + \ + rl->rl_##name##_ext.rr_nranges = name##_len; \ + rl->rl_##name##_ext.rr_rune_ranges = re; \ + \ + end_fre = fre + name##_len; \ + while (fre < end_fre) { \ + re->re_min = be32toh((uint32_t)fre->fre_min); \ + re->re_max = be32toh((uint32_t)fre->fre_max); \ + re->re_map = be32toh((uint32_t)fre->fre_map); \ + if (re->re_map != 0) { \ + re->re_rune_types = NULL; \ + } else { \ + re->re_rune_types = rune; \ + len = re->re_max - re->re_min + 1; \ + n = len * sizeof(*frune); \ + if (lenvar < n) { \ + ret = EFTYPE; \ + goto err; \ + } \ + lenvar -= n; \ + end_frune = frune + len; \ + while (frune < end_frune) \ + *rune++ = be32toh(*frune++); \ + } \ + ++fre, ++re; \ + } \ +} while (/*CONSTCOND*/0) + + READ_RANGE(runetype); + READ_RANGE(maplower); + READ_RANGE(mapupper); + + memcpy((void *)rune, (void const *)frune, variable_len); + rl->rl_variable_len = variable_len; + rl->rl_variable = (void *)rune; + + if (lenvar > 0) { + ret = EFTYPE; + goto err; + } + + _rune_find_codeset(rlp->rlp_codeset, sizeof(rlp->rlp_codeset), + (char *)rl->rl_variable, &rl->rl_variable_len); + + ret = _citrus_ctype_open(&rl->rl_citrus_ctype, frl->frl_encoding, + rl->rl_variable, rl->rl_variable_len, _PRIVSIZE); + if (ret) + goto err; + if (__mb_len_max_runtime < + _citrus_ctype_get_mb_cur_max(rl->rl_citrus_ctype)) { + ret = EINVAL; + goto err; + } + + for (i = 0; i < _CTYPE_CACHE_SIZE; ++i) { + wint_t wc; + + ret = _citrus_ctype_btowc(rl->rl_citrus_ctype, i, &wc); + if (ret) + goto err; + if (wc == WEOF) { + rlp->rlp_ctype_tab[i + 1] = 0; + rlp->rlp_tolower_tab[i + 1] = i; + rlp->rlp_toupper_tab[i + 1] = i; + } else { + rlp->rlp_ctype_tab[i + 1] = (unsigned char) + _runetype_to_ctype(_runetype_priv(rl, wc)); + +#define CONVERT_MAP(name) \ +do { \ + wint_t map; \ + int c; \ + \ + map = _towctrans_priv(wc, _wctrans_##name(rl)); \ + if (map == wc || (_citrus_ctype_wctob(rl->rl_citrus_ctype, \ + map, &c) || c == EOF)) \ + c = i; \ + rlp->rlp_to##name##_tab[i + 1] = (short)c; \ +} while (/*CONSTCOND*/0) + + CONVERT_MAP(lower); + CONVERT_MAP(upper); + } + } + *prl = rl; + return 0; + +err: + free(rlp); + return ret; +} + +static __inline int +_rune_read_bsdctype(const char * __restrict var, size_t lenvar, + _RuneLocale ** __restrict prl) +{ + const _FileBSDCTypeLocale *fbl; + uint32_t value; + int i, bits; + uint16_t lower, upper; + _RuneLocalePriv *rlp; + _RuneLocale *rl; + + if (lenvar < sizeof(*fbl)) + return EFTYPE; + fbl = (const _FileBSDCTypeLocale *)(const void *)var; + if (memcmp(&fbl->fbl_id[0], _CTYPE_ID, sizeof(fbl->fbl_id))) + return EFTYPE; + value = be32toh(fbl->fbl_rev); + if (value != _CTYPE_REV) + return EFTYPE; + value = be32toh(fbl->fbl_num_chars); + if (value != _CTYPE_CACHE_SIZE) + return EFTYPE; + rlp = (_RuneLocalePriv *)malloc(sizeof(*rlp)); + if (rlp == NULL) + return ENOMEM; + _rune_init_priv(rlp); + rlp->rlp_codeset[0] = '\0'; + + rl = &rlp->rl; + for (i = 0; i < _CTYPE_CACHE_SIZE; ++i) { + bits = fbl->fbl_ctype_tab[i]; + lower = be16toh(fbl->fbl_tolower_tab[i]); + upper = be16toh(fbl->fbl_toupper_tab[i]); + + rlp->rlp_ctype_tab [i + 1] = (unsigned char)bits; + rlp->rlp_tolower_tab[i + 1] = (short)lower; + rlp->rlp_toupper_tab[i + 1] = (short)upper; + + rl->rl_runetype[i] = _runetype_from_ctype(bits, i); + rl->rl_maplower[i] = (__nbrune_t)lower; + rl->rl_mapupper[i] = (__nbrune_t)upper; + } + *prl = rl; + return 0; +} + +int +_rune_load(const char * __restrict var, size_t lenvar, + _RuneLocale ** __restrict prl) +{ + int ret; + + _DIAGASSERT(var != NULL || lenvar < 1); + _DIAGASSERT(prl != NULL); + + if (lenvar < 1) + return EFTYPE; + switch (*var) { + case 'R': + ret = _rune_read_file(var, lenvar, prl); + break; + case 'B': + ret = _rune_read_bsdctype(var, lenvar, prl); + break; + default: + ret = EFTYPE; + } + return ret; +} diff --git a/lib/nbsd_libc/locale/runetable.c b/lib/nbsd_libc/locale/runetable.c new file mode 100644 index 000000000..75ff584f8 --- /dev/null +++ b/lib/nbsd_libc/locale/runetable.c @@ -0,0 +1,351 @@ +/* $NetBSD: runetable.c,v 1.27 2010/06/19 13:26:52 tnozaki Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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. + * + * $FreeBSD: src/lib/libc/locale/table.c,v 1.13 2000/02/08 07:43:25 obrien Exp $ + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/27/93"; +#else +__RCSID("$NetBSD: runetable.c,v 1.27 2010/06/19 13:26:52 tnozaki Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "runetype_local.h" + +const _RuneLocale _DefaultRuneLocale = { + { /*00*/ _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + /*08*/ _RUNETYPE_C, + _RUNETYPE_C|_RUNETYPE_S|_RUNETYPE_B, + _RUNETYPE_C|_RUNETYPE_S, + _RUNETYPE_C|_RUNETYPE_S, + _RUNETYPE_C|_RUNETYPE_S, + _RUNETYPE_C|_RUNETYPE_S, + _RUNETYPE_C, + _RUNETYPE_C, + /*10*/ _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + /*18*/ _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + _RUNETYPE_C, + /*20*/ _RUNETYPE_S|_RUNETYPE_B|_RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + /*28*/ _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + /*30*/ _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|0, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|1, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|2, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|3, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|4, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|5, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|6, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|7, + /*38*/ _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|8, + _RUNETYPE_D|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_X|_RUNETYPE_SW1|9, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + /*40*/ _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|10, + _RUNETYPE_U|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|11, + _RUNETYPE_U|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|12, + _RUNETYPE_U|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|13, + _RUNETYPE_U|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|14, + _RUNETYPE_U|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|15, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + /*48*/ _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + /*50*/ _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + /*58*/ _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_U|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + /*60*/ _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|10, + _RUNETYPE_L|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|11, + _RUNETYPE_L|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|12, + _RUNETYPE_L|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|13, + _RUNETYPE_L|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|14, + _RUNETYPE_L|_RUNETYPE_X|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1|15, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + /*68*/ _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + /*70*/ _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + /*78*/ _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_L|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_A|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_P|_RUNETYPE_R|_RUNETYPE_G|_RUNETYPE_SW1, + _RUNETYPE_C, +#ifdef ALL_80_TO_FF_SW1 + /*80*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + /*90*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + /*A0*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + /*B0*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + /*C0*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + /*D0*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + /*E0*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + /*F0*/ _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, + _RUNETYPE_R|_RUNETYPE_SW1, _RUNETYPE_R|_RUNETYPE_SW1, +#endif + }, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + }, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + }, + { 0, NULL }, + { 0, NULL }, + { 0, NULL }, + NULL, 0, + "646", + &_citrus_ctype_default, + { + { "towlower", + __UNCONST(&_DefaultRuneLocale.rl_maplower[0]), + __UNCONST(&_DefaultRuneLocale.rl_maplower_ext) + }, + { "towupper", + __UNCONST(&_DefaultRuneLocale.rl_mapupper[0]), + __UNCONST(&_DefaultRuneLocale.rl_mapupper_ext) + }, + }, + { + { "alnum", _RUNETYPE_A|_RUNETYPE_D }, + { "alpha", _RUNETYPE_A }, + { "blank", _RUNETYPE_B }, + { "cntrl", _RUNETYPE_C }, + { "digit", _RUNETYPE_D }, + { "graph", _RUNETYPE_G }, + { "lower", _RUNETYPE_L }, + { "print", _RUNETYPE_R }, + { "punct", _RUNETYPE_P }, + { "space", _RUNETYPE_S }, + { "upper", _RUNETYPE_U }, + { "xdigit", _RUNETYPE_X }, + }, + _C_ctype_, + _C_tolower_, + _C_toupper_ +}; + +const _RuneLocale *_CurrentRuneLocale = &_DefaultRuneLocale; diff --git a/lib/nbsd_libc/locale/runetype_file.h b/lib/nbsd_libc/locale/runetype_file.h new file mode 100644 index 000000000..67d59dbf8 --- /dev/null +++ b/lib/nbsd_libc/locale/runetype_file.h @@ -0,0 +1,136 @@ +/* $NetBSD: runetype_file.h,v 1.3 2010/06/20 02:23:15 tnozaki Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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. + * + * @(#)runetype.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _RUNETYPE_FILE_H_ +#define _RUNETYPE_FILE_H_ + +#include +#include + +#include "ctype_local.h" + +/* for cross host tools on older systems */ +#ifndef UINT32_C +/* assumes sizeof(unsigned int)>=4 */ +#define UINT32_C(c) ((uint32_t)(c##U)) +#endif + +typedef uint32_t __nbrune_t; +typedef uint64_t __runepad_t; + +#define _DEFAULT_INVALID_RUNE ((__nbrune_t)-3) + +/* + * The lower 8 bits of runetype[] contain the digit value of the rune. + */ +typedef uint32_t _RuneType; +#define _RUNETYPE_A UINT32_C(0x00000100) /* Alpha */ +#define _RUNETYPE_C UINT32_C(0x00000200) /* Control */ +#define _RUNETYPE_D UINT32_C(0x00000400) /* Digit */ +#define _RUNETYPE_G UINT32_C(0x00000800) /* Graph */ +#define _RUNETYPE_L UINT32_C(0x00001000) /* Lower */ +#define _RUNETYPE_P UINT32_C(0x00002000) /* Punct */ +#define _RUNETYPE_S UINT32_C(0x00004000) /* Space */ +#define _RUNETYPE_U UINT32_C(0x00008000) /* Upper */ +#define _RUNETYPE_X UINT32_C(0x00010000) /* X digit */ +#define _RUNETYPE_B UINT32_C(0x00020000) /* Blank */ +#define _RUNETYPE_R UINT32_C(0x00040000) /* Print */ +#define _RUNETYPE_I UINT32_C(0x00080000) /* Ideogram */ +#define _RUNETYPE_T UINT32_C(0x00100000) /* Special */ +#define _RUNETYPE_Q UINT32_C(0x00200000) /* Phonogram */ +#define _RUNETYPE_SWM UINT32_C(0xc0000000)/* Mask to get screen width data */ +#define _RUNETYPE_SWS 30 /* Bits to shift to get width */ +#define _RUNETYPE_SW0 UINT32_C(0x20000000) /* 0 width character */ +#define _RUNETYPE_SW1 UINT32_C(0x40000000) /* 1 width character */ +#define _RUNETYPE_SW2 UINT32_C(0x80000000) /* 2 width character */ +#define _RUNETYPE_SW3 UINT32_C(0xc0000000) /* 3 width character */ + +/* + * rune file format. network endian. + */ +typedef struct { + int32_t fre_min; /* First rune of the range */ + int32_t fre_max; /* Last rune (inclusive) of the range */ + int32_t fre_map; /* What first maps to in maps */ + uint32_t fre_pad1; /* backward compatibility */ + __runepad_t fre_pad2; /* backward compatibility */ +} __packed _FileRuneEntry; + + +typedef struct { + uint32_t frr_nranges; /* Number of ranges stored */ + uint32_t frr_pad1; /* backward compatibility */ + __runepad_t frr_pad2; /* backward compatibility */ +} __packed _FileRuneRange; + + +typedef struct { + char frl_magic[8]; /* Magic saying what version we are */ + char frl_encoding[32];/* ASCII name of this encoding */ + + __runepad_t frl_pad1; /* backward compatibility */ + __runepad_t frl_pad2; /* backward compatibility */ + int32_t frl_invalid_rune; + uint32_t frl_pad3; /* backward compatibility */ + + _RuneType frl_runetype[_CTYPE_CACHE_SIZE]; + int32_t frl_maplower[_CTYPE_CACHE_SIZE]; + int32_t frl_mapupper[_CTYPE_CACHE_SIZE]; + + /* + * The following are to deal with Runes larger than _CTYPE_CACHE_SIZE - 1. + * Their data is actually contiguous with this structure so as to make + * it easier to read/write from/to disk. + */ + _FileRuneRange frl_runetype_ext; + _FileRuneRange frl_maplower_ext; + _FileRuneRange frl_mapupper_ext; + + __runepad_t frl_pad4; /* backward compatibility */ + int32_t frl_variable_len;/* how long that data is */ + uint32_t frl_pad5; /* backward compatibility */ + + /* variable size data follows */ +} __packed _FileRuneLocale; + + +/* magic number for LC_CTYPE (rune)locale declaration */ +#define _RUNECT10_MAGIC "RuneCT10" /* Indicates version 0 of RuneLocale */ + +/* codeset tag */ +#define _RUNE_CODESET "CODESET=" + +#endif /* !_RUNETYPE_FILE_H_ */ diff --git a/lib/nbsd_libc/locale/runetype_local.h b/lib/nbsd_libc/locale/runetype_local.h new file mode 100644 index 000000000..1fd9bb90c --- /dev/null +++ b/lib/nbsd_libc/locale/runetype_local.h @@ -0,0 +1,141 @@ +/* $NetBSD: runetype_local.h,v 1.12 2010/06/20 02:23:15 tnozaki Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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. + * + * @(#)rune.h 8.1 (Berkeley) 6/27/93 + * @(#)runetype.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _RUNETYPE_LOCAL_H_ +#define _RUNETYPE_LOCAL_H_ + +#include +#include +#include + +#include "runetype_misc.h" + +#define _RUNE_ISCACHED(c) ((c)>=0 && (c)<_CTYPE_CACHE_SIZE) + + +/* + * expanded rune locale declaration. local to the host. host endian. + */ +typedef struct { + __nbrune_t re_min; /* First rune of the range */ + __nbrune_t re_max; /* Last rune (inclusive) of the range */ + __nbrune_t re_map; /* What first maps to in maps */ + _RuneType *re_rune_types; /* Array of types in range */ +} _RuneEntry; + + +typedef struct { + uint32_t rr_nranges; /* Number of ranges stored */ + _RuneEntry *rr_rune_ranges; +} _RuneRange; + + +/* + * wctrans stuffs. + */ +typedef struct _WCTransEntry { + const char *te_name; + __nbrune_t *te_cached; + _RuneRange *te_extmap; +} _WCTransEntry; +#define _WCTRANS_INDEX_LOWER 0 +#define _WCTRANS_INDEX_UPPER 1 +#define _WCTRANS_NINDEXES 2 + +/* + * wctype stuffs. + */ +typedef struct _WCTypeEntry { + const char *te_name; + _RuneType te_mask; +} _WCTypeEntry; +#define _WCTYPE_INDEX_ALNUM 0 +#define _WCTYPE_INDEX_ALPHA 1 +#define _WCTYPE_INDEX_BLANK 2 +#define _WCTYPE_INDEX_CNTRL 3 +#define _WCTYPE_INDEX_DIGIT 4 +#define _WCTYPE_INDEX_GRAPH 5 +#define _WCTYPE_INDEX_LOWER 6 +#define _WCTYPE_INDEX_PRINT 7 +#define _WCTYPE_INDEX_PUNCT 8 +#define _WCTYPE_INDEX_SPACE 9 +#define _WCTYPE_INDEX_UPPER 10 +#define _WCTYPE_INDEX_XDIGIT 11 +#define _WCTYPE_NINDEXES 12 + +/* + * ctype stuffs + */ + +typedef struct _RuneLocale { + /* + * copied from _FileRuneLocale + */ + _RuneType rl_runetype[_CTYPE_CACHE_SIZE]; + __nbrune_t rl_maplower[_CTYPE_CACHE_SIZE]; + __nbrune_t rl_mapupper[_CTYPE_CACHE_SIZE]; + _RuneRange rl_runetype_ext; + _RuneRange rl_maplower_ext; + _RuneRange rl_mapupper_ext; + + void *rl_variable; + size_t rl_variable_len; + + /* + * the following portion is generated on the fly + */ + const char *rl_codeset; + struct _citrus_ctype_rec *rl_citrus_ctype; + _WCTransEntry rl_wctrans[_WCTRANS_NINDEXES]; + _WCTypeEntry rl_wctype[_WCTYPE_NINDEXES]; + + const unsigned char *rl_ctype_tab; + const short *rl_tolower_tab; + const short *rl_toupper_tab; +} _RuneLocale; + +/* + * global variables + */ +extern const _RuneLocale _DefaultRuneLocale; +extern const _RuneLocale *_CurrentRuneLocale; + +__BEGIN_DECLS +int _rune_load(const char * __restrict, size_t, _RuneLocale ** __restrict); +__END_DECLS + +#endif /* !_RUNETYPE_LOCAL_H_ */ diff --git a/lib/nbsd_libc/locale/runetype_misc.h b/lib/nbsd_libc/locale/runetype_misc.h new file mode 100644 index 000000000..df4c5b7d1 --- /dev/null +++ b/lib/nbsd_libc/locale/runetype_misc.h @@ -0,0 +1,130 @@ +/* $NetBSD: runetype_misc.h,v 1.2 2010/12/14 02:28:57 joerg Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * 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. + * + * @(#)runetype.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _RUNETYPE_MISC_H_ +#define _RUNETYPE_MISC_H_ + +#include +#include "runetype_file.h" + +static __inline int +_runetype_to_ctype(_RuneType bits) +{ + int ret; + + if (bits == (_RuneType)0) + return 0; + ret = 0; + if (bits & _RUNETYPE_U) + ret |= _CTYPE_U; + if (bits & _RUNETYPE_L) + ret |= _CTYPE_L; + if (bits & _RUNETYPE_D) + ret |= _CTYPE_N; + if (bits & _RUNETYPE_S) + ret |= _CTYPE_S; + if (bits & _RUNETYPE_P) + ret |= _CTYPE_P; + if (bits & _RUNETYPE_C) + ret |= _CTYPE_C; + if (bits & _RUNETYPE_X) + ret |= _CTYPE_X; + /* + * TWEAK! _B has been used incorrectly (or with older + * declaration) in ctype.h isprint() macro. + * _B does not mean isblank, it means "isprint && !isgraph". + * the following is okay since isblank() was hardcoded in + * function (i.e. isblank() is inherently locale unfriendly). + */ +#if 1 + if ((bits & (_RUNETYPE_R | _RUNETYPE_G)) == _RUNETYPE_R) + ret |= _CTYPE_B; +#else + if (bits & _RUNETYPE_B) + ret |= _CTYPE_B; +#endif + return ret; +} + +static __inline _RuneType +_runetype_from_ctype(int bits, int ch) +{ + _RuneType ret; + + /* + * TWEAKS! + * - old locale file declarations do not have proper _B + * in many cases. + * - isprint() declaration in ctype.h incorrectly uses _B. + * _B means "isprint but !isgraph", not "isblank" with the + * declaration. + * - _X and _RUNETYPE_X have negligible difference in meaning. + * - we don't set digit value, fearing that it would be + * too much of hardcoding. we may need to revisit it. + */ + + ret = (_RuneType)0; + if (bits & _CTYPE_U) + ret |= _RUNETYPE_U; + if (bits & _CTYPE_L) + ret |= _RUNETYPE_L; + if (bits & _CTYPE_N) + ret |= _RUNETYPE_D; + if (bits & _CTYPE_S) + ret |= _RUNETYPE_S; + if (bits & _CTYPE_P) + ret |= _RUNETYPE_P; + if (bits & _CTYPE_C) + ret |= _RUNETYPE_C; + /* derived flag bits, duplicate of ctype.h */ + if (bits & (_CTYPE_U|_CTYPE_L)) + ret |= _RUNETYPE_A; + if (bits & (_CTYPE_N|_CTYPE_X)) + ret |= _RUNETYPE_X; + if (bits & (_CTYPE_P|_CTYPE_U|_CTYPE_L|_CTYPE_N)) + ret |= _RUNETYPE_G; + /* we don't really trust _B in the file. see above. */ + if (bits & _CTYPE_B) + ret |= _RUNETYPE_B; + if ((bits & (_CTYPE_P|_CTYPE_U|_CTYPE_L|_CTYPE_N|_CTYPE_B)) || + ch == ' ') + ret |= (_RUNETYPE_R | _RUNETYPE_SW1); + if (ch == ' ' || ch == '\t') + ret |= _RUNETYPE_B; + return ret; +} + +#endif /* !_RUNETYPE_MISC_H_ */ diff --git a/lib/nbsd_libc/locale/setlocale.3 b/lib/nbsd_libc/locale/setlocale.3 new file mode 100644 index 000000000..9d88b9929 --- /dev/null +++ b/lib/nbsd_libc/locale/setlocale.3 @@ -0,0 +1,407 @@ +.\" $NetBSD: setlocale.3,v 1.21 2004/01/24 16:58:54 wiz Exp $ +.\" +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Donn Seeley at BSDI. +.\" +.\" 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. +.\" +.\" @(#)setlocale.3 8.1 (Berkeley) 6/9/93 +.\" +.Dd May 30, 2003 +.Dt SETLOCALE 3 +.Os +.Sh NAME +.Nm setlocale , +.Nm localeconv +.Nd natural language formatting for C +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In locale.h +.Ft char * +.Fn setlocale "int category" "const char *locale" +.Ft struct lconv * +.Fn localeconv "void" +.Sh DESCRIPTION +The +.Fn setlocale +function sets the C library's notion +of natural language formatting style +for particular sets of routines. +Each such style is called a +.Sq locale +and is invoked using an appropriate name passed as a C string. +The +.Fn localeconv +routine returns the current locale's parameters +for formatting numbers. +.Pp +The +.Fn setlocale +function recognizes several categories of routines. +These are the categories and the sets of routines they select: +.Bl -tag -width LC_MONETARY +.It Dv LC_ALL +Set the entire locale generically. +.It Dv LC_COLLATE +Set a locale for string collation routines. +This controls alphabetic ordering in +.Fn strcoll +and +.Fn strxfrm . +.It Dv LC_CTYPE +Set a locale for the +.Xr ctype 3 +functions. +This controls recognition of upper and lower case, +alphabetic or non-alphabetic characters, +and so on. +The real work is done by the +.Fn setrunelocale +function. +.It Dv LC_MESSAGES +Set a locale for message catalogs. +This controls the selection of message catalogs by the +.Xr catgets 3 +and +.Xr gettext 3 +families of functions. +.It Dv LC_MONETARY +Set a locale for formatting monetary values; +this affects the +.Fn localeconv +function. +.It Dv LC_NUMERIC +Set a locale for formatting numbers. +This controls the formatting of decimal points +in input and output of floating point numbers +in functions such as +.Fn printf +and +.Fn scanf , +as well as values returned by +.Fn localeconv . +.It Dv LC_TIME +Set a locale for formatting dates and times using the +.Fn strftime +function. +.El +.Pp +Only three locales are defined by default, +the empty string +.Li "\&""\|"" +which denotes the native environment, and the +.Li "\&""C"" +and +.Li "\&""POSIX"" +locales, which denote the C language environment. +A +.Fa locale +argument of +.Dv NULL +causes +.Fn setlocale +to return the current locale. +By default, C programs start in the +.Li "\&""C"" +locale. +The format of the locale string is described in +.Xr nls 7 . +.Pp +The only function in the library that sets the locale is +.Fn setlocale ; +the locale is never changed as a side effect of some other routine. +.Pp +Changing the setting of +.Dv LC_MESSAGES +has no effect on catalogs that have already been opened by +.Xr catopen 3 . +.Pp +The +.Fn localeconv +function returns a pointer to a structure +which provides parameters for formatting numbers, +especially currency values: +.Bd -literal -offset indent +struct lconv { + char *decimal_point; + char *thousands_sep; + char *grouping; + char *int_curr_symbol; + char *currency_symbol; + char *mon_decimal_point; + char *mon_thousands_sep; + char *mon_grouping; + char *positive_sign; + char *negative_sign; + char int_frac_digits; + char frac_digits; + char p_cs_precedes; + char p_sep_by_space; + char n_cs_precedes; + char n_sep_by_space; + char p_sign_posn; + char n_sign_posn; + char int_p_cs_precedes; + char int_n_cs_precedes; + char int_p_sep_by_space; + char int_n_sep_by_space; + char int_p_sign_posn; + char int_n_sign_posn; +}; +.Ed +.Pp +The individual fields have the following meanings: +.Bl -tag -width int_p_sep_by_space +.It Fa decimal_point +The decimal point character, except for monetary values. +.It Fa thousands_sep +The separator between groups of digits +before the decimal point, except for monetary values. +.It Fa grouping +The sizes of the groups of digits, except for monetary values. +This is a pointer to a vector of integers, each of size +.Va char , +representing group size from low order digit groups +to high order (right to left). +The list may be terminated with 0 or +.Dv CHAR_MAX . +If the list is terminated with 0, +the last group size before the 0 is repeated to account for all the digits. +If the list is terminated with +.Dv CHAR_MAX , +no more grouping is performed. +.It Fa int_curr_symbol +The standardized (ISO 4217:1995) international currency symbol. +.It Fa currency_symbol +The local currency symbol. +.It Fa mon_decimal_point +The decimal point character for monetary values. +.It Fa mon_thousands_sep +The separator for digit groups in monetary values. +.It Fa mon_grouping +Like +.Fa grouping +but for monetary values. +.It Fa positive_sign +The character used to denote nonnegative monetary values, +usually the empty string. +.It Fa negative_sign +The character used to denote negative monetary values, +usually a minus sign. +.It Fa int_frac_digits +The number of digits after the decimal point +in an internationally formatted monetary value. +.It Fa frac_digits +The number of digits after the decimal point +in an locally formatted monetary value. +.It Fa p_cs_precedes +1 if the currency symbol precedes the monetary value +for nonnegative values, 0 if it follows. +.It Fa p_sep_by_space +1 if a space is inserted between the currency symbol +and the monetary value for nonnegative values, 0 otherwise. +.It Fa n_cs_precedes +Like +.Fa p_cs_precedes +but for negative values. +.It Fa n_sep_by_space +Like +.Fa p_sep_by_space +but for negative values. +.It Fa p_sign_posn +The location of the +.Fa positive_sign +with respect to a nonnegative quantity and the +.Fa currency_symbol . +.It Fa n_sign_posn +Like +.Fa p_sign_posn +but for negative currency values. +.It Fa int_p_cs_precedes +1 if the currency symbol precedes the internationally +formatted monetary value for nonnegative values, 0 if it follows. +.It Fa int_n_cs_precedes +Like +.Fa int_p_cs_precedes +but for negative values. +.It Fa int_p_sep_by_space +1 if a space is inserted between the currency symbol +and the internationally formatted monetary value for +nonnegative values, 0 otherwise. +.It Fa int_n_sep_by_space +Like +.Fa int_p_sep_by_space +but for negative values. +.It Fa int_p_sign_posn +The location of the +.Fa positive_sign +with respect to a nonnegative quantity and the +.Fa currency_symbol , +for internationally formatted nonnegative monetary values. +.It Fa int_n_sign_posn +Like +.Fa int_p_sign_posn +but for negative values. +.El +.Pp +The positional parameters in +.Fa p_sign_posn , +.Fa n_sign_posn , +.Fa int_p_sign_posn +and +.Fa int_n_sign_posn +are encoded as follows: +.Bl -tag -width 3n -compact +.It Li 0 +Parentheses around the entire string. +.It Li 1 +Before the string. +.It Li 2 +After the string. +.It Li 3 +Just before +.Fa currency_symbol . +.It Li 4 +Just after +.Fa currency_symbol . +.El +.Pp +Unless mentioned above, +an empty string as a value for a field +indicates a zero length result or +a value that is not in the current locale. +A +.Dv CHAR_MAX +result similarly denotes an unavailable value. +.Sh RETURN VALUES +The +.Fn setlocale +function returns +.Dv NULL +and fails to change the locale +if the given combination of +.Fa category +and +.Fa locale +makes no sense. +The +.Fn localeconv +function returns a pointer to a static object +which may be altered by later calls to +.Fn setlocale +or +.Fn localeconv . +.Sh EXAMPLES +The following code illustrates how a program can initialize the +international environment for one language, while selectively +modifying the program's locale such that regular expressions and +string operations can be applied to text recorded in a different +language: +.Bd -literal + setlocale(LC_ALL, "de"); + setlocale(LC_COLLATE, "fr"); +.Ed +.Pp +When a process is started, its current locale is set to the C or POSIX +locale. +An internationalized program that depends on locale data not defined in +the C or POSIX locale must invoke the setlocale subroutine in the +following manner before using any of the locale-specific information: +.Bd -literal + setlocale(LC_ALL, ""); +.Ed +.\" .Sh FILES XXX +.\" .Bl -tag -width /usr/share/locale/locale/category -compact XXX +.\" .It Pa $PATH_LOCALE/\fIlocale\fP/\fIcategory\fP XXX +.\" .It Pa /usr/share/locale/\fIlocale\fP/\fIcategory\fP XXX +.\" locale file for the locale \fIlocale\fP XXX +.\" and the category \fIcategory\fP. XXX +.\" .El +.Sh SEE ALSO +.Xr catopen 3 , +.Xr gettext 3 , +.Xr nl_langinfo 3 , +.Xr nls 7 +.\" .Xr strcoll 3 , XXX +.\" .Xr strxfrm 3 XXX +.Sh STANDARDS +The +.Fn setlocale +and +.Fn localeconv +functions conform to +.St -ansiC +and +.St -isoC-90 . +.Pp +The +.Fa int_p_cs_precedes , +.Fa int_n_cs_precedes , +.Fa int_p_sep_by_space , +.Fa int_n_sep_by_space , +.Fa int_p_sign_posn +and +.Fa int_n_sign_posn +members of +.Ft struct lconv +were introduced in +.St -isoC-99 . +.Sh HISTORY +The +.Fn setlocale +and +.Fn localeconv +functions first appeared in +.Bx 4.4 . +.Sh BUGS +The current implementation supports only the +.Li "\&""C"" +and +.Li "\&""POSIX"" +locales for all but the +.Dv LC_CTYPE +locale. +.Pp +In spite of the gnarly currency support in +.Fn localeconv , +the standards don't include any functions +for generalized currency formatting. +.Pp +.Dv LC_COLLATE +does not make sense for many languages. +Use of +.Dv LC_MONETARY +could lead to misleading results until we have a real time currency +conversion function. +.Dv LC_NUMERIC +and +.Dv LC_TIME +are personal choices and should not be wrapped up with the other categories. +.Pp +Multibyte locales aren't supported for static binaries. diff --git a/lib/nbsd_libc/locale/setlocale.c b/lib/nbsd_libc/locale/setlocale.c new file mode 100644 index 000000000..bc3734ed6 --- /dev/null +++ b/lib/nbsd_libc/locale/setlocale.c @@ -0,0 +1,141 @@ +/* $NetBSD: setlocale.c,v 1.58 2010/06/07 13:52:30 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: setlocale.c,v 1.58 2010/06/07 13:52:30 tnozaki Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +#include "setlocale_local.h" + +const char *_PathLocale = NULL; + +__link_set_decl(all_categories, _locale_category_t); + +extern const _locale_category_t _generic_LC_ALL_desc; +extern const _locale_category_t _dummy_LC_COLLATE_desc; +#ifdef WITH_RUNE +extern const _locale_category_t _citrus_LC_CTYPE_desc; +extern const _locale_category_t _citrus_LC_MONETARY_desc; +extern const _locale_category_t _citrus_LC_NUMERIC_desc; +extern const _locale_category_t _citrus_LC_TIME_desc; +extern const _locale_category_t _citrus_LC_MESSAGES_desc; +#else +extern const _locale_category_t _localeio_LC_CTYPE_desc; +extern const _locale_category_t _localeio_LC_MONETARY_desc; +extern const _locale_category_t _localeio_LC_NUMERIC_desc; +extern const _locale_category_t _localeio_LC_TIME_desc; +extern const _locale_category_t _localeio_LC_MESSAGES_desc; +#endif + +__link_set_add_data(all_categories, _generic_LC_ALL_desc); +__link_set_add_data(all_categories, _dummy_LC_COLLATE_desc); +#ifdef WITH_RUNE +__link_set_add_data(all_categories, _citrus_LC_CTYPE_desc); +__link_set_add_data(all_categories, _citrus_LC_MONETARY_desc); +__link_set_add_data(all_categories, _citrus_LC_NUMERIC_desc); +__link_set_add_data(all_categories, _citrus_LC_TIME_desc); +__link_set_add_data(all_categories, _citrus_LC_MESSAGES_desc); +#else +__link_set_add_data(all_categories, _localeio_LC_CTYPE_desc); +__link_set_add_data(all_categories, _localeio_LC_MONETARY_desc); +__link_set_add_data(all_categories, _localeio_LC_NUMERIC_desc); +__link_set_add_data(all_categories, _localeio_LC_TIME_desc); +__link_set_add_data(all_categories, _localeio_LC_MESSAGES_desc); +#endif + +_locale_category_t * +_find_category(int category) +{ + _locale_category_t * const *p; + + __link_set_foreach(p, all_categories) { + if ((*p)->category == category) + return *p; + } + return NULL; +} + +const char * +_get_locale_env(const char *category) +{ + const char *name; + + /* 1. check LC_ALL */ + name = (const char *)getenv("LC_ALL"); + if (name == NULL || *name == '\0') { + /* 2. check LC_* */ + name = (const char *)getenv(category); + if (name == NULL || *name == '\0') { + /* 3. check LANG */ + name = getenv("LANG"); + } + } + if (name == NULL || *name == '\0' || strchr(name, '/')) + /* 4. if none is set, fall to "C" */ + name = _C_LOCALE; + return name; +} + +char * +__setlocale(int category, const char *name) +{ + _locale_category_t *l; + struct _locale_impl_t *impl; + + if (category >= LC_ALL && category < _LC_LAST) { + l = _find_category(category); + if (l != NULL) { + if (issetugid() || ((_PathLocale == NULL && + (_PathLocale = getenv("PATH_LOCALE")) == NULL) || + *_PathLocale == '\0')) + _PathLocale = _PATH_LOCALE; + impl = *_current_locale(); + return __UNCONST((*l->setlocale)(name, impl)); + } + } + return NULL; +} + +char * +setlocale(int category, const char *locale) +{ + + /* locale may be NULL */ + + __mb_len_max_runtime = MB_LEN_MAX; + return __setlocale(category, locale); +} diff --git a/lib/nbsd_libc/locale/setlocale_local.h b/lib/nbsd_libc/locale/setlocale_local.h new file mode 100644 index 000000000..15601a994 --- /dev/null +++ b/lib/nbsd_libc/locale/setlocale_local.h @@ -0,0 +1,83 @@ +/* $NetBSD: setlocale_local.h,v 1.7 2010/06/07 13:52:30 tnozaki Exp $ */ + +/*- + * Copyright (c)2008 Citrus Project, + * 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 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. + */ + +#ifndef _SETLOCALE_LOCAL_H_ +#define _SETLOCALE_LOCAL_H_ + +#define _LOCALENAME_LEN_MAX 33 + +#define _C_LOCALE "C" +#define _POSIX_LOCALE "POSIX" + +extern const char *_PathLocale; +#define _LOCALE_ALIAS_NAME "locale.alias" + +typedef void *_locale_part_t; + +struct _locale_cache_t { + const unsigned char *ctype_tab; + const short *tolower_tab; + const short *toupper_tab; + size_t mb_cur_max; + struct lconv *ldata; + const char **items; +}; + +struct _locale_impl_t { + struct _locale_cache_t *cache; + char query[_LOCALENAME_LEN_MAX * (_LC_LAST - 1)]; + const char *part_name[_LC_LAST]; + _locale_part_t part_impl[_LC_LAST]; +}; + +typedef const char *(*_locale_set_t)(const char * __restrict, + struct _locale_impl_t * __restrict); + +typedef struct { + const char* name; + int category; + _locale_set_t setlocale; +} _locale_category_t; + +__BEGIN_DECLS +_locale_category_t *_find_category(int); +const char *_get_locale_env(const char *); +struct _locale_impl_t **_current_locale(void); +char *__setlocale(int, const char *); +__END_DECLS + +static __inline struct _locale_cache_t * +_current_cache(void) +{ + return (*_current_locale())->cache; +} + +extern struct _locale_impl_t _global_locale; +extern size_t __mb_len_max_runtime; + +#endif /*_SETLOCALE_LOCAL_H_*/ diff --git a/lib/nbsd_libc/locale/towctrans.3 b/lib/nbsd_libc/locale/towctrans.3 new file mode 100644 index 000000000..7772755ad --- /dev/null +++ b/lib/nbsd_libc/locale/towctrans.3 @@ -0,0 +1,87 @@ +.\" $NetBSD: towctrans.3,v 1.6 2005/06/27 14:18:36 wiz Exp $ +.\" +.\" Copyright (c)2003 Citrus Project, +.\" 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 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. +.\" +.Dd March 4, 2003 +.Dt TOWCTRANS 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm towctrans +.Nd convert a wide character with a specified map +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wctype.h +.Ft wint_t +.Fn towctrans "wint_t wc" "wctrans_t charmap" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn towctrans +function converts a wide character +.Fa wc +with a character mapping +.Fa charmap . +.Pp +The behaviour of +.Fn towctrans +is undefined if the +.Fn towctrans +function is called with an invalid +.Fa charmap +(changes of +.Dv LC_CTYPE +category invalidate +.Fa charmap ) +or invalid wide character +.Fa wc . +.Pp +The behaviour of +.Fn towctrans +is affected by the +.Dv LC_CTYPE +category of the current locale. +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +.Fn towctrans +returns the resulting character of the conversion. +.\" ---------------------------------------------------------------------- +.Sh ERRORS +No errors are defined. +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr iswctype 3 , +.Xr setlocale 3 , +.Xr wctrans 3 , +.Xr wctype 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn towctrans +function conforms to +.St -isoC-amd1 . diff --git a/lib/nbsd_libc/locale/towlower.3 b/lib/nbsd_libc/locale/towlower.3 new file mode 100644 index 000000000..f6a675b10 --- /dev/null +++ b/lib/nbsd_libc/locale/towlower.3 @@ -0,0 +1,69 @@ +.\" $NetBSD: towlower.3,v 1.7 2003/09/08 17:54:31 wiz Exp $ +.\" +.\" Copyright (c) 1989, 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. +.\" +.\" @(#)tolower.3 5.2 (Berkeley) 6/29/91 +.\" +.Dd December 22, 2000 +.Dt TOWLOWER 3 +.Os +.Sh NAME +.Nm towlower +.Nd wide character case letter conversion utilities +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wctype.h +.Ft wint_t +.Fn towlower "wint_t wc" +.Ft wint_t +.Fn towupper "wint_t wc" +.Sh DESCRIPTION +The +.Fn towlower +function converts an upper-case wide character to the corresponding lower-case +letter. +The +.Fn towupper +function converts an lower-case wide character to the corresponding upper-case +letter. +.Sh RETURN VALUES +If the argument is an upper/lower-case letter, the +.Fn tolower +function returns the corresponding counterpart if there is +one; otherwise the argument is returned unchanged. +.Sh SEE ALSO +.Xr tolower 3 , +.Xr toupper 3 +.Sh STANDARDS +The functions conform to +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/wcrtomb.3 b/lib/nbsd_libc/locale/wcrtomb.3 new file mode 100644 index 000000000..fb03f05f8 --- /dev/null +++ b/lib/nbsd_libc/locale/wcrtomb.3 @@ -0,0 +1,145 @@ +.\" $NetBSD: wcrtomb.3,v 1.9 2007/02/20 08:33:25 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd February 4, 2002 +.Dt WCRTOMB 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm wcrtomb +.Nd converts a wide character to a multibyte character (restartable) +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wchar.h +.Ft size_t +.Fn wcrtomb "char * restrict s" "wchar_t wc" "mbstate_t * restrict ps" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +.Fn wcrtomb +converts the wide character given by +.Fa wc +to the corresponding multibyte character, and stores it in the array +pointed to by +.Fa s +unless +.Fa s +is a null pointer. +This function will modify the first at most +.Dv MB_CUR_MAX +bytes of the array pointed to by +.Fa s . +.Pp +The behaviour of +.Fn wcrtomb +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +These are the special cases: +.Bl -tag -width 012345678901 +.It "wc == 0" +For state-dependent encodings, +.Fn wcrtomb +stores a nul byte preceded by special byte sequence (if any) +to return to an initial state in the array pointed to by +.Fa s , +and the state object pointed to by +.Fa ps +also returns to an initial state. +.It "s == NULL" +.Fn wcrtomb +just places +.Fa ps +into an initial state. +It is equivalent to the following call: +.Bd -literal -offset indent +wcrtomb(buf, L'\\0', ps); +.Ed +.Pp +Here, +.Fa buf +is a dummy buffer. +In this case, +.Fa wc +is ignored. +.It "ps == NULL" +.Fn mbrtowc +uses its own internal state object to keep the conversion state, +instead of +.Fa ps +mentioned in this manual page. +.Pp +Calling any other functions in +.Lb libc +never changes the internal +state of +.Fn mbrtowc , +which is initialized at startup time of the program. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +.Fn wcrtomb +returns: +.Bl -tag -width 012345678901 +.It "positive" +The number of bytes (including any shift sequences) +which are stored in the array. +.It "(size_t)-1" +.Fa wc +is not a valid wide character. +In this case, +.Fn wcrtomb +also sets +.Va errno +to indicate the error. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn wcrtomb +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa wc +is not a valid wide character. +.It Bq Er EINVAL +.Fa ps +points to an invalid or uninitialized mbstate_t object. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr setlocale 3 , +.Xr wctomb 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn wcrtomb +function conforms to +.St -isoC-amd1 . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/wcscoll.3 b/lib/nbsd_libc/locale/wcscoll.3 new file mode 100644 index 000000000..1e238158e --- /dev/null +++ b/lib/nbsd_libc/locale/wcscoll.3 @@ -0,0 +1,112 @@ +.\" $NetBSD: wcscoll.3,v 1.3 2010/12/16 17:42:27 wiz Exp $ +.\" +.\" 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 +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. 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. +.\" +.\" @(#)strcoll.3 8.1 (Berkeley) 6/4/93 +.\" FreeBSD: src/lib/libc/string/strcoll.3,v 1.11 2001/10/01 16:09:00 ru Exp +.\" FreeBSD: /repoman/r/ncvs/src/lib/libc/string/wcscoll.3,v 1.2 2002/12/09 14:04:05 ru Exp +.\" +.Dd October 13, 2006 +.Dt WCSCOLL 3 +.Os +.Sh NAME +.Nm wcscoll +.Nd compare wide strings according to current collation +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wchar.h +.Ft int +.Fn wcscoll "const wchar_t *s1" "const wchar_t *s2" +.Sh DESCRIPTION +The +.Fn wcscoll +function compares the nul-terminated strings +.Fa s1 +and +.Fa s2 +according to the current locale collation order. +In the +.Dq Li C +locale, +.Fn wcscoll +is equivalent to +.Fn wcscmp . +.Sh RETURN VALUES +The +.Fn wcscoll +function returns an integer greater than, equal to, or less than +0, if +.Fa s1 +is greater than, equal to, or less than +.Fa s2 . +.Pp +No return value is reserved to indicate errors; +callers should set +.Va errno +to 0 before calling +.Fn wcscoll . +If it is non-zero upon return from +.Fn wcscoll , +an error has occurred. +.Sh ERRORS +The +.Fn wcscoll +function will fail if: +.Bl -tag -width Er +.It Bq Er EILSEQ +An invalid wide-character code was specified. +.It Bq Er ENOMEM +Cannot allocate enough memory for temporary buffers. +.El +.Sh SEE ALSO +.Xr setlocale 3 , +.Xr strcoll 3 , +.Xr wcscmp 3 , +.Xr wcsxfrm 3 +.Sh STANDARDS +The +.Fn wcscoll +function conforms to +.St -isoC-99 . +.Sh BUGS +The current implementation of +.Fn wcscoll +function disregards +.Dv LC_COLLATE +locales, and falls back to using the +.Fn wcscmp +function. diff --git a/lib/nbsd_libc/locale/wcscoll.c b/lib/nbsd_libc/locale/wcscoll.c new file mode 100644 index 000000000..2e589f390 --- /dev/null +++ b/lib/nbsd_libc/locale/wcscoll.c @@ -0,0 +1,48 @@ +/* $NetBSD: wcscoll.c,v 1.1 2003/03/02 22:18:16 tshiozak Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcscoll.c,v 1.1 2003/03/02 22:18:16 tshiozak Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +/* + * Compare strings with using collating information. + */ +int +wcscoll(s1, s2) + const wchar_t *s1, *s2; +{ + /* XXX: LC_COLLATE should be implemented. */ + return (wcscmp(s1, s2)); +} diff --git a/lib/nbsd_libc/locale/wcsftime.3 b/lib/nbsd_libc/locale/wcsftime.3 new file mode 100644 index 000000000..779593a62 --- /dev/null +++ b/lib/nbsd_libc/locale/wcsftime.3 @@ -0,0 +1,69 @@ +.\" $NetBSD: wcsftime.3,v 1.2 2005/04/06 21:39:17 kleink Exp $ +.\" +.\" Copyright (c) 2002 Tim J. Robbins +.\" 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 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. +.\" +.\" $FreeBSD: /repoman/r/ncvs/src/lib/libc/locale/wcsftime.3,v 1.2 2002/11/29 17:35:09 ru Exp $ +.\" +.Dd September 8, 2002 +.Dt WCSFTIME 3 +.Os +.Sh NAME +.Nm wcsftime +.Nd "convert date and time to a wide-character string" +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wchar.h +.Ft size_t +.Fo wcsftime +.Fa "wchar_t * restrict wcs" "size_t maxsize" +.Fa "const wchar_t * restrict format" "const struct tm * restrict timeptr" +.Fc +.Sh DESCRIPTION +The +.Fn wcsftime +function is equivalent to the +.Fn strftime +function except for the types of its arguments +and the return value indicating the number of wide characters. +Refer to +.Xr strftime 3 +for a detailed description. +.Sh COMPATIBILITY +Some early implementations of +.Fn wcsftime +had a +.Fa format +argument with type +.Vt "const char *" +instead of +.Vt "const wchar_t *" . +.Sh SEE ALSO +.Xr strftime 3 +.Sh STANDARDS +The +.Fn wcsftime +function conforms to +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/wcsftime.c b/lib/nbsd_libc/locale/wcsftime.c new file mode 100644 index 000000000..2816ae18d --- /dev/null +++ b/lib/nbsd_libc/locale/wcsftime.c @@ -0,0 +1,108 @@ +/* $NetBSD: wcsftime.c,v 1.3 2007/05/21 15:32:17 tnozaki Exp $ */ +/*- + * Copyright (c) 2002 Tim J. Robbins + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +__FBSDID("$FreeBSD: src/lib/libc/locale/wcsftime.c,v 1.4 2004/04/07 09:47:56 tjr Exp $"); +#else +__RCSID("$NetBSD: wcsftime.c,v 1.3 2007/05/21 15:32:17 tnozaki Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include + +/* + * Convert date and time to a wide-character string. + * + * This is the wide-character counterpart of strftime(). So that we do not + * have to duplicate the code of strftime(), we convert the format string to + * multibyte, call strftime(), then convert the result back into wide + * characters. + * + * This technique loses in the presence of stateful multibyte encoding if any + * of the conversions in the format string change conversion state. When + * stateful encoding is implemented, we will need to reset the state between + * format specifications in the format string. + */ +size_t +wcsftime(wchar_t *wcs, size_t maxsize, + const wchar_t *format, const struct tm *timeptr) +{ + char *dst, *dstp, *sformat; + size_t n, sflen; + int sverrno; + + sformat = dst = NULL; + + /* + * Convert the supplied format string to a multibyte representation + * for strftime(), which only handles single-byte characters. + */ + sflen = wcstombs(NULL, format, 0); + if (sflen == (size_t)-1) + goto error; + if ((sformat = malloc(sflen + 1)) == NULL) + goto error; + wcstombs(sformat, format, sflen + 1); + + /* + * Allocate memory for longest multibyte sequence that will fit + * into the caller's buffer and call strftime() to fill it. + * Then, copy and convert the result back into wide characters in + * the caller's buffer. + */ + if (SIZE_T_MAX / MB_CUR_MAX <= maxsize) { + /* maxsize is preposterously large - avoid int. overflow. */ + errno = EINVAL; + goto error; + } + dst = malloc(maxsize * MB_CUR_MAX); + if (dst == NULL) + goto error; + if (strftime(dst, maxsize, sformat, timeptr) == 0) + goto error; + dstp = dst; + n = mbstowcs(wcs, dstp, maxsize); + if (n == (size_t)-2 || n == (size_t)-1) + goto error; + + free(sformat); + free(dst); + return n; + +error: + sverrno = errno; + free(sformat); + free(dst); + errno = sverrno; + return 0; +} diff --git a/lib/nbsd_libc/locale/wcsrtombs.3 b/lib/nbsd_libc/locale/wcsrtombs.3 new file mode 100644 index 000000000..1f30eb9ad --- /dev/null +++ b/lib/nbsd_libc/locale/wcsrtombs.3 @@ -0,0 +1,176 @@ +.\" $NetBSD: wcsrtombs.3,v 1.13 2010/12/16 17:42:27 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd August 8, 2006 +.Dt WCSRTOMBS 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm wcsrtombs +.Nd converts a wide-character string to a multibyte character string \ +(restartable) +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In wchar.h +.Ft size_t +.Fn wcsrtombs "char * restrict s" "const wchar_t ** restrict pwcs" \ +"size_t n" "mbstate_t * restrict ps" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn wcsrtombs +converts the nul-terminated wide-character string indirectly pointed +to by +.Fa pwcs +to the corresponding multibyte character string, +and stores it in the array pointed to by +.Fa s . +The conversion stops due to the following reasons: +.Bl -bullet +.It +The conversion reaches a nul wide character. +In this case, the nul wide character is also converted. +.It +The +.Fn wcsrtombs +has already stored +.Fa n +bytes in the array pointed to by +.Fa s . +.It +The conversion encounters an invalid character. +.El +.Pp +Each character will be converted as if +.Xr wcrtomb 3 +is continuously called, except the internal state of +.Xr wcrtomb 3 +will not be affected. +.Pp +After conversion, +if +.Fa s +is not a null pointer, the pointer object pointed to by +.Fa pwcs +is a null pointer (if the conversion is stopped due to reaching a +nul wide character) or the first byte of the character just after +the last character converted. +.Pp +If +.Fa s +is not a null pointer and the conversion is stopped due to reaching +a nul wide character, +.Fn wcsrtombs +places the state object pointed to by +.Fa ps +to an initial state after the conversion is taken place. +.Pp +The behaviour of +.Fn wcsrtombs +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +These are the special cases: +.Bl -tag -width 012345678901 +.It "s == NULL" +.Fn wcsrtombs +returns the number of bytes to store the whole multibyte character string +corresponding to the wide-character string pointed to by +.Fa pwcs , +not including the terminating nul byte. +In this case, +.Fa n +is ignored. +.It "pwcs == NULL || *pwcs == NULL" +Undefined (may cause the program to crash). +.It "ps == NULL" +.Fn wcsrtombs +uses its own internal state object to keep the conversion state, +instead of +.Fa ps +mentioned in this manual page. +.Pp +Calling any other functions in +.Lb libc +never changes the internal +state of +.Fn wcsrtombs , +which is initialized at startup time of the program. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +.Fn wcsrtombs +returns: +.Bl -tag -width 012345678901 +.It 0 or positive +Number of bytes stored in the array pointed to by +.Fa s , +except for a nul byte. +There are no cases that the value returned is greater than +.Fa n +(unless +.Fa s +is a null pointer). +If the return value is equal to +.Fa n , +the string pointed to by +.Fa s +will not be nul-terminated. +.It (size_t)-1 +.Fa pwcs +points to a string containing an invalid wide character. +The +.Fn wcsrtombs +also sets +.Va errno +to indicate the error. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn wcsrtombs +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa pwcs +points to a string containing an invalid wide character. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr setlocale 3 , +.Xr wcrtomb 3 , +.Xr wcstombs 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn wcsrtombs +function conforms to +.St -ansiC . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/wcstod.3 b/lib/nbsd_libc/locale/wcstod.3 new file mode 100644 index 000000000..965527faf --- /dev/null +++ b/lib/nbsd_libc/locale/wcstod.3 @@ -0,0 +1,78 @@ +.\" $NetBSD: wcstod.3,v 1.5 2007/05/21 15:29:51 tnozaki Exp $ +.\" +.\" Copyright (c) 2002, 2003 Tim J. Robbins +.\" 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 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. +.\" +.\" $FreeBSD: wcstod.3,v 1.4 2003/05/22 13:02:27 ru Exp $ +.\" +.Dd February 22, 2003 +.Dt WCSTOD 3 +.Os +.Sh NAME +.Nm wcstof , +.Nm wcstod , +.Nm wcstold +.Nd convert string to +.Vt float , double , +or +.Vt "long double" +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wchar.h +.Ft float +.Fn wcstof "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" +.Ft "long double" +.Fn wcstold "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" +.Ft double +.Fn wcstod "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" +.Sh DESCRIPTION +The +.Fn wcstof , +.Fn wcstod , +and +.Fn wcstold +functions are the wide-character versions of the +.Fn strtof , +.Fn strtod , +and +.Fn strtold +functions. +Refer to +.Xr strtod 3 +for details. +.Sh SEE ALSO +.Xr strtod 3 , +.Xr wcstol 3 +.Sh STANDARDS +The +.Fn wcstod +function conforms to +.St -isoC-amd1 . +The +.Fn wcstof +and +.Fn wcstold +functions conform to +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/wcstod.c b/lib/nbsd_libc/locale/wcstod.c new file mode 100644 index 000000000..849dfd212 --- /dev/null +++ b/lib/nbsd_libc/locale/wcstod.c @@ -0,0 +1,50 @@ +/* $NetBSD: wcstod.c,v 1.14 2008/04/25 16:43:00 christos Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstod.c,v 1.14 2008/04/25 16:43:00 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include + +__weak_alias(wcstod,_wcstod) + +#define _FUNCNAME wcstod +#define _RETURN_TYPE double +#define _STRTOD_FUNC strtod + +#include "_wcstod.h" diff --git a/lib/nbsd_libc/locale/wcstof.c b/lib/nbsd_libc/locale/wcstof.c new file mode 100644 index 000000000..9b4e436bd --- /dev/null +++ b/lib/nbsd_libc/locale/wcstof.c @@ -0,0 +1,50 @@ +/* $NetBSD: wcstof.c,v 1.3 2008/04/25 16:43:00 christos Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstof.c,v 1.3 2008/04/25 16:43:00 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include + +__weak_alias(wcstof,_wcstof) + +#define _FUNCNAME wcstof +#define _RETURN_TYPE float +#define _STRTOD_FUNC strtof + +#include "_wcstod.h" diff --git a/lib/nbsd_libc/locale/wcstoimax.c b/lib/nbsd_libc/locale/wcstoimax.c new file mode 100644 index 000000000..7b5ea95aa --- /dev/null +++ b/lib/nbsd_libc/locale/wcstoimax.c @@ -0,0 +1,49 @@ +/* $NetBSD: wcstoimax.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstoimax.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +#include "__wctoint.h" + +#define _FUNCNAME wcstoimax +#define __INT intmax_t +#define __INT_MIN INTMAX_MIN +#define __INT_MAX INTMAX_MAX + +#include "_wcstol.h" diff --git a/lib/nbsd_libc/locale/wcstol.3 b/lib/nbsd_libc/locale/wcstol.3 new file mode 100644 index 000000000..549928592 --- /dev/null +++ b/lib/nbsd_libc/locale/wcstol.3 @@ -0,0 +1,96 @@ +.\" $NetBSD: wcstol.3,v 1.3 2010/12/16 17:42:27 wiz Exp $ +.\" +.\" Copyright (c) 2002 Tim J. Robbins +.\" 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 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. +.\" +.\" $FreeBSD: src/lib/libc/locale/wcstol.3,v 1.4 2002/11/29 17:35:09 ru Exp $ +.\" +.Dd September 7, 2002 +.Dt WCSTOL 3 +.Os +.Sh NAME +.Nm wcstol , wcstoul , +.Nm wcstoll , wcstoull , +.Nm wcstoimax , wcstoumax +.Nd "convert a wide-character string value to a" +.Vt long , +.Vt "unsigned long" , +.Vt "long long" , +.Vt "unsigned long long" , +.Vt intmax_t +or +.Vt uintmax_t +integer +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wchar.h +.Ft long +.Fn wcstol "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base" +.Ft "unsigned long" +.Fn wcstoul "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base" +.Ft "long long" +.Fn wcstoll "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base" +.Ft "unsigned long long" +.Fn wcstoull "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base" +.In inttypes.h +.Ft intmax_t +.Fn wcstoimax "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base" +.Ft uintmax_t +.Fn wcstoumax "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base" +.Sh DESCRIPTION +The +.Fn wcstol , +.Fn wcstoul , +.Fn wcstoll , +.Fn wcstoull , +.Fn wcstoimax +and +.Fn wcstoumax +functions are wide-character versions of the +.Fn strtol , +.Fn strtoul , +.Fn strtoll , +.Fn strtoull , +.Fn strtoimax +and +.Fn strtoumax +functions, respectively. +Refer to their manual pages (for example +.Xr strtol 3 ) +for details. +.Sh SEE ALSO +.Xr strtol 3 , +.Xr strtoul 3 +.Sh STANDARDS +The +.Fn wcstol , +.Fn wcstoul , +.Fn wcstoll , +.Fn wcstoull , +.Fn wcstoimax +and +.Fn wcstoumax +functions conform to +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/wcstol.c b/lib/nbsd_libc/locale/wcstol.c new file mode 100644 index 000000000..0bb35b7c0 --- /dev/null +++ b/lib/nbsd_libc/locale/wcstol.c @@ -0,0 +1,48 @@ +/* $NetBSD: wcstol.c,v 1.4 2007/03/10 00:12:23 hubertf Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstol.c,v 1.4 2007/03/10 00:12:23 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#include "__wctoint.h" + +#define _FUNCNAME wcstol +#define __INT long +#define __INT_MIN LONG_MIN +#define __INT_MAX LONG_MAX + +#include "_wcstol.h" diff --git a/lib/nbsd_libc/locale/wcstold.c b/lib/nbsd_libc/locale/wcstold.c new file mode 100644 index 000000000..a9eaab8dd --- /dev/null +++ b/lib/nbsd_libc/locale/wcstold.c @@ -0,0 +1,50 @@ +/* $NetBSD: wcstold.c,v 1.3 2008/07/08 00:23:28 gmcgarry Exp $ */ + +/*- + * Copyright (c)2006 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstold.c,v 1.3 2008/07/08 00:23:28 gmcgarry Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include + +__weak_alias(wcstold,_wcstold) + +#define _FUNCNAME wcstold +#define _RETURN_TYPE long double +#define _STRTOD_FUNC strtold + +#include "_wcstod.h" diff --git a/lib/nbsd_libc/locale/wcstoll.c b/lib/nbsd_libc/locale/wcstoll.c new file mode 100644 index 000000000..332d87622 --- /dev/null +++ b/lib/nbsd_libc/locale/wcstoll.c @@ -0,0 +1,48 @@ +/* $NetBSD: wcstoll.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstoll.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#include "__wctoint.h" + +#define _FUNCNAME wcstoll +#define __INT /* LONGLONG */ long long int +#define __INT_MIN LLONG_MIN +#define __INT_MAX LLONG_MAX + +#include "_wcstol.h" diff --git a/lib/nbsd_libc/locale/wcstombs.3 b/lib/nbsd_libc/locale/wcstombs.3 new file mode 100644 index 000000000..3b426fec1 --- /dev/null +++ b/lib/nbsd_libc/locale/wcstombs.3 @@ -0,0 +1,132 @@ +.\" $NetBSD: wcstombs.3,v 1.12 2010/12/16 17:42:27 wiz Exp $ +.\" +.\" Copyright (c)2002 Citrus Project, +.\" 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 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. +.\" +.Dd August 8, 2006 +.Dt WCSTOMBS 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm wcstombs +.Nd converts a wide-character string to a multibyte character string +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In stdlib.h +.Ft size_t +.Fn wcstombs "char * restrict s" "const wchar_t * restrict pwcs" "size_t n" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +.Fn wcstombs +converts the nul-terminated wide-character string pointed to by +.Fa pwcs +to the corresponding multibyte character string, +and stores it in the array pointed to by +.Fa s . +This function may modify the first at most +.Fa n +bytes of the array pointed to by +.Fa s . +Each character will be converted as if +.Xr wctomb 3 +is continuously called, except the internal state of +.Xr wctomb 3 +will not be affected. +.Pp +For state-dependent encoding, the +.Fn wcstombs +implies the result multibyte character string pointed to by +.Fa s +always to begin with an initial state. +.Pp +The behaviour of +.Fn wcstombs +is affected by the +.Dv LC_CTYPE +category of the current locale. +.Pp +These are the special cases: +.Bl -tag -width 012345678901 +.It s == NULL +The +.Fn wcstombs +returns the number of bytes to store the whole multibyte character string +corresponding to the wide-character string pointed to by +.Fa pwcs . +In this case, +.Fa n +is ignored. +.It pwcs == NULL +Undefined (may cause the program to crash). +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +.Fn wcstombs +returns: +.Bl -tag -width 012345678901 +.It 0 or positive +Number of bytes stored in the array pointed to by +.Fa s . +There are no cases that the value returned is greater than +.Fa n +(unless +.Fa s +is a null pointer). +If the return value is equal to +.Fa n , +the string pointed to by +.Fa s +will not be nul-terminated. +.It (size_t)-1 +.Fa pwcs +points to a string containing an invalid wide character. +.Fn wcstombs +also sets +.Va errno +to indicate the error. +.El +.\" ---------------------------------------------------------------------- +.Sh ERRORS +.Fn wcstombs +may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EILSEQ +.Fa pwcs +points to a string containing an invalid wide character. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr setlocale 3 , +.Xr wctomb 3 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +The +.Fn wcstombs +function conforms to +.St -ansiC . +The restrict qualifier is added at +.St -isoC-99 . diff --git a/lib/nbsd_libc/locale/wcstoul.c b/lib/nbsd_libc/locale/wcstoul.c new file mode 100644 index 000000000..a29d610b7 --- /dev/null +++ b/lib/nbsd_libc/locale/wcstoul.c @@ -0,0 +1,47 @@ +/* $NetBSD: wcstoul.c,v 1.4 2007/03/10 00:12:23 hubertf Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstoul.c,v 1.4 2007/03/10 00:12:23 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#include "__wctoint.h" + +#define _FUNCNAME wcstoul +#define __UINT unsigned long +#define __UINT_MAX ULONG_MAX + +#include "_wcstoul.h" diff --git a/lib/nbsd_libc/locale/wcstoull.c b/lib/nbsd_libc/locale/wcstoull.c new file mode 100644 index 000000000..6ef74023f --- /dev/null +++ b/lib/nbsd_libc/locale/wcstoull.c @@ -0,0 +1,47 @@ +/* $NetBSD: wcstoull.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstoull.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#include "__wctoint.h" + +#define _FUNCNAME wcstoull +#define __UINT /* LONGLONG */ unsigned long long int +#define __UINT_MAX ULLONG_MAX + +#include "_wcstoul.h" diff --git a/lib/nbsd_libc/locale/wcstoumax.c b/lib/nbsd_libc/locale/wcstoumax.c new file mode 100644 index 000000000..fc11ec515 --- /dev/null +++ b/lib/nbsd_libc/locale/wcstoumax.c @@ -0,0 +1,48 @@ +/* $NetBSD: wcstoumax.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcstoumax.c,v 1.3 2007/03/10 00:12:23 hubertf Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +#include "__wctoint.h" + +#define _FUNCNAME wcstoumax +#define __UINT uintmax_t +#define __UINT_MAX UINTMAX_MAX + +#include "_wcstoul.h" diff --git a/lib/nbsd_libc/locale/wcsxfrm.3 b/lib/nbsd_libc/locale/wcsxfrm.3 new file mode 100644 index 000000000..96d07cc6a --- /dev/null +++ b/lib/nbsd_libc/locale/wcsxfrm.3 @@ -0,0 +1,107 @@ +.\" $NetBSD: wcsxfrm.3,v 1.4 2010/12/16 17:42:27 wiz Exp $ +.\" +.\" 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 +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. 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. +.\" +.\" @(#)strxfrm.3 8.1 (Berkeley) 6/4/93 +.\" FreeBSD: src/lib/libc/string/strxfrm.3,v 1.16 2002/09/06 11:24:06 tjr Exp +.\" FreeBSD: /repoman/r/ncvs/src/lib/libc/string/wcsxfrm.3,v 1.2 2002/12/09 14:04:05 ru Exp +.\" +.Dd October 13, 2006 +.Dt WCSXFRM 3 +.Os +.Sh NAME +.Nm wcsxfrm +.Nd transform a wide string under locale +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In wchar.h +.Ft size_t +.Fn wcsxfrm "wchar_t * restrict dst" "const wchar_t * restrict src" "size_t n" +.Sh DESCRIPTION +The +.Fn wcsxfrm +function transforms a nul-terminated wide-character string pointed to by +.Fa src +according to the current locale collation order then copies the +transformed string into +.Fa dst . +No more than +.Fa n +wide characters are copied into +.Fa dst , +including the terminating nul character added. +If +.Fa n +is set to 0 +(it helps to determine an actual size needed for transformation), +.Fa dst +is permitted to be a null pointer. +.Pp +Comparing two strings using +.Fn wcscmp +after +.Fn wcsxfrm +is equivalent to comparing two original strings with +.Fn wcscoll . +.Sh RETURN VALUES +Upon successful completion, +.Fn wcsxfrm +returns the length of the transformed string not including +the terminating nul character. +If this value is +.Fa n +or more, the contents of +.Fa dst +are indeterminate. +.Sh SEE ALSO +.Xr setlocale 3 , +.Xr strxfrm 3 , +.Xr wcscmp 3 , +.Xr wcscoll 3 +.Sh STANDARDS +The +.Fn wcsxfrm +function conforms to +.St -isoC-99 . +.Sh BUGS +The current implementation of +.Fn wcsxfrm +function disregards +.Dv LC_COLLATE +locales, and falls back to using the +.Fn wcsncpy +function. diff --git a/lib/nbsd_libc/locale/wcsxfrm.c b/lib/nbsd_libc/locale/wcsxfrm.c new file mode 100644 index 000000000..87b68ec19 --- /dev/null +++ b/lib/nbsd_libc/locale/wcsxfrm.c @@ -0,0 +1,65 @@ +/* $NetBSD: wcsxfrm.c,v 1.2 2006/10/15 16:14:08 christos Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: wcsxfrm.c,v 1.2 2006/10/15 16:14:08 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include + +/* + * Compare strings with using collating information. + */ +size_t +wcsxfrm(s1, s2, n) + wchar_t *s1; + const wchar_t *s2; + size_t n; +{ + size_t len; + + /* XXX: LC_COLLATE should be implemented. */ + + len = wcslen(s2); + if (len + +# MD4/MD5 sources +.PATH: ${ARCHDIR}/md ${.CURDIR}/md + +SRCS+= md4c.c md5c.c md4hl.c md5hl.c + +MAN+= md4.3 md5.3 + +MLINKS+=md4.3 MD4Init.3 md4.3 MD4Update.3 md4.3 MD4Final.3 +MLINKS+=md4.3 MD4End.3 md4.3 MD4File.3 md4.3 MD4Data.3 +MLINKS+=md5.3 MD5Init.3 md5.3 MD5Update.3 md5.3 MD5Final.3 +MLINKS+=md5.3 MD5End.3 md5.3 MD5File.3 md5.3 MD5Data.3 + +CLEANFILES+= md[45]hl.c md[45].3 + +.if ${MKMAN} != "no" +md4.3: mdX.3 + sed -e 's/mdX/md4/g' -e 's/MDX/MD4/g' $> > $@ + cat ${>:H}/md4.copyright >> $@ + +md5.3: mdX.3 + sed -e 's/mdX/md5/g' -e 's/MDX/MD5/g' $> > $@ + cat ${>:H}/md5.copyright >> $@ +.endif diff --git a/lib/nbsd_libc/md/md2hl.c b/lib/nbsd_libc/md/md2hl.c new file mode 100644 index 000000000..8d26581ab --- /dev/null +++ b/lib/nbsd_libc/md/md2hl.c @@ -0,0 +1,24 @@ +/* $NetBSD: md2hl.c,v 1.5 2005/06/12 05:34:34 lukem Exp $ */ + +/* + * Written by Jason R. Thorpe , April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: md2hl.c,v 1.5 2005/06/12 05:34:34 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define MDALGORITHM MD2 + +#include "namespace.h" +#include + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#if !HAVE_MD2_H +#include "mdXhl.c" +#endif diff --git a/lib/nbsd_libc/md/md4.copyright b/lib/nbsd_libc/md/md4.copyright new file mode 100644 index 000000000..5a395f0c2 --- /dev/null +++ b/lib/nbsd_libc/md/md4.copyright @@ -0,0 +1,21 @@ +.\" $NetBSD: md4.copyright,v 1.1 1997/01/30 01:01:40 thorpej Exp $ +.\" from FreeBSD Id: md4.copyright,v 1.2 1996/10/09 21:00:43 wollman Exp +Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All +rights reserved. +.Pp +License to copy and use this software is granted provided that it +is identified as the "RSA Data Security, Inc. MD4 Message-Digest +Algorithm" in all material mentioning or referencing this software +or this function. +License is also granted to make and use derivative works provided +that such works are identified as "derived from the RSA Data +Security, Inc. MD4 Message-Digest Algorithm" in all material +mentioning or referencing the derived work. +.Pp +RSA Data Security, Inc. makes no representations concerning either +the merchantability of this software or the suitability of this +software for any particular purpose. It is provided "as is" +without express or implied warranty of any kind. +.Pp +These notices must be retained in any copies of any part of this +documentation and/or software. diff --git a/lib/nbsd_libc/md/md4hl.c b/lib/nbsd_libc/md/md4hl.c new file mode 100644 index 000000000..4517bf10a --- /dev/null +++ b/lib/nbsd_libc/md/md4hl.c @@ -0,0 +1,16 @@ +/* $NetBSD: md4hl.c,v 1.7 2005/09/26 03:01:41 christos Exp $ */ + +/* + * Written by Jason R. Thorpe , April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: md4hl.c,v 1.7 2005/09/26 03:01:41 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define MDALGORITHM MD4 +#define MDINCLUDE + +#include "mdXhl.c" diff --git a/lib/nbsd_libc/md/md5.copyright b/lib/nbsd_libc/md/md5.copyright new file mode 100644 index 000000000..b7e1beed9 --- /dev/null +++ b/lib/nbsd_libc/md/md5.copyright @@ -0,0 +1,22 @@ +.\" $NetBSD: md5.copyright,v 1.1 1997/01/30 01:01:41 thorpej Exp $ +.\" from FreeBSD Id: md5.copyright,v 1.2 1996/10/09 21:00:46 wollman Exp +Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All +rights reserved. +.Pp +License to copy and use this software is granted provided that it +is identified as the "RSA Data Security, Inc. MD5 Message-Digest +Algorithm" in all material mentioning or referencing this software +or this function. +.Pp +License is also granted to make and use derivative works provided +that such works are identified as "derived from the RSA Data +Security, Inc. MD5 Message-Digest Algorithm" in all material +mentioning or referencing the derived work. +.Pp +RSA Data Security, Inc. makes no representations concerning either +the merchantability of this software or the suitability of this +software for any particular purpose. It is provided "as is" +without express or implied warranty of any kind. +.Pp +These notices must be retained in any copies of any part of this +documentation and/or software. diff --git a/lib/nbsd_libc/md/md5hl.c b/lib/nbsd_libc/md/md5hl.c new file mode 100644 index 000000000..7604efa05 --- /dev/null +++ b/lib/nbsd_libc/md/md5hl.c @@ -0,0 +1,16 @@ +/* $NetBSD: md5hl.c,v 1.7 2005/09/26 03:01:41 christos Exp $ */ + +/* + * Written by Jason R. Thorpe , April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: md5hl.c,v 1.7 2005/09/26 03:01:41 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define MDALGORITHM MD5 +#define MDINCLUDE + +#include "mdXhl.c" diff --git a/lib/nbsd_libc/md/mdX.3 b/lib/nbsd_libc/md/mdX.3 new file mode 100644 index 000000000..39934b515 --- /dev/null +++ b/lib/nbsd_libc/md/mdX.3 @@ -0,0 +1,143 @@ +.\" $NetBSD: mdX.3,v 1.10 2003/06/13 01:28:41 lukem Exp $ +.\" +.\" ---------------------------------------------------------------------------- +.\" "THE BEER-WARE LICENSE" (Revision 42): +.\" wrote this file. As long as you retain this notice you +.\" can do whatever you want with this stuff. If we meet some day, and you think +.\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp +.\" ---------------------------------------------------------------------------- +.\" +.\" from FreeBSD Id: mdX.3,v 1.7 1996/10/22 16:28:56 phk Exp +.\" +.Dd June 13, 2003 +.Dt MDX 3 +.Os +.Sh NAME +.Nm MDXInit , +.Nm MDXUpdate , +.Nm MDXFinal , +.Nm MDXEnd , +.Nm MDXFile , +.Nm MDXData +.Nd calculate the RSA Data Security, Inc., +.Dq MDX +message digest +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In mdX.h +.Ft void +.Fn MDXInit "MDX_CTX *context" +.Ft void +.Fn MDXUpdate "MDX_CTX *context" "const unsigned char *data" "unsigned int len" +.Ft void +.Fn MDXFinal "unsigned char digest[16]" "MDX_CTX *context" +.Ft "char *" +.Fn MDXEnd "MDX_CTX *context" "char *buf" +.Ft "char *" +.Fn MDXFile "const char *filename" "char *buf" +.Ft "char *" +.Fn MDXData "const unsigned char *data" "unsigned int len" "char *buf" +.Sh DESCRIPTION +The MDX functions calculate a 128-bit cryptographic checksum (digest) +for any number of input bytes. +A cryptographic checksum is a one-way +hash-function, that is, you cannot find (except by exhaustive search) +the input corresponding to a particular output. +This net result is +a ``fingerprint'' of the input-data, which doesn't disclose the actual +input. +.Pp +MD2 is the slowest, MD4 is the fastest and MD5 is somewhere in the middle. +MD2 can only be used for Privacy-Enhanced Mail. +MD4 has been criticized for being too weak, so MD5 was developed in +response as ``MD4 with safety-belts''. +When in doubt, use MD5. +.Pp +The +.Fn MDXInit , +.Fn MDXUpdate , +and +.Fn MDXFinal +functions are the core functions. +Allocate an MDX_CTX, initialize it with +.Fn MDXInit , +run over the data with +.Fn MDXUpdate , +and finally extract the result using +.Fn MDXFinal . +.Pp +.Fn MDXEnd +is a wrapper for +.Fn MDXFinal +which converts the return value to a 33-character +(including the terminating '\e0') +.Tn ASCII +string which represents the 128 bits in hexadecimal. +.Pp +.Fn MDXFile +calculates the digest of a file, and uses +.Fn MDXEnd +to return the result. +If the file cannot be opened, a null pointer is returned. +.Fn MDXData +calculates the digest of a chunk of data in memory, and uses +.Fn MDXEnd +to return the result. +.Pp +When using +.Fn MDXEnd , +.Fn MDXFile , +or +.Fn MDXData , +the +.Ar buf +argument can be a null pointer, in which case the returned string +is allocated with +.Xr malloc 3 +and subsequently must be explicitly deallocated using +.Xr free 3 +after use. +If the +.Ar buf +argument is non-null it must point to at least 33 characters of buffer space. +.Sh SEE ALSO +.Xr md2 3 , +.Xr md4 3 , +.Xr md5 3 +.Rs +.%A B. Kaliski +.%T The MD2 Message-Digest Algorithm +.%O RFC 1319 +.Re +.Rs +.%A R. Rivest +.%T The MD4 Message-Digest Algorithm +.%O RFC 1186 +.Re +.Rs +.%A R. Rivest +.%T The MD5 Message-Digest Algorithm +.%O RFC 1321 +.Re +.Rs +.%A RSA Laboratories +.%T Frequently Asked Questions About today's Cryptography +.Re +.Sh HISTORY +These functions appeared in +.Nx 1.3 . +.Sh AUTHORS +The original MDX routines were developed by +.Tn RSA +Data Security, Inc., and published in the above references. +This code is derived directly from these implementations by Poul-Henning Kamp +.Aq Li phk@login.dkuug.dk +.Pp +Phk ristede runen. +.Sh BUGS +No method is known to exist which finds two files having the same hash value, +nor to find a file with a specific hash value. +There is on the other hand no guarantee that such a method doesn't exist. +.Sh COPYRIGHT diff --git a/lib/nbsd_libc/md/mdXhl.c b/lib/nbsd_libc/md/mdXhl.c new file mode 100644 index 000000000..69e0e25ff --- /dev/null +++ b/lib/nbsd_libc/md/mdXhl.c @@ -0,0 +1,117 @@ +/* $NetBSD: mdXhl.c,v 1.8 2009/03/06 18:15:24 apb Exp $ */ + +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + * + * from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp + */ + +/* + * Modified April 29, 1997 by Jason R. Thorpe + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#define CONCAT(x,y) __CONCAT(x,y) +#define MDNAME(x) CONCAT(MDALGORITHM,x) + +#if !defined(_KERNEL) && defined(__weak_alias) && !defined(HAVE_NBTOOL_CONFIG_H) +#define WA(a,b) __weak_alias(a,b) +WA(MDNAME(End),CONCAT(_,MDNAME(End))) +WA(MDNAME(File),CONCAT(_,MDNAME(File))) +WA(MDNAME(Data),CONCAT(_,MDNAME(Data))) +#undef WA +#endif + +#include "namespace.h" + +#include + +#include MDINCLUDE +#include +#include +#include +#include +#include +#include + + + +char * +MDNAME(End)(ctx, buf) + MDNAME(_CTX) *ctx; + char *buf; +{ + int i; + unsigned char digest[16]; + static const char hex[]="0123456789abcdef"; + + _DIAGASSERT(ctx != 0); + + if (buf == NULL) + buf = malloc(33); + if (buf == NULL) + return (NULL); + + MDNAME(Final)(digest, ctx); + + for (i = 0; i < 16; i++) { + buf[i+i] = hex[(u_int32_t)digest[i] >> 4]; + buf[i+i+1] = hex[digest[i] & 0x0f]; + } + + buf[i+i] = '\0'; + return (buf); +} + +char * +MDNAME(File)(filename, buf) + const char *filename; + char *buf; +{ + unsigned char buffer[BUFSIZ]; + MDNAME(_CTX) ctx; + int f, i, j; + + _DIAGASSERT(filename != 0); + /* buf may be NULL */ + + MDNAME(Init)(&ctx); + f = open(filename, O_RDONLY, 0666); + if (f < 0) + return NULL; + + while ((i = read(f, buffer, sizeof(buffer))) > 0) + MDNAME(Update)(&ctx, buffer, (unsigned int)i); + + j = errno; + close(f); + errno = j; + + if (i < 0) + return NULL; + + return (MDNAME(End)(&ctx, buf)); +} + +char * +MDNAME(Data)(data, len, buf) + const unsigned char *data; + unsigned int len; + char *buf; +{ + MDNAME(_CTX) ctx; + + _DIAGASSERT(data != 0); + + MDNAME(Init)(&ctx); + MDNAME(Update)(&ctx, data, len); + return (MDNAME(End)(&ctx, buf)); +} diff --git a/lib/nbsd_libc/misc/Makefile.inc b/lib/nbsd_libc/misc/Makefile.inc new file mode 100644 index 000000000..d5cb9ef87 --- /dev/null +++ b/lib/nbsd_libc/misc/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.1 2007/11/13 15:21:20 ad Exp $ +# @(#)Makefile.inc 8.3 (Berkeley) 10/24/94 + +.PATH: ${.CURDIR}/misc + +# constructor +SRCS+= initfini.c + +# for -fstack-protector +SRCS+= stack_protector.c diff --git a/lib/nbsd_libc/misc/initfini.c b/lib/nbsd_libc/misc/initfini.c new file mode 100644 index 000000000..39d7f11f4 --- /dev/null +++ b/lib/nbsd_libc/misc/initfini.c @@ -0,0 +1,66 @@ +/* $NetBSD: initfini.c,v 1.7 2010/11/14 18:11:42 tron Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Doran. + * + * 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 +__RCSID("$NetBSD: initfini.c,v 1.7 2010/11/14 18:11:42 tron Exp $"); + +#ifdef _LIBC +#include "namespace.h" +#endif + +void __libc_init(void) __attribute__((__constructor__, __used__)); + +void __guard_setup(void); +void __libc_thr_init(void); +void __libc_atomic_init(void); +void __libc_atexit_init(void); +void __libc_env_init(void); + +/* LINTED used */ +void +__libc_init(void) +{ + + /* For -fstack-protector */ + __guard_setup(); + + /* Atomic operations */ + __libc_atomic_init(); + + /* Threads */ + __libc_thr_init(); + + /* Initialize the atexit mutexes */ + __libc_atexit_init(); + + /* Initialize environment memory RB tree. */ + __libc_env_init(); +} diff --git a/lib/nbsd_libc/misc/stack_protector.c b/lib/nbsd_libc/misc/stack_protector.c new file mode 100644 index 000000000..313982c83 --- /dev/null +++ b/lib/nbsd_libc/misc/stack_protector.c @@ -0,0 +1,126 @@ +/* $NetBSD: stack_protector.c,v 1.5 2010/12/07 20:10:53 joerg Exp $ */ +/* $OpenBSD: stack_protector.c,v 1.10 2006/03/31 05:34:44 deraadt Exp $ */ + +/* + * Copyright (c) 2002 Hiroaki Etoh, Federico G. Schwindt, and Miodrag Vallat. + * 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 BY THE AUTHORS ``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 AUTHORS 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 +__RCSID("$NetBSD: stack_protector.c,v 1.5 2010/12/07 20:10:53 joerg Exp $"); + +#ifdef _LIBC +#include "namespace.h" +#endif +#include +#include +#include +#include +#include +#include +#ifdef _LIBC +#include +#include "extern.h" +#else +#define __sysctl sysctl +void xprintf(const char *fmt, ...); +#include +#endif + +long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0}; +static void __fail(const char *) __attribute__((__noreturn__)); +void __stack_chk_fail_local(void); +void __guard_setup(void); + +void +__guard_setup(void) +{ + int mib[2]; + size_t len; + + if (__stack_chk_guard[0] != 0) + return; + + mib[0] = CTL_KERN; + mib[1] = KERN_ARND; + + len = sizeof(__stack_chk_guard); + if (__sysctl(mib, 2, __stack_chk_guard, &len, NULL, 0) == -1 || + len != sizeof(__stack_chk_guard)) { + /* If sysctl was unsuccessful, use the "terminator canary". */ + ((unsigned char *)(void *)__stack_chk_guard)[0] = 0; + ((unsigned char *)(void *)__stack_chk_guard)[1] = 0; + ((unsigned char *)(void *)__stack_chk_guard)[2] = '\n'; + ((unsigned char *)(void *)__stack_chk_guard)[3] = 255; + } +} + +/*ARGSUSED*/ +static void +__fail(const char *msg) +{ +#ifdef _LIBC + struct syslog_data sdata = SYSLOG_DATA_INIT; +#endif + struct sigaction sa; + sigset_t mask; + + /* Immediately block all signal handlers from running code */ + (void)sigfillset(&mask); + (void)sigdelset(&mask, SIGABRT); + (void)sigprocmask(SIG_BLOCK, &mask, NULL); + +#ifdef _LIBC + /* This may fail on a chroot jail... */ + syslog_ss(LOG_CRIT, &sdata, "%s", msg); +#else + xprintf("%s: %s\n", getprogname(), msg); +#endif + + (void)memset(&sa, 0, sizeof(sa)); + (void)sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_DFL; + (void)sigaction(SIGABRT, &sa, NULL); + (void)raise(SIGABRT); + _exit(127); +} + +void +__stack_chk_fail(void) +{ + __fail("stack overflow detected; terminated"); +} + +void +__chk_fail(void) +{ + __fail("buffer overflow detected; terminated"); +} + +void +__stack_chk_fail_local(void) +{ + __stack_chk_fail(); +} diff --git a/lib/nbsd_libc/nameser/Makefile.inc b/lib/nbsd_libc/nameser/Makefile.inc new file mode 100644 index 000000000..317a1c3a5 --- /dev/null +++ b/lib/nbsd_libc/nameser/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD: Makefile.inc,v 1.1 2004/05/20 20:02:26 christos Exp $ + +# net sources +.PATH: ${.CURDIR}/nameser + +SRCS+= ns_name.c ns_netint.c ns_parse.c ns_print.c \ + ns_samedomain.c ns_ttl.c diff --git a/lib/nbsd_libc/nameser/ns_name.c b/lib/nbsd_libc/nameser/ns_name.c new file mode 100644 index 000000000..ec2045e58 --- /dev/null +++ b/lib/nbsd_libc/nameser/ns_name.c @@ -0,0 +1,1161 @@ +/* $NetBSD: ns_name.c,v 1.8 2009/04/12 19:43:37 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#ifndef lint +#ifdef notdef +static const char rcsid[] = "Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp"; +#else +__RCSID("$NetBSD: ns_name.c,v 1.8 2009/04/12 19:43:37 christos Exp $"); +#endif +#endif + +#include "port_before.h" + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef SPRINTF_CHAR +# define SPRINTF(x) strlen(sprintf/**/x) +#else +# define SPRINTF(x) ((size_t)sprintf x) +#endif + +#define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */ +#define DNS_LABELTYPE_BITSTRING 0x41 + +/* Data. */ + +static const char digits[] = "0123456789"; + +static const char digitvalue[256] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/ + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/ + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/ +}; + +/* Forward. */ + +static int special(int); +static int printable(int); +static int dn_find(const u_char *, const u_char *, + const u_char * const *, + const u_char * const *); +static int encode_bitsring(const char **, const char *, + unsigned char **, unsigned char **, + unsigned const char *); +static int labellen(const u_char *); +static int decode_bitstring(const unsigned char **, + char *, const char *); + +/* Public. */ + +/*% + * Convert an encoded domain name to printable ascii as per RFC1035. + + * return: + *\li Number of bytes written to buffer, or -1 (with errno set) + * + * notes: + *\li The root is returned as "." + *\li All other domains are returned in non absolute form + */ +int +ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) +{ + const u_char *cp; + char *dn, *eom; + u_char c; + u_int n; + int l; + + cp = src; + dn = dst; + eom = dst + dstsiz; + + while ((n = *cp++) != 0) { + if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { + /* Some kind of compression pointer. */ + errno = EMSGSIZE; + return (-1); + } + if (dn != dst) { + if (dn >= eom) { + errno = EMSGSIZE; + return (-1); + } + *dn++ = '.'; + } + if ((l = labellen(cp - 1)) < 0) { + errno = EMSGSIZE; /*%< XXX */ + return (-1); + } + if (dn + l >= eom) { + errno = EMSGSIZE; + return (-1); + } + if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) { + int m; + + if (n != DNS_LABELTYPE_BITSTRING) { + /* XXX: labellen should reject this case */ + errno = EINVAL; + return (-1); + } + if ((m = decode_bitstring(&cp, dn, eom)) < 0) + { + errno = EMSGSIZE; + return (-1); + } + dn += m; + continue; + } + for (; l > 0; l--) { + c = *cp++; + if (special(c)) { + if (dn + 1 >= eom) { + errno = EMSGSIZE; + return (-1); + } + *dn++ = '\\'; + *dn++ = (char)c; + } else if (!printable(c)) { + if (dn + 3 >= eom) { + errno = EMSGSIZE; + return (-1); + } + *dn++ = '\\'; + *dn++ = digits[c / 100]; + *dn++ = digits[(c % 100) / 10]; + *dn++ = digits[c % 10]; + } else { + if (dn >= eom) { + errno = EMSGSIZE; + return (-1); + } + *dn++ = (char)c; + } + } + } + if (dn == dst) { + if (dn >= eom) { + errno = EMSGSIZE; + return (-1); + } + *dn++ = '.'; + } + if (dn >= eom) { + errno = EMSGSIZE; + return (-1); + } + *dn++ = '\0'; + return (dn - dst); +} + +/*% + * Convert a ascii string into an encoded domain name as per RFC1035. + * + * return: + * + *\li -1 if it fails + *\li 1 if string was fully qualified + *\li 0 is string was not fully qualified + * + * notes: + *\li Enforces label and domain length limits. + */ +int +ns_name_pton(const char *src, u_char *dst, size_t dstsiz) { + return (ns_name_pton2(src, dst, dstsiz, NULL)); +} + +/* + * ns_name_pton2(src, dst, dstsiz, *dstlen) + * Convert a ascii string into an encoded domain name as per RFC1035. + * return: + * -1 if it fails + * 1 if string was fully qualified + * 0 is string was not fully qualified + * side effects: + * fills in *dstlen (if non-NULL) + * notes: + * Enforces label and domain length limits. + */ +int +ns_name_pton2(const char *src, u_char *dst, size_t dstsiz, size_t *dstlen) { + u_char *label, *bp, *eom; + int c, n, escaped, e = 0; + char *cp; + + escaped = 0; + bp = dst; + eom = dst + dstsiz; + label = bp++; + + while ((c = *src++) != 0) { + if (escaped) { + if (c == '[') { /*%< start a bit string label */ + if ((cp = strchr(src, ']')) == NULL) { + errno = EINVAL; /*%< ??? */ + return (-1); + } + if ((e = encode_bitsring(&src, cp + 2, + &label, &bp, eom)) + != 0) { + errno = e; + return (-1); + } + escaped = 0; + label = bp++; + if ((c = *src++) == 0) + goto done; + else if (c != '.') { + errno = EINVAL; + return (-1); + } + continue; + } + else if ((cp = strchr(digits, c)) != NULL) { + n = (cp - digits) * 100; + if ((c = *src++) == 0 || + (cp = strchr(digits, c)) == NULL) { + errno = EMSGSIZE; + return (-1); + } + n += (cp - digits) * 10; + if ((c = *src++) == 0 || + (cp = strchr(digits, c)) == NULL) { + errno = EMSGSIZE; + return (-1); + } + n += (cp - digits); + if (n > 255) { + errno = EMSGSIZE; + return (-1); + } + c = n; + } + escaped = 0; + } else if (c == '\\') { + escaped = 1; + continue; + } else if (c == '.') { + c = (bp - label - 1); + if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */ + errno = EMSGSIZE; + return (-1); + } + if (label >= eom) { + errno = EMSGSIZE; + return (-1); + } + *label = c; + /* Fully qualified ? */ + if (*src == '\0') { + if (c != 0) { + if (bp >= eom) { + errno = EMSGSIZE; + return (-1); + } + *bp++ = '\0'; + } + if ((bp - dst) > MAXCDNAME) { + errno = EMSGSIZE; + return (-1); + } + if (dstlen != NULL) + *dstlen = (bp - dst); + return (1); + } + if (c == 0 || *src == '.') { + errno = EMSGSIZE; + return (-1); + } + label = bp++; + continue; + } + if (bp >= eom) { + errno = EMSGSIZE; + return (-1); + } + *bp++ = (u_char)c; + } + c = (bp - label - 1); + if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */ + errno = EMSGSIZE; + return (-1); + } + done: + if (label >= eom) { + errno = EMSGSIZE; + return (-1); + } + *label = c; + if (c != 0) { + if (bp >= eom) { + errno = EMSGSIZE; + return (-1); + } + *bp++ = 0; + } + if ((bp - dst) > MAXCDNAME) { /*%< src too big */ + errno = EMSGSIZE; + return (-1); + } + if (dstlen != NULL) + *dstlen = (bp - dst); + return (0); +} + +/*% + * Convert a network strings labels into all lowercase. + * + * return: + *\li Number of bytes written to buffer, or -1 (with errno set) + * + * notes: + *\li Enforces label and domain length limits. + */ + +int +ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) +{ + const u_char *cp; + u_char *dn, *eom; + u_char c; + u_int n; + int l; + + cp = src; + dn = dst; + eom = dst + dstsiz; + + if (dn >= eom) { + errno = EMSGSIZE; + return (-1); + } + while ((n = *cp++) != 0) { + if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { + /* Some kind of compression pointer. */ + errno = EMSGSIZE; + return (-1); + } + *dn++ = n; + if ((l = labellen(cp - 1)) < 0) { + errno = EMSGSIZE; + return (-1); + } + if (dn + l >= eom) { + errno = EMSGSIZE; + return (-1); + } + for (; l > 0; l--) { + c = *cp++; + if (isascii(c) && isupper(c)) + *dn++ = tolower(c); + else + *dn++ = c; + } + } + *dn++ = '\0'; + return (dn - dst); +} + +/*% + * Unpack a domain name from a message, source may be compressed. + * + * return: + *\li -1 if it fails, or consumed octets if it succeeds. + */ +int +ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, + u_char *dst, size_t dstsiz) +{ + return (ns_name_unpack2(msg, eom, src, dst, dstsiz, NULL)); +} + +/* + * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen) + * Unpack a domain name from a message, source may be compressed. + * return: + * -1 if it fails, or consumed octets if it succeeds. + * side effect: + * fills in *dstlen (if non-NULL). + */ +int +ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src, + u_char *dst, size_t dstsiz, size_t *dstlen) +{ + const u_char *srcp, *dstlim; + u_char *dstp; + int n, len, checked, l; + + len = -1; + checked = 0; + dstp = dst; + srcp = src; + dstlim = dst + dstsiz; + if (srcp < msg || srcp >= eom) { + errno = EMSGSIZE; + return (-1); + } + /* Fetch next label in domain name. */ + while ((n = *srcp++) != 0) { + /* Check for indirection. */ + switch (n & NS_CMPRSFLGS) { + case 0: + case NS_TYPE_ELT: + /* Limit checks. */ + if ((l = labellen(srcp - 1)) < 0) { + errno = EMSGSIZE; + return (-1); + } + if (dstp + l + 1 >= dstlim || srcp + l >= eom) { + errno = EMSGSIZE; + return (-1); + } + checked += l + 1; + *dstp++ = n; + memcpy(dstp, srcp, (size_t)l); + dstp += l; + srcp += l; + break; + + case NS_CMPRSFLGS: + if (srcp >= eom) { + errno = EMSGSIZE; + return (-1); + } + if (len < 0) + len = srcp - src + 1; + srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); + if (srcp < msg || srcp >= eom) { /*%< Out of range. */ + errno = EMSGSIZE; + return (-1); + } + checked += 2; + /* + * Check for loops in the compressed name; + * if we've looked at the whole message, + * there must be a loop. + */ + if (checked >= eom - msg) { + errno = EMSGSIZE; + return (-1); + } + break; + + default: + errno = EMSGSIZE; + return (-1); /*%< flag error */ + } + } + *dstp++ = 0; + if (dstlen != NULL) + *dstlen = dstp - dst; + if (len < 0) + len = srcp - src; + return (len); +} + +/*% + * Pack domain name 'domain' into 'comp_dn'. + * + * return: + *\li Size of the compressed name, or -1. + * + * notes: + *\li 'dnptrs' is an array of pointers to previous compressed names. + *\li dnptrs[0] is a pointer to the beginning of the message. The array + * ends with NULL. + *\li 'lastdnptr' is a pointer to the end of the array pointed to + * by 'dnptrs'. + * + * Side effects: + *\li The list of pointers in dnptrs is updated for labels inserted into + * the message as we compress the name. If 'dnptr' is NULL, we don't + * try to compress names. If 'lastdnptr' is NULL, we don't update the + * list. + */ +int +ns_name_pack(const u_char *src, u_char *dst, int dstsiz, + const u_char **dnptrs, const u_char **lastdnptr) +{ + u_char *dstp; + const u_char **cpp, **lpp, *eob, *msg; + const u_char *srcp; + int n, l, first = 1; + + srcp = src; + dstp = dst; + eob = dstp + dstsiz; + lpp = cpp = NULL; + if (dnptrs != NULL) { + if ((msg = *dnptrs++) != NULL) { + for (cpp = dnptrs; *cpp != NULL; cpp++) + continue; + lpp = cpp; /*%< end of list to search */ + } + } else + msg = NULL; + + /* make sure the domain we are about to add is legal */ + l = 0; + do { + int l0; + + n = *srcp; + if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { + errno = EMSGSIZE; + return (-1); + } + if ((l0 = labellen(srcp)) < 0) { + errno = EINVAL; + return (-1); + } + l += l0 + 1; + if (l > MAXCDNAME) { + errno = EMSGSIZE; + return (-1); + } + srcp += l0 + 1; + } while (n != 0); + + /* from here on we need to reset compression pointer array on error */ + srcp = src; + do { + /* Look to see if we can use pointers. */ + n = *srcp; + if (n != 0 && msg != NULL) { + l = dn_find(srcp, msg, (const u_char * const *)dnptrs, + (const u_char * const *)lpp); + if (l >= 0) { + if (dstp + 1 >= eob) { + goto cleanup; + } + *dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS; + *dstp++ = l % 256; + return (dstp - dst); + } + /* Not found, save it. */ + if (lastdnptr != NULL && cpp < lastdnptr - 1 && + (dstp - msg) < 0x4000 && first) { + *cpp++ = dstp; + *cpp = NULL; + first = 0; + } + } + /* copy label to buffer */ + if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { + /* Should not happen. */ + goto cleanup; + } + n = labellen(srcp); + if (dstp + 1 + n >= eob) { + goto cleanup; + } + memcpy(dstp, srcp, (size_t)(n + 1)); + srcp += n + 1; + dstp += n + 1; + } while (n != 0); + + if (dstp > eob) { +cleanup: + if (msg != NULL) + *lpp = NULL; + errno = EMSGSIZE; + return (-1); + } + return (dstp - dst); +} + +/*% + * Expand compressed domain name to presentation format. + * + * return: + *\li Number of bytes read out of `src', or -1 (with errno set). + * + * note: + *\li Root domain returns as "." not "". + */ +int +ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src, + char *dst, size_t dstsiz) +{ + u_char tmp[NS_MAXCDNAME]; + int n; + + if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1) + return (-1); + if (ns_name_ntop(tmp, dst, dstsiz) == -1) + return (-1); + return (n); +} + +/*% + * Compress a domain name into wire format, using compression pointers. + * + * return: + *\li Number of bytes consumed in `dst' or -1 (with errno set). + * + * notes: + *\li 'dnptrs' is an array of pointers to previous compressed names. + *\li dnptrs[0] is a pointer to the beginning of the message. + *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the + * array pointed to by 'dnptrs'. Side effect is to update the list of + * pointers for labels inserted into the message as we compress the name. + *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' + * is NULL, we don't update the list. + */ +int +ns_name_compress(const char *src, u_char *dst, size_t dstsiz, + const u_char **dnptrs, const u_char **lastdnptr) +{ + u_char tmp[NS_MAXCDNAME]; + + if (ns_name_pton(src, tmp, sizeof tmp) == -1) + return (-1); + return (ns_name_pack(tmp, dst, (int)dstsiz, dnptrs, lastdnptr)); +} + +/*% + * Reset dnptrs so that there are no active references to pointers at or + * after src. + */ +void +ns_name_rollback(const u_char *src, const u_char **dnptrs, + const u_char **lastdnptr) +{ + while (dnptrs < lastdnptr && *dnptrs != NULL) { + if (*dnptrs >= src) { + *dnptrs = NULL; + break; + } + dnptrs++; + } +} + +/*% + * Advance *ptrptr to skip over the compressed name it points at. + * + * return: + *\li 0 on success, -1 (with errno set) on failure. + */ +int +ns_name_skip(const u_char **ptrptr, const u_char *eom) +{ + const u_char *cp; + u_int n; + int l; + + cp = *ptrptr; + while (cp < eom && (n = *cp++) != 0) { + /* Check for indirection. */ + switch (n & NS_CMPRSFLGS) { + case 0: /*%< normal case, n == len */ + cp += n; + continue; + case NS_TYPE_ELT: /*%< EDNS0 extended label */ + if ((l = labellen(cp - 1)) < 0) { + errno = EMSGSIZE; /*%< XXX */ + return (-1); + } + cp += l; + continue; + case NS_CMPRSFLGS: /*%< indirection */ + cp++; + break; + default: /*%< illegal type */ + errno = EMSGSIZE; + return (-1); + } + break; + } + if (cp > eom) { + errno = EMSGSIZE; + return (-1); + } + *ptrptr = cp; + return (0); +} + +/* Find the number of octets an nname takes up, including the root label. + * (This is basically ns_name_skip() without compression-pointer support.) + * ((NOTE: can only return zero if passed-in namesiz argument is zero.)) + */ +ssize_t +ns_name_length(ns_nname_ct nname, size_t namesiz) { + ns_nname_ct orig = nname; + u_int n; + + while (namesiz-- > 0 && (n = *nname++) != 0) { + if ((n & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + if (n > namesiz) { + errno = EMSGSIZE; + return (-1); + } + nname += n; + namesiz -= n; + } + return (nname - orig); +} + +/* Compare two nname's for equality. Return -1 on error (setting errno). + */ +int +ns_name_eq(ns_nname_ct a, size_t as, ns_nname_ct b, size_t bs) { + ns_nname_ct ae = a + as, be = b + bs; + int ac, bc; + + while (ac = *a, bc = *b, ac != 0 && bc != 0) { + if ((ac & NS_CMPRSFLGS) != 0 || (bc & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + if (a + ac >= ae || b + bc >= be) { + errno = EMSGSIZE; + return (-1); + } + if (ac != bc || strncasecmp((const char *) ++a, + (const char *) ++b, + (size_t)ac) != 0) + return (0); + a += ac, b += bc; + } + return (ac == 0 && bc == 0); +} + +/* Is domain "A" owned by (at or below) domain "B"? + */ +int +ns_name_owned(ns_namemap_ct a, int an, ns_namemap_ct b, int bn) { + /* If A is shorter, it cannot be owned by B. */ + if (an < bn) + return (0); + + /* If they are unequal before the length of the shorter, A cannot... */ + while (bn > 0) { + if (a->len != b->len || + strncasecmp((const char *) a->base, + (const char *) b->base, (size_t)a->len) != 0) + return (0); + a++, an--; + b++, bn--; + } + + /* A might be longer or not, but either way, B owns it. */ + return (1); +} + +/* Build an array of tuples from an nname, top-down order. + * Return the number of tuples (labels) thus discovered. + */ +int +ns_name_map(ns_nname_ct nname, size_t namelen, ns_namemap_t map, int mapsize) { + u_int n; + int l; + + n = *nname++; + namelen--; + + /* Root zone? */ + if (n == 0) { + /* Extra data follows name? */ + if (namelen > 0) { + errno = EMSGSIZE; + return (-1); + } + return (0); + } + + /* Compression pointer? */ + if ((n & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + + /* Label too long? */ + if (n > namelen) { + errno = EMSGSIZE; + return (-1); + } + + /* Recurse to get rest of name done first. */ + l = ns_name_map(nname + n, namelen - n, map, mapsize); + if (l < 0) + return (-1); + + /* Too many labels? */ + if (l >= mapsize) { + errno = ENAMETOOLONG; + return (-1); + } + + /* We're on our way back up-stack, store current map data. */ + map[l].base = nname; + map[l].len = n; + return (l + 1); +} + +/* Count the labels in a domain name. Root counts, so COM. has two. This + * is to make the result comparable to the result of ns_name_map(). + */ +int +ns_name_labels(ns_nname_ct nname, size_t namesiz) { + int ret = 0; + u_int n; + + while (namesiz-- > 0 && (n = *nname++) != 0) { + if ((n & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + if (n > namesiz) { + errno = EMSGSIZE; + return (-1); + } + nname += n; + namesiz -= n; + ret++; + } + return (ret + 1); +} + +/* Private. */ + +/*% + * Thinking in noninternationalized USASCII (per the DNS spec), + * is this characted special ("in need of quoting") ? + * + * return: + *\li boolean. + */ +static int +special(int ch) { + switch (ch) { + case 0x22: /*%< '"' */ + case 0x2E: /*%< '.' */ + case 0x3B: /*%< ';' */ + case 0x5C: /*%< '\\' */ + case 0x28: /*%< '(' */ + case 0x29: /*%< ')' */ + /* Special modifiers in zone files. */ + case 0x40: /*%< '@' */ + case 0x24: /*%< '$' */ + return (1); + default: + return (0); + } +} + +/*% + * Thinking in noninternationalized USASCII (per the DNS spec), + * is this character visible and not a space when printed ? + * + * return: + *\li boolean. + */ +static int +printable(int ch) { + return (ch > 0x20 && ch < 0x7f); +} + +/*% + * Thinking in noninternationalized USASCII (per the DNS spec), + * convert this character to lower case if it's upper case. + */ +static int +mklower(int ch) { + if (ch >= 0x41 && ch <= 0x5A) + return (ch + 0x20); + return (ch); +} + +/*% + * Search for the counted-label name in an array of compressed names. + * + * return: + *\li offset from msg if found, or -1. + * + * notes: + *\li dnptrs is the pointer to the first name on the list, + *\li not the pointer to the start of the message. + */ +static int +dn_find(const u_char *domain, const u_char *msg, + const u_char * const *dnptrs, + const u_char * const *lastdnptr) +{ + const u_char *dn, *cp, *sp; + const u_char * const *cpp; + u_int n; + + for (cpp = dnptrs; cpp < lastdnptr; cpp++) { + sp = *cpp; + /* + * terminate search on: + * root label + * compression pointer + * unusable offset + */ + while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 && + (sp - msg) < 0x4000) { + dn = domain; + cp = sp; + while ((n = *cp++) != 0) { + /* + * check for indirection + */ + switch (n & NS_CMPRSFLGS) { + case 0: /*%< normal case, n == len */ + n = labellen(cp - 1); /*%< XXX */ + if (n != *dn++) + goto next; + + for (; n > 0; n--) + if (mklower(*dn++) != + mklower(*cp++)) + goto next; + /* Is next root for both ? */ + if (*dn == '\0' && *cp == '\0') + return (sp - msg); + if (*dn) + continue; + goto next; + case NS_CMPRSFLGS: /*%< indirection */ + cp = msg + (((n & 0x3f) << 8) | *cp); + break; + + default: /*%< illegal type */ + errno = EMSGSIZE; + return (-1); + } + } + next: ; + sp += *sp + 1; + } + } + errno = ENOENT; + return (-1); +} + +static int +decode_bitstring(const unsigned char **cpp, char *dn, const char *eom) +{ + const unsigned char *cp = *cpp; + char *beg = dn, tc; + int b, blen, plen, i; + + if ((blen = (*cp & 0xff)) == 0) + blen = 256; + plen = (blen + 3) / 4; + plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1); + if (dn + plen >= eom) + return (-1); + + cp++; + i = SPRINTF((dn, "\\[x")); + if (i < 0) + return (-1); + dn += i; + for (b = blen; b > 7; b -= 8, cp++) { + i = SPRINTF((dn, "%02x", *cp & 0xff)); + if (i < 0) + return (-1); + dn += i; + } + if (b > 4) { + tc = *cp++; + i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b)))); + if (i < 0) + return (-1); + dn += i; + } else if (b > 0) { + tc = *cp++; + i = SPRINTF((dn, "%1x", + (((u_int32_t)tc >> 4) & 0x0f) & (0x0f << (4 - b)))); + if (i < 0) + return (-1); + dn += i; + } + i = SPRINTF((dn, "/%d]", blen)); + if (i < 0) + return (-1); + dn += i; + + *cpp = cp; + return (dn - beg); +} + +static int +encode_bitsring(const char **bp, const char *end, unsigned char **labelp, + unsigned char ** dst, unsigned const char *eom) +{ + int afterslash = 0; + const char *cp = *bp; + unsigned char *tp; + char c; + const char *beg_blen; + char *end_blen = NULL; + int value = 0, count = 0, tbcount = 0, blen = 0; + + beg_blen = end_blen = NULL; + + /* a bitstring must contain at least 2 characters */ + if (end - cp < 2) + return (EINVAL); + + /* XXX: currently, only hex strings are supported */ + if (*cp++ != 'x') + return (EINVAL); + if (!isxdigit((*cp) & 0xff)) /*%< reject '\[x/BLEN]' */ + return (EINVAL); + + for (tp = *dst + 1; cp < end && tp < eom; cp++) { + switch((c = *cp)) { + case ']': /*%< end of the bitstring */ + if (afterslash) { + if (beg_blen == NULL) + return (EINVAL); + blen = (int)strtol(beg_blen, &end_blen, 10); + if (*end_blen != ']') + return (EINVAL); + } + if (count) + *tp++ = ((value << 4) & 0xff); + cp++; /*%< skip ']' */ + goto done; + case '/': + afterslash = 1; + break; + default: + if (afterslash) { + if (!isdigit(c&0xff)) + return (EINVAL); + if (beg_blen == NULL) { + + if (c == '0') { + /* blen never begings with 0 */ + return (EINVAL); + } + beg_blen = cp; + } + } else { + if (!isxdigit(c&0xff)) + return (EINVAL); + value <<= 4; + value += digitvalue[(int)c]; + count += 4; + tbcount += 4; + if (tbcount > 256) + return (EINVAL); + if (count == 8) { + *tp++ = value; + count = 0; + } + } + break; + } + } + done: + if (cp >= end || tp >= eom) + return (EMSGSIZE); + + /* + * bit length validation: + * If a is present, the number of digits in the + * MUST be just sufficient to contain the number of bits specified + * by the . If there are insignificant bits in a final + * hexadecimal or octal digit, they MUST be zero. + * RFC2673, Section 3.2. + */ + if (blen > 0) { + int traillen; + + if (((blen + 3) & ~3) != tbcount) + return (EINVAL); + traillen = tbcount - blen; /*%< between 0 and 3 */ + if (((value << (8 - traillen)) & 0xff) != 0) + return (EINVAL); + } + else + blen = tbcount; + if (blen == 256) + blen = 0; + + /* encode the type and the significant bit fields */ + **labelp = DNS_LABELTYPE_BITSTRING; + **dst = blen; + + *bp = cp; + *dst = tp; + + return (0); +} + +static int +labellen(const u_char *lp) +{ + int bitlen; + u_char l = *lp; + + if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) { + /* should be avoided by the caller */ + return (-1); + } + + if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) { + if (l == DNS_LABELTYPE_BITSTRING) { + if ((bitlen = *(lp + 1)) == 0) + bitlen = 256; + return ((bitlen + 7 ) / 8 + 1); + } + return (-1); /*%< unknwon ELT */ + } + return (l); +} + +/*! \file */ diff --git a/lib/nbsd_libc/nameser/ns_netint.c b/lib/nbsd_libc/nameser/ns_netint.c new file mode 100644 index 000000000..7e4e77d1e --- /dev/null +++ b/lib/nbsd_libc/nameser/ns_netint.c @@ -0,0 +1,65 @@ +/* $NetBSD: ns_netint.c,v 1.6 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#ifndef lint +#ifdef notdef +static const char rcsid[] = "Id: ns_netint.c,v 1.3 2005/04/27 04:56:40 sra Exp"; +#else +__RCSID("$NetBSD: ns_netint.c,v 1.6 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif + +/* Import. */ + +#include "port_before.h" + +#include + +#include "port_after.h" + +/* Public. */ + +u_int16_t +ns_get16(const u_char *src) { + u_int dst; + + NS_GET16(dst, src); + return (dst); +} + +u_int32_t +ns_get32(const u_char *src) { + u_long dst; + + NS_GET32(dst, src); + return (dst); +} + +void +ns_put16(u_int16_t src, u_char *dst) { + NS_PUT16(src, dst); +} + +void +ns_put32(u_int32_t src, u_char *dst) { + NS_PUT32(src, dst); +} + +/*! \file */ diff --git a/lib/nbsd_libc/nameser/ns_parse.c b/lib/nbsd_libc/nameser/ns_parse.c new file mode 100644 index 000000000..007606964 --- /dev/null +++ b/lib/nbsd_libc/nameser/ns_parse.c @@ -0,0 +1,283 @@ +/* $NetBSD: ns_parse.c,v 1.8 2009/04/12 19:43:37 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#ifndef lint +#ifdef notdef +static const char rcsid[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp"; +#else +__RCSID("$NetBSD: ns_parse.c,v 1.8 2009/04/12 19:43:37 christos Exp $"); +#endif +#endif + +/* Import. */ + +#include "port_before.h" + +#include + +#include +#include + +#include +#include +#include + +#include "port_after.h" + +/* Forward. */ + +static void setsection(ns_msg *msg, ns_sect sect); + +/* Macros. */ + +#if !defined(SOLARIS2) || defined(__COVERITY__) +#define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0) +#else +#define RETERR(err) \ + do { errno = (err); if (errno == errno) return (-1); } while (0) +#endif + +#define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */ +#define PARSE_FMT_WIRE 1 /* Parse using network-format names */ + +/* Public. */ + +/* These need to be in the same order as the nres.h:ns_flag enum. */ +struct _ns_flagdata _ns_flagdata[16] = { + { 0x8000, 15 }, /*%< qr. */ + { 0x7800, 11 }, /*%< opcode. */ + { 0x0400, 10 }, /*%< aa. */ + { 0x0200, 9 }, /*%< tc. */ + { 0x0100, 8 }, /*%< rd. */ + { 0x0080, 7 }, /*%< ra. */ + { 0x0040, 6 }, /*%< z. */ + { 0x0020, 5 }, /*%< ad. */ + { 0x0010, 4 }, /*%< cd. */ + { 0x000f, 0 }, /*%< rcode. */ + { 0x0000, 0 }, /*%< expansion (1/6). */ + { 0x0000, 0 }, /*%< expansion (2/6). */ + { 0x0000, 0 }, /*%< expansion (3/6). */ + { 0x0000, 0 }, /*%< expansion (4/6). */ + { 0x0000, 0 }, /*%< expansion (5/6). */ + { 0x0000, 0 }, /*%< expansion (6/6). */ +}; + +int ns_msg_getflag(ns_msg handle, int flag) { + return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); +} + +int +ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { + const u_char *optr = ptr; + + for (; count > 0; count--) { + int b, rdlength; + + b = dn_skipname(ptr, eom); + if (b < 0) + RETERR(EMSGSIZE); + ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; + if (section != ns_s_qd) { + if (ptr + NS_INT32SZ + NS_INT16SZ > eom) + RETERR(EMSGSIZE); + ptr += NS_INT32SZ/*TTL*/; + NS_GET16(rdlength, ptr); + ptr += rdlength/*RData*/; + } + } + if (ptr > eom) + RETERR(EMSGSIZE); + return (ptr - optr); +} + +int +ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { + const u_char *eom = msg + msglen; + int i; + + handle->_msg = msg; + handle->_eom = eom; + if (msg + NS_INT16SZ > eom) + RETERR(EMSGSIZE); + NS_GET16(handle->_id, msg); + if (msg + NS_INT16SZ > eom) + RETERR(EMSGSIZE); + NS_GET16(handle->_flags, msg); + for (i = 0; i < ns_s_max; i++) { + if (msg + NS_INT16SZ > eom) + RETERR(EMSGSIZE); + NS_GET16(handle->_counts[i], msg); + } + for (i = 0; i < ns_s_max; i++) + if (handle->_counts[i] == 0) + handle->_sections[i] = NULL; + else { + int b = ns_skiprr(msg, eom, (ns_sect)i, + handle->_counts[i]); + + if (b < 0) + return (-1); + handle->_sections[i] = msg; + msg += b; + } + if (msg != eom) + RETERR(EMSGSIZE); + setsection(handle, ns_s_max); + return (0); +} + +int +ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { + int b; + int tmp; + + /* Make section right. */ + tmp = section; + if (tmp < 0 || section >= ns_s_max) + RETERR(ENODEV); + if (section != handle->_sect) + setsection(handle, section); + + /* Make rrnum right. */ + if (rrnum == -1) + rrnum = handle->_rrnum; + if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) + RETERR(ENODEV); + if (rrnum < handle->_rrnum) + setsection(handle, section); + if (rrnum > handle->_rrnum) { + b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, + rrnum - handle->_rrnum); + + if (b < 0) + return (-1); + handle->_msg_ptr += b; + handle->_rrnum = rrnum; + } + + /* Do the parse. */ + b = dn_expand(handle->_msg, handle->_eom, + handle->_msg_ptr, rr->name, NS_MAXDNAME); + if (b < 0) + return (-1); + handle->_msg_ptr += b; + if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) + RETERR(EMSGSIZE); + NS_GET16(rr->type, handle->_msg_ptr); + NS_GET16(rr->rr_class, handle->_msg_ptr); + if (section == ns_s_qd) { + rr->ttl = 0; + rr->rdlength = 0; + rr->rdata = NULL; + } else { + if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) + RETERR(EMSGSIZE); + NS_GET32(rr->ttl, handle->_msg_ptr); + NS_GET16(rr->rdlength, handle->_msg_ptr); + if (handle->_msg_ptr + rr->rdlength > handle->_eom) + RETERR(EMSGSIZE); + rr->rdata = handle->_msg_ptr; + handle->_msg_ptr += rr->rdlength; + } + if (++handle->_rrnum > handle->_counts[(int)section]) + setsection(handle, (ns_sect)((int)section + 1)); + + /* All done. */ + return (0); +} + +/* + * This is identical to the above but uses network-format (uncompressed) names. + */ +int +ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) { + int b; + int tmp; + + /* Make section right. */ + tmp = section; + if (tmp < 0 || section >= ns_s_max) + RETERR(ENODEV); + if (section != handle->_sect) + setsection(handle, section); + + /* Make rrnum right. */ + if (rrnum == -1) + rrnum = handle->_rrnum; + if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) + RETERR(ENODEV); + if (rrnum < handle->_rrnum) + setsection(handle, section); + if (rrnum > handle->_rrnum) { + b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, + rrnum - handle->_rrnum); + + if (b < 0) + return (-1); + handle->_msg_ptr += b; + handle->_rrnum = rrnum; + } + + /* Do the parse. */ + b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr, + rr->nname, NS_MAXNNAME, &rr->nnamel); + if (b < 0) + return (-1); + handle->_msg_ptr += b; + if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) + RETERR(EMSGSIZE); + NS_GET16(rr->type, handle->_msg_ptr); + NS_GET16(rr->rr_class, handle->_msg_ptr); + if (section == ns_s_qd) { + rr->ttl = 0; + rr->rdlength = 0; + rr->rdata = NULL; + } else { + if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) + RETERR(EMSGSIZE); + NS_GET32(rr->ttl, handle->_msg_ptr); + NS_GET16(rr->rdlength, handle->_msg_ptr); + if (handle->_msg_ptr + rr->rdlength > handle->_eom) + RETERR(EMSGSIZE); + rr->rdata = handle->_msg_ptr; + handle->_msg_ptr += rr->rdlength; + } + if (++handle->_rrnum > handle->_counts[(int)section]) + setsection(handle, (ns_sect)((int)section + 1)); + + /* All done. */ + return (0); +} + +/* Private. */ + +static void +setsection(ns_msg *msg, ns_sect sect) { + msg->_sect = sect; + if (sect == ns_s_max) { + msg->_rrnum = -1; + msg->_msg_ptr = NULL; + } else { + msg->_rrnum = 0; + msg->_msg_ptr = msg->_sections[(int)sect]; + } +} + +/*! \file */ diff --git a/lib/nbsd_libc/nameser/ns_print.c b/lib/nbsd_libc/nameser/ns_print.c new file mode 100644 index 000000000..51fe16ee4 --- /dev/null +++ b/lib/nbsd_libc/nameser/ns_print.c @@ -0,0 +1,1258 @@ +/* $NetBSD: ns_print.c,v 1.10 2009/04/12 19:43:37 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#ifndef lint +#ifdef notdef +static const char rcsid[] = "Id: ns_print.c,v 1.12 2009/03/03 05:29:58 each Exp"; +#else +__RCSID("$NetBSD: ns_print.c,v 1.10 2009/04/12 19:43:37 christos Exp $"); +#endif +#endif + +/* Import. */ + +#include "port_before.h" + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef SPRINTF_CHAR +# define SPRINTF(x) strlen(sprintf/**/x) +#else +# define SPRINTF(x) ((size_t)sprintf x) +#endif + +/* Forward. */ + +static size_t prune_origin(const char *name, const char *origin); +static int charstr(const u_char *rdata, const u_char *edata, + char **buf, size_t *buflen); +static int addname(const u_char *msg, size_t msglen, + const u_char **p, const char *origin, + char **buf, size_t *buflen); +static void addlen(size_t len, char **buf, size_t *buflen); +static int addstr(const char *src, size_t len, + char **buf, size_t *buflen); +static int addtab(size_t len, size_t target, int spaced, + char **buf, size_t *buflen); + +/* Macros. */ + +#define T(x) \ + do { \ + if ((x) < 0) \ + return (-1); \ + } while (/*CONSTCOND*/0) + +static const char base32hex[] = + "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv"; + +/* Public. */ + +/*% + * Convert an RR to presentation format. + * + * return: + *\li Number of characters written to buf, or -1 (check errno). + */ +int +ns_sprintrr(const ns_msg *handle, const ns_rr *rr, + const char *name_ctx, const char *origin, + char *buf, size_t buflen) +{ + int n; + + n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle), + ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr), + ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr), + name_ctx, origin, buf, buflen); + return (n); +} + +/*% + * Convert the fields of an RR into presentation format. + * + * return: + *\li Number of characters written to buf, or -1 (check errno). + */ +int +ns_sprintrrf(const u_char *msg, size_t msglen, + const char *name, ns_class class, ns_type type, + u_long ttl, const u_char *rdata, size_t rdlen, + const char *name_ctx, const char *origin, + char *buf, size_t buflen) +{ + const char *obuf = buf; + const u_char *edata = rdata + rdlen; + int spaced = 0; + + const char *comment; + char tmp[100]; + int len, x; + + /* + * Owner. + */ + if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) { + T(addstr("\t\t\t", (size_t)3, &buf, &buflen)); + } else { + len = prune_origin(name, origin); + if (*name == '\0') { + goto root; + } else if (len == 0) { + T(addstr("@\t\t\t", (size_t)4, &buf, &buflen)); + } else { + T(addstr(name, (size_t)len, &buf, &buflen)); + /* Origin not used or not root, and no trailing dot? */ + if (((origin == NULL || origin[0] == '\0') || + (origin[0] != '.' && origin[1] != '\0' && + name[len] == '\0')) && name[len - 1] != '.') { + root: + T(addstr(".", (size_t)1, &buf, &buflen)); + len++; + } + T(spaced = addtab((size_t)len, 24, spaced, &buf, &buflen)); + } + } + + /* + * TTL, Class, Type. + */ + T(x = ns_format_ttl(ttl, buf, buflen)); + addlen((size_t)x, &buf, &buflen); + len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + T(spaced = addtab((size_t)(x + len), (size_t)16, spaced, &buf, &buflen)); + + /* + * RData. + */ + switch (type) { + case ns_t_a: + if (rdlen != (size_t)NS_INADDRSZ) + goto formerr; + (void) inet_ntop(AF_INET, rdata, buf, buflen); + addlen(strlen(buf), &buf, &buflen); + break; + + case ns_t_cname: + case ns_t_mb: + case ns_t_mg: + case ns_t_mr: + case ns_t_ns: + case ns_t_ptr: + case ns_t_dname: + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + break; + + case ns_t_hinfo: + case ns_t_isdn: + /* First word. */ + T(len = charstr(rdata, edata, &buf, &buflen)); + if (len == 0) + goto formerr; + rdata += len; + T(addstr(" ", (size_t)1, &buf, &buflen)); + + + /* Second word, optional in ISDN records. */ + if (type == ns_t_isdn && rdata == edata) + break; + + T(len = charstr(rdata, edata, &buf, &buflen)); + if (len == 0) + goto formerr; + rdata += len; + break; + + case ns_t_soa: { + u_long t; + + /* Server name. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + T(addstr(" ", (size_t)1, &buf, &buflen)); + + /* Administrator name. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + T(addstr(" (\n", (size_t)3, &buf, &buflen)); + spaced = 0; + + if ((edata - rdata) != 5*NS_INT32SZ) + goto formerr; + + /* Serial number. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + T(addstr("\t\t\t\t\t", (size_t)5, &buf, &buflen)); + len = SPRINTF((tmp, "%lu", t)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + T(spaced = addtab((size_t)len, (size_t)16, spaced, &buf, &buflen)); + T(addstr("; serial\n", (size_t)9, &buf, &buflen)); + spaced = 0; + + /* Refresh interval. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + T(addstr("\t\t\t\t\t", (size_t)5, &buf, &buflen)); + T(len = ns_format_ttl(t, buf, buflen)); + addlen((size_t)len, &buf, &buflen); + T(spaced = addtab((size_t)len, (size_t)16, spaced, &buf, &buflen)); + T(addstr("; refresh\n", (size_t)10, &buf, &buflen)); + spaced = 0; + + /* Retry interval. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + T(addstr("\t\t\t\t\t", (size_t)5, &buf, &buflen)); + T(len = ns_format_ttl(t, buf, buflen)); + addlen((size_t)len, &buf, &buflen); + T(spaced = addtab((size_t)len, (size_t)16, spaced, &buf, &buflen)); + T(addstr("; retry\n", (size_t)8, &buf, &buflen)); + spaced = 0; + + /* Expiry. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + T(addstr("\t\t\t\t\t", (size_t)5, &buf, &buflen)); + T(len = ns_format_ttl(t, buf, buflen)); + addlen((size_t)len, &buf, &buflen); + T(spaced = addtab((size_t)len, (size_t)16, spaced, &buf, &buflen)); + T(addstr("; expiry\n", (size_t)9, &buf, &buflen)); + spaced = 0; + + /* Minimum TTL. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + T(addstr("\t\t\t\t\t", (size_t)5, &buf, &buflen)); + T(len = ns_format_ttl(t, buf, buflen)); + addlen((size_t)len, &buf, &buflen); + T(addstr(" )", (size_t)2, &buf, &buflen)); + T(spaced = addtab((size_t)len, (size_t)16, spaced, &buf, &buflen)); + T(addstr("; minimum\n", (size_t)10, &buf, &buflen)); + + break; + } + + case ns_t_mx: + case ns_t_afsdb: + case ns_t_rt: + case ns_t_kx: { + u_int t; + + if (rdlen < (size_t)NS_INT16SZ) + goto formerr; + + /* Priority. */ + t = ns_get16(rdata); + rdata += NS_INT16SZ; + len = SPRINTF((tmp, "%u ", t)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Target. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + + break; + } + + case ns_t_px: { + u_int t; + + if (rdlen < (size_t)NS_INT16SZ) + goto formerr; + + /* Priority. */ + t = ns_get16(rdata); + rdata += NS_INT16SZ; + len = SPRINTF((tmp, "%u ", t)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Name1. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + T(addstr(" ", (size_t)1, &buf, &buflen)); + + /* Name2. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + + break; + } + + case ns_t_x25: + T(len = charstr(rdata, edata, &buf, &buflen)); + if (len == 0) + goto formerr; + rdata += len; + break; + + case ns_t_txt: + case ns_t_spf: + while (rdata < edata) { + T(len = charstr(rdata, edata, &buf, &buflen)); + if (len == 0) + goto formerr; + rdata += len; + if (rdata < edata) + T(addstr(" ", (size_t)1, &buf, &buflen)); + } + break; + + case ns_t_nsap: { + char t[2+255*3]; + + (void) inet_nsap_ntoa((int)rdlen, rdata, t); + T(addstr(t, strlen(t), &buf, &buflen)); + break; + } + + case ns_t_aaaa: + if (rdlen != (size_t)NS_IN6ADDRSZ) + goto formerr; + (void) inet_ntop(AF_INET6, rdata, buf, buflen); + addlen(strlen(buf), &buf, &buflen); + break; + + case ns_t_loc: { + char t[255]; + + /* XXX protocol format checking? */ + (void) loc_ntoa(rdata, t); + T(addstr(t, strlen(t), &buf, &buflen)); + break; + } + + case ns_t_naptr: { + u_int order, preference; + char t[50]; + + if (rdlen < 2U*NS_INT16SZ) + goto formerr; + + /* Order, Precedence. */ + order = ns_get16(rdata); rdata += NS_INT16SZ; + preference = ns_get16(rdata); rdata += NS_INT16SZ; + len = SPRINTF((t, "%u %u ", order, preference)); + T(addstr(t, (size_t)len, &buf, &buflen)); + + /* Flags. */ + T(len = charstr(rdata, edata, &buf, &buflen)); + if (len == 0) + goto formerr; + rdata += len; + T(addstr(" ", (size_t)1, &buf, &buflen)); + + /* Service. */ + T(len = charstr(rdata, edata, &buf, &buflen)); + if (len == 0) + goto formerr; + rdata += len; + T(addstr(" ", (size_t)1, &buf, &buflen)); + + /* Regexp. */ + T(len = charstr(rdata, edata, &buf, &buflen)); + if (len < 0) + return (-1); + if (len == 0) + goto formerr; + rdata += len; + T(addstr(" ", (size_t)1, &buf, &buflen)); + + /* Server. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + break; + } + + case ns_t_srv: { + u_int priority, weight, port; + char t[50]; + + if (rdlen < 3U*NS_INT16SZ) + goto formerr; + + /* Priority, Weight, Port. */ + priority = ns_get16(rdata); rdata += NS_INT16SZ; + weight = ns_get16(rdata); rdata += NS_INT16SZ; + port = ns_get16(rdata); rdata += NS_INT16SZ; + len = SPRINTF((t, "%u %u %u ", priority, weight, port)); + T(addstr(t, (size_t)len, &buf, &buflen)); + + /* Server. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + break; + } + + case ns_t_minfo: + case ns_t_rp: + /* Name1. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + T(addstr(" ", (size_t)1, &buf, &buflen)); + + /* Name2. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + + break; + + case ns_t_wks: { + int n, lcnt; + + if (rdlen < 1U + NS_INT32SZ) + goto formerr; + + /* Address. */ + (void) inet_ntop(AF_INET, rdata, buf, buflen); + addlen(strlen(buf), &buf, &buflen); + rdata += NS_INADDRSZ; + + /* Protocol. */ + len = SPRINTF((tmp, " %u ( ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata += NS_INT8SZ; + + /* Bit map. */ + n = 0; + lcnt = 0; + while (rdata < edata) { + u_int c = *rdata++; + do { + if (c & 0200) { + if (lcnt == 0) { + T(addstr("\n\t\t\t\t", (size_t)5, + &buf, &buflen)); + lcnt = 10; + spaced = 0; + } + len = SPRINTF((tmp, "%d ", n)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + lcnt--; + } + c <<= 1; + } while (++n & 07); + } + T(addstr(")", (size_t)1, &buf, &buflen)); + + break; + } + + case ns_t_key: + case ns_t_dnskey: { + char base64_key[NS_MD5RSA_MAX_BASE64]; + u_int keyflags, protocol, algorithm, key_id; + const char *leader; + int n; + + if (rdlen < 0U + NS_INT16SZ + NS_INT8SZ + NS_INT8SZ) + goto formerr; + + /* Key flags, Protocol, Algorithm. */ +#ifndef _LIBC + key_id = dst_s_dns_key_id(rdata, edata-rdata); +#else + key_id = 0; +#endif + keyflags = ns_get16(rdata); rdata += NS_INT16SZ; + protocol = *rdata++; + algorithm = *rdata++; + len = SPRINTF((tmp, "0x%04x %u %u", + keyflags, protocol, algorithm)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Public key data. */ + len = b64_ntop(rdata, (size_t)(edata - rdata), + base64_key, sizeof base64_key); + if (len < 0) + goto formerr; + if (len > 15) { + T(addstr(" (", (size_t)2, &buf, &buflen)); + leader = "\n\t\t"; + spaced = 0; + } else + leader = " "; + for (n = 0; n < len; n += 48) { + T(addstr(leader, strlen(leader), &buf, &buflen)); + T(addstr(base64_key + n, (size_t)MIN(len - n, 48), + &buf, &buflen)); + } + if (len > 15) + T(addstr(" )", (size_t)2, &buf, &buflen)); + n = SPRINTF((tmp, " ; key_tag= %u", key_id)); + T(addstr(tmp, (size_t)n, &buf, &buflen)); + + break; + } + + case ns_t_sig: + case ns_t_rrsig: { + char base64_key[NS_MD5RSA_MAX_BASE64]; + u_int typ, algorithm, labels, footprint; + const char *leader; + u_long t; + int n; + + if (rdlen < 22U) + goto formerr; + + /* Type covered, Algorithm, Label count, Original TTL. */ + typ = ns_get16(rdata); rdata += NS_INT16SZ; + algorithm = *rdata++; + labels = *rdata++; + t = ns_get32(rdata); rdata += NS_INT32SZ; + len = SPRINTF((tmp, "%s %d %d %lu ", + p_type((int)typ), algorithm, labels, t)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + if (labels > (u_int)dn_count_labels(name)) + goto formerr; + + /* Signature expiry. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + len = SPRINTF((tmp, "%s ", p_secstodate(t))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Time signed. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + len = SPRINTF((tmp, "%s ", p_secstodate(t))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Signature Footprint. */ + footprint = ns_get16(rdata); rdata += NS_INT16SZ; + len = SPRINTF((tmp, "%u ", footprint)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Signer's name. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + + /* Signature. */ + len = b64_ntop(rdata, (size_t)(edata - rdata), + base64_key, sizeof base64_key); + if (len > 15) { + T(addstr(" (", (size_t)2, &buf, &buflen)); + leader = "\n\t\t"; + spaced = 0; + } else + leader = " "; + if (len < 0) + goto formerr; + for (n = 0; n < len; n += 48) { + T(addstr(leader, strlen(leader), &buf, &buflen)); + T(addstr(base64_key + n, (size_t)MIN(len - n, 48), + &buf, &buflen)); + } + if (len > 15) + T(addstr(" )", (size_t)2, &buf, &buflen)); + break; + } + + case ns_t_nxt: { + int n, c; + + /* Next domain name. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + + /* Type bit map. */ + n = edata - rdata; + for (c = 0; c < n*8; c++) + if (NS_NXT_BIT_ISSET(c, rdata)) { + len = SPRINTF((tmp, " %s", p_type(c))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + } + break; + } + + case ns_t_cert: { + u_int c_type, key_tag, alg; + int n; + unsigned int siz; + char base64_cert[8192], tmp1[40]; + const char *leader; + + c_type = ns_get16(rdata); rdata += NS_INT16SZ; + key_tag = ns_get16(rdata); rdata += NS_INT16SZ; + alg = (u_int) *rdata++; + + len = SPRINTF((tmp1, "%d %d %d ", c_type, key_tag, alg)); + T(addstr(tmp1, (size_t)len, &buf, &buflen)); + siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */ + if (siz > sizeof(base64_cert) * 3/4) { + const char *str = "record too long to print"; + T(addstr(str, strlen(str), &buf, &buflen)); + } + else { + len = b64_ntop(rdata, (size_t)(edata-rdata), + base64_cert, siz); + + if (len < 0) + goto formerr; + else if (len > 15) { + T(addstr(" (", (size_t)2, &buf, &buflen)); + leader = "\n\t\t"; + spaced = 0; + } + else + leader = " "; + + for (n = 0; n < len; n += 48) { + T(addstr(leader, strlen(leader), + &buf, &buflen)); + T(addstr(base64_cert + n, (size_t)MIN(len - n, 48), + &buf, &buflen)); + } + if (len > 15) + T(addstr(" )", (size_t)2, &buf, &buflen)); + } + break; + } + + case ns_t_tkey: { + /* KJD - need to complete this */ + u_long t; + int mode, err, keysize; + + /* Algorithm name. */ + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + T(addstr(" ", (size_t)1, &buf, &buflen)); + + /* Inception. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + len = SPRINTF((tmp, "%s ", p_secstodate(t))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Experation. */ + t = ns_get32(rdata); rdata += NS_INT32SZ; + len = SPRINTF((tmp, "%s ", p_secstodate(t))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* Mode , Error, Key Size. */ + /* Priority, Weight, Port. */ + mode = ns_get16(rdata); rdata += NS_INT16SZ; + err = ns_get16(rdata); rdata += NS_INT16SZ; + keysize = ns_get16(rdata); rdata += NS_INT16SZ; + len = SPRINTF((tmp, "%u %u %u ", mode, err, keysize)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + /* XXX need to dump key, print otherdata length & other data */ + break; + } + + case ns_t_tsig: { + /* BEW - need to complete this */ + int n; + + T(len = addname(msg, msglen, &rdata, origin, &buf, &buflen)); + T(addstr(" ", (size_t)1, &buf, &buflen)); + rdata += 8; /*%< time */ + n = ns_get16(rdata); rdata += INT16SZ; + rdata += n; /*%< sig */ + n = ns_get16(rdata); rdata += INT16SZ; /*%< original id */ + sprintf(buf, "%d", ns_get16(rdata)); + rdata += INT16SZ; + addlen(strlen(buf), &buf, &buflen); + break; + } + + case ns_t_a6: { + struct in6_addr a; + int pbyte, pbit; + + /* prefix length */ + if (rdlen == 0U) goto formerr; + len = SPRINTF((tmp, "%d ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + pbit = *rdata; + if (pbit > 128) goto formerr; + pbyte = (pbit & ~7) / 8; + rdata++; + + /* address suffix: provided only when prefix len != 128 */ + if (pbit < 128) { + if (rdata + pbyte >= edata) goto formerr; + memset(&a, 0, sizeof(a)); + memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte); + (void) inet_ntop(AF_INET6, &a, buf, buflen); + addlen(strlen(buf), &buf, &buflen); + rdata += sizeof(a) - pbyte; + } + + /* prefix name: provided only when prefix len > 0 */ + if (pbit == 0) + break; + if (rdata >= edata) goto formerr; + T(addstr(" ", (size_t)1, &buf, &buflen)); + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + + break; + } + + case ns_t_opt: { + len = SPRINTF((tmp, "%u bytes", class)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + break; + } + + case ns_t_ds: + case ns_t_dlv: + case ns_t_sshfp: { + u_int t; + + if (type == ns_t_ds || type == ns_t_dlv) { + if (rdlen < 4U) goto formerr; + t = ns_get16(rdata); + rdata += NS_INT16SZ; + len = SPRINTF((tmp, "%u ", t)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + } else + if (rdlen < 2U) goto formerr; + + len = SPRINTF((tmp, "%u ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + + len = SPRINTF((tmp, "%u ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + + while (rdata < edata) { + len = SPRINTF((tmp, "%02X", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + } + break; + } + + case ns_t_nsec3: + case ns_t_nsec3param: { + u_int t, w, l, j, k, c; + + len = SPRINTF((tmp, "%u ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + + len = SPRINTF((tmp, "%u ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + + t = ns_get16(rdata); + rdata += NS_INT16SZ; + len = SPRINTF((tmp, "%u ", t)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + t = *rdata++; + if (t == 0) { + T(addstr("-", 1, &buf, &buflen)); + } else { + while (t-- > 0) { + len = SPRINTF((tmp, "%02X", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + } + } + if (type == ns_t_nsec3param) + break; + T(addstr(" ", 1, &buf, &buflen)); + + t = *rdata++; + while (t > 0) { + switch (t) { + case 1: + tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)]; + tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)]; + tmp[2] = tmp[3] = tmp[4] = '='; + tmp[5] = tmp[6] = tmp[7] = '='; + break; + case 2: + tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)]; + tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)| + (((uint32_t)rdata[1]>>6)&0x03)]; + tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)]; + tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)]; + tmp[4] = tmp[5] = tmp[6] = tmp[7] = '='; + break; + case 3: + tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)]; + tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)| + (((uint32_t)rdata[1]>>6)&0x03)]; + tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)]; + tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)| + (((uint32_t)rdata[2]>>4)&0x0f)]; + tmp[4] = base32hex[(((uint32_t)rdata[2]<<1)&0x1e)]; + tmp[5] = tmp[6] = tmp[7] = '='; + break; + case 4: + tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)]; + tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)| + (((uint32_t)rdata[1]>>6)&0x03)]; + tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)]; + tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)| + (((uint32_t)rdata[2]>>4)&0x0f)]; + tmp[4] = base32hex[(((uint32_t)rdata[2]<<1)&0x1e)| + (((uint32_t)rdata[3]>>7)&0x01)]; + tmp[5] = base32hex[(((uint32_t)rdata[3]>>2)&0x1f)]; + tmp[6] = base32hex[((uint32_t)rdata[3]<<3)&0x18]; + tmp[7] = '='; + break; + default: + tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)]; + tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)| + (((uint32_t)rdata[1]>>6)&0x03)]; + tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)]; + tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)| + (((uint32_t)rdata[2]>>4)&0x0f)]; + tmp[4] = base32hex[(((uint32_t)rdata[2]<<1)&0x1e)| + (((uint32_t)rdata[3]>>7)&0x01)]; + tmp[5] = base32hex[(((uint32_t)rdata[3]>>2)&0x1f)]; + tmp[6] = base32hex[(((uint32_t)rdata[3]<<3)&0x18)| + (((uint32_t)rdata[4]>>5)&0x07)]; + tmp[7] = base32hex[(rdata[4]&0x1f)]; + break; + } + T(addstr(tmp, 8, &buf, &buflen)); + if (t >= 5) { + rdata += 5; + t -= 5; + } else { + rdata += t; + t -= t; + } + } + + while (rdata < edata) { + w = *rdata++; + l = *rdata++; + for (j = 0; j < l; j++) { + if (rdata[j] == 0) + continue; + for (k = 0; k < 8; k++) { + if ((rdata[j] & (0x80 >> k)) == 0) + continue; + c = w * 256 + j * 8 + k; + len = SPRINTF((tmp, " %s", p_type((ns_type)c))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + } + } + rdata += l; + } + break; + } + + case ns_t_nsec: { + u_int w, l, j, k, c; + + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + + while (rdata < edata) { + w = *rdata++; + l = *rdata++; + for (j = 0; j < l; j++) { + if (rdata[j] == 0) + continue; + for (k = 0; k < 8; k++) { + if ((rdata[j] & (0x80 >> k)) == 0) + continue; + c = w * 256 + j * 8 + k; + len = SPRINTF((tmp, " %s", p_type((ns_type)c))); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + } + } + rdata += l; + } + break; + } + + case ns_t_dhcid: { + int n; + unsigned int siz; + char base64_dhcid[8192]; + const char *leader; + + siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */ + if (siz > sizeof(base64_dhcid) * 3/4) { + const char *str = "record too long to print"; + T(addstr(str, strlen(str), &buf, &buflen)); + } else { + len = b64_ntop(rdata, (size_t)(edata-rdata), + base64_dhcid, siz); + + if (len < 0) + goto formerr; + + else if (len > 15) { + T(addstr(" (", 2, &buf, &buflen)); + leader = "\n\t\t"; + spaced = 0; + } + else + leader = " "; + + for (n = 0; n < len; n += 48) { + T(addstr(leader, strlen(leader), + &buf, &buflen)); + T(addstr(base64_dhcid + n, + (size_t)MIN(len - n, 48), &buf, &buflen)); + } + if (len > 15) + T(addstr(" )", 2, &buf, &buflen)); + } + break; + } + + case ns_t_ipseckey: { + int n; + unsigned int siz; + char base64_key[8192]; + const char *leader; + + if (rdlen < 2) + goto formerr; + + switch (rdata[1]) { + case 0: + case 3: + if (rdlen < 3) + goto formerr; + break; + case 1: + if (rdlen < 7) + goto formerr; + break; + case 2: + if (rdlen < 19) + goto formerr; + break; + default: + comment = "unknown IPSECKEY gateway type"; + goto hexify; + } + + len = SPRINTF((tmp, "%u ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + + len = SPRINTF((tmp, "%u ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + + len = SPRINTF((tmp, "%u ", *rdata)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + + switch (rdata[-2]) { + case 0: + T(addstr(".", 1, &buf, &buflen)); + break; + case 1: + (void) inet_ntop(AF_INET, rdata, buf, buflen); + addlen(strlen(buf), &buf, &buflen); + rdata += 4; + break; + case 2: + (void) inet_ntop(AF_INET6, rdata, buf, buflen); + addlen(strlen(buf), &buf, &buflen); + rdata += 16; + break; + case 3: + T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); + break; + } + + if (rdata >= edata) + break; + + siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */ + if (siz > sizeof(base64_key) * 3/4) { + const char *str = "record too long to print"; + T(addstr(str, strlen(str), &buf, &buflen)); + } else { + len = b64_ntop(rdata, (size_t)(edata-rdata), + base64_key, siz); + + if (len < 0) + goto formerr; + + else if (len > 15) { + T(addstr(" (", 2, &buf, &buflen)); + leader = "\n\t\t"; + spaced = 0; + } + else + leader = " "; + + for (n = 0; n < len; n += 48) { + T(addstr(leader, strlen(leader), + &buf, &buflen)); + T(addstr(base64_key + n, + (size_t)MIN(len - n, 48), &buf, &buflen)); + } + if (len > 15) + T(addstr(" )", 2, &buf, &buflen)); + } + break; + } + + case ns_t_hip: { + unsigned int i, hip_len, algorithm, key_len; + char base64_key[NS_MD5RSA_MAX_BASE64]; + unsigned int siz; + const char *leader = "\n\t\t\t\t\t"; + + hip_len = *rdata++; + algorithm = *rdata++; + key_len = ns_get16(rdata); + rdata += NS_INT16SZ; + + siz = key_len*4/3 + 4; /* "+4" accounts for trailing \0 */ + if (siz > sizeof(base64_key) * 3/4) { + const char *str = "record too long to print"; + T(addstr(str, strlen(str), &buf, &buflen)); + } else { + len = sprintf(tmp, "( %u ", algorithm); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + + for (i = 0; i < hip_len; i++) { + len = sprintf(tmp, "%02X", *rdata); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + rdata++; + } + T(addstr(leader, strlen(leader), &buf, &buflen)); + + len = b64_ntop(rdata, key_len, base64_key, siz); + if (len < 0) + goto formerr; + + T(addstr(base64_key, (size_t)len, &buf, &buflen)); + + rdata += key_len; + while (rdata < edata) { + T(addstr(leader, strlen(leader), &buf, &buflen)); + T(addname(msg, msglen, &rdata, origin, + &buf, &buflen)); + } + T(addstr(" )", 2, &buf, &buflen)); + } + break; + } + + default: + comment = "unknown RR type"; + goto hexify; + } + return (buf - obuf); + formerr: + comment = "RR format error"; + hexify: { + int n, m; + char *p; + + len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata), + rdlen != 0U ? " (" : "", comment)); + T(addstr(tmp, (size_t)len, &buf, &buflen)); + while (rdata < edata) { + p = tmp; + p += SPRINTF((p, "\n\t")); + spaced = 0; + n = MIN(16, edata - rdata); + for (m = 0; m < n; m++) + p += SPRINTF((p, "%02x ", rdata[m])); + T(addstr(tmp, (size_t)(p - tmp), &buf, &buflen)); + if (n < 16) { + T(addstr(")", (size_t)1, &buf, &buflen)); + T(addtab((size_t)(p - tmp + 1), (size_t)48, spaced, &buf, &buflen)); + } + p = tmp; + p += SPRINTF((p, "; ")); + for (m = 0; m < n; m++) + *p++ = (isascii(rdata[m]) && isprint(rdata[m])) + ? rdata[m] + : '.'; + T(addstr(tmp, (size_t)(p - tmp), &buf, &buflen)); + rdata += n; + } + return (buf - obuf); + } +} + +/* Private. */ + +/*% + * size_t + * prune_origin(name, origin) + * Find out if the name is at or under the current origin. + * return: + * Number of characters in name before start of origin, + * or length of name if origin does not match. + * notes: + * This function should share code with samedomain(). + */ +static size_t +prune_origin(const char *name, const char *origin) { + const char *oname = name; + + while (*name != '\0') { + if (origin != NULL && ns_samename(name, origin) == 1) + return (name - oname - (name > oname)); + while (*name != '\0') { + if (*name == '\\') { + name++; + /* XXX need to handle \nnn form. */ + if (*name == '\0') + break; + } else if (*name == '.') { + name++; + break; + } + name++; + } + } + return (name - oname); +} + +/*% + * int + * charstr(rdata, edata, buf, buflen) + * Format a into the presentation buffer. + * return: + * Number of rdata octets consumed + * 0 for protocol format error + * -1 for output buffer error + * side effects: + * buffer is advanced on success. + */ +static int +charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) { + const u_char *odata = rdata; + size_t save_buflen = *buflen; + char *save_buf = *buf; + + if (addstr("\"", (size_t)1, buf, buflen) < 0) + goto enospc; + if (rdata < edata) { + int n = *rdata; + + if (rdata + 1 + n <= edata) { + rdata++; + while (n-- > 0) { + if (strchr("\n\"\\", *rdata) != NULL) + if (addstr("\\", (size_t)1, buf, buflen) < 0) + goto enospc; + if (addstr((const char *)rdata, (size_t)1, + buf, buflen) < 0) + goto enospc; + rdata++; + } + } + } + if (addstr("\"", (size_t)1, buf, buflen) < 0) + goto enospc; + return (rdata - odata); + enospc: + errno = ENOSPC; + *buf = save_buf; + *buflen = save_buflen; + return (-1); +} + +static int +addname(const u_char *msg, size_t msglen, + const u_char **pp, const char *origin, + char **buf, size_t *buflen) +{ + size_t newlen, save_buflen = *buflen; + char *save_buf = *buf; + int n; + + n = dn_expand(msg, msg + msglen, *pp, *buf, (int)*buflen); + if (n < 0) + goto enospc; /*%< Guess. */ + newlen = prune_origin(*buf, origin); + if (**buf == '\0') { + goto root; + } else if (newlen == 0U) { + /* Use "@" instead of name. */ + if (newlen + 2 > *buflen) + goto enospc; /* No room for "@\0". */ + (*buf)[newlen++] = '@'; + (*buf)[newlen] = '\0'; + } else { + if (((origin == NULL || origin[0] == '\0') || + (origin[0] != '.' && origin[1] != '\0' && + (*buf)[newlen] == '\0')) && (*buf)[newlen - 1] != '.') { + /* No trailing dot. */ + root: + if (newlen + 2 > *buflen) + goto enospc; /* No room for ".\0". */ + (*buf)[newlen++] = '.'; + (*buf)[newlen] = '\0'; + } + } + *pp += n; + addlen(newlen, buf, buflen); + **buf = '\0'; + return (newlen); + enospc: + errno = ENOSPC; + *buf = save_buf; + *buflen = save_buflen; + return (-1); +} + +static void +addlen(size_t len, char **buf, size_t *buflen) { + INSIST(len <= *buflen); + *buf += len; + *buflen -= len; +} + +static int +addstr(const char *src, size_t len, char **buf, size_t *buflen) { + if (len >= *buflen) { + errno = ENOSPC; + return (-1); + } + memcpy(*buf, src, len); + addlen(len, buf, buflen); + **buf = '\0'; + return (0); +} + +static int +addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) { + size_t save_buflen = *buflen; + char *save_buf = *buf; + int t; + + if (spaced || len >= target - 1) { + T(addstr(" ", (size_t)2, buf, buflen)); + spaced = 1; + } else { + for (t = (target - len - 1) / 8; t >= 0; t--) + if (addstr("\t", (size_t)1, buf, buflen) < 0) { + *buflen = save_buflen; + *buf = save_buf; + return (-1); + } + spaced = 0; + } + return (spaced); +} + +/*! \file */ diff --git a/lib/nbsd_libc/nameser/ns_samedomain.c b/lib/nbsd_libc/nameser/ns_samedomain.c new file mode 100644 index 000000000..05cb48dfc --- /dev/null +++ b/lib/nbsd_libc/nameser/ns_samedomain.c @@ -0,0 +1,216 @@ +/* $NetBSD: ns_samedomain.c,v 1.6 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1995,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#ifndef lint +#ifdef notdef +static const char rcsid[] = "Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp"; +#else +__RCSID("$NetBSD: ns_samedomain.c,v 1.6 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif + +#include "port_before.h" + +#include +#include +#include +#include + +#include "port_after.h" + +#ifndef _LIBC +/*% + * Check whether a name belongs to a domain. + * + * Inputs: + *\li a - the domain whose ancestory is being verified + *\li b - the potential ancestor we're checking against + * + * Return: + *\li boolean - is a at or below b? + * + * Notes: + *\li Trailing dots are first removed from name and domain. + * Always compare complete subdomains, not only whether the + * domain name is the trailing string of the given name. + * + *\li "host.foobar.top" lies in "foobar.top" and in "top" and in "" + * but NOT in "bar.top" + */ + +int +ns_samedomain(const char *a, const char *b) { + size_t la, lb; + int diff, i, escaped; + const char *cp; + + la = strlen(a); + lb = strlen(b); + + /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */ + if (la != 0U && a[la - 1] == '.') { + escaped = 0; + /* Note this loop doesn't get executed if la==1. */ + for (i = la - 2; i >= 0; i--) + if (a[i] == '\\') { + if (escaped) + escaped = 0; + else + escaped = 1; + } else + break; + if (!escaped) + la--; + } + + /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */ + if (lb != 0U && b[lb - 1] == '.') { + escaped = 0; + /* note this loop doesn't get executed if lb==1 */ + for (i = lb - 2; i >= 0; i--) + if (b[i] == '\\') { + if (escaped) + escaped = 0; + else + escaped = 1; + } else + break; + if (!escaped) + lb--; + } + + /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */ + if (lb == 0U) + return (1); + + /* 'b' longer than 'a' means 'a' can't be in 'b'. */ + if (lb > la) + return (0); + + /* 'a' and 'b' being equal at this point indicates sameness. */ + if (lb == la) + return (strncasecmp(a, b, lb) == 0); + + /* Ok, we know la > lb. */ + + diff = la - lb; + + /* + * If 'a' is only 1 character longer than 'b', then it can't be + * a subdomain of 'b' (because of the need for the '.' label + * separator). + */ + if (diff < 2) + return (0); + + /* + * If the character before the last 'lb' characters of 'b' + * isn't '.', then it can't be a match (this lets us avoid + * having "foobar.com" match "bar.com"). + */ + if (a[diff - 1] != '.') + return (0); + + /* + * We're not sure about that '.', however. It could be escaped + * and thus not a really a label separator. + */ + escaped = 0; + for (i = diff - 2; i >= 0; i--) + if (a[i] == '\\') { + if (escaped) + escaped = 0; + else + escaped = 1; + } else + break; + if (escaped) + return (0); + + /* Now compare aligned trailing substring. */ + cp = a + diff; + return (strncasecmp(cp, b, lb) == 0); +} + +/*% + * is "a" a subdomain of "b"? + */ +int +ns_subdomain(const char *a, const char *b) { + return (ns_samename(a, b) != 1 && ns_samedomain(a, b)); +} +#endif + +/*% + * make a canonical copy of domain name "src" + * + * notes: + * \code + * foo -> foo. + * foo. -> foo. + * foo.. -> foo. + * foo\. -> foo\.. + * foo\\. -> foo\\. + * \endcode + */ + +int +ns_makecanon(const char *src, char *dst, size_t dstsize) { + size_t n = strlen(src); + + if (n + sizeof "." > dstsize) { /*%< Note: sizeof == 2 */ + errno = EMSGSIZE; + return (-1); + } + strcpy(dst, src); + while (n >= 1U && dst[n - 1] == '.') /*%< Ends in "." */ + if (n >= 2U && dst[n - 2] == '\\' && /*%< Ends in "\." */ + (n < 3U || dst[n - 3] != '\\')) /*%< But not "\\." */ + break; + else + dst[--n] = '\0'; + dst[n++] = '.'; + dst[n] = '\0'; + return (0); +} + +/*% + * determine whether domain name "a" is the same as domain name "b" + * + * return: + *\li -1 on error + *\li 0 if names differ + *\li 1 if names are the same + */ + +int +ns_samename(const char *a, const char *b) { + char ta[NS_MAXDNAME], tb[NS_MAXDNAME]; + + if (ns_makecanon(a, ta, sizeof ta) < 0 || + ns_makecanon(b, tb, sizeof tb) < 0) + return (-1); + if (strcasecmp(ta, tb) == 0) + return (1); + else + return (0); +} + +/*! \file */ diff --git a/lib/nbsd_libc/nameser/ns_ttl.c b/lib/nbsd_libc/nameser/ns_ttl.c new file mode 100644 index 000000000..2545ffda1 --- /dev/null +++ b/lib/nbsd_libc/nameser/ns_ttl.c @@ -0,0 +1,171 @@ +/* $NetBSD: ns_ttl.c,v 1.7 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#ifndef lint +#ifdef notdef +static const char rcsid[] = "Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp"; +#else +__RCSID("$NetBSD: ns_ttl.c,v 1.7 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif + +/* Import. */ + +#include "port_before.h" + +#include + +#include +#include +#include +#include + +#include "port_after.h" + +#ifdef SPRINTF_CHAR +# define SPRINTF(x) strlen(sprintf/**/x) +#else +# define SPRINTF(x) ((size_t)sprintf x) +#endif + +/* Forward. */ + +static int fmt1(int t, char s, char **buf, size_t *buflen); + +/* Macros. */ + +#define T(x) if ((x) < 0) return (-1) + +/* Public. */ + +int +ns_format_ttl(u_long src, char *dst, size_t dstlen) { + char *odst = dst; + int secs, mins, hours, days, weeks, x; + char *p; + + secs = src % 60; src /= 60; + mins = src % 60; src /= 60; + hours = src % 24; src /= 24; + days = src % 7; src /= 7; + weeks = src; src = 0; + + x = 0; + if (weeks) { + T(fmt1(weeks, 'W', &dst, &dstlen)); + x++; + } + if (days) { + T(fmt1(days, 'D', &dst, &dstlen)); + x++; + } + if (hours) { + T(fmt1(hours, 'H', &dst, &dstlen)); + x++; + } + if (mins) { + T(fmt1(mins, 'M', &dst, &dstlen)); + x++; + } + if (secs || !(weeks || days || hours || mins)) { + T(fmt1(secs, 'S', &dst, &dstlen)); + x++; + } + + if (x > 1) { + int ch; + + for (p = odst; (ch = *p) != '\0'; p++) + if (isascii(ch) && isupper(ch)) + *p = tolower(ch); + } + + return (dst - odst); +} + +#ifndef _LIBC +int +ns_parse_ttl(const char *src, u_long *dst) { + u_long ttl, tmp; + int ch, digits, dirty; + + ttl = 0; + tmp = 0; + digits = 0; + dirty = 0; + while ((ch = *src++) != '\0') { + if (!isascii(ch) || !isprint(ch)) + goto einval; + if (isdigit(ch)) { + tmp *= 10; + tmp += (ch - '0'); + digits++; + continue; + } + if (digits == 0) + goto einval; + if (islower(ch)) + ch = toupper(ch); + switch (ch) { + case 'W': tmp *= 7; /*FALLTHROUGH*/ + case 'D': tmp *= 24; /*FALLTHROUGH*/ + case 'H': tmp *= 60; /*FALLTHROUGH*/ + case 'M': tmp *= 60; /*FALLTHROUGH*/ + case 'S': break; + default: goto einval; + } + ttl += tmp; + tmp = 0; + digits = 0; + dirty = 1; + } + if (digits > 0) { + if (dirty) + goto einval; + else + ttl += tmp; + } else if (!dirty) + goto einval; + *dst = ttl; + return (0); + + einval: + errno = EINVAL; + return (-1); +} +#endif + +/* Private. */ + +static int +fmt1(int t, char s, char **buf, size_t *buflen) { + char tmp[50]; + size_t len; + + len = SPRINTF((tmp, "%d%c", t, s)); + if (len + 1 > *buflen) + return (-1); + strcpy(*buf, tmp); + *buf += len; + *buflen -= len; + return (0); +} + +/*! \file */ diff --git a/lib/nbsd_libc/net/Lint_htonl.c b/lib/nbsd_libc/net/Lint_htonl.c new file mode 100644 index 000000000..e1d7e03bf --- /dev/null +++ b/lib/nbsd_libc/net/Lint_htonl.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_htonl.c,v 1.4 2001/08/22 07:42:08 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef htonl + +/*ARGSUSED*/ +uint32_t +htonl(host32) + uint32_t host32; +{ + return (0); +} diff --git a/lib/nbsd_libc/net/Lint_htons.c b/lib/nbsd_libc/net/Lint_htons.c new file mode 100644 index 000000000..f0b06b292 --- /dev/null +++ b/lib/nbsd_libc/net/Lint_htons.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_htons.c,v 1.4 2001/08/22 07:42:09 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef htons + +/*ARGSUSED*//*NOSTRICT*/ +uint16_t +htons(host16) + uint16_t host16; +{ + return (0); +} diff --git a/lib/nbsd_libc/net/Lint_ntohl.c b/lib/nbsd_libc/net/Lint_ntohl.c new file mode 100644 index 000000000..c2be9e467 --- /dev/null +++ b/lib/nbsd_libc/net/Lint_ntohl.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_ntohl.c,v 1.4 2001/08/22 07:42:09 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef ntohl + +/*ARGSUSED*/ +uint32_t +ntohl(net32) + uint32_t net32; +{ + return (0); +} diff --git a/lib/nbsd_libc/net/Lint_ntohs.c b/lib/nbsd_libc/net/Lint_ntohs.c new file mode 100644 index 000000000..6841907e4 --- /dev/null +++ b/lib/nbsd_libc/net/Lint_ntohs.c @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_ntohs.c,v 1.4 2001/08/22 07:42:09 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef ntohs + +/*ARGSUSED*//*NOSTRICT*/ +uint16_t +ntohs(net16) + uint16_t net16; +{ + return (0); +} diff --git a/lib/nbsd_libc/net/Makefile.inc b/lib/nbsd_libc/net/Makefile.inc new file mode 100644 index 000000000..7c1f493cd --- /dev/null +++ b/lib/nbsd_libc/net/Makefile.inc @@ -0,0 +1,145 @@ +# $NetBSD: Makefile.inc,v 1.78 2009/10/02 02:45:29 tsarna Exp $ +# @(#)Makefile.inc 8.2 (Berkeley) 9/5/93 + +# net sources +.PATH: ${ARCHDIR}/net ${.CURDIR}/net + +SRCS+= __cmsg_alignbytes.c base64.c ethers.c gethnamaddr.c getifaddrs.c \ + getnetnamadr.c getnetent.c getpeereid.c \ + getprotobyname.c getprotobynumber.c getprotoent.c \ + getprotobyname_r.c getprotobynumber_r.c getprotoent_r.c \ + getservbyname.c getservbyport.c getservent.c \ + getservbyname_r.c getservbyport_r.c getservent_r.c \ + iso_addr.c linkaddr.c \ + nsdispatch.c nslexer.l nsparser.y nsap_addr.c \ + rcmd.c recv.c send.c sethostent.c \ + sockatmark.c + +.if (${MKHESIOD} != "no") +SRCS+= hesiod.c +.endif + +SRCS+= getaddrinfo.c getnameinfo.c +.if (${USE_INET6} != "no") +SRCS+= ip6opt.c rthdr.c vars6.c +.endif +SRCS+= if_indextoname.c if_nameindex.c if_nametoindex.c + +LPREFIX=_nsyy +YPREFIX=_nsyy +YHEADER=1 + +CLEANFILES+=nsparser.c nslexer.c nsparser.h + +nslexer.c: nslexer.l nsparser.h + ${_MKTARGET_LEX} + ${LEX.l} -t ${.IMPSRC} | sed -e '/YY_BUF_SIZE/s/16384/1024/' >${.TARGET} + +# machine-dependent net sources +# m-d Makefile.inc must include sources for: +# htonl() htons() ntohl() ntohs() + +.include "${ARCHDIR}/net/Makefile.inc" + +MAN+= byteorder.3 ethers.3 gethostbyname.3 getifaddrs.3 \ + getnetent.3 getprotoent.3 getpeereid.3 \ + getservent.3 inet.3 inet_net.3 iso_addr.3 linkaddr.3 \ + nsdispatch.3 rcmd.3 resolver.3 sockatmark.3 + +.if (${MKHESIOD} != "no") +MAN+= hesiod.3 +.endif + +MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \ + byteorder.3 ntohs.3 +MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \ + ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 +MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \ + gethostbyname.3 gethostent.3 gethostbyname.3 sethostent.3 \ + gethostbyname.3 herror.3 gethostbyname.3 hstrerror.3 \ + gethostbyname.3 gethostbyname2.3 +MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \ + getnetent.3 getnetbyname.3 getnetent.3 setnetent.3 +MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \ + getprotoent.3 getprotobynumber.3 getprotoent.3 setprotoent.3 +MLINKS+=getservent.3 endservent.3 getservent.3 getservbyname.3 \ + getservent.3 getservbyport.3 getservent.3 setservent.3 +.if (${MKHESIOD} != "no") +MLINKS+=hesiod.3 hesiod_end.3 hesiod.3 hesiod_free_list.3 \ + hesiod.3 hesiod_init.3 hesiod.3 hesiod_resolve.3 \ + hesiod.3 hesiod_to_bind.3 +.endif +MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \ + inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ + inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \ + inet.3 ntoa.3 inet.3 inet_ntop.3 inet.3 inet_pton.3 +MLINKS+=inet_net.3 inet_net_ntop.3 inet_net.3 inet_net_pton.3 +MLINKS+=linkaddr.3 link_addr.3 linkaddr.3 link_ntoa.3 linkaddr.3 linkntoa.3 +MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 rcmd.3 orcmd.3 +MLINKS+=resolver.3 dn_comp.3 \ + resolver.3 dn_expand.3 \ + resolver.3 fp_nquery.3 \ + resolver.3 fp_resstat.3 \ + resolver.3 hostalias.3 \ + resolver.3 p_query.3 \ + resolver.3 res_close.3 \ + resolver.3 res_findzonecut.3 \ + resolver.3 res_getservers.3 \ + resolver.3 res_hostalias.3 \ + resolver.3 res_init.3 \ + resolver.3 res_isourserver.3 \ + resolver.3 res_mkquery.3 \ + resolver.3 res_nclose.3 \ + resolver.3 res_ndestroy.3 \ + resolver.3 res_ninit.3 \ + resolver.3 res_nmkquery.3 \ + resolver.3 res_nmkupdate.3 \ + resolver.3 res_nquery.3 \ + resolver.3 res_nquerydomain.3 \ + resolver.3 res_nsearch.3 \ + resolver.3 res_nsend.3 \ + resolver.3 res_nsendsigned.3 \ + resolver.3 res_nupdate.3 \ + resolver.3 res_ourserver_p.3 \ + resolver.3 res_pquery.3 \ + resolver.3 res_query.3 \ + resolver.3 res_querydomain.3 \ + resolver.3 res_search.3 \ + resolver.3 res_send.3 \ + resolver.3 res_setservers.3 \ + resolver.3 res_update.3 + +# IPv6 +MAN+= gai_strerror.3 getaddrinfo.3 getnameinfo.3 if_indextoname.3 \ + inet6_option_space.3 inet6_rthdr_space.3 \ + inet6_opt_init.3 inet6_rth_space.3 +MLINKS+=getaddrinfo.3 freeaddrinfo.3 \ + getaddrinfo.3 allocaddrinfo.3 \ + getifaddrs.3 freeifaddrs.3 \ + if_indextoname.3 if_nametoindex.3 if_indextoname.3 if_nameindex.3 \ + if_indextoname.3 if_freenameindex.3 \ + inet6_option_space.3 inet6_option_init.3 \ + inet6_option_space.3 inet6_option_append.3 \ + inet6_option_space.3 inet6_option_alloc.3 \ + inet6_option_space.3 inet6_option_next.3 \ + inet6_option_space.3 inet6_option_find.3 \ + inet6_rthdr_space.3 inet6_rthdr_init.3 \ + inet6_rthdr_space.3 inet6_rthdr_add.3 \ + inet6_rthdr_space.3 inet6_rthdr_lasthop.3 \ + inet6_rthdr_space.3 inet6_rthdr_reverse.3 \ + inet6_rthdr_space.3 inet6_rthdr_segments.3 \ + inet6_rthdr_space.3 inet6_rthdr_getaddr.3 \ + inet6_rthdr_space.3 inet6_rthdr_getflags.3 \ + inet6_opt_init.3 inet6_opt_append.3 \ + inet6_opt_init.3 inet6_opt_finish.3 \ + inet6_opt_init.3 inet6_opt_set_val.3 \ + inet6_opt_init.3 inet6_opt_next.3 \ + inet6_opt_init.3 inet6_opt_find.3 \ + inet6_opt_init.3 inet6_opt_get_val.3 \ + inet6_rth_space.3 inet6_rth_init.3 \ + inet6_rth_space.3 inet6_rth_add.3 \ + inet6_rth_space.3 inet6_rth_reverse.3 \ + inet6_rth_space.3 inet6_rth_segments.3 \ + inet6_rth_space.3 inet6_rth_getaddr.3 \ + rcmd.3 rcmd_af.3 rcmd.3 iruserok_sa.3 rcmd.3 rresvport_af.3 \ + rcmd.3 orcmd_af.3 diff --git a/lib/nbsd_libc/net/base64.c b/lib/nbsd_libc/net/base64.c new file mode 100644 index 000000000..1868f7657 --- /dev/null +++ b/lib/nbsd_libc/net/base64.c @@ -0,0 +1,343 @@ +/* $NetBSD: base64.c,v 1.12 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +/* + * Portions Copyright (c) 1995 by International Business Machines, Inc. + * + * International Business Machines, Inc. (hereinafter called IBM) grants + * permission under its copyrights to use, copy, modify, and distribute this + * Software with or without fee, provided that the above copyright notice and + * all paragraphs of this notice appear in all copies, and that the name of IBM + * not be used in connection with the marketing of any product incorporating + * the Software or modifications thereof, without specific, written prior + * permission. + * + * To the extent it has a right to do so, IBM grants an immunity from suit + * under its patents, if any, for the use, sale or manufacture of products to + * the extent that such products are used for performing Domain Name System + * dynamic updates in TCP/IP networks by means of the Software. No immunity is + * granted for any product per se or for any other function of any product. + * + * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, + * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN + * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: base64.c,v 1.4 2005/04/27 04:56:34 sra Exp"; +#else +__RCSID("$NetBSD: base64.c,v 1.12 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "port_after.h" + +#define Assert(Cond) if (!(Cond)) abort() + +static const char Base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Pad64 = '='; + +/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) + The following encoding technique is taken from RFC1521 by Borenstein + and Freed. It is reproduced here in a slightly edited form for + convenience. + + A 65-character subset of US-ASCII is used, enabling 6 bits to be + represented per printable character. (The extra 65th character, "=", + is used to signify a special processing function.) + + The encoding process represents 24-bit groups of input bits as output + strings of 4 encoded characters. Proceeding from left to right, a + 24-bit input group is formed by concatenating 3 8-bit input groups. + These 24 bits are then treated as 4 concatenated 6-bit groups, each + of which is translated into a single digit in the base64 alphabet. + + Each 6-bit group is used as an index into an array of 64 printable + characters. The character referenced by the index is placed in the + output string. + + Table 1: The Base64 Alphabet + + Value Encoding Value Encoding Value Encoding Value Encoding + 0 A 17 R 34 i 51 z + 1 B 18 S 35 j 52 0 + 2 C 19 T 36 k 53 1 + 3 D 20 U 37 l 54 2 + 4 E 21 V 38 m 55 3 + 5 F 22 W 39 n 56 4 + 6 G 23 X 40 o 57 5 + 7 H 24 Y 41 p 58 6 + 8 I 25 Z 42 q 59 7 + 9 J 26 a 43 r 60 8 + 10 K 27 b 44 s 61 9 + 11 L 28 c 45 t 62 + + 12 M 29 d 46 u 63 / + 13 N 30 e 47 v + 14 O 31 f 48 w (pad) = + 15 P 32 g 49 x + 16 Q 33 h 50 y + + Special processing is performed if fewer than 24 bits are available + at the end of the data being encoded. A full encoding quantum is + always completed at the end of a quantity. When fewer than 24 input + bits are available in an input group, zero bits are added (on the + right) to form an integral number of 6-bit groups. Padding at the + end of the data is performed using the '=' character. + + Since all base64 input is an integral number of octets, only the + ------------------------------------------------- + following cases can arise: + + (1) the final quantum of encoding input is an integral + multiple of 24 bits; here, the final unit of encoded + output will be an integral multiple of 4 characters + with no "=" padding, + (2) the final quantum of encoding input is exactly 8 bits; + here, the final unit of encoded output will be two + characters followed by two "=" padding characters, or + (3) the final quantum of encoding input is exactly 16 bits; + here, the final unit of encoded output will be three + characters followed by one "=" padding character. + */ + +int +b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) { + size_t datalength = 0; + u_char input[3]; + u_char output[4]; + size_t i; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(target != NULL); + + while (2U < srclength) { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + srclength -= 3; + + output[0] = (u_int32_t)input[0] >> 2; + output[1] = ((u_int32_t)(input[0] & 0x03) << 4) + + ((u_int32_t)input[1] >> 4); + output[2] = ((u_int32_t)(input[1] & 0x0f) << 2) + + ((u_int32_t)input[2] >> 6); + output[3] = input[2] & 0x3f; + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + Assert(output[3] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + target[datalength++] = Base64[output[2]]; + target[datalength++] = Base64[output[3]]; + } + + /* Now we worry about padding. */ + if (0U != srclength) { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for (i = 0; i < srclength; i++) + input[i] = *src++; + + output[0] = (u_int32_t)input[0] >> 2; + output[1] = ((u_int32_t)(input[0] & 0x03) << 4) + + ((u_int32_t)input[1] >> 4); + output[2] = ((u_int32_t)(input[1] & 0x0f) << 2) + + ((u_int32_t)input[2] >> 6); + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + if (srclength == 1U) + target[datalength++] = Pad64; + else + target[datalength++] = Base64[output[2]]; + target[datalength++] = Pad64; + } + if (datalength >= targsize) + return (-1); + target[datalength] = '\0'; /*%< Returned value doesn't count \\0. */ + return (datalength); +} + +/* skips all whitespace anywhere. + converts characters, four at a time, starting at (or after) + src from base - 64 numbers into three 8 bit bytes in the target area. + it returns the number of data bytes stored at the target, or -1 on error. + */ + +int +b64_pton(src, target, targsize) + char const *src; + u_char *target; + size_t targsize; +{ + size_t tarindex; + int state, ch; + char *pos; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(target != NULL); + + state = 0; + tarindex = 0; + + while ((ch = (u_char) *src++) != '\0') { + if (isspace(ch)) /*%< Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + pos = strchr(Base64, ch); + if (pos == 0) /*%< A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] = (pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= + (u_int32_t)(pos - Base64) >> 4; + target[tarindex+1] = ((pos - Base64) & 0x0f) + << 4 ; + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= + (u_int32_t)(pos - Base64) >> 2; + target[tarindex+1] = ((pos - Base64) & 0x03) + << 6; + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64); + } + tarindex++; + state = 0; + break; + default: + abort(); + } + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + + if (ch == Pad64) { /*%< We got a pad char. */ + ch = *src++; /*%< Skip it, get next. */ + switch (state) { + case 0: /*%< Invalid = in first position */ + case 1: /*%< Invalid = in second position */ + return (-1); + + case 2: /*%< Valid, means one byte of info */ + /* Skip any number of spaces. */ + for (; ch != '\0'; ch = (u_char) *src++) + if (!isspace(ch)) + break; + /* Make sure there is another trailing = sign. */ + if (ch != Pad64) + return (-1); + ch = *src++; /*%< Skip the = */ + /* Fall through to "single trailing =" case. */ + /* FALLTHROUGH */ + + case 3: /*%< Valid, means two bytes of info */ + /* + * We know this char is an =. Is there anything but + * whitespace after it? + */ + for (; ch != '\0'; ch = (u_char) *src++) + if (!isspace(ch)) + return (-1); + + /* + * Now make sure for cases 2 and 3 that the "extra" + * bits that slopped past the last full byte were + * zeros. If we don't check them, they become a + * subliminal channel. + */ + if (target && target[tarindex] != 0) + return (-1); + } + } else { + /* + * We ended by seeing the end of the string. Make sure we + * have no partial bytes lying around. + */ + if (state != 0) + return (-1); + } + + return (tarindex); +} + +/*! \file */ diff --git a/lib/nbsd_libc/net/byteorder.3 b/lib/nbsd_libc/net/byteorder.3 new file mode 100644 index 000000000..bc52e511a --- /dev/null +++ b/lib/nbsd_libc/net/byteorder.3 @@ -0,0 +1,91 @@ +.\" $NetBSD: byteorder.3,v 1.14 2006/02/04 22:47:28 uwe Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)byteorder.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 10, 2004 +.Dt BYTEORDER 3 +.Os +.Sh NAME +.Nm htonl , +.Nm htons , +.Nm ntohl , +.Nm ntohs +.Nd convert values between host and network byte order +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In arpa/inet.h +.Ft uint32_t +.Fn htonl "uint32_t host32" +.Ft uint16_t +.Fn htons "uint16_t host16" +.Ft uint32_t +.Fn ntohl "uint32_t net32" +.Ft uint16_t +.Fn ntohs "uint16_t net16" +.Sh DESCRIPTION +These routines convert 16 and 32 bit quantities between network +byte order and host byte order. +.Pp +On machines which have a byte order which is the same as the network +order, these routines are defined as macros that expand to the value of +their argument. +.Pp +These routines are most often used in conjunction with Internet +addresses and ports as returned by +.Xr gethostbyname 3 +and +.Xr getservent 3 . +.Sh SEE ALSO +.Xr gethostbyname 3 , +.Xr getservent 3 +.Sh STANDARDS +The +.Fn htonl , +.Fn htons , +.Fn ntohl , +and +.Fn ntohs +functions conform to +.St -p1003.1-2001 . +Their use of the fixed-width integer types +.Fa uint16_t +and +.Fa uint32_t +first appeared in +.St -xns5 . +.Sh HISTORY +The +.Nm byteorder +functions appeared in +.Bx 4.2 . +.Sh BUGS +The `l' and `s' suffixes in the names are not meaningful in machines +where long integers are not 32 bits. diff --git a/lib/nbsd_libc/net/ethers.3 b/lib/nbsd_libc/net/ethers.3 new file mode 100644 index 000000000..212ed1fde --- /dev/null +++ b/lib/nbsd_libc/net/ethers.3 @@ -0,0 +1,120 @@ +.\" $NetBSD: ethers.3,v 1.13 2003/04/16 13:34:41 wiz Exp $ +.\" +.\" Written by roland@frob.com. Public domain. +.\" +.Dd November 2, 1997 +.Dt ETHERS 3 +.Os +.Sh NAME +.Nm ether_ntoa , +.Nm ether_aton , +.Nm ether_ntohost , +.Nm ether_hostton , +.Nm ether_line , +.Nd get ethers entry +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/socket.h +.In net/if.h +.In net/if_ether.h +.Ft char * +.Fn ether_ntoa "const struct ether_addr *e" +.Ft struct ether_addr * +.Fn ether_aton "const char *s" +.Ft int +.Fn ether_ntohost "char *hostname" "const struct ether_addr *e" +.Ft int +.Fn ether_hostton "const char *hostname" "struct ether_addr *e" +.Ft int +.Fn ether_line "const char *line" "struct ether_addr *e" "char *hostname" +.Sh DESCRIPTION +Ethernet addresses are represented by the +following structure: +.Bd -literal -offset indent +struct ether_addr { + u_char ether_addr_octet[6]; +}; +.Ed +.Pp +The +.Fn ether_ntoa +function converts this structure into an ASCII string of the form +``xx:xx:xx:xx:xx:xx'', consisting of 6 hexadecimal numbers separated +by colons. It returns a pointer to a static buffer that is reused for +each call. +The +.Fn ether_aton +converts an ASCII string of the same form and to a structure +containing the 6 octets of the address. It returns a pointer to a +static structure that is reused for each call. +.Pp +The +.Fn ether_ntohost +and +.Fn ether_hostton +functions interrogate the data base mapping host names to Ethernet +addresses, +.Pa /etc/ethers . +The +.Fn ether_ntohost +function looks up the given Ethernet address and writes the associated +host name into the character buffer passed. +The +.Fn ether_hostton +function looks up the given host name and writes the associated +Ethernet address into the structure passed. Both functions return +zero if they find the requested host name or address, and -1 if not. +Each call reads +.Pa /etc/ethers +from the beginning; if a + appears alone on a line in the file, then +.Fn ether_hostton +will consult the +.Pa ethers.byname +YP map, and +.Fn ether_ntohost +will consult the +.Pa ethers.byaddr +YP map. +.Pp +The +.Fn ether_line +function parses a line from the +.Pa /etc/ethers +file and fills in the passed ``struct ether_addr'' and character +buffer with the Ethernet address and host name on the line. It +returns zero if the line was successfully parsed and -1 if not. +.Pp +The +.Fa hostname +buffer for +.Fn ether_line +and +.Fn ether_ntohost +should be at least +.Dv MAXHOSTNAMELEN ++ 1 +characters long, to prevent a buffer overflow during parsing. +.Sh FILES +.Bl -tag -width /etc/ethers -compact +.It Pa /etc/ethers +.El +.Sh SEE ALSO +.Xr ethers 5 +.Sh HISTORY +The +.Fn ether_ntoa , +.Fn ether_aton , +.Fn ether_ntohost , +.Fn ether_hostton , +and +.Fn ether_line +functions were adopted from +.Tn SunOS +and appeared in +.Nx 1.0 . +.Sh BUGS +The data space used by these functions is static; if future use +requires the data, it should be copied before any subsequent calls to +these functions overwrite it. diff --git a/lib/nbsd_libc/net/ethers.c b/lib/nbsd_libc/net/ethers.c new file mode 100644 index 000000000..25a582c09 --- /dev/null +++ b/lib/nbsd_libc/net/ethers.c @@ -0,0 +1,230 @@ +/* $NetBSD: ethers.c,v 1.21 2006/10/15 10:55:01 martin Exp $ */ + +/* + * ethers(3N) a la Sun. + * + * Written by Roland McGrath 10/14/93. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ethers.c,v 1.21 2006/10/15 10:55:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifdef YP +#include +#endif + +#ifdef __weak_alias +__weak_alias(ether_aton,_ether_aton) +__weak_alias(ether_hostton,_ether_hostton) +__weak_alias(ether_line,_ether_line) +__weak_alias(ether_ntoa,_ether_ntoa) +__weak_alias(ether_ntohost,_ether_ntohost) +#endif + +#ifndef _PATH_ETHERS +#define _PATH_ETHERS "/etc/ethers" +#endif + +char * +ether_ntoa(e) + const struct ether_addr *e; +{ + static char a[18]; + + _DIAGASSERT(e != NULL); + + (void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x", + e->ether_addr_octet[0], e->ether_addr_octet[1], + e->ether_addr_octet[2], e->ether_addr_octet[3], + e->ether_addr_octet[4], e->ether_addr_octet[5]); + return a; +} + +struct ether_addr * +ether_aton(s) + const char *s; +{ + static struct ether_addr n; + u_int i[6]; + + _DIAGASSERT(s != NULL); + + if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1], + &i[2], &i[3], &i[4], &i[5]) == 6) { + n.ether_addr_octet[0] = (u_char)i[0]; + n.ether_addr_octet[1] = (u_char)i[1]; + n.ether_addr_octet[2] = (u_char)i[2]; + n.ether_addr_octet[3] = (u_char)i[3]; + n.ether_addr_octet[4] = (u_char)i[4]; + n.ether_addr_octet[5] = (u_char)i[5]; + return &n; + } + return NULL; +} + +int +ether_ntohost(hostname, e) + char *hostname; + const struct ether_addr *e; +{ + FILE *f; + char *p; + size_t len; + struct ether_addr try; +#ifdef YP + char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; + int trylen; +#endif + + _DIAGASSERT(hostname != NULL); + _DIAGASSERT(e != NULL); + +#ifdef YP + trylen = snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x", + e->ether_addr_octet[0], e->ether_addr_octet[1], + e->ether_addr_octet[2], e->ether_addr_octet[3], + e->ether_addr_octet[4], e->ether_addr_octet[5]); +#endif + + f = fopen(_PATH_ETHERS, "r"); + if (f == NULL) + return -1; + while ((p = fgetln(f, &len)) != NULL) { + if (p[len - 1] != '\n') + continue; /* skip lines w/o \n */ + p[--len] = '\0'; +#ifdef YP + /* A + in the file means try YP now. */ + if (len == 1 && *p == '+') { + char *ypbuf, *ypdom; + int ypbuflen; + + if (yp_get_default_domain(&ypdom)) + continue; + if (yp_match(ypdom, "ethers.byaddr", trybuf, + trylen, &ypbuf, &ypbuflen)) + continue; + if (ether_line(ypbuf, &try, hostname) == 0) { + free(ypbuf); + (void)fclose(f); + return 0; + } + free(ypbuf); + continue; + } +#endif + if (ether_line(p, &try, hostname) == 0 && + memcmp(&try, e, sizeof try) == 0) { + (void)fclose(f); + return 0; + } + } + (void)fclose(f); + errno = ENOENT; + return -1; +} + +int +ether_hostton(hostname, e) + const char *hostname; + struct ether_addr *e; +{ + FILE *f; + char *p; + size_t len; + char try[MAXHOSTNAMELEN + 1]; +#ifdef YP + int hostlen = strlen(hostname); +#endif + + _DIAGASSERT(hostname != NULL); + _DIAGASSERT(e != NULL); + + f = fopen(_PATH_ETHERS, "r"); + if (f==NULL) + return -1; + + while ((p = fgetln(f, &len)) != NULL) { + if (p[len - 1] != '\n') + continue; /* skip lines w/o \n */ + p[--len] = '\0'; +#ifdef YP + /* A + in the file means try YP now. */ + if (len == 1 && *p == '+') { + char *ypbuf, *ypdom; + int ypbuflen; + + if (yp_get_default_domain(&ypdom)) + continue; + if (yp_match(ypdom, "ethers.byname", hostname, hostlen, + &ypbuf, &ypbuflen)) + continue; + if (ether_line(ypbuf, e, try) == 0) { + free(ypbuf); + (void)fclose(f); + return 0; + } + free(ypbuf); + continue; + } +#endif + if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) { + (void)fclose(f); + return 0; + } + } + (void)fclose(f); + errno = ENOENT; + return -1; +} + +int +ether_line(l, e, hostname) + const char *l; + struct ether_addr *e; + char *hostname; +{ + u_int i[6]; + +#define S2(arg) #arg +#define S1(arg) S2(arg) + static const char fmt[] = " %x:%x:%x:%x:%x:%x" + " %" S1(MAXHOSTNAMELEN) "s\n"; +#undef S2 +#undef S1 + + _DIAGASSERT(l != NULL); + _DIAGASSERT(e != NULL); + _DIAGASSERT(hostname != NULL); + + if (sscanf(l, fmt, + &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) { + e->ether_addr_octet[0] = (u_char)i[0]; + e->ether_addr_octet[1] = (u_char)i[1]; + e->ether_addr_octet[2] = (u_char)i[2]; + e->ether_addr_octet[3] = (u_char)i[3]; + e->ether_addr_octet[4] = (u_char)i[4]; + e->ether_addr_octet[5] = (u_char)i[5]; + return 0; + } + errno = EINVAL; + return -1; +} diff --git a/lib/nbsd_libc/net/gai_strerror.3 b/lib/nbsd_libc/net/gai_strerror.3 new file mode 100644 index 000000000..d3e905935 --- /dev/null +++ b/lib/nbsd_libc/net/gai_strerror.3 @@ -0,0 +1,96 @@ +.\" $NetBSD: gai_strerror.3,v 1.5 2010/03/22 19:30:54 joerg Exp $ +.\" $KAME: gai_strerror.3,v 1.1 2005/01/05 03:04:47 itojun Exp $ +.\" $OpenBSD: gai_strerror.3,v 1.4 2004/12/20 23:04:53 millert Exp $ +.\" +.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") +.\" Copyright (C) 2000, 2001 Internet Software Consortium. +.\" +.\" 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 ISC DISCLAIMS ALL WARRANTIES WITH +.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +.\" AND FITNESS. IN NO EVENT SHALL ISC 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 February 22, 2006 +.Dt GAI_STRERROR 3 +.Os +.Sh NAME +.Nm gai_strerror +.Nd get error message string from EAI_xxx error code +.Sh SYNOPSIS +.In sys/types.h +.In sys/socket.h +.In netdb.h +.Ft const char * +.Fn gai_strerror "int ecode" +.Sh DESCRIPTION +The +.Fn gai_strerror +function returns an error message string corresponding to the error code +returned by +.Xr getaddrinfo 3 +or +.Xr getnameinfo 3 . +.Pp +The following error codes and their meaning are defined in +.In netdb.h : +.Pp +.Bl -tag -width "EAI_ADDRFAMILYXX" -offset indent -compact +.It Dv EAI_ADDRFAMILY +address family for +.Fa hostname +not supported +.It Dv EAI_AGAIN +temporary failure in name resolution +.It Dv EAI_BADFLAGS +invalid value for +.Fa ai_flags +.It Dv EAI_BADHINTS +invalid value for +.Fa hints +.It Dv EAI_FAIL +non-recoverable failure in name resolution +.It Dv EAI_FAMILY +.Fa ai_family +not supported. +.It Dv EAI_MEMORY +memory allocation failure +.It Dv EAI_NODATA +no address associated with +.Fa hostname +.It Dv EAI_NONAME +.Fa hostname +or +.Fa servname +not provided, or not known +.It Dv EAI_OVERFLOW +argument buffer overflow +.It Dv EAI_PROTOCOL +resolved protocol is unknown +.It Dv EAI_SERVICE +.Fa servname +not supported for +.Fa ai_socktype +.It Dv EAI_SOCKTYPE +.Fa ai_socktype +not supported +.It Dv EAI_SYSTEM +system error returned in +.Va errno +.El +.Sh RETURN VALUES +.Fn gai_strerror +returns a pointer to the error message string corresponding to +.Fa ecode . +If +.Fa ecode +is out of range, an implementation-specific error message string is returned. +.Sh SEE ALSO +.Xr getaddrinfo 3 , +.Xr getnameinfo 3 diff --git a/lib/nbsd_libc/net/getaddrinfo.3 b/lib/nbsd_libc/net/getaddrinfo.3 new file mode 100644 index 000000000..ca0a90354 --- /dev/null +++ b/lib/nbsd_libc/net/getaddrinfo.3 @@ -0,0 +1,488 @@ +.\" $NetBSD: getaddrinfo.3,v 1.55 2010/04/17 20:28:47 wiz Exp $ +.\" $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $ +.\" $OpenBSD: getaddrinfo.3,v 1.35 2004/12/21 03:40:31 jaredy Exp $ +.\" +.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") +.\" Copyright (C) 2000, 2001 Internet Software Consortium. +.\" +.\" 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 ISC DISCLAIMS ALL WARRANTIES WITH +.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +.\" AND FITNESS. IN NO EVENT SHALL ISC 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 April 17, 2010 +.Dt GETADDRINFO 3 +.Os +.Sh NAME +.Nm getaddrinfo , +.Nm freeaddrinfo , +.Nm allocaddrinfo +.Nd host and service name to socket address structure +.Sh SYNOPSIS +.In netdb.h +.Ft int +.Fn getaddrinfo "const char * restrict hostname" \ + "const char * restrict servname" \ + "const struct addrinfo * restrict hints" "struct addrinfo ** restrict res" +.Ft void +.Fn freeaddrinfo "struct addrinfo *ai" +.Ft struct addrinfo * +.Fn allocaddrinfo "socklen_t len" +.Sh DESCRIPTION +The +.Fn getaddrinfo +function is used to get a list of +.Tn IP +addresses and port numbers for host +.Fa hostname +and service +.Fa servname . +It is a replacement for and provides more flexibility than the +.Xr gethostbyname 3 +and +.Xr getservbyname 3 +functions. +.Pp +The +.Fa hostname +and +.Fa servname +arguments are either pointers to NUL-terminated strings or the null pointer. +An acceptable value for +.Fa hostname +is either a valid host name or a numeric host address string consisting +of a dotted decimal IPv4 address or an IPv6 address. +The +.Fa servname +is either a decimal port number or a service name listed in +.Xr services 5 . +At least one of +.Fa hostname +and +.Fa servname +must be non-null. +.Pp +.Fa hints +is an optional pointer to a +.Li struct addrinfo , +as defined by +.In netdb.h : +.Bd -literal +struct addrinfo { + int ai_flags; /* input flags */ + int ai_family; /* address family for socket */ + int ai_socktype; /* socket type */ + int ai_protocol; /* protocol for socket */ + socklen_t ai_addrlen; /* length of socket-address */ + struct sockaddr *ai_addr; /* socket-address for socket */ + char *ai_canonname; /* canonical name for service location */ + struct addrinfo *ai_next; /* pointer to next in list */ +}; +.Ed +.Pp +This structure can be used to provide hints concerning the type of socket +that the caller supports or wishes to use. +The caller can supply the following structure elements in +.Fa hints : +.Bl -tag -width "ai_socktypeXX" +.It Fa ai_family +The address +.Pq and protocol +family that should be used. +When +.Fa ai_family +is set to +.Dv AF_UNSPEC , +it means the caller will accept any address family supported by the +operating system. +Note that while address families +.Pq Dv AF_* +and protocol families +.Pq Dv PF_* +are theoretically distinct, in practice the distinction has been lost. +.\" (.Dv !? Consistent with usage below though...) +.Dv "RFC 3493" +defines +.Fn getaddrinfo +in terms of the address family constants +.Dv AF_* +even though +.Fa ai_family +is to be passed as a protocol family to +.Xr socket 2 . +.It Fa ai_socktype +Denotes the type of socket that is wanted: +.Dv SOCK_STREAM , +.Dv SOCK_DGRAM , +or +.Dv SOCK_RAW . +When +.Fa ai_socktype +is zero the caller will accept any socket type. +.It Fa ai_protocol +Indicates which transport protocol is desired, +.Dv IPPROTO_UDP +or +.Dv IPPROTO_TCP . +If +.Fa ai_protocol +is zero the caller will accept any protocol. +.It Fa ai_flags +.Fa ai_flags +is formed by +.Tn OR Ns 'ing +the following values: +.Bl -tag -width "AI_CANONNAMEXX" +.It Dv AI_CANONNAME +If the +.Dv AI_CANONNAME +bit is set, a successful call to +.Fn getaddrinfo +will return a NUL-terminated string containing the canonical name +of the specified hostname in the +.Fa ai_canonname +element of the first +.Li addrinfo +structure returned. +.It Dv AI_NUMERICHOST +If the +.Dv AI_NUMERICHOST +bit is set, it indicates that +.Fa hostname +should be treated as a numeric string defining an IPv4 or IPv6 address +and no name resolution should be attempted. +.It Dv AI_NUMERICSERV +If the +.Dv AI_NUMERICSERV +bit is set, it indicates that the +.Fa servname +string contains a numeric port number. +This is used to prevent service name resolution. +.It Dv AI_PASSIVE +If the +.Dv AI_PASSIVE +bit is set it indicates that the returned socket address structure +is intended for use in a call to +.Xr bind 2 . +In this case, if the +.Fa hostname +argument is the null pointer, then the IP address portion of the +socket address structure will be set to +.Dv INADDR_ANY +for an IPv4 address or +.Dv IN6ADDR_ANY_INIT +for an IPv6 address. +.Pp +If the +.Dv AI_PASSIVE +bit is not set, the returned socket address structure will be ready +for use in a call to +.Xr connect 2 +for a connection-oriented protocol or +.Xr connect 2 , +.Xr sendto 2 , +or +.Xr sendmsg 2 +if a connectionless protocol was chosen. +The +.Tn IP +address portion of the socket address structure will be set to the +loopback address if +.Fa hostname +is the null pointer and +.Dv AI_PASSIVE +is not set. +.El +.El +.Pp +All other elements of the +.Li addrinfo +structure passed via +.Fa hints +must be zero or the null pointer. +.Pp +If +.Fa hints +is the null pointer, +.Fn getaddrinfo +behaves as if the caller provided a +.Li struct addrinfo +with +.Fa ai_family +set to +.Dv AF_UNSPEC +and all other elements set to zero or +.Dv NULL . +.Pp +After a successful call to +.Fn getaddrinfo , +.Fa *res +is a pointer to a linked list of one or more +.Li addrinfo +structures. +The list can be traversed by following the +.Fa ai_next +pointer in each +.Li addrinfo +structure until a null pointer is encountered. +The three members +.Fa ai_family , +.Fa ai_socktype , +and +.Fa ai_protocol +in each returned +.Li addrinfo +structure are suitable for a call to +.Xr socket 2 . +For each +.Li addrinfo +structure in the list, the +.Fa ai_addr +member points to a filled-in socket address structure of length +.Fa ai_addrlen . +.Pp +This implementation of +.Fn getaddrinfo +allows numeric IPv6 address notation with scope identifier, +as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt. +By appending the percent character and scope identifier to addresses, +one can fill the +.Li sin6_scope_id +field for addresses. +This would make management of scoped addresses easier +and allows cut-and-paste input of scoped addresses. +.Pp +At this moment the code supports only link-local addresses with the format. +The scope identifier is hardcoded to the name of the hardware interface +associated +with the link +.Po +such as +.Li ne0 +.Pc . +An example is +.Dq Li fe80::1%ne0 , +which means +.Do +.Li fe80::1 +on the link associated with the +.Li ne0 +interface +.Dc . +.Pp +The current implementation assumes a one-to-one relationship between +the interface and link, which is not necessarily true from the specification. +.Pp +All of the information returned by +.Fn getaddrinfo +is dynamically allocated: the +.Li addrinfo +structures themselves as well as the socket address structures and +the canonical host name strings included in the +.Li addrinfo +structures. +.Pp +Memory allocated for the dynamically allocated structures created by +a successful call to +.Fn getaddrinfo +is released by the +.Fn freeaddrinfo +function. +The +.Fa ai +pointer should be an +.Li addrinfo +structure created by a call to +.Fn getaddrinfo +or +.Fn allocaddrinfo . +The +.Fn allocaddrinfo +function is intended primarily for authors of +.Xr nsdispatch 3 +plugins implementing +.Fn getaddrinfo +backends. +.Fn allocaddrinfo +allocates a +.Li struct addrinfo +in a way that is compatible with being returned from +.Fn getaddrinfo +and being ultimately freed by +.Fn freeaddrinfo . +The returned structure is zeroed, except for the +.Fa ai_addr +field, which +will point to +.Fa len +bytes of memory for storage of a socket address. +It is safe to allocate memory separately for +.Fa ai_canonname +with +.Xr malloc 3 , +or in any other way that is compatible with deallocation by +.Xr free 3 . +.Sh RETURN VALUES +.Fn getaddrinfo +returns zero on success or one of the error codes listed in +.Xr gai_strerror 3 +if an error occurs. +.Sh EXAMPLES +The following code tries to connect to +.Dq Li www.kame.net +service +.Dq Li http +via a stream socket. +It loops through all the addresses available, regardless of address family. +If the destination resolves to an IPv4 address, it will use an +.Dv AF_INET +socket. +Similarly, if it resolves to IPv6, an +.Dv AF_INET6 +socket is used. +Observe that there is no hardcoded reference to a particular address family. +The code works even if +.Fn getaddrinfo +returns addresses that are not IPv4/v6. +.Bd -literal -offset indent +struct addrinfo hints, *res, *res0; +int error; +int s; +const char *cause = NULL; + +memset(\*[Am]hints, 0, sizeof(hints)); +hints.ai_family = AF_UNSPEC; +hints.ai_socktype = SOCK_STREAM; +error = getaddrinfo("www.kame.net", "http", \*[Am]hints, \*[Am]res0); +if (error) { + errx(1, "%s", gai_strerror(error)); + /*NOTREACHED*/ +} +s = -1; +for (res = res0; res; res = res-\*[Gt]ai_next) { + s = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype, + res-\*[Gt]ai_protocol); + if (s \*[Lt] 0) { + cause = "socket"; + continue; + } + + if (connect(s, res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 0) { + cause = "connect"; + close(s); + s = -1; + continue; + } + + break; /* okay we got one */ +} +if (s \*[Lt] 0) { + err(1, "%s", cause); + /*NOTREACHED*/ +} +freeaddrinfo(res0); +.Ed +.Pp +The following example tries to open a wildcard listening socket onto service +.Dq Li http , +for all the address families available. +.Bd -literal -offset indent +struct addrinfo hints, *res, *res0; +int error; +int s[MAXSOCK]; +int nsock; +const char *cause = NULL; + +memset(\*[Am]hints, 0, sizeof(hints)); +hints.ai_family = AF_UNSPEC; +hints.ai_socktype = SOCK_STREAM; +hints.ai_flags = AI_PASSIVE; +error = getaddrinfo(NULL, "http", \*[Am]hints, \*[Am]res0); +if (error) { + errx(1, "%s", gai_strerror(error)); + /*NOTREACHED*/ +} +nsock = 0; +for (res = res0; res \*[Am]\*[Am] nsock \*[Lt] MAXSOCK; res = res-\*[Gt]ai_next) { + s[nsock] = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype, + res-\*[Gt]ai_protocol); + if (s[nsock] \*[Lt] 0) { + cause = "socket"; + continue; + } + + if (bind(s[nsock], res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 0) { + cause = "bind"; + close(s[nsock]); + continue; + } + (void) listen(s[nsock], 5); + + nsock++; +} +if (nsock == 0) { + err(1, "%s", cause); + /*NOTREACHED*/ +} +freeaddrinfo(res0); +.Ed +.Sh SEE ALSO +.Xr bind 2 , +.Xr connect 2 , +.Xr send 2 , +.Xr socket 2 , +.Xr gai_strerror 3 , +.Xr gethostbyname 3 , +.Xr getnameinfo 3 , +.Xr getservbyname 3 , +.Xr resolver 3 , +.Xr hosts 5 , +.Xr resolv.conf 5 , +.Xr services 5 , +.Xr hostname 7 , +.Xr named 8 +.Rs +.%A R. Gilligan +.%A S. Thomson +.%A J. Bound +.%A J. McCann +.%A W. Stevens +.%T Basic Socket Interface Extensions for IPv6 +.%R RFC 3493 +.%D February 2003 +.Re +.Rs +.%A S. Deering +.%A B. Haberman +.%A T. Jinmei +.%A E. Nordmark +.%A B. Zill +.%T "IPv6 Scoped Address Architecture" +.%R internet draft +.%N draft-ietf-ipv6-scoping-arch-02.txt +.%O work in progress material +.Re +.Rs +.%A Craig Metz +.%T "Protocol Independence Using the Sockets API" +.%I USENIX Association +.%B Proceedings of the FREENIX Track: 2000 USENIX Annual Technical Conference +.%D June 18-23, 2000 +.%P 99-108 +.%U http://www.usenix.org/events/usenix2000/freenix/metzprotocol/metzprotocol.pdf +.Re +.Sh STANDARDS +The +.Fn getaddrinfo +function is defined by the +.St -p1003.1g-2000 +draft specification and documented in +.Dv "RFC 3493" , +.Dq Basic Socket Interface Extensions for IPv6 . diff --git a/lib/nbsd_libc/net/getaddrinfo.c b/lib/nbsd_libc/net/getaddrinfo.c new file mode 100644 index 000000000..41e64882b --- /dev/null +++ b/lib/nbsd_libc/net/getaddrinfo.c @@ -0,0 +1,1958 @@ +/* $NetBSD: getaddrinfo.c,v 1.95 2009/10/02 07:41:08 wiz Exp $ */ +/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ + +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * 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 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 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 PROJECT 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. + */ + +/* + * Issues to be discussed: + * - Return values. There are nonstandard return values defined and used + * in the source code. This is because RFC2553 is silent about which error + * code must be returned for which situation. + * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 + * says to use inet_aton() to convert IPv4 numeric to binary (alows + * classful form as a result). + * current code - disallow classful form for IPv4 (due to use of inet_pton). + * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is + * invalid. + * current code - SEGV on freeaddrinfo(NULL) + * Note: + * - The code filters out AFs that are not supported by the kernel, + * when globbing NULL hostname (to loopback, or wildcard). Is it the right + * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG + * in ai_flags? + * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. + * (1) what should we do against numeric hostname (2) what should we do + * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? + * non-loopback address configured? global address configured? + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getaddrinfo.c,v 1.95 2009/10/02 07:41:08 wiz Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifdef YP +#include +#include +#include +#endif + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getaddrinfo,_getaddrinfo) +__weak_alias(freeaddrinfo,_freeaddrinfo) +__weak_alias(gai_strerror,_gai_strerror) +#endif + +#define SUCCESS 0 +#define ANY 0 +#define YES 1 +#define NO 0 + +static const char in_addrany[] = { 0, 0, 0, 0 }; +static const char in_loopback[] = { 127, 0, 0, 1 }; +#ifdef INET6 +static const char in6_addrany[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; +static const char in6_loopback[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 +}; +#endif + +static const struct afd { + int a_af; + int a_addrlen; + int a_socklen; + int a_off; + const char *a_addrany; + const char *a_loopback; + int a_scoped; +} afdl [] = { +#ifdef INET6 + {PF_INET6, sizeof(struct in6_addr), + sizeof(struct sockaddr_in6), + offsetof(struct sockaddr_in6, sin6_addr), + in6_addrany, in6_loopback, 1}, +#endif + {PF_INET, sizeof(struct in_addr), + sizeof(struct sockaddr_in), + offsetof(struct sockaddr_in, sin_addr), + in_addrany, in_loopback, 0}, + {0, 0, 0, 0, NULL, NULL, 0}, +}; + +struct explore { + int e_af; + int e_socktype; + int e_protocol; + const char *e_protostr; + int e_wild; +#define WILD_AF(ex) ((ex)->e_wild & 0x01) +#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) +#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) +}; + +static const struct explore explore[] = { +#if 0 + { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, +#endif +#ifdef INET6 + { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, + { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, + { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, +#endif + { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, + { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, + { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, + { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, + { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, + { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, + { -1, 0, 0, NULL, 0 }, +}; + +#ifdef INET6 +#define PTON_MAX 16 +#else +#define PTON_MAX 4 +#endif + +static const ns_src default_dns_files[] = { + { NSSRC_FILES, NS_SUCCESS }, + { NSSRC_DNS, NS_SUCCESS }, + { 0, 0 } +}; + +#define MAXPACKET (64*1024) + +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +struct res_target { + struct res_target *next; + const char *name; /* domain name */ + int qclass, qtype; /* class and type of query */ + u_char *answer; /* buffer to put answer */ + int anslen; /* size of answer buffer */ + int n; /* result length */ +}; + +static int str2number(const char *); +static int explore_fqdn(const struct addrinfo *, const char *, + const char *, struct addrinfo **, struct servent_data *); +static int explore_null(const struct addrinfo *, + const char *, struct addrinfo **, struct servent_data *); +static int explore_numeric(const struct addrinfo *, const char *, + const char *, struct addrinfo **, const char *, struct servent_data *); +static int explore_numeric_scope(const struct addrinfo *, const char *, + const char *, struct addrinfo **, struct servent_data *); +static int get_canonname(const struct addrinfo *, + struct addrinfo *, const char *); +static struct addrinfo *get_ai(const struct addrinfo *, + const struct afd *, const char *); +static int get_portmatch(const struct addrinfo *, const char *, + struct servent_data *); +static int get_port(const struct addrinfo *, const char *, int, + struct servent_data *); +static const struct afd *find_afd(int); +#ifdef INET6 +static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); +#endif + +static struct addrinfo *getanswer(const querybuf *, int, const char *, int, + const struct addrinfo *); +static void aisort(struct addrinfo *s, res_state res); +static int _dns_getaddrinfo(void *, void *, va_list); +static void _sethtent(FILE **); +static void _endhtent(FILE **); +static struct addrinfo *_gethtent(FILE **, const char *, + const struct addrinfo *); +static int _files_getaddrinfo(void *, void *, va_list); +#ifdef YP +static struct addrinfo *_yphostent(char *, const struct addrinfo *); +static int _yp_getaddrinfo(void *, void *, va_list); +#endif + +static int res_queryN(const char *, struct res_target *, res_state); +static int res_searchN(const char *, struct res_target *, res_state); +static int res_querydomainN(const char *, const char *, + struct res_target *, res_state); + +static const char * const ai_errlist[] = { + "Success", + "Address family for hostname not supported", /* EAI_ADDRFAMILY */ + "Temporary failure in name resolution", /* EAI_AGAIN */ + "Invalid value for ai_flags", /* EAI_BADFLAGS */ + "Non-recoverable failure in name resolution", /* EAI_FAIL */ + "ai_family not supported", /* EAI_FAMILY */ + "Memory allocation failure", /* EAI_MEMORY */ + "No address associated with hostname", /* EAI_NODATA */ + "hostname nor servname provided, or not known", /* EAI_NONAME */ + "servname not supported for ai_socktype", /* EAI_SERVICE */ + "ai_socktype not supported", /* EAI_SOCKTYPE */ + "System error returned in errno", /* EAI_SYSTEM */ + "Invalid value for hints", /* EAI_BADHINTS */ + "Resolved protocol is unknown", /* EAI_PROTOCOL */ + "Argument buffer overflow", /* EAI_OVERFLOW */ + "Unknown error", /* EAI_MAX */ +}; + +/* XXX macros that make external reference is BAD. */ + +#define GET_AI(ai, afd, addr) \ +do { \ + /* external reference: pai, error, and label free */ \ + (ai) = get_ai(pai, (afd), (addr)); \ + if ((ai) == NULL) { \ + error = EAI_MEMORY; \ + goto free; \ + } \ +} while (/*CONSTCOND*/0) + +#define GET_PORT(ai, serv, svd) \ +do { \ + /* external reference: error and label free */ \ + error = get_port((ai), (serv), 0, (svd)); \ + if (error != 0) \ + goto free; \ +} while (/*CONSTCOND*/0) + +#define GET_CANONNAME(ai, str) \ +do { \ + /* external reference: pai, error and label free */ \ + error = get_canonname(pai, (ai), (str)); \ + if (error != 0) \ + goto free; \ +} while (/*CONSTCOND*/0) + +#define ERR(err) \ +do { \ + /* external reference: error, and label bad */ \ + error = (err); \ + goto bad; \ + /*NOTREACHED*/ \ +} while (/*CONSTCOND*/0) + +#define MATCH_FAMILY(x, y, w) \ + ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \ + (y) == PF_UNSPEC))) +#define MATCH(x, y, w) \ + ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) + +const char * +gai_strerror(int ecode) +{ + if (ecode < 0 || ecode > EAI_MAX) + ecode = EAI_MAX; + return ai_errlist[ecode]; +} + +void +freeaddrinfo(struct addrinfo *ai) +{ + struct addrinfo *next; + + _DIAGASSERT(ai != NULL); + + do { + next = ai->ai_next; + if (ai->ai_canonname) + free(ai->ai_canonname); + /* no need to free(ai->ai_addr) */ + free(ai); + ai = next; + } while (ai); +} + +static int +str2number(const char *p) +{ + char *ep; + unsigned long v; + + _DIAGASSERT(p != NULL); + + if (*p == '\0') + return -1; + ep = NULL; + errno = 0; + v = strtoul(p, &ep, 10); + if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) + return v; + else + return -1; +} + +int +getaddrinfo(const char *hostname, const char *servname, + const struct addrinfo *hints, struct addrinfo **res) +{ + struct addrinfo sentinel; + struct addrinfo *cur; + int error = 0; + struct addrinfo ai; + struct addrinfo ai0; + struct addrinfo *pai; + const struct explore *ex; + struct servent_data svd; + + /* hostname is allowed to be NULL */ + /* servname is allowed to be NULL */ + /* hints is allowed to be NULL */ + _DIAGASSERT(res != NULL); + + (void)memset(&svd, 0, sizeof(svd)); + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + memset(&ai, 0, sizeof(ai)); + pai = &ai; + pai->ai_flags = 0; + pai->ai_family = PF_UNSPEC; + pai->ai_socktype = ANY; + pai->ai_protocol = ANY; + pai->ai_addrlen = 0; + pai->ai_canonname = NULL; + pai->ai_addr = NULL; + pai->ai_next = NULL; + + if (hostname == NULL && servname == NULL) + return EAI_NONAME; + if (hints) { + /* error check for hints */ + if (hints->ai_addrlen || hints->ai_canonname || + hints->ai_addr || hints->ai_next) + ERR(EAI_BADHINTS); /* xxx */ + if (hints->ai_flags & ~AI_MASK) + ERR(EAI_BADFLAGS); + switch (hints->ai_family) { + case PF_UNSPEC: + case PF_INET: +#ifdef INET6 + case PF_INET6: +#endif + break; + default: + ERR(EAI_FAMILY); + } + memcpy(pai, hints, sizeof(*pai)); + + /* + * if both socktype/protocol are specified, check if they + * are meaningful combination. + */ + if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { + for (ex = explore; ex->e_af >= 0; ex++) { + if (pai->ai_family != ex->e_af) + continue; + if (ex->e_socktype == ANY) + continue; + if (ex->e_protocol == ANY) + continue; + if (pai->ai_socktype == ex->e_socktype + && pai->ai_protocol != ex->e_protocol) { + ERR(EAI_BADHINTS); + } + } + } + } + + /* + * check for special cases. (1) numeric servname is disallowed if + * socktype/protocol are left unspecified. (2) servname is disallowed + * for raw and other inet{,6} sockets. + */ + if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) +#ifdef PF_INET6 + || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) +#endif + ) { + ai0 = *pai; /* backup *pai */ + + if (pai->ai_family == PF_UNSPEC) { +#ifdef PF_INET6 + pai->ai_family = PF_INET6; +#else + pai->ai_family = PF_INET; +#endif + } + error = get_portmatch(pai, servname, &svd); + if (error) + ERR(error); + + *pai = ai0; + } + + ai0 = *pai; + + /* NULL hostname, or numeric hostname */ + for (ex = explore; ex->e_af >= 0; ex++) { + *pai = ai0; + + /* PF_UNSPEC entries are prepared for DNS queries only */ + if (ex->e_af == PF_UNSPEC) + continue; + + if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) + continue; + if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) + continue; + if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) + continue; + + if (pai->ai_family == PF_UNSPEC) + pai->ai_family = ex->e_af; + if (pai->ai_socktype == ANY && ex->e_socktype != ANY) + pai->ai_socktype = ex->e_socktype; + if (pai->ai_protocol == ANY && ex->e_protocol != ANY) + pai->ai_protocol = ex->e_protocol; + + if (hostname == NULL) + error = explore_null(pai, servname, &cur->ai_next, + &svd); + else + error = explore_numeric_scope(pai, hostname, servname, + &cur->ai_next, &svd); + + if (error) + goto free; + + while (cur->ai_next) + cur = cur->ai_next; + } + + /* + * XXX + * If numeric representation of AF1 can be interpreted as FQDN + * representation of AF2, we need to think again about the code below. + */ + if (sentinel.ai_next) + goto good; + + if (hostname == NULL) + ERR(EAI_NODATA); + if (pai->ai_flags & AI_NUMERICHOST) + ERR(EAI_NONAME); + + /* + * hostname as alphabetical name. + * we would like to prefer AF_INET6 than AF_INET, so we'll make a + * outer loop by AFs. + */ + for (ex = explore; ex->e_af >= 0; ex++) { + *pai = ai0; + + /* require exact match for family field */ + if (pai->ai_family != ex->e_af) + continue; + + if (!MATCH(pai->ai_socktype, ex->e_socktype, + WILD_SOCKTYPE(ex))) { + continue; + } + if (!MATCH(pai->ai_protocol, ex->e_protocol, + WILD_PROTOCOL(ex))) { + continue; + } + + if (pai->ai_socktype == ANY && ex->e_socktype != ANY) + pai->ai_socktype = ex->e_socktype; + if (pai->ai_protocol == ANY && ex->e_protocol != ANY) + pai->ai_protocol = ex->e_protocol; + + error = explore_fqdn(pai, hostname, servname, &cur->ai_next, + &svd); + + while (cur && cur->ai_next) + cur = cur->ai_next; + } + + /* XXX */ + if (sentinel.ai_next) + error = 0; + + if (error) + goto free; + + if (sentinel.ai_next) { + good: + endservent_r(&svd); + *res = sentinel.ai_next; + return SUCCESS; + } else + error = EAI_FAIL; + free: + bad: + endservent_r(&svd); + if (sentinel.ai_next) + freeaddrinfo(sentinel.ai_next); + *res = NULL; + return error; +} + +/* + * FQDN hostname, DNS lookup + */ +static int +explore_fqdn(const struct addrinfo *pai, const char *hostname, + const char *servname, struct addrinfo **res, struct servent_data *svd) +{ + struct addrinfo *result; + struct addrinfo *cur; + int error = 0; + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getaddrinfo, NULL) + { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_getaddrinfo, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(pai != NULL); + /* hostname may be NULL */ + /* servname may be NULL */ + _DIAGASSERT(res != NULL); + + result = NULL; + + /* + * if the servname does not match socktype/protocol, ignore it. + */ + if (get_portmatch(pai, servname, svd) != 0) + return 0; + + switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", + default_dns_files, hostname, pai)) { + case NS_TRYAGAIN: + error = EAI_AGAIN; + goto free; + case NS_UNAVAIL: + error = EAI_FAIL; + goto free; + case NS_NOTFOUND: + error = EAI_NODATA; + goto free; + case NS_SUCCESS: + error = 0; + for (cur = result; cur; cur = cur->ai_next) { + GET_PORT(cur, servname, svd); + /* canonname should be filled already */ + } + break; + } + + *res = result; + + return 0; + +free: + if (result) + freeaddrinfo(result); + return error; +} + +/* + * hostname == NULL. + * passive socket -> anyaddr (0.0.0.0 or ::) + * non-passive socket -> localhost (127.0.0.1 or ::1) + */ +static int +explore_null(const struct addrinfo *pai, const char *servname, + struct addrinfo **res, struct servent_data *svd) +{ + int s; + const struct afd *afd; + struct addrinfo *cur; + struct addrinfo sentinel; + int error; + + _DIAGASSERT(pai != NULL); + /* servname may be NULL */ + _DIAGASSERT(res != NULL); + + *res = NULL; + sentinel.ai_next = NULL; + cur = &sentinel; + + /* + * filter out AFs that are not supported by the kernel + * XXX errno? + */ + s = socket(pai->ai_family, SOCK_DGRAM, 0); + if (s < 0) { + if (errno != EMFILE) + return 0; + } else + close(s); + + /* + * if the servname does not match socktype/protocol, ignore it. + */ + if (get_portmatch(pai, servname, svd) != 0) + return 0; + + afd = find_afd(pai->ai_family); + if (afd == NULL) + return 0; + + if (pai->ai_flags & AI_PASSIVE) { + GET_AI(cur->ai_next, afd, afd->a_addrany); + /* xxx meaningless? + * GET_CANONNAME(cur->ai_next, "anyaddr"); + */ + GET_PORT(cur->ai_next, servname, svd); + } else { + GET_AI(cur->ai_next, afd, afd->a_loopback); + /* xxx meaningless? + * GET_CANONNAME(cur->ai_next, "localhost"); + */ + GET_PORT(cur->ai_next, servname, svd); + } + cur = cur->ai_next; + + *res = sentinel.ai_next; + return 0; + +free: + if (sentinel.ai_next) + freeaddrinfo(sentinel.ai_next); + return error; +} + +/* + * numeric hostname + */ +static int +explore_numeric(const struct addrinfo *pai, const char *hostname, + const char *servname, struct addrinfo **res, const char *canonname, + struct servent_data *svd) +{ + const struct afd *afd; + struct addrinfo *cur; + struct addrinfo sentinel; + int error; + char pton[PTON_MAX]; + + _DIAGASSERT(pai != NULL); + /* hostname may be NULL */ + /* servname may be NULL */ + _DIAGASSERT(res != NULL); + + *res = NULL; + sentinel.ai_next = NULL; + cur = &sentinel; + + /* + * if the servname does not match socktype/protocol, ignore it. + */ + if (get_portmatch(pai, servname, svd) != 0) + return 0; + + afd = find_afd(pai->ai_family); + if (afd == NULL) + return 0; + + switch (afd->a_af) { +#if 0 /*X/Open spec*/ + case AF_INET: + if (inet_aton(hostname, (struct in_addr *)pton) == 1) { + if (pai->ai_family == afd->a_af || + pai->ai_family == PF_UNSPEC /*?*/) { + GET_AI(cur->ai_next, afd, pton); + GET_PORT(cur->ai_next, servname, svd); + if ((pai->ai_flags & AI_CANONNAME)) { + /* + * Set the numeric address itself as + * the canonical name, based on a + * clarification in rfc2553bis-03. + */ + GET_CANONNAME(cur->ai_next, canonname); + } + while (cur && cur->ai_next) + cur = cur->ai_next; + } else + ERR(EAI_FAMILY); /*xxx*/ + } + break; +#endif + default: + if (inet_pton(afd->a_af, hostname, pton) == 1) { + if (pai->ai_family == afd->a_af || + pai->ai_family == PF_UNSPEC /*?*/) { + GET_AI(cur->ai_next, afd, pton); + GET_PORT(cur->ai_next, servname, svd); + if ((pai->ai_flags & AI_CANONNAME)) { + /* + * Set the numeric address itself as + * the canonical name, based on a + * clarification in rfc2553bis-03. + */ + GET_CANONNAME(cur->ai_next, canonname); + } + while (cur->ai_next) + cur = cur->ai_next; + } else + ERR(EAI_FAMILY); /*xxx*/ + } + break; + } + + *res = sentinel.ai_next; + return 0; + +free: +bad: + if (sentinel.ai_next) + freeaddrinfo(sentinel.ai_next); + return error; +} + +/* + * numeric hostname with scope + */ +static int +explore_numeric_scope(const struct addrinfo *pai, const char *hostname, + const char *servname, struct addrinfo **res, struct servent_data *svd) +{ +#if !defined(SCOPE_DELIMITER) || !defined(INET6) + return explore_numeric(pai, hostname, servname, res, hostname, svd); +#else + const struct afd *afd; + struct addrinfo *cur; + int error; + char *cp, *hostname2 = NULL, *scope, *addr; + struct sockaddr_in6 *sin6; + + _DIAGASSERT(pai != NULL); + /* hostname may be NULL */ + /* servname may be NULL */ + _DIAGASSERT(res != NULL); + + /* + * if the servname does not match socktype/protocol, ignore it. + */ + if (get_portmatch(pai, servname, svd) != 0) + return 0; + + afd = find_afd(pai->ai_family); + if (afd == NULL) + return 0; + + if (!afd->a_scoped) + return explore_numeric(pai, hostname, servname, res, hostname, + svd); + + cp = strchr(hostname, SCOPE_DELIMITER); + if (cp == NULL) + return explore_numeric(pai, hostname, servname, res, hostname, + svd); + + /* + * Handle special case of + */ + hostname2 = strdup(hostname); + if (hostname2 == NULL) + return EAI_MEMORY; + /* terminate at the delimiter */ + hostname2[cp - hostname] = '\0'; + addr = hostname2; + scope = cp + 1; + + error = explore_numeric(pai, addr, servname, res, hostname, svd); + if (error == 0) { + u_int32_t scopeid; + + for (cur = *res; cur; cur = cur->ai_next) { + if (cur->ai_family != AF_INET6) + continue; + sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; + if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { + free(hostname2); + return(EAI_NODATA); /* XXX: is return OK? */ + } + sin6->sin6_scope_id = scopeid; + } + } + + free(hostname2); + + return error; +#endif +} + +static int +get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str) +{ + + _DIAGASSERT(pai != NULL); + _DIAGASSERT(ai != NULL); + _DIAGASSERT(str != NULL); + + if ((pai->ai_flags & AI_CANONNAME) != 0) { + ai->ai_canonname = strdup(str); + if (ai->ai_canonname == NULL) + return EAI_MEMORY; + } + return 0; +} + +struct addrinfo * +allocaddrinfo(socklen_t addrlen) +{ + struct addrinfo *ai; + + ai = calloc(sizeof(struct addrinfo) + addrlen, 1); + if (ai) { + ai->ai_addr = (void *)(ai+1); + ai->ai_addrlen = ai->ai_addr->sa_len = addrlen; + } + + return ai; +} + +static struct addrinfo * +get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr) +{ + char *p; + struct addrinfo *ai; + struct sockaddr *save; + + _DIAGASSERT(pai != NULL); + _DIAGASSERT(afd != NULL); + _DIAGASSERT(addr != NULL); + + ai = allocaddrinfo((socklen_t)afd->a_socklen); + if (ai == NULL) + return NULL; + + save = ai->ai_addr; + memcpy(ai, pai, sizeof(struct addrinfo)); + + /* since we just overwrote all of ai, we have + to restore ai_addr and ai_addrlen */ + ai->ai_addr = save; + ai->ai_addrlen = (socklen_t)afd->a_socklen; + + ai->ai_addr->sa_family = ai->ai_family = afd->a_af; + p = (char *)(void *)(ai->ai_addr); + memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); + return ai; +} + +static int +get_portmatch(const struct addrinfo *ai, const char *servname, + struct servent_data *svd) +{ + + _DIAGASSERT(ai != NULL); + /* servname may be NULL */ + + return get_port(ai, servname, 1, svd); +} + +static int +get_port(const struct addrinfo *ai, const char *servname, int matchonly, + struct servent_data *svd) +{ + const char *proto; + struct servent *sp; + int port; + int allownumeric; + + _DIAGASSERT(ai != NULL); + /* servname may be NULL */ + + if (servname == NULL) + return 0; + switch (ai->ai_family) { + case AF_INET: +#ifdef AF_INET6 + case AF_INET6: +#endif + break; + default: + return 0; + } + + switch (ai->ai_socktype) { + case SOCK_RAW: + return EAI_SERVICE; + case SOCK_DGRAM: + case SOCK_STREAM: + allownumeric = 1; + break; + case ANY: + /* + * This was 0. It is now 1 so that queries specifying + * a NULL hint, or hint without socktype (but, hopefully, + * with protocol) and numeric address actually work. + */ + allownumeric = 1; + break; + default: + return EAI_SOCKTYPE; + } + + port = str2number(servname); + if (port >= 0) { + if (!allownumeric) + return EAI_SERVICE; + if (port < 0 || port > 65535) + return EAI_SERVICE; + port = htons(port); + } else { + struct servent sv; + if (ai->ai_flags & AI_NUMERICSERV) + return EAI_NONAME; + + switch (ai->ai_socktype) { + case SOCK_DGRAM: + proto = "udp"; + break; + case SOCK_STREAM: + proto = "tcp"; + break; + default: + proto = NULL; + break; + } + + sp = getservbyname_r(servname, proto, &sv, svd); + if (sp == NULL) + return EAI_SERVICE; + port = sp->s_port; + } + + if (!matchonly) { + switch (ai->ai_family) { + case AF_INET: + ((struct sockaddr_in *)(void *) + ai->ai_addr)->sin_port = port; + break; +#ifdef INET6 + case AF_INET6: + ((struct sockaddr_in6 *)(void *) + ai->ai_addr)->sin6_port = port; + break; +#endif + } + } + + return 0; +} + +static const struct afd * +find_afd(int af) +{ + const struct afd *afd; + + if (af == PF_UNSPEC) + return NULL; + for (afd = afdl; afd->a_af; afd++) { + if (afd->a_af == af) + return afd; + } + return NULL; +} + +#ifdef INET6 +/* convert a string to a scope identifier. XXX: IPv6 specific */ +static int +ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid) +{ + u_long lscopeid; + struct in6_addr *a6; + char *ep; + + _DIAGASSERT(scope != NULL); + _DIAGASSERT(sin6 != NULL); + _DIAGASSERT(scopeid != NULL); + + a6 = &sin6->sin6_addr; + + /* empty scopeid portion is invalid */ + if (*scope == '\0') + return -1; + + if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { + /* + * We currently assume a one-to-one mapping between links + * and interfaces, so we simply use interface indices for + * like-local scopes. + */ + *scopeid = if_nametoindex(scope); + if (*scopeid == 0) + goto trynumeric; + return 0; + } + + /* still unclear about literal, allow numeric only - placeholder */ + if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) + goto trynumeric; + if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) + goto trynumeric; + else + goto trynumeric; /* global */ + + /* try to convert to a numeric id as a last resort */ + trynumeric: + errno = 0; + lscopeid = strtoul(scope, &ep, 10); + *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); + if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) + return 0; + else + return -1; +} +#endif + +/* code duplicate with gethnamaddr.c */ + +static const char AskedForGot[] = + "gethostby*.getanswer: asked for \"%s\", got \"%s\""; + +static struct addrinfo * +getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, + const struct addrinfo *pai) +{ + struct addrinfo sentinel, *cur; + struct addrinfo ai; + const struct afd *afd; + char *canonname; + const HEADER *hp; + const u_char *cp; + int n; + const u_char *eom; + char *bp, *ep; + int type, class, ancount, qdcount; + int haveanswer, had_error; + char tbuf[MAXDNAME]; + int (*name_ok) (const char *); + char hostbuf[8*1024]; + + _DIAGASSERT(answer != NULL); + _DIAGASSERT(qname != NULL); + _DIAGASSERT(pai != NULL); + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + canonname = NULL; + eom = answer->buf + anslen; + switch (qtype) { + case T_A: + case T_AAAA: + case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ + name_ok = res_hnok; + break; + default: + return NULL; /* XXX should be abort(); */ + } + /* + * find first satisfactory answer + */ + hp = &answer->hdr; + ancount = ntohs(hp->ancount); + qdcount = ntohs(hp->qdcount); + bp = hostbuf; + ep = hostbuf + sizeof hostbuf; + cp = answer->buf + HFIXEDSZ; + if (qdcount != 1) { + h_errno = NO_RECOVERY; + return (NULL); + } + n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + if ((n < 0) || !(*name_ok)(bp)) { + h_errno = NO_RECOVERY; + return (NULL); + } + cp += n + QFIXEDSZ; + if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { + /* res_send() has already verified that the query name is the + * same as the one we sent; this just gets the expanded name + * (i.e., with the succeeding search-domain tacked on). + */ + n = strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + h_errno = NO_RECOVERY; + return (NULL); + } + canonname = bp; + bp += n; + /* The qname can be abbreviated, but h_name is now absolute. */ + qname = canonname; + } + haveanswer = 0; + had_error = 0; + while (ancount-- > 0 && cp < eom && !had_error) { + n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + if ((n < 0) || !(*name_ok)(bp)) { + had_error++; + continue; + } + cp += n; /* name */ + type = _getshort(cp); + cp += INT16SZ; /* type */ + class = _getshort(cp); + cp += INT16SZ + INT32SZ; /* class, TTL */ + n = _getshort(cp); + cp += INT16SZ; /* len */ + if (class != C_IN) { + /* XXX - debug? syslog? */ + cp += n; + continue; /* XXX - had_error++ ? */ + } + if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && + type == T_CNAME) { + n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); + if ((n < 0) || !(*name_ok)(tbuf)) { + had_error++; + continue; + } + cp += n; + /* Get canonical name. */ + n = strlen(tbuf) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + strlcpy(bp, tbuf, (size_t)(ep - bp)); + canonname = bp; + bp += n; + continue; + } + if (qtype == T_ANY) { + if (!(type == T_A || type == T_AAAA)) { + cp += n; + continue; + } + } else if (type != qtype) { + if (type != T_KEY && type != T_SIG) { + struct syslog_data sd = SYSLOG_DATA_INIT; + syslog_r(LOG_NOTICE|LOG_AUTH, &sd, + "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", + qname, p_class(C_IN), p_type(qtype), + p_type(type)); + } + cp += n; + continue; /* XXX - had_error++ ? */ + } + switch (type) { + case T_A: + case T_AAAA: + if (strcasecmp(canonname, bp) != 0) { + struct syslog_data sd = SYSLOG_DATA_INIT; + syslog_r(LOG_NOTICE|LOG_AUTH, &sd, + AskedForGot, canonname, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + if (type == T_A && n != INADDRSZ) { + cp += n; + continue; + } + if (type == T_AAAA && n != IN6ADDRSZ) { + cp += n; + continue; + } + if (type == T_AAAA) { + struct in6_addr in6; + memcpy(&in6, cp, IN6ADDRSZ); + if (IN6_IS_ADDR_V4MAPPED(&in6)) { + cp += n; + continue; + } + } + if (!haveanswer) { + int nn; + + canonname = bp; + nn = strlen(bp) + 1; /* for the \0 */ + bp += nn; + } + + /* don't overwrite pai */ + ai = *pai; + ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; + afd = find_afd(ai.ai_family); + if (afd == NULL) { + cp += n; + continue; + } + cur->ai_next = get_ai(&ai, afd, (const char *)cp); + if (cur->ai_next == NULL) + had_error++; + while (cur && cur->ai_next) + cur = cur->ai_next; + cp += n; + break; + default: + abort(); + } + if (!had_error) + haveanswer++; + } + if (haveanswer) { + if (!canonname) + (void)get_canonname(pai, sentinel.ai_next, qname); + else + (void)get_canonname(pai, sentinel.ai_next, canonname); + h_errno = NETDB_SUCCESS; + return sentinel.ai_next; + } + + h_errno = NO_RECOVERY; + return NULL; +} + +#define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr) +#define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr) + +static void +aisort(struct addrinfo *s, res_state res) +{ + struct addrinfo head, *t, *p; + int i; + + head.ai_next = NULL; + t = &head; + + for (i = 0; i < res->nsort; i++) { + p = s; + while (p->ai_next) { + if ((p->ai_next->ai_family != AF_INET) + || SORTMATCH(p, res->sort_list[i])) { + t->ai_next = p->ai_next; + t = t->ai_next; + p->ai_next = p->ai_next->ai_next; + } else { + p = p->ai_next; + } + } + } + + /* add rest of list and reset s to the new list*/ + t->ai_next = s->ai_next; + s->ai_next = head.ai_next; +} + +/*ARGSUSED*/ +static int +_dns_getaddrinfo(void *rv, void *cb_data, va_list ap) +{ + struct addrinfo *ai; + querybuf *buf, *buf2; + const char *name; + const struct addrinfo *pai; + struct addrinfo sentinel, *cur; + struct res_target q, q2; + res_state res; + + name = va_arg(ap, char *); + pai = va_arg(ap, const struct addrinfo *); + + memset(&q, 0, sizeof(q)); + memset(&q2, 0, sizeof(q2)); + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + buf2 = malloc(sizeof(*buf2)); + if (buf2 == NULL) { + free(buf); + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + + switch (pai->ai_family) { + case AF_UNSPEC: + /* prefer IPv6 */ + q.name = name; + q.qclass = C_IN; + q.qtype = T_AAAA; + q.answer = buf->buf; + q.anslen = sizeof(buf->buf); + q.next = &q2; + q2.name = name; + q2.qclass = C_IN; + q2.qtype = T_A; + q2.answer = buf2->buf; + q2.anslen = sizeof(buf2->buf); + break; + case AF_INET: + q.name = name; + q.qclass = C_IN; + q.qtype = T_A; + q.answer = buf->buf; + q.anslen = sizeof(buf->buf); + break; + case AF_INET6: + q.name = name; + q.qclass = C_IN; + q.qtype = T_AAAA; + q.answer = buf->buf; + q.anslen = sizeof(buf->buf); + break; + default: + free(buf); + free(buf2); + return NS_UNAVAIL; + } + + res = __res_get_state(); + if (res == NULL) { + free(buf); + free(buf2); + return NS_NOTFOUND; + } + + if (res_searchN(name, &q, res) < 0) { + __res_put_state(res); + free(buf); + free(buf2); + return NS_NOTFOUND; + } + ai = getanswer(buf, q.n, q.name, q.qtype, pai); + if (ai) { + cur->ai_next = ai; + while (cur && cur->ai_next) + cur = cur->ai_next; + } + if (q.next) { + ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); + if (ai) + cur->ai_next = ai; + } + free(buf); + free(buf2); + if (sentinel.ai_next == NULL) { + __res_put_state(res); + switch (h_errno) { + case HOST_NOT_FOUND: + return NS_NOTFOUND; + case TRY_AGAIN: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + + if (res->nsort) + aisort(&sentinel, res); + + __res_put_state(res); + + *((struct addrinfo **)rv) = sentinel.ai_next; + return NS_SUCCESS; +} + +static void +_sethtent(FILE **hostf) +{ + + if (!*hostf) + *hostf = fopen(_PATH_HOSTS, "r" ); + else + rewind(*hostf); +} + +static void +_endhtent(FILE **hostf) +{ + + if (*hostf) { + (void) fclose(*hostf); + *hostf = NULL; + } +} + +static struct addrinfo * +_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai) +{ + char *p; + char *cp, *tname, *cname; + struct addrinfo hints, *res0, *res; + int error; + const char *addr; + char hostbuf[8*1024]; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(pai != NULL); + + if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r" ))) + return (NULL); + again: + if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) + return (NULL); + if (*p == '#') + goto again; + if (!(cp = strpbrk(p, "#\n"))) + goto again; + *cp = '\0'; + if (!(cp = strpbrk(p, " \t"))) + goto again; + *cp++ = '\0'; + addr = p; + /* if this is not something we're looking for, skip it. */ + cname = NULL; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (!cname) + cname = cp; + tname = cp; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + if (strcasecmp(name, tname) == 0) + goto found; + } + goto again; + +found: + hints = *pai; + hints.ai_flags = AI_NUMERICHOST; + error = getaddrinfo(addr, NULL, &hints, &res0); + if (error) + goto again; + for (res = res0; res; res = res->ai_next) { + /* cover it up */ + res->ai_flags = pai->ai_flags; + + if (pai->ai_flags & AI_CANONNAME) { + if (get_canonname(pai, res, cname) != 0) { + freeaddrinfo(res0); + goto again; + } + } + } + return res0; +} + +/*ARGSUSED*/ +static int +_files_getaddrinfo(void *rv, void *cb_data, va_list ap) +{ + const char *name; + const struct addrinfo *pai; + struct addrinfo sentinel, *cur; + struct addrinfo *p; +#ifndef _REENTRANT + static +#endif + FILE *hostf = NULL; + + name = va_arg(ap, char *); + pai = va_arg(ap, const struct addrinfo *); + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + _sethtent(&hostf); + while ((p = _gethtent(&hostf, name, pai)) != NULL) { + cur->ai_next = p; + while (cur && cur->ai_next) + cur = cur->ai_next; + } + _endhtent(&hostf); + + *((struct addrinfo **)rv) = sentinel.ai_next; + if (sentinel.ai_next == NULL) + return NS_NOTFOUND; + return NS_SUCCESS; +} + +#ifdef YP +/*ARGSUSED*/ +static struct addrinfo * +_yphostent(char *line, const struct addrinfo *pai) +{ + struct addrinfo sentinel, *cur; + struct addrinfo hints, *res, *res0; + int error; + char *p; + const char *addr, *canonname; + char *nextline; + char *cp; + + _DIAGASSERT(line != NULL); + _DIAGASSERT(pai != NULL); + + p = line; + addr = canonname = NULL; + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + +nextline: + /* terminate line */ + cp = strchr(p, '\n'); + if (cp) { + *cp++ = '\0'; + nextline = cp; + } else + nextline = NULL; + + cp = strpbrk(p, " \t"); + if (cp == NULL) { + if (canonname == NULL) + return (NULL); + else + goto done; + } + *cp++ = '\0'; + + addr = p; + + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (!canonname) + canonname = cp; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + } + + hints = *pai; + hints.ai_flags = AI_NUMERICHOST; + error = getaddrinfo(addr, NULL, &hints, &res0); + if (error == 0) { + for (res = res0; res; res = res->ai_next) { + /* cover it up */ + res->ai_flags = pai->ai_flags; + + if (pai->ai_flags & AI_CANONNAME) + (void)get_canonname(pai, res, canonname); + } + } else + res0 = NULL; + if (res0) { + cur->ai_next = res0; + while (cur->ai_next) + cur = cur->ai_next; + } + + if (nextline) { + p = nextline; + goto nextline; + } + +done: + return sentinel.ai_next; +} + +/*ARGSUSED*/ +static int +_yp_getaddrinfo(void *rv, void *cb_data, va_list ap) +{ + struct addrinfo sentinel, *cur; + struct addrinfo *ai = NULL; + char *ypbuf; + int ypbuflen, r; + const char *name; + const struct addrinfo *pai; + char *ypdomain; + + if (_yp_check(&ypdomain) == 0) + return NS_UNAVAIL; + + name = va_arg(ap, char *); + pai = va_arg(ap, const struct addrinfo *); + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + /* hosts.byname is only for IPv4 (Solaris8) */ + if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { + r = yp_match(ypdomain, "hosts.byname", name, + (int)strlen(name), &ypbuf, &ypbuflen); + if (r == 0) { + struct addrinfo ai4; + + ai4 = *pai; + ai4.ai_family = AF_INET; + ai = _yphostent(ypbuf, &ai4); + if (ai) { + cur->ai_next = ai; + while (cur && cur->ai_next) + cur = cur->ai_next; + } + } + free(ypbuf); + } + + /* ipnodes.byname can hold both IPv4/v6 */ + r = yp_match(ypdomain, "ipnodes.byname", name, + (int)strlen(name), &ypbuf, &ypbuflen); + if (r == 0) { + ai = _yphostent(ypbuf, pai); + if (ai) + cur->ai_next = ai; + free(ypbuf); + } + + if (sentinel.ai_next == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + *((struct addrinfo **)rv) = sentinel.ai_next; + return NS_SUCCESS; +} +#endif + +/* resolver logic */ + +/* + * Formulate a normal query, send, and await answer. + * Returned answer is placed in supplied buffer "answer". + * Perform preliminary check of answer, returning success only + * if no error is indicated and the answer count is nonzero. + * Return the size of the response on success, -1 on error. + * Error number is left in h_errno. + * + * Caller must parse answer and determine whether it answers the question. + */ +static int +res_queryN(const char *name, /* domain name */ struct res_target *target, + res_state res) +{ + u_char buf[MAXPACKET]; + HEADER *hp; + int n; + struct res_target *t; + int rcode; + int ancount; + + _DIAGASSERT(name != NULL); + /* XXX: target may be NULL??? */ + + rcode = NOERROR; + ancount = 0; + + for (t = target; t; t = t->next) { + int class, type; + u_char *answer; + int anslen; + + hp = (HEADER *)(void *)t->answer; + hp->rcode = NOERROR; /* default */ + + /* make it easier... */ + class = t->qclass; + type = t->qtype; + answer = t->answer; + anslen = t->anslen; +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_nquery(%s, %d, %d)\n", name, class, type); +#endif + + n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, + buf, sizeof(buf)); +#ifdef RES_USE_EDNS0 + if (n > 0 && (res->options & RES_USE_EDNS0) != 0) + n = res_nopt(res, n, buf, sizeof(buf), anslen); +#endif + if (n <= 0) { +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_nquery: mkquery failed\n"); +#endif + h_errno = NO_RECOVERY; + return n; + } + n = res_nsend(res, buf, n, answer, anslen); +#if 0 + if (n < 0) { +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_query: send error\n"); +#endif + h_errno = TRY_AGAIN; + return n; + } +#endif + + if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { + rcode = hp->rcode; /* record most recent error */ +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; rcode = %u, ancount=%u\n", hp->rcode, + ntohs(hp->ancount)); +#endif + continue; + } + + ancount += ntohs(hp->ancount); + + t->n = n; + } + + if (ancount == 0) { + switch (rcode) { + case NXDOMAIN: + h_errno = HOST_NOT_FOUND; + break; + case SERVFAIL: + h_errno = TRY_AGAIN; + break; + case NOERROR: + h_errno = NO_DATA; + break; + case FORMERR: + case NOTIMP: + case REFUSED: + default: + h_errno = NO_RECOVERY; + break; + } + return -1; + } + return ancount; +} + +/* + * Formulate a normal query, send, and retrieve answer in supplied buffer. + * Return the size of the response on success, -1 on error. + * If enabled, implement search rules until answer or unrecoverable failure + * is detected. Error code, if any, is left in h_errno. + */ +static int +res_searchN(const char *name, struct res_target *target, res_state res) +{ + const char *cp, * const *domain; + HEADER *hp; + u_int dots; + int trailing_dot, ret, saved_herrno; + int got_nodata = 0, got_servfail = 0, tried_as_is = 0; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(target != NULL); + + hp = (HEADER *)(void *)target->answer; /*XXX*/ + + errno = 0; + h_errno = HOST_NOT_FOUND; /* default, if we never query */ + dots = 0; + for (cp = name; *cp; cp++) + dots += (*cp == '.'); + trailing_dot = 0; + if (cp > name && *--cp == '.') + trailing_dot++; + + /* + * if there aren't any dots, it could be a user-level alias + */ + if (!dots && (cp = __hostalias(name)) != NULL) { + ret = res_queryN(cp, target, res); + return ret; + } + + /* + * If there are dots in the name already, let's just give it a try + * 'as is'. The threshold can be set with the "ndots" option. + */ + saved_herrno = -1; + if (dots >= res->ndots) { + ret = res_querydomainN(name, NULL, target, res); + if (ret > 0) + return (ret); + saved_herrno = h_errno; + tried_as_is++; + } + + /* + * We do at least one level of search if + * - there is no dot and RES_DEFNAME is set, or + * - there is at least one dot, there is no trailing dot, + * and RES_DNSRCH is set. + */ + if ((!dots && (res->options & RES_DEFNAMES)) || + (dots && !trailing_dot && (res->options & RES_DNSRCH))) { + int done = 0; + + for (domain = (const char * const *)res->dnsrch; + *domain && !done; + domain++) { + + ret = res_querydomainN(name, *domain, target, res); + if (ret > 0) + return ret; + + /* + * If no server present, give up. + * If name isn't found in this domain, + * keep trying higher domains in the search list + * (if that's enabled). + * On a NO_DATA error, keep trying, otherwise + * a wildcard entry of another type could keep us + * from finding this entry higher in the domain. + * If we get some other error (negative answer or + * server failure), then stop searching up, + * but try the input name below in case it's + * fully-qualified. + */ + if (errno == ECONNREFUSED) { + h_errno = TRY_AGAIN; + return -1; + } + + switch (h_errno) { + case NO_DATA: + got_nodata++; + /* FALLTHROUGH */ + case HOST_NOT_FOUND: + /* keep trying */ + break; + case TRY_AGAIN: + if (hp->rcode == SERVFAIL) { + /* try next search element, if any */ + got_servfail++; + break; + } + /* FALLTHROUGH */ + default: + /* anything else implies that we're done */ + done++; + } + /* + * if we got here for some reason other than DNSRCH, + * we only wanted one iteration of the loop, so stop. + */ + if (!(res->options & RES_DNSRCH)) + done++; + } + } + + /* + * if we have not already tried the name "as is", do that now. + * note that we do this regardless of how many dots were in the + * name or whether it ends with a dot. + */ + if (!tried_as_is) { + ret = res_querydomainN(name, NULL, target, res); + if (ret > 0) + return ret; + } + + /* + * if we got here, we didn't satisfy the search. + * if we did an initial full query, return that query's h_errno + * (note that we wouldn't be here if that query had succeeded). + * else if we ever got a nodata, send that back as the reason. + * else send back meaningless h_errno, that being the one from + * the last DNSRCH we did. + */ + if (saved_herrno != -1) + h_errno = saved_herrno; + else if (got_nodata) + h_errno = NO_DATA; + else if (got_servfail) + h_errno = TRY_AGAIN; + return -1; +} + +/* + * Perform a call on res_query on the concatenation of name and domain, + * removing a trailing dot from name if domain is NULL. + */ +static int +res_querydomainN(const char *name, const char *domain, + struct res_target *target, res_state res) +{ + char nbuf[MAXDNAME]; + const char *longname = nbuf; + size_t n, d; + + _DIAGASSERT(name != NULL); + /* XXX: target may be NULL??? */ + +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_querydomain(%s, %s)\n", + name, domain?domain:""); +#endif + if (domain == NULL) { + /* + * Check for trailing '.'; + * copy without '.' if present. + */ + n = strlen(name); + if (n + 1 > sizeof(nbuf)) { + h_errno = NO_RECOVERY; + return -1; + } + if (n > 0 && name[--n] == '.') { + strncpy(nbuf, name, n); + nbuf[n] = '\0'; + } else + longname = name; + } else { + n = strlen(name); + d = strlen(domain); + if (n + 1 + d + 1 > sizeof(nbuf)) { + h_errno = NO_RECOVERY; + return -1; + } + snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); + } + return res_queryN(longname, target, res); +} diff --git a/lib/nbsd_libc/net/gethnamaddr.c b/lib/nbsd_libc/net/gethnamaddr.c new file mode 100644 index 000000000..1c3a68d24 --- /dev/null +++ b/lib/nbsd_libc/net/gethnamaddr.c @@ -0,0 +1,1441 @@ +/* $NetBSD: gethnamaddr.c,v 1.76 2010/08/29 15:40:35 christos Exp $ */ + +/* + * ++Copyright++ 1985, 1988, 1993 + * - + * Copyright (c) 1985, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * - + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * 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, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION 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. + * - + * --Copyright-- + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp "; +#else +__RCSID("$NetBSD: gethnamaddr.c,v 1.76 2010/08/29 15:40:35 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if defined(_LIBC) +#include "namespace.h" +#endif +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef LOG_AUTH +# define LOG_AUTH 0 +#endif + +#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ + +#include +#include +#include + +#ifdef YP +#include +#include +#include +#endif + +#if defined(_LIBC) && defined(__weak_alias) +__weak_alias(gethostbyaddr,_gethostbyaddr) +__weak_alias(gethostbyname,_gethostbyname) +__weak_alias(gethostent,_gethostent) +#endif + +#define MAXALIASES 35 +#define MAXADDRS 35 + +static const char AskedForGot[] = + "gethostby*.getanswer: asked for \"%s\", got \"%s\""; + +static char *h_addr_ptrs[MAXADDRS + 1]; + +#ifdef YP +static char *__ypdomain; +#endif + +static struct hostent host; +static char *host_aliases[MAXALIASES]; +static char hostbuf[8*1024]; +static u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */ +static FILE *hostf = NULL; +static int stayopen = 0; + +#define MAXPACKET (64*1024) + +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +typedef union { + int32_t al; + char ac; +} align; + +#ifdef DEBUG +static void debugprintf(const char *, res_state, ...) + __attribute__((__format__(__printf__, 1, 3))); +#endif +static struct hostent *getanswer(const querybuf *, int, const char *, int, + res_state); +static void map_v4v6_address(const char *, char *); +static void map_v4v6_hostent(struct hostent *, char **, char *); +static void addrsort(char **, int, res_state); + +void _sethtent(int); +void _endhtent(void); +struct hostent *_gethtent(void); +void ht_sethostent(int); +void ht_endhostent(void); +struct hostent *ht_gethostbyname(char *); +struct hostent *ht_gethostbyaddr(const char *, int, int); +void dns_service(void); +#undef dn_skipname +int dn_skipname(const u_char *, const u_char *); +int _gethtbyaddr(void *, void *, va_list); +int _gethtbyname(void *, void *, va_list); +struct hostent *_gethtbyname2(const char *, int); +int _dns_gethtbyaddr(void *, void *, va_list); +int _dns_gethtbyname(void *, void *, va_list); +#ifdef YP +struct hostent *_yphostent(char *, int); +int _yp_gethtbyaddr(void *, void *, va_list); +int _yp_gethtbyname(void *, void *, va_list); +#endif + +static struct hostent *gethostbyname_internal(const char *, int, res_state); + +static const ns_src default_dns_files[] = { + { NSSRC_FILES, NS_SUCCESS }, + { NSSRC_DNS, NS_SUCCESS }, + { 0, 0 } +}; + + +#ifdef DEBUG +static void +debugprintf(const char *msg, res_state res, ...) +{ + _DIAGASSERT(msg != NULL); + + if (res->options & RES_DEBUG) { + int save = errno; + va_list ap; + + va_start (ap, res); + vprintf(msg, ap); + va_end (ap); + + errno = save; + } +} +#else +# define debugprintf(msg, res, num) /*nada*/ +#endif + +#define BOUNDED_INCR(x) \ + do { \ + cp += (x); \ + if (cp > eom) { \ + h_errno = NO_RECOVERY; \ + return NULL; \ + } \ + } while (/*CONSTCOND*/0) + +#define BOUNDS_CHECK(ptr, count) \ + do { \ + if ((ptr) + (count) > eom) { \ + h_errno = NO_RECOVERY; \ + return NULL; \ + } \ + } while (/*CONSTCOND*/0) + +static struct hostent * +getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, + res_state res) +{ + const HEADER *hp; + const u_char *cp; + int n; + const u_char *eom, *erdata; + char *bp, **ap, **hap, *ep; + int type, class, ancount, qdcount; + int haveanswer, had_error; + int toobig = 0; + char tbuf[MAXDNAME]; + const char *tname; + int (*name_ok)(const char *); + + _DIAGASSERT(answer != NULL); + _DIAGASSERT(qname != NULL); + + tname = qname; + host.h_name = NULL; + eom = answer->buf + anslen; + switch (qtype) { + case T_A: + case T_AAAA: + name_ok = res_hnok; + break; + case T_PTR: + name_ok = res_dnok; + break; + default: + return NULL; /* XXX should be abort(); */ + } + /* + * find first satisfactory answer + */ + hp = &answer->hdr; + ancount = ntohs(hp->ancount); + qdcount = ntohs(hp->qdcount); + bp = hostbuf; + ep = hostbuf + sizeof hostbuf; + cp = answer->buf; + BOUNDED_INCR(HFIXEDSZ); + if (qdcount != 1) { + h_errno = NO_RECOVERY; + return NULL; + } + n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + if ((n < 0) || !(*name_ok)(bp)) { + h_errno = NO_RECOVERY; + return NULL; + } + BOUNDED_INCR(n + QFIXEDSZ); + if (qtype == T_A || qtype == T_AAAA) { + /* res_send() has already verified that the query name is the + * same as the one we sent; this just gets the expanded name + * (i.e., with the succeeding search-domain tacked on). + */ + n = strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + h_errno = NO_RECOVERY; + return NULL; + } + host.h_name = bp; + bp += n; + /* The qname can be abbreviated, but h_name is now absolute. */ + qname = host.h_name; + } + ap = host_aliases; + *ap = NULL; + host.h_aliases = host_aliases; + hap = h_addr_ptrs; + *hap = NULL; + host.h_addr_list = h_addr_ptrs; + haveanswer = 0; + had_error = 0; + while (ancount-- > 0 && cp < eom && !had_error) { + n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + if ((n < 0) || !(*name_ok)(bp)) { + had_error++; + continue; + } + cp += n; /* name */ + BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); + type = _getshort(cp); + cp += INT16SZ; /* type */ + class = _getshort(cp); + cp += INT16SZ + INT32SZ; /* class, TTL */ + n = _getshort(cp); + cp += INT16SZ; /* len */ + BOUNDS_CHECK(cp, n); + erdata = cp + n; + if (class != C_IN) { + /* XXX - debug? syslog? */ + cp += n; + continue; /* XXX - had_error++ ? */ + } + if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { + if (ap >= &host_aliases[MAXALIASES-1]) + continue; + n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); + if ((n < 0) || !(*name_ok)(tbuf)) { + had_error++; + continue; + } + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + /* Store alias. */ + *ap++ = bp; + n = strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + bp += n; + /* Get canonical name. */ + n = strlen(tbuf) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + strlcpy(bp, tbuf, (size_t)(ep - bp)); + host.h_name = bp; + bp += n; + continue; + } + if (qtype == T_PTR && type == T_CNAME) { + n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); + if (n < 0 || !res_dnok(tbuf)) { + had_error++; + continue; + } + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + /* Get canonical name. */ + n = strlen(tbuf) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + strlcpy(bp, tbuf, (size_t)(ep - bp)); + tname = bp; + bp += n; + continue; + } + if (type != qtype) { + if (type != T_KEY && type != T_SIG) + syslog(LOG_NOTICE|LOG_AUTH, + "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", + qname, p_class(C_IN), p_type(qtype), + p_type(type)); + cp += n; + continue; /* XXX - had_error++ ? */ + } + switch (type) { + case T_PTR: + if (strcasecmp(tname, bp) != 0) { + syslog(LOG_NOTICE|LOG_AUTH, + AskedForGot, qname, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + if ((n < 0) || !res_hnok(bp)) { + had_error++; + break; + } +#if MULTI_PTRS_ARE_ALIASES + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + if (!haveanswer) + host.h_name = bp; + else if (ap < &host_aliases[MAXALIASES-1]) + *ap++ = bp; + else + n = -1; + if (n != -1) { + n = strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + had_error++; + break; + } + bp += n; + } + break; +#else + host.h_name = bp; + if (res->options & RES_USE_INET6) { + n = strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + had_error++; + break; + } + bp += n; + map_v4v6_hostent(&host, &bp, ep); + } + h_errno = NETDB_SUCCESS; + return &host; +#endif + case T_A: + case T_AAAA: + if (strcasecmp(host.h_name, bp) != 0) { + syslog(LOG_NOTICE|LOG_AUTH, + AskedForGot, host.h_name, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + if (n != host.h_length) { + cp += n; + continue; + } + if (type == T_AAAA) { + struct in6_addr in6; + memcpy(&in6, cp, IN6ADDRSZ); + if (IN6_IS_ADDR_V4MAPPED(&in6)) { + cp += n; + continue; + } + } + if (!haveanswer) { + int nn; + + host.h_name = bp; + nn = strlen(bp) + 1; /* for the \0 */ + bp += nn; + } + + bp += sizeof(align) - + (size_t)((u_long)bp % sizeof(align)); + + if (bp + n >= &hostbuf[sizeof hostbuf]) { + debugprintf("size (%d) too big\n", res, n); + had_error++; + continue; + } + if (hap >= &h_addr_ptrs[MAXADDRS-1]) { + if (!toobig++) { + debugprintf("Too many addresses (%d)\n", + res, MAXADDRS); + } + cp += n; + continue; + } + (void)memcpy(*hap++ = bp, cp, (size_t)n); + bp += n; + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + break; + default: + abort(); + } + if (!had_error) + haveanswer++; + } + if (haveanswer) { + *ap = NULL; + *hap = NULL; + /* + * Note: we sort even if host can take only one address + * in its return structures - should give it the "best" + * address in that case, not some random one + */ + if (res->nsort && haveanswer > 1 && qtype == T_A) + addrsort(h_addr_ptrs, haveanswer, res); + if (!host.h_name) { + n = strlen(qname) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) + goto no_recovery; + strlcpy(bp, qname, (size_t)(ep - bp)); + host.h_name = bp; + bp += n; + } + if (res->options & RES_USE_INET6) + map_v4v6_hostent(&host, &bp, ep); + h_errno = NETDB_SUCCESS; + return &host; + } + no_recovery: + h_errno = NO_RECOVERY; + return NULL; +} + +struct hostent * +gethostbyname(const char *name) +{ + struct hostent *hp; + res_state res = __res_get_state(); + + if (res == NULL) + return NULL; + + _DIAGASSERT(name != NULL); + + if (res->options & RES_USE_INET6) { + hp = gethostbyname_internal(name, AF_INET6, res); + if (hp) { + __res_put_state(res); + return hp; + } + } + hp = gethostbyname_internal(name, AF_INET, res); + __res_put_state(res); + return hp; +} + +struct hostent * +gethostbyname2(const char *name, int af) +{ + struct hostent *hp; + res_state res = __res_get_state(); + + if (res == NULL) + return NULL; + hp = gethostbyname_internal(name, af, res); + __res_put_state(res); + return hp; +} + +static struct hostent * +gethostbyname_internal(const char *name, int af, res_state res) +{ + const char *cp; + char *bp, *ep; + int size; + struct hostent *hp; + static const ns_dtab dtab[] = { + NS_FILES_CB(_gethtbyname, NULL) + { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_gethtbyname, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(name != NULL); + + switch (af) { + case AF_INET: + size = INADDRSZ; + break; + case AF_INET6: + size = IN6ADDRSZ; + break; + default: + h_errno = NETDB_INTERNAL; + errno = EAFNOSUPPORT; + return NULL; + } + + host.h_addrtype = af; + host.h_length = size; + + /* + * if there aren't any dots, it could be a user-level alias. + * this is also done in res_nquery() since we are not the only + * function that looks up host names. + */ + if (!strchr(name, '.') && (cp = __hostalias(name))) + name = cp; + + /* + * disallow names consisting only of digits/dots, unless + * they end in a dot. + */ + if (isdigit((u_char) name[0])) + for (cp = name;; ++cp) { + if (!*cp) { + if (*--cp == '.') + break; + /* + * All-numeric, no dot at the end. + * Fake up a hostent as if we'd actually + * done a lookup. + */ + if (inet_pton(af, name, + (char *)(void *)host_addr) <= 0) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + strncpy(hostbuf, name, MAXDNAME); + hostbuf[MAXDNAME] = '\0'; + bp = hostbuf + MAXDNAME; + ep = hostbuf + sizeof hostbuf; + host.h_name = hostbuf; + host.h_aliases = host_aliases; + host_aliases[0] = NULL; + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + if (res->options & RES_USE_INET6) + map_v4v6_hostent(&host, &bp, ep); + h_errno = NETDB_SUCCESS; + return &host; + } + if (!isdigit((u_char) *cp) && *cp != '.') + break; + } + if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || + name[0] == ':') + for (cp = name;; ++cp) { + if (!*cp) { + if (*--cp == '.') + break; + /* + * All-IPv6-legal, no dot at the end. + * Fake up a hostent as if we'd actually + * done a lookup. + */ + if (inet_pton(af, name, + (char *)(void *)host_addr) <= 0) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + strncpy(hostbuf, name, MAXDNAME); + hostbuf[MAXDNAME] = '\0'; + bp = hostbuf + MAXDNAME; + ep = hostbuf + sizeof hostbuf; + host.h_name = hostbuf; + host.h_aliases = host_aliases; + host_aliases[0] = NULL; + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + h_errno = NETDB_SUCCESS; + return &host; + } + if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') + break; + } + + hp = NULL; + h_errno = NETDB_INTERNAL; + if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname", + default_dns_files, name, strlen(name), af) != NS_SUCCESS) + return NULL; + h_errno = NETDB_SUCCESS; + return hp; +} + +struct hostent * +gethostbyaddr(const char *addr, /* XXX should have been def'd as u_char! */ + socklen_t len, int af) +{ + const u_char *uaddr = (const u_char *)addr; + socklen_t size; + struct hostent *hp; + static const ns_dtab dtab[] = { + NS_FILES_CB(_gethtbyaddr, NULL) + { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_gethtbyaddr, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(addr != NULL); + + if (af == AF_INET6 && len == IN6ADDRSZ && + (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) || + IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + if (af == AF_INET6 && len == IN6ADDRSZ && + (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) || + IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) { + /* Unmap. */ + addr += IN6ADDRSZ - INADDRSZ; + uaddr += IN6ADDRSZ - INADDRSZ; + af = AF_INET; + len = INADDRSZ; + } + switch (af) { + case AF_INET: + size = INADDRSZ; + break; + case AF_INET6: + size = IN6ADDRSZ; + break; + default: + errno = EAFNOSUPPORT; + h_errno = NETDB_INTERNAL; + return NULL; + } + if (size != len) { + errno = EINVAL; + h_errno = NETDB_INTERNAL; + return NULL; + } + hp = NULL; + h_errno = NETDB_INTERNAL; + if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr", + default_dns_files, uaddr, len, af) != NS_SUCCESS) + return NULL; + h_errno = NETDB_SUCCESS; + return hp; +} + +void +_sethtent(int f) +{ + if (!hostf) + hostf = fopen(_PATH_HOSTS, "r" ); + else + rewind(hostf); + stayopen = f; +} + +void +_endhtent(void) +{ + if (hostf && !stayopen) { + (void) fclose(hostf); + hostf = NULL; + } +} + +struct hostent * +_gethtent(void) +{ + char *p; + char *cp, **q; + int af, len; + + if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { + h_errno = NETDB_INTERNAL; + return NULL; + } + again: + if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + if (*p == '#') + goto again; + if (!(cp = strpbrk(p, "#\n"))) + goto again; + *cp = '\0'; + if (!(cp = strpbrk(p, " \t"))) + goto again; + *cp++ = '\0'; + if (inet_pton(AF_INET6, p, (char *)(void *)host_addr) > 0) { + af = AF_INET6; + len = IN6ADDRSZ; + } else if (inet_pton(AF_INET, p, (char *)(void *)host_addr) > 0) { + res_state res = __res_get_state(); + if (res == NULL) + return NULL; + if (res->options & RES_USE_INET6) { + map_v4v6_address((char *)(void *)host_addr, + (char *)(void *)host_addr); + af = AF_INET6; + len = IN6ADDRSZ; + } else { + af = AF_INET; + len = INADDRSZ; + } + __res_put_state(res); + } else { + goto again; + } + /* if this is not something we're looking for, skip it. */ + if (host.h_addrtype != 0 && host.h_addrtype != af) + goto again; + if (host.h_length != 0 && host.h_length != len) + goto again; + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + host.h_length = len; + host.h_addrtype = af; + while (*cp == ' ' || *cp == '\t') + cp++; + host.h_name = cp; + q = host.h_aliases = host_aliases; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &host_aliases[MAXALIASES - 1]) + *q++ = cp; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + } + *q = NULL; + h_errno = NETDB_SUCCESS; + return &host; +} + +/*ARGSUSED*/ +int +_gethtbyname(void *rv, void *cb_data, va_list ap) +{ + struct hostent *hp; + const char *name; + int af; + + _DIAGASSERT(rv != NULL); + + name = va_arg(ap, char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + hp = NULL; +#if 0 + { + res_state res = __res_get_state(); + if (res == NULL) + return NS_NOTFOUND; + if (res->options & RES_USE_INET6) + hp = _gethtbyname2(name, AF_INET6); + if (hp==NULL) + hp = _gethtbyname2(name, AF_INET); + __res_put_state(res); + } +#else + hp = _gethtbyname2(name, af); +#endif + *((struct hostent **)rv) = hp; + if (hp == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + return NS_SUCCESS; +} + +struct hostent * +_gethtbyname2(const char *name, int af) +{ + struct hostent *p; + char *tmpbuf, *ptr, **cp; + int num; + size_t len; + + _DIAGASSERT(name != NULL); + + _sethtent(stayopen); + ptr = tmpbuf = NULL; + num = 0; + while ((p = _gethtent()) != NULL && num < MAXADDRS) { + if (p->h_addrtype != af) + continue; + if (strcasecmp(p->h_name, name) != 0) { + for (cp = p->h_aliases; *cp != NULL; cp++) + if (strcasecmp(*cp, name) == 0) + break; + if (*cp == NULL) continue; + } + + if (num == 0) { + size_t bufsize; + char *src; + + bufsize = strlen(p->h_name) + 2 + + MAXADDRS * p->h_length + + ALIGNBYTES; + for (cp = p->h_aliases; *cp != NULL; cp++) + bufsize += strlen(*cp) + 1; + + if ((tmpbuf = malloc(bufsize)) == NULL) { + h_errno = NETDB_INTERNAL; + return NULL; + } + + ptr = tmpbuf; + src = p->h_name; + while ((*ptr++ = *src++) != '\0'); + for (cp = p->h_aliases; *cp != NULL; cp++) { + src = *cp; + while ((*ptr++ = *src++) != '\0'); + } + *ptr++ = '\0'; + + ptr = (char *)(void *)ALIGN(ptr); + } + + (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length); + ptr += p->h_length; + num++; + } + _endhtent(); + if (num == 0) return NULL; + + len = ptr - tmpbuf; + if (len > (sizeof(hostbuf) - ALIGNBYTES)) { + free(tmpbuf); + errno = ENOSPC; + h_errno = NETDB_INTERNAL; + return NULL; + } + ptr = memcpy((void *)ALIGN(hostbuf), tmpbuf, len); + free(tmpbuf); + + host.h_name = ptr; + while (*ptr++); + + cp = host_aliases; + while (*ptr) { + *cp++ = ptr; + while (*ptr++); + } + ptr++; + *cp = NULL; + + ptr = (char *)(void *)ALIGN(ptr); + cp = h_addr_ptrs; + while (num--) { + *cp++ = ptr; + ptr += host.h_length; + } + *cp = NULL; + + return &host; +} + +/*ARGSUSED*/ +int +_gethtbyaddr(void *rv, void *cb_data, va_list ap) +{ + struct hostent *p; + const unsigned char *addr; + int len, af; + + _DIAGASSERT(rv != NULL); + + addr = va_arg(ap, unsigned char *); + len = va_arg(ap, int); + af = va_arg(ap, int); + + host.h_length = len; + host.h_addrtype = af; + + _sethtent(stayopen); + while ((p = _gethtent()) != NULL) + if (p->h_addrtype == af && !memcmp(p->h_addr, addr, + (size_t)len)) + break; + _endhtent(); + *((struct hostent **)rv) = p; + if (p==NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + return NS_SUCCESS; +} + +static void +map_v4v6_address(const char *src, char *dst) +{ + u_char *p = (u_char *)dst; + char tmp[INADDRSZ]; + int i; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + /* Stash a temporary copy so our caller can update in place. */ + (void)memcpy(tmp, src, INADDRSZ); + /* Mark this ipv6 addr as a mapped ipv4. */ + for (i = 0; i < 10; i++) + *p++ = 0x00; + *p++ = 0xff; + *p++ = 0xff; + /* Retrieve the saved copy and we're done. */ + (void)memcpy((void *)p, tmp, INADDRSZ); +} + +static void +map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) +{ + char **ap; + + _DIAGASSERT(hp != NULL); + _DIAGASSERT(bpp != NULL); + _DIAGASSERT(ep != NULL); + + if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) + return; + hp->h_addrtype = AF_INET6; + hp->h_length = IN6ADDRSZ; + for (ap = hp->h_addr_list; *ap; ap++) { + int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align)); + + if (ep - *bpp < (i + IN6ADDRSZ)) { + /* Out of memory. Truncate address list here. XXX */ + *ap = NULL; + return; + } + *bpp += i; + map_v4v6_address(*ap, *bpp); + *ap = *bpp; + *bpp += IN6ADDRSZ; + } +} + +static void +addrsort(char **ap, int num, res_state res) +{ + int i, j; + char **p; + short aval[MAXADDRS]; + int needsort = 0; + + _DIAGASSERT(ap != NULL); + + p = ap; + for (i = 0; i < num; i++, p++) { + for (j = 0 ; (unsigned)j < res->nsort; j++) + if (res->sort_list[j].addr.s_addr == + (((struct in_addr *)(void *)(*p))->s_addr & + res->sort_list[j].mask)) + break; + aval[i] = j; + if (needsort == 0 && i > 0 && j < aval[i-1]) + needsort = i; + } + if (!needsort) + return; + + while (needsort < num) { + for (j = needsort - 1; j >= 0; j--) { + if (aval[j] > aval[j+1]) { + char *hp; + + i = aval[j]; + aval[j] = aval[j+1]; + aval[j+1] = i; + + hp = ap[j]; + ap[j] = ap[j+1]; + ap[j+1] = hp; + } else + break; + } + needsort++; + } +} + +struct hostent * +gethostent(void) +{ + host.h_addrtype = 0; + host.h_length = 0; + return _gethtent(); +} + +/*ARGSUSED*/ +int +_dns_gethtbyname(void *rv, void *cb_data, va_list ap) +{ + querybuf *buf; + int n, type; + struct hostent *hp; + const char *name; + int af; + res_state res; + + _DIAGASSERT(rv != NULL); + + name = va_arg(ap, char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + switch (af) { + case AF_INET: + type = T_A; + break; + case AF_INET6: + type = T_AAAA; + break; + default: + return NS_UNAVAIL; + } + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + n = res_nsearch(res, name, C_IN, type, buf->buf, sizeof(buf->buf)); + if (n < 0) { + free(buf); + debugprintf("res_nsearch failed (%d)\n", res, n); + __res_put_state(res); + return NS_NOTFOUND; + } + hp = getanswer(buf, n, name, type, res); + free(buf); + __res_put_state(res); + if (hp == NULL) + switch (h_errno) { + case HOST_NOT_FOUND: + return NS_NOTFOUND; + case TRY_AGAIN: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + *((struct hostent **)rv) = hp; + return NS_SUCCESS; +} + +/*ARGSUSED*/ +int +_dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) +{ + char qbuf[MAXDNAME + 1], *qp, *ep; + int n; + querybuf *buf; + struct hostent *hp; + const unsigned char *uaddr; + int len, af, advance; + res_state res; + + _DIAGASSERT(rv != NULL); + + uaddr = va_arg(ap, unsigned char *); + len = va_arg(ap, int); + af = va_arg(ap, int); + + switch (af) { + case AF_INET: + (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", + (uaddr[3] & 0xff), (uaddr[2] & 0xff), + (uaddr[1] & 0xff), (uaddr[0] & 0xff)); + break; + + case AF_INET6: + qp = qbuf; + ep = qbuf + sizeof(qbuf) - 1; + for (n = IN6ADDRSZ - 1; n >= 0; n--) { + advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", + uaddr[n] & 0xf, + ((unsigned int)uaddr[n] >> 4) & 0xf); + if (advance > 0 && qp + advance < ep) + qp += advance; + else { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + } + if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + break; + default: + abort(); + } + + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf)); + if (n < 0) { + free(buf); + debugprintf("res_nquery failed (%d)\n", res, n); + __res_put_state(res); + return NS_NOTFOUND; + } + hp = getanswer(buf, n, qbuf, T_PTR, res); + free(buf); + if (hp == NULL) { + __res_put_state(res); + switch (h_errno) { + case HOST_NOT_FOUND: + return NS_NOTFOUND; + case TRY_AGAIN: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + hp->h_addrtype = af; + hp->h_length = len; + (void)memcpy(host_addr, uaddr, (size_t)len); + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + if (af == AF_INET && (res->options & RES_USE_INET6)) { + map_v4v6_address((char *)(void *)host_addr, + (char *)(void *)host_addr); + hp->h_addrtype = AF_INET6; + hp->h_length = IN6ADDRSZ; + } + + __res_put_state(res); + *((struct hostent **)rv) = hp; + h_errno = NETDB_SUCCESS; + return NS_SUCCESS; +} + +#ifdef YP +/*ARGSUSED*/ +struct hostent * +_yphostent(char *line, int af) +{ + static struct in_addr host_addrs[MAXADDRS]; + static struct in6_addr host6_addrs[MAXADDRS]; + char *p = line; + char *cp, **q; + char **hap; + int addrok; + int more; + size_t naddrs; + + _DIAGASSERT(line != NULL); + + host.h_name = NULL; + host.h_addr_list = h_addr_ptrs; + host.h_addrtype = af; + switch (af) { + case AF_INET: + host.h_length = INADDRSZ; + break; + case AF_INET6: + host.h_length = IN6ADDRSZ; + break; + default: + return NULL; + } + hap = h_addr_ptrs; + q = host.h_aliases = host_aliases; + naddrs = 0; + +nextline: + /* check for host_addrs overflow */ + if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0])) + goto done; + if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0])) + goto done; + + more = 0; + cp = strpbrk(p, " \t"); + if (cp == NULL) + goto done; + *cp++ = '\0'; + + /* p has should have an address */ + addrok = 0; + switch (af) { + case AF_INET: + addrok = inet_aton(p, &host_addrs[naddrs]); + break; + case AF_INET6: + addrok = inet_pton(af, p, &host6_addrs[naddrs]); + break; + } + if (addrok != 1) { + /* skip to the next line */ + while (cp && *cp) { + if (*cp == '\n') { + cp++; + goto nextline; + } + cp++; + } + + goto done; + } + + switch (af) { + case AF_INET: + *hap++ = (char *)(void *)&host_addrs[naddrs++]; + break; + case AF_INET6: + *hap++ = (char *)(void *)&host6_addrs[naddrs++]; + break; + } + + while (*cp == ' ' || *cp == '\t') + cp++; + p = cp; + cp = strpbrk(p, " \t\n"); + if (cp != NULL) { + if (*cp == '\n') + more = 1; + *cp++ = '\0'; + } + if (!host.h_name) + host.h_name = p; + else if (strcmp(host.h_name, p)==0) + ; + else if (q < &host_aliases[MAXALIASES - 1]) + *q++ = p; + p = cp; + if (more) + goto nextline; + + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (*cp == '\n') { + cp++; + goto nextline; + } + if (q < &host_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + +done: + if (host.h_name == NULL) + return NULL; + *q = NULL; + *hap = NULL; + return &host; +} + +/*ARGSUSED*/ +int +_yp_gethtbyaddr(void *rv, void *cb_data, va_list ap) +{ + struct hostent *hp = NULL; + static char *__ypcurrent; + int __ypcurrentlen, r; + char name[INET6_ADDRSTRLEN]; /* XXX enough? */ + const unsigned char *uaddr; + int af; + const char *map; + + _DIAGASSERT(rv != NULL); + + uaddr = va_arg(ap, unsigned char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + /* + * XXX unfortunately, we cannot support IPv6 extended scoped address + * notation here. gethostbyaddr() is not scope-aware. too bad. + */ + if (inet_ntop(af, uaddr, name, sizeof(name)) == NULL) + return NS_UNAVAIL; + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + switch (af) { + case AF_INET: + map = "hosts.byaddr"; + break; + default: + map = "ipnodes.byaddr"; + break; + } + r = yp_match(__ypdomain, map, name, + (int)strlen(name), &__ypcurrent, &__ypcurrentlen); + if (r == 0) + hp = _yphostent(__ypcurrent, af); + if (hp == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + *((struct hostent **)rv) = hp; + return NS_SUCCESS; +} + +/*ARGSUSED*/ +int +_yp_gethtbyname(void *rv, void *cb_data, va_list ap) +{ + struct hostent *hp = NULL; + static char *__ypcurrent; + int __ypcurrentlen, r; + const char *name; + int af; + const char *map; + + _DIAGASSERT(rv != NULL); + + name = va_arg(ap, char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + switch (af) { + case AF_INET: + map = "hosts.byname"; + break; + default: + map = "ipnodes.byname"; + break; + } + r = yp_match(__ypdomain, map, name, + (int)strlen(name), &__ypcurrent, &__ypcurrentlen); + if (r == 0) + hp = _yphostent(__ypcurrent, af); + if (hp == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + *((struct hostent **)rv) = hp; + return NS_SUCCESS; +} +#endif diff --git a/lib/nbsd_libc/net/gethostbyname.3 b/lib/nbsd_libc/net/gethostbyname.3 new file mode 100644 index 000000000..e0224bc3b --- /dev/null +++ b/lib/nbsd_libc/net/gethostbyname.3 @@ -0,0 +1,333 @@ +.\" $NetBSD: gethostbyname.3,v 1.28 2006/10/07 21:30:39 apb Exp $ +.\" +.\" Copyright (c) 1983, 1987, 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. +.\" +.\" @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95 +.\" +.Dd October 7, 2006 +.Dt GETHOSTBYNAME 3 +.Os +.Sh NAME +.Nm gethostbyname , +.Nm gethostbyname2 , +.Nm gethostbyaddr , +.Nm gethostent , +.Nm sethostent , +.Nm endhostent , +.Nm herror , +.Nm hstrerror +.Nd get network host entry +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In netdb.h +.Fd extern int h_errno; +.Ft struct hostent * +.Fn gethostbyname "const char *name" +.Ft struct hostent * +.Fn gethostbyname2 "const char *name" "int af" +.Ft struct hostent * +.Fn gethostbyaddr "const char *addr" "socklen_t len" "int type" +.Ft struct hostent * +.Fn gethostent void +.Ft void +.Fn sethostent "int stayopen" +.Ft void +.Fn endhostent void +.Ft void +.Fn herror "const char *string" +.Ft const char * +.Fn hstrerror "int err" +.Sh DESCRIPTION +The +.Fn gethostbyname , +.Fn gethostbyname2 +and +.Fn gethostbyaddr +functions +each return a pointer to an object with the +following structure describing an internet host. +.Bd -literal +struct hostent { + char *h_name; /* official name of host */ + char **h_aliases; /* alias list */ + int h_addrtype; /* host address type */ + int h_length; /* length of address */ + char **h_addr_list; /* list of addresses from name server */ +}; +#define h_addr h_addr_list[0] /* address, for backward compatibility */ +.Ed +.Pp +The members of this structure are: +.Bl -tag -width h_addr_list +.It Fa h_name +Official name of the host. +.It Fa h_aliases +A NULL-terminated array of alternative names for the host. +.It Fa h_addrtype +The type of address being returned; currently always +.Dv AF_INET . +.It Fa h_length +The length, in bytes, of the address. +.It Fa h_addr_list +A NULL-terminated array of network addresses for the host. +Host addresses are returned in network byte order. +.It Fa h_addr +The first address in +.Fa h_addr_list ; +this is for backward compatibility. +.El +.Pp +In the case of +.Fn gethostbyname +and +.Fn gethostbyname2 , +the host is specified by name, +or using a string representation of a numeric address. +In the case of +.Fn gethostbyaddr , +the host is specified using a binary representation of an address. +.Pp +The returned +.Ft "struct hostent" +structure may contain +the result of a simple string to binary conversion, +information obtained from the domain name resolver (see +.Xr resolver 3 ) , +broken-out fields from a line in +.Pa /etc/hosts , +or database entries supplied by the +.Xr yp 8 +system. +The order of the lookups is controlled by the +.Sq hosts +entry in +.Xr nsswitch.conf 5 . +.Pp +When using the domain name resolver, +.Fn gethostbyname +and +.Fn gethostbyname2 +will search for the named host in the current domain and its parents +unless the name ends in a dot. +If the name contains no dot, and if the environment variable +.Dq Ev HOSTALIASES +contains the name of an alias file, the alias file will first be searched +for an alias matching the input name. +See +.Xr hostname 7 +for the domain search procedure and the alias file format. +.Pp +The +.Fn gethostbyname2 +function is an evolution of +.Fn gethostbyname +which is intended to allow lookups in address families other than +.Dv AF_INET , +for example +.Dv AF_INET6 . +Currently the +.Fa af +argument must be specified as +.Dv AF_INET +or +.Dv AF_INET6 , +else the function will return +.Dv NULL +after having set +.Va h_errno +to +.Dv NETDB_INTERNAL . +.Pp +The +.Fn gethostent +function +reads the next line of the +.Pa /etc/hosts +file, opening the file if necessary. +.Pp +The +.Fn sethostent +function +may be used to request the use of a connected +.Tn TCP +socket for queries. +If the +.Fa stayopen +flag is non-zero, +this sets the option to send all queries to the name server using +.Tn TCP +and to retain the connection after each call to +.Fn gethostbyname , +.Fn gethostbyname2 , +or +.Fn gethostbyaddr . +Otherwise, queries are performed using +.Tn UDP +datagrams. +.Pp +The +.Fn endhostent +function +closes the +.Tn TCP +connection. +.Pp +The +.Fn herror +function writes a message to the diagnostic output consisting of the +string parameter +.Fa s , +the constant string ": ", and a message corresponding to the value of +.Va h_errno . +.Pp +The +.Fn hstrerror +function returns a string which is the message text corresponding to the +value of the +.Fa err +parameter. +.Sh FILES +.Bl -tag -width /etc/hosts -compact +.It Pa /etc/hosts +.El +.Sh DIAGNOSTICS +Error return status from +.Fn gethostbyent , +.Fn gethostbyname , +.Fn gethostbyname2 , +and +.Fn gethostbyaddr +is indicated by return of a null pointer. +The external integer +.Va h_errno +may then be checked to see whether this is a temporary failure +or an invalid or unknown host. +The routine +.Fn herror +can be used to print an error message describing the failure. +If its argument +.Fa string +is +.Pf non Dv -NULL , +it is printed, followed by a colon and a space. +The error message is printed with a trailing newline. +.Pp +The variable +.Va h_errno +can have the following values: +.Bl -tag -width HOST_NOT_FOUND +.It Dv HOST_NOT_FOUND +No such host is known. +.It Dv TRY_AGAIN +This is usually a temporary error +and means that the local server did not receive +a response from an authoritative server. +A retry at some later time may succeed. +.It Dv NO_RECOVERY +Some unexpected server failure was encountered. +This is a non-recoverable error. +.It Dv NO_DATA +The requested name is valid but does not have an IP address; +this is not a temporary error. +This means that the name is known to the name server but there is no address +associated with this name. +Another type of request to the name server using this domain name +will result in an answer; +for example, a mail-forwarder may be registered for this domain. +.El +.Sh SEE ALSO +.Xr resolver 3 , +.Xr hosts 5 , +.Xr nsswitch.conf 5 , +.Xr hostname 7 , +.Xr named 8 +.Sh HISTORY +The +.Fn herror +function appeared in +.Bx 4.3 . +The +.Fn endhostent , +.Fn gethostbyaddr , +.Fn gethostbyname , +.Fn gethostent , +and +.Fn sethostent +functions appeared in +.Bx 4.2 . +The +.Fn gethostbyname2 +function first appeared in bind-4.9.4. +IPv6 support was implemented in WIDE Hydrangea IPv6 protocol stack kit. +.Sh CAVEATS +If the search routines specified in +.Xr nsswitch.conf 5 +decide to read the +.Pa /etc/hosts +file, +.Fn gethostbyname , +.Fn gethostbyname2 , +and +.Fn gethostbyaddr +will +read the next line of the file, +re-opening the file if necessary. +.Pp +The +.Fn sethostent +function +opens and/or rewinds the file +.Pa /etc/hosts . +If the +.Fa stayopen +argument is non-zero, +the file will not be closed after each call to +.Fn gethostbyname , +.Fn gethostbyname2 , +.Fn gethostbyaddr , +or +.Fn gethostent . +.Pp +The +.Fn endhostent +function closes the file. +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. +Only the Internet +address format is currently understood. +.Pp +The +.Fn gethostent +does not currently follow the search order specified in +.Xr nsswitch.conf 5 +and only reads the +.Pa /etc/hosts +file. diff --git a/lib/nbsd_libc/net/getifaddrs.3 b/lib/nbsd_libc/net/getifaddrs.3 new file mode 100644 index 000000000..f698186c4 --- /dev/null +++ b/lib/nbsd_libc/net/getifaddrs.3 @@ -0,0 +1,167 @@ +.\" $NetBSD: getifaddrs.3,v 1.12 2010/03/22 19:30:54 joerg Exp $ +.\" BSDI getifaddrs.3,v 2.5 2000/02/23 14:51:59 dab Exp +.\" +.\" Copyright (c) 1995, 1999 +.\" Berkeley Software Design, Inc. 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. +.\" +.\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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 April 21, 2009 +.Dt GETIFADDRS 3 +.Os +.Sh NAME +.Nm getifaddrs +.Nd get interface addresses +.Sh SYNOPSIS +.In sys/types.h +.In sys/socket.h +.In ifaddrs.h +.Ft int +.Fn getifaddrs "struct ifaddrs **ifap" +.Ft void +.Fn freeifaddrs "struct ifaddrs *ifp" +.Sh DESCRIPTION +The +.Fn getifaddrs +function stores a reference to a linked list of the network interfaces +on the local machine in the memory referenced by +.Fa ifap . +The list consists of +.Nm ifaddrs +structures, as defined in the include file +.In ifaddrs.h . +The +.Nm ifaddrs +structure contains at least the following entries: +.Bd -literal + struct ifaddrs *ifa_next; /* Pointer to next struct */ + char *ifa_name; /* Interface name */ + u_int ifa_flags; /* Interface flags */ + struct sockaddr *ifa_addr; /* Interface address */ + struct sockaddr *ifa_netmask; /* Interface netmask */ + struct sockaddr *ifa_broadaddr; /* Interface broadcast address */ + struct sockaddr *ifa_dstaddr; /* P2P interface destination */ + void *ifa_data; /* Address specific data */ +.Ed +.Pp +The +.Li ifa_next +field contains a pointer to the next structure on the list. +This field is +.Dv NULL +in last structure on the list. +.Pp +The +.Li ifa_name +field contains the interface name. +.Pp +The +.Li ifa_flags +field contains the interface flags, as set by +.Xr ifconfig 8 +utility. +.Pp +The +.Li ifa_addr +field references either the address of the interface or the link level +address of the interface, if one exists, otherwise it is +.Dv NULL . +(The +.Li sa_family +field of the +.Li ifa_addr +field should be consulted to determine the format of the +.Li ifa_addr +address.) +.Pp +The +.Li ifa_netmask +field references the netmask associated with +.Li ifa_addr , +if one is set, otherwise it is +.Dv NULL . +.Pp +The +.Li ifa_broadaddr +field, +which should only be referenced for non-P2P interfaces, +references the broadcast address associated with +.Li ifa_addr , +if one exists, otherwise it is +.Dv NULL . +.Pp +The +.Li ifa_dstaddr +field references the destination address on a P2P interface, +if one exists, otherwise it is +.Dv NULL . +.Pp +The +.Li ifa_data +field references address family specific data. +For +.Dv AF_LINK +addresses it contains a pointer to the +.Fa struct if_data +.Pq as defined in include file Aq Pa net/if.h +which contains various interface attributes and statistics. +For all other address families, it is +.Dv NULL . +.Pp +The data returned by +.Fn getifaddrs +is dynamically allocated and should be freed using +.Fn freeifaddrs +when no longer needed. +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. +Otherwise, a value of -1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn getifaddrs +may fail and set +.Va errno +for any of the errors specified for the library routines +.Xr ioctl 2 , +.Xr socket 2 , +.Xr malloc 3 +or +.Xr sysctl 3 . +.Sh SEE ALSO +.Xr ioctl 2 , +.Xr socket 2 , +.Xr sysctl 3 , +.Xr networking 4 , +.Xr ifconfig 8 +.Sh HISTORY +The +.Nm +implementation first appeared in +.Bsx . +.Sh BUGS +If both +.In net/if.h +and +.In ifaddrs.h +are being included, +.In net/if.h +.Em must +be included before +.In ifaddrs.h . diff --git a/lib/nbsd_libc/net/getifaddrs.c b/lib/nbsd_libc/net/getifaddrs.c new file mode 100644 index 000000000..7fde5aba7 --- /dev/null +++ b/lib/nbsd_libc/net/getifaddrs.c @@ -0,0 +1,305 @@ +/* $NetBSD: getifaddrs.c,v 1.13 2010/11/05 16:23:56 pooka Exp $ */ + +/* + * Copyright (c) 1995, 1999 + * Berkeley Software Design, Inc. 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. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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. + * + * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getifaddrs.c,v 1.13 2010/11/05 16:23:56 pooka Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef RUMP_ACTION +#include "namespace.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#if defined(__weak_alias) && !defined(RUMP_ACTION) +__weak_alias(getifaddrs,_getifaddrs) +__weak_alias(freeifaddrs,_freeifaddrs) +#endif + +#ifdef RUMP_ACTION +#include +#define sysctl(a,b,c,d,e,f) rump_sys___sysctl(a,b,c,d,e,f) +#endif + +#define SALIGN (sizeof(long) - 1) +#define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1)) + +int +getifaddrs(struct ifaddrs **pif) +{ + int icnt = 1; + int dcnt = 0; + int ncnt = 0; + int mib[6]; + size_t needed; + char *buf; + char *next; + struct ifaddrs cif; + char *p, *p0; + struct rt_msghdr *rtm; + struct if_msghdr *ifm; + struct ifa_msghdr *ifam; + struct sockaddr *sa; + struct ifaddrs *ifa, *ift; + u_short idx = 0; + int i; + size_t len, alen; + char *data; + char *names; + + _DIAGASSERT(pif != NULL); + + mib[0] = CTL_NET; + mib[1] = PF_ROUTE; + mib[2] = 0; /* protocol */ + mib[3] = 0; /* wildcard address family */ + mib[4] = NET_RT_IFLIST; + mib[5] = 0; /* no flags */ + if (sysctl(mib, __arraycount(mib), NULL, &needed, NULL, 0) < 0) + return (-1); + if ((buf = malloc(needed)) == NULL) + return (-1); + if (sysctl(mib, __arraycount(mib), buf, &needed, NULL, 0) < 0) { + free(buf); + return (-1); + } + + for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { + rtm = (struct rt_msghdr *)(void *)next; + if (rtm->rtm_version != RTM_VERSION) + continue; + switch (rtm->rtm_type) { + case RTM_IFINFO: + ifm = (struct if_msghdr *)(void *)rtm; + if (ifm->ifm_addrs & RTA_IFP) { + const struct sockaddr_dl *dl; + + idx = ifm->ifm_index; + ++icnt; + dl = (struct sockaddr_dl *)(void *)(ifm + 1); + dcnt += SA_RLEN((const struct sockaddr *)(const void *)dl) + + ALIGNBYTES; + dcnt += sizeof(ifm->ifm_data); + ncnt += dl->sdl_nlen + 1; + } else + idx = 0; + break; + + case RTM_NEWADDR: + ifam = (struct ifa_msghdr *)(void *)rtm; + if (idx && ifam->ifam_index != idx) + abort(); /* this cannot happen */ + +#define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD) + if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) + break; + p = (char *)(void *)(ifam + 1); + ++icnt; + /* Scan to look for length of address */ + alen = 0; + for (p0 = p, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + if (i == RTAX_IFA) { + alen = len; + break; + } + p += len; + } + for (p = p0, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + if (i == RTAX_NETMASK && sa->sa_len == 0) + dcnt += alen; + else + dcnt += len; + p += len; + } + break; + } + } + + if (icnt + dcnt + ncnt == 1) { + *pif = NULL; + free(buf); + return (0); + } + data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt); + if (data == NULL) { + free(buf); + return(-1); + } + + ifa = (struct ifaddrs *)(void *)data; + data += sizeof(struct ifaddrs) * icnt; + names = data + dcnt; + + memset(ifa, 0, sizeof(struct ifaddrs) * icnt); + ift = ifa; + + idx = 0; + for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { + rtm = (struct rt_msghdr *)(void *)next; + if (rtm->rtm_version != RTM_VERSION) + continue; + switch (rtm->rtm_type) { + case RTM_IFINFO: + ifm = (struct if_msghdr *)(void *)rtm; + if (ifm->ifm_addrs & RTA_IFP) { + const struct sockaddr_dl *dl; + + idx = ifm->ifm_index; + dl = (struct sockaddr_dl *)(void *)(ifm + 1); + + memset(&cif, 0, sizeof(cif)); + + cif.ifa_name = names; + cif.ifa_flags = (int)ifm->ifm_flags; + memcpy(names, dl->sdl_data, + (size_t)dl->sdl_nlen); + names[dl->sdl_nlen] = 0; + names += dl->sdl_nlen + 1; + + cif.ifa_addr = (struct sockaddr *)(void *)data; + memcpy(data, dl, (size_t)dl->sdl_len); + data += SA_RLEN((const struct sockaddr *)(const void *)dl); + + /* ifm_data needs to be aligned */ + cif.ifa_data = data = (void *)ALIGN(data); + memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data)); + data += sizeof(ifm->ifm_data); + } else + idx = 0; + break; + + case RTM_NEWADDR: + ifam = (struct ifa_msghdr *)(void *)rtm; + if (idx && ifam->ifam_index != idx) + abort(); /* this cannot happen */ + + if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) + break; + ift->ifa_name = cif.ifa_name; + ift->ifa_flags = cif.ifa_flags; + ift->ifa_data = NULL; + p = (char *)(void *)(ifam + 1); + /* Scan to look for length of address */ + alen = 0; + for (p0 = p, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + if (i == RTAX_IFA) { + alen = len; + break; + } + p += len; + } + for (p = p0, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + switch (i) { + case RTAX_IFA: + ift->ifa_addr = + (struct sockaddr *)(void *)data; + memcpy(data, p, len); + data += len; + if (ift->ifa_addr->sa_family == AF_LINK) + ift->ifa_data = cif.ifa_data; + break; + + case RTAX_NETMASK: + ift->ifa_netmask = + (struct sockaddr *)(void *)data; + if (sa->sa_len == 0) { + memset(data, 0, alen); + data += alen; + break; + } + memcpy(data, p, len); + data += len; + break; + + case RTAX_BRD: + ift->ifa_broadaddr = + (struct sockaddr *)(void *)data; + memcpy(data, p, len); + data += len; + break; + } + p += len; + } + + + ift = (ift->ifa_next = ift + 1); + break; + } + } + + free(buf); + if (--ift >= ifa) { + ift->ifa_next = NULL; + *pif = ifa; + } else { + *pif = NULL; + free(ifa); + } + return (0); +} + +void +freeifaddrs(struct ifaddrs *ifp) +{ + + _DIAGASSERT(ifp != NULL); + + free(ifp); +} diff --git a/lib/nbsd_libc/net/getnameinfo.3 b/lib/nbsd_libc/net/getnameinfo.3 new file mode 100644 index 000000000..5cd3539f1 --- /dev/null +++ b/lib/nbsd_libc/net/getnameinfo.3 @@ -0,0 +1,268 @@ +.\" $NetBSD: getnameinfo.3,v 1.37 2010/03/22 19:30:54 joerg Exp $ +.\" $KAME: getnameinfo.3,v 1.37 2005/01/05 03:23:05 itojun Exp $ +.\" $OpenBSD: getnameinfo.3,v 1.36 2004/12/21 09:48:20 jmc Exp $ +.\" +.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") +.\" Copyright (C) 2000, 2001 Internet Software Consortium. +.\" +.\" 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 ISC DISCLAIMS ALL WARRANTIES WITH +.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +.\" AND FITNESS. IN NO EVENT SHALL ISC 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 March 21, 2005 +.Dt GETNAMEINFO 3 +.Os +.Sh NAME +.Nm getnameinfo +.Nd socket address structure to hostname and service name +.Sh SYNOPSIS +.In netdb.h +.Ft int +.Fn getnameinfo "const struct sockaddr * restrict sa" "socklen_t salen" \ + "char * restrict host" "size_t hostlen" "char * restrict serv" \ + "size_t servlen" "int flags" +.Sh DESCRIPTION +The +.Fn getnameinfo +function is used to convert a +.Li sockaddr +structure to a pair of host name and service strings. +It is a replacement for and provides more flexibility than the +.Xr gethostbyaddr 3 +and +.Xr getservbyport 3 +functions and is the converse of the +.Xr getaddrinfo 3 +function. +.Pp +The +.Li sockaddr +structure +.Fa sa +should point to either a +.Li sockaddr_in +or +.Li sockaddr_in6 +structure (for IPv4 or IPv6 respectively) that is +.Fa salen +bytes long. +.Pp +The host and service names associated with +.Fa sa +are stored in +.Fa host +and +.Fa serv +which have length parameters +.Fa hostlen +and +.Fa servlen . +The maximum value for +.Fa hostlen +is +.Dv NI_MAXHOST +and the maximum value for +.Fa servlen +is +.Dv NI_MAXSERV , +as defined by +.In netdb.h . +If a length parameter is zero, no string will be stored. +Otherwise, enough space must be provided to store the +host name or service string plus a byte for the NUL terminator. +.Pp +The +.Fa flags +argument is formed by +.Sy OR Ns 'ing +the following values: +.Bl -tag -width "NI_NUMERICHOSTXX" +.It Dv NI_NOFQDN +A fully qualified domain name is not required for local hosts. +The local part of the fully qualified domain name is returned instead. +.It Dv NI_NUMERICHOST +Return the address in numeric form, as if calling +.Xr inet_ntop 3 , +instead of a host name. +.It Dv NI_NAMEREQD +A name is required. +If the host name cannot be found in DNS and this flag is set, +a non-zero error code is returned. +If the host name is not found and the flag is not set, the +address is returned in numeric form. +.It NI_NUMERICSERV +The service name is returned as a digit string representing the port number. +.It NI_DGRAM +Specifies that the service being looked up is a datagram +service, and causes +.Xr getservbyport 3 +to be called with a second argument of +.Dq udp +instead of its default of +.Dq tcp . +This is required for the few ports (512\-514) that have different services +for +.Tn UDP +and +.Tn TCP . +.El +.Pp +This implementation allows numeric IPv6 address notation with scope identifier, +as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt. +IPv6 link-local address will appear as a string like +.Dq Li fe80::1%ne0 . +Refer to +.Xr getaddrinfo 3 +for more information. +.Sh RETURN VALUES +.Fn getnameinfo +returns zero on success or one of the error codes listed in +.Xr gai_strerror 3 +if an error occurs. +.Sh EXAMPLES +The following code tries to get a numeric host name, and service name, +for a given socket address. +Observe that there is no hardcoded reference to a particular address family. +.Bd -literal -offset indent +struct sockaddr *sa; /* input */ +char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; + +if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), sbuf, + sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { + errx(1, "could not get numeric hostname"); + /*NOTREACHED*/ +} +printf("host=%s, serv=%s\en", hbuf, sbuf); +.Ed +.Pp +The following version checks if the socket address has a reverse address mapping: +.Bd -literal -offset indent +struct sockaddr *sa; /* input */ +char hbuf[NI_MAXHOST]; + +if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), NULL, 0, + NI_NAMEREQD)) { + errx(1, "could not resolve hostname"); + /*NOTREACHED*/ +} +printf("host=%s\en", hbuf); +.Ed +.Sh SEE ALSO +.Xr gai_strerror 3 , +.Xr getaddrinfo 3 , +.Xr gethostbyaddr 3 , +.Xr getservbyport 3 , +.Xr inet_ntop 3 , +.Xr resolver 3 , +.Xr hosts 5 , +.Xr resolv.conf 5 , +.Xr services 5 , +.Xr hostname 7 , +.Xr named 8 +.Rs +.%A R. Gilligan +.%A S. Thomson +.%A J. Bound +.%A W. Stevens +.%T Basic Socket Interface Extensions for IPv6 +.%R RFC 2553 +.%D March 1999 +.Re +.Rs +.%A S. Deering +.%A B. Haberman +.%A T. Jinmei +.%A E. Nordmark +.%A B. Zill +.%T "IPv6 Scoped Address Architecture" +.%R internet draft +.%N draft-ietf-ipv6-scoping-arch-02.txt +.%O work in progress material +.Re +.Rs +.%A Craig Metz +.%T Protocol Independence Using the Sockets API +.%B "Proceedings of the FREENIX track: 2000 USENIX annual technical conference" +.%D June 2000 +.Re +.Sh STANDARDS +The +.Fn getnameinfo +function is defined by the +.St -p1003.1g-2000 +draft specification and documented in +.Sy "RFC 2553" , +.Dq Basic Socket Interface Extensions for IPv6 . +.Sh CAVEATS +.Fn getnameinfo +can return both numeric and FQDN forms of the address specified in +.Fa sa . +There is no return value that indicates whether the string returned in +.Fa host +is a result of binary to numeric-text translation (like +.Xr inet_ntop 3 ) , +or is the result of a DNS reverse lookup. +Because of this, malicious parties could set up a PTR record as follows: +.Bd -literal -offset indent +1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1 +.Ed +.Pp +and trick the caller of +.Fn getnameinfo +into believing that +.Fa sa +is +.Li 10.1.1.1 +when it is actually +.Li 127.0.0.1 . +.Pp +To prevent such attacks, the use of +.Dv NI_NAMEREQD +is recommended when the result of +.Fn getnameinfo +is used for access control purposes: +.Bd -literal -offset indent +struct sockaddr *sa; +socklen_t salen; +char addr[NI_MAXHOST]; +struct addrinfo hints, *res; +int error; + +error = getnameinfo(sa, salen, addr, sizeof(addr), + NULL, 0, NI_NAMEREQD); +if (error == 0) { + memset(\*[Am]hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ + hints.ai_flags = AI_NUMERICHOST; + if (getaddrinfo(addr, "0", \*[Am]hints, \*[Am]res) == 0) { + /* malicious PTR record */ + freeaddrinfo(res); + printf("bogus PTR record\en"); + return -1; + } + /* addr is FQDN as a result of PTR lookup */ +} else { + /* addr is numeric string */ + error = getnameinfo(sa, salen, addr, sizeof(addr), + NULL, 0, NI_NUMERICHOST); +} +.Ed +.Sh BUGS +The implementation of +.Fn getnameinfo +is not thread-safe. +.\".Pp +.\".Ox +.\"intentionally uses a different +.\".Dv NI_MAXHOST +.\"value from what +.\".Tn "RFC 2553" +.\"suggests, to avoid buffer length handling mistakes. diff --git a/lib/nbsd_libc/net/getnameinfo.c b/lib/nbsd_libc/net/getnameinfo.c new file mode 100644 index 000000000..bd373ae12 --- /dev/null +++ b/lib/nbsd_libc/net/getnameinfo.c @@ -0,0 +1,595 @@ +/* $NetBSD: getnameinfo.c,v 1.50 2010/06/29 14:44:19 seanb Exp $ */ +/* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */ + +/* + * Copyright (c) 2000 Ben Harris. + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * 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 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 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 PROJECT 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. + */ + +/* + * Issues to be discussed: + * - Thread safe-ness must be checked + * - RFC2553 says that we should raise error on short buffer. X/Open says + * we need to truncate the result. We obey RFC2553 (and X/Open should be + * modified). ipngwg rough consensus seems to follow RFC2553. + * - What is "local" in NI_FQDN? + * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. + * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if + * sin6_scope_id is filled - standardization status? + * XXX breaks backward compat for code that expects no scopeid. + * beware on merge. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getnameinfo.c,v 1.50 2010/06/29 14:44:19 seanb Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getnameinfo,_getnameinfo) +#endif + +static const struct afd { + int a_af; + socklen_t a_addrlen; + socklen_t a_socklen; + int a_off; +} afdl [] = { +#ifdef INET6 + {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), + offsetof(struct sockaddr_in6, sin6_addr)}, +#endif + {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), + offsetof(struct sockaddr_in, sin_addr)}, + {0, 0, 0, 0}, +}; + +struct sockinet { + u_char si_len; + u_char si_family; + u_short si_port; +}; + +static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int)); +#ifdef INET6 +static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *, + socklen_t, int)); +static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, + int)); +#endif +static int getnameinfo_atalk __P((const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int)); + +static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int)); +static int hexname __P((const u_int8_t *, size_t, char *, socklen_t)); + +/* + * Top-level getnameinfo() code. Look at the address family, and pick an + * appropriate function to call. + */ +int +getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) + const struct sockaddr *sa; + socklen_t salen; + char *host, *serv; + socklen_t hostlen, servlen; + int flags; +{ + + switch (sa->sa_family) { + case AF_APPLETALK: + return getnameinfo_atalk(sa, salen, host, hostlen, + serv, servlen, flags); + case AF_INET: + case AF_INET6: + return getnameinfo_inet(sa, salen, host, hostlen, + serv, servlen, flags); + case AF_LINK: + return getnameinfo_link(sa, salen, host, hostlen, + serv, servlen, flags); + default: + return EAI_FAMILY; + } +} + +/* + * getnameinfo_atalk(): + * Format an AppleTalk address into a printable format. + */ +/* ARGSUSED */ +static int +getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen, + char *host, socklen_t hostlen, char *serv, socklen_t servlen, + int flags) +{ + char numserv[8]; + int n, m=0; + + const struct sockaddr_at *sat = + (const struct sockaddr_at *)(const void *)sa; + + if (serv != NULL && servlen > 0) { + snprintf(numserv, sizeof(numserv), "%u", sat->sat_port); + if (strlen(numserv) + 1 > servlen) + return EAI_MEMORY; + strlcpy(serv, numserv, servlen); + } + + n = snprintf(host, hostlen, "%u.%u", + ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node); + + if (n < 0 || (socklen_t)(m+n) >= hostlen) + goto errout; + + m += n; + + if (sat->sat_range.r_netrange.nr_phase) { + n = snprintf(host+m, hostlen-m, " phase %u", + sat->sat_range.r_netrange.nr_phase); + + if (n < 0 || (socklen_t)(m+n) >= hostlen) + goto errout; + + m += n; + } + if (sat->sat_range.r_netrange.nr_firstnet) { + n = snprintf(host+m, hostlen-m, " range %u - %u", + ntohs(sat->sat_range.r_netrange.nr_firstnet), + ntohs(sat->sat_range.r_netrange.nr_lastnet )); + + if (n < 0 || (socklen_t)(m+n) >= hostlen) + goto errout; + + m += n; + } + + return 0; + +errout: + if (host && hostlen>0) + host[m] = '\0'; /* XXX ??? */ + + return EAI_MEMORY; +} + +/* + * getnameinfo_inet(): + * Format an IPv4 or IPv6 sockaddr into a printable string. + */ +static int +getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags) + const struct sockaddr *sa; + socklen_t salen; + char *host; + socklen_t hostlen; + char *serv; + socklen_t servlen; + int flags; +{ + const struct afd *afd; + struct servent *sp; + struct hostent *hp; + u_short port; + int family, i; + const char *addr; + u_int32_t v4a; + char numserv[512]; + char numaddr[512]; + + /* sa is checked below */ + /* host may be NULL */ + /* serv may be NULL */ + + if (sa == NULL) + return EAI_FAIL; + + family = sa->sa_family; + for (i = 0; afdl[i].a_af; i++) + if (afdl[i].a_af == family) { + afd = &afdl[i]; + goto found; + } + return EAI_FAMILY; + + found: + if (salen != afd->a_socklen) + return EAI_FAIL; + + /* network byte order */ + port = ((const struct sockinet *)(const void *)sa)->si_port; + addr = (const char *)(const void *)sa + afd->a_off; + + if (serv == NULL || servlen == 0) { + /* + * do nothing in this case. + * in case you are wondering if "&&" is more correct than + * "||" here: rfc2553bis-03 says that serv == NULL OR + * servlen == 0 means that the caller does not want the result. + */ + } else { + struct servent_data svd; + struct servent sv; + + if (flags & NI_NUMERICSERV) + sp = NULL; + else { + (void)memset(&svd, 0, sizeof(svd)); + sp = getservbyport_r(port, + (flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd); + } + if (sp) { + if (strlen(sp->s_name) + 1 > servlen) { + endservent_r(&svd); + return EAI_MEMORY; + } + strlcpy(serv, sp->s_name, servlen); + endservent_r(&svd); + } else { + snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); + if (strlen(numserv) + 1 > servlen) + return EAI_MEMORY; + strlcpy(serv, numserv, servlen); + } + } + + switch (sa->sa_family) { + case AF_INET: + v4a = (u_int32_t) + ntohl(((const struct sockaddr_in *) + (const void *)sa)->sin_addr.s_addr); + if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) + flags |= NI_NUMERICHOST; + v4a >>= IN_CLASSA_NSHIFT; + if (v4a == 0) + flags |= NI_NUMERICHOST; + break; +#ifdef INET6 + case AF_INET6: + { + const struct sockaddr_in6 *sin6; + sin6 = (const struct sockaddr_in6 *)(const void *)sa; + switch (sin6->sin6_addr.s6_addr[0]) { + case 0x00: + if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) + ; + else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) + ; + else + flags |= NI_NUMERICHOST; + break; + default: + if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { + flags |= NI_NUMERICHOST; + } + else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) + flags |= NI_NUMERICHOST; + break; + } + } + break; +#endif + } + if (host == NULL || hostlen == 0) { + /* + * do nothing in this case. + * in case you are wondering if "&&" is more correct than + * "||" here: rfc2553bis-03 says that host == NULL or + * hostlen == 0 means that the caller does not want the result. + */ + } else if (flags & NI_NUMERICHOST) { + size_t numaddrlen; + + /* NUMERICHOST and NAMEREQD conflicts with each other */ + if (flags & NI_NAMEREQD) + return EAI_NONAME; + + switch(afd->a_af) { +#ifdef INET6 + case AF_INET6: + { + int error; + + if ((error = ip6_parsenumeric(sa, addr, host, + hostlen, flags)) != 0) + return(error); + break; + } +#endif + default: + if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) + == NULL) + return EAI_SYSTEM; + numaddrlen = strlen(numaddr); + if (numaddrlen + 1 > hostlen) /* don't forget terminator */ + return EAI_MEMORY; + strlcpy(host, numaddr, hostlen); + break; + } + } else { + hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); + + if (hp) { +#if 0 + /* + * commented out, since "for local host" is not + * implemented here - see RFC2553 p30 + */ + if (flags & NI_NOFQDN) { + char *p; + p = strchr(hp->h_name, '.'); + if (p) + *p = '\0'; + } +#endif + if (strlen(hp->h_name) + 1 > hostlen) { + return EAI_MEMORY; + } + strlcpy(host, hp->h_name, hostlen); + } else { + if (flags & NI_NAMEREQD) + return EAI_NONAME; + switch(afd->a_af) { +#ifdef INET6 + case AF_INET6: + { + int error; + + if ((error = ip6_parsenumeric(sa, addr, host, + hostlen, + flags)) != 0) + return(error); + break; + } +#endif + default: + if (inet_ntop(afd->a_af, addr, host, + hostlen) == NULL) + return EAI_SYSTEM; + break; + } + } + } + return(0); +} + +#ifdef INET6 +static int +ip6_parsenumeric(sa, addr, host, hostlen, flags) + const struct sockaddr *sa; + const char *addr; + char *host; + socklen_t hostlen; + int flags; +{ + size_t numaddrlen; + char numaddr[512]; + + _DIAGASSERT(sa != NULL); + _DIAGASSERT(addr != NULL); + _DIAGASSERT(host != NULL); + + if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) + return EAI_SYSTEM; + + numaddrlen = strlen(numaddr); + if (numaddrlen + 1 > hostlen) /* don't forget terminator */ + return EAI_OVERFLOW; + strlcpy(host, numaddr, hostlen); + + if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) { + char zonebuf[MAXHOSTNAMELEN]; + int zonelen; + + zonelen = ip6_sa2str( + (const struct sockaddr_in6 *)(const void *)sa, + zonebuf, sizeof(zonebuf), flags); + if (zonelen < 0) + return EAI_OVERFLOW; + if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen) + return EAI_OVERFLOW; + /* construct */ + memcpy(host + numaddrlen + 1, zonebuf, + (size_t)zonelen); + host[numaddrlen] = SCOPE_DELIMITER; + host[numaddrlen + 1 + zonelen] = '\0'; + } + + return 0; +} + +/* ARGSUSED */ +static int +ip6_sa2str(sa6, buf, bufsiz, flags) + const struct sockaddr_in6 *sa6; + char *buf; + size_t bufsiz; + int flags; +{ + unsigned int ifindex; + const struct in6_addr *a6; + int n; + + _DIAGASSERT(sa6 != NULL); + _DIAGASSERT(buf != NULL); + + ifindex = (unsigned int)sa6->sin6_scope_id; + a6 = &sa6->sin6_addr; + +#ifdef NI_NUMERICSCOPE + if ((flags & NI_NUMERICSCOPE) != 0) { + n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); + if (n < 0 || (size_t)n >= bufsiz) + return -1; + else + return n; + } +#endif + + /* if_indextoname() does not take buffer size. not a good api... */ + if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && + bufsiz >= IF_NAMESIZE) { + char *p = if_indextoname(ifindex, buf); + if (p) { + return(strlen(p)); + } + } + + /* last resort */ + n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); + if (n < 0 || (size_t) n >= bufsiz) + return -1; + else + return n; +} +#endif /* INET6 */ + + +/* + * getnameinfo_link(): + * Format a link-layer address into a printable format, paying attention to + * the interface type. + */ +/* ARGSUSED */ +static int +getnameinfo_link(const struct sockaddr *sa, socklen_t salen, + char *host, socklen_t hostlen, char *serv, socklen_t servlen, + int flags) +{ + const struct sockaddr_dl *sdl = + (const struct sockaddr_dl *)(const void *)sa; + const struct ieee1394_hwaddr *iha; + int n; + + if (serv != NULL && servlen > 0) + *serv = '\0'; + + if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) { + n = snprintf(host, hostlen, "link#%u", sdl->sdl_index); + if (n < 0 || (socklen_t) n > hostlen) { + *host = '\0'; + return EAI_MEMORY; + } + return 0; + } + + switch (sdl->sdl_type) { +#ifdef IFT_ECONET + case IFT_ECONET: + if (sdl->sdl_alen < 2) + return EAI_FAMILY; + if (CLLADDR(sdl)[1] == 0) + n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]); + else + n = snprintf(host, hostlen, "%u.%u", + CLLADDR(sdl)[1], CLLADDR(sdl)[0]); + if (n < 0 || (socklen_t) n >= hostlen) { + *host = '\0'; + return EAI_MEMORY; + } else + return 0; +#endif + case IFT_IEEE1394: + if (sdl->sdl_alen < sizeof(iha->iha_uid)) + return EAI_FAMILY; + iha = + (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl); + return hexname(iha->iha_uid, sizeof(iha->iha_uid), + host, hostlen); + /* + * The following have zero-length addresses. + * IFT_ATM (net/if_atmsubr.c) + * IFT_FAITH (net/if_faith.c) + * IFT_GIF (net/if_gif.c) + * IFT_LOOP (net/if_loop.c) + * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c) + * IFT_SLIP (net/if_sl.c, net/if_strip.c) + * IFT_STF (net/if_stf.c) + * IFT_L2VLAN (net/if_vlan.c) + * IFT_PROPVIRTUAL (net/if_bridge.h> + */ + /* + * The following use IPv4 addresses as link-layer addresses: + * IFT_OTHER (net/if_gre.c) + */ + case IFT_ARCNET: /* default below is believed correct for all these. */ + case IFT_ETHER: + case IFT_FDDI: + case IFT_HIPPI: + case IFT_ISO88025: + default: + return hexname((const u_int8_t *)CLLADDR(sdl), + (size_t)sdl->sdl_alen, host, hostlen); + } +} + +static int +hexname(cp, len, host, hostlen) + const u_int8_t *cp; + char *host; + size_t len; + socklen_t hostlen; +{ + int n; + size_t i; + char *outp = host; + + *outp = '\0'; + for (i = 0; i < len; i++) { + n = snprintf(outp, hostlen, "%s%02x", + i ? ":" : "", cp[i]); + if (n < 0 || (socklen_t) n >= hostlen) { + *host = '\0'; + return EAI_MEMORY; + } + outp += n; + hostlen -= n; + } + return 0; +} diff --git a/lib/nbsd_libc/net/getnetent.3 b/lib/nbsd_libc/net/getnetent.3 new file mode 100644 index 000000000..622211ed1 --- /dev/null +++ b/lib/nbsd_libc/net/getnetent.3 @@ -0,0 +1,152 @@ +.\" $NetBSD: getnetent.3,v 1.13 2004/05/08 18:52:15 kleink Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)getnetent.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd May 8, 2004 +.Dt GETNETENT 3 +.Os +.Sh NAME +.Nm getnetent , +.Nm getnetbyaddr , +.Nm getnetbyname , +.Nm setnetent , +.Nm endnetent +.Nd get network entry +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In netdb.h +.Ft struct netent * +.Fn getnetent +.Ft struct netent * +.Fn getnetbyname "const char *name" +.Ft struct netent * +.Fn getnetbyaddr "uint32_t net" "int type" +.Fn setnetent "int stayopen" +.Fn endnetent +.Sh DESCRIPTION +The +.Fn getnetent , +.Fn getnetbyname , +and +.Fn getnetbyaddr +functions +each return a pointer to an object with the +following structure +containing the broken-out +fields of a line in the network data base as described in +.Xr networks 5 . +.Bd -literal -offset indent +struct netent { + char *n_name; /* official name of net */ + char **n_aliases; /* alias list */ + int n_addrtype; /* net number type */ + uint32_t n_net; /* net number */ +}; +.Ed +.Pp +The members of this structure are: +.Bl -tag -width n_addrtype +.It Fa n_name +The official name of the network. +.It Fa n_aliases +A zero terminated list of alternative names for the network. +.It Fa n_addrtype +The type of the network number returned; currently only AF_INET. +.It Fa n_net +The network number. Network numbers are returned in machine byte +order. +.El +.Pp +The +.Fn getnetent +function +reads the next line of the file, opening the file if necessary. +.Pp +The +.Fn setnetent +function +opens and rewinds the file. If the +.Fa stayopen +flag is non-zero, +the net data base will not be closed after each call to +.Fn getnetbyname +or +.Fn getnetbyaddr . +.Pp +The +.Fn endnetent +function +closes the file. +.Pp +The +.Fn getnetbyname +function +and +.Fn getnetbyaddr +sequentially search from the beginning +of the file until a matching +net name or +net address and type is found, +or until +.Dv EOF +is encountered. +Network numbers are supplied in host order. +.Sh FILES +.Bl -tag -width /etc/networks -compact +.It Pa /etc/networks +.El +.Sh DIAGNOSTICS +Null pointer +(0) returned on +.Dv EOF +or error. +.Sh SEE ALSO +.Xr networks 5 , +.Xr nsswitch.conf 5 +.Sh HISTORY +The +.Fn getnetent , +.Fn getnetbyaddr , +.Fn getnetbyname , +.Fn setnetent , +and +.Fn endnetent +functions appeared in +.Bx 4.2 . +.Sh BUGS +The data space used by +these functions is static; if future use requires the data, it should be +copied before any subsequent calls to these functions overwrite it. +Only Internet network +numbers are currently understood. +Expecting network numbers to fit +in no more than 32 bits is probably +naive. diff --git a/lib/nbsd_libc/net/getnetent.c b/lib/nbsd_libc/net/getnetent.c new file mode 100644 index 000000000..f685f32d2 --- /dev/null +++ b/lib/nbsd_libc/net/getnetent.c @@ -0,0 +1,170 @@ +/* $NetBSD: getnetent.c,v 1.18 2007/01/27 22:27:35 christos Exp $ */ + +/* + * Copyright (c) 1983, 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. + * + * Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro + * Dep. Matematica Universidade de Coimbra, Portugal, Europe + * + * 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. + * + * from getnetent.c 1.1 (Coimbra) 93/06/02 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp "; +#else +__RCSID("$NetBSD: getnetent.c,v 1.18 2007/01/27 22:27:35 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(endnetent,_endnetent) +__weak_alias(getnetent,_getnetent) +__weak_alias(setnetent,_setnetent) +#endif + +#define MAXALIASES 35 + +static FILE *netf; +static char line[BUFSIZ+1]; +static struct netent net; +static char *net_aliases[MAXALIASES]; +int _net_stayopen; + +static void __setnetent __P((int)); +static void __endnetent __P((void)); + +void +setnetent(stayopen) + int stayopen; +{ + + sethostent(stayopen); + __setnetent(stayopen); +} + +void +endnetent() +{ + + endhostent(); + __endnetent(); +} + +static void +__setnetent(f) + int f; +{ + + if (netf == NULL) + netf = fopen(_PATH_NETWORKS, "r" ); + else + rewind(netf); + _net_stayopen |= f; +} + +static void +__endnetent() +{ + + if (netf) { + fclose(netf); + netf = NULL; + } + _net_stayopen = 0; +} + +struct netent * +getnetent() +{ + char *p; + register char *cp, **q; + + if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) + return (NULL); +#if (defined(__sparc__) && defined(_LP64)) || \ + defined(__alpha__) || \ + (defined(__i386__) && defined(_LP64)) || \ + (defined(__sh__) && defined(_LP64)) + net.__n_pad0 = 0; +#endif +again: + p = fgets(line, sizeof line, netf); + if (p == NULL) + return (NULL); + if (*p == '#') + goto again; + cp = strpbrk(p, "#\n"); + if (cp == NULL) + goto again; + *cp = '\0'; + net.n_name = p; + cp = strpbrk(p, " \t"); + if (cp == NULL) + goto again; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + net.n_net = inet_network(cp); + net.n_addrtype = AF_INET; + q = net.n_aliases = net_aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &net_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + } + *q = NULL; + return (&net); +} diff --git a/lib/nbsd_libc/net/getnetnamadr.c b/lib/nbsd_libc/net/getnetnamadr.c new file mode 100644 index 000000000..ef2e48fab --- /dev/null +++ b/lib/nbsd_libc/net/getnetnamadr.c @@ -0,0 +1,647 @@ +/* $NetBSD: getnetnamadr.c,v 1.41 2008/05/18 22:36:15 lukem Exp $ */ + +/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro + * Dep. Matematica Universidade de Coimbra, Portugal, Europe + * + * 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. + */ +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; +static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03"; +static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp "; +#else +__RCSID("$NetBSD: getnetnamadr.c,v 1.41 2008/05/18 22:36:15 lukem Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef YP +#include +#include +#include +#endif + +#ifdef __weak_alias +__weak_alias(getnetbyaddr,_getnetbyaddr) +__weak_alias(getnetbyname,_getnetbyname) +#endif + +extern int _net_stayopen; + +#define BYADDR 0 +#define BYNAME 1 +#define MAXALIASES 35 + +#define MAXPACKET (64*1024) + +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +typedef union { + long al; + char ac; +} align; + +#ifdef YP +static char *__ypdomain; +static char *__ypcurrent; +static int __ypcurrentlen; +#endif + +static struct netent net_entry; +static char *net_aliases[MAXALIASES]; + +static int parse_reversed_addr(const char *, in_addr_t *); +static struct netent *getnetanswer(querybuf *, int, int); +static int _files_getnetbyaddr(void *, void *, va_list); +static int _files_getnetbyname(void *, void *, va_list); +static int _dns_getnetbyaddr(void *, void *, va_list); +static int _dns_getnetbyname(void *, void *, va_list); +#ifdef YP +static int _yp_getnetbyaddr(void *, void *, va_list); +static int _yp_getnetbyname(void *, void *, va_list); +static struct netent *_ypnetent(char *); +#endif + +/* + * parse_reversed_addr -- + * parse str, which should be of the form 'd.c.b.a.IN-ADDR.ARPA' + * (a PTR as per RFC 1101) and convert into an in_addr_t of the + * address 'a.b.c.d'. + * returns 0 on success (storing in *result), or -1 on error. + */ +static int +parse_reversed_addr(const char *str, in_addr_t *result) +{ + unsigned long octet[4]; + const char *sp; + char *ep; + int octidx; + + sp = str; + /* find the four octets 'd.b.c.a.' */ + for (octidx = 0; octidx < 4; octidx++) { + /* ensure it's a number */ + if (!isdigit((unsigned char)*sp)) + return -1; + octet[octidx] = strtoul(sp, &ep, 10); + /* with a trailing '.' */ + if (*ep != '.') + return -1; + /* and is 0 <= octet <= 255 */ + if (octet[octidx] > 255) + return -1; + sp = ep + 1; + } + /* ensure trailer is correct */ + if (strcasecmp(sp, "IN-ADDR.ARPA") != 0) + return -1; + *result = 0; + /* build result from octets in reverse */ + for (octidx = 3; octidx >= 0; octidx--) { + *result <<= 8; + *result |= (octet[octidx] & 0xff); + } + return 0; +} + +static struct netent * +getnetanswer(querybuf *answer, int anslen, int net_i) +{ + static char n_name[MAXDNAME]; + static char netbuf[PACKETSZ]; + + HEADER *hp; + u_char *cp; + int n; + u_char *eom; + int type, class, ancount, qdcount, haveanswer; + char *in, *bp, **ap, *ep; + + _DIAGASSERT(answer != NULL); + + /* + * find first satisfactory answer + * + * answer --> +------------+ ( MESSAGE ) + * | Header | + * +------------+ + * | Question | the question for the name server + * +------------+ + * | Answer | RRs answering the question + * +------------+ + * | Authority | RRs pointing toward an authority + * | Additional | RRs holding additional information + * +------------+ + */ + eom = answer->buf + anslen; + hp = &answer->hdr; + ancount = ntohs(hp->ancount); /* #/records in the answer section */ + qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ + bp = netbuf; + ep = netbuf + sizeof(netbuf); + cp = answer->buf + HFIXEDSZ; + if (!qdcount) { + if (hp->aa) + h_errno = HOST_NOT_FOUND; + else + h_errno = TRY_AGAIN; + return NULL; + } + while (qdcount-- > 0) { + n = __dn_skipname(cp, eom); + if (n < 0 || (cp + n + QFIXEDSZ) > eom) { + h_errno = NO_RECOVERY; + return(NULL); + } + cp += n + QFIXEDSZ; + } + ap = net_aliases; + *ap = NULL; + net_entry.n_aliases = net_aliases; + haveanswer = 0; + n_name[0] = '\0'; + while (--ancount >= 0 && cp < eom) { + n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + if ((n < 0) || !res_dnok(bp)) + break; + cp += n; + (void)strlcpy(n_name, bp, sizeof(n_name)); + GETSHORT(type, cp); + GETSHORT(class, cp); + cp += INT32SZ; /* TTL */ + GETSHORT(n, cp); + if (class == C_IN && type == T_PTR) { + n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + if ((n < 0) || !res_hnok(bp)) { + cp += n; + return NULL; + } + cp += n; + *ap++ = bp; + bp += strlen(bp) + 1; + net_entry.n_addrtype = + (class == C_IN) ? AF_INET : AF_UNSPEC; + haveanswer++; + } + } + if (haveanswer) { + *ap = NULL; + switch (net_i) { + case BYADDR: + net_entry.n_name = *net_entry.n_aliases; + net_entry.n_net = 0L; + break; + case BYNAME: + ap = net_entry.n_aliases; + next_alias: + in = *ap++; + if (in == NULL) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + net_entry.n_name = n_name; + if (parse_reversed_addr(in, &net_entry.n_net) == -1) + goto next_alias; + break; + } + net_entry.n_aliases++; +#if (defined(__sparc__) && defined(_LP64)) || \ + defined(__alpha__) || \ + (defined(__i386__) && defined(_LP64)) || \ + (defined(__sh__) && defined(_LP64)) + net_entry.__n_pad0 = 0; +#endif + return &net_entry; + } + h_errno = TRY_AGAIN; + return NULL; +} + +/*ARGSUSED*/ +static int +_files_getnetbyaddr(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + uint32_t net = va_arg(ap, uint32_t); + int type = va_arg(ap, int); + + struct netent *np; + + setnetent(_net_stayopen); + while ((np = getnetent()) != NULL) + if (np->n_addrtype == type && np->n_net == net) + break; + if (!_net_stayopen) + endnetent(); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +/*ARGSUSED*/ +static int +_dns_getnetbyaddr(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + uint32_t net = va_arg(ap, uint32_t); + int type = va_arg(ap, int); + + unsigned int netbr[4]; + int nn, anslen; + querybuf *buf; + char qbuf[MAXDNAME]; + uint32_t net2; + struct netent *np; + res_state res; + + if (type != AF_INET) + return NS_UNAVAIL; + + for (nn = 4, net2 = net; net2; net2 >>= 8) + netbr[--nn] = (unsigned int)(net2 & 0xff); + switch (nn) { + default: + return NS_UNAVAIL; + case 3: /* Class A */ + snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]); + break; + case 2: /* Class B */ + snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa", + netbr[3], netbr[2]); + break; + case 1: /* Class C */ + snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa", + netbr[3], netbr[2], netbr[1]); + break; + case 0: /* Class D - E */ + snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", + netbr[3], netbr[2], netbr[1], netbr[0]); + break; + } + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf)); + if (anslen < 0) { + free(buf); +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf("res_query failed\n"); +#endif + __res_put_state(res); + return NS_NOTFOUND; + } + __res_put_state(res); + np = getnetanswer(buf, anslen, BYADDR); + free(buf); + if (np) { + /* maybe net should be unsigned? */ + uint32_t u_net = net; + + /* Strip trailing zeros */ + while ((u_net & 0xff) == 0 && u_net != 0) + u_net >>= 8; + np->n_net = u_net; + } + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +struct netent * +getnetbyaddr(uint32_t net, int net_type) +{ + int rv; + struct netent *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getnetbyaddr, NULL) + { NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_getnetbyaddr, NULL) + NS_NULL_CB + }; + + retval = NULL; + h_errno = NETDB_INTERNAL; + rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyaddr", + __nsdefaultsrc, &retval, net, net_type); + if (rv == NS_SUCCESS) { + h_errno = NETDB_SUCCESS; + return retval; + } + return NULL; +} + +/*ARGSUSED*/ +static int +_files_getnetbyname(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + const char *name = va_arg(ap, const char *); + + struct netent *np; + char **cp; + + setnetent(_net_stayopen); + while ((np = getnetent()) != NULL) { + if (strcasecmp(np->n_name, name) == 0) + break; + for (cp = np->n_aliases; *cp != 0; cp++) + if (strcasecmp(*cp, name) == 0) + goto found; + } +found: + if (!_net_stayopen) + endnetent(); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +/*ARGSUSED*/ +static int +_dns_getnetbyname(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + const char *name = va_arg(ap, const char *); + + int anslen; + querybuf *buf; + char qbuf[MAXDNAME]; + struct netent *np; + res_state res; + + strlcpy(&qbuf[0], name, sizeof(qbuf)); + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf, + sizeof(buf->buf)); + if (anslen < 0) { + free(buf); +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf("res_search failed\n"); +#endif + __res_put_state(res); + return NS_NOTFOUND; + } + __res_put_state(res); + np = getnetanswer(buf, anslen, BYNAME); + free(buf); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +struct netent * +getnetbyname(const char *name) +{ + int rv; + struct netent *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getnetbyname, NULL) + { NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_getnetbyname, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(name != NULL); + + retval = NULL; + h_errno = NETDB_INTERNAL; + rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyname", + __nsdefaultsrc, &retval, name); + if (rv == NS_SUCCESS) { + h_errno = NETDB_SUCCESS; + return retval; + } + return NULL; +} + +#ifdef YP +/*ARGSUSED*/ +static int +_yp_getnetbyaddr(void *cbrv, void *cb_data, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + uint32_t net = va_arg(ap, uint32_t); + int type = va_arg(ap, int); + + struct netent *np; + char qbuf[MAXDNAME]; + unsigned int netbr[4]; + uint32_t net2; + int r; + + if (type != AF_INET) + return NS_UNAVAIL; + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + np = NULL; + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + for (r = 4, net2 = net; net2; net2 >>= 8) + netbr[--r] = (unsigned int)(net2 & 0xff); + switch (r) { + default: + return NS_UNAVAIL; + case 3: /* Class A */ + snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]); + break; + case 2: /* Class B */ + snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]); + break; + case 1: /* Class C */ + snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2], + netbr[3]); + break; + case 0: /* Class D - E */ + snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1], + netbr[2], netbr[3]); + break; + } + r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf), + &__ypcurrent, &__ypcurrentlen); + if (r == 0) + np = _ypnetent(__ypcurrent); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +/*ARGSUSED*/ +static int +_yp_getnetbyname(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + const char *name = va_arg(ap, const char *); + + struct netent *np; + int r; + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + np = NULL; + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name), + &__ypcurrent, &__ypcurrentlen); + if (r == 0) + np = _ypnetent(__ypcurrent); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +static struct netent * +_ypnetent(char *line) +{ + char *cp, *p, **q; + + _DIAGASSERT(line != NULL); + + net_entry.n_name = line; + cp = strpbrk(line, " \t"); + if (cp == NULL) + return NULL; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + net_entry.n_net = inet_network(cp); +#if (defined(__sparc__) && defined(_LP64)) || \ + defined(__alpha__) || \ + (defined(__i386__) && defined(_LP64)) || \ + (defined(__sh__) && defined(_LP64)) + net_entry.__n_pad0 = 0; +#endif + net_entry.n_addrtype = AF_INET; + q = net_entry.n_aliases = net_aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &net_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + } + *q = NULL; + + return &net_entry; +} +#endif diff --git a/lib/nbsd_libc/net/getpeereid.3 b/lib/nbsd_libc/net/getpeereid.3 new file mode 100644 index 000000000..77df2b305 --- /dev/null +++ b/lib/nbsd_libc/net/getpeereid.3 @@ -0,0 +1,142 @@ +.\" +.\" Copyright (c) 2001 Dima Dorfman. +.\" 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 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. +.\" +.\" $FreeBSD: src/lib/libc/gen/getpeereid.3,v 1.6 2002/12/18 10:13:54 ru Exp $ +.\" +.\" $NetBSD: getpeereid.3,v 1.2 2008/01/29 13:55:27 abs Exp $ +.\" +.Dd August 8, 2007 +.Dt GETPEEREID 3 +.Os +.Sh NAME +.Nm getpeereid +.Nd get the effective credentials of a UNIX-domain peer +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In unistd.h +.Ft int +.Fn getpeereid "int s" "uid_t *euid" "gid_t *egid" +.Sh DESCRIPTION +The +.Fn getpeereid +function returns the effective user and group IDs of the +peer connected to a +.Ux Ns -domain +socket. +The argument +.Fa s +must be a +.Ux Ns -domain +socket +.Pq Xr unix 4 +of type +.Dv SOCK_STREAM +on which either +.Xr connect 2 +has been called, or one returned from +.Xr accept 2 +after +.Xr bind 2 +and +.Xr listen 2 +have been called. +If non-NULL, the effective used ID is placed in +.Fa euid , +and the effective group ID in +.Fa egid . +.Pp +The credentials returned to the +.Xr accept 2 +caller are those of its peer at the time it called +.Xr connect 2 ; +the credentials returned to the +.Xr connect 2 +caller are those of its peer at the time it called +.Xr bind 2 . +This mechanism is reliable; there is no way for either side to influence +the credentials returned to its peer except by calling the appropriate +system call (i.e., either +.Xr connect 2 +or +.Xr bind 2 ) +under different effective credentials. +.Pp +One common use of this routine is for a +.Ux Ns -domain +server +to verify the credentials of its client. +Likewise, the client can verify the credentials of the server. +.Sh IMPLEMENTATION NOTES +On +.Nx , +.Fn getpeereid +is implemented in terms of the +.Dv LOCAL_PEEREID +.Xr unix 4 +socket option. +.Sh RETURN VALUES +.Rv -std getpeereid +.Sh ERRORS +The +.Fn getpeereid +function +fails if: +.Bl -tag -width Er +.It Bq Er EBADF +The argument +.Fa s +is not a valid descriptor. +.It Bq Er ENOTSOCK +The argument +.Fa s +is a file, not a socket. +.It Bq Er ENOTCONN +The argument +.Fa s +does not refer to a socket on which +.Xr connect 2 +have been called nor one returned from +.Xr listen 2 . +.It Bq Er EINVAL +The argument +.Fa s +does not refer to a socket of type +.Dv SOCK_STREAM , +or the kernel returned invalid data. +.El +.Sh SEE ALSO +.Xr connect 2 , +.Xr getpeername 2 , +.Xr getsockname 2 , +.Xr getsockopt 2 , +.Xr listen 2 , +.Xr unix 4 +.Sh HISTORY +The +.Fn getpeereid +function appeared in +.Nx 5.0 . diff --git a/lib/nbsd_libc/net/getpeereid.c b/lib/nbsd_libc/net/getpeereid.c new file mode 100644 index 000000000..72652caeb --- /dev/null +++ b/lib/nbsd_libc/net/getpeereid.c @@ -0,0 +1,57 @@ +/* $NetBSD: getpeereid.c,v 1.2 2008/04/29 06:53:01 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Arne H. Juul. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getpeereid.c,v 1.2 2008/04/29 06:53:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + + +int +getpeereid(int s, uid_t *euid, gid_t *egid) +{ + struct unpcbid cred; + socklen_t len = sizeof(cred); + if (getsockopt(s, 0, LOCAL_PEEREID, &cred, &len) < 0) { + return -1; + } else { + if (euid != NULL) + *euid = cred.unp_euid; + if (egid != NULL) + *egid = cred.unp_egid; + return 0; + } +} diff --git a/lib/nbsd_libc/net/getprotobyname.c b/lib/nbsd_libc/net/getprotobyname.c new file mode 100644 index 000000000..e5aa8300c --- /dev/null +++ b/lib/nbsd_libc/net/getprotobyname.c @@ -0,0 +1,61 @@ +/* $NetBSD: getprotobyname.c,v 1.4 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getprotobyname.c,v 1.4 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobyname,_getprotobyname) +#endif + +#ifdef _REENTRANT +extern mutex_t _protoent_mutex; +#endif +extern struct protoent_data _protoent_data; + +struct protoent * +getprotobyname(const char *name) +{ + struct protoent *p; + + mutex_lock(&_protoent_mutex); + p = getprotobyname_r(name, &_protoent_data.proto, &_protoent_data); + mutex_unlock(&_protoent_mutex); + return (p); +} diff --git a/lib/nbsd_libc/net/getprotobyname_r.c b/lib/nbsd_libc/net/getprotobyname_r.c new file mode 100644 index 000000000..3b621e0ef --- /dev/null +++ b/lib/nbsd_libc/net/getprotobyname_r.c @@ -0,0 +1,77 @@ +/* $NetBSD: getprotobyname_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getprotoname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getprotobyname_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobyname_r,_getprotobyname_r) +#endif + +struct protoent * +getprotobyname_r(const char *name, struct protoent *pr, + struct protoent_data *pd) +{ + struct protoent *p; + char **cp; + + _DIAGASSERT(name != NULL); + + setprotoent_r(pd->stayopen, pd); + while ((p = getprotoent_r(pr, pd)) != NULL) { + if (strcmp(p->p_name, name) == 0) + break; + for (cp = p->p_aliases; *cp != NULL; cp++) + if (strcmp(*cp, name) == 0) + goto found; + } +found: + if (!pd->stayopen) + if (pd->fp != NULL) { + (void)fclose(pd->fp); + pd->fp = NULL; + } + return p; +} diff --git a/lib/nbsd_libc/net/getprotobynumber.c b/lib/nbsd_libc/net/getprotobynumber.c new file mode 100644 index 000000000..4840052d9 --- /dev/null +++ b/lib/nbsd_libc/net/getprotobynumber.c @@ -0,0 +1,62 @@ +/* $NetBSD: getprotobynumber.c,v 1.4 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getprotobynumber.c,v 1.4 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobynumber,_getprotobynumber) +#endif + +#ifdef _REENTRANT +extern mutex_t _protoent_mutex; +#endif +extern struct protoent_data _protoent_data; + +struct protoent * +getprotobynumber(int proto) +{ + struct protoent *p; + + mutex_lock(&_protoent_mutex); + p = getprotobynumber_r(proto, &_protoent_data.proto, &_protoent_data); + mutex_unlock(&_protoent_mutex); + return (p); +} diff --git a/lib/nbsd_libc/net/getprotobynumber_r.c b/lib/nbsd_libc/net/getprotobynumber_r.c new file mode 100644 index 000000000..e1cb4f1ef --- /dev/null +++ b/lib/nbsd_libc/net/getprotobynumber_r.c @@ -0,0 +1,66 @@ +/* $NetBSD: getprotobynumber_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getproto.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getprotobynumber_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobynumber_r,_getprotobynumber_r) +#endif + +struct protoent * +getprotobynumber_r(int proto, struct protoent *pr, struct protoent_data *pd) +{ + struct protoent *p; + + setprotoent_r(pd->stayopen, pd); + while ((p = getprotoent_r(pr, pd)) != NULL) + if (p->p_proto == proto) + break; + if (!pd->stayopen) + if (pd->fp != NULL) { + (void)fclose(pd->fp); + pd->fp = NULL; + } + return p; +} diff --git a/lib/nbsd_libc/net/getprotoent.3 b/lib/nbsd_libc/net/getprotoent.3 new file mode 100644 index 000000000..8b8d693c7 --- /dev/null +++ b/lib/nbsd_libc/net/getprotoent.3 @@ -0,0 +1,144 @@ +.\" $NetBSD: getprotoent.3,v 1.11 2003/08/07 16:43:09 agc Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)getprotoent.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt GETPROTOENT 3 +.Os +.Sh NAME +.Nm getprotoent , +.Nm getprotobynumber , +.Nm getprotobyname , +.Nm setprotoent , +.Nm endprotoent +.Nd get protocol entry +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In netdb.h +.Ft struct protoent * +.Fn getprotoent +.Ft struct protoent * +.Fn getprotobyname "const char *name" +.Ft struct protoent * +.Fn getprotobynumber "int proto" +.Fn setprotoent "int stayopen" +.Fn endprotoent +.Sh DESCRIPTION +The +.Fn getprotoent , +.Fn getprotobyname , +and +.Fn getprotobynumber +functions +each return a pointer to an object with the +following structure +containing the broken-out +fields of a line in the network protocol data base, +.Pa /etc/protocols . +.Bd -literal -offset indent +.Pp +struct protoent { + char *p_name; /* official name of protocol */ + char **p_aliases; /* alias list */ + int p_proto; /* protocol number */ +}; +.Ed +.Pp +The members of this structure are: +.Bl -tag -width p_aliases +.It Fa p_name +The official name of the protocol. +.It Fa p_aliases +A zero terminated list of alternative names for the protocol. +.It Fa p_proto +The protocol number. +.El +.Pp +The +.Fn getprotoent +function +reads the next line of the file, opening the file if necessary. +.Pp +The +.Fn setprotoent +function +opens and rewinds the file. If the +.Fa stayopen +flag is non-zero, +the net data base will not be closed after each call to +.Fn getprotobyname +or +.Fn getprotobynumber . +.Pp +The +.Fn endprotoent +function +closes the file. +.Pp +The +.Fn getprotobyname +function +and +.Fn getprotobynumber +sequentially search from the beginning +of the file until a matching +protocol name or +protocol number is found, +or until +.Dv EOF +is encountered. +.Sh RETURN VALUES +Null pointer +(0) returned on +.Dv EOF +or error. +.Sh FILES +.Bl -tag -width /etc/protocols -compact +.It Pa /etc/protocols +.El +.Sh SEE ALSO +.Xr protocols 5 +.Sh HISTORY +The +.Fn getprotoent , +.Fn getprotobynumber , +.Fn getprotobyname , +.Fn setprotoent , +and +.Fn endprotoent +functions appeared in +.Bx 4.2 . +.Sh BUGS +These functions use a static data space; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. +Only the Internet +protocols are currently understood. diff --git a/lib/nbsd_libc/net/getprotoent.c b/lib/nbsd_libc/net/getprotoent.c new file mode 100644 index 000000000..1912b4e12 --- /dev/null +++ b/lib/nbsd_libc/net/getprotoent.c @@ -0,0 +1,80 @@ +/* $NetBSD: getprotoent.c,v 1.12 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getprotoent.c,v 1.12 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(endprotoent,_endprotoent) +__weak_alias(getprotoent,_getprotoent) +__weak_alias(setprotoent,_setprotoent) +#endif + +#ifdef _REENTRANT +mutex_t _protoent_mutex = MUTEX_INITIALIZER; +#endif +struct protoent_data _protoent_data; + +void +setprotoent(int f) +{ + mutex_lock(&_protoent_mutex); + setprotoent_r(f, &_protoent_data); + mutex_unlock(&_protoent_mutex); +} + +void +endprotoent(void) +{ + mutex_lock(&_protoent_mutex); + endprotoent_r(&_protoent_data); + mutex_unlock(&_protoent_mutex); +} + +struct protoent * +getprotoent(void) +{ + struct protoent *p; + + mutex_lock(&_protoent_mutex); + p = getprotoent_r(&_protoent_data.proto, &_protoent_data); + mutex_unlock(&_protoent_mutex); + return (p); +} diff --git a/lib/nbsd_libc/net/getprotoent_r.c b/lib/nbsd_libc/net/getprotoent_r.c new file mode 100644 index 000000000..01b0891c9 --- /dev/null +++ b/lib/nbsd_libc/net/getprotoent_r.c @@ -0,0 +1,153 @@ +/* $NetBSD: getprotoent_r.c,v 1.5 2005/04/18 19:39:45 kleink Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getprotoent_r.c,v 1.5 2005/04/18 19:39:45 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(endprotoent_r,_endprotoent_r) +__weak_alias(getprotoent_r,_getprotoent_r) +__weak_alias(setprotoent_r,_setprotoent_r) +#endif + +void +setprotoent_r(int f, struct protoent_data *pd) +{ + if (pd->fp == NULL) + pd->fp = fopen(_PATH_PROTOCOLS, "r"); + else + rewind(pd->fp); + pd->stayopen |= f; +} + +void +endprotoent_r(struct protoent_data *pd) +{ + if (pd->fp) { + (void)fclose(pd->fp); + pd->fp = NULL; + } + if (pd->aliases) { + free(pd->aliases); + pd->aliases = NULL; + pd->maxaliases = 0; + } + if (pd->line) { + free(pd->line); + pd->line = NULL; + } + pd->stayopen = 0; +} + +struct protoent * +getprotoent_r(struct protoent *pr, struct protoent_data *pd) +{ + char *p, *cp, **q; + size_t i = 0; + int oerrno; + + if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "r")) == NULL) + return NULL; + + for (;;) { + if (pd->line) + free(pd->line); + pd->line = fparseln(pd->fp, NULL, NULL, NULL, + FPARSELN_UNESCALL); + if (pd->line == NULL) + return NULL; + pr->p_name = p = pd->line; + cp = strpbrk(p, " \t"); + if (cp == NULL) + continue; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + pr->p_proto = atoi(cp); + if (pd->aliases == NULL) { + pd->maxaliases = 10; + pd->aliases = malloc(pd->maxaliases * sizeof(char *)); + if (pd->aliases == NULL) { + oerrno = errno; + endprotoent_r(pd); + errno = oerrno; + return NULL; + } + } + q = pr->p_aliases = pd->aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (i == pd->maxaliases - 2) { + pd->maxaliases *= 2; + q = realloc(q, + pd->maxaliases * sizeof(char *)); + if (q == NULL) { + oerrno = errno; + endprotoent_r(pd); + errno = oerrno; + return NULL; + } + pr->p_aliases = pd->aliases = q; + } + q[i++] = cp; + + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + } + q[i] = NULL; + return pr; + } +} diff --git a/lib/nbsd_libc/net/getservbyname.c b/lib/nbsd_libc/net/getservbyname.c new file mode 100644 index 000000000..d208aded9 --- /dev/null +++ b/lib/nbsd_libc/net/getservbyname.c @@ -0,0 +1,61 @@ +/* $NetBSD: getservbyname.c,v 1.14 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getservbyname.c,v 1.14 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyname,_getservbyname) +#endif + +#ifdef _REENTRANT +extern mutex_t _servent_mutex; +#endif +extern struct servent_data _servent_data; + +struct servent * +getservbyname(const char *name, const char *proto) +{ + struct servent *s; + + mutex_lock(&_servent_mutex); + s = getservbyname_r(name, proto, &_servent_data.serv, &_servent_data); + mutex_unlock(&_servent_mutex); + return (s); +} diff --git a/lib/nbsd_libc/net/getservbyname_r.c b/lib/nbsd_libc/net/getservbyname_r.c new file mode 100644 index 000000000..62ee2dec4 --- /dev/null +++ b/lib/nbsd_libc/net/getservbyname_r.c @@ -0,0 +1,147 @@ +/* $NetBSD: getservbyname_r.c,v 1.7 2010/04/25 00:54:46 joerg Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getservbyname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getservbyname_r.c,v 1.7 2010/04/25 00:54:46 joerg Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyname_r,_getservbyname_r) +#endif + +static struct servent * +_servent_getbyname(struct servent_data *sd, struct servent *sp, + const char *name, const char *proto) +{ + + if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0) + return NULL; + + if (sd->flags & _SV_CDB) { + uint8_t buf[255 * 2 + 2]; + size_t namelen, protolen; + const uint8_t *data, *data_end; + const void *data_ptr; + size_t datalen; + + namelen = strlen(name); + if (namelen == 0 || namelen > 255) + return NULL; + if (proto != NULL && *proto == '\0') + return NULL; + if (proto != NULL) + protolen = strlen(proto); + else + protolen = 0; + + buf[0] = namelen; + buf[1] = protolen; + memcpy(buf + 2, name, namelen); + memcpy(buf + 2 + namelen, proto, protolen); + + if (cdbr_find(sd->cdb, buf, 2 + namelen + protolen, + &data_ptr, &datalen)) + return NULL; + + if (datalen < namelen + protolen + 6) + return NULL; + + data = data_ptr; + data_end = data + datalen; + if (protolen) { + if (data[2] != protolen) + return NULL; + if (memcmp(data + 3, proto, protolen + 1)) + return NULL; + } + data += 3 + data[2] + 1; + if (data > data_end) + return NULL; + while (data != data_end) { + if (*data == '\0') + return NULL; + if (data + data[0] + 2 > data_end) + return NULL; + if (data[0] == namelen && + memcmp(data + 1, name, namelen + 1) == 0) + return _servent_parsedb(sd, sp, data_ptr, + datalen); + data += data[0] + 2; + } + return NULL; + } else { + while (_servent_getline(sd) != -1) { + char **cp; + if (_servent_parseline(sd, sp) == NULL) + continue; + + if (strcmp(name, sp->s_name) == 0) + goto gotname; + + for (cp = sp->s_aliases; *cp; cp++) + if (strcmp(name, *cp) == 0) + goto gotname; + continue; +gotname: + if (proto == NULL || strcmp(sp->s_proto, proto) == 0) + return sp; + } + return NULL; + } +} + +struct servent * +getservbyname_r(const char *name, const char *proto, struct servent *sp, + struct servent_data *sd) +{ + _DIAGASSERT(name != NULL); + /* proto may be NULL */ + + setservent_r(sd->flags & _SV_STAYOPEN, sd); + sp = _servent_getbyname(sd, sp, name, proto); + if (!(sd->flags & _SV_STAYOPEN)) + _servent_close(sd); + return sp; +} diff --git a/lib/nbsd_libc/net/getservbyport.c b/lib/nbsd_libc/net/getservbyport.c new file mode 100644 index 000000000..8562c3d3d --- /dev/null +++ b/lib/nbsd_libc/net/getservbyport.c @@ -0,0 +1,62 @@ +/* $NetBSD: getservbyport.c,v 1.12 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getservbyport.c,v 1.12 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyport,_getservbyport) +#endif + +#ifdef _REENTRANT +extern mutex_t _servent_mutex; +#endif +extern struct servent_data _servent_data; + +struct servent * +getservbyport(int port, const char *proto) +{ + struct servent *s; + + mutex_lock(&_servent_mutex); + s = getservbyport_r(port, proto, &_servent_data.serv, &_servent_data); + mutex_unlock(&_servent_mutex); + return (s); +} diff --git a/lib/nbsd_libc/net/getservbyport_r.c b/lib/nbsd_libc/net/getservbyport_r.c new file mode 100644 index 000000000..e5b7ea640 --- /dev/null +++ b/lib/nbsd_libc/net/getservbyport_r.c @@ -0,0 +1,123 @@ +/* $NetBSD: getservbyport_r.c,v 1.7 2010/04/25 00:54:46 joerg Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getservbyport.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getservbyport_r.c,v 1.7 2010/04/25 00:54:46 joerg Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyport_r,_getservbyport_r) +#endif + +static struct servent * +_servent_getbyport(struct servent_data *sd, struct servent *sp, int port, + const char *proto) +{ + + if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0) + return NULL; + + if (sd->flags & _SV_CDB) { + uint8_t buf[255 + 4]; + size_t protolen; + const uint8_t *data; + const void *data_ptr; + size_t datalen; + + port = be16toh(port); + + if (proto != NULL && *proto == '\0') + return NULL; + if (proto != NULL) + protolen = strlen(proto); + else + protolen = 0; + if (port < 0 || port > 65536) + return NULL; + + buf[0] = 0; + buf[1] = protolen; + be16enc(buf + 2, port); + memcpy(buf + 4, proto, protolen); + + if (cdbr_find(sd->cdb, buf, 4 + protolen, + &data_ptr, &datalen)) + return NULL; + + if (datalen < protolen + 4) + return NULL; + + data = data_ptr; + if (be16dec(data) != port) + return NULL; + if (protolen) { + if (data[2] != protolen) + return NULL; + if (memcmp(data + 3, proto, protolen + 1)) + return NULL; + } + return _servent_parsedb(sd, sp, data, datalen); + } else { + while (_servent_getline(sd) != -1) { + if (_servent_parseline(sd, sp) == NULL) + continue; + if (sp->s_port != port) + continue; + if (proto == NULL || strcmp(sp->s_proto, proto) == 0) + return sp; + } + return NULL; + } +} + +struct servent * +getservbyport_r(int port, const char *proto, struct servent *sp, + struct servent_data *sd) +{ + setservent_r(sd->flags & _SV_STAYOPEN, sd); + sp = _servent_getbyport(sd, sp, port, proto); + if (!(sd->flags & _SV_STAYOPEN)) + _servent_close(sd); + return sp; +} diff --git a/lib/nbsd_libc/net/getservent.3 b/lib/nbsd_libc/net/getservent.3 new file mode 100644 index 000000000..d6bcdfe28 --- /dev/null +++ b/lib/nbsd_libc/net/getservent.3 @@ -0,0 +1,154 @@ +.\" $NetBSD: getservent.3,v 1.14 2003/08/07 16:43:10 agc Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)getservent.3 8.4 (Berkeley) 5/25/95 +.\" +.Dd May 25, 1995 +.Dt GETSERVENT 3 +.Os +.Sh NAME +.Nm getservent , +.Nm getservbyport , +.Nm getservbyname , +.Nm setservent , +.Nm endservent +.Nd get service entry +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In netdb.h +.Ft struct servent * +.Fn getservent +.Ft struct servent * +.Fn getservbyname "const char *name" "const char *proto" +.Ft struct servent * +.Fn getservbyport "int port" "const char *proto" +.Ft void +.Fn setservent "int stayopen" +.Ft void +.Fn endservent void +.Sh DESCRIPTION +The +.Fn getservent , +.Fn getservbyname , +and +.Fn getservbyport +functions +each return a pointer to an object with the +following structure +containing the broken-out +fields of a line in the network services data base, +.Pa /etc/services . +.Bd -literal -offset indent +struct servent { + char *s_name; /* official name of service */ + char **s_aliases; /* alias list */ + int s_port; /* port service resides at */ + char *s_proto; /* protocol to use */ +}; +.Ed +.Pp +The members of this structure are: +.Bl -tag -width s_aliases +.It Fa s_name +The official name of the service. +.It Fa s_aliases +A NULL terminated list of alternative names for the service. +.It Fa s_port +The port number at which the service resides. +Port numbers must be given and are returned in network byte order. +.It Fa s_proto +The name of the protocol to use when contacting the +service. +.El +.Pp +The +.Fn getservent +function +reads the next line of the file, opening the file if necessary. +.Pp +The +.Fn setservent +function +opens and rewinds the file. If the +.Fa stayopen +flag is non-zero, +the net data base will not be closed after each call to +.Fn getservbyname +or +.Fn getservbyport . +.Pp +The +.Fn endservent +function +closes the file. +.Pp +The +.Fn getservbyname +and +.Fn getservbyport +functions +sequentially search from the beginning +of the file until a matching +protocol name or +port number is found, +or until +.Dv EOF +is encountered. +If a protocol name is also supplied (non-\c +.Dv NULL ) , +searches must also match the protocol. +.Sh FILES +.Bl -tag -width /etc/services -compact +.It Pa /etc/services +.El +.Sh DIAGNOSTICS +Null pointer +(0) returned on +.Dv EOF +or error. +.Sh SEE ALSO +.Xr getprotoent 3 , +.Xr services 5 +.Sh HISTORY +The +.Fn getservent , +.Fn getservbyport , +.Fn getservbyname , +.Fn setservent , +and +.Fn endservent +functions appeared in +.Bx 4.2 . +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. +Expecting port numbers to fit in a 32 bit +quantity is probably naive. diff --git a/lib/nbsd_libc/net/getservent.c b/lib/nbsd_libc/net/getservent.c new file mode 100644 index 000000000..5465d6aa6 --- /dev/null +++ b/lib/nbsd_libc/net/getservent.c @@ -0,0 +1,80 @@ +/* $NetBSD: getservent.c,v 1.12 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getservent.c,v 1.12 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(endservent,_endservent) +__weak_alias(getservent,_getservent) +__weak_alias(setservent,_setservent) +#endif + +#ifdef _REENTRANT +mutex_t _servent_mutex = MUTEX_INITIALIZER; +#endif +struct servent_data _servent_data; + +void +setservent(int f) +{ + mutex_lock(&_servent_mutex); + setservent_r(f, &_servent_data); + mutex_unlock(&_servent_mutex); +} + +void +endservent(void) +{ + mutex_lock(&_servent_mutex); + endservent_r(&_servent_data); + mutex_unlock(&_servent_mutex); +} + +struct servent * +getservent(void) +{ + struct servent *s; + + mutex_lock(&_servent_mutex); + s = getservent_r(&_servent_data.serv, &_servent_data); + mutex_unlock(&_servent_mutex); + return (s); +} diff --git a/lib/nbsd_libc/net/getservent_r.c b/lib/nbsd_libc/net/getservent_r.c new file mode 100644 index 000000000..22da50f7b --- /dev/null +++ b/lib/nbsd_libc/net/getservent_r.c @@ -0,0 +1,323 @@ +/* $NetBSD: getservent_r.c,v 1.10 2010/04/25 00:54:46 joerg Exp $ */ + +/* + * Copyright (c) 1983, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getservent_r.c,v 1.10 2010/04/25 00:54:46 joerg Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(endservent_r,_endservent_r) +__weak_alias(getservent_r,_getservent_r) +__weak_alias(setservent_r,_setservent_r) +#endif + +int +_servent_open(struct servent_data *sd) +{ + if (sd->flags & (_SV_CDB | _SV_PLAINFILE)) { + sd->flags |= _SV_FIRST; + return 0; + } + + free(sd->line); + sd->line = NULL; + free(sd->cdb_buf); + sd->cdb_buf = NULL; + sd->cdb_buf_len = 0; + free(sd->aliases); + sd->aliases = NULL; + sd->maxaliases = 0; + sd->flags |= _SV_FIRST; + + sd->cdb = cdbr_open(_PATH_SERVICES_CDB, CDBR_DEFAULT); + if (sd->cdb != NULL) { + sd->flags |= _SV_CDB; + return 0; + } + + sd->plainfile = fopen(_PATH_SERVICES, "r"); + if (sd->plainfile != NULL) { + sd->flags |= _SV_PLAINFILE; + return 0; + } + return -1; +} + +void +_servent_close(struct servent_data *sd) +{ + if (sd->flags & _SV_CDB) { + cdbr_close(sd->cdb); + sd->cdb = NULL; + sd->flags &= ~_SV_CDB; + } + + if (sd->flags & _SV_PLAINFILE) { + (void)fclose(sd->plainfile); + sd->plainfile = NULL; + sd->flags &= ~_SV_PLAINFILE; + } + sd->flags &= ~_SV_STAYOPEN; +} + + +int +_servent_getline(struct servent_data *sd) +{ + + if (sd->flags & _SV_CDB) + return -1; + + if ((sd->flags & _SV_PLAINFILE) == 0) + return -1; + + free(sd->line); + sd->line = NULL; + + if (sd->flags & _SV_FIRST) { + (void)rewind((FILE *)sd->plainfile); + sd->flags &= ~_SV_FIRST; + } + sd->line = fparseln(sd->plainfile, NULL, NULL, NULL, + FPARSELN_UNESCALL); + return sd->line == NULL ? -1 : 0; +} + +struct servent * +_servent_parseline(struct servent_data *sd, struct servent *sp) +{ + size_t i = 0; + int oerrno; + char *p, *cp, **q; + + if (sd->line == NULL) + return NULL; + + sp->s_name = p = sd->line; + p = strpbrk(p, " \t"); + if (p == NULL) + return NULL; + *p++ = '\0'; + while (*p == ' ' || *p == '\t') + p++; + cp = strpbrk(p, ",/"); + if (cp == NULL) + return NULL; + *cp++ = '\0'; + sp->s_port = htons((u_short)atoi(p)); + sp->s_proto = cp; + if (sd->aliases == NULL) { + sd->maxaliases = 10; + sd->aliases = calloc(sd->maxaliases, sizeof(*sd->aliases)); + if (sd->aliases == NULL) { + oerrno = errno; + endservent_r(sd); + errno = oerrno; + return NULL; + } + } + sp->s_aliases = sd->aliases; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (i == sd->maxaliases - 2) { + sd->maxaliases *= 2; + q = realloc(sd->aliases, sd->maxaliases * sizeof(*q)); + if (q == NULL) { + oerrno = errno; + endservent_r(sd); + errno = oerrno; + return NULL; + } + sp->s_aliases = sd->aliases = q; + } + sp->s_aliases[i++] = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + sp->s_aliases[i] = NULL; + return sp; +} + +void +setservent_r(int f, struct servent_data *sd) +{ + (void)_servent_open(sd); + sd->flags |= f ? _SV_STAYOPEN : 0; +} + +void +endservent_r(struct servent_data *sd) +{ + _servent_close(sd); + free(sd->aliases); + sd->aliases = NULL; + sd->maxaliases = 0; + free(sd->line); + sd->line = NULL; + free(sd->cdb_buf); + sd->cdb_buf = NULL; + sd->cdb_buf_len = 0; +} + +struct servent * +getservent_r(struct servent *sp, struct servent_data *sd) +{ + + if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0 && + _servent_open(sd) == -1) + return NULL; + + if (sd->flags & _SV_CDB) { + const void *data; + size_t len; + + if (sd->flags & _SV_FIRST) { + sd->cdb_index = 0; + sd->flags &= ~_SV_FIRST; + } + + if (cdbr_get(sd->cdb, sd->cdb_index, &data, &len)) + return NULL; + ++sd->cdb_index; + return _servent_parsedb(sd, sp, data, len); + } + if (sd->flags & _SV_PLAINFILE) { + for (;;) { + if (_servent_getline(sd) == -1) + return NULL; + if (_servent_parseline(sd, sp) == NULL) + continue; + return sp; + } + } + return NULL; +} + +struct servent * +_servent_parsedb(struct servent_data *sd, struct servent *sp, + const uint8_t *data, size_t len) +{ + char **q; + size_t i; + int oerrno; + + if ((sd->flags & _SV_STAYOPEN) == 0) { + if (len > sd->cdb_buf_len) { + void *tmp = realloc(sd->cdb_buf, len); + if (tmp == NULL) + goto fail; + sd->cdb_buf = tmp; + sd->cdb_buf_len = len; + } + memcpy(sd->cdb_buf, data, len); + data = sd->cdb_buf; + } + + if (len < 2) + goto fail; + sp->s_port = htobe16(be16dec(data)); + data += 2; + len -= 2; + + if (len == 0 || len < (size_t)data[0] + 2) + goto fail; + sp->s_proto = __UNCONST(data + 1); + + if (sp->s_proto[data[0]] != '\0') + goto fail; + + len -= 2 + data[0]; + data += 2 + data[0]; + + if (len == 0) + goto fail; + if (len < (size_t)data[0] + 2) + goto fail; + + sp->s_name = __UNCONST(data + 1); + len -= 2 + data[0]; + data += 2 + data[0]; + + if (sd->aliases == NULL) { + sd->maxaliases = 10; + sd->aliases = malloc(sd->maxaliases * sizeof(char *)); + if (sd->aliases == NULL) + goto fail; + } + sp->s_aliases = sd->aliases; + i = 0; + while (len) { + if (len < (size_t)data[0] + 2) + goto fail; + if (i == sd->maxaliases - 2) { + sd->maxaliases *= 2; + q = realloc(sd->aliases, sd->maxaliases * sizeof(*q)); + if (q == NULL) + goto fail; + sp->s_aliases = sd->aliases = q; + } + sp->s_aliases[i++] = __UNCONST(data + 1); + len -= 2 + data[0]; + data += 2 + data[0]; + } + sp->s_aliases[i] = NULL; + return sp; + +fail: + oerrno = errno; + endservent_r(sd); + errno = oerrno; + return NULL; +} + diff --git a/lib/nbsd_libc/net/hesiod.3 b/lib/nbsd_libc/net/hesiod.3 new file mode 100644 index 000000000..f2b9a3e1c --- /dev/null +++ b/lib/nbsd_libc/net/hesiod.3 @@ -0,0 +1,150 @@ +.\" $NetBSD: hesiod.3,v 1.7 2009/03/10 23:36:10 joerg Exp $ +.\" +.\" from: #Id: hesiod.3,v 1.9.2.1 1997/01/03 21:02:23 ghudson Exp # +.\" +.\" Copyright 1988, 1996 by the Massachusetts Institute of Technology. +.\" +.\" Permission to use, copy, modify, and distribute this +.\" software and its documentation for any purpose and without +.\" fee is hereby granted, provided that the above copyright +.\" notice appear in all copies and that both that copyright +.\" notice and this permission notice appear in supporting +.\" documentation, and that the name of M.I.T. not be used in +.\" advertising or publicity pertaining to distribution of the +.\" software without specific, written prior permission. +.\" M.I.T. makes no representations about the suitability of +.\" this software for any purpose. It is provided "as is" +.\" without express or implied warranty. +.\" +.Dd September 16, 2001 +.Dt HESIOD 3 +.Os +.Sh NAME +.Nm hesiod , +.Nm hesiod_init , +.Nm hesiod_resolve , +.Nm hesiod_free_list , +.Nm hesiod_to_bind , +.Nm hesiod_end +.Nd Hesiod name server interface library +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In hesiod.h +.Ft int +.Fn hesiod_init "void **context" +.Ft char +.Fn **hesiod_resolve "void *context" "const char *name" "const char *type" +.Ft void +.Fn hesiod_free_list "void *context" "char **list" +.Ft char +.Fn *hesiod_to_bind "void *context" "const char *name" "const char *type" +.Ft void +.Fn hesiod_end "void *context" +.Sh DESCRIPTION +This family of functions allows you to perform lookups of Hesiod +information, which is stored as text records in the Domain Name +Service. To perform lookups, you must first initialize a +.Fa context , +an opaque object which stores information used internally by the +library between calls. +.Fn hesiod_init +initializes a context, storing a pointer to the context in the +location pointed to by the +.Fa context +argument. +.Fn hesiod_end +frees the resources used by a context. +.Pp +.Fn hesiod_resolve +is the primary interface to the library. If successful, it returns a +list of one or more strings giving the records matching +.Fa name +and +.Fa type . +The last element of the list is followed by a +.Dv NULL +pointer. It is the caller's responsibility to call +.Fn hesiod_free_list +to free the resources used by the returned list. +.Pp +.Fn hesiod_to_bind +converts +.Fa name +and +.Fa type +into the DNS name used by +.Fn hesiod_resolve . +It is the caller's responsibility to free the returned string using +.Xr free 3 . +.Sh RETURN VALUES +If successful, +.Fn hesiod_init +returns 0; otherwise it returns \-1 and sets +.Va errno +to indicate the error. On failure, +.Fn hesiod_resolve +and +.Fn hesiod_to_bind +return +.Dv NULL +and set the global variable +.Va errno +to indicate the error. +.Sh ENVIRONMENT +If the environment variable +.Ev HES_DOMAIN +is set, it will override the domain in the Hesiod configuration file. +If the environment variable +.Ev HESIOD_CONFIG +is set, it specifies the location of the Hesiod configuration file. +.Sh ERRORS +Hesiod calls may fail because of: +.Bl -tag -width ECONNREFUSED -compact +.It Er ENOMEM +Insufficient memory was available to carry out the requested operation. +.It Er ENOEXEC +.Fn hesiod_init +failed because the Hesiod configuration file was invalid. +.It Er ECONNREFUSED +.Fn hesiod_resolve +failed because no name server could be contacted to answer the query. +.It Er EMSGSIZE +.Fn hesiod_resolve +or +.Fn hesiod_to_bind +failed because the query or response was too big to fit into the +packet buffers. +.It Er ENOENT +.Fn hesiod_resolve +failed because the name server had no text records matching +.Fa name +and +.Fa type , +or +.Fn hesiod_to_bind +failed because the +.Fa name +argument had a domain extension which could not be resolved with type +.Dq rhs-extension +in the local Hesiod domain. +.El +.Sh SEE ALSO +.Xr hesiod.conf 5 , +.Xr named 8 +.Rs +.%T Hesiod - Project Athena Technical Plan -- Name Service +.Re +.Sh AUTHORS +.An Steve Dyer, IBM/Project Athena +.An Greg Hudson, MIT Team Athena +.Pp +Copyright 1987, 1988, 1995, 1996 by the Massachusetts Institute of Technology. +.Sh BUGS +The strings corresponding to the +.Ev errno +values set by the Hesiod functions are not particularly indicative of +what went wrong, especially for +.Er ENOEXEC +and +.Er ENOENT . diff --git a/lib/nbsd_libc/net/hesiod.c b/lib/nbsd_libc/net/hesiod.c new file mode 100644 index 000000000..2e7fb9827 --- /dev/null +++ b/lib/nbsd_libc/net/hesiod.c @@ -0,0 +1,649 @@ +/* $NetBSD: hesiod.c,v 1.25 2011/01/05 00:09:43 wiz Exp $ */ + +/* Copyright (c) 1996 by Internet Software Consortium. + * + * 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM 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. + */ + +/* Copyright 1996 by the Massachusetts Institute of Technology. + * + * Permission to use, copy, modify, and distribute this + * software and its documentation for any purpose and without + * fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting + * documentation, and that the name of M.I.T. not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" + * without express or implied warranty. + */ + +/* This file is part of the hesiod library. It implements the core + * portion of the hesiod resolver. + * + * This file is loosely based on an interim version of hesiod.c from + * the BIND IRS library, which was in turn based on an earlier version + * of this file. Extensive changes have been made on each step of the + * path. + * + * This implementation is thread-safe because it uses res_nsend(). + */ + +#include + +#if defined(LIBC_SCCS) && !defined(lint) +__IDSTRING(rcsid_hesiod_c, + "#Id: hesiod.c,v 1.18.2.1 1997/01/03 20:48:20 ghudson Exp #"); +__IDSTRING(rcsid_hesiod_p_h, + "#Id: hesiod_p.h,v 1.1 1996/12/08 21:39:37 ghudson Exp #"); +__IDSTRING(rcsid_hescompat_c, + "#Id: hescompat.c,v 1.1.2.1 1996/12/16 08:37:45 ghudson Exp #"); +__RCSID("$NetBSD: hesiod.c,v 1.25 2011/01/05 00:09:43 wiz Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(hesiod_init,_hesiod_init) +__weak_alias(hesiod_end,_hesiod_end) +__weak_alias(hesiod_to_bind,_hesiod_to_bind) +__weak_alias(hesiod_resolve,_hesiod_resolve) +__weak_alias(hesiod_free_list,_hesiod_free_list) +__weak_alias(hes_init,_hes_init) +__weak_alias(hes_to_bind,_hes_to_bind) +__weak_alias(hes_resolve,_hes_resolve) +__weak_alias(hes_error,_hes_error) +__weak_alias(hes_free,_hes_free) +#endif + +struct hesiod_p { + char *lhs; /* normally ".ns" */ + char *rhs; /* AKA the default hesiod domain */ + int classes[2]; /* The class search order. */ +}; + +#define MAX_HESRESP 1024 + +static int read_config_file __P((struct hesiod_p *, const char *)); +static char **get_txt_records __P((int, const char *)); +static int init_context __P((void)); +static void translate_errors __P((void)); + + +/* + * hesiod_init -- + * initialize a hesiod_p. + */ +int +hesiod_init(context) + void **context; +{ + struct hesiod_p *ctx; + const char *p, *configname; + int serrno; + + _DIAGASSERT(context != NULL); + + ctx = calloc(1, sizeof(struct hesiod_p)); + if (ctx) { + *context = ctx; + /* + * don't permit overrides from environment + * for set.id programs + */ + if (issetugid()) + configname = NULL; + else + configname = getenv("HESIOD_CONFIG"); + if (!configname) + configname = _PATH_HESIOD_CONF; + if (read_config_file(ctx, configname) >= 0) { + /* + * The default rhs can be overridden by an + * environment variable, unless set.id. + */ + if (issetugid()) + p = NULL; + else + p = getenv("HES_DOMAIN"); + if (p) { + if (ctx->rhs) + free(ctx->rhs); + ctx->rhs = malloc(strlen(p) + 2); + if (ctx->rhs) { + *ctx->rhs = '.'; + strcpy(ctx->rhs + 1, + (*p == '.') ? p + 1 : p); + return 0; + } else + errno = ENOMEM; + } else + return 0; + } + } else + errno = ENOMEM; + + serrno = errno; + if (ctx) { + if (ctx->lhs) + free(ctx->lhs); + if (ctx->rhs) + free(ctx->rhs); + free(ctx); + } + errno = serrno; + return -1; +} + +/* + * hesiod_end -- + * Deallocates the hesiod_p. + */ +void +hesiod_end(context) + void *context; +{ + struct hesiod_p *ctx = (struct hesiod_p *) context; + + _DIAGASSERT(context != NULL); + + free(ctx->rhs); + if (ctx->lhs) + free(ctx->lhs); + free(ctx); +} + +/* + * hesiod_to_bind -- + * takes a hesiod (name, type) and returns a DNS + * name which is to be resolved. + */ +char * +hesiod_to_bind(void *context, const char *name, const char *type) +{ + struct hesiod_p *ctx = (struct hesiod_p *) context; + char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL; + const char *rhs; + size_t len; + + _DIAGASSERT(context != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); + + if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) { + errno = EMSGSIZE; + return NULL; + } + + /* + * Find the right right hand side to use, possibly + * truncating bindname. + */ + p = strchr(bindname, '@'); + if (p) { + *p++ = 0; + if (strchr(p, '.')) + rhs = name + (p - bindname); + else { + rhs_list = hesiod_resolve(context, p, "rhs-extension"); + if (rhs_list) + rhs = *rhs_list; + else { + errno = ENOENT; + return NULL; + } + } + } else + rhs = ctx->rhs; + + /* See if we have enough room. */ + len = strlen(bindname) + 1 + strlen(type); + if (ctx->lhs) + len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0); + len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0); + if (len > sizeof(bindname) - 1) { + if (rhs_list) + hesiod_free_list(context, rhs_list); + errno = EMSGSIZE; + return NULL; + } + /* Put together the rest of the domain. */ + strlcat(bindname, ".", sizeof(bindname)); + strlcat(bindname, type, sizeof(bindname)); + /* Only append lhs if it isn't empty. */ + if (ctx->lhs && ctx->lhs[0] != '\0' ) { + if (ctx->lhs[0] != '.') + strlcat(bindname, ".", sizeof(bindname)); + strlcat(bindname, ctx->lhs, sizeof(bindname)); + } + if (rhs[0] != '.') + strlcat(bindname, ".", sizeof(bindname)); + strlcat(bindname, rhs, sizeof(bindname)); + + /* rhs_list is no longer needed, since we're done with rhs. */ + if (rhs_list) + hesiod_free_list(context, rhs_list); + + /* Make a copy of the result and return it to the caller. */ + ret = strdup(bindname); + if (ret == NULL) + errno = ENOMEM; + return ret; +} + +/* + * hesiod_resolve -- + * Given a hesiod name and type, return an array of strings returned + * by the resolver. + */ +char ** +hesiod_resolve(context, name, type) + void *context; + const char *name; + const char *type; +{ + struct hesiod_p *ctx = (struct hesiod_p *) context; + char *bindname, **retvec; + + _DIAGASSERT(context != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); + + bindname = hesiod_to_bind(context, name, type); + if (!bindname) + return NULL; + + retvec = get_txt_records(ctx->classes[0], bindname); + if (retvec == NULL && errno == ENOENT && ctx->classes[1]) + retvec = get_txt_records(ctx->classes[1], bindname); + + free(bindname); + return retvec; +} + +/*ARGSUSED*/ +void +hesiod_free_list(context, list) + void *context; + char **list; +{ + char **p; + + _DIAGASSERT(context != NULL); + + if (list == NULL) + return; + for (p = list; *p; p++) + free(*p); + free(list); +} + + +/* read_config_file -- + * Parse the /etc/hesiod.conf file. Returns 0 on success, + * -1 on failure. On failure, it might leave values in ctx->lhs + * or ctx->rhs which need to be freed by the caller. + */ +static int +read_config_file(ctx, filename) + struct hesiod_p *ctx; + const char *filename; +{ + char *key, *data, *p, **which; + char buf[MAXDNAME + 7]; + int n; + FILE *fp; + + _DIAGASSERT(ctx != NULL); + _DIAGASSERT(filename != NULL); + + /* Set default query classes. */ + ctx->classes[0] = C_IN; + ctx->classes[1] = C_HS; + + /* Try to open the configuration file. */ + fp = fopen(filename, "r"); + if (!fp) { + /* Use compiled in default domain names. */ + ctx->lhs = strdup(DEF_LHS); + ctx->rhs = strdup(DEF_RHS); + if (ctx->lhs && ctx->rhs) + return 0; + else { + errno = ENOMEM; + return -1; + } + } + ctx->lhs = NULL; + ctx->rhs = NULL; + while (fgets(buf, sizeof(buf), fp) != NULL) { + p = buf; + if (*p == '#' || *p == '\n' || *p == '\r') + continue; + while (*p == ' ' || *p == '\t') + p++; + key = p; + while (*p != ' ' && *p != '\t' && *p != '=' && *p) + p++; + + if (*p == '\0') + continue; + + *p++ = 0; + + while (isspace((u_char) *p) || *p == '=') + p++; + + if (*p == '\0') + continue; + + data = p; + while (!isspace((u_char) *p) && *p) + p++; + + *p = 0; + + if (strcasecmp(key, "lhs") == 0 || + strcasecmp(key, "rhs") == 0) { + which = (strcasecmp(key, "lhs") == 0) + ? &ctx->lhs : &ctx->rhs; + *which = strdup(data); + if (!*which) { + errno = ENOMEM; + (void)fclose(fp); + return -1; + } + } else { + if (strcasecmp(key, "classes") == 0) { + n = 0; + while (*data && n < 2) { + p = data; + while (*p && *p != ',') + p++; + if (*p) + *p++ = 0; + if (strcasecmp(data, "IN") == 0) + ctx->classes[n++] = C_IN; + else + if (strcasecmp(data, "HS") == 0) + ctx->classes[n++] = + C_HS; + data = p; + } + while (n < 2) + ctx->classes[n++] = 0; + } + } + } + fclose(fp); + + if (!ctx->rhs || ctx->classes[0] == 0 || + ctx->classes[0] == ctx->classes[1]) { + errno = ENOEXEC; + return -1; + } + return 0; +} + +/* + * get_txt_records -- + * Given a DNS class and a DNS name, do a lookup for TXT records, and + * return a list of them. + */ +static char ** +get_txt_records(qclass, name) + int qclass; + const char *name; +{ + HEADER *hp; + unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor; + char *dst, **list; + int ancount, qdcount, i, j, n, skip, type, class, len; + res_state res = __res_get_state(); + + if (res == NULL) + return NULL; + + _DIAGASSERT(name != NULL); + + /* Construct the query. */ + n = res_nmkquery(res, QUERY, name, qclass, T_TXT, NULL, 0, + NULL, qbuf, PACKETSZ); + if (n < 0) { + errno = EMSGSIZE; + __res_put_state(res); + return NULL; + } + + /* Send the query. */ + n = res_nsend(res, qbuf, n, abuf, MAX_HESRESP); + __res_put_state(res); + if (n < 0) { + errno = ECONNREFUSED; + return NULL; + } + /* Parse the header of the result. */ + hp = (HEADER *) (void *) abuf; + ancount = ntohs(hp->ancount); + qdcount = ntohs(hp->qdcount); + p = abuf + sizeof(HEADER); + eom = abuf + n; + + /* + * Skip questions, trying to get to the answer section + * which follows. + */ + for (i = 0; i < qdcount; i++) { + skip = dn_skipname(p, eom); + if (skip < 0 || p + skip + QFIXEDSZ > eom) { + errno = EMSGSIZE; + return NULL; + } + p += skip + QFIXEDSZ; + } + + /* Allocate space for the text record answers. */ + list = malloc((ancount + 1) * sizeof(char *)); + if (!list) { + errno = ENOMEM; + return NULL; + } + /* Parse the answers. */ + j = 0; + for (i = 0; i < ancount; i++) { + /* Parse the header of this answer. */ + skip = dn_skipname(p, eom); + if (skip < 0 || p + skip + 10 > eom) + break; + type = p[skip + 0] << 8 | p[skip + 1]; + class = p[skip + 2] << 8 | p[skip + 3]; + len = p[skip + 8] << 8 | p[skip + 9]; + p += skip + 10; + if (p + len > eom) { + errno = EMSGSIZE; + break; + } + /* Skip entries of the wrong class and type. */ + if (class != qclass || type != T_TXT) { + p += len; + continue; + } + /* Allocate space for this answer. */ + list[j] = malloc((size_t)len); + if (!list[j]) { + errno = ENOMEM; + break; + } + dst = list[j++]; + + /* Copy answer data into the allocated area. */ + eor = p + len; + while (p < eor) { + n = (unsigned char) *p++; + if (p + n > eor) { + errno = EMSGSIZE; + break; + } + memcpy(dst, p, (size_t)n); + p += n; + dst += n; + } + if (p < eor) { + errno = EMSGSIZE; + break; + } + *dst = 0; + } + + /* + * If we didn't terminate the loop normally, something + * went wrong. + */ + if (i < ancount) { + for (i = 0; i < j; i++) + free(list[i]); + free(list); + return NULL; + } + if (j == 0) { + errno = ENOENT; + free(list); + return NULL; + } + list[j] = NULL; + return list; +} + +/* + * COMPATIBILITY FUNCTIONS + */ + +static int inited = 0; +static void *context; +static int errval = HES_ER_UNINIT; + +int +hes_init() +{ + init_context(); + return errval; +} + +char * +hes_to_bind(name, type) + const char *name; + const char *type; +{ + static char *bindname; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); + + if (init_context() < 0) + return NULL; + if (bindname) + free(bindname); + bindname = hesiod_to_bind(context, name, type); + if (!bindname) + translate_errors(); + return bindname; +} + +char ** +hes_resolve(name, type) + const char *name; + const char *type; +{ + static char **list; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); + + if (init_context() < 0) + return NULL; + + /* + * In the old Hesiod interface, the caller was responsible for + * freeing the returned strings but not the vector of strings itself. + */ + if (list) + free(list); + + list = hesiod_resolve(context, name, type); + if (!list) + translate_errors(); + return list; +} + +int +hes_error() +{ + return errval; +} + +void +hes_free(hp) + char **hp; +{ + hesiod_free_list(context, hp); +} + +static int +init_context() +{ + if (!inited) { + inited = 1; + if (hesiod_init(&context) < 0) { + errval = HES_ER_CONFIG; + return -1; + } + errval = HES_ER_OK; + } + return 0; +} + +static void +translate_errors() +{ + switch (errno) { + case ENOENT: + errval = HES_ER_NOTFOUND; + break; + case ECONNREFUSED: + case EMSGSIZE: + errval = HES_ER_NET; + break; + default: + /* Not a good match, but the best we can do. */ + errval = HES_ER_CONFIG; + break; + } +} diff --git a/lib/nbsd_libc/net/if_indextoname.3 b/lib/nbsd_libc/net/if_indextoname.3 new file mode 100644 index 000000000..6b27d194a --- /dev/null +++ b/lib/nbsd_libc/net/if_indextoname.3 @@ -0,0 +1,138 @@ +.\" $NetBSD: if_indextoname.3,v 1.12 2010/03/22 19:30:54 joerg Exp $ +.\" $KAME: if_indextoname.3,v 1.10 2000/11/24 08:13:51 itojun Exp $ +.\" BSDI Id: if_indextoname.3,v 2.2 2000/04/17 22:38:05 dab Exp +.\" +.\" Copyright (c) 1997, 2000 +.\" Berkeley Software Design, Inc. 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. +.\" +.\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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 March 11, 2005 +.Dt IF_NAMETOINDEX 3 +.Os +.Sh NAME +.Nm if_nametoindex , +.Nm if_indextoname , +.Nm if_nameindex , +.Nm if_freenameindex +.Nd provide mappings between interface names and indexes +.Sh SYNOPSIS +.In net/if.h +.Ft unsigned int +.Fn if_nametoindex "const char *ifname" +.Ft char * +.Fn if_indextoname "unsigned int ifindex" "char *ifname" +.Ft struct if_nameindex * +.Fn if_nameindex "void" +.Ft void +.Fn if_freenameindex "struct if_nameindex *ptr" +.Sh DESCRIPTION +The +.Fn if_nametoindex +function maps the interface name specified in +.Ar ifname +to its corresponding index. +If the specified interface does not exist, it returns 0. +.Pp +The +.Fn if_indextoname +function maps the interface index specified in +.Ar ifindex +to it corresponding name, which is copied into the +buffer pointed to by +.Ar ifname , +which must be of at least IFNAMSIZ bytes. +This pointer is also the return value of the function. +If there is no interface corresponding to the specified +index, NULL is returned. +.Pp +The +.Fn if_nameindex +function returns an array of +.Nm if_nameindex +structures, one structure per interface, as +defined in the include file +.In net/if.h . +The +.Nm if_nameindex +structure contains at least the following entries: +.Bd -literal + unsigned int if_index; /* 1, 2, ... */ + char *if_name; /* null terminated name: "le0", ... */ +.Ed +.Pp +The end of the array of structures is indicated by a structure with an +.Nm if_index +of 0 and an +.Nm if_name +of NULL. +A NULL pointer is returned upon an error. +.Pp +The +.Fn if_freenameindex +function frees the dynamic memory that was +allocated by +.Fn if_nameindex . +.Sh RETURN VALUES +Upon successful completion, +.Fn if_nametoindex +returns the index number of the interface. +If the interface is not found, a value of 0 is returned and +.Va errno +is set to +.Er ENXIO . +A value of 0 is also returned if an error +occurs while retrieving the list of interfaces via +.Xr getifaddrs 3 . +.Pp +Upon successful completion, +.Fn if_indextoname +returns +.Ar ifname . +If the interface is not found, a NULL pointer is returned and +.Va errno +is set to +.Er ENXIO . +A NULL pointer is also returned if an error +occurs while retrieving the list of interfaces via +.Xr getifaddrs 3 . +.Pp +The +.Fn if_nameindex +returns a NULL pointer if an error +occurs while retrieving the list of interfaces via +.Xr getifaddrs 3 , +or if sufficient memory cannot be allocated. +.Sh SEE ALSO +.Xr getifaddrs 3 , +.Xr networking 4 +.Sh STANDARDS +The +.Fn if_nametoindex , +.Fn if_indextoname , +.Fn if_nameindex , +and +.Fn if_freenameindex +functions conform to +.St -p1003.1-2001 , +.St -xns5.2 , +and RFC 3493. +.Sh HISTORY +The implementation first appeared in +.Bsx . diff --git a/lib/nbsd_libc/net/if_indextoname.c b/lib/nbsd_libc/net/if_indextoname.c new file mode 100644 index 000000000..32877bbd2 --- /dev/null +++ b/lib/nbsd_libc/net/if_indextoname.c @@ -0,0 +1,101 @@ +/* $NetBSD: if_indextoname.c,v 1.7 2010/12/13 23:10:12 pooka Exp $ */ +/* $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $ */ + +/*- + * Copyright (c) 1997, 2000 + * Berkeley Software Design, Inc. 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. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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. + * + * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: if_indextoname.c,v 1.7 2010/12/13 23:10:12 pooka Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef RUMP_ACTION +#include "namespace.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef RUMP_ACTION +#ifdef __weak_alias +__weak_alias(if_indextoname,_if_indextoname) +#endif +#endif + +/* + * From RFC 2533: + * + * The second function maps an interface index into its corresponding + * name. + * + * #include + * + * char *if_indextoname(unsigned int ifindex, char *ifname); + * + * The ifname argument must point to a buffer of at least IF_NAMESIZE + * bytes into which the interface name corresponding to the specified + * index is returned. (IF_NAMESIZE is also defined in and + * its value includes a terminating null byte at the end of the + * interface name.) This pointer is also the return value of the + * function. If there is no interface corresponding to the specified + * index, NULL is returned, and errno is set to ENXIO, if there was a + * system error (such as running out of memory), if_indextoname returns + * NULL and errno would be set to the proper value (e.g., ENOMEM). + */ + +char * +if_indextoname(unsigned int ifindex, char *ifname) +{ + struct ifaddrs *ifaddrs, *ifa; + int error = 0; + + if (getifaddrs(&ifaddrs) < 0) + return(NULL); /* getifaddrs properly set errno */ + + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK && + ifindex == ((struct sockaddr_dl*) + (void *)ifa->ifa_addr)->sdl_index) + break; + } + + if (ifa == NULL) { + error = ENXIO; + ifname = NULL; + } + else + strlcpy(ifname, ifa->ifa_name, IFNAMSIZ); + + freeifaddrs(ifaddrs); + + errno = error; + return(ifname); +} diff --git a/lib/nbsd_libc/net/if_nameindex.c b/lib/nbsd_libc/net/if_nameindex.c new file mode 100644 index 000000000..2be5a28b6 --- /dev/null +++ b/lib/nbsd_libc/net/if_nameindex.c @@ -0,0 +1,156 @@ +/* $NetBSD: if_nameindex.c,v 1.6 2000/12/20 18:47:11 christos Exp $ */ +/* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */ + +/*- + * Copyright (c) 1997, 2000 + * Berkeley Software Design, Inc. 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. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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. + * + * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: if_nameindex.c,v 1.6 2000/12/20 18:47:11 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(if_nameindex,_if_nameindex) +__weak_alias(if_freenameindex,_if_freenameindex) +#endif +/* + * From RFC 2553: + * + * 4.3 Return All Interface Names and Indexes + * + * The if_nameindex structure holds the information about a single + * interface and is defined as a result of including the + * header. + * + * struct if_nameindex { + * unsigned int if_index; + * char *if_name; + * }; + * + * The final function returns an array of if_nameindex structures, one + * structure per interface. + * + * struct if_nameindex *if_nameindex(void); + * + * The end of the array of structures is indicated by a structure with + * an if_index of 0 and an if_name of NULL. The function returns a NULL + * pointer upon an error, and would set errno to the appropriate value. + * + * The memory used for this array of structures along with the interface + * names pointed to by the if_name members is obtained dynamically. + * This memory is freed by the next function. + * + * 4.4. Free Memory + * + * The following function frees the dynamic memory that was allocated by + * if_nameindex(). + * + * #include + * + * void if_freenameindex(struct if_nameindex *ptr); + * + * The argument to this function must be a pointer that was returned by + * if_nameindex(). + */ + +struct if_nameindex * +if_nameindex(void) +{ + struct ifaddrs *ifaddrs, *ifa; + unsigned int ni; + int nbytes; + struct if_nameindex *ifni, *ifni2; + char *cp; + + if (getifaddrs(&ifaddrs) < 0) + return(NULL); + + /* + * First, find out how many interfaces there are, and how + * much space we need for the string names. + */ + ni = 0; + nbytes = 0; + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK) { + nbytes += strlen(ifa->ifa_name) + 1; + ni++; + } + } + + /* + * Next, allocate a chunk of memory, use the first part + * for the array of structures, and the last part for + * the strings. + */ + cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes); + ifni = (struct if_nameindex *)(void *)cp; + if (ifni == NULL) + goto out; + cp += (ni + 1) * sizeof(struct if_nameindex); + + /* + * Now just loop through the list of interfaces again, + * filling in the if_nameindex array and making copies + * of all the strings. + */ + ifni2 = ifni; + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK) { + ifni2->if_index = + ((struct sockaddr_dl*) + (void *)ifa->ifa_addr)->sdl_index; + ifni2->if_name = cp; + strcpy(cp, ifa->ifa_name); + ifni2++; + cp += strlen(cp) + 1; + } + } + /* + * Finally, don't forget to terminate the array. + */ + ifni2->if_index = 0; + ifni2->if_name = NULL; +out: + freeifaddrs(ifaddrs); + return(ifni); +} + +void +if_freenameindex(struct if_nameindex *ptr) +{ + free(ptr); +} diff --git a/lib/nbsd_libc/net/if_nametoindex.c b/lib/nbsd_libc/net/if_nametoindex.c new file mode 100644 index 000000000..3e8297d1b --- /dev/null +++ b/lib/nbsd_libc/net/if_nametoindex.c @@ -0,0 +1,92 @@ +/* $NetBSD: if_nametoindex.c,v 1.4 2000/11/24 08:21:12 itojun Exp $ */ +/* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */ + +/*- + * Copyright (c) 1997, 2000 + * Berkeley Software Design, Inc. 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. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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. + * + * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: if_nametoindex.c,v 1.4 2000/11/24 08:21:12 itojun Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(if_nametoindex,_if_nametoindex) +#endif + +/* + * From RFC 2553: + * + * 4.1 Name-to-Index + * + * + * The first function maps an interface name into its corresponding + * index. + * + * #include + * + * unsigned int if_nametoindex(const char *ifname); + * + * If the specified interface name does not exist, the return value is + * 0, and errno is set to ENXIO. If there was a system error (such as + * running out of memory), the return value is 0 and errno is set to the + * proper value (e.g., ENOMEM). + */ + +unsigned int +if_nametoindex(const char *ifname) +{ + struct ifaddrs *ifaddrs, *ifa; + unsigned int ni; + + if (getifaddrs(&ifaddrs) < 0) + return(0); + + ni = 0; + + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK && + strcmp(ifa->ifa_name, ifname) == 0) { + ni = ((struct sockaddr_dl*) + (void *)ifa->ifa_addr)->sdl_index; + break; + } + } + + freeifaddrs(ifaddrs); + if (!ni) + errno = ENXIO; + return(ni); +} diff --git a/lib/nbsd_libc/net/inet6_opt_init.3 b/lib/nbsd_libc/net/inet6_opt_init.3 new file mode 100644 index 000000000..c5350a96d --- /dev/null +++ b/lib/nbsd_libc/net/inet6_opt_init.3 @@ -0,0 +1,337 @@ +.\" $NetBSD: inet6_opt_init.3,v 1.1 2006/05/05 00:03:21 rpaulo Exp $ +.\" $KAME: inet6_opt_init.3,v 1.7 2004/12/27 05:08:23 itojun Exp $ +.\" +.\" Copyright (C) 2004 WIDE Project. +.\" 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 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 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 PROJECT 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 December 23, 2004 +.Dt INET6_OPT_INIT 3 +.Os +.\" +.Sh NAME +.Nm inet6_opt_init , +.Nm inet6_opt_append , +.Nm inet6_opt_finish , +.Nm inet6_opt_set_val , +.Nm inet6_opt_next , +.Nm inet6_opt_find , +.Nm inet6_opt_get_val +.Nd IPv6 Hop-by-Hop and Destination Options manipulation +.\" +.Sh SYNOPSIS +.In netinet/in.h +.Ft "int" +.Fn inet6_opt_init "void *extbuf" "socklen_t extlen" +.Ft "int" +.Fn inet6_opt_append "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t len" "u_int8_t align" "void **databufp" +.Ft "int" +.Fn inet6_opt_finish "void *extbuf" "socklen_t extlen" "int offset" +.Ft "int" +.Fn inet6_opt_set_val "void *databuf" "int offset" "void *val" "socklen_t vallen" +.Ft "int" +.Fn inet6_opt_next "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t *typep" "socklen_t *lenp" "void **databufp" +.Ft "int" +.Fn inet6_opt_find "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t *lenp" "void **databufp" +.Ft "int" +.Fn inet6_opt_get_val "void *databuf" "socklen_t offset" "void *val" "socklen_t vallen" +.\" +.Sh DESCRIPTION +Building and parsing the Hop-by-Hop and Destination options is +complicated. +The advanced sockets API defines a set of functions to +help applications create and manipulate Hop-by-Hope and Destination +options. +.\"This man page describes the functions specified in +.\"IETF Draft RFC3542 while the +.\".Xr inet6_options_space 3 +.\"man page documents the functions defined in RFC 2292. +.\"It is expected +.\"that this set of functions will supersede those in RFC 2292 but for +.\"the time being both APIs are retained. +These functions use the +formatting rules specified in Appendix B in RFC2460, i.e., that the +largest field is placed last in the option. +The function prototypes +for these functions are all contained in the +.In netinet/in.h +header file. +.\" +.Ss inet6_opt_init +The +.Fn inet6_opt_init +function +returns the number of bytes needed for an empty +extension header, one without any options. +If the +.Va extbuf +argument points to a valid section of memory +then the +.Fn inet6_opt_init +function also initializes the extension header's length field. +When attempting to initialize an extension buffer passed in the +.Va extbuf argument +.Fa extlen +must be a positive multiple of 8 or else the function fails and +returns \-1 to the caller. +.\" +.Ss inet6_opt_append +The +.Fn inet6_opt_append +function can perform to different jobs. +When a valid +.Fa extbuf +argument is supplied it appends an option to the extension buffer and +returns the updated total length as well as a pointer to the newly +created option in +.Fa databufp . +If the value +of +.Fa extbuf +is +.Dv NULL +then the +.Fn inet6_opt_append function only reports what the total length would +be if the option were actually appended. +The +.Fa len +and +.Fa align +arguments specify the length of the option and the required data +alignment which must be used when appending the option. +The +.Fa offset +argument should be the length returned by the +.Fn inet6_opt_init +function or a previous call to +.Fn inet6_opt_append . +.Pp +The +.Fa type +argument is the 8-bit option type. +.Pp +After +.Fn inet6_opt_append +has been called, the application can use the buffer pointed to by +.Fa databufp +directly, or use +.Fn inet6_opt_set_val +to specify the data to be contained in the option. +.Pp +Option types of +.Li 0 +and +.Li 1 +are reserved for the +.Li Pad1 +and +.Li PadN +options. +All other values from 2 through 255 may be used by applications. +.Pp +The length of the option data is contained in an 8-bit value and so +may contain any value from 0 through 255. +.Pp +The +.Fa align +parameter must have a value of 1, 2, 4, or 8 and cannot exceed the +value of +.Fa len . +The alignment values represent no alignment, 16 bit, 32 bit and 64 bit +alignments respectively. +.\" +.Ss inet6_opt_finish +The +.Fn inet6_opt_finish +calculates the final padding necessary to make the extension header a +multiple of 8 bytes, as required by the IPv6 extension header +specification, and returns the extension header's updated total +length. +The +.Fa offset +argument should be the length returned by +.Fn inet6_opt_init +or +.Fn inet6_opt_append . +When +.Fa extbuf +is not +.Dv NULL +the function also sets up the appropriate padding bytes by inserting a +Pad1 or PadN option of the proper length. +.Pp +If the extension header is too small to contain the proper padding +then an error of \-1 is returned to the caller. +.\" +.Ss inet6_opt_set_val +The +.Fn inet6_opt_set_val +function inserts data items of various sizes into the data portion of +the option. +The +.Fa databuf +argument is a pointer to memory that was returned by the +.Fn inet6_opt_append +call and the +.Fa offset argument specifies where the option should be placed in the +data buffer. +The +.Fa val +argument points to an area of memory containing the data to be +inserted into the extension header, and the +.Fa vallen +argument indicates how much data to copy. +.Pp +The caller should ensure that each field is aligned on its natural +boundaries as described in Appendix B of RFC2460. +.Pp +The function returns the offset for the next field which is calculated as +.Fa offset ++ +.Fa vallen +and is used when composing options with multiple fields. +.\" +.Ss inet6_opt_next +The +.Fn inet6_opt_next +function parses received extension headers. +The +.Fa extbuf +and +.Fa extlen +arguments specify the location and length of the extension header +being parsed. +The +.Fa offset +argument should either be zero, for the first option, or the length value +returned by a previous call to +.Fn inet6_opt_next +or +.Fn inet6_opt_find . +The return value specifies the position where to continue scanning the +extension buffer. +The option is returned in the arguments +.Fa typep , lenp , +and +.Fa databufp . +.Fa typep, lenp, +and +.Fa databufp +point to the 8-bit option type, the 8-bit option length and the option +data respectively. +This function does not return any PAD1 or PADN options. +When an error occurs or there are no more options the return +value is \-1. +.\" +.Ss inet6_opt_find +The +.Fn inet6_opt_find +function searches the extension buffer for a particular option type, +passed in through the +.Fa type +argument. +If the option is found then the +.Fa lenp +and +.Fa databufp +arguments are updated to point to the option's length and data +respectively. +.Fa extbuf +and +.Fa extlen +must point to a valid extension buffer and give its length. +The +.Fa offset +argument can be used to search from a location anywhere in the +extension header. +.Ss inet6_opt_get_val +The +.Fn inet6_opt_get_val +function extracts data items of various sizes in the data portion of +the option. +The +.Fa databuf +is a pointer returned by the +.Fn inet6_opt_next +or +.Fn inet6_opt_find +functions. +The +.Fa val +argument points where the data will be extracted. +The +.Fa offset +argument specifies from where in the data portion of the option the +value should be extracted; the first byte of option data is specified +by an offset of zero. +.Pp +It is expected that each field is aligned on its natural boundaries as +described in Appendix B of RFC2460. +.Pp +The function returns the offset for the next field +by calculating +.Fa offset ++ +.Fa vallen +which can be used when extracting option content with multiple fields. +Robust receivers must verify alignment before calling this function. +.\" +.Sh DIAGNOSTICS +All the functions return +\-1 +on an error. +.\" +.Sh EXAMPLES +RFC3542 gives comprehensive examples in Section 23. +.Pp +KAME also provides examples in the +.Pa advapitest +directory of its kit. +.\" +.Sh SEE ALSO +.Rs +.%A W. Stevens +.%A M. Thomas +.%A E. Nordmark +.%A T. Jinmei +.%T "Advanced Sockets API for IPv6" +.%N RFC3542 +.%D October 2002 +.Re +.Rs +.%A S. Deering +.%A R. Hinden +.%T "Internet Protocol, Version 6 (IPv6) Specification" +.%N RFC2460 +.%D December 1998 +.Re +.Sh HISTORY +The implementation first appeared in KAME advanced networking kit. +.Sh STANDARDS +The functions are documented in +.Dq Advanced Sockets API for IPv6 +.Pq RFC3542 . +.\" diff --git a/lib/nbsd_libc/net/inet6_option_space.3 b/lib/nbsd_libc/net/inet6_option_space.3 new file mode 100644 index 000000000..28aadacc3 --- /dev/null +++ b/lib/nbsd_libc/net/inet6_option_space.3 @@ -0,0 +1,448 @@ +.\" $NetBSD: inet6_option_space.3,v 1.17 2010/03/22 19:30:54 joerg Exp $ +.\" $KAME: inet6_option_space.3,v 1.7 2000/05/17 14:32:13 itojun Exp $ +.\" +.\" Copyright (c) 1983, 1987, 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. +.\" +.Dd December 10, 1999 +.Dt INET6_OPTION_SPACE 3 +.Os +.\" +.Sh NAME +.Nm inet6_option_space , +.Nm inet6_option_init , +.Nm inet6_option_append , +.Nm inet6_option_alloc , +.Nm inet6_option_next , +.Nm inet6_option_find +.Nd IPv6 Hop-by-Hop and Destination Options manipulation +.\" +.Sh SYNOPSIS +.In netinet/in.h +.Ft "int" +.Fn inet6_option_space "int nbytes" +.Ft "int" +.Fn inet6_option_init "void *bp" "struct cmsghdr **cmsgp" "int type" +.Ft "int" +.Fn inet6_option_append "struct cmsghdr *cmsg" "const uint8_t *typep" "int multx" "int plusy" +.Ft "uint8_t *" +.Fn inet6_option_alloc "struct cmsghdr *cmsg" "int datalen" "int multx" "int plusy" +.Ft "int" +.Fn inet6_option_next "const struct cmsghdr *cmsg" "uint8_t **tptrp" +.Ft "int" +.Fn inet6_option_find "const struct cmsghdr *cmsg" "uint8_t **tptrp" "int type" +.\" +.Sh DESCRIPTION +.\" +Building and parsing the Hop-by-Hop and Destination options is +complicated due to alignment constraints, padding and +ancillary data manipulation. +RFC 2292 defines a set of functions to help the application. +The function prototypes for +these functions are all in the +.In netinet/in.h +header. +.\" +.Ss inet6_option_space +.Fn inet6_option_space +returns the number of bytes required to hold an option when it is stored as +ancillary data, including the +.Li cmsghdr +structure at the beginning, +and any padding at the end +.Po +to make its size a multiple of 8 bytes +.Pc . +The argument is the size of the structure defining the option, +which must include any pad bytes at the beginning +.Po +the value +.Li y +in the alignment term +.Dq Li xn + y +.Pc , +the type byte, the length byte, and the option data. +.Pp +Note: If multiple options are stored in a single ancillary data +object, which is the recommended technique, this function +overestimates the amount of space required by the size of +.Li N-1 +.Li cmsghdr +structures, +where +.Li N +is the number of options to be stored in the object. +This is of little consequence, since it is assumed that most +Hop-by-Hop option headers and Destination option headers carry only +one option +.Pq appendix B of [RFC 2460] . +.\" +.Ss inet6_option_init +.Fn inet6_option_init +is called once per ancillary data object that will +contain either Hop-by-Hop or Destination options. +It returns +.Li 0 +on success or +.Li -1 +on an error. +.Pp +.Fa bp +is a pointer to previously allocated space that will contain the +ancillary data object. +It must be large enough to contain all the +individual options to be added by later calls to +.Fn inet6_option_append +and +.Fn inet6_option_alloc . +.Pp +.Fa cmsgp +is a pointer to a pointer to a +.Li cmsghdr +structure. +.Fa *cmsgp +is initialized by this function to point to the +.Li cmsghdr +structure constructed by this function in the buffer pointed to by +.Fa bp . +.Pp +.Fa type +is either +.Dv IPV6_HOPOPTS +or +.Dv IPV6_DSTOPTS . +This +.Fa type +is stored in the +.Li cmsg_type +member of the +.Li cmsghdr +structure pointed to by +.Fa *cmsgp . +.\" +.Ss inet6_option_append +This function appends a Hop-by-Hop option or a Destination option +into an ancillary data object that has been initialized by +.Fn inet6_option_init . +This function returns +.Li 0 +if it succeeds or +.Li -1 +on an error. +.Pp +.Fa cmsg +is a pointer to the +.Li cmsghdr +structure that must have been +initialized by +.Fn inet6_option_init . +.Pp +.Fa typep +is a pointer to the 8-bit option type. +It is assumed that this +field is immediately followed by the 8-bit option data length field, +which is then followed immediately by the option data. +The caller +initializes these three fields +.Pq the type-length-value, or TLV +before calling this function. +.Pp +The option type must have a value from +.Li 2 +to +.Li 255 , +inclusive. +.Po +.Li 0 +and +.Li 1 +are reserved for the +.Li Pad1 +and +.Li PadN +options, respectively. +.Pc +.Pp +The option data length must have a value between +.Li 0 +and +.Li 255 , +inclusive, and is the length of the option data that follows. +.Pp +.Fa multx +is the value +.Li x +in the alignment term +.Dq Li xn + y . +It must have a value of +.Li 1 , +.Li 2 , +.Li 4 , +or +.Li 8 . +.Pp +.Fa plusy +is the value +.Li y +in the alignment term +.Dq Li xn + y . +It must have a value between +.Li 0 +and +.Li 7 , +inclusive. +.\" +.Ss inet6_option_alloc +This function appends a Hop-by-Hop option or a Destination option +into an ancillary data object that has been initialized by +.Fn inet6_option_init . +This function returns a pointer to the 8-bit +option type field that starts the option on success, or +.Dv NULL +on an error. +.Pp +The difference between this function and +.Fn inet6_option_append +is that the latter copies the contents of a previously built option into +the ancillary data object while the current function returns a +pointer to the space in the data object where the option's TLV must +then be built by the caller. +.Pp +.Fa cmsg +is a pointer to the +.Li cmsghdr +structure that must have been +initialized by +.Fn inet6_option_init . +.Pp +.Fa datalen +is the value of the option data length byte for this option. +This value is required as an argument to allow the function to +determine if padding must be appended at the end of the option. +.Po +The +.Fn inet6_option_append +function does not need a data length argument +since the option data length must already be stored by the caller. +.Pc +.Pp +.Fa multx +is the value +.Li x +in the alignment term +.Dq Li xn + y . +It must have a value of +.Li 1 , +.Li 2 , +.Li 4 , +or +.Li 8 . +.Pp +.Fa plusy +is the value +.Li y +in the alignment term +.Dq Li xn + y . +It must have a value between +.Li 0 +and +.Li 7 , +inclusive. +.\" +.Ss inet6_option_next +This function processes the next Hop-by-Hop option or Destination +option in an ancillary data object. +If another option remains to be +processed, the return value of the function is +.Li 0 +and +.Fa *tptrp +points to +the 8-bit option type field +.Po +which is followed by the 8-bit option +data length, followed by the option data +.Pc . +If no more options remain +to be processed, the return value is +.Li -1 +and +.Fa *tptrp +is +.Dv NULL . +If an error occurs, the return value is +.Li -1 +and +.Fa *tptrp +is not +.Dv NULL . +.Pp +.Fa cmsg +is a pointer to +.Li cmsghdr +structure of which +.Li cmsg_level +equals +.Dv IPPROTO_IPV6 +and +.Li cmsg_type +equals either +.Dv IPV6_HOPOPTS +or +.Dv IPV6_DSTOPTS . +.Pp +.Fa tptrp +is a pointer to a pointer to an 8-bit byte and +.Fa *tptrp +is used +by the function to remember its place in the ancillary data object +each time the function is called. +The first time this function is +called for a given ancillary data object, +.Fa *tptrp +must be set to +.Dv NULL . +.Pp +Each time this function returns success, +.Fa *tptrp +points to the 8-bit +option type field for the next option to be processed. +.\" +.Ss inet6_option_find +This function is similar to the previously described +.Fn inet6_option_next +function, except this function lets the caller +specify the option type to be searched for, instead of always +returning the next option in the ancillary data object. +.Fa cmsg +is a +pointer to +.Li cmsghdr +structure of which +.Li cmsg_level +equals +.Dv IPPROTO_IPV6 +and +.Li cmsg_type +equals either +.Dv IPV6_HOPOPTS +or +.Dv IPV6_DSTOPTS . +.Pp +.Fa tptrp +is a pointer to a pointer to an 8-bit byte and +.Fa *tptrp +is used +by the function to remember its place in the ancillary data object +each time the function is called. +The first time this function is +called for a given ancillary data object, +.Fa *tptrp +must be set to +.Dv NULL . +.Pa +This function starts searching for an option of the specified type +beginning after the value of +.Fa *tptrp . +If an option of the specified +type is located, this function returns +.Li 0 +and +.Fa *tptrp +points to the 8- +bit option type field for the option of the specified type. +If an +option of the specified type is not located, the return value is +.Li -1 +and +.Fa *tptrp +is +.Dv NULL . +If an error occurs, the return value is +.Li -1 +and +.Fa *tptrp +is not +.Dv NULL . +.\" +.Sh EXAMPLES +RFC 2292 gives comprehensive examples in chapter 6. +.\" +.Sh DIAGNOSTICS +.Fn inet6_option_init +and +.Fn inet6_option_append +return +.Li 0 +on success or +.Li -1 +on an error. +.Pp +.Fn inet6_option_alloc +returns +.Dv NULL +on an error. +.Pp +On errors, +.Fn inet6_option_next +and +.Fn inet6_option_find +return +.Li -1 +setting +.Fa *tptrp +to non +.Dv NULL +value. +.\" +.Sh SEE ALSO +.Rs +.%A W. Stevens +.%A M. Thomas +.%T "Advanced Sockets API for IPv6" +.%N RFC 2292 +.%D February 1998 +.Re +.Rs +.%A S. Deering +.%A R. Hinden +.%T "Internet Protocol, Version 6 (IPv6) Specification" +.%N RFC 2460 +.%D December 1998 +.Re +.\" +.Sh STANDARDS +The functions +are documented in +.Dq Advanced Sockets API for IPv6 +.Pq RFC 2292 . +.\" +.Sh HISTORY +The implementation first appeared in KAME advanced networking kit. +.\" +.Sh BUGS +The text was shamelessly copied from RFC 2292. diff --git a/lib/nbsd_libc/net/inet6_rth_space.3 b/lib/nbsd_libc/net/inet6_rth_space.3 new file mode 100644 index 000000000..6aac100a4 --- /dev/null +++ b/lib/nbsd_libc/net/inet6_rth_space.3 @@ -0,0 +1,223 @@ +.\" $NetBSD: inet6_rth_space.3,v 1.1 2006/05/05 00:03:21 rpaulo Exp $ +.\" $KAME: inet6_rth_space.3,v 1.7 2005/01/05 03:00:44 itojun Exp $ +.\" +.\" Copyright (C) 2004 WIDE Project. +.\" 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 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 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 PROJECT 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 December 24, 2004 +.Dt INET6_RTH_SPACE 3 +.Os +.\" +.Sh NAME +.Nm inet6_rth_space , +.Nm inet6_rth_init , +.Nm inet6_rth_add , +.Nm inet6_rth_reverse , +.Nm inet6_rth_segments , +.Nm inet6_rth_getaddr +.Nd IPv6 Routing Header Options manipulation +.\" +.Sh SYNOPSIS +.In netinet/in.h +.Ft socklen_t +.Fn inet6_rth_space "int" "int" +.Ft "void *" +.Fn inet6_rth_init "void *" "socklen_t" "int" "int" +.Ft int +.Fn inet6_rth_add "void *" "const struct in6_addr *" +.Ft int +.Fn inet6_rth_reverse "const void *" "void *" +.Ft int +.Fn inet6_rth_segments "const void *" +.Ft "struct in6_addr *" +.Fn inet6_rth_getaddr "const void *" "int" +.\" +.Sh DESCRIPTION +The IPv6 Advanced API, RFC 3542, defines the functions that an +application calls to build and examine IPv6 Routing headers. +Routing headers are used to perform source routing in IPv6 networks. +The RFC uses the word +.Dq segments +to describe addresses and that is the term used here as well. +All of the functions are defined in the +.In netinet/in.h +header file. +The functions described in this manual page all operate +on routing header structures which are defined in +.In netinet/ip6.h +but which should not need to be modified outside the use of this API. +The size and shape of the route header structures may change, so using +the APIs is a more portable, long term, solution. +.Pp +The functions in the API are split into two groups, those that build a +routing header and those that parse a received routing header. +We will describe the builder functions followed by the parser functions. +.Ss inet6_rth_space +The +.Fn inet6_rth_space +function returns the number of bytes required to hold a Routing Header +of the type, specified in the +.Fa type +argument and containing the number of addresses specified in the +.Fa segments +argumment. +When the type is +.Dv IPV6_RTHDR_TYPE_0 +the number of segments must be from 0 through 127. +Routing headers of type +.Dv IPV6_RTHDR_TYPE_2 +contain only one segment, and are only used with Mobile IPv6. +The return value from this function is the number of bytes required to +store the routing header. +If the value 0 is returned then either the +route header type was not recognized or another error occurred. +.Ss inet6_rth_init +The +.Fn inet6_rth_init +function initializes the pre-allocated buffer pointed to by +.Fa bp +to contain a routing header of the specified type The +.Fa bp_len +argument is used to verify that the buffer is large enough. +The caller must allocate the buffer pointed to by bp. +The necessary buffer size should be determined by calling +.Fn inet6_rth_space +described in the previous sections. +.Pp +The +.Fn inet6_rth_init +function returns a pointer to +.Fa bp +on success and +.Dv NULL +when there is an error. +.Ss inet6_rth_add +The +.Fn inet6_rth_add +function adds the IPv6 address pointed to by +.Fa addr +to the end of the routing header being constructed. +.Pp +A successful addition results in the function returning 0, otherwise +\-1 is returned. +.Ss inet6_rth_reverse +The +.Fn inet6_rth_reverse +function takes a routing header, pointed to by the +argument +.Fa in , +and writes a new routing header into the argument pointed to by +.Fa out . +The routing header at that sends datagrams along the reverse of that +route. +Both arguments are allowed to point to the same buffer meaning +that the reversal can occur in place. +.Pp +The return value of the function is 0 on success, or \-1 when +there is an error. +.\" +.Pp +The next set of functions operate on a routing header that the +application wants to parse. +In the usual case such a routing header +is received from the network, although these functions can also be +used with routing headers that the application itself created. +.Ss inet6_rth_segments +The +.Fn inet6_rth_segments +function returns the number of segments contained in the +routing header pointed to by +.Fa bp . +The return value is the number of segments contained in the routing +header, or \-1 if an error occurred. +It is not an error for 0 to be +returned as a routing header may contain 0 segments. +.\" +.Ss inet6_rth_getaddr +The +.Fn inet6_rth_getaddr +function is used to retrieve a single address from a routing header. +The +.Fa index +is the location in the routing header from which the application wants +to retrieve an address. +The +.Fa index +parameter must have a value between 0 and one less than the number of +segments present in the routing header. +The +.Fn inet6_rth_segments +function, described in the last section, should be used to determine +the total number of segments in the routing header. +The +.Fn inet6_rth_getaddr +function returns a pointer to an IPv6 address on success or +.Dv NULL +when an error has occurred. +.\" +.Sh DIAGNOSTICS +The +.Fn inet6_rth_space +and +.Fn inet6_rth_getaddr +functions return 0 on errors. +.Pp +The +.Fn inet6_rthdr_init +function returns +.Dv NULL +on error. +The +.Fn inet6_rth_add +and +.Fn inet6_rth_reverse +functions return 0 on success, or \-1 upon an error. +.\" +.Sh EXAMPLES +RFC 3542 gives extensive examples in Section 21, Appendix B. +.Pp +KAME also provides examples in the advapitest directory of its kit. +.\" +.Sh SEE ALSO +.Rs +.%A W. Stevens +.%A M. Thomas +.%A E. Nordmark +.%A T. Jinmei +.%T "Advanced Sockets API for IPv6" +.%N RFC 3542 +.%D May 2003 +.Re +.Rs +.%A S. Deering +.%A R. Hinden +.%T "Internet Protocol, Version 6 (IPv6) Specification" +.%N RFC2460 +.%D December 1998 +.Re +.Sh HISTORY +The implementation first appeared in KAME advanced networking kit. diff --git a/lib/nbsd_libc/net/inet6_rthdr_space.3 b/lib/nbsd_libc/net/inet6_rthdr_space.3 new file mode 100644 index 000000000..1d9a04396 --- /dev/null +++ b/lib/nbsd_libc/net/inet6_rthdr_space.3 @@ -0,0 +1,321 @@ +.\" $NetBSD: inet6_rthdr_space.3,v 1.16 2010/03/22 19:30:54 joerg Exp $ +.\" $KAME: inet6_rthdr_space.3,v 1.8 2000/05/17 14:30:15 itojun Exp $ +.\" +.\" Copyright (c) 1983, 1987, 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. +.\" +.Dd December 10, 1999 +.Dt INET6_RTHDR_SPACE 3 +.Os +.\" +.Sh NAME +.Nm inet6_rthdr_space , +.Nm inet6_rthdr_init , +.Nm inet6_rthdr_add , +.Nm inet6_rthdr_lasthop , +.Nm inet6_rthdr_reverse , +.Nm inet6_rthdr_segments , +.Nm inet6_rthdr_getaddr , +.Nm inet6_rthdr_getflags +.Nd IPv6 Routing Header Options manipulation +.\" +.Sh SYNOPSIS +.In netinet/in.h +.Ft size_t +.Fn inet6_rthdr_space "int type" "int segments" +.Ft "struct cmsghdr *" +.Fn inet6_rthdr_init "void *bp" "int type" +.Ft int +.Fn inet6_rthdr_add "struct cmsghdr *cmsg" "const struct in6_addr *addr" "unsigned int flags" +.Ft int +.Fn inet6_rthdr_lasthop "struct cmsghdr *cmsg" "unsigned int flags" +.Ft int +.Fn inet6_rthdr_reverse "const struct cmsghdr *in" "struct cmsghdr *out" +.Ft int +.Fn inet6_rthdr_segments "const struct cmsghdr *cmsg" +.Ft "struct in6_addr *" +.Fn inet6_rthdr_getaddr "struct cmsghdr *cmsg" "int index" +.Ft int +.Fn inet6_rthdr_getflags "const struct cmsghdr *cmsg" "int index" +.\" +.Sh DESCRIPTION +RFC 2292 IPv6 advanced API defines eight +functions that the application calls to build and examine a Routing +header. +Four functions build a Routing header: +.Bl -hang +.It Fn inet6_rthdr_space +return #bytes required for ancillary data +.It Fn inet6_rthdr_init +initialize ancillary data for Routing header +.It Fn inet6_rthdr_add +add IPv6 address \*[Am] flags to Routing header +.It Fn inet6_rthdr_lasthop +specify the flags for the final hop +.El +.Pp +Four functions deal with a returned Routing header: +.Bl -hang +.It Fn inet6_rthdr_reverse +reverse a Routing header +.It Fn inet6_rthdr_segments +return #segments in a Routing header +.It Fn inet6_rthdr_getaddr +fetch one address from a Routing header +.It Fn inet6_rthdr_getflags +fetch one flag from a Routing header +.El +.Pp +The function prototypes for these functions are all in the +.In netinet/in.h +header. +.\" +.Ss inet6_rthdr_space +This function returns the number of bytes required to hold a Routing +header of the specified +.Fa type +containing the specified number of +.Fa segments +.Pq addresses . +For an IPv6 Type 0 Routing header, the number +of segments must be between 1 and 23, inclusive. +The return value +includes the size of the cmsghdr structure that precedes the Routing +header, and any required padding. +.Pp +If the return value is 0, then either the type of the Routing header +is not supported by this implementation or the number of segments is +invalid for this type of Routing header. +.Pp +Note: This function returns the size but does not allocate the space +required for the ancillary data. +This allows an application to +allocate a larger buffer, if other ancillary data objects are +desired, since all the ancillary data objects must be specified to +.Xr sendmsg 2 +as a single +.Li msg_control +buffer. +.\" +.Ss inet6_rthdr_init +This function initializes the buffer pointed to by +.Fa bp +to contain a +.Li cmsghdr +structure followed by a Routing header of the specified +.Fa type . +The +.Li cmsg_len +member of the +.Li cmsghdr +structure is initialized to the +size of the structure plus the amount of space required by the +Routing header. +The +.Li cmsg_level +and +.Li cmsg_type +members are also initialized as required. +.Pp +The caller must allocate the buffer and its size can be determined by +calling +.Fn inet6_rthdr_space . +.Pp +Upon success the return value is the pointer to the +.Li cmsghdr +structure, and this is then used as the first argument to the next +two functions. +Upon an error the return value is +.Dv NULL . +.\" +.Ss inet6_rthdr_add +This function adds the address pointed to by +.Fa addr +to the end of the +Routing header being constructed and sets the type of this hop to the +value of +.Fa flags . +For an IPv6 Type 0 Routing header, +.Fa flags +must be +either +.Dv IPV6_RTHDR_LOOSE +or +.Dv IPV6_RTHDR_STRICT . +.Pp +If successful, the +.Li cmsg_len +member of the +.Li cmsghdr +structure is +updated to account for the new address in the Routing header and the +return value of the function is 0. +Upon an error the return value of +the function is -1. +.\" +.Ss inet6_rthdr_lasthop +This function specifies the Strict/Loose flag for the final hop of a +Routing header. +For an IPv6 Type 0 Routing header, +.Fa flags +must be either +.Dv IPV6_RTHDR_LOOSE +or +.Dv IPV6_RTHDR_STRICT . +.Pp +The return value of the function is 0 upon success, or -1 upon an error. +.Pp +Notice that a Routing header specifying +.Li N +intermediate nodes requires +.Li N+1 +Strict/Loose flags. +This requires +.Li N +calls to +.Fn inet6_rthdr_add +followed by one call to +.Fn inet6_rthdr_lasthop . +.\" +.Ss inet6_rthdr_reverse +This function takes a Routing header that was received as ancillary +data +.Po +pointed to by the first argument, +.Fa in +.Pc +and writes a new Routing +header that sends datagrams along the reverse of that route. +Both +arguments are allowed to point to the same buffer +.Pq that is, the reversal can occur in place . +.Pp +The return value of the function is 0 on success, or -1 upon an +error. +.\" +.Ss inet6_rthdr_segments +This function returns the number of segments +.Pq addresses +contained in +the Routing header described by +.Fa cmsg . +On success the return value is +between 1 and 23, inclusive. +The return value of the function is -1 upon an error. +.\" +.Ss inet6_rthdr_getaddr +This function returns a pointer to the IPv6 address specified by +.Fa index +.Po +which must have a value between 1 and the value returned by +.Fn inet6_rthdr_segments +.Pc +in the Routing header described by +.Fa cmsg . +An +application should first call +.Fn inet6_rthdr_segments +to obtain the number of segments in the Routing header. +.Pp +Upon an error the return value of the function is +.Dv NULL . +.\" +.Ss inet6_rthdr_getflags +This function returns the flags value specified by +.Fa index +.Po +which must +have a value between 0 and the value returned by +.Fn inet6_rthdr_segments +.Pc +in the Routing header described by +.Fa cmsg . +For an IPv6 Type 0 Routing header the return value will be either +.Dv IPV6_RTHDR_LOOSE +or +.Dv IPV6_RTHDR_STRICT . +.Pp +Upon an error the return value of the function is -1. +.Pp +Note: Addresses are indexed starting at 1, and flags starting at 0, +to maintain consistency with the terminology and figures in RFC 2460. +.\" +.Sh EXAMPLES +RFC 2292 gives comprehensive examples in chapter 8. +.\" +.Sh DIAGNOSTICS +.Fn inet6_rthdr_space +returns 0 on errors. +.Pp +.Fn inet6_rthdr_add , +.Fn inet6_rthdr_lasthop +and +.Fn inet6_rthdr_reverse +return 0 on success, and returns -1 on error. +.Pp +.Fn inet6_rthdr_init +and +.Fn inet6_rthdr_getaddr +return +.Dv NULL +on error. +.Pp +.Fn inet6_rthdr_segments +and +.Fn inet6_rthdr_getflags +return -1 on error. +.\" +.Sh SEE ALSO +.Rs +.%A W. Stevens +.%A M. Thomas +.%T "Advanced Sockets API for IPv6" +.%N RFC 2292 +.%D February 1998 +.Re +.Rs +.%A S. Deering +.%A R. Hinden +.%T "Internet Protocol, Version 6 (IPv6) Specification" +.%N RFC 2460 +.%D December 1998 +.Re +.\" +.Sh STANDARDS +The functions +are documented in +.Dq Advanced Sockets API for IPv6 +.Pq RFC 2292 . +.\" +.Sh HISTORY +The implementation first appeared in KAME advanced networking kit. +.\" +.Sh BUGS +The text was shamelessly copied from RFC 2292. +.Pp +.Fn inet6_rthdr_reverse +is not implemented yet. diff --git a/lib/nbsd_libc/net/ip6opt.c b/lib/nbsd_libc/net/ip6opt.c new file mode 100644 index 000000000..f7a9380f3 --- /dev/null +++ b/lib/nbsd_libc/net/ip6opt.c @@ -0,0 +1,658 @@ +/* $NetBSD: ip6opt.c,v 1.12 2009/01/30 23:43:30 lukem Exp $ */ + +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * 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 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 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 PROJECT 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ip6opt.c,v 1.12 2009/01/30 23:43:30 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet6_option_alloc,_inet6_option_alloc) +__weak_alias(inet6_option_append,_inet6_option_append) +__weak_alias(inet6_option_find,_inet6_option_find) +__weak_alias(inet6_option_init,_inet6_option_init) +__weak_alias(inet6_option_next,_inet6_option_next) +__weak_alias(inet6_option_space,_inet6_option_space) +__weak_alias(inet6_opt_init, _inet6_opt_init) +__weak_alias(inet6_opt_append, _inet6_opt_append) +__weak_alias(inet6_opt_finish, _inet6_opt_finish) +__weak_alias(inet6_opt_set_val, _inet6_opt_set_val) +__weak_alias(inet6_opt_next, _inet6_opt_next) +__weak_alias(inet6_opt_find, _inet6_opt_find) +__weak_alias(inet6_opt_get_val, _inet6_opt_get_val) +#endif + +static int ip6optlen(u_int8_t *opt, u_int8_t *lim); +static void inet6_insert_padopt(u_char *p, size_t len); + +/* + * This function returns the number of bytes required to hold an option + * when it is stored as ancillary data, including the cmsghdr structure + * at the beginning, and any padding at the end (to make its size a + * multiple of 8 bytes). The argument is the size of the structure + * defining the option, which must include any pad bytes at the + * beginning (the value y in the alignment term "xn + y"), the type + * byte, the length byte, and the option data. + */ +int +inet6_option_space(nbytes) + int nbytes; +{ + nbytes += 2; /* we need space for nxt-hdr and length fields */ + return(CMSG_SPACE((nbytes + 7) & ~7)); +} + +/* + * This function is called once per ancillary data object that will + * contain either Hop-by-Hop or Destination options. It returns 0 on + * success or -1 on an error. + */ +int +inet6_option_init(bp, cmsgp, type) + void *bp; + struct cmsghdr **cmsgp; + int type; +{ + register struct cmsghdr *ch; + + _DIAGASSERT(bp != NULL); + _DIAGASSERT(cmsgp != NULL); + + ch = (struct cmsghdr *)bp; + + /* argument validation */ + if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS) + return(-1); + + ch->cmsg_level = IPPROTO_IPV6; + ch->cmsg_type = type; + ch->cmsg_len = CMSG_LEN(0); + + *cmsgp = ch; + return(0); +} + +/* + * This function appends a Hop-by-Hop option or a Destination option + * into an ancillary data object that has been initialized by + * inet6_option_init(). This function returns 0 if it succeeds or -1 on + * an error. + * multx is the value x in the alignment term "xn + y" described + * earlier. It must have a value of 1, 2, 4, or 8. + * plusy is the value y in the alignment term "xn + y" described + * earlier. It must have a value between 0 and 7, inclusive. + */ +int +inet6_option_append(cmsg, typep, multx, plusy) + struct cmsghdr *cmsg; + const u_int8_t *typep; + int multx; + int plusy; +{ + size_t padlen, optlen, off; + register u_char *bp; + struct ip6_ext *eh; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(typep != NULL); + + bp = (u_char *)(void *)cmsg + cmsg->cmsg_len; + eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg); + + /* argument validation */ + if (multx != 1 && multx != 2 && multx != 4 && multx != 8) + return(-1); + if (plusy < 0 || plusy > 7) + return(-1); + + /* + * If this is the first option, allocate space for the + * first 2 bytes(for next header and length fields) of + * the option header. + */ + if (bp == (u_char *)(void *)eh) { + bp += 2; + cmsg->cmsg_len += 2; + } + + /* calculate pad length before the option. */ + off = bp - (u_char *)(void *)eh; + padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - + (off % multx); + padlen += plusy; + padlen %= multx; /* keep the pad as short as possible */ + /* insert padding */ + inet6_insert_padopt(bp, padlen); + cmsg->cmsg_len += padlen; + bp += padlen; + + /* copy the option */ + if (typep[0] == IP6OPT_PAD1) + optlen = 1; + else + optlen = typep[1] + 2; + memcpy(bp, typep, (size_t)optlen); + bp += optlen; + cmsg->cmsg_len += optlen; + + /* calculate pad length after the option and insert the padding */ + off = bp - (u_char *)(void *)eh; + padlen = ((off + 7) & ~7) - off; + inet6_insert_padopt(bp, padlen); + bp += padlen; + cmsg->cmsg_len += padlen; + + /* update the length field of the ip6 option header */ + off = bp - (u_char *)(void *)eh; + eh->ip6e_len = (off >> 3) - 1; + + return(0); +} + +/* + * This function appends a Hop-by-Hop option or a Destination option + * into an ancillary data object that has been initialized by + * inet6_option_init(). This function returns a pointer to the 8-bit + * option type field that starts the option on success, or NULL on an + * error. + * The difference between this function and inet6_option_append() is + * that the latter copies the contents of a previously built option into + * the ancillary data object while the current function returns a + * pointer to the space in the data object where the option's TLV must + * then be built by the caller. + * + */ +u_int8_t * +inet6_option_alloc(cmsg, datalen, multx, plusy) + struct cmsghdr *cmsg; + int datalen; + int multx; + int plusy; +{ + size_t padlen, off; + register u_int8_t *bp; + u_int8_t *retval; + struct ip6_ext *eh; + + _DIAGASSERT(cmsg != NULL); + + bp = (u_char *)(void *)cmsg + cmsg->cmsg_len; + eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg); + + /* argument validation */ + if (multx != 1 && multx != 2 && multx != 4 && multx != 8) + return(NULL); + if (plusy < 0 || plusy > 7) + return(NULL); + + /* + * If this is the first option, allocate space for the + * first 2 bytes(for next header and length fields) of + * the option header. + */ + if (bp == (u_char *)(void *)eh) { + bp += 2; + cmsg->cmsg_len += 2; + } + + /* calculate pad length before the option. */ + off = bp - (u_char *)(void *)eh; + padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - + (off % multx); + padlen += plusy; + padlen %= multx; /* keep the pad as short as possible */ + /* insert padding */ + inet6_insert_padopt(bp, padlen); + cmsg->cmsg_len += padlen; + bp += padlen; + + /* keep space to store specified length of data */ + retval = bp; + bp += datalen; + cmsg->cmsg_len += datalen; + + /* calculate pad length after the option and insert the padding */ + off = bp - (u_char *)(void *)eh; + padlen = ((off + 7) & ~7) - off; + inet6_insert_padopt(bp, padlen); + bp += padlen; + cmsg->cmsg_len += padlen; + + /* update the length field of the ip6 option header */ + off = bp - (u_char *)(void *)eh; + eh->ip6e_len = (off >> 3) - 1; + + return(retval); +} + +/* + * This function processes the next Hop-by-Hop option or Destination + * option in an ancillary data object. If another option remains to be + * processed, the return value of the function is 0 and *tptrp points to + * the 8-bit option type field (which is followed by the 8-bit option + * data length, followed by the option data). If no more options remain + * to be processed, the return value is -1 and *tptrp is NULL. If an + * error occurs, the return value is -1 and *tptrp is not NULL. + * (RFC 2292, 6.3.5) + */ +int +inet6_option_next(cmsg, tptrp) + const struct cmsghdr *cmsg; + u_int8_t **tptrp; +{ + struct ip6_ext *ip6e; + int hdrlen, optlen; + u_int8_t *lim; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(tptrp != NULL); + + if (cmsg->cmsg_level != IPPROTO_IPV6 || + (cmsg->cmsg_type != IPV6_HOPOPTS && + cmsg->cmsg_type != IPV6_DSTOPTS)) + return(-1); + + /* message length validation */ + if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) + return(-1); + ip6e = __UNCONST(CCMSG_DATA(cmsg)); + hdrlen = (ip6e->ip6e_len + 1) << 3; + if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) + return(-1); + + /* + * If the caller does not specify the starting point, + * simply return the 1st option. + * Otherwise, search the option list for the next option. + */ + lim = (u_int8_t *)(void *)ip6e + hdrlen; + if (*tptrp == NULL) + *tptrp = (u_int8_t *)(void *)(ip6e + 1); + else { + if ((optlen = ip6optlen(*tptrp, lim)) == 0) + return(-1); + + *tptrp = *tptrp + optlen; + } + if (*tptrp >= lim) { /* there is no option */ + *tptrp = NULL; + return(-1); + } + /* + * Finally, checks if the next option is safely stored in the + * cmsg data. + */ + if (ip6optlen(*tptrp, lim) == 0) + return(-1); + else + return(0); +} + +/* + * This function is similar to the inet6_option_next() function, + * except this function lets the caller specify the option type to be + * searched for, instead of always returning the next option in the + * ancillary data object. + * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think + * it's a typo. The variable should be type of u_int8_t **. + */ +int +inet6_option_find(cmsg, tptrp, type) + const struct cmsghdr *cmsg; + u_int8_t **tptrp; + int type; +{ + struct ip6_ext *ip6e; + int hdrlen, optlen; + u_int8_t *optp, *lim; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(tptrp != NULL); + + if (cmsg->cmsg_level != IPPROTO_IPV6 || + (cmsg->cmsg_type != IPV6_HOPOPTS && + cmsg->cmsg_type != IPV6_DSTOPTS)) + return(-1); + + /* message length validation */ + if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) + return(-1); + ip6e = __UNCONST(CCMSG_DATA(cmsg)); + hdrlen = (ip6e->ip6e_len + 1) << 3; + if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) + return(-1); + + /* + * If the caller does not specify the starting point, + * search from the beginning of the option list. + * Otherwise, search from *the next option* of the specified point. + */ + lim = (u_int8_t *)(void *)ip6e + hdrlen; + if (*tptrp == NULL) + *tptrp = (u_int8_t *)(void *)(ip6e + 1); + else { + if ((optlen = ip6optlen(*tptrp, lim)) == 0) + return(-1); + + *tptrp = *tptrp + optlen; + } + for (optp = *tptrp; optp < lim; optp += optlen) { + if (*optp == type) { + *tptrp = optp; + return(0); + } + if ((optlen = ip6optlen(optp, lim)) == 0) + return(-1); + } + + /* search failed */ + *tptrp = NULL; + return(-1); +} + +/* + * Calculate the length of a given IPv6 option. Also checks + * if the option is safely stored in user's buffer according to the + * calculated length and the limitation of the buffer. + */ +static int +ip6optlen(opt, lim) + u_int8_t *opt, *lim; +{ + int optlen; + + _DIAGASSERT(opt != NULL); + _DIAGASSERT(lim != NULL); + + if (*opt == IP6OPT_PAD1) + optlen = 1; + else { + /* is there enough space to store type and len? */ + if (opt + 2 > lim) + return(0); + optlen = *(opt + 1) + 2; + } + if (opt + optlen <= lim) + return(optlen); + + return(0); +} + +static void +inet6_insert_padopt(u_char *p, size_t len) +{ + + _DIAGASSERT(p != NULL); + + switch(len) { + case 0: + return; + case 1: + p[0] = IP6OPT_PAD1; + return; + default: + p[0] = IP6OPT_PADN; + p[1] = len - 2; + memset(&p[2], 0, len - 2); + return; + } +} + +/* + * The following functions are defined in RFC3542, which is a successor + * of RFC2292. + */ + +int +inet6_opt_init(void *extbuf, socklen_t extlen) +{ + struct ip6_ext *ext = (struct ip6_ext *)extbuf; + + if (extlen % 8) + return (-1); + + if (ext) { + if (extlen == 0) + return (-1); + ext->ip6e_len = (extlen >> 3) - 1; + } + + return (2); /* sizeof the next and the length fields */ +} + +int +inet6_opt_append(void *extbuf, socklen_t extlen, int offset, u_int8_t type, + socklen_t len, u_int8_t align, void **databufp) +{ + int currentlen = offset; + size_t padlen = 0; + + /* + * The option type must have a value from 2 to 255, inclusive. + * (0 and 1 are reserved for the Pad1 and PadN options, respectively.) + */ + if (type < 2) + return (-1); + + /* + * The option data length must have a value between 0 and 255, + * inclusive, and is the length of the option data that follows. + */ + if (len > 255) + return (-1); + + /* + * The align parameter must have a value of 1, 2, 4, or 8. + * The align value can not exceed the value of len. + */ + if (align != 1 && align != 2 && align != 4 && align != 8) + return (-1); + if (align > len) + return (-1); + + /* Calculate the padding length. */ + currentlen += 2 + len; /* 2 means "type + len" */ + if (currentlen % align) + padlen = align - (currentlen % align); + + /* The option must fit in the extension header buffer. */ + currentlen += padlen; + if (extlen && /* XXX: right? */ + (socklen_t)currentlen > extlen) + return (-1); + + if (extbuf) { + u_int8_t *optp = (u_int8_t *)extbuf + offset; + + if (padlen == 1) { + /* insert a Pad1 option */ + *optp = IP6OPT_PAD1; + optp++; + } else if (padlen > 0) { + /* insert a PadN option for alignment */ + *optp++ = IP6OPT_PADN; + *optp++ = padlen - 2; + memset(optp, 0, padlen - 2); + optp += (padlen - 2); + } + + *optp++ = type; + *optp++ = len; + + *databufp = optp; + } + + return (currentlen); +} + +int +inet6_opt_finish(void *extbuf, socklen_t extlen, int offset) +{ + int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0; + + if (extbuf) { + u_int8_t *padp; + size_t padlen = updatelen - offset; + + if ((socklen_t)updatelen > extlen) + return (-1); + + padp = (u_int8_t *)extbuf + offset; + if (padlen == 1) + *padp = IP6OPT_PAD1; + else if (padlen > 0) { + *padp++ = IP6OPT_PADN; + *padp++ = (padlen - 2); + memset(padp, 0, padlen - 2); + } + } + + return (updatelen); +} + +int +inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen) +{ + + memcpy((u_int8_t *)databuf + offset, val, vallen); + return (offset + vallen); +} + +int +inet6_opt_next(void *extbuf, socklen_t extlen, int offset, u_int8_t *typep, + socklen_t *lenp, void **databufp) +{ + u_int8_t *optp, *lim; + int optlen; + + /* Validate extlen. XXX: is the variable really necessary?? */ + if (extlen == 0 || (extlen % 8)) + return (-1); + lim = (u_int8_t *)extbuf + extlen; + + /* + * If this is the first time this function called for this options + * header, simply return the 1st option. + * Otherwise, search the option list for the next option. + */ + if (offset == 0) + optp = (u_int8_t *)(void *)((struct ip6_hbh *)extbuf + 1); + else + optp = (u_int8_t *)extbuf + offset; + + /* Find the next option skipping any padding options. */ + while (optp < lim) { + switch(*optp) { + case IP6OPT_PAD1: + optp++; + break; + case IP6OPT_PADN: + if ((optlen = ip6optlen(optp, lim)) == 0) + goto optend; + optp += optlen; + break; + default: /* found */ + if ((optlen = ip6optlen(optp, lim)) == 0) + goto optend; + *typep = *optp; + *lenp = optlen - 2; + *databufp = optp + 2; + return (optp + optlen - (u_int8_t *)extbuf); + } + } + + optend: + *databufp = NULL; /* for safety */ + return (-1); +} + +int +inet6_opt_find(void *extbuf, socklen_t extlen, int offset, u_int8_t type, + socklen_t *lenp, void **databufp) +{ + u_int8_t *optp, *lim; + int optlen; + + /* Validate extlen. XXX: is the variable really necessary?? */ + if (extlen == 0 || (extlen % 8)) + return (-1); + lim = (u_int8_t *)extbuf + extlen; + + /* + * If this is the first time this function called for this options + * header, simply return the 1st option. + * Otherwise, search the option list for the next option. + */ + if (offset == 0) + optp = (u_int8_t *)(void *)((struct ip6_hbh *)extbuf + 1); + else + optp = (u_int8_t *)extbuf + offset; + + /* Find the specified option */ + while (optp < lim) { + if ((optlen = ip6optlen(optp, lim)) == 0) + goto optend; + + if (*optp == type) { /* found */ + *lenp = optlen - 2; + *databufp = optp + 2; + return (optp + optlen - (u_int8_t *)extbuf); + } + + optp += optlen; + } + + optend: + *databufp = NULL; /* for safety */ + return (-1); +} + +int +inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen) +{ + + /* we can't assume alignment here */ + memcpy(val, (u_int8_t *)databuf + offset, vallen); + + return (offset + vallen); +} diff --git a/lib/nbsd_libc/net/iso_addr.3 b/lib/nbsd_libc/net/iso_addr.3 new file mode 100644 index 000000000..d8cd18538 --- /dev/null +++ b/lib/nbsd_libc/net/iso_addr.3 @@ -0,0 +1,110 @@ +.\" $NetBSD: iso_addr.3,v 1.9 2009/04/11 15:32:03 joerg Exp $ +.\" +.\" Copyright (c) 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. +.\" +.\" @(#)iso_addr.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt ISO_ADDR 3 +.Os +.Sh NAME +.Nm iso_addr , +.Nm iso_ntoa +.Nd elementary network address conversion routines for Open System Interconnection +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In netiso/iso.h +.Ft struct iso_addr * +.Fn iso_addr "const char *cp" +.Ft char * +.Fn iso_ntoa "struct iso_addr *isoa" +.Sh DESCRIPTION +The routine +.Fn iso_addr +interprets character strings representing +.Tn OSI +addresses, returning binary information suitable +for use in system calls. +The routine +.Fn iso_ntoa +takes +.Tn OSI +addresses and returns +.Tn ASCII +strings representing NSAPs (network service +access points) in a +notation inverse to that accepted by +.Fn iso_addr . +.Pp +Unfortunately, no universal standard exists for representing +.Tn OSI +network addresses. +.Pp +The format employed by +.Fn iso_addr +is a sequence of hexadecimal +.Dq digits +(optionally separated by periods), +of the form: +.Bd -filled -offset indent +\*[Lt]hex digits\*[Gt].\*[Lt]hex digits\*[Gt].\*[Lt]hex digits\*[Gt] +.Ed +.Pp +Each pair of hexadecimal digits represents a byte +with the leading digit indicating the higher-ordered bits. +A period following an even number of bytes has no +effect (but may be used to increase legibility). +A period following an odd number of bytes has the +effect of causing the byte of address being translated +to have its higher order bits filled with zeros. +.Sh RETURN VALUES +.Fn iso_ntoa +always returns a null terminated string. +.Fn iso_addr +always returns a pointer to a struct iso_addr. +(See +.Sx BUGS . ) +.Sh SEE ALSO +.Xr iso 4 +.Sh HISTORY +The +.Fn iso_addr +and +.Fn iso_ntoa +functions appeared in +.Bx 4.3 Reno . +.Sh BUGS +The returned values +reside in a static memory area. +.Pp +The function +.Fn iso_addr +should diagnose improperly formed input, and there should be an unambiguous +way to recognize this. diff --git a/lib/nbsd_libc/net/iso_addr.c b/lib/nbsd_libc/net/iso_addr.c new file mode 100644 index 000000000..cb0b7c1c2 --- /dev/null +++ b/lib/nbsd_libc/net/iso_addr.c @@ -0,0 +1,130 @@ +/* $NetBSD: iso_addr.c,v 1.12 2005/11/29 03:11:59 christos Exp $ */ + +/* + * Copyright (c) 1989, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)iso_addr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: iso_addr.c,v 1.12 2005/11/29 03:11:59 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include +#include + +/* States*/ +#define VIRGIN 0 +#define GOTONE 1 +#define GOTTWO 2 +/* Inputs */ +#define DIGIT (4*0) +#define END (4*1) +#define DELIM (4*2) + +struct iso_addr * +iso_addr(addr) + register const char *addr; +{ + static struct iso_addr out_addr; + register char *cp = out_addr.isoa_genaddr; + char *cplim = cp + sizeof(out_addr.isoa_genaddr); + register int byte = 0, state = VIRGIN; + register int newaddr = 0; /* pacify gcc */ + + _DIAGASSERT(addr != NULL); + + (void)memset(&out_addr, 0, sizeof (out_addr)); + do { + if ((*addr >= '0') && (*addr <= '9')) { + newaddr = *addr - '0'; + } else if ((*addr >= 'a') && (*addr <= 'f')) { + newaddr = *addr - 'a' + 10; + } else if ((*addr >= 'A') && (*addr <= 'F')) { + newaddr = *addr - 'A' + 10; + } else if (*addr == 0) + state |= END; + else + state |= DELIM; + addr++; + switch (state /* | INPUT */) { + case GOTTWO | DIGIT: + *cp++ = byte; /*FALLTHROUGH*/ + case VIRGIN | DIGIT: + state = GOTONE; byte = newaddr; continue; + case GOTONE | DIGIT: + state = GOTTWO; byte = newaddr + (byte << 4); continue; + default: /* | DELIM */ + state = VIRGIN; *cp++ = byte; byte = 0; continue; + case GOTONE | END: + case GOTTWO | END: + *cp++ = byte; /* FALLTHROUGH */ + case VIRGIN | END: + break; + } + break; + } while (cp < cplim); + out_addr.isoa_len = cp - out_addr.isoa_genaddr; + return (&out_addr); +} + +static const char hexlist[16] = "0123456789abcdef"; + +char * +iso_ntoa(isoa) + const struct iso_addr *isoa; +{ + static char obuf[64]; + char *out = obuf; + size_t i; + const u_char *in = (const u_char *)isoa->isoa_genaddr; + const u_char *inlim = in + isoa->isoa_len; + + _DIAGASSERT(isoa != NULL); + + out[1] = 0; + while (in < inlim) { + i = *in++; + *out++ = '.'; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + out[0] = hexlist[i]; + out += 2; + } else + *out++ = hexlist[i]; + } + *out = 0; + return(obuf + 1); +} diff --git a/lib/nbsd_libc/net/linkaddr.3 b/lib/nbsd_libc/net/linkaddr.3 new file mode 100644 index 000000000..d0f590dff --- /dev/null +++ b/lib/nbsd_libc/net/linkaddr.3 @@ -0,0 +1,134 @@ +.\" $NetBSD: linkaddr.3,v 1.10 2003/08/07 16:43:11 agc Exp $ +.\" +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Donn Seeley at BSDI. +.\" +.\" 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. +.\" +.\" @(#)linkaddr.3 8.1 (Berkeley) 7/28/93 +.\" +.Dd July 28, 1993 +.Dt LINK_ADDR 3 +.Os +.Sh NAME +.Nm link_addr , +.Nm link_ntoa +.Nd elementary address specification routines for link level access +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/socket.h +.In net/if_dl.h +.Ft void +.Fn link_addr "const char *addr" "struct sockaddr_dl *sdl" +.Ft char * +.Fn link_ntoa "const struct sockaddr_dl *sdl" +.Sh DESCRIPTION +The routine +.Fn link_addr +interprets character strings representing link-level addresses, +returning binary information suitable for use in system calls. +The routine +.Fn link_ntoa +takes a link-level address and returns an +.Tn ASCII +string representing some of the information present, including the +link level address itself, and the interface name or number, if present. +This facility is experimental and is still subject to change. +.Pp +Prior to a call to +.Fn link_addr , +.Fa sdl-\*[Gt]sdl_len +must be initialized to the size of the link-level socket structure, +typically +.Fa sizeof(struct sockaddr_dl) . +.Pp +For +.Fn link_addr , +the string +.Fa addr +may contain +an optional network interface identifier of the form +.Dq "name unit-number" , +suitable for the first argument to +.Xr ifconfig 8 , +followed in all cases by a colon and +an interface address in the form of +groups of hexadecimal digits +separated by periods. +Each group represents a byte of address; +address bytes are filled left to right from +low order bytes through high order bytes. +.Pp +.\" A regular expression may make this format clearer: +.\" .Bd -literal -offset indent +.\" ([a-z]+[0-9]+:)?[0-9a-f]+(\e.[0-9a-f]+)* +.\" .Ed +.\" .Pp +Thus +.Li le0:8.0.9.13.d.30 +represents an ethernet address +to be transmitted on the first Lance ethernet interface. +.Sh RETURN VALUES +.Fn link_ntoa +always returns a null terminated string. +.Fn link_addr +has no return value (See +.Sx BUGS ) . +.Sh SEE ALSO +.Xr ethers 3 , +.Xr iso 4 +.Sh HISTORY +The +.Fn link_addr +and +.Fn link_ntoa +functions appeared in +.Bx 4.3 Reno . +.Sh BUGS +The returned values for +.Fn link_ntoa +reside in a static memory area. +.Pp +The function +.Fn link_addr +should diagnose improperly formed input, and there should be an unambiguous +way to recognize this. +.Pp +If the +.Va sdl_len +field of the link socket address +.Fa sdl +is 0, +.Fn link_ntoa +will not insert a colon before the interface address bytes. +If this translated address is given to +.Fn link_addr +without inserting an initial colon, +the latter will not interpret it correctly. diff --git a/lib/nbsd_libc/net/linkaddr.c b/lib/nbsd_libc/net/linkaddr.c new file mode 100644 index 000000000..401a4edfd --- /dev/null +++ b/lib/nbsd_libc/net/linkaddr.c @@ -0,0 +1,169 @@ +/* $NetBSD: linkaddr.c,v 1.14 2005/11/29 03:11:59 christos 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)linkaddr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: linkaddr.c,v 1.14 2005/11/29 03:11:59 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include +#include + +/* States*/ +#define NAMING 0 +#define GOTONE 1 +#define GOTTWO 2 +#define RESET 3 +/* Inputs */ +#define DIGIT (4*0) +#define END (4*1) +#define DELIM (4*2) +#define LETTER (4*3) + +void +link_addr(addr, sdl) + register const char *addr; + register struct sockaddr_dl *sdl; +{ + register char *cp = sdl->sdl_data; + char *cplim = sdl->sdl_len + (char *)(void *)sdl; + register int byte = 0, state = NAMING; + register int newaddr = 0; /* pacify gcc */ + + _DIAGASSERT(addr != NULL); + _DIAGASSERT(sdl != NULL); + + (void)memset(&sdl->sdl_family, 0, (size_t)sdl->sdl_len - 1); + sdl->sdl_family = AF_LINK; + do { + state &= ~LETTER; + if ((*addr >= '0') && (*addr <= '9')) { + newaddr = *addr - '0'; + } else if ((*addr >= 'a') && (*addr <= 'f')) { + newaddr = *addr - 'a' + 10; + } else if ((*addr >= 'A') && (*addr <= 'F')) { + newaddr = *addr - 'A' + 10; + } else if (*addr == 0) { + state |= END; + } else if (state == NAMING && + (((*addr >= 'A') && (*addr <= 'Z')) || + ((*addr >= 'a') && (*addr <= 'z')))) + state |= LETTER; + else + state |= DELIM; + addr++; + switch (state /* | INPUT */) { + case NAMING | DIGIT: + case NAMING | LETTER: + *cp++ = addr[-1]; + continue; + case NAMING | DELIM: + state = RESET; + sdl->sdl_nlen = cp - sdl->sdl_data; + continue; + case GOTTWO | DIGIT: + *cp++ = byte; + /* FALLTHROUGH */ + case RESET | DIGIT: + state = GOTONE; + byte = newaddr; + continue; + case GOTONE | DIGIT: + state = GOTTWO; + byte = newaddr + (byte << 4); + continue; + default: /* | DELIM */ + state = RESET; + *cp++ = byte; + byte = 0; + continue; + case GOTONE | END: + case GOTTWO | END: + *cp++ = byte; + /* FALLTHROUGH */ + case RESET | END: + break; + } + break; + } while (cp < cplim); + sdl->sdl_alen = cp - LLADDR(sdl); + newaddr = cp - (char *)(void *)sdl; + if ((size_t) newaddr > sizeof(*sdl)) + sdl->sdl_len = newaddr; + return; +} + +static const char hexlist[16] = "0123456789abcdef"; + +char * +link_ntoa(sdl) + register const struct sockaddr_dl *sdl; +{ + static char obuf[64]; + register char *out = obuf; + register size_t i; + const u_char *in = (const u_char *)CLLADDR(sdl); + const u_char *inlim = in + sdl->sdl_alen; + int firsttime = 1; + + _DIAGASSERT(sdl != NULL); + + if (sdl->sdl_nlen) { + (void)memcpy(obuf, sdl->sdl_data, (size_t)sdl->sdl_nlen); + out += sdl->sdl_nlen; + if (sdl->sdl_alen) + *out++ = ':'; + } + while (in < inlim) { + if (firsttime) + firsttime = 0; + else + *out++ = '.'; + i = *in++; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + out[0] = hexlist[i]; + out += 2; + } else + *out++ = hexlist[i]; + } + *out = 0; + return (obuf); +} diff --git a/lib/nbsd_libc/net/nsdispatch.3 b/lib/nbsd_libc/net/nsdispatch.3 new file mode 100644 index 000000000..d22e8d23c --- /dev/null +++ b/lib/nbsd_libc/net/nsdispatch.3 @@ -0,0 +1,940 @@ +.\" $NetBSD: nsdispatch.3,v 1.29 2009/10/14 17:24:03 joerg Exp $ +.\" +.\" Copyright (c) 1997, 1998, 1999, 2004, 2005, 2008 +.\" The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Luke Mewburn; and by Jason R. Thorpe. +.\" +.\" 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 May 8, 2008 +.Dt NSDISPATCH 3 +.Os +.Sh NAME +.Nm nsdispatch +.Nd name-service switch dispatcher routine +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In nsswitch.h +.Ft int +.Fo nsdispatch +.Fa "void *nsdrv" +.Fa "const ns_dtab dtab[]" +.Fa "const char *database" +.Fa "const char *name" +.Fa "const ns_src defaults[]" +.Fa "..." +.Fc +.Sh DESCRIPTION +The +.Fn nsdispatch +function invokes the callback functions specified in +.Fa dtab +in the order given in +.Pa /etc/nsswitch.conf +for the database +.Fa database +until the action criteria for a source of that database is fulfilled. +.Pp +.Fa nsdrv +is passed to each callback function to use as necessary +(to pass back to the caller of +.Fn nsdispatch ) . +.Pp +.Fa dtab +is an array of +.Fa ns_dtab +structures, which have the following format: +.Bl -item -offset indent +.It +.Bd -literal +typedef struct { + const char *src; + nss_method cb; + void *cb_data; +} ns_dtab; +.Ed +.It +The +.Fa dtab +array should consist of one entry for each source type that has a +static implementation, +with +.Fa src +as the name of the source, +.Fa cb +as a callback function which handles that source, and +.Fa cb_data +as a pointer to arbitrary data to be passed to the callback function. +The last entry in +.Fa dtab +should contain +.Dv NULL +values for +.Fa src , +.Fa cb , +and +.Fa cb_data . +.It +The callback function signature is described by the typedef: +.Bd -ragged -offset indent +.Ft typedef int +.Fo \*(lp*nss_method\*(rp +.Fa "void *cbrv" +.Fa "void *cbdata" +.Fa "va_list ap" +.Fc ; +.Bl -tag -width cbdata +.It Fa cbrv +The +.Fa nsdrv +that +.Fn nsdispatch +was invoked with. +.It Fa cbdata +The +.Fa cb_data +member of the array entry for the source that this +callback function implements in the +.Fa dtab +argument of +.Fn nsdispatch . +.It Fa ap +The +.Fa ... +arguments to +.Fn nsdispatch , +converted to a +.Ft va_list . +.El +.Ed +.El +.Pp +.Fa database +and +.Fa name +are used to select methods from optional per-source +dynamically-loaded modules. +.Fa name +is usually the name of the function calling +.Fn nsdispatch . +Note that the callback functions provided by +.Fa dtab +take priority over those implemented in dynamically-loaded modules in the +event of a conflict. +.Pp +.Fa defaults +contains a list of default sources to try in the case of +a missing or corrupt +.Xr nsswitch.conf 5 , +or if there isn't a relevant entry for +.Fa database . +It is an array of +.Fa ns_src +structures, which have the following format: +.Bl -item -offset indent +.It +.Bd -literal +typedef struct { + const char *src; + uint32_t flags; +} ns_src; +.Ed +.It +The +.Fa defaults +array should consist of one entry for each source to consult by default +indicated by +.Fa src , +and +.Fa flags +set to the desired behavior +(usually +.Dv NS_SUCCESS ; +refer to +.Sx Callback function return values +for more information). +The last entry in +.Fa defaults +should have +.Fa src +set to +.Dv NULL +and +.Fa flags +set to 0. +.It +Some invokers of +.Fn nsdispatch +(such as +.Xr setgrent 3 ) +need to force all callback functions to be invoked, +irrespective of the action criteria listed in +.Xr nsswitch.conf 5 . +This can be achieved by adding +.Dv NS_FORCEALL +to +.Fa defaults[0].flags +before invoking +.Fn nsdispatch . +The return value of +.Fn nsdispatch +will be the result of the final callback function invoked. +.It +For convenience, a global variable defined as: +.Dl extern const ns_src __nsdefaultsrc[]; +exists which contains a single default entry for +.Sq files +for use by callers which don't require complicated default rules. +.El +.Pp +.Fa ... +are optional extra arguments, which +are passed to the appropriate callback function as a +.Xr stdarg 3 +variable argument +list of the type +.Fa va_list . +.Pp +.Nm +returns the value of the callback function that caused the dispatcher +to finish, or +.Dv NS_NOTFOUND +otherwise. +.\" +.Ss Dynamically-loaded module interface +The +.Fn nsdispatch +function loads callback functions from the run-time link-editor's search +path using the following naming convention: +.Bl -item -offset indent +.It +.Bd -literal +nss_\*[Lt]source\*[Gt].so.\*[Lt]version\*[Gt] +.Ed +.Bl -tag -width XversionX +.It Aq source +The source that the module implements. +.It Aq version +The +.Nm nsdispatch +module interface version, which is defined by the integer +.Dv NSS_MODULE_INTERFACE_VERSION , +which has the value 0. +.El +.El +.Pp +When a module is loaded, +.Fn nsdispatch +looks for and calls the following function in the module: +.Pp +.Bd -ragged -offset indent +.Ft ns_mtab * +.Fo nss_module_register +.Fa "const char *source" +.Fa "u_int *nelems" +.Fa "nss_module_unregister_fn *unreg" +.Fc ; +.Pp +.Bl -tag -width source +.It Fa source +The name of the source that the module implements, as used by +.Fn nsdispatch +to construct the module's name. +.It Fa nelems +A pointer to an unsigned integer that +.Fn nss_module_register +should set to the number of elements in the +.Ft ns_mtab +array returned by +.Fn nss_module_register , +or +.Dv 0 +if there was a failure. +.It Fa unreg +A pointer to a function pointer that +.Fn nss_module_register +can optionally set to an unregister function to be invoked when the module is +unloaded, or +.Dv NULL +if there isn't one. +.El +.Ed +.Pp +The unregister function signature is described by the typedef: +.Pp +.Bd -ragged -offset indent +.Ft typedef void +.Fo \*(lp*nss_module_unregister_fn\*(rp +.Fa "ns_mtab *mtab" +.Fa "u_int nelems" +.Fc ; +.Pp +.Bl -tag -width nelems +.It Fa mtab +The array of +.Ft ns_mtab +structures returned by +.Fn nss_module_register . +.It Fa nelems +The +.Fa *nelems +value set by +.Fn nss_module_register . +.El +.Ed +.Pp +.Fn nss_module_register +returns an array of +.Ft ns_mtab +structures +(with +.Fa *nelems +entries), or +.Dv NULL +if there was a failure. +The +.Ft ns_mtab +structures have the following format: +.Bl -item -offset indent +.It +.Bd -literal +typedef struct { + const char *database; + const char *name; + nss_method method; + void *mdata; +} ns_mtab; +.Ed +.It +The +.Fa mtab +array should consist of one entry for each callback function (method) +that is implemented, +with +.Fa database +as the name of the database, +.Fa name +as the name of the callback function, +.Fa method +as the +.Ft nss_method +callback function that implements the method, and +.Fa mdata +as a pointer to arbitrary data to be passed to the callback function as its +.Fa cbdata +argument. +.El +.\" +.Ss Valid source types +While there is support for arbitrary sources, the following +#defines for commonly implemented sources are provided: +.Bl -column NSSRC_COMPAT COMPAT -offset indent +.It Sy #define Value +.It NSSRC_FILES "files" +.It NSSRC_DNS "dns" +.It NSSRC_NIS "nis" +.It NSSRC_COMPAT "compat" +.El +.Pp +Refer to +.Xr nsswitch.conf 5 +for a complete description of what each source type is. +.\" +.Ss Valid database types +While there is support for arbitrary databases, the following +#defines for currently implemented system databases are provided: +.Bl -column NSDB_PASSWD_COMPAT PASSWD_COMPAT -offset indent +.It Sy #define Value +.It NSDB_HOSTS "hosts" +.It NSDB_GROUP "group" +.It NSDB_GROUP_COMPAT "group_compat" +.It NSDB_NETGROUP "netgroup" +.It NSDB_NETWORKS "networks" +.It NSDB_PASSWD "passwd" +.It NSDB_PASSWD_COMPAT "passwd_compat" +.It NSDB_SHELLS "shells" +.El +.Pp +Refer to +.Xr nsswitch.conf 5 +for a complete description of what each database is. +.\" +.Ss Callback function return values +The callback functions should return one of the following values +depending upon status of the lookup: +.Bl -column NS_NOTFOUND -offset indent +.It Sy "Return value" Status code +.It NS_SUCCESS The requested entry was found. +.It NS_NOTFOUND The entry is not present at this source. +.It NS_TRYAGAIN The source is busy, and may respond to retries. +.It NS_UNAVAIL The source is not responding, or entry is corrupt. +.El +.\" +.Sh CALLBACK FUNCTION API FOR STANDARD DATABASES +The organization of the +.Fa ap +argument for an +.Fn nss_method +callback function for a standard method in a standard database is: +.Bl -enum -offset indent -compact +.It +Pointer to return value of the standard function. +.It +First argument of the standard function. +.It +(etc.) +.El +.Pp +For example, given the standard function +.Xr getgrnam 3 : +.Bd -ragged -offset indent -compact +.Ft struct group * +.Fn getgrnam "const char *name" +.Ed +the +.Fa ap +organization used by the callback functions is: +.Bl -enum -offset indent -compact +.It +.Ft "struct group **" +.It +.Ft "const char *" +.El +.Pp +.Sy NOTE: +Not all standard databases are using this calling convention yet; +those that aren't are noted below. +These will be changed in the future. +.Pp +The callback function names and +.Ft va_list +organization for various standard database callback functions are: +.\" +.Ss Methods for hosts database +.Sy NOTE: +The method APIs for this database will be changing in the near future. +.Bl -tag -width 3n +.It Sy getaddrinfo +.Ft "char *name" , +.Ft "const struct addrinfo *pai" +.Pp +Returns +.Ft "struct addrinfo *" +via +.Ft "void *cbrv" . +.It Sy gethostbyaddr +.Ft "unsigned char *addr" , +.Ft "int addrlen" , +.Ft "int af" +.Pp +Returns +.Ft "struct hostent *" +via +.Ft "void *cbrv" . +.It Sy gethostbyname +.Ft "char *name" , +.Ft "int namelen" , +.Ft "int af" +.Pp +Returns +.Ft "struct hostent *" +via +.Ft "void *cbrv" . +.El +.\" +.Ss Methods for group and group_compat databases +.Bl -tag -width 3n +.It Sy endgrent +Empty +.Fa ap . +.Pp +All methods for all sources are invoked for this method name. +.It Sy getgrent +.Ft "struct group **retval" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct group" +on success, +.Dv NULL otherwise. +.Pp +.Xr getgrent 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.It Sy getgrent_r +.Ft "int *retval" , +.Ft "struct group *grp" , +.Ft "char *buffer" , +.Ft "size_t buflen" , +.Ft "struct group **result" +.Pp +.Fa *retval +should be set to an appropriate +.Xr errno 2 +on failure. +.Pp +.Xr getgrent_r 3 +returns 0 +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS +or +.Dv NS_NOTFOUND , +and +.Fa *retval +otherwise. +.It Sy getgrgid +.Ft "struct group **retval" , +.Ft "gid_t gid" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct group" +on success, +.Dv NULL otherwise. +.Pp +.Xr getgrgid 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.It Sy getgrgid_r +.Ft "int *retval" , +.Ft "gid_t gid" , +.Ft "struct group *grp" , +.Ft "char *buffer" , +.Ft "size_t buflen" , +.Ft "struct group **result" +.Pp +.Fa *retval +should be set to an appropriate +.Xr errno 2 +on failure. +.Pp +.Xr getgrgid_r 3 +returns 0 +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS +or +.Dv NS_NOTFOUND , +and +.Fa *retval +otherwise. +.It Sy getgrnam +.Ft "struct group **retval" , +.Ft "const char *name" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct group" +on success, +.Dv NULL otherwise. +.Pp +.Xr getgrnam 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.It Sy getgrnam_r +.Ft "int *retval" , +.Ft "const char *name" , +.Ft "struct group *grp" , +.Ft "char *buffer" , +.Ft "size_t buflen" , +.Ft "struct group **result" +.Pp +.Fa *retval +should be set to an appropriate +.Xr errno 2 +on failure. +.Pp +.Xr getgrnam_r 3 +returns 0 +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS +or +.Dv NS_NOTFOUND , +and +.Fa *retval +otherwise. +.It Sy getgroupmembership +.Ft "int *retval" , +.Ft "const char *name" , +.Ft "gid_t basegid" , +.Ft "gid_t *groups" , +.Ft "int maxgrp" , +.Ft "int *groupc" +.Pp +.Fa retval +is unused. +.Pp +Lookups for +.Sy group_compat +are also stopped if +.Dv NS_SUCCESS +was returned to prevent multiple +.Dq "+:" +compat entries from being expanded. +.Pp +.Xr getgroupmembership 3 +returns +is -1 if +.Fa *groupc +is greater than to +.Fa maxgrp , +and 0 otherwise. +.It Sy setgroupent +.Ft "int *retval" , +.Ft "int stayopen" +.Pp +.Fa retval +should be set to 0 on failure and 1 on success. +.Pp +All methods for all sources are invoked for this method name. +.It Sy setgrent +Empty +.Fa ap . +.Pp +All methods for all sources are invoked for this method name. +.El +.\" +.Ss Methods for netgroup database +.Sy NOTE: +The method APIs for this database will be changing in the near future. +.Bl -tag -width 3n +.It Sy endnetgrent +Empty +.Fa ap . +.It Sy lookup +.Ft "char *name" , +.Ft "char **line" , +.Ft "int bywhat" +.Pp +Find the given +.Fa name +and return its value in +.Fa line . +.Fa bywhat +is one of +.Dv _NG_KEYBYNAME , +.Dv _NG_KEYBYUSER , +or +.Dv _NG_KEYBYHOST . +.It Sy getnetgrent +.Ft "int *retval" , +.Ft "const char **host" , +.Ft "const char **user" , +.Ft "const char **domain" +.Pp +.Fa *retval +should be set to 0 for no more netgroup members and 1 otherwise. +.Pp +.Xr getnetgrent 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +0 otherwise. +.It Sy innetgr +.Ft "int *retval" , +.Ft "const char *grp" , +.Ft "const char *host" , +.Ft "const char *user" , +.Ft "const char *domain" +.Pp +.Fa *retval +should be set to 1 for a successful match and 0 otherwise. +.It Sy setnetgrent +.Ft "const char *netgroup" +.El +.\" +.Ss Methods for networks database +.Bl -tag -width 3n +.It Sy getnetbyaddr +.Ft "struct netent **retval" , +.Ft "uint32_t net" , +.Ft "int type" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct netent" +on success, +.Dv NULL otherwise. +.Pp +.Xr getnetbyaddr 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.It Sy getnetbyname +.Ft "struct netent **retval" , +.Ft "const char *name" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct netent" +on success, +.Dv NULL otherwise. +.Pp +.Xr getnetbyname 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.El +.\" +.Ss Methods for passwd and passwd_compat databases +.Bl -tag -width 3n +.It Sy endpwent +Empty +.Fa ap . +.Pp +All methods for all sources are invoked for this method name. +.It Sy getpwent +.Ft "struct passwd **retval" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct passwd" +on success, +.Dv NULL otherwise. +.Pp +.Xr getpwent 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.It Sy getpwent_r +.Ft "int *retval" , +.Ft "struct passwd *pw" , +.Ft "char *buffer" , +.Ft "size_t buflen" , +.Ft "struct passwd **result" +.Pp +.Fa *retval +should be set to an appropriate +.Xr errno 2 +on failure. +.Pp +.Xr getpwent_r 3 +returns 0 +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS +or +.Dv NS_NOTFOUND , +and +.Fa *retval +otherwise. +.It Sy getpwnam +.Ft "struct passwd **retval" , +.Ft "const char *name" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct passwd" +on success, +.Dv NULL otherwise. +.Pp +.Xr getpwnam 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.It Sy getpwnam_r +.Ft "int *retval" , +.Ft "const char *name" , +.Ft "struct passwd *pw" , +.Ft "char *buffer" , +.Ft "size_t buflen" , +.Ft "struct passwd **result" +.Pp +.Fa *retval +should be set to an appropriate +.Xr errno 2 +on failure. +.Pp +.Xr getpwnam_r 3 +returns 0 +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS +or +.Dv NS_NOTFOUND , +and +.Fa *retval +otherwise. +.It Sy getpwuid +.Ft "struct passwd **retval" , +.Ft "uid_t uid" +.Pp +.Fa *retval +should be set to a pointer to an internal static +.Ft "struct passwd" +on success, +.Dv NULL otherwise. +.Pp +.Xr getpwuid 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +.Dv NULL +otherwise. +.It Sy getpwuid_r +.Ft "int *retval" , +.Ft "uid_t uid" , +.Ft "struct passwd *pw" , +.Ft "char *buffer" , +.Ft "size_t buflen" , +.Ft "struct passwd **result" +.Pp +.Fa *retval +should be set to an appropriate +.Xr errno 2 +on failure. +.Pp +.Xr getpwuid_r +returns 0 +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS +or +.Dv NS_NOTFOUND , +and +.Fa *retval +otherwise. +.It Sy setpassent +.Ft "int *retval" , +.Ft "int stayopen" +.Pp +.Fa retval +should be set to 0 on failure and 1 on success. +.Pp +All methods for all sources are invoked for this method name. +.It Sy setpwent +Empty +.Fa ap . +.Pp +All methods for all sources are invoked for this method name. +.El +.\" +.Ss Methods for shells database +.Bl -tag -width 3n +.It Sy endusershell +Empty +.Fa ap . +.Pp +All methods for all sources are invoked for this method name. +.It Sy getusershell +.Ft "char **retval" +.Pp +.Xr getusershell 3 +returns +.Fa *retval +if +.Fn nsdispatch +returns +.Dv NS_SUCCESS , +and 0 otherwise. +.It Sy setusershell +Empty +.Fa ap . +.Pp +All methods for all sources are invoked for this method name. +.El +.\" +.Sh SEE ALSO +.Xr ld.elf_so 1 , +.Xr hesiod 3 , +.Xr stdarg 3 , +.Xr ypclnt 3 , +.Xr nsswitch.conf 5 +.Sh HISTORY +The +.Nm +routines first appeared in +.Nx 1.4 . +Support for dynamically-loaded modules first appeared in +.Nx 3.0 . +.Sh AUTHORS +Luke Mewburn +.Aq lukem@NetBSD.org +wrote this freely distributable name-service switch implementation, +using ideas from the +.Tn ULTRIX +.Xr svc.conf 5 +and +.Tn Solaris +.Xr nsswitch.conf 4 +manual pages. +Support for dynamically-loaded modules was added by Jason Thorpe +.Aq thorpej@NetBSD.org , +based on code developed by the +.Fx +Project. diff --git a/lib/nbsd_libc/net/nsdispatch.c b/lib/nbsd_libc/net/nsdispatch.c new file mode 100644 index 000000000..3def5d19d --- /dev/null +++ b/lib/nbsd_libc/net/nsdispatch.c @@ -0,0 +1,693 @@ +/* $NetBSD: nsdispatch.c,v 1.34 2009/02/05 13:21:11 lukem Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn; and by Jason R. Thorpe. + * + * 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. + */ + +/*- + * Copyright (c) 2003 Networks Associates Technology, Inc. + * All rights reserved. + * + * Portions of this software were developed for the FreeBSD Project by + * Jacques A. Vidrine, Safeport Network Services, and Network + * Associates Laboratories, the Security Research Division of Network + * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 + * ("CBOSS"), as part of the DARPA CHATS research program. + * + * 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 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nsdispatch.c,v 1.34 2009/02/05 13:21:11 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include + +#include +#ifdef __ELF__ +#include +#endif /* __ELF__ */ +#include +#include +#define _NS_PRIVATE +#include +#include +#include +#include +#include +#include + +#include "reentrant.h" + +extern FILE *_nsyyin; +extern int _nsyyparse(void); + + +#ifdef __weak_alias +__weak_alias(nsdispatch,_nsdispatch) +#endif + + +/* + * default sourcelist: `files' + */ +const ns_src __nsdefaultsrc[] = { + { NSSRC_FILES, NS_SUCCESS }, + { 0, 0 }, +}; + +const ns_src __nsdefaultcompat[] = { + { NSSRC_COMPAT, NS_SUCCESS }, + { 0, 0 } +}; + +const ns_src __nsdefaultcompat_forceall[] = { + { NSSRC_COMPAT, NS_SUCCESS | NS_FORCEALL }, + { 0, 0 } +}; + +const ns_src __nsdefaultfiles[] = { + { NSSRC_FILES, NS_SUCCESS }, + { 0, 0 }, +}; + +const ns_src __nsdefaultfiles_forceall[] = { + { NSSRC_FILES, NS_SUCCESS | NS_FORCEALL }, + { 0, 0 }, +}; + +const ns_src __nsdefaultnis[] = { + { NSSRC_NIS, NS_SUCCESS }, + { 0, 0 } +}; + +const ns_src __nsdefaultnis_forceall[] = { + { NSSRC_NIS, NS_SUCCESS | NS_FORCEALL }, + { 0, 0 } +}; + + +/* Database, source mappings. */ +static u_int _nsmapsize; +static ns_dbt *_nsmap; + +/* Nsswitch modules. */ +static u_int _nsmodsize; +static ns_mod *_nsmod; + +/* Placeholder for built-in modules' dlopen() handles. */ +static void *_nsbuiltin = &_nsbuiltin; + +#ifdef _REENTRANT +/* + * Global nsswitch data structures are mostly read-only, but we update them + * when we read or re-read nsswitch.conf. + */ +static rwlock_t _nslock = RWLOCK_INITIALIZER; + +/* + * List of threads currently in nsdispatch(). We use this to detect + * recursive calls and avoid reloading configuration in such cases, + * which could cause deadlock. + */ +struct _ns_drec { + LIST_ENTRY(_ns_drec) list; + thr_t thr; +}; +static LIST_HEAD(, _ns_drec) _ns_drec = LIST_HEAD_INITIALIZER(&_ns_drec); +static mutex_t _ns_drec_lock = MUTEX_INITIALIZER; +#endif /* _REENTRANT */ + + +/* + * Runtime determination of whether we are dynamically linked or not. + */ +#ifdef __ELF__ +extern int _DYNAMIC __weak_reference(_DYNAMIC); +#define is_dynamic() (&_DYNAMIC != NULL) +#else +#define is_dynamic() (0) /* don't bother - switch to ELF! */ +#endif /* __ELF__ */ + + +/* + * size of dynamic array chunk for _nsmap and _nsmap[x].srclist (and other + * growing arrays). + */ +#define NSELEMSPERCHUNK 8 + +/* + * Dynamically growable arrays are used for lists of databases, sources, + * and modules. The following "vector" API is used to isolate the + * common operations. + */ +typedef void (*_nsvect_free_elem)(void *); + +static void * +_nsvect_append(const void *elem, void *vec, u_int *count, size_t esize) +{ + void *p; + + if ((*count % NSELEMSPERCHUNK) == 0) { + p = realloc(vec, (*count + NSELEMSPERCHUNK) * esize); + if (p == NULL) + return (NULL); + vec = p; + } + memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize); + (*count)++; + return (vec); +} + +static void * +_nsvect_elem(u_int i, void *vec, u_int count, size_t esize) +{ + + if (i < count) + return ((void *)((uintptr_t)vec + (i * esize))); + else + return (NULL); +} + +static void +_nsvect_free(void *vec, u_int *count, size_t esize, _nsvect_free_elem free_elem) +{ + void *elem; + u_int i; + + for (i = 0; i < *count; i++) { + elem = _nsvect_elem(i, vec, *count, esize); + if (elem != NULL) + (*free_elem)(elem); + } + if (vec != NULL) + free(vec); + *count = 0; +} +#define _NSVECT_FREE(v, c, s, f) \ +do { \ + _nsvect_free((v), (c), (s), (f)); \ + (v) = NULL; \ +} while (/*CONSTCOND*/0) + +static int +_nsdbtcmp(const void *a, const void *b) +{ + + return (strcasecmp(((const ns_dbt *)a)->name, + ((const ns_dbt *)b)->name)); +} + +static int +_nsmodcmp(const void *a, const void *b) +{ + + return (strcasecmp(((const ns_mod *)a)->name, + ((const ns_mod *)b)->name)); +} + +static int +_nsmtabcmp(const void *a, const void *b) +{ + int cmp; + + cmp = strcmp(((const ns_mtab *)a)->name, + ((const ns_mtab *)b)->name); + if (cmp) + return (cmp); + + return (strcasecmp(((const ns_mtab *)a)->database, + ((const ns_mtab *)b)->database)); +} + +static void +_nsmodfree(ns_mod *mod) +{ + + free(__UNCONST(mod->name)); + if (mod->handle == NULL) + return; + if (mod->unregister != NULL) + (*mod->unregister)(mod->mtab, mod->mtabsize); +#ifdef __ELF__ + if (mod->handle != _nsbuiltin) + (void) dlclose(mod->handle); +#endif /* __ELF__ */ +} + +/* + * Load a built-in or dyanamically linked module. If the `reg_fn' + * argument is non-NULL, assume a built-in module and use `reg_fn' + * to register it. Otherwise, search for a dynamic nsswitch module. + */ +static int +_nsloadmod(const char *source, nss_module_register_fn reg_fn) +{ +#ifdef __ELF__ + char buf[PATH_MAX]; +#endif + ns_mod mod, *new; + + memset(&mod, 0, sizeof(mod)); + mod.name = strdup(source); + if (mod.name == NULL) + return (-1); + + if (reg_fn != NULL) { + /* + * The placeholder is required, as a NULL handle + * represents an invalid module. + */ + mod.handle = _nsbuiltin; + } else if (!is_dynamic()) { + goto out; + } else { +#ifdef __ELF__ + if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name, + NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf)) + goto out; + mod.handle = dlopen(buf, RTLD_LOCAL | RTLD_LAZY); + if (mod.handle == NULL) { +#ifdef _NSS_DEBUG + /* + * This gets pretty annoying, since the built-in + * sources are not yet modules. + */ + /* XXX log some error? */ +#endif + goto out; + } + reg_fn = (nss_module_register_fn) dlsym(mod.handle, + "nss_module_register"); + if (reg_fn == NULL) { + (void) dlclose(mod.handle); + mod.handle = NULL; + /* XXX log some error? */ + goto out; + } +#else /* ! __ELF__ */ + mod.handle = NULL; +#endif /* __ELF__ */ + } + mod.mtab = (*reg_fn)(mod.name, &mod.mtabsize, &mod.unregister); + if (mod.mtab == NULL || mod.mtabsize == 0) { +#ifdef __ELF__ + if (mod.handle != _nsbuiltin) + (void) dlclose(mod.handle); +#endif /* __ELF__ */ + mod.handle = NULL; + /* XXX log some error? */ + goto out; + } + if (mod.mtabsize > 1) + qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]), + _nsmtabcmp); + out: + new = _nsvect_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod)); + if (new == NULL) { + _nsmodfree(&mod); + return (-1); + } + _nsmod = new; + /* _nsmodsize already incremented */ + + qsort(_nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp); + return (0); +} + +static void +_nsloadbuiltin(void) +{ + + /* Do nothing, for now. */ +} + +int +_nsdbtaddsrc(ns_dbt *dbt, const ns_src *src) +{ + void *new; + const ns_mod *mod; + ns_mod modkey; + + _DIAGASSERT(dbt != NULL); + _DIAGASSERT(src != NULL); + + new = _nsvect_append(src, dbt->srclist, &dbt->srclistsize, + sizeof(*src)); + if (new == NULL) + return (-1); + dbt->srclist = new; + /* dbt->srclistsize already incremented */ + + modkey.name = src->name; + mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod), + _nsmodcmp); + if (mod == NULL) + return (_nsloadmod(src->name, NULL)); + + return (0); +} + +void +_nsdbtdump(const ns_dbt *dbt) +{ + unsigned int i; + + _DIAGASSERT(dbt != NULL); + + printf("%s (%d source%s):", dbt->name, dbt->srclistsize, + dbt->srclistsize == 1 ? "" : "s"); + for (i = 0; i < dbt->srclistsize; i++) { + printf(" %s", dbt->srclist[i].name); + if (!(dbt->srclist[i].flags & + (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) && + (dbt->srclist[i].flags & NS_SUCCESS)) + continue; + printf(" ["); + if (!(dbt->srclist[i].flags & NS_SUCCESS)) + printf(" SUCCESS=continue"); + if (dbt->srclist[i].flags & NS_UNAVAIL) + printf(" UNAVAIL=return"); + if (dbt->srclist[i].flags & NS_NOTFOUND) + printf(" NOTFOUND=return"); + if (dbt->srclist[i].flags & NS_TRYAGAIN) + printf(" TRYAGAIN=return"); + printf(" ]"); + } + printf("\n"); +} + +static void +_nssrclist_free(ns_src **src, u_int srclistsize) +{ + u_int i; + + for (i = 0; i < srclistsize; i++) { + if ((*src)[i].name != NULL) + free(__UNCONST((*src)[i].name)); + } + free(*src); + *src = NULL; +} + +static void +_nsdbtfree(ns_dbt *dbt) +{ + + _nssrclist_free(&dbt->srclist, dbt->srclistsize); + if (dbt->name != NULL) + free(__UNCONST(dbt->name)); +} + +int +_nsdbtput(const ns_dbt *dbt) +{ + ns_dbt *p; + void *new; + u_int i; + + _DIAGASSERT(dbt != NULL); + + for (i = 0; i < _nsmapsize; i++) { + p = _nsvect_elem(i, _nsmap, _nsmapsize, sizeof(*_nsmap)); + if (strcasecmp(dbt->name, p->name) == 0) { + /* overwrite existing entry */ + if (p->srclist != NULL) + _nssrclist_free(&p->srclist, p->srclistsize); + memmove(p, dbt, sizeof(*dbt)); + return (0); + } + } + new = _nsvect_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap)); + if (new == NULL) + return (-1); + _nsmap = new; + /* _nsmapsize already incremented */ + + return (0); +} + +/* + * This function is called each time nsdispatch() is called. If this + * is the first call, or if the configuration has changed, (re-)prepare + * the global data used by NSS. + */ +static int +_nsconfigure(void) +{ +#ifdef _REENTRANT + static mutex_t _nsconflock = MUTEX_INITIALIZER; +#endif + static time_t _nsconfmod; + struct stat statbuf; + + mutex_lock(&_nsconflock); + + if (stat(_PATH_NS_CONF, &statbuf) == -1) { + /* + * No nsswitch.conf; just use whatever configuration we + * currently have, or fall back on the defaults specified + * by the caller. + */ + mutex_unlock(&_nsconflock); + return (0); + } + + if (statbuf.st_mtime <= _nsconfmod) { + /* Internal state is up-to-date with nsswitch.conf. */ + mutex_unlock(&_nsconflock); + return (0); + } + + /* + * Ok, we've decided we need to update the nsswitch configuration + * structures. Acquire a write-lock on _nslock while continuing + * to hold _nsconflock. Acquiring a write-lock blocks while + * waiting for other threads already holding a read-lock to clear. + * We hold _nsconflock for the duration, and update the time stamp + * at the end of the update operation, at which time we release + * both locks. + */ + rwlock_wrlock(&_nslock); + + _nsyyin = fopen(_PATH_NS_CONF, "r"); + if (_nsyyin == NULL) { + /* + * Unable to open nsswitch.conf; behave as though the + * stat() above failed. Even though we have already + * updated _nsconfmod, if the file reappears, the + * mtime will change. + */ + goto out; + } + + _NSVECT_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap), + (_nsvect_free_elem) _nsdbtfree); + _NSVECT_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod), + (_nsvect_free_elem) _nsmodfree); + + _nsloadbuiltin(); + + _nsyyparse(); + (void) fclose(_nsyyin); + if (_nsmapsize != 0) + qsort(_nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp); + + _nsconfmod = statbuf.st_mtime; + + out: + rwlock_unlock(&_nslock); + mutex_unlock(&_nsconflock); + return (0); +} + +static nss_method +_nsmethod(const char *source, const char *database, const char *method, + const ns_dtab disp_tab[], void **cb_data) +{ + int curdisp; + ns_mod *mod, modkey; + ns_mtab *mtab, mtabkey; + + if (disp_tab != NULL) { + for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) { + if (strcasecmp(source, disp_tab[curdisp].src) == 0) { + *cb_data = disp_tab[curdisp].cb_data; + return (disp_tab[curdisp].callback); + } + } + } + + modkey.name = source; + mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod), + _nsmodcmp); + if (mod != NULL && mod->handle != NULL) { + mtabkey.database = database; + mtabkey.name = method; + mtab = bsearch(&mtabkey, mod->mtab, mod->mtabsize, + sizeof(mod->mtab[0]), _nsmtabcmp); + if (mtab != NULL) { + *cb_data = mtab->mdata; + return (mtab->method); + } + } + + *cb_data = NULL; + return (NULL); +} + +int +/*ARGSUSED*/ +nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database, + const char *method, const ns_src defaults[], ...) +{ + static int _nsdispatching; +#ifdef _REENTRANT + struct _ns_drec drec, *ldrec; +#endif + va_list ap; + int i, result; + ns_dbt key; + const ns_dbt *dbt; + const ns_src *srclist; + int srclistsize; + nss_method cb; + void *cb_data; + + /* retval may be NULL */ + /* disp_tab may be NULL */ + _DIAGASSERT(database != NULL); + _DIAGASSERT(method != NULL); + _DIAGASSERT(defaults != NULL); + if (database == NULL || method == NULL || defaults == NULL) + return (NS_UNAVAIL); + + /* + * In both the threaded and non-threaded cases, avoid reloading + * the configuration if the current thread is already running + * nsdispatch() (i.e. recursive call). + * + * In the non-threaded case, this avoids changing the data structures + * while we're using them. + * + * In the threaded case, this avoids trying to take a write lock + * while the current thread holds a read lock (which would result + * in deadlock). + */ +#ifdef _REENTRANT + if (__isthreaded) { + drec.thr = thr_self(); + mutex_lock(&_ns_drec_lock); + LIST_FOREACH(ldrec, &_ns_drec, list) { + if (ldrec->thr == drec.thr) + break; + } + LIST_INSERT_HEAD(&_ns_drec, &drec, list); + mutex_unlock(&_ns_drec_lock); + if (ldrec == NULL && _nsconfigure()) { + mutex_lock(&_ns_drec_lock); + LIST_REMOVE(&drec, list); + mutex_unlock(&_ns_drec_lock); + return (NS_UNAVAIL); + } + } else +#endif /* _REENTRANT */ + if (_nsdispatching++ == 0 && _nsconfigure()) { + _nsdispatching--; + return (NS_UNAVAIL); + } + + rwlock_rdlock(&_nslock); + + key.name = database; + dbt = bsearch(&key, _nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp); + if (dbt != NULL) { + srclist = dbt->srclist; + srclistsize = dbt->srclistsize; + } else { + srclist = defaults; + srclistsize = 0; + while (srclist[srclistsize].name != NULL) + srclistsize++; + } + result = 0; + + for (i = 0; i < srclistsize; i++) { + cb = _nsmethod(srclist[i].name, database, method, + disp_tab, &cb_data); + result = 0; + if (cb != NULL) { + va_start(ap, defaults); + result = (*cb)(retval, cb_data, ap); + va_end(ap); + if (defaults[0].flags & NS_FORCEALL) + continue; + if (result & srclist[i].flags) + break; + } + } + result &= NS_STATUSMASK; /* clear private flags in result */ + + rwlock_unlock(&_nslock); + +#ifdef _REENTRANT + if (__isthreaded) { + mutex_lock(&_ns_drec_lock); + LIST_REMOVE(&drec, list); + mutex_unlock(&_ns_drec_lock); + } else +#endif /* _REENTRANT */ + _nsdispatching--; + + return (result ? result : NS_NOTFOUND); +} diff --git a/lib/nbsd_libc/net/nslexer.l b/lib/nbsd_libc/net/nslexer.l new file mode 100644 index 000000000..45381d1b3 --- /dev/null +++ b/lib/nbsd_libc/net/nslexer.l @@ -0,0 +1,109 @@ +%{ +/* $NetBSD: nslexer.l,v 1.11 2010/12/08 03:19:19 christos Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nslexer.l,v 1.11 2010/12/08 03:19:19 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#define _NS_PRIVATE +#include +#include +#include + +#include "nsparser.h" + +%} + +%option yylineno nounput noinput +%option never-interactive + +BLANK [ \t] +CR \n +STRING [a-zA-Z][a-zA-Z0-9_]* + +%% + +{BLANK}+ ; /* skip whitespace */ + +#.* ; /* skip comments */ + +\\{CR} ; /* allow continuation */ + +{CR} return NL; + +[sS][uU][cC][cC][eE][sS][sS] return SUCCESS; +[uU][nN][aA][vV][aA][iI][lL] return UNAVAIL; +[nN][oO][tT][fF][oO][uU][nN][dD] return NOTFOUND; +[tT][rR][yY][aA][gG][aA][iI][nN] return TRYAGAIN; + +[rR][eE][tT][uU][rR][nN] return RETURN; +[cC][oO][nN][tT][iI][nN][uU][eE] return CONTINUE; + +{STRING} { + char *p; + size_t i; + + if ((p = strdup(yytext)) == NULL) { + syslog(LOG_ERR, "libc nsdispatch: %m"); + return NL; + } + + for (i = 0; i < strlen(p); i++) { + if (isupper((unsigned char)p[i])) + p[i] = tolower((unsigned char)p[i]); + } + _nsyylval.str = p; + return STRING; + } + +. return yytext[0]; + +%% + +#undef _nsyywrap +int +_nsyywrap() +{ + return 1; +} /* _nsyywrap */ + +void +_nsyyerror(msg) + const char *msg; +{ + + syslog(LOG_WARNING, "libc nsdispatch: %s line %d: %s at '%s'", + _PATH_NS_CONF, yylineno, msg, yytext); +} /* _nsyyerror */ diff --git a/lib/nbsd_libc/net/nsparser.y b/lib/nbsd_libc/net/nsparser.y new file mode 100644 index 000000000..5845eca4e --- /dev/null +++ b/lib/nbsd_libc/net/nsparser.y @@ -0,0 +1,192 @@ +%{ +/* $NetBSD: nsparser.y,v 1.11 2009/02/05 13:21:11 lukem Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nsparser.y,v 1.11 2009/02/05 13:21:11 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#define _NS_PRIVATE +#include +#include +#include +#include + + +static void _nsaddsrctomap __P((const char *)); + +static ns_dbt curdbt; +static ns_src cursrc; + +extern char * _nsyytext; +extern int _nsyylineno; +%} + +%union { + char *str; + int mapval; +} + +%token NL +%token SUCCESS UNAVAIL NOTFOUND TRYAGAIN +%token RETURN CONTINUE +%token STRING + +%type Status Action + +%% + +File + : /* empty */ + | Lines + ; + +Lines + : Entry + | Lines Entry + ; + +Entry + : NL + | Database ':' NL + | Database ':' Srclist NL + { + int lineno; + + lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0); + if (_nsdbtput(&curdbt) == -1) + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s", + _PATH_NS_CONF, lineno, + "error adding entry"); + } + | error NL + { + yyerrok; + } + ; + +Database + : STRING + { + curdbt.name = yylval.str; + curdbt.srclist = NULL; + curdbt.srclistsize = 0; + } + ; + +Srclist + : Item + | Srclist Item + ; + +Item + : STRING + { + cursrc.flags = NS_SUCCESS; + _nsaddsrctomap($1); + } + | STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']' + { + _nsaddsrctomap($1); + } + ; + +Criteria + : Criterion + | Criteria Criterion + ; + +Criterion + : Status '=' Action + { + if ($3) /* if action == RETURN set RETURN bit */ + cursrc.flags |= $1; + else /* else unset it */ + cursrc.flags &= ~$1; + } + ; + +Status + : SUCCESS { $$ = NS_SUCCESS; } + | UNAVAIL { $$ = NS_UNAVAIL; } + | NOTFOUND { $$ = NS_NOTFOUND; } + | TRYAGAIN { $$ = NS_TRYAGAIN; } + ; + +Action + : RETURN { $$ = 1L; } + | CONTINUE { $$ = 0L; } + ; + +%% + +static void +_nsaddsrctomap(elem) + const char *elem; +{ + unsigned int i; + int lineno; + + _DIAGASSERT(elem != NULL); + + lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0); + if (curdbt.srclistsize > 0) { + if ((strcasecmp(elem, NSSRC_COMPAT) == 0) || + (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) { + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s", + _PATH_NS_CONF, lineno, + "'compat' used with other sources"); + return; + } + } + for (i = 0; i < curdbt.srclistsize; i++) { + if (strcasecmp(curdbt.srclist[i].name, elem) == 0) { + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s '%s'", + _PATH_NS_CONF, lineno, + "duplicate source", elem); + return; + } + } + cursrc.name = elem; + if (_nsdbtaddsrc(&curdbt, &cursrc) == -1) { + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s '%s'", + _PATH_NS_CONF, lineno, + "error adding", elem); + } +} diff --git a/lib/nbsd_libc/net/protoent.h b/lib/nbsd_libc/net/protoent.h new file mode 100644 index 000000000..4c98a64f3 --- /dev/null +++ b/lib/nbsd_libc/net/protoent.h @@ -0,0 +1,50 @@ +/* $NetBSD: protoent.h,v 1.2 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +struct protoent_data { + FILE *fp; + struct protoent proto; + char **aliases; + size_t maxaliases; + int stayopen; + char *line; + void *dummy; +}; + +struct protoent *getprotoent_r(struct protoent *, struct protoent_data *); +struct protoent *getprotobyname_r(const char *, + struct protoent *, struct protoent_data *); +struct protoent *getprotobynumber_r(int, + struct protoent *, struct protoent_data *); +void setprotoent_r(int, struct protoent_data *); +void endprotoent_r(struct protoent_data *); diff --git a/lib/nbsd_libc/net/rcmd.3 b/lib/nbsd_libc/net/rcmd.3 new file mode 100644 index 000000000..41b8ef0ed --- /dev/null +++ b/lib/nbsd_libc/net/rcmd.3 @@ -0,0 +1,311 @@ +.\" $NetBSD: rcmd.3,v 1.28 2010/03/22 19:30:54 joerg Exp $ +.\" +.\" Copyright (c) 1983, 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. +.\" +.\" @(#)rcmd.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd March 30, 2005 +.Dt RCMD 3 +.Os +.Sh NAME +.Nm rcmd , +.Nm orcmd , +.Nm rcmd_af , +.Nm orcmd_af , +.Nm rresvport , +.Nm rresvport_af , +.Nm iruserok , +.Nm ruserok , +.Nm iruserok_sa +.Nd routines for returning a stream to a remote command +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In unistd.h +.Ft int +.Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" +.Ft int +.Fn orcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" +.Ft int +.Fn rcmd_af "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" "int af" +.Ft int +.Fn orcmd_af "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" "int af" +.Ft int +.Fn rresvport "int *port" +.Ft int +.Fn rresvport_af "int *port" "int family" +.Ft int +.Fn iruserok "uint32_t raddr" "int superuser" "const char *ruser" "const char *luser" +.Ft int +.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" +.Ft int +.Fn iruserok_sa "const void *raddr" "int rlen" "int superuser" "const char *ruser" "const char *luser" +.Sh DESCRIPTION +The +.Fn rcmd +function is available for use by anyone to run commands on a +remote system. It acts like the +.Fn orcmd +command, with the exception that it makes a call out to the +.Xr rcmd 1 +command, or any other user-specified command, to perform the +actual connection (thus not requiring +that the caller be running as the super-user), and is only +available for the +.Dq shell/tcp +port. +The +.Fn orcmd +function +is used by the super-user to execute a command on +a remote machine using an authentication scheme based +on reserved port numbers. +While +.Fn rcmd +and +.Fn orcmd +can only handle IPv4 address in the first argument, +.Fn rcmd_af +and +.Fn orcmd_af +can handle other cases as well. +The +.Fn rresvport +function +returns a descriptor to a socket +with an address in the privileged port space. +The +.Fn rresvport_af +function is similar to +.Fn rresvport , +but you can explicitly specify the address family to use. +Calling +.Fn rresvport_af +with +.Dv AF_INET +has the same effect as +.Fn rresvport . +The +.Fn iruserok +and +.Fn ruserok +functions are used by servers +to authenticate clients requesting service with +.Fn rcmd . +All six functions are present in the same file and are used +by the +.Xr rshd 8 +server (among others). +.Fn iruserok_sa +is an address family independent variant of +.Fn iruserok . +.Pp +The +.Fn rcmd +function +looks up the host +.Fa *ahost +using +.Xr gethostbyname 3 , +returning \-1 if the host does not exist. +Otherwise +.Fa *ahost +is set to the standard name of the host +and a connection is established to a server +residing at the well-known Internet port +.Fa inport . +.Pp +If the connection succeeds, +a socket in the Internet domain of type +.Dv SOCK_STREAM +is returned to the caller, and given to the remote +command as +.Em stdin +and +.Em stdout . +If +.Fa fd2p +is non-zero, then an auxiliary channel to a control +process will be set up, and a descriptor for it will be placed +in +.Fa *fd2p . +The control process will return diagnostic +output from the command (unit 2) on this channel, and will also +accept bytes on this channel as being +.Ux +signal numbers, to be +forwarded to the process group of the command. +If +.Fa fd2p +is 0, then the +.Em stderr +(unit 2 of the remote +command) will be made the same as the +.Em stdout +and no +provision is made for sending arbitrary signals to the remote process, +although you may be able to get its attention by using out-of-band data. +.Pp +.Fn rcmd_af +and +.Fn orcmd_af +take address family in the last argument. +If the last argument is +.Dv PF_UNSPEC , +interpretation of +.Fa *ahost +will obey the underlying address resolution like DNS. +.Pp +The protocol is described in detail in +.Xr rshd 8 . +.Pp +The +.Fn rresvport +and +.Fn rresvport_af +functions are used to obtain a socket with a privileged +address bound to it. This socket is suitable for use +by +.Fn rcmd +and several other functions. Privileged Internet ports are those +in the range 0 to 1023. Only the super-user +is allowed to bind an address of this sort to a socket. +.Pp +The +.Fn iruserok +and +.Fn ruserok +functions take a remote host's IP address or name, respectively, +two user names and a flag indicating whether the local user's +name is that of the super-user. +Then, if the user is +.Em NOT +the super-user, it checks the +.Pa /etc/hosts.equiv +file. +If that lookup is not done, or is unsuccessful, the +.Pa .rhosts +in the local user's home directory is checked to see if the request for +service is allowed. +.Pp +If this file does not exist, is not a regular file, is owned by anyone +other than the user or the super-user, or is writable by anyone other +than the owner, the check automatically fails. +Zero is returned if the machine name is listed in the +.Dq Pa hosts.equiv +file, or the host and remote user name are found in the +.Dq Pa .rhosts +file; otherwise +.Fn iruserok +and +.Fn ruserok +return \-1. +If the local domain (as obtained from +.Xr gethostname 3 ) +is the same as the remote domain, only the machine name need be specified. +.Pp +If the IP address of the remote host is known, +.Fn iruserok +should be used in preference to +.Fn ruserok , +as it does not require trusting the DNS server for the remote host's domain. +.Pp +While +.Fn iruserok +can handle IPv4 addresses only, +.Fn iruserok_sa +and +.Fn ruserok +can handle other address families as well, like IPv6. +The first argument of +.Fn iruserok_sa +is typed as +.Fa "void *" +to avoid dependency between +.In unistd.h +and +.In sys/socket.h . +.Sh ENVIRONMENT +.Bl -tag -width RCMD_CMDxx -compact +.It Ev RCMD_CMD +When using the +.Fn rcmd +function, this variable is used as the program to run instead of +.Xr rcmd 1 . +.El +.Sh DIAGNOSTICS +The +.Fn rcmd +function +returns a valid socket descriptor on success. +It returns \-1 on error and prints a diagnostic message on the standard error. +.Pp +The +.Fn rresvport +and +.Fn rresvport_af +function +return a valid, bound socket descriptor on success. +They return \-1 on error with the global value +.Va errno +set according to the reason for failure. +The error code +.Dv EAGAIN +is overloaded to mean ``All network ports in use.'' +.Sh SEE ALSO +.Xr rcmd 1 , +.Xr rlogin 1 , +.Xr rsh 1 , +.Xr intro 2 , +.Xr rexec 3 , +.Xr hosts.equiv 5 , +.Xr rhosts 5 , +.Xr rexecd 8 , +.Xr rlogind 8 , +.Xr rshd 8 +.Sh HISTORY +The +.Fn orcmd , +.Fn rresvport , +.Fn iruserok +and +.Fn ruserok +functions appeared in +.Bx 4.2 , +where the +.Fn orcmd +function was called +.Fn rcmd . +The (newer) +.Fn rcmd +function appeared in +.Nx 1.3 . +.Fn rcmd_af +and +.Fn rresvport_af +were defined in RFC2292. diff --git a/lib/nbsd_libc/net/rcmd.c b/lib/nbsd_libc/net/rcmd.c new file mode 100644 index 000000000..8e136ee53 --- /dev/null +++ b/lib/nbsd_libc/net/rcmd.c @@ -0,0 +1,1025 @@ +/* $NetBSD: rcmd.c,v 1.65 2007/01/03 11:46:22 ws Exp $ */ + +/* + * Copyright (c) 1983, 1993, 1994 + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; +#else +__RCSID("$NetBSD: rcmd.c,v 1.65 2007/01/03 11:46:22 ws Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef _LIBC +#include "namespace.h" +#endif +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pathnames.h" + +int orcmd __P((char **, u_int, const char *, const char *, const char *, + int *)); +int orcmd_af __P((char **, u_int, const char *, const char *, const char *, + int *, int)); +int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); +int __ivaliduser_sa __P((FILE *, const struct sockaddr *, socklen_t, + const char *, const char *)); +static int rshrcmd __P((char **, u_int32_t, const char *, const char *, + const char *, int *, const char *)); +static int resrcmd __P((struct addrinfo *, char **, u_int32_t, const char *, + const char *, const char *, int *)); +static int __icheckhost __P((const struct sockaddr *, socklen_t, + const char *)); +static char *__gethostloop __P((const struct sockaddr *, socklen_t)); + +int +rcmd(ahost, rport, locuser, remuser, cmd, fd2p) + char **ahost; + u_short rport; + const char *locuser, *remuser, *cmd; + int *fd2p; +{ + + return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); +} + +int +rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) + char **ahost; + u_short rport; + const char *locuser, *remuser, *cmd; + int *fd2p; + int af; +{ + static char hbuf[MAXHOSTNAMELEN]; + char pbuf[NI_MAXSERV]; + struct addrinfo hints, *res; + int error; + struct servent *sp; + + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = af; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + error = getaddrinfo(*ahost, pbuf, &hints, &res); + if (error) { + warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ + return (-1); + } + if (res->ai_canonname) { + /* + * Canonicalise hostname. + * XXX: Should we really do this? + */ + strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); + *ahost = hbuf; + } + + /* + * Check if rport is the same as the shell port, and that the fd2p. If + * it is not, the program isn't expecting 'rsh' and so we can't use the + * RCMD_CMD environment. + */ + sp = getservbyname("shell", "tcp"); + if (sp != NULL && sp->s_port == rport) + error = rshrcmd(ahost, (u_int32_t)rport, + locuser, remuser, cmd, fd2p, getenv("RCMD_CMD")); + else + error = resrcmd(res, ahost, (u_int32_t)rport, + locuser, remuser, cmd, fd2p); + freeaddrinfo(res); + return (error); +} + +/* this is simply a wrapper around hprcmd() that handles ahost first */ +int +orcmd(ahost, rport, locuser, remuser, cmd, fd2p) + char **ahost; + u_int rport; + const char *locuser, *remuser, *cmd; + int *fd2p; +{ + return orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); +} + +int +orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) + char **ahost; + u_int rport; + const char *locuser, *remuser, *cmd; + int *fd2p; + int af; +{ + static char hbuf[MAXHOSTNAMELEN]; + char pbuf[NI_MAXSERV]; + struct addrinfo hints, *res; + int error; + + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = af; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + error = getaddrinfo(*ahost, pbuf, &hints, &res); + if (error) { + warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ + return (-1); + } + if (res->ai_canonname) { + strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); + *ahost = hbuf; + } + + error = resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p); + freeaddrinfo(res); + return (error); +} + +/*ARGSUSED*/ +static int +resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p) + struct addrinfo *res; + char **ahost; + u_int32_t rport; + const char *locuser, *remuser, *cmd; + int *fd2p; +{ + struct addrinfo *r; + struct sockaddr_storage from; + struct pollfd reads[2]; + sigset_t nmask, omask; + pid_t pid; + int s, lport, timo; + int pollr; + char c; + int refused; + + _DIAGASSERT(res != NULL); + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + r = res; + refused = 0; + pid = getpid(); + sigemptyset(&nmask); + sigaddset(&nmask, SIGURG); + if (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1) + return -1; + for (timo = 1, lport = IPPORT_RESERVED - 1;;) { + s = rresvport_af(&lport, r->ai_family); + if (s < 0) { + if (errno == EAGAIN) + warnx("rcmd: socket: All ports in use"); + else + warn("rcmd: socket"); + if (r->ai_next) { + r = r->ai_next; + continue; + } else { + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return (-1); + } + } + fcntl(s, F_SETOWN, pid); + if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) + break; + (void)close(s); + if (errno == EADDRINUSE) { + lport--; + continue; + } else if (errno == ECONNREFUSED) + refused++; + if (r->ai_next) { + int oerrno = errno; + char hbuf[NI_MAXHOST]; + const int niflags = NI_NUMERICHOST; + + hbuf[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, + hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) + strlcpy(hbuf, "(invalid)", sizeof(hbuf)); + errno = oerrno; + warn("rcmd: connect to address %s", hbuf); + r = r->ai_next; + hbuf[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, + hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) + strlcpy(hbuf, "(invalid)", sizeof(hbuf)); + (void)fprintf(stderr, "Trying %s...\n", hbuf); + continue; + } + if (refused && timo <= 16) { + (void)sleep((unsigned int)timo); + timo *= 2; + r = res; + refused = 0; + continue; + } + (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, + strerror(errno)); + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return (-1); + } + lport--; + if (fd2p == 0) { + write(s, "", 1); + lport = 0; + } else { + char num[8]; + int s2 = rresvport_af(&lport, r->ai_family), s3; + socklen_t len = sizeof(from); + + if (s2 < 0) + goto bad; + listen(s2, 1); + (void)snprintf(num, sizeof(num), "%d", lport); + if (write(s, num, strlen(num) + 1) != + (ssize_t) (strlen(num) + 1)) { + warn("rcmd: write (setting up stderr)"); + (void)close(s2); + goto bad; + } + reads[0].fd = s; + reads[0].events = POLLIN; + reads[1].fd = s2; + reads[1].events = POLLIN; + errno = 0; + pollr = poll(reads, 2, INFTIM); + if (pollr < 1 || (reads[1].revents & POLLIN) == 0) { + if (errno != 0) + warn("poll: setting up stderr"); + else + warnx("poll: protocol failure in circuit setup"); + (void)close(s2); + goto bad; + } + s3 = accept(s2, (struct sockaddr *)(void *)&from, &len); + (void)close(s2); + if (s3 < 0) { + warn("rcmd: accept"); + lport = 0; + goto bad; + } + *fd2p = s3; + switch (((struct sockaddr *)(void *)&from)->sa_family) { + case AF_INET: +#ifdef INET6 + case AF_INET6: +#endif + if (getnameinfo((struct sockaddr *)(void *)&from, len, + NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || + (atoi(num) >= IPPORT_RESERVED || + atoi(num) < IPPORT_RESERVED / 2)) { + warnx("rcmd: protocol failure in circuit setup."); + goto bad2; + } + break; + default: + break; + } + } + + (void)write(s, locuser, strlen(locuser)+1); + (void)write(s, remuser, strlen(remuser)+1); + (void)write(s, cmd, strlen(cmd)+1); + if (read(s, &c, 1) != 1) { + warn("%s", *ahost); + goto bad2; + } + if (c != 0) { + while (read(s, &c, 1) == 1) { + (void)write(STDERR_FILENO, &c, 1); + if (c == '\n') + break; + } + goto bad2; + } + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return (s); +bad2: + if (lport) + (void)close(*fd2p); +bad: + (void)close(s); + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return (-1); +} + +/* + * based on code written by Chris Siebenmann + */ +/* ARGSUSED */ +static int +rshrcmd(ahost, rport, locuser, remuser, cmd, fd2p, rshcmd) + char **ahost; + u_int32_t rport; + const char *locuser, *remuser, *cmd; + int *fd2p; + const char *rshcmd; +{ + pid_t pid; + int sp[2], ep[2]; + char *p; + struct passwd *pw, pwres; + char pwbuf[1024]; + + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + /* What rsh/shell to use. */ + if (rshcmd == NULL) + rshcmd = _PATH_BIN_RCMD; + + /* locuser must exist on this host. */ + if (getpwnam_r(locuser, &pwres, pwbuf, sizeof(pwbuf), &pw) != 0 || + pw == NULL) { + warnx("rshrcmd: unknown user: %s", locuser); + return(-1); + } + + /* get a socketpair we'll use for stdin and stdout. */ + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sp) < 0) { + warn("rshrcmd: socketpair"); + return (-1); + } + /* we will use this for the fd2 pointer */ + if (fd2p) { + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, ep) < 0) { + warn("rshrcmd: socketpair"); + return (-1); + } + *fd2p = ep[0]; + } + + pid = fork(); + if (pid < 0) { + warn("rshrcmd: fork"); + return (-1); + } + if (pid == 0) { + /* + * child + * - we use sp[1] to be stdin/stdout, and close sp[0] + * - with fd2p, we use ep[1] for stderr, and close ep[0] + */ + (void)close(sp[0]); + if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { + warn("rshrcmd: dup2"); + _exit(1); + } + (void)close(sp[1]); + if (fd2p) { + if (dup2(ep[1], 2) < 0) { + warn("rshrcmd: dup2"); + _exit(1); + } + (void)close(ep[0]); + (void)close(ep[1]); + } else if (dup2(0, 2) < 0) { + warn("rshrcmd: dup2"); + _exit(1); + } + /* fork again to lose parent. */ + pid = fork(); + if (pid < 0) { + warn("rshrcmd: second fork"); + _exit(1); + } + if (pid > 0) + _exit(0); + + /* Orphan. Become local user for rshprog. */ + if (setuid(pw->pw_uid)) { + warn("rshrcmd: setuid(%lu)", (u_long)pw->pw_uid); + _exit(1); + } + + /* + * If we are rcmd'ing to "localhost" as the same user as we are, + * then avoid running remote shell for efficiency. + */ + if (strcmp(*ahost, "localhost") == 0 && + strcmp(locuser, remuser) == 0) { + if (pw->pw_shell[0] == '\0') + rshcmd = _PATH_BSHELL; + else + rshcmd = pw->pw_shell; + p = strrchr(rshcmd, '/'); + execlp(rshcmd, p ? p + 1 : rshcmd, "-c", cmd, NULL); + } else { + p = strrchr(rshcmd, '/'); + execlp(rshcmd, p ? p + 1 : rshcmd, *ahost, "-l", + remuser, cmd, NULL); + } + warn("rshrcmd: exec %s", rshcmd); + _exit(1); + } + /* Parent */ + (void)close(sp[1]); + if (fd2p) + (void)close(ep[1]); + + (void)waitpid(pid, NULL, 0); + return (sp[0]); +} + +int +rresvport(alport) + int *alport; +{ + + _DIAGASSERT(alport != NULL); + + return rresvport_af(alport, AF_INET); +} + +int +rresvport_af(alport, family) + int *alport; + int family; +{ + struct sockaddr_storage ss; + struct sockaddr *sa; + int salen; + int s; + u_int16_t *portp; + + _DIAGASSERT(alport != NULL); + + memset(&ss, 0, sizeof(ss)); + sa = (struct sockaddr *)(void *)&ss; + switch (family) { + case AF_INET: +#ifdef BSD4_4 + sa->sa_len = +#endif + salen = sizeof(struct sockaddr_in); + portp = &((struct sockaddr_in *)(void *)sa)->sin_port; + break; +#ifdef INET6 + case AF_INET6: +#ifdef BSD4_4 + sa->sa_len = +#endif + salen = sizeof(struct sockaddr_in6); + portp = &((struct sockaddr_in6 *)(void *)sa)->sin6_port; + break; +#endif + default: + errno = EAFNOSUPPORT; + return (-1); + } + sa->sa_family = family; + s = socket(family, SOCK_STREAM, 0); + if (s < 0) + return (-1); +#ifdef BSD4_4 + switch (family) { + case AF_INET: + case AF_INET6: + *portp = 0; + if (bindresvport(s, (struct sockaddr_in *)(void *)sa) < 0) { + int sverr = errno; + + (void)close(s); + errno = sverr; + return (-1); + } + *alport = (int)ntohs(*portp); + return (s); + default: + /* is it necessary to try keep code for other AFs? */ + break; + } +#endif + for (;;) { + *portp = htons((u_short)*alport); + if (bind(s, sa, (socklen_t)salen) >= 0) + return (s); + if (errno != EADDRINUSE) { + (void)close(s); + return (-1); + } + (*alport)--; + if (*alport == IPPORT_RESERVED/2) { + (void)close(s); + errno = EAGAIN; /* close */ + return (-1); + } + } +} + +int __check_rhosts_file = 1; +const char *__rcmd_errstr; + +int +ruserok(rhost, superuser, ruser, luser) + const char *rhost, *ruser, *luser; + int superuser; +{ + struct addrinfo hints, *res, *r; + int error; + + _DIAGASSERT(rhost != NULL); + _DIAGASSERT(ruser != NULL); + _DIAGASSERT(luser != NULL); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ + error = getaddrinfo(rhost, "0", &hints, &res); + if (error) + return (-1); + + for (r = res; r; r = r->ai_next) { + if (iruserok_sa(r->ai_addr, (int)r->ai_addrlen, superuser, + ruser, luser) == 0) { + freeaddrinfo(res); + return (0); + } + } + freeaddrinfo(res); + return (-1); +} + +/* + * New .rhosts strategy: We are passed an ip address. We spin through + * hosts.equiv and .rhosts looking for a match. When the .rhosts only + * has ip addresses, we don't have to trust a nameserver. When it + * contains hostnames, we spin through the list of addresses the nameserver + * gives us and look for a match. + * + * Returns 0 if ok, -1 if not ok. + */ +int +iruserok(raddr, superuser, ruser, luser) + u_int32_t raddr; + int superuser; + const char *ruser, *luser; +{ + struct sockaddr_in irsin; + + memset(&irsin, 0, sizeof(irsin)); + irsin.sin_family = AF_INET; +#ifdef BSD4_4 + irsin.sin_len = sizeof(struct sockaddr_in); +#endif + memcpy(&irsin.sin_addr, &raddr, sizeof(irsin.sin_addr)); + return iruserok_sa(&irsin, sizeof(struct sockaddr_in), superuser, ruser, + luser); +} + +/* + * 2nd and 3rd arguments are typed like this, to avoid dependency between + * unistd.h and sys/socket.h. There's no better way. + */ +int +iruserok_sa(raddr, rlen, superuser, ruser, luser) + const void *raddr; + int rlen; + int superuser; + const char *ruser, *luser; +{ + const struct sockaddr *sa; + struct stat sbuf; + struct passwd *pwd, pwres; + FILE *hostf; + uid_t uid; + gid_t gid; + int isvaliduser; + char pbuf[MAXPATHLEN]; + char pwbuf[1024]; + + _DIAGASSERT(raddr != NULL); + _DIAGASSERT(ruser != NULL); + _DIAGASSERT(luser != NULL); + + sa = raddr; + + __rcmd_errstr = NULL; + + hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); + + if (hostf) { + if (__ivaliduser_sa(hostf, sa, (socklen_t)rlen, luser, + ruser) == 0) { + (void)fclose(hostf); + return (0); + } + (void)fclose(hostf); + } + + isvaliduser = -1; + if (__check_rhosts_file || superuser) { + + if (getpwnam_r(luser, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 + || pwd == NULL) + return (-1); + (void)strlcpy(pbuf, pwd->pw_dir, sizeof(pbuf)); + (void)strlcat(pbuf, "/.rhosts", sizeof(pbuf)); + + /* + * Change effective uid while opening and reading .rhosts. + * If root and reading an NFS mounted file system, can't + * read files that are protected read/write owner only. + */ + uid = geteuid(); + gid = getegid(); + (void)setegid(pwd->pw_gid); + initgroups(pwd->pw_name, pwd->pw_gid); + (void)seteuid(pwd->pw_uid); + hostf = fopen(pbuf, "r"); + + if (hostf != NULL) { + /* + * If not a regular file, or is owned by someone other + * than user or root or if writable by anyone but the + * owner, quit. + */ + if (lstat(pbuf, &sbuf) < 0) + __rcmd_errstr = ".rhosts lstat failed"; + else if (!S_ISREG(sbuf.st_mode)) + __rcmd_errstr = ".rhosts not regular file"; + else if (fstat(fileno(hostf), &sbuf) < 0) + __rcmd_errstr = ".rhosts fstat failed"; + else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) + __rcmd_errstr = "bad .rhosts owner"; + else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) + __rcmd_errstr = + ".rhosts writable by other than owner"; + else + isvaliduser = + __ivaliduser_sa(hostf, sa, (socklen_t)rlen, + luser, ruser); + + (void)fclose(hostf); + } + (void)seteuid(uid); + (void)setegid(gid); + + } + return (isvaliduser); +} + +/* + * XXX + * Don't make static, used by lpd(8). We will be able to change the function + * into static function, when we bump libc major #. + * + * Returns 0 if ok, -1 if not ok. + */ +#ifdef notdef /*_LIBC*/ +static +#endif +int +__ivaliduser(hostf, raddr, luser, ruser) + FILE *hostf; + u_int32_t raddr; + const char *luser, *ruser; +{ + struct sockaddr_in ivusin; + + memset(&ivusin, 0, sizeof(ivusin)); + ivusin.sin_family = AF_INET; +#ifdef BSD4_4 + ivusin.sin_len = sizeof(struct sockaddr_in); +#endif + memcpy(&ivusin.sin_addr, &raddr, sizeof(ivusin.sin_addr)); + return __ivaliduser_sa(hostf, (struct sockaddr *)(void *)&ivusin, + sizeof(struct sockaddr_in), luser, ruser); +} + +#ifdef notdef /*_LIBC*/ +static +#endif +int +__ivaliduser_sa(hostf, raddr, salen, luser, ruser) + FILE *hostf; + const struct sockaddr *raddr; + socklen_t salen; + const char *luser, *ruser; +{ + register char *user, *p; + int ch; + char buf[MAXHOSTNAMELEN + 128]; /* host + login */ + const char *auser, *ahost; + int hostok, userok; + char *rhost = NULL; + int firsttime = 1; + char domain[MAXHOSTNAMELEN]; + + getdomainname(domain, sizeof(domain)); + + _DIAGASSERT(hostf != NULL); + _DIAGASSERT(luser != NULL); + _DIAGASSERT(ruser != NULL); + + while (fgets(buf, sizeof(buf), hostf)) { + p = buf; + /* Skip lines that are too long. */ + if (strchr(p, '\n') == NULL) { + while ((ch = getc(hostf)) != '\n' && ch != EOF) + ; + continue; + } + while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { + *p = isupper((unsigned char)*p) ? + tolower((unsigned char)*p) : *p; + p++; + } + if (*p == ' ' || *p == '\t') { + *p++ = '\0'; + while (*p == ' ' || *p == '\t') + p++; + user = p; + while (*p != '\n' && *p != ' ' && + *p != '\t' && *p != '\0') + p++; + } else + user = p; + *p = '\0'; + + if (p == buf) + continue; + + auser = *user ? user : luser; + ahost = buf; + + if (ahost[0] == '+') + switch (ahost[1]) { + case '\0': + hostok = 1; + break; + + case '@': + if (firsttime) { + rhost = __gethostloop(raddr, salen); + firsttime = 0; + } + if (rhost) + hostok = innetgr(&ahost[2], rhost, + NULL, domain); + else + hostok = 0; + break; + + default: + hostok = __icheckhost(raddr, salen, &ahost[1]); + break; + } + else if (ahost[0] == '-') + switch (ahost[1]) { + case '\0': + hostok = -1; + break; + + case '@': + if (firsttime) { + rhost = __gethostloop(raddr, salen); + firsttime = 0; + } + if (rhost) + hostok = -innetgr(&ahost[2], rhost, + NULL, domain); + else + hostok = 0; + break; + + default: + hostok = -__icheckhost(raddr, salen, &ahost[1]); + break; + } + else + hostok = __icheckhost(raddr, salen, ahost); + + + if (auser[0] == '+') + switch (auser[1]) { + case '\0': + userok = 1; + break; + + case '@': + userok = innetgr(&auser[2], NULL, ruser, + domain); + break; + + default: + userok = strcmp(ruser, &auser[1]) == 0; + break; + } + else if (auser[0] == '-') + switch (auser[1]) { + case '\0': + userok = -1; + break; + + case '@': + userok = -innetgr(&auser[2], NULL, ruser, + domain); + break; + + default: + userok = + -(strcmp(ruser, &auser[1]) == 0 ? 1 : 0); + break; + } + else + userok = strcmp(ruser, auser) == 0; + + /* Check if one component did not match */ + if (hostok == 0 || userok == 0) + continue; + + /* Check if we got a forbidden pair */ + if (userok == -1 || hostok == -1) + return -1; + + /* Check if we got a valid pair */ + if (hostok == 1 && userok == 1) + return 0; + } + return -1; +} + +/* + * Returns "true" if match, 0 if no match. + */ +static int +__icheckhost(raddr, salen, lhost) + const struct sockaddr *raddr; + socklen_t salen; + const char *lhost; +{ + struct addrinfo hints, *res, *r; + char h1[NI_MAXHOST], h2[NI_MAXHOST]; + int error; + const int niflags = NI_NUMERICHOST; + + _DIAGASSERT(raddr != NULL); + _DIAGASSERT(lhost != NULL); + + h1[0] = '\0'; + if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, + niflags) != 0) + return (0); + + /* Resolve laddr into sockaddr */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = raddr->sa_family; + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ + res = NULL; + error = getaddrinfo(lhost, "0", &hints, &res); + if (error) + return (0); + + /* + * Try string comparisons between raddr and laddr. + */ + for (r = res; r; r = r->ai_next) { + h2[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), + NULL, 0, niflags) != 0) + continue; + if (strcmp(h1, h2) == 0) { + freeaddrinfo(res); + return (1); + } + } + + /* No match. */ + freeaddrinfo(res); + return (0); +} + +/* + * Return the hostname associated with the supplied address. + * Do a reverse lookup as well for security. If a loop cannot + * be found, pack the numeric IP address into the string. + */ +static char * +__gethostloop(raddr, salen) + const struct sockaddr *raddr; + socklen_t salen; +{ + static char remotehost[NI_MAXHOST]; + char h1[NI_MAXHOST], h2[NI_MAXHOST]; + struct addrinfo hints, *res, *r; + int error; + const int niflags = NI_NUMERICHOST; + + _DIAGASSERT(raddr != NULL); + + h1[0] = remotehost[0] = '\0'; + if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), + NULL, 0, NI_NAMEREQD) != 0) + return (NULL); + if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, + niflags) != 0) + return (NULL); + + /* + * Look up the name and check that the supplied + * address is in the list + */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = raddr->sa_family; + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ + hints.ai_flags = AI_CANONNAME; + res = NULL; + error = getaddrinfo(remotehost, "0", &hints, &res); + if (error) + return (NULL); + + for (r = res; r; r = r->ai_next) { + h2[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), + NULL, 0, niflags) != 0) + continue; + if (strcmp(h1, h2) == 0) { + freeaddrinfo(res); + return (remotehost); + } + } + + /* + * either the DNS adminstrator has made a configuration + * mistake, or someone has attempted to spoof us + */ + syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", + h1, res->ai_canonname ? res->ai_canonname : remotehost); + freeaddrinfo(res); + return (NULL); +} diff --git a/lib/nbsd_libc/net/recv.c b/lib/nbsd_libc/net/recv.c new file mode 100644 index 000000000..0b1d5d7bc --- /dev/null +++ b/lib/nbsd_libc/net/recv.c @@ -0,0 +1,53 @@ +/* $NetBSD: recv.c,v 1.9 2003/10/22 15:40:19 drochner Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)recv.c 8.2 (Berkeley) 2/21/94"; +#else +__RCSID("$NetBSD: recv.c,v 1.9 2003/10/22 15:40:19 drochner Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include + +ssize_t +recv(s, buf, len, flags) + int s, flags; + size_t len; + void *buf; +{ + return (recvfrom(s, buf, len, flags, NULL, NULL)); +} diff --git a/lib/nbsd_libc/net/resolver.3 b/lib/nbsd_libc/net/resolver.3 new file mode 100644 index 000000000..e9570040f --- /dev/null +++ b/lib/nbsd_libc/net/resolver.3 @@ -0,0 +1,717 @@ +.\" $NetBSD: resolver.3,v 1.23 2009/10/02 06:49:23 cegger Exp $ +.\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") +.\" +.\" 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 ISC DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. +.\" +.\" Copyright (c) 1985, 1995 The Regents of the University of California. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms are permitted provided +.\" that: (1) source distributions retain this entire copyright notice and +.\" comment, and (2) distributions including binaries display the following +.\" acknowledgement: ``This product includes software developed by the +.\" University of California, Berkeley and its contributors'' in the +.\" documentation or other materials provided with the distribution and in +.\" all advertising materials mentioning features or use of this software. +.\" 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED +.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +.\" +.\" @(#)resolver.3 6.5 (Berkeley) 6/23/90 +.\" Id: resolver.man3,v 1.2 2009/01/21 00:12:34 each Exp +.\" +.Dd July 4, 2000 +.Dt RESOLVER 3 +.Os +.Sh NAME +.Nm res_ninit , +.Nm res_ourserver_p , +.Nm fp_resstat , +.Nm res_hostalias , +.Nm res_pquery , +.Nm res_nquery , +.Nm res_nsearch , +.Nm res_nquerydomain , +.Nm res_nmkquery , +.Nm res_nsend , +.Nm res_nupdate , +.Nm res_nmkupdate , +.Nm res_nclose , +.Nm res_nsendsigned , +.Nm res_findzonecut , +.Nm res_getservers , +.Nm res_setservers , +.Nm res_ndestroy , +.Nm dn_comp , +.Nm dn_expand , +.\" .Nm hstrerror , +.Nm res_init , +.Nm res_isourserver , +.Nm fp_nquery , +.Nm p_query , +.Nm hostalias , +.Nm res_query , +.Nm res_search , +.Nm res_querydomain , +.Nm res_mkquery , +.Nm res_send , +.Nm res_update , +.Nm res_close , +.\" .Nm herror +.Nd resolver routines +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In netinet/in.h +.In arpa/nameser.h +.In resolv.h +.In res_update.h +.Vt typedef struct __res_state *res_state ; +.Pp +.Ft int +.Fn res_ninit "res_state statp" +.Ft int +.Fn res_ourserver_p "const res_state statp" "const struct sockaddr_in *addr" +.Ft void +.Fn fp_resstat "const res_state statp" "FILE *fp" +.Ft "const char *" +.Fn res_hostalias "const res_state statp" "const char *name" "char *buf" "size_t buflen" +.Ft int +.Fn res_pquery "const res_state statp" "const u_char *msg" "int msglen" "FILE *fp" +.Ft int +.Fn res_nquery "res_state statp" "const char *dname" "int class" "int type" "u_char *answer" "int anslen" +.Ft int +.Fn res_nsearch "res_state statp" "const char *dname" "int class" "int type" "u_char * answer" "int anslen" +.Ft int +.Fn res_nquerydomain "res_state statp" "const char *name" "const char *domain" "int class" "int type" "u_char *answer" "int anslen" +.Ft int +.Fo res_nmkquery +.Fa "res_state statp" +.Fa "int op" +.Fa "const char *dname" +.Fa "int class" +.Fa "int type" +.Fa "const u_char *data" +.Fa "int datalen" +.Fa "const u_char *newrr" +.Fa "u_char *buf" +.Fa "int buflen" +.Fc +.Ft int +.Fn res_nsend "res_state statp" "const u_char *msg" "int msglen" "u_char *answer" "int anslen" +.Ft int +.Fn res_nupdate "res_state statp" "ns_updrec *rrecp_in" +.Ft int +.Fn res_nmkupdate "res_state statp" "ns_updrec *rrecp_in" "u_char *buf" "int buflen" +.Ft void +.Fn res_nclose "res_state statp" +.Ft int +.Fn res_nsendsigned "res_state statp" "const u_char *msg" "int msglen" "ns_tsig_key *key" "u_char *answer" "int anslen" +.Ft int +.Fn res_findzonecut "res_state statp" "const char *dname" "ns_class class" "int options" "char *zname" "size_t zsize" "struct in_addr *addrs" "int naddrs" +.Ft int +.Fn res_getservers "res_state statp" "union res_sockaddr_union *set" "int cnt" +.Ft void +.Fn res_setservers "res_state statp" "const union res_sockaddr_union *set" "int cnt" +.Ft void +.Fn res_ndestroy "res_state statp" +.Ft int +.Fn dn_comp "const char *exp_dn" "u_char *comp_dn" "int length" "u_char **dnptrs" "u_char **lastdnptr" +.Ft int +.Fn dn_expand "const u_char *msg" "const u_char *eomorig" "const u_char *comp_dn" "char *exp_dn" "int length" +.\" .Ft "const char *" +.\" .Fn hstrerror "int err" +.Ss DEPRECATED +.In sys/types.h +.In netinet/in.h +.In arpa/nameser.h +.In resolv.h +.In res_update.h +.Ft int +.Fn res_init "void" +.Ft int +.Fn res_isourserver "const struct sockaddr_in *addr" +.Ft int +.Fn fp_nquery "const u_char *msg" "int msglen" "FILE *fp" +.Ft void +.Fn p_query "const u_char *msg" "FILE *fp" +.Ft "const char *" +.Fn hostalias "const char *name" +.Ft int +.Fn res_query "const char *dname" "int class" "int type" "u_char *answer" "int anslen" +.Ft int +.Fn res_search "const char *dname" "int class" "int type" "u_char *answer" "int anslen" +.Ft int +.Fn res_querydomain "const char *name" "const char *domain" "int class" "int type" "u_char *answer" "int anslen" +.Ft int +.Fo res_mkquery +.Fa "int op" +.Fa "const char *dname" +.Fa "int class" +.Fa "int type" +.Fa "const char *data" +.Fa "int datalen" +.Fa "struct rrec *newrr" +.Fa "u_char *buf" +.Fa "int buflen" +.Fc +.Ft int +.Fn res_send "const u_char *msg" "int msglen" "u_char *answer" "int anslen" +.Ft int +.Fn res_update "ns_updrec *rrecp_in" +.Ft void +.Fn res_close "void" +.\" .Ft void +.\" .Fn herror "const char *s" +.Sh DESCRIPTION +These routines are used for making, sending and interpreting +query and reply messages with Internet domain name servers. +.Pp +State information is kept in +.Fa statp +and is used to control the behavior of these functions. +.Fa statp +should be set to all zeros prior to the first call to any of these functions. +.Pp +The functions +.Fn res_init , +.Fn res_isourserver , +.Fn fp_nquery , +.Fn p_query , +.Fn hostalias , +.Fn res_query , +.Fn res_search , +.Fn res_querydomain , +.Fn res_mkquery , +.Fn res_send , +.Fn res_update , +.Fn res_close +.\" and +.\" .Fn herror +are deprecated and are supplied for compatability with old source +code. +They use global configuration and state information that is +kept in the structure +.Ft _res +rather than that referenced through +.Ft statp . +.Pp +Most of the values in +.Ft statp +and +.Ft _res +are initialized on the first call to +.Fn res_ninit +/ +.Fn res_init +to reasonable defaults and can be ignored. +Options +stored in +.Ft statp->options +/ +.Ft _res.options +are defined in +.Pa resolv.h +and are as follows. +Options are stored as a simple bit mask containing the bitwise +.Dq OR +of the options enabled. +.Bl -tag -width "RES_USE_INET6" +.It Dv RES_INIT +True if the initial name server address and default domain name are +initialized (i.e., +.Fn res_ninit +/ +.Fn res_init +has been called). +.It Dv RES_DEBUG +Print debugging messages. +.It Dv RES_AAONLY +Accept authoritative answers only. +Should continue until it finds an authoritative answer or finds an error. +Currently this is not implemented. +.It Dv RES_USEVC +Use TCP connections for queries instead of UDP datagrams. +.It Dv RES_STAYOPEN +Used with +.Dv RES_USEVC +to keep the TCP connection open between queries. +This is useful only in programs that regularly do many queries. +UDP should be the normal mode used. +.It Dv RES_IGNTC +Ignore truncation errors, i.e., don't retry with TCP. +.It Dv RES_RECURSE +Set the recursion-desired bit in queries. +This is the default. +(\c +.Fn res_nsend +/ +.Fn res_send +does not do iterative queries and expects the name server +to handle recursion.) +.It Dv RES_DEFNAMES +If set, +.Fn res_nsearch +/ +.Fn res_search +will append the default domain name to single-component names +(those that do not contain a dot). +This option is enabled by default. +.It Dv RES_DNSRCH +If this option is set, +.Fn res_nsearch +/ +.Fn res_search +will search for host names in the current domain and in parent domains; see +.Xr hostname 7 . +This is used by the standard host lookup routine +.Xr gethostbyname 3 . +This option is enabled by default. +.It Dv RES_USE_INET6 +Enables support for IPv6-only applications. +This causes IPv4 addresses to be returned as an IPv4 mapped address. +For example, 10.1.1.1 will be returned as ::ffff:10.1.1.1. +The option is meaningful with certain kernel configuration only. +.It Dv RES_USE_EDNS0 +Enables support for OPT pseudo-RR for EDNS0 extension. +With the option, resolver code will attach OPT pseudo-RR into DNS queries, +to inform of our receive buffer size. +The option will allow DNS servers to take advantage of non-default receive +buffer size, and to send larger replies. +DNS query packets with EDNS0 extension is not compatible with +non-EDNS0 DNS servers. +.It Dv RES_NOALIASES +This option turns off the user level aliasing feature controlled by +the +.Ev HOSTALIASES +environment variable. +Network daemons should set this option. +.It Dv RES_ROTATE +This options causes the +.Fn res_nsend +/ +.Fn res_send +to rotate the list of nameservers in +.Fa statp->nsaddr_list +/ +.Fa _res.nsaddr_list . +.It Dv RES_KEEPTSIG +This option causes +.Fn res_nsendsigned +to leave the message unchanged after TSIG verification; otherwise the TSIG +record would be removed and the header updated. +.It Dv RES_NOTLDQUERY +This option causes +.Fn res_nsearch +to not attempt to resolve an unqualified name as if it were a top level +domain (TLD). +This option can cause problems if the site has "localhost" as a TLD rather +than having localhost on one or more elements of the search list. +This option has no effect if neither +.Dv RES_DEFNAMES +or +.Dv RES_DNSRCH +is set. +.El +.Pp +The +.Fn res_ninit +/ +.Fn res_init +routine +reads the configuration file (if any; see +.Xr resolv.conf 5 ) +to get the default domain name, search list and +the Internet address of the local name server(s). +If no server is configured, the host running the resolver is tried. +The current domain name is defined by the hostname +if not specified in the configuration file; +it can be overridden by the environment variable +.Ev LOCALDOMAIN . +This environment variable may contain several blank-separated +tokens if you wish to override the +.Fa search list +on a per-process basis. This is similar to the +.Fa search +command in the configuration file. +Another environment variable +.Ev RES_OPTIONS +can be set to override certain internal resolver options which are otherwise +set by changing fields in the +.Ft statp +/ +.Ft _res +structure or are inherited from the configuration file's +.Fa options +command. +The syntax of the +.Ev RES_OPTIONS +environment variable is explained in +.Xr resolv.conf 5 . +Initialization normally occurs on the first call +to one of the other resolver routines. +.Pp +The memory referred to by +.Ft statp +must be set to all zeros prior to the first call to +.Fn res_ninit . +.Fn res_ndestroy +should be call to free memory allocated by +.Fn res_ninit +after last use. +.Pp +The +.Fn res_nquery +/ +.Fn res_query +functions provides interfaces to the server query mechanism. +They constructs a query, sends it to the local server, +awaits a response, and makes preliminary checks on the reply. +The query requests information of the specified +.Fa type +and +.Fa class +for the specified fully-qualified domain name +.Fa dname . +The reply message is left in the +.Fa answer +buffer with length +.Fa anslen +supplied by the caller. +.Fn res_nquery +/ +.Fn res_query +return -1 on error or the length of the answer. +.Pp +The +.Fn res_nsearch +/ +.Fn res_search +routines make a query and awaits a response like +.Fn res_nquery +/ +.Fn res_query , +but in addition, it implements the default and search rules +controlled by the +.Dv RES_DEFNAMES +and +.Dv RES_DNSRCH +options. +It returns the length of the first successful reply which is stored in +.Ft answer +or -1 on error. +.Pp +The remaining routines are lower-level routines used by +.Fn res_nquery +/ +.Fn res_query . +The +.Fn res_nmkquery +/ +.Fn res_mkquery +functions +constructs a standard query message and places it in +.Fa buf . +It returns the size of the query, or \-1 if the query is +larger than +.Fa buflen . +The query type +.Fa op +is usually +.Dv QUERY , +but can be any of the query types defined in +.Pa . +The domain name for the query is given by +.Fa dname . +.Fa newrr +is currently unused but is intended for making update messages. +.Pp +The +.Fn res_nsend +/ +.Fn res_send +/ +.Fn res_nsendsigned +routines +sends a pre-formatted query and returns an answer. +It will call +.Fn res_ninit +/ +.Fn res_init +if +.Dv RES_INIT +is not set, send the query to the local name server, and +handle timeouts and retries. Additionally, +.Fn res_nsendsigned +will use TSIG signatures to add authentication to the query and verify the +response. In this case, only one nameserver will be contacted. +The length of the reply message is returned, or \-1 if there were errors. +.Pp +.Fn res_nquery +/ +.Fn res_query , +.Fn res_nsearch +/ +.Fn res_search +and +.Fn res_nsend +/ +.Fn res_send +return a length that may be bigger than +.Fa anslen . +In that case the query should be retried with a bigger buffer. +NOTE the answer to the second query may be larger still so supplying +a buffer that bigger that the answer returned by the previous +query is recommended. +.Pp +.Fa answer +MUST be big enough to receive a maximum UDP response from the server or +parts of the answer will be silently discarded. +The default maximum UDP response size is 512 bytes. +.Pp +The function +.Fn res_ourserver_p +returns true when +.Fa inp +is one of the servers in +.Fa statp->nsaddr_list +/ +.Fa _res.nsaddr_list . +.Pp +The functions +.Fn fp_nquery +/ +.Fn p_query +print out the query and any answer in +.Fa msg +on +.Fa fp . +.Fn p_query +is equivalent to +.Fn fp_nquery +with +.Fa msglen +set to 512. +.Pp +The function +.Fn fp_resstat +prints out the active flag bits in +.Fa statp->options +preceeded by the text ";; res options:" on +.Fa file . +.Pp +The functions +.Fn res_hostalias +/ +.Fn hostalias +lookup up name in the file referred to by the +.Ev HOSTALIASES +files return a fully qualified hostname if found or NULL if +not found or an error occurred. +.Fn res_hostalias +uses +.Fa buf +to store the result in, +.Fn hostalias +uses a static buffer. +.Pp +The functions +.Fn res_getservers +and +.Fn res_setservers +are used to get and set the list of server to be queried. +.Pp +The functions +.Fn res_nupdate +/ +.Fn res_update +take a list of ns_updrec +.Fa rrecp_in . +Identifies the containing zone for each record and groups the records +according to containing zone maintaining in zone order then sends and update +request to the servers for these zones. The number of zones updated is +returned or -1 on error. Note that +.Fn res_nupdate +will perform TSIG authenticated dynamic update operations if the key is not +NULL. +.Pp +The function +.Fn res_findzonecut +discovers the closest enclosing zone cut for a specified domain name, +and finds the IP addresses of the zone's master servers. +.Pp +The functions +.Fn res_nmkupdate +/ +.Fn res_mkupdate +take a linked list of ns_updrec +.Fa rrecp_in +and construct a UPDATE message in +.Fa buf . +.Fn res_nmkupdate +/ +.Fn res_mkupdate +return the length of the constructed message on no error or one of the +following error values. +.Bl -inset -width "-5" +.It -1 +An error occurred parsing +.Fa rrecp_in . +.It -2 +The buffer +.Fa buf +was too small. +.It -3 +The first record was not a zone section or there was a section order problem. +The section order is S_ZONE, S_PREREQ and S_UPDATE. +.It -4 +A number overflow occurred. +.It -5 +Unknown operation or no records. +.El +.Pp +The functions +.Fn res_nclose +/ +.Fn res_close +close any open files referenced through +.Fa statp +/ +.Fa _res . +.Pp +The function +.Fn res_ndestroy +calls +.Fn res_nclose +then frees any memory allocated by +.Fn res_ninit . +.Pp +The +.Fn dn_comp +function +compresses the domain name +.Fa exp_dn +and stores it in +.Fa comp_dn . +The size of the compressed name is returned or \-1 if there were errors. +The size of the array pointed to by +.Fa comp_dn +is given by +.Fa length . +The compression uses +an array of pointers +.Fa dnptrs +to previously-compressed names in the current message. +The first pointer points to +the beginning of the message and the list ends with +.Dv NULL . +The limit to the array is specified by +.Fa lastdnptr . +A side effect of +.Fn dn_comp +is to update the list of pointers for labels inserted into the message +as the name is compressed. If +.Fa dnptr +is +.Dv NULL , +names are not compressed. If +.Fa lastdnptr +is +.Dv NULL , +the list of labels is not updated. +.Pp +The +.Fn dn_expand +entry expands the compressed domain name +.Fa comp_dn +to a full domain name. +The compressed name is contained in a query or reply message; +.Fa msg +is a pointer to the beginning of the message. +.Fa eomorig +is a pointer to the first location after the message. +The uncompressed name is placed in the buffer indicated by +.Fa exp_dn +which is of size +.Fa length . +The size of compressed name is returned or \-1 if there was an error. +.Pp +The variables +.Ft statp->res_h_errno +/ +.Ft _res.res_h_errno +and external variable +.Ft h_errno +is set whenever an error occurs during resolver operation. The following +definitions are given in +.Pa : +.Bd -literal +#define NETDB_INTERNAL -1 +/* see errno */ +#define NETDB_SUCCESS 0 +/* no problem */ +#define HOST_NOT_FOUND 1 +/* Authoritative Answer Host not found */ +#define TRY_AGAIN 2 +/* Non-Authoritative not found, or SERVFAIL */ +#define NO_RECOVERY 3 +/* Non-Recoverable: FORMERR, REFUSED, NOTIMP */ +#define NO_DATA 4 +/* Valid name, no data for requested type */ +.Ed +.\" .Pp +.\" The +.\" .Fn herror +.\" function writes a message to the diagnostic output consisting of the string +.\" parameter +.\" .Fa s , +.\" the constant string ": ", and a message corresponding to the value of +.\" .Ft h_errno . +.\" .Pp +.\" The +.\" .Fn hstrerror +.\" function returns a string which is the message text corresponding to the +.\" value of the +.\" .Fa err +.\" parameter. +.Sh FILES +.Bl -tag -width "/etc/resolv.conf " +.It Pa +/etc/resolv.conf +The configuration file, see +.Xr resolv.conf 5 . +.El +.Sh SEE ALSO +.Xr gethostbyname 3 , +.Xr hostname 7 , +.Xr resolv.conf 5 , +.Xr named 8 +.Pp +.%T RFC 974 , +.%T RFC 1032 , +.%T RFC 1033 , +.%T RFC 1034 , +.%T RFC 1035 , +.%T RFC 1535 +.Rs +.%T "Name Server Operations Guide for BIND" +.Re +.Sh HISTORY +The +.Nm +function appeared in +.Bx 4.3 . diff --git a/lib/nbsd_libc/net/rthdr.c b/lib/nbsd_libc/net/rthdr.c new file mode 100644 index 000000000..f008c5898 --- /dev/null +++ b/lib/nbsd_libc/net/rthdr.c @@ -0,0 +1,462 @@ +/* $NetBSD: rthdr.c,v 1.17 2009/02/05 23:22:39 lukem Exp $ */ + +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * 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 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 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 PROJECT 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: rthdr.c,v 1.17 2009/02/05 23:22:39 lukem Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet6_rthdr_add,_inet6_rthdr_add) +__weak_alias(inet6_rthdr_getaddr,_inet6_rthdr_getaddr) +__weak_alias(inet6_rthdr_getflags,_inet6_rthdr_getflags) +__weak_alias(inet6_rthdr_init,_inet6_rthdr_init) +__weak_alias(inet6_rthdr_lasthop,_inet6_rthdr_lasthop) +__weak_alias(inet6_rthdr_segments,_inet6_rthdr_segments) +__weak_alias(inet6_rthdr_space,_inet6_rthdr_space) +__weak_alias(inet6_rth_space, _inet6_rth_space) +__weak_alias(inet6_rth_init, _inet6_rth_init) +__weak_alias(inet6_rth_add, _inet6_rth_add) +__weak_alias(inet6_rth_reverse, _inet6_rth_reverse) +__weak_alias(inet6_rth_segments, _inet6_rth_segments) +__weak_alias(inet6_rth_getaddr, _inet6_rth_getaddr) +#endif + +/* + * RFC2292 API + */ + +size_t +inet6_rthdr_space(type, seg) + int type, seg; +{ + switch (type) { + case IPV6_RTHDR_TYPE_0: + if (seg < 1 || seg > 23) + return (0); + return (CMSG_SPACE(sizeof(struct in6_addr) * seg + + sizeof(struct ip6_rthdr0))); + default: + return (0); + } +} + +struct cmsghdr * +inet6_rthdr_init(bp, type) + void *bp; + int type; +{ + struct cmsghdr *ch; + struct ip6_rthdr *rthdr; + + _DIAGASSERT(bp != NULL); + + ch = (struct cmsghdr *)bp; + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(ch); + + ch->cmsg_level = IPPROTO_IPV6; + ch->cmsg_type = IPV6_RTHDR; + + switch (type) { + case IPV6_RTHDR_TYPE_0: +#ifdef COMPAT_RFC2292 + ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0) - + sizeof(struct in6_addr)); +#else + ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0)); +#endif + (void)memset(rthdr, 0, sizeof(struct ip6_rthdr0)); + rthdr->ip6r_type = IPV6_RTHDR_TYPE_0; + return (ch); + default: + return (NULL); + } +} + +int +inet6_rthdr_add(cmsg, addr, flags) + struct cmsghdr *cmsg; + const struct in6_addr *addr; + u_int flags; +{ + struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(addr != NULL); + + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr; + if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) + return (-1); + if (rt0->ip6r0_segleft == 23) + return (-1); + if (flags != IPV6_RTHDR_LOOSE) + return (-1); + rt0->ip6r0_segleft++; + (void)memcpy(((caddr_t)(void *)rt0) + + ((rt0->ip6r0_len + 1) << 3), addr, sizeof(struct in6_addr)); + rt0->ip6r0_len += sizeof(struct in6_addr) >> 3; + cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3); + break; + } + default: + return (-1); + } + + return (0); +} + +int +inet6_rthdr_lasthop(cmsg, flags) + struct cmsghdr *cmsg; + unsigned int flags; +{ + struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr; + if (rt0->ip6r0_segleft > 23) + return (-1); + if (flags != IPV6_RTHDR_LOOSE) + return (-1); + break; + } + default: + return (-1); + } + + return (0); +} + +#if 0 +int +inet6_rthdr_reverse(in, out) + const struct cmsghdr *in; + struct cmsghdr *out; +{ + + return (-1); +} +#endif + +int +inet6_rthdr_segments(cmsg) + const struct cmsghdr *cmsg; +{ + const struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = __UNCONST(CCMSG_DATA(cmsg)); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + const struct ip6_rthdr0 *rt0 = + (const struct ip6_rthdr0 *)(const void *)rthdr; + + if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) + return (-1); + + return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); + } + + default: + return (-1); + } +} + +struct in6_addr * +inet6_rthdr_getaddr(cmsg, idx) + struct cmsghdr *cmsg; + int idx; +{ + struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr; + int naddr; + + if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) + return NULL; + naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); + if (idx <= 0 || naddr < idx) + return NULL; +#ifdef COMPAT_RFC2292 + return ((struct in6_addr *)(void *)(rt0 + 1)) + idx - 1; +#else + return ((struct in6_addr *)(void *)(rt0 + 1)) + idx; +#endif + } + + default: + return NULL; + } +} + +int +inet6_rthdr_getflags(cmsg, idx) + const struct cmsghdr *cmsg; + int idx; +{ + const struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = __UNCONST(CCMSG_DATA(cmsg)); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + const struct ip6_rthdr0 *rt0 = (const struct ip6_rthdr0 *) + (const void *)rthdr; + int naddr; + + if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) + return (-1); + naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); + if (idx < 0 || naddr < idx) + return (-1); + return IPV6_RTHDR_LOOSE; + } + + default: + return (-1); + } +} + +/* + * RFC3542 (2292bis) API + */ + +socklen_t +inet6_rth_space(int type, int segments) +{ + switch (type) { + case IPV6_RTHDR_TYPE_0: + return (((segments * 2) + 1) << 3); + default: + return (0); /* type not suppported */ + } +} + +void * +inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments) +{ + struct ip6_rthdr *rth; + struct ip6_rthdr0 *rth0; + + _DIAGASSERT(bp != NULL); + + rth = (struct ip6_rthdr *)bp; + + switch (type) { + case IPV6_RTHDR_TYPE_0: + /* length validation */ + if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments)) + return (NULL); + + memset(bp, 0, bp_len); + rth0 = (struct ip6_rthdr0 *)(void *)rth; + rth0->ip6r0_len = segments * 2; + rth0->ip6r0_type = IPV6_RTHDR_TYPE_0; + rth0->ip6r0_segleft = 0; + rth0->ip6r0_reserved = 0; + break; + default: + return (NULL); /* type not supported */ + } + + return (bp); +} + +int +inet6_rth_add(void *bp, const struct in6_addr *addr) +{ + struct ip6_rthdr *rth; + struct ip6_rthdr0 *rth0; + struct in6_addr *nextaddr; + + _DIAGASSERT(bp != NULL); + + rth = (struct ip6_rthdr *)bp; + + switch (rth->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rth0 = (struct ip6_rthdr0 *)(void *)rth; + nextaddr = (struct in6_addr *)(void *)(rth0 + 1) + + rth0->ip6r0_segleft; + *nextaddr = *addr; + rth0->ip6r0_segleft++; + break; + default: + return (-1); /* type not supported */ + } + + return (0); +} + +int +inet6_rth_reverse(const void *in, void *out) +{ + const struct ip6_rthdr *rth_in; + const struct ip6_rthdr0 *rth0_in; + struct ip6_rthdr0 *rth0_out; + int i, segments; + + _DIAGASSERT(in != NULL); + _DIAGASSERT(out != NULL); + + rth_in = (const struct ip6_rthdr *)in; + + switch (rth_in->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rth0_in = (const struct ip6_rthdr0 *)in; + rth0_out = (struct ip6_rthdr0 *)out; + + /* parameter validation XXX too paranoid? */ + if (rth0_in->ip6r0_len % 2) + return (-1); + segments = rth0_in->ip6r0_len / 2; + + /* we can't use memcpy here, since in and out may overlap */ + memmove((void *)rth0_out, (const void *)rth0_in, + (unsigned int)(((rth0_in->ip6r0_len) + 1) << 3)); + rth0_out->ip6r0_segleft = segments; + + /* reverse the addresses */ + for (i = 0; i < segments / 2; i++) { + struct in6_addr addr_tmp, *addr1, *addr2; + + addr1 = (struct in6_addr *)(void *)(rth0_out + 1) + i; + addr2 = (struct in6_addr *)(void *)(rth0_out + 1) + + (segments - i - 1); + addr_tmp = *addr1; + *addr1 = *addr2; + *addr2 = addr_tmp; + } + + break; + default: + return (-1); /* type not supported */ + } + + return (0); +} + +int +inet6_rth_segments(const void *bp) +{ + const struct ip6_rthdr *rh; + const struct ip6_rthdr0 *rh0; + unsigned int addrs; + + _DIAGASSERT(bp != NULL); + + rh = (const struct ip6_rthdr *)bp; + + switch (rh->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rh0 = (const struct ip6_rthdr0 *)bp; + + /* + * Validation for a type-0 routing header. + * Is this too strict? + */ + if ((rh0->ip6r0_len % 2) != 0 || + (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft) + return (-1); + + return (addrs); + default: + return (-1); /* unknown type */ + } +} + +struct in6_addr * +inet6_rth_getaddr(const void *bp, int idx) +{ + const struct ip6_rthdr *rh; + const struct ip6_rthdr0 *rh0; + unsigned int addrs; + + _DIAGASSERT(bp != NULL); + + rh = (const struct ip6_rthdr *)bp; + + switch (rh->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rh0 = (const struct ip6_rthdr0 *)bp; + + /* + * Validation for a type-0 routing header. + * Is this too strict? + */ + if ((rh0->ip6r0_len % 2) != 0 || + (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft) + return (NULL); + + if (idx < 0 || addrs <= (unsigned int)idx) + return (NULL); + + return (((struct in6_addr *)(void *)__UNCONST(rh0 + 1)) + idx); + default: + return (NULL); /* unknown type */ + } +} diff --git a/lib/nbsd_libc/net/send.c b/lib/nbsd_libc/net/send.c new file mode 100644 index 000000000..d7b35141c --- /dev/null +++ b/lib/nbsd_libc/net/send.c @@ -0,0 +1,58 @@ +/* $NetBSD: send.c,v 1.9 2003/08/07 16:43:15 agc Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)send.c 8.2 (Berkeley) 2/21/94"; +#else +__RCSID("$NetBSD: send.c,v 1.9 2003/08/07 16:43:15 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include + +#ifdef __weak_alias +__weak_alias(send, _send) +#endif + +ssize_t +send(s, msg, len, flags) + int s, flags; + size_t len; + const void *msg; +{ + return (sendto(s, msg, len, flags, NULL, 0)); +} diff --git a/lib/nbsd_libc/net/servent.h b/lib/nbsd_libc/net/servent.h new file mode 100644 index 000000000..c7dfcc214 --- /dev/null +++ b/lib/nbsd_libc/net/servent.h @@ -0,0 +1,65 @@ +/* $NetBSD: servent.h,v 1.4 2010/04/25 00:54:46 joerg Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 + +struct servent_data { + FILE *plainfile; + struct cdbr *cdb; + struct servent serv; + char **aliases; + size_t maxaliases; + int flags; +#define _SV_STAYOPEN 1 +#define _SV_CDB 2 +#define _SV_PLAINFILE 4 +#define _SV_FIRST 8 + uint32_t cdb_index; + uint8_t *cdb_buf; + size_t cdb_buf_len; + char *line; + void *dummy; +}; + +struct servent *getservent_r(struct servent *, struct servent_data *); +struct servent *getservbyname_r(const char *, const char *, + struct servent *, struct servent_data *); +struct servent *getservbyport_r(int, const char *, + struct servent *, struct servent_data *); +void setservent_r(int, struct servent_data *); +void endservent_r(struct servent_data *); + +int _servent_open(struct servent_data *); +void _servent_close(struct servent_data *); +int _servent_getline(struct servent_data *); +struct servent *_servent_parseline(struct servent_data *, struct servent *); +struct servent *_servent_parsedb(struct servent_data *, struct servent *, + const uint8_t *, size_t); diff --git a/lib/nbsd_libc/net/sethostent.c b/lib/nbsd_libc/net/sethostent.c new file mode 100644 index 000000000..e96709563 --- /dev/null +++ b/lib/nbsd_libc/net/sethostent.c @@ -0,0 +1,82 @@ +/* $NetBSD: sethostent.c,v 1.16 2007/01/27 22:27:35 christos Exp $ */ + +/* + * Copyright (c) 1985, 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp "; +#else +__RCSID("$NetBSD: sethostent.c,v 1.16 2007/01/27 22:27:35 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(sethostent,_sethostent) +__weak_alias(endhostent,_endhostent) +#endif + +void _endhtent __P((void)); +#ifndef _REENTRANT +void res_close __P((void)); +#endif +void _sethtent __P((int)); + +void +/*ARGSUSED*/ +sethostent(stayopen) + int stayopen; +{ +#ifndef _REENTRANT + if ((_res.options & RES_INIT) == 0 && res_init() == -1) + return; + if (stayopen) + _res.options |= RES_STAYOPEN | RES_USEVC; +#endif + _sethtent(stayopen); +} + +void +endhostent() +{ +#ifndef _REENTRANT + _res.options &= ~(RES_STAYOPEN | RES_USEVC); + res_close(); +#endif + _endhtent(); +} diff --git a/lib/nbsd_libc/net/sockatmark.3 b/lib/nbsd_libc/net/sockatmark.3 new file mode 100644 index 000000000..58183f434 --- /dev/null +++ b/lib/nbsd_libc/net/sockatmark.3 @@ -0,0 +1,103 @@ +.\" $NetBSD: sockatmark.3,v 1.9 2008/04/30 13:10:50 martin Exp $ +.\" +.\" Copyright (c) 2001 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 May 15, 2003 +.Dt SOCKATMARK 3 +.Os +.Sh NAME +.Nm sockatmark +.Nd determine whether a socket is at the out-of-band mark +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/socket.h +.Ft int +.Fn sockatmark "int s" +.Sh DESCRIPTION +The +.Nm sockatmark +function determines whether the socket referenced by the file descriptor +.Fa s +is at the out-of-band mark. +.Sh RETURN VALUES +If successful, the +.Nm sockatmark +function returns 1 to indicate that the socket is at an out-of-band mark; +0 is returned if there is no out-of-band mark or the mark is preceded +by in-band data. +Otherwise, -1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Nm sockatmark +function will fail if: +.Bl -tag -width Er +.It Bq Er EBADF +The argument +.Fa s +is not a valid file descriptor. +.It Bq Er ENOTTY +The file descriptor +.Fa s +does not refer to a socket. +.El +.Sh SEE ALSO +.Xr ioctl 2 , +.Xr recv 2 , +.Xr socket 2 +.Rs +.%T "An Introductory 4.4BSD Interprocess Communication Tutorial" +.%A Stuart Sechrest +.Re +.Pq see Pa /usr/share/doc/psd/20.ipctut +.Rs +.%T "Advanced 4.4BSD IPC Tutorial" +.%A Samuel J. Leffler +.%A Robert S. Fabry +.%A William N. Joy +.%A Phil Lapsley +.%A Steve Miller +.%A Chris Torek +.Re +.Pq see Pa /usr/share/doc/psd/21.ipc +.Sh STANDARDS +The +.Nm sockatmark +function conforms to +.St -p1003.1-2001 . +.Sh HISTORY +The +.Nm sockatmark +function appeared in +.St -p1003.1g-2000 +as a replacement for the +.Dv SIOCATMARK +.Xr ioctl 2 +interface. diff --git a/lib/nbsd_libc/net/sockatmark.c b/lib/nbsd_libc/net/sockatmark.c new file mode 100644 index 000000000..887c2cead --- /dev/null +++ b/lib/nbsd_libc/net/sockatmark.c @@ -0,0 +1,56 @@ +/* $NetBSD: sockatmark.c,v 1.2 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2001 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. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sockatmark.c,v 1.2 2008/04/28 20:23:00 martin Exp $"); +#endif + +#include "namespace.h" + +#include +#include + +#include + +int +sockatmark(s) + int s; +{ + int val; + + _DIAGASSERT(s != -1); + + if (ioctl(s, SIOCATMARK, &val) == -1) + return (-1); + + return (val); +} diff --git a/lib/nbsd_libc/net/vars6.c b/lib/nbsd_libc/net/vars6.c new file mode 100644 index 000000000..09abc4c90 --- /dev/null +++ b/lib/nbsd_libc/net/vars6.c @@ -0,0 +1,57 @@ +/* $NetBSD: vars6.c,v 1.7 2005/08/07 16:00:01 christos Exp $ */ + +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * 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 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 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 PROJECT 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: vars6.c,v 1.7 2005/08/07 16:00:01 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(in6addr_any, _in6addr_any) +__weak_alias(in6addr_loopback, _in6addr_loopback) +__weak_alias(in6addr_nodelocal_allnodes, _in6addr_nodelocal_allnodes) +__weak_alias(in6addr_linklocal_allnodes, _in6addr_linklocal_allnodes) +__weak_alias(in6addr_linklocal_allrouters, _in6addr_linklocal_allrouters) +#endif + +/* + * Definitions of some constant IPv6 addresses. + */ +const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; +const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; +const struct in6_addr in6addr_nodelocal_allnodes = IN6ADDR_NODELOCAL_ALLNODES_INIT; +const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; +const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; + diff --git a/lib/nbsd_libc/nls/C.msg b/lib/nbsd_libc/nls/C.msg new file mode 100644 index 000000000..cdc7c6853 --- /dev/null +++ b/lib/nbsd_libc/nls/C.msg @@ -0,0 +1,258 @@ +$set 1 +$ EPERM +1 Operation not permitted +$ ENOENT +2 No such file or directory +$ ESRCH +3 No such process +$ EINTR +4 Interrupted system call +$ EIO +5 Input/output error +$ ENXIO +6 Device not configured +$ E2BIG +7 Argument list too long +$ ENOEXEC +8 Exec format error +$ EBADF +9 Bad file descriptor +$ ECHILD +10 No child processes +$ EDEADLK +11 Resource deadlock avoided +$ ENOMEM +12 Cannot allocate memory +$ EACCES +13 Permission denied +$ EFAULT +14 Bad address +$ ENOTBLK +15 Block device required +$ EBUSY +16 Device busy +$ EEXIST +17 File exists +$ EXDEV +18 Cross-device link +$ ENODEV +19 Operation not supported by device +$ ENOTDIR +20 Not a directory +$ EISDIR +21 Is a directory +$ EINVAL +22 Invalid argument +$ ENFILE +23 Too many open files in system +$ EMFILE +24 Too many open files +$ ENOTTY +25 Inappropriate ioctl for device +$ ETXTBSY +26 Text file busy +$ EFBIG +27 File too large +$ ENOSPC +28 No space left on device +$ ESPIPE +29 Illegal seek +$ EROFS +30 Read-only file system +$ EMLINK +31 Too many links +$ EPIPE +32 Broken pipe +$ EDOM +33 Numerical argument out of domain +$ ERANGE +34 Result too large or too small +$ EAGAIN, EWOULDBLOCK +35 Resource temporarily unavailable +$ EINPROGRESS +36 Operation now in progress +$ EALREADY +37 Operation already in progress +$ ENOTSOCK +38 Socket operation on non-socket +$ EDESTADDRREQ +39 Destination address required +$ EMSGSIZE +40 Message too long +$ EPROTOTYPE +41 Protocol wrong type for socket +$ ENOPROTOOPT +42 Protocol option not available +$ EPROTONOSUPPORT +43 Protocol not supported +$ ESOCKTNOSUPPORT +44 Socket type not supported +$ EOPNOTSUPP +45 Operation not supported +$ EPFNOSUPPORT +46 Protocol family not supported +$ EAFNOSUPPORT +47 Address family not supported by protocol family +$ EADDRINUSE +48 Address already in use +$ EADDRNOTAVAIL +49 Can't assign requested address +$ ENETDOWN +50 Network is down +$ ENETUNREACH +51 Network is unreachable +$ ENETRESET +52 Network dropped connection on reset +$ ECONNABORTED +53 Software caused connection abort +$ ECONNRESET +54 Connection reset by peer +$ ENOBUFS +55 No buffer space available +$ EISCONN +56 Socket is already connected +$ ENOTCONN +57 Socket is not connected +$ ESHUTDOWN +58 Can't send after socket shutdown +$ ETOOMANYREFS +59 Too many references: can't splice +$ ETIMEDOUT +60 Connection timed out +$ ECONNREFUSED +61 Connection refused +$ ELOOP +62 Too many levels of symbolic links +$ ENAMETOOLONG +63 File name too long +$ EHOSTDOWN +64 Host is down +$ EHOSTUNREACH +65 No route to host +$ ENOTEMPTY +66 Directory not empty +$ EPROCLIM +67 Too many processes +$ EUSERS +68 Too many users +$ EDQUOT +69 Disc quota exceeded +$ ESTALE +70 Stale NFS file handle +$ EREMOTE +71 Too many levels of remote in path +$ EBADRPC +72 RPC struct is bad +$ ERPCMISMATCH +73 RPC version wrong +$ EPROGUNAVAIL +74 RPC prog. not avail +$ EPROGMISMATCH +75 Program version wrong +$ EPROCUNAVAIL +76 Bad procedure for program +$ ENOLCK +77 No locks available +$ ENOSYS +78 Function not implemented +$ EFTYPE +79 Inappropriate file type or format +$ EAUTH +80 Authentication error +$ ENEEDAUTH +81 Need authenticator +$ EIDRM +82 Identifier removed +$ ENOMSG +83 No message of desired type +$ EOVERFLOW +84 Value too large to be stored in data type +$ EILSEQ +85 Illegal byte sequence +$ ENOTSUP +86 Not supported +$ ECANCELED +87 Operation Canceled +$ EBADMSG +88 Bad or Corrupt message +$ ENODATA +89 No message available +$ ENOSR +90 No STREAM resources +$ ENOSTR +91 Not a STREAM +$ ETIME +92 STREAM ioctl timeout +$ ENOATTR +93 Attribute not found +$ EMULTIHOP +94 Multihop attempted +$ ENOLINK +95 Link has been severed +$ EPROTO +96 Protocol error +$set 2 +$ SIGHUP +1 Hangup +$ SIGINT +2 Interrupt +$ SIGQUIT +3 Quit +$ SIGILL +4 Illegal instruction +$ SIGTRAP +5 Trace/BPT trap +$ SIGABRT +6 Abort trap +$ SIGEMT +7 EMT trap +$ SIGFPE +8 Floating point exception +$ SIGKILL +9 Killed +$ SIGBUS +10 Bus error +$ SIGSEGV +11 Segmentation fault +$ SIGSYS +12 Bad system call +$ SIGPIPE +13 Broken pipe +$ SIGALRM +14 Alarm clock +$ SIGTERM +15 Terminated +$ SIGURG +16 Urgent I/O condition +$ SIGSTOP +17 Stopped (signal) +$ SIGTSTP +18 Stopped +$ SIGCONT +19 Continued +$ SIGCHLD +20 Child exited +$ SIGTTIN +21 Stopped (tty input) +$ SIGTTOU +22 Stopped (tty output) +$ SIGIO +23 I/O possible +$ SIGXCPU +24 Cputime limit exceeded +$ SIGXFSZ +25 Filesize limit exceeded +$ SIGVTALRM +26 Virtual timer expired +$ SIGPROF +27 Profiling timer expired +$ SIGWINCH +28 Window size changes +$ SIGINFO +29 Information request +$ SIGUSR1 +30 User defined signal 1 +$ SIGUSR2 +31 User defined signal 2 +$ SIGPWR +32 Power fail/restart diff --git a/lib/nbsd_libc/nls/Makefile.inc b/lib/nbsd_libc/nls/Makefile.inc new file mode 100644 index 000000000..2266b5de0 --- /dev/null +++ b/lib/nbsd_libc/nls/Makefile.inc @@ -0,0 +1,16 @@ +# $NetBSD: Makefile.inc,v 1.10 2009/01/11 02:46:29 christos Exp $ + +.PATH: ${.CURDIR}/nls + +SRCS+= catclose.c catgets.c catopen.c +MAN+= catclose.3 catgets.3 catopen.3 + +# indirect reference stubs, to be removed soon. +SRCS+= _catclose.c _catgets.c _catopen.c + +.if ${CITRUS} == "yes" +CPPFLAGS.catopen.c+= -DHAVE_CITRUS -I${LIBCDIR}/citrus +.else +CPPFLAGS.catopen.c+= -UHAVE_CITRUS -I${LIBCDIR}/locale +.endif + diff --git a/lib/nbsd_libc/nls/Pig.msg b/lib/nbsd_libc/nls/Pig.msg new file mode 100644 index 000000000..edff3cd69 --- /dev/null +++ b/lib/nbsd_libc/nls/Pig.msg @@ -0,0 +1,236 @@ +$set 1 +$ EPERM +1 Operationway otnay ermittedpay +$ ENOENT +2 Onay uchsay ilefay orway irectoryday +$ ESRCH +3 Onay uchsay ocesspray +$ EINTR +4 Interruptedway ystemsay allcay +$ EIO +5 Inputway/outputway errorway +$ ENXIO +6 Eviceday otnay onfiguredcay +$ E2BIG +7 Argumentway istlay ootay onglay +$ ENOEXEC +8 Execway ormatfay errorway +$ EBADF +9 Adbay ilefay escriptorday +$ ECHILD +10 Onay ildchay ocessespray +$ EDEADLK +11 Esourceray eadlockday avoidedway +$ ENOMEM +12 Annotcay allocateway emorymay +$ EACCES +13 Ermissionpay eniedday +$ EFAULT +14 Adbay addressway +$ ENOTBLK +15 Ockblay eviceday equiredray +$ EBUSY +16 Eviceday usybay +$ EEXIST +17 Ilefay existsway +$ EXDEV +18 Osscray-eviceday inklay +$ ENODEV +19 Operationway otnay upportedsay ybay eviceday +$ ENOTDIR +20 Otnay away irectoryday +$ EISDIR +21 Isway away irectoryday +$ EINVAL +22 Invalidway argumentway +$ ENFILE +23 Ootay anymay openway ilesfay inway ystemsay +$ EMFILE +24 Ootay anymay openway ilesfay +$ ENOTTY +25 Inappropriateway ioctlway orfay eviceday +$ ETXTBSY +26 Exttay ilefay usybay +$ EFBIG +27 Ilefay ootay argelay +$ ENOSPC +28 Onay acespay eftlay onway eviceday +$ ESPIPE +29 Illegalway eeksay +$ EROFS +30 Eadray-onlyway ilefay ystemsay +$ EMLINK +31 Ootay anymay inkslay +$ EPIPE +32 Okenbray ipepay +$ EDOM +33 Umericalnay argumentway outway ofway omainday +$ ERANGE +34 Esultray ootay argelay +$ EAGAIN, EWOULDBLOCK +35 Esourceray emporarilytay unavailableway +$ EINPROGRESS +36 Operationway ownay inway ogresspray +$ EALREADY +37 Operationway alreadyway inway ogresspray +$ ENOTSOCK +38 Ocketsay operationway onway onnay-ocketsay +$ EDESTADDRREQ +39 Estinationday addressway equiredray +$ EMSGSIZE +40 Essagemay ootay onglay +$ EPROTOTYPE +41 Otocolpray ongwray ypetay orfay ocketsay +$ ENOPROTOOPT +42 Otocolpray otnay availableway +$ EPROTONOSUPPORT +43 Otocolpray otnay upportedsay +$ ESOCKTNOSUPPORT +44 Ocketsay ypetay otnay upportedsay +$ EOPNOTSUPP +45 Operationway otnay upportedsay +$ EPFNOSUPPORT +46 Otocolpray amilyfay otnay upportedsay +$ EAFNOSUPPORT +47 Addressway amilyfay otnay upportedsay ybay otocolpray amilyfay +$ EADDRINUSE +48 Addressway alreadyway inway useway +$ EADDRNOTAVAIL +49 Ancay'tay assignway equestedray addressway +$ ENETDOWN +50 Etworknay isway ownday +$ ENETUNREACH +51 Etworknay isway unreachableway +$ ENETRESET +52 Etworknay oppeddray onnectioncay onway esetray +$ ECONNABORTED +53 Oftwaresay ausedcay onnectioncay abortway +$ ECONNRESET +54 Onnectioncay esetray ybay eerpay +$ ENOBUFS +55 Onay ufferbay acespay availableway +$ EISCONN +56 Ocketsay isway alreadyway onnectedcay +$ ENOTCONN +57 Ocketsay isway otnay onnectedcay +$ ESHUTDOWN +58 Ancay'tay endsay afterway ocketsay utdownshay +$ ETOOMANYREFS +59 Ootay anymay eferencesray: ancay'tay icesplay +$ ETIMEDOUT +60 Onnectioncay imedtay outway +$ ECONNREFUSED +61 Onnectioncay efusedray +$ ELOOP +62 Ootay anymay evelslay ofway ymbolicsay inkslay +$ ENAMETOOLONG +63 Ilefay amenay ootay onglay +$ EHOSTDOWN +64 Osthay isway ownday +$ EHOSTUNREACH +65 Onay outeray otay osthay +$ ENOTEMPTY +66 Irectoryday otnay emptyway +$ EPROCLIM +67 Ootay anymay ocessespray +$ EUSERS +68 Ootay anymay usersway +$ EDQUOT +69 Iscday otaquay exceededway +$ ESTALE +70 Alestay NFSay ilefay andlehay +$ EREMOTE +71 Ootay anymay evelslay ofway emoteray inway athpay +$ EBADRPC +72 RPCay uctstray isway adbay +$ ERPCMISMATCH +73 RPCay ersionvay ongwray +$ EPROGUNAVAIL +74 RPCay ogpray. otnay availway +$ EPROGMISMATCH +75 Ogrampray ersionvay ongwray +$ EPROCUNAVAIL +76 Adbay ocedurepray orfay ogrampray +$ ENOLCK +77 Onay ockslay availableway +$ ENOSYS +78 Unctionfay otnay implementedway +$ EFTYPE +79 Inappropriateway ilefay ypetay orway ormatfay +$ EAUTH +80 Authenticationway errorway +$ ENEEDAUTH +81 Eednay authenticatorway +$ EIDRM +82 Identifierway emovedray +$ ENOMSG +83 Onay essagemay ofway esiredday ypetay +$ EOVERFLOW +84 Aluevay ootay argelay otay ebay oredstay inway ataday ypetay +$ EILSEQ +85 Illegalway ytebay equencesay +$set 2 +$ SIGHUP +1 Anguphay +$ SIGINT +2 Interruptway +$ SIGQUIT +3 Itquay +$ SIGILL +4 Illegalway instructionway +$ SIGTRAP +5 aceTray/BPTay aptray +$ SIGABRT +6 Abortway aptray +$ SIGEMT +7 EMTway aptray +$ SIGFPE +8 Oatingflay ointpay exceptionway +$ SIGKILL +9 Illedkay +$ SIGBUS +10 Usbay errorway +$ SIGSEGV +11 Egmentationsay aultfay +$ SIGSYS +12 Adbay ystemsay allcay +$ SIGPIPE +13 Okenbray ipepay +$ SIGALRM +14 Alarmway ockclay +$ SIGTERM +15 Erminatedtay +$ SIGURG +16 Urgentway Iway/Oway onditioncay +$ SIGSTOP +17 Oppedstay (ignalsay) +$ SIGTSTP +18 Oppedstay +$ SIGCONT +19 Ontinuedcay +$ SIGCHLD +20 Ildchay exitedway +$ SIGTTIN +21 Oppedstay (yttay inputway) +$ SIGTTOU +22 Oppedstay (yttay outputway) +$ SIGIO +23 Iway/Oway ossiblepay +$ SIGXCPU +24 Utimecpay imitlay exceededway +$ SIGXFSZ +25 Ilesizefay imitlay exceededway +$ SIGVTALRM +26 Irtualvay imertay expiredway +$ SIGPROF +27 Ofilingpray imertay expiredway +$ SIGWINCH +28 Indowway izesay angeschay +$ SIGINFO +29 Informationway equestray +$ SIGUSR1 +30 Userway efinedday ignalsay 1 +$ SIGUSR2 +31 Userway efinedday ignalsay 2 +$ SIGPWR +32 Owerpay ailfay/estartray diff --git a/lib/nbsd_libc/nls/_catclose.c b/lib/nbsd_libc/nls/_catclose.c new file mode 100644 index 000000000..97398c732 --- /dev/null +++ b/lib/nbsd_libc/nls/_catclose.c @@ -0,0 +1,26 @@ +/* $NetBSD: _catclose.c,v 1.7 2005/09/13 01:44:09 christos Exp $ */ + +/* + * Written by J.T. Conklin, 10/05/94 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _catclose.c,v 1.7 2005/09/13 01:44:09 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_catclose, catclose) +#else + +#include +int _catclose(nl_catd); + +int +catclose(nl_catd catd) +{ + return _catclose(catd); +} + +#endif diff --git a/lib/nbsd_libc/nls/_catgets.c b/lib/nbsd_libc/nls/_catgets.c new file mode 100644 index 000000000..ea506e75d --- /dev/null +++ b/lib/nbsd_libc/nls/_catgets.c @@ -0,0 +1,26 @@ +/* $NetBSD: _catgets.c,v 1.8 2005/09/13 01:44:10 christos Exp $ */ + +/* + * Written by J.T. Conklin, 10/05/94 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _catgets.c,v 1.8 2005/09/13 01:44:10 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_catgets, catgets) +#else + +#include +char *_catgets(nl_catd, int, int, const char *); + +char * +catgets(nl_catd catd, int set_id, int msg_id, const char *s) +{ + return _catgets(catd, set_id, msg_id, s); +} + +#endif diff --git a/lib/nbsd_libc/nls/_catopen.c b/lib/nbsd_libc/nls/_catopen.c new file mode 100644 index 000000000..440a8aa9a --- /dev/null +++ b/lib/nbsd_libc/nls/_catopen.c @@ -0,0 +1,26 @@ +/* $NetBSD: _catopen.c,v 1.7 2005/09/13 01:44:10 christos Exp $ */ + +/* + * Written by J.T. Conklin, 10/05/94 + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: _catopen.c,v 1.7 2005/09/13 01:44:10 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(_catopen, catopen) +#else + +#include +nl_catd _catopen(__const char *, int); + +nl_catd +catopen(__const char *name, int oflag) +{ + return _catopen(name, oflag); +} + +#endif diff --git a/lib/nbsd_libc/nls/ca.msg b/lib/nbsd_libc/nls/ca.msg new file mode 100644 index 000000000..740028fb8 --- /dev/null +++ b/lib/nbsd_libc/nls/ca.msg @@ -0,0 +1,258 @@ +$set 1 +$ EPERM +1 Operació no permesa +$ ENOENT +2 Arxiu o directori inexistent +$ ESRCH +3 Procés inexistent +$ EINTR +4 Crida del sistema interrompuda +$ EIO +5 Error d'entrada/sortida +$ ENXIO +6 Dispositiu no configurat +$ E2BIG +7 Llista de paràmetres massa llarga +$ ENOEXEC +8 Error en el format de l'executable +$ EBADF +9 Descriptor d'arxiu incorrecte +$ ECHILD +10 No hi ha processos fills +$ EDEADLK +11 S'ha evitat el bloqueig del recurs +$ ENOMEM +12 No es pot assignar la memòria demanada +$ EACCES +13 Permís denegat +$ EFAULT +14 Adreça incorrecta +$ ENOTBLK +15 Es necessita un dispositiu de blocs +$ EBUSY +16 Dispositiu ocupat +$ EEXIST +17 L'arxiu ja existeix +$ EXDEV +18 Enllaç entre dispositius +$ ENODEV +19 Operació no suportada pel dispositiu +$ ENOTDIR +20 No és un directori +$ EISDIR +21 És un directori +$ EINVAL +22 Paràmetre incorrecte +$ ENFILE +23 Hi ha massa arxius oberts al sistema +$ EMFILE +24 Hi ha massa arxius oberts +$ ENOTTY +25 L'ioctl no és adecuat per al dispositiu +$ ETXTBSY +26 Arxiu de text ocupat +$ EFBIG +27 Arxiu massa gran +$ ENOSPC +28 No queda espai lliure en el dispositiu +$ ESPIPE +29 Cerca il·legal +$ EROFS +30 Sistema d'arxius de només lectura +$ EMLINK +31 Massa enllaços +$ EPIPE +32 Canal (pipe) trencat +$ EDOM +33 El resultat surt fora de rang +$ ERANGE +34 Resultat massa gran +$ EAGAIN, EWOULDBLOCK +35 El recurs no està disponible temporalment +$ EINPROGRESS +36 L'operació es troba en progrés actualment +$ EALREADY +37 L'operació ja es troba en progrés +$ ENOTSOCK +38 Operació de tipus socket en quelcom que no ho és +$ EDESTADDRREQ +39 Es requereix l'adreça de destí +$ EMSGSIZE +40 Missatge massa llarg +$ EPROTOTYPE +41 Tipus de protocol incorrecte per al socket +$ ENOPROTOOPT +42 Protocol no disponible +$ EPROTONOSUPPORT +43 Protocol no suportat +$ ESOCKTNOSUPPORT +44 Tipus de socket no suportat +$ EOPNOTSUPP +45 Operació no suportada +$ EPFNOSUPPORT +46 Família de protocols no suportada +$ EAFNOSUPPORT +47 Família d'adreces no suportada per la família de protocols +$ EADDRINUSE +48 L'adreça ja es troba en ús +$ EADDRNOTAVAIL +49 No es pot assignar l'adreça demanada +$ ENETDOWN +50 La xarxa no es troba disponible +$ ENETUNREACH +51 No es pot accedir a la xarxa +$ ENETRESET +52 La connexió a la xarxa s'ha perdut durant la reinicialització +$ ECONNABORTED +53 El programari ha causat l'avort de la connexió +$ ECONNRESET +54 L'interlocutor ha reinicialitzat la comunicació +$ ENOBUFS +55 No hi ha prou espai per a la memoria intermèdia (buffer) +$ EISCONN +56 El socket ja es troba connectat +$ ENOTCONN +57 El socket no es troba connectat +$ ESHUTDOWN +58 No es pot enviar desprès de la desconnexió del socket +$ ETOOMANYREFS +59 Hi ha massa referències: no es poden unir +$ ETIMEDOUT +60 El temps de connexió s'ha esgotat +$ ECONNREFUSED +61 Connexió rebutjada +$ ELOOP +62 Hi ha massa nivells d'enllaços simbòlics +$ ENAMETOOLONG +63 Nom d'arxiu massa llarg +$ EHOSTDOWN +64 La màquina no es troba disponible +$ EHOSTUNREACH +65 No hi ha cap camí fins a la màquina +$ ENOTEMPTY +66 El directori no està buit +$ EPROCLIM +67 Hi ha massa processos +$ EUSERS +68 Hi ha massa usuaris +$ EDQUOT +69 Quota de disc sobrepassada +$ ESTALE +70 Descriptor d'arxiu NFS incorrecte +$ EREMOTE +71 Massa nivells en el camí de destí +$ EBADRPC +72 L'estructura RPC es incorrecta +$ ERPCMISMATCH +73 La versió del RPC es incorrecta +$ EPROGUNAVAIL +74 El programa RPC no es troba disponible +$ EPROGMISMATCH +75 Versió incorrecta del programa +$ EPROCUNAVAIL +76 Procediment erroni per al programa +$ ENOLCK +77 No hi ha bloquejos disponibles +$ ENOSYS +78 Funció no implementada +$ EFTYPE +79 Tipus d'arxiu o de format inadequat +$ EAUTH +80 Error d'autenticació +$ ENEEDAUTH +81 Es necessita un autenticador +$ EIDRM +82 Identificador eliminat +$ ENOMSG +83 No hi ha missatges del tipus desitjat +$ EOVERFLOW +84 Valor massa gran per a ésser emmagatzemat en el tipus de dades +$ EILSEQ +85 Seqüència de bytes il·legal +$ ENOTSUP +86 No suportat +$ ECANCELED +87 Operació cancel·lada +$ EBADMSG +88 Missatje incorrecte o corrupte +$ ENODATA +89 No hi ha missatges disponibles +$ ENOSR +90 No hi ha recursos de tipus STREAM +$ ENOSTR +91 No és un STREAM +$ ETIME +92 Temps d'espera esgotat en el ioctl STREAM +$ ENOATTR +93 Atribut inexistent +$ EMULTIHOP +94 S'ha intentat un multisalt +$ ENOLINK +95 L'enllaç s'ha servit +$ EPROTO +96 Error de protocol +$set 2 +$ SIGHUP +1 Fí de línia (hangup) +$ SIGINT +2 Interrupció +$ SIGQUIT +3 Finalització +$ SIGILL +4 Instrucció il·legal +$ SIGTRAP +5 Depuració (Trace/BPT) +$ SIGABRT +6 Crida d'avort +$ SIGEMT +7 Captura d'EMT +$ SIGFPE +8 Excepció de coma flotant +$ SIGKILL +9 Matat +$ SIGBUS +10 Error del bus +$ SIGSEGV +11 Error de segmentació +$ SIGSYS +12 Crida al sistema incorrecta +$ SIGPIPE +13 Canal (pipe) trencat +$ SIGALRM +14 Alarma de rellotge +$ SIGTERM +15 Finalitzat +$ SIGURG +16 Condició urgent d'E/S +$ SIGSTOP +17 Parat (per senyal) +$ SIGTSTP +18 Parat +$ SIGCONT +19 Continuant +$ SIGCHLD +20 El fill ha acabat +$ SIGTTIN +21 Parat (entrada de tty) +$ SIGTTOU +22 Parat (sortida de tty) +$ SIGIO +23 I/O permesa +$ SIGXCPU +24 S'ha sobrepassat el límit de temps de la CPU +$ SIGXFSZ +25 S'ha sobrepassat el límit de la longitud de l'arxiu +$ SIGVTALRM +26 El temporitzador virtual ha expirat +$ SIGPROF +27 El temporitzador del perfilador ha expirat +$ SIGWINCH +28 Canvis en la mida de la finestra +$ SIGINFO +29 Demanda d'informació +$ SIGUSR1 +30 Senyal 1 definida per l'usuari +$ SIGUSR2 +31 Senyal 2 definida per l'usuari +$ SIGPWR +32 Fallada/reinicialització de l'alimentació diff --git a/lib/nbsd_libc/nls/catclose.3 b/lib/nbsd_libc/nls/catclose.3 new file mode 100644 index 000000000..abfada75b --- /dev/null +++ b/lib/nbsd_libc/nls/catclose.3 @@ -0,0 +1,32 @@ +.\" $NetBSD: catclose.3,v 1.12 2003/07/26 19:24:49 salo Exp $ +.\" +.\" Written by J.T. Conklin . +.\" Public domain. +.\" +.Dd May 29, 1994 +.Dt CATCLOSE 3 +.Os +.Sh NAME +.Nm catclose +.Nd close message catalog +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In nl_types.h +.Ft int +.Fn catclose "nl_catd catd" +.Sh DESCRIPTION +The +.Fn catclose +function closes the message catalog specified by the argument +.Fa catd . +.Sh SEE ALSO +.Xr gencat 1 , +.Xr catgets 3 , +.Xr catopen 3 , +.Xr nls 7 +.Sh STANDARDS +The +.Fn catclose +function conforms to +.St -xpg3 . diff --git a/lib/nbsd_libc/nls/catclose.c b/lib/nbsd_libc/nls/catclose.c new file mode 100644 index 000000000..3c9944084 --- /dev/null +++ b/lib/nbsd_libc/nls/catclose.c @@ -0,0 +1,65 @@ +/* $NetBSD: catclose.c,v 1.13 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: catclose.c,v 1.13 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define _NLS_PRIVATE + +#include "namespace.h" +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(catclose, _catclose) +#endif + +int +_catclose(catd) + nl_catd catd; +{ + if (catd == (nl_catd) -1) { + errno = EBADF; + return -1; + } + + if (catd) { + munmap(catd->__data, (size_t)catd->__size); + free (catd); + } + + return 0; +} diff --git a/lib/nbsd_libc/nls/catgets.3 b/lib/nbsd_libc/nls/catgets.3 new file mode 100644 index 000000000..87c798f78 --- /dev/null +++ b/lib/nbsd_libc/nls/catgets.3 @@ -0,0 +1,73 @@ +.\" $NetBSD: catgets.3,v 1.16 2003/07/26 19:24:49 salo Exp $ +.\" +.\" Written by J.T. Conklin . +.\" Public domain. +.\" +.Dd February 12, 2003 +.Dt CATGETS 3 +.Os +.Sh NAME +.Nm catgets +.Nd retrieve string from message catalog +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In nl_types.h +.Ft char * +.Fn catgets "nl_catd catd" "int set_id" "int msg_id" "const char *s" +.Sh DESCRIPTION +The +.Fn catgets +function attempts to retrieve message +.Fa msg_id +of set +.Fa set_id +from the message catalog referenced by the descriptor +.Fa catd . +The argument +.Fa s +points to a default message which is returned if the function +is unable to retrieve the specified message. +.Sh RETURN VALUES +If the specified message was retrieved successfully, +.Fn catgets +returns a pointer to an internal buffer containing the message string; +otherwise it returns +.Fa s . +.Sh ERRORS +The +.Fn catgets +function will fail if: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa catd +argument is not a valid message catalog descriptor open for reading. +.It Bq Er EINTR +The operation was interrupted by a signal. +.It Bq Er ENOMSG +The message identified by +.Fa set_id +and +.Fa msg_id +is not in the message catalog. +.El +.Sh SEE ALSO +.Xr gencat 1 , +.Xr catclose 3 , +.Xr catopen 3 , +.Xr nls 7 +.Sh STANDARDS +The +.Fn catgets +function conforms to +.St -xpg4.2 . +.Pp +Major Unix vendors are split over the adoption of the two most +important message catalog specifications: catgets or +.Xr gettext 3 . +The primary concern with the catgets interface is that every +translatable string has to define a number (or a symbolic constant) +which must correspond to the message in the catalog. +Duplicate message IDs are not allowed. +Constructing message catalogs is difficult. diff --git a/lib/nbsd_libc/nls/catgets.c b/lib/nbsd_libc/nls/catgets.c new file mode 100644 index 000000000..acee17bd0 --- /dev/null +++ b/lib/nbsd_libc/nls/catgets.c @@ -0,0 +1,117 @@ +/* $NetBSD: catgets.c,v 1.18 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: catgets.c,v 1.18 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define _NLS_PRIVATE + +#include "namespace.h" +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(catgets, _catgets) +#endif + +char * +_catgets(catd, set_id, msg_id, s) + nl_catd catd; + int set_id; + int msg_id; + const char *s; +{ + struct _nls_cat_hdr *cat_hdr; + struct _nls_set_hdr *set_hdr; + struct _nls_msg_hdr *msg_hdr; + int l, u, i, r; + + if (catd == (nl_catd) -1) { + errno = EBADF; + return __UNCONST(s); + } + + cat_hdr = (struct _nls_cat_hdr *)catd->__data; + set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data + + sizeof(struct _nls_cat_hdr)); + + /* binary search, see knuth algorithm b */ + l = 0; + u = ntohl((u_int32_t)cat_hdr->__nsets) - 1; + while (l <= u) { + i = (l + u) / 2; + r = set_id - ntohl((u_int32_t)set_hdr[i].__setno); + + if (r == 0) { + msg_hdr = (struct _nls_msg_hdr *) + (void *)((char *)catd->__data + + sizeof(struct _nls_cat_hdr) + + ntohl((u_int32_t)cat_hdr->__msg_hdr_offset)); + + l = ntohl((u_int32_t)set_hdr[i].__index); + u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1; + while (l <= u) { + i = (l + u) / 2; + r = msg_id - + ntohl((u_int32_t)msg_hdr[i].__msgno); + if (r == 0) { + return ((char *) catd->__data + + sizeof(struct _nls_cat_hdr) + + ntohl((u_int32_t) + cat_hdr->__msg_txt_offset) + + ntohl((u_int32_t) + msg_hdr[i].__offset)); + } else if (r < 0) { + u = i - 1; + } else { + l = i + 1; + } + } + + /* not found */ + goto notfound; + + } else if (r < 0) { + u = i - 1; + } else { + l = i + 1; + } + } + +notfound: + /* not found */ + errno = ENOMSG; + return __UNCONST(s); +} diff --git a/lib/nbsd_libc/nls/catopen.3 b/lib/nbsd_libc/nls/catopen.3 new file mode 100644 index 000000000..c26364f75 --- /dev/null +++ b/lib/nbsd_libc/nls/catopen.3 @@ -0,0 +1,61 @@ +.\" $NetBSD: catopen.3,v 1.14 2003/07/26 19:24:49 salo Exp $ +.\" +.\" Written by J.T. Conklin . +.\" Public domain. +.\" +.Dd May 29, 1994 +.Dt CATOPEN 3 +.Os +.Sh NAME +.Nm catopen +.Nd open message catalog +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In nl_types.h +.Ft nl_catd +.Fn catopen "const char *name" "int oflag" +.Sh DESCRIPTION +The +.Fn catopen +function opens the message catalog specified by +.Fa name +and returns a message catalog descriptor. +If +.Fa name +contains a +.Sq / +then +.Fa name +specifies the full pathname for the message catalog, otherwise the value +of the environment variable +.Ev NLSPATH +is used with +.Fa name +substituted for %N. +.Pp +The +.Fa oflag +argument is reserved for future use and should be set to zero. +.Sh RETURN VALUES +Upon successful completion, +.Fn catopen +returns a message catalog descriptor. +Otherwise, (nl_catd) -1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er ENOMEM +Insufficient memory is available. +.El +.Sh SEE ALSO +.Xr gencat 1 , +.Xr catclose 3 , +.Xr catgets 3 , +.Xr nls 7 +.Sh STANDARDS +The +.Fn catopen +function conforms to +.St -xpg3 . diff --git a/lib/nbsd_libc/nls/catopen.c b/lib/nbsd_libc/nls/catopen.c new file mode 100644 index 000000000..ac1eabb60 --- /dev/null +++ b/lib/nbsd_libc/nls/catopen.c @@ -0,0 +1,194 @@ +/* $NetBSD: catopen.c,v 1.28 2009/03/10 13:15:40 joerg Exp $ */ + +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: catopen.c,v 1.28 2009/03/10 13:15:40 joerg Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define _NLS_PRIVATE + +#include "namespace.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_CITRUS +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_lookup.h" +#include "citrus_aliasname_local.h" +#else +#include "aliasname_local.h" +#endif + +#define NLS_ALIAS_DB "/usr/share/nls/nls.alias" + +#define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L" +#define NLS_DEFAULT_LANG "C" + +#ifdef __weak_alias +__weak_alias(catopen, _catopen) +#endif + +static nl_catd load_msgcat __P((const char *)); + +nl_catd +_catopen(name, oflag) + const char *name; + int oflag; +{ + char tmppath[PATH_MAX+1]; + const char *nlspath; + const char *lang, *reallang; + char *t; + const char *s, *u; + nl_catd catd; + char langbuf[PATH_MAX]; + + if (name == NULL || *name == '\0') + return (nl_catd)-1; + + /* absolute or relative path? */ + if (strchr(name, '/')) + return load_msgcat(name); + + if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL) + nlspath = NLS_DEFAULT_PATH; + if (oflag == NL_CAT_LOCALE) { + lang = setlocale(LC_MESSAGES, NULL); + } + else { + lang = getenv("LANG"); + } + if (lang == NULL || strchr(lang, '/')) + lang = NLS_DEFAULT_LANG; + + reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf)); + if (reallang == NULL) + reallang = lang; + + s = nlspath; + t = tmppath; + do { + while (*s && *s != ':') { + if (*s == '%') { + switch (*(++s)) { + case 'L': /* locale */ + u = reallang; + while (*u && t < tmppath + PATH_MAX) + *t++ = *u++; + break; + case 'N': /* name */ + u = name; + while (*u && t < tmppath + PATH_MAX) + *t++ = *u++; + break; + case 'l': /* lang */ + case 't': /* territory */ + case 'c': /* codeset */ + break; + default: + if (t < tmppath + PATH_MAX) + *t++ = *s; + } + } else { + if (t < tmppath + PATH_MAX) + *t++ = *s; + } + s++; + } + + *t = '\0'; + catd = load_msgcat(tmppath); + if (catd != (nl_catd)-1) + return catd; + + if (*s) + s++; + t = tmppath; + } while (*s); + + return (nl_catd)-1; +} + +static nl_catd +load_msgcat(path) + const char *path; +{ + struct stat st; + nl_catd catd; + void *data; + int fd; + + _DIAGASSERT(path != NULL); + + if ((fd = open(path, O_RDONLY)) == -1) + return (nl_catd)-1; + + if (fstat(fd, &st) != 0) { + close (fd); + return (nl_catd)-1; + } + + data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, + (off_t)0); + close (fd); + + if (data == MAP_FAILED) { + return (nl_catd)-1; + } + + if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) != + _NLS_MAGIC) { + munmap(data, (size_t)st.st_size); + return (nl_catd)-1; + } + + if ((catd = malloc(sizeof (*catd))) == NULL) { + munmap(data, (size_t)st.st_size); + return (nl_catd)-1; + } + + catd->__data = data; + catd->__size = (int)st.st_size; + return catd; +} diff --git a/lib/nbsd_libc/nls/cs.msg b/lib/nbsd_libc/nls/cs.msg new file mode 100644 index 000000000..97e6cbff4 --- /dev/null +++ b/lib/nbsd_libc/nls/cs.msg @@ -0,0 +1,258 @@ +$set 1 +$ EPERM +1 Operace není povolena +$ ENOENT +2 Neexistující soubor nebo adresáø +$ ESRCH +3 Proces neexistuje +$ EINTR +4 Systémové volání pøeru¹eno +$ EIO +5 Vstupní/výstupní chyba +$ ENXIO +6 Zaøízení není nakonfigurováno +$ E2BIG +7 Seznam argumentù je pøíli¹ dlouhý +$ ENOEXEC +8 Chyba formátu spu¹tìného souboru +$ EBADF +9 ©patný souborový deskriptor +$ ECHILD +10 Neexistuje ¾ádný potomek procesu +$ EDEADLK +11 Zabránìno uváznutí (deadlock) prostøedku +$ ENOMEM +12 Nedostatek pamìti +$ EACCES +13 Pøístup odmítnut +$ EFAULT +14 Chybná adresa +$ ENOTBLK +15 Vy¾adováno blokové zaøízení +$ EBUSY +16 Zdroj nebo zaøízení je zaneprázdnìno +$ EEXIST +17 Soubor existuje +$ EXDEV +18 Odkaz vede na jiné zaøízení +$ ENODEV +19 Operace není zaøízením podporována +$ ENOTDIR +20 Toto není adresáø +$ EISDIR +21 Toto je adresáø +$ EINVAL +22 Chybný argument +$ ENFILE +23 Pøíli¹ mnoho otevøených souborù v systému +$ EMFILE +24 Pøíli¹ mnoho otevøených souborù +$ ENOTTY +25 Pro dané zaøízení nevhodné ioctl +$ ETXTBSY +26 S textovým souborem se pracuje +$ EFBIG +27 Soubor je pøíli¹ velký +$ ENOSPC +28 Na zaøízení není volné místo +$ ESPIPE +29 Neplatný lseek(2) +$ EROFS +30 Souborový systém je pouze pro ètení +$ EMLINK +31 Pøíli¹ mnoho odkazù +$ EPIPE +32 Pøeru¹ená roura +$ EDOM +33 Èíselný argument mimo definièní obor +$ ERANGE +34 Výsledek pøíli¹ velký nebo pøíli¹ malý +$ EAGAIN, EWOULDBLOCK +35 Prostøedek je doèasnì nedostupný +$ EINPROGRESS +36 Operace nyní probíhá +$ EALREADY +37 Operace ji¾ probíhá +$ ENOTSOCK +38 Socketová operace na neèem, co není socket +$ EDESTADDRREQ +39 Vy¾adována adresa cíle +$ EMSGSIZE +40 Zpráva je pøíli¹ dlouhá +$ EPROTOTYPE +41 Protokol nepodporuje daný typ socketu +$ ENOPROTOOPT +42 Protokol není dostupný +$ EPROTONOSUPPORT +43 Protokol není podporován +$ ESOCKTNOSUPPORT +44 Typ socketu není podporován +$ EOPNOTSUPP +45 Operace není podporována +$ EPFNOSUPPORT +46 Rodina protokolù není podporována +$ EAFNOSUPPORT +47 Rodina adres není protokolem podporována +$ EADDRINUSE +48 Adresa je ji¾ pou¾ívána +$ EADDRNOTAVAIL +49 Není mo¾no pøidìlit po¾adovanou adresu +$ ENETDOWN +50 Sí» je odpojena +$ ENETUNREACH +51 Sí» je nedosa¾itelná +$ ENETRESET +52 Sí» zru¹ila spojení po resetu +$ ECONNABORTED +53 Program zpùsobil ukonèení spojení +$ ECONNRESET +54 Spojení zru¹eno partnerem +$ ENOBUFS +55 Vyrovnávací pamì» není k dispozici +$ EISCONN +56 Socket je ji¾ u¾ pøipojen +$ ENOTCONN +57 Socket není pøipojen +$ ESHUTDOWN +58 Není mo¾no posílat po shutdownu socketu +$ ETOOMANYREFS +59 Pøíli¹ mnoho referencí: nelze spojit +$ ETIMEDOUT +60 Pøekroèen èasový limit pokusu o spojení +$ ECONNREFUSED +61 Spojení odmítnuto +$ ELOOP +62 Pøíli¹ mnoho úrovní symbolických odkazù +$ ENAMETOOLONG +63 Jméno souboru pøíli¹ dlouhé +$ EHOSTDOWN +64 Vzdálený uzel je odpojen +$ EHOSTUNREACH +65 Neexistuje cesta ke vzdálenému uzlu +$ ENOTEMPTY +66 Adresáø není prázdný +$ EPROCLIM +67 Pøíli¹ mnoho procesù +$ EUSERS +68 Pøíli¹ mnoho u¾ivatelù +$ EDQUOT +69 Disková kvóta pøekroèena +$ ESTALE +70 Propadnutý souborový ukazatel pro NFS +$ EREMOTE +71 Pøíli¹ mnoho úrovní vzdáleného v cestì +$ EBADRPC +72 ©patná RPC struktura +$ ERPCMISMATCH +73 RPC: Nekompatibilní verze RPC +$ EPROGUNAVAIL +74 RPC: Program není dostupný +$ EPROGMISMATCH +75 RPC: Neshoda programu nebo verze +$ EPROCUNAVAIL +76 RPC: Procedura není dostupná +$ ENOLCK +77 Zámky souborových záznamù nejsou dostupné +$ ENOSYS +78 Funkce není implementována +$ EFTYPE +79 Nevhodný typ nebo formát souboru +$ EAUTH +80 Chyba autentifikace +$ ENEEDAUTH +81 Je tøeba se autentifikovat +$ EIDRM +82 Identifikátor zru¹en +$ ENOMSG +83 Neexistuje zpráva ¾ádaného typu +$ EOVERFLOW +84 Hodnota je pro daný datový typ pøíli¹ velká +$ EILSEQ +85 Neplatná posloupnost bajtù +$ ENOTSUP +86 Nepodporováno +$ ECANCELED +87 Operace strornována +$ EBADMSG +88 ©patná nebo poru¹ená zpráva +$ ENODATA +89 ®ádná zpráva není dostupná +$ ENOSR +90 ®ádné STREAM prostøedky +$ ENOSTR +91 Není STREAM +$ ETIME +92 Vypr¹el èasový limit STREAM ioctl +$ ENOATTR +93 Atribut nebyl nalezen +$ EMULTIHOP +94 Pokus o spojení pøes více uzlù +$ ENOLINK +95 Odkaz byl zpøetrhán +$ EPROTO +96 Chyba protokolu +$set 2 +$ SIGHUP +1 Terminál odpojen +$ SIGINT +2 Pøeru¹ení +$ SIGQUIT +3 Konec +$ SIGILL +4 Chybná instrukce +$ SIGTRAP +5 Ladící instrukce +$ SIGABRT +6 Násilné ukonèení +$ SIGEMT +7 Emulovaná instrukce +$ SIGFPE +8 Výjimka pohyblivé èárky +$ SIGKILL +9 Zabito +$ SIGBUS +10 Chyba sbìrnice +$ SIGSEGV +11 Chyba segmentace +$ SIGSYS +12 ©patné systémove volání +$ SIGPIPE +13 Broken pipe +$ SIGALRM +14 Alarm èasovaè +$ SIGTERM +15 Ukonèeno +$ SIGURG +16 Naléhavý I/O po¾adavek +$ SIGSTOP +17 Pozastaveno (signál) +$ SIGTSTP +18 Pozastaveno +$ SIGCONT +19 Pokraèování +$ SIGCHLD +20 Potomek procesu ukonèen +$ SIGTTIN +21 Pozastaveno (terminálový vstup) +$ SIGTTOU +22 Pozastaveno (terminálový výstup) +$ SIGIO +23 Vstup/výstup mo¾ný +$ SIGXCPU +24 Pøekroèen èasový limit pro CPU +$ SIGXFSZ +25 Pøekroèen limit velikosti souboru +$ SIGVTALRM +26 Vypr¹el virtuální èasovaè +$ SIGPROF +27 Vypr¹el profilovací èasovaè +$ SIGWINCH +28 Velikost okna zmìnìna +$ SIGINFO +29 Informaèní po¾adavek +$ SIGUSR1 +30 U¾ivatelský signál 1 +$ SIGUSR2 +31 U¾ivatelský signál 2 +$ SIGPWR +32 Selhání/nastartování napájení diff --git a/lib/nbsd_libc/nls/de.msg b/lib/nbsd_libc/nls/de.msg new file mode 100644 index 000000000..32d4101d2 --- /dev/null +++ b/lib/nbsd_libc/nls/de.msg @@ -0,0 +1,258 @@ +$set 1 +$ EPERM +1 Operation nicht erlaubt +$ ENOENT +2 Datei oder Verzeichnis nicht gefunden +$ ESRCH +3 Prozess nicht gefunden +$ EINTR +4 Interrupt innerhalb eines Systemaufrufs +$ EIO +5 Ein/Ausgabefehler +$ ENXIO +6 Gerät ist nicht konfiguriert +$ E2BIG +7 Argumentliste ist zu lang +$ ENOEXEC +8 Die Datei hat kein bekanntes ausführbares Format +$ EBADF +9 Ungültiger Dateideskriptor +$ ECHILD +10 Kein Kindprozess +$ EDEADLK +11 Ein Deadlock wurde vermieden +$ ENOMEM +12 Kann nicht genug Speicher belegen +$ EACCES +13 Zugriff verboten +$ EFAULT +14 Ungültige Adresse +$ ENOTBLK +15 Es wird ein Blockgerät benötigt +$ EBUSY +16 Das Gerät ist belegt +$ EEXIST +17 Datei existiert bereits +$ EXDEV +18 Link zwischen verschiedenen Geräten +$ ENODEV +19 Die Operation wird von diesem Gerät nicht unterstützt +$ ENOTDIR +20 Kein Verzeichnis +$ EISDIR +21 Ist ein Verzeichnis +$ EINVAL +22 Ungültiges Argument +$ ENFILE +23 Zu viele offene Dateien im gesamten System +$ EMFILE +24 Zu viele offene Dateien +$ ENOTTY +25 Ungültiger Ioctl für dieses Gerät +$ ETXTBSY +26 Ausführbare Datei wird benutzt +$ EFBIG +27 Datei zu groß +$ ENOSPC +28 Kein Platz mehr auf dem Gerät +$ ESPIPE +29 Ungültige Positionierung +$ EROFS +30 Dateisystem ist schreibgeschützt +$ EMLINK +31 Zu viele Links +$ EPIPE +32 Unterbrochene Pipe +$ EDOM +33 Numerisches Argument ausserhalb des Wertebereichs +$ ERANGE +34 Ergebnis zu groß oder zu klein +$ EAGAIN, EWOULDBLOCK +35 Ressource vorübergehend nicht verfügbar +$ EINPROGRESS +36 Operation wird jetzt fortgesetzt +$ EALREADY +37 Operation wird bereits ausgeführt +$ ENOTSOCK +38 Deskriptor ist kein Socket +$ EDESTADDRREQ +39 Zieladresse benötigt +$ EMSGSIZE +40 Nachricht zu lang +$ EPROTOTYPE +41 Ungültiger Protokolltyp für diesen Socket +$ ENOPROTOOPT +42 Protokoll nicht verfügbar +$ EPROTONOSUPPORT +43 Protokoll nicht unterstützt +$ ESOCKTNOSUPPORT +44 Sockettyp nicht unterstützt +$ EOPNOTSUPP +45 Operation nicht unterstützt +$ EPFNOSUPPORT +46 Protokollfamilie nicht unterstützt +$ EAFNOSUPPORT +47 Addressart wird von der Protokollfamilie nicht unterstützt +$ EADDRINUSE +48 Adresse wird bereits benutzt +$ EADDRNOTAVAIL +49 Kann angeforderte Adresse nicht belegen +$ ENETDOWN +50 Netzwerk nicht verfügbar +$ ENETUNREACH +51 Netzwerk nicht erreichbar +$ ENETRESET +52 Netzwerk hat Verbindung mit Reset abgebrochen +$ ECONNABORTED +53 Software verursachte einen Verbindungsabbruch +$ ECONNRESET +54 Verbindung wurde von der Gegenstelle geschlossen +$ ENOBUFS +55 Keine Buffer verfügbar +$ EISCONN +56 Socket ist schon verbunden +$ ENOTCONN +57 Socket ist nicht verbunden +$ ESHUTDOWN +58 Kann nach einem Socket-Shutdown nicht mehr senden +$ ETOOMANYREFS +59 Zu viele Referenzen, kann nicht verbinden +$ ETIMEDOUT +60 Verbindungsabbruch durch Zeitüberschreitung +$ ECONNREFUSED +61 Verbindung wurde abgelehnt +$ ELOOP +62 Zu viele symbolische Links (zirkulär?) +$ ENAMETOOLONG +63 Dateiname zu lang +$ EHOSTDOWN +64 Host nicht verfügbar +$ EHOSTUNREACH +65 Keine Route zum Host +$ ENOTEMPTY +66 Verzeichnis ist nicht leer +$ EPROCLIM +67 Zu viele Prozesse +$ EUSERS +68 Zu viele Benutzer +$ EDQUOT +69 Plattenplatzlimit erschöpft +$ ESTALE +70 Verwaister NFS-Dateideskriptor +$ EREMOTE +71 Zu viele Fernverweise in diesem Zugriff +$ EBADRPC +72 RPC-Struktur ist ungültig +$ ERPCMISMATCH +73 RPC-Version stimmt nicht +$ EPROGUNAVAIL +74 RPC-Programm nicht verfügbar +$ EPROGMISMATCH +75 Falsche Programmversion +$ EPROCUNAVAIL +76 Falsche Prozedur für dieses Programm +$ ENOLCK +77 Keine Dateisperren verfügbar +$ ENOSYS +78 Funktion nicht implementiert +$ EFTYPE +79 Ungültiger Dateityp oder Dateiformat +$ EAUTH +80 Authentikationsfehler +$ ENEEDAUTH +81 Authentikator benötigt +$ EIDRM +82 Identifizierung entfernt +$ ENOMSG +83 Keine Nachricht vom gewünschten Typ +$ EOVERFLOW +84 Wert zu groß, um in Datentyp zu speichern +$ EILSEQ +85 Illegale Byte-Sequenz +$ ENOTSUP +86 Operation nicht unterstützt +$ ECANCELED +87 Operation abgebrochen +$ EBADMSG +88 Ungültige Nachricht +$ ENODATA +89 Keine Nachricht verfügbar +$ ENOSR +90 Keine STREAM-Ressourcen verfügbar +$ ENOSTR +91 Kein STREAM +$ ETIME +92 Zeitüberschreitung bei STREAM Ioctl +$ ENOATTR +93 Attribut nicht gefunden +$ EMULTIHOP +94 Multihopversuch +$ ENOLINK +95 Verbindung wurde getrennt +$ EPROTO +96 Protokollfehler +$set 2 +$ SIGHUP +1 Verbindungsende +$ SIGINT +2 Unterbrechung +$ SIGQUIT +3 Programmende +$ SIGILL +4 Ungültiger Maschinenbefehl +$ SIGTRAP +5 Trace/BPT trap +$ SIGABRT +6 Abort trap +$ SIGEMT +7 EMT trap +$ SIGFPE +8 Fließkommafehler +$ SIGKILL +9 Unbedingter Programmabbruch +$ SIGBUS +10 Bus-Zugriffsfehler +$ SIGSEGV +11 Illegaler Speicherzugriff +$ SIGSYS +12 Ungültiger Systemaufruf +$ SIGPIPE +13 Unterbrochene Pipe +$ SIGALRM +14 Wecker +$ SIGTERM +15 Beendet +$ SIGURG +16 Dringende Ein/Ausgabeanforderung +$ SIGSTOP +17 Gestoppt (Signal) +$ SIGTSTP +18 Gestoppt +$ SIGCONT +19 Fortgesetzt +$ SIGCHLD +20 Kindprozess beendet +$ SIGTTIN +21 Gestoppt (Eingabe) +$ SIGTTOU +22 Gestoppt (Ausgabe) +$ SIGIO +23 Ein/Ausgabe ist möglich +$ SIGXCPU +24 CPU-Zeitlimit erschöpft +$ SIGXFSZ +25 Dateigröße hat das Limit erreicht +$ SIGVTALRM +26 Virtueller Wecker abgelaufen +$ SIGPROF +27 Profil-Wecker abgelaufen +$ SIGWINCH +28 Fenstergröße hat sich geändert +$ SIGINFO +29 Informationsanforderung +$ SIGUSR1 +30 Benutzerdefiniertes Signal 1 +$ SIGUSR2 +31 Benutzerdefiniertes Signal 2 +$ SIGPWR +32 Statusänderung der Energieversorgung diff --git a/lib/nbsd_libc/nls/es.msg b/lib/nbsd_libc/nls/es.msg new file mode 100644 index 000000000..7428ed6be --- /dev/null +++ b/lib/nbsd_libc/nls/es.msg @@ -0,0 +1,258 @@ +$set 1 +$ EPERM +1 Operación no permitida +$ ENOENT +2 Archivo o directorio inexistente +$ ESRCH +3 Proceso inexistente +$ EINTR +4 Llamada del sistema interrumpida +$ EIO +5 Error de E/S +$ ENXIO +6 Dispositivo no configurado +$ E2BIG +7 La lista de argumentos es demasiado larga +$ ENOEXEC +8 Error en el formato del ejecutable +$ EBADF +9 Descriptor de archivo incorrecto +$ ECHILD +10 No hay procesos hijo +$ EDEADLK +11 Se ha evitado bloqueo del recurso +$ ENOMEM +12 No se pudo asignar la memoria pedida +$ EACCES +13 Permiso denegado +$ EFAULT +14 Dirección incorrecta +$ ENOTBLK +15 Se necesita un dispositivo de bloques +$ EBUSY +16 Dispositivo ocupado +$ EEXIST +17 El archivo ya existe +$ EXDEV +18 Enlace entre dispositivos +$ ENODEV +19 Operación inadecuada para este dispositivo +$ ENOTDIR +20 No es un directorio +$ EISDIR +21 Es un directorio +$ EINVAL +22 Argumento inadecuado +$ ENFILE +23 Hay demasiados archivos abiertos en el sistema +$ EMFILE +24 Hay demasiados archivos abiertos +$ ENOTTY +25 IOCTL inapropiado para el dispositivo +$ ETXTBSY +26 Archivo de texto ocupado +$ EFBIG +27 Archivo demasiado grande +$ ENOSPC +28 No queda espacio libre en el dispositivo +$ ESPIPE +29 Búsqueda ilegal +$ EROFS +30 Archivo del sistema de sólo lectura +$ EMLINK +31 Demasiados enlaces +$ EPIPE +32 Canal (pipe) roto +$ EDOM +33 El argumento numérico está fuera de rango +$ ERANGE +34 El resultado es demasiado grande +$ EAGAIN, EWOULDBLOCK +35 El recurso no está disponible temporalmente +$ EINPROGRESS +36 Operación actualmente en proceso +$ EALREADY +37 La operación ya estaba realizándose +$ ENOTSOCK +38 Operación de socket inaceptable por el dispositivo +$ EDESTADDRREQ +39 Se necesita una dirección de destino +$ EMSGSIZE +40 Mensaje demasiado largo +$ EPROTOTYPE +41 Tipo erróneo de protocolo para el socket +$ ENOPROTOOPT +42 Protocolo no disponible +$ EPROTONOSUPPORT +43 Protocolo no contemplado +$ ESOCKTNOSUPPORT +44 Tipo de socket no contemplado +$ EOPNOTSUPP +45 Operación no contemplada +$ EPFNOSUPPORT +46 Familia de protocolos no contemplada +$ EAFNOSUPPORT +47 Familia de direcciones no contemplada por la familia de protocolos +$ EADDRINUSE +48 La dirección ya está en uso +$ EADDRNOTAVAIL +49 No se pudo asignar la dirección pedida +$ ENETDOWN +50 La red no funciona +$ ENETUNREACH +51 No se puede acceder a la red +$ ENETRESET +52 La conexión a la red se interrumpió al reinicializar +$ ECONNABORTED +53 La conexión se ha abortado debido a problemas en el software +$ ECONNRESET +54 El interlocutor ha reinicializado la comunicación +$ ENOBUFS +55 No queda espacio en el buffer +$ EISCONN +56 El socket ya estaba conectado +$ ENOTCONN +57 El socket no está conectado +$ ESHUTDOWN +58 No se puede enviar tras la desconexión del socket +$ ETOOMANYREFS +59 Demasiadas referencias: no se pueden unir +$ ETIMEDOUT +60 El tiempo de conexión ha expirado +$ ECONNREFUSED +61 Conexión rehusada +$ ELOOP +62 Demasiados niveles de enlaces simbólicos +$ ENAMETOOLONG +63 Nombre de archivo demasiado largo +$ EHOSTDOWN +64 La máquina está fuera de servicio +$ EHOSTUNREACH +65 No hay ruta hasta la máquina +$ ENOTEMPTY +66 Directorio no vacío +$ EPROCLIM +67 Demasiados procesos +$ EUSERS +68 Demasiados usuarios +$ EDQUOT +69 Cuota de disco sobrepasada +$ ESTALE +70 Descriptor de archivo NFS inválido +$ EREMOTE +71 Ruta con demasiados niveles +$ EBADRPC +72 La estructura de la RPC es errónea +$ ERPCMISMATCH +73 La versión de la RPC es errónea +$ EPROGUNAVAIL +74 La RPC no está accesible +$ EPROGMISMATCH +75 Versión errónea del programa +$ EPROCUNAVAIL +76 Procedimiento erróneo para el programa +$ ENOLCK +77 No hay bloqueos disponibles +$ ENOSYS +78 Función no realizada +$ EFTYPE +79 Tipo de archivo o formato inapropiado +$ EAUTH +80 Error de la autentificación +$ ENEEDAUTH +81 Se necesita un autenticador +$ EIDRM +82 Identificador quitado +$ ENOMSG +83 No hay mensajes del tipo deseado +$ EOVERFLOW +84 Valor demasiado grande para ser almacenado en el tipo de datos +$ EILSEQ +85 Secuencia de bytes no legal +$ ENOTSUP +86 No soportado +$ ECANCELED +87 Operación cancelada +$ EBADMSG +88 Mensaje incorrecto o corrupto +$ ENODATA +89 No hay mensajes disponibles +$ ENOSR +90 No hay recursos de tipo STREAM +$ ENOSTR +91 No es un STREAM +$ ETIME +92 Tiempo de espera agotado en el ioctl STREAM +$ ENOATTR +93 Atributo no encontrado +$ EMULTIHOP +94 Se ha intentado un multisalto +$ ENOLINK +95 El enlace se ha servido +$ EPROTO +96 Error de protocolo +$set 2 +$ SIGHUP +1 Fín de línea (hangup) +$ SIGINT +2 Interrumpido +$ SIGQUIT +3 Terminado +$ SIGILL +4 Instrucción ilegal +$ SIGTRAP +5 Depuración +$ SIGABRT +6 Llamada de aborto +$ SIGEMT +7 Captura de EMT +$ SIGFPE +8 Excepción de coma flotante +$ SIGKILL +9 Matado +$ SIGBUS +10 Error en el bus +$ SIGSEGV +11 Fallo de segmentación +$ SIGSYS +12 Llamada al sistema errónea +$ SIGPIPE +13 Canal (pipe) roto +$ SIGALRM +14 Alarma del reloj +$ SIGTERM +15 Terminado +$ SIGURG +16 Condición urgente de E/S +$ SIGSTOP +17 Detenido (señal) +$ SIGTSTP +18 Detenido +$ SIGCONT +19 Continuando +$ SIGCHLD +20 Proceso hijo finalizado +$ SIGTTIN +21 Detenido (entrada tty) +$ SIGTTOU +22 Detenido (salida tty) +$ SIGIO +23 E/S posible +$ SIGXCPU +24 Se ha sobrepasado el tiempo límite de la CPU +$ SIGXFSZ +25 Se ha sobrepasado el límite de longitud de archivo +$ SIGVTALRM +26 Temporizador virtual expirado +$ SIGPROF +27 Temporizador de perfilación expirado +$ SIGWINCH +28 Cambios en el tamaño de la ventana +$ SIGINFO +29 Petición de información +$ SIGUSR1 +30 Señal definida por el usuario nº 1 +$ SIGUSR2 +31 Señal definida por el usuario nº 2 +$ SIGPWR +32 Fallo o reinicio de la alimentación diff --git a/lib/nbsd_libc/nls/fi.msg b/lib/nbsd_libc/nls/fi.msg new file mode 100644 index 000000000..d9475498a --- /dev/null +++ b/lib/nbsd_libc/nls/fi.msg @@ -0,0 +1,224 @@ +$set 1 +$ EPERM +1 Toimintoa ei sallita +$ ENOENT +2 Tiedostoa tai hakemistoa ei löydy +$ ESRCH +3 Prosessia ei löydy +$ EINTR +4 Systeemikutsu keskeytyi +$ EIO +5 Syöttö/tulostusvirhe +$ ENXIO +6 Laitetta ei määritelty +$ E2BIG +7 Liikaa argumentteja +$ ENOEXEC +8 Tuntematon ohjelmatyyppi +$ EBADF +9 Virheellinen tiedosto-osoitin +$ ECHILD +10 Ei lapsiprosesseja +$ EDEADLK +11 Resurssin ristiinlukitus vältetty +$ ENOMEM +12 Muistinvaraus epäonnistui +$ EACCES +13 Lupa kielletty +$ EFAULT +14 Virheellinen osoite +$ ENOTBLK +15 Tarvitaan lohko-osoitettava laite +$ EBUSY +16 Laite käytössä +$ EEXIST +17 Tiedosto on jo olemassa +$ EXDEV +18 Laitteiden välinen linkki +$ ENODEV +19 Laite ei tue toimintoa +$ ENOTDIR +20 Kohde ei ole hakemisto +$ EISDIR +21 Kohde on hakemisto +$ EINVAL +22 Virheellinen argumentti +$ ENFILE +23 Järjestelmässä on liian monta avointa tiedostoa +$ EMFILE +24 Liian monta avointa tiedostoa +$ ENOTTY +25 Virheellinen ohjaustoiminto laitteelle +$ ETXTBSY +26 Tiedosto on käytössä +$ EFBIG +27 Tiedosto liian suuri +$ ENOSPC +28 Laitteella ei ole tilaa +$ ESPIPE +29 Virheellinen haku +$ EROFS +30 Vain luettava tiedostojärjestelmä +$ EMLINK +31 Liian monta linkkiä +$ EPIPE +32 Katkennut putki +$ EDOM +33 Numeerinen syöte virheellinen +$ ERANGE +34 Tulos liian suuri +$ EAGAIN, EWOULDBLOCK +35 Resurssi ei ole tilapäisesti saatavilla +$ EINPROGRESS +36 Toiminta on käynnissä +$ EALREADY +37 Toiminta oli jo käynnissä +$ ENOTSOCK +38 Socket-operaatio muulla kuin socketilla +$ EDESTADDRREQ +39 Tarvitaan kohdeosoite +$ EMSGSIZE +40 Sanoma liian pitkä +$ EPROTOTYPE +41 Väärä protokolla socketille +$ ENOPROTOOPT +42 Protokolla ei ole käytettävissä +$ EPROTONOSUPPORT +43 Protokollaa ei tueta +$ ESOCKTNOSUPPORT +44 Socket-tyyppiä ei tueta +$ EOPNOTSUPP +45 Toimintoa ei tueta +$ EPFNOSUPPORT +46 Protokollaperhettä ei tueta +$ EAFNOSUPPORT +47 Protokollaperhe ei tue osoiteperhettä +$ EADDRINUSE +48 Osoite on jo käytössä +$ EADDRNOTAVAIL +49 Ei pysty antamaan pyydettyä osoitetta +$ ENETDOWN +50 Verkko ei ole käytettävissä +$ ENETUNREACH +51 Verkkoon ei ole yhteyttä +$ ENETRESET +52 Verkko sulki yhteyden +$ ECONNABORTED +53 Ohjelmiston aiheuttama yhteyden keskeytyminen +$ ECONNRESET +54 Isäntä nollasi yhteyden +$ ENOBUFS +55 Puskuritila on lopussa +$ EISCONN +56 Yhteys on jo olemassa +$ ENOTCONN +57 Yhteyttä ei ole olemassa +$ ESHUTDOWN +58 Lähettäminen ei ole mahdollista yhteyden katkaisun jälkeen +$ ETOOMANYREFS +59 Liikaa viittauksia: ei voi yhdistää +$ ETIMEDOUT +60 Yhteyden aikavalvontakatkaisu +$ ECONNREFUSED +61 Yhteys hylätty +$ ELOOP +62 Liian monta peräkkäistä symbolista linkkiä +$ ENAMETOOLONG +63 Tiedoston nimi on liian pitkä +$ EHOSTDOWN +64 Isäntä ei vastaa +$ EHOSTUNREACH +65 Ei reittiä isäntään +$ ENOTEMPTY +66 Hakemisto ei ole tyhjä +$ EPROCLIM +67 Liian monta prosessia +$ EUSERS +68 Liian monta käyttäjää +$ EDQUOT +69 Levytilarajoitus ylittyi +$ ESTALE +70 Vanhentunut NFS-yhteys +$ EREMOTE +71 Liian monta verkkolevyä polussa +$ EBADRPC +72 Virheellinen RPC-pyyntö +$ ERPCMISMATCH +73 Väärä RPC-versio +$ EPROGUNAVAIL +74 RPC ei käytettävissä +$ EPROGMISMATCH +75 Väärä ohjelmaversio +$ EPROCUNAVAIL +76 Väärä RPC-pyyntö ohjelmalle +$ ENOLCK +77 Lukitus ei käytettävissä +$ ENOSYS +78 Toimintoa ei ole +$ EFTYPE +79 Väärä tiedostotyyppi tai -formaatti +$set 2 +$ SIGHUP +1 Katkaisu +$ SIGINT +2 Keskeytys +$ SIGQUIT +3 Lopetus +$ SIGILL +4 Laiton käsky +$ SIGTRAP +5 Jäljitys/BPT ansa +$ SIGABRT +6 Poistumisansa +$ SIGEMT +7 EMT-ansa +$ SIGFPE +8 Liukulukuvirhe +$ SIGKILL +9 Tapettu +$ SIGBUS +10 Väylävirhe +$ SIGSEGV +11 Suojausvirhe +$ SIGSYS +12 Virheellinen systeemikutsu +$ SIGPIPE +13 Katkennut putki +$ SIGALRM +14 Hälytyskello +$ SIGTERM +15 Lopetettu +$ SIGURG +16 Kiireellinen syöttö/tulostus +$ SIGSTOP +17 Pysäytetty (signaali) +$ SIGTSTP +18 Pysäytetty +$ SIGCONT +19 Jatkettu +$ SIGCHLD +20 Lapsiprosessi päättynyt +$ SIGTTIN +21 Pysäytetty (tty-syöte) +$ SIGTTOU +22 Pysäytetty (tty-tuloste) +$ SIGIO +23 Syöttö ja tulostus mahdollisia +$ SIGXCPU +24 Keskusyksikköaikarajoitus ylitetty +$ SIGXFSZ +25 Tiedoston kokorajoitus ylitetty +$ SIGVTALRM +26 Virtuaali-ajastin laukesi +$ SIGPROF +27 Profilointiajastin laukesi +$ SIGWINCH +28 Ikkunan koko muuttuu +$ SIGINFO +29 Informaatiopyyntö +$ SIGUSR1 +30 Käyttäjän määriteltävä signaali 1 +$ SIGUSR2 +31 Käyttäjän määriteltävä signaali 2 +$ SIGPWR +32 Virransaannin tilassa muutos diff --git a/lib/nbsd_libc/nls/fr.msg b/lib/nbsd_libc/nls/fr.msg new file mode 100644 index 000000000..099670e53 --- /dev/null +++ b/lib/nbsd_libc/nls/fr.msg @@ -0,0 +1,252 @@ +$set 1 +$ EPERM +1 Opération non autorisée +$ ENOENT +2 Fichier ou répertoire introuvable +$ ESRCH +3 Processus introuvable +$ EINTR +4 Appel système interrompu +$ EIO +5 Erreur d'entrée/sortie +$ ENXIO +6 Périphérique non configuré +$ E2BIG +7 Liste de paramètres trop longue +$ ENOEXEC +8 Erreur de format d'exécutable +$ EBADF +9 Descripteur de fichier invalide +$ ECHILD +10 Pas de processus fils +$ EDEADLK +11 Etreinte fatale évitée +$ ENOMEM +12 Plus de mémoire +$ EACCES +13 Autorisation refusée +$ EFAULT +14 Adresse invalide +$ ENOTBLK +15 Nécessite un périphérique en mode bloc +$ EBUSY +16 Périphérique occupé +$ EEXIST +17 Fichier existant +$ EXDEV +18 Lien hors du périphérique +$ ENODEV +19 Opération non supportée par le périphérique +$ ENOTDIR +20 N'est pas un répertoire +$ EISDIR +21 C'est un réperoire +$ EINVAL +22 Paramètre invalide +$ ENFILE +23 Trop de fichiers ouverts dans le système +$ EMFILE +24 Trop de fichiers ouverts +$ ENOTTY +25 Ioctl inconnu du périphérique +$ ETXTBSY +26 Fichier exécutable utilisé +$ EFBIG +27 Fichier trop grand +$ ENOSPC +28 Plus de place sur le périphérique +$ ESPIPE +29 Positionnement illégal +$ EROFS +30 Système de fichier protégé en écriture +$ EMLINK +31 Trop de liens +$ EPIPE +32 Tube cassé +$ EDOM +33 Paramètre numérique hors du domaine +$ ERANGE +34 Résultat trop grand +$ EAGAIN, EWOULDBLOCK +35 Ressource temporairement indisponible +$ EINPROGRESS +36 Opération actuellement en cours +$ EALREADY +37 Opération déjà en cours +$ ENOTSOCK +38 Opération réservée aux sockets +$ EDESTADDRREQ +39 Adresse de destination nécéssaire +$ EMSGSIZE +40 Message trop long +$ EPROTOTYPE +41 Protocole inadapté au socket +$ ENOPROTOOPT +42 Protocole non disponible +$ EPROTONOSUPPORT +43 Protocole non supporté +$ ESOCKTNOSUPPORT +44 Type de socket non supporté +$ EOPNOTSUPP +45 Opération non supportée +$ EPFNOSUPPORT +46 Famille de protocoles non supportée +$ EAFNOSUPPORT +47 Famille d'adresses non supporté par cette famille de protocoles +$ EADDRINUSE +48 Adresse déjà utilisée +$ EADDRNOTAVAIL +49 Impossible d'affecter l'adresse demandée +$ ENETDOWN +50 Réseau arrêté +$ ENETUNREACH +51 Réseau non atteignable +$ ENETRESET +52 Connexion perdue après un RAZ du réseau +$ ECONNABORTED +53 Fin de connexion causée par logiciel +$ ECONNRESET +54 Connexion terminée par le correspondant +$ ENOBUFS +55 Plus de place pour la mémoire tampon +$ EISCONN +56 Socket déjà connecté +$ ENOTCONN +57 Socket non connecté +$ ESHUTDOWN +58 Impossible de transmettre après fermeture du socket +$ ETOOMANYREFS +59 Trop de références: impossible à réassembler +$ ETIMEDOUT +60 Délai de connexion expiré +$ ECONNREFUSED +61 Connexion refusée +$ ELOOP +62 Trop de niveaux de liens symboliques +$ ENAMETOOLONG +63 Nom de fichier trop long +$ EHOSTDOWN +64 Machine arrêtée +$ EHOSTUNREACH +65 Pas de route vers cette machine +$ ENOTEMPTY +66 Répertoire non vide +$ EPROCLIM +67 Trop de processus +$ EUSERS +68 Trop d'utilisateurs +$ EDQUOT +69 Quota disque épuisé +$ ESTALE +70 Identificateur NFS périmé +$ EREMOTE +71 Trop de points de montages dans le chemin +$ EBADRPC +72 Structure RPC invalide +$ ERPCMISMATCH +73 Mauvaise version RPC +$ EPROGUNAVAIL +74 Programme RPC non disponible +$ EPROGMISMATCH +75 Mauvaise version de programme +$ EPROCUNAVAIL +76 Procédure inexistante +$ ENOLCK +77 Pas de verrous disponibles +$ ENOSYS +78 Fonction non implémentée +$ EFTYPE +79 Type ou format de fichier inadapté +$ EAUTH +80 Erreur lors de l'authentification +$ ENEEDAUTH +81 Authentication nécessaire +$ EIDRM +82 Identifiant supprimé +$ ENOMSG +83 Pas de message du type demandé +$ EOVERFLOW +84 Valeur trop grande pour stocker dans le type de données +$ EILSEQ +85 Séquence d'octets illégale +$ ENOTSUP +86 Non supporté +$ ECANCELED +87 Opération annulée +$ EBADMSG +88 Message invalide ou corrompu +$ ENODATA +89 Pas de message disponible +$ ENOSR +90 Pas de ressource de type STREAM disponible +$ ENOSTR +91 N'est pas un STREAM +$ ETIME +92 Délai expiré pour un ioctl STREAM +$ ENOATTR +93 Attribut non trouvé +$set 2 +$ SIGHUP +1 Raccroché +$ SIGINT +2 Interruption +$ SIGQUIT +3 Quitte +$ SIGILL +4 Instruction illégale +$ SIGTRAP +5 Point de trace/arrêt +$ SIGABRT +6 Avorté +$ SIGEMT +7 Appel émulateur +$ SIGFPE +8 Exception numérique +$ SIGKILL +9 Tué +$ SIGBUS +10 Erreur bus +$ SIGSEGV +11 Erreur de segmentation +$ SIGSYS +12 Appel système invalide +$ SIGPIPE +13 Tube cassé +$ SIGALRM +14 Alarme +$ SIGTERM +15 Terminé +$ SIGURG +16 Condition d'E/S urgente +$ SIGSTOP +17 Arrêté par un signal +$ SIGTSTP +18 Arrêté +$ SIGCONT +19 Reprise +$ SIGCHLD +20 Fin d'un fils +$ SIGTTIN +21 Arrêté (lecture sur tty) +$ SIGTTOU +22 Arrêté (écriture sur tty) +$ SIGIO +23 E/S possible +$ SIGXCPU +24 Limite du temps CPU atteinte +$ SIGXFSZ +25 Limite de taille de fichier atteinte +$ SIGVTALRM +26 Timer virtuel expiré +$ SIGPROF +27 Timer de profiling expiré +$ SIGWINCH +28 Changement de taille de la fenêtre +$ SIGINFO +29 Demande d'informations +$ SIGUSR1 +30 Signal utilisateur 1 +$ SIGUSR2 +31 Signal utilisateur 2 +$ SIGPWR +32 Coupure/reprise d'alimentation diff --git a/lib/nbsd_libc/nls/nl.msg b/lib/nbsd_libc/nls/nl.msg new file mode 100644 index 000000000..dc63a290f --- /dev/null +++ b/lib/nbsd_libc/nls/nl.msg @@ -0,0 +1,258 @@ +$set 1 +$ EPERM +1 Bewerking niet toegestaan +$ ENOENT +2 Bestand of directory niet gevonden +$ ESRCH +3 Taak bestaat niet +$ EINTR +4 Onderbroken systeemaanroep +$ EIO +5 Invoer/uitvoer fout +$ ENXIO +6 Apparaat niet geconfigureerd +$ E2BIG +7 Argumenten lijst is te lang +$ ENOEXEC +8 Programma kan niet worden uitgevoerd +$ EBADF +9 Ongeldige bestandsverwijzing +$ ECHILD +10 Geen kind processen +$ EDEADLK +11 Een "deadlock" is vermeden +$ ENOMEM +12 Kan geen geheugen meer verkrijgen +$ EACCES +13 Toegang geweigerd +$ EFAULT +14 Ongeldig adres +$ ENOTBLK +15 Een per blok adresseerbaar apparaat is vereist +$ EBUSY +16 Apparaat is bezig +$ EEXIST +17 Bestand bestaat reeds +$ EXDEV +18 Verwijzing tussen bestanden op verschillende bestandssystemen +$ ENODEV +19 Bewerking wordt niet ondersteund door dit apparaat +$ ENOTDIR +20 Dit is geen directory +$ EISDIR +21 Dit is een directory +$ EINVAL +22 Ongeldig argument +$ ENFILE +23 Te veel open bestanden in het systeem +$ EMFILE +24 Te veel open bestanden +$ ENOTTY +25 ioctl niet van toepassing op dit apparaat +$ ETXTBSY +26 Programma bestand is bezig +$ EFBIG +27 Bestand is te groot +$ ENOSPC +28 Geen ruimte meer op dit apparaat +$ ESPIPE +29 Onuitvoerbare zoekopdracht +$ EROFS +30 Van dit bestandssysteem kan alleen worden gelezen +$ EMLINK +31 Te veel bestandsverwijzingen +$ EPIPE +32 Verbroken pijp +$ EDOM +33 Numeriek argument valt buiten domein +$ ERANGE +34 Resultaat te groot of te klein +$ EAGAIN, EWOULDBLOCK +35 Middel tijdelijk onbeschikbaar +$ EINPROGRESS +36 Bewerking in gang gezet +$ EALREADY +37 Bewerking is al in gang gezet +$ ENOTSOCK +38 Voor deze bewerking is een contactpunt vereist +$ EDESTADDRREQ +39 Een bestemmingsadres is vereist +$ EMSGSIZE +40 Te grote boodschap +$ EPROTOTYPE +41 Protocol past niet bij dit contactpunt +$ ENOPROTOOPT +42 Protocol is niet beschikbaar +$ EPROTONOSUPPORT +43 Protocol is niet ondersteund +$ ESOCKTNOSUPPORT +44 Dit soort contactpunt is niet ondersteund +$ EOPNOTSUPP +45 Bewerking niet ondersteund +$ EPFNOSUPPORT +46 Protocol familie niet ondersteund +$ EAFNOSUPPORT +47 Adressen familie niet ondersteund door protocol familie +$ EADDRINUSE +48 Adres is al in gebruik +$ EADDRNOTAVAIL +49 Het gevraagde adres kan niet worden toegekend +$ ENETDOWN +50 Netwerk is plat +$ ENETUNREACH +51 Netwerk is onbereikbaar +$ ENETRESET +52 Netwerk onderbrak verbinding tijdens herstart +$ ECONNABORTED +53 Verbroken verbinding veroorzaakt door software +$ ECONNRESET +54 Verbinding werd aan de andere kant verbroken +$ ENOBUFS +55 Geen buffer ruimte meer beschikbaar +$ EISCONN +56 Dit contactpunt is al verbonden +$ ENOTCONN +57 Contactpunt is niet verbonden +$ ESHUTDOWN +58 Een afgesloten contactpunt kan geen gegevens meer verzenden +$ ETOOMANYREFS +59 Te veel verwijzingen: splitsen niet mogelijk +$ ETIMEDOUT +60 Verbinding te lang niet mogelijk +$ ECONNREFUSED +61 Verbinding geweigerd +$ ELOOP +62 Te veel niveaus van symbolische verwijzingen +$ ENAMETOOLONG +63 Bestandsnaam te lang +$ EHOSTDOWN +64 Bestemming niet actief +$ EHOSTUNREACH +65 Bestemming niet bereikbaar +$ ENOTEMPTY +66 Directory is niet leeg +$ EPROCLIM +67 Te veel taken +$ EUSERS +68 Te veel gebruikers +$ EDQUOT +69 Schijf quota overschreden +$ ESTALE +70 Verlopen NFS bestandsverwijzing +$ EREMOTE +71 Te veel verwijzingen op afstand in dit pad +$ EBADRPC +72 RPC argument structuur is incorrect +$ ERPCMISMATCH +73 RPC versie is verkeerd +$ EPROGUNAVAIL +74 RPC programma niet beschikbaar +$ EPROGMISMATCH +75 Programma versie is verkeerd +$ EPROCUNAVAIL +76 Taak kan door dit programma niet worden uitgevoerd +$ ENOLCK +77 Geen locks beschikbaar +$ ENOSYS +78 Deze systeem functie is niet geimplementeerd +$ EFTYPE +79 Bestandsformaat niet van toepassing +$ EAUTH +80 Aanmeldingsfout +$ ENEEDAUTH +81 Aanmeldingsprocedure benodigd +$ EIDRM +82 De aanwijzer is verwijderd +$ ENOMSG +83 Geen boodschap van het gewenste type +$ EOVERFLOW +84 Waarde te groot om te bewaren in data type +$ EILSEQ +85 Ongeldige byte reeks +$ ENOTSUP +86 Niet ondersteund +$ ECANCELED +87 Bewerking geannuleerd +$ EBADMSG +88 Verkeerde of defecte boodschap +$ ENODATA +89 Geen boodschap beschikbaar +$ ENOSR +90 Geen STREAM voorraad +$ ENOSTR +91 Dit is geen STREAM +$ ETIME +92 STREAM ioctl verlopen +$ ENOATTR +93 Attribuut niet gevonden +$ EMULTIHOP +94 Multihopverzoek +$ ENOLINK +95 Verbinding werd verstoord +$ EPROTO +96 Protocolfout +$set 2 +$ SIGHUP +1 Opgehangen +$ SIGINT +2 Onderbroken +$ SIGQUIT +3 Opgegeven +$ SIGILL +4 Verboden instructie +$ SIGTRAP +5 Spoor/BPT val +$ SIGABRT +6 Abort val +$ SIGEMT +7 EMT val +$ SIGFPE +8 Drijvende komma fout +$ SIGKILL +9 Gedood +$ SIGBUS +10 Bus fout +$ SIGSEGV +11 Segmentatie fout +$ SIGSYS +12 Verkeerde systeemaanroep +$ SIGPIPE +13 Gebroken pijp +$ SIGALRM +14 Wekker +$ SIGTERM +15 Beeindigd +$ SIGURG +16 Dringende I/O opgemerkt +$ SIGSTOP +17 Gestopt (signaal) +$ SIGTSTP +18 Gestopt +$ SIGCONT +19 Voortgezet +$ SIGCHLD +20 Kindproces beeindigd +$ SIGTTIN +21 Gestopt (tty invoer) +$ SIGTTOU +22 Gestopt (tty uitvoer) +$ SIGIO +23 I/O mogelijk +$ SIGXCPU +24 Te veel CPU tijd verbruikt +$ SIGXFSZ +25 Maximum bestandsgrootte overschreden +$ SIGVTALRM +26 Virtuele wekker +$ SIGPROF +27 Profiling wekker +$ SIGWINCH +28 Venstergrootte veranderd +$ SIGINFO +29 Informatie verzoek +$ SIGUSR1 +30 Gebruikersignaal 1 +$ SIGUSR2 +31 Gebruikersignaal 2 +$ SIGPWR +32 Stroom uitval/inschakeling diff --git a/lib/nbsd_libc/nls/no.msg b/lib/nbsd_libc/nls/no.msg new file mode 100644 index 000000000..762f45c50 --- /dev/null +++ b/lib/nbsd_libc/nls/no.msg @@ -0,0 +1,222 @@ +$set 1 +$ EPERM +1 Operasjonen er ikke tillatt +$ ENOENT +2 Filen eller katalogen finnes ikke +$ ESRCH +3 Prosessen finnes ikke +$ EINTR +4 Avbrudt systemkall +$ EIO +5 I/O feil +$ ENXIO +6 Enheten er ikke konfigurert +$ E2BIG +7 Argumentlisten er for lang +$ ENOEXEC +8 Ukjent kjørbart format +$ EBADF +9 Ugyldig fildeskriptor +$ ECHILD +10 Ingen barneprosess +$ EDEADLK +11 VranglÃ¥s unngÃ¥tt +$ ENOMEM +12 Kan ikke allokere nok minne +$ EACCES +13 Ingen adgang +$ EFAULT +14 Ugyldig adresse +$ ENOTBLK +15 Blokk-enhet pÃ¥krevd +$ EBUSY +16 Enheten er opptatt +$ EEXIST +17 Filen finnes +$ EXDEV +18 Link mellom forskjellige enheter +$ ENODEV +19 Operasjonen er ikke støttet av enheten +$ ENOTDIR +20 Ikke en katalog +$ EISDIR +21 Er en katalog +$ EINVAL +22 Ugyldig argument +$ ENFILE +23 For mange Ã¥pne filer i systemet +$ EMFILE +24 For mange Ã¥pne filer +$ ENOTTY +25 Ugyldig ioctl for enheten +$ ETXTBSY +26 Kjørbar fil i bruk +$ EFBIG +27 Filen er for stor +$ ENOSPC +28 Ingen ledig plass pÃ¥ enheten +$ ESPIPE +29 Ugyldig seek operasjon +$ EROFS +30 Filsystemet er skrivebeskyttet +$ EMLINK +31 For mange linker +$ EPIPE +32 Brudt pipe +$ EDOM +33 Numerisk argument utenfor arbeidsomrÃ¥det +$ ERANGE +34 Resultatet er for stort +$ EAGAIN, EWOULDBLOCK +35 Ressurs midlertidig utilgjengelig +$ EINPROGRESS +36 Operasjonen er nÃ¥ i gang +$ EALREADY +37 Operasjonen er allerede i gang +$ ENOTSOCK +38 Deskriptoren er ikke en socket +$ EDESTADDRREQ +39 Mottakeradresse er pÃ¥krevd +$ EMSGSIZE +40 Meldingen er for lang +$ EPROTOTYPE +41 Ugyldig protokolltype for denne socketen +$ ENOPROTOOPT +42 Protokollen er ikke tilgjengelig +$ EPROTONOSUPPORT +43 Protokollen er ikke støttet +$ ESOCKTNOSUPPORT +44 Socket-typen er ikke støttet +$ EOPNOTSUPP +45 Operasjonen er ikke støttet +$ EPFNOSUPPORT +46 Protokollfamilien er ikke støttet +$ EAFNOSUPPORT +47 Adressetypen er ikke støttet av protokollfamilien +$ EADDRINUSE +48 Adressen er allerede i bruk +$ EADDRNOTAVAIL +49 Kan ikke bruke den ønskede adressen +$ ENETDOWN +50 Nettverket er nede +$ ENETUNREACH +51 Nettverket er utilgjengelig +$ ENETRESET +52 Nettverket kuttet forbindelsen ved reset +$ ECONNABORTED +53 Programvaren forÃ¥rsaket brudd av forbindelsen +$ ECONNRESET +54 Forbindelsen avbrudt av korrespondenten +$ ENOBUFS +55 Buffer-plass ikke tilgjengelig +$ EISCONN +56 Socketen er allerede forbundet +$ ENOTCONN +57 Socketen er ikke forbundet +$ ESHUTDOWN +58 Kan ikke sende etter at socketen er tatt ned +$ ETOOMANYREFS +59 For mange referanser: kan ikke slÃ¥ dem sammen +$ ETIMEDOUT +60 Tiden til forbindelsen utløp +$ ECONNREFUSED +61 Forbindelse nektet +$ ELOOP +62 For mange nivÃ¥er med symbolske linker +$ ENAMETOOLONG +63 Filnavnet er for langt +$ EHOSTDOWN +64 Maskinen er nede +$ EHOSTUNREACH +65 Ingen rute til maskinen +$ ENOTEMPTY +66 Katalogen er ikke tom +$ EPROCLIM +67 For mange prosesser +$ EUSERS +68 For mange brukere +$ EDQUOT +69 Diskkvote overskredet +$ ESTALE +70 FastlÃ¥st NFS fildeskriptor +$ EREMOTE +71 For mange nivÃ¥er med remote i stien +$ EBADRPC +72 Ugyldig RPC struktur +$ ERPCMISMATCH +73 Feil RPC versjon +$ EPROGUNAVAIL +74 RPC program ikke tilgjengelig +$ EPROGMISMATCH +75 Feil programversjon +$ EPROCUNAVAIL +76 Prosedyren finnes ikke i programmet +$ ENOLCK +77 Ingen lÃ¥sing tilgjengelig +$ ENOSYS +78 Funksjonen er ikke implementert +$ EFTYPE +79 Ugyldig filtype eller format +$set 2 +$ SIGHUP +1 Hangup +$ SIGINT +2 Avbrudd +$ SIGQUIT +3 Avslutt +$ SIGILL +4 Ugyldig instruksjon +$ SIGTRAP +5 Trace/BPT trap +$ SIGABRT +6 Abort trap +$ SIGEMT +7 EMT trap +$ SIGFPE +8 Flyttallsfeil +$ SIGKILL +9 Drept +$ SIGBUS +10 Buss feil +$ SIGSEGV +11 Segmenteringsfeil +$ SIGSYS +12 Ugyldig systemkall +$ SIGPIPE +13 Brudt pipe +$ SIGALRM +14 Alarmklokke +$ SIGTERM +15 Terminert +$ SIGURG +16 Urgent I/O condition +$ SIGSTOP +17 Stoppet (signal) +$ SIGTSTP +18 Stoppet +$ SIGCONT +19 Fortsetter +$ SIGCHLD +20 Barn avsluttet +$ SIGTTIN +21 Stoppet (tty input) +$ SIGTTOU +22 Stoppet (tty output) +$ SIGIO +23 I/O mulig +$ SIGXCPU +24 CPU-tid overskredet +$ SIGXFSZ +25 Maksimal filstørrelse overskredet +$ SIGVTALRM +26 Virtuell timer utløpt +$ SIGPROF +27 Profileringstimer utløpt +$ SIGWINCH +28 Vindustørrelse endres +$ SIGINFO +29 Informasjonsforespørsel +$ SIGUSR1 +30 Brukerdefinert signal 1 +$ SIGUSR2 +31 Brukerdefinert signal 2 diff --git a/lib/nbsd_libc/nls/pl.msg b/lib/nbsd_libc/nls/pl.msg new file mode 100644 index 000000000..4c5d2533d --- /dev/null +++ b/lib/nbsd_libc/nls/pl.msg @@ -0,0 +1,236 @@ +$set 1 +$ EPERM +1 Operacja nie dozwolona +$ ENOENT +2 Nie ma takiego pliku ani katalogu +$ ESRCH +3 Nie ma takiego procesu +$ EINTR +4 Przerwane wywo³anie systemowe +$ EIO +5 B³±d wej¶cia/wyj¶cia +$ ENXIO +6 Urz±dzenie nie skonfigurowane +$ E2BIG +7 Lista argumentów jest za d³uga +$ ENOEXEC +8 B³êdny format pliku wykonywalnego +$ EBADF +9 Z³y deskryptor pliku +$ ECHILD +10 Brak procesów potomnych +$ EDEADLK +11 Unikniêto zakleszczenia zasobów +$ ENOMEM +12 Brak pamiêci do przydzia³u +$ EACCES +13 Brak dostêpu +$ EFAULT +14 Z³y adres +$ ENOTBLK +15 Wymagane urz±dzenie blokowe +$ EBUSY +16 Urz±dzenie zajête +$ EEXIST +17 Plik istnieje +$ EXDEV +18 Powi±zanie miedzy urz±dzeniami +$ ENODEV +19 Operacja nie obs³ugiwana przez urz±dzenie +$ ENOTDIR +20 To nie jest katalog +$ EISDIR +21 To jest katalog +$ EINVAL +22 B³êdny argument +$ ENFILE +23 Za du¿o otwrtych plików w systemie +$ EMFILE +24 Za du¿o otwartych plików +$ ENOTTY +25 Niew³a¶ciwy dostêp do urz±dzenia +$ ETXTBSY +26 Plik tekstowy zajêty +$ EFBIG +27 Plik zbyt du¿y +$ ENOSPC +28 Nie ma miejsca na urz±dzeniu +$ ESPIPE +29 B³êdne przesuniêcie +$ EROFS +30 System plików tylko do odczytu +$ EMLINK +31 Za du¿o linków +$ EPIPE +32 Przerwany potok +$ EDOM +33 Argument liczbowy spoza zakresu +$ ERANGE +34 Za du¿y wynik +$ EAGAIN, EWOULDBLOCK +35 Zasoby chwilowo niedostêpne +$ EINPROGRESS +36 Operacja jest w³a¶nie wykonywana +$ EALREADY +37 Operacja jest ju¿ wykonywana +$ ENOTSOCK +38 Operacja na obiekcie, który nie jest gniazdem +$ EDESTADDRREQ +39 Wymagany adres przeznaczenia +$ EMSGSIZE +40 Wiadomo¶æ za d³uga +$ EPROTOTYPE +41 Ty protoko³u nie pasuje do gniazda +$ ENOPROTOOPT +42 Protocó³ nie jest dostêpny +$ EPROTONOSUPPORT +43 Protocó³ nie jest obs³ugiwany +$ ESOCKTNOSUPPORT +44 Nie obs³ugiwany typ gniazda +$ EOPNOTSUPP +45 Operacja nie obs³ugiwana +$ EPFNOSUPPORT +46 Nie obs³ugiwana rodzina protoko³ów +$ EAFNOSUPPORT +47 Rodzina adresów nie obs³ugiwana przez rodzinê protoko³ów +$ EADDRINUSE +48 Adres jest aktualnie w u¿yciu +$ EADDRNOTAVAIL +49 Nie mo¿na przypisaæ ¿±danego adresu +$ ENETDOWN +50 Sieæ nie dzia³a +$ ENETUNREACH +51 Sieæ jest niedostêpna +$ ENETRESET +52 Sieæ przerwa³a po³±czenie po resecie +$ ECONNABORTED +53 Oprogramowanie spowodowa³o przerwanie po³±czenia +$ ECONNRESET +54 Po³±czenie zerwane przez drug± stronê +$ ENOBUFS +55 Brak miejsca w buforze +$ EISCONN +56 Gniazdo jest ju¿ po³±czone +$ ENOTCONN +57 Gniazdo nie jest po³±czone +$ ESHUTDOWN +58 Nie mo¿na wysy³aæ po zamkniêciu gniazda +$ ETOOMANYREFS +59 Za du¿o powi±zañ: dowi±zanie nie mo¿liwe +$ ETIMEDOUT +60 Przekroczono czas oczekiwania na po³±czenie +$ ECONNREFUSED +61 Po³±czenie odrzucone +$ ELOOP +62 Za du¿o wzajemnych symlinków +$ ENAMETOOLONG +63 Zbyt d³uga nazwa pliku +$ EHOSTDOWN +64 Host jest wy³±czony +$ EHOSTUNREACH +65 Brak drogi do hosta +$ ENOTEMPTY +66 Katalog nie jest pusty +$ EPROCLIM +67 Za du¿o procesów +$ EUSERS +68 Zbyt wielu u¿ytkowników +$ EDQUOT +69 Przekroczono limit miejsca na dysku +$ ESTALE +70 Uchwyt pliku NFS jest nieaktualny +$ EREMOTE +71 Za du¿o poziomów zagnie¿d¿enia w ¶cie¿ce +$ EBADRPC +72 RPC b³êdna struktura +$ ERPCMISMATCH +73 RPC z³a wersja +$ EPROGUNAVAIL +74 RPC program niedostêpny +$ EPROGMISMATCH +75 Z³a wersja programu +$ EPROCUNAVAIL +76 Z³a procedura dla programu +$ ENOLCK +77 Blokady nie s± dostêpne +$ ENOSYS +78 Niezaimplementowana funkcja +$ EFTYPE +79 Niew³a¶ciwy typ lub format pliku +$ EAUTH +80 B³±d uwierzytelnienia +$ ENEEDAUTH +81 Wymagane uwierzytelnienie +$ EIDRM +82 Identyfikator zosta³ usuniêty +$ ENOMSG +83 Brak komunikatu po¿adanego typu +$ EOVERFLOW +84 Warto¶æ za du¿a aby zapamiêtaæ j± w zdefiniowanym typie danych +$ EILSEQ +85 B³êdna sekwencja bajtów +$set 2 +$ SIGHUP +1 Roz³±czenie +$ SIGINT +2 Przerwanie +$ SIGQUIT +3 Wyj¶cie +$ SIGILL +4 Nieznana instrukcja +$ SIGTRAP +5 Pu³apka debuggera/BPT +$ SIGABRT +6 Przerwanana pu³apka +$ SIGEMT +7 Pu³apka EMT +$ SIGFPE +8 B³±d w obliczeniach zmiennoprzecinkowych +$ SIGKILL +9 Unicestwiony +$ SIGBUS +10 B³±d szyny +$ SIGSEGV +11 Naruszenie ochrony pamiêci +$ SIGSYS +12 B³êdne wywo³anie systemowe +$ SIGPIPE +13 Przerwany potok +$ SIGALRM +14 Budzik +$ SIGTERM +15 Zakoñczony +$ SIGURG +16 Nag³y wypadek I/O +$ SIGSTOP +17 Zatrzymany (sygna³) +$ SIGTSTP +18 Zatrzymany +$ SIGCONT +19 Kontynuacja +$ SIGCHLD +20 Proces potomny zakoñczy³ pracê +$ SIGTTIN +21 Zatrzymany (wej¶cie z tty) +$ SIGTTOU +22 Zatrzymany (wyj¶cie z tty) +$ SIGIO +23 Wej/Wyj dozwolone +$ SIGXCPU +24 Przekroczony limit czasu procesora +$ SIGXFSZ +25 przekroczony limit wielko¶ci pliku +$ SIGVTALRM +26 Wirtualny stoper wyczerpany +$ SIGPROF +27 Koniec stopera profiluj±cego +$ SIGWINCH +28 Zmiana rozmiaru okna +$ SIGINFO +29 ¯±danie infromacji +$ SIGUSR1 +30 Sygna³ u¿ytkownika 1 +$ SIGUSR2 +31 Sygna³ u¿ytkownika 2 +$ SIGPWR +32 Awaria zasilania/restart diff --git a/lib/nbsd_libc/nls/sk.msg b/lib/nbsd_libc/nls/sk.msg new file mode 100644 index 000000000..d784d375c --- /dev/null +++ b/lib/nbsd_libc/nls/sk.msg @@ -0,0 +1,258 @@ +$set 1 +$ EPERM +1 Operácia nie je povolená +$ ENOENT +2 Neexistujúci súbor alebo adresár +$ ESRCH +3 Proces neexistuje +$ EINTR +4 Systémové volanie preru¹ené +$ EIO +5 Chyba vstupu/výstupu +$ ENXIO +6 Zariadenie nie je nakonfigurované +$ E2BIG +7 Príli¹ dlhý zoznam argumentov +$ ENOEXEC +8 Chybný formát spusteného súboru +$ EBADF +9 Chybný deskriptor súboru +$ ECHILD +10 Neexistuje ¾iaden potomok procesu +$ EDEADLK +11 Bolo zabránené zablokovaniu prostriedku +$ ENOMEM +12 Nie je mo¾né prideli» pamä» +$ EACCES +13 Prístup odmietnutý +$ EFAULT +14 Chybná adresa +$ ENOTBLK +15 Vy¾adované blokové zariadenie +$ EBUSY +16 Zariadenie je pou¾ívané +$ EEXIST +17 Súbor existuje +$ EXDEV +18 Odkaz medzi zariadeniami +$ ENODEV +19 Operácia nie je zariadením podporovaná +$ ENOTDIR +20 Nie je adresár +$ EISDIR +21 Je adresár +$ EINVAL +22 Chybný argument +$ ENFILE +23 Priveµa otvorených súborov v systéme +$ EMFILE +24 Priveµa otvorených súborov +$ ENOTTY +25 Nevhodné ioctl pre dané zariadenie +$ ETXTBSY +26 Textový súbor je pou¾ívaný +$ EFBIG +27 Súbor je príli¹ veµký +$ ENOSPC +28 Na zariadení nie je voµné miesto +$ ESPIPE +29 Neprípustné nastavenie pozície +$ EROFS +30 Súborový systém je len na èítanie +$ EMLINK +31 Priveµa odkazov +$ EPIPE +32 Preru¹ená rúra +$ EDOM +33 Èíselný argument mimo definièný obor +$ ERANGE +34 Výsledok príli¹ veµký alebo príli¹ malý +$ EAGAIN, EWOULDBLOCK +35 Zdroj je doèasne nedostupný +$ EINPROGRESS +36 Operácia práve prebieha +$ EALREADY +37 Operácia u¾ prebieha +$ ENOTSOCK +38 Socketová operácia na objekte, ktorý nie je socket +$ EDESTADDRREQ +39 Vy¾adovaná cieµová adresa +$ EMSGSIZE +40 Príli¹ dlhá správa +$ EPROTOTYPE +41 Protokol nie je socketom podporovaný +$ ENOPROTOOPT +42 Protokol nie je k dispozícii +$ EPROTONOSUPPORT +43 Protokol nie je podporovaný +$ ESOCKTNOSUPPORT +44 Typ socketu nie je podporovaný +$ EOPNOTSUPP +45 Operácia nie je podporovaná +$ EPFNOSUPPORT +46 Rodina protokolov nie je podporovaná +$ EAFNOSUPPORT +47 Rodina adries nie je podporovaná rodinou protokolov +$ EADDRINUSE +48 Adresa je u¾ pou¾ívaná +$ EADDRNOTAVAIL +49 Nie je mo¾né prideli» po¾adovanú adresu +$ ENETDOWN +50 Sie» je nefunkèná +$ ENETUNREACH +51 Sie» je nedostupná +$ ENETRESET +52 Sie» zru¹ila spojenie po resete +$ ECONNABORTED +53 Program spôsobil ukonèenie spojenia +$ ECONNRESET +54 Spojenie zru¹ené druhou stranou +$ ENOBUFS +55 Vyrovnávacia pamä» nie je k dispozícii +$ EISCONN +56 Socket je u¾ pripojený +$ ENOTCONN +57 Socket nie je pripojený +$ ESHUTDOWN +58 Nie je mo¾né posiela» po uzavretí socketu +$ ETOOMANYREFS +59 Príli¹ mnoho odkazov: nie je mo¾né spoji» +$ ETIMEDOUT +60 Èasový limit pre spojenie vypr¹al +$ ECONNREFUSED +61 Spojenie odmietnuté +$ ELOOP +62 Priveµa úrovní symbolických odkazov +$ ENAMETOOLONG +63 Meno súboru príli¹ dlhé +$ EHOSTDOWN +64 Vzdialený uzol je odpojený +$ EHOSTUNREACH +65 Neexistuje cesta k vzdialenému uzlu +$ ENOTEMPTY +66 Adresár nie je prázdny +$ EPROCLIM +67 Priveµa procesov +$ EUSERS +68 Priveµa pou¾ívateµov +$ EDQUOT +69 Disková kvóta prekroèená +$ ESTALE +70 Zastaralý NFS súborový ukazateµ +$ EREMOTE +71 Priveµa úrovní vzdialeného v ceste +$ EBADRPC +72 RPC ¹truktúra je chybná +$ ERPCMISMATCH +73 Chybná verzia RPC +$ EPROGUNAVAIL +74 RPC program nie je k dispozícii +$ EPROGMISMATCH +75 Chybná verzia RPC programu +$ EPROCUNAVAIL +76 Chybná RPC procedúra pre program +$ ENOLCK +77 Zámky nie sú k dispozícii +$ ENOSYS +78 Funkcia nie je implementovaná +$ EFTYPE +79 Nevhodný typ alebo formát súboru +$ EAUTH +80 Overenie práv neúspe¹né +$ ENEEDAUTH +81 Vy¾adovaný overovací objekt +$ EIDRM +82 Identifikátor odstránený +$ ENOMSG +83 Neexistuje správa ¾elaného typu +$ EOVERFLOW +84 Hodnota je pre daný dátový typ priveµká +$ EILSEQ +85 Neprípustná postupnos» bajtov +$ ENOTSUP +86 Nie je podporované +$ ECANCELED +87 Operácia zru¹ená +$ EBADMSG +88 Chybná alebo poru¹ená správa +$ ENODATA +89 ®iadna správa nie je k dispozícii +$ ENOSR +90 ®iadne STREAM zdroje +$ ENOSTR +91 Nie je STREAM +$ ETIME +92 Èasový limit pre STREAM ioctl vypr¹al +$ ENOATTR +93 Atribút nenájdený +$ EMULTIHOP +94 Pokus o spojenie cez viacero uzlov +$ ENOLINK +95 Odkaz bol pretrhnutý +$ EPROTO +96 Chyba protokolu +$set 2 +$ SIGHUP +1 Terminál odpojený +$ SIGINT +2 Preru¹enie +$ SIGQUIT +3 Koniec +$ SIGILL +4 Chybná in¹trukcia +$ SIGTRAP +5 Trasovacia/ladiaca in¹trukcia +$ SIGABRT +6 Násilné ukonèenie +$ SIGEMT +7 Emulovaná in¹trukcia +$ SIGFPE +8 Výnimka pohyblivej rádovej èiarky +$ SIGKILL +9 Zabité +$ SIGBUS +10 Chyba na zbernici +$ SIGSEGV +11 Chyba segmentácie +$ SIGSYS +12 Chybné systémové volanie +$ SIGPIPE +13 Preru¹ená rúra +$ SIGALRM +14 Budík +$ SIGTERM +15 Ukonèené +$ SIGURG +16 Naliehavý vstupný/výstupný stav +$ SIGSTOP +17 Pozastavené (signál) +$ SIGTSTP +18 Pozastavené +$ SIGCONT +19 Pokraèovanie +$ SIGCHLD +20 Potomok procesu ukonèený +$ SIGTTIN +21 Pozastavené (terminálový vstup) +$ SIGTTOU +22 Pozastavené (terminálový výstup) +$ SIGIO +23 Vstup/výstup mo¾ný +$ SIGXCPU +24 Prekroèený èasový limit pre procesor +$ SIGXFSZ +25 Prekroèený limit veµkosti súboru +$ SIGVTALRM +26 Vypr¹al virtuálny èasovaè +$ SIGPROF +27 Vypr¹al profilovací èasovaè +$ SIGWINCH +28 Veµkos» okna zmenená +$ SIGINFO +29 ®iados» o informáciu +$ SIGUSR1 +30 Pou¾ívateµom definovaný signál 1 +$ SIGUSR2 +31 Pou¾ívateµom definovaný signál 2 +$ SIGPWR +32 Zlyhanie/opakované spustenie napájania diff --git a/lib/nbsd_libc/nls/sv.msg b/lib/nbsd_libc/nls/sv.msg new file mode 100644 index 000000000..a309983a8 --- /dev/null +++ b/lib/nbsd_libc/nls/sv.msg @@ -0,0 +1,224 @@ +$set 1 +$ EPERM +1 OtillÃ¥ten operation +$ ENOENT +2 Filen eller katalogen finns ej +$ ESRCH +3 Denna process finns ej +$ EINTR +4 Avbrutet systemanrop +$ EIO +5 In-/utmatningsfel +$ ENXIO +6 Enheten är ej konfigurerad +$ E2BIG +7 Argumentlistan är för lÃ¥ng +$ ENOEXEC +8 Ej körbar fil +$ EBADF +9 Felaktigt filhandtag +$ ECHILD +10 Inga barnprocesser +$ EDEADLK +11 Undvek resursdödläge +$ ENOMEM +12 Kan ej erhÃ¥lla minne +$ EACCES +13 TillstÃ¥nd nekas +$ EFAULT +14 Felaktig adress +$ ENOTBLK +15 Blockenhet krävs +$ EBUSY +16 Enheten är upptagen +$ EEXIST +17 Filen finns redan +$ EXDEV +18 Länken korsar enheter +$ ENODEV +19 Enheten stöder ej operationen +$ ENOTDIR +20 Är ej en katalog +$ EISDIR +21 Är en katalog +$ EINVAL +22 Ogiltigt argument +$ ENFILE +23 För mÃ¥nga öppna filer i systemet +$ EMFILE +24 För mÃ¥nga öppna filer +$ ENOTTY +25 Olämplig ioctl för enheten +$ ETXTBSY +26 Programfilen är upptagen +$ EFBIG +27 Filen är för stor +$ ENOSPC +28 Inget utrymme kvar pÃ¥ enheten +$ ESPIPE +29 OtillÃ¥ten sökning +$ EROFS +30 Skrivskyddat filsystem +$ EMLINK +31 För mÃ¥nga länkar +$ EPIPE +32 Avbruten kommunikationskanal +$ EDOM +33 Numeriskt argument utanför domänen +$ ERANGE +34 Resultatet är för stort +$ EAGAIN, EWOULDBLOCK +35 Resursen är tillfälligt otillgänglig +$ EINPROGRESS +36 Operationen är igÃ¥ng +$ EALREADY +37 Operationen är redan igÃ¥ng +$ ENOTSOCK +38 Sockeloperation pÃ¥ icke-sockel +$ EDESTADDRREQ +39 Destinationsadress erfordras +$ EMSGSIZE +40 För lÃ¥ngt meddelande +$ EPROTOTYPE +41 Fel protokolltyp för sockeln +$ ENOPROTOOPT +42 Protokollet otillgängligt +$ EPROTONOSUPPORT +43 Protokollet är ej understött +$ ESOCKTNOSUPPORT +44 Sockeltypen är ej understödd +$ EOPNOTSUPP +45 Operationen är ej understödd +$ EPFNOSUPPORT +46 Protokollfamiljen är ej understödd +$ EAFNOSUPPORT +47 Adressfamiljen är ej understödd av protokollfamiljen +$ EADDRINUSE +48 Adressen är upptagen +$ EADDRNOTAVAIL +49 Kan ej tilldela den begärda adressen +$ ENETDOWN +50 Nätverket fungerar inte +$ ENETUNREACH +51 Nätverket är ej kontaktbart +$ ENETRESET +52 Nätverket tappade kontakten vid Ã¥terställningen +$ ECONNABORTED +53 Mjukvara orsakade nedkoppling +$ ECONNRESET +54 Motparten avbröt uppkopplingen +$ ENOBUFS +55 Inget buffertutrymme tillgängligt +$ EISCONN +56 Sockeln är redan uppkopplad +$ ENOTCONN +57 Sockeln är ej uppkopplad +$ ESHUTDOWN +58 Kan ej sända efter att sockeln nedkopplats +$ ETOOMANYREFS +59 För mÃ¥nga referenser: kan inte delas +$ ETIMEDOUT +60 Uppkopplingstiden tog slut +$ ECONNREFUSED +61 Uppkopplingen nekad +$ ELOOP +62 För mÃ¥nga nivÃ¥er av symboliska länkar +$ ENAMETOOLONG +63 Alldeles för lÃ¥ngt filnamn +$ EHOSTDOWN +64 Värddatorn är nere +$ EHOSTUNREACH +65 Väg till värddatorn saknas +$ ENOTEMPTY +66 Katalogen ej tom +$ EPROCLIM +67 För mÃ¥nga processer +$ EUSERS +68 För mÃ¥nga användare +$ EDQUOT +69 Diskkvot överskriden +$ ESTALE +70 Inaktuellt NFS-filhandtag +$ EREMOTE +71 För mÃ¥nga fjärrnivÃ¥er i sökvägen +$ EBADRPC +72 Felaktig RPC-struktur +$ ERPCMISMATCH +73 Felaktig RPC-version +$ EPROGUNAVAIL +74 RPC-programmet otillgängligt +$ EPROGMISMATCH +75 Fel programversion +$ EPROCUNAVAIL +76 Felaktig procedur för programmet +$ ENOLCK +77 Inga lÃ¥s tillgängliga +$ ENOSYS +78 Funktionen är ej implementerad +$ EFTYPE +79 Olämplig filtyp eller format +$set 2 +$ SIGHUP +1 Lägg pÃ¥ +$ SIGINT +2 Avbryt +$ SIGQUIT +3 Avsluta +$ SIGILL +4 Olaglig instruktion +$ SIGTRAP +5 SpÃ¥r- eller brytpunktsfälla +$ SIGABRT +6 Avslutsfälla +$ SIGEMT +7 Emuleringsfälla +$ SIGFPE +8 Flyttalsavbrott +$ SIGKILL +9 Dräpt +$ SIGBUS +10 Bussfel +$ SIGSEGV +11 Segmentfel +$ SIGSYS +12 Felaktigt systemanrop +$ SIGPIPE +13 Avbruten kommunikationskanal +$ SIGALRM +14 Äggklocka +$ SIGTERM +15 Terminerad +$ SIGURG +16 BrÃ¥dskande In/Ut-tillstÃ¥nd +$ SIGSTOP +17 Stoppad (signal) +$ SIGTSTP +18 Stoppad +$ SIGCONT +19 Fortsätter +$ SIGCHLD +20 Barn avslutat +$ SIGTTIN +21 Stoppad (terminalinmatning) +$ SIGTTOU +22 Stoppad (terminalutmatning) +$ SIGIO +23 In- och utmatning möjlig +$ SIGXCPU +24 Cputidsgränsen överskriden +$ SIGXFSZ +25 Filstorleksgränsen överskriden +$ SIGVTALRM +26 Virtuella äggklockan ringde +$ SIGPROF +27 Profileringsäggklockan ringde +$ SIGWINCH +28 Fönsterstorleken ändras +$ SIGINFO +29 InformationsförfrÃ¥gan +$ SIGUSR1 +30 Användardefinierad signal 1 +$ SIGUSR2 +31 Användardefinierad signal 2 +$ SIGPWR +32 Kraftbortfall/omstart diff --git a/lib/nbsd_libc/quad/Makefile.inc b/lib/nbsd_libc/quad/Makefile.inc new file mode 100644 index 000000000..62e53c154 --- /dev/null +++ b/lib/nbsd_libc/quad/Makefile.inc @@ -0,0 +1,23 @@ +# $NetBSD: Makefile.inc,v 1.11 2009/12/06 05:34:42 uebayasi Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +# Quad support +SRCS.quad= cmpdi2.c divdi3.c fixdfdi.c fixsfdi.c fixunsdfdi.c \ + fixunssfdi.c floatdidf.c floatdisf.c floatunsdidf.c \ + moddi3.c muldi3.c negdi2.c qdivrem.c \ + ucmpdi2.c udivdi3.c umoddi3.c + +.if (${MACHINE_ARCH} == "m68k") +SRCS.quad+= ashldi3.S ashrdi3.S lshrdi3.S +.elif (${MACHINE_ARCH} == "m68000") +SRCS.quad+= ashldi3.S lshrdi3.S +.else +SRCS.quad+= ashldi3.c ashrdi3.c lshrdi3.c +.endif + +# XXX as far as I can tell, these are never used and can be removed +SRCS.quad+= adddi3.c anddi3.c iordi3.c notdi2.c subdi3.c xordi3.c + +SRCS+= ${SRCS.quad} + +.PATH: ${ARCHDIR}/quad ${.CURDIR}/quad diff --git a/lib/nbsd_libc/quad/TESTS/Makefile b/lib/nbsd_libc/quad/TESTS/Makefile new file mode 100644 index 000000000..decf428a2 --- /dev/null +++ b/lib/nbsd_libc/quad/TESTS/Makefile @@ -0,0 +1,12 @@ +# $NetBSD: Makefile,v 1.2 1995/02/27 17:31:26 cgd Exp $ +# @(#)Makefile 8.1 (Berkeley) 6/4/93 + +all: mul divrem + +MUL= mul.c ../muldi3.c +mul: ${MUL} + gcc -g -DSPARC_XXX ${MUL} -o $@ + +DIVREM= divrem.c ../qdivrem.c +divrem: ${DIVREM} + gcc -g -DSPARC_XXX ${DIVREM} -o $@ diff --git a/lib/nbsd_libc/quad/TESTS/divrem.c b/lib/nbsd_libc/quad/TESTS/divrem.c new file mode 100644 index 000000000..7c2139d85 --- /dev/null +++ b/lib/nbsd_libc/quad/TESTS/divrem.c @@ -0,0 +1,80 @@ +/* $NetBSD: divrem.c,v 1.4 2003/08/07 16:43:18 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1992, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)divrem.c 8.1 (Berkeley) 6/4/93"; +#else +static char rcsid[] = "$NetBSD: divrem.c,v 1.4 2003/08/07 16:43:18 agc Exp $"; +#endif +#endif /* not lint */ + +#include + +main() +{ + union { long long q; unsigned int v[2]; } a, b, q, r; + char buf[300]; + extern long long __qdivrem(unsigned long long, unsigned long long, + unsigned long long *); + + for (;;) { + printf("> "); + if (fgets(buf, sizeof buf, stdin) == NULL) + break; + if (sscanf(buf, "%u:%u %u:%u", + &a.v[0], &a.v[1], &b.v[0], &b.v[1]) != 4 && + sscanf(buf, "0x%x:%x 0x%x:%x", + &a.v[0], &a.v[1], &b.v[0], &b.v[1]) != 4) { + printf("eh?\n"); + continue; + } + q.q = __qdivrem(a.q, b.q, &r.q); + printf("%x:%x /%% %x:%x => q=%x:%x r=%x:%x\n", + a.v[0], a.v[1], b.v[0], b.v[1], + q.v[0], q.v[1], r.v[0], r.v[1]); + printf(" = %X%08X / %X%08X => %X%08X\n\ + = %X%08X %% %X%08X => %X%08X\n", + a.v[0], a.v[1], b.v[0], b.v[1], q.v[0], q.v[1], + a.v[0], a.v[1], b.v[0], b.v[1], r.v[0], r.v[1]); + } + exit(0); +} diff --git a/lib/nbsd_libc/quad/TESTS/mul.c b/lib/nbsd_libc/quad/TESTS/mul.c new file mode 100644 index 000000000..2826d7a7b --- /dev/null +++ b/lib/nbsd_libc/quad/TESTS/mul.c @@ -0,0 +1,76 @@ +/* $NetBSD: mul.c,v 1.4 2003/08/07 16:43:19 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1992, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)mul.c 8.1 (Berkeley) 6/4/93"; +#else +static char rcsid[] = "$NetBSD: mul.c,v 1.4 2003/08/07 16:43:19 agc Exp $"; +#endif +#endif /* not lint */ + +#include + +main() +{ + union { long long q; unsigned int v[2]; } a, b, m; + char buf[300]; + extern long long __muldi3(long long, long long); + + for (;;) { + printf("> "); + if (fgets(buf, sizeof buf, stdin) == NULL) + break; + if (sscanf(buf, "%u:%u %u:%u", + &a.v[0], &a.v[1], &b.v[0], &b.v[1]) != 4 && + sscanf(buf, "0x%x:%x 0x%x:%x", + &a.v[0], &a.v[1], &b.v[0], &b.v[1]) != 4) { + printf("eh?\n"); + continue; + } + m.q = __muldi3(a.q, b.q); + printf("%x:%x * %x:%x => %x:%x\n", + a.v[0], a.v[1], b.v[0], b.v[1], m.v[0], m.v[1]); + printf(" = %X%08X * %X%08X => %X%08X\n", + a.v[0], a.v[1], b.v[0], b.v[1], m.v[0], m.v[1]); + } + exit(0); +} diff --git a/lib/nbsd_libc/quad/fixdfdi.c b/lib/nbsd_libc/quad/fixdfdi.c new file mode 100644 index 000000000..dd0f17f38 --- /dev/null +++ b/lib/nbsd_libc/quad/fixdfdi.c @@ -0,0 +1,65 @@ +/* $NetBSD: fixdfdi.c,v 1.4 2003/08/07 16:43:16 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fixdfdi.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: fixdfdi.c,v 1.4 2003/08/07 16:43:16 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Convert double to (signed) quad. + * We clamp anything that is out of range. + */ +quad_t +__fixdfdi(x) + double x; +{ + if (x < 0) + if (x <= QUAD_MIN) + return (QUAD_MIN); + else + return ((quad_t)-(u_quad_t)-x); + else + if (x >= QUAD_MAX) + return (QUAD_MAX); + else + return ((quad_t)(u_quad_t)x); +} diff --git a/lib/nbsd_libc/quad/fixsfdi.c b/lib/nbsd_libc/quad/fixsfdi.c new file mode 100644 index 000000000..56cc03e37 --- /dev/null +++ b/lib/nbsd_libc/quad/fixsfdi.c @@ -0,0 +1,66 @@ +/* $NetBSD: fixsfdi.c,v 1.4 2003/08/07 16:43:16 agc Exp $ */ + +/*- + * Copyright (c) 1992 The Regents of the University of California. + * All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fixsfdi.c 5.1 (Berkeley) 7/7/92"; +#else +__RCSID("$NetBSD: fixsfdi.c,v 1.4 2003/08/07 16:43:16 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Convert float to (signed) quad. + * We clamp anything that is out of range. + * + * N.B.: must use new ANSI syntax (sorry). + */ +quad_t +__fixsfdi(float x) +{ + if (x < 0) + if (x <= QUAD_MIN) + return (QUAD_MIN); + else + return ((quad_t)-(u_quad_t)-x); + else + if (x >= QUAD_MAX) + return (QUAD_MAX); + else + return ((quad_t)(u_quad_t)x); +} diff --git a/lib/nbsd_libc/quad/fixunsdfdi.c b/lib/nbsd_libc/quad/fixunsdfdi.c new file mode 100644 index 000000000..7db43a1b5 --- /dev/null +++ b/lib/nbsd_libc/quad/fixunsdfdi.c @@ -0,0 +1,83 @@ +/* $NetBSD: fixunsdfdi.c,v 1.7 2003/08/07 16:43:16 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fixunsdfdi.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: fixunsdfdi.c,v 1.7 2003/08/07 16:43:16 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +#define ONE_FOURTH ((int)1 << (INT_BITS - 2)) +#define ONE_HALF (ONE_FOURTH * 2.0) +#define ONE (ONE_FOURTH * 4.0) + +/* + * Convert double to (unsigned) quad. + * Not sure what to do with negative numbers---for now, anything out + * of range becomes UQUAD_MAX. + */ +u_quad_t +__fixunsdfdi(x) + double x; +{ + union uu t; + unsigned int tmp; + + if (x < 0) + return (UQUAD_MAX); /* ??? should be 0? ERANGE??? */ +#ifdef notdef /* this falls afoul of a GCC bug */ + if (x >= UQUAD_MAX) + return (UQUAD_MAX); +#else /* so we wire in 2^64-1 instead */ + if (x >= 18446744073709551615.0) /* XXX */ + return (UQUAD_MAX); +#endif + /* + * Now we know that 0 <= x <= 18446744073709549568. The upper + * limit is one ulp less than 18446744073709551615 tested for above. + * Dividing this by 2^32 will *not* round irrespective of any + * rounding modes (except if the result is an IEEE denorm). + * Furthermore, the quotient will fit into a 32-bit integer. + */ + tmp = x / ONE; + t.ul[L] = (unsigned int) (x - tmp * ONE); + t.ul[H] = tmp; + return (t.uq); +} diff --git a/lib/nbsd_libc/quad/fixunssfdi.c b/lib/nbsd_libc/quad/fixunssfdi.c new file mode 100644 index 000000000..21cfc3077 --- /dev/null +++ b/lib/nbsd_libc/quad/fixunssfdi.c @@ -0,0 +1,103 @@ +/* $NetBSD: fixunssfdi.c,v 1.6 2003/08/07 16:43:16 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fixunssfdi.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: fixunssfdi.c,v 1.6 2003/08/07 16:43:16 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +#define ONE_FOURTH ((int)1 << (INT_BITS - 2)) +#define ONE_HALF (ONE_FOURTH * 2.0) +#define ONE (ONE_FOURTH * 4.0) + +/* + * Convert float to (unsigned) quad. We do most of our work in double, + * out of sheer paranoia. + * + * Not sure what to do with negative numbers---for now, anything out + * of range becomes UQUAD_MAX. + * + * N.B.: must use new ANSI syntax (sorry). + */ +u_quad_t +__fixunssfdi(float f) +{ + double x, toppart; + union uu t; + + if (f < 0) + return (UQUAD_MAX); /* ??? should be 0? ERANGE??? */ +#ifdef notdef /* this falls afoul of a GCC bug */ + if (f >= UQUAD_MAX) + return (UQUAD_MAX); +#else /* so we wire in 2^64-1 instead */ + if (f >= 18446744073709551615.0) /* XXX */ + return (UQUAD_MAX); +#endif + x = f; + /* + * Get the upper part of the result. Note that the divide + * may round up; we want to avoid this if possible, so we + * subtract `1/2' first. + */ + toppart = (x - ONE_HALF) / ONE; + /* + * Now build a u_quad_t out of the top part. The difference + * between x and this is the bottom part (this may introduce + * a few fuzzy bits, but what the heck). With any luck this + * difference will be nonnegative: x should wind up in the + * range [0..UINT_MAX]. For paranoia, we assume [INT_MIN.. + * 2*UINT_MAX] instead. + */ + t.ul[H] = (unsigned int)toppart; + t.ul[L] = 0; + x -= (double)t.uq; + if (x < 0) { + t.ul[H]--; + x += UINT_MAX; + } + if (x > UINT_MAX) { + t.ul[H]++; + x -= UINT_MAX; + } + t.ul[L] = (u_int)x; + return (t.uq); +} diff --git a/lib/nbsd_libc/quad/floatdidf.c b/lib/nbsd_libc/quad/floatdidf.c new file mode 100644 index 000000000..ff60cb920 --- /dev/null +++ b/lib/nbsd_libc/quad/floatdidf.c @@ -0,0 +1,77 @@ +/* $NetBSD: floatdidf.c,v 1.6 2003/08/07 16:43:16 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)floatdidf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: floatdidf.c,v 1.6 2003/08/07 16:43:16 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Convert (signed) quad to double. + */ +double +__floatdidf(x) + quad_t x; +{ + double d; + union uu u; + int neg; + + /* + * Get an unsigned number first, by negating if necessary. + */ + if (x < 0) + u.q = -x, neg = 1; + else + u.q = x, neg = 0; + + /* + * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L] + * has the units. Ideally we could just set d, add INT_BITS to + * its exponent, and then add the units, but this is portable + * code and does not know how to get at an exponent. Machine- + * specific code may be able to do this more efficiently. + */ + d = (double)u.ul[H] * (((int)1 << (INT_BITS - 2)) * 4.0); + d += u.ul[L]; + + return (neg ? -d : d); +} diff --git a/lib/nbsd_libc/quad/floatdisf.c b/lib/nbsd_libc/quad/floatdisf.c new file mode 100644 index 000000000..0802fb90c --- /dev/null +++ b/lib/nbsd_libc/quad/floatdisf.c @@ -0,0 +1,79 @@ +/* $NetBSD: floatdisf.c,v 1.6 2003/08/07 16:43:16 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)floatdisf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: floatdisf.c,v 1.6 2003/08/07 16:43:16 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Convert (signed) quad to float. + */ +float +__floatdisf(x) + quad_t x; +{ + float f; + union uu u; + int neg; + + /* + * Get an unsigned number first, by negating if necessary. + */ + if (x < 0) + u.q = -x, neg = 1; + else + u.q = x, neg = 0; + + /* + * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L] + * has the units. Ideally we could just set f, add INT_BITS to + * its exponent, and then add the units, but this is portable + * code and does not know how to get at an exponent. Machine- + * specific code may be able to do this more efficiently. + * + * Using double here may be excessive paranoia. + */ + f = (double)u.ul[H] * (((int)1 << (INT_BITS - 2)) * 4.0); + f += u.ul[L]; + + return (neg ? -f : f); +} diff --git a/lib/nbsd_libc/quad/floatunsdidf.c b/lib/nbsd_libc/quad/floatunsdidf.c new file mode 100644 index 000000000..17d8b6ca9 --- /dev/null +++ b/lib/nbsd_libc/quad/floatunsdidf.c @@ -0,0 +1,62 @@ +/* $NetBSD: floatunsdidf.c,v 1.6 2003/08/07 16:43:16 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)floatunsdidf.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: floatunsdidf.c,v 1.6 2003/08/07 16:43:16 agc Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "quad.h" + +/* + * Convert (unsigned) quad to double. + * This is exactly like floatdidf.c except that negatives never occur. + */ +double +__floatunsdidf(x) + u_quad_t x; +{ + double d; + union uu u; + + u.uq = x; + d = (double)u.ul[H] * (((int)1 << (INT_BITS - 2)) * 4.0); + d += u.ul[L]; + return (d); +} diff --git a/lib/nbsd_libc/regex/COPYRIGHT b/lib/nbsd_libc/regex/COPYRIGHT new file mode 100644 index 000000000..f7a8f20c3 --- /dev/null +++ b/lib/nbsd_libc/regex/COPYRIGHT @@ -0,0 +1,54 @@ +$NetBSD: COPYRIGHT,v 1.5 2003/08/07 16:43:19 agc Exp $ + +Copyright 1992, 1993, 1994 Henry Spencer. All rights reserved. +This software is not subject to any license of the American Telephone +and Telegraph Company or of the Regents of the University of California. + +Permission is granted to anyone to use this software for any purpose on +any computer system, and to alter it and redistribute it, subject +to the following restrictions: + +1. The author is not responsible for the consequences of use of this + software, no matter how awful, even if they arise from flaws in it. + +2. The origin of this software must not be misrepresented, either by + explicit claim or by omission. Since few users ever read sources, + credits must appear in the documentation. + +3. Altered versions must be plainly marked as such, and must not be + misrepresented as being the original software. Since few users + ever read sources, credits must appear in the documentation. + +4. This notice may not be removed or altered. + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +/*- + * Copyright (c) 1994 + * 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. + * + * @(#)COPYRIGHT 8.1 (Berkeley) 3/16/94 + */ diff --git a/lib/nbsd_libc/regex/Makefile.inc b/lib/nbsd_libc/regex/Makefile.inc new file mode 100644 index 000000000..4dd74cf84 --- /dev/null +++ b/lib/nbsd_libc/regex/Makefile.inc @@ -0,0 +1,14 @@ +# $NetBSD: Makefile.inc,v 1.7 1997/11/14 02:04:46 mrg Exp $ +# @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 + +# regex sources +.PATH: ${.CURDIR}/regex + +CPPFLAGS+=-DPOSIX_MISTAKE + +SRCS+= regcomp.c regerror.c regexec.c regfree.c + +MAN+= regex.3 re_format.7 + +MLINKS+=regex.3 regcomp.3 regex.3 regexec.3 regex.3 regerror.3 \ + regex.3 regfree.3 diff --git a/lib/nbsd_libc/regex/WHATSNEW b/lib/nbsd_libc/regex/WHATSNEW new file mode 100644 index 000000000..93eb93606 --- /dev/null +++ b/lib/nbsd_libc/regex/WHATSNEW @@ -0,0 +1,95 @@ +# $NetBSD: WHATSNEW,v 1.6 1995/02/27 13:28:25 cgd Exp $ +# @(#)WHATSNEW 8.3 (Berkeley) 3/18/94 + +New in alpha3.4: The complex bug alluded to below has been fixed (in a +slightly kludgey temporary way that may hurt efficiency a bit; this is +another "get it out the door for 4.4" release). The tests at the end of +the tests file have accordingly been uncommented. The primary sign of +the bug was that something like a?b matching ab matched b rather than ab. +(The bug was essentially specific to this exact situation, else it would +have shown up earlier.) + +New in alpha3.3: The definition of word boundaries has been altered +slightly, to more closely match the usual programming notion that "_" +is an alphabetic. Stuff used for pre-ANSI systems is now in a subdir, +and the makefile no longer alludes to it in mysterious ways. The +makefile has generally been cleaned up some. Fixes have been made +(again!) so that the regression test will run without -DREDEBUG, at +the cost of weaker checking. A workaround for a bug in some folks' + has been added. And some more things have been added to +tests, including a couple right at the end which are commented out +because the code currently flunks them (complex bug; fix coming). +Plus the usual minor cleanup. + +New in alpha3.2: Assorted bits of cleanup and portability improvement +(the development base is now a BSDI system using GCC instead of an ancient +Sun system, and the newer compiler exposed some glitches). Fix for a +serious bug that affected REs using many [] (including REG_ICASE REs +because of the way they are implemented), *sometimes*, depending on +memory-allocation patterns. The header-file prototypes no longer name +the parameters, avoiding possible name conflicts. The possibility that +some clot has defined CHAR_MIN as (say) `-128' instead of `(-128)' is +now handled gracefully. "uchar" is no longer used as an internal type +name (too many people have the same idea). Still the same old lousy +performance, alas. + +New in alpha3.1: Basically nothing, this release is just a bookkeeping +convenience. Stay tuned. + +New in alpha3.0: Performance is no better, alas, but some fixes have been +made and some functionality has been added. (This is basically the "get +it out the door in time for 4.4" release.) One bug fix: regfree() didn't +free the main internal structure (how embarrassing). It is now possible +to put NULs in either the RE or the target string, using (resp.) a new +REG_PEND flag and the old REG_STARTEND flag. The REG_NOSPEC flag to +regcomp() makes all characters ordinary, so you can match a literal +string easily (this will become more useful when performance improves!). +There are now primitives to match beginnings and ends of words, although +the syntax is disgusting and so is the implementation. The REG_ATOI +debugging interface has changed a bit. And there has been considerable +internal cleanup of various kinds. + +New in alpha2.3: Split change list out of README, and moved flags notes +into Makefile. Macro-ized the name of regex(7) in regex(3), since it has +to change for 4.4BSD. Cleanup work in engine.c, and some new regression +tests to catch tricky cases thereof. + +New in alpha2.2: Out-of-date manpages updated. Regerror() acquires two +small extensions -- REG_ITOA and REG_ATOI -- which avoid debugging kludges +in my own test program and might be useful to others for similar purposes. +The regression test will now compile (and run) without REDEBUG. The +BRE \$ bug is fixed. Most uses of "uchar" are gone; it's all chars now. +Char/uchar parameters are now written int/unsigned, to avoid possible +portability problems with unpromoted parameters. Some unsigned casts have +been introduced to minimize portability problems with shifting into sign +bits. + +New in alpha2.1: Lots of little stuff, cleanup and fixes. The one big +thing is that regex.h is now generated, using mkh, rather than being +supplied in the distribution; due to circularities in dependencies, +you have to build regex.h explicitly by "make h". The two known bugs +have been fixed (and the regression test now checks for them), as has a +problem with assertions not being suppressed in the absence of REDEBUG. +No performance work yet. + +New in alpha2: Backslash-anything is an ordinary character, not an +error (except, of course, for the handful of backslashed metacharacters +in BREs), which should reduce script breakage. The regression test +checks *where* null strings are supposed to match, and has generally +been tightened up somewhat. Small bug fixes in parameter passing (not +harmful, but technically errors) and some other areas. Debugging +invoked by defining REDEBUG rather than not defining NDEBUG. + +New in alpha+3: full prototyping for internal routines, using a little +helper program, mkh, which extracts prototypes given in stylized comments. +More minor cleanup. Buglet fix: it's CHAR_BIT, not CHAR_BITS. Simple +pre-screening of input when a literal string is known to be part of the +RE; this does wonders for performance. + +New in alpha+2: minor bits of cleanup. Notably, the number "32" for the +word width isn't hardwired into regexec.c any more, the public header +file prototypes the functions if __STDC__ is defined, and some small typos +in the manpages have been fixed. + +New in alpha+1: improvements to the manual pages, and an important +extension, the REG_STARTEND option to regexec(). diff --git a/lib/nbsd_libc/regex/cclass.h b/lib/nbsd_libc/regex/cclass.h new file mode 100644 index 000000000..3ab2ccba4 --- /dev/null +++ b/lib/nbsd_libc/regex/cclass.h @@ -0,0 +1,104 @@ +/* $NetBSD: cclass.h,v 1.7 2003/08/07 16:43:19 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)cclass.h 8.3 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)cclass.h 8.3 (Berkeley) 3/20/94 + */ + +/* character-class table */ +static const struct cclass { + const char *name; + const char *chars; + const char *multis; +} cclasses[] = { + { "alnum", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\ +0123456789", "" }, + { "alpha", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", + "" }, + { "blank", " \t", "" }, + { "cntrl", "\007\b\t\n\v\f\r\1\2\3\4\5\6\16\17\20\21\22\23\24\ +\25\26\27\30\31\32\33\34\35\36\37\177", "" }, + { "digit", "0123456789", "" }, + { "graph", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\ +0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", + "" }, + { "lower", "abcdefghijklmnopqrstuvwxyz", + "" }, + { "print", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\ +0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ", + "" }, + { "punct", "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", + "" }, + { "space", "\t\n\v\f\r ", "" }, + { "upper", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + "" }, + { "xdigit", "0123456789ABCDEFabcdef", + "" }, + { NULL, 0, "" } +}; diff --git a/lib/nbsd_libc/regex/cname.h b/lib/nbsd_libc/regex/cname.h new file mode 100644 index 000000000..4b9ef3919 --- /dev/null +++ b/lib/nbsd_libc/regex/cname.h @@ -0,0 +1,175 @@ +/* $NetBSD: cname.h,v 1.7 2003/08/07 16:43:19 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)cname.h 8.3 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)cname.h 8.3 (Berkeley) 3/20/94 + */ + +/* character-name table */ +static const struct cname { + const char *name; + char code; +} cnames[] = { + { "NUL", '\0' }, + { "SOH", '\001' }, + { "STX", '\002' }, + { "ETX", '\003' }, + { "EOT", '\004' }, + { "ENQ", '\005' }, + { "ACK", '\006' }, + { "BEL", '\007' }, + { "alert", '\007' }, + { "BS", '\010' }, + { "backspace", '\b' }, + { "HT", '\011' }, + { "tab", '\t' }, + { "LF", '\012' }, + { "newline", '\n' }, + { "VT", '\013' }, + { "vertical-tab", '\v' }, + { "FF", '\014' }, + { "form-feed", '\f' }, + { "CR", '\015' }, + { "carriage-return", '\r' }, + { "SO", '\016' }, + { "SI", '\017' }, + { "DLE", '\020' }, + { "DC1", '\021' }, + { "DC2", '\022' }, + { "DC3", '\023' }, + { "DC4", '\024' }, + { "NAK", '\025' }, + { "SYN", '\026' }, + { "ETB", '\027' }, + { "CAN", '\030' }, + { "EM", '\031' }, + { "SUB", '\032' }, + { "ESC", '\033' }, + { "IS4", '\034' }, + { "FS", '\034' }, + { "IS3", '\035' }, + { "GS", '\035' }, + { "IS2", '\036' }, + { "RS", '\036' }, + { "IS1", '\037' }, + { "US", '\037' }, + { "space", ' ' }, + { "exclamation-mark", '!' }, + { "quotation-mark", '"' }, + { "number-sign", '#' }, + { "dollar-sign", '$' }, + { "percent-sign", '%' }, + { "ampersand", '&' }, + { "apostrophe", '\'' }, + { "left-parenthesis", '(' }, + { "right-parenthesis", ')' }, + { "asterisk", '*' }, + { "plus-sign", '+' }, + { "comma", ',' }, + { "hyphen", '-' }, + { "hyphen-minus", '-' }, + { "period", '.' }, + { "full-stop", '.' }, + { "slash", '/' }, + { "solidus", '/' }, + { "zero", '0' }, + { "one", '1' }, + { "two", '2' }, + { "three", '3' }, + { "four", '4' }, + { "five", '5' }, + { "six", '6' }, + { "seven", '7' }, + { "eight", '8' }, + { "nine", '9' }, + { "colon", ':' }, + { "semicolon", ';' }, + { "less-than-sign", '<' }, + { "equals-sign", '=' }, + { "greater-than-sign", '>' }, + { "question-mark", '?' }, + { "commercial-at", '@' }, + { "left-square-bracket", '[' }, + { "backslash", '\\' }, + { "reverse-solidus", '\\' }, + { "right-square-bracket", ']' }, + { "circumflex", '^' }, + { "circumflex-accent", '^' }, + { "underscore", '_' }, + { "low-line", '_' }, + { "grave-accent", '`' }, + { "left-brace", '{' }, + { "left-curly-bracket", '{' }, + { "vertical-line", '|' }, + { "right-brace", '}' }, + { "right-curly-bracket", '}' }, + { "tilde", '~' }, + { "DEL", '\177' }, + { NULL, 0 }, +}; diff --git a/lib/nbsd_libc/regex/engine.c b/lib/nbsd_libc/regex/engine.c new file mode 100644 index 000000000..f51ff9cc3 --- /dev/null +++ b/lib/nbsd_libc/regex/engine.c @@ -0,0 +1,1188 @@ +/* $NetBSD: engine.c,v 1.22 2009/02/12 05:06:54 lukem Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)engine.c 8.5 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)engine.c 8.5 (Berkeley) 3/20/94 + */ + +/* + * The matching engine and friends. This file is #included by regexec.c + * after suitable #defines of a variety of macros used herein, so that + * different state representations can be used without duplicating masses + * of code. + */ + +#ifdef SNAMES +#define matcher smatcher +#define fast sfast +#define slow sslow +#define dissect sdissect +#define backref sbackref +#define step sstep +#define print sprint +#define at sat +#define match smat +#define nope snope +#endif +#ifdef LNAMES +#define matcher lmatcher +#define fast lfast +#define slow lslow +#define dissect ldissect +#define backref lbackref +#define step lstep +#define print lprint +#define at lat +#define match lmat +#define nope lnope +#endif + +/* another structure passed up and down to avoid zillions of parameters */ +struct match { + struct re_guts *g; + int eflags; + regmatch_t *pmatch; /* [nsub+1] (0 element unused) */ + const char *offp; /* offsets work from here */ + const char *beginp; /* start of string -- virtual NUL precedes */ + const char *endp; /* end of string -- virtual NUL here */ + const char *coldp; /* can be no match starting before here */ + const char **lastpos; /* [nplus+1] */ + STATEVARS; + states st; /* current states */ + states fresh; /* states for a fresh start */ + states tmp; /* temporary */ + states empty; /* empty set of states */ +}; + +/* ========= begin header generated by ./mkh ========= */ +#ifdef __cplusplus +extern "C" { +#endif + +/* === engine.c === */ +static int matcher(struct re_guts *g, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags); +static const char *dissect(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst); +static const char *backref(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst, sopno lev); +static const char *fast(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst); +static const char *slow(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst); +static states step(struct re_guts *g, sopno start, sopno stop, states bef, int ch, states aft); +#define BOL (OUT+1) +#define EOL (BOL+1) +#define BOLEOL (BOL+2) +#define NOTHING (BOL+3) +#define BOW (BOL+4) +#define EOW (BOL+5) +#define CODEMAX (BOL+5) /* highest code used */ +#define NONCHAR(c) ((c) > CHAR_MAX) +#define NNONCHAR (CODEMAX-CHAR_MAX) +#ifdef REDEBUG +static void print(struct match *m, char *caption, states st, int ch, FILE *d); +#endif +#ifdef REDEBUG +static void at(struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst); +#endif +#ifdef REDEBUG +static char *pchar(int ch); +#endif + +#ifdef __cplusplus +} +#endif +/* ========= end header generated by ./mkh ========= */ + +#ifdef REDEBUG +#define SP(t, s, c) print(m, t, s, c, stdout) +#define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2) +#define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); } +static int nope = 0; +#else +#define SP(t, s, c) /* nothing */ +#define AT(t, p1, p2, s1, s2) /* nothing */ +#define NOTE(s) /* nothing */ +#endif + +/* + - matcher - the actual matching engine + == static int matcher(struct re_guts *g, char *string, \ + == size_t nmatch, regmatch_t pmatch[], int eflags); + */ +static int /* 0 success, REG_NOMATCH failure */ +matcher( + struct re_guts *g, + const char *string, + size_t nmatch, + regmatch_t pmatch[], + int eflags) +{ + const char *endp; + size_t i; + struct match mv; + struct match *m = &mv; + const char *dp; + const sopno gf = g->firststate+1; /* +1 for OEND */ + const sopno gl = g->laststate; + const char *start; + const char *stop; + int error = 0; + + _DIAGASSERT(g != NULL); + _DIAGASSERT(string != NULL); + /* pmatch checked below */ + + /* simplify the situation where possible */ + if (g->cflags®_NOSUB) + nmatch = 0; + if (eflags®_STARTEND) { + _DIAGASSERT(pmatch != NULL); + start = string + (size_t)pmatch[0].rm_so; + stop = string + (size_t)pmatch[0].rm_eo; + } else { + start = string; + stop = start + strlen(start); + } + if (stop < start) + return(REG_INVARG); + + /* prescreening; this does wonders for this rather slow code */ + if (g->must != NULL) { + for (dp = start; dp < stop; dp++) + if (*dp == g->must[0] && stop - dp >= g->mlen && + memcmp(dp, g->must, (size_t)g->mlen) == 0) + break; + if (dp == stop) /* we didn't find g->must */ + return(REG_NOMATCH); + } + + /* match struct setup */ + m->g = g; + m->eflags = eflags; + m->pmatch = NULL; + m->lastpos = NULL; + m->offp = string; + m->beginp = start; + m->endp = stop; + STATESETUP(m, 4); + SETUP(m->st); + SETUP(m->fresh); + SETUP(m->tmp); + SETUP(m->empty); + CLEAR(m->empty); + + /* this loop does only one repetition except for backrefs */ + for (;;) { + endp = fast(m, start, stop, gf, gl); + if (endp == NULL) { /* a miss */ + error = REG_NOMATCH; + goto done; + } + if (nmatch == 0 && !g->backrefs) + break; /* no further info needed */ + + /* where? */ + assert(m->coldp != NULL); + for (;;) { + NOTE("finding start"); + endp = slow(m, m->coldp, stop, gf, gl); + if (endp != NULL) + break; + assert(m->coldp < m->endp); + m->coldp++; + } + if (nmatch == 1 && !g->backrefs) + break; /* no further info needed */ + + /* oh my, he wants the subexpressions... */ + if (m->pmatch == NULL) + m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) * + sizeof(regmatch_t)); + if (m->pmatch == NULL) { + error = REG_ESPACE; + goto done; + } + for (i = 1; i <= m->g->nsub; i++) + m->pmatch[i].rm_so = m->pmatch[i].rm_eo = (regoff_t)-1; + if (!g->backrefs && !(m->eflags®_BACKR)) { + NOTE("dissecting"); + dp = dissect(m, m->coldp, endp, gf, gl); + } else { + if (g->nplus > 0 && m->lastpos == NULL) + m->lastpos = malloc((g->nplus+1) * + sizeof(const char *)); + if (g->nplus > 0 && m->lastpos == NULL) { + error = REG_ESPACE; + goto done; + } + NOTE("backref dissect"); + dp = backref(m, m->coldp, endp, gf, gl, (sopno)0); + } + if (dp != NULL) + break; + + /* uh-oh... we couldn't find a subexpression-level match */ + assert(g->backrefs); /* must be back references doing it */ + assert(g->nplus == 0 || m->lastpos != NULL); + for (;;) { + if (dp != NULL || endp <= m->coldp) + break; /* defeat */ + NOTE("backoff"); + endp = slow(m, m->coldp, endp-1, gf, gl); + if (endp == NULL) + break; /* defeat */ + /* try it on a shorter possibility */ +#ifndef NDEBUG + for (i = 1; i <= m->g->nsub; i++) { + assert(m->pmatch[i].rm_so == (regoff_t)-1); + assert(m->pmatch[i].rm_eo == (regoff_t)-1); + } +#endif + NOTE("backoff dissect"); + dp = backref(m, m->coldp, endp, gf, gl, (sopno)0); + } + assert(dp == NULL || dp == endp); + if (dp != NULL) /* found a shorter one */ + break; + + /* despite initial appearances, there is no match here */ + NOTE("false alarm"); + start = m->coldp + 1; /* recycle starting later */ + assert(start <= stop); + } + + /* fill in the details if requested */ + if (nmatch > 0) { + _DIAGASSERT(pmatch != NULL); + pmatch[0].rm_so = m->coldp - m->offp; + pmatch[0].rm_eo = endp - m->offp; + } + if (nmatch > 1) { + assert(m->pmatch != NULL); + for (i = 1; i < nmatch; i++) + if (i <= m->g->nsub) + pmatch[i] = m->pmatch[i]; + else { + pmatch[i].rm_so = (regoff_t)-1; + pmatch[i].rm_eo = (regoff_t)-1; + } + } + +done: + if (m->pmatch != NULL) { + free(m->pmatch); + m->pmatch = NULL; + } + if (m->lastpos != NULL) { + free(m->lastpos); + m->lastpos = NULL; + } + STATETEARDOWN(m); + return error; +} + +/* + - dissect - figure out what matched what, no back references + == static const char *dissect(struct match *m, const char *start, \ + == const char *stop, sopno startst, sopno stopst); + */ +static const char * /* == stop (success) always */ +dissect( + struct match *m, + const char *start, + const char *stop, + sopno startst, + sopno stopst) +{ + int i; + sopno ss; /* start sop of current subRE */ + sopno es; /* end sop of current subRE */ + const char *sp; /* start of string matched by it */ + const char *stp; /* string matched by it cannot pass here */ + const char *rest; /* start of rest of string */ + const char *tail; /* string unmatched by rest of RE */ + sopno ssub; /* start sop of subsubRE */ + sopno esub; /* end sop of subsubRE */ + const char *ssp; /* start of string matched by subsubRE */ + const char *sep; /* end of string matched by subsubRE */ + const char *oldssp; /* previous ssp */ +#ifndef NDEBUG + const char *dp; +#endif + + _DIAGASSERT(m != NULL); + _DIAGASSERT(start != NULL); + _DIAGASSERT(stop != NULL); + + AT("diss", start, stop, startst, stopst); + sp = start; + for (ss = startst; ss < stopst; ss = es) { + /* identify end of subRE */ + es = ss; + switch (OP(m->g->strip[es])) { + case OPLUS_: + case OQUEST_: + es += OPND(m->g->strip[es]); + break; + case OCH_: + while (OP(m->g->strip[es]) != O_CH) + es += OPND(m->g->strip[es]); + break; + } + es++; + + /* figure out what it matched */ + switch (OP(m->g->strip[ss])) { + case OEND: + assert(nope); + break; + case OCHAR: + sp++; + break; + case OBOL: + case OEOL: + case OBOW: + case OEOW: + break; + case OANY: + case OANYOF: + sp++; + break; + case OBACK_: + case O_BACK: + assert(nope); + break; + /* cases where length of match is hard to find */ + case OQUEST_: + stp = stop; + for (;;) { + /* how long could this one be? */ + rest = slow(m, sp, stp, ss, es); + assert(rest != NULL); /* it did match */ + /* could the rest match the rest? */ + tail = slow(m, rest, stop, es, stopst); + if (tail == stop) + break; /* yes! */ + /* no -- try a shorter match for this one */ + stp = rest - 1; + assert(stp >= sp); /* it did work */ + } + ssub = ss + 1; + esub = es - 1; + /* did innards match? */ + if (slow(m, sp, rest, ssub, esub) != NULL) { +#ifdef NDEBUG + (void) +#else + dp = +#endif + dissect(m, sp, rest, ssub, esub); + assert(dp == rest); + } else /* no */ + assert(sp == rest); + sp = rest; + break; + case OPLUS_: + stp = stop; + for (;;) { + /* how long could this one be? */ + rest = slow(m, sp, stp, ss, es); + assert(rest != NULL); /* it did match */ + /* could the rest match the rest? */ + tail = slow(m, rest, stop, es, stopst); + if (tail == stop) + break; /* yes! */ + /* no -- try a shorter match for this one */ + stp = rest - 1; + assert(stp >= sp); /* it did work */ + } + ssub = ss + 1; + esub = es - 1; + ssp = sp; + oldssp = ssp; + for (;;) { /* find last match of innards */ + sep = slow(m, ssp, rest, ssub, esub); + if (sep == NULL || sep == ssp) + break; /* failed or matched null */ + oldssp = ssp; /* on to next try */ + ssp = sep; + } + if (sep == NULL) { + /* last successful match */ + sep = ssp; + ssp = oldssp; + } + assert(sep == rest); /* must exhaust substring */ + assert(slow(m, ssp, sep, ssub, esub) == rest); +#ifdef NDEBUG + (void) +#else + dp = +#endif + dissect(m, ssp, sep, ssub, esub); + assert(dp == sep); + sp = rest; + break; + case OCH_: + stp = stop; + for (;;) { + /* how long could this one be? */ + rest = slow(m, sp, stp, ss, es); + assert(rest != NULL); /* it did match */ + /* could the rest match the rest? */ + tail = slow(m, rest, stop, es, stopst); + if (tail == stop) + break; /* yes! */ + /* no -- try a shorter match for this one */ + stp = rest - 1; + assert(stp >= sp); /* it did work */ + } + ssub = ss + 1; + esub = ss + OPND(m->g->strip[ss]) - 1; + assert(OP(m->g->strip[esub]) == OOR1); + for (;;) { /* find first matching branch */ + if (slow(m, sp, rest, ssub, esub) == rest) + break; /* it matched all of it */ + /* that one missed, try next one */ + assert(OP(m->g->strip[esub]) == OOR1); + esub++; + assert(OP(m->g->strip[esub]) == OOR2); + ssub = esub + 1; + esub += OPND(m->g->strip[esub]); + if (OP(m->g->strip[esub]) == OOR2) + esub--; + else + assert(OP(m->g->strip[esub]) == O_CH); + } +#ifdef NDEBUG + (void) +#else + dp = +#endif + dissect(m, sp, rest, ssub, esub); + assert(dp == rest); + sp = rest; + break; + case O_PLUS: + case O_QUEST: + case OOR1: + case OOR2: + case O_CH: + assert(nope); + break; + case OLPAREN: + i = OPND(m->g->strip[ss]); + assert(0 < i && i <= m->g->nsub); + m->pmatch[i].rm_so = sp - m->offp; + break; + case ORPAREN: + i = OPND(m->g->strip[ss]); + assert(0 < i && i <= m->g->nsub); + m->pmatch[i].rm_eo = sp - m->offp; + break; + default: /* uh oh */ + assert(nope); + break; + } + } + + assert(sp == stop); + return(sp); +} + +/* + - backref - figure out what matched what, figuring in back references + == static const char *backref(struct match *m, const char *start, \ + == const char *stop, sopno startst, sopno stopst, sopno lev); + */ +static const char * /* == stop (success) or NULL (failure) */ +backref( + struct match *m, + const char *start, + const char *stop, + sopno startst, + sopno stopst, + sopno lev) /* PLUS nesting level */ +{ + int i; + sopno ss; /* start sop of current subRE */ + const char *sp; /* start of string matched by it */ + sopno ssub; /* start sop of subsubRE */ + sopno esub; /* end sop of subsubRE */ + const char *ssp; /* start of string matched by subsubRE */ + const char *dp; + size_t len; + int hard; + sop s; + regoff_t offsave; + cset *cs; + + _DIAGASSERT(m != NULL); + _DIAGASSERT(start != NULL); + _DIAGASSERT(stop != NULL); + + AT("back", start, stop, startst, stopst); + sp = start; + + /* get as far as we can with easy stuff */ + hard = 0; + for (ss = startst; !hard && ss < stopst; ss++) + switch (OP(s = m->g->strip[ss])) { + case OCHAR: + if (sp == stop || *sp++ != (char)OPND(s)) + return(NULL); + break; + case OANY: + if (sp == stop) + return(NULL); + sp++; + break; + case OANYOF: + cs = &m->g->sets[OPND(s)]; + if (sp == stop || !CHIN(cs, *sp++)) + return(NULL); + break; + case OBOL: + if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) || + (sp < m->endp && *(sp-1) == '\n' && + (m->g->cflags®_NEWLINE)) ) + { /* yes */ } + else + return(NULL); + break; + case OEOL: + if ( (sp == m->endp && !(m->eflags®_NOTEOL)) || + (sp < m->endp && *sp == '\n' && + (m->g->cflags®_NEWLINE)) ) + { /* yes */ } + else + return(NULL); + break; + case OBOW: + if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) || + (sp < m->endp && *(sp-1) == '\n' && + (m->g->cflags®_NEWLINE)) || + (sp > m->beginp && + !ISWORD(*(sp-1))) ) && + (sp < m->endp && ISWORD(*sp)) ) + { /* yes */ } + else + return(NULL); + break; + case OEOW: + if (( (sp == m->endp && !(m->eflags®_NOTEOL)) || + (sp < m->endp && *sp == '\n' && + (m->g->cflags®_NEWLINE)) || + (sp < m->endp && !ISWORD(*sp)) ) && + (sp > m->beginp && ISWORD(*(sp-1))) ) + { /* yes */ } + else + return(NULL); + break; + case O_QUEST: + break; + case OOR1: /* matches null but needs to skip */ + ss++; + s = m->g->strip[ss]; + do { + assert(OP(s) == OOR2); + ss += OPND(s); + } while (OP(s = m->g->strip[ss]) != O_CH); + /* note that the ss++ gets us past the O_CH */ + break; + default: /* have to make a choice */ + hard = 1; + break; + } + if (!hard) { /* that was it! */ + if (sp != stop) + return(NULL); + return(sp); + } + ss--; /* adjust for the for's final increment */ + + /* the hard stuff */ + AT("hard", sp, stop, ss, stopst); + s = m->g->strip[ss]; + switch (OP(s)) { + case OBACK_: /* the vilest depths */ + i = OPND(s); + assert(0 < i && i <= m->g->nsub); + if (m->pmatch[i].rm_eo == (regoff_t)-1) + return(NULL); + assert(m->pmatch[i].rm_so != (regoff_t)-1); + len = (size_t)(m->pmatch[i].rm_eo - m->pmatch[i].rm_so); + if (len == 0) + return(NULL); + assert(stop - m->beginp >= len); + if (sp > stop - len) + return(NULL); /* not enough left to match */ + ssp = m->offp + (size_t)m->pmatch[i].rm_so; + if (memcmp(sp, ssp, len) != 0) + return(NULL); + while (m->g->strip[ss] != SOP(O_BACK, i)) + ss++; + return(backref(m, sp+len, stop, ss+1, stopst, lev)); + + case OQUEST_: /* to null or not */ + dp = backref(m, sp, stop, ss+1, stopst, lev); + if (dp != NULL) + return(dp); /* not */ + return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev)); + + case OPLUS_: + assert(m->lastpos != NULL); + assert(lev+1 <= m->g->nplus); + m->lastpos[lev+1] = sp; + return(backref(m, sp, stop, ss+1, stopst, lev+1)); + + case O_PLUS: + if (sp == m->lastpos[lev]) /* last pass matched null */ + return(backref(m, sp, stop, ss+1, stopst, lev-1)); + /* try another pass */ + m->lastpos[lev] = sp; + dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev); + if (dp == NULL) + dp = backref(m, sp, stop, ss+1, stopst, lev-1); + return(dp); + + case OCH_: /* find the right one, if any */ + ssub = ss + 1; + esub = ss + OPND(s) - 1; + assert(OP(m->g->strip[esub]) == OOR1); + for (;;) { /* find first matching branch */ + dp = backref(m, sp, stop, ssub, esub, lev); + if (dp != NULL) + return(dp); + /* that one missed, try next one */ + if (OP(m->g->strip[esub]) == O_CH) + return(NULL); /* there is none */ + esub++; + assert(OP(m->g->strip[esub]) == OOR2); + ssub = esub + 1; + esub += OPND(m->g->strip[esub]); + if (OP(m->g->strip[esub]) == OOR2) + esub--; + else + assert(OP(m->g->strip[esub]) == O_CH); + } + + case OLPAREN: /* must undo assignment if rest fails */ + i = OPND(s); + assert(0 < i && i <= m->g->nsub); + offsave = m->pmatch[i].rm_so; + m->pmatch[i].rm_so = sp - m->offp; + dp = backref(m, sp, stop, ss+1, stopst, lev); + if (dp != NULL) + return(dp); + m->pmatch[i].rm_so = offsave; + return(NULL); + + case ORPAREN: /* must undo assignment if rest fails */ + i = OPND(s); + assert(0 < i && i <= m->g->nsub); + offsave = m->pmatch[i].rm_eo; + m->pmatch[i].rm_eo = sp - m->offp; + dp = backref(m, sp, stop, ss+1, stopst, lev); + if (dp != NULL) + return(dp); + m->pmatch[i].rm_eo = offsave; + return(NULL); + + default: /* uh oh */ + assert(nope); + break; + } + + /* "can't happen" */ + assert(nope); + /* NOTREACHED */ + return NULL; +} + +/* + - fast - step through the string at top speed + == static const char *fast(struct match *m, const char *start, \ + == const char *stop, sopno startst, sopno stopst); + */ +static const char * /* where tentative match ended, or NULL */ +fast( + struct match *m, + const char *start, + const char *stop, + sopno startst, + sopno stopst) +{ + states st = m->st; + states fresh = m->fresh; + states tmp = m->tmp; + const char *p = start; + int c = (start == m->beginp) ? OUT : *(start-1); + int lastc; /* previous c */ + int flagch; + int i; + const char *coldp; /* last p after which no match was underway */ + + _DIAGASSERT(m != NULL); + _DIAGASSERT(start != NULL); + _DIAGASSERT(stop != NULL); + + CLEAR(st); + SET1(st, startst); + st = step(m->g, startst, stopst, st, NOTHING, st); + ASSIGN(fresh, st); + SP("start", st, *p); + coldp = NULL; + for (;;) { + /* next character */ + lastc = c; + c = (p == m->endp) ? OUT : *p; + if (EQ(st, fresh)) + coldp = p; + + /* is there an EOL and/or BOL between lastc and c? */ + flagch = '\0'; + i = 0; + if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || + (lastc == OUT && !(m->eflags®_NOTBOL)) ) { + flagch = BOL; + i = m->g->nbol; + } + if ( (c == '\n' && m->g->cflags®_NEWLINE) || + (c == OUT && !(m->eflags®_NOTEOL)) ) { + flagch = (flagch == BOL) ? BOLEOL : EOL; + i += m->g->neol; + } + if (i != 0) { + for (; i > 0; i--) + st = step(m->g, startst, stopst, st, flagch, st); + SP("boleol", st, c); + } + + /* how about a word boundary? */ + if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) && + (c != OUT && ISWORD(c)) ) { + flagch = BOW; + } + if ( (lastc != OUT && ISWORD(lastc)) && + (flagch == EOL || (c != OUT && !ISWORD(c))) ) { + flagch = EOW; + } + if (flagch == BOW || flagch == EOW) { + st = step(m->g, startst, stopst, st, flagch, st); + SP("boweow", st, c); + } + + /* are we done? */ + if (ISSET(st, stopst) || p == stop) + break; /* NOTE BREAK OUT */ + + /* no, we must deal with this character */ + ASSIGN(tmp, st); + ASSIGN(st, fresh); + assert(c != OUT); + st = step(m->g, startst, stopst, tmp, c, st); + SP("aft", st, c); + assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); + p++; + } + + assert(coldp != NULL); + m->coldp = coldp; + if (ISSET(st, stopst)) + return(p+1); + else + return(NULL); +} + +/* + - slow - step through the string more deliberately + == static const char *slow(struct match *m, const char *start, \ + == const char *stop, sopno startst, sopno stopst); + */ +static const char * /* where it ended */ +slow( + struct match *m, + const char *start, + const char *stop, + sopno startst, + sopno stopst) +{ + states st = m->st; + states empty = m->empty; + states tmp = m->tmp; + const char *p = start; + int c = (start == m->beginp) ? OUT : *(start-1); + int lastc; /* previous c */ + int flagch; + int i; + const char *matchp; /* last p at which a match ended */ + + _DIAGASSERT(m != NULL); + _DIAGASSERT(start != NULL); + _DIAGASSERT(stop != NULL); + + AT("slow", start, stop, startst, stopst); + CLEAR(st); + SET1(st, startst); + SP("sstart", st, *p); + st = step(m->g, startst, stopst, st, NOTHING, st); + matchp = NULL; + for (;;) { + /* next character */ + lastc = c; + c = (p == m->endp) ? OUT : *p; + + /* is there an EOL and/or BOL between lastc and c? */ + flagch = '\0'; + i = 0; + if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || + (lastc == OUT && !(m->eflags®_NOTBOL)) ) { + flagch = BOL; + i = m->g->nbol; + } + if ( (c == '\n' && m->g->cflags®_NEWLINE) || + (c == OUT && !(m->eflags®_NOTEOL)) ) { + flagch = (flagch == BOL) ? BOLEOL : EOL; + i += m->g->neol; + } + if (i != 0) { + for (; i > 0; i--) + st = step(m->g, startst, stopst, st, flagch, st); + SP("sboleol", st, c); + } + + /* how about a word boundary? */ + if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) && + (c != OUT && ISWORD(c)) ) { + flagch = BOW; + } + if ( (lastc != OUT && ISWORD(lastc)) && + (flagch == EOL || (c != OUT && !ISWORD(c))) ) { + flagch = EOW; + } + if (flagch == BOW || flagch == EOW) { + st = step(m->g, startst, stopst, st, flagch, st); + SP("sboweow", st, c); + } + + /* are we done? */ + if (ISSET(st, stopst)) + matchp = p; + if (EQ(st, empty) || p == stop) + break; /* NOTE BREAK OUT */ + + /* no, we must deal with this character */ + ASSIGN(tmp, st); + ASSIGN(st, empty); + assert(c != OUT); + st = step(m->g, startst, stopst, tmp, c, st); + SP("saft", st, c); + assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); + p++; + } + + return(matchp); +} + + +/* + - step - map set of states reachable before char to set reachable after + == static states step(struct re_guts *g, sopno start, sopno stop, \ + == states bef, int ch, states aft); + == #define BOL (OUT+1) + == #define EOL (BOL+1) + == #define BOLEOL (BOL+2) + == #define NOTHING (BOL+3) + == #define BOW (BOL+4) + == #define EOW (BOL+5) + == #define CODEMAX (BOL+5) // highest code used + == #define NONCHAR(c) ((c) > CHAR_MAX) + == #define NNONCHAR (CODEMAX-CHAR_MAX) + */ +static states +step( + struct re_guts *g, + sopno start, /* start state within strip */ + sopno stop, /* state after stop state within strip */ + states bef, /* states reachable before */ + int ch, /* character or NONCHAR code */ + states aft) /* states already known reachable after */ +{ + cset *cs; + sop s; + sopno pc; + onestate here; /* note, macros know this name */ + sopno look; + int i; + + _DIAGASSERT(g != NULL); + + for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) { + s = g->strip[pc]; + switch (OP(s)) { + case OEND: + assert(pc == stop-1); + break; + case OCHAR: + /* only characters can match */ + assert(!NONCHAR(ch) || ch != (char)OPND(s)); + if (ch == (char)OPND(s)) + FWD(aft, bef, 1); + break; + case OBOL: + if (ch == BOL || ch == BOLEOL) + FWD(aft, bef, 1); + break; + case OEOL: + if (ch == EOL || ch == BOLEOL) + FWD(aft, bef, 1); + break; + case OBOW: + if (ch == BOW) + FWD(aft, bef, 1); + break; + case OEOW: + if (ch == EOW) + FWD(aft, bef, 1); + break; + case OANY: + if (!NONCHAR(ch)) + FWD(aft, bef, 1); + break; + case OANYOF: + cs = &g->sets[OPND(s)]; + if (!NONCHAR(ch) && CHIN(cs, ch)) + FWD(aft, bef, 1); + break; + case OBACK_: /* ignored here */ + case O_BACK: + FWD(aft, aft, 1); + break; + case OPLUS_: /* forward, this is just an empty */ + FWD(aft, aft, 1); + break; + case O_PLUS: /* both forward and back */ + FWD(aft, aft, 1); + i = ISSETBACK(aft, OPND(s)); + BACK(aft, aft, OPND(s)); + if (!i && ISSETBACK(aft, OPND(s))) { + /* oho, must reconsider loop body */ + pc -= OPND(s) + 1; + INIT(here, pc); + } + break; + case OQUEST_: /* two branches, both forward */ + FWD(aft, aft, 1); + FWD(aft, aft, OPND(s)); + break; + case O_QUEST: /* just an empty */ + FWD(aft, aft, 1); + break; + case OLPAREN: /* not significant here */ + case ORPAREN: + FWD(aft, aft, 1); + break; + case OCH_: /* mark the first two branches */ + FWD(aft, aft, 1); + assert(OP(g->strip[pc+OPND(s)]) == OOR2); + FWD(aft, aft, OPND(s)); + break; + case OOR1: /* done a branch, find the O_CH */ + if (ISSTATEIN(aft, here)) { + for (look = 1; + OP(s = g->strip[pc+look]) != O_CH; + look += OPND(s)) + assert(OP(s) == OOR2); + FWD(aft, aft, look); + } + break; + case OOR2: /* propagate OCH_'s marking */ + FWD(aft, aft, 1); + if (OP(g->strip[pc+OPND(s)]) != O_CH) { + assert(OP(g->strip[pc+OPND(s)]) == OOR2); + FWD(aft, aft, OPND(s)); + } + break; + case O_CH: /* just empty */ + FWD(aft, aft, 1); + break; + default: /* ooooops... */ + assert(nope); + break; + } + } + + return(aft); +} + +#ifdef REDEBUG +/* + - print - print a set of states + == #ifdef REDEBUG + == static void print(struct match *m, char *caption, states st, \ + == int ch, FILE *d); + == #endif + */ +static void +print( + struct match *m, + char *caption, + states st, + int ch, + FILE *d) +{ + struct re_guts *g = m->g; + int i; + int first = 1; + + _DIAGASSERT(m != NULL); + _DIAGASSERT(caption != NULL); + + if (!(m->eflags®_TRACE)) + return; + + _DIAGASSERT(d != NULL); + + fprintf(d, "%s", caption); + if (ch != '\0') + fprintf(d, " %s", pchar(ch)); + for (i = 0; i < g->nstates; i++) + if (ISSET(st, i)) { + fprintf(d, "%s%d", (first) ? "\t" : ", ", i); + first = 0; + } + fprintf(d, "\n"); +} + +/* + - at - print current situation + == #ifdef REDEBUG + == static void at(struct match *m, char *title, char *start, char *stop, \ + == sopno startst, sopno stopst); + == #endif + */ +static void +at( + struct match *m, + char *title, + char *start, + char *stop, + sopno startst, + sopno stopst) +{ + + _DIAGASSERT(m != NULL); + _DIAGASSERT(title != NULL); + _DIAGASSERT(start != NULL); + _DIAGASSERT(stop != NULL); + + if (!(m->eflags®_TRACE)) + return; + + printf("%s %s-", title, pchar(*start)); + printf("%s ", pchar(*stop)); + printf("%ld-%ld\n", (long)startst, (long)stopst); +} + +#ifndef PCHARDONE +#define PCHARDONE /* never again */ +/* + - pchar - make a character printable + == #ifdef REDEBUG + == static char *pchar(int ch); + == #endif + * + * Is this identical to regchar() over in debug.c? Well, yes. But a + * duplicate here avoids having a debugging-capable regexec.o tied to + * a matching debug.o, and this is convenient. It all disappears in + * the non-debug compilation anyway, so it doesn't matter much. + */ +static char * /* -> representation */ +pchar( + int ch) +{ + static char pbuf[10]; + + if (isprint(ch) || ch == ' ') + (void)snprintf(pbuf, sizeof pbuf, "%c", ch); + else + (void)snprintf(pbuf, sizeof pbuf, "\\%o", ch); + return(pbuf); +} +#endif +#endif + +#undef matcher +#undef fast +#undef slow +#undef dissect +#undef backref +#undef step +#undef print +#undef at +#undef match +#undef nope diff --git a/lib/nbsd_libc/regex/re_format.7 b/lib/nbsd_libc/regex/re_format.7 new file mode 100644 index 000000000..2b1ced522 --- /dev/null +++ b/lib/nbsd_libc/regex/re_format.7 @@ -0,0 +1,315 @@ +.\" $NetBSD: re_format.7,v 1.9 2009/04/21 14:46:02 joerg Exp $ +.\" +.\" Copyright (c) 1992, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Henry Spencer. +.\" +.\" 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. +.\" +.\" Copyright (c) 1992, 1993, 1994 Henry Spencer. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Henry Spencer. +.\" +.\" 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 by the University of +.\" California, Berkeley and its contributors. +.\" 4. 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. +.\" +.\" @(#)re_format.7 8.3 (Berkeley) 3/20/94 +.\" +.Dd March 20, 1994 +.Dt RE_FORMAT 7 +.Os +.Sh NAME +.Nm re_format +.Nd POSIX 1003.2 regular expressions +.Sh DESCRIPTION +Regular expressions (``RE''s), +as defined in POSIX 1003.2, come in two forms: +modern REs (roughly those of +.Xr egrep 1 ; +1003.2 calls these ``extended'' REs) +and obsolete REs (roughly those of +.Xr ed 1 ; +1003.2 ``basic'' REs). +Obsolete REs mostly exist for backward compatibility in some old programs; +they will be discussed at the end. +1003.2 leaves some aspects of RE syntax and semantics open; +`\(dg' marks decisions on these aspects that +may not be fully portable to other 1003.2 implementations. +.Pp +A (modern) RE is one\(dg or more non-empty\(dg +.Em branches , +separated by `|'. +It matches anything that matches one of the branches. +.Pp +A branch is one\(dg or more +.Em pieces , +concatenated. +It matches a match for the first, followed by a match for the second, etc. +.Pp +A piece is an +.Em atom +possibly followed +by a single\(dg `*', `+', `?', or +.Em bound . +An atom followed by `*' matches a sequence of 0 or more matches of the atom. +An atom followed by `+' matches a sequence of 1 or more matches of the atom. +An atom followed by `?' matches a sequence of 0 or 1 matches of the atom. +.Pp +A +.Em bound +is `{' followed by an unsigned decimal integer, possibly followed by `,' +possibly followed by another unsigned decimal integer, +always followed by `}'. +The integers must lie between 0 and RE_DUP_MAX (255\(dg) inclusive, +and if there are two of them, the first may not exceed the second. +An atom followed by a bound containing one integer +.Em i +and no comma matches a sequence of exactly +.Em i +matches of the atom. +An atom followed by a bound containing one integer +.Em i +and a comma matches a sequence of +.Em i +or more matches of the atom. +An atom followed by a bound containing two integers +.Em i +and +.Em j +matches a sequence of +.Em i +through +.Em j +(inclusive) matches of the atom. +.Pp +An atom is a regular expression enclosed in `()' (matching a match for the +regular expression), an empty set of `()' (matching the null string)\(dg, a +.Em bracket expression +(see below), `.' (matching any single character), +`^' (matching the null string at the beginning of a line), +`$' (matching the null string at the end of a line), +a `\e' followed by one of the characters `^.[$()|*+?{\e' +(matching that character taken as an ordinary character), +a `\e' followed by any other character\(dg +(matching that character taken as an ordinary character, +as if the `\e' had not been present\(dg), +or a single character with no other significance (matching that character). +A `{' followed by a character other than a digit is an ordinary +character, not the beginning of a bound\(dg. +It is illegal to end an RE with `\e'. +.Pp +A +.Em bracket expression +is a list of characters enclosed in `[]'. +It normally matches any single character from the list (but see below). +If the list begins with `^', +it matches any single character (but see below) +.Em not +from the rest of the list. +If two characters in the list are separated by `\-', this is shorthand +for the full +.Em range +of characters between those two (inclusive) in the collating sequence, +e.g. `[0-9]' in ASCII matches any decimal digit. +It is illegal\(dg for two ranges to share an endpoint, e.g. `a-c-e'. +Ranges are very collating-sequence-dependent, +and portable programs should avoid relying on them. +.Pp +To include a literal `]' in the list, make it the first character +(following a possible `^'). +To include a literal `\-', make it the first or last character, +or the second endpoint of a range. +To use a literal `\-' as the first endpoint of a range, +enclose it in `[.' and `.]' to make it a collating element (see below). +With the exception of these and some combinations using `[' (see next +paragraphs), all other special characters, including `\e', lose their +special significance within a bracket expression. +.Pp +Within a bracket expression, a collating element (a character, +a multi-character sequence that collates as if it were a single character, +or a collating-sequence name for either) +enclosed in `[.' and `.]' stands for the +sequence of characters of that collating element. +The sequence is a single element of the bracket expression's list. +A bracket expression containing a multi-character collating element +can thus match more than one character, +e.g. if the collating sequence includes a `ch' collating element, +then the RE `[[.ch.]]*c' matches the first five characters +of `chchcc'. +.Pp +Within a bracket expression, a collating element enclosed in `[=' and +`=]' is an equivalence class, standing for the sequences of characters +of all collating elements equivalent to that one, including itself. +(If there are no other equivalent collating elements, +the treatment is as if the enclosing delimiters were `[.' and `.]'.) +For example, if o and '\(^o' are the members of an equivalence class, +then `[[=o=]]', `[[=\(^o'=]]', and `[o\(^o']' are all synonymous. +An equivalence class may not\(dg be an endpoint +of a range. +.Pp +Within a bracket expression, the name of a +.Em character class +enclosed in `[:' and `:]' stands for the list of all characters +belonging to that class. +Standard character class names are: +.Bl -column "alnum" "digit" "xdigit" +.It alnum digit punct +.It alpha graph space +.It blank lower upper +.It cntrl print xdigit +.El +.Pp +These stand for the character classes defined in +.Xr ctype 3 . +A locale may provide others. +A character class may not be used as an endpoint of a range. +.Pp +There are two special cases\(dg of bracket expressions: +the bracket expressions `[[:\*[Lt]:]]' and `[[:\*[Gt]:]]' match +the null string at the beginning and end of a word respectively. +A word is defined as a sequence of word characters +which is neither preceded nor followed by word characters. +A word character is an +.Em alnum +character (as defined by +.Xr ctype 3 ) +or an underscore. +This is an extension, compatible with but not specified by POSIX 1003.2, +and should be used with caution in software intended to be portable +to other systems. +.Pp +In the event that an RE could match more than one substring of a given +string, the RE matches the one starting earliest in the string. +If the RE could match more than one substring starting at that point, +it matches the longest. +Subexpressions also match the longest possible substrings, subject to +the constraint that the whole match be as long as possible, +with subexpressions starting earlier in the RE taking priority over +ones starting later. +Note that higher-level subexpressions thus take priority over +their lower-level component subexpressions. +.Pp +Match lengths are measured in characters, not collating elements. +A null string is considered longer than no match at all. +For example, +`bb*' matches the three middle characters of `abbbc', +`(wee|week)(knights|nights)' matches all ten characters of `weeknights', +when `(.*).*' is matched against `abc' the parenthesized subexpression +matches all three characters, and +when `(a*)*' is matched against `bc' both the whole RE and the parenthesized +subexpression match the null string. +.Pp +If case-independent matching is specified, +the effect is much as if all case distinctions had vanished from the +alphabet. +When an alphabetic that exists in multiple cases appears as an +ordinary character outside a bracket expression, it is effectively +transformed into a bracket expression containing both cases, +e.g. `x' becomes `[xX]'. +When it appears inside a bracket expression, all case counterparts +of it are added to the bracket expression, so that (e.g.) `[x]' +becomes `[xX]' and `[^x]' becomes `[^xX]'. +.Pp +No particular limit is imposed on the length of REs\(dg. +Programs intended to be portable should not employ REs longer +than 256 bytes, +as an implementation can refuse to accept such REs and remain +POSIX-compliant. +.Pp +Obsolete (``basic'') regular expressions differ in several respects. +`|', `+', and `?' are ordinary characters and there is no equivalent +for their functionality. +The delimiters for bounds are `\e{' and `\e}', +with `{' and `}' by themselves ordinary characters. +The parentheses for nested subexpressions are `\e(' and `\e)', +with `(' and `)' by themselves ordinary characters. +`^' is an ordinary character except at the beginning of the +RE or\(dg the beginning of a parenthesized subexpression, +`$' is an ordinary character except at the end of the +RE or\(dg the end of a parenthesized subexpression, +and `*' is an ordinary character if it appears at the beginning of the +RE or the beginning of a parenthesized subexpression +(after a possible leading `^'). +Finally, there is one new type of atom, a +.Em back reference : +`\e' followed by a non-zero decimal digit +.Em d +matches the same sequence of characters +matched by the +.Em d Ns th parenthesized subexpression +(numbering subexpressions by the positions of their opening parentheses, +left to right), +so that (e.g.) `\e([bc]\e)\e1' matches `bb' or `cc' but not `bc'. +.Sh SEE ALSO +.Xr regex 3 +.Pp +POSIX 1003.2, section 2.8 (Regular Expression Notation). +.Sh BUGS +Having two kinds of REs is a botch. +.Pp +The current 1003.2 spec says that `)' is an ordinary character in +the absence of an unmatched `('; +this was an unintentional result of a wording error, and change is likely. +Avoid relying on it. +.Pp +Back references are a dreadful botch, +posing major problems for efficient implementations. +They are also somewhat vaguely defined +(does `a\e(\e(b\e)*\e2\e)*d' match `abbbd'?). +Avoid using them. +.Pp +1003.2's specification of case-independent matching is vague. +The ``one case implies all cases'' definition given above +is current consensus among implementors as to the right interpretation. +.Pp +The syntax for word boundaries is incredibly ugly. diff --git a/lib/nbsd_libc/regex/regcomp.c b/lib/nbsd_libc/regex/regcomp.c new file mode 100644 index 000000000..fdaa6ac6f --- /dev/null +++ b/lib/nbsd_libc/regex/regcomp.c @@ -0,0 +1,1902 @@ +/* $NetBSD: regcomp.c,v 1.29 2009/02/12 05:06:54 lukem Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)regcomp.c 8.5 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)regcomp.c 8.5 (Berkeley) 3/20/94 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94"; +#else +__RCSID("$NetBSD: regcomp.c,v 1.29 2009/02/12 05:06:54 lukem Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(regcomp,_regcomp) +#endif + +#include "utils.h" +#include "regex2.h" + +#include "cclass.h" +#include "cname.h" + +/* + * parse structure, passed up and down to avoid global variables and + * other clumsinesses + */ +struct parse { + const char *next; /* next character in RE */ + const char *end; /* end of string (-> NUL normally) */ + int error; /* has an error been seen? */ + sop *strip; /* malloced strip */ + sopno ssize; /* malloced strip size (allocated) */ + sopno slen; /* malloced strip length (used) */ + int ncsalloc; /* number of csets allocated */ + struct re_guts *g; +# define NPAREN 10 /* we need to remember () 1-9 for back refs */ + sopno pbegin[NPAREN]; /* -> ( ([0] unused) */ + sopno pend[NPAREN]; /* -> ) ([0] unused) */ +}; + +/* ========= begin header generated by ./mkh ========= */ +#ifdef __cplusplus +extern "C" { +#endif + +/* === regcomp.c === */ +static void p_ere(struct parse *p, int stop); +static void p_ere_exp(struct parse *p); +static void p_str(struct parse *p); +static void p_bre(struct parse *p, int end1, int end2); +static int p_simp_re(struct parse *p, int starordinary); +static int p_count(struct parse *p); +static void p_bracket(struct parse *p); +static void p_b_term(struct parse *p, cset *cs); +static void p_b_cclass(struct parse *p, cset *cs); +static void p_b_eclass(struct parse *p, cset *cs); +static char p_b_symbol(struct parse *p); +static char p_b_coll_elem(struct parse *p, int endc); +static int othercase(int ch); +static void bothcases(struct parse *p, int ch); +static void ordinary(struct parse *p, int ch); +static void nonnewline(struct parse *p); +static void repeat(struct parse *p, sopno start, int from, int to); +static int seterr(struct parse *p, int e); +static cset *allocset(struct parse *p); +static void freeset(struct parse *p, cset *cs); +static int freezeset(struct parse *p, cset *cs); +static int firstch(struct parse *p, cset *cs); +static int nch(struct parse *p, cset *cs); +static void mcadd(struct parse *p, cset *cs, const char *cp); +#if 0 +static void mcsub(cset *cs, char *cp); +static int mcin(cset *cs, char *cp); +static char *mcfind(cset *cs, char *cp); +#endif +static void mcinvert(struct parse *p, cset *cs); +static void mccase(struct parse *p, cset *cs); +static int isinsets(struct re_guts *g, int c); +static int samesets(struct re_guts *g, int c1, int c2); +static void categorize(struct parse *p, struct re_guts *g); +static sopno dupl(struct parse *p, sopno start, sopno finish); +static void doemit(struct parse *p, sop op, sopno opnd); +static void doinsert(struct parse *p, sop op, sopno opnd, sopno pos); +static void dofwd(struct parse *p, sopno pos, sopno value); +static void enlarge(struct parse *p, sopno size); +static void stripsnug(struct parse *p, struct re_guts *g); +static void findmust(struct parse *p, struct re_guts *g); +static sopno pluscount(struct parse *p, struct re_guts *g); + +#ifdef __cplusplus +} +#endif +/* ========= end header generated by ./mkh ========= */ + +static char nuls[10]; /* place to point scanner in event of error */ + +/* + * macros for use with parse structure + * BEWARE: these know that the parse structure is named `p' !!! + */ +#define PEEK() (*p->next) +#define PEEK2() (*(p->next+1)) +#define MORE() (p->next < p->end) +#define MORE2() (p->next+1 < p->end) +#define SEE(c) (MORE() && PEEK() == (c)) +#define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b)) +#define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0) +#define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0) +#define NEXT() (p->next++) +#define NEXT2() (p->next += 2) +#define NEXTn(n) (p->next += (n)) +#define GETNEXT() (*p->next++) +#define SETERROR(e) seterr(p, (e)) +#define REQUIRE(co, e) (void) ((co) || SETERROR(e)) +#define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e)) +#define MUSTEAT(c, e) (void) (REQUIRE(MORE() && GETNEXT() == (c), e)) +#define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e)) +#define EMIT(op, sopnd) doemit(p, (sop)(op), sopnd) +#define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos) +#define AHEAD(pos) dofwd(p, pos, HERE()-(pos)) +#define ASTERN(sop, pos) EMIT(sop, HERE()-pos) +#define HERE() (p->slen) +#define THERE() (p->slen - 1) +#define THERETHERE() (p->slen - 2) +#define DROP(n) (p->slen -= (n)) + +#ifndef NDEBUG +static int never = 0; /* for use in asserts; shuts lint up */ +#else +#define never 0 /* some s have bugs too */ +#endif + +/* + - regcomp - interface for parser and compilation + = extern int regcomp(regex_t *, const char *, int); + = #define REG_BASIC 0000 + = #define REG_EXTENDED 0001 + = #define REG_ICASE 0002 + = #define REG_NOSUB 0004 + = #define REG_NEWLINE 0010 + = #define REG_NOSPEC 0020 + = #define REG_PEND 0040 + = #define REG_DUMP 0200 + */ +int /* 0 success, otherwise REG_something */ +regcomp( + regex_t *preg, + const char *pattern, + int cflags) +{ + struct parse pa; + struct re_guts *g; + struct parse *p = &pa; + int i; + size_t len; +#ifdef REDEBUG +# define GOODFLAGS(f) (f) +#else +# define GOODFLAGS(f) ((f)&~REG_DUMP) +#endif + + _DIAGASSERT(preg != NULL); + _DIAGASSERT(pattern != NULL); + + cflags = GOODFLAGS(cflags); + if ((cflags®_EXTENDED) && (cflags®_NOSPEC)) + return(REG_INVARG); + + if (cflags®_PEND) { + if (preg->re_endp < pattern) + return(REG_INVARG); + len = preg->re_endp - pattern; + } else + len = strlen(pattern); + + /* do the mallocs early so failure handling is easy */ + g = (struct re_guts *)malloc(sizeof(struct re_guts) + + (NC-1)*sizeof(cat_t)); + if (g == NULL) + return(REG_ESPACE); + p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */ + p->strip = (sop *)malloc(p->ssize * sizeof(sop)); + p->slen = 0; + if (p->strip == NULL) { + free(g); + return(REG_ESPACE); + } + + /* set things up */ + p->g = g; + p->next = pattern; + p->end = p->next + len; + p->error = 0; + p->ncsalloc = 0; + for (i = 0; i < NPAREN; i++) { + p->pbegin[i] = 0; + p->pend[i] = 0; + } + g->csetsize = NC; + g->sets = NULL; + g->setbits = NULL; + g->ncsets = 0; + g->cflags = cflags; + g->iflags = 0; + g->nbol = 0; + g->neol = 0; + g->must = NULL; + g->mlen = 0; + g->nsub = 0; + g->ncategories = 1; /* category 0 is "everything else" */ + g->categories = &g->catspace[-(CHAR_MIN)]; + (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t)); + g->backrefs = 0; + + /* do it */ + EMIT(OEND, 0); + g->firststate = THERE(); + if (cflags®_EXTENDED) + p_ere(p, OUT); + else if (cflags®_NOSPEC) + p_str(p); + else + p_bre(p, OUT, OUT); + EMIT(OEND, 0); + g->laststate = THERE(); + + /* tidy up loose ends and fill things in */ + categorize(p, g); + stripsnug(p, g); + findmust(p, g); + g->nplus = pluscount(p, g); + g->magic = MAGIC2; + preg->re_nsub = g->nsub; + preg->re_g = g; + preg->re_magic = MAGIC1; +#ifndef REDEBUG + /* not debugging, so can't rely on the assert() in regexec() */ + if (g->iflags&BAD) + SETERROR(REG_ASSERT); +#endif + + /* win or lose, we're done */ + if (p->error != 0) /* lose */ + regfree(preg); + return(p->error); +} + +/* + - p_ere - ERE parser top level, concatenation and alternation + == static void p_ere(struct parse *p, int stop); + */ +static void +p_ere( + struct parse *p, + int stop) /* character this ERE should end at */ +{ + char c; + sopno prevback = 0; /* pacify gcc */ + sopno prevfwd = 0; /* pacify gcc */ + sopno conc; + int first = 1; /* is this the first alternative? */ + + _DIAGASSERT(p != NULL); + + for (;;) { + /* do a bunch of concatenated expressions */ + conc = HERE(); + while (MORE() && (c = PEEK()) != '|' && c != stop) + p_ere_exp(p); + REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */ + + if (!EAT('|')) + break; /* NOTE BREAK OUT */ + + if (first) { + INSERT(OCH_, conc); /* offset is wrong */ + prevfwd = conc; + prevback = conc; + first = 0; + } + ASTERN(OOR1, prevback); + prevback = THERE(); + AHEAD(prevfwd); /* fix previous offset */ + prevfwd = HERE(); + EMIT(OOR2, 0); /* offset is very wrong */ + } + + if (!first) { /* tail-end fixups */ + AHEAD(prevfwd); + ASTERN(O_CH, prevback); + } + + assert(!MORE() || SEE(stop)); +} + +/* + - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op + == static void p_ere_exp(struct parse *p); + */ +static void +p_ere_exp( + struct parse *p) +{ + char c; + sopno pos; + int count; + int count2; + sopno subno; + int wascaret = 0; + + _DIAGASSERT(p != NULL); + + assert(MORE()); /* caller should have ensured this */ + c = GETNEXT(); + + pos = HERE(); + switch (c) { + case '(': + REQUIRE(MORE(), REG_EPAREN); + p->g->nsub++; + subno = p->g->nsub; + if (subno < NPAREN) + p->pbegin[subno] = HERE(); + EMIT(OLPAREN, subno); + if (!SEE(')')) + p_ere(p, ')'); + if (subno < NPAREN) { + p->pend[subno] = HERE(); + assert(p->pend[subno] != 0); + } + EMIT(ORPAREN, subno); + MUSTEAT(')', REG_EPAREN); + break; +#ifndef POSIX_MISTAKE + case ')': /* happens only if no current unmatched ( */ + /* + * You may ask, why the ifndef? Because I didn't notice + * this until slightly too late for 1003.2, and none of the + * other 1003.2 regular-expression reviewers noticed it at + * all. So an unmatched ) is legal POSIX, at least until + * we can get it fixed. + */ + SETERROR(REG_EPAREN); + break; +#endif + case '^': + EMIT(OBOL, 0); + p->g->iflags |= USEBOL; + p->g->nbol++; + wascaret = 1; + break; + case '$': + EMIT(OEOL, 0); + p->g->iflags |= USEEOL; + p->g->neol++; + break; + case '|': + SETERROR(REG_EMPTY); + break; + case '*': + case '+': + case '?': + SETERROR(REG_BADRPT); + break; + case '.': + if (p->g->cflags®_NEWLINE) + nonnewline(p); + else + EMIT(OANY, 0); + break; + case '[': + p_bracket(p); + break; + case '\\': + REQUIRE(MORE(), REG_EESCAPE); + c = GETNEXT(); + ordinary(p, c); + break; + case '{': /* okay as ordinary except if digit follows */ + REQUIRE(!MORE() || !isdigit((unsigned char)PEEK()), REG_BADRPT); + /* FALLTHROUGH */ + default: + ordinary(p, c); + break; + } + + if (!MORE()) + return; + c = PEEK(); + /* we call { a repetition if followed by a digit */ + if (!( c == '*' || c == '+' || c == '?' || + (c == '{' && MORE2() && isdigit((unsigned char)PEEK2())) )) + return; /* no repetition, we're done */ + NEXT(); + + REQUIRE(!wascaret, REG_BADRPT); + switch (c) { + case '*': /* implemented as +? */ + /* this case does not require the (y|) trick, noKLUDGE */ + INSERT(OPLUS_, pos); + ASTERN(O_PLUS, pos); + INSERT(OQUEST_, pos); + ASTERN(O_QUEST, pos); + break; + case '+': + INSERT(OPLUS_, pos); + ASTERN(O_PLUS, pos); + break; + case '?': + /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ + INSERT(OCH_, pos); /* offset slightly wrong */ + ASTERN(OOR1, pos); /* this one's right */ + AHEAD(pos); /* fix the OCH_ */ + EMIT(OOR2, 0); /* offset very wrong... */ + AHEAD(THERE()); /* ...so fix it */ + ASTERN(O_CH, THERETHERE()); + break; + case '{': + count = p_count(p); + if (EAT(',')) { + if (isdigit((unsigned char)PEEK())) { + count2 = p_count(p); + REQUIRE(count <= count2, REG_BADBR); + } else /* single number with comma */ + count2 = INFINITY; + } else /* just a single number */ + count2 = count; + repeat(p, pos, count, count2); + if (!EAT('}')) { /* error heuristics */ + while (MORE() && PEEK() != '}') + NEXT(); + REQUIRE(MORE(), REG_EBRACE); + SETERROR(REG_BADBR); + } + break; + } + + if (!MORE()) + return; + c = PEEK(); + if (!( c == '*' || c == '+' || c == '?' || + (c == '{' && MORE2() && isdigit((unsigned char)PEEK2())) ) ) + return; + SETERROR(REG_BADRPT); +} + +/* + - p_str - string (no metacharacters) "parser" + == static void p_str(struct parse *p); + */ +static void +p_str( + struct parse *p) +{ + + _DIAGASSERT(p != NULL); + + REQUIRE(MORE(), REG_EMPTY); + while (MORE()) + ordinary(p, GETNEXT()); +} + +/* + - p_bre - BRE parser top level, anchoring and concatenation + == static void p_bre(struct parse *p, int end1, \ + == int end2); + * Giving end1 as OUT essentially eliminates the end1/end2 check. + * + * This implementation is a bit of a kludge, in that a trailing $ is first + * taken as an ordinary character and then revised to be an anchor. The + * only undesirable side effect is that '$' gets included as a character + * category in such cases. This is fairly harmless; not worth fixing. + * The amount of lookahead needed to avoid this kludge is excessive. + */ +static void +p_bre( + struct parse *p, + int end1, /* first terminating character */ + int end2) /* second terminating character */ +{ + sopno start; + int first = 1; /* first subexpression? */ + int wasdollar = 0; + + _DIAGASSERT(p != NULL); + + start = HERE(); + + if (EAT('^')) { + EMIT(OBOL, 0); + p->g->iflags |= USEBOL; + p->g->nbol++; + } + while (MORE() && !SEETWO(end1, end2)) { + wasdollar = p_simp_re(p, first); + first = 0; + } + if (wasdollar) { /* oops, that was a trailing anchor */ + DROP(1); + EMIT(OEOL, 0); + p->g->iflags |= USEEOL; + p->g->neol++; + } + + REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */ +} + +/* + - p_simp_re - parse a simple RE, an atom possibly followed by a repetition + == static int p_simp_re(struct parse *p, int starordinary); + */ +static int /* was the simple RE an unbackslashed $? */ +p_simp_re( + struct parse *p, + int starordinary) /* is a leading * an ordinary character? */ +{ + int c; + int count; + int count2; + sopno pos; + int i; + sopno subno; +# define BACKSL (1<g->cflags®_NEWLINE) + nonnewline(p); + else + EMIT(OANY, 0); + break; + case '[': + p_bracket(p); + break; + case BACKSL|'{': + SETERROR(REG_BADRPT); + break; + case BACKSL|'(': + p->g->nsub++; + subno = p->g->nsub; + if (subno < NPAREN) + p->pbegin[subno] = HERE(); + EMIT(OLPAREN, subno); + /* the MORE here is an error heuristic */ + if (MORE() && !SEETWO('\\', ')')) + p_bre(p, '\\', ')'); + if (subno < NPAREN) { + p->pend[subno] = HERE(); + assert(p->pend[subno] != 0); + } + EMIT(ORPAREN, subno); + REQUIRE(EATTWO('\\', ')'), REG_EPAREN); + break; + case BACKSL|')': /* should not get here -- must be user */ + case BACKSL|'}': + SETERROR(REG_EPAREN); + break; + case BACKSL|'1': + case BACKSL|'2': + case BACKSL|'3': + case BACKSL|'4': + case BACKSL|'5': + case BACKSL|'6': + case BACKSL|'7': + case BACKSL|'8': + case BACKSL|'9': + i = (c&~BACKSL) - '0'; + assert(i < NPAREN); + if (p->pend[i] != 0) { + assert(i <= p->g->nsub); + EMIT(OBACK_, i); + assert(p->pbegin[i] != 0); + assert(OP(p->strip[p->pbegin[i]]) == OLPAREN); + assert(OP(p->strip[p->pend[i]]) == ORPAREN); + (void) dupl(p, p->pbegin[i]+1, p->pend[i]); + EMIT(O_BACK, i); + } else + SETERROR(REG_ESUBREG); + p->g->backrefs = 1; + break; + case '*': + REQUIRE(starordinary, REG_BADRPT); + /* FALLTHROUGH */ + default: + ordinary(p, c &~ BACKSL); + break; + } + + if (EAT('*')) { /* implemented as +? */ + /* this case does not require the (y|) trick, noKLUDGE */ + INSERT(OPLUS_, pos); + ASTERN(O_PLUS, pos); + INSERT(OQUEST_, pos); + ASTERN(O_QUEST, pos); + } else if (EATTWO('\\', '{')) { + count = p_count(p); + if (EAT(',')) { + if (MORE() && isdigit((unsigned char)PEEK())) { + count2 = p_count(p); + REQUIRE(count <= count2, REG_BADBR); + } else /* single number with comma */ + count2 = INFINITY; + } else /* just a single number */ + count2 = count; + repeat(p, pos, count, count2); + if (!EATTWO('\\', '}')) { /* error heuristics */ + while (MORE() && !SEETWO('\\', '}')) + NEXT(); + REQUIRE(MORE(), REG_EBRACE); + SETERROR(REG_BADBR); + } + } else if (c == (unsigned char)'$') /* $ (but not \$) ends it */ + return(1); + + return(0); +} + +/* + - p_count - parse a repetition count + == static int p_count(struct parse *p); + */ +static int /* the value */ +p_count( + struct parse *p) +{ + int count = 0; + int ndigits = 0; + + _DIAGASSERT(p != NULL); + + while (MORE() && isdigit((unsigned char)PEEK()) && count <= DUPMAX) { + count = count*10 + (GETNEXT() - '0'); + ndigits++; + } + + REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR); + return(count); +} + +/* + - p_bracket - parse a bracketed character list + == static void p_bracket(struct parse *p); + * + * Note a significant property of this code: if the allocset() did SETERROR, + * no set operations are done. + */ +static void +p_bracket( + struct parse *p) +{ + cset *cs; + int invert = 0; + + _DIAGASSERT(p != NULL); + + cs = allocset(p); + + /* Dept of Truly Sickening Special-Case Kludges */ + if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", + (size_t)6) == 0) { + EMIT(OBOW, 0); + NEXTn(6); + return; + } + if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", + (size_t)6) == 0) { + EMIT(OEOW, 0); + NEXTn(6); + return; + } + + if (EAT('^')) + invert++; /* make note to invert set at end */ + if (EAT(']')) + CHadd(cs, ']'); + else if (EAT('-')) + CHadd(cs, '-'); + while (MORE() && PEEK() != ']' && !SEETWO('-', ']')) + p_b_term(p, cs); + if (EAT('-')) + CHadd(cs, '-'); + MUSTEAT(']', REG_EBRACK); + + if (p->error != 0) /* don't mess things up further */ + return; + + if (p->g->cflags®_ICASE) { + int i; + int ci; + + for (i = p->g->csetsize - 1; i >= 0; i--) + if (CHIN(cs, i) && isalpha(i)) { + ci = othercase(i); + if (ci != i) + CHadd(cs, ci); + } + if (cs->multis != NULL) + mccase(p, cs); + } + if (invert) { + int i; + + for (i = p->g->csetsize - 1; i >= 0; i--) + if (CHIN(cs, i)) + CHsub(cs, i); + else + CHadd(cs, i); + if (p->g->cflags®_NEWLINE) + CHsub(cs, '\n'); + if (cs->multis != NULL) + mcinvert(p, cs); + } + + assert(cs->multis == NULL); /* xxx */ + + if (nch(p, cs) == 1) { /* optimize singleton sets */ + ordinary(p, firstch(p, cs)); + freeset(p, cs); + } else + EMIT(OANYOF, freezeset(p, cs)); +} + +/* + - p_b_term - parse one term of a bracketed character list + == static void p_b_term(struct parse *p, cset *cs); + */ +static void +p_b_term( + struct parse *p, + cset *cs) +{ + char c; + char start, finish; + int i; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + /* classify what we've got */ + switch ((MORE()) ? PEEK() : '\0') { + case '[': + c = (MORE2()) ? PEEK2() : '\0'; + break; + + case '-': + SETERROR(REG_ERANGE); + return; /* NOTE RETURN */ + + default: + c = '\0'; + break; + } + + switch (c) { + case ':': /* character class */ + NEXT2(); + REQUIRE(MORE(), REG_EBRACK); + c = PEEK(); + REQUIRE(c != '-' && c != ']', REG_ECTYPE); + p_b_cclass(p, cs); + REQUIRE(MORE(), REG_EBRACK); + REQUIRE(EATTWO(':', ']'), REG_ECTYPE); + break; + case '=': /* equivalence class */ + NEXT2(); + REQUIRE(MORE(), REG_EBRACK); + c = PEEK(); + REQUIRE(c != '-' && c != ']', REG_ECOLLATE); + p_b_eclass(p, cs); + REQUIRE(MORE(), REG_EBRACK); + REQUIRE(EATTWO('=', ']'), REG_ECOLLATE); + break; + default: /* symbol, ordinary character, or range */ +/* xxx revision needed for multichar stuff */ + start = p_b_symbol(p); + if (SEE('-') && MORE2() && PEEK2() != ']') { + /* range */ + NEXT(); + if (EAT('-')) + finish = '-'; + else + finish = p_b_symbol(p); + } else + finish = start; +/* xxx what about signed chars here... */ + REQUIRE(start <= finish, REG_ERANGE); + for (i = start; i <= finish; i++) + CHadd(cs, i); + break; + } +} + +/* + - p_b_cclass - parse a character-class name and deal with it + == static void p_b_cclass(struct parse *p, cset *cs); + */ +static void +p_b_cclass( + struct parse *p, + cset *cs) +{ + const char *sp; + const struct cclass *cp; + size_t len; + const char *u; + char c; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + sp = p->next; + + while (MORE() && isalpha((unsigned char)PEEK())) + NEXT(); + len = p->next - sp; + for (cp = cclasses; cp->name != NULL; cp++) + if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0') + break; + if (cp->name == NULL) { + /* oops, didn't find it */ + SETERROR(REG_ECTYPE); + return; + } + + u = cp->chars; + while ((c = *u++) != '\0') + CHadd(cs, c); + for (u = cp->multis; *u != '\0'; u += strlen(u) + 1) + MCadd(p, cs, u); +} + +/* + - p_b_eclass - parse an equivalence-class name and deal with it + == static void p_b_eclass(struct parse *p, cset *cs); + * + * This implementation is incomplete. xxx + */ +static void +p_b_eclass( + struct parse *p, + cset *cs) +{ + char c; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + c = p_b_coll_elem(p, '='); + CHadd(cs, c); +} + +/* + - p_b_symbol - parse a character or [..]ed multicharacter collating symbol + == static char p_b_symbol(struct parse *p); + */ +static char /* value of symbol */ +p_b_symbol( + struct parse *p) +{ + char value; + + _DIAGASSERT(p != NULL); + + REQUIRE(MORE(), REG_EBRACK); + if (!EATTWO('[', '.')) + return(GETNEXT()); + + /* collating symbol */ + value = p_b_coll_elem(p, '.'); + REQUIRE(EATTWO('.', ']'), REG_ECOLLATE); + return(value); +} + +/* + - p_b_coll_elem - parse a collating-element name and look it up + == static char p_b_coll_elem(struct parse *p, int endc); + */ +static char /* value of collating element */ +p_b_coll_elem( + struct parse *p, + int endc) /* name ended by endc,']' */ +{ + const char *sp; + const struct cname *cp; + size_t len; + + _DIAGASSERT(p != NULL); + + sp = p->next; + + while (MORE() && !SEETWO(endc, ']')) + NEXT(); + if (!MORE()) { + SETERROR(REG_EBRACK); + return(0); + } + len = p->next - sp; + for (cp = cnames; cp->name != NULL; cp++) + if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0') + return(cp->code); /* known name */ + if (len == 1) + return(*sp); /* single character */ + SETERROR(REG_ECOLLATE); /* neither */ + return(0); +} + +/* + - othercase - return the case counterpart of an alphabetic + == static int othercase(int ch); + */ +static int /* if no counterpart, return ch */ +othercase( + int ch) +{ + assert(isalpha(ch)); + if (isupper(ch)) + return(tolower(ch)); + else if (islower(ch)) + return(toupper(ch)); + else /* peculiar, but could happen */ + return(ch); +} + +/* + - bothcases - emit a dualcase version of a two-case character + == static void bothcases(struct parse *p, int ch); + * + * Boy, is this implementation ever a kludge... + */ +static void +bothcases( + struct parse *p, + int ch) +{ + const char *oldnext; + const char *oldend; + char bracket[3]; + + _DIAGASSERT(p != NULL); + + oldnext = p->next; + oldend = p->end; + + assert(othercase(ch) != ch); /* p_bracket() would recurse */ + p->next = bracket; + p->end = bracket+2; + bracket[0] = ch; + bracket[1] = ']'; + bracket[2] = '\0'; + p_bracket(p); + assert(p->next == bracket+2); + p->next = oldnext; + p->end = oldend; +} + +/* + - ordinary - emit an ordinary character + == static void ordinary(struct parse *p, int ch); + */ +static void +ordinary( + struct parse *p, + int ch) +{ + cat_t *cap; + + _DIAGASSERT(p != NULL); + + cap = p->g->categories; + if ((p->g->cflags®_ICASE) && isalpha((unsigned char) ch) + && othercase((unsigned char) ch) != (unsigned char) ch) + bothcases(p, (unsigned char) ch); + else { + EMIT(OCHAR, (unsigned char)ch); + if (cap[ch] == 0) + cap[ch] = p->g->ncategories++; + } +} + +/* + - nonnewline - emit REG_NEWLINE version of OANY + == static void nonnewline(struct parse *p); + * + * Boy, is this implementation ever a kludge... + */ +static void +nonnewline( + struct parse *p) +{ + const char *oldnext; + const char *oldend; + char bracket[4]; + + _DIAGASSERT(p != NULL); + + oldnext = p->next; + oldend = p->end; + + p->next = bracket; + p->end = bracket+3; + bracket[0] = '^'; + bracket[1] = '\n'; + bracket[2] = ']'; + bracket[3] = '\0'; + p_bracket(p); + assert(p->next == bracket+3); + p->next = oldnext; + p->end = oldend; +} + +/* + - repeat - generate code for a bounded repetition, recursively if needed + == static void repeat(struct parse *p, sopno start, int from, int to); + */ +static void +repeat( + struct parse *p, + sopno start, /* operand from here to end of strip */ + int from, /* repeated from this number */ + int to) /* to this number of times (maybe INFINITY) */ +{ + sopno finish; +# define N 2 +# define INF 3 +# define REP(f, t) ((f)*8 + (t)) +# define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N) + sopno copy; + + _DIAGASSERT(p != NULL); + + finish = HERE(); + + if (p->error != 0) /* head off possible runaway recursion */ + return; + + assert(from <= to); + + switch (REP(MAP(from), MAP(to))) { + case REP(0, 0): /* must be user doing this */ + DROP(finish-start); /* drop the operand */ + break; + case REP(0, 1): /* as x{1,1}? */ + case REP(0, N): /* as x{1,n}? */ + case REP(0, INF): /* as x{1,}? */ + /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ + INSERT(OCH_, start); /* offset is wrong... */ + repeat(p, start+1, 1, to); + ASTERN(OOR1, start); + AHEAD(start); /* ... fix it */ + EMIT(OOR2, 0); + AHEAD(THERE()); + ASTERN(O_CH, THERETHERE()); + break; + case REP(1, 1): /* trivial case */ + /* done */ + break; + case REP(1, N): /* as x?x{1,n-1} */ + /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ + INSERT(OCH_, start); + ASTERN(OOR1, start); + AHEAD(start); + EMIT(OOR2, 0); /* offset very wrong... */ + AHEAD(THERE()); /* ...so fix it */ + ASTERN(O_CH, THERETHERE()); + copy = dupl(p, start+1, finish+1); + assert(copy == finish+4); + repeat(p, copy, 1, to-1); + break; + case REP(1, INF): /* as x+ */ + INSERT(OPLUS_, start); + ASTERN(O_PLUS, start); + break; + case REP(N, N): /* as xx{m-1,n-1} */ + copy = dupl(p, start, finish); + repeat(p, copy, from-1, to-1); + break; + case REP(N, INF): /* as xx{n-1,INF} */ + copy = dupl(p, start, finish); + repeat(p, copy, from-1, to); + break; + default: /* "can't happen" */ + SETERROR(REG_ASSERT); /* just in case */ + break; + } +} + +/* + - seterr - set an error condition + == static int seterr(struct parse *p, int e); + */ +static int /* useless but makes type checking happy */ +seterr( + struct parse *p, + int e) +{ + + _DIAGASSERT(p != NULL); + + if (p->error == 0) /* keep earliest error condition */ + p->error = e; + p->next = nuls; /* try to bring things to a halt */ + p->end = nuls; + return(0); /* make the return value well-defined */ +} + +/* + - allocset - allocate a set of characters for [] + == static cset *allocset(struct parse *p); + */ +static cset * +allocset( + struct parse *p) +{ + int no; + size_t nc; + size_t nbytes; + cset *cs; + size_t css; + int i; + + _DIAGASSERT(p != NULL); + + no = p->g->ncsets++; + css = (size_t)p->g->csetsize; + if (no >= p->ncsalloc) { /* need another column of space */ + p->ncsalloc += CHAR_BIT; + nc = p->ncsalloc; + assert(nc % CHAR_BIT == 0); + nbytes = nc / CHAR_BIT * css; + if (p->g->sets == NULL) + p->g->sets = malloc(nc * sizeof(cset)); + else + p->g->sets = realloc(p->g->sets, nc * sizeof(cset)); + if (p->g->setbits == NULL) + p->g->setbits = malloc(nbytes); + else { + p->g->setbits = realloc(p->g->setbits, nbytes); + /* xxx this isn't right if setbits is now NULL */ + for (i = 0; i < no; i++) + p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT); + } + if (p->g->sets != NULL && p->g->setbits != NULL) + (void) memset((char *)p->g->setbits + (nbytes - css), + 0, css); + else { + no = 0; + SETERROR(REG_ESPACE); + /* caller's responsibility not to do set ops */ + } + } + + assert(p->g->sets != NULL); /* xxx */ + cs = &p->g->sets[no]; + cs->ptr = p->g->setbits + css*((no)/CHAR_BIT); + cs->mask = 1 << ((no) % CHAR_BIT); + cs->hash = 0; + cs->smultis = 0; + cs->multis = NULL; + + return(cs); +} + +/* + - freeset - free a now-unused set + == static void freeset(struct parse *p, cset *cs); + */ +static void +freeset( + struct parse *p, + cset *cs) +{ + size_t i; + cset *top; + size_t css; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + top = &p->g->sets[p->g->ncsets]; + css = (size_t)p->g->csetsize; + + for (i = 0; i < css; i++) + CHsub(cs, i); + if (cs == top-1) /* recover only the easy case */ + p->g->ncsets--; +} + +/* + - freezeset - final processing on a set of characters + == static int freezeset(struct parse *p, cset *cs); + * + * The main task here is merging identical sets. This is usually a waste + * of time (although the hash code minimizes the overhead), but can win + * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash + * is done using addition rather than xor -- all ASCII [aA] sets xor to + * the same value! + */ +static int /* set number */ +freezeset( + struct parse *p, + cset *cs) +{ + uch h; + size_t i; + cset *top; + cset *cs2; + size_t css; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + h = cs->hash; + top = &p->g->sets[p->g->ncsets]; + css = (size_t)p->g->csetsize; + + /* look for an earlier one which is the same */ + for (cs2 = &p->g->sets[0]; cs2 < top; cs2++) + if (cs2->hash == h && cs2 != cs) { + /* maybe */ + for (i = 0; i < css; i++) + if (!!CHIN(cs2, i) != !!CHIN(cs, i)) + break; /* no */ + if (i == css) + break; /* yes */ + } + + if (cs2 < top) { /* found one */ + freeset(p, cs); + cs = cs2; + } + + return((int)(cs - p->g->sets)); +} + +/* + - firstch - return first character in a set (which must have at least one) + == static int firstch(struct parse *p, cset *cs); + */ +static int /* character; there is no "none" value */ +firstch( + struct parse *p, + cset *cs) +{ + size_t i; + size_t css; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + css = (size_t)p->g->csetsize; + + for (i = 0; i < css; i++) + if (CHIN(cs, i)) + return((char)i); + assert(never); + return(0); /* arbitrary */ +} + +/* + - nch - number of characters in a set + == static int nch(struct parse *p, cset *cs); + */ +static int +nch( + struct parse *p, + cset *cs) +{ + size_t i; + size_t css; + int n = 0; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + css = (size_t)p->g->csetsize; + + for (i = 0; i < css; i++) + if (CHIN(cs, i)) + n++; + return(n); +} + +/* + - mcadd - add a collating element to a cset + == static void mcadd(struct parse *p, cset *cs, \ + == char *cp); + */ +static void +mcadd( + struct parse *p, + cset *cs, + const char *cp) +{ + size_t oldend; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + _DIAGASSERT(cp != NULL); + + oldend = cs->smultis; + + cs->smultis += strlen(cp) + 1; + if (cs->multis == NULL) + cs->multis = malloc(cs->smultis); + else + cs->multis = realloc(cs->multis, cs->smultis); + if (cs->multis == NULL) { + SETERROR(REG_ESPACE); + return; + } + + (void) strcpy(cs->multis + oldend - 1, cp); + cs->multis[cs->smultis - 1] = '\0'; +} + +#if 0 +/* + - mcsub - subtract a collating element from a cset + == static void mcsub(cset *cs, char *cp); + */ +static void +mcsub( + cset *cs, + char *cp) +{ + char *fp; + size_t len; + + _DIAGASSERT(cs != NULL); + _DIAGASSERT(cp != NULL); + + fp = mcfind(cs, cp); + len = strlen(fp); + + assert(fp != NULL); + (void) memmove(fp, fp + len + 1, + cs->smultis - (fp + len + 1 - cs->multis)); + cs->smultis -= len; + + if (cs->smultis == 0) { + free(cs->multis); + cs->multis = NULL; + return; + } + + cs->multis = realloc(cs->multis, cs->smultis); + assert(cs->multis != NULL); +} + +/* + - mcin - is a collating element in a cset? + == static int mcin(cset *cs, char *cp); + */ +static int +mcin( + cset *cs, + char *cp) +{ + + _DIAGASSERT(cs != NULL); + _DIAGASSERT(cp != NULL); + + return(mcfind(cs, cp) != NULL); +} + +/* + - mcfind - find a collating element in a cset + == static char *mcfind(cset *cs, char *cp); + */ +static char * +mcfind( + cset *cs, + char *cp) +{ + char *p; + + _DIAGASSERT(cs != NULL); + _DIAGASSERT(cp != NULL); + + if (cs->multis == NULL) + return(NULL); + for (p = cs->multis; *p != '\0'; p += strlen(p) + 1) + if (strcmp(cp, p) == 0) + return(p); + return(NULL); +} +#endif + +/* + - mcinvert - invert the list of collating elements in a cset + == static void mcinvert(struct parse *p, cset *cs); + * + * This would have to know the set of possibilities. Implementation + * is deferred. + */ +/* ARGSUSED */ +static void +mcinvert( + struct parse *p, + cset *cs) +{ + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + assert(cs->multis == NULL); /* xxx */ +} + +/* + - mccase - add case counterparts of the list of collating elements in a cset + == static void mccase(struct parse *p, cset *cs); + * + * This would have to know the set of possibilities. Implementation + * is deferred. + */ +/* ARGSUSED */ +static void +mccase( + struct parse *p, + cset *cs) +{ + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + assert(cs->multis == NULL); /* xxx */ +} + +/* + - isinsets - is this character in any sets? + == static int isinsets(struct re_guts *g, int c); + */ +static int /* predicate */ +isinsets( + struct re_guts *g, + int c) +{ + uch *col; + int i; + int ncols; + unsigned uc = (unsigned char)c; + + _DIAGASSERT(g != NULL); + + ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT; + + for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize) + if (col[uc] != 0) + return(1); + return(0); +} + +/* + - samesets - are these two characters in exactly the same sets? + == static int samesets(struct re_guts *g, int c1, int c2); + */ +static int /* predicate */ +samesets( + struct re_guts *g, + int c1, + int c2) +{ + uch *col; + int i; + int ncols; + unsigned uc1 = (unsigned char)c1; + unsigned uc2 = (unsigned char)c2; + + _DIAGASSERT(g != NULL); + + ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT; + + for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize) + if (col[uc1] != col[uc2]) + return(0); + return(1); +} + +/* + - categorize - sort out character categories + == static void categorize(struct parse *p, struct re_guts *g); + */ +static void +categorize( + struct parse *p, + struct re_guts *g) +{ + cat_t *cats; + int c; + int c2; + cat_t cat; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(g != NULL); + + cats = g->categories; + + /* avoid making error situations worse */ + if (p->error != 0) + return; + + for (c = CHAR_MIN; c <= CHAR_MAX; c++) + if (cats[c] == 0 && isinsets(g, c)) { + cat = g->ncategories++; + cats[c] = cat; + for (c2 = c+1; c2 <= CHAR_MAX; c2++) + if (cats[c2] == 0 && samesets(g, c, c2)) + cats[c2] = cat; + } +} + +/* + - dupl - emit a duplicate of a bunch of sops + == static sopno dupl(struct parse *p, sopno start, sopno finish); + */ +static sopno /* start of duplicate */ +dupl( + struct parse *p, + sopno start, /* from here */ + sopno finish) /* to this less one */ +{ + sopno ret; + sopno len = finish - start; + + _DIAGASSERT(p != NULL); + + ret = HERE(); + + assert(finish >= start); + if (len == 0) + return(ret); + enlarge(p, p->ssize + len); /* this many unexpected additions */ + assert(p->ssize >= p->slen + len); + (void)memcpy(p->strip + p->slen, p->strip + start, + (size_t)len * sizeof(sop)); + p->slen += len; + return(ret); +} + +/* + - doemit - emit a strip operator + == static void doemit(struct parse *p, sop op, size_t opnd); + * + * It might seem better to implement this as a macro with a function as + * hard-case backup, but it's just too big and messy unless there are + * some changes to the data structures. Maybe later. + */ +static void +doemit( + struct parse *p, + sop op, + sopno opnd) +{ + + _DIAGASSERT(p != NULL); + + /* avoid making error situations worse */ + if (p->error != 0) + return; + + /* deal with oversize operands ("can't happen", more or less) */ + assert(opnd < 1<slen >= p->ssize) + enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */ + assert(p->slen < p->ssize); + + /* finally, it's all reduced to the easy case */ + p->strip[p->slen++] = SOP(op, opnd); +} + +/* + - doinsert - insert a sop into the strip + == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); + */ +static void +doinsert( + struct parse *p, + sop op, + sopno opnd, + sopno pos) +{ + sopno sn; + sop s; + int i; + + _DIAGASSERT(p != NULL); + + /* avoid making error situations worse */ + if (p->error != 0) + return; + + sn = HERE(); + EMIT(op, opnd); /* do checks, ensure space */ + assert(HERE() == sn+1); + s = p->strip[sn]; + + /* adjust paren pointers */ + assert(pos > 0); + for (i = 1; i < NPAREN; i++) { + if (p->pbegin[i] >= pos) { + p->pbegin[i]++; + } + if (p->pend[i] >= pos) { + p->pend[i]++; + } + } + + memmove(&p->strip[pos+1], &p->strip[pos], (HERE()-pos-1)*sizeof(sop)); + p->strip[pos] = s; +} + +/* + - dofwd - complete a forward reference + == static void dofwd(struct parse *p, sopno pos, sop value); + */ +static void +dofwd( + struct parse *p, + sopno pos, + sopno value) +{ + + _DIAGASSERT(p != NULL); + + /* avoid making error situations worse */ + if (p->error != 0) + return; + + assert(value < 1<strip[pos] = OP(p->strip[pos]) | value; +} + +/* + - enlarge - enlarge the strip + == static void enlarge(struct parse *p, sopno size); + */ +static void +enlarge( + struct parse *p, + sopno size) +{ + sop *sp; + + _DIAGASSERT(p != NULL); + + if (p->ssize >= size) + return; + + sp = (sop *)realloc(p->strip, size*sizeof(sop)); + if (sp == NULL) { + SETERROR(REG_ESPACE); + return; + } + p->strip = sp; + p->ssize = size; +} + +/* + - stripsnug - compact the strip + == static void stripsnug(struct parse *p, struct re_guts *g); + */ +static void +stripsnug( + struct parse *p, + struct re_guts *g) +{ + + _DIAGASSERT(p != NULL); + _DIAGASSERT(g != NULL); + + g->nstates = p->slen; + g->strip = realloc(p->strip, p->slen * sizeof(sop)); + if (g->strip == NULL) { + SETERROR(REG_ESPACE); + g->strip = p->strip; + } +} + +/* + - findmust - fill in must and mlen with longest mandatory literal string + == static void findmust(struct parse *p, struct re_guts *g); + * + * This algorithm could do fancy things like analyzing the operands of | + * for common subsequences. Someday. This code is simple and finds most + * of the interesting cases. + * + * Note that must and mlen got initialized during setup. + */ +static void +findmust( + struct parse *p, + struct re_guts *g) +{ + sop *scan; + sop *start = NULL; + sop *newstart = NULL; + sopno newlen; + sop s; + char *cp; + sopno i; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(g != NULL); + + /* avoid making error situations worse */ + if (p->error != 0) + return; + + /* find the longest OCHAR sequence in strip */ + newlen = 0; + scan = g->strip + 1; + do { + s = *scan++; + switch (OP(s)) { + case OCHAR: /* sequence member */ + if (newlen == 0) /* new sequence */ + newstart = scan - 1; + newlen++; + break; + case OPLUS_: /* things that don't break one */ + case OLPAREN: + case ORPAREN: + break; + case OQUEST_: /* things that must be skipped */ + case OCH_: + scan--; + do { + scan += OPND(s); + s = *scan; + /* assert() interferes w debug printouts */ + if (OP(s) != O_QUEST && OP(s) != O_CH && + OP(s) != OOR2) { + g->iflags |= BAD; + return; + } + } while (OP(s) != O_QUEST && OP(s) != O_CH); + /* FALLTHROUGH */ + default: /* things that break a sequence */ + if (newlen > g->mlen) { /* ends one */ + start = newstart; + g->mlen = newlen; + } + newlen = 0; + break; + } + } while (OP(s) != OEND); + + if (start == NULL) + g->mlen = 0; + + if (g->mlen == 0) /* there isn't one */ + return; + + /* turn it into a character string */ + g->must = malloc((size_t)g->mlen + 1); + if (g->must == NULL) { /* argh; just forget it */ + g->mlen = 0; + return; + } + cp = g->must; + scan = start; + for (i = g->mlen; i > 0; i--) { + while (OP(s = *scan++) != OCHAR) + continue; + assert(cp < g->must + g->mlen); + *cp++ = (char)OPND(s); + } + assert(cp == g->must + g->mlen); + *cp++ = '\0'; /* just on general principles */ +} + +/* + - pluscount - count + nesting + == static sopno pluscount(struct parse *p, struct re_guts *g); + */ +static sopno /* nesting depth */ +pluscount( + struct parse *p, + struct re_guts *g) +{ + sop *scan; + sop s; + sopno plusnest = 0; + sopno maxnest = 0; + + _DIAGASSERT(p != NULL); + _DIAGASSERT(g != NULL); + + if (p->error != 0) + return(0); /* there may not be an OEND */ + + scan = g->strip + 1; + do { + s = *scan++; + switch (OP(s)) { + case OPLUS_: + plusnest++; + break; + case O_PLUS: + if (plusnest > maxnest) + maxnest = plusnest; + plusnest--; + break; + } + } while (OP(s) != OEND); + if (plusnest != 0) + g->iflags |= BAD; + return(maxnest); +} diff --git a/lib/nbsd_libc/regex/regerror.c b/lib/nbsd_libc/regex/regerror.c new file mode 100644 index 000000000..e00d7c035 --- /dev/null +++ b/lib/nbsd_libc/regex/regerror.c @@ -0,0 +1,223 @@ +/* $NetBSD: regerror.c,v 1.23 2007/02/09 23:44:18 junyoung Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)regerror.c 8.4 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)regerror.c 8.4 (Berkeley) 3/20/94 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)regerror.c 8.4 (Berkeley) 3/20/94"; +#else +__RCSID("$NetBSD: regerror.c,v 1.23 2007/02/09 23:44:18 junyoung Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(regerror,_regerror) +#endif + +#include "utils.h" + +/* ========= begin header generated by ./mkh ========= */ +#ifdef __cplusplus +extern "C" { +#endif + +/* === regerror.c === */ +static const char *regatoi(const regex_t *preg, char *localbuf, size_t buflen); + +#ifdef __cplusplus +} +#endif +/* ========= end header generated by ./mkh ========= */ +/* + = #define REG_NOMATCH 1 + = #define REG_BADPAT 2 + = #define REG_ECOLLATE 3 + = #define REG_ECTYPE 4 + = #define REG_EESCAPE 5 + = #define REG_ESUBREG 6 + = #define REG_EBRACK 7 + = #define REG_EPAREN 8 + = #define REG_EBRACE 9 + = #define REG_BADBR 10 + = #define REG_ERANGE 11 + = #define REG_ESPACE 12 + = #define REG_BADRPT 13 + = #define REG_EMPTY 14 + = #define REG_ASSERT 15 + = #define REG_INVARG 16 + = #define REG_ATOI 255 // convert name to number (!) + = #define REG_ITOA 0400 // convert number to name (!) + */ +static const struct rerr { + int code; + const char *name; + const char *explain; +} rerrs[] = { + { REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match" }, + { REG_BADPAT, "REG_BADPAT", "invalid regular expression" }, + { REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element" }, + { REG_ECTYPE, "REG_ECTYPE", "invalid character class" }, + { REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)" }, + { REG_ESUBREG, "REG_ESUBREG", "invalid backreference number" }, + { REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced" }, + { REG_EPAREN, "REG_EPAREN", "parentheses not balanced" }, + { REG_EBRACE, "REG_EBRACE", "braces not balanced" }, + { REG_BADBR, "REG_BADBR", "invalid repetition count(s)" }, + { REG_ERANGE, "REG_ERANGE", "invalid character range" }, + { REG_ESPACE, "REG_ESPACE", "out of memory" }, + { REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid" }, + { REG_EMPTY, "REG_EMPTY", "empty (sub)expression" }, + { REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug" }, + { REG_INVARG, "REG_INVARG", "invalid argument to regex routine" }, + { 0, "", "*** unknown regexp error code ***" } +}; + +/* + * regerror - the interface to error numbers + * extern size_t regerror(int, const regex_t *, char *, size_t); + */ +/* ARGSUSED */ +size_t +regerror( + int errcode, + const regex_t *preg, + char *errbuf, + size_t errbuf_size) +{ + const struct rerr *r; + size_t len; + int target = errcode &~ REG_ITOA; + const char *s; + char convbuf[50]; + + _DIAGASSERT(errcode != REG_ATOI || preg != NULL); + _DIAGASSERT(errbuf != NULL); + + if (errcode == REG_ATOI) + s = regatoi(preg, convbuf, sizeof convbuf); + else { + for (r = rerrs; r->code != 0; r++) + if (r->code == target) + break; + + if (errcode & REG_ITOA) { + if (r->code != 0) { + (void)strlcpy(convbuf, r->name, sizeof convbuf); + } else + (void)snprintf(convbuf, sizeof convbuf, + "REG_0x%x", target); + s = convbuf; + } else + s = r->explain; + } + + len = strlen(s) + 1; + if (errbuf_size > 0) + (void)strlcpy(errbuf, s, errbuf_size); + + return(len); +} + +/* + * regatoi - internal routine to implement REG_ATOI + * static const char *regatoi(const regex_t *preg, char *localbuf, + * size_t buflen); + */ +static const char * +regatoi( + const regex_t *preg, + char *localbuf, + size_t buflen) +{ + const struct rerr *r; + + for (r = rerrs; r->code != 0; r++) + if (strcmp(r->name, preg->re_endp) == 0) + break; + if (r->code == 0) + return "0"; + + (void)snprintf(localbuf, buflen, "%d", r->code); + return localbuf; +} diff --git a/lib/nbsd_libc/regex/regex.3 b/lib/nbsd_libc/regex/regex.3 new file mode 100644 index 000000000..dae8e173b --- /dev/null +++ b/lib/nbsd_libc/regex/regex.3 @@ -0,0 +1,631 @@ +.\" $NetBSD: regex.3,v 1.21 2010/03/22 19:30:54 joerg Exp $ +.\" +.\" Copyright (c) 1992, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Henry Spencer. +.\" +.\" 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. +.\" +.\" Copyright (c) 1992, 1993, 1994 Henry Spencer. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Henry Spencer. +.\" +.\" 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 by the University of +.\" California, Berkeley and its contributors. +.\" 4. 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. +.\" +.\" @(#)regex.3 8.4 (Berkeley) 3/20/94 +.\" +.Dd December 29, 2003 +.Dt REGEX 3 +.Os +.Sh NAME +.Nm regex , +.Nm regcomp , +.Nm regexec , +.Nm regerror , +.Nm regfree +.Nd regular-expression library +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In regex.h +.Ft int +.Fn regcomp "regex_t * restrict preg" "const char * restrict pattern" "int cflags" +.Ft int +.Fn regexec "const regex_t * restrict preg" "const char * restrict string" "size_t nmatch" "regmatch_t pmatch[]" "int eflags" +.Ft size_t +.Fn regerror "int errcode" "const regex_t * restrict preg" "char * restrict errbuf" "size_t errbuf_size" +.Ft void +.Fn regfree "regex_t *preg" +.Sh DESCRIPTION +These routines implement +.St -p1003.2-92 +regular expressions (``RE''s); +see +.Xr re_format 7 . +.Fn regcomp +compiles an RE written as a string into an internal form, +.Fn regexec +matches that internal form against a string and reports results, +.Fn regerror +transforms error codes from either into human-readable messages, +and +.Fn regfree +frees any dynamically-allocated storage used by the internal form +of an RE. +.Pp +The header +.In regex.h +declares two structure types, +.Fa regex_t +and +.Fa regmatch_t , +the former for compiled internal forms and the latter for match reporting. +It also declares the four functions, +a type +.Fa regoff_t , +and a number of constants with names starting with ``REG_''. +.Pp +.Fn regcomp +compiles the regular expression contained in the +.Fa pattern +string, +subject to the flags in +.Fa cflags , +and places the results in the +.Fa regex_t +structure pointed to by +.Fa preg . +.Fa cflags +is the bitwise OR of zero or more of the following flags: +.Bl -tag -width XXXREG_EXTENDED +.It Dv REG_EXTENDED +Compile modern (``extended'') REs, rather than the obsolete +(``basic'') REs that are the default. +.It Dv REG_BASIC +This is a synonym for 0, +provided as a counterpart to REG_EXTENDED to improve readability. +.It Dv REG_NOSPEC +Compile with recognition of all special characters turned off. +All characters are thus considered ordinary, so the ``RE'' is a literal +string. +This is an extension, compatible with but not specified by +.St -p1003.2-92 , +and should be used with caution in software intended to be portable to +other systems. +.Dv REG_EXTENDED +and +.Dv REG_NOSPEC +may not be used in the same call to +.Fn regcomp . +.It Dv REG_ICASE +Compile for matching that ignores upper/lower case distinctions. +See +.Xr re_format 7 . +.It Dv REG_NOSUB +Compile for matching that need only report success or failure, not +what was matched. +.It Dv REG_NEWLINE +Compile for newline-sensitive matching. +By default, newline is a completely ordinary character with no special +meaning in either REs or strings. +With this flag, +`[^' bracket expressions and `.' never match newline, +a `^' anchor matches the null string after any newline in the string +in addition to its normal function, +and the `$' anchor matches the null string before any newline in the +string in addition to its normal function. +.It Dv REG_PEND +The regular expression ends, not at the first NUL, but just before the +character pointed to by the +.Fa re_endp +member of the structure pointed to by +.Fa preg . +The +.Fa re_endp +member is of type +.Fa "const\ char\ *" . +This flag permits inclusion of NULs in the RE; they are considered +ordinary characters. +This is an extension, compatible with but not specified by +.St -p1003.2-92 , +and should be used with caution in software intended to be portable to +other systems. +.El +.Pp +When successful, +.Fn regcomp +returns 0 and fills in the structure pointed to by +.Fa preg . +One member of that structure (other than +.Fa re_endp ) +is publicized: +.Fa re_nsub , +of type +.Fa size_t , +contains the number of parenthesized subexpressions within the RE +(except that the value of this member is undefined if the +.Dv REG_NOSUB +flag was used). +If +.Fn regcomp +fails, it returns a non-zero error code; +see +.Sx DIAGNOSTICS . +.Pp +.Fn regexec +matches the compiled RE pointed to by +.Fa preg +against the +.Fa string , +subject to the flags in +.Fa eflags , +and reports results using +.Fa nmatch , +.Fa pmatch , +and the returned value. +The RE must have been compiled by a previous invocation of +.Fn regcomp . +The compiled form is not altered during execution of +.Fn regexec , +so a single compiled RE can be used simultaneously by multiple threads. +.Pp +By default, +the NUL-terminated string pointed to by +.Fa string +is considered to be the text of an entire line, minus any terminating +newline. +The +.Fa eflags +argument is the bitwise OR of zero or more of the following flags: +.Bl -tag -width XXXREG_NOTBOL +.It Dv REG_NOTBOL +The first character of the string +is not the beginning of a line, so the `^' anchor should not match before it. +This does not affect the behavior of newlines under +.Dv REG_NEWLINE . +.It Dv REG_NOTEOL +The NUL terminating the string does not end a line, so the `$' anchor +should not match before it. +This does not affect the behavior of newlines under +.Dv REG_NEWLINE . +.It Dv REG_STARTEND +The string is considered to start at +.Fa string ++ +.Fa pmatch[0].rm_so +and to have a terminating NUL located at +.Fa string ++ +.Fa pmatch[0].rm_eo +(there need not actually be a NUL at that location), +regardless of the value of +.Fa nmatch . +See below for the definition of +.Fa pmatch +and +.Fa nmatch . +This is an extension, compatible with but not specified by +.St -p1003.2-92 , +and should be used with caution in software intended to be portable to +other systems. +Note that a non-zero +.Fa rm_so +does not imply +.Dv REG_NOTBOL ; +.Dv REG_STARTEND +affects only the location of the string, not how it is matched. +.El +.Pp +See +.Xr re_format 7 +for a discussion of what is matched in situations where an RE or a +portion thereof could match any of several substrings of +.Fa string . +.Pp +Normally, +.Fn regexec +returns 0 for success and the non-zero code +.Dv REG_NOMATCH +for failure. +Other non-zero error codes may be returned in exceptional situations; +see +.Sx DIAGNOSTICS . +.Pp +If +.Dv REG_NOSUB +was specified in the compilation of the RE, or if +.Fa nmatch +is 0, +.Fn regexec +ignores the +.Fa pmatch +argument (but see below for the case where +.Dv REG_STARTEND +is specified). +Otherwise, +.Fa pmatch +points to an array of +.Fa nmatch +structures of type +.Fa regmatch_t . +Such a structure has at least the members +.Fa rm_so +and +.Fa rm_eo , +both of type +.Fa regoff_t +(a signed arithmetic type at least as large as an +.Fa off_t +and a +.Fa ssize_t ) , +containing respectively the offset of the first character of a substring +and the offset of the first character after the end of the substring. +Offsets are measured from the beginning of the +.Fa string +argument given to +.Fn regexec . +An empty substring is denoted by equal offsets, +both indicating the character following the empty substring. +.Pp +The 0th member of the +.Fa pmatch +array is filled in to indicate what substring of +.Fa string +was matched by the entire RE. +Remaining members report what substring was matched by parenthesized +subexpressions within the RE; +member +.Fa i +reports subexpression +.Fa i , +with subexpressions counted (starting at 1) by the order of their +opening parentheses in the RE, left to right. +Unused entries in the array\(emcorresponding either to subexpressions that +did not participate in the match at all, or to subexpressions that do not +exist in the RE (that is, +.Fa i +\*[Gt] +.Fa preg-\*[Gt]re_nsub ) +\(emhave both +.Fa rm_so +and +.Fa rm_eo +set to -1. +If a subexpression participated in the match several times, +the reported substring is the last one it matched. +(Note, as an example in particular, that when the RE `(b*)+' matches `bbb', +the parenthesized subexpression matches each of the three `b's and then +an infinite number of empty strings following the last `b', +so the reported substring is one of the empties.) +.Pp +If +.Dv REG_STARTEND +is specified, +.Fa pmatch +must point to at least one +.Fa regmatch_t +(even if +.Fa nmatch +is 0 or +.Dv REG_NOSUB +was specified), +to hold the input offsets for +.Dv REG_STARTEND . +Use for output is still entirely controlled by +.Fa nmatch ; +if +.Fa nmatch +is 0 or +.Dv REG_NOSUB +was specified, +the value of +.Fa pmatch [0] +will not be changed by a successful +.Fn regexec . +.Pp +.Fn regerror +maps a non-zero +.Fa errcode +from either +.Fn regcomp +or +.Fn regexec +to a human-readable, printable message. +If +.Fa preg +is non-NULL, +the error code should have arisen from use of the +.Fa regex_t +pointed to by +.Fa preg , +and if the error code came from +.Fn regcomp , +it should have been the result from the most recent +.Fn regcomp +using that +.Fa regex_t . ( +.Fn regerror +may be able to supply a more detailed message using information +from the +.Fa regex_t . ) +.Fn regerror +places the NUL-terminated message into the buffer pointed to by +.Fa errbuf , +limiting the length (including the NUL) to at most +.Fa errbuf_size +bytes. +If the whole message won't fit, +as much of it as will fit before the terminating NUL is supplied. +In any case, +the returned value is the size of buffer needed to hold the whole +message (including terminating NUL). +If +.Fa errbuf_size +is 0, +.Fa errbuf +is ignored but the return value is still correct. +.Pp +If the +.Fa errcode +given to +.Fn regerror +is first ORed with +.Dv REG_ITOA , +the ``message'' that results is the printable name of the error code, +e.g. ``REG_NOMATCH'', +rather than an explanation thereof. +If +.Fa errcode +is +.Dv REG_ATOI , +then +.Fa preg +shall be non-NULL and the +.Fa re_endp +member of the structure it points to +must point to the printable name of an error code; +in this case, the result in +.Fa errbuf +is the decimal digits of +the numeric value of the error code +(0 if the name is not recognized). +.Dv REG_ITOA +and +.Dv REG_ATOI +are intended primarily as debugging facilities; +they are extensions, compatible with but not specified by +.St -p1003.2-92 , +and should be used with caution in software intended to be portable to +other systems. +Be warned also that they are considered experimental and changes are possible. +.Pp +.Fn regfree +frees any dynamically-allocated storage associated with the compiled RE +pointed to by +.Fa preg . +The remaining +.Fa regex_t +is no longer a valid compiled RE +and the effect of supplying it to +.Fn regexec +or +.Fn regerror +is undefined. +.Pp +None of these functions references global variables except for tables +of constants; +all are safe for use from multiple threads if the arguments are safe. +.Sh IMPLEMENTATION CHOICES +There are a number of decisions that +.St -p1003.2-92 +leaves up to the implementor, +either by explicitly saying ``undefined'' or by virtue of them being +forbidden by the RE grammar. +This implementation treats them as follows. +.Pp +See +.Xr re_format 7 +for a discussion of the definition of case-independent matching. +.Pp +There is no particular limit on the length of REs, +except insofar as memory is limited. +Memory usage is approximately linear in RE size, and largely insensitive +to RE complexity, except for bounded repetitions. +See BUGS for one short RE using them +that will run almost any system out of memory. +.Pp +A backslashed character other than one specifically given a magic meaning +by +.St -p1003.2-92 +(such magic meanings occur only in obsolete [``basic''] REs) +is taken as an ordinary character. +.Pp +Any unmatched [ is a +.Dv REG_EBRACK +error. +.Pp +Equivalence classes cannot begin or end bracket-expression ranges. +The endpoint of one range cannot begin another. +.Pp +.Dv RE_DUP_MAX , +the limit on repetition counts in bounded repetitions, is 255. +.Pp +A repetition operator (?, *, +, or bounds) cannot follow another +repetition operator. +A repetition operator cannot begin an expression or subexpression +or follow `^' or `|'. +.Pp +`|' cannot appear first or last in a (sub)expression or after another `|', +i.e. an operand of `|' cannot be an empty subexpression. +An empty parenthesized subexpression, `()', is legal and matches an +empty (sub)string. +An empty string is not a legal RE. +.Pp +A `{' followed by a digit is considered the beginning of bounds for a +bounded repetition, which must then follow the syntax for bounds. +A `{' +.Em not +followed by a digit is considered an ordinary character. +.Pp +`^' and `$' beginning and ending subexpressions in obsolete (``basic'') +REs are anchors, not ordinary characters. +.Sh DIAGNOSTICS +Non-zero error codes from +.Fn regcomp +and +.Fn regexec +include the following: +.Pp +.Bl -tag -width XXXREG_ECOLLATE -compact +.It Dv REG_NOMATCH +.Fn regexec +failed to match +.It Dv REG_BADPAT +invalid regular expression +.It Dv REG_ECOLLATE +invalid collating element +.It Dv REG_ECTYPE +invalid character class +.It Dv REG_EESCAPE +\e applied to unescapable character +.It Dv REG_ESUBREG +invalid backreference number +.It Dv REG_EBRACK +brackets [ ] not balanced +.It Dv REG_EPAREN +parentheses ( ) not balanced +.It Dv REG_EBRACE +braces { } not balanced +.It Dv REG_BADBR +invalid repetition count(s) in { } +.It Dv REG_ERANGE +invalid character range in [ ] +.It Dv REG_ESPACE +ran out of memory +.It Dv REG_BADRPT +?, *, or + operand invalid +.It Dv REG_EMPTY +empty (sub)expression +.It Dv REG_ASSERT +``can't happen''\(emyou found a bug +.It Dv REG_INVARG +invalid argument, e.g. negative-length string +.El +.Sh SEE ALSO +.Xr grep 1 , +.Xr sed 1 , +.Xr re_format 7 +.Pp +.St -p1003.2-92 , +sections 2.8 (Regular Expression Notation) +and +B.5 (C Binding for Regular Expression Matching). +.Sh HISTORY +Originally written by Henry Spencer. +Altered for inclusion in the +.Bx 4.4 +distribution. +.Sh BUGS +There is one known functionality bug. +The implementation of internationalization is incomplete: +the locale is always assumed to be the default one of +.St -p1003.2-92 , +and only the collating elements etc. of that locale are available. +.Pp +The back-reference code is subtle and doubts linger about its correctness +in complex cases. +.Pp +.Fn regexec +performance is poor. +This will improve with later releases. +.Fa nmatch +exceeding 0 is expensive; +.Fa nmatch +exceeding 1 is worse. +.Fa regexec +is largely insensitive to RE complexity +.Em except +that back references are massively expensive. +RE length does matter; in particular, there is a strong speed bonus +for keeping RE length under about 30 characters, +with most special characters counting roughly double. +.Pp +.Fn regcomp +implements bounded repetitions by macro expansion, +which is costly in time and space if counts are large +or bounded repetitions are nested. +An RE like, say, +`((((a{1,100}){1,100}){1,100}){1,100}){1,100}' +will (eventually) run almost any existing machine out of swap space. +.Pp +There are suspected problems with response to obscure error conditions. +Notably, +certain kinds of internal overflow, +produced only by truly enormous REs or by multiply nested bounded repetitions, +are probably not handled well. +.Pp +Due to a mistake in +.St -p1003.2-92 , +things like `a)b' are legal REs because `)' is a special character +only in the presence of a previous unmatched `('. +This can't be fixed until the spec is fixed. +.Pp +The standard's definition of back references is vague. +For example, does +`a\e(\e(b\e)*\e2\e)*d' match `abbbd'? +Until the standard is clarified, behavior in such cases should not be +relied on. +.Pp +The implementation of word-boundary matching is a bit of a kludge, +and bugs may lurk in combinations of word-boundary matching and anchoring. diff --git a/lib/nbsd_libc/regex/regex2.h b/lib/nbsd_libc/regex/regex2.h new file mode 100644 index 000000000..0fdbe74c4 --- /dev/null +++ b/lib/nbsd_libc/regex/regex2.h @@ -0,0 +1,209 @@ +/* $NetBSD: regex2.h,v 1.12 2009/02/12 05:06:54 lukem Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)regex2.h 8.4 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)regex2.h 8.4 (Berkeley) 3/20/94 + */ + +/* + * First, the stuff that ends up in the outside-world include file + = typedef off_t regoff_t; + = typedef struct { + = int re_magic; + = size_t re_nsub; // number of parenthesized subexpressions + = const char *re_endp; // end pointer for REG_PEND + = struct re_guts *re_g; // none of your business :-) + = } regex_t; + = typedef struct { + = regoff_t rm_so; // start of match + = regoff_t rm_eo; // end of match + = } regmatch_t; + */ +/* + * internals of regex_t + */ +#define MAGIC1 ((('r'^0200)<<8) | 'e') + +/* + * The internal representation is a *strip*, a sequence of + * operators ending with an endmarker. (Some terminology etc. is a + * historical relic of earlier versions which used multiple strips.) + * Certain oddities in the representation are there to permit running + * the machinery backwards; in particular, any deviation from sequential + * flow must be marked at both its source and its destination. Some + * fine points: + * + * - OPLUS_ and O_PLUS are *inside* the loop they create. + * - OQUEST_ and O_QUEST are *outside* the bypass they create. + * - OCH_ and O_CH are *outside* the multi-way branch they create, while + * OOR1 and OOR2 are respectively the end and the beginning of one of + * the branches. Note that there is an implicit OOR2 following OCH_ + * and an implicit OOR1 preceding O_CH. + * + * In state representations, an operator's bit is on to signify a state + * immediately *preceding* "execution" of that operator. + */ +typedef u_int32_t sop; /* strip operator */ +typedef int sopno; +#define OPRMASK ((u_int32_t)0xf8000000UL) +#define OPDMASK ((u_int32_t)0x07ffffffUL) +#define OPSHIFT ((unsigned)27) +#define OP(n) ((n)&OPRMASK) +#define OPND(n) ((int)((n)&OPDMASK)) +#define SOP(op, opnd) ((op)|(opnd)) + +#define OPC(n) (((u_int32_t)(n))< uch [csetsize] */ + uch mask; /* bit within array */ + uch hash; /* hash code */ + size_t smultis; + char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */ +} cset; +/* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */ +#define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c)) +#define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c)) +#define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask) +#define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */ +#define MCsub(p, cs, cp) mcsub(p, cs, cp) +#define MCin(p, cs, cp) mcin(p, cs, cp) + +/* stuff for character categories */ +typedef unsigned char cat_t; + +/* + * main compiled-expression structure + */ +struct re_guts { + int magic; +# define MAGIC2 ((('R'^0200)<<8)|'E') + sop *strip; /* malloced area for strip */ + int csetsize; /* number of bits in a cset vector */ + int ncsets; /* number of csets in use */ + cset *sets; /* -> cset [ncsets] */ + uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */ + int cflags; /* copy of regcomp() cflags argument */ + sopno nstates; /* = number of sops */ + sopno firststate; /* the initial OEND (normally 0) */ + sopno laststate; /* the final OEND */ + int iflags; /* internal flags */ +# define USEBOL 01 /* used ^ */ +# define USEEOL 02 /* used $ */ +# define BAD 04 /* something wrong */ + int nbol; /* number of ^ used */ + int neol; /* number of $ used */ + int ncategories; /* how many character categories */ + cat_t *categories; /* ->catspace[-CHAR_MIN] */ + char *must; /* match must contain this string */ + int mlen; /* length of must */ + size_t nsub; /* copy of re_nsub */ + int backrefs; /* does it use back references? */ + sopno nplus; /* how deep does it nest +s? */ + /* catspace must be last */ + cat_t catspace[1]; /* actually [NC] */ +}; + +/* misc utilities */ +#define OUT (CHAR_MAX+1) /* a non-character value */ +#define ISWORD(c) (isalnum((unsigned char)c) || (c) == '_') diff --git a/lib/nbsd_libc/regex/regexec.c b/lib/nbsd_libc/regex/regexec.c new file mode 100644 index 000000000..d0e10842f --- /dev/null +++ b/lib/nbsd_libc/regex/regexec.c @@ -0,0 +1,234 @@ +/* $NetBSD: regexec.c,v 1.21 2009/02/12 05:06:54 lukem Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)regexec.c 8.3 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)regexec.c 8.3 (Berkeley) 3/20/94 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94"; +#else +__RCSID("$NetBSD: regexec.c,v 1.21 2009/02/12 05:06:54 lukem Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * the outer shell of regexec() + * + * This file includes engine.c *twice*, after muchos fiddling with the + * macros that code uses. This lets the same code operate on two different + * representations for state sets. + */ +#include "namespace.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(regexec,_regexec) +#endif + +#include "utils.h" +#include "regex2.h" + +/* macros for manipulating states, small version */ +#define states unsigned long +#define states1 unsigned long /* for later use in regexec() decision */ +#define CLEAR(v) ((v) = 0) +#define SET0(v, n) ((v) &= ~((unsigned long)1 << (n))) +#define SET1(v, n) ((v) |= (unsigned long)1 << (n)) +#define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0) +#define ASSIGN(d, s) ((d) = (s)) +#define EQ(a, b) ((a) == (b)) +#define STATEVARS int dummy /* dummy version */ +#define STATESETUP(m, n) /* nothing */ +#define STATETEARDOWN(m) /* nothing */ +#define SETUP(v) ((v) = 0) +#define onestate unsigned long +#define INIT(o, n) ((o) = (unsigned long)1 << (n)) +#define INC(o) ((o) <<= 1) +#define ISSTATEIN(v, o) (((v) & (o)) != 0) +/* some abbreviations; note that some of these know variable names! */ +/* do "if I'm here, I can also be there" etc without branches */ +#define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n)) +#define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n)) +#define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0) +/* function names */ +#define SNAMES /* engine.c looks after details */ + +#include "engine.c" + +/* now undo things */ +#undef states +#undef CLEAR +#undef SET0 +#undef SET1 +#undef ISSET +#undef ASSIGN +#undef EQ +#undef STATEVARS +#undef STATESETUP +#undef STATETEARDOWN +#undef SETUP +#undef onestate +#undef INIT +#undef INC +#undef ISSTATEIN +#undef FWD +#undef BACK +#undef ISSETBACK +#undef SNAMES + +/* macros for manipulating states, large version */ +#define states char * +#define CLEAR(v) memset(v, 0, (size_t)m->g->nstates) +#define SET0(v, n) ((v)[n] = 0) +#define SET1(v, n) ((v)[n] = 1) +#define ISSET(v, n) ((v)[n]) +#define ASSIGN(d, s) memcpy(d, s, (size_t)m->g->nstates) +#define EQ(a, b) (memcmp(a, b, (size_t)m->g->nstates) == 0) +#define STATEVARS int vn; char *space +#define STATESETUP(m, nv) \ + if (((m)->space = malloc((size_t)((nv)*(m)->g->nstates))) == NULL) \ + return(REG_ESPACE); \ + else \ + (m)->vn = 0 + +#define STATETEARDOWN(m) { free((m)->space); m->space = NULL; } +#define SETUP(v) ((v) = &m->space[(size_t)(m->vn++ * m->g->nstates)]) +#define onestate int +#define INIT(o, n) ((o) = (n)) +#define INC(o) ((o)++) +#define ISSTATEIN(v, o) ((v)[o]) +/* some abbreviations; note that some of these know variable names! */ +/* do "if I'm here, I can also be there" etc without branches */ +#define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) +#define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) +#define ISSETBACK(v, n) ((v)[here - (n)]) +/* function names */ +#define LNAMES /* flag */ + +#include "engine.c" + +/* + - regexec - interface for matching + = extern int regexec(const regex_t *, const char *, size_t, \ + = regmatch_t [], int); + = #define REG_NOTBOL 00001 + = #define REG_NOTEOL 00002 + = #define REG_STARTEND 00004 + = #define REG_TRACE 00400 // tracing of execution + = #define REG_LARGE 01000 // force large representation + = #define REG_BACKR 02000 // force use of backref code + * + * We put this here so we can exploit knowledge of the state representation + * when choosing which matcher to call. Also, by this point the matchers + * have been prototyped. + */ +int /* 0 success, REG_NOMATCH failure */ +regexec( + const regex_t *preg, + const char *string, + size_t nmatch, + regmatch_t pmatch[], + int eflags) +{ + struct re_guts *g = preg->re_g; + char *s; +#ifdef REDEBUG +# define GOODFLAGS(f) (f) +#else +# define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) +#endif + + _DIAGASSERT(preg != NULL); + _DIAGASSERT(string != NULL); + + if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) + return(REG_BADPAT); + assert(!(g->iflags&BAD)); + if (g->iflags&BAD) /* backstop for no-debug case */ + return(REG_BADPAT); + eflags = GOODFLAGS(eflags); + + s = __UNCONST(string); + + if (g->nstates <= (sopno)(CHAR_BIT*sizeof(states1)) && !(eflags®_LARGE)) + return(smatcher(g, s, nmatch, pmatch, eflags)); + else + return(lmatcher(g, s, nmatch, pmatch, eflags)); +} diff --git a/lib/nbsd_libc/regex/regfree.c b/lib/nbsd_libc/regex/regfree.c new file mode 100644 index 000000000..ce011eade --- /dev/null +++ b/lib/nbsd_libc/regex/regfree.c @@ -0,0 +1,129 @@ +/* $NetBSD: regfree.c,v 1.15 2007/02/09 23:44:18 junyoung Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)regfree.c 8.3 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)regfree.c 8.3 (Berkeley) 3/20/94 + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)regfree.c 8.3 (Berkeley) 3/20/94"; +#else +__RCSID("$NetBSD: regfree.c,v 1.15 2007/02/09 23:44:18 junyoung Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(regfree,_regfree) +#endif + +#include "utils.h" +#include "regex2.h" + +/* + - regfree - free everything + = extern void regfree(regex_t *); + */ +void +regfree( + regex_t *preg) +{ + struct re_guts *g; + + _DIAGASSERT(preg != NULL); + + _DIAGASSERT(preg->re_magic == MAGIC1); + if (preg->re_magic != MAGIC1) /* oops */ + return; /* nice to complain, but hard */ + + g = preg->re_g; + if (g == NULL || g->magic != MAGIC2) /* oops again */ + return; + preg->re_magic = 0; /* mark it invalid */ + g->magic = 0; /* mark it invalid */ + + if (g->strip != NULL) + free(g->strip); + if (g->sets != NULL) + free(g->sets); + if (g->setbits != NULL) + free(g->setbits); + if (g->must != NULL) + free(g->must); + free(g); +} diff --git a/lib/nbsd_libc/regex/utils.h b/lib/nbsd_libc/regex/utils.h new file mode 100644 index 000000000..762caeeb6 --- /dev/null +++ b/lib/nbsd_libc/regex/utils.h @@ -0,0 +1,91 @@ +/* $NetBSD: utils.h,v 1.6 2003/08/07 16:43:21 agc Exp $ */ + +/*- + * Copyright (c) 1992, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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. + * + * @(#)utils.h 8.3 (Berkeley) 3/20/94 + */ + +/*- + * Copyright (c) 1992, 1993, 1994 Henry Spencer. + * + * This code is derived from software contributed to Berkeley by + * Henry Spencer. + * + * 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 by the University of + * California, Berkeley and its contributors. + * 4. 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. + * + * @(#)utils.h 8.3 (Berkeley) 3/20/94 + */ + +/* utility definitions */ +#define DUPMAX _POSIX2_RE_DUP_MAX /* xxx is this right? */ +#define INFINITY (DUPMAX + 1) +#define NC (CHAR_MAX - CHAR_MIN + 1) +typedef unsigned char uch; + +/* switch off assertions (if not already off) if no REDEBUG */ +#ifndef REDEBUG +#ifndef NDEBUG +#define NDEBUG /* no assertions please */ +#endif +#endif +#include + +/* for old systems with bcopy() but no memmove() */ +#ifdef USEBCOPY +#define memmove(d, s, c) bcopy(s, d, c) +#endif diff --git a/lib/nbsd_libc/resolv/Makefile.inc b/lib/nbsd_libc/resolv/Makefile.inc new file mode 100644 index 000000000..6b5282925 --- /dev/null +++ b/lib/nbsd_libc/resolv/Makefile.inc @@ -0,0 +1,12 @@ +# $NetBSD: Makefile.inc,v 1.4 2007/01/28 02:14:05 christos Exp $ + +# net sources +.PATH: ${.CURDIR}/resolv +CPPFLAGS+=-DCOMPAT__RES -DUSE_POLL + +SRCS+= __dn_comp.c __res_close.c __res_send.c h_errno.c \ + herror.c res_comp.c res_data.c res_debug.c \ + res_init.c res_mkquery.c res_query.c res_send.c \ + res_state.c mtctxres.c +# For COMPAT__RES +SRCS+= res_compat.c diff --git a/lib/nbsd_libc/resolv/__dn_comp.c b/lib/nbsd_libc/resolv/__dn_comp.c new file mode 100644 index 000000000..f8403315e --- /dev/null +++ b/lib/nbsd_libc/resolv/__dn_comp.c @@ -0,0 +1,34 @@ +/* $NetBSD: __dn_comp.c,v 1.5 2007/01/17 16:39:20 seanb Exp $ */ + +/* + * written by matthew green, 22/04/97. + * public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __dn_comp.c,v 1.5 2007/01/17 16:39:20 seanb Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(__dn_comp,dn_comp) +#else + +#include +#include +#include + +/* XXX THIS IS A MESS! SEE XXX */ + +#undef dn_comp +int dn_comp(const char *, u_char *, int, u_char **, u_char **); + +int +dn_comp(const char *exp_dn, u_char *comp_dn, int length, u_char **dnptrs, + u_char **lastdnptr) +{ + + return __dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr); +} + +#endif diff --git a/lib/nbsd_libc/resolv/__res_close.c b/lib/nbsd_libc/resolv/__res_close.c new file mode 100644 index 000000000..1d6d2ab23 --- /dev/null +++ b/lib/nbsd_libc/resolv/__res_close.c @@ -0,0 +1,33 @@ +/* $NetBSD: __res_close.c,v 1.4 2005/09/13 01:44:10 christos Exp $ */ + +/* + * written by matthew green, 22/04/97. + * public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __res_close.c,v 1.4 2005/09/13 01:44:10 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#if defined(__indr_reference) +__indr_reference(__res_close, res_close) +#else + +#include +#include +#include + +/* XXX THIS IS A MESS! SEE XXX */ + +#undef res_close +void res_close(void); + +void +res_close(void) +{ + + __res_close(); +} + +#endif diff --git a/lib/nbsd_libc/resolv/__res_send.c b/lib/nbsd_libc/resolv/__res_send.c new file mode 100644 index 000000000..af1ea36c4 --- /dev/null +++ b/lib/nbsd_libc/resolv/__res_send.c @@ -0,0 +1,33 @@ +/* $NetBSD: __res_send.c,v 1.4 2005/09/13 01:44:10 christos Exp $ */ + +/* + * written by matthew green, 22/04/97. + * public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: __res_send.c,v 1.4 2005/09/13 01:44:10 christos Exp $"); +#endif + +#if defined(__indr_reference) +__indr_reference(__res_send, res_send) +#else + +#include +#include +#include + +/* XXX THIS IS A MESS! SEE XXX */ + +#undef res_send +int res_send(const u_char *, int, u_char *, int); + +int +res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) +{ + + return __res_send(buf, buflen, ans, anssiz); +} + +#endif diff --git a/lib/nbsd_libc/resolv/h_errno.c b/lib/nbsd_libc/resolv/h_errno.c new file mode 100644 index 000000000..ec18d35f7 --- /dev/null +++ b/lib/nbsd_libc/resolv/h_errno.c @@ -0,0 +1,58 @@ +/* $NetBSD: h_errno.c,v 1.3 2008/06/21 20:41:48 christos Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: h_errno.c,v 1.3 2008/06/21 20:41:48 christos Exp $"); +#endif + +#include +#include +#include +#include +#include + +#undef h_errno + +extern int h_errno; +extern struct __res_state _nres; + +int * +__h_errno(void) +{ + return &_nres.res_h_errno; +} + +void +__h_errno_set(res_state res, int err) +{ + h_errno = res->res_h_errno = err; +} diff --git a/lib/nbsd_libc/resolv/herror.c b/lib/nbsd_libc/resolv/herror.c new file mode 100644 index 000000000..a1f0010dd --- /dev/null +++ b/lib/nbsd_libc/resolv/herror.c @@ -0,0 +1,140 @@ +/* $NetBSD: herror.c,v 1.8 2009/04/12 17:07:17 christos 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. 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. + */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Portions Copyright (c) 1996-1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#if defined(LIBC_SCCS) && !defined(lint) +#ifdef notdef +static const char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93"; +static const char rcsid[] = "Id: herror.c,v 1.4 2005/04/27 04:56:41 sra Exp"; +#else +__RCSID("$NetBSD: herror.c,v 1.8 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include "port_after.h" + +const char *h_errlist[] = { + "Resolver Error 0 (no error)", + "Unknown host", /*%< 1 HOST_NOT_FOUND */ + "Host name lookup failure", /*%< 2 TRY_AGAIN */ + "Unknown server error", /*%< 3 NO_RECOVERY */ + "No address associated with name", /*%< 4 NO_ADDRESS */ +}; +int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] }; + +#if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) +#undef h_errno +int h_errno; +#endif + +#ifdef __weak_alias +__weak_alias(herror,_herror) +#endif + +/*% + * herror -- + * print the error indicated by the h_errno value. + */ +void +herror(const char *s) { + struct iovec iov[4], *v = iov; + char *t; + + if (s != NULL && *s != '\0') { + DE_CONST(s, t); + v->iov_base = t; + v->iov_len = strlen(t); + v++; + DE_CONST(": ", t); + v->iov_base = t; + v->iov_len = 2; + v++; + } + DE_CONST(hstrerror(*__h_errno()), t); + v->iov_base = t; + v->iov_len = strlen(v->iov_base); + v++; + DE_CONST("\n", t); + v->iov_base = t; + v->iov_len = 1; + writev(STDERR_FILENO, iov, (v - iov) + 1); +} + +/*% + * hstrerror -- + * return the string associated with a given "host" errno value. + */ +const char * +hstrerror(int err) { + if (err < 0) + return ("Resolver internal error"); + else if (err < h_nerr) + return (h_errlist[err]); + return ("Unknown resolver error"); +} + +/*! \file */ diff --git a/lib/nbsd_libc/resolv/mtctxres.c b/lib/nbsd_libc/resolv/mtctxres.c new file mode 100644 index 000000000..6e3281a75 --- /dev/null +++ b/lib/nbsd_libc/resolv/mtctxres.c @@ -0,0 +1,130 @@ +/* $NetBSD: mtctxres.c,v 1.4 2007/03/30 20:40:52 ghen Exp $ */ + +#include +#ifdef DO_PTHREADS +#include +#endif +#include +#include +#include +#include +#include +#include + +#ifdef DO_PTHREADS +static pthread_key_t key; +static int mt_key_initialized = 0; + +static int __res_init_ctx(void); +static void __res_destroy_ctx(void *); + +#if defined(sun) && !defined(__GNUC__) +#pragma init (_mtctxres_init) +#endif +#endif + +static mtctxres_t sharedctx; + +#ifdef DO_PTHREADS +/* + * Initialize the TSD key. By doing this at library load time, we're + * implicitly running without interference from other threads, so there's + * no need for locking. + */ +static void +_mtctxres_init(void) { + int pthread_keycreate_ret; + + pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx); + if (pthread_keycreate_ret == 0) + mt_key_initialized = 1; +} +#endif + +/* + * To support binaries that used the private MT-safe interface in + * Solaris 8, we still need to provide the __res_enable_mt() + * and __res_disable_mt() entry points. They're do-nothing routines. + */ +int +__res_enable_mt(void) { + return (-1); +} + +int +__res_disable_mt(void) { + return (0); +} + +#ifdef DO_PTHREADS +static int +__res_init_ctx(void) { + + mtctxres_t *mt; + int ret; + + + if (pthread_getspecific(key) != 0) { + /* Already exists */ + return (0); + } + + if ((mt = malloc(sizeof (mtctxres_t))) == 0) { + errno = ENOMEM; + return (-1); + } + + memset(mt, 0, sizeof (mtctxres_t)); + + if ((ret = pthread_setspecific(key, mt)) != 0) { + free(mt); + errno = ret; + return (-1); + } + + return (0); +} + +static void +__res_destroy_ctx(void *value) { + + mtctxres_t *mt = (mtctxres_t *)value; + + if (mt != 0) + free(mt); +} +#endif + +mtctxres_t * +___mtctxres(void) { +#ifdef DO_PTHREADS + mtctxres_t *mt; + + /* + * This if clause should only be executed if we are linking + * statically. When linked dynamically _mtctxres_init() should + * be called at binding time due the #pragma above. + */ + if (!mt_key_initialized) { + static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER; + if (pthread_mutex_lock(&keylock) == 0) { + _mtctxres_init(); + (void) pthread_mutex_unlock(&keylock); + } + } + + /* + * If we have already been called in this thread return the existing + * context. Otherwise recreat a new context and return it. If + * that fails return a global context. + */ + if (mt_key_initialized) { + if (((mt = pthread_getspecific(key)) != 0) || + (__res_init_ctx() == 0 && + (mt = pthread_getspecific(key)) != 0)) { + return (mt); + } + } +#endif + return (&sharedctx); +} diff --git a/lib/nbsd_libc/resolv/res_comp.c b/lib/nbsd_libc/resolv/res_comp.c new file mode 100644 index 000000000..eaa4b2b7a --- /dev/null +++ b/lib/nbsd_libc/resolv/res_comp.c @@ -0,0 +1,285 @@ +/* $NetBSD: res_comp.c,v 1.10 2009/04/12 17:07:17 christos Exp $ */ + +/* + * Copyright (c) 1985, 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. 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. + */ + +/* + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * 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, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION 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. + */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Portions Copyright (c) 1996-1999 by Internet Software Consortium. + * + * 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 ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 +#if defined(LIBC_SCCS) && !defined(lint) +#ifdef notdef +static const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; +static const char rcsid[] = "Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp"; +#else +__RCSID("$NetBSD: res_comp.c,v 1.10 2009/04/12 17:07:17 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(dn_expand,_dn_expand) +__weak_alias(dn_comp,__dn_comp) +#if 0 +__weak_alias(dn_skipname,__dn_skipname) +__weak_alias(res_hnok,__res_hnok) +__weak_alias(res_ownok,__res_ownok) +__weak_alias(res_mailok,__res_mailok) +__weak_alias(res_dnok,__res_dnok) +#endif +#endif + +/*% + * Expand compressed domain name 'src' to full domain name. + * + * \li 'msg' is a pointer to the begining of the message, + * \li 'eom' points to the first location after the message, + * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result. + * \li Return size of compressed name or -1 if there was an error. + */ +int +dn_expand(const u_char *msg, const u_char *eom, const u_char *src, + char *dst, int dstsiz) +{ + int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); + + if (n > 0 && dst[0] == '.') + dst[0] = '\0'; + return (n); +} + +/*% + * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. + * + * \li Return the size of the compressed name or -1. + * \li 'length' is the size of the array pointed to by 'comp_dn'. + */ +int +dn_comp(const char *src, u_char *dst, int dstsiz, + u_char **dnptrs, u_char **lastdnptr) +{ + return (ns_name_compress(src, dst, (size_t)dstsiz, + (const u_char **)dnptrs, + (const u_char **)lastdnptr)); +} + +/*% + * Skip over a compressed domain name. Return the size or -1. + */ +int +dn_skipname(const u_char *ptr, const u_char *eom) { + const u_char *saveptr = ptr; + + if (ns_name_skip(&ptr, eom) == -1) + return (-1); + return (ptr - saveptr); +} + +/*% + * Verify that a domain name uses an acceptable character set. + * + * Note the conspicuous absence of ctype macros in these definitions. On + * non-ASCII hosts, we can't depend on string literals or ctype macros to + * tell us anything about network-format data. The rest of the BIND system + * is not careful about this, but for some reason, we're doing it right here. + */ +#define PERIOD 0x2e +#define hyphenchar(c) ((c) == 0x2d) +#define bslashchar(c) ((c) == 0x5c) +#define periodchar(c) ((c) == PERIOD) +#define asterchar(c) ((c) == 0x2a) +#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ + || ((c) >= 0x61 && (c) <= 0x7a)) +#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) + +#define borderchar(c) (alphachar(c) || digitchar(c)) +#define middlechar(c) (borderchar(c) || hyphenchar(c)) +#define domainchar(c) ((c) > 0x20 && (c) < 0x7f) + +int +res_hnok(const char *dn) { + int pch = PERIOD, ch = *dn++; + + while (ch != '\0') { + int nch = *dn++; + + if (periodchar(ch)) { + ; + } else if (periodchar(pch)) { + if (!borderchar(ch)) + return (0); + } else if (periodchar(nch) || nch == '\0') { + if (!borderchar(ch)) + return (0); + } else { + if (!middlechar(ch)) + return (0); + } + pch = ch, ch = nch; + } + return (1); +} + +/*% + * hostname-like (A, MX, WKS) owners can have "*" as their first label + * but must otherwise be as a host name. + */ +int +res_ownok(const char *dn) { + if (asterchar(dn[0])) { + if (periodchar(dn[1])) + return (res_hnok(dn+2)); + if (dn[1] == '\0') + return (1); + } + return (res_hnok(dn)); +} + +/*% + * SOA RNAMEs and RP RNAMEs can have any printable character in their first + * label, but the rest of the name has to look like a host name. + */ +int +res_mailok(const char *dn) { + int ch, escaped = 0; + + /* "." is a valid missing representation */ + if (*dn == '\0') + return (1); + + /* otherwise