[BACK]Return to error.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/error.c, Revision 1.5

1.5     ! espie       1: /* $OpenPackages$ */
        !             2: /* $OpenBSD$ */
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.
                     21:  * 3. All advertising materials mentioning features or use of this software
                     22:  *    must display the following acknowledgement:
                     23:  *     This product includes software developed by the University of
                     24:  *     California, Berkeley and its contributors.
                     25:  * 4. Neither the name of the University nor the names of its contributors
                     26:  *    may be used to endorse or promote products derived from this software
                     27:  *    without specific prior written permission.
                     28:  *
                     29:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     30:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     31:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     32:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     33:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     34:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     35:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     36:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     37:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     38:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     39:  * SUCH DAMAGE.
                     40:  */
                     41:
                     42: #include <sys/types.h>
                     43: #include <sys/stat.h>
                     44: #include <errno.h>
                     45: #include <stdio.h>
                     46: #include <stdlib.h>
                     47: #include <string.h>
                     48: #include <unistd.h>
                     49: #include "error.h"
                     50:
1.5     ! espie      51: #ifdef __GNUC__
        !            52: #define UNUSED __attribute__((unused))
        !            53: #else
        !            54: #define UNUSED
        !            55: #endif
        !            56:
        !            57: static void enomem(size_t);
1.1       espie      58:
                     59: /*
                     60:  * emalloc --
                     61:  *     malloc, but die on error.
                     62:  */
                     63: void *
                     64: emalloc(len)
                     65:        size_t len;
                     66: {
                     67:        void *p;
                     68:
                     69:        if ((p = malloc(len)) == NULL)
                     70:                enomem(len);
                     71:        return p;
                     72: }
                     73:
                     74: /*
                     75:  * estrdup --
                     76:  *     strdup, but die on error.
                     77:  */
                     78: char *
                     79: estrdup(str)
                     80:        const char *str;
                     81: {
                     82:        char *p;
                     83:        size_t size;
                     84:
                     85:        size = strlen(str) + 1;
                     86:
                     87:        p = emalloc(size);
                     88:        memcpy(p, str, size);
                     89:        return p;
                     90: }
                     91:
                     92: /*
                     93:  * erealloc --
                     94:  *     realloc, but die on error.
                     95:  */
                     96: void *
                     97: erealloc(ptr, size)
                     98:        void *ptr;
                     99:        size_t size;
                    100: {
                    101:        if ((ptr = realloc(ptr, size)) == NULL)
                    102:                enomem(size);
                    103:        return ptr;
                    104: }
                    105:
                    106: void *
                    107: ecalloc(s1, s2)
                    108:        size_t s1;
                    109:        size_t s2;
                    110: {
                    111:        void *p;
                    112:
                    113:        if ((p = calloc(s1, s2)) == NULL)
                    114:                enomem(s1 * s2);
                    115:        return p;
                    116: }
                    117:
1.3       espie     118: /* Support routines for hash tables.  */
                    119: void *
                    120: hash_alloc(s, u)
                    121:        size_t s;
1.5     ! espie     122:        void *u         UNUSED;
1.3       espie     123: {
                    124:        return ecalloc(s, 1);
                    125: }
                    126:
                    127: void
                    128: hash_free(p, s, u)
                    129:        void *p;
1.5     ! espie     130:        size_t s        UNUSED;
        !           131:        void *u         UNUSED;
1.3       espie     132: {
                    133:        free(p);
                    134: }
                    135:
                    136: void *
                    137: element_alloc(s, u)
                    138:        size_t s;
1.5     ! espie     139:        void *u         UNUSED;
1.3       espie     140: {
                    141:        return emalloc(s);
                    142: }
1.5     ! espie     143:
        !           144:
        !           145:
1.1       espie     146: /*
                    147:  * enomem --
                    148:  *     die when out of memory.
                    149:  */
                    150: void
                    151: enomem(size)
                    152:        size_t size;
                    153: {
1.4       espie     154:        fprintf(stderr, "make: %s (%lu)\n", strerror(errno), (u_long)size);
                    155:        exit(2);
                    156: }
                    157:
                    158: /*
                    159:  * esetenv --
                    160:  *     change environment, die on error.
                    161:  */
                    162: void
                    163: esetenv(name, value)
                    164:        const char *name;
                    165:        const char *value;
                    166: {
                    167:        if (setenv(name, value, 1) == 0)
                    168:            return;
1.1       espie     169:
1.4       espie     170:        fprintf(stderr, "make: setenv failed (%s)\n", strerror(errno));
1.1       espie     171:        exit(2);
                    172: }
                    173:
1.5     ! espie     174:
1.1       espie     175: /*
                    176:  * enunlink --
                    177:  *     Remove a file carefully, avoiding directories.
                    178:  */
                    179: int
                    180: eunlink(file)
                    181:        const char *file;
                    182: {
                    183:        struct stat st;
                    184:
                    185:        if (lstat(file, &st) == -1)
                    186:                return -1;
                    187:
                    188:        if (S_ISDIR(st.st_mode)) {
                    189:                errno = EISDIR;
                    190:                return -1;
                    191:        }
                    192:        return unlink(file);
                    193: }
1.5     ! espie     194: