Annotation of src/usr.bin/sudo/alloc.c, Revision 1.3
1.1 millert 1: /*
1.3 ! millert 2: * Copyright (c) 1999-2001 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: *
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
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.
15: *
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.
18: *
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.
22: *
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.
33: */
34:
35: #include "config.h"
36:
1.3 ! millert 37: #include <sys/types.h>
! 38: #include <sys/param.h>
1.1 millert 39: #include <stdio.h>
40: #ifdef STDC_HEADERS
1.3 ! millert 41: # include <stdlib.h>
! 42: # include <stddef.h>
! 43: #else
! 44: # ifdef HAVE_STDLIB_H
! 45: # include <stdlib.h>
! 46: # endif
1.1 millert 47: #endif /* STDC_HEADERS */
48: #ifdef HAVE_STRING_H
1.3 ! millert 49: # include <string.h>
! 50: #else
! 51: # ifdef HAVE_STRINGS_H
! 52: # include <strings.h>
! 53: # endif
1.1 millert 54: #endif /* HAVE_STRING_H */
55: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
1.3 ! millert 56: # include <malloc.h>
1.1 millert 57: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
58:
59: #include "sudo.h"
60:
61: #ifndef STDC_HEADERS
62: #if !defined(__GNUC__) && !defined(HAVE_MALLOC_H)
63: extern VOID *malloc __P((size_t));
64: #endif /* !__GNUC__ && !HAVE_MALLOC_H */
65: #endif /* !STDC_HEADERS */
66:
67: extern char **Argv; /* from sudo.c */
68:
69: #ifndef lint
1.3 ! millert 70: static const char rcsid[] = "$Sudo: alloc.c,v 1.10 2001/12/14 19:52:47 millert Exp $";
1.1 millert 71: #endif /* lint */
72:
73:
74: /*
75: * emalloc() calls the system malloc(3) and exits with an error if
76: * malloc(3) fails.
77: */
78: VOID *
79: emalloc(size)
80: size_t size;
81: {
82: VOID *ptr;
83:
84: if ((ptr = malloc(size)) == NULL) {
85: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
86: exit(1);
87: }
88: return(ptr);
89: }
90:
91: /*
92: * erealloc() calls the system realloc(3) and exits with an error if
93: * realloc(3) fails. You can call erealloc() with a NULL pointer even
94: * if the system realloc(3) does not support this.
95: */
96: VOID *
97: erealloc(ptr, size)
98: VOID *ptr;
99: size_t size;
100: {
101:
102: if ((ptr = ptr ? realloc(ptr, size) : malloc(size)) == NULL) {
103: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
104: exit(1);
105: }
106: return(ptr);
107: }
108:
109: /*
110: * estrdup() is like strdup(3) except that it exits with an error if
111: * malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal.
112: */
113: char *
114: estrdup(src)
115: const char *src;
116: {
117: char *dst = NULL;
118:
119: if (src != NULL) {
120: dst = (char *) emalloc(strlen(src) + 1);
121: (void) strcpy(dst, src);
122: }
123: return(dst);
124: }
125:
126: /*
127: * easprintf() calls vasprintf() and exits with an error if vasprintf()
128: * returns -1 (out of memory).
129: */
1.2 millert 130: int
1.1 millert 131: #ifdef __STDC__
132: easprintf(char **ret, const char *fmt, ...)
133: #else
134: easprintf(va_alist)
135: va_dcl
136: #endif
137: {
138: int len;
139: va_list ap;
140: #ifdef __STDC__
141: va_start(ap, fmt);
142: #else
143: char **ret;
144: const char *fmt;
145:
146: va_start(ap);
147: ret = va_arg(ap, char **);
148: fmt = va_arg(ap, const char *);
149: #endif
150: len = vasprintf(ret, fmt, ap);
151: va_end(ap);
152:
153: if (len == -1) {
154: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
155: exit(1);
156: }
1.2 millert 157: return(len);
1.1 millert 158: }
159:
160: /*
161: * evasprintf() calls vasprintf() and exits with an error if vasprintf()
162: * returns -1 (out of memory).
163: */
1.2 millert 164: int
1.1 millert 165: evasprintf(ret, format, args)
166: char **ret;
167: const char *format;
168: va_list args;
169: {
1.2 millert 170: int len;
1.1 millert 171:
1.2 millert 172: if ((len = vasprintf(ret, format, args)) == -1) {
1.1 millert 173: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
174: exit(1);
175: }
1.2 millert 176: return(len);
1.1 millert 177: }