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 | |
| 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')
| -rw-r--r-- | src/dynarec/arm64/arm64_printer.c | 29 | ||||
| -rw-r--r-- | src/include/fileutils.h | 10 | ||||
| -rw-r--r-- | src/include/os.h | 27 | ||||
| -rw-r--r-- | src/os/os_linux.c | 22 | ||||
| -rw-r--r-- | src/os/os_wine.c | 22 | ||||
| -rw-r--r-- | src/tools/env.c | 86 | ||||
| -rw-r--r-- | src/tools/fileutils.c | 34 |
7 files changed, 139 insertions, 91 deletions
diff --git a/src/dynarec/arm64/arm64_printer.c b/src/dynarec/arm64/arm64_printer.c index 20294a32..101233ab 100644 --- a/src/dynarec/arm64/arm64_printer.c +++ b/src/dynarec/arm64/arm64_printer.c @@ -2,6 +2,7 @@ #include <stddef.h> #include <string.h> #include <stdio.h> +#include <inttypes.h> #include "arm64_printer.h" #include "debug.h" @@ -407,9 +408,9 @@ const char* arm64_print(uint32_t opcode, uintptr_t addr) if(sf) { uint64_t noti=~(((uint64_t)imm)<<(hw*16)); if(!hw) - snprintf(buff, sizeof(buff), "MOVN %s, 0x%x\t; 0x%lx", Xt[Rd], imm, noti); + snprintf(buff, sizeof(buff), "MOVN %s, 0x%x\t; 0x%"PRIx64, Xt[Rd], imm, noti); else - snprintf(buff, sizeof(buff), "MOVN %s, 0x%x LSL %d\t; 0x%lx", Xt[Rd], imm, 16*hw, noti); + snprintf(buff, sizeof(buff), "MOVN %s, 0x%x LSL %d\t; 0x%"PRIx64, Xt[Rd], imm, 16*hw, noti); } else { uint32_t noti=~(((uint32_t)imm)<<(hw*16)); if(!hw) @@ -569,11 +570,11 @@ const char* arm64_print(uint32_t opcode, uintptr_t addr) uint64_t i = DecodeBitMasks(a.N, imms, immr); if(!sf) i&=0xffffffff; if(sf==0 && a.N==1) - snprintf(buff, sizeof(buff), "invalid ANDS %s, %s, 0x%lx", Wt[Rd], Wt[Rn], i); + snprintf(buff, sizeof(buff), "invalid ANDS %s, %s, 0x%"PRIx64, Wt[Rd], Wt[Rn], i); else if(Rd==31) - snprintf(buff, sizeof(buff), "TST %s, 0x%lx", sf?Xt[Rn]:Wt[Rn], i); + snprintf(buff, sizeof(buff), "TST %s, 0x%"PRIx64, sf?Xt[Rn]:Wt[Rn], i); else - snprintf(buff, sizeof(buff), "ANDS %s, %s, 0x%lx", sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); + snprintf(buff, sizeof(buff), "ANDS %s, %s, 0x%"PRIx64, sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); return buff; } if(isMask(opcode, "f1101010hh0mmmmmiiiiiinnnnnddddd", &a)) { @@ -611,9 +612,9 @@ const char* arm64_print(uint32_t opcode, uintptr_t addr) uint64_t i = DecodeBitMasks(a.N, imms, immr); if(!sf) i&=0xffffffff; if(sf==0 && a.N==1) - snprintf(buff, sizeof(buff), "invalid ORR %s, %s, 0x%lx", Wt[Rd], Wt[Rn], i); + snprintf(buff, sizeof(buff), "invalid ORR %s, %s, 0x%"PRIx64, Wt[Rd], Wt[Rn], i); else - snprintf(buff, sizeof(buff), "ORR %s, %s, 0x%lx", sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); + snprintf(buff, sizeof(buff), "ORR %s, %s, 0x%"PRIx64, sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); return buff; } if(isMask(opcode, "f0101010hh1mmmmmiiiiiinnnnnddddd", &a)) { @@ -646,9 +647,9 @@ const char* arm64_print(uint32_t opcode, uintptr_t addr) uint64_t i = DecodeBitMasks(a.N, imms, immr); if(!sf) i&=0xffffffff; if(sf==0 && a.N==1) - snprintf(buff, sizeof(buff), "invalid EOR %s, %s, 0x%lx", Wt[Rd], Wt[Rn], i); + snprintf(buff, sizeof(buff), "invalid EOR %s, %s, 0x%"PRIx64, Wt[Rd], Wt[Rn], i); else - snprintf(buff, sizeof(buff), "EOR %s, %s, 0x%lx", sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); + snprintf(buff, sizeof(buff), "EOR %s, %s, 0x%"PRIx64, sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); return buff; } if(isMask(opcode, "f1001010hh0mmmmmiiiiiinnnnnddddd", &a)) { @@ -663,9 +664,9 @@ const char* arm64_print(uint32_t opcode, uintptr_t addr) uint64_t i = DecodeBitMasks(a.N, imms, immr); if(!sf) i&=0xffffffff; if(sf==0 && a.N==1) - snprintf(buff, sizeof(buff), "invalid AND %s, %s, 0x%lx", Wt[Rd], Wt[Rn], i); + snprintf(buff, sizeof(buff), "invalid AND %s, %s, 0x%"PRIx64, Wt[Rd], Wt[Rn], i); else - snprintf(buff, sizeof(buff), "AND %s, %s, 0x%lx", sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); + snprintf(buff, sizeof(buff), "AND %s, %s, 0x%"PRIx64, sf?Xt[Rd]:Wt[Rd], sf?Xt[Rn]:Wt[Rn], i); return buff; } if(isMask(opcode, "f0001010hh0mmmmmiiiiiinnnnnddddd", &a)) { @@ -1075,9 +1076,9 @@ const char* arm64_print(uint32_t opcode, uintptr_t addr) tmp64u |= 0xffLL<<(i*8); if(a.Q) - snprintf(buff, sizeof(buff), "MOVI V%d.2D, #0x%016lx", Rd, tmp64u); + snprintf(buff, sizeof(buff), "MOVI V%d.2D, #0x%016"PRIx64, Rd, tmp64u); else - snprintf(buff, sizeof(buff), "MOVI D%d, #0x%016lx", Rd, tmp64u); + snprintf(buff, sizeof(buff), "MOVI D%d, #0x%016"PRIx64, Rd, tmp64u); return buff; } @@ -1118,7 +1119,7 @@ const char* arm64_print(uint32_t opcode, uintptr_t addr) // ADR if(isMask(opcode, "0ss10000iiiiiiiiiiiiiiiiiiiddddd", &a)) { - snprintf(buff, sizeof(buff), "ADR, %s, %ld", Xt[Rd], signExtend((imm)<<2|(imms), 20)); + snprintf(buff, sizeof(buff), "ADR, %s, %"PRId64, Xt[Rd], signExtend((imm)<<2|(imms), 20)); return buff; } diff --git a/src/include/fileutils.h b/src/include/fileutils.h index 858c6324..855eb8ff 100644 --- a/src/include/fileutils.h +++ b/src/include/fileutils.h @@ -3,13 +3,6 @@ #include "pathcoll.h" -#define IS_EXECUTABLE (1<<0) -#define IS_FILE (1<<1) - - -// 0 : doesn't exist, 1: Does exist -int FileExist(const char* filename, int flags); - // find a file, using Path if needed, resolving symlinks char* ResolveFile(const char* filename, path_collection_t* paths); // find a file, using Path if needed, NOT resolving symlinks @@ -23,9 +16,6 @@ int FileIsShell(const char* filename); // return temp folder (will return /tmp if nothing is correct) const char* GetTmpDir(void); -// will lower case the string and return a copy. Nothing fancy here, just A..Z transformed to a..z, rest is untouched -char* LowerCase(const char* s); - #if defined(RPI) || defined(RK3399) || defined(RK3326) void sanitize_mojosetup_gtk_background(void); #endif diff --git a/src/include/os.h b/src/include/os.h index f17a9a37..dc3e2c33 100644 --- a/src/include/os.h +++ b/src/include/os.h @@ -8,6 +8,7 @@ #include <dlfcn.h> #include <sys/mman.h> #else +#include <windows.h> typedef __int64 ssize_t; #define dlsym(a, b) NULL @@ -101,4 +102,30 @@ void PrintfFtrace(int prefix, const char* fmt, ...); void* GetEnv(const char* name); +#define IS_EXECUTABLE (1 << 0) +#define IS_FILE (1 << 1) + +// 0 : doesn't exist, 1: does exist. +int FileExist(const char* filename, int flags); + +#ifdef _WIN32 +#define BOXFILE_BUFSIZE 4096 +typedef struct { + HANDLE hFile; + char buffer[BOXFILE_BUFSIZE]; + size_t buf_pos; + size_t buf_size; + int eof; +} BOXFILE; + +BOXFILE* box_fopen(const char* filename, const char* mode); +char* box_fgets(char* str, int num, BOXFILE* stream); +int box_fclose(BOXFILE* stream); +#else +#define BOXFILE FILE +#define box_fopen fopen +#define box_fgets fgets +#define box_fclose fclose +#endif + #endif //__OS_H_ diff --git a/src/os/os_linux.c b/src/os/os_linux.c index 570ce2ad..29d0a1d1 100644 --- a/src/os/os_linux.c +++ b/src/os/os_linux.c @@ -4,6 +4,7 @@ #include <unistd.h> #include <stdint.h> #include <sys/personality.h> +#include <sys/stat.h> #include <dlfcn.h> #include <string.h> #include <stdarg.h> @@ -207,4 +208,25 @@ void PrintfFtrace(int prefix, const char* fmt, ...) void* GetEnv(const char* name) { return getenv(name); +} + +int FileExist(const char* filename, int flags) +{ + struct stat sb; + if (stat(filename, &sb) == -1) + return 0; + if (flags == -1) + return 1; + // check type of file? should be executable, or folder + if (flags & IS_FILE) { + if (!S_ISREG(sb.st_mode)) + return 0; + } else if (!S_ISDIR(sb.st_mode)) + return 0; + + if (flags & IS_EXECUTABLE) { + if ((sb.st_mode & S_IXUSR) != S_IXUSR) + return 0; // nope + } + return 1; } \ No newline at end of file diff --git a/src/os/os_wine.c b/src/os/os_wine.c index 7b222706..19955274 100644 --- a/src/os/os_wine.c +++ b/src/os/os_wine.c @@ -225,4 +225,26 @@ void* GetEnv(const char* name) static char buf[1024] = { 0 }; int len = GetEnvironmentVariableA(name, buf, sizeof(buf)); return len ? buf : NULL; +} + +int FileExist(const char* filename, int flags) +{ + DWORD attrs = GetFileAttributesA(filename); + if (attrs == INVALID_FILE_ATTRIBUTES) return 0; + if (flags == -1) return 1; + + if (flags & IS_FILE) { + if ((attrs & FILE_ATTRIBUTE_DIRECTORY) || (attrs & FILE_ATTRIBUTE_DEVICE) || (attrs & FILE_ATTRIBUTE_REPARSE_POINT)) { + return 0; + } + } else { + if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) + return 0; + } + + if (flags & IS_EXECUTABLE) { + printf_log(LOG_NONE, "Warning: Executable check not implemented for Windows\n"); + } + + return 1; } \ No newline at end of file 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() diff --git a/src/tools/fileutils.c b/src/tools/fileutils.c index 11dc6905..cfadf3ce 100644 --- a/src/tools/fileutils.c +++ b/src/tools/fileutils.c @@ -21,27 +21,6 @@ static const char* bashsign= "#!/bin/bash"; static const char* shsign = "#!/bin/sh"; static const char* bashsign2="#!/usr/bin/env bash"; -int FileExist(const char* filename, int flags) -{ - struct stat sb; - if (stat(filename, &sb) == -1) - return 0; - if(flags==-1) - return 1; - // check type of file? should be executable, or folder - if(flags&IS_FILE) { - if(!S_ISREG(sb.st_mode)) - return 0; - } else if(!S_ISDIR(sb.st_mode)) - return 0; - - if(flags&IS_EXECUTABLE) { - if((sb.st_mode&S_IXUSR)!=S_IXUSR) - return 0; // nope - } - return 1; -} - static char* ResolvePathInner(const char* path, int resolve_symlink) { if (resolve_symlink) { return box_realpath(path, NULL); @@ -152,19 +131,6 @@ const char* GetTmpDir() { return "/tmp"; // meh... } -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; -} - #if defined(RPI) || defined(RK3399) || defined(RK3326) void sanitize_mojosetup_gtk_background() { |