summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShannon Booth <shannon.ml.booth@gmail.com>2019-12-26 16:40:35 +1300
committerAndreas Kling <awesomekling@gmail.com>2019-12-26 10:05:59 +0100
commitf6bd4f86919de178c3e3d74dd7a08b7e32942410 (patch)
tree9f2c9af963b1e635c00251aa2597b25a6bd591c6
parent17aef7dc990973247c8234bd5a9649b68e412ab6 (diff)
downloadserenity-f6bd4f86919de178c3e3d74dd7a08b7e32942410.zip
LibC: Use shared functon to generate unique filenames
Move some dupliated code into __generate_unique_filename()
-rw-r--r--Libraries/LibC/stdlib.cpp74
-rw-r--r--Libraries/LibC/stdlib.h2
2 files changed, 33 insertions, 43 deletions
diff --git a/Libraries/LibC/stdlib.cpp b/Libraries/LibC/stdlib.cpp
index 84e634deea..69239b3c15 100644
--- a/Libraries/LibC/stdlib.cpp
+++ b/Libraries/LibC/stdlib.cpp
@@ -104,9 +104,34 @@ static inline T strtol_impl(const char* nptr, char** endptr, int base)
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" {
-// 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 void __cxa_finalize(void* dso_handle);
@@ -508,28 +533,9 @@ int system(const char* command)
char* mktemp(char* pattern)
{
- int length = strlen(pattern);
-
- if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
+ if (!__generate_unique_filename(pattern))
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;
}
@@ -546,31 +552,13 @@ int mkstemp(char* pattern)
char* mkdtemp(char* pattern)
{
- int length = strlen(pattern);
-
- if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
- errno = EINVAL;
+ if (!__generate_unique_filename(pattern))
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;
- }
- }
+ if (mkdir(pattern, 0700) < 0)
+ return nullptr;
- errno = EEXIST;
- return nullptr;
+ return pattern;
}
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
diff --git a/Libraries/LibC/stdlib.h b/Libraries/LibC/stdlib.h
index 7523636232..a4cb21a410 100644
--- a/Libraries/LibC/stdlib.h
+++ b/Libraries/LibC/stdlib.h
@@ -4,6 +4,8 @@
#include <sys/cdefs.h>
#include <sys/types.h>
+[[nodiscard]] bool __generate_unique_filename(char* pattern);
+
__BEGIN_DECLS
#define EXIT_SUCCESS 0