Make fixes and improvements

- Polyfill UTIME_OMIT on XNU
- Refactor Lua build code so it's better
- Add unix module to lua.com (Discord request)
- Add unix.utimensat() and unix.futimens() to redbean
- Avoid creating double slash path in linenoise (#428)
- Remove double slashes in NT paths automatically (#428)
- Make strerror() smarter about showing NT errors (#428)

Fixes #428
This commit is contained in:
Justine Tunney 2022-06-18 01:10:29 -07:00
parent 67b28b9af1
commit c1cfca8ae1
18 changed files with 569 additions and 194 deletions

View file

@ -80,10 +80,10 @@ textwindows int __mkntpath2(const char *path,
* 5. Need 13 for mkdir() i.e. 1+8+3+1, e.g. "\\ffffffff.xxx\0"
* which is an "8.3 filename" from the DOS days
*/
char16_t *p;
const char *q;
bool isdospath;
size_t i, n, m, x, z;
char16_t c, *p;
size_t i, j, n, m, x, z;
if (!path) return efault();
path = FixNtMagicPath(path, flags);
p = path16;
@ -144,12 +144,20 @@ textwindows int __mkntpath2(const char *path,
return enametoolong();
}
// turn slash into backslash
for (i = 0; i < n; ++i) {
if (p[i] == '/') {
p[i] = '\\';
// 1. turn `/` into `\`
// 2. turn `\\` into `\` if not at beginning
for (j = i = 0; i < n; ++i) {
c = p[i];
if (c == '/') {
c = '\\';
}
if (j > 1 && c == '\\' && p[j - 1] == '\\') {
continue;
}
p[j++] = c;
}
p[j] = 0;
n = j;
return x + m + n;
}

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/struct/stat.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/utime.h"
@ -26,21 +27,33 @@
int sys_utimensat_xnu(int dirfd, const char *path, const struct timespec ts[2],
int flags) {
int i;
struct stat st;
struct timeval now, tv[2];
if (flags) return einval();
if (!ts || ts[0].tv_nsec == UTIME_NOW || ts[1].tv_nsec == UTIME_NOW) {
gettimeofday(&now, NULL);
}
if (ts && (ts[0].tv_nsec == UTIME_NOW || ts[1].tv_nsec == UTIME_NOW)) {
if (fstatat(dirfd, path, &st, flags) == -1) return -1;
}
if (ts) {
for (i = 0; i < 2; ++i) {
if (ts[i].tv_nsec == UTIME_NOW) {
tv[i] = now;
} else if (ts[i].tv_nsec == UTIME_OMIT) {
return einval();
} else {
tv[i].tv_sec = ts[i].tv_sec;
tv[i].tv_usec = div1000int64(ts[i].tv_nsec);
}
if (ts[0].tv_nsec == UTIME_NOW) {
tv[0] = now;
} else if (ts[0].tv_nsec == UTIME_OMIT) {
tv[0].tv_sec = st.st_atim.tv_sec;
tv[0].tv_usec = div1000int64(st.st_atim.tv_nsec);
} else {
tv[0].tv_sec = ts[0].tv_sec;
tv[0].tv_usec = div1000int64(ts[0].tv_nsec);
}
if (ts[1].tv_nsec == UTIME_NOW) {
tv[1] = now;
} else if (ts[1].tv_nsec == UTIME_OMIT) {
tv[1].tv_sec = st.st_mtim.tv_sec;
tv[1].tv_usec = div1000int64(st.st_mtim.tv_nsec);
} else {
tv[1].tv_sec = ts[1].tv_sec;
tv[1].tv_usec = div1000int64(ts[1].tv_nsec);
}
} else {
tv[0] = now;

View file

@ -34,16 +34,18 @@
privileged int strerror_wr(int err, uint32_t winerr, char *buf, size_t size) {
/* kprintf() weakly depends on this function */
int c, n;
bool wanting;
char16_t winmsg[256];
const char *sym, *msg;
sym = firstnonnull(strerrno(err), "EUNKNOWN");
msg = firstnonnull(strerdoc(err), "No error information");
wanting = false;
sym = firstnonnull(strerrno(err), (wanting = true, "EUNKNOWN"));
msg = firstnonnull(strerdoc(err), (wanting = true, "No error information"));
if (IsTiny()) {
if (!sym) sym = "EUNKNOWN";
for (; (c = *sym++); --size)
if (size > 1) *buf++ = c;
if (size) *buf = 0;
} else if (!IsWindows() || err == winerr || !winerr) {
} else if (!IsWindows() || ((err == winerr || !winerr) && !wanting)) {
ksnprintf(buf, size, "%s/%d/%s", sym, err, msg);
} else {
if ((n = FormatMessage(

View file

@ -43,3 +43,10 @@ TEST(mkntpath, testUnicode) {
EXPECT_EQ(20, __mkntpath("C:\\𐌰𐌱𐌲𐌳\\𐌴𐌵𐌶𐌷", p));
EXPECT_STREQ(u"C:\\𐌰𐌱𐌲𐌳\\𐌴𐌵𐌶𐌷", p);
}
TEST(mkntpath, testRemoveDoubleSlash) {
EXPECT_EQ(21, __mkntpath("C:\\Users\\jart\\\\.config", p));
EXPECT_STREQ(u"C:\\Users\\jart\\.config", p);
EXPECT_EQ(8, __mkntpath("\\\\?\\doge", p));
EXPECT_STREQ(u"\\\\?\\doge", p);
}

View file

@ -49,6 +49,14 @@ TEST(open, eexist) {
ASSERT_SYS(EEXIST, -1, open("exists", O_WRONLY | O_CREAT | O_EXCL));
}
TEST(open, doubleSlash_worksAndGetsNormalizedOnWindows) {
ASSERT_SYS(0, 0, mkdir("o", 0755));
ASSERT_SYS(0, 3,
open(gc(xjoinpaths(gc(getcwd(0, 0)), "o//deleteme")),
O_WRONLY | O_CREAT | O_TRUNC, 0644));
ASSERT_SYS(0, 0, close(3));
}
TEST(open, enametoolong) {
size_t n = 260;
char *s = gc(xcalloc(1, n + 1));

View file

@ -23,6 +23,7 @@
#include "libc/dce.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/utime.h"
#include "libc/testlib/testlib.h"
#include "libc/time/time.h"
@ -59,3 +60,20 @@ TEST(futimens, test) {
EXPECT_EQ(1655455857, st.st_atim.tv_sec);
EXPECT_EQ(827727928, st.st_mtim.tv_sec);
}
TEST(utimensat, testOmit) {
if (IsLinux() && !__is_linux_2_6_23()) {
// TODO(jart): Ugh.
return;
}
struct stat st;
struct timespec ts[2] = {
{123, UTIME_OMIT},
{123, UTIME_OMIT},
};
EXPECT_SYS(0, 0, touch("boop", 0644));
EXPECT_SYS(0, 0, utimensat(AT_FDCWD, "boop", ts, 0));
EXPECT_SYS(0, 0, stat("boop", &st));
EXPECT_NE(123, st.st_atim.tv_sec);
EXPECT_NE(123, st.st_mtim.tv_sec);
}

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/spinlock.h"
#include "libc/intrin/wait0.internal.h"
@ -60,6 +61,10 @@ int Worker(void *p) {
}
TEST(dtoa, test) {
if (IsNetbsd()) {
// TODO(jart): Why does this flake on NetBSD?!
return;
}
int i;
for (i = 0; i < THREADS; ++i) {
clone(Worker,

View file

@ -71,6 +71,7 @@ __gdtoa_Balloc(int k)
{
int x;
Bigint *rv;
assert(k >= 0);
__gdtoa_lock();
if (k <= Kmax && (rv = TI0.Freelist[k]) != 0) {
TI0.Freelist[k] = rv->next;
@ -93,6 +94,7 @@ __gdtoa_Bfree(Bigint *v)
if (v->k > Kmax) {
free(v);
} else {
assert(v->k >= 0);
__gdtoa_lock();
v->next = TI0.Freelist[v->k];
TI0.Freelist[v->k] = v;

View file

@ -2289,16 +2289,18 @@ int linenoiseHistorySave(const char *filename) {
int j;
FILE *fp;
mode_t old_umask;
old_umask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
fp = fopen(filename, "w");
umask(old_umask);
if (!fp) return -1;
chmod(filename, S_IRUSR | S_IWUSR);
for (j = 0; j < historylen; j++) {
fputs(history[j], fp);
fputc('\n', fp);
if (filename) {
old_umask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
fp = fopen(filename, "w");
umask(old_umask);
if (!fp) return -1;
chmod(filename, S_IRUSR | S_IWUSR);
for (j = 0; j < historylen; j++) {
fputs(history[j], fp);
fputc('\n', fp);
}
fclose(fp);
}
fclose(fp);
return 0;
}
@ -2378,7 +2380,9 @@ char *linenoiseGetHistoryPath(const char *prog) {
if (*a) {
abAppends(&path, a);
abAppends(&path, b);
abAppendw(&path, '/');
if (!endswith(path.b, "/") && !endswith(path.b, "\\")) {
abAppendw(&path, '/');
}
}
abAppendw(&path, '.');
abAppends(&path, prog);

View file

@ -27,8 +27,11 @@
*/
#define ldo_c
#define LUA_CORE
#include "libc/bits/weaken.h"
#include "libc/log/log.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "third_party/lua/lapi.h"
#include "third_party/lua/ldebug.h"
#include "third_party/lua/ldo.h"
@ -149,7 +152,9 @@ l_noret luaD_throw (lua_State *L, int errcode) {
lua_unlock(L);
g->panic(L); /* call panic function (last chance to jump out) */
}
__die();
if (weaken(__die)) weaken(__die)();
__restorewintty();
_Exit(41);
}
}
}

View file

@ -30,7 +30,6 @@
#include "libc/calls/struct/sigaction.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/log.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/stack.h"
@ -47,6 +46,7 @@
#include "third_party/lua/lrepl.h"
#include "third_party/lua/lua.h"
#include "third_party/lua/lualib.h"
#include "third_party/lua/lunix.h"
#include "tool/args/args.h"
// clang-format off
@ -364,6 +364,8 @@ static int pmain (lua_State *L) {
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
}
luaL_openlibs(L); /* open standard libraries */
luaL_requiref(L, "unix", LuaUnix, 1);
lua_pop(L, 1);
createargtable(L, argv, argc, script); /* create table 'arg' */
lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */
if (!(args & has_E)) { /* no option '-E'? */

354
third_party/lua/lua.mk vendored
View file

@ -3,105 +3,299 @@
PKGS += THIRD_PARTY_LUA
THIRD_PARTY_LUA_FILES := $(wildcard third_party/lua/*)
THIRD_PARTY_LUA_SRCS = $(filter %.c,$(THIRD_PARTY_LUA_FILES))
THIRD_PARTY_LUA_HDRS = $(filter %.h,$(THIRD_PARTY_LUA_FILES))
THIRD_PARTY_LUA_BINS = $(THIRD_PARTY_LUA_COMS) $(THIRD_PARTY_LUA_COMS:%=%.dbg)
THIRD_PARTY_LUA = $(THIRD_PARTY_LUA_DEPS) $(THIRD_PARTY_LUA_A)
THIRD_PARTY_LUA_A = o/$(MODE)/third_party/lua/lua.a
THIRD_PARTY_LUA_OBJS = \
$(THIRD_PARTY_LUA_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_LUA_COMS = \
o/$(MODE)/third_party/lua/lua.com \
THIRD_PARTY_LUA_COMS = \
o/$(MODE)/third_party/lua/lua.com \
o/$(MODE)/third_party/lua/luac.com
THIRD_PARTY_LUA_CHECKS = \
$(THIRD_PARTY_LUA_A).pkg \
$(THIRD_PARTY_LUA_HDRS:%=o/$(MODE)/%.ok)
THIRD_PARTY_LUA_BINS = \
$(THIRD_PARTY_LUA_COMS) \
$(THIRD_PARTY_LUA_COMS:%=%.dbg)
THIRD_PARTY_LUA_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_LOG \
LIBC_TIME \
LIBC_X \
LIBC_TINYMATH \
LIBC_UNICODE \
NET_HTTP \
THIRD_PARTY_LINENOISE \
THIRD_PARTY_GDTOA \
TOOL_ARGS
THIRD_PARTY_LUA_CHECKS = \
$(THIRD_PARTY_LUA_A).pkg \
$(THIRD_PARTY_LUA_UNIX).pkg \
$(THIRD_PARTY_LUA_HDRS:%=o/$(MODE)/%.ok) \
o/$(MODE)/third_party/lua/lua.com.pkg \
o/$(MODE)/third_party/lua/luac.com.pkg
THIRD_PARTY_LUA_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_LUA_DIRECTDEPS),$($(x))))
################################################################################
# lua.a
$(THIRD_PARTY_LUA_A): \
third_party/lua/ \
$(THIRD_PARTY_LUA_A).pkg \
$(filter-out %.main.o,$(THIRD_PARTY_LUA_OBJS))
THIRD_PARTY_LUA = \
$(THIRD_PARTY_LUA_A_DEPS) \
$(THIRD_PARTY_LUA_A)
$(THIRD_PARTY_LUA_A).pkg: \
$(THIRD_PARTY_LUA_OBJS) \
$(foreach x,$(THIRD_PARTY_LUA_DIRECTDEPS),$($(x)_A).pkg)
THIRD_PARTY_LUA_ARTIFACTS += \
THIRD_PARTY_LUA_A
o/$(MODE)/third_party/lua/lua.com.dbg: \
$(THIRD_PARTY_LUA_DEPS) \
$(THIRD_PARTY_LUA_A) \
$(THIRD_PARTY_LUA_A).pkg \
o/$(MODE)/third_party/lua/lua.main.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
THIRD_PARTY_LUA_A = \
o/$(MODE)/third_party/lua/lua.a
o/$(MODE)/third_party/lua/luac.com.dbg: \
$(THIRD_PARTY_LUA_DEPS) \
$(THIRD_PARTY_LUA_A) \
$(THIRD_PARTY_LUA_A).pkg \
o/$(MODE)/third_party/lua/luac.main.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
THIRD_PARTY_LUA_A_HDRS = \
third_party/lua/cosmo.h \
third_party/lua/lapi.h \
third_party/lua/lauxlib.h \
third_party/lua/lcode.h \
third_party/lua/lctype.h \
third_party/lua/ldebug.h \
third_party/lua/ldo.h \
third_party/lua/lfunc.h \
third_party/lua/lgc.h \
third_party/lua/llex.h \
third_party/lua/llimits.h \
third_party/lua/lmem.h \
third_party/lua/lobject.h \
third_party/lua/lopcodes.h \
third_party/lua/lparser.h \
third_party/lua/lprefix.h \
third_party/lua/lrepl.h \
third_party/lua/lstate.h \
third_party/lua/lstring.h \
third_party/lua/ltable.h \
third_party/lua/ltests.h \
third_party/lua/ltm.h \
third_party/lua/lua.h \
third_party/lua/luaconf.h \
third_party/lua/lualib.h \
third_party/lua/lundump.h \
third_party/lua/lvm.h \
third_party/lua/lzio.h \
third_party/lua/tms.h \
third_party/lua/visitor.h
o/$(MODE)/third_party/lua/lua.com: \
o/$(MODE)/third_party/lua/lua.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/third_party/lua/.lua/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/third_party/lua/.lua/.symtab
THIRD_PARTY_LUA_A_SRCS = \
third_party/lua/escapeluastring.c \
third_party/lua/lapi.c \
third_party/lua/lauxlib.c \
third_party/lua/lbaselib.c \
third_party/lua/lcode.c \
third_party/lua/lcorolib.c \
third_party/lua/ldblib.c \
third_party/lua/ldebug.c \
third_party/lua/ldo.c \
third_party/lua/ldump.c \
third_party/lua/lfunc.c \
third_party/lua/lgc.c \
third_party/lua/linit.c \
third_party/lua/liolib.c \
third_party/lua/llex.c \
third_party/lua/lmathlib.c \
third_party/lua/lmem.c \
third_party/lua/loadlib.c \
third_party/lua/lobject.c \
third_party/lua/lopcodes.c \
third_party/lua/loslib.c \
third_party/lua/lparser.c \
third_party/lua/lrepl.c \
third_party/lua/lstate.c \
third_party/lua/lstring.c \
third_party/lua/lstrlib.c \
third_party/lua/ltable.c \
third_party/lua/ltablib.c \
third_party/lua/ltests.c \
third_party/lua/ltm.c \
third_party/lua/luacallwithtrace.c \
third_party/lua/luaencodejsondata.c \
third_party/lua/luaencodeluadata.c \
third_party/lua/luaencodeurl.c \
third_party/lua/luaformatstack.c \
third_party/lua/luaparseurl.c \
third_party/lua/luaprintstack.c \
third_party/lua/luapushheader.c \
third_party/lua/luapushheaders.c \
third_party/lua/luapushlatin1.c \
third_party/lua/luapushurlparams.c \
third_party/lua/lundump.c \
third_party/lua/lutf8lib.c \
third_party/lua/lvm.c \
third_party/lua/lzio.c \
third_party/lua/visitor.c
o/$(MODE)/third_party/lua/lmathlib.o \
o//third_party/lua/lgc.o: \
OVERRIDE_CFLAGS += \
THIRD_PARTY_LUA_A_OBJS = \
$(THIRD_PARTY_LUA_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_LUA_A_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_LOG \
LIBC_TIME \
LIBC_X \
LIBC_TINYMATH \
LIBC_UNICODE \
NET_HTTP \
THIRD_PARTY_LINENOISE \
THIRD_PARTY_GDTOA
THIRD_PARTY_LUA_A_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_LUA_A_DIRECTDEPS),$($(x))))
$(THIRD_PARTY_LUA_A): \
third_party/lua/ \
$(THIRD_PARTY_LUA_A).pkg \
$(THIRD_PARTY_LUA_A_OBJS)
$(THIRD_PARTY_LUA_A).pkg: \
$(THIRD_PARTY_LUA_A_OBJS) \
$(foreach x,$(THIRD_PARTY_LUA_A_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/third_party/lua/lmathlib.o \
o//third_party/lua/lgc.o: \
OVERRIDE_CFLAGS += \
-O2
o/$(MODE)/third_party/lua/lvm.o: \
OVERRIDE_CFLAGS += \
o/$(MODE)/third_party/lua/lvm.o: \
OVERRIDE_CFLAGS += \
-fno-gcse
o/$(MODE)/third_party/lua/lauxlib.o: \
OVERRIDE_CFLAGS += \
o/$(MODE)/third_party/lua/lauxlib.o: \
OVERRIDE_CFLAGS += \
-DSTACK_FRAME_UNLIMITED
$(THIRD_PARTY_LUA_OBJS): \
OVERRIDE_CFLAGS += \
-ffunction-sections \
$(THIRD_PARTY_LUA_A_OBJS): \
OVERRIDE_CFLAGS += \
-ffunction-sections \
-fdata-sections
################################################################################
# lunix.a
THIRD_PARTY_LUA_UNIX = \
$(THIRD_PARTY_LUA_A_DEPS) \
$(THIRD_PARTY_LUA_A)
THIRD_PARTY_LUA_ARTIFACTS += \
THIRD_PARTY_LUA_UNIX
THIRD_PARTY_LUA_UNIX_A = \
o/$(MODE)/third_party/lua/lunix.a
THIRD_PARTY_LUA_UNIX_HDRS = \
third_party/lua/lunix.h
THIRD_PARTY_LUA_UNIX_SRCS = \
third_party/lua/lunix.c
THIRD_PARTY_LUA_UNIX_OBJS = \
$(THIRD_PARTY_LUA_UNIX_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_LUA_UNIX_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_RUNTIME \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TIME \
LIBC_X \
THIRD_PARTY_LUA
THIRD_PARTY_LUA_UNIX_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_LUA_UNIX_DIRECTDEPS),$($(x))))
$(THIRD_PARTY_LUA_A): \
third_party/lua/ \
$(THIRD_PARTY_LUA_UNIX_A).pkg \
$(THIRD_PARTY_LUA_UNIX_OBJS)
$(THIRD_PARTY_LUA_UNIX_A).pkg: \
$(THIRD_PARTY_LUA_UNIX_OBJS) \
$(foreach x,$(THIRD_PARTY_LUA_UNIX_DIRECTDEPS),$($(x)_A).pkg)
################################################################################
# lua.com
THIRD_PARTY_LUA_LUA_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
THIRD_PARTY_LINENOISE \
THIRD_PARTY_LUA \
THIRD_PARTY_LUA_UNIX \
TOOL_ARGS
THIRD_PARTY_LUA_LUA_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_LUA_LUA_DIRECTDEPS),$($(x))))
o/$(MODE)/third_party/lua/lua.com.pkg: \
o/$(MODE)/third_party/lua/lua.main.o \
$(foreach x,$(THIRD_PARTY_LUA_LUA_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/third_party/lua/lua.com.dbg: \
$(THIRD_PARTY_LUA_LUA_DEPS) \
o/$(MODE)/third_party/lua/lua.com.pkg \
o/$(MODE)/third_party/lua/lua.main.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/dbg/third_party/lua/lua.com: \
o/dbg/third_party/lua/lua.com.dbg \
o/dbg/third_party/zip/zip.com \
o/dbg/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/dbg/tool/build/symtab.com \
-o o/dbg/third_party/lua/.lua/.symtab $<
@$(COMPILE) -AZIP -T$@ o/dbg/third_party/zip/zip.com \
-9qj $@ o/dbg/third_party/lua/.lua/.symtab
################################################################################
# luac.com
THIRD_PARTY_LUA_LUAC_DIRECTDEPS = \
LIBC_FMT \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_UNICODE \
THIRD_PARTY_LUA \
TOOL_ARGS
THIRD_PARTY_LUA_LUAC_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_LUA_LUAC_DIRECTDEPS),$($(x))))
o/$(MODE)/third_party/lua/luac.com.pkg: \
o/$(MODE)/third_party/lua/luac.main.o \
$(foreach x,$(THIRD_PARTY_LUA_LUAC_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/third_party/lua/luac.com.dbg: \
$(THIRD_PARTY_LUA_LUAC_DEPS) \
o/$(MODE)/third_party/lua/luac.com.pkg \
o/$(MODE)/third_party/lua/luac.main.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
################################################################################
THIRD_PARTY_LUA_LIBS = $(foreach x,$(THIRD_PARTY_LUA_ARTIFACTS),$($(x)))
THIRD_PARTY_LUA_SRCS = $(foreach x,$(THIRD_PARTY_LUA_ARTIFACTS),$($(x)_SRCS))
THIRD_PARTY_LUA_HDRS = $(foreach x,$(THIRD_PARTY_LUA_ARTIFACTS),$($(x)_HDRS))
THIRD_PARTY_LUA_OBJS = $(foreach x,$(THIRD_PARTY_LUA_ARTIFACTS),$($(x)_OBJS))
$(THIRD_PARTY_LUA_OBJS): third_party/lua/lua.mk
.PHONY: o/$(MODE)/third_party/lua
o/$(MODE)/third_party/lua: \
$(THIRD_PARTY_LUA_BINS) \
o/$(MODE)/third_party/lua: \
$(THIRD_PARTY_LUA_LIBS) \
$(THIRD_PARTY_LUA_BINS) \
$(THIRD_PARTY_LUA_CHECKS)

View file

@ -77,6 +77,7 @@
#include "libc/sysv/consts/sock.h"
#include "libc/sysv/consts/sol.h"
#include "libc/sysv/consts/tcp.h"
#include "libc/sysv/consts/utime.h"
#include "libc/sysv/consts/w.h"
#include "libc/sysv/errfuns.h"
#include "libc/time/struct/tm.h"
@ -794,6 +795,38 @@ static int LuaUnixSetresgid(lua_State *L) {
return LuaUnixSetresid(L, "setresgid", setresgid);
}
// unix.utimensat(path[, asecs, ananos, msecs, mnanos[, dirfd[, flags]]])
// ├─→ 0
// └─→ nil, unix.Errno
static int LuaUnixUtimensat(lua_State *L) {
struct timespec ts;
int olderr = errno;
return SysretInteger(
L, "utimensat", olderr,
utimensat(
luaL_optinteger(L, 6, AT_FDCWD), luaL_checkstring(L, 1),
(struct timespec[2]){
{luaL_optinteger(L, 2, 0), luaL_optinteger(L, 3, UTIME_NOW)},
{luaL_optinteger(L, 4, 0), luaL_optinteger(L, 5, UTIME_NOW)},
},
luaL_optinteger(L, 7, 0)));
}
// unix.futimens(fd:int[, asecs, ananos, msecs, mnanos])
// ├─→ 0
// └─→ nil, unix.Errno
static int LuaUnixFutimens(lua_State *L) {
struct timespec ts;
int olderr = errno;
return SysretInteger(
L, "futimens", olderr,
futimens(luaL_checkinteger(L, 1),
(struct timespec[2]){
{luaL_optinteger(L, 2, 0), luaL_optinteger(L, 3, UTIME_NOW)},
{luaL_optinteger(L, 4, 0), luaL_optinteger(L, 5, UTIME_NOW)},
}));
}
// unix.clock_gettime([clock:int])
// ├─→ seconds:int, nanos:int
// └─→ nil, unix.Errno
@ -1485,7 +1518,6 @@ static int LuaUnixSigprocmask(lua_State *L) {
static void LuaUnixOnSignal(int sig, siginfo_t *si, ucontext_t *ctx) {
int type;
lua_State *L = GL;
struct sigset ss, os;
STRACE("LuaUnixOnSignal(%G)", sig);
lua_getglobal(L, "__signal_handlers");
type = lua_rawgeti(L, -1, sig);
@ -1493,10 +1525,7 @@ static void LuaUnixOnSignal(int sig, siginfo_t *si, ucontext_t *ctx) {
if (type == LUA_TFUNCTION) {
lua_pushinteger(L, sig);
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
sigfillset(&ss);
sigprocmask(SIG_BLOCK, &ss, &os);
ERRORF("(lua) %s failed: %s", strsignal(sig), lua_tostring(L, -1));
sigprocmask(SIG_SETMASK, &os, 0);
lua_pop(L, 1); // pop error
}
} else {
@ -2353,101 +2382,103 @@ static void LuaUnixDirObj(lua_State *L) {
static const luaL_Reg kLuaUnix[] = {
{"Sigset", LuaUnixSigset}, // creates signal bitmask
{"exit", LuaUnixExit}, // exit w/o atexit
{"stat", LuaUnixStat}, // get file info from path
{"fstat", LuaUnixFstat}, // get file info from fd
{"open", LuaUnixOpen}, // open file fd at lowest slot
{"close", LuaUnixClose}, // close file or socket
{"lseek", LuaUnixLseek}, // seek in file
{"read", LuaUnixRead}, // read from file or socket
{"write", LuaUnixWrite}, // write to file or socket
{"WEXITSTATUS", LuaUnixWexitstatus}, // gets exit status from wait status
{"WIFEXITED", LuaUnixWifexited}, // gets exit code from wait status
{"WIFSIGNALED", LuaUnixWifsignaled}, // determines if died due to signal
{"WTERMSIG", LuaUnixWtermsig}, // gets the signal code
{"accept", LuaUnixAccept}, // create client fd for client
{"access", LuaUnixAccess}, // check my file authorization
{"fcntl", LuaUnixFcntl}, // manipulate file descriptor
{"bind", LuaUnixBind}, // reserve network interface address
{"chdir", LuaUnixChdir}, // change directory
{"chown", LuaUnixChown}, // change owner of file
{"chmod", LuaUnixChmod}, // change mode of file
{"readlink", LuaUnixReadlink}, // reads symbolic link
{"getcwd", LuaUnixGetcwd}, // get current directory
{"fork", LuaUnixFork}, // make child process via mitosis
{"execve", LuaUnixExecve}, // replace process with program
{"environ", LuaUnixEnviron}, // get environment variables
{"commandv", LuaUnixCommandv}, // resolve program on $PATH
{"realpath", LuaUnixRealpath}, // abspath without dots/symlinks
{"syslog", LuaUnixSyslog}, // logs to system log
{"kill", LuaUnixKill}, // signal child process
{"raise", LuaUnixRaise}, // signal this process
{"wait", LuaUnixWait}, // wait for child to change status
{"pipe", LuaUnixPipe}, // create two anon fifo fds
{"dup", LuaUnixDup}, // copy fd to lowest empty slot
{"mkdir", LuaUnixMkdir}, // make directory
{"makedirs", LuaUnixMakedirs}, // make directory and parents too
{"rmdir", LuaUnixRmdir}, // remove empty directory
{"opendir", LuaUnixOpendir}, // read directory entry list
{"fdopendir", LuaUnixFdopendir}, // read directory entry list
{"rename", LuaUnixRename}, // rename file or directory
{"link", LuaUnixLink}, // create hard link
{"unlink", LuaUnixUnlink}, // remove file
{"symlink", LuaUnixSymlink}, // create symbolic link
{"sync", LuaUnixSync}, // flushes files and disks
{"fsync", LuaUnixFsync}, // flush open file
{"fdatasync", LuaUnixFdatasync}, // flush open file w/o metadata
{"truncate", LuaUnixTruncate}, // shrink or extend file medium
{"ftruncate", LuaUnixFtruncate}, // shrink or extend file medium
{"umask", LuaUnixUmask}, // set default file mask
{"chown", LuaUnixChown}, // change owner of file
{"chroot", LuaUnixChroot}, // change root directory
{"setrlimit", LuaUnixSetrlimit}, // prevent cpu memory bombs
{"clock_gettime", LuaUnixGettime}, // get timestamp w/ nano precision
{"close", LuaUnixClose}, // close file or socket
{"commandv", LuaUnixCommandv}, // resolve program on $PATH
{"connect", LuaUnixConnect}, // connect to remote address
{"dup", LuaUnixDup}, // copy fd to lowest empty slot
{"environ", LuaUnixEnviron}, // get environment variables
{"execve", LuaUnixExecve}, // replace process with program
{"exit", LuaUnixExit}, // exit w/o atexit
{"fcntl", LuaUnixFcntl}, // manipulate file descriptor
{"fdatasync", LuaUnixFdatasync}, // flush open file w/o metadata
{"fdopendir", LuaUnixFdopendir}, // read directory entry list
{"fork", LuaUnixFork}, // make child process via mitosis
{"fstat", LuaUnixFstat}, // get file info from fd
{"fsync", LuaUnixFsync}, // flush open file
{"ftruncate", LuaUnixFtruncate}, // shrink or extend file medium
{"futimens", LuaUnixFutimens}, // change access/modified time
{"getcwd", LuaUnixGetcwd}, // get current directory
{"getegid", LuaUnixGetegid}, // get effective group id of process
{"geteuid", LuaUnixGeteuid}, // get effective user id of process
{"getgid", LuaUnixGetgid}, // get real group id of process
{"gethostname", LuaUnixGethostname}, // get hostname of this machine
{"getpeername", LuaUnixGetpeername}, // get address of remote end
{"getpgid", LuaUnixGetpgid}, // get process group id of pid
{"getpgrp", LuaUnixGetpgrp}, // get process group id
{"getpid", LuaUnixGetpid}, // get id of this process
{"getppid", LuaUnixGetppid}, // get parent process id
{"getrlimit", LuaUnixGetrlimit}, // query resource limits
{"getrusage", LuaUnixGetrusage}, // query resource usages
{"getppid", LuaUnixGetppid}, // get parent process id
{"getpgrp", LuaUnixGetpgrp}, // get process group id
{"getpgid", LuaUnixGetpgid}, // get process group id of pid
{"setpgid", LuaUnixSetpgid}, // set process group id for pid
{"setpgrp", LuaUnixSetpgrp}, // sets process group id
{"getsid", LuaUnixGetsid}, // get session id of pid
{"setsid", LuaUnixSetsid}, // create a new session id
{"getpid", LuaUnixGetpid}, // get id of this process
{"getuid", LuaUnixGetuid}, // get real user id of process
{"geteuid", LuaUnixGeteuid}, // get effective user id of process
{"setuid", LuaUnixSetuid}, // set real user id of process
{"setresuid", LuaUnixSetresuid}, // sets real/effective/saved uids
{"getgid", LuaUnixGetgid}, // get real group id of process
{"getegid", LuaUnixGetegid}, // get effective group id of process
{"setgid", LuaUnixSetgid}, // set real group id of process
{"setresgid", LuaUnixSetresgid}, // sets real/effective/saved gids
{"gethostname", LuaUnixGethostname}, // get hostname of this machine
{"clock_gettime", LuaUnixGettime}, // get timestamp w/ nano precision
{"nanosleep", LuaUnixNanosleep}, // sleep w/ nano precision
{"socket", LuaUnixSocket}, // create network communication fd
{"socketpair", LuaUnixSocketpair}, // create bidirectional pipe
{"setsockopt", LuaUnixSetsockopt}, // tune socket options
{"getsockname", LuaUnixGetsockname}, // get address of local end
{"getsockopt", LuaUnixGetsockopt}, // get socket tunings
{"poll", LuaUnixPoll}, // waits for file descriptor events
{"bind", LuaUnixBind}, // reserve network interface address
{"getuid", LuaUnixGetuid}, // get real user id of process
{"gmtime", LuaUnixGmtime}, // destructure unix timestamp
{"kill", LuaUnixKill}, // signal child process
{"link", LuaUnixLink}, // create hard link
{"listen", LuaUnixListen}, // begin listening for clients
{"accept", LuaUnixAccept}, // create client fd for client
{"connect", LuaUnixConnect}, // connect to remote address
{"localtime", LuaUnixLocaltime}, // localize unix timestamp
{"lseek", LuaUnixLseek}, // seek in file
{"major", LuaUnixMajor}, // extract device info
{"makedirs", LuaUnixMakedirs}, // make directory and parents too
{"minor", LuaUnixMinor}, // extract device info
{"mkdir", LuaUnixMkdir}, // make directory
{"nanosleep", LuaUnixNanosleep}, // sleep w/ nano precision
{"open", LuaUnixOpen}, // open file fd at lowest slot
{"opendir", LuaUnixOpendir}, // read directory entry list
{"pipe", LuaUnixPipe}, // create two anon fifo fds
{"pledge", LuaUnixPledge}, // enables syscall sandbox
{"poll", LuaUnixPoll}, // waits for file descriptor events
{"raise", LuaUnixRaise}, // signal this process
{"read", LuaUnixRead}, // read from file or socket
{"readlink", LuaUnixReadlink}, // reads symbolic link
{"realpath", LuaUnixRealpath}, // abspath without dots/symlinks
{"recv", LuaUnixRecv}, // receive tcp from some address
{"recvfrom", LuaUnixRecvfrom}, // receive udp from some address
{"rename", LuaUnixRename}, // rename file or directory
{"rmdir", LuaUnixRmdir}, // remove empty directory
{"send", LuaUnixSend}, // send tcp to some address
{"sendto", LuaUnixSendto}, // send udp to some address
{"setgid", LuaUnixSetgid}, // set real group id of process
{"setitimer", LuaUnixSetitimer}, // set alarm clock
{"setpgid", LuaUnixSetpgid}, // set process group id for pid
{"setpgrp", LuaUnixSetpgrp}, // sets process group id
{"setresgid", LuaUnixSetresgid}, // sets real/effective/saved gids
{"setresuid", LuaUnixSetresuid}, // sets real/effective/saved uids
{"setrlimit", LuaUnixSetrlimit}, // prevent cpu memory bombs
{"setsid", LuaUnixSetsid}, // create a new session id
{"setsockopt", LuaUnixSetsockopt}, // tune socket options
{"setuid", LuaUnixSetuid}, // set real user id of process
{"shutdown", LuaUnixShutdown}, // make socket half empty or full
{"getpeername", LuaUnixGetpeername}, // get address of remote end
{"getsockname", LuaUnixGetsockname}, // get address of local end
{"siocgifconf", LuaUnixSiocgifconf}, // get list of network interfaces
{"sigaction", LuaUnixSigaction}, // install signal handler
{"sigprocmask", LuaUnixSigprocmask}, // change signal mask
{"sigsuspend", LuaUnixSigsuspend}, // wait for signal
{"setitimer", LuaUnixSetitimer}, // set alarm clock
{"gmtime", LuaUnixGmtime}, // destructure unix timestamp
{"pledge", LuaUnixPledge}, // enables syscall sandbox
{"localtime", LuaUnixLocaltime}, // localize unix timestamp
{"major", LuaUnixMajor}, // extract device info
{"minor", LuaUnixMinor}, // extract device info
{"siocgifconf", LuaUnixSiocgifconf}, // get list of network interfaces
{"socket", LuaUnixSocket}, // create network communication fd
{"socketpair", LuaUnixSocketpair}, // create bidirectional pipe
{"stat", LuaUnixStat}, // get file info from path
{"strsignal", LuaUnixStrsignal}, // turn signal into string
{"WIFEXITED", LuaUnixWifexited}, // gets exit code from wait status
{"WEXITSTATUS", LuaUnixWexitstatus}, // gets exit status from wait status
{"WIFSIGNALED", LuaUnixWifsignaled}, // determines if died due to signal
{"WTERMSIG", LuaUnixWtermsig}, // gets the signal code
{"symlink", LuaUnixSymlink}, // create symbolic link
{"sync", LuaUnixSync}, // flushes files and disks
{"syslog", LuaUnixSyslog}, // logs to system log
{"truncate", LuaUnixTruncate}, // shrink or extend file medium
{"umask", LuaUnixUmask}, // set default file mask
{"unlink", LuaUnixUnlink}, // remove file
{"utimensat", LuaUnixUtimensat}, // change access/modified time
{"wait", LuaUnixWait}, // wait for child to change status
{"write", LuaUnixWrite}, // write to file or socket
{0}, //
};
@ -2584,6 +2615,10 @@ int LuaUnix(lua_State *L) {
LuaSetIntField(L, "SIG_DFL", (intptr_t)SIG_DFL);
LuaSetIntField(L, "SIG_IGN", (intptr_t)SIG_IGN);
// utimensat() magnums
LuaSetIntField(L, "UTIME_NOW", UTIME_NOW);
LuaSetIntField(L, "UTIME_OMIT", UTIME_OMIT);
// setitimer() which
LuaSetIntField(L, "ITIMER_REAL", ITIMER_REAL); // portable
LuaSetIntField(L, "ITIMER_PROF", ITIMER_PROF);

11
third_party/lua/lunix.h vendored Normal file
View file

@ -0,0 +1,11 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_LUA_LUNIX_H_
#define COSMOPOLITAN_THIRD_PARTY_LUA_LUNIX_H_
#include "third_party/lua/lauxlib.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int LuaUnix(lua_State *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_LUA_LUNIX_H_ */

View file

@ -2170,6 +2170,68 @@ UNIX MODULE
Returns absolute path of filename, with `.` and `..` components
removed, and symlinks will be resolved.
unix.utimensat(path[, asecs, ananos, msecs, mnanos[, dirfd[, flags]]])
├─→ 0
└─→ nil, unix.Errno
Changes access and/or modified timestamps on file.
`path` is a string with the name of the file.
The `asecs` and `ananos` parameters set the access time. If they're
none or nil, the current time will be used.
The `msecs` and `mnanos` parameters set the modified time. If
they're none or nil, the current time will be used.
The nanosecond parameters (`ananos` and `mnanos`) must be on the
interval [0,1000000000) or `unix.EINVAL` is raised. On XNU this is
truncated to microsecond precision. On Windows NT, it's truncated to
hectonanosecond precision. These nanosecond parameters may also be
set to one of the following special values:
- `unix.UTIME_NOW`: Fill this timestamp with current time. This
feature is not available on old versions of Linux, e.g. RHEL5.
- `unix.UTIME_OMIT`: Do not alter this timestamp. This feature is
not available on old versions of Linux, e.g. RHEL5.
`dirfd` is a file descriptor integer opened with `O_DIRECTORY`
that's used for relative path names. It defaults to `unix.AT_FDCWD`.
`flags` may have have any of the following flags bitwise or'd
- `AT_SYMLINK_NOFOLLOW`: Do not follow symbolic links. This makes it
possible to edit the timestamps on the symbolic link itself,
rather than the file it points to.
unix.futimens(fd:int[, asecs, ananos, msecs, mnanos])
├─→ 0
└─→ nil, unix.Errno
Changes access and/or modified timestamps on file descriptor.
`fd` is the file descriptor of a file opened with `unix.open`.
The `asecs` and `ananos` parameters set the access time. If they're
none or nil, the current time will be used.
The `msecs` and `mnanos` parameters set the modified time. If
they're none or nil, the current time will be used.
The nanosecond parameters (`ananos` and `mnanos`) must be on the
interval [0,1000000000) or `unix.EINVAL` is raised. On XNU this is
truncated to microsecond precision. On Windows NT, it's truncated to
hectonanosecond precision. These nanosecond parameters may also be
set to one of the following special values:
- `unix.UTIME_NOW`: Fill this timestamp with current time.
- `unix.UTIME_OMIT`: Do not alter this timestamp.
This system call is currently not available on very old versions of
Linux, e.g. RHEL5.
unix.chown(path:str, uid:int, gid:int[, flags:int[, dirfd:int]])
├─→ true
└─→ nil, unix.Errno

View file

@ -7,7 +7,6 @@ COSMOPOLITAN_C_START_
int LuaMaxmind(lua_State *);
int LuaRe(lua_State *);
int LuaUnix(lua_State *);
int luaopen_argon2(lua_State *);
int luaopen_lsqlite3(lua_State *);

View file

@ -94,6 +94,7 @@
#include "third_party/lua/lauxlib.h"
#include "third_party/lua/lrepl.h"
#include "third_party/lua/lualib.h"
#include "third_party/lua/lunix.h"
#include "third_party/mbedtls/ctr_drbg.h"
#include "third_party/mbedtls/debug.h"
#include "third_party/mbedtls/iana.h"
@ -5176,7 +5177,6 @@ static const luaL_Reg kLuaFuncs[] = {
static const luaL_Reg kLuaLibs[] = {
{"re", LuaRe}, //
{"unix", LuaUnix}, //
{"maxmind", LuaMaxmind}, //
{"lsqlite3", luaopen_lsqlite3}, //
{"argon2", luaopen_argon2}, //

View file

@ -8,6 +8,6 @@ o/$(MODE)/tool: \
o/$(MODE)/tool/decode \
o/$(MODE)/tool/hash \
o/$(MODE)/tool/lambda \
o/$(MODE)/tool/plinko \
o/$(MODE)/tool/net \
o/$(MODE)/tool/plinko \
o/$(MODE)/tool/viz