1 /* $OpenPackages$ */ 2 /* $OpenBSD: memory.c,v 1.1 2001/05/23 12:34:47 espie Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1989, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * Copyright (c) 1989 by Berkeley Softworks 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Adam de Boor. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 */ 41 42 #include <sys/types.h> 43 #include <sys/stat.h> 44 #include <errno.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include "defines.h" 50 #include "memory.h" 51 52 static void enomem(size_t); 53 54 /* 55 * emalloc -- 56 * malloc, but die on error. 57 */ 58 void * 59 emalloc(len) 60 size_t len; 61 { 62 void *p; 63 64 if ((p = malloc(len)) == NULL) 65 enomem(len); 66 return p; 67 } 68 69 /* 70 * estrdup -- 71 * strdup, but die on error. 72 */ 73 char * 74 estrdup(str) 75 const char *str; 76 { 77 char *p; 78 size_t size; 79 80 size = strlen(str) + 1; 81 82 p = emalloc(size); 83 memcpy(p, str, size); 84 return p; 85 } 86 87 /* 88 * erealloc -- 89 * realloc, but die on error. 90 */ 91 void * 92 erealloc(ptr, size) 93 void *ptr; 94 size_t size; 95 { 96 if ((ptr = realloc(ptr, size)) == NULL) 97 enomem(size); 98 return ptr; 99 } 100 101 void * 102 ecalloc(s1, s2) 103 size_t s1; 104 size_t s2; 105 { 106 void *p; 107 108 if ((p = calloc(s1, s2)) == NULL) 109 enomem(s1 * s2); 110 return p; 111 } 112 113 /* Support routines for hash tables. */ 114 void * 115 hash_alloc(s, u) 116 size_t s; 117 void *u UNUSED; 118 { 119 return ecalloc(s, 1); 120 } 121 122 void 123 hash_free(p, s, u) 124 void *p; 125 size_t s UNUSED; 126 void *u UNUSED; 127 { 128 free(p); 129 } 130 131 void * 132 element_alloc(s, u) 133 size_t s; 134 void *u UNUSED; 135 { 136 return emalloc(s); 137 } 138 139 140 141 /* 142 * enomem -- 143 * die when out of memory. 144 */ 145 void 146 enomem(size) 147 size_t size; 148 { 149 fprintf(stderr, "make: %s (%lu)\n", strerror(errno), (u_long)size); 150 exit(2); 151 } 152 153 /* 154 * esetenv -- 155 * change environment, die on error. 156 */ 157 void 158 esetenv(name, value) 159 const char *name; 160 const char *value; 161 { 162 if (setenv(name, value, 1) == 0) 163 return; 164 165 fprintf(stderr, "make: setenv failed (%s)\n", strerror(errno)); 166 exit(2); 167 } 168 169 170 /* 171 * enunlink -- 172 * Remove a file carefully, avoiding directories. 173 */ 174 int 175 eunlink(file) 176 const char *file; 177 { 178 struct stat st; 179 180 if (lstat(file, &st) == -1) 181 return -1; 182 183 if (S_ISDIR(st.st_mode)) { 184 errno = EISDIR; 185 return -1; 186 } 187 return unlink(file); 188 } 189 190