-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-note.c
448 lines (350 loc) · 10.4 KB
/
create-note.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define ARRAY_LEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define PROGRAM_VERSION "0.2.0"
#ifndef PROGRAM_NAME
#define PROGRAM_NAME "create-note"
#endif
#define DATE_TAG "{{TODAY}}"
// pre-definitions
typedef struct {
char* template;
char** outputs;
char* date_fmt;
char* name_fmt;
bool overwrite;
int time_offset;
} Options;
typedef char* Arg[2];
void usage(const char* argv0, const char* error);
void run(Options opts);
char* argv0;
// functions
int main(int argc, char* argv[]) {
if (argc >= 1) argv0 = argv[0];
// decode argument vector
int length = 0;
Arg* args = malloc(8 * sizeof(Arg));
bool no_more_args = false;
for (int i = 1; i < argc; i++) {
if (length % 8 == 7)
args = realloc(args, (length + 9) * sizeof(Arg));
if (no_more_args || argv[i][0] != '-') {
char* value = malloc(strlen(argv[i]));
strcpy(value, argv[i]);
args[length][0] = NULL;
args[length][1] = value;
length++;
continue;
}
if (argv[i][1] == '-') {
if (argv[i][2] == 0) {
no_more_args = true;
continue;
}
size_t arg_len = strcspn(argv[i] + 2, "=");
char* arg = malloc(arg_len + 1);
strncpy(arg, argv[i] + 2, arg_len);
arg[arg_len] = 0;
char* value = NULL;
if (argv[i][2 + arg_len] == '=') {
value = malloc(strlen(argv[i] + 3 + arg_len) + 1);
strcpy(value, argv[i] + 3 + arg_len);
}
args[length][0] = arg;
args[length][1] = value;
length++;
} else {
size_t arg_len = strcspn(argv[i] + 1, "=");
for (size_t j = 0; j < arg_len; j++) {
char* arg = malloc(2);
arg[0] = argv[i][j + 1];
arg[1] = 0;
char* value = NULL;
if (argv[i][j + 2] == '=') {
value = malloc(strlen(argv[i] + j + 3) + 1);
strcpy(value, argv[i] + j + 3);
}
args[length][0] = arg;
args[length][1] = value;
length++;
}
}
}
args[length][0] = NULL;
args[length][1] = NULL;
// parse arguments
char* template = NULL;
char** outputs = malloc(8 * sizeof(char*));
int n_outputs = 0;
bool help = false;
bool version = false;
Options opts = {
.template = NULL,
.outputs = NULL,
.date_fmt = "%x",
.name_fmt = "%Y-%m-%d",
.overwrite = false,
.time_offset = 0
};
struct {
char flag_short;
char* flag_long;
bool value;
bool present;
} options[] = {
{ 'h', "help", false },
{ 'v', "version", false },
{ 'd', "date", true },
{ 'n', "name", true },
{ 'O', "overwrite", false },
{ 't', "tz", true }
};
for (Arg* arg = args; (*arg)[0] || (*arg)[1]; arg++) {
if ((*arg)[0] == NULL) {
if (template == NULL) {
template = (*arg)[1];
} else {
outputs[n_outputs++] = (*arg)[1];
if (n_outputs % 8 == 0)
outputs = realloc(outputs, (n_outputs + 8) * sizeof(char*));
outputs[n_outputs] = NULL;
}
} else {
for (size_t i = 0; i < ARRAY_LEN(options); i++) {
if (
(
(*arg)[0][1] == 0 &&
(*arg)[0][0] == options[i].flag_short
) ||
strcmp((*arg)[0], options[i].flag_long) == 0
) {
if ((*arg)[1] != NULL && !options[i].value) {
usage(argv0, "unexpected argument value");
exit(EXIT_FAILURE);
}
bool consumed = false;
if ((*arg)[1] == NULL && options[i].value) {
if ((*(arg + 1))[0] == NULL && (*(arg + 1))[1] != NULL) {
(*arg)[1] = (*(arg + 1))[1];
consumed = true;
} else {
usage(argv0, "argument value missing");
exit(EXIT_FAILURE);
}
}
if (options[i].present) {
usage(argv0, "duplicate argument");
exit(EXIT_FAILURE);
}
options[i].present = true;
switch (options[i].flag_short) {
case 'h':
help = true;
break;
case 'v':
version = true;
break;
case 'd':
opts.date_fmt = (*arg)[1];
break;
case 'n':
opts.name_fmt = (*arg)[1];
break;
case 'O':
opts.overwrite = true;
break;
case 't':
opts.time_offset = (int)(3600.0 * strtof((*arg)[1], NULL));
break;
default:
abort(); // unreachable
}
if (consumed) arg++;
goto next;
}
}
usage(argv0, "unknown argument");
exit(EXIT_FAILURE);
next: {}
}
}
opts.template = template;
opts.outputs = outputs;
// main functionality
if (help) {
usage(argv0, NULL);
} else if (version) {
fprintf(stderr, "%s v%s\n", PROGRAM_NAME, PROGRAM_VERSION);
fputs("\n", stderr);
fputs("Written by 19wintersp for Replit task 004.\n", stderr);
fputs("Licensed under the MIT licence.\n", stderr);
} else if (template != NULL) {
run(opts);
} else {
usage(argv0, "no template provided");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
void usage(const char* argv0, const char* error) {
// print error or help with usage
if (argv0 == NULL)
argv0 = PROGRAM_NAME;
if (error != NULL)
fprintf(stderr, "%s: %s\n", argv0, error);
fprintf(stderr, "Usage: %s [OPTION]... <TEMPLATE> [OUTPUT]...\n", argv0);
if (error != NULL) {
fprintf(stderr, "Try '%s --help' for more information\n", argv0);
} else {
fputs("Create a copy of TEMPLATE with a name given by OUTPUT.\n", stderr);
fputs("\n", stderr);
fputs("If OUTPUT is not specified, today's date is used.\n", stderr);
fputs("If OUTPUT is a folder, the default name is used within.\n", stderr);
fputs("\n", stderr);
fputs("You can modify the program with these options:\n", stderr);
fputs(" -d, --date=FMT Specify date format (or local)\n", stderr);
fputs(" -h, --help Print this help text\n", stderr);
fputs(" -n, --name=FMT Specify name format (or YYYY-MM-DD)\n", stderr);
fputs(" -O, --overwrite Overwrite if the target exists\n", stderr);
fputs(" -t, --tz=HOURS Time offset from UTC\n", stderr);
fputs(" -v, --version Print version information\n", stderr);
fputs("\n", stderr);
fputs("The date format uses a few special symbols:\n", stderr);
fputs(" %Y Four-digit year\n", stderr);
fputs(" %y Two-digit year\n", stderr);
fputs(" %m Two-digit month number\n", stderr);
fputs(" %B Month name\n", stderr);
fputs(" %b Short month name\n", stderr);
fputs(" %d Two-digit date number\n", stderr);
fputs(" %A Weekday name\n", stderr);
fputs(" %a Short weekday name\n", stderr);
fputs(" %H Two-digit 24h hour number\n", stderr);
fputs(" %I Two-digit 12h hour number\n", stderr);
fputs(" %M Two-digit minute number\n", stderr);
fputs(" %S Two-digit second number\n", stderr);
fputs(" %P AM or PM marker\n", stderr);
fputs("Non-symbol characters are just copied over.\n", stderr);
fputs("\n", stderr);
fprintf(
stderr,
"The program exits with %d on success, or %d on failure.\n",
EXIT_SUCCESS, EXIT_FAILURE
);
}
}
const char* file_name(const char* path) {
// return file name part of path
char* file = strrchr(path, '/');
return file == NULL ? path : file + 1;
}
const char* file_extension(const char* path) {
// return extension part of path with "."
const char* file = file_name(path);
char* ext = strrchr(file, '.');
return ext == file ? NULL : ext;
}
void copy(const char* src_path, const char* dest_path, const char* date) {
struct stat file;
stat(src_path, &file);
int src = open(src_path, O_RDONLY);
int dest = open(dest_path, O_WRONLY | O_CREAT, file.st_mode);
if (src == -1 || dest == -1) {
usage(argv0, strerror(errno));
exit(EXIT_FAILURE);
}
// loading the whole file into memory is painfully inefficient (I would prefer
// to use a streaming replacement) but it's easier and faster, and the memory
// usage of a program of this sort isn't a great concern.
char* buf = malloc(file.st_size + 1);
buf[file.st_size] = 0;
ssize_t n, sum = 0;
while ((n = read(src, buf + sum, 4096)) > 0) sum += n;
if (n == -1) goto copy_error;
char* ptr = buf;
for (;;) {
char* tag = strstr(ptr, DATE_TAG);
if (tag == NULL) break;
if (write(dest, ptr, tag - ptr) == -1) goto copy_error;
if (write(dest, date, strlen(date)) == -1) goto copy_error;
ptr = tag + strlen(DATE_TAG);
}
n = write(dest, ptr, strlen(ptr));
if (n == -1) {
copy_error:
usage(argv0, strerror(errno));
exit(EXIT_FAILURE);
}
close(src);
close(dest);
}
void run(Options opts) {
struct stat file;
time_t c_time = time(NULL) + opts.time_offset;
const struct tm* c_tm = gmtime(&c_time);
size_t date_len = strftime(NULL, -1, opts.date_fmt, c_tm); // ub?
char* date = calloc(date_len + 1, sizeof(char));
strftime(date, date_len + 1, opts.date_fmt, c_tm);
size_t time_len = strftime(NULL, -1, opts.name_fmt, c_tm); // ub?
char* time = calloc(time_len + 1, sizeof(char));
strftime(time, time_len + 1, opts.name_fmt, c_tm);
const char* ext = file_extension(opts.template);
if (ext == NULL) ext = "";
size_t ext_len = strlen(ext);
if (ext_len > 1023) {
usage(argv0, "file extension is too long");
exit(EXIT_FAILURE);
}
char* default_name = calloc(time_len + ext_len + 1, sizeof(char));
snprintf(default_name, time_len + ext_len + 1, "%s%s", time, ext);
if (stat(opts.template, &file) == -1) {
usage(argv0, strerror(errno));
exit(EXIT_FAILURE);
}
char** output = opts.outputs;
for (; *output != NULL; output++) {
if (stat(*output, &file) == 0) {
if (S_ISDIR(file.st_mode)) {
size_t dest_len = strlen(*output) + 1 + strlen(default_name);
char* dest = calloc(dest_len + 1, sizeof(char));
char* slash = (*output)[strlen(*output) - 1] != '/' ? "/" : "";
snprintf(dest, dest_len + 1, "%s%s%s", *output, slash, default_name);
copy(opts.template, dest, date);
} else if (opts.overwrite) {
copy(opts.template, *output, date);
} else {
usage(argv0, "target already exists");
exit(EXIT_FAILURE);
}
} else if (errno == ENOENT) {
copy(opts.template, *output, date);
} else {
usage(argv0, strerror(errno));
exit(EXIT_FAILURE);
}
}
if (output == opts.outputs) {
if (stat(default_name, &file) == 0 && !opts.overwrite) {
char* incremented_name;
unsigned int i = 0;
do {
i++;
if (incremented_name != NULL) free(incremented_name);
size_t length = snprintf(NULL, 0, "%s-%u%s", time, i, ext);
incremented_name = calloc(length + 1, sizeof(char));
snprintf(incremented_name, length + 1, "%s-%u%s", time, i, ext);
} while (stat(incremented_name, &file) == 0);
copy(opts.template, incremented_name, date);
} else {
copy(opts.template, default_name, date);
}
}
}