platform: get full path of the project

issue #2
This commit is contained in:
tylen 2024-09-25 19:41:00 +00:00 committed by Vasily Davydov
parent 93d0d9ed9d
commit 3c5fc76d1d
3 changed files with 63 additions and 2 deletions

View File

@ -4,6 +4,7 @@
#include "cmd.h" #include "cmd.h"
#include "tools/log.h" #include "tools/log.h"
#include "platform/operations.h"
static void __attribute__((constructor)) init_cdo(void) { static void __attribute__((constructor)) init_cdo(void) {
atexit(exit_cmd); atexit(exit_cmd);
@ -13,7 +14,7 @@ static void __attribute__((constructor)) init_cdo(void) {
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
struct ArgList* args = parse_args(argc, argv); struct ArgList* args = parse_args(argc, argv);
const char* project_path = extract_value_from_arg(args, ARG_PROJECT); const char* relative_project_path = extract_value_from_arg(args, ARG_PROJECT);
LOG_INFO("Project full path: %s", project_path); LOG_INFO("Project full path: %s", convert_relative_to_full_path(relative_project_path));
return 0; return 0;
} }

50
src/platform/operations.c Normal file
View File

@ -0,0 +1,50 @@
#include "operations.h"
#include "../tools/alloc_wrappers.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
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;
}

10
src/platform/operations.h Normal file
View File

@ -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__*/