diff options
| author | Chloe M <chloe@faracom.org> | 2026-07-27 21:21:54 -0400 |
|---|---|---|
| committer | Chloe M <chloe@faracom.org> | 2026-07-27 21:21:54 -0400 |
| commit | 58295b2ce9e0f49f4b1e2f7035b082f027c082c7 (patch) | |
| tree | 6b14f9c23e599dfe19ea1e3766db0980a47e485c /core | |
| parent | 9ba1fbb9b0c0ed5fd11398da7c895f900aff2d27 (diff) | |
rdar: Recurse directories
Signed-off-by: Chloe M <chloe@faracom.org>
Diffstat (limited to 'core')
| -rw-r--r-- | core/rdar.c | 78 |
1 files changed, 77 insertions, 1 deletions
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 <unistd.h> #include <stdio.h> #include <stdint.h> #include <stddef.h> +#include <dirent.h> +#include <string.h> /* 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; } |
