#include <stdio.h>
#include <errno.h>
#include "make.h"
#ifndef lint
__RCSID("$FreeBSD: src/usr.bin/make/util.c,v 1.7 2000/07/09 00:06:22 wsanchez Exp $");
#endif
#if !__STDC__
# ifndef const
# define const
# endif
#endif
#ifdef sun
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *
strerror(e)
int e;
{
static char buf[100];
if (e < 0 || e >= sys_nerr) {
sprintf(buf, "Unknown error %d", e);
return buf;
}
else
return sys_errlist[e];
}
#endif
#ifdef ultrix
#include <string.h>
char *
strdup(str)
const char *str;
{
size_t len;
if (str == NULL)
return NULL;
len = strlen(str) + 1;
if ((p = malloc(len)) == NULL)
return NULL;
return memcpy(p, str, len);
}
#endif
#if defined(sun) || defined(__hpux) || defined(__sgi)
int
setenv(name, value, dum)
const char *name;
const char *value;
int dum;
{
register char *p;
int len = strlen(name) + strlen(value) + 2;
char *ptr = (char*) malloc(len);
(void) dum;
if (ptr == NULL)
return -1;
p = ptr;
while (*name)
*p++ = *name++;
*p++ = '=';
while (*value)
*p++ = *value++;
*p = '\0';
len = putenv(ptr);
return len;
}
#endif
#ifdef __hpux
#include <sys/types.h>
#include <sys/param.h>
#include <sys/syscall.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
int
killpg(pid, sig)
int pid, sig;
{
return kill(-pid, sig);
}
void
srandom(seed)
long seed;
{
srand48(seed);
}
long
random()
{
return lrand48();
}
void (*
signal(s, a)) ()
int s;
void (*a)();
{
struct sigvec osv, sv;
(void) sigvector(s, (struct sigvec *) 0, &osv);
sv = osv;
sv.sv_handler = a;
#ifdef SV_BSDSIG
sv.sv_flags = SV_BSDSIG;
#endif
if (sigvector(s, &sv, (struct sigvec *) 0) == -1)
return (BADSIG);
return (osv.sv_handler);
}
#if !defined(BSD) && !defined(d_fileno)
# define d_fileno d_ino
#endif
#ifndef DEV_DEV_COMPARE
# define DEV_DEV_COMPARE(a, b) ((a) == (b))
#endif
#define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
#define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
static char *
strrcpy(ptr, str)
register char *ptr, *str;
{
register int len = strlen(str);
while (len)
*--ptr = str[--len];
return (ptr);
}
char *
getwd(pathname)
char *pathname;
{
DIR *dp;
struct dirent *d;
struct stat st_root, st_cur, st_next, st_dotdot;
char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
char *pathptr, *nextpathptr, *cur_name_add;
if (stat("/", &st_root) == -1) {
(void) sprintf(pathname,
"getwd: Cannot stat \"/\" (%s)", strerror(errno));
return (NULL);
}
pathbuf[MAXPATHLEN - 1] = '\0';
pathptr = &pathbuf[MAXPATHLEN - 1];
nextpathbuf[MAXPATHLEN - 1] = '\0';
cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
if (lstat(".", &st_cur) == -1) {
(void) sprintf(pathname,
"getwd: Cannot stat \".\" (%s)", strerror(errno));
return (NULL);
}
nextpathptr = strrcpy(nextpathptr, "../");
for (;;) {
if (st_cur.st_ino == st_root.st_ino &&
DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
(void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
return (pathname);
}
if (stat(nextpathptr, &st_dotdot) == -1) {
(void) sprintf(pathname,
"getwd: Cannot stat directory \"%s\" (%s)",
nextpathptr, strerror(errno));
return (NULL);
}
if ((dp = opendir(nextpathptr)) == NULL) {
(void) sprintf(pathname,
"getwd: Cannot open directory \"%s\" (%s)",
nextpathptr, strerror(errno));
return (NULL);
}
if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
for (d = readdir(dp); d != NULL; d = readdir(dp))
if (d->d_fileno == st_cur.st_ino)
break;
}
else {
for (d = readdir(dp); d != NULL; d = readdir(dp)) {
if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
continue;
(void) strcpy(cur_name_add, d->d_name);
if (lstat(nextpathptr, &st_next) == -1) {
(void) sprintf(pathname, "getwd: Cannot stat \"%s\" (%s)",
d->d_name, strerror(errno));
(void) closedir(dp);
return (NULL);
}
if (st_next.st_ino == st_cur.st_ino &&
DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
break;
}
}
if (d == NULL) {
(void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
(void) closedir(dp);
return (NULL);
}
st_cur = st_dotdot;
pathptr = strrcpy(pathptr, d->d_name);
pathptr = strrcpy(pathptr, "/");
nextpathptr = strrcpy(nextpathptr, "../");
(void) closedir(dp);
*cur_name_add = '\0';
}
}
char *sys_siglist[] = {
"Signal 0",
"Hangup",
"Interrupt",
"Quit",
"Illegal instruction",
"Trace/BPT trap",
"IOT trap",
"EMT trap",
"Floating point exception",
"Killed",
"Bus error",
"Segmentation fault",
"Bad system call",
"Broken pipe",
"Alarm clock",
"Terminated",
"User defined signal 1",
"User defined signal 2",
"Child exited",
"Power-fail restart",
"Virtual timer expired",
"Profiling timer expired",
"I/O possible",
"Window size changes",
"Stopped (signal)",
"Stopped",
"Continued",
"Stopped (tty input)",
"Stopped (tty output)",
"Urgent I/O condition",
"Remote lock lost (NFS)",
"Signal 31",
"DIL signal"
};
int
utimes(file, tvp)
char *file;
struct timeval tvp[2];
{
struct utimbuf t;
t.actime = tvp[0].tv_sec;
t.modtime = tvp[1].tv_sec;
return(utime(file, &t));
}
#endif
#if defined(sun) && defined(__svr4__)
#include <signal.h>
void (*
signal(s, a)) ()
int s;
void (*a)();
{
struct sigaction sa, osa;
sa.sa_handler = a;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(s, &sa, &osa) == -1)
return SIG_ERR;
else
return osa.sa_handler;
}
#endif