Add minor improvements and cleanup

This commit is contained in:
Justine Tunney 2020-10-27 03:39:46 -07:00
parent 9e3e985ae5
commit feed0d2b0e
163 changed files with 2286 additions and 2245 deletions

View file

@ -111,7 +111,7 @@ int fchmod(int, uint32_t) nothrow;
int fchmodat(int, const char *, uint32_t, uint32_t);
int fchown(int, uint32_t, uint32_t);
int fchownat(int, const char *, uint32_t, uint32_t, uint32_t);
int fcntl();
int fcntl(int, int, ...);
int fdatasync(int);
int filecmp(const char *, const char *);
int flock(int, int);
@ -189,7 +189,7 @@ int uname(struct utsname *);
int unlink(const char *);
int unlink_s(const char **);
int unlinkat(int, const char *, int);
int vfork(void);
int vfork(void) returnstwice;
int wait(int *);
int wait3(int *, int, struct rusage *);
int wait4(int, int *, int, struct rusage *);
@ -242,7 +242,7 @@ int vdprintf(int, const char *, va_list) paramsnonnull();
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
void _init_onntconsoleevent(void);
void _init_onwincrash(void);
void _init_wincrash(void);
#define __SIGACTION(FN, SIG, ...) \
({ \
@ -260,14 +260,14 @@ void _init_onwincrash(void);
case SIGSEGV: \
case SIGABRT: \
case SIGFPE: \
YOINK(_init_onwincrash); \
YOINK(_init_wincrash); \
break; \
default: \
break; \
} \
} else { \
YOINK(_init_onntconsoleevent); \
YOINK(_init_onwincrash); \
YOINK(_init_wincrash); \
} \
} \
(FN)(SIG, __VA_ARGS__); \

View file

@ -24,16 +24,17 @@
#include "libc/sysv/errfuns.h"
textwindows int fcntl$nt(int fd, int cmd, unsigned arg) {
uint32_t flags;
if (!isfdkind(fd, kFdFile)) return ebadf();
switch (cmd) {
case F_GETFD:
return GetHandleInformation(g_fds.p[fd].handle, &arg) ? (arg ^ FD_CLOEXEC)
: -1;
if (!GetHandleInformation(g_fds.p[fd].handle, &flags)) return -1;
arg = (flags & FD_CLOEXEC) ^ FD_CLOEXEC;
return arg;
case F_SETFD:
return SetHandleInformation(g_fds.p[fd].handle, FD_CLOEXEC,
arg ^ FD_CLOEXEC)
? 0
: -1;
arg ^= FD_CLOEXEC;
if (!SetHandleInformation(g_fds.p[fd].handle, FD_CLOEXEC, arg)) return -1;
return 0;
default:
return 0; /* TODO(jart): Implement me. */
}

View file

@ -31,7 +31,12 @@
* @return 0 on success, or -1 w/ errno
* @asyncsignalsafe
*/
int fcntl(int fd, int cmd, int arg) {
int fcntl(int fd, int cmd, ...) {
va_list va;
unsigned arg;
va_start(va, cmd);
arg = va_arg(va, unsigned);
va_end(va);
if (!IsWindows()) {
return fcntl$sysv(fd, cmd, arg);
} else {

View file

@ -1,40 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/internal.h"
#include "libc/calls/ntmagicpaths.h"
#include "libc/nexgen32e/tinystrcmp.h"
#include "libc/sysv/consts/o.h"
textwindows const char *(fixntmagicpath)(const char *path, unsigned flags) {
const struct NtMagicPaths *mp = &kNtMagicPaths;
asm("" : "+r"(mp));
if (path[0] != '/') return path;
if (tinystrcmp(path, mp->devtty) == 0) {
if ((flags & O_ACCMODE) == O_RDONLY) {
return mp->conin;
} else if ((flags & O_ACCMODE) == O_WRONLY) {
return mp->conout;
}
}
if (tinystrcmp(path, mp->devnull) == 0) return mp->nul;
if (tinystrcmp(path, mp->devstdin) == 0) return mp->conin;
if (tinystrcmp(path, mp->devstdout) == 0) return mp->conout;
return path;
}

View file

@ -261,15 +261,11 @@ void ntcontext2linux(struct ucontext *, const struct NtContext *) hidden;
struct NtOverlapped *offset2overlap(int64_t, struct NtOverlapped *) hidden;
bool32 ntsetprivilege(i64, const char16_t *, u32) hidden;
bool32 onntconsoleevent$nt(u32) hidden;
void onntalarm(void *, uint32_t, uint32_t) hidden;
void __winalarm(void *, uint32_t, uint32_t) hidden;
int ntaccesscheck(const char16_t *, u32) paramsnonnull() hidden;
i64 ntreturn(u32);
i64 winerr(void) nocallback privileged;
const char *__fixntmagicpath(const char *, unsigned) paramsnonnull() hidden;
int __mkntpath(const char *, unsigned, char16_t[hasatleast PATH_MAX - 16])
paramsnonnull() hidden;
#define mkntpath(PATH, PATH16) mkntpath2(PATH, -1u, PATH16)
#define mkntpath2(PATH, FLAGS, PATH16) \
({ \
@ -281,16 +277,6 @@ int __mkntpath(const char *, unsigned, char16_t[hasatleast PATH_MAX - 16])
Count; \
})
#define fixntmagicpath(PATH, FLAGS) \
({ \
const char *Path2; \
asm("call\tfixntmagicpath" \
: "=a"(Path2) \
: "D"(PATH), "S"(FLAGS), "m"((PATH)[0]) \
: "cc"); \
Path2; \
})
#undef sigset
#undef i32
#undef i64

View file

@ -17,15 +17,31 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/pushpop.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/hefty/ntspawn.h"
#include "libc/calls/internal.h"
#include "libc/conv/conv.h"
#include "libc/calls/ntmagicpaths.h"
#include "libc/nexgen32e/tinystrcmp.h"
#include "libc/str/str.h"
#include "libc/str/tpdecode.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
textwindows static const char *FixNtMagicPath(const char *path,
unsigned flags) {
const struct NtMagicPaths *mp = &kNtMagicPaths;
asm("" : "+r"(mp));
if (path[0] != '/') return path;
if (tinystrcmp(path, mp->devtty) == 0) {
if ((flags & O_ACCMODE) == O_RDONLY) {
return mp->conin;
} else if ((flags & O_ACCMODE) == O_WRONLY) {
return mp->conout;
}
}
if (tinystrcmp(path, mp->devnull) == 0) return mp->nul;
if (tinystrcmp(path, mp->devstdin) == 0) return mp->conin;
if (tinystrcmp(path, mp->devstdout) == 0) return mp->conout;
return path;
}
/**
* Copies path for Windows NT.
*
@ -33,13 +49,13 @@
* forward-slashes with backslashes; and (3) remapping several
* well-known paths (e.g. /dev/null NUL) for convenience.
*
* @param flags is used by open()
* @param path16 is shortened so caller can prefix, e.g. \\.\pipe\, and
* due to a plethora of special-cases throughout the Win32 API
* @param flags is used by open(), see fixntmagicpath2()
* @return short count excluding NUL on success, or -1 w/ errno
* @error ENAMETOOLONG
*/
forcealignargpointer textwindows int(mkntpath)(
forcealignargpointer textwindows int mkntpath(
const char *path, unsigned flags,
char16_t path16[hasatleast PATH_MAX - 16]) {
/*
@ -52,7 +68,7 @@ forcealignargpointer textwindows int(mkntpath)(
int rc;
wint_t wc;
size_t i, j;
path = fixntmagicpath(path, flags);
path = FixNtMagicPath(path, flags);
i = 0;
j = 0;
for (;;) {

View file

@ -55,7 +55,7 @@ static struct ItimerNt {
static uint32_t ItimerWorker(void *arg) {
do {
if (!WaitForSingleObject(g_itimernt.ith, -1)) {
onntalarm(NULL, 0, 0);
__winalarm(NULL, 0, 0);
}
} while (g_itimernt.ith && g_itimernt.tid == GetCurrentThreadId());
return 0;

View file

@ -23,5 +23,5 @@
onntconsoleevent$nt:
ezlea onntconsoleevent,ax
jmp nt2sysv
jmp __nt2sysv
.endfn onntconsoleevent$nt,globl,hidden

View file

@ -21,7 +21,7 @@
.text.windows
.source __FILE__
onwincrash$nt:
ezlea onwincrash,ax
jmp nt2sysv
.endfn onwincrash$nt,globl
__wincrash$nt:
ezlea __wincrash,ax
jmp __nt2sysv
.endfn __wincrash$nt,globl

View file

@ -21,7 +21,7 @@
.text.windows
.source __FILE__
onntalarm$nt:
ezlea onntalarm,ax
jmp nt2sysv
.endfn onntalarm$nt,globl,hidden
__winalarm$nt:
ezlea __winalarm,ax
jmp __nt2sysv
.endfn __winalarm$nt,globl,hidden

View file

@ -22,8 +22,8 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
void onntalarm(void *lpArgToCompletionRoutine, uint32_t dwTimerLowValue,
uint32_t dwTimerHighValue) {
void __winalarm(void *lpArgToCompletionRoutine, uint32_t dwTimerLowValue,
uint32_t dwTimerHighValue) {
siginfo_t info;
memset(&info, 0, sizeof(info));
info.si_signo = SIGALRM;

View file

@ -27,7 +27,7 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
textwindows unsigned onwincrash(struct NtExceptionPointers *ep) {
textwindows unsigned __wincrash(struct NtExceptionPointers *ep) {
int sig;
struct Goodies {
ucontext_t ctx;

View file

@ -20,8 +20,8 @@
#include "libc/macros.h"
.source __FILE__
.init.start 300,_init_onwincrash
.init.start 300,_init_wincrash
pushpop 1,%rcx
ezlea onwincrash$nt,dx
ezlea __wincrash$nt,dx
ntcall __imp_AddVectoredExceptionHandler
.init.end 300,_init_onwincrash,globl,hidden
.init.end 300,_init_wincrash,globl,hidden