Annotation of src/usr.bin/make/memory.c, Revision 1.3
1.1 espie 1: /* $OpenPackages$ */
1.3 ! espie 2: /* $OpenBSD: memory.c,v 1.2 2003/06/03 02:56:12 millert Exp $ */
1.1 espie 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.
1.2 millert 21: * 3. Neither the name of the University nor the names of its contributors
1.1 espie 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 *
1.3 ! espie 55: emalloc(size_t size)
1.1 espie 56: {
57: void *p;
58:
1.3 ! espie 59: if ((p = malloc(size)) == NULL)
! 60: enomem(size);
1.1 espie 61: return p;
62: }
63:
64: /*
65: * estrdup --
66: * strdup, but die on error.
67: */
68: char *
1.3 ! espie 69: estrdup(const char *str)
1.1 espie 70: {
71: char *p;
72: size_t size;
73:
74: size = strlen(str) + 1;
75:
76: p = emalloc(size);
77: memcpy(p, str, size);
78: return p;
79: }
80:
81: /*
82: * erealloc --
83: * realloc, but die on error.
84: */
85: void *
1.3 ! espie 86: erealloc(void *ptr, size_t size)
1.1 espie 87: {
88: if ((ptr = realloc(ptr, size)) == NULL)
89: enomem(size);
90: return ptr;
91: }
92:
93: void *
1.3 ! espie 94: ecalloc(size_t s1, size_t s2)
1.1 espie 95: {
96: void *p;
97:
98: if ((p = calloc(s1, s2)) == NULL)
99: enomem(s1 * s2);
100: return p;
101: }
102:
103: /* Support routines for hash tables. */
104: void *
1.3 ! espie 105: hash_alloc(size_t s, void *u UNUSED)
1.1 espie 106: {
107: return ecalloc(s, 1);
108: }
109:
110: void
1.3 ! espie 111: hash_free(void *p, size_t s UNUSED, void *u UNUSED)
1.1 espie 112: {
113: free(p);
114: }
115:
116: void *
1.3 ! espie 117: element_alloc(size_t s, void *u UNUSED)
1.1 espie 118: {
119: return emalloc(s);
120: }
121:
122:
123:
124: /*
125: * enomem --
126: * die when out of memory.
127: */
128: void
1.3 ! espie 129: enomem(size_t size)
1.1 espie 130: {
131: fprintf(stderr, "make: %s (%lu)\n", strerror(errno), (u_long)size);
132: exit(2);
133: }
134:
135: /*
136: * esetenv --
137: * change environment, die on error.
138: */
139: void
1.3 ! espie 140: esetenv(const char *name, const char *value)
1.1 espie 141: {
142: if (setenv(name, value, 1) == 0)
143: return;
144:
145: fprintf(stderr, "make: setenv failed (%s)\n", strerror(errno));
146: exit(2);
147: }
148:
149:
150: /*
151: * enunlink --
152: * Remove a file carefully, avoiding directories.
153: */
154: int
1.3 ! espie 155: eunlink(const char *file)
1.1 espie 156: {
157: struct stat st;
158:
159: if (lstat(file, &st) == -1)
160: return -1;
161:
162: if (S_ISDIR(st.st_mode)) {
163: errno = EISDIR;
164: return -1;
165: }
166: return unlink(file);
167: }
168: