diff --git a/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt b/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt index 1ed0445..ae1753a 100644 --- a/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt +++ b/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-joystick-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-joystick-util io-util util dl) diff --git a/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt b/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt index 33e50de..c612159 100644 --- a/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt +++ b/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-keyboard-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-keyboard-util io-util util dl) diff --git a/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt b/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt index 9dc3c1b..7782117 100644 --- a/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt +++ b/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-joystick-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-joystick-util io-util util dl) diff --git a/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt b/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt index b8a40c3..bc1d62c 100644 --- a/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt +++ b/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-keyboard-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-keyboard-util io-util util dl) diff --git a/src/main/util/proc.c b/src/main/util/proc.c index 31bd913..5d43445 100644 --- a/src/main/util/proc.c +++ b/src/main/util/proc.c @@ -210,6 +210,8 @@ bool util_proc_get_folder_path_shared_object( void *symbol, char *buffer, size_t size) { Dl_info info; + const char *separator; + size_t folder_len; if (!symbol || !buffer || size == 0) { return false; @@ -219,23 +221,20 @@ bool util_proc_get_folder_path_shared_object( return false; } - if (strlen(info.dli_fname) >= size) { + separator = strrchr(info.dli_fname, '/'); + + if (!separator) { return false; } - strcpy(buffer, info.dli_fname); + folder_len = separator == info.dli_fname ? 1 : separator - info.dli_fname; - // If shared object in the root folder, keep the single / - size_t pos = strlen(buffer) - 1; - while (pos > 0 && buffer[pos] != '/') { - buffer[pos] = '\0'; - pos--; + if (folder_len >= size) { + return false; } - // delete / - if (pos > 0) { - buffer[pos] = '\0'; - } + memcpy(buffer, info.dli_fname, folder_len); + buffer[folder_len] = '\0'; return true; } @@ -257,4 +256,4 @@ void util_proc_log_info() log_info( "========================================================================" "========================"); -} \ No newline at end of file +} diff --git a/src/main/util/proc.h b/src/main/util/proc.h index 297253a..91d2d64 100644 --- a/src/main/util/proc.h +++ b/src/main/util/proc.h @@ -50,7 +50,8 @@ bool util_proc_get_folder_path_executable_no_ld_linux( * @param symbol Pointer to any symbol in the target shared object. * @param buffer Buffer to read the path into. * @param size Size of the buffer. - * @return True on success, false on failure. + * @return True on success, false when the symbol cannot be resolved, the + * resolved name has no folder, or the buffer is too small. */ bool util_proc_get_folder_path_shared_object( void *symbol, char *buffer, size_t size);