diff --git a/client/src/preferences.c b/client/src/preferences.c index 4c8fefb77..89d9cc188 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -32,7 +32,7 @@ static int setCmdHelp(const char *Cmd); static char *prefGetFilename(void) { char *path; - if (searchHomeFilePath(&path, preferencesFilename, false) == PM3_SUCCESS) + if (searchHomeFilePath(&path, NULL, preferencesFilename, false) == PM3_SUCCESS) return path; else return strdup(preferencesFilename); diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 427619e75..a461d4c95 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -206,7 +206,7 @@ main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) { } char *my_history_path = NULL; - if (searchHomeFilePath(&my_history_path, PROXHISTORY, true) != PM3_SUCCESS) { + if (searchHomeFilePath(&my_history_path, NULL, PROXHISTORY, true) != PM3_SUCCESS) { PrintAndLogEx(ERR, "No history will be recorded"); my_history_path = NULL; } else { diff --git a/client/src/ui.c b/client/src/ui.c index 9414fbb34..798ed165a 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -54,7 +54,7 @@ pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER; static void fPrintAndLog(FILE *stream, const char *fmt, ...); // needed by flasher, so let's put it here instead of fileutils.c -int searchHomeFilePath(char **foundpath, const char *filename, bool create_home) { +int searchHomeFilePath(char **foundpath, const char *subdir, const char *filename, bool create_home) { if (foundpath == NULL) return PM3_EINVARG; const char *user_path = get_my_user_directory(); @@ -62,7 +62,8 @@ int searchHomeFilePath(char **foundpath, const char *filename, bool create_home) fprintf(stderr, "Could not retrieve $HOME from the environment\n"); return PM3_EFILE; } - char *path = calloc(strlen(user_path) + strlen(PM3_USER_DIRECTORY) + 1, sizeof(char)); + size_t pathlen = strlen(user_path) + strlen(PM3_USER_DIRECTORY) + 1; + char *path = calloc(pathlen, sizeof(char)); if (path == NULL) return PM3_EMALLOC; strcpy(path, user_path); @@ -96,11 +97,48 @@ int searchHomeFilePath(char **foundpath, const char *filename, bool create_home) return PM3_EFILE; } } + if (subdir != NULL) { + pathlen += strlen(subdir); + path = realloc(path, pathlen * sizeof(char)); + if (path == NULL) + return PM3_EMALLOC; + strcat(path, subdir); + +#ifdef _WIN32 + // Mingw _stat fails if path ends with /, so let's use a stripped path + if (path[strlen(path) - 1] == '/') { + path[strlen(path) - 1] = '\0'; + result = _stat(path, &st); + path[strlen(path)] = '/'; + } else { + result = _stat(path, &st); + } +#else + result = stat(path, &st); +#endif + if ((result != 0) && create_home) { + +#ifdef _WIN32 + if (_mkdir(path)) +#else + if (mkdir(path, 0700)) +#endif + { + fprintf(stderr, "Could not create user directory %s\n", path); + free(path); + return PM3_EFILE; + } + } + } + if (filename == NULL) { *foundpath = path; return PM3_SUCCESS; } - path = realloc(path, (strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(filename) + 1) * sizeof(char)); + pathlen += strlen(filename); + path = realloc(path, pathlen * sizeof(char)); + if (path == NULL) + return PM3_EMALLOC; strcat(path, filename); *foundpath = path; return PM3_SUCCESS; @@ -266,7 +304,7 @@ static void fPrintAndLog(FILE *stream, const char *fmt, ...) { time_t now = time(NULL); timenow = gmtime(&now); strftime(filename, sizeof(filename), PROXLOG, timenow); - if (searchHomeFilePath(&my_logfile_path, filename, true) != PM3_SUCCESS) { + if (searchHomeFilePath(&my_logfile_path, LOGS_SUBDIR, filename, true) != PM3_SUCCESS) { fprintf(stderr, "[-] Logging disabled!\n\n"); my_logfile_path = NULL; logging = 0; diff --git a/client/src/ui.h b/client/src/ui.h index 20b5a3619..60064c0aa 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -59,7 +59,7 @@ void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter); void memcpy_filter_rlmarkers(void *dest, const void *src, size_t n); void memcpy_filter_emoji(void *dest, const void *src, size_t n, emojiMode_t mode); -int searchHomeFilePath(char **foundpath, const char *filename, bool create_home); +int searchHomeFilePath(char **foundpath, const char *subdir, const char *filename, bool create_home); extern pthread_mutex_t print_lock; diff --git a/include/common.h b/include/common.h index a66ddabd5..4d6efcce2 100644 --- a/include/common.h +++ b/include/common.h @@ -30,6 +30,7 @@ #define LUA_SCRIPTS_SUBDIR "luascripts" PATHSEP #define RESOURCES_SUBDIR "resources" PATHSEP #define TRACES_SUBDIR "traces" PATHSEP +#define LOGS_SUBDIR "logs" PATHSEP #define FIRMWARES_SUBDIR "firmware" PATHSEP #define BOOTROM_SUBDIR "bootrom" PATHSEP "obj" PATHSEP #define FULLIMAGE_SUBDIR "armsrc" PATHSEP "obj" PATHSEP