diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-02-26 15:57:59 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-02-26 15:57:59 +0100 |
commit | a356746d04b50968f4b5d4eb43c587efc61dffba (patch) | |
tree | e55e826ddbdfc2e184c325933a910228ec310019 /LibC/stdlib.cpp | |
parent | 2e5b9d318f51f73b0ac155f00345a70bf287f5e6 (diff) | |
download | serenity-a356746d04b50968f4b5d4eb43c587efc61dffba.zip |
Compat work towards porting vim.
Diffstat (limited to 'LibC/stdlib.cpp')
-rw-r--r-- | LibC/stdlib.cpp | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp index d7e12e9276..37078f4986 100644 --- a/LibC/stdlib.cpp +++ b/LibC/stdlib.cpp @@ -10,6 +10,8 @@ #include <AK/Types.h> #include <Kernel/Syscall.h> #include <AK/StdLibExtras.h> +#include <AK/HashMap.h> +#include <AK/AKString.h> extern "C" { @@ -31,8 +33,8 @@ struct MallocFooter { uint32_t xorcheck; }; -#define CHUNK_SIZE 8 -#define POOL_SIZE 128 * 1024 +#define CHUNK_SIZE 16 +#define POOL_SIZE 4 * 1048576 static const size_t malloc_budget = POOL_SIZE; static byte s_malloc_map[POOL_SIZE / CHUNK_SIZE / 8]; @@ -201,13 +203,56 @@ char* getenv(const char* name) return nullptr; } -int putenv(char*) +int putenv(char* new_var) { - assert(false); + HashMap<String, String> environment; + + auto handle_environment_entry = [&environment] (const char* decl) { + char* eq = strchr(decl, '='); + if (!eq) + return; + size_t var_length = eq - decl; + char* var = (char*)alloca(var_length + 1); + memcpy(var, decl, var_length); + var[var_length] = '\0'; + const char* value = eq + 1; + environment.set(var, value); + }; + for (size_t i = 0; environ[i]; ++i) + handle_environment_entry(environ[i]); + handle_environment_entry(new_var); + + //extern bool __environ_is_malloced; + //if (__environ_is_malloced) + // free(environ); + //__environ_is_malloced = true; + + int environment_size = sizeof(char*); // For the null sentinel. + for (auto& it : environment) + environment_size += (int)sizeof(char*) + it.key.length() + 1 + it.value.length() + 1; + + char* buffer = (char*)malloc(environment_size); + environ = (char**)buffer; + char* bufptr = buffer + sizeof(char*) * (environment.size() + 1); + + int i = 0; + for (auto& it : environment) { + environ[i] = bufptr; + memcpy(bufptr, it.key.characters(), it.key.length()); + bufptr += it.key.length(); + *(bufptr++) = '='; + memcpy(bufptr, it.value.characters(), it.value.length()); + bufptr += it.value.length(); + *(bufptr++) = '\0'; + ++i; + } + environ[environment.size()] = nullptr; + return 0; } -double atof(const char*) +double atof(const char* str) { + dbgprintf("LibC: atof: '%s'\n", str); assert(false); } |