add get_my_user_directory() to centralize HOME processing

This commit is contained in:
Philippe Teuwen 2019-09-12 20:13:01 +02:00
parent da68369e02
commit 46574ae0f5
5 changed files with 22 additions and 10 deletions

View file

@ -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)

View file

@ -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);

View file

@ -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

View file

@ -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];

View file

@ -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;