diff --git a/source/build/src/compat.cpp b/source/build/src/compat.cpp index aecab026b..40023f1c1 100644 --- a/source/build/src/compat.cpp +++ b/source/build/src/compat.cpp @@ -72,17 +72,11 @@ void set_memerr_handler(void(*handlerfunc)(int32_t, const char *, const char *)) // // Stuff which must be a function // -#ifdef _WIN32 -typedef BOOL (WINAPI * aSHGetSpecialFolderPathAtype)(HWND, LPTSTR, int, BOOL); -#endif - char *Bgethomedir(void) { #ifdef _WIN32 - aSHGetSpecialFolderPathAtype aSHGetSpecialFolderPathA; - TCHAR appdata[MAX_PATH]; int32_t loaded = 0; - HMODULE hShell32 = GetModuleHandle("shell32.dll"); + auto hShell32 = GetModuleHandle("shell32.dll"); if (hShell32 == NULL) { @@ -93,14 +87,20 @@ char *Bgethomedir(void) if (hShell32 == NULL) return NULL; - aSHGetSpecialFolderPathA = (aSHGetSpecialFolderPathAtype)(void (*)(void))GetProcAddress(hShell32, "SHGetSpecialFolderPathA"); + using SHGSFPA_t = BOOL (WINAPI *)(HWND, LPTSTR, int, BOOL); + auto aSHGetSpecialFolderPathA = (SHGSFPA_t)(void (*)(void))GetProcAddress(hShell32, "SHGetSpecialFolderPathA"); + if (aSHGetSpecialFolderPathA != NULL) + { + TCHAR appdata[MAX_PATH]; + if (SUCCEEDED(aSHGetSpecialFolderPathA(NULL, appdata, CSIDL_APPDATA, FALSE))) { if (loaded) FreeLibrary(hShell32); return Xstrdup(appdata); } + } if (loaded) FreeLibrary(hShell32); @@ -141,11 +141,13 @@ char *Bgetappdir(void) #elif defined __FreeBSD__ // the sysctl should also work when /proc/ is not mounted (which seems to // be common on FreeBSD), so use it.. - char buf[PATH_MAX] = {0}; - int name[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; - size_t len = sizeof(buf)-1; - int ret = sysctl(name, sizeof(name)/sizeof(name[0]), buf, &len, NULL, 0); - if(ret == 0 && buf[0] != '\0') { + char buf[PATH_MAX] = {0}; + int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + size_t len = sizeof(buf) - 1; + int ret = sysctl(name, ARRAY_SIZE(name), buf, &len, NULL, 0); + + if (ret == 0 && buf[0] != '\0') + { // again, remove executable name with dirname() // on FreeBSD dirname() seems to use some internal buffer dir = Xstrdup(dirname(buf)); @@ -172,12 +174,8 @@ char *Bgetappdir(void) int32_t Bcorrectfilename(char *filename, int32_t removefn) { - char *fn; - char *tokarr[64], *first, *next = NULL, *token; - int32_t i, ntok = 0, leadslash = 0, trailslash = 0; - - fn = Xstrdup(filename); - if (!fn) return -1; + char *fn = Xstrdup(filename); + char *tokarr[64], *first, *next = NULL; for (first=fn; *first; first++) { @@ -185,13 +183,15 @@ int32_t Bcorrectfilename(char *filename, int32_t removefn) if (*first == '\\') *first = '/'; #endif } - leadslash = (*fn == '/'); - trailslash = (first>fn && first[-1] == '/'); + + int leadslash = (*fn == '/'); + int trailslash = (first>fn && first[-1] == '/'); + int ntok = 0; first = fn; do { - token = Bstrtoken(first, "/", &next, 1); + char *token = Bstrtoken(first, "/", &next, 1); first = NULL; if (!token) break; else if (token[0] == 0) continue; @@ -206,10 +206,10 @@ int32_t Bcorrectfilename(char *filename, int32_t removefn) first = filename; if (leadslash) *(first++) = '/'; - for (i=0; i0) *(first++) = '/'; - for (token=tokarr[i]; *token; token++) + for (char *token=tokarr[i]; *token; token++) *(first++) = *token; } if (trailslash) *(first++) = '/'; @@ -302,9 +302,6 @@ char *Bgetsystemdrives(void) } str = p = (char *)Xmalloc(1 + (3 * number)); - if (!str) - return NULL; - number = 0; for (mask = 1; mask < 0x8000000l; mask <<= 1, number++) { @@ -350,18 +347,9 @@ BDIR *Bopendir(const char *name) #ifdef _MSC_VER char *t, *tt; t = (char *)Xmalloc(Bstrlen(name) + 1 + 4); - if (!t) - return NULL; #endif dirr = (BDIR_real *)Xmalloc(sizeof(BDIR_real) + Bstrlen(name)); - if (!dirr) - { -#ifdef _MSC_VER - Bfree(t); -#endif - return NULL; - } #ifdef _MSC_VER Bstrcpy(t, name); @@ -431,18 +419,15 @@ struct Bdirent *Breaddir(BDIR *dir) dirr->info.mtime = 0; char *fn = (char *)Xmalloc(Bstrlen(dirr->name) + 1 + dirr->info.namlen + 1); - if (fn) + Bsprintf(fn, "%s/%s", dirr->name, dirr->info.name); + struct Bstat st; + if (!Bstat(fn, &st)) { - Bsprintf(fn, "%s/%s", dirr->name, dirr->info.name); - struct Bstat st; - if (!Bstat(fn, &st)) - { - dirr->info.mode = st.st_mode; - dirr->info.size = st.st_size; - dirr->info.mtime = st.st_mtime; - } - Bfree(fn); + dirr->info.mode = st.st_mode; + dirr->info.size = st.st_size; + dirr->info.mtime = st.st_mtime; } + Bfree(fn); return &dirr->info; }