2006-04-23 06:44:19 +00:00
|
|
|
/*
|
2019-02-20 21:30:11 +00:00
|
|
|
* Playing-field leveler for Build
|
2006-04-23 06:44:19 +00:00
|
|
|
*/
|
|
|
|
|
2017-06-14 06:59:50 +00:00
|
|
|
#define LIBDIVIDE_BODY
|
2014-11-24 06:30:47 +00:00
|
|
|
#include "compat.h"
|
2018-10-07 05:21:20 +00:00
|
|
|
#include "debugbreak.h"
|
2014-11-24 06:30:47 +00:00
|
|
|
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _WIN32
|
2017-02-25 08:15:01 +00:00
|
|
|
# define NEED_SHLOBJ_H
|
|
|
|
# include "windows_inc.h"
|
2014-12-08 04:31:57 +00:00
|
|
|
#elif __APPLE__
|
|
|
|
# include "osxbits.h"
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#ifndef USE_PHYSFS
|
2009-07-09 02:29:48 +00:00
|
|
|
#if defined(_MSC_VER)
|
2006-04-23 06:44:19 +00:00
|
|
|
# include <io.h>
|
|
|
|
#else
|
|
|
|
# include <dirent.h>
|
|
|
|
#endif
|
2019-03-01 08:51:50 +00:00
|
|
|
#endif
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2015-03-02 07:54:24 +00:00
|
|
|
#if defined __linux || defined EDUKE32_BSD
|
2014-12-08 04:31:57 +00:00
|
|
|
# include <libgen.h> // for dirname()
|
|
|
|
#endif
|
2016-06-06 22:13:05 +00:00
|
|
|
#if defined EDUKE32_BSD
|
2015-03-02 07:54:24 +00:00
|
|
|
# include <limits.h> // for PATH_MAX
|
2014-12-08 04:31:57 +00:00
|
|
|
# include <sys/sysctl.h> // for sysctl() to get path to executable
|
|
|
|
#endif
|
|
|
|
|
2010-01-16 20:17:33 +00:00
|
|
|
#include "baselayer.h"
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#include "vfs.h"
|
|
|
|
|
2014-05-30 00:02:16 +00:00
|
|
|
////////// PANICKING ALLOCATION FUNCTIONS //////////
|
|
|
|
|
|
|
|
static void (*g_MemErrHandler)(int32_t line, const char *file, const char *func);
|
|
|
|
|
|
|
|
#ifdef DEBUGGINGAIDS
|
|
|
|
static const char *g_MemErrFunc = "???";
|
|
|
|
static const char *g_MemErrFile = "???";
|
|
|
|
static int32_t g_MemErrLine;
|
|
|
|
|
|
|
|
void xalloc_set_location(int32_t line, const char *file, const char *func)
|
|
|
|
{
|
|
|
|
g_MemErrLine = line;
|
|
|
|
g_MemErrFile = file;
|
|
|
|
|
|
|
|
if (func)
|
|
|
|
g_MemErrFunc = func;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-25 23:30:22 +00:00
|
|
|
void *handle_memerr(void *p)
|
2014-05-30 00:02:16 +00:00
|
|
|
{
|
2018-10-25 23:30:22 +00:00
|
|
|
UNREFERENCED_PARAMETER(p);
|
2018-10-07 05:21:20 +00:00
|
|
|
debug_break();
|
|
|
|
|
2014-09-30 04:12:27 +00:00
|
|
|
if (g_MemErrHandler)
|
2014-05-30 00:02:16 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUGGINGAIDS
|
2014-09-30 04:12:27 +00:00
|
|
|
g_MemErrHandler(g_MemErrLine, g_MemErrFile, g_MemErrFunc);
|
2014-05-30 00:02:16 +00:00
|
|
|
#else
|
2014-09-30 04:12:27 +00:00
|
|
|
g_MemErrHandler(0, "???", "???");
|
2014-05-30 00:02:16 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-30 04:12:27 +00:00
|
|
|
Bexit(EXIT_FAILURE);
|
2018-10-25 23:30:22 +00:00
|
|
|
EDUKE32_UNREACHABLE_SECTION(return &handle_memerr);
|
2014-05-30 00:02:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 04:12:27 +00:00
|
|
|
void set_memerr_handler(void(*handlerfunc)(int32_t, const char *, const char *))
|
2014-05-30 00:02:16 +00:00
|
|
|
{
|
2014-09-30 04:12:27 +00:00
|
|
|
g_MemErrHandler = handlerfunc;
|
2014-05-30 00:02:16 +00:00
|
|
|
}
|
|
|
|
|
2006-04-23 06:44:19 +00:00
|
|
|
//
|
|
|
|
// Stuff which must be a function
|
|
|
|
//
|
|
|
|
char *Bgethomedir(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t loaded = 0;
|
2018-10-25 23:30:17 +00:00
|
|
|
auto hShell32 = GetModuleHandle("shell32.dll");
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2008-08-23 03:12:23 +00:00
|
|
|
if (hShell32 == NULL)
|
|
|
|
{
|
|
|
|
hShell32 = LoadLibrary("shell32.dll");
|
|
|
|
loaded = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hShell32 == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2018-10-25 23:30:17 +00:00
|
|
|
using SHGSFPA_t = BOOL (WINAPI *)(HWND, LPTSTR, int, BOOL);
|
|
|
|
auto aSHGetSpecialFolderPathA = (SHGSFPA_t)(void (*)(void))GetProcAddress(hShell32, "SHGetSpecialFolderPathA");
|
|
|
|
|
2008-03-07 21:59:10 +00:00
|
|
|
if (aSHGetSpecialFolderPathA != NULL)
|
2018-10-25 23:30:17 +00:00
|
|
|
{
|
|
|
|
TCHAR appdata[MAX_PATH];
|
|
|
|
|
2008-03-07 21:59:10 +00:00
|
|
|
if (SUCCEEDED(aSHGetSpecialFolderPathA(NULL, appdata, CSIDL_APPDATA, FALSE)))
|
2008-08-23 03:12:23 +00:00
|
|
|
{
|
|
|
|
if (loaded)
|
|
|
|
FreeLibrary(hShell32);
|
2018-10-25 23:28:56 +00:00
|
|
|
return Xstrdup(appdata);
|
2008-08-23 03:12:23 +00:00
|
|
|
}
|
2018-10-25 23:30:17 +00:00
|
|
|
}
|
2008-08-23 03:12:23 +00:00
|
|
|
|
|
|
|
if (loaded)
|
|
|
|
FreeLibrary(hShell32);
|
2006-04-24 19:04:22 +00:00
|
|
|
return NULL;
|
2015-02-14 07:26:10 +00:00
|
|
|
#elif defined EDUKE32_OSX
|
2014-12-08 04:31:57 +00:00
|
|
|
return osx_gethomedir();
|
2012-05-01 12:40:08 +00:00
|
|
|
#elif defined(GEKKO)
|
|
|
|
// return current drive's name
|
|
|
|
char *drv, cwd[BMAX_PATH] = {0};
|
2019-03-01 08:51:50 +00:00
|
|
|
buildvfs_getcwd(cwd, BMAX_PATH);
|
2012-05-01 12:40:08 +00:00
|
|
|
drv = strchr(cwd, ':');
|
|
|
|
if (drv)
|
|
|
|
drv[1] = '\0';
|
2018-10-25 23:28:56 +00:00
|
|
|
return Xstrdup(cwd);
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2006-04-24 19:04:22 +00:00
|
|
|
char *e = getenv("HOME");
|
|
|
|
if (!e) return NULL;
|
2018-10-25 23:28:56 +00:00
|
|
|
return Xstrdup(e);
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-08 04:31:57 +00:00
|
|
|
char *Bgetappdir(void)
|
|
|
|
{
|
|
|
|
char *dir = NULL;
|
2014-12-08 07:32:58 +00:00
|
|
|
|
2014-12-08 04:31:57 +00:00
|
|
|
#ifdef _WIN32
|
2018-08-09 16:07:09 +00:00
|
|
|
TCHAR appdir[MAX_PATH];
|
2014-12-08 07:32:58 +00:00
|
|
|
|
2018-08-09 16:07:09 +00:00
|
|
|
if (GetModuleFileName(NULL, appdir, MAX_PATH) > 0) {
|
|
|
|
// trim off the filename
|
|
|
|
char *slash = Bstrrchr(appdir, '\\');
|
|
|
|
if (slash) slash[0] = 0;
|
2018-10-25 23:28:56 +00:00
|
|
|
dir = Xstrdup(appdir);
|
2014-12-08 04:31:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:26:10 +00:00
|
|
|
#elif defined EDUKE32_OSX
|
2014-12-08 04:31:57 +00:00
|
|
|
dir = osx_getappdir();
|
2015-03-02 07:54:24 +00:00
|
|
|
#elif defined __FreeBSD__
|
|
|
|
// the sysctl should also work when /proc/ is not mounted (which seems to
|
|
|
|
// be common on FreeBSD), so use it..
|
2018-10-25 23:30:17 +00:00
|
|
|
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')
|
|
|
|
{
|
2015-03-02 07:54:24 +00:00
|
|
|
// again, remove executable name with dirname()
|
|
|
|
// on FreeBSD dirname() seems to use some internal buffer
|
2018-10-25 23:28:56 +00:00
|
|
|
dir = Xstrdup(dirname(buf));
|
2015-03-02 07:54:24 +00:00
|
|
|
}
|
|
|
|
#elif defined __linux || defined EDUKE32_BSD
|
2014-12-08 04:31:57 +00:00
|
|
|
char buf[PATH_MAX] = {0};
|
|
|
|
char buf2[PATH_MAX] = {0};
|
|
|
|
# ifdef __linux
|
2015-01-11 04:53:30 +00:00
|
|
|
Bsnprintf(buf, sizeof(buf), "/proc/%d/exe", getpid());
|
2014-12-08 04:31:57 +00:00
|
|
|
# else // the BSDs.. except for FreeBSD which has a sysctl
|
2015-01-11 04:53:30 +00:00
|
|
|
Bsnprintf(buf, sizeof(buf), "/proc/%d/file", getpid());
|
2014-12-08 04:31:57 +00:00
|
|
|
# endif
|
|
|
|
int len = readlink(buf, buf2, sizeof(buf2));
|
|
|
|
if (len != -1) {
|
|
|
|
// remove executable name with dirname(3)
|
|
|
|
// on Linux, dirname() will modify buf2 (cutting off executable name) and return it
|
|
|
|
// on FreeBSD it seems to use some internal buffer instead.. anyway, just strdup()
|
2018-10-25 23:28:56 +00:00
|
|
|
dir = Xstrdup(dirname(buf2));
|
2014-12-08 04:31:57 +00:00
|
|
|
}
|
2006-07-01 01:40:18 +00:00
|
|
|
#endif
|
2014-12-08 07:32:58 +00:00
|
|
|
|
2014-12-08 04:31:57 +00:00
|
|
|
return dir;
|
2006-07-01 01:40:18 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t Bcorrectfilename(char *filename, int32_t removefn)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2018-10-25 23:30:17 +00:00
|
|
|
char *fn = Xstrdup(filename);
|
|
|
|
char *tokarr[64], *first, *next = NULL;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
for (first=fn; *first; first++)
|
|
|
|
{
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _WIN32
|
2006-04-24 19:04:22 +00:00
|
|
|
if (*first == '\\') *first = '/';
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
2018-10-25 23:30:17 +00:00
|
|
|
|
|
|
|
int leadslash = (*fn == '/');
|
|
|
|
int trailslash = (first>fn && first[-1] == '/');
|
|
|
|
int ntok = 0;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
first = fn;
|
2007-12-12 17:42:14 +00:00
|
|
|
do
|
|
|
|
{
|
2018-10-25 23:30:17 +00:00
|
|
|
char *token = Bstrtoken(first, "/", &next, 1);
|
2006-04-24 19:04:22 +00:00
|
|
|
first = NULL;
|
|
|
|
if (!token) break;
|
|
|
|
else if (token[0] == 0) continue;
|
|
|
|
else if (token[0] == '.' && token[1] == 0) continue;
|
|
|
|
else if (token[0] == '.' && token[1] == '.' && token[2] == 0) ntok = max(0,ntok-1);
|
|
|
|
else tokarr[ntok++] = token;
|
2007-12-12 17:42:14 +00:00
|
|
|
}
|
|
|
|
while (1);
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (!trailslash && removefn) { ntok = max(0,ntok-1); trailslash = 1; }
|
2006-04-24 19:04:22 +00:00
|
|
|
if (ntok == 0 && trailslash && leadslash) trailslash = 0;
|
|
|
|
|
|
|
|
first = filename;
|
|
|
|
if (leadslash) *(first++) = '/';
|
2018-10-25 23:30:17 +00:00
|
|
|
for (int i=0; i<ntok; i++)
|
2007-12-12 17:42:14 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
if (i>0) *(first++) = '/';
|
2018-10-25 23:30:17 +00:00
|
|
|
for (char *token=tokarr[i]; *token; token++)
|
2006-04-24 19:04:22 +00:00
|
|
|
*(first++) = *token;
|
|
|
|
}
|
|
|
|
if (trailslash) *(first++) = '/';
|
|
|
|
*(first++) = 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(fn);
|
2006-04-24 19:04:22 +00:00
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#ifndef USE_PHYSFS
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t Bcanonicalisefilename(char *filename, int32_t removefn)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
char cwd[BMAX_PATH];
|
2006-04-24 19:04:22 +00:00
|
|
|
char *fnp = filename;
|
|
|
|
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _WIN32
|
2015-01-11 04:53:30 +00:00
|
|
|
int drv = 0;
|
|
|
|
|
|
|
|
if (filename[0] && filename[1] == ':')
|
2006-04-24 19:04:22 +00:00
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
// filename is prefixed with a drive
|
|
|
|
drv = toupper(filename[0]) - 'A' + 1;
|
|
|
|
fnp += 2;
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
2015-01-11 04:53:30 +00:00
|
|
|
|
|
|
|
if (!_getdcwd(drv, cwd, sizeof(cwd)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (char *p = cwd; *p; p++)
|
|
|
|
if (*p == '\\')
|
|
|
|
*p = '/';
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2019-03-01 08:51:50 +00:00
|
|
|
if (!buildvfs_getcwd(cwd, sizeof(cwd)))
|
2015-01-11 04:53:30 +00:00
|
|
|
return -1;
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
char *p = Bstrrchr(cwd, '/');
|
|
|
|
if (!p || p[1])
|
|
|
|
Bstrcat(cwd, "/");
|
|
|
|
|
|
|
|
char fn[BMAX_PATH];
|
|
|
|
Bstrcpy(fn, fnp);
|
|
|
|
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _WIN32
|
2015-01-11 04:53:30 +00:00
|
|
|
for (p = fn; *p; p++)
|
|
|
|
if (*p == '\\')
|
|
|
|
*p = '/';
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (fn[0] != '/')
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
// we are dealing with a path relative to the current directory
|
2015-01-11 04:53:30 +00:00
|
|
|
Bstrcpy(filename, cwd);
|
|
|
|
Bstrcat(filename, fn);
|
2007-12-12 17:42:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _WIN32
|
2006-04-24 19:04:22 +00:00
|
|
|
filename[0] = cwd[0];
|
|
|
|
filename[1] = ':';
|
|
|
|
filename[2] = 0;
|
2015-01-11 04:53:30 +00:00
|
|
|
Bstrcat(filename, fn);
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2015-01-11 04:53:30 +00:00
|
|
|
Bstrcpy(filename, fn);
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
|
|
|
fnp = filename;
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _WIN32
|
2015-01-11 04:53:30 +00:00
|
|
|
fnp += 2; // skip the drive
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2015-01-11 04:53:30 +00:00
|
|
|
UNREFERENCED_PARAMETER(removefn); // change the call below to use removefn instead of 1?
|
|
|
|
return Bcorrectfilename(fnp, 1);
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
2019-03-01 08:51:50 +00:00
|
|
|
#endif
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
char *Bgetsystemdrives(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2006-04-24 19:04:22 +00:00
|
|
|
char *str, *p;
|
|
|
|
DWORD drv, mask;
|
2015-01-11 04:53:30 +00:00
|
|
|
int32_t number = 0;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
drv = GetLogicalDrives();
|
2015-01-11 04:53:30 +00:00
|
|
|
if (drv == 0)
|
|
|
|
return NULL;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
for (mask = 1; mask < 0x8000000l; mask <<= 1)
|
2007-12-12 17:42:14 +00:00
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
if ((drv & mask) == 0)
|
|
|
|
continue;
|
2006-04-24 19:04:22 +00:00
|
|
|
number++;
|
|
|
|
}
|
|
|
|
|
2018-10-25 23:28:56 +00:00
|
|
|
str = p = (char *)Xmalloc(1 + (3 * number));
|
2006-04-24 19:04:22 +00:00
|
|
|
number = 0;
|
2015-01-11 04:53:30 +00:00
|
|
|
for (mask = 1; mask < 0x8000000l; mask <<= 1, number++)
|
2007-12-12 17:42:14 +00:00
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
if ((drv & mask) == 0)
|
|
|
|
continue;
|
2006-04-24 19:04:22 +00:00
|
|
|
*(p++) = 'A' + number;
|
|
|
|
*(p++) = ':';
|
|
|
|
*(p++) = 0;
|
|
|
|
}
|
|
|
|
*(p++) = 0;
|
|
|
|
|
|
|
|
return str;
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2006-04-24 19:04:22 +00:00
|
|
|
// Perhaps have Unix OS's put /, /home/user, and /mnt/* in the "drives" list?
|
|
|
|
return NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#ifndef USE_PHYSFS
|
2007-12-12 17:42:14 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _MSC_VER
|
2014-02-11 07:17:45 +00:00
|
|
|
intptr_t dir;
|
2006-04-24 19:04:22 +00:00
|
|
|
struct _finddata_t fid;
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2006-04-24 19:04:22 +00:00
|
|
|
DIR *dir;
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
struct Bdirent info;
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t status;
|
2006-04-24 19:04:22 +00:00
|
|
|
char name[1];
|
2006-04-23 06:44:19 +00:00
|
|
|
} BDIR_real;
|
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
BDIR *Bopendir(const char *name)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
BDIR_real *dirr;
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _MSC_VER
|
2015-01-11 04:53:30 +00:00
|
|
|
char *t, *tt;
|
2018-10-25 23:28:56 +00:00
|
|
|
t = (char *)Xmalloc(Bstrlen(name) + 1 + 4);
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-25 23:28:56 +00:00
|
|
|
dirr = (BDIR_real *)Xmalloc(sizeof(BDIR_real) + Bstrlen(name));
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2015-01-11 04:53:30 +00:00
|
|
|
Bstrcpy(t, name);
|
|
|
|
tt = t + Bstrlen(name) - 1;
|
|
|
|
while (*tt == ' ' && tt > t) tt--;
|
|
|
|
if (*tt != '/' && *tt != '\\')
|
|
|
|
*(++tt) = '/';
|
2006-04-24 19:04:22 +00:00
|
|
|
*(++tt) = '*';
|
|
|
|
*(++tt) = '.';
|
|
|
|
*(++tt) = '*';
|
|
|
|
*(++tt) = 0;
|
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
dirr->dir = _findfirst(t, &dirr->fid);
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(t);
|
2007-12-12 17:42:14 +00:00
|
|
|
if (dirr->dir == -1)
|
|
|
|
{
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(dirr);
|
2006-04-24 19:04:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2006-04-24 19:04:22 +00:00
|
|
|
dirr->dir = opendir(name);
|
2007-12-12 17:42:14 +00:00
|
|
|
if (dirr->dir == NULL)
|
|
|
|
{
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(dirr);
|
2006-04-24 19:04:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
dirr->status = 0;
|
2015-01-11 04:53:30 +00:00
|
|
|
Bstrcpy(dirr->name, name);
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
return (BDIR *)dirr;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
struct Bdirent *Breaddir(BDIR *dir)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2011-01-16 02:50:27 +00:00
|
|
|
BDIR_real *dirr = (BDIR_real *)dir;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2007-12-12 17:42:14 +00:00
|
|
|
if (dirr->status > 0)
|
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
if (_findnext(dirr->dir, &dirr->fid) != 0)
|
2007-12-12 17:42:14 +00:00
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
dirr->status = -1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-01-11 04:53:30 +00:00
|
|
|
dirr->info.namlen = Bstrlen(dirr->fid.name);
|
2006-04-24 19:04:22 +00:00
|
|
|
dirr->info.name = dirr->fid.name;
|
|
|
|
dirr->status++;
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2015-01-11 04:53:30 +00:00
|
|
|
struct dirent *de = readdir(dirr->dir);
|
2007-12-12 17:42:14 +00:00
|
|
|
if (de == NULL)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
dirr->status = -1;
|
|
|
|
return NULL;
|
2007-12-12 17:42:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
dirr->status++;
|
|
|
|
}
|
2015-01-11 04:53:30 +00:00
|
|
|
dirr->info.namlen = Bstrlen(de->d_name);
|
|
|
|
dirr->info.name = de->d_name;
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
dirr->info.mode = 0;
|
|
|
|
dirr->info.size = 0;
|
|
|
|
dirr->info.mtime = 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2018-10-25 23:28:56 +00:00
|
|
|
char *fn = (char *)Xmalloc(Bstrlen(dirr->name) + 1 + dirr->info.namlen + 1);
|
2018-10-25 23:30:17 +00:00
|
|
|
Bsprintf(fn, "%s/%s", dirr->name, dirr->info.name);
|
2019-03-01 08:51:50 +00:00
|
|
|
|
|
|
|
#ifdef USE_PHYSFS
|
|
|
|
PHYSFS_Stat st;
|
|
|
|
if (PHYSFS_stat(fn, &st))
|
|
|
|
{
|
|
|
|
// dirr->info.mode = TODO;
|
|
|
|
dirr->info.size = st.filesize;
|
|
|
|
dirr->info.mtime = st.modtime;
|
|
|
|
}
|
|
|
|
#else
|
2018-10-25 23:30:17 +00:00
|
|
|
struct Bstat st;
|
|
|
|
if (!Bstat(fn, &st))
|
2007-12-12 17:42:14 +00:00
|
|
|
{
|
2018-10-25 23:30:17 +00:00
|
|
|
dirr->info.mode = st.st_mode;
|
|
|
|
dirr->info.size = st.st_size;
|
|
|
|
dirr->info.mtime = st.st_mtime;
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
2019-03-01 08:51:50 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(fn);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
return &dirr->info;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t Bclosedir(BDIR *dir)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2011-01-16 02:50:27 +00:00
|
|
|
BDIR_real *dirr = (BDIR_real *)dir;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2006-04-23 06:44:19 +00:00
|
|
|
#ifdef _MSC_VER
|
2006-04-24 19:04:22 +00:00
|
|
|
_findclose(dirr->dir);
|
2006-04-23 06:44:19 +00:00
|
|
|
#else
|
2006-04-24 19:04:22 +00:00
|
|
|
closedir(dirr->dir);
|
2006-04-23 06:44:19 +00:00
|
|
|
#endif
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(dirr);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
return 0;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
2019-03-01 08:51:50 +00:00
|
|
|
#endif
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
char *Bstrtoken(char *s, const char *delim, char **ptrptr, int chop)
|
2006-04-23 06:44:19 +00:00
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
if (!ptrptr)
|
|
|
|
return NULL;
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
char *p = s ? s : *ptrptr;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
while (*p != 0 && Bstrchr(delim, *p)) p++;
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
if (*p == 0)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
*ptrptr = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-11 04:53:30 +00:00
|
|
|
|
|
|
|
char * const start = p;
|
|
|
|
|
|
|
|
while (*p != 0 && !Bstrchr(delim, *p)) p++;
|
|
|
|
|
|
|
|
if (*p == 0)
|
|
|
|
*ptrptr = NULL;
|
2007-12-12 17:42:14 +00:00
|
|
|
else
|
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
if (chop)
|
|
|
|
*(p++) = 0;
|
2006-04-24 19:04:22 +00:00
|
|
|
*ptrptr = p;
|
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
return start;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 04:48:42 +00:00
|
|
|
char *Bstrtolower(char *str)
|
|
|
|
{
|
2015-01-11 04:53:30 +00:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
2012-03-12 04:48:42 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
int len = Bstrlen(str);
|
2012-03-12 04:48:42 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
if (len <= 0)
|
|
|
|
return str;
|
2012-03-12 04:48:42 +00:00
|
|
|
|
2015-01-11 04:53:30 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*(str + i) = Btolower(*(str + i));
|
|
|
|
i++;
|
|
|
|
} while (--len);
|
2012-03-12 04:48:42 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
//Brute-force case-insensitive, slash-insensitive, * and ? wildcard matcher
|
|
|
|
//Returns: 1:matches, 0:doesn't match
|
2014-10-25 03:29:21 +00:00
|
|
|
#ifndef WITHKPLIB
|
|
|
|
extern char toupperlookup[256];
|
2006-04-24 19:04:22 +00:00
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
static int32_t wildmatch(const char *match, const char *wild)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
do
|
|
|
|
{
|
2014-10-25 03:29:21 +00:00
|
|
|
if (*match && (toupperlookup[*wild] == toupperlookup[*match] || *wild == '?'))
|
2006-04-24 19:04:22 +00:00
|
|
|
{
|
2014-10-25 03:29:21 +00:00
|
|
|
wild++, match++;
|
2006-04-24 19:04:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-10-25 03:29:21 +00:00
|
|
|
else if ((*match|*wild) == '\0')
|
|
|
|
return 1;
|
|
|
|
else if (*wild == '*')
|
|
|
|
{
|
2015-01-15 06:45:27 +00:00
|
|
|
do { wild++; } while (*wild == '*');
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (*wild == '\0')
|
|
|
|
return 1;
|
|
|
|
while (*match && toupperlookup[*match] != toupperlookup[*wild]) match++;
|
|
|
|
if (*match && *(match+1) && toupperlookup[*(match+1)] != toupperlookup[*(wild+1)])
|
|
|
|
{
|
|
|
|
match++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (1);
|
2014-10-25 03:29:21 +00:00
|
|
|
if (toupperlookup[*match] == toupperlookup[*wild])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return 0;
|
2015-01-15 06:45:27 +00:00
|
|
|
}
|
|
|
|
while (1);
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
2014-10-25 03:29:21 +00:00
|
|
|
#endif
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
char *Bstrlwr(char *s)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!s) return s;
|
2015-01-11 04:53:30 +00:00
|
|
|
char *t = s;
|
2007-12-12 17:42:14 +00:00
|
|
|
while (*t) { *t = Btolower(*t); t++; }
|
2006-04-24 19:04:22 +00:00
|
|
|
return s;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *Bstrupr(char *s)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
if (!s) return s;
|
2015-01-11 04:53:30 +00:00
|
|
|
char *t = s;
|
2007-12-12 17:42:14 +00:00
|
|
|
while (*t) { *t = Btoupper(*t); t++; }
|
2006-04-24 19:04:22 +00:00
|
|
|
return s;
|
2006-04-23 06:44:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-02 05:55:32 +00:00
|
|
|
#define BMAXPAGESIZE 8192
|
|
|
|
|
|
|
|
int Bgetpagesize(void)
|
|
|
|
{
|
|
|
|
static int pageSize = -1;
|
|
|
|
|
|
|
|
if (pageSize == -1)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
pageSize = system_info.dwPageSize;
|
|
|
|
#else
|
|
|
|
pageSize = sysconf(_SC_PAGESIZE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return (unsigned)pageSize < BMAXPAGESIZE ? pageSize : BMAXPAGESIZE;
|
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
|
|
|
//
|
2010-01-16 20:17:33 +00:00
|
|
|
// Bgetsysmemsize() -- gets the amount of system memory in the machine
|
2006-04-23 06:44:19 +00:00
|
|
|
//
|
2012-11-05 02:49:08 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
typedef BOOL (WINAPI *aGlobalMemoryStatusExType)(LPMEMORYSTATUSEX);
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
uint32_t Bgetsysmemsize(void)
|
|
|
|
{
|
2012-09-02 14:06:30 +00:00
|
|
|
uint32_t siz = UINT32_MAX;
|
2019-02-18 22:02:50 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2011-01-16 02:50:27 +00:00
|
|
|
HMODULE lib = LoadLibrary("KERNEL32.DLL");
|
|
|
|
|
|
|
|
if (lib)
|
|
|
|
{
|
2012-11-05 02:49:08 +00:00
|
|
|
aGlobalMemoryStatusExType aGlobalMemoryStatusEx =
|
2018-08-09 16:07:09 +00:00
|
|
|
(aGlobalMemoryStatusExType)(void (*)(void))GetProcAddress(lib, "GlobalMemoryStatusEx");
|
2011-01-16 02:50:27 +00:00
|
|
|
|
|
|
|
if (aGlobalMemoryStatusEx)
|
|
|
|
{
|
|
|
|
//WinNT
|
|
|
|
MEMORYSTATUSEX memst;
|
|
|
|
memst.dwLength = sizeof(MEMORYSTATUSEX);
|
|
|
|
if (aGlobalMemoryStatusEx(&memst))
|
2019-01-30 00:13:00 +00:00
|
|
|
siz = min<decltype(memst.ullTotalPhys)>(UINT32_MAX, memst.ullTotalPhys);
|
2011-01-16 02:50:27 +00:00
|
|
|
}
|
2019-01-30 00:13:00 +00:00
|
|
|
|
2019-02-18 22:02:50 +00:00
|
|
|
if (!aGlobalMemoryStatusEx || siz == 0)
|
2011-01-16 02:50:27 +00:00
|
|
|
{
|
2012-03-12 04:48:42 +00:00
|
|
|
initprintf("Bgetsysmemsize(): error determining system memory size!\n");
|
2019-01-30 00:13:00 +00:00
|
|
|
siz = UINT32_MAX;
|
2011-01-16 02:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FreeLibrary(lib);
|
|
|
|
}
|
2019-01-30 00:13:00 +00:00
|
|
|
else initprintf("Bgetsysmemsize(): unable to load KERNEL32.DLL!\n");
|
2012-05-01 12:40:08 +00:00
|
|
|
#elif (defined(_SC_PAGE_SIZE) || defined(_SC_PAGESIZE)) && defined(_SC_PHYS_PAGES) && !defined(GEKKO)
|
2011-01-16 02:50:27 +00:00
|
|
|
#ifdef _SC_PAGE_SIZE
|
2018-10-25 23:31:49 +00:00
|
|
|
int64_t const scpagesiz = sysconf(_SC_PAGE_SIZE);
|
2011-01-16 02:50:27 +00:00
|
|
|
#else
|
2018-10-25 23:31:49 +00:00
|
|
|
int64_t const scpagesiz = sysconf(_SC_PAGESIZE);
|
2011-01-16 02:50:27 +00:00
|
|
|
#endif
|
2018-10-25 23:31:49 +00:00
|
|
|
int64_t const scphyspages = sysconf(_SC_PHYS_PAGES);
|
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
if (scpagesiz >= 0 && scphyspages >= 0)
|
2019-02-18 22:02:50 +00:00
|
|
|
siz = (uint32_t)min<uint64_t>(UINT32_MAX, scpagesiz * scphyspages);
|
2011-01-16 02:50:27 +00:00
|
|
|
|
|
|
|
//initprintf("Bgetsysmemsize(): %d pages of %d bytes, %d bytes of system memory\n",
|
|
|
|
// scphyspages, scpagesiz, siz);
|
|
|
|
|
|
|
|
#endif
|
2019-02-18 22:02:50 +00:00
|
|
|
|
|
|
|
return siz;
|
2011-01-16 02:50:27 +00:00
|
|
|
}
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2012-05-01 12:40:08 +00:00
|
|
|
#ifdef GEKKO
|
|
|
|
int access(const char *pathname, int mode)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(pathname, &st)==-1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// TODO: Check mode against st_mode
|
2013-02-18 08:50:08 +00:00
|
|
|
UNREFERENCED_PARAMETER(mode);
|
2006-04-23 06:44:19 +00:00
|
|
|
|
2012-05-01 12:40:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|