diff options
author | assar <assar@FreeBSD.org> | 2001-07-22 22:26:37 +0000 |
---|---|---|
committer | assar <assar@FreeBSD.org> | 2001-07-22 22:26:37 +0000 |
commit | 8668d652182d9e8318c7d79f423f8a0163258715 (patch) | |
tree | 2e4bb8906aed4fcfcfcd6fd955816ec78126208b /lib | |
parent | 08e82b6d3d1211c44bc3fcb70a0aef0a3e64d43f (diff) | |
download | FreeBSD-src-8668d652182d9e8318c7d79f423f8a0163258715.zip FreeBSD-src-8668d652182d9e8318c7d79f423f8a0163258715.tar.gz |
add ecalloc, emalloc, erealloc, estrdup - versions of the e-less
functions that exit instead of failing
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libutil/Makefile | 8 | ||||
-rw-r--r-- | lib/libutil/ecalloc.c | 49 | ||||
-rw-r--r-- | lib/libutil/emalloc.3 | 64 | ||||
-rw-r--r-- | lib/libutil/emalloc.c | 49 | ||||
-rw-r--r-- | lib/libutil/erealloc.c | 49 | ||||
-rw-r--r-- | lib/libutil/estrdup.c | 50 | ||||
-rw-r--r-- | lib/libutil/libutil.h | 4 |
7 files changed, 271 insertions, 2 deletions
diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index 6c261e6..d310df5 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -6,8 +6,9 @@ SHLIB_MAJOR= 3 SHLIB_MINOR= 0 CFLAGS+=-Wall -DLIBC_SCCS -I${.CURDIR} CFLAGS+=-DINET6 -SRCS= _secure_path.c auth.c extattr.c fparseln.c login.c login_auth.c \ - login_cap.c login_class.c login_crypt.c login_ok.c login_times.c \ +SRCS= _secure_path.c auth.c ecalloc.c emalloc.c erealloc.c estrdup.c \ + extattr.c fparseln.c login.c login_auth.c \ + login_cap.c login_class.c login_crypt.c login_ok.c login_times.c \ login_tty.c logout.c logwtmp.c property.c pty.c realhostname.c stub.c \ trimdomain.c uucplock.c INCS= libutil.h login_cap.h @@ -18,6 +19,8 @@ MAN+= login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \ _secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \ realhostname_sa.3 trimdomain.3 fparseln.3 MAN+= login.conf.5 auth.conf.5 +MAN+= emalloc.3 + MLINKS+= extattr.3 extattr_namespace_to_string.3 \ extattr.3 extattr_string_to_namespace.3 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 @@ -41,5 +44,6 @@ MLINKS+=login_ok.3 auth_ttyok.3 login_ok.3 auth_hostok.3 \ MLINKS+=login_auth.3 auth_checknologin.3 login_auth.3 auth_cat.3 MLINKS+=uucplock.3 uu_lock.3 uucplock.3 uu_lock_txfr.3 \ uucplock.3 uu_unlock.3 uucplock.3 uu_lockerr.3 +MLINKS+=emalloc.3 ecalloc.3 emalloc.3 erealloc.3 emalloc.3 estrdup.3 .include <bsd.lib.mk> diff --git a/lib/libutil/ecalloc.c b/lib/libutil/ecalloc.c new file mode 100644 index 0000000..ce778df --- /dev/null +++ b/lib/libutil/ecalloc.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this 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$ + */ + +#include <stdlib.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like calloc but never fails. + */ + +void * +ecalloc(size_t number, size_t size) +{ + void *tmp = calloc(number, size); + + if (tmp == NULL && number * size != 0) + errx(1, "calloc %lu failed", (unsigned long)number * size); + return tmp; +} diff --git a/lib/libutil/emalloc.3 b/lib/libutil/emalloc.3 new file mode 100644 index 0000000..f087e86 --- /dev/null +++ b/lib/libutil/emalloc.3 @@ -0,0 +1,64 @@ +.\" Copyright (c) 2001 Assar Westerlund +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this 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$ +.\" +.Dd June 17, 2001 +.Os +.Dt EMALLOC 3 +.Sh NAME +.Nm emalloc , ecalloc , erealloc , estrdup +.Nd non-failing memory allocation functions +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <libutil.h> +.Ft void * +.Fn emalloc "size_t size" +.Ft void * +.Fn ecalloc "size_t number" "size_t size" +.Ft void * +.Fn erealloc "void *ptr" "size_t size" +.Ft char * +.Fn estrdup "const char *str" +.Sh DESCRIPTION +These functions work as +.Fn malloc , +.Fn calloc , +.Fn realloc , +and +.Fn strdup , +except that they never return +.Dv NULL , +but instead call +.Fn errx +to abort the program if memory allocation fails. +.Sh SEE ALSO +.Xr malloc 3 , +.Xr calloc 3 , +.Xr realloc 3 , +.Xr strdup 3 , +.Xr errx 3 diff --git a/lib/libutil/emalloc.c b/lib/libutil/emalloc.c new file mode 100644 index 0000000..29dd3da --- /dev/null +++ b/lib/libutil/emalloc.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this 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$ + */ + +#include <stdlib.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like malloc but never fails. + */ + +void * +emalloc(size_t sz) +{ + void *tmp = malloc(sz); + + if (tmp == NULL && sz != 0) + errx(1, "malloc %lu failed", (unsigned long)sz); + return tmp; +} diff --git a/lib/libutil/erealloc.c b/lib/libutil/erealloc.c new file mode 100644 index 0000000..6c3e2a1 --- /dev/null +++ b/lib/libutil/erealloc.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this 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$ + */ + +#include <stdlib.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like realloc but never fails. + */ + +void * +erealloc(void *ptr, size_t sz) +{ + void *tmp = realloc(ptr, sz); + + if (tmp == NULL && sz != 0) + errx(1, "realloc %lu failed", (unsigned long)sz); + return tmp; +} diff --git a/lib/libutil/estrdup.c b/lib/libutil/estrdup.c new file mode 100644 index 0000000..4dfeac1 --- /dev/null +++ b/lib/libutil/estrdup.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this 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$ + */ + +#include <stdlib.h> +#include <string.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like strdup but never fails. + */ + +char * +estrdup(const char *str) +{ + void *tmp = strdup(str); + + if (tmp == NULL) + errx(1, "strdup"); + return tmp; +} diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h index aa82caa..758136e 100644 --- a/lib/libutil/libutil.h +++ b/lib/libutil/libutil.h @@ -76,6 +76,10 @@ int realhostname_sa __P((char *host, size_t hsize, struct sockaddr *addr, #ifdef _STDIO_H_ /* avoid adding new includes */ char *fparseln __P((FILE *, size_t *, size_t *, const char[3], int)); #endif +void *emalloc (size_t); +void *ecalloc (size_t, size_t); +void *erealloc (void *, size_t); +char *estrdup (const char *); __END_DECLS #define UU_LOCK_INUSE (1) |