diff options
| author | Yang Liu <liuyang22@iscas.ac.cn> | 2025-06-05 15:43:30 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-05 09:43:30 +0200 |
| commit | e9242b884980187cb2090ae6a589e74da9613311 (patch) | |
| tree | a1a5d294f133e63428a4cbef3e686e70dcdbb1ec /src/tools/env.c | |
| parent | c5755c8241177c2cc7d95709fa0f0ac42111d141 (diff) | |
| download | box64-e9242b884980187cb2090ae6a589e74da9613311.tar.gz box64-e9242b884980187cb2090ae6a589e74da9613311.zip | |
[WOW64] Added rcfile support (#2708)
* [WOW64] Fixed more compilation warnings * [WOW64] Messing around with file APIs * [WOW64] Added rcfile support * [DOCS] Update rcfile usage on WowBox64
Diffstat (limited to 'src/tools/env.c')
| -rw-r--r-- | src/tools/env.c | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/src/tools/env.c b/src/tools/env.c index 5e0a1de4..ed1bf326 100644 --- a/src/tools/env.c +++ b/src/tools/env.c @@ -2,12 +2,9 @@ #include <stdio.h> #include <stdlib.h> #include <stddef.h> -#ifndef _WIN32 -#include <sys/mman.h> -#include <sys/stat.h> -#endif #include <fcntl.h> #include <string.h> +#include <inttypes.h> #include "os.h" #include "env.h" @@ -73,6 +70,27 @@ static const char default_rcfile[] = "BOX64_EXIT=1\n" ; +#ifdef _WIN32 +#define PATHSEP "\\" +#define HOME "USERPROFILE" +#else +#define PATHSEP "/" +#define HOME "HOME" +#endif + +char* LowerCase(const char* s) +{ + if (!s) + return NULL; + char* ret = box_calloc(1, strlen(s) + 1); + size_t i = 0; + while (*s) { + ret[i++] = (*s >= 'A' && *s <= 'Z') ? (*s - 'A' + 'a') : (*s); + ++s; + } + + return ret; +} static void addNewEnvVar(const char* s) { @@ -96,10 +114,10 @@ static void parseRange(const char* s, uintptr_t* start, uintptr_t* end) { if (!s) return; if (!strchr(s, '-')) return; - if (sscanf(s, "%ld-%ld", start, end) == 2) return; - if (sscanf(s, "0x%lX-0x%lX", start, end) == 2) return; - if (sscanf(s, "0x%lx-0x%lx", start, end) == 2) return; - sscanf(s, "%lx-%lx", start, end); + if (sscanf(s, "%" PRId64 "-%" PRId64, start, end) == 2) return; + if (sscanf(s, "0x%" PRIX64 "-0x%" PRIX64, start, end) == 2) return; + if (sscanf(s, "0x%" PRIx64 "-0x%" PRIx64, start, end) == 2) return; + sscanf(s, "%" PRIx64 "-%" PRIx64, start, end); } void AddNewLibs(const char* list); @@ -273,12 +291,12 @@ static void initializeEnvFile(const char* filename) { if (box64env.noenvfiles) return; + BOXFILE* f = NULL; + if (filename) + f = box_fopen(filename, "r"); #ifndef _WIN32 - FILE* f = NULL; - if (filename) - f = fopen(filename, "r"); else { - #define TMP_MEMRCFILE "/box64_rcfile" +#define TMP_MEMRCFILE "/box64_rcfile" int tmp = shm_open(TMP_MEMRCFILE, O_RDWR | O_CREAT, S_IRWXU); if(tmp<0) return; // error, bye bye shm_unlink(TMP_MEMRCFILE); // remove the shm file, but it will still exist because it's currently in use @@ -287,10 +305,9 @@ static void initializeEnvFile(const char* filename) lseek(tmp, 0, SEEK_SET); f = fdopen(tmp, "r"); } - if (!f) { - printf("Error: Cannot open env file %s\n", filename); - return; - } +#endif + + if (!f) return; if (!box64env_entries) box64env_entries = kh_init(box64env_entry); @@ -299,10 +316,10 @@ static void initializeEnvFile(const char* filename) box64env_t current_env = { 0 }; size_t linesize = 0, len = 0; - char *line = NULL, *current_name = NULL; - int ret; + char* current_name = NULL; bool is_wildcard_name = false; - while ((ret = getline(&line, &linesize, f)) != -1) { + char line[1024]; + while ((box_fgets(line, 1024, f)) != NULL) { // remove comments char* p = strchr(line, '#'); if (p) *p = '\0'; @@ -324,8 +341,14 @@ static void initializeEnvFile(const char* filename) *strchr(key, '=') = '\0'; trimStringInplace(key); trimStringInplace(val); +#ifdef _WIN32 +#define VALID(a) a +#else +#define VALID(a) 1 +#endif + #define INTEGER(NAME, name, default, min, max, wine) \ - else if (!strcmp(key, #NAME)) \ + else if (!strcmp(key, #NAME) && VALID(wine)) \ { \ int v = strtol(val, &p, 0); \ if (p != val && v >= min && v <= max) { \ @@ -335,7 +358,7 @@ static void initializeEnvFile(const char* filename) } \ } #define INTEGER64(NAME, name, default, wine) \ - else if (!strcmp(key, #NAME)) \ + else if (!strcmp(key, #NAME) && VALID(wine)) \ { \ int64_t v = strtoll(val, &p, 0); \ if (p != val) { \ @@ -345,7 +368,7 @@ static void initializeEnvFile(const char* filename) } \ } #define BOOLEAN(NAME, name, default, wine) \ - else if (!strcmp(key, #NAME)) \ + else if (!strcmp(key, #NAME) && VALID(wine)) \ { \ if (strcmp(val, "0")) { \ current_env.is_##name##_overridden = 1; \ @@ -358,7 +381,7 @@ static void initializeEnvFile(const char* filename) } \ } #define ADDRESS(NAME, name, wine) \ - else if (!strcmp(key, #NAME)) \ + else if (!strcmp(key, #NAME) && VALID(wine)) \ { \ uintptr_t v = (uintptr_t)strtoll(val, &p, 0); \ if (p != val) { \ @@ -368,7 +391,7 @@ static void initializeEnvFile(const char* filename) } \ } #define STRING(NAME, name, wine) \ - else if (!strcmp(key, #NAME)) \ + else if (!strcmp(key, #NAME) && VALID(wine)) \ { \ current_env.is_##name##_overridden = 1; \ current_env.is_any_overridden = 1; \ @@ -387,6 +410,7 @@ static void initializeEnvFile(const char* filename) #undef BOOLEAN #undef ADDRESS #undef STRING +#undef VALID } } // push the last entry @@ -394,15 +418,13 @@ static void initializeEnvFile(const char* filename) pushNewEntry(current_name, ¤t_env, is_wildcard_name); box_free(current_name); } - box_free(line); - fclose(f); -#endif + box_fclose(f); } void InitializeEnvFiles() { -#ifndef _WIN32 +#ifndef _WIN32 // FIXME: this needs some consideration on Windows, so for now, only do it on Linux if (BOX64ENV(envfile) && FileExist(BOX64ENV(envfile), IS_FILE)) initializeEnvFile(BOX64ENV(envfile)); #ifndef TERMUX @@ -416,17 +438,17 @@ void InitializeEnvFiles() #endif else initializeEnvFile(NULL); // load default rcfile +#endif - char* p = getenv("HOME"); + char* p = GetEnv(HOME); if (p) { static char tmp[4096]; strncpy(tmp, p, 4095); - strncat(tmp, "/.box64rc", 4095); + strncat(tmp, PATHSEP ".box64rc", 4095); if (FileExist(tmp, IS_FILE)) { initializeEnvFile(tmp); } } -#endif } static char old_entryname[256] = ""; @@ -476,7 +498,6 @@ static void internalApplyEnvFileEntry(const char* entryname, const box64env_t* e void ApplyEnvFileEntry(const char* entryname) { -#ifndef _WIN32 if (!entryname || !box64env_entries) return; if (!strcasecmp(entryname, old_entryname)) return; @@ -499,7 +520,6 @@ void ApplyEnvFileEntry(const char* entryname) box64env_t* env = &kh_value(box64env_entries, k1); internalApplyEnvFileEntry(entryname, env); applyCustomRules(); -#endif } void LoadEnvVariables() |