mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:07:35 +00:00
LibC: Use shared functon to generate unique filenames
Move some dupliated code into __generate_unique_filename()
This commit is contained in:
parent
17aef7dc99
commit
f6bd4f8691
2 changed files with 33 additions and 43 deletions
|
@ -104,9 +104,34 @@ static inline T strtol_impl(const char* nptr, char** endptr, int base)
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool __generate_unique_filename(char* pattern)
|
||||||
|
{
|
||||||
|
int length = strlen(pattern);
|
||||||
|
|
||||||
|
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int start = length - 6;
|
||||||
|
|
||||||
|
static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < 100; ++attempt) {
|
||||||
|
for (int i = 0; i < 6; ++i)
|
||||||
|
pattern[start + i] = random_characters[(rand() % sizeof(random_characters))];
|
||||||
|
struct stat st;
|
||||||
|
int rc = lstat(pattern, &st);
|
||||||
|
if (rc < 0 && errno == ENOENT)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
errno = EEXIST;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
// Itanium C++ ABI methods defined in crt0.cpp
|
// Itanium C++ ABI methods defined in crt0.cpp
|
||||||
extern int __cxa_atexit(void (*function)(void*), void* paramter, void* dso_handle);
|
extern int __cxa_atexit(void (*function)(void*), void* paramter, void* dso_handle);
|
||||||
extern void __cxa_finalize(void* dso_handle);
|
extern void __cxa_finalize(void* dso_handle);
|
||||||
|
|
||||||
|
@ -508,28 +533,9 @@ int system(const char* command)
|
||||||
|
|
||||||
char* mktemp(char* pattern)
|
char* mktemp(char* pattern)
|
||||||
{
|
{
|
||||||
int length = strlen(pattern);
|
if (!__generate_unique_filename(pattern))
|
||||||
|
|
||||||
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
|
||||||
pattern[0] = '\0';
|
pattern[0] = '\0';
|
||||||
errno = EINVAL;
|
|
||||||
return pattern;
|
|
||||||
}
|
|
||||||
|
|
||||||
int start = length - 6;
|
|
||||||
|
|
||||||
static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
||||||
|
|
||||||
for (int attempt = 0; attempt < 100; ++attempt) {
|
|
||||||
for (int i = 0; i < 6; ++i)
|
|
||||||
pattern[start + i] = random_characters[(rand() % sizeof(random_characters))];
|
|
||||||
struct stat st;
|
|
||||||
int rc = lstat(pattern, &st);
|
|
||||||
if (rc < 0 && errno == ENOENT)
|
|
||||||
return pattern;
|
|
||||||
}
|
|
||||||
pattern[0] = '\0';
|
|
||||||
errno = EEXIST;
|
|
||||||
return pattern;
|
return pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -546,31 +552,13 @@ int mkstemp(char* pattern)
|
||||||
|
|
||||||
char* mkdtemp(char* pattern)
|
char* mkdtemp(char* pattern)
|
||||||
{
|
{
|
||||||
int length = strlen(pattern);
|
if (!__generate_unique_filename(pattern))
|
||||||
|
|
||||||
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
|
||||||
errno = EINVAL;
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
|
||||||
|
|
||||||
int start = length - 6;
|
if (mkdir(pattern, 0700) < 0)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
return pattern;
|
||||||
|
|
||||||
for (int attempt = 0; attempt < 100; ++attempt) {
|
|
||||||
for (int i = 0; i < 6; ++i)
|
|
||||||
pattern[start + i] = random_characters[(rand() % sizeof(random_characters))];
|
|
||||||
struct stat st;
|
|
||||||
int rc = lstat(pattern, &st);
|
|
||||||
if (rc < 0 && errno == ENOENT) {
|
|
||||||
if (mkdir(pattern, 0700) < 0)
|
|
||||||
return nullptr;
|
|
||||||
return pattern;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
errno = EEXIST;
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
|
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
[[nodiscard]] bool __generate_unique_filename(char* pattern);
|
||||||
|
|
||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
|
|
||||||
#define EXIT_SUCCESS 0
|
#define EXIT_SUCCESS 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue