From e52dac3b2542f07522f5f7f21748787e4211905e Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 30 Jul 2024 12:02:30 +0300 Subject: [PATCH] LibC: Implement tmpnam() This implementation is not really spec compliant as rand() does not guarantee TMP_MAX different outputs + seeding --- userspace/libraries/LibC/stdio.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/userspace/libraries/LibC/stdio.cpp b/userspace/libraries/LibC/stdio.cpp index 6a298e02a1..757cfad02a 100644 --- a/userspace/libraries/LibC/stdio.cpp +++ b/userspace/libraries/LibC/stdio.cpp @@ -667,8 +667,23 @@ char* tempnam(const char*, const char*); // TODO FILE* tmpfile(void); -// TODO -char* tmpnam(char*); +char* tmpnam(char* storage) +{ + static int s_counter = rand(); + static char s_storage[PATH_MAX]; + if (storage == nullptr) + storage = s_storage; + for (int i = 0; i < TMP_MAX; i++) + { + sprintf(storage, "/tmp/tmp_%04x", s_counter); + s_counter = rand(); + + struct stat st; + if (stat(storage, &st) == -1 && errno == ENOENT) + break; + } + return storage; +} // TODO int ungetc(int, FILE*);