From f45dd85be3be80cc249666f3e03d2ae45a089ab5 Mon Sep 17 00:00:00 2001 From: rjkiv <76180273+rjkiv@users.noreply.github.com> Date: Mon, 1 Sep 2025 14:10:25 -0700 Subject: [PATCH] more wstr funcs --- dll/msvcrt.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- strutil.cpp | 13 +++++++++++++ strutil.h | 1 + 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/dll/msvcrt.cpp b/dll/msvcrt.cpp index bdf5074..e12f982 100644 --- a/dll/msvcrt.cpp +++ b/dll/msvcrt.cpp @@ -1,5 +1,6 @@ #include "common.h" #include +#include #include #include #include @@ -117,6 +118,10 @@ namespace msvcrt { return 0; } + char* WIN_ENTRY getenv(const char *varname){ + return std::getenv(varname); + } + char* WIN_ENTRY setlocale(int category, const char *locale){ return std::setlocale(category, locale); } @@ -151,6 +156,34 @@ namespace msvcrt { return 0; } + int WIN_ENTRY _wgetenv_s(size_t* pReturnValue, uint16_t* buffer, size_t numberOfElements, const uint16_t* varname){ + std::string var_str = wideStringToString(varname); + DEBUG_LOG("_wgetenv_s: var name %s\n", var_str.c_str()); + if(!buffer || !varname) return 22; + + size_t varnamelen = wstrlen(varname); + + for(uint16_t** env = __winitenv; env && *env; ++env){ + uint16_t* cur = *env; + std::string cur_str = wideStringToString(cur); + if(wstrncmp(cur, varname, varnamelen) == 0 && cur[varnamelen] == L'='){ + uint16_t* value = cur + varnamelen + 1; + size_t value_len = wstrlen(value); + + size_t copy_len = (value_len < numberOfElements - 1) ? value_len : numberOfElements - 1; + wstrncpy(buffer, value, copy_len); + buffer[copy_len] = 0; + + if(pReturnValue) *pReturnValue = value_len + 1; + return 0; + } + } + + buffer[0] = 0; + if(pReturnValue) *pReturnValue = 0; + return 0; + } + void* WIN_ENTRY malloc(size_t size){ return std::malloc(size); } @@ -371,10 +404,18 @@ namespace msvcrt { return wstrstr(dest, src); } - int WIN_ENTRY iswspace(wint_t w){ + int WIN_ENTRY iswspace(uint32_t w){ return std::iswspace(w); } + int WIN_ENTRY iswdigit(uint32_t w){ + return std::iswdigit(w); + } + + const uint16_t* WIN_ENTRY wcschr(const uint16_t* str, uint16_t c){ + return wstrchr(str, c); + } + const uint16_t* WIN_ENTRY wcsrchr(const uint16_t *str, uint16_t c){ return wstrrchr(str, c); } @@ -421,6 +462,10 @@ static void *resolveByName(const char *name) { if (strcmp(name, "iswspace") == 0) return (void*)msvcrt::iswspace; if (strcmp(name, "wcsrchr") == 0) return (void*)msvcrt::wcsrchr; if (strcmp(name, "wcstoul") == 0) return (void*)msvcrt::wcstoul; + if (strcmp(name, "iswdigit") == 0) return (void*)msvcrt::iswdigit; + if (strcmp(name, "wcschr") == 0) return (void*)msvcrt::wcschr; + if (strcmp(name, "getenv") == 0) return (void*)msvcrt::getenv; + if (strcmp(name, "_wgetenv_s") == 0) return (void*)msvcrt::_wgetenv_s; return nullptr; } diff --git a/strutil.cpp b/strutil.cpp index 6212312..31eb9b1 100644 --- a/strutil.cpp +++ b/strutil.cpp @@ -50,6 +50,19 @@ const uint16_t* wstrstr(const uint16_t *dest, const uint16_t *src){ return nullptr; } +uint16_t* wstrchr(const uint16_t* str, uint16_t c) { + for (; *str != 0; str++) { + if (*str == c) { + return (uint16_t*)str; + } + } + // If searching for '\0', return pointer to terminator + if (c == 0) { + return (uint16_t*)str; + } + return nullptr; +} + uint16_t* wstrrchr(const uint16_t* str, uint16_t c){ const uint16_t* last = nullptr; for (; *str != 0; str++) { diff --git a/strutil.h b/strutil.h index 12fa42a..4aa4728 100644 --- a/strutil.h +++ b/strutil.h @@ -5,6 +5,7 @@ size_t wstrlen(const uint16_t *str); size_t wstrnlen(const uint16_t* str, size_t numberOfElements); int wstrncmp(const uint16_t *string1, const uint16_t *string2, size_t count); const uint16_t* wstrstr(const uint16_t *dest, const uint16_t *src); +uint16_t* wstrchr(const uint16_t* str, uint16_t c); uint16_t* wstrrchr(const uint16_t* str, uint16_t c); uint16_t* wstrcat(uint16_t* dest, const uint16_t* src); uint16_t* wstrncat(uint16_t* dest, const uint16_t* src, size_t count);