#include "sys_defs.h"
#ifdef MISSING_STRERROR
extern int errno;
extern char *sys_errlist[];
extern int sys_nerr;
#include <vstring.h>
const char *strerror(int err)
{
static VSTRING *buf;
if (err < 0 || err >= sys_nerr) {
if (buf == 0)
buf = vstring_alloc(10);
vstring_sprintf(buf, "Unknown error %d", err);
return (vstring_str(buf));
} else {
return (sys_errlist[errno]);
}
}
#endif
#ifdef MISSING_SETENV
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int setenv(const char *name, const char *value, int clobber)
{
char *cp;
if (clobber == 0 && getenv(name) != 0)
return (0);
if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0)
return (1);
sprintf(cp, "%s=%s", name, value);
return (putenv(cp));
}
#endif
#ifdef MISSING_SETEUID
#ifdef HAVE_SETRESUID
#include <unistd.h>
int seteuid(uid_t euid)
{
return setresuid(-1, euid, -1);
}
#else
#error MISSING_SETEUID
#endif
#endif
#ifdef MISSING_SETEGID
#ifdef HAVE_SETRESGID
#include <unistd.h>
int setegid(gid_t egid)
{
return setresgid(-1, egid, -1);
}
#else
#error MISSING_SETEGID
#endif
#endif
#ifdef MISSING_MKFIFO
#include <sys/stat.h>
int mkfifo(char *path, int mode)
{
return mknod(path, (mode & ~_S_IFMT) | _S_IFIFO, 0);
}
#endif
#ifdef MISSING_WAITPID
#ifdef HAS_WAIT4
#include <sys/wait.h>
#include <errno.h>
int waitpid(int pid, WAIT_STATUS_T *status, int options)
{
if (pid == -1)
pid = 0;
return wait4(pid, status, options, (struct rusage *) 0);
}
#else
#error MISSING_WAITPID
#endif
#endif
#ifdef MISSING_SETSID
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifdef TIOCNOTTY
#include <msg.h>
int setsid(void)
{
int p = getpid();
int fd;
if (setpgrp(p, p))
return -1;
fd = open("/dev/tty", O_RDONLY, 0);
if (fd >= 0 || errno != ENXIO) {
if (fd < 0) {
msg_warn("open /dev/tty: %m");
return -1;
}
if (ioctl(fd, TIOCNOTTY, 0)) {
msg_warn("ioctl TIOCNOTTY: %m");
return -1;
}
close(fd);
}
return 0;
}
#else
#error MISSING_SETSID
#endif
#endif
#ifdef DUP2_DUPS_CLOSE_ON_EXEC
#include "iostuff.h"
int dup2_pass_on_exec(int oldd, int newd)
{
int res;
if ((res = dup2(oldd, newd)) >= 0)
close_on_exec(newd, PASS_ON_EXEC);
return res;
}
#endif