Strengthen the pledge() polyfill

This commit is contained in:
Justine Tunney 2022-06-27 13:01:58 -07:00
parent a6f65eea7c
commit 3c92adfd6e
79 changed files with 1457 additions and 357 deletions

View file

@ -22,32 +22,39 @@
#include "libc/calls/struct/winsize.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/errno.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"
/**
* Returns true if file descriptor is backed by a terminal device.
* Tells if file descriptor is a terminal.
*
* @param fd is file descriptor
* @return 1 if is terminal, otherwise 0 w/ errno
* @raise EBADF if fd isn't a valid file descriptor
* @raise ENOTTY if fd is something other than a terminal
* @raise EPERM if pledge() was used without tty
*/
bool32 isatty(int fd) {
int e;
bool32 res;
struct winsize ws;
e = errno;
if (fd >= 0) {
if (__isfdkind(fd, kFdZip)) {
res = false;
} else if (IsMetal()) {
res = false;
} else if (!IsWindows()) {
res = sys_ioctl(fd, TIOCGWINSZ, &ws) != -1;
} else {
res = sys_isatty_nt(fd);
}
if (__isfdkind(fd, kFdZip)) {
enotty();
res = false;
} else if (IsWindows()) {
res = sys_isatty_nt(fd);
} else if (IsMetal()) {
res = sys_isatty_metal(fd);
} else if (!sys_ioctl(fd, TIOCGWINSZ, &ws)) {
res = true;
} else {
res = false;
if (errno != EBADF && errno != EPERM) {
enotty();
}
}
STRACE("isatty(%d) → %hhhd% m", fd, res);
errno = e;
return res;
}