From b40d41085dff4c4e83f8b8a16a346c263239352c Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 13 Aug 2023 07:17:40 -0700 Subject: [PATCH] Fix the build --- libc/calls/mkstemp.c | 50 +++++++++++++++++++----------------- libc/calls/tmpfd.c | 4 ++- libc/sysv/consts.sh | 2 +- libc/sysv/consts/O_TMPFILE.S | 2 +- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/libc/calls/mkstemp.c b/libc/calls/mkstemp.c index a24747f3a..d84c8ff51 100644 --- a/libc/calls/mkstemp.c +++ b/libc/calls/mkstemp.c @@ -26,6 +26,32 @@ #include "libc/sysv/consts/o.h" #include "libc/sysv/errfuns.h" +int _mkstemp(char *template, int oflags) { + uint64_t w; + int i, n, e, fd; + if ((n = strlen(template)) < 6 || + READ16LE(template + n - 2) != READ16LE("XX") || + READ32LE(template + n - 6) != READ32LE("XXXX")) { + return einval(); + } + for (;;) { + w = _rand64(); + for (i = 0; i < 6; ++i) { + template[n - 6 + i] = "0123456789abcdefghijklmnopqrstuvwxyz"[w % 36]; + w /= 36; + } + e = errno; + if ((fd = open(template, O_RDWR | O_CREAT | O_EXCL | oflags, 0600)) != -1) { + return fd; + } else if (errno == EEXIST) { + errno = e; + } else { + template[0] = 0; + return fd; + } + } +} + /** * Creates temporary file name and file descriptor. * @@ -52,27 +78,5 @@ * @see tmpfd() if you don't need a path */ int mkstemp(char *template) { - uint64_t w; - int i, n, e, fd; - if ((n = strlen(template)) < 6 || - READ16LE(template + n - 2) != READ16LE("XX") || - READ32LE(template + n - 6) != READ32LE("XXXX")) { - return einval(); - } - for (;;) { - w = _rand64(); - for (i = 0; i < 6; ++i) { - template[n - 6 + i] = "0123456789abcdefghijklmnopqrstuvwxyz"[w % 36]; - w /= 36; - } - e = errno; - if ((fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600)) != -1) { - return fd; - } else if (errno == EEXIST) { - errno = e; - } else { - template[0] = 0; - return fd; - } - } + return _mkstemp(template, 0); } diff --git a/libc/calls/tmpfd.c b/libc/calls/tmpfd.c index a2df85044..736be6d1e 100644 --- a/libc/calls/tmpfd.c +++ b/libc/calls/tmpfd.c @@ -27,6 +27,8 @@ #define _O_TMPFILE 000020200000 +int _mkstemp(char *, int); + /** * Returns file descriptor of open anonymous file, e.g. * @@ -88,7 +90,7 @@ int tmpfd(void) { if (!(prog = program_invocation_short_name)) prog = "tmp"; strlcat(path, prog, sizeof(path)); strlcat(path, ".XXXXXX", sizeof(path)); - if ((fd = mkstemp(path)) == -1) return -1; + if ((fd = _mkstemp(path, IsWindows() ? 0x00410000 : 0)) == -1) return -1; if (!IsWindows()) unassert(!unlink(path)); return fd; } diff --git a/libc/sysv/consts.sh b/libc/sysv/consts.sh index bfbff020f..6b7df79cd 100755 --- a/libc/sysv/consts.sh +++ b/libc/sysv/consts.sh @@ -193,7 +193,7 @@ syscon open O_SEQUENTIAL 0 0 0 0 0 0 0 0x40000000 # kNtFileFl syscon open O_COMPRESSED 0 0 0 0 0 0 0 0x20000000 # kNtFileAttributeCompressed [SYNC libc/calls/open-nt.c] syscon open O_INDEXED 0 0 0 0 0 0 0 0x10000000 # !kNtFileAttributeNotContentIndexed [SYNC libc/calls/open-nt.c] syscon open O_CLOEXEC 0x00080000 0x00080000 0x01000000 0x01000000 0x00100000 0x00010000 0x00400000 0x00080000 # NT faked as Linux [SYNC libc/calls/open-nt.c] -syscon open O_TMPFILE 0x00410000 0x00404000 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0x00410000 # please use tmpfd(); Linux 3.11+ (c. 2013) __O_TMPFILE | O_DIRECTORY; kNtFileAttributeTemporary|kNtFileFlagDeleteOnClose [SYNC libc/calls/open-nt.c] +syscon open O_TMPFILE 0x00410000 0x00404000 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff # please use tmpfd(); Linux 3.11+ (c. 2013) __O_TMPFILE | O_DIRECTORY; kNtFileAttributeTemporary|kNtFileFlagDeleteOnClose [SYNC libc/calls/open-nt.c] syscon open O_SPARSE 0 0 0 0 0 0 0 0 # wut syscon open O_NONBLOCK 0x00000800 0x00000800 0x00000004 0x00000004 0x00000004 0x00000004 0x00000004 0x00000800 # bsd consensus syscon open O_ASYNC 0x00002000 0x00002000 0x00000040 0x00000040 0x00000040 0x00000040 0x00000040 0 # bsd consensus diff --git a/libc/sysv/consts/O_TMPFILE.S b/libc/sysv/consts/O_TMPFILE.S index 95671cf79..84899b72c 100644 --- a/libc/sysv/consts/O_TMPFILE.S +++ b/libc/sysv/consts/O_TMPFILE.S @@ -1,2 +1,2 @@ #include "libc/sysv/consts/syscon.internal.h" -.syscon open,O_TMPFILE,0x00410000,0x00404000,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0x00410000 +.syscon open,O_TMPFILE,0x00410000,0x00404000,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff