version 1.13, 2000/09/14 13:32:08 |
version 1.14, 2001/05/03 13:41:12 |
|
|
|
/* $OpenPackages$ */ |
/* $OpenBSD$ */ |
/* $OpenBSD$ */ |
/* $NetBSD: util.c,v 1.10 1996/12/31 17:56:04 christos Exp $ */ |
/* $NetBSD: util.c,v 1.10 1996/12/31 17:56:04 christos Exp $ */ |
|
|
|
|
* Missing stuff from OS's |
* Missing stuff from OS's |
*/ |
*/ |
|
|
#include <stdio.h> |
|
#include "make.h" |
|
#include <sys/param.h> |
|
|
|
#ifndef lint |
#ifndef lint |
UNUSED |
|
static char rcsid[] = "$OpenBSD$"; |
static char rcsid[] = "$OpenBSD$"; |
#endif |
#endif |
|
|
|
#include <stdio.h> |
|
#include "make.h" |
|
#include <sys/param.h> |
|
|
#ifndef __STDC__ |
#ifndef __STDC__ |
# ifndef const |
# ifndef const |
# define const |
# define const |
|
|
const char *value; |
const char *value; |
int dum; |
int dum; |
{ |
{ |
register char *p; |
char *p; |
int len = strlen(name) + strlen(value) + 2; /* = \0 */ |
int len = strlen(name) + strlen(value) + 2; /* = \0 */ |
char *ptr = (char*) malloc(len); |
char *ptr = (char*)malloc(len); |
|
|
(void) dum; |
|
|
|
if (ptr == NULL) |
if (ptr == NULL) |
return -1; |
return -1; |
|
|
|
|
|
|
/* turn into bsd signals */ |
/* turn into bsd signals */ |
void (* |
void (* |
signal(s, a)) () |
signal(s, a))() |
int s; |
int s; |
void (*a)(); |
void (*a)(); |
{ |
{ |
struct sigvec osv, sv; |
struct sigvec osv, sv; |
|
|
(void) sigvector(s, (struct sigvec *) 0, &osv); |
(void)sigvector(s, (struct sigvec *)0, &osv); |
sv = osv; |
sv = osv; |
sv.sv_handler = a; |
sv.sv_handler = a; |
#ifdef SV_BSDSIG |
#ifdef SV_BSDSIG |
sv.sv_flags = SV_BSDSIG; |
sv.sv_flags = SV_BSDSIG; |
#endif |
#endif |
|
|
if (sigvector(s, &sv, (struct sigvec *) 0) == -1) |
if (sigvector(s, &sv, (struct sigvec *)0) == -1) |
return (SIG_ERR); |
return SIG_ERR; |
return (osv.sv_handler); |
return osv.sv_handler; |
} |
} |
|
|
#if !defined(BSD) && !defined(d_fileno) |
#if !defined(BSD) && !defined(d_fileno) |
|
|
#ifndef DEV_DEV_COMPARE |
#ifndef DEV_DEV_COMPARE |
# define DEV_DEV_COMPARE(a, b) ((a) == (b)) |
# define DEV_DEV_COMPARE(a, b) ((a) == (b)) |
#endif |
#endif |
#define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/'))) |
#define ISDOT(c) ((c)[0] == '.' && ((c)[1] == '\0' || (c)[1] == '/')) |
#define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1]))) |
#define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1]))) |
|
|
|
|
|
|
*/ |
*/ |
static char * |
static char * |
strrcpy(ptr, str) |
strrcpy(ptr, str) |
register char *ptr, *str; |
char *ptr, *str; |
{ |
{ |
register int len = strlen(str); |
size_t len = strlen(str); |
|
|
while (len) |
while (len) |
*--ptr = str[--len]; |
*--ptr = str[--len]; |
|
|
return (ptr); |
return ptr; |
} /* end strrcpy */ |
} /* end strrcpy */ |
|
|
|
|
|
|
|
|
/* find the inode of root */ |
/* find the inode of root */ |
if (stat("/", &st_root) == -1) { |
if (stat("/", &st_root) == -1) { |
(void) sprintf(pathname, |
(void)sprintf(pathname, |
"getwd: Cannot stat \"/\" (%s)", strerror(errno)); |
"getwd: Cannot stat \"/\" (%s)", strerror(errno)); |
return (NULL); |
return NULL; |
} |
} |
pathbuf[MAXPATHLEN - 1] = '\0'; |
pathbuf[MAXPATHLEN - 1] = '\0'; |
pathptr = &pathbuf[MAXPATHLEN - 1]; |
pathptr = &pathbuf[MAXPATHLEN - 1]; |
|
|
|
|
/* find the inode of the current directory */ |
/* find the inode of the current directory */ |
if (lstat(".", &st_cur) == -1) { |
if (lstat(".", &st_cur) == -1) { |
(void) sprintf(pathname, |
(void)sprintf(pathname, |
"getwd: Cannot stat \".\" (%s)", strerror(errno)); |
"getwd: Cannot stat \".\" (%s)", strerror(errno)); |
return (NULL); |
return NULL; |
} |
} |
nextpathptr = strrcpy(nextpathptr, "../"); |
nextpathptr = strrcpy(nextpathptr, "../"); |
|
|
|
|
/* look if we found root yet */ |
/* look if we found root yet */ |
if (st_cur.st_ino == st_root.st_ino && |
if (st_cur.st_ino == st_root.st_ino && |
DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) { |
DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) { |
(void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr); |
(void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr); |
return (pathname); |
return pathname; |
} |
} |
|
|
/* open the parent directory */ |
/* open the parent directory */ |
if (stat(nextpathptr, &st_dotdot) == -1) { |
if (stat(nextpathptr, &st_dotdot) == -1) { |
(void) sprintf(pathname, |
(void)sprintf(pathname, |
"getwd: Cannot stat directory \"%s\" (%s)", |
"getwd: Cannot stat directory \"%s\" (%s)", |
nextpathptr, strerror(errno)); |
nextpathptr, strerror(errno)); |
return (NULL); |
return NULL; |
} |
} |
if ((dp = opendir(nextpathptr)) == NULL) { |
if ((dp = opendir(nextpathptr)) == NULL) { |
(void) sprintf(pathname, |
(void)sprintf(pathname, |
"getwd: Cannot open directory \"%s\" (%s)", |
"getwd: Cannot open directory \"%s\" (%s)", |
nextpathptr, strerror(errno)); |
nextpathptr, strerror(errno)); |
return (NULL); |
return NULL; |
} |
} |
|
|
/* look in the parent for the entry with the same inode */ |
/* look in the parent for the entry with the same inode */ |
|
|
for (d = readdir(dp); d != NULL; d = readdir(dp)) { |
for (d = readdir(dp); d != NULL; d = readdir(dp)) { |
if (ISDOT(d->d_name) || ISDOTDOT(d->d_name)) |
if (ISDOT(d->d_name) || ISDOTDOT(d->d_name)) |
continue; |
continue; |
(void) strcpy(cur_name_add, d->d_name); |
(void)strcpy(cur_name_add, d->d_name); |
if (lstat(nextpathptr, &st_next) == -1) { |
if (lstat(nextpathptr, &st_next) == -1) { |
(void) sprintf(pathname, "getwd: Cannot stat \"%s\" (%s)", |
(void)sprintf(pathname, "getwd: Cannot stat \"%s\" (%s)", |
d->d_name, strerror(errno)); |
d->d_name, strerror(errno)); |
(void) closedir(dp); |
(void)closedir(dp); |
return (NULL); |
return NULL; |
} |
} |
/* check if we found it yet */ |
/* check if we found it yet */ |
if (st_next.st_ino == st_cur.st_ino && |
if (st_next.st_ino == st_cur.st_ino && |
|
|
} |
} |
} |
} |
if (d == NULL) { |
if (d == NULL) { |
(void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\""); |
(void)sprintf(pathname, "getwd: Cannot find \".\" in \"..\""); |
(void) closedir(dp); |
(void)closedir(dp); |
return (NULL); |
return NULL; |
} |
} |
st_cur = st_dotdot; |
st_cur = st_dotdot; |
pathptr = strrcpy(pathptr, d->d_name); |
pathptr = strrcpy(pathptr, d->d_name); |
pathptr = strrcpy(pathptr, "/"); |
pathptr = strrcpy(pathptr, "/"); |
nextpathptr = strrcpy(nextpathptr, "../"); |
nextpathptr = strrcpy(nextpathptr, "../"); |
(void) closedir(dp); |
(void)closedir(dp); |
*cur_name_add = '\0'; |
*cur_name_add = '\0'; |
} |
} |
} /* end getwd */ |
} /* end getwd */ |
|
|
|
|
char *sys_siglist[] = { |
char *sys_siglist[] = { |
"Signal 0", |
"Signal 0", |
"Hangup", /* SIGHUP */ |
"Hangup", /* SIGHUP */ |
"Interrupt", /* SIGINT */ |
"Interrupt", /* SIGINT */ |
"Quit", /* SIGQUIT */ |
"Quit", /* SIGQUIT */ |
"Illegal instruction", /* SIGILL */ |
"Illegal instruction", /* SIGILL */ |
"Trace/BPT trap", /* SIGTRAP */ |
"Trace/BPT trap", /* SIGTRAP */ |
"IOT trap", /* SIGIOT */ |
"IOT trap", /* SIGIOT */ |
"EMT trap", /* SIGEMT */ |
"EMT trap", /* SIGEMT */ |
"Floating point exception", /* SIGFPE */ |
"Floating point exception", /* SIGFPE */ |
"Killed", /* SIGKILL */ |
"Killed", /* SIGKILL */ |
"Bus error", /* SIGBUS */ |
"Bus error", /* SIGBUS */ |
"Segmentation fault", /* SIGSEGV */ |
"Segmentation fault", /* SIGSEGV */ |
"Bad system call", /* SIGSYS */ |
"Bad system call", /* SIGSYS */ |
"Broken pipe", /* SIGPIPE */ |
"Broken pipe", /* SIGPIPE */ |
"Alarm clock", /* SIGALRM */ |
"Alarm clock", /* SIGALRM */ |
"Terminated", /* SIGTERM */ |
"Terminated", /* SIGTERM */ |
"User defined signal 1", /* SIGUSR1 */ |
"User defined signal 1", /* SIGUSR1 */ |
"User defined signal 2", /* SIGUSR2 */ |
"User defined signal 2", /* SIGUSR2 */ |
"Child exited", /* SIGCLD */ |
"Child exited", /* SIGCLD */ |
"Power-fail restart", /* SIGPWR */ |
"Power-fail restart", /* SIGPWR */ |
"Virtual timer expired", /* SIGVTALRM */ |
"Virtual timer expired", /* SIGVTALRM */ |
"Profiling timer expired", /* SIGPROF */ |
"Profiling timer expired", /* SIGPROF */ |
"I/O possible", /* SIGIO */ |
"I/O possible", /* SIGIO */ |
"Window size changes", /* SIGWINDOW */ |
"Window size changes", /* SIGWINDOW */ |
"Stopped (signal)", /* SIGSTOP */ |
"Stopped (signal)", /* SIGSTOP */ |
"Stopped", /* SIGTSTP */ |
"Stopped", /* SIGTSTP */ |
"Continued", /* SIGCONT */ |
"Continued", /* SIGCONT */ |
"Stopped (tty input)", /* SIGTTIN */ |
"Stopped (tty input)", /* SIGTTIN */ |
"Stopped (tty output)", /* SIGTTOU */ |
"Stopped (tty output)", /* SIGTTOU */ |
"Urgent I/O condition", /* SIGURG */ |
"Urgent I/O condition", /* SIGURG */ |
"Remote lock lost (NFS)", /* SIGLOST */ |
"Remote lock lost (NFS)", /* SIGLOST */ |
"Signal 31", /* reserved */ |
"Signal 31", /* reserved */ |
"DIL signal" /* SIGDIL */ |
"DIL signal" /* SIGDIL */ |
}; |
}; |
|
|
int |
int |
|
|
|
|
t.actime = tvp[0].tv_sec; |
t.actime = tvp[0].tv_sec; |
t.modtime = tvp[1].tv_sec; |
t.modtime = tvp[1].tv_sec; |
return(utime(file, &t)); |
return utime(file, &t); |
} |
} |
|
|
|
|
|
|
|
|
/* turn into bsd signals */ |
/* turn into bsd signals */ |
void (* |
void (* |
signal(s, a)) () |
signal(s, a))() |
int s; |
int s; |
void (*a)(); |
void (*a)(); |
{ |
{ |
|
|
#endif |
#endif |
|
|
#ifdef _IOSTRG |
#ifdef _IOSTRG |
#define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */ |
#define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */ |
#else |
#else |
#define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */ |
#define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */ |
#endif |
#endif |
|
|
int |
int |
|
|
* Some os's are char * _ptr, others are unsigned char *_ptr... |
* Some os's are char * _ptr, others are unsigned char *_ptr... |
* We cast to void * to make everyone happy. |
* We cast to void * to make everyone happy. |
*/ |
*/ |
fakebuf._ptr = (void *) s; |
fakebuf._ptr = (void *)s; |
fakebuf._cnt = n-1; |
fakebuf._cnt = n-1; |
fakebuf._file = -1; |
fakebuf._file = -1; |
_doprnt(fmt, args, &fakebuf); |
_doprnt(fmt, args, &fakebuf); |
|
|
putc('\0', &fakebuf); |
putc('\0', &fakebuf); |
if (fakebuf._cnt<0) |
if (fakebuf._cnt<0) |
fakebuf._cnt = 0; |
fakebuf._cnt = 0; |
return (n-fakebuf._cnt-1); |
return n-fakebuf._cnt-1; |
} |
} |
|
|
int |
int |
|
|
return rv; |
return rv; |
} |
} |
#endif |
#endif |
|
|
#ifdef NEED_STRSTR |
#ifdef NEED_STRSTR |
char * |
char * |
strstr(string, substring) |
strstr(string, substring) |
|
|
static size_t buflen = 0; |
static size_t buflen = 0; |
|
|
if (buflen == 0) { |
if (buflen == 0) { |
buflen = 512; |
buflen = 512; |
buffer = emalloc(buflen+1); |
buffer = emalloc(buflen+1); |
} |
} |
if (fgets(buffer, buflen+1, stream) == NULL) |
if (fgets(buffer, buflen+1, stream) == NULL) |