aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/fs.c
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-31 00:28:00 +0900
committernsfisis <nsfisis@gmail.com>2025-08-31 12:04:19 +0900
commitda65f1e21a24f0a173ee5ae08f00db91cba5ffda (patch)
tree3ec802f40d7cd0bd9e162ea5b3e50b76f319a884 /src/fs.c
parenta5ee9d944edf665c814bbfeded2849f2bb980ed8 (diff)
downloadducc-da65f1e21a24f0a173ee5ae08f00db91cba5ffda.tar.gz
ducc-da65f1e21a24f0a173ee5ae08f00db91cba5ffda.tar.zst
ducc-da65f1e21a24f0a173ee5ae08f00db91cba5ffda.zip
feat: support -MMD flag
Diffstat (limited to 'src/fs.c')
-rw-r--r--src/fs.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/fs.c b/src/fs.c
new file mode 100644
index 0000000..a299ef9
--- /dev/null
+++ b/src/fs.c
@@ -0,0 +1,26 @@
+#include "fs.h"
+#include "std.h"
+
+// 'ext' must include '.'.
+char* replace_extension(const char* file_name, const char* ext) {
+ size_t len = strlen(file_name);
+ const char* last_slash = strrchr(file_name, '/');
+ const char* last_dot = strrchr(file_name, '.');
+
+ size_t ext_len = strlen(ext);
+ size_t base_len;
+ // !last_slash: foo.c
+ // last_slash < last_dot: ./bar/foo.c
+ if (last_dot && (!last_slash || last_slash < last_dot)) {
+ base_len = last_dot - file_name;
+ } else {
+ base_len = len;
+ }
+
+ char* result = calloc(base_len + ext_len + 1, sizeof(char));
+ memcpy(result, file_name, base_len);
+ memcpy(result + base_len, ext, ext_len);
+ result[base_len + ext_len] = '\0';
+
+ return result;
+}