mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-01 00:38:31 +00:00
Apply clang-format update to repo (#1154)
Commit bc6c183
introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
This commit is contained in:
parent
342d0c81e5
commit
6e6fc38935
863 changed files with 9201 additions and 4627 deletions
|
@ -59,7 +59,8 @@ int _ptsname(int fd, char *buf, size_t size) {
|
|||
t.sn[5] = 0;
|
||||
|
||||
if (IsLinux()) {
|
||||
if (sys_ioctl(fd, TIOCGPTN, &pty)) return -1;
|
||||
if (sys_ioctl(fd, TIOCGPTN, &pty))
|
||||
return -1;
|
||||
t.sn[5] = 'p';
|
||||
t.sn[6] = 't';
|
||||
t.sn[7] = 's';
|
||||
|
|
|
@ -101,7 +101,9 @@ int cfsetispeed(struct termios *t, uint32_t speed) {
|
|||
* @asyncsignalsafe
|
||||
*/
|
||||
int cfsetspeed(struct termios *t, uint32_t speed) {
|
||||
if (cfsetispeed(t, speed) == -1) return -1;
|
||||
if (cfsetospeed(t, speed) == -1) return -1;
|
||||
if (cfsetispeed(t, speed) == -1)
|
||||
return -1;
|
||||
if (cfsetospeed(t, speed) == -1)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -33,7 +33,8 @@ textwindows int sys_chdir_nt_impl(char16_t path[hasatleast PATH_MAX],
|
|||
char16_t var[4];
|
||||
|
||||
if (len && path[len - 1] != u'\\') {
|
||||
if (len + 2 > PATH_MAX) return enametoolong();
|
||||
if (len + 2 > PATH_MAX)
|
||||
return enametoolong();
|
||||
path[len + 0] = u'\\';
|
||||
path[len + 1] = u'\0';
|
||||
}
|
||||
|
@ -84,7 +85,9 @@ textwindows int sys_chdir_nt_impl(char16_t path[hasatleast PATH_MAX],
|
|||
textwindows int sys_chdir_nt(const char *path) {
|
||||
int len;
|
||||
char16_t path16[PATH_MAX];
|
||||
if ((len = __mkntpath(path, path16)) == -1) return -1;
|
||||
if (!len) return enoent();
|
||||
if ((len = __mkntpath(path, path16)) == -1)
|
||||
return -1;
|
||||
if (!len)
|
||||
return enoent();
|
||||
return sys_chdir_nt_impl(path16, len);
|
||||
}
|
||||
|
|
|
@ -24,11 +24,16 @@
|
|||
|
||||
textwindows int _check_signal(bool restartable) {
|
||||
int status;
|
||||
if (_check_cancel() == -1) return -1;
|
||||
if (!_weaken(__sig_check)) return 0;
|
||||
if (!(status = _weaken(__sig_check)())) return 0;
|
||||
if (_check_cancel() == -1) return -1;
|
||||
if (status == 2 && restartable) return 0;
|
||||
if (_check_cancel() == -1)
|
||||
return -1;
|
||||
if (!_weaken(__sig_check))
|
||||
return 0;
|
||||
if (!(status = _weaken(__sig_check)()))
|
||||
return 0;
|
||||
if (_check_cancel() == -1)
|
||||
return -1;
|
||||
if (status == 2 && restartable)
|
||||
return 0;
|
||||
return eintr();
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,8 @@ static dontinline int __clk_tck_init(void) {
|
|||
} else {
|
||||
x = __getauxval(AT_CLKTCK).value;
|
||||
}
|
||||
if (x < 1) x = 100;
|
||||
if (x < 1)
|
||||
x = 100;
|
||||
clk_tck = x;
|
||||
return x;
|
||||
}
|
||||
|
|
|
@ -44,7 +44,8 @@ int sys_clock_gettime_mono(struct timespec *time) {
|
|||
#ifdef __x86_64__
|
||||
// intel architecture guarantees that a mapping exists between rdtsc &
|
||||
// nanoseconds only if the cpu advertises invariant timestamps support
|
||||
if (!X86_HAVE(INVTSC)) return -EINVAL;
|
||||
if (!X86_HAVE(INVTSC))
|
||||
return -EINVAL;
|
||||
#endif
|
||||
cosmo_once(&g_mono.once, sys_clock_gettime_mono_init);
|
||||
cycles = rdtsc() - g_mono.base_tick;
|
||||
|
|
|
@ -60,14 +60,17 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
|
|||
}
|
||||
return 0;
|
||||
} else if (clock == CLOCK_MONOTONIC) {
|
||||
if (!ts) return 0;
|
||||
if (!ts)
|
||||
return 0;
|
||||
return sys_clock_gettime_mono(ts);
|
||||
} else if (clock == CLOCK_BOOTTIME) {
|
||||
struct timeval x;
|
||||
size_t n = sizeof(x);
|
||||
int mib[] = {CTL_KERN, KERN_BOOTTIME};
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return -1;
|
||||
if (ts) *ts = timeval_totimespec(timeval_sub(timeval_real(), x));
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
return -1;
|
||||
if (ts)
|
||||
*ts = timeval_totimespec(timeval_sub(timeval_real(), x));
|
||||
return 0;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
|
|
|
@ -57,7 +57,8 @@ int cosmo_clock_nanosleep(int clock, int flags, const struct timespec *req,
|
|||
struct timespec quantum = timespec_fromnanos(1000000000 / CLK_TCK);
|
||||
clock_gettime(time_clock, &start);
|
||||
deadline = flags & TIMER_ABSTIME ? *req : timespec_add(start, *req);
|
||||
if (timespec_cmp(start, deadline) >= 0) return 0;
|
||||
if (timespec_cmp(start, deadline) >= 0)
|
||||
return 0;
|
||||
remain = timespec_sub(deadline, start);
|
||||
if (timespec_cmp(remain, quantum) > 0) {
|
||||
waitfor = timespec_sub(remain, quantum);
|
||||
|
|
|
@ -32,10 +32,13 @@ static textwindows int sys_clock_nanosleep_nt_impl(int clock,
|
|||
uint32_t msdelay;
|
||||
struct timespec now;
|
||||
for (;;) {
|
||||
if (sys_clock_gettime_nt(clock, &now)) return -1;
|
||||
if (timespec_cmp(now, abs) >= 0) return 0;
|
||||
if (sys_clock_gettime_nt(clock, &now))
|
||||
return -1;
|
||||
if (timespec_cmp(now, abs) >= 0)
|
||||
return 0;
|
||||
msdelay = timespec_tomillis(timespec_sub(abs, now));
|
||||
if (_park_norestart(msdelay, waitmask)) return -1;
|
||||
if (_park_norestart(msdelay, waitmask))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +51,8 @@ textwindows int sys_clock_nanosleep_nt(int clock, int flags,
|
|||
if (flags & TIMER_ABSTIME) {
|
||||
abs = *req;
|
||||
} else {
|
||||
if ((rc = sys_clock_gettime_nt(clock, &now))) goto BailOut;
|
||||
if ((rc = sys_clock_gettime_nt(clock, &now)))
|
||||
goto BailOut;
|
||||
abs = timespec_add(now, *req);
|
||||
}
|
||||
rc = sys_clock_nanosleep_nt_impl(clock, abs, m);
|
||||
|
|
|
@ -46,7 +46,8 @@ int sys_clock_nanosleep_xnu(int clock, int flags, const struct timespec *req,
|
|||
} else {
|
||||
int rc;
|
||||
struct timespec beg;
|
||||
if (rem) sys_clock_gettime_xnu(CLOCK_REALTIME, &beg);
|
||||
if (rem)
|
||||
sys_clock_gettime_xnu(CLOCK_REALTIME, &beg);
|
||||
struct timeval rel = timespec_totimeval(*req); // rounds up
|
||||
rc = sys_select(0, 0, 0, 0, &rel);
|
||||
if (rc == -1 && rem && errno == EINTR) {
|
||||
|
|
|
@ -30,7 +30,8 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
textwindows int sys_close_nt(int fd, int fildes) {
|
||||
if (fd + 0u >= g_fds.n) return ebadf();
|
||||
if (fd + 0u >= g_fds.n)
|
||||
return ebadf();
|
||||
struct Fd *f = g_fds.p + fd;
|
||||
switch (f->kind) {
|
||||
case kFdZip:
|
||||
|
|
|
@ -97,12 +97,14 @@ int close(int fd) {
|
|||
BLOCK_SIGNALS;
|
||||
__fds_lock();
|
||||
rc = close_impl(fd);
|
||||
if (!__vforked) __releasefd(fd);
|
||||
if (!__vforked)
|
||||
__releasefd(fd);
|
||||
__fds_unlock();
|
||||
ALLOW_SIGNALS;
|
||||
} else {
|
||||
rc = close_impl(fd);
|
||||
if (!__vforked) __releasefd(fd);
|
||||
if (!__vforked)
|
||||
__releasefd(fd);
|
||||
}
|
||||
STRACE("close(%d) → %d% m", fd, rc);
|
||||
return rc;
|
||||
|
|
|
@ -37,10 +37,13 @@ ssize_t copyfd(int in, int out, size_t n) {
|
|||
ssize_t dr, dw;
|
||||
for (i = 0; i < n; i += dr) {
|
||||
dr = read(in, buf, MIN(n - i, sizeof(buf)));
|
||||
if (dr == -1) return -1;
|
||||
if (!dr) break;
|
||||
if (dr == -1)
|
||||
return -1;
|
||||
if (!dr)
|
||||
break;
|
||||
dw = write(out, buf, dr);
|
||||
if (dw == -1) return -1;
|
||||
if (dw == -1)
|
||||
return -1;
|
||||
if (dw != dr) {
|
||||
// POSIX requires atomic IO up to PIPE_BUF
|
||||
// The minimum permissible PIPE_BUF is 512
|
||||
|
|
|
@ -156,9 +156,12 @@ textwindows int GetNtOpenFlags(int flags, int mode, uint32_t *out_perm,
|
|||
}
|
||||
|
||||
// Not certain yet what benefit these flags offer.
|
||||
if (flags & _O_SEQUENTIAL) attr |= kNtFileFlagSequentialScan;
|
||||
if (flags & _O_RANDOM) attr |= kNtFileFlagRandomAccess;
|
||||
if (flags & _O_DIRECT) attr |= kNtFileFlagNoBuffering;
|
||||
if (flags & _O_SEQUENTIAL)
|
||||
attr |= kNtFileFlagSequentialScan;
|
||||
if (flags & _O_RANDOM)
|
||||
attr |= kNtFileFlagRandomAccess;
|
||||
if (flags & _O_DIRECT)
|
||||
attr |= kNtFileFlagNoBuffering;
|
||||
|
||||
// TODO(jart): Should we *always* open with write permission if the
|
||||
// kernel will give it to us? We'd then deny write access
|
||||
|
@ -172,9 +175,13 @@ textwindows int GetNtOpenFlags(int flags, int mode, uint32_t *out_perm,
|
|||
// writing to a file across a network can occasionally return
|
||||
// kNtErrorAccessDenied." -Quoth MSDN
|
||||
|
||||
if (out_perm) *out_perm = perm;
|
||||
if (out_share) *out_share = share;
|
||||
if (out_disp) *out_disp = disp;
|
||||
if (out_attr) *out_attr = attr;
|
||||
if (out_perm)
|
||||
*out_perm = perm;
|
||||
if (out_share)
|
||||
*out_share = share;
|
||||
if (out_disp)
|
||||
*out_disp = disp;
|
||||
if (out_attr)
|
||||
*out_attr = attr;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -42,7 +42,8 @@ textwindows char16_t *__create_pipe_name(char16_t *a) {
|
|||
char16_t *p = a;
|
||||
const char *q = "\\\\?\\pipe\\cosmo\\";
|
||||
static atomic_uint x;
|
||||
while (*q) *p++ = *q++;
|
||||
while (*q)
|
||||
*p++ = *q++;
|
||||
p = itoa16(p, __pid);
|
||||
*p++ = '-';
|
||||
p = itoa16(p, atomic_fetch_add(&x, 1));
|
||||
|
|
|
@ -66,12 +66,14 @@
|
|||
int dup2(int oldfd, int newfd) {
|
||||
int rc;
|
||||
// helps guarantee stderr log gets duplicated before user closes
|
||||
if (_weaken(kloghandle)) _weaken(kloghandle)();
|
||||
if (_weaken(kloghandle))
|
||||
_weaken(kloghandle)();
|
||||
#ifdef __aarch64__
|
||||
if (oldfd == newfd) {
|
||||
// linux aarch64 defines dup3() but not dup2(), which wasn't such a
|
||||
// great decision, since the two syscalls don't behave the same way
|
||||
if (!(rc = read(oldfd, 0, 0))) rc = oldfd;
|
||||
if (!(rc = read(oldfd, 0, 0)))
|
||||
rc = oldfd;
|
||||
} else
|
||||
#endif
|
||||
if (!IsWindows()) {
|
||||
|
|
|
@ -65,7 +65,8 @@
|
|||
int dup3(int oldfd, int newfd, int flags) {
|
||||
int rc;
|
||||
// helps guarantee stderr log gets duplicated before user closes
|
||||
if (_weaken(kloghandle)) _weaken(kloghandle)();
|
||||
if (_weaken(kloghandle))
|
||||
_weaken(kloghandle)();
|
||||
if (oldfd == newfd || (flags & ~O_CLOEXEC)) {
|
||||
rc = einval(); // NetBSD doesn't do this
|
||||
} else if (oldfd < 0 || newfd < 0) {
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
textwindows int sys_faccessat_nt(int dirfd, const char *path, int mode,
|
||||
uint32_t flags) {
|
||||
char16_t path16[PATH_MAX];
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1) return -1;
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1)
|
||||
return -1;
|
||||
return __fix_enotdir(ntaccesscheck(path16, mode), path16);
|
||||
}
|
||||
|
|
|
@ -64,7 +64,8 @@ int faccessat(int dirfd, const char *path, int amode, int flags) {
|
|||
rc = _weaken(__zipos_access)(&zipname, amode);
|
||||
} else if (!IsWindows()) {
|
||||
e = errno;
|
||||
if (!flags) goto NoFlags;
|
||||
if (!flags)
|
||||
goto NoFlags;
|
||||
if ((rc = sys_faccessat2(dirfd, path, amode, flags)) == -1) {
|
||||
if (errno == ENOSYS) {
|
||||
errno = e;
|
||||
|
|
|
@ -37,8 +37,10 @@ static textwindows int sys_fadvise_nt_impl(int fd, uint64_t offset,
|
|||
int rc, flags, mode;
|
||||
uint32_t perm, share, attr;
|
||||
|
||||
if ((int64_t)len < 0) return einval();
|
||||
if (!__isfdkind(fd, kFdFile)) return ebadf();
|
||||
if ((int64_t)len < 0)
|
||||
return einval();
|
||||
if (!__isfdkind(fd, kFdFile))
|
||||
return ebadf();
|
||||
h1 = g_fds.p[fd].handle;
|
||||
mode = g_fds.p[fd].mode;
|
||||
flags = g_fds.p[fd].flags;
|
||||
|
|
|
@ -27,7 +27,8 @@ int sys_chdir_nt_impl(char16_t[hasatleast PATH_MAX], uint32_t);
|
|||
|
||||
textwindows int sys_fchdir_nt(int dirfd) {
|
||||
char16_t dir[PATH_MAX];
|
||||
if (!__isfdkind(dirfd, kFdFile)) return ebadf();
|
||||
if (!__isfdkind(dirfd, kFdFile))
|
||||
return ebadf();
|
||||
return sys_chdir_nt_impl(
|
||||
dir, GetFinalPathNameByHandle(g_fds.p[dirfd].handle, dir, ARRAYLEN(dir),
|
||||
kNtFileNameNormalized | kNtVolumeNameDos));
|
||||
|
|
|
@ -29,8 +29,10 @@
|
|||
textwindows int sys_fchmod_nt(int fd, uint32_t mode) {
|
||||
|
||||
// validate file descriptor
|
||||
if (fd + 0u >= g_fds.n) return ebadf();
|
||||
if (g_fds.p[fd].kind == kFdEmpty) return ebadf();
|
||||
if (fd + 0u >= g_fds.n)
|
||||
return ebadf();
|
||||
if (g_fds.p[fd].kind == kFdEmpty)
|
||||
return ebadf();
|
||||
|
||||
// get current information
|
||||
struct NtFileBasicInfo fbi;
|
||||
|
|
|
@ -26,7 +26,8 @@ textwindows int sys_fchmodat_nt(int dirfd, const char *path, uint32_t mode,
|
|||
int flags) {
|
||||
uint32_t attr;
|
||||
uint16_t path16[PATH_MAX];
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1) return -1;
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1)
|
||||
return -1;
|
||||
if ((attr = GetFileAttributes(path16)) != -1u) {
|
||||
if (mode & 0222) {
|
||||
attr &= ~kNtFileAttributeReadonly;
|
||||
|
|
|
@ -129,7 +129,8 @@ textwindows void sys_fcntl_nt_lock_cleanup(int fd) {
|
|||
|
||||
static textwindows int64_t GetfileSize(int64_t handle) {
|
||||
struct NtByHandleFileInformation wst;
|
||||
if (!GetFileInformationByHandle(handle, &wst)) return __winerr();
|
||||
if (!GetFileInformationByHandle(handle, &wst))
|
||||
return __winerr();
|
||||
return (wst.nFileSizeHigh + 0ull) << 32 | wst.nFileSizeLow;
|
||||
}
|
||||
|
||||
|
@ -156,7 +157,8 @@ static textwindows int sys_fcntl_nt_lock(struct Fd *f, int fd, int cmd,
|
|||
break;
|
||||
case SEEK_END: {
|
||||
int64_t size;
|
||||
if ((size = GetfileSize(f->handle)) == -1) return -1;
|
||||
if ((size = GetfileSize(f->handle)) == -1)
|
||||
return -1;
|
||||
off = size - off;
|
||||
break;
|
||||
}
|
||||
|
@ -254,7 +256,8 @@ static textwindows int sys_fcntl_nt_lock(struct Fd *f, int fd, int cmd,
|
|||
}
|
||||
|
||||
if (l->l_type == F_UNLCK) {
|
||||
if (cmd == F_GETLK) return einval();
|
||||
if (cmd == F_GETLK)
|
||||
return einval();
|
||||
|
||||
// allow a big range to unlock many small ranges
|
||||
for (flp = &g_locks.list, fl = *flp; fl;) {
|
||||
|
@ -318,7 +321,8 @@ static textwindows int sys_fcntl_nt_lock(struct Fd *f, int fd, int cmd,
|
|||
}
|
||||
|
||||
static textwindows int sys_fcntl_nt_dupfd(int fd, int cmd, int start) {
|
||||
if (start < 0) return einval();
|
||||
if (start < 0)
|
||||
return einval();
|
||||
return sys_dup_nt(fd, -1, (cmd == F_DUPFD_CLOEXEC ? _O_CLOEXEC : 0), start);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,17 +27,23 @@
|
|||
|
||||
textwindows int sys_fdatasync_nt(int fd, bool fake) {
|
||||
struct NtByHandleFileInformation wst;
|
||||
if (!__isfdopen(fd)) return ebadf();
|
||||
if (!__isfdkind(fd, kFdFile)) return einval();
|
||||
if (GetFileType(g_fds.p[fd].handle) != kNtFileTypeDisk) return einval();
|
||||
if (!GetFileInformationByHandle(g_fds.p[fd].handle, &wst)) return __winerr();
|
||||
if (!__isfdopen(fd))
|
||||
return ebadf();
|
||||
if (!__isfdkind(fd, kFdFile))
|
||||
return einval();
|
||||
if (GetFileType(g_fds.p[fd].handle) != kNtFileTypeDisk)
|
||||
return einval();
|
||||
if (!GetFileInformationByHandle(g_fds.p[fd].handle, &wst))
|
||||
return __winerr();
|
||||
if (wst.dwFileAttributes & kNtFileAttributeDirectory) {
|
||||
// Flushing a directory handle is possible, but it needs
|
||||
// kNtGenericWrite access, and MSDN doesn't document it.
|
||||
return 0;
|
||||
}
|
||||
if (fake) return 0;
|
||||
if (_check_signal(false) == -1) return -1;
|
||||
if (fake)
|
||||
return 0;
|
||||
if (_check_signal(false) == -1)
|
||||
return -1;
|
||||
return FlushFileBuffers(g_fds.p[fd].handle) ? 0 : __winerr();
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,8 @@
|
|||
textwindows int sys_flock_nt(int fd, int op) {
|
||||
int64_t h;
|
||||
struct NtByHandleFileInformation info;
|
||||
if (!__isfdkind(fd, kFdFile)) return ebadf();
|
||||
if (!__isfdkind(fd, kFdFile))
|
||||
return ebadf();
|
||||
h = g_fds.p[fd].handle;
|
||||
struct NtOverlapped ov = {.hEvent = h};
|
||||
|
||||
|
|
|
@ -23,7 +23,8 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
int sys_fstat_metal(int fd, struct stat *st) {
|
||||
if (fd < 0) return einval();
|
||||
if (fd < 0)
|
||||
return einval();
|
||||
if (fd < g_fds.n && g_fds.p[fd].kind == kFdSerial) {
|
||||
bzero(st, sizeof(*st));
|
||||
st->st_dev = g_fds.p[fd].handle;
|
||||
|
|
|
@ -97,7 +97,8 @@ textwindows int sys_fstat_nt_special(int kind, struct stat *st) {
|
|||
}
|
||||
|
||||
textwindows int sys_fstat_nt(int fd, struct stat *st) {
|
||||
if (fd + 0u >= g_fds.n) return ebadf();
|
||||
if (fd + 0u >= g_fds.n)
|
||||
return ebadf();
|
||||
switch (g_fds.p[fd].kind) {
|
||||
case kFdEmpty:
|
||||
return ebadf();
|
||||
|
@ -174,7 +175,8 @@ textwindows int sys_fstat_nt_handle(int64_t handle, const char16_t *path,
|
|||
if (S_ISLNK(st.st_mode)) {
|
||||
if (!st.st_size) {
|
||||
long size = GetSizeOfReparsePoint(handle);
|
||||
if (size == -1) return -1;
|
||||
if (size == -1)
|
||||
return -1;
|
||||
st.st_size = size;
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -36,7 +36,8 @@
|
|||
static int Atoi(const char *str) {
|
||||
int c;
|
||||
unsigned x = 0;
|
||||
if (!*str) return -1;
|
||||
if (!*str)
|
||||
return -1;
|
||||
while ((c = *str++)) {
|
||||
if ('0' <= c && c <= '9') {
|
||||
x *= 10;
|
||||
|
|
|
@ -35,7 +35,8 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
static inline const char *__strace_fstatat_flags(char buf[12], int flags) {
|
||||
if (flags == AT_SYMLINK_NOFOLLOW) return "AT_SYMLINK_NOFOLLOW";
|
||||
if (flags == AT_SYMLINK_NOFOLLOW)
|
||||
return "AT_SYMLINK_NOFOLLOW";
|
||||
FormatInt32(buf, flags);
|
||||
return buf;
|
||||
}
|
||||
|
|
|
@ -54,7 +54,8 @@ textwindows int sys_fstatfs_nt(int64_t handle, struct statfs *f) {
|
|||
st = NtQueryVolumeInformationFile(handle, &io, &fs, sizeof(fs),
|
||||
kNtFileFsFullSizeInformation);
|
||||
if (!NtSuccess(st)) {
|
||||
if (st == kNtStatusDllNotFound) return enosys();
|
||||
if (st == kNtStatusDllNotFound)
|
||||
return enosys();
|
||||
return eio();
|
||||
}
|
||||
for (h = j = i = 0; FileSystemNameBuffer[i]; i++) {
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
*/
|
||||
int ftok(const char *path, int id) {
|
||||
struct stat st;
|
||||
if (stat(path, &st) == -1) return -1;
|
||||
if (stat(path, &st) == -1)
|
||||
return -1;
|
||||
return (uint32_t)id << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff);
|
||||
}
|
||||
|
|
|
@ -64,7 +64,8 @@ int getcpu(unsigned *out_opt_cpu, unsigned *out_opt_node) {
|
|||
}
|
||||
} else {
|
||||
int rc = sys_getcpu(&cpu, &node, 0);
|
||||
if (rc == -1) return -1;
|
||||
if (rc == -1)
|
||||
return -1;
|
||||
}
|
||||
if (out_opt_cpu) {
|
||||
*out_opt_cpu = cpu;
|
||||
|
|
|
@ -82,14 +82,17 @@ static dontinline textwindows int sys_getcwd_nt(char *buf, size_t size) {
|
|||
// get current directory from the system
|
||||
char16_t p16[PATH_MAX];
|
||||
uint32_t n = GetCurrentDirectory(PATH_MAX, p16);
|
||||
if (!n) return eacces(); // system call failed
|
||||
if (n >= PATH_MAX) return erange(); // not enough room?!?
|
||||
if (!n)
|
||||
return eacces(); // system call failed
|
||||
if (n >= PATH_MAX)
|
||||
return erange(); // not enough room?!?
|
||||
|
||||
// convert utf-16 to utf-8
|
||||
// we can't modify `buf` until we're certain of success
|
||||
char p8[PATH_MAX], *p = p8;
|
||||
n = tprecode16to8(p, PATH_MAX, p16).ax;
|
||||
if (n >= PATH_MAX) return erange(); // utf-8 explosion
|
||||
if (n >= PATH_MAX)
|
||||
return erange(); // utf-8 explosion
|
||||
|
||||
// turn \\?\c:\... into c:\...
|
||||
if (p[0] == '\\' && //
|
||||
|
|
|
@ -43,7 +43,8 @@ struct loadavg {
|
|||
int getloadavg(double *a, int n) {
|
||||
// cat /proc/loadavg
|
||||
int i, rc;
|
||||
if (n > 3) n = 3;
|
||||
if (n > 3)
|
||||
n = 3;
|
||||
if (!n) {
|
||||
rc = 0;
|
||||
} else if (n < 0) {
|
||||
|
|
|
@ -64,7 +64,8 @@ static ssize_t GetRandomCpu(char *p, size_t n, int f, bool impl(uint64_t *)) {
|
|||
for (i = 0; i < n; i += j) {
|
||||
TryAgain:
|
||||
if (!impl(&x)) {
|
||||
if (f || i >= 256) break;
|
||||
if (f || i >= 256)
|
||||
break;
|
||||
goto TryAgain;
|
||||
}
|
||||
for (j = 0; j < 8 && i + j < n; ++j) {
|
||||
|
|
|
@ -60,7 +60,8 @@ static bool have_getrandom;
|
|||
|
||||
static void GetRandomEntropy(char *p, size_t n) {
|
||||
unassert(n <= 256);
|
||||
if (sys_getentropy(p, n)) notpossible;
|
||||
if (sys_getentropy(p, n))
|
||||
notpossible;
|
||||
}
|
||||
|
||||
static void GetRandomArnd(char *p, size_t n) {
|
||||
|
@ -69,8 +70,10 @@ static void GetRandomArnd(char *p, size_t n) {
|
|||
cmd[0] = 1; // CTL_KERN
|
||||
cmd[1] = IsFreebsd() ? 37 : 81; // KERN_ARND
|
||||
unassert((m = n) <= 256);
|
||||
if (sys_sysctl(cmd, 2, p, &n, 0, 0) == -1) notpossible;
|
||||
if (m != n) notpossible;
|
||||
if (sys_sysctl(cmd, 2, p, &n, 0, 0) == -1)
|
||||
notpossible;
|
||||
if (m != n)
|
||||
notpossible;
|
||||
}
|
||||
|
||||
static ssize_t GetRandomBsd(char *p, size_t n, void impl(char *, size_t)) {
|
||||
|
@ -193,7 +196,8 @@ ssize_t getrandom(void *p, size_t n, unsigned f) {
|
|||
__attribute__((__constructor__(30))) static textstartup void getrandom_init(
|
||||
void) {
|
||||
int e, rc;
|
||||
if (IsWindows() || IsMetal()) return;
|
||||
if (IsWindows() || IsMetal())
|
||||
return;
|
||||
BLOCK_CANCELATION;
|
||||
e = errno;
|
||||
if (!(rc = sys_getrandom(0, 0, 0))) {
|
||||
|
|
|
@ -34,15 +34,20 @@ int getresgid(uint32_t *real, uint32_t *effective, uint32_t *saved) {
|
|||
int rc, gid;
|
||||
if (IsWindows()) {
|
||||
gid = getgid();
|
||||
if (real) *real = gid;
|
||||
if (effective) *effective = gid;
|
||||
if (saved) *saved = gid;
|
||||
if (real)
|
||||
*real = gid;
|
||||
if (effective)
|
||||
*effective = gid;
|
||||
if (saved)
|
||||
*saved = gid;
|
||||
rc = 0;
|
||||
} else if (saved) {
|
||||
rc = sys_getresgid(real, effective, saved);
|
||||
} else {
|
||||
if (real) *real = sys_getgid();
|
||||
if (effective) *effective = sys_getegid();
|
||||
if (real)
|
||||
*real = sys_getgid();
|
||||
if (effective)
|
||||
*effective = sys_getegid();
|
||||
rc = 0;
|
||||
}
|
||||
STRACE("getresgid([%d], [%d], [%d]) → %d% m", real ? *real : 0,
|
||||
|
|
|
@ -34,15 +34,20 @@ int getresuid(uint32_t *real, uint32_t *effective, uint32_t *saved) {
|
|||
int rc, uid;
|
||||
if (IsWindows()) {
|
||||
uid = getuid();
|
||||
if (real) *real = uid;
|
||||
if (effective) *effective = uid;
|
||||
if (saved) *saved = uid;
|
||||
if (real)
|
||||
*real = uid;
|
||||
if (effective)
|
||||
*effective = uid;
|
||||
if (saved)
|
||||
*saved = uid;
|
||||
rc = 0;
|
||||
} else if (saved) {
|
||||
rc = sys_getresuid(real, effective, saved);
|
||||
} else {
|
||||
if (real) *real = sys_getuid();
|
||||
if (effective) *effective = sys_geteuid();
|
||||
if (real)
|
||||
*real = sys_getuid();
|
||||
if (effective)
|
||||
*effective = sys_geteuid();
|
||||
rc = 0;
|
||||
}
|
||||
STRACE("getresuid([%d], [%d], [%d]) → %d% m", real ? *real : 0,
|
||||
|
|
|
@ -25,7 +25,8 @@ textwindows char *GetSystemDirectoryPath(char *buf, const char *path,
|
|||
uint32_t syslen = GetSystemDirectoryA(buf, size);
|
||||
size_t pathlen = strlen(path);
|
||||
if (syslen && syslen + pathlen + 1 < size) {
|
||||
if (buf[syslen] == '\\') --syslen;
|
||||
if (buf[syslen] == '\\')
|
||||
--syslen;
|
||||
memcpy(buf + syslen, path, pathlen + 1);
|
||||
return buf;
|
||||
} else {
|
||||
|
|
|
@ -29,7 +29,8 @@ textwindows uint32_t sys_getuid_nt(void) {
|
|||
if (!(tmp = atomic_load_explicit(&uid, memory_order_acquire))) {
|
||||
GetUserName(&buf, &size);
|
||||
tmp = __fnv(buf, size >> 1) & 32767;
|
||||
if (!tmp) ++tmp;
|
||||
if (!tmp)
|
||||
++tmp;
|
||||
atomic_store_explicit(&uid, tmp, memory_order_release);
|
||||
}
|
||||
return tmp;
|
||||
|
|
|
@ -244,11 +244,16 @@ static textwindows struct HostAdapterInfoNode *appendHostInfo(
|
|||
* IFF_PROMISC ** NOT SUPPORTED, unknown how to retrieve it
|
||||
*/
|
||||
flags = 0;
|
||||
if (aa->OperStatus == kNtIfOperStatusUp) flags |= IFF_UP | IFF_RUNNING;
|
||||
if (aa->IfType == kNtIfTypePpp) flags |= IFF_POINTOPOINT;
|
||||
if (!(aa->Flags & kNtIpAdapterNoMulticast)) flags |= IFF_MULTICAST;
|
||||
if (aa->IfType == kNtIfTypeSoftwareLoopback) flags |= IFF_LOOPBACK;
|
||||
if (aa->FirstPrefix) flags |= IFF_BROADCAST;
|
||||
if (aa->OperStatus == kNtIfOperStatusUp)
|
||||
flags |= IFF_UP | IFF_RUNNING;
|
||||
if (aa->IfType == kNtIfTypePpp)
|
||||
flags |= IFF_POINTOPOINT;
|
||||
if (!(aa->Flags & kNtIpAdapterNoMulticast))
|
||||
flags |= IFF_MULTICAST;
|
||||
if (aa->IfType == kNtIfTypeSoftwareLoopback)
|
||||
flags |= IFF_LOOPBACK;
|
||||
if (aa->FirstPrefix)
|
||||
flags |= IFF_BROADCAST;
|
||||
node->flags = flags;
|
||||
} else {
|
||||
/* Copy from previous node */
|
||||
|
@ -344,13 +349,16 @@ static textwindows int createHostInfo(
|
|||
baseName[IFNAMSIZ - 2] = '\0';
|
||||
/* Replace any space with a '_' */
|
||||
for (i = 0; i < IFNAMSIZ - 2; ++i) {
|
||||
if (baseName[i] == ' ') baseName[i] = '_';
|
||||
if (!baseName[i]) break;
|
||||
if (baseName[i] == ' ')
|
||||
baseName[i] = '_';
|
||||
if (!baseName[i])
|
||||
break;
|
||||
}
|
||||
for (count = 0, ua = aa->FirstUnicastAddress, ap = aa->FirstPrefix;
|
||||
(ua != NULL) && (count < MAX_UNICAST_ADDR); ++count) {
|
||||
node = appendHostInfo(node, baseName, aa, &ua, &ap, count);
|
||||
if (!node) goto err;
|
||||
if (!node)
|
||||
goto err;
|
||||
if (!__hostInfo) {
|
||||
__hostInfo = node;
|
||||
if (_cmpxchg(&once, false, true)) {
|
||||
|
@ -444,7 +452,8 @@ static textwindows int ioctl_siocgifconf_nt(int fd, struct ifconf *ifc) {
|
|||
static textwindows int ioctl_siocgifaddr_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) return ebadf();
|
||||
if (!node)
|
||||
return ebadf();
|
||||
memcpy(&ifr->ifr_addr, &node->unicast, sizeof(struct sockaddr));
|
||||
return 0;
|
||||
}
|
||||
|
@ -453,7 +462,8 @@ static textwindows int ioctl_siocgifaddr_nt(int fd, struct ifreq *ifr) {
|
|||
static textwindows int ioctl_siocgifflags_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) return ebadf();
|
||||
if (!node)
|
||||
return ebadf();
|
||||
ifr->ifr_flags = node->flags;
|
||||
return 0;
|
||||
}
|
||||
|
@ -462,7 +472,8 @@ static textwindows int ioctl_siocgifflags_nt(int fd, struct ifreq *ifr) {
|
|||
static textwindows int ioctl_siocgifnetmask_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) return ebadf();
|
||||
if (!node)
|
||||
return ebadf();
|
||||
memcpy(&ifr->ifr_netmask, &node->netmask, sizeof(struct sockaddr));
|
||||
return 0;
|
||||
}
|
||||
|
@ -473,7 +484,8 @@ static textwindows int ioctl_siocgifnetmask_nt(int fd, struct ifreq *ifr) {
|
|||
static textwindows int ioctl_siocgifbrdaddr_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) return ebadf();
|
||||
if (!node)
|
||||
return ebadf();
|
||||
memcpy(&ifr->ifr_broadaddr, &node->broadcast, sizeof(struct sockaddr));
|
||||
return 0;
|
||||
}
|
||||
|
@ -513,7 +525,8 @@ static int ioctl_siocgifconf_sysv(int fd, struct ifconf *ifc) {
|
|||
for (p = b, e = p + MIN(bufMax, READ32LE(ifcBsd)); p + 16 + 16 <= e;
|
||||
p += IsBsd() ? 16 + MAX(16, p[16] & 255) : 40) {
|
||||
fam = p[IsBsd() ? 17 : 16] & 255;
|
||||
if (fam != AF_INET) continue;
|
||||
if (fam != AF_INET)
|
||||
continue;
|
||||
ip = READ32BE(p + 20);
|
||||
bzero(req, sizeof(*req));
|
||||
memcpy(req->ifr_name, p, 16);
|
||||
|
@ -541,8 +554,10 @@ static inline void ioctl_sockaddr2linux(void *saddr) {
|
|||
* requires adjustment between Linux and XNU
|
||||
*/
|
||||
static int ioctl_siocgifaddr_sysv(int fd, uint64_t op, struct ifreq *ifr) {
|
||||
if (sys_ioctl(fd, op, ifr) == -1) return -1;
|
||||
if (IsBsd()) ioctl_sockaddr2linux(&ifr->ifr_addr);
|
||||
if (sys_ioctl(fd, op, ifr) == -1)
|
||||
return -1;
|
||||
if (IsBsd())
|
||||
ioctl_sockaddr2linux(&ifr->ifr_addr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,8 @@
|
|||
bool isdirectory_nt(const char *path) {
|
||||
uint32_t x;
|
||||
char16_t path16[PATH_MAX];
|
||||
if (__mkntpath(path, path16) == -1) return -1;
|
||||
if (__mkntpath(path, path16) == -1)
|
||||
return -1;
|
||||
if ((x = GetFileAttributes(path16)) != -1u) {
|
||||
return !!(x & kNtFileAttributeDirectory);
|
||||
} else {
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
*/
|
||||
bool32 isexecutable(const char *path) {
|
||||
struct stat st;
|
||||
if (fstatat(AT_FDCWD, path, &st, 0)) return 0;
|
||||
if (fstatat(AT_FDCWD, path, &st, 0))
|
||||
return 0;
|
||||
return !S_ISDIR(st.st_mode) && !!(st.st_mode & 0111);
|
||||
}
|
||||
|
|
|
@ -33,7 +33,8 @@ static struct {
|
|||
|
||||
static bool __is_linux_2_6_23_impl(void) {
|
||||
int rc;
|
||||
if (IsGenuineBlink()) return true;
|
||||
if (IsGenuineBlink())
|
||||
return true;
|
||||
asm volatile("syscall"
|
||||
: "=a"(rc)
|
||||
: "0"(157), "D"(PR_GET_SECCOMP)
|
||||
|
|
|
@ -28,7 +28,8 @@
|
|||
bool isregularfile_nt(const char *path) {
|
||||
uint32_t x;
|
||||
char16_t path16[PATH_MAX];
|
||||
if (__mkntpath(path, path16) == -1) return -1;
|
||||
if (__mkntpath(path, path16) == -1)
|
||||
return -1;
|
||||
if ((x = GetFileAttributes(path16)) != -1u) {
|
||||
return !(x & (kNtFileAttributeDirectory | kNtFileAttributeReparsePoint));
|
||||
} else {
|
||||
|
|
|
@ -26,7 +26,8 @@
|
|||
bool issymlink_nt(const char *path) {
|
||||
uint32_t x;
|
||||
char16_t path16[PATH_MAX];
|
||||
if (__mkntpath(path, path16) == -1) return -1;
|
||||
if (__mkntpath(path, path16) == -1)
|
||||
return -1;
|
||||
if ((x = GetFileAttributes(path16)) != -1u) {
|
||||
return !!(x & kNtFileAttributeReparsePoint);
|
||||
} else {
|
||||
|
|
|
@ -52,31 +52,42 @@ int makedirs(const char *path, unsigned mode) {
|
|||
|
||||
e = errno;
|
||||
n = strlen(path);
|
||||
if (n >= PATH_MAX) return enametoolong();
|
||||
if (n >= PATH_MAX)
|
||||
return enametoolong();
|
||||
memcpy(buf, path, n + 1);
|
||||
i = n;
|
||||
|
||||
// descend
|
||||
while (i) {
|
||||
if (!mkdir(buf, mode)) break;
|
||||
if (!mkdir(buf, mode))
|
||||
break;
|
||||
if (errno == EEXIST) {
|
||||
if (i == n) goto CheckTop;
|
||||
if (i == n)
|
||||
goto CheckTop;
|
||||
break;
|
||||
}
|
||||
if (errno != ENOENT) return -1;
|
||||
while (i && buf[i - 1] == '/') buf[--i] = 0;
|
||||
while (i && buf[i - 1] != '/') buf[--i] = 0;
|
||||
if (errno != ENOENT)
|
||||
return -1;
|
||||
while (i && buf[i - 1] == '/')
|
||||
buf[--i] = 0;
|
||||
while (i && buf[i - 1] != '/')
|
||||
buf[--i] = 0;
|
||||
}
|
||||
|
||||
// ascend
|
||||
for (;;) {
|
||||
if (mkdir(buf, mode)) {
|
||||
if (errno != EEXIST) return -1;
|
||||
if (i == n) goto CheckTop;
|
||||
if (errno != EEXIST)
|
||||
return -1;
|
||||
if (i == n)
|
||||
goto CheckTop;
|
||||
}
|
||||
if (i == n) break;
|
||||
while (i < n && (c = path[i]) != '/') buf[i++] = c;
|
||||
while (i < n && (c = path[i]) == '/') buf[i++] = c;
|
||||
if (i == n)
|
||||
break;
|
||||
while (i < n && (c = path[i]) != '/')
|
||||
buf[i++] = c;
|
||||
while (i < n && (c = path[i]) == '/')
|
||||
buf[i++] = c;
|
||||
}
|
||||
|
||||
Finish:
|
||||
|
@ -84,7 +95,9 @@ Finish:
|
|||
return 0;
|
||||
|
||||
CheckTop:
|
||||
if (stat(path, &st)) return -1;
|
||||
if (S_ISDIR(st.st_mode)) goto Finish;
|
||||
if (stat(path, &st))
|
||||
return -1;
|
||||
if (S_ISDIR(st.st_mode))
|
||||
goto Finish;
|
||||
return eexist();
|
||||
}
|
||||
|
|
|
@ -23,7 +23,9 @@
|
|||
|
||||
textwindows int sys_mkdirat_nt(int dirfd, const char *path, uint32_t mode) {
|
||||
char16_t path16[PATH_MAX];
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1) return -1;
|
||||
if (CreateDirectory(path16, 0)) return 0;
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1)
|
||||
return -1;
|
||||
if (CreateDirectory(path16, 0))
|
||||
return 0;
|
||||
return __fix_enotdir(-1, path16);
|
||||
}
|
||||
|
|
|
@ -42,10 +42,14 @@
|
|||
*/
|
||||
int mknod(const char *path, uint32_t mode, uint64_t dev) {
|
||||
int e, rc;
|
||||
if (IsAsan() && !__asan_is_valid_str(path)) return efault();
|
||||
if (mode & S_IFREG) return creat(path, mode & ~S_IFREG);
|
||||
if (mode & S_IFDIR) return mkdir(path, mode & ~S_IFDIR);
|
||||
if (mode & S_IFIFO) return enosys(); // no named pipes!
|
||||
if (IsAsan() && !__asan_is_valid_str(path))
|
||||
return efault();
|
||||
if (mode & S_IFREG)
|
||||
return creat(path, mode & ~S_IFREG);
|
||||
if (mode & S_IFDIR)
|
||||
return mkdir(path, mode & ~S_IFDIR);
|
||||
if (mode & S_IFIFO)
|
||||
return enosys(); // no named pipes!
|
||||
if (!IsWindows()) {
|
||||
// TODO(jart): Whys there code out there w/ S_xxx passed via dev?
|
||||
e = errno;
|
||||
|
|
|
@ -86,7 +86,8 @@ textwindows int mkntcmdline(char16_t cmdline[32767], char *const argv[]) {
|
|||
size_t i, j, k, s;
|
||||
char argbuf[PATH_MAX];
|
||||
for (k = i = 0; argv[i]; ++i) {
|
||||
if (i) APPEND(u' ');
|
||||
if (i)
|
||||
APPEND(u' ');
|
||||
if (LooksLikeCosmoDrivePath(argv[i]) &&
|
||||
strlcpy(argbuf, argv[i], PATH_MAX) < PATH_MAX) {
|
||||
mungentpath(argbuf);
|
||||
|
@ -112,7 +113,8 @@ textwindows int mkntcmdline(char16_t cmdline[32767], char *const argv[]) {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (!x) break;
|
||||
if (!x)
|
||||
break;
|
||||
if (x == '\\') {
|
||||
++slashes;
|
||||
} else if (x == '"') {
|
||||
|
@ -125,7 +127,8 @@ textwindows int mkntcmdline(char16_t cmdline[32767], char *const argv[]) {
|
|||
}
|
||||
slashes = 0;
|
||||
uint32_t w = EncodeUtf16(x);
|
||||
do APPEND(w);
|
||||
do
|
||||
APPEND(w);
|
||||
while ((w >>= 16));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,9 +44,12 @@ static textwindows int Compare(const char *l, const char *r) {
|
|||
for (;;) {
|
||||
a = l[i] & 255;
|
||||
b = r[i] & 255;
|
||||
if (a == '=') a = 0;
|
||||
if (b == '=') b = 0;
|
||||
if (a != b || !b) break;
|
||||
if (a == '=')
|
||||
a = 0;
|
||||
if (b == '=')
|
||||
b = 0;
|
||||
if (a != b || !b)
|
||||
break;
|
||||
++i;
|
||||
}
|
||||
return a - b;
|
||||
|
@ -56,13 +59,15 @@ static textwindows int InsertString(struct EnvBuilder *env, const char *str) {
|
|||
int c, i, cmp;
|
||||
char *var, *path = 0;
|
||||
|
||||
if (!str) return 0;
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
// copy key=val to buf
|
||||
var = env->buf + env->bufi;
|
||||
do {
|
||||
c = *str++;
|
||||
if (env->bufi + 2 > 32767) return e2big();
|
||||
if (env->bufi + 2 > 32767)
|
||||
return e2big();
|
||||
env->buf[env->bufi++] = c;
|
||||
if (c == '=' && str[0] == '/' && IsAlpha(str[1]) && str[2] == '/') {
|
||||
path = env->buf + env->bufi;
|
||||
|
@ -70,7 +75,8 @@ static textwindows int InsertString(struct EnvBuilder *env, const char *str) {
|
|||
} while (c);
|
||||
|
||||
// fixup key=/c/... → key=c:\...
|
||||
if (path) mungentpath(path);
|
||||
if (path)
|
||||
mungentpath(path);
|
||||
|
||||
// append key=val to sorted list using insertion sort technique
|
||||
for (i = env->vari;; --i) {
|
||||
|
@ -143,8 +149,10 @@ textwindows int mkntenvblock(char16_t envblock[32767], char *const envp[],
|
|||
#pragma GCC pop_options
|
||||
|
||||
// load new environment into string pointer array and fix file paths
|
||||
if (InsertStrings(&env, envp) == -1) return -1;
|
||||
if (InsertStrings(&env, extravars) == -1) return -1;
|
||||
if (InsertStrings(&env, envp) == -1)
|
||||
return -1;
|
||||
if (InsertStrings(&env, extravars) == -1)
|
||||
return -1;
|
||||
if (environ) {
|
||||
// https://jpassing.com/2009/12/28/the-hidden-danger-of-forgetting-to-specify-systemroot-in-a-custom-environment-block/
|
||||
e = __getenv(environ, "SYSTEMROOT");
|
||||
|
|
|
@ -54,8 +54,10 @@ textwindows size_t __normntpath(char16_t *p, size_t n) {
|
|||
(i + 1 < n && p[i + 1] == '.') && //
|
||||
(i + 2 == n || IsSlash(p[i + 2]))) {
|
||||
// matched "/../" or "/..$"
|
||||
while (j && p[j - 1] == '\\') --j;
|
||||
while (j && p[j - 1] != '\\') --j;
|
||||
while (j && p[j - 1] == '\\')
|
||||
--j;
|
||||
while (j && p[j - 1] != '\\')
|
||||
--j;
|
||||
} else {
|
||||
p[j++] = c;
|
||||
}
|
||||
|
@ -156,7 +158,8 @@ textwindows int __mkntpath2(const char *path,
|
|||
if (!x && IsSlash(q[0]) && q[1] == 't' && q[2] == 'm' && q[3] == 'p' &&
|
||||
(IsSlash(q[4]) || !q[4])) {
|
||||
m = GetTempPath(z, p);
|
||||
if (!q[4]) return m;
|
||||
if (!q[4])
|
||||
return m;
|
||||
q += 5;
|
||||
p += m;
|
||||
z -= m;
|
||||
|
|
|
@ -33,14 +33,19 @@ static textwindows int __mkntpathath_impl(int64_t dirhand, const char *path,
|
|||
size_t n;
|
||||
char16_t dir[PATH_MAX];
|
||||
uint32_t dirlen, filelen;
|
||||
if (!isutf8(path, -1)) return eilseq(); // thwart overlong nul in conversion
|
||||
if ((filelen = __mkntpath2(path, file, flags)) == -1) return -1;
|
||||
if (!filelen) return enoent();
|
||||
if (!isutf8(path, -1))
|
||||
return eilseq(); // thwart overlong nul in conversion
|
||||
if ((filelen = __mkntpath2(path, file, flags)) == -1)
|
||||
return -1;
|
||||
if (!filelen)
|
||||
return enoent();
|
||||
if (file[0] != u'\\' && dirhand != AT_FDCWD) { // ProTip: \\?\C:\foo
|
||||
dirlen = GetFinalPathNameByHandle(dirhand, dir, ARRAYLEN(dir),
|
||||
kNtFileNameNormalized | kNtVolumeNameDos);
|
||||
if (!dirlen) return __winerr();
|
||||
if (dirlen + 1 + filelen + 1 > ARRAYLEN(dir)) return enametoolong();
|
||||
if (!dirlen)
|
||||
return __winerr();
|
||||
if (dirlen + 1 + filelen + 1 > ARRAYLEN(dir))
|
||||
return enametoolong();
|
||||
dir[dirlen] = u'\\';
|
||||
memcpy(dir + dirlen + 1, file, (filelen + 1) * sizeof(char16_t));
|
||||
memcpy(file, dir, ((n = dirlen + 1 + filelen) + 1) * sizeof(char16_t));
|
||||
|
|
|
@ -84,7 +84,8 @@ int mount(const char *source, const char *target, const char *type,
|
|||
if (!IsBsd()) {
|
||||
return sys_mount_linux(source, target, type, flags, data);
|
||||
} else {
|
||||
if (!strcmp(type, "iso9660")) type = "cd9660";
|
||||
if (!strcmp(type, "iso9660"))
|
||||
type = "cd9660";
|
||||
if (!strcmp(type, "vfat")) {
|
||||
if (IsOpenbsd() || IsNetbsd()) {
|
||||
type = "msdos";
|
||||
|
|
|
@ -47,7 +47,8 @@ void *sys_mremap(void *p, size_t n, size_t m, int f, void *q) {
|
|||
: "=a"(res)
|
||||
: "0"(0x019), "D"(p), "S"(n), "d"(m), "r"(r10), "r"(r8)
|
||||
: "rcx", "r11", "memory", "cc");
|
||||
if (res > -4096ul) errno = -res, res = -1;
|
||||
if (res > -4096ul)
|
||||
errno = -res, res = -1;
|
||||
} else if (IsNetbsd()) {
|
||||
if (f & MREMAP_MAYMOVE) {
|
||||
res = 0x19B;
|
||||
|
@ -57,7 +58,8 @@ void *sys_mremap(void *p, size_t n, size_t m, int f, void *q) {
|
|||
: CFLAG_CONSTRAINT(cf), "+a"(res), "=d"(rdx)
|
||||
: "D"(p), "S"(n), "2"(q), "r"(r10), "r"(r8)
|
||||
: "rcx", "r9", "r11", "memory", "cc");
|
||||
if (cf) errno = res, res = -1;
|
||||
if (cf)
|
||||
errno = res, res = -1;
|
||||
} else {
|
||||
res = einval();
|
||||
}
|
||||
|
|
|
@ -69,7 +69,8 @@ textwindows int ntaccesscheck(const char16_t *pathname, uint32_t flags) {
|
|||
int64_t hToken, hImpersonatedToken, hFile;
|
||||
intptr_t buffer[1024 / sizeof(intptr_t)];
|
||||
BLOCK_SIGNALS;
|
||||
if (flags & X_OK) flags |= R_OK;
|
||||
if (flags & X_OK)
|
||||
flags |= R_OK;
|
||||
granted = 0;
|
||||
result = false;
|
||||
flagmask = flags;
|
||||
|
|
|
@ -157,7 +157,8 @@ textwindows int ntspawn(
|
|||
}
|
||||
}
|
||||
}
|
||||
if (sb) ntspawn_free(sb);
|
||||
if (sb)
|
||||
ntspawn_free(sb);
|
||||
ALLOW_SIGNALS;
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -184,7 +184,8 @@ static textwindows int sys_open_nt_dup(int fd, int flags, int mode, int oldfd) {
|
|||
static int Atoi(const char *str) {
|
||||
int c;
|
||||
unsigned x = 0;
|
||||
if (!*str) return -1;
|
||||
if (!*str)
|
||||
return -1;
|
||||
while ((c = *str++)) {
|
||||
if ('0' <= c && c <= '9') {
|
||||
x *= 10;
|
||||
|
@ -202,7 +203,8 @@ textwindows int sys_open_nt(int dirfd, const char *file, uint32_t flags,
|
|||
int fd, oldfd;
|
||||
BLOCK_SIGNALS;
|
||||
__fds_lock();
|
||||
if (!(flags & _O_CREAT)) mode = 0;
|
||||
if (!(flags & _O_CREAT))
|
||||
mode = 0;
|
||||
if ((rc = fd = __reservefd_unlocked(-1)) != -1) {
|
||||
if (startswith(file, "/dev/")) {
|
||||
if (!strcmp(file + 5, "tty")) {
|
||||
|
|
|
@ -40,20 +40,26 @@
|
|||
int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) {
|
||||
int fd;
|
||||
struct MetalFile *state;
|
||||
if (dirfd != AT_FDCWD || strcmp(file, APE_COM_NAME)) return enoent();
|
||||
if (flags != O_RDONLY) return eacces();
|
||||
if (!_weaken(__ape_com_base) || !_weaken(__ape_com_size)) return eopnotsupp();
|
||||
if ((fd = __reservefd(-1)) == -1) return -1;
|
||||
if (dirfd != AT_FDCWD || strcmp(file, APE_COM_NAME))
|
||||
return enoent();
|
||||
if (flags != O_RDONLY)
|
||||
return eacces();
|
||||
if (!_weaken(__ape_com_base) || !_weaken(__ape_com_size))
|
||||
return eopnotsupp();
|
||||
if ((fd = __reservefd(-1)) == -1)
|
||||
return -1;
|
||||
if (!_weaken(calloc) || !_weaken(free)) {
|
||||
struct DirectMap dm;
|
||||
dm = sys_mmap_metal(NULL, ROUNDUP(sizeof(struct MetalFile), 4096),
|
||||
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1,
|
||||
0);
|
||||
state = dm.addr;
|
||||
if (state == (void *)-1) return -1;
|
||||
if (state == (void *)-1)
|
||||
return -1;
|
||||
} else {
|
||||
state = _weaken(calloc)(1, sizeof(struct MetalFile));
|
||||
if (!state) return -1;
|
||||
if (!state)
|
||||
return -1;
|
||||
}
|
||||
state->base = (char *)__ape_com_base;
|
||||
state->size = __ape_com_size;
|
||||
|
|
|
@ -64,12 +64,16 @@ static int openpty_impl(int *mfd, int *sfd, char *name,
|
|||
}
|
||||
*mfd = m;
|
||||
*sfd = s;
|
||||
if (name) strcpy(name, t.sname);
|
||||
if (tio) npassert(!tcsetattr(s, TCSAFLUSH, tio));
|
||||
if (wsz) npassert(!tcsetwinsize(s, wsz));
|
||||
if (name)
|
||||
strcpy(name, t.sname);
|
||||
if (tio)
|
||||
npassert(!tcsetattr(s, TCSAFLUSH, tio));
|
||||
if (wsz)
|
||||
npassert(!tcsetwinsize(s, wsz));
|
||||
return 0;
|
||||
OnError:
|
||||
if (m != -1) sys_close(m);
|
||||
if (m != -1)
|
||||
sys_close(m);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,8 @@
|
|||
static textwindows int _park_thread(uint32_t msdelay, sigset_t waitmask,
|
||||
bool restartable) {
|
||||
int sig, handler_was_called;
|
||||
if (_check_cancel() == -1) return -1;
|
||||
if (_check_cancel() == -1)
|
||||
return -1;
|
||||
if (_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
|
||||
goto HandleSignal;
|
||||
}
|
||||
|
@ -46,7 +47,8 @@ static textwindows int _park_thread(uint32_t msdelay, sigset_t waitmask,
|
|||
if (ok && _weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
|
||||
HandleSignal:
|
||||
handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
|
||||
if (_check_cancel() == -1) return -1;
|
||||
if (_check_cancel() == -1)
|
||||
return -1;
|
||||
if (!restartable || (handler_was_called & SIG_HANDLED_NO_RESTART)) {
|
||||
return eintr();
|
||||
}
|
||||
|
|
|
@ -22,7 +22,8 @@
|
|||
|
||||
textwindows int sys_pause_nt(void) {
|
||||
int rc;
|
||||
while (!(rc = _park_norestart(-1u, 0))) donothing;
|
||||
while (!(rc = _park_norestart(-1u, 0)))
|
||||
donothing;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
*/
|
||||
void perror(const char *thing) {
|
||||
const char *reason;
|
||||
if (!(reason = _strerdoc(errno))) reason = "Unknown error";
|
||||
if (!(reason = _strerdoc(errno)))
|
||||
reason = "Unknown error";
|
||||
tinyprint(2, thing ? thing : "", thing ? ": " : "", reason, "\n", NULL);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,8 @@
|
|||
|
||||
int32_t sys_pipe2(int pipefd[hasatleast 2], unsigned flags) {
|
||||
int e, rc;
|
||||
if (!flags) goto OldSkool;
|
||||
if (!flags)
|
||||
goto OldSkool;
|
||||
e = errno;
|
||||
rc = __sys_pipe2(pipefd, flags);
|
||||
if (rc == -1 && errno == ENOSYS) {
|
||||
|
|
|
@ -1023,18 +1023,21 @@ static const struct sock_filter kFilterIgnoreExitGroup[] = {
|
|||
|
||||
static privileged unsigned long StrLen(const char *s) {
|
||||
unsigned long n = 0;
|
||||
while (*s++) ++n;
|
||||
while (*s++)
|
||||
++n;
|
||||
return n;
|
||||
}
|
||||
|
||||
static privileged void *MemCpy(void *d, const void *s, unsigned long n) {
|
||||
unsigned long i = 0;
|
||||
for (; i < n; ++i) ((char *)d)[i] = ((char *)s)[i];
|
||||
for (; i < n; ++i)
|
||||
((char *)d)[i] = ((char *)s)[i];
|
||||
return (char *)d + n;
|
||||
}
|
||||
|
||||
static privileged char *FixCpy(char p[17], uint64_t x, int k) {
|
||||
while (k > 0) *p++ = "0123456789abcdef"[(x >> (k -= 4)) & 15];
|
||||
while (k > 0)
|
||||
*p++ = "0123456789abcdef"[(x >> (k -= 4)) & 15];
|
||||
*p = '\0';
|
||||
return p;
|
||||
}
|
||||
|
@ -1305,7 +1308,8 @@ static privileged void MonitorSigSys(void) {
|
|||
|
||||
static privileged void AppendFilter(struct Filter *f,
|
||||
const struct sock_filter *p, size_t n) {
|
||||
if (UNLIKELY(f->n + n > ARRAYLEN(f->p))) notpossible;
|
||||
if (UNLIKELY(f->n + n > ARRAYLEN(f->p)))
|
||||
notpossible;
|
||||
MemCpy(f->p + f->n, p, n * sizeof(*f->p));
|
||||
f->n += n;
|
||||
}
|
||||
|
@ -2170,7 +2174,8 @@ static privileged void AppendPledge(struct Filter *f, //
|
|||
if ((count = CountUnspecial(p, len))) {
|
||||
if (count < 256) {
|
||||
for (j = i = 0; i < len; ++i) {
|
||||
if (p[i] & SPECIAL) continue;
|
||||
if (p[i] & SPECIAL)
|
||||
continue;
|
||||
// jump to ALLOW rule below if accumulator equals ordinal
|
||||
struct sock_filter fragment[] = {
|
||||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, // instruction
|
||||
|
@ -2192,7 +2197,8 @@ static privileged void AppendPledge(struct Filter *f, //
|
|||
|
||||
// handle "special" ordinals which use hand-crafted bpf
|
||||
for (i = 0; i < len; ++i) {
|
||||
if (!(p[i] & SPECIAL)) continue;
|
||||
if (!(p[i] & SPECIAL))
|
||||
continue;
|
||||
switch (p[i]) {
|
||||
case __NR_linux_mmap | EXEC:
|
||||
AllowMmapExec(f);
|
||||
|
|
|
@ -249,12 +249,17 @@ int pledge(const char *promises, const char *execpromises) {
|
|||
// may use pledge(0,0) to perform a support check, to determine if
|
||||
// pledge() will be able to impose the restrictions it advertises
|
||||
// within the host environment.
|
||||
if (execpromises) return einval();
|
||||
if (IsGenuineBlink()) return enosys();
|
||||
if (IsOpenbsd()) return sys_pledge(0, 0);
|
||||
if (!IsLinux()) return enosys();
|
||||
if (execpromises)
|
||||
return einval();
|
||||
if (IsGenuineBlink())
|
||||
return enosys();
|
||||
if (IsOpenbsd())
|
||||
return sys_pledge(0, 0);
|
||||
if (!IsLinux())
|
||||
return enosys();
|
||||
rc = sys_prctl(PR_GET_SECCOMP, 0, 0, 0, 0);
|
||||
if (rc == 0 || rc == 2) return 0; // 2 means we're already filtered
|
||||
if (rc == 0 || rc == 2)
|
||||
return 0; // 2 means we're already filtered
|
||||
unassert(rc < 0);
|
||||
errno = -rc;
|
||||
return -1;
|
||||
|
@ -274,9 +279,11 @@ int pledge(const char *promises, const char *execpromises) {
|
|||
STRACE("execpromises must be a subset of promises");
|
||||
rc = einval();
|
||||
} else {
|
||||
if (notsubset) iexecpromises = ipromises;
|
||||
if (notsubset)
|
||||
iexecpromises = ipromises;
|
||||
rc = sys_pledge_linux(ipromises, __pledge_mode);
|
||||
if (rc > -4096u) errno = -rc, rc = -1;
|
||||
if (rc > -4096u)
|
||||
errno = -rc, rc = -1;
|
||||
}
|
||||
} else {
|
||||
e = errno;
|
||||
|
|
|
@ -71,7 +71,8 @@ int sys_poll_metal(struct pollfd *fds, size_t nfds, unsigned timeout_ms) {
|
|||
fds[i].revents = POLLNVAL;
|
||||
}
|
||||
}
|
||||
if (fds[i].revents) ++rc;
|
||||
if (fds[i].revents)
|
||||
++rc;
|
||||
}
|
||||
if (rc || !blocking || unsignedsubtract(rdtsc(), start) >= timeout) {
|
||||
break;
|
||||
|
|
|
@ -81,7 +81,8 @@ static textwindows int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
|
|||
// we might need to spawn threads and open pipes
|
||||
__fds_lock();
|
||||
for (gotinvals = rc = sn = pn = i = 0; i < nfds; ++i) {
|
||||
if (fds[i].fd < 0) continue;
|
||||
if (fds[i].fd < 0)
|
||||
continue;
|
||||
if (__isfdopen(fds[i].fd)) {
|
||||
if (__isfdkind(fds[i].fd, kFdSocket)) {
|
||||
if (sn < ARRAYLEN(sockfds)) {
|
||||
|
|
|
@ -43,7 +43,8 @@ int posix_openpt(int flags) {
|
|||
rc = sys_openat(AT_FDCWD, "/dev/ptm", flags, 0);
|
||||
} else if (IsFreebsd()) {
|
||||
rc = sys_posix_openpt(flags);
|
||||
if (rc == -1 && errno == ENOSPC) errno = EAGAIN;
|
||||
if (rc == -1 && errno == ENOSPC)
|
||||
errno = EAGAIN;
|
||||
} else {
|
||||
rc = enosys();
|
||||
}
|
||||
|
|
|
@ -87,9 +87,11 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
|
|||
(timeout->tv_nsec + 999999) / 1000000)) {
|
||||
ms = -1;
|
||||
}
|
||||
if (sigmask) sys_sigprocmask(SIG_SETMASK, sigmask, &oldmask);
|
||||
if (sigmask)
|
||||
sys_sigprocmask(SIG_SETMASK, sigmask, &oldmask);
|
||||
rc = poll(fds, nfds, ms);
|
||||
if (sigmask) sys_sigprocmask(SIG_SETMASK, &oldmask, 0);
|
||||
if (sigmask)
|
||||
sys_sigprocmask(SIG_SETMASK, &oldmask, 0);
|
||||
}
|
||||
} else {
|
||||
uint32_t ms;
|
||||
|
|
|
@ -84,7 +84,8 @@ static ssize_t Preadv(int fd, struct iovec *iov, int iovlen, int64_t off) {
|
|||
|
||||
e = errno;
|
||||
rc = sys_preadv(fd, iov, iovlen, off, off);
|
||||
if (rc != -1 || errno != ENOSYS) return rc;
|
||||
if (rc != -1 || errno != ENOSYS)
|
||||
return rc;
|
||||
errno = e;
|
||||
|
||||
for (toto = i = 0; i < iovlen; ++i) {
|
||||
|
|
|
@ -49,13 +49,16 @@ void __printfds(struct Fd *fds, size_t fdslen) {
|
|||
int i;
|
||||
char buf[128];
|
||||
for (i = 0; i < fdslen; ++i) {
|
||||
if (!fds[i].kind) continue;
|
||||
if (!fds[i].kind)
|
||||
continue;
|
||||
kprintf("%3d %s", i, __fdkind2str(fds[i].kind));
|
||||
if (fds[i].flags) {
|
||||
kprintf(" flags=%s", (DescribeOpenFlags)(buf, fds[i].flags));
|
||||
}
|
||||
if (fds[i].mode) kprintf(" mode=%#o", fds[i].mode);
|
||||
if (fds[i].handle) kprintf(" handle=%ld", fds[i].handle);
|
||||
if (fds[i].mode)
|
||||
kprintf(" mode=%#o", fds[i].mode);
|
||||
if (fds[i].handle)
|
||||
kprintf(" handle=%ld", fds[i].handle);
|
||||
kprintf("\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,8 @@ char *program_invocation_short_name;
|
|||
__attribute__((__constructor__(10))) static textstartup void
|
||||
program_invocation_short_name_init(void) {
|
||||
char *p, *r;
|
||||
if (!__argc) return;
|
||||
if (!__argc)
|
||||
return;
|
||||
if ((p = strrchr(__argv[0], '/'))) {
|
||||
r = p + 1;
|
||||
} else {
|
||||
|
|
|
@ -47,9 +47,11 @@ long ptrace(int request, ...) {
|
|||
rc = einval(); /* see consts.sh */
|
||||
} else {
|
||||
ispeek = IsLinux() && request - 1u < 3;
|
||||
if (ispeek) data = &peek;
|
||||
if (ispeek)
|
||||
data = &peek;
|
||||
rc = __sys_ptrace(request, pid, addr, data);
|
||||
if (rc != -1 && ispeek) rc = peek;
|
||||
if (rc != -1 && ispeek)
|
||||
rc = peek;
|
||||
}
|
||||
STRACE("ptrace(%s, %d, %p, %p) → %p% m", DescribePtrace(request), pid, addr,
|
||||
data, rc);
|
||||
|
|
|
@ -83,7 +83,8 @@ static ssize_t Pwritev(int fd, const struct iovec *iov, int iovlen,
|
|||
|
||||
e = errno;
|
||||
rc = sys_pwritev(fd, iov, iovlen, off, off);
|
||||
if (rc != -1 || errno != ENOSYS) return rc;
|
||||
if (rc != -1 || errno != ENOSYS)
|
||||
return rc;
|
||||
errno = e;
|
||||
|
||||
for (toto = i = 0; i < iovlen; ++i) {
|
||||
|
|
|
@ -73,7 +73,8 @@ uint64_t rdrand(void) {
|
|||
: CFLAG_CONSTRAINT(cf), "=r"(x)
|
||||
: /* no inputs */
|
||||
: "cc");
|
||||
if (cf) return x;
|
||||
if (cf)
|
||||
return x;
|
||||
asm volatile("pause");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -276,7 +276,8 @@ static textwindows int ProcessKeyEvent(const struct NtInputRecord *r, char *p) {
|
|||
} else {
|
||||
v = -v;
|
||||
}
|
||||
do p[n++] = v;
|
||||
do
|
||||
p[n++] = v;
|
||||
while ((v >>= 8));
|
||||
return n;
|
||||
}
|
||||
|
@ -360,7 +361,8 @@ static textwindows int ProcessKeyEvent(const struct NtInputRecord *r, char *p) {
|
|||
|
||||
// finally apply thompson-pike varint encoding
|
||||
uint64_t w = tpenc(c);
|
||||
do p[n++] = w;
|
||||
do
|
||||
p[n++] = w;
|
||||
while ((w >>= 8));
|
||||
return n;
|
||||
}
|
||||
|
@ -482,7 +484,8 @@ static textwindows bool EraseKeystroke(void) {
|
|||
struct Keystroke *k = KEYSTROKE_CONTAINER(e);
|
||||
FreeKeystroke(&__keystroke.line, e);
|
||||
for (int i = k->buflen; i--;) {
|
||||
if ((k->buf[i] & 0300) == 0200) continue; // utf-8 cont
|
||||
if ((k->buf[i] & 0300) == 0200)
|
||||
continue; // utf-8 cont
|
||||
EraseCharacter();
|
||||
if (!(__ttyconf.magic & kTtyEchoRaw) && IsCtl(k->buf[i])) {
|
||||
EraseCharacter();
|
||||
|
@ -552,12 +555,15 @@ static textwindows void IngestConsoleInput(void) {
|
|||
uint32_t i, n;
|
||||
struct NtInputRecord records[16];
|
||||
for (;;) {
|
||||
if (!__keystroke.freekeys) return;
|
||||
if (__keystroke.end_of_file) return;
|
||||
if (!__keystroke.freekeys)
|
||||
return;
|
||||
if (__keystroke.end_of_file)
|
||||
return;
|
||||
if (!GetNumberOfConsoleInputEvents(__keystroke.cin, &n)) {
|
||||
goto UnexpectedEof;
|
||||
}
|
||||
if (!n) return;
|
||||
if (!n)
|
||||
return;
|
||||
n = MIN(__keystroke.freekeys, MIN(ARRAYLEN(records), n));
|
||||
if (!ReadConsoleInput(__keystroke.cin, records, n, &n)) {
|
||||
goto UnexpectedEof;
|
||||
|
@ -722,8 +728,10 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
|
|||
ms = __ttyconf.vtime * 100;
|
||||
}
|
||||
}
|
||||
if (_check_cancel() == -1) return -1;
|
||||
if (f->flags & _O_NONBLOCK) return eagain();
|
||||
if (_check_cancel() == -1)
|
||||
return -1;
|
||||
if (f->flags & _O_NONBLOCK)
|
||||
return eagain();
|
||||
if (_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
|
||||
goto DeliverSignal;
|
||||
}
|
||||
|
@ -734,15 +742,21 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
|
|||
wi = WaitForMultipleObjects(2, (int64_t[2]){__keystroke.cin, sem}, 0, ms);
|
||||
atomic_store_explicit(&pt->pt_blocker, 0, memory_order_release);
|
||||
CloseHandle(sem);
|
||||
if (wi == kNtWaitTimeout) return 0; // vtime elapsed
|
||||
if (wi == 0) return -2; // console data
|
||||
if (wi != 1) return __winerr(); // wait failed
|
||||
if (wi == kNtWaitTimeout)
|
||||
return 0; // vtime elapsed
|
||||
if (wi == 0)
|
||||
return -2; // console data
|
||||
if (wi != 1)
|
||||
return __winerr(); // wait failed
|
||||
if (_weaken(__sig_get)) {
|
||||
if (!(sig = _weaken(__sig_get)(waitmask))) return eintr();
|
||||
if (!(sig = _weaken(__sig_get)(waitmask)))
|
||||
return eintr();
|
||||
DeliverSignal:
|
||||
int handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
|
||||
if (_check_cancel() == -1) return -1;
|
||||
if (!(handler_was_called & SIG_HANDLED_NO_RESTART)) return -2;
|
||||
if (_check_cancel() == -1)
|
||||
return -1;
|
||||
if (!(handler_was_called & SIG_HANDLED_NO_RESTART))
|
||||
return -2;
|
||||
}
|
||||
return eintr();
|
||||
}
|
||||
|
@ -756,7 +770,8 @@ static textwindows ssize_t ReadFromConsole(struct Fd *f, void *data,
|
|||
IngestConsoleInput();
|
||||
bool done = DigestConsoleInput(data, size, &rc);
|
||||
UnlockKeystrokes();
|
||||
if (done) return rc;
|
||||
if (done)
|
||||
return rc;
|
||||
} while ((rc = WaitForConsole(f, waitmask)) == -2);
|
||||
return rc;
|
||||
}
|
||||
|
@ -778,7 +793,8 @@ textwindows ssize_t ReadBuffer(int fd, void *data, size_t size, int64_t offset,
|
|||
// perform heavy lifting
|
||||
ssize_t rc;
|
||||
rc = sys_readwrite_nt(fd, data, size, offset, f->handle, waitmask, ReadFile);
|
||||
if (rc != -2) return rc;
|
||||
if (rc != -2)
|
||||
return rc;
|
||||
|
||||
// mops up win32 errors
|
||||
switch (GetLastError()) {
|
||||
|
@ -798,11 +814,14 @@ static textwindows ssize_t ReadIovecs(int fd, const struct iovec *iov,
|
|||
sigset_t waitmask) {
|
||||
ssize_t rc;
|
||||
size_t i, total;
|
||||
if (opt_offset < -1) return einval();
|
||||
while (iovlen && !iov[0].iov_len) iov++, iovlen--;
|
||||
if (opt_offset < -1)
|
||||
return einval();
|
||||
while (iovlen && !iov[0].iov_len)
|
||||
iov++, iovlen--;
|
||||
if (iovlen) {
|
||||
for (total = i = 0; i < iovlen; ++i) {
|
||||
if (!iov[i].iov_len) continue;
|
||||
if (!iov[i].iov_len)
|
||||
continue;
|
||||
rc =
|
||||
ReadBuffer(fd, iov[i].iov_base, iov[i].iov_len, opt_offset, waitmask);
|
||||
if (rc == -1) {
|
||||
|
@ -813,8 +832,10 @@ static textwindows ssize_t ReadIovecs(int fd, const struct iovec *iov,
|
|||
}
|
||||
}
|
||||
total += rc;
|
||||
if (opt_offset != -1) opt_offset += rc;
|
||||
if (rc < iov[i].iov_len) break;
|
||||
if (opt_offset != -1)
|
||||
opt_offset += rc;
|
||||
if (rc < iov[i].iov_len)
|
||||
break;
|
||||
}
|
||||
return total;
|
||||
} else {
|
||||
|
|
|
@ -68,7 +68,8 @@ ssize_t readansi(int fd, char *p, size_t n) {
|
|||
e = errno;
|
||||
t = kAscii;
|
||||
x = i = j = 0;
|
||||
if (n) p[0] = 0;
|
||||
if (n)
|
||||
p[0] = 0;
|
||||
do {
|
||||
for (;;) {
|
||||
if (n) {
|
||||
|
@ -102,7 +103,8 @@ ssize_t readansi(int fd, char *p, size_t n) {
|
|||
++i;
|
||||
switch (t) {
|
||||
Whoopsie:
|
||||
if (n) p[0] = c;
|
||||
if (n)
|
||||
p[0] = c;
|
||||
t = kAscii;
|
||||
i = 1;
|
||||
/* fallthrough */
|
||||
|
|
|
@ -40,10 +40,12 @@ static textwindows ssize_t sys_readlinkat_nt_impl(int dirfd, const char *path,
|
|||
char *buf, size_t bufsiz) {
|
||||
|
||||
char16_t path16[PATH_MAX];
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1) return -1;
|
||||
if (__mkntpathat(dirfd, path, 0, path16) == -1)
|
||||
return -1;
|
||||
size_t len = strlen16(path16);
|
||||
bool must_be_directory = len > 1 && path16[len - 1] == '\\';
|
||||
if (must_be_directory) path16[--len] = 0;
|
||||
if (must_be_directory)
|
||||
path16[--len] = 0;
|
||||
|
||||
int64_t h;
|
||||
ssize_t rc;
|
||||
|
|
|
@ -41,7 +41,8 @@ ssize_t sys_readv_metal(int fd, const struct iovec *iov, int iovlen) {
|
|||
*/
|
||||
if (_weaken(sys_readv_vga)) {
|
||||
ssize_t res = _weaken(sys_readv_vga)(g_fds.p + fd, iov, iovlen);
|
||||
if (res > 0) return res;
|
||||
if (res > 0)
|
||||
return res;
|
||||
}
|
||||
/* fall through */
|
||||
case kFdSerial:
|
||||
|
@ -50,7 +51,8 @@ ssize_t sys_readv_metal(int fd, const struct iovec *iov, int iovlen) {
|
|||
file = (struct MetalFile *)g_fds.p[fd].handle;
|
||||
for (toto = i = 0; i < iovlen && file->pos < file->size; ++i) {
|
||||
got = MIN(iov[i].iov_len, file->size - file->pos);
|
||||
if (got) memcpy(iov[i].iov_base, file->base, got);
|
||||
if (got)
|
||||
memcpy(iov[i].iov_base, file->base, got);
|
||||
toto += got;
|
||||
}
|
||||
return toto;
|
||||
|
|
|
@ -82,7 +82,8 @@ sys_readwrite_nt(int fd, void *data, size_t size, ssize_t offset,
|
|||
RestartOperation:
|
||||
bool eagained = false;
|
||||
// check for signals and cancelation
|
||||
if (_check_cancel() == -1) return -1; // ECANCELED
|
||||
if (_check_cancel() == -1)
|
||||
return -1; // ECANCELED
|
||||
if (_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
|
||||
goto HandleInterrupt;
|
||||
}
|
||||
|
@ -136,7 +137,8 @@ RestartOperation:
|
|||
if (_weaken(__sig_relay) && (sig = _weaken(__sig_get)(waitmask))) {
|
||||
HandleInterrupt:
|
||||
int handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
|
||||
if (_check_cancel() == -1) return -1; // possible if we SIGTHR'd
|
||||
if (_check_cancel() == -1)
|
||||
return -1; // possible if we SIGTHR'd
|
||||
// read() is @restartable unless non-SA_RESTART hands were called
|
||||
if (!(handler_was_called & SIG_HANDLED_NO_RESTART)) {
|
||||
goto RestartOperation;
|
||||
|
|
|
@ -24,7 +24,8 @@
|
|||
// really want to avoid locking here so close() needn't block signals
|
||||
void __releasefd(int fd) {
|
||||
int f1, f2;
|
||||
if (!(0 <= fd && fd < g_fds.n)) return;
|
||||
if (!(0 <= fd && fd < g_fds.n))
|
||||
return;
|
||||
g_fds.p[fd].kind = kFdEmpty;
|
||||
bzero(g_fds.p + fd, sizeof(*g_fds.p));
|
||||
f1 = atomic_load_explicit(&g_fds.f, memory_order_relaxed);
|
||||
|
|
|
@ -28,8 +28,10 @@
|
|||
*/
|
||||
int remove(const char *name) {
|
||||
int e = errno;
|
||||
if (!unlinkat(AT_FDCWD, name, 0)) return 0;
|
||||
if (errno != EISDIR) return -1;
|
||||
if (!unlinkat(AT_FDCWD, name, 0))
|
||||
return 0;
|
||||
if (errno != EISDIR)
|
||||
return -1;
|
||||
errno = e;
|
||||
return unlinkat(AT_FDCWD, name, AT_REMOVEDIR);
|
||||
}
|
||||
|
|
|
@ -43,7 +43,8 @@ static struct termios __oldtermios;
|
|||
|
||||
static size_t __strlen(const char *s) {
|
||||
size_t i = 0;
|
||||
while (s[i]) ++i;
|
||||
while (s[i])
|
||||
++i;
|
||||
return i;
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,8 @@ int sched_getcpu(void) {
|
|||
} else {
|
||||
unsigned cpu = 0;
|
||||
int rc = sys_getcpu(&cpu, 0, 0);
|
||||
if (rc == -1) return -1;
|
||||
if (rc == -1)
|
||||
return -1;
|
||||
return cpu;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,8 @@ static int64_t GetCurrentProcessSecurityToken(void) {
|
|||
|
||||
bool32 ElevateSeDebugPrivilege(void) {
|
||||
int64_t hToken;
|
||||
if (!(hToken = GetCurrentProcessSecurityToken())) return false;
|
||||
if (!(hToken = GetCurrentProcessSecurityToken()))
|
||||
return false;
|
||||
SetPrivilegeNt(hToken, u"SeDebugPrivilege", true);
|
||||
RevertToSelf();
|
||||
CloseHandle(hToken);
|
||||
|
|
|
@ -40,9 +40,12 @@ int sys_select_nt(int nfds, fd_set *readfds, fd_set *writefds,
|
|||
struct pollfd fds[64];
|
||||
for (pfds = i = 0; i < nfds; ++i) {
|
||||
events = 0;
|
||||
if (readfds && FD_ISSET(i, readfds)) events |= POLLIN;
|
||||
if (writefds && FD_ISSET(i, writefds)) events |= POLLOUT;
|
||||
if (exceptfds && FD_ISSET(i, exceptfds)) events |= POLLERR;
|
||||
if (readfds && FD_ISSET(i, readfds))
|
||||
events |= POLLIN;
|
||||
if (writefds && FD_ISSET(i, writefds))
|
||||
events |= POLLOUT;
|
||||
if (exceptfds && FD_ISSET(i, exceptfds))
|
||||
events |= POLLERR;
|
||||
if (events) {
|
||||
if (pfds < ARRAYLEN(fds)) {
|
||||
fds[pfds].fd = i;
|
||||
|
@ -71,12 +74,16 @@ int sys_select_nt(int nfds, fd_set *readfds, fd_set *writefds,
|
|||
// call our nt poll implementation
|
||||
fdcount = sys_poll_nt(fds, pfds, &millis, sigmask);
|
||||
unassert(fdcount < 64);
|
||||
if (fdcount < 0) return -1;
|
||||
if (fdcount < 0)
|
||||
return -1;
|
||||
|
||||
// convert pollfd back to bitsets
|
||||
if (readfds) FD_ZERO(readfds);
|
||||
if (writefds) FD_ZERO(writefds);
|
||||
if (exceptfds) FD_ZERO(exceptfds);
|
||||
if (readfds)
|
||||
FD_ZERO(readfds);
|
||||
if (writefds)
|
||||
FD_ZERO(writefds);
|
||||
if (exceptfds)
|
||||
FD_ZERO(exceptfds);
|
||||
int bits = 0;
|
||||
for (i = 0; i < pfds; ++i) {
|
||||
if (fds[i].revents & POLLIN) {
|
||||
|
|
|
@ -194,7 +194,8 @@ textwindows int __sig_raise(volatile int sig, int sic) {
|
|||
|
||||
// update the signal mask in preparation for signal handller
|
||||
sigset_t blocksigs = __sighandmask[sig];
|
||||
if (!(flags & SA_NODEFER)) blocksigs |= 1ull << (sig - 1);
|
||||
if (!(flags & SA_NODEFER))
|
||||
blocksigs |= 1ull << (sig - 1);
|
||||
ctx.uc_sigmask = atomic_fetch_or_explicit(&pt->tib->tib_sigmask, blocksigs,
|
||||
memory_order_acquire);
|
||||
|
||||
|
@ -265,7 +266,8 @@ static textwindows wontreturn void __sig_tramp(struct SignalFrame *sf) {
|
|||
|
||||
// update the signal mask in preparation for signal handller
|
||||
sigset_t blocksigs = __sighandmask[sig];
|
||||
if (!(sf->flags & SA_NODEFER)) blocksigs |= 1ull << (sig - 1);
|
||||
if (!(sf->flags & SA_NODEFER))
|
||||
blocksigs |= 1ull << (sig - 1);
|
||||
sf->ctx.uc_sigmask = atomic_fetch_or_explicit(&tib->tib_sigmask, blocksigs,
|
||||
memory_order_acquire);
|
||||
|
||||
|
@ -425,7 +427,8 @@ textwindows void __sig_generate(int sig, int sic) {
|
|||
for (e = dll_first(_pthread_list); e; e = dll_next(_pthread_list, e)) {
|
||||
pt = POSIXTHREAD_CONTAINER(e);
|
||||
// we don't want to signal ourself
|
||||
if (pt == _pthread_self()) continue;
|
||||
if (pt == _pthread_self())
|
||||
continue;
|
||||
// we don't want to signal a thread that isn't running
|
||||
if (atomic_load_explicit(&pt->pt_status, memory_order_acquire) >=
|
||||
kPosixThreadTerminated) {
|
||||
|
@ -584,7 +587,8 @@ static void __sig_unmaskable(struct NtExceptionPointers *ep, int code, int sig,
|
|||
siginfo_t si = {.si_signo = sig, .si_code = code, .si_addr = si_addr};
|
||||
_ntcontext2linux(&ctx, ep->ContextRecord);
|
||||
sigset_t blocksigs = __sighandmask[sig];
|
||||
if (!(flags & SA_NODEFER)) blocksigs |= 1ull << (sig - 1);
|
||||
if (!(flags & SA_NODEFER))
|
||||
blocksigs |= 1ull << (sig - 1);
|
||||
ctx.uc_sigmask = atomic_fetch_or_explicit(&tib->tib_sigmask, blocksigs,
|
||||
memory_order_acquire);
|
||||
__sig_handler(rva)(sig, &si, &ctx);
|
||||
|
@ -668,7 +672,8 @@ textwindows int __sig_check(void) {
|
|||
}
|
||||
|
||||
__attribute__((__constructor__(10))) textstartup void __sig_init(void) {
|
||||
if (!IsWindows()) return;
|
||||
if (!IsWindows())
|
||||
return;
|
||||
AddVectoredExceptionHandler(true, (void *)__sig_crash);
|
||||
SetConsoleCtrlHandler((void *)__sig_console, true);
|
||||
}
|
||||
|
|
|
@ -58,7 +58,8 @@ static void sigaction_cosmo2native(union metasigaction *sa) {
|
|||
void *restorer;
|
||||
uint32_t masklo;
|
||||
uint32_t maskhi;
|
||||
if (!sa) return;
|
||||
if (!sa)
|
||||
return;
|
||||
flags = sa->cosmo.sa_flags;
|
||||
handler = sa->cosmo.sa_handler;
|
||||
restorer = sa->cosmo.sa_restorer;
|
||||
|
@ -106,7 +107,8 @@ static void sigaction_native2cosmo(union metasigaction *sa) {
|
|||
void *restorer = 0;
|
||||
uint32_t masklo;
|
||||
uint32_t maskhi = 0;
|
||||
if (!sa) return;
|
||||
if (!sa)
|
||||
return;
|
||||
if (IsLinux()) {
|
||||
flags = sa->linux.sa_flags;
|
||||
handler = sa->linux.sa_handler;
|
||||
|
@ -159,9 +161,12 @@ static int __sigaction(int sig, const struct sigaction *act,
|
|||
int rc, rva, oldrva;
|
||||
sigaction_f sigenter;
|
||||
struct sigaction *ap, copy;
|
||||
if (IsMetal()) return enosys(); /* TODO: Signals on Metal */
|
||||
if (!(1 <= sig && sig <= _NSIG)) return einval();
|
||||
if (sig == SIGKILL || sig == SIGSTOP) return einval();
|
||||
if (IsMetal())
|
||||
return enosys(); /* TODO: Signals on Metal */
|
||||
if (!(1 <= sig && sig <= _NSIG))
|
||||
return einval();
|
||||
if (sig == SIGKILL || sig == SIGSTOP)
|
||||
return einval();
|
||||
if (IsAsan() && ((act && !__asan_is_valid(act, sizeof(*act))) ||
|
||||
(oldact && !__asan_is_valid(oldact, sizeof(*oldact))))) {
|
||||
return efault();
|
||||
|
|
|
@ -77,10 +77,12 @@ static textwindows int sigaltstack_cosmo(const struct sigaltstack *neu,
|
|||
sigaltstack_setnew(neu);
|
||||
if (tib->tib_sigstack_addr <= bp &&
|
||||
bp <= tib->tib_sigstack_addr + tib->tib_sigstack_size) {
|
||||
if (old) old->ss_flags |= SS_ONSTACK;
|
||||
if (old)
|
||||
old->ss_flags |= SS_ONSTACK;
|
||||
tib->tib_sigstack_flags = SS_ONSTACK; // can't disable if on it
|
||||
} else if (!tib->tib_sigstack_size) {
|
||||
if (old) old->ss_flags = SS_DISABLE;
|
||||
if (old)
|
||||
old->ss_flags = SS_DISABLE;
|
||||
tib->tib_sigstack_flags = SS_DISABLE;
|
||||
}
|
||||
return 0;
|
||||
|
@ -90,7 +92,8 @@ static int sigaltstack_bsd(const struct sigaltstack *neu,
|
|||
struct sigaltstack *old) {
|
||||
int rc;
|
||||
struct sigaltstack_bsd oldbsd, neubsd, *neup = 0;
|
||||
if (neu) sigaltstack2bsd(&neubsd, neu), neup = &neubsd;
|
||||
if (neu)
|
||||
sigaltstack2bsd(&neubsd, neu), neup = &neubsd;
|
||||
if (IsXnuSilicon()) {
|
||||
rc = _sysret(__syslib->__sigaltstack(neup, &oldbsd));
|
||||
} else {
|
||||
|
@ -99,7 +102,8 @@ static int sigaltstack_bsd(const struct sigaltstack *neu,
|
|||
if (rc == -1) {
|
||||
return -1;
|
||||
}
|
||||
if (old) sigaltstack2linux(old, &oldbsd);
|
||||
if (old)
|
||||
sigaltstack2linux(old, &oldbsd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -135,10 +139,12 @@ int sigaltstack(const struct sigaltstack *neu, struct sigaltstack *old) {
|
|||
rc = enomem();
|
||||
} else if (IsLinux()) {
|
||||
rc = sys_sigaltstack(neu, old);
|
||||
if (!rc) sigaltstack_setnew(neu);
|
||||
if (!rc)
|
||||
sigaltstack_setnew(neu);
|
||||
} else if (IsBsd()) {
|
||||
rc = sigaltstack_bsd(neu, old);
|
||||
if (!rc) sigaltstack_setnew(neu);
|
||||
if (!rc)
|
||||
sigaltstack_setnew(neu);
|
||||
} else {
|
||||
rc = sigaltstack_cosmo(neu, old);
|
||||
}
|
||||
|
|
|
@ -56,7 +56,8 @@ int sigsuspend(const sigset_t *ignore) {
|
|||
} else {
|
||||
sigset_t waitmask = ignore ? *ignore : 0;
|
||||
if (IsWindows() || IsMetal()) {
|
||||
while (!(rc = _park_norestart(-1u, waitmask))) donothing;
|
||||
while (!(rc = _park_norestart(-1u, waitmask)))
|
||||
donothing;
|
||||
} else {
|
||||
rc = sys_sigsuspend((uint64_t[2]){waitmask}, 8);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,8 @@
|
|||
|
||||
int sigwait(const sigset_t *mask, int *sig) {
|
||||
siginfo_t si;
|
||||
if (sigtimedwait(mask, &si, 0) < 0) return -1;
|
||||
if (sigtimedwait(mask, &si, 0) < 0)
|
||||
return -1;
|
||||
*sig = si.si_signo;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -54,7 +54,8 @@ unsigned sleep(unsigned seconds) {
|
|||
if (cs != -1) {
|
||||
_pthread_allow_cancelation(cs);
|
||||
}
|
||||
if (!err) return 0;
|
||||
if (!err)
|
||||
return 0;
|
||||
unassert(err == EINTR);
|
||||
unslept = tv.tv_sec;
|
||||
if (tv.tv_nsec && unslept < UINT_MAX) {
|
||||
|
|
|
@ -32,7 +32,8 @@ textwindows int sys_statfs_nt(const char *path, struct statfs *sf) {
|
|||
int rc;
|
||||
int64_t h;
|
||||
char16_t path16[PATH_MAX];
|
||||
if (__mkntpath(path, path16) == -1) return -1;
|
||||
if (__mkntpath(path, path16) == -1)
|
||||
return -1;
|
||||
BLOCK_SIGNALS;
|
||||
h = __fix_enotdir(
|
||||
CreateFile(path16, kNtFileGenericRead,
|
||||
|
|
|
@ -44,12 +44,15 @@ static textwindows void InitializeWinlink(void) {
|
|||
int64_t tok;
|
||||
struct NtLuid id;
|
||||
struct NtTokenPrivileges tp;
|
||||
if (!OpenProcessToken(GetCurrentProcess(), kNtTokenAllAccess, &tok)) return;
|
||||
if (!LookupPrivilegeValue(0, u"SeCreateSymbolicLinkPrivilege", &id)) return;
|
||||
if (!OpenProcessToken(GetCurrentProcess(), kNtTokenAllAccess, &tok))
|
||||
return;
|
||||
if (!LookupPrivilegeValue(0, u"SeCreateSymbolicLinkPrivilege", &id))
|
||||
return;
|
||||
tp.PrivilegeCount = 1;
|
||||
tp.Privileges[0].Luid = id;
|
||||
tp.Privileges[0].Attributes = kNtSePrivilegeEnabled;
|
||||
if (!AdjustTokenPrivileges(tok, 0, &tp, sizeof(tp), 0, 0)) return;
|
||||
if (!AdjustTokenPrivileges(tok, 0, &tp, sizeof(tp), 0, 0))
|
||||
return;
|
||||
g_winlink.allowed = GetLastError() != kNtErrorNotAllAssigned;
|
||||
}
|
||||
|
||||
|
@ -67,8 +70,10 @@ textwindows int sys_symlinkat_nt(const char *target, int newdirfd,
|
|||
uint32_t attrs, flags;
|
||||
|
||||
// convert the paths
|
||||
if (__mkntpathat(newdirfd, linkpath, 0, M.linkpath16) == -1) return -1;
|
||||
if ((targetlen = __mkntpath(target, M.target16)) == -1) return -1;
|
||||
if (__mkntpathat(newdirfd, linkpath, 0, M.linkpath16) == -1)
|
||||
return -1;
|
||||
if ((targetlen = __mkntpath(target, M.target16)) == -1)
|
||||
return -1;
|
||||
|
||||
// determine if we need directory flag
|
||||
if ((attrs = GetFileAttributes(M.target16)) != -1u) {
|
||||
|
|
|
@ -41,7 +41,8 @@ textwindows int sys_sync_nt(void) {
|
|||
}
|
||||
}
|
||||
for (drives = GetLogicalDrives(), i = 0; i <= 'Z' - 'A'; ++i) {
|
||||
if (!(drives & (1 << i))) continue;
|
||||
if (!(drives & (1 << i)))
|
||||
continue;
|
||||
path[4] = 'A' + i;
|
||||
if (ntaccesscheck(path, R_OK | W_OK) != -1) {
|
||||
BLOCK_SIGNALS;
|
||||
|
|
|
@ -41,11 +41,13 @@ struct loadavg {
|
|||
};
|
||||
|
||||
static int64_t GetUptime(void) {
|
||||
if (IsNetbsd()) return 0; // TODO(jart): Why?
|
||||
if (IsNetbsd())
|
||||
return 0; // TODO(jart): Why?
|
||||
struct timeval x;
|
||||
size_t n = sizeof(x);
|
||||
int mib[] = {CTL_KERN, KERN_BOOTTIME};
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return 0;
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
return 0;
|
||||
return timespec_real().tv_sec - x.tv_sec;
|
||||
}
|
||||
|
||||
|
@ -53,7 +55,8 @@ static int64_t GetPhysmem(void) {
|
|||
uint64_t x = 0;
|
||||
size_t n = sizeof(x);
|
||||
int mib[] = {CTL_HW, HW_PHYSMEM};
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return 0;
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1)
|
||||
return 0;
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,8 @@
|
|||
#define TIOCDRAIN 0x2000745e // xnu, freebsd, openbsd, netbsd
|
||||
|
||||
static dontinline textwindows int sys_tcdrain_nt(int fd) {
|
||||
if (!sys_isatty(fd)) return -1; // ebadf, enotty
|
||||
if (!sys_isatty(fd))
|
||||
return -1; // ebadf, enotty
|
||||
// Tried FlushFileBuffers but it made Emacs hang when run in cmd.exe
|
||||
// "Console output is not buffered." -Quoth MSDN on FlushFileBuffers
|
||||
return 0;
|
||||
|
|
|
@ -41,10 +41,14 @@
|
|||
#define TIOCIXOFF 0x20007480 // xnu
|
||||
|
||||
static const char *DescribeFlow(char buf[12], int action) {
|
||||
if (action == TCOOFF) return "TCOOFF";
|
||||
if (action == TCOON) return "TCOON";
|
||||
if (action == TCIOFF) return "TCIOFF";
|
||||
if (action == TCION) return "TCION";
|
||||
if (action == TCOOFF)
|
||||
return "TCOOFF";
|
||||
if (action == TCOON)
|
||||
return "TCOON";
|
||||
if (action == TCIOFF)
|
||||
return "TCIOFF";
|
||||
if (action == TCION)
|
||||
return "TCION";
|
||||
FormatInt32(buf, action);
|
||||
return buf;
|
||||
}
|
||||
|
@ -89,7 +93,8 @@ static int sys_tcflow_bsd(int fd, int action) {
|
|||
static dontinline textwindows int sys_tcflow_nt(int fd, int action) {
|
||||
bool32 ok;
|
||||
int64_t h;
|
||||
if (!__isfdopen(fd)) return ebadf();
|
||||
if (!__isfdopen(fd))
|
||||
return ebadf();
|
||||
h = g_fds.p[fd].handle;
|
||||
switch (action) {
|
||||
case TCOON:
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue