From 67702b2d77d8474c2a7fe7f1816f4eb9a0a98af1 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 10 Oct 2024 01:09:17 -0400 Subject: Fix issue with non-ID chars in filenames --- stdlib/files.c | 13 +++++++++++++ stdlib/files.h | 2 ++ 2 files changed, 15 insertions(+) (limited to 'stdlib') diff --git a/stdlib/files.c b/stdlib/files.c index 4a4220e7..cf777689 100644 --- a/stdlib/files.c +++ b/stdlib/files.c @@ -66,6 +66,19 @@ public char *resolve_path(const char *path, const char *relative_to, const char } public char *file_base_name(const char *path) +{ + const char *slash = strrchr(path, '/'); + if (slash) path = slash + 1; + assert(!isdigit(*path)); + const char *end = strchrnul(path, '.'); + size_t len = (size_t)(end - path); + char *buf = GC_MALLOC_ATOMIC(len+1); + strncpy(buf, path, len); + buf[len] = '\0'; + return buf; +} + +public char *file_base_id(const char *path) { const char *slash = strrchr(path, '/'); if (slash) path = slash + 1; diff --git a/stdlib/files.h b/stdlib/files.h index f650f78e..68827c2a 100644 --- a/stdlib/files.h +++ b/stdlib/files.h @@ -18,6 +18,8 @@ typedef struct { char *resolve_path(const char *path, const char *relative_to, const char *system_path); __attribute__((pure, nonnull)) char *file_base_name(const char *path); +__attribute__((pure, nonnull)) +char *file_base_id(const char *path); __attribute__((nonnull)) file_t *load_file(const char *filename); __attribute__((nonnull, returns_nonnull)) -- cgit v1.2.3