commit 2e94c46e2ce16cb6330461dc241094c435e046bb parent 68f318f5909faf15e010788ae846e74da338caf1 Author: Francesco Saccone <francesco@francescosaccone.com> Date: Mon, 31 Mar 2025 17:16:21 +0200 feat: add get_normalised_path function Signed-off-by: Francesco Saccone <francesco@francescosaccone.com> Diffstat:
M | file.c | | | 33 | +++++++++++++++++++++++++++++++++ |
M | file.h | | | 3 | +++ |
2 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/file.c b/file.c @@ -1,8 +1,41 @@ #include "file.h" +#include <stdlib.h> +#include <string.h> #include <unistd.h> int is_file_readable(char *path) { return (access(path, R_OK) == 0); } + +char * +get_normalised_path(char *path) { + char *normalised; + int i, + j; + + if (path == NULL) { + return path; + } + + normalised = malloc(strlen(path) + 1); + + if (normalised == NULL) { + return normalised; + } + + j = 0; + for (i = 0; path[i] != '\0'; i++) { + if (path[i] != '/' || + i == 0 || + path[i - 1] != '/') { + normalised[j] = path[i]; + j++; + } + } + + normalised[j] = '\0'; + + return normalised; +} diff --git a/file.h b/file.h @@ -4,4 +4,7 @@ int is_file_readable(char *path); +char * +get_normalised_path(char *path); + #endif