aboutsummaryrefslogtreecommitdiff
path: root/stdlib/files.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-10-10 01:09:17 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-10-10 01:09:17 -0400
commit67702b2d77d8474c2a7fe7f1816f4eb9a0a98af1 (patch)
tree0294659e9ef662577c175af0c5989ab9a8f254c5 /stdlib/files.c
parenta2490f4a500fca4b84e5652d31fc621f9f20742b (diff)
Fix issue with non-ID chars in filenames
Diffstat (limited to 'stdlib/files.c')
-rw-r--r--stdlib/files.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/stdlib/files.c b/stdlib/files.c
index 4a4220e7..cf777689 100644
--- a/stdlib/files.c
+++ b/stdlib/files.c
@@ -75,6 +75,19 @@ public char *file_base_name(const char *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;
+ 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';
for (char *p = buf; *p; p++) {
if (!isalnum(*p))
*p = '_';