diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-17 15:09:23 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-17 15:09:23 +0530 |
commit | 257908233e8fd3a4deec3d0971f4cbcd90c626fd (patch) | |
tree | 40d511bdfe08393f521f601e5c6f4d0acf792a73 | |
parent | 71d481b4559e85b57ba92fe7b06b7c074f1cafdd (diff) |
(assets): ftw implmentation for recursive copy
-rw-r--r-- | compromyse.xyz/assets/abc | 1 | ||||
-rw-r--r-- | msg.c | 35 |
2 files changed, 36 insertions, 0 deletions
diff --git a/compromyse.xyz/assets/abc b/compromyse.xyz/assets/abc index e69de29..2d05e13 100644 --- a/compromyse.xyz/assets/abc +++ b/compromyse.xyz/assets/abc @@ -0,0 +1 @@ +AHHH @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include <ctype.h> +#include <ftw.h> #include <libgen.h> #include <mkdio.h> #include <stdbool.h> @@ -238,6 +239,38 @@ handle_file(const char *path) } int +copy_recursively(const char *fpath, + const struct stat *sb, + int typeflag, + struct FTW *ftwbuf) +{ + (void) sb; + (void) ftwbuf; + + const char *path = fpath + strlen(DIRECTORY) + 1; + char *output_path = NULL; + asprintf(&output_path, "%s/%s", OUTPUT, path); + + if (typeflag == FTW_D) { + mkdir(output_path, 0700); + return FTW_CONTINUE; + } + + if (typeflag != FTW_F) + return FTW_CONTINUE; + + FILE *in = fopen(fpath, "r"); + FILE *out = fopen(output_path, "w"); + + size_t size = fsize(in); + char *content = fcontent(in, size); + + fprintf(out, "%s", content); + + return FTW_CONTINUE; +} + +int main(int argc, char **argv) { (void) argc; @@ -259,6 +292,8 @@ main(int argc, char **argv) fclose(base); mkdir(OUTPUT, 0700); + nftw( + DIRECTORY "/" ASSETS, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL); char **x; char *filepath; |