From df10544e84cc201d528340c3f952775b434cdfce Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 28 Mar 2023 19:47:47 +0200 Subject: [PATCH] libc: Add libgen.h --- libc/include/libgen.h | 23 +++++++++++ libluna/include/luna/CPath.h | 7 +++- libluna/src/CPath.cpp | 79 +++++++++++++++++++----------------- 3 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 libc/include/libgen.h diff --git a/libc/include/libgen.h b/libc/include/libgen.h new file mode 100644 index 00000000..4914ff38 --- /dev/null +++ b/libc/include/libgen.h @@ -0,0 +1,23 @@ +/* libgen.h: Path parsing functions. */ + +#ifndef _LIBGEN_H +#define _LIBGEN_H + +// These functions are implemented in libluna (CPath.h) + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Returns path's base name. Will modify path. */ + char* basename(char* path); + + /* Returns path's parent directory. Will modify path. */ + char* dirname(char* path); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libluna/include/luna/CPath.h b/libluna/include/luna/CPath.h index 5bbd38c0..c1fad9fe 100644 --- a/libluna/include/luna/CPath.h +++ b/libluna/include/luna/CPath.h @@ -1,4 +1,7 @@ #pragma once -char* basename(char*); -char* dirname(char*); +extern "C" +{ + char* basename(char*); + char* dirname(char*); +} diff --git a/libluna/src/CPath.cpp b/libluna/src/CPath.cpp index d956201b..0fe5e8af 100644 --- a/libluna/src/CPath.cpp +++ b/libluna/src/CPath.cpp @@ -3,45 +3,48 @@ static char dot[] = "."; -char* basename(char* path) +extern "C" { - // If path is NULL, or the string's length is 0, return . - if (!path) return dot; - usize len = strlen(path); - if (!len) return dot; + char* basename(char* path) + { + // If path is NULL, or the string's length is 0, return . + if (!path) return dot; + usize len = strlen(path); + if (!len) return dot; - // Strip trailing slashes. - char* it = path + len - 1; - while (*it == '/' && it != path) { it--; } - *(it + 1) = 0; - if (it == path) return path; + // Strip trailing slashes. + char* it = path + len - 1; + while (*it == '/' && it != path) { it--; } + *(it + 1) = 0; + if (it == path) return path; - // Return path from the first character if there are no more slashes, or from the first character after the last - // slash. - char* beg = strrchr(path, '/'); - if (!beg) return path; - return beg + 1; -} - -char* dirname(char* path) -{ - // If path is NULL, or the string's length is 0, return . - if (!path) return dot; - usize len = strlen(path); - if (!len) return dot; - - // Strip trailing slashes. - char* it = path + len - 1; - while (*it == '/' && it != path) { it--; } - *(char*)(it + 1) = 0; - if (it == path) return path; - - // Search for the last slash. If there is none, return . - // Otherwise, we end the string there and return. - char* end = strrchr(path, '/'); - if (!end) return dot; - if (end != path) *end = 0; - else - *(end + 1) = 0; - return path; + // Return path from the first character if there are no more slashes, or from the first character after the last + // slash. + char* beg = strrchr(path, '/'); + if (!beg) return path; + return beg + 1; + } + + char* dirname(char* path) + { + // If path is NULL, or the string's length is 0, return . + if (!path) return dot; + usize len = strlen(path); + if (!len) return dot; + + // Strip trailing slashes. + char* it = path + len - 1; + while (*it == '/' && it != path) { it--; } + *(char*)(it + 1) = 0; + if (it == path) return path; + + // Search for the last slash. If there is none, return . + // Otherwise, we end the string there and return. + char* end = strrchr(path, '/'); + if (!end) return dot; + if (end != path) *end = 0; + else + *(end + 1) = 0; + return path; + } }