From: Tulio A M Mendes Date: Thu, 12 Feb 2026 03:10:44 +0000 (-0300) Subject: feat: ulibc realpath() — resolves '.', '..', relative paths via getcwd X-Git-Url: https://projects.tadryanom.me/sitemap.xml?a=commitdiff_plain;h=9a462d7a19005520021f961fe76320268ae17860;p=AdrOS.git feat: ulibc realpath() — resolves '.', '..', relative paths via getcwd --- diff --git a/user/ulibc/include/stdlib.h b/user/ulibc/include/stdlib.h index 6794a99..96b9c97 100644 --- a/user/ulibc/include/stdlib.h +++ b/user/ulibc/include/stdlib.h @@ -9,6 +9,7 @@ void* calloc(size_t nmemb, size_t size); void* realloc(void* ptr, size_t size); int atoi(const char* s); +char* realpath(const char* path, char* resolved); void exit(int status) __attribute__((noreturn)); #ifndef NULL diff --git a/user/ulibc/src/stdlib.c b/user/ulibc/src/stdlib.c index 072ff02..de10e2a 100644 --- a/user/ulibc/src/stdlib.c +++ b/user/ulibc/src/stdlib.c @@ -70,6 +70,42 @@ int atoi(const char* s) { return neg ? -n : n; } +char* realpath(const char* path, char* resolved) { + if (!path || !resolved) return (void*)0; + + char tmp[256]; + int tpos = 0; + + if (path[0] != '/') { + if (getcwd(tmp, sizeof(tmp)) < 0) return (void*)0; + tpos = (int)strlen(tmp); + if (tpos > 0 && tmp[tpos - 1] != '/') tmp[tpos++] = '/'; + } + + while (*path) { + if (*path == '/') { path++; continue; } + if (path[0] == '.' && (path[1] == '/' || path[1] == '\0')) { + path += 1; continue; + } + if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || path[2] == '\0')) { + path += 2; + if (tpos > 1) { + tpos--; + while (tpos > 0 && tmp[tpos - 1] != '/') tpos--; + } + continue; + } + if (tpos > 0 && tmp[tpos - 1] != '/') tmp[tpos++] = '/'; + while (*path && *path != '/' && tpos < 254) tmp[tpos++] = *path++; + } + + if (tpos == 0) tmp[tpos++] = '/'; + tmp[tpos] = '\0'; + + memcpy(resolved, tmp, (size_t)(tpos + 1)); + return resolved; +} + void exit(int status) { _exit(status); }