From 05555a23a88581b303492061cc0d5c4f160332c7 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Thu, 27 Apr 2017 08:52:14 -0700 Subject: [PATCH] SDL: fix deflt bat saving, improve dir checking By default the SDL port tried to save the battery to a nonexistant directory in saveDir, because if (saveDir) {...} evaluted to true since saveDir was a non-NULL pointer but empty. Change sdlCheckDirectory to return a bool indicating if the directory is good or not, and if not set screenShotDir, saveDir and batteryDir to NULL so that code that checks for their existance works correctly. --- src/sdl/SDL.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/sdl/SDL.cpp b/src/sdl/SDL.cpp index ac328e8e..55902a16 100644 --- a/src/sdl/SDL.cpp +++ b/src/sdl/SDL.cpp @@ -283,10 +283,12 @@ void StopLirc(void) #define S_IFDIR _S_IFDIR #endif -void sdlCheckDirectory(const char* dir) +bool sdlCheckDirectory(const char* dir) { + bool res = false; + if (!dir || !dir[0]) { - return; + return false; } struct stat buf; @@ -295,16 +297,21 @@ void sdlCheckDirectory(const char* dir) char* p = (char*)dir + len - 1; - if (*p == '/' || *p == '\\') + while (p != dir && (*p == '/' || *p == '\\')) { *p = 0; + p--; + } if (stat(dir, &buf) == 0) { if (!(buf.st_mode & S_IFDIR)) { fprintf(stderr, "Error: %s is not a directory\n", dir); } + res = true; } else { fprintf(stderr, "Error: %s does not exist\n", dir); } + + return res; } char* sdlGetFilename(char* name) @@ -649,7 +656,7 @@ static char* sdlStateName(int num) { static char stateName[2048]; - if (saveDir && strlen(saveDir)) + if (saveDir) sprintf(stateName, "%s/%s%d.sgm", saveDir, sdlGetFilename(filename), num + 1); else if (homeDir) @@ -1716,9 +1723,12 @@ int main(int argc, char** argv) LoadConfig(); // Parse command line arguments (overrides ini) ReadOpts(argc, argv); - sdlCheckDirectory(screenShotDir); - sdlCheckDirectory(saveDir); - sdlCheckDirectory(batteryDir); + if (!sdlCheckDirectory(screenShotDir)) + screenShotDir = NULL; + if (!sdlCheckDirectory(saveDir)) + saveDir = NULL; + if (!sdlCheckDirectory(batteryDir)) + batteryDir = NULL; sdlSaveKeysSwitch = (ReadPrefHex("saveKeysSwitch")); sdlOpenglScale = (ReadPrefHex("openGLscale"));