LibC: Implement basic tmpfile

This commit is contained in:
Bananymous 2025-05-20 19:10:22 +03:00
parent 0f0accf82c
commit b907263f35
1 changed files with 23 additions and 1 deletions

View File

@ -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)