From a072ddb3f3d4c814d69d4e3523cffa3ecd02b966 Mon Sep 17 00:00:00 2001 From: Marek Kraus Date: Sat, 7 Jan 2023 16:57:58 +0100 Subject: [PATCH] Move some stuff to new util file --- tools/blisp/CMakeLists.txt | 2 +- tools/blisp/src/cmd/write.c | 51 ++------------------------------- tools/blisp/src/util.c | 56 +++++++++++++++++++++++++++++++++++++ tools/blisp/src/util.h | 12 ++++++++ 4 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 tools/blisp/src/util.c create mode 100644 tools/blisp/src/util.h diff --git a/tools/blisp/CMakeLists.txt b/tools/blisp/CMakeLists.txt index f118db9..763fc61 100644 --- a/tools/blisp/CMakeLists.txt +++ b/tools/blisp/CMakeLists.txt @@ -1,6 +1,6 @@ add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/argtable3 ${CMAKE_CURRENT_BINARY_DIR}/argtable3) -add_executable(blisp src/main.c src/cmd/write.c) +add_executable(blisp src/main.c src/cmd/write.c src/util.c) target_include_directories(blisp PRIVATE "${CMAKE_SOURCE_DIR}/include" diff --git a/tools/blisp/src/cmd/write.c b/tools/blisp/src/cmd/write.c index 94653fe..fb1a6a0 100644 --- a/tools/blisp/src/cmd/write.c +++ b/tools/blisp/src/cmd/write.c @@ -1,5 +1,4 @@ // SPDX-License-Identifier: MIT -#include #include #include #include @@ -7,17 +6,10 @@ #include "../cmd.h" #include "argtable3.h" #include "blisp_struct.h" +#include "../util.h" #ifdef __linux__ #include -#include -#elif defined(_MSC_VER) -#include -typedef SSIZE_T ssize_t; -#include -#define PATH_MAX MAX_PATH -#elif defined(__APPLE__) -#include #endif #define REG_EXTENDED 1 @@ -30,45 +22,6 @@ static struct arg_lit* reset; static struct arg_end* end; static void* cmd_write_argtable[6]; -#ifdef __APPLE__ -// Ugh. This stuff is just so messy without C++17 or Qt... -// These are not thread safe, but it doesn't place the responsibility -// to free an allocated buffer on the caller.nn - -static void get_executable_path(char* buffer_out, uint32_t max_size) { - assert(max_size >= PATH_MAX); // n.b. 1024 on MacOS. 4K on most Linux. - - char raw_path_name[PATH_MAX]; // $HOME/../../var/tmp/x - char real_path_name[PATH_MAX]; // /var/tmp/x - uint32_t raw_path_size = sizeof(raw_path_name); - - if (!_NSGetExecutablePath(raw_path_name, &raw_path_size)) { - realpath(raw_path_name, real_path_name); - } - // *real_path_name is appropriately sized and null terminated. - strcpy(buffer_out, real_path_name); -} -#endif - -ssize_t get_binary_folder(char* buffer, uint32_t buffer_size) { -#ifdef __linux__ - if (readlink("/proc/self/exe", buffer, buffer_size) <= 0) { - return -1; - } - char* pos = strrchr(buffer, '/'); -#elif defined(__APPLE__) - get_executable_path(buffer, buffer_size); - char* pos = strrchr(buffer, '/'); -#else - if (GetModuleFileName(NULL, buffer, buffer_size) <= 0) { - return -1; - } - char* pos = strrchr(buffer, '\\'); -#endif - pos[0] = '\0'; - return pos - buffer; -} - void fill_up_boot_header(struct bfl_boot_header* boot_header) { memcpy(boot_header->magiccode, "BFNP", 4); @@ -278,7 +231,7 @@ void blisp_flash_firmware() { char exe_path[PATH_MAX]; char eflash_loader_path[PATH_MAX]; - if (get_binary_folder(exe_path, PATH_MAX) <= 0) { + if (util_get_binary_folder(exe_path, PATH_MAX) <= 0) { fprintf(stderr, "Failed to find executable path to search for the " "eflash loader\n"); diff --git a/tools/blisp/src/util.c b/tools/blisp/src/util.c new file mode 100644 index 0000000..4eb2157 --- /dev/null +++ b/tools/blisp/src/util.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: MIT +#include "util.h" +#include +#include +#include + +#ifdef __linux__ +#include +#elif defined(_MSC_VER) +#include +typedef SSIZE_T ssize_t; +#include +#define PATH_MAX MAX_PATH +#elif defined(__APPLE__) +#include +#include +#endif + +#ifdef __APPLE__ +// Ugh. This stuff is just so messy without C++17 or Qt... +// These are not thread safe, but it doesn't place the responsibility +// to free an allocated buffer on the caller.nn + +static void util_get_executable_path(char* buffer_out, uint32_t max_size) { + assert(max_size >= PATH_MAX); // n.b. 1024 on MacOS. 4K on most Linux. + + char raw_path_name[PATH_MAX]; // $HOME/../../var/tmp/x + char real_path_name[PATH_MAX]; // /var/tmp/x + uint32_t raw_path_size = sizeof(raw_path_name); + + if (!_NSGetExecutablePath(raw_path_name, &raw_path_size)) { + realpath(raw_path_name, real_path_name); + } + // *real_path_name is appropriately sized and null terminated. + strcpy(buffer_out, real_path_name); +} +#endif + +ssize_t util_get_binary_folder(char* buffer, uint32_t buffer_size) { +#ifdef __linux__ + if (readlink("/proc/self/exe", buffer, buffer_size) <= 0) { + return -1; + } + char* pos = strrchr(buffer, '/'); +#elif defined(__APPLE__) + util_get_executable_path(buffer, buffer_size); + char* pos = strrchr(buffer, '/'); +#else + if (GetModuleFileName(NULL, buffer, buffer_size) <= 0) { + return -1; + } + char* pos = strrchr(buffer, '\\'); +#endif + pos[0] = '\0'; + return pos - buffer; +} \ No newline at end of file diff --git a/tools/blisp/src/util.h b/tools/blisp/src/util.h new file mode 100644 index 0000000..348876a --- /dev/null +++ b/tools/blisp/src/util.h @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +#ifndef BLISP_UTIL_H +#define BLISP_UTIL_H + +#include +#include + + +ssize_t util_get_binary_folder(char* buffer, uint32_t buffer_size); + + +#endif \ No newline at end of file