From 3c5fc76d1dbb1ecd897da136a2e9875da15ab7e4 Mon Sep 17 00:00:00 2001 From: tylen Date: Wed, 25 Sep 2024 19:41:00 +0000 Subject: [PATCH] platform: get full path of the project issue #2 --- src/cdo.c | 5 ++-- src/platform/operations.c | 50 +++++++++++++++++++++++++++++++++++++++ src/platform/operations.h | 10 ++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/platform/operations.c create mode 100644 src/platform/operations.h diff --git a/src/cdo.c b/src/cdo.c index c58ca9f..682d670 100644 --- a/src/cdo.c +++ b/src/cdo.c @@ -4,6 +4,7 @@ #include "cmd.h" #include "tools/log.h" +#include "platform/operations.h" static void __attribute__((constructor)) init_cdo(void) { atexit(exit_cmd); @@ -13,7 +14,7 @@ static void __attribute__((constructor)) init_cdo(void) { int main (int argc, char *argv[]) { struct ArgList* args = parse_args(argc, argv); - const char* project_path = extract_value_from_arg(args, ARG_PROJECT); - LOG_INFO("Project full path: %s", project_path); + const char* relative_project_path = extract_value_from_arg(args, ARG_PROJECT); + LOG_INFO("Project full path: %s", convert_relative_to_full_path(relative_project_path)); return 0; } diff --git a/src/platform/operations.c b/src/platform/operations.c new file mode 100644 index 0000000..b63731a --- /dev/null +++ b/src/platform/operations.c @@ -0,0 +1,50 @@ +#include "operations.h" +#include "../tools/alloc_wrappers.h" + +#include +#include +#include +#include + +const char* convert_relative_to_full_path(const char* relative_path) { + LOG_DEBUG("Entering convert_relative_to_full_path function"); + + char* cwd; + cwd = getcwd(NULL, 0); + if (cwd == NULL) { + LOG_ERROR_EXIT("Can't get current working directory. ERRNO: %d; %s", + errno, strerror(errno)); + } + LOG_DEBUG("Current working directory: %s", cwd); + + if (relative_path == NULL) { + LOG_WARNING("No relative_path provided, using current working directory."); + LOG_DEBUG("Returning current working directory as full path"); + return cwd; + } + + const size_t relative_path_len = strlen(relative_path); + const size_t cwd_len = strlen(cwd); + LOG_DEBUG("Relative path length: %lu, Current working directory length: %lu", relative_path_len, cwd_len); + + const size_t full_path_length = cwd_len + relative_path_len + 2; // +2 for '/' and '\0' + char* full_path = cdo_calloc(full_path_length, sizeof(char)); + const int written_bytes = snprintf(full_path, + full_path_length, + "%s/%s", + cwd, + relative_path); + if (written_bytes != full_path_length - 1) { // -1 because \0 is not counted + LOG_ERROR_EXIT("Full path is broken. Should have length = %lu, actual = %d", + full_path_length, written_bytes); + } + LOG_DEBUG("Returning full path: %s", full_path); + + free(cwd); + return full_path; +} + + +const char* get_platform_name_from_project(const char* project_path) { + return NULL; +} diff --git a/src/platform/operations.h b/src/platform/operations.h new file mode 100644 index 0000000..5a4d6c5 --- /dev/null +++ b/src/platform/operations.h @@ -0,0 +1,10 @@ +#ifndef __PLATFORM_OPERATIONS_H__ +#define __PLATFORM_OPERATIONS_H__ + +#define PATH_TO_GIT_CONFIG_RELATIVE_TO_PROJ ".git/config" + +const char* convert_relative_to_full_path(const char* relative_path); +const char* get_platform_name_from_project(const char* project_path); + + +#endif /*__PLATFORM_OPERATIONS_H__*/ \ No newline at end of file