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(