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