Clean up Bgethomedir() and a few of the other directory functions

git-svn-id: https://svn.eduke32.com/eduke32@7094 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-10-25 23:30:17 +00:00
parent 9987b1ffa0
commit b65120200a

View file

@ -72,17 +72,11 @@ void set_memerr_handler(void(*handlerfunc)(int32_t, const char *, const char *))
// //
// Stuff which must be a function // Stuff which must be a function
// //
#ifdef _WIN32
typedef BOOL (WINAPI * aSHGetSpecialFolderPathAtype)(HWND, LPTSTR, int, BOOL);
#endif
char *Bgethomedir(void) char *Bgethomedir(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
aSHGetSpecialFolderPathAtype aSHGetSpecialFolderPathA;
TCHAR appdata[MAX_PATH];
int32_t loaded = 0; int32_t loaded = 0;
HMODULE hShell32 = GetModuleHandle("shell32.dll"); auto hShell32 = GetModuleHandle("shell32.dll");
if (hShell32 == NULL) if (hShell32 == NULL)
{ {
@ -93,14 +87,20 @@ char *Bgethomedir(void)
if (hShell32 == NULL) if (hShell32 == NULL)
return 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) if (aSHGetSpecialFolderPathA != NULL)
{
TCHAR appdata[MAX_PATH];
if (SUCCEEDED(aSHGetSpecialFolderPathA(NULL, appdata, CSIDL_APPDATA, FALSE))) if (SUCCEEDED(aSHGetSpecialFolderPathA(NULL, appdata, CSIDL_APPDATA, FALSE)))
{ {
if (loaded) if (loaded)
FreeLibrary(hShell32); FreeLibrary(hShell32);
return Xstrdup(appdata); return Xstrdup(appdata);
} }
}
if (loaded) if (loaded)
FreeLibrary(hShell32); FreeLibrary(hShell32);
@ -144,8 +144,10 @@ char *Bgetappdir(void)
char buf[PATH_MAX] = {0}; char buf[PATH_MAX] = {0};
int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
size_t len = sizeof(buf) - 1; size_t len = sizeof(buf) - 1;
int ret = sysctl(name, sizeof(name)/sizeof(name[0]), buf, &len, NULL, 0); int ret = sysctl(name, ARRAY_SIZE(name), buf, &len, NULL, 0);
if(ret == 0 && buf[0] != '\0') {
if (ret == 0 && buf[0] != '\0')
{
// again, remove executable name with dirname() // again, remove executable name with dirname()
// on FreeBSD dirname() seems to use some internal buffer // on FreeBSD dirname() seems to use some internal buffer
dir = Xstrdup(dirname(buf)); dir = Xstrdup(dirname(buf));
@ -172,12 +174,8 @@ char *Bgetappdir(void)
int32_t Bcorrectfilename(char *filename, int32_t removefn) int32_t Bcorrectfilename(char *filename, int32_t removefn)
{ {
char *fn; char *fn = Xstrdup(filename);
char *tokarr[64], *first, *next = NULL, *token; char *tokarr[64], *first, *next = NULL;
int32_t i, ntok = 0, leadslash = 0, trailslash = 0;
fn = Xstrdup(filename);
if (!fn) return -1;
for (first=fn; *first; first++) for (first=fn; *first; first++)
{ {
@ -185,13 +183,15 @@ int32_t Bcorrectfilename(char *filename, int32_t removefn)
if (*first == '\\') *first = '/'; if (*first == '\\') *first = '/';
#endif #endif
} }
leadslash = (*fn == '/');
trailslash = (first>fn && first[-1] == '/'); int leadslash = (*fn == '/');
int trailslash = (first>fn && first[-1] == '/');
int ntok = 0;
first = fn; first = fn;
do do
{ {
token = Bstrtoken(first, "/", &next, 1); char *token = Bstrtoken(first, "/", &next, 1);
first = NULL; first = NULL;
if (!token) break; if (!token) break;
else if (token[0] == 0) continue; else if (token[0] == 0) continue;
@ -206,10 +206,10 @@ int32_t Bcorrectfilename(char *filename, int32_t removefn)
first = filename; first = filename;
if (leadslash) *(first++) = '/'; if (leadslash) *(first++) = '/';
for (i=0; i<ntok; i++) for (int i=0; i<ntok; i++)
{ {
if (i>0) *(first++) = '/'; if (i>0) *(first++) = '/';
for (token=tokarr[i]; *token; token++) for (char *token=tokarr[i]; *token; token++)
*(first++) = *token; *(first++) = *token;
} }
if (trailslash) *(first++) = '/'; if (trailslash) *(first++) = '/';
@ -302,9 +302,6 @@ char *Bgetsystemdrives(void)
} }
str = p = (char *)Xmalloc(1 + (3 * number)); str = p = (char *)Xmalloc(1 + (3 * number));
if (!str)
return NULL;
number = 0; number = 0;
for (mask = 1; mask < 0x8000000l; mask <<= 1, number++) for (mask = 1; mask < 0x8000000l; mask <<= 1, number++)
{ {
@ -350,18 +347,9 @@ BDIR *Bopendir(const char *name)
#ifdef _MSC_VER #ifdef _MSC_VER
char *t, *tt; char *t, *tt;
t = (char *)Xmalloc(Bstrlen(name) + 1 + 4); t = (char *)Xmalloc(Bstrlen(name) + 1 + 4);
if (!t)
return NULL;
#endif #endif
dirr = (BDIR_real *)Xmalloc(sizeof(BDIR_real) + Bstrlen(name)); dirr = (BDIR_real *)Xmalloc(sizeof(BDIR_real) + Bstrlen(name));
if (!dirr)
{
#ifdef _MSC_VER
Bfree(t);
#endif
return NULL;
}
#ifdef _MSC_VER #ifdef _MSC_VER
Bstrcpy(t, name); Bstrcpy(t, name);
@ -431,8 +419,6 @@ struct Bdirent *Breaddir(BDIR *dir)
dirr->info.mtime = 0; dirr->info.mtime = 0;
char *fn = (char *)Xmalloc(Bstrlen(dirr->name) + 1 + dirr->info.namlen + 1); char *fn = (char *)Xmalloc(Bstrlen(dirr->name) + 1 + dirr->info.namlen + 1);
if (fn)
{
Bsprintf(fn, "%s/%s", dirr->name, dirr->info.name); Bsprintf(fn, "%s/%s", dirr->name, dirr->info.name);
struct Bstat st; struct Bstat st;
if (!Bstat(fn, &st)) if (!Bstat(fn, &st))
@ -442,7 +428,6 @@ struct Bdirent *Breaddir(BDIR *dir)
dirr->info.mtime = st.st_mtime; dirr->info.mtime = st.st_mtime;
} }
Bfree(fn); Bfree(fn);
}
return &dirr->info; return &dirr->info;
} }