[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.3

1.3     ! deraadt     1: /*     $OpenBSD: util.c,v 1.5 1995/11/22 17:40:17 christos Exp $       */
1.2       deraadt     2: /*     $NetBSD: util.c,v 1.5 1995/11/22 17:40:17 christos Exp $        */
1.1       deraadt     3:
                      4: /*
                      5:  * Missing stuff from OS's
                      6:  *
                      7:  */
                      8:
                      9: #ifndef lint
1.3     ! deraadt    10: static char rcsid[] = "$OpenBSD: util.c,v 1.2 1995/12/14 03:23:39 deraadt Exp $";
1.1       deraadt    11: #endif
                     12:
                     13: #include <stdio.h>
                     14: #include "make.h"
                     15:
                     16: #if !__STDC__
                     17: # ifndef const
                     18: #  define const
                     19: # endif
                     20: #endif
                     21:
                     22: #ifdef sun
                     23:
                     24:
                     25:
                     26: extern int errno, sys_nerr;
                     27: extern char *sys_errlist[];
                     28:
                     29: char *
                     30: strerror(e)
                     31:     int e;
                     32: {
                     33:     static char buf[100];
                     34:     if (e < 0 || e >= sys_nerr) {
                     35:        sprintf(buf, "Unknown error %d", e);
                     36:        return buf;
                     37:     }
                     38:     else
                     39:        return sys_errlist[e];
                     40: }
                     41: #endif
                     42:
                     43: #if defined(sun) || defined(__hpux)
                     44:
                     45: int
                     46: setenv(name, value, dum)
                     47:     const char *name;
                     48:     const char *value;
                     49:     int dum;
                     50: {
                     51:     register char *p;
                     52:     int len = strlen(name) + strlen(value) + 2; /* = \0 */
                     53:     char *ptr = (char*) malloc(len);
                     54:
                     55:     (void) dum;
                     56:
                     57:     if (ptr == NULL)
                     58:        return -1;
                     59:
                     60:     p = ptr;
                     61:
                     62:     while (*name)
                     63:        *p++ = *name++;
                     64:
                     65:     *p++ = '=';
                     66:
                     67:     while (*value)
                     68:        *p++ = *value++;
                     69:
                     70:     *p = '\0';
                     71:
                     72:     len = putenv(ptr);
                     73: /*    free(ptr); */
                     74:     return len;
                     75: }
                     76: #endif
                     77:
                     78: #ifdef __hpux
                     79: #include <sys/types.h>
                     80: #include <sys/param.h>
                     81: #include <sys/syscall.h>
                     82: #include <sys/signal.h>
                     83: #include <sys/stat.h>
                     84: #include <stdio.h>
                     85: #include <dirent.h>
                     86: #include <sys/time.h>
                     87: #include <time.h>
                     88: #include <unistd.h>
                     89:
                     90:
                     91: int
                     92: killpg(pid, sig)
                     93:     int pid, sig;
                     94: {
                     95:     return kill(-pid, sig);
                     96: }
                     97:
                     98: void
                     99: srandom(seed)
                    100:     long seed;
                    101: {
                    102:     srand48(seed);
                    103: }
                    104:
                    105: long
                    106: random()
                    107: {
                    108:     return lrand48();
                    109: }
                    110:
                    111: /* turn into bsd signals */
                    112: void (*
                    113: signal(s, a)) ()
                    114:     int     s;
                    115:     void (*a)();
                    116: {
                    117:     struct sigvec osv, sv;
                    118:
                    119:     (void) sigvector(s, (struct sigvec *) 0, &osv);
                    120:     sv = osv;
                    121:     sv.sv_handler = a;
                    122: #ifdef SV_BSDSIG
                    123:     sv.sv_flags = SV_BSDSIG;
                    124: #endif
                    125:
                    126:     if (sigvector(s, &sv, (struct sigvec *) 0) == -1)
                    127:         return (BADSIG);
                    128:     return (osv.sv_handler);
                    129: }
                    130:
                    131: #if !defined(BSD) && !defined(d_fileno)
                    132: # define d_fileno d_ino
                    133: #endif
                    134:
                    135: #ifndef DEV_DEV_COMPARE
                    136: # define DEV_DEV_COMPARE(a, b) ((a) == (b))
                    137: #endif
                    138: #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
                    139: #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
                    140:
                    141:
                    142: /* strrcpy():
                    143:  *     Like strcpy, going backwards and returning the new pointer
                    144:  */
                    145: static char *
                    146: strrcpy(ptr, str)
                    147:     register char *ptr, *str;
                    148: {
                    149:     register int len = strlen(str);
                    150:
                    151:     while (len)
                    152:        *--ptr = str[--len];
                    153:
                    154:     return (ptr);
                    155: } /* end strrcpy */
                    156:
                    157:
                    158: char   *
                    159: getwd(pathname)
                    160:     char   *pathname;
                    161: {
                    162:     DIR    *dp;
                    163:     struct dirent *d;
                    164:     extern int errno;
                    165:
                    166:     struct stat st_root, st_cur, st_next, st_dotdot;
                    167:     char    pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
                    168:     char   *pathptr, *nextpathptr, *cur_name_add;
                    169:
                    170:     /* find the inode of root */
                    171:     if (stat("/", &st_root) == -1) {
                    172:        (void) sprintf(pathname,
                    173:                        "getwd: Cannot stat \"/\" (%s)", strerror(errno));
                    174:        return (NULL);
                    175:     }
                    176:     pathbuf[MAXPATHLEN - 1] = '\0';
                    177:     pathptr = &pathbuf[MAXPATHLEN - 1];
                    178:     nextpathbuf[MAXPATHLEN - 1] = '\0';
                    179:     cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
                    180:
                    181:     /* find the inode of the current directory */
                    182:     if (lstat(".", &st_cur) == -1) {
                    183:        (void) sprintf(pathname,
                    184:                        "getwd: Cannot stat \".\" (%s)", strerror(errno));
                    185:        return (NULL);
                    186:     }
                    187:     nextpathptr = strrcpy(nextpathptr, "../");
                    188:
                    189:     /* Descend to root */
                    190:     for (;;) {
                    191:
                    192:        /* look if we found root yet */
                    193:        if (st_cur.st_ino == st_root.st_ino &&
                    194:            DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
                    195:            (void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
                    196:            return (pathname);
                    197:        }
                    198:
                    199:        /* open the parent directory */
                    200:        if (stat(nextpathptr, &st_dotdot) == -1) {
                    201:            (void) sprintf(pathname,
                    202:                            "getwd: Cannot stat directory \"%s\" (%s)",
                    203:                            nextpathptr, strerror(errno));
                    204:            return (NULL);
                    205:        }
                    206:        if ((dp = opendir(nextpathptr)) == NULL) {
                    207:            (void) sprintf(pathname,
                    208:                            "getwd: Cannot open directory \"%s\" (%s)",
                    209:                            nextpathptr, strerror(errno));
                    210:            return (NULL);
                    211:        }
                    212:
                    213:        /* look in the parent for the entry with the same inode */
                    214:        if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
                    215:            /* Parent has same device. No need to stat every member */
                    216:            for (d = readdir(dp); d != NULL; d = readdir(dp))
                    217:                if (d->d_fileno == st_cur.st_ino)
                    218:                    break;
                    219:        }
                    220:        else {
                    221:            /*
                    222:             * Parent has a different device. This is a mount point so we
                    223:             * need to stat every member
                    224:             */
                    225:            for (d = readdir(dp); d != NULL; d = readdir(dp)) {
                    226:                if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
                    227:                    continue;
                    228:                (void) strcpy(cur_name_add, d->d_name);
                    229:                if (lstat(nextpathptr, &st_next) == -1) {
                    230:                    (void) sprintf(pathname, "getwd: Cannot stat \"%s\" (%s)",
                    231:                                    d->d_name, strerror(errno));
                    232:                    (void) closedir(dp);
                    233:                    return (NULL);
                    234:                }
                    235:                /* check if we found it yet */
                    236:                if (st_next.st_ino == st_cur.st_ino &&
                    237:                    DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
                    238:                    break;
                    239:            }
                    240:        }
                    241:        if (d == NULL) {
                    242:            (void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
                    243:            (void) closedir(dp);
                    244:            return (NULL);
                    245:        }
                    246:        st_cur = st_dotdot;
                    247:        pathptr = strrcpy(pathptr, d->d_name);
                    248:        pathptr = strrcpy(pathptr, "/");
                    249:        nextpathptr = strrcpy(nextpathptr, "../");
                    250:        (void) closedir(dp);
                    251:        *cur_name_add = '\0';
                    252:     }
                    253: } /* end getwd */
                    254:
                    255:
                    256: char    *sys_siglist[] = {
                    257:         "Signal 0",
                    258:         "Hangup",                       /* SIGHUP    */
                    259:         "Interrupt",                    /* SIGINT    */
                    260:         "Quit",                         /* SIGQUIT   */
                    261:         "Illegal instruction",          /* SIGILL    */
                    262:         "Trace/BPT trap",               /* SIGTRAP   */
                    263:         "IOT trap",                     /* SIGIOT    */
                    264:         "EMT trap",                     /* SIGEMT    */
                    265:         "Floating point exception",     /* SIGFPE    */
                    266:         "Killed",                       /* SIGKILL   */
                    267:         "Bus error",                    /* SIGBUS    */
                    268:         "Segmentation fault",           /* SIGSEGV   */
                    269:         "Bad system call",              /* SIGSYS    */
                    270:         "Broken pipe",                  /* SIGPIPE   */
                    271:         "Alarm clock",                  /* SIGALRM   */
                    272:         "Terminated",                   /* SIGTERM   */
                    273:         "User defined signal 1",        /* SIGUSR1   */
                    274:         "User defined signal 2",        /* SIGUSR2   */
                    275:         "Child exited",                 /* SIGCLD    */
                    276:         "Power-fail restart",           /* SIGPWR    */
                    277:         "Virtual timer expired",        /* SIGVTALRM */
                    278:         "Profiling timer expired",      /* SIGPROF   */
                    279:         "I/O possible",                 /* SIGIO     */
                    280:         "Window size changes",          /* SIGWINDOW */
                    281:         "Stopped (signal)",             /* SIGSTOP   */
                    282:         "Stopped",                      /* SIGTSTP   */
                    283:         "Continued",                    /* SIGCONT   */
                    284:         "Stopped (tty input)",          /* SIGTTIN   */
                    285:         "Stopped (tty output)",         /* SIGTTOU   */
                    286:         "Urgent I/O condition",         /* SIGURG    */
                    287:         "Remote lock lost (NFS)",       /* SIGLOST   */
                    288:         "Signal 31",                    /* reserved  */
                    289:         "DIL signal"                    /* SIGDIL    */
                    290: };
                    291:
                    292: int
                    293: utimes(file, tvp)
                    294:     char *file;
                    295:     struct timeval tvp[2];
                    296: {
                    297:     struct utimbuf t;
                    298:
                    299:     t.actime  = tvp[0].tv_sec;
                    300:     t.modtime = tvp[1].tv_sec;
                    301:     return(utime(file, &t));
                    302: }
                    303:
                    304:
                    305: #endif /* __hpux */
1.2       deraadt   306:
                    307: #if defined(sun) && defined(__svr4__)
                    308: #include <signal.h>
                    309:
                    310: /* turn into bsd signals */
                    311: void (*
                    312: signal(s, a)) ()
                    313:     int     s;
                    314:     void (*a)();
                    315: {
                    316:     struct sigaction sa, osa;
                    317:
                    318:     sa.sa_handler = a;
                    319:     sigemptyset(&sa.sa_mask);
                    320:     sa.sa_flags = SA_RESTART;
                    321:
                    322:     if (sigaction(s, &sa, &osa) == -1)
                    323:        return SIG_ERR;
                    324:     else
                    325:        return osa.sa_handler;
                    326: }
                    327:
                    328: #endif