From d50ea76bdc4a7ae81cd6e81edd9273b5c6bf95d6 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 19 Jun 2023 11:52:38 +0200 Subject: [PATCH] libc: Make tmpfile() create files in /tmp's filesystem --- libc/src/stdio.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index d58cae21..0ce0b5fc 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -11,6 +11,13 @@ FILE* stdin = nullptr; FILE* stderr = nullptr; FILE* stdout = nullptr; +static const char* read_tmpdir() +{ + const char* tmpdir = getenv("TMPDIR"); + if (!tmpdir) return "/tmp"; + return tmpdir; +} + static int fopen_parse_mode(const char* mode) { int result = 0; @@ -477,8 +484,7 @@ extern "C" FILE* tmpfile() { - // FIXME: use /tmp as the directory when the tmpfs is mounted only there. - int fd = open("/", O_RDWR | O_TMPFILE, 0600); + int fd = open(read_tmpdir(), O_RDWR | O_TMPFILE, 0600); if (fd < 0) return nullptr; FILE* f = fdopen(fd, "w+b");