Annotation of src/usr.bin/sudo/alloc.c, Revision 1.1.1.1
1.1 millert 1: /*
2: * Copyright (c) 1999 Todd C. Miller <Todd.Miller@courtesan.com>
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:
37: #include <stdio.h>
38: #ifdef STDC_HEADERS
39: #include <stdlib.h>
40: #endif /* STDC_HEADERS */
41: #ifdef HAVE_STRING_H
42: #include <string.h>
43: #endif /* HAVE_STRING_H */
44: #ifdef HAVE_STRINGS_H
45: #include <strings.h>
46: #endif /* HAVE_STRINGS_H */
47: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
48: #include <malloc.h>
49: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
50: #include <sys/param.h>
51: #include <sys/types.h>
52:
53: #include "sudo.h"
54:
55: #ifndef STDC_HEADERS
56: #if !defined(__GNUC__) && !defined(HAVE_MALLOC_H)
57: extern VOID *malloc __P((size_t));
58: #endif /* !__GNUC__ && !HAVE_MALLOC_H */
59: #endif /* !STDC_HEADERS */
60:
61: extern char **Argv; /* from sudo.c */
62:
63: #ifndef lint
64: static const char rcsid[] = "$Sudo: alloc.c,v 1.8 1999/07/31 16:19:44 millert Exp $";
65: #endif /* lint */
66:
67:
68: /*
69: * emalloc() calls the system malloc(3) and exits with an error if
70: * malloc(3) fails.
71: */
72: VOID *
73: emalloc(size)
74: size_t size;
75: {
76: VOID *ptr;
77:
78: if ((ptr = malloc(size)) == NULL) {
79: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
80: exit(1);
81: }
82: return(ptr);
83: }
84:
85: /*
86: * erealloc() calls the system realloc(3) and exits with an error if
87: * realloc(3) fails. You can call erealloc() with a NULL pointer even
88: * if the system realloc(3) does not support this.
89: */
90: VOID *
91: erealloc(ptr, size)
92: VOID *ptr;
93: size_t size;
94: {
95:
96: if ((ptr = ptr ? realloc(ptr, size) : malloc(size)) == NULL) {
97: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
98: exit(1);
99: }
100: return(ptr);
101: }
102:
103: /*
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.
106: */
107: char *
108: estrdup(src)
109: const char *src;
110: {
111: char *dst = NULL;
112:
113: if (src != NULL) {
114: dst = (char *) emalloc(strlen(src) + 1);
115: (void) strcpy(dst, src);
116: }
117: return(dst);
118: }
119:
120: /*
121: * easprintf() calls vasprintf() and exits with an error if vasprintf()
122: * returns -1 (out of memory).
123: */
124: void
125: #ifdef __STDC__
126: easprintf(char **ret, const char *fmt, ...)
127: #else
128: easprintf(va_alist)
129: va_dcl
130: #endif
131: {
132: int len;
133: va_list ap;
134: #ifdef __STDC__
135: va_start(ap, fmt);
136: #else
137: char **ret;
138: const char *fmt;
139:
140: va_start(ap);
141: ret = va_arg(ap, char **);
142: fmt = va_arg(ap, const char *);
143: #endif
144: len = vasprintf(ret, fmt, ap);
145: va_end(ap);
146:
147: if (len == -1) {
148: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
149: exit(1);
150: }
151: }
152:
153: /*
154: * evasprintf() calls vasprintf() and exits with an error if vasprintf()
155: * returns -1 (out of memory).
156: */
157: void
158: evasprintf(ret, format, args)
159: char **ret;
160: const char *format;
161: va_list args;
162: {
163:
164: if (vasprintf(ret, format, args) == -1) {
165: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
166: exit(1);
167: }
168: }