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