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