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