LibC: Templatize unique filename enumeration for mkstemp() et al

This allows us to implement mkstemp() with open() directly, instead of
first lstat()'ing, and then open()'ing the filename.

Also implement tmpfile() in terms of mkstemp() instead of mktemp().
This commit is contained in:
Andreas Kling 2021-01-22 19:23:36 +01:00
commit b0f19c2af4
Notes: sideshowbarker 2024-07-18 23:00:19 +09:00
2 changed files with 38 additions and 30 deletions

View file

@ -1205,16 +1205,11 @@ void funlockfile([[maybe_unused]] FILE* filehandle)
FILE* tmpfile()
{
char tmp_path[] = "/tmp/XXXXXX";
if (__generate_unique_filename(tmp_path) < 0)
return nullptr;
int fd = open(tmp_path, O_CREAT | O_EXCL | O_RDWR, S_IWUSR | S_IRUSR);
int fd = mkstemp(tmp_path);
if (fd < 0)
return nullptr;
// FIXME: instead of using this hack, implement with O_TMPFILE or similar
unlink(tmp_path);
return fdopen(fd, "rw");
}
}