1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 16:18:12 +00:00

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
parent 2cd07c6212
commit b0f19c2af4
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");
}
}