From 08a5de1f0edea2531c5393ab945873d9447d6db6 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 30 Aug 2019 08:32:34 +0200 Subject: [PATCH] searchFile traces --- client/cmddata.c | 14 +++++++++++--- client/fileutils.c | 42 ++++++++++++++++++++++++++++++++++++++++-- include/common.h | 1 + 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/client/cmddata.c b/client/cmddata.c index 81060cd59..817a63930 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -23,6 +23,7 @@ #include "lfdemod.h" // for demod code #include "loclass/cipherutils.h" // for decimating samples in getsamples #include "cmdlfem4x.h" // askem410xdecode +#include "fileutils.h" // searchFile uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN]; size_t DemodBufferLen = 0; @@ -1646,12 +1647,19 @@ static int CmdLoad(const char *Cmd) { if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; memcpy(filename, Cmd, len); - FILE *f = fopen(filename, "r"); - if (!f) { - PrintAndLogEx(WARNING, "couldn't open '%s'", filename); + char *path; + if (searchFile(&path, TRACES_SUBDIR, filename, "") != PM3_SUCCESS) { return PM3_EFILE; } + FILE *f = fopen(path, "r"); + if (!f) { + PrintAndLogEx(WARNING, "couldn't open '%s'", path); + free(path); + return PM3_EFILE; + } + free(path); + GraphTraceLen = 0; char line[80]; while (fgets(line, sizeof(line), f)) { diff --git a/client/fileutils.c b/client/fileutils.c index bff8771ad..ae7531771 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -869,6 +869,9 @@ static int searchFinalFile(char **foundpath, const char *pm3dir, const char *sea char *filename = calloc(strlen(searchname) + 1, sizeof(char)); if (filename == NULL) return PM3_EMALLOC; strcpy(filename, searchname); + if (g_debugMode == 2) { + PrintAndLogEx(INFO, "Searching %s", filename); + } if (((strlen(filename) > 1) && (filename[0] == '/')) || ((strlen(filename) > 2) && (filename[0] == '.') && (filename[1] == '/'))) { if (fileExists(filename)) { @@ -887,15 +890,44 @@ static int searchFinalFile(char **foundpath, const char *pm3dir, const char *sea return PM3_SUCCESS; } } - // try pm3 dirs in current workdir (dev mode) + // try pm3 dirs in current client workdir (dev mode) const char *exec_path = get_my_executable_directory(); - if (exec_path != NULL) { + if ((exec_path != NULL) && + ((strcmp(DICTIONARIES_SUBDIR, pm3dir) == 0) || + (strcmp(LUA_LIBRARIES_SUBDIR, pm3dir) == 0) || + (strcmp(LUA_SCRIPTS_SUBDIR, pm3dir) == 0) || + (strcmp(RESOURCES_SUBDIR, pm3dir) == 0))) { char *path = calloc(strlen(exec_path) + strlen(pm3dir) + strlen(filename) + 1, sizeof(char)); if (path == NULL) goto out; strcpy(path, exec_path); strcat(path, pm3dir); strcat(path, filename); + if (g_debugMode == 2) { + PrintAndLogEx(INFO, "Searching %s", path); + } + if (fileExists(path)) { + free(filename); + *foundpath = path; + return PM3_SUCCESS; + } else { + free(path); + } + } + // try pm3 dirs in current repo workdir (dev mode) + if ((exec_path != NULL) && + ((strcmp(TRACES_SUBDIR, pm3dir) == 0))) { + char *above = "../"; + char *path = calloc(strlen(exec_path) + strlen(above) + strlen(pm3dir) + strlen(filename) + 1, sizeof(char)); + if (path == NULL) + goto out; + strcpy(path, exec_path); + strcat(path, above); + strcat(path, pm3dir); + strcat(path, filename); + if (g_debugMode == 2) { + PrintAndLogEx(INFO, "Searching %s", path); + } if (fileExists(path)) { free(filename); *foundpath = path; @@ -914,6 +946,9 @@ static int searchFinalFile(char **foundpath, const char *pm3dir, const char *sea strcat(path, PM3_USER_DIRECTORY); strcat(path, pm3dir); strcat(path, filename); + if (g_debugMode == 2) { + PrintAndLogEx(INFO, "Searching %s", path); + } if (fileExists(path)) { free(filename); *foundpath = path; @@ -930,6 +965,9 @@ static int searchFinalFile(char **foundpath, const char *pm3dir, const char *sea strcpy(path, PM3_SHARE_PATH); strcat(path, pm3dir); strcat(path, filename); + if (g_debugMode == 2) { + PrintAndLogEx(INFO, "Searching %s", path); + } if (fileExists(path)) { free(filename); *foundpath = path; diff --git a/include/common.h b/include/common.h index 830a99e79..c4c5d6370 100644 --- a/include/common.h +++ b/include/common.h @@ -31,6 +31,7 @@ #define LUA_LIBRARIES_SUBDIR "lualibs/" #define LUA_SCRIPTS_SUBDIR "luascripts/" #define RESOURCES_SUBDIR "resources/" +#define TRACES_SUBDIR "traces/" #define PACKED __attribute__((packed))