2 * Copyright (c) 1999-2002 Todd C. Miller <Todd.Miller@courtesan.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * 4. Products derived from this software may not be called "Sudo" nor
20 * may "Sudo" appear in their names without specific prior written
21 * permission from the author.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
38 #include <sys/param.h>
47 #endif /* STDC_HEADERS */
51 # ifdef HAVE_STRINGS_H
54 #endif /* HAVE_STRING_H */
55 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
57 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
62 static const char rcsid[] = "$Sudo: alloc.c,v 1.11 2002/01/09 16:56:04 millert Exp $";
65 extern char **Argv; /* from sudo.c */
68 * emalloc() calls the system malloc(3) and exits with an error if
77 if ((ptr = (VOID *) malloc(size)) == NULL) {
78 (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
85 * erealloc() calls the system realloc(3) and exits with an error if
86 * realloc(3) fails. You can call erealloc() with a NULL pointer even
87 * if the system realloc(3) does not support this.
95 ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
97 (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
104 * estrdup() is like strdup(3) except that it exits with an error if
105 * malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal.
114 dst = (char *) emalloc(strlen(src) + 1);
115 (void) strcpy(dst, src);
121 * easprintf() calls vasprintf() and exits with an error if vasprintf()
122 * returns -1 (out of memory).
126 easprintf(char **ret, const char *fmt, ...)
141 ret = va_arg(ap, char **);
142 fmt = va_arg(ap, const char *);
144 len = vasprintf(ret, fmt, ap);
148 (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
155 * evasprintf() calls vasprintf() and exits with an error if vasprintf()
156 * returns -1 (out of memory).
159 evasprintf(ret, format, args)
166 if ((len = vasprintf(ret, format, args)) == -1) {
167 (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);