diff --git a/winpr/libwinpr/path/shell.c b/winpr/libwinpr/path/shell.c index dc075e7df..5078e1daf 100644 --- a/winpr/libwinpr/path/shell.c +++ b/winpr/libwinpr/path/shell.c @@ -36,6 +36,8 @@ #if defined(WIN32) #include +#else +#include #endif static char* GetPath_XDG_CONFIG_HOME(void); @@ -438,46 +440,35 @@ char* GetCombinedPath(const char* basePath, const char* subPath) BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes) { - size_t length; - const char delim = PathGetSeparatorA(0); - char* cur; - char* copy_org = _strdup(path); - char* copy = copy_org; +#ifdef _WIN32 + return (SHCreateDirectoryExA(NULL, path, lpAttributes) == ERROR_SUCCESS); +#else + const char delim = PathGetSeparatorA(PATH_STYLE_NATIVE); + char* dup; + char* p; - if (!copy_org) + /* we only operate on a non-null, absolute path */ + if (!path || *path != delim) return FALSE; - length = strlen(copy_org); + if (!(dup = _strdup(path))) + return FALSE; - /* Find first path element that exists. */ - while (copy) + for (p = dup; p;) { - if (!PathFileExistsA(copy)) - { - cur = strrchr(copy, delim); - if (cur) - *cur = '\0'; - } - else - break; - } + if ((p = strchr(p + 1, delim))) + *p = '\0'; - /* Create directories. */ - while(copy) - { - if (!PathFileExistsA(copy)) - { - if (!CreateDirectoryA(copy, NULL)) + if (mkdir(dup, 0777) != 0) + if (errno != EEXIST) break; - } - if (strlen(copy) < length) - copy[strlen(copy)] = delim; - else - break; + if (p) + *p = delim; } - free (copy_org); - return PathFileExistsA(path); + free(dup); + return (p == NULL); +#endif } #if !defined(_WIN32) || defined(_UWP) diff --git a/winpr/libwinpr/utils/test/TestWLog.c b/winpr/libwinpr/utils/test/TestWLog.c index 74e1c15a7..54b2ce4ae 100644 --- a/winpr/libwinpr/utils/test/TestWLog.c +++ b/winpr/libwinpr/utils/test/TestWLog.c @@ -2,6 +2,7 @@ #include #include #include +#include #include int TestWLog(int argc, char* argv[]) @@ -11,6 +12,14 @@ int TestWLog(int argc, char* argv[]) wLog* logB; wLogLayout* layout; wLogAppender* appender; + char* tmp_path; + char* wlog_file; + + if (!(tmp_path = GetKnownPath(KNOWN_PATH_TEMP))) + { + fprintf(stderr, "Failed to get temporary directory!\n"); + return -1; + } WLog_Init(); @@ -21,7 +30,7 @@ int TestWLog(int argc, char* argv[]) appender = WLog_GetLogAppender(root); if(!WLog_ConfigureAppender(appender, "outputfilename", "test_w.log")) return 1; - if(!WLog_ConfigureAppender(appender, "outputfilepath", "/tmp/")) + if(!WLog_ConfigureAppender(appender, "outputfilepath", tmp_path)) return 1; layout = WLog_GetLogLayout(root); @@ -49,5 +58,11 @@ int TestWLog(int argc, char* argv[]) WLog_Uninit(); + if ((wlog_file = GetCombinedPath(tmp_path, "test_w.log"))) + { + DeleteFileA(wlog_file); + free(wlog_file); + } + return 0; }