From 08a31f01db0c91ec520b0765c9aaa8f3a3844556 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 3 Sep 2026 14:59:37 +0200 Subject: [PATCH] =?UTF-8?q?client=20now=20supports=20tilde=20sign=20'~/?= =?UTF-8?q?=C2=B4=20=20in=20paths?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + client/src/fileutils.c | 111 ++++++++++++++++++++++++++++++++++++----- client/src/fileutils.h | 14 ++++++ 3 files changed, 113 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab58209a3..9ab912986 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed pathing - client now supports `~/` tilde sign in paths (@iceman1001) - Added `hf_legic_rdv4` - Standalone mode for managing 7 persistent legic dumps (@digitalhoax) - Fixed `lf t55xx detect` - dropped the psk3 candidates, which run the same demodulation as the psk2 one beside them and so can never match a psk3 tag, only fire on demodulation errors (@mfcarroll) - Fixed `lf t55xx detect` - a psk3 tag read back as psk2 with a block 0 one bit out, and a freshly wiped psk2 tag was reported as psk3 on no evidence; the two are now reported as ambiguous where the waveform cannot separate them, and block 0 is narrowed using the subcarrier, the broadcast period and whether password mode is in use (@mfcarroll) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index ebbdc555d..0763efc7a 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -161,6 +161,75 @@ bool path_is_regular_file(const char *path) { return S_ISREG(st.st_mode) != 0; } +/** + * @brief checks if path is an absolute path. + * @param path + * @return + */ +bool path_is_absolute(const char *path) { + if (path == NULL || path[0] == '\0') { + return false; + } + + if ((path[0] == '/') || (path[0] == '\\')) { + return true; + } + +#ifdef _WIN32 + // drive letter, "C:/..." or "C:\..." + if (isalpha((unsigned char)path[0]) && (path[1] == ':') && ((path[2] == '/') || (path[2] == '\\'))) { + return true; + } +#endif + return false; +} + +/** + * @brief expands a leading "~" into the user home directory. + * + * The pm3 prompt is not a shell, so nobody expands "~" for us and it wouldotherwise be taken as a directory named "~". + * + * "~user/..." is not supported and is returned unchanged. + * + * @param path + * @return newly allocated string, caller must free. NULL on failure + */ +char *path_expand_homedir(const char *path) { + if (path == NULL) { + return NULL; + } + + // only a leading "~", "~/" or "~\" is expanded + if ((path[0] != '~') || ((path[1] != '\0') && (path[1] != '/') && (path[1] != '\\'))) { + return str_dup(path); + } + + const char *user_path = get_my_user_directory(); + if (user_path == NULL) { + return str_dup(path); + } + + // skip the "~" and the path separators directly after it + const char *tail = path + 1; + while ((*tail == '/') || (*tail == '\\')) { + tail++; + } + + bool sep = ((*tail != '\0') && + (str_endswith(user_path, "/") == false) && + (str_endswith(user_path, "\\") == false)); + + size_t n = strlen(user_path) + (sep ? strlen(PATHSEP) : 0) + strlen(tail) + 1; + char *res = (char *)calloc(n, sizeof(uint8_t)); + if (res == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + return NULL; + } + + snprintf(res, n, "%s%s%s", user_path, sep ? PATHSEP : "", tail); + return res; +} + const char *path_basename(const char *path) { if (path == NULL) { return ""; @@ -324,13 +393,21 @@ static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; if (suffix == NULL) return NULL; - char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t)); - if (fileName == NULL) { - PrintAndLogEx(WARNING, "Failed to allocate memory"); + char *expanded = path_expand_homedir(preferredName); + if (expanded == NULL) { return NULL; } - strcpy(fileName, preferredName); + char *fileName = (char *) calloc(strlen(expanded) + strlen(suffix) + 1, sizeof(uint8_t)); + if (fileName == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + free(expanded); + return NULL; + } + + strcpy(fileName, expanded); + free(expanded); + if (str_endswith(fileName, suffix)) { return fileName; } @@ -355,6 +432,11 @@ char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePath return NULL; } + char *expanded = path_expand_homedir(preferredName); + if (expanded == NULL) { + return NULL; + } + // 1: null terminator // 16: room for filenum to ensure new filename // save_path_len + strlen(PATHSEP): the user preference save paths @@ -363,13 +445,14 @@ char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePath char *fileName = (char *) calloc(len, sizeof(uint8_t)); if (fileName == NULL) { + free(expanded); return NULL; } char *pfn = fileName; // if given path is not an absolute path - if ((preferredName[0] != '/') && (preferredName[0] != '\\')) { + if (path_is_absolute(expanded) == false) { // user preference save paths size_t save_path_len = path_size(e_save_path); if (save_path_len && save_path_len < (FILE_PATH_SIZE - strlen(PATHSEP))) { @@ -380,8 +463,8 @@ char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePath } // remove file extension if exist in name - size_t p_namelen = strlen(preferredName); - if (str_endswith(preferredName, suffix)) { + size_t p_namelen = strlen(expanded); + if (str_endswith(expanded, suffix)) { p_namelen -= strlen(suffix); } @@ -389,7 +472,7 @@ char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePath len -= p_namelen; // modify filename - snprintf(pfn, len, "%.*s%s", (int)p_namelen, preferredName, suffix); + snprintf(pfn, len, "%.*s%s", (int)p_namelen, expanded, suffix); // "-001" len -= 4; @@ -398,10 +481,11 @@ char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePath // check complete path/filename if exists while (fileExists(fileName)) { // modify filename - snprintf(pfn, len, "%.*s-%03d%s", (int)p_namelen, preferredName, num, suffix); + snprintf(pfn, len, "%.*s-%03d%s", (int)p_namelen, expanded, num, suffix); num++; } + free(expanded); return fileName; } @@ -3736,10 +3820,6 @@ int searchFile(char **foundpath, const char *pm3dir, const char *searchname, con return PM3_EINVARG; } - if (path_is_directory(searchname)) { - return PM3_EINVARG; - } - char *filename = filenamemcopy(searchname, suffix); if (filename == NULL) { return PM3_EMALLOC; @@ -3750,6 +3830,11 @@ int searchFile(char **foundpath, const char *pm3dir, const char *searchname, con return PM3_EFILE; } + if (path_is_directory(filename)) { + free(filename); + return PM3_EINVARG; + } + int res = searchFinalFile(foundpath, pm3dir, filename, silent); if (res != PM3_SUCCESS) { if ((res == PM3_EFILE) && (!silent)) { diff --git a/client/src/fileutils.h b/client/src/fileutils.h index 6d4ab7e88..7323a66e2 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -130,6 +130,20 @@ bool path_is_directory(const char *path); */ bool path_is_regular_file(const char *path); +/** + * @brief Check whether a path is absolute. + */ +bool path_is_absolute(const char *path); + +/** + * @brief Expand a leading "~" into the user home directory. + * + * The pm3 prompt is not a shell, so "~" reaches the client verbatim. + * Returns a newly allocated string which the caller must free, NULL on failure. + * "~user/..." is not supported and is returned unchanged. + */ +char *path_expand_homedir(const char *path); + /** * @brief Return the final path component, or an empty string for NULL input. */