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