Annotation of src/usr.bin/make/error.c, Revision 1.1
1.1 ! espie 1: /* $OpenBSD$ */
! 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:
! 111: /*
! 112: * enomem --
! 113: * die when out of memory.
! 114: */
! 115: void
! 116: enomem(size)
! 117: size_t size;
! 118: {
! 119: int myerr = errno;
! 120:
! 121: fprintf(stderr, "make: %s (%d)\n", strerror(myerr), size);
! 122: exit(2);
! 123: }
! 124:
! 125: /*
! 126: * enunlink --
! 127: * Remove a file carefully, avoiding directories.
! 128: */
! 129: int
! 130: eunlink(file)
! 131: const char *file;
! 132: {
! 133: struct stat st;
! 134:
! 135: if (lstat(file, &st) == -1)
! 136: return -1;
! 137:
! 138: if (S_ISDIR(st.st_mode)) {
! 139: errno = EISDIR;
! 140: return -1;
! 141: }
! 142: return unlink(file);
! 143: }
! 144: