Annotation of src/usr.bin/make/memory.c, Revision 1.4
1.1 espie 1: /* $OpenPackages$ */
1.4 ! espie 2: /* $OpenBSD: memory.c,v 1.3 2004/04/07 13:11:36 espie 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>
1.4 ! espie 45: #include <stddef.h>
! 46: #include <stdint.h>
! 47: #include <ohash.h>
1.1 espie 48: #include "defines.h"
49: #include "memory.h"
50:
51: static void enomem(size_t);
52:
53: /*
54: * emalloc --
55: * malloc, but die on error.
56: */
57: void *
1.3 espie 58: emalloc(size_t size)
1.1 espie 59: {
60: void *p;
61:
1.3 espie 62: if ((p = malloc(size)) == NULL)
63: enomem(size);
1.1 espie 64: return p;
65: }
66:
67: /*
68: * estrdup --
69: * strdup, but die on error.
70: */
71: char *
1.3 espie 72: estrdup(const char *str)
1.1 espie 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 *
1.3 espie 89: erealloc(void *ptr, size_t size)
1.1 espie 90: {
91: if ((ptr = realloc(ptr, size)) == NULL)
92: enomem(size);
93: return ptr;
94: }
95:
96: void *
1.3 espie 97: ecalloc(size_t s1, size_t s2)
1.1 espie 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 *
1.3 espie 108: hash_alloc(size_t s, void *u UNUSED)
1.1 espie 109: {
110: return ecalloc(s, 1);
111: }
112:
113: void
1.3 espie 114: hash_free(void *p, size_t s UNUSED, void *u UNUSED)
1.1 espie 115: {
116: free(p);
117: }
118:
119: void *
1.3 espie 120: element_alloc(size_t s, void *u UNUSED)
1.1 espie 121: {
122: return emalloc(s);
123: }
124:
125:
126:
127: /*
128: * enomem --
129: * die when out of memory.
130: */
131: void
1.3 espie 132: enomem(size_t size)
1.1 espie 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
1.3 espie 143: esetenv(const char *name, const char *value)
1.1 espie 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
1.3 espie 158: eunlink(const char *file)
1.1 espie 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);
1.4 ! espie 170: }
! 171:
! 172: void
! 173: free_hash(struct ohash *h)
! 174: {
! 175: void *e;
! 176: unsigned int i;
! 177:
! 178: for (e = ohash_first(h, &i); e != NULL; e = ohash_next(h, &i))
! 179: free(e);
! 180: ohash_delete(h);
1.1 espie 181: }
182: