Let an asbolute path work with -logfile

This commit is contained in:
James R 2019-12-23 17:40:43 -08:00
parent 457e986b75
commit 5fbe77cdda
3 changed files with 56 additions and 21 deletions

View File

@ -2489,11 +2489,15 @@ boolean M_IsPathAbsolute(const char *path)
/** I_mkdir for each part of the path. /** I_mkdir for each part of the path.
*/ */
void M_MkdirEach(const char *cpath, int start, int mode) void M_MkdirEachUntil(const char *cpath, int start, int end, int mode)
{ {
char path[MAX_WADPATH]; char path[MAX_WADPATH];
char *p; char *p;
char *t; char *t;
if (end > 0 && end <= start)
return;
strlcpy(path, cpath, sizeof path); strlcpy(path, cpath, sizeof path);
#ifdef _WIN32 #ifdef _WIN32
if (strncmp(&path[1], ":\\", 2) == 0) if (strncmp(&path[1], ":\\", 2) == 0)
@ -2501,6 +2505,10 @@ void M_MkdirEach(const char *cpath, int start, int mode)
else else
#endif #endif
p = path; p = path;
if (end > 0)
end -= start;
for (; start > 0; --start) for (; start > 0; --start)
{ {
p += strspn(p, PATHSEP); p += strspn(p, PATHSEP);
@ -2510,6 +2518,9 @@ void M_MkdirEach(const char *cpath, int start, int mode)
p += strspn(p, PATHSEP); p += strspn(p, PATHSEP);
for (;;) for (;;)
{ {
if (end > 0 && !--end)
break;
t = p; t = p;
if (( p = strchr(p, PATHSEP[0]) )) if (( p = strchr(p, PATHSEP[0]) ))
{ {
@ -2526,3 +2537,8 @@ void M_MkdirEach(const char *cpath, int start, int mode)
} }
} }
} }
void M_MkdirEach(const char *path, int start, int mode)
{
M_MkdirEachUntil(path, start, -1, mode);
}

View File

@ -99,6 +99,7 @@ const char *M_FileError(FILE *handle);
int M_PathParts (const char *path); int M_PathParts (const char *path);
boolean M_IsPathAbsolute (const char *path); boolean M_IsPathAbsolute (const char *path);
void M_MkdirEach (const char *path, int start, int mode); void M_MkdirEach (const char *path, int start, int mode);
void M_MkdirEachUntil (const char *path, int start, int end, int mode);
// counting bits, for weapon ammo code, usually // counting bits, for weapon ammo code, usually
FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size);

View File

@ -137,6 +137,7 @@ int main(int argc, char **argv)
const char *format; const char *format;
const char *reldir; const char *reldir;
int left; int left;
boolean fileabs;
logdir = D_Home(); logdir = D_Home();
@ -144,38 +145,55 @@ int main(int argc, char **argv)
timeinfo = localtime(&my_time); timeinfo = localtime(&my_time);
if (M_CheckParm("-logfile") && M_IsNextParm()) if (M_CheckParm("-logfile") && M_IsNextParm())
{
format = M_GetNextParm(); format = M_GetNextParm();
fileabs = M_IsPathAbsolute(format);
}
else else
{
format = "log-%Y-%m-%d_%H-%M-%S.txt"; format = "log-%Y-%m-%d_%H-%M-%S.txt";
fileabs = false;
}
if (M_CheckParm("-logdir") && M_IsNextParm()) if (fileabs)
reldir = M_GetNextParm();
else
reldir = "logs";
if (M_IsPathAbsolute(reldir))
{ {
left = snprintf(logfile, sizeof logfile, strftime(logfile, sizeof logfile, format, timeinfo);
"%s"PATHSEP, reldir);
M_MkdirEachUntil(logfile,
M_PathParts(logdir) - 1,
M_PathParts(logfile) - 1, 0755);
} }
else else
{
if (M_CheckParm("-logdir") && M_IsNextParm())
reldir = M_GetNextParm();
else
reldir = "logs";
if (M_IsPathAbsolute(reldir))
{
left = snprintf(logfile, sizeof logfile,
"%s"PATHSEP, reldir);
}
else
#ifdef DEFAULTDIR #ifdef DEFAULTDIR
if (logdir) if (logdir)
{ {
left = snprintf(logfile, sizeof logfile, left = snprintf(logfile, sizeof logfile,
"%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir);
} }
else else
#endif/*DEFAULTDIR*/ #endif/*DEFAULTDIR*/
{ {
left = snprintf(logfile, sizeof logfile, left = snprintf(logfile, sizeof logfile,
"."PATHSEP"%s"PATHSEP, reldir); "."PATHSEP"%s"PATHSEP, reldir);
} }
#endif/*LOGMESSAGES*/ #endif/*LOGMESSAGES*/
M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755);
strftime(&logfile[left], sizeof logfile - left, format, timeinfo); strftime(&logfile[left], sizeof logfile - left, format, timeinfo);
}
logstream = fopen(logfile, "wt"); logstream = fopen(logfile, "wt");
} }