From b907263f3509899644d26e860fc5e032cf922ebd Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 20 May 2025 19:10:22 +0300 Subject: [PATCH] LibC: Implement basic tmpfile --- userspace/libraries/LibC/stdio.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/userspace/libraries/LibC/stdio.cpp b/userspace/libraries/LibC/stdio.cpp index 0f1a91ba..400d7148 100644 --- a/userspace/libraries/LibC/stdio.cpp +++ b/userspace/libraries/LibC/stdio.cpp @@ -965,7 +965,29 @@ char* tempnam(const char*, const char*); FILE* tmpfile(void) { - ASSERT_NOT_REACHED(); + for (;;) + { + char path[PATH_MAX]; + if (tmpnam(path) == nullptr) + return nullptr; + + int fd = open(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); + if (fd == -1) + { + if (errno == EEXIST) + continue; + return nullptr; + } + + (void)unlink(path); + + FILE* fp = fdopen(fd, "wb+"); + if (fp != nullptr) + return fp; + + close(fd); + return nullptr; + } } char* tmpnam(char* storage)