#define _GNU_SOURCE
#include <fcntl.h>
#include <stdarg.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include "util.h"
#include "out.h"
#include "os.h"
int
os_open(const char *pathname, int flags, ...)
{
int mode_required = (flags & O_CREAT) == O_CREAT;
#ifdef O_TMPFILE
mode_required |= (flags & O_TMPFILE) == O_TMPFILE;
#endif
if (mode_required) {
va_list arg;
va_start(arg, flags);
int mode = va_arg(arg, int);
va_end(arg);
return open(pathname, flags, (mode_t)mode);
} else {
return open(pathname, flags);
}
}
int
os_stat(const char *pathname, os_stat_t *buf)
{
return stat(pathname, buf);
}
int
os_unlink(const char *pathname)
{
return unlink(pathname);
}
int
os_access(const char *pathname, int mode)
{
return access(pathname, mode);
}
FILE *
os_fopen(const char *pathname, const char *mode)
{
return fopen(pathname, mode);
}
FILE *
os_fdopen(int fd, const char *mode)
{
return fdopen(fd, mode);
}
int
os_chmod(const char *pathname, mode_t mode)
{
return chmod(pathname, mode);
}
int
os_mkstemp(char *temp)
{
return mkstemp(temp);
}
int
os_posix_fallocate(int fd, os_off_t offset, off_t len)
{
return posix_fallocate(fd, offset, len);
}
int
os_ftruncate(int fd, os_off_t length)
{
return ftruncate(fd, length);
}
int
os_flock(int fd, int operation)
{
int opt = 0;
if (operation & OS_LOCK_EX)
opt |= LOCK_EX;
if (operation & OS_LOCK_SH)
opt |= LOCK_SH;
if (operation & OS_LOCK_UN)
opt |= LOCK_UN;
if (operation & OS_LOCK_NB)
opt |= LOCK_NB;
return flock(fd, opt);
}
ssize_t
os_writev(int fd, const struct iovec *iov, int iovcnt)
{
return writev(fd, iov, iovcnt);
}
int
os_clock_gettime(int id, struct timespec *ts)
{
return clock_gettime(id, ts);
}
int
os_rand_r(unsigned *seedp)
{
return rand_r(seedp);
}
int
os_unsetenv(const char *name)
{
return unsetenv(name);
}
int
os_setenv(const char *name, const char *value, int overwrite)
{
return setenv(name, value, overwrite);
}
#ifdef __FreeBSD__
static char *
secure_getenv(const char *name)
{
if (issetugid() != 0)
return NULL;
return getenv(name);
}
#endif
char *
os_getenv(const char *name)
{
return secure_getenv(name);
}
const char *os_strsignal(int sig) {
return strsignal(sig);
}