From 58295b2ce9e0f49f4b1e2f7035b082f027c082c7 Mon Sep 17 00:00:00 2001 From: Chloe M Date: Mon, 27 Jul 2026 21:21:54 -0400 Subject: rdar: Recurse directories Signed-off-by: Chloe M --- core/rdar.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/rdar.c b/core/rdar.c index ff5022f..a5f3b48 100644 --- a/core/rdar.c +++ b/core/rdar.c @@ -3,10 +3,13 @@ * Provided under the BSD-3 clause. */ +#define _DEFAULT_SOURCE #include #include #include #include +#include +#include /* Version conversion macros */ #define VERSION_U16(MAJOR, MINOR) \ @@ -24,12 +27,21 @@ */ #define RDAR_VERSION VERSION_U16(1, 0) /* v1.0 */ +/* Fallback path if '-o' is not specified */ +#define FALLBACK_OUTPUT_PATH "out.rdar" + +/* Globals */ +static const char *input_dir = NULL; +static const char *output_path = NULL; + static void help(void) { printf("usage: ./rdar <... flags>\n"); printf("[-h] Display this help menu\n"); printf("[-v] Display the program version\n"); + printf("[-i] Input directory to pack\n"); + printf("[-o] Output path for archive file\n"); } static void @@ -42,6 +54,41 @@ version(void) printf("Version v%d.%d\n", Major, Minor); } +static void +recurse_dir(const char *dirpath) +{ + DIR *dir; + struct dirent *dirent; + char pathbuf[264]; + + dir = opendir(dirpath); + if (dir == NULL) { + printf("fatal: failed to open '%s'\n", input_dir); + perror("opendir"); + return; + } + + while ((dirent = readdir(dir)) != NULL) { + if (dirent->d_name[0] == '.') { + continue; + } + + switch (dirent->d_type) { + case DT_REG: + snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, dirent->d_name); + printf("[f] %s\n", pathbuf); + break; + case DT_DIR: + snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, dirent->d_name); + printf("[d] %s\n", pathbuf); + recurse_dir(pathbuf); + break; + } + } + + closedir(dir); +} + int main(int argc, char **argv) { @@ -53,7 +100,7 @@ main(int argc, char **argv) return -1; } - while ((opt = getopt(argc, argv, "hv")) != -1) { + while ((opt = getopt(argc, argv, "hvi:o:")) != -1) { switch (opt) { case 'h': help(); @@ -61,6 +108,35 @@ main(int argc, char **argv) case 'v': version(); return -1; + case 'i': + input_dir = strdup(optarg); + if (input_dir == NULL) { + printf("fatal: failed to allocate input_dir\n"); + return -1; + } + + break; + case 'o': + output_path = strdup(optarg); + if (output_path == NULL) { + printf("fatal: failed to allocate output dir\n"); + return -1; + } + + break; } } + + if (input_dir == NULL) { + printf("fatal: input path not specified\n"); + help(); + return -1; + } + + if (output_path == NULL) { + output_path = FALLBACK_OUTPUT_PATH; + } + + recurse_dir(input_dir); + return 0; } -- cgit v1.2.3