1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
* Copyright (c) 2026, Chloe M.
* 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) \
(((MINOR) << 8) | (MAJOR))
#define VERSION_MAJOR(VERSION) \
((VERSION) & 0xFF)
#define VERSION_MINOR(VERSION) \
(((VERSION) >> 8) & 0xFF)
/*
* Software version
*
* XXX: We encode it as a U16 so that we may bundle it
* with the file data as binary.
*/
#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
version(void)
{
uint16_t Major, Minor;
Major = VERSION_MAJOR(RDAR_VERSION);
Minor = VERSION_MINOR(RDAR_VERSION);
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)
{
int opt;
if (argc < 2) {
printf("fatal: too few arguments\n");
help();
return -1;
}
while ((opt = getopt(argc, argv, "hvi:o:")) != -1) {
switch (opt) {
case 'h':
help();
return -1;
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;
}
|