diff --git a/client/fileutils.c b/client/fileutils.c index 8e0e3bb72..23553d15c 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -850,10 +850,10 @@ int searchAndList(const char *pm3dir, const char *ext) { filelist(script_directory_path, ext, false, true); } // try pm3 dirs in user .proxmark3 (user mode) - char *userpath = getenv("HOME"); - if (userpath != NULL) { - char script_directory_path[strlen(userpath) + strlen(PM3_USER_DIRECTORY) + strlen(pm3dir) + 1]; - strcpy(script_directory_path, userpath); + const char *user_path = get_my_user_directory(); + if (user_path != NULL) { + char script_directory_path[strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(pm3dir) + 1]; + strcpy(script_directory_path, user_path); strcat(script_directory_path, PM3_USER_DIRECTORY); strcat(script_directory_path, pm3dir); filelist(script_directory_path, ext, false, false); @@ -904,7 +904,7 @@ static int searchFinalFile(char **foundpath, const char *pm3dir, const char *sea } } // try pm3 dirs in user .proxmark3 (user mode) - char *user_path = getenv("HOME"); + const char *user_path = get_my_user_directory(); if (user_path != NULL) { char *path = calloc(strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(pm3dir) + strlen(filename) + 1, sizeof(char)); if (path == NULL) diff --git a/client/proxmark3.c b/client/proxmark3.c index aa37cda9a..b07884b91 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -315,6 +315,16 @@ static void set_my_executable_path(void) { } } +static char *my_user_directory = NULL; + +const char *get_my_user_directory(void) { + return my_user_directory; +} + +static void set_my_user_directory(void) { + my_user_directory = getenv("HOME"); +} + static void show_help(bool showFullHelp, char *exec_name) { PrintAndLogEx(NORMAL, "\nsyntax: %s [-h|-t|-m]", exec_name); @@ -486,6 +496,10 @@ int main(int argc, char *argv[]) { int flash_num_files = 0; char *flash_filenames[FLASH_MAX_FILES]; + // set global variables soon enough to get the log path + set_my_executable_path(); + set_my_user_directory(); + for (int i = 1; i < argc; i++) { if (argv[i][0] != '-') { @@ -685,9 +699,6 @@ int main(int argc, char *argv[]) { if (speed == 0) speed = USART_BAUD_RATE; - // set global variables - set_my_executable_path(); - if (flash_mode) { flash_pm3(port, flash_num_files, flash_filenames, flash_can_write_bl); exit(EXIT_SUCCESS); diff --git a/client/proxmark3.h b/client/proxmark3.h index c370efdff..9313e4c28 100644 --- a/client/proxmark3.h +++ b/client/proxmark3.h @@ -30,6 +30,7 @@ extern "C" { int push_cmdscriptfile(char *path, bool stayafter); const char *get_my_executable_path(void); const char *get_my_executable_directory(void); +const char *get_my_user_directory(void); void main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop); #ifdef __cplusplus diff --git a/client/scripting.c b/client/scripting.c index 724f72235..6e35f809f 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -1173,7 +1173,7 @@ int set_pm3_libraries(lua_State *L) { strcat(libraries_path, LUA_LIBRARIES_WILDCARD); setLuaPath(L, libraries_path); } - char *user_path = getenv("HOME"); + const char *user_path = get_my_user_directory(); if (user_path != NULL) { // from the $HOME/.proxmark3/luascripts/ directory char scripts_path[strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(LUA_SCRIPTS_SUBDIR) + strlen(LUA_LIBRARIES_WILDCARD) + 1]; diff --git a/client/ui.c b/client/ui.c index 883ea963a..d12713b9b 100644 --- a/client/ui.c +++ b/client/ui.c @@ -49,7 +49,7 @@ static void fPrintAndLog(FILE *stream, const char *fmt, ...); int searchHomeFilePath(char **foundpath, const char *filename, bool create_home) { if (foundpath == NULL) return PM3_EINVARG; - char *user_path = getenv("HOME"); + const char *user_path = get_my_user_directory(); if (user_path == NULL) { fprintf(stderr, "Could not retrieve $HOME from the environment\n"); return PM3_EFILE;