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

Annotation of src/usr.bin/make/util.c, Revision 1.14

1.14    ! espie       1: /*     $OpenPackages$ */
        !             2: /*     $OpenBSD: util.c,v 1.10 1998/12/20 23:38:11 deraadt Exp $       */
1.6       millert     3: /*     $NetBSD: util.c,v 1.10 1996/12/31 17:56:04 christos Exp $       */
1.1       deraadt     4:
                      5: /*
                      6:  * Missing stuff from OS's
                      7:  */
                      8:
1.14    ! espie       9: #ifndef lint
        !            10: static char rcsid[] = "$OpenBSD: util.c,v 1.10 1998/12/20 23:38:11 deraadt Exp $";
        !            11: #endif
        !            12:
1.1       deraadt    13: #include <stdio.h>
                     14: #include "make.h"
1.6       millert    15: #include <sys/param.h>
1.13      espie      16:
1.7       mickey     17: #ifndef __STDC__
1.1       deraadt    18: # ifndef const
                     19: #  define const
                     20: # endif
                     21: #endif
                     22:
                     23: #ifdef sun
                     24:
                     25:
                     26:
                     27: extern int errno, sys_nerr;
                     28: extern char *sys_errlist[];
                     29:
                     30: char *
1.5       millert    31: strerror(e)
                     32:     int e;
1.1       deraadt    33: {
                     34:     static char buf[100];
                     35:     if (e < 0 || e >= sys_nerr) {
                     36:        sprintf(buf, "Unknown error %d", e);
                     37:        return buf;
                     38:     }
                     39:     else
                     40:        return sys_errlist[e];
                     41: }
                     42: #endif
                     43:
1.4       briggs     44: #ifdef ultrix
                     45: #include <string.h>
                     46:
                     47: /* strdup
                     48:  *
                     49:  * Make a duplicate of a string.
                     50:  * For systems which lack this function.
                     51:  */
                     52: char *
                     53: strdup(str)
                     54:     const char *str;
                     55: {
                     56:     size_t len;
1.5       millert    57:     char *p;
1.4       briggs     58:
                     59:     if (str == NULL)
                     60:        return NULL;
                     61:     len = strlen(str) + 1;
                     62:     if ((p = malloc(len)) == NULL)
                     63:        return NULL;
                     64:
                     65:     return memcpy(p, str, len);
                     66: }
                     67:
                     68: #endif
                     69:
                     70: #if defined(sun) || defined(__hpux) || defined(__sgi)
1.1       deraadt    71:
                     72: int
                     73: setenv(name, value, dum)
1.5       millert    74:     const char *name;
1.1       deraadt    75:     const char *value;
                     76:     int dum;
                     77: {
1.14    ! espie      78:     char *p;
1.1       deraadt    79:     int len = strlen(name) + strlen(value) + 2; /* = \0 */
1.14    ! espie      80:     char *ptr = (char*)malloc(len);
1.1       deraadt    81:
                     82:     if (ptr == NULL)
                     83:        return -1;
1.5       millert    84:
1.1       deraadt    85:     p = ptr;
                     86:
1.5       millert    87:     while (*name)
1.1       deraadt    88:        *p++ = *name++;
                     89:
                     90:     *p++ = '=';
                     91:
1.5       millert    92:     while (*value)
1.1       deraadt    93:        *p++ = *value++;
                     94:
                     95:     *p = '\0';
                     96:
                     97:     len = putenv(ptr);
                     98: /*    free(ptr); */
                     99:     return len;
                    100: }
                    101: #endif
                    102:
                    103: #ifdef __hpux
                    104: #include <sys/types.h>
                    105: #include <sys/param.h>
                    106: #include <sys/syscall.h>
                    107: #include <sys/signal.h>
                    108: #include <sys/stat.h>
                    109: #include <stdio.h>
                    110: #include <dirent.h>
                    111: #include <sys/time.h>
                    112: #include <time.h>
                    113: #include <unistd.h>
                    114:
                    115:
                    116: int
                    117: killpg(pid, sig)
                    118:     int pid, sig;
                    119: {
                    120:     return kill(-pid, sig);
                    121: }
                    122:
                    123: void
                    124: srandom(seed)
                    125:     long seed;
                    126: {
                    127:     srand48(seed);
                    128: }
                    129:
                    130: long
                    131: random()
                    132: {
                    133:     return lrand48();
                    134: }
                    135:
                    136: /* turn into bsd signals */
                    137: void (*
1.14    ! espie     138: signal(s, a))()
1.1       deraadt   139:     int     s;
                    140:     void (*a)();
                    141: {
                    142:     struct sigvec osv, sv;
                    143:
1.14    ! espie     144:     (void)sigvector(s, (struct sigvec *)0, &osv);
1.1       deraadt   145:     sv = osv;
                    146:     sv.sv_handler = a;
                    147: #ifdef SV_BSDSIG
                    148:     sv.sv_flags = SV_BSDSIG;
                    149: #endif
                    150:
1.14    ! espie     151:     if (sigvector(s, &sv, (struct sigvec *)0) == -1)
        !           152:        return SIG_ERR;
        !           153:     return osv.sv_handler;
1.1       deraadt   154: }
                    155:
                    156: #if !defined(BSD) && !defined(d_fileno)
                    157: # define d_fileno d_ino
                    158: #endif
                    159:
                    160: #ifndef DEV_DEV_COMPARE
                    161: # define DEV_DEV_COMPARE(a, b) ((a) == (b))
                    162: #endif
1.14    ! espie     163: #define ISDOT(c) ((c)[0] == '.' && ((c)[1] == '\0' || (c)[1] == '/'))
1.1       deraadt   164: #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
                    165:
                    166:
                    167: /* strrcpy():
                    168:  *     Like strcpy, going backwards and returning the new pointer
                    169:  */
                    170: static char *
                    171: strrcpy(ptr, str)
1.14    ! espie     172:     char *ptr, *str;
1.1       deraadt   173: {
1.14    ! espie     174:     size_t len = strlen(str);
1.1       deraadt   175:
                    176:     while (len)
                    177:        *--ptr = str[--len];
                    178:
1.14    ! espie     179:     return ptr;
1.1       deraadt   180: } /* end strrcpy */
                    181:
                    182:
                    183: char   *
                    184: getwd(pathname)
                    185:     char   *pathname;
                    186: {
                    187:     DIR    *dp;
                    188:     struct dirent *d;
                    189:     extern int errno;
                    190:
                    191:     struct stat st_root, st_cur, st_next, st_dotdot;
                    192:     char    pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
                    193:     char   *pathptr, *nextpathptr, *cur_name_add;
                    194:
                    195:     /* find the inode of root */
                    196:     if (stat("/", &st_root) == -1) {
1.14    ! espie     197:        (void)sprintf(pathname,
1.1       deraadt   198:                        "getwd: Cannot stat \"/\" (%s)", strerror(errno));
1.14    ! espie     199:        return NULL;
1.1       deraadt   200:     }
                    201:     pathbuf[MAXPATHLEN - 1] = '\0';
                    202:     pathptr = &pathbuf[MAXPATHLEN - 1];
                    203:     nextpathbuf[MAXPATHLEN - 1] = '\0';
                    204:     cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
                    205:
                    206:     /* find the inode of the current directory */
                    207:     if (lstat(".", &st_cur) == -1) {
1.14    ! espie     208:        (void)sprintf(pathname,
1.1       deraadt   209:                        "getwd: Cannot stat \".\" (%s)", strerror(errno));
1.14    ! espie     210:        return NULL;
1.1       deraadt   211:     }
                    212:     nextpathptr = strrcpy(nextpathptr, "../");
                    213:
                    214:     /* Descend to root */
                    215:     for (;;) {
                    216:
                    217:        /* look if we found root yet */
                    218:        if (st_cur.st_ino == st_root.st_ino &&
                    219:            DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
1.14    ! espie     220:            (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
        !           221:            return pathname;
1.1       deraadt   222:        }
                    223:
                    224:        /* open the parent directory */
                    225:        if (stat(nextpathptr, &st_dotdot) == -1) {
1.14    ! espie     226:            (void)sprintf(pathname,
1.1       deraadt   227:                            "getwd: Cannot stat directory \"%s\" (%s)",
                    228:                            nextpathptr, strerror(errno));
1.14    ! espie     229:            return NULL;
1.1       deraadt   230:        }
                    231:        if ((dp = opendir(nextpathptr)) == NULL) {
1.14    ! espie     232:            (void)sprintf(pathname,
1.1       deraadt   233:                            "getwd: Cannot open directory \"%s\" (%s)",
                    234:                            nextpathptr, strerror(errno));
1.14    ! espie     235:            return NULL;
1.1       deraadt   236:        }
                    237:
                    238:        /* look in the parent for the entry with the same inode */
                    239:        if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
                    240:            /* Parent has same device. No need to stat every member */
1.5       millert   241:            for (d = readdir(dp); d != NULL; d = readdir(dp))
1.1       deraadt   242:                if (d->d_fileno == st_cur.st_ino)
                    243:                    break;
                    244:        }
                    245:        else {
1.5       millert   246:            /*
                    247:             * Parent has a different device. This is a mount point so we
                    248:             * need to stat every member
1.1       deraadt   249:             */
                    250:            for (d = readdir(dp); d != NULL; d = readdir(dp)) {
                    251:                if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
                    252:                    continue;
1.14    ! espie     253:                (void)strcpy(cur_name_add, d->d_name);
1.1       deraadt   254:                if (lstat(nextpathptr, &st_next) == -1) {
1.14    ! espie     255:                    (void)sprintf(pathname, "getwd: Cannot stat \"%s\" (%s)",
1.1       deraadt   256:                                    d->d_name, strerror(errno));
1.14    ! espie     257:                    (void)closedir(dp);
        !           258:                    return NULL;
1.1       deraadt   259:                }
                    260:                /* check if we found it yet */
                    261:                if (st_next.st_ino == st_cur.st_ino &&
1.5       millert   262:                    DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
1.1       deraadt   263:                    break;
                    264:            }
                    265:        }
                    266:        if (d == NULL) {
1.14    ! espie     267:            (void)sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
        !           268:            (void)closedir(dp);
        !           269:            return NULL;
1.1       deraadt   270:        }
                    271:        st_cur = st_dotdot;
                    272:        pathptr = strrcpy(pathptr, d->d_name);
                    273:        pathptr = strrcpy(pathptr, "/");
                    274:        nextpathptr = strrcpy(nextpathptr, "../");
1.14    ! espie     275:        (void)closedir(dp);
1.1       deraadt   276:        *cur_name_add = '\0';
                    277:     }
                    278: } /* end getwd */
                    279:
                    280:
1.14    ! espie     281: char   *sys_siglist[] = {
        !           282:        "Signal 0",
        !           283:        "Hangup",                       /* SIGHUP    */
        !           284:        "Interrupt",                    /* SIGINT    */
        !           285:        "Quit",                         /* SIGQUIT   */
        !           286:        "Illegal instruction",          /* SIGILL    */
        !           287:        "Trace/BPT trap",               /* SIGTRAP   */
        !           288:        "IOT trap",                     /* SIGIOT    */
        !           289:        "EMT trap",                     /* SIGEMT    */
        !           290:        "Floating point exception",     /* SIGFPE    */
        !           291:        "Killed",                       /* SIGKILL   */
        !           292:        "Bus error",                    /* SIGBUS    */
        !           293:        "Segmentation fault",           /* SIGSEGV   */
        !           294:        "Bad system call",              /* SIGSYS    */
        !           295:        "Broken pipe",                  /* SIGPIPE   */
        !           296:        "Alarm clock",                  /* SIGALRM   */
        !           297:        "Terminated",                   /* SIGTERM   */
        !           298:        "User defined signal 1",        /* SIGUSR1   */
        !           299:        "User defined signal 2",        /* SIGUSR2   */
        !           300:        "Child exited",                 /* SIGCLD    */
        !           301:        "Power-fail restart",           /* SIGPWR    */
        !           302:        "Virtual timer expired",        /* SIGVTALRM */
        !           303:        "Profiling timer expired",      /* SIGPROF   */
        !           304:        "I/O possible",                 /* SIGIO     */
        !           305:        "Window size changes",          /* SIGWINDOW */
        !           306:        "Stopped (signal)",             /* SIGSTOP   */
        !           307:        "Stopped",                      /* SIGTSTP   */
        !           308:        "Continued",                    /* SIGCONT   */
        !           309:        "Stopped (tty input)",          /* SIGTTIN   */
        !           310:        "Stopped (tty output)",         /* SIGTTOU   */
        !           311:        "Urgent I/O condition",         /* SIGURG    */
        !           312:        "Remote lock lost (NFS)",       /* SIGLOST   */
        !           313:        "Signal 31",                    /* reserved  */
        !           314:        "DIL signal"                    /* SIGDIL    */
1.1       deraadt   315: };
                    316:
                    317: int
                    318: utimes(file, tvp)
                    319:     char *file;
                    320:     struct timeval tvp[2];
                    321: {
                    322:     struct utimbuf t;
                    323:
                    324:     t.actime  = tvp[0].tv_sec;
                    325:     t.modtime = tvp[1].tv_sec;
1.14    ! espie     326:     return utime(file, &t);
1.1       deraadt   327: }
                    328:
                    329:
                    330: #endif /* __hpux */
1.2       deraadt   331:
                    332: #if defined(sun) && defined(__svr4__)
                    333: #include <signal.h>
                    334:
                    335: /* turn into bsd signals */
                    336: void (*
1.14    ! espie     337: signal(s, a))()
1.2       deraadt   338:     int     s;
                    339:     void (*a)();
                    340: {
                    341:     struct sigaction sa, osa;
                    342:
1.8       deraadt   343:     memset(&sa, 0, sizeof sa);
1.2       deraadt   344:     sa.sa_handler = a;
                    345:     sigemptyset(&sa.sa_mask);
                    346:     sa.sa_flags = SA_RESTART;
                    347:
                    348:     if (sigaction(s, &sa, &osa) == -1)
                    349:        return SIG_ERR;
                    350:     else
                    351:        return osa.sa_handler;
                    352: }
                    353:
1.6       millert   354: #endif
                    355:
                    356: #ifndef BSD4_4
                    357: #ifdef __STDC__
                    358: #include <stdarg.h>
                    359: #else
                    360: #include <varargs.h>
                    361: #endif
                    362:
                    363: #ifdef _IOSTRG
1.14    ! espie     364: #define STRFLAG (_IOSTRG|_IOWRT)       /* no _IOWRT: avoid stdio bug */
1.6       millert   365: #else
1.14    ! espie     366: #define STRFLAG (_IOREAD)              /* XXX: Assume svr4 stdio */
1.6       millert   367: #endif
                    368:
                    369: int
                    370: vsnprintf(s, n, fmt, args)
                    371:        char *s;
                    372:        size_t n;
                    373:        const char *fmt;
                    374:        va_list args;
                    375: {
                    376:        FILE fakebuf;
                    377:
                    378:        fakebuf._flag = STRFLAG;
                    379:        /*
                    380:         * Some os's are char * _ptr, others are unsigned char *_ptr...
                    381:         * We cast to void * to make everyone happy.
                    382:         */
1.14    ! espie     383:        fakebuf._ptr = (void *)s;
1.6       millert   384:        fakebuf._cnt = n-1;
                    385:        fakebuf._file = -1;
                    386:        _doprnt(fmt, args, &fakebuf);
                    387:        fakebuf._cnt++;
                    388:        putc('\0', &fakebuf);
                    389:        if (fakebuf._cnt<0)
                    390:            fakebuf._cnt = 0;
1.14    ! espie     391:        return n-fakebuf._cnt-1;
1.6       millert   392: }
                    393:
                    394: int
                    395: #ifdef __STDC__
                    396: snprintf(char *s, size_t n, const char *fmt, ...)
                    397: #else
                    398: snprintf(va_alist)
                    399:        va_dcl
                    400: #endif
                    401: {
                    402:        va_list ap;
                    403:        int rv;
                    404: #ifdef __STDC__
                    405:        va_start(ap, fmt);
                    406: #else
                    407:        char *s;
                    408:        size_t n;
                    409:        const char *fmt;
                    410:
                    411:        va_start(ap);
                    412:
                    413:        s = va_arg(ap, char *);
                    414:        n = va_arg(ap, size_t);
                    415:        fmt = va_arg(ap, const char *);
                    416: #endif
                    417:        rv = vsnprintf(s, n, fmt, ap);
                    418:        va_end(ap);
                    419:        return rv;
1.11      espie     420: }
                    421: #endif
                    422: #ifdef NEED_STRSTR
                    423: char *
                    424: strstr(string, substring)
                    425:        const char *string;             /* String to search. */
                    426:        const char *substring;          /* Substring to find in string */
                    427: {
                    428:        const char *a, *b;
                    429:
                    430:        /*
                    431:         * First scan quickly through the two strings looking for a single-
                    432:         * character match.  When it's found, then compare the rest of the
                    433:         * substring.
                    434:         */
                    435:
                    436:        for (b = substring; *string != 0; string += 1) {
                    437:                if (*string != *b)
                    438:                        continue;
                    439:                a = string;
                    440:                for (;;) {
                    441:                        if (*b == 0)
                    442:                                return (char *)string;
                    443:                        if (*a++ != *b++)
                    444:                                break;
                    445:                }
                    446:                b = substring;
                    447:        }
                    448:        return NULL;
1.12      espie     449: }
                    450: #endif
                    451:
                    452: #ifdef NEED_FGETLN
                    453: char *
                    454: fgetln(stream, len)
                    455:     FILE *stream;
                    456:     size_t *len;
                    457: {
                    458:     static char *buffer = NULL;
                    459:     static size_t buflen = 0;
                    460:
                    461:     if (buflen == 0) {
1.14    ! espie     462:        buflen = 512;
1.12      espie     463:        buffer = emalloc(buflen+1);
                    464:     }
                    465:     if (fgets(buffer, buflen+1, stream) == NULL)
                    466:        return NULL;
                    467:     *len = strlen(buffer);
                    468:     while (*len == buflen && buffer[*len-1] != '\n') {
                    469:        buffer = erealloc(buffer, 2*buflen + 1);
                    470:        if (fgets(buffer + buflen, buflen + 1, stream) == NULL)
                    471:            return NULL;
                    472:        *len += strlen(buffer + buflen);
                    473:        buflen *= 2;
                    474:     }
                    475:     return buffer;
1.6       millert   476: }
1.2       deraadt   477: #endif