LibC: Implement tmpnam()

This implementation is not really spec compliant as rand() does not
guarantee TMP_MAX different outputs + seeding
This commit is contained in:
Bananymous 2024-07-30 12:02:30 +03:00
parent 62db9a8ef3
commit e52dac3b25
1 changed files with 17 additions and 2 deletions

View File

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