mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-25 15:39:55 +00:00
LibC: Implement mkdtemp library function
This commit is contained in:
parent
161cb89e87
commit
2d24b12a34
Notes:
sideshowbarker
2024-07-19 12:06:59 +09:00
Author: https://github.com/mauri870 Commit: https://github.com/SerenityOS/serenity/commit/2d24b12a348 Pull-request: https://github.com/SerenityOS/serenity/pull/559
2 changed files with 32 additions and 2 deletions
|
@ -13,6 +13,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -278,8 +279,7 @@ char* mktemp(char* pattern)
|
|||
{
|
||||
int length = strlen(pattern);
|
||||
|
||||
// FIXME: Check for an invalid template pattern and return EINVAL.
|
||||
if (length < 6) {
|
||||
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
||||
pattern[0] = '\0';
|
||||
errno = EINVAL;
|
||||
return pattern;
|
||||
|
@ -302,6 +302,35 @@ char* mktemp(char* pattern)
|
|||
return pattern;
|
||||
}
|
||||
|
||||
char* mkdtemp(char* pattern)
|
||||
{
|
||||
int length = strlen(pattern);
|
||||
|
||||
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
||||
errno = EINVAL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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) {
|
||||
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*))
|
||||
{
|
||||
dbgprintf("FIXME(LibC): bsearch(%p, %p, %u, %u, %p)\n", key, base, nmemb, size, compar);
|
||||
|
|
|
@ -38,6 +38,7 @@ long labs(long);
|
|||
double atof(const char*);
|
||||
int system(const char* command);
|
||||
char* mktemp(char*);
|
||||
char* mkdtemp(char*);
|
||||
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
|
||||
|
||||
#define RAND_MAX 32767
|
||||
|
|
Loading…
Reference in a new issue