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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
|
/*********************************************************************
* bfs *
* Copyright (C) 2015-2016 Tavian Barnes <tavianator@tavianator.com> *
* *
* This program is free software. It comes without any warranty, to *
* the extent permitted by applicable law. You can redistribute it *
* and/or modify it under the terms of the Do What The Fuck You Want *
* To Public License, Version 2, as published by Sam Hocevar. See *
* the COPYING file or http://www.wtfpl.net/ for more details. *
*********************************************************************/
#include "bfs.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
/**
* Create a new expression.
*/
static struct expr *new_expr(eval_fn *eval) {
struct expr *expr = malloc(sizeof(struct expr));
if (!expr) {
perror("malloc()");
return NULL;
}
expr->lhs = NULL;
expr->rhs = NULL;
expr->eval = eval;
return expr;
}
/**
* Singleton true expression instance.
*/
static struct expr expr_true = {
.lhs = NULL,
.rhs = NULL,
.eval = eval_true,
};
/**
* Singleton false expression instance.
*/
static struct expr expr_false = {
.lhs = NULL,
.rhs = NULL,
.eval = eval_false,
};
/**
* Free an expression.
*/
static void free_expr(struct expr *expr) {
if (expr && expr != &expr_true && expr != &expr_false) {
free_expr(expr->lhs);
free_expr(expr->rhs);
free(expr);
}
}
/**
* Create a new unary expression.
*/
static struct expr *new_unary_expr(struct expr *rhs, eval_fn *eval) {
struct expr *expr = new_expr(eval);
if (!expr) {
free_expr(rhs);
return NULL;
}
expr->rhs = rhs;
expr->eval = eval;
return expr;
}
/**
* Create a new binary expression.
*/
static struct expr *new_binary_expr(struct expr *lhs, struct expr *rhs, eval_fn *eval) {
struct expr *expr = new_expr(eval);
if (!expr) {
free_expr(rhs);
free_expr(lhs);
return NULL;
}
expr->lhs = lhs;
expr->rhs = rhs;
expr->eval = eval;
return expr;
}
/**
* Free the parsed command line.
*/
void free_cmdline(struct cmdline *cl) {
if (cl) {
free_expr(cl->expr);
free_colors(cl->colors);
free(cl->roots);
free(cl);
}
}
/**
* Add a root path to the cmdline.
*/
static bool cmdline_add_root(struct cmdline *cl, const char *root) {
size_t i = cl->nroots++;
const char **roots = realloc(cl->roots, cl->nroots*sizeof(const char *));
if (!roots) {
perror("realloc()");
return false;
}
roots[i] = root;
cl->roots = roots;
return true;
}
/**
* Ephemeral state for parsing the command line.
*/
struct parser_state {
/** The command line being parsed. */
struct cmdline *cl;
/** The command line arguments. */
char **argv;
/** Current argument index. */
int i;
/** Whether a -print action is implied. */
bool implicit_print;
/** Whether warnings are enabled (see -warn, -nowarn). */
bool warn;
/** Whether any non-option arguments have been encountered. */
bool non_option_seen;
/** The current time. */
struct timespec now;
};
/**
* Parse the expression specified on the command line.
*/
static struct expr *parse_expr(struct parser_state *state);
/**
* While parsing an expression, skip any paths and add them to the cmdline.
*/
static const char *skip_paths(struct parser_state *state) {
while (true) {
const char *arg = state->argv[state->i];
if (!arg
|| arg[0] == '-'
|| strcmp(arg, "(") == 0
|| strcmp(arg, ")") == 0
|| strcmp(arg, "!") == 0
|| strcmp(arg, ",") == 0) {
return arg;
}
if (!cmdline_add_root(state->cl, arg)) {
return NULL;
}
++state->i;
}
}
/**
* Parse an integer.
*/
static bool parse_int(const char *str, int *value) {
char *endptr;
long result = strtol(str, &endptr, 10);
if (*str == '\0' || *endptr != '\0') {
goto bad;
}
if (result < INT_MIN || result > INT_MAX) {
goto bad;
}
*value = result;
return true;
bad:
fprintf(stderr, "'%s' is not a valid integer.\n", str);
return false;
}
/**
* Parse an integer and a comparison flag.
*/
static bool parse_icmp(const char *str, struct expr *expr) {
switch (str[0]) {
case '-':
expr->cmp = CMP_LESS;
++str;
break;
case '+':
expr->cmp = CMP_GREATER;
++str;
break;
default:
expr->cmp = CMP_EXACT;
break;
}
return parse_int(str, &expr->idata);
}
/**
* Create a new option expression.
*/
static struct expr *new_option(struct parser_state *state, const char *option) {
if (state->warn && state->non_option_seen) {
fprintf(stderr,
"The '%s' option applies to the entire command line.\n"
"For clarity, place it before any non-option arguments.\n\n",
option);
}
return &expr_true;
}
/**
* Create a new positional option expression.
*/
static struct expr *new_positional_option(struct parser_state *state) {
return &expr_true;
}
/**
* Create a new test expression.
*/
static struct expr *new_test(struct parser_state *state, eval_fn *eval) {
state->non_option_seen = true;
return new_expr(eval);
}
/**
* Create a new test expression with integer data.
*/
static struct expr *new_test_idata(struct parser_state *state, eval_fn *eval, int idata) {
struct expr *test = new_test(state, eval);
if (test) {
test->idata = idata;
}
return test;
}
/**
* Create a new test expression with string data.
*/
static struct expr *new_test_sdata(struct parser_state *state, eval_fn *eval, const char *sdata) {
struct expr *test = new_test(state, eval);
if (test) {
test->sdata = sdata;
}
return test;
}
/**
* Create a new action expression.
*/
static struct expr *new_action(struct parser_state *state, eval_fn *eval) {
if (eval != eval_nohidden && eval != eval_prune) {
state->implicit_print = false;
}
state->non_option_seen = true;
return new_expr(eval);
}
/**
* Parse a test expression with integer data and a comparison flag.
*/
static struct expr *parse_test_icmp(struct parser_state *state, const char *test, eval_fn *eval) {
const char *arg = state->argv[state->i];
if (!arg) {
fprintf(stderr, "%s needs a value.\n", test);
return NULL;
}
++state->i;
struct expr *expr = new_test(state, eval);
if (expr) {
if (!parse_icmp(arg, expr)) {
free_expr(expr);
expr = NULL;
}
}
return expr;
}
/**
* Parse a test that takes a string argument.
*/
static struct expr *parse_test_sdata(struct parser_state *state, const char *test, eval_fn *eval) {
const char *arg = state->argv[state->i];
if (!arg) {
fprintf(stderr, "%s needs a value.\n", test);
return NULL;
}
++state->i;
return new_test_sdata(state, eval, arg);
}
/**
* Parse -[acm]{min,time}.
*/
static struct expr *parse_acmtime(struct parser_state *state, const char *option, enum timefield field, enum timeunit unit) {
struct expr *expr = parse_test_icmp(state, option, eval_acmtime);
if (expr) {
expr->reftime = state->now;
expr->timefield = field;
expr->timeunit = unit;
}
return expr;
}
/**
* Parse -[ac]?newer.
*/
static struct expr *parse_acnewer(struct parser_state *state, const char *option, enum timefield field) {
struct expr *expr = parse_test_sdata(state, option, eval_acnewer);
if (!expr) {
return NULL;
}
struct stat sb;
if (fstatat(AT_FDCWD, expr->sdata, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
print_error(NULL, expr->sdata, errno);
free_expr(expr);
return NULL;
}
expr->reftime = sb.st_mtim;
expr->timefield = field;
return expr;
}
/**
* "Parse" -daystart.
*/
static struct expr *parse_daystart(struct parser_state *state) {
// Should be called before localtime_r() according to POSIX.1-2004
tzset();
struct tm tm;
if (!localtime_r(&state->now.tv_sec, &tm)) {
perror("localtime_r()");
return NULL;
}
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 0;
++tm.tm_mday;
time_t time = mktime(&tm);
if (time == -1) {
perror("mktime()");
return NULL;
}
state->now.tv_sec = time;
state->now.tv_nsec = 0;
return new_positional_option(state);
}
/**
* Parse -{min,max}depth N.
*/
static struct expr *parse_depth(struct parser_state *state, const char *option, int *depth) {
const char *arg = state->argv[state->i];
if (!arg) {
fprintf(stderr, "%s needs a value.\n", option);
return NULL;
}
++state->i;
if (!parse_int(arg, depth)) {
return NULL;
}
return new_option(state, option);
}
/**
* Parse -type [bcdpfls].
*/
static struct expr *parse_type(struct parser_state *state) {
const char *arg = state->argv[state->i];
if (!arg) {
fputs("-type needs a value.\n", stderr);
return NULL;
}
int typeflag = BFTW_UNKNOWN;
switch (arg[0]) {
case 'b':
typeflag = BFTW_BLK;
break;
case 'c':
typeflag = BFTW_CHR;
break;
case 'd':
typeflag = BFTW_DIR;
break;
case 'p':
typeflag = BFTW_FIFO;
break;
case 'f':
typeflag = BFTW_REG;
break;
case 'l':
typeflag = BFTW_LNK;
break;
case 's':
typeflag = BFTW_SOCK;
break;
}
if (typeflag == BFTW_UNKNOWN || arg[1] != '\0') {
fprintf(stderr, "Unknown type flag '%s'.\n", arg);
return NULL;
}
++state->i;
return new_test_idata(state, eval_type, typeflag);
}
/**
* LITERAL : OPTION
* | TEST
* | ACTION
*/
static struct expr *parse_literal(struct parser_state *state) {
// Paths are already skipped at this point
const char *arg = state->argv[state->i++];
if (strcmp(arg, "-amin") == 0) {
return parse_acmtime(state, arg, ATIME, MINUTES);
} else if (strcmp(arg, "-atime") == 0) {
return parse_acmtime(state, arg, ATIME, DAYS);
} else if (strcmp(arg, "-anewer") == 0) {
return parse_acnewer(state, arg, ATIME);
} else if (strcmp(arg, "-cmin") == 0) {
return parse_acmtime(state, arg, CTIME, MINUTES);
} else if (strcmp(arg, "-ctime") == 0) {
return parse_acmtime(state, arg, CTIME, DAYS);
} else if (strcmp(arg, "-cnewer") == 0) {
return parse_acnewer(state, arg, CTIME);
} else if (strcmp(arg, "-color") == 0) {
state->cl->color = true;
return new_option(state, arg);
} else if (strcmp(arg, "-nocolor") == 0) {
state->cl->color = false;
return new_option(state, arg);
} else if (strcmp(arg, "-daystart") == 0) {
return parse_daystart(state);
} else if (strcmp(arg, "-delete") == 0) {
state->cl->flags |= BFTW_DEPTH;
return new_action(state, eval_delete);
} else if (strcmp(arg, "-d") == 0 || strcmp(arg, "-depth") == 0) {
state->cl->flags |= BFTW_DEPTH;
return new_option(state, arg);
} else if (strcmp(arg, "-empty") == 0) {
return new_test(state, eval_empty);
} else if (strcmp(arg, "-executable") == 0) {
return new_test_idata(state, eval_access, X_OK);
} else if (strcmp(arg, "-false") == 0) {
return &expr_false;
} else if (strcmp(arg, "-gid") == 0) {
return parse_test_icmp(state, arg, eval_gid);
} else if (strcmp(arg, "-uid") == 0) {
return parse_test_icmp(state, arg, eval_uid);
} else if (strcmp(arg, "-hidden") == 0) {
return new_test(state, eval_hidden);
} else if (strcmp(arg, "-nohidden") == 0) {
return new_action(state, eval_nohidden);
} else if (strcmp(arg, "-mindepth") == 0) {
return parse_depth(state, arg, &state->cl->mindepth);
} else if (strcmp(arg, "-maxdepth") == 0) {
return parse_depth(state, arg, &state->cl->maxdepth);
} else if (strcmp(arg, "-mmin") == 0) {
return parse_acmtime(state, arg, MTIME, MINUTES);
} else if (strcmp(arg, "-mtime") == 0) {
return parse_acmtime(state, arg, MTIME, DAYS);
} else if (strcmp(arg, "-name") == 0) {
return parse_test_sdata(state, arg, eval_name);
} else if (strcmp(arg, "-newer") == 0) {
return parse_acnewer(state, arg, MTIME);
} else if (strcmp(arg, "-path") == 0 || strcmp(arg, "-wholename") == 0) {
return parse_test_sdata(state, arg, eval_path);
} else if (strcmp(arg, "-print") == 0) {
return new_action(state, eval_print);
} else if (strcmp(arg, "-print0") == 0) {
return new_action(state, eval_print0);
} else if (strcmp(arg, "-prune") == 0) {
return new_action(state, eval_prune);
} else if (strcmp(arg, "-quit") == 0) {
return new_action(state, eval_quit);
} else if (strcmp(arg, "-readable") == 0) {
return new_test_idata(state, eval_access, R_OK);
} else if (strcmp(arg, "-true") == 0) {
return &expr_true;
} else if (strcmp(arg, "-type") == 0) {
return parse_type(state);
} else if (strcmp(arg, "-warn") == 0) {
state->warn = true;
return new_positional_option(state);
} else if (strcmp(arg, "-nowarn") == 0) {
state->warn = false;
return new_positional_option(state);
} else if (strcmp(arg, "-writable") == 0) {
return new_test_idata(state, eval_access, W_OK);
} else {
fprintf(stderr, "Unknown argument '%s'.\n", arg);
return NULL;
}
}
/**
* Create a "not" expression.
*/
static struct expr *new_not_expr(struct expr *rhs) {
if (rhs == &expr_true) {
return &expr_false;
} else if (rhs == &expr_false) {
return &expr_true;
} else {
return new_unary_expr(rhs, eval_not);
}
}
/**
* FACTOR : "(" EXPR ")"
* | "!" FACTOR | "-not" FACTOR
* | LITERAL
*/
static struct expr *parse_factor(struct parser_state *state) {
const char *arg = skip_paths(state);
if (!arg) {
fputs("Expression terminated prematurely.\n", stderr);
return NULL;
}
if (strcmp(arg, "(") == 0) {
++state->i;
struct expr *expr = parse_expr(state);
if (!expr) {
return NULL;
}
arg = skip_paths(state);
if (!arg || strcmp(arg, ")") != 0) {
fputs("Expected a ')'.\n", stderr);
free_expr(expr);
return NULL;
}
++state->i;
return expr;
} else if (strcmp(arg, "!") == 0 || strcmp(arg, "-not") == 0) {
++state->i;
struct expr *factor = parse_factor(state);
if (!factor) {
return NULL;
}
return new_not_expr(factor);
} else {
return parse_literal(state);
}
}
/**
* Create an "and" expression.
*/
static struct expr *new_and_expr(struct expr *lhs, struct expr *rhs) {
if (lhs == &expr_true) {
return rhs;
} else if (lhs == &expr_false) {
free_expr(rhs);
return lhs;
} else if (rhs == &expr_true) {
return lhs;
} else {
return new_binary_expr(lhs, rhs, eval_and);
}
}
/**
* TERM : FACTOR
* | TERM FACTOR
* | TERM "-a" FACTOR
* | TERM "-and" FACTOR
*/
static struct expr *parse_term(struct parser_state *state) {
struct expr *term = parse_factor(state);
while (term) {
const char *arg = skip_paths(state);
if (!arg) {
break;
}
if (strcmp(arg, "-o") == 0 || strcmp(arg, "-or") == 0
|| strcmp(arg, ",") == 0
|| strcmp(arg, ")") == 0) {
break;
}
if (strcmp(arg, "-a") == 0 || strcmp(arg, "-and") == 0) {
++state->i;
}
struct expr *lhs = term;
struct expr *rhs = parse_factor(state);
if (!rhs) {
free_expr(lhs);
return NULL;
}
term = new_and_expr(lhs, rhs);
}
return term;
}
/**
* Create an "or" expression.
*/
static struct expr *new_or_expr(struct expr *lhs, struct expr *rhs) {
if (lhs == &expr_true) {
free_expr(rhs);
return lhs;
} else if (lhs == &expr_false) {
return rhs;
} else if (rhs == &expr_false) {
return lhs;
} else {
return new_binary_expr(lhs, rhs, eval_or);
}
}
/**
* CLAUSE : TERM
* | CLAUSE "-o" TERM
* | CLAUSE "-or" TERM
*/
static struct expr *parse_clause(struct parser_state *state) {
struct expr *clause = parse_term(state);
while (clause) {
const char *arg = skip_paths(state);
if (!arg) {
break;
}
if (strcmp(arg, "-o") != 0 && strcmp(arg, "-or") != 0) {
break;
}
++state->i;
struct expr *lhs = clause;
struct expr *rhs = parse_term(state);
if (!rhs) {
free_expr(lhs);
return NULL;
}
clause = new_or_expr(lhs, rhs);
}
return clause;
}
/**
* Create a "comma" expression.
*/
static struct expr *new_comma_expr(struct expr *lhs, struct expr *rhs) {
if (lhs == &expr_true || lhs == &expr_false) {
return rhs;
} else {
return new_binary_expr(lhs, rhs, eval_comma);
}
}
/**
* EXPR : CLAUSE
* | EXPR "," CLAUSE
*/
static struct expr *parse_expr(struct parser_state *state) {
struct expr *expr = parse_clause(state);
while (expr) {
const char *arg = skip_paths(state);
if (!arg) {
break;
}
if (strcmp(arg, ",") != 0) {
break;
}
++state->i;
struct expr *lhs = expr;
struct expr *rhs = parse_clause(state);
if (!rhs) {
free_expr(lhs);
return NULL;
}
expr = new_comma_expr(lhs, rhs);
}
return expr;
}
/**
* Parse the command line.
*/
struct cmdline *parse_cmdline(int argc, char *argv[]) {
struct cmdline *cl = malloc(sizeof(struct cmdline));
if (!cl) {
goto fail;
}
cl->roots = NULL;
cl->nroots = 0;
cl->colors = NULL;
cl->color = isatty(STDOUT_FILENO);
cl->mindepth = 0;
cl->maxdepth = INT_MAX;
cl->flags = BFTW_RECOVER;
cl->expr = &expr_true;
struct parser_state state = {
.cl = cl,
.argv = argv,
.i = 1,
.implicit_print = true,
.warn = true,
.non_option_seen = false,
};
if (clock_gettime(CLOCK_REALTIME, &state.now) != 0) {
perror("clock_gettime()");
goto fail;
}
if (skip_paths(&state)) {
cl->expr = parse_expr(&state);
if (!cl->expr) {
goto fail;
}
}
if (state.i < argc) {
fprintf(stderr, "Unexpected argument '%s'.\n", argv[state.i]);
goto fail;
}
if (state.implicit_print) {
struct expr *print = new_expr(eval_print);
if (!print) {
goto fail;
}
cl->expr = new_and_expr(cl->expr, print);
if (!cl->expr) {
goto fail;
}
}
if (cl->nroots == 0) {
if (!cmdline_add_root(cl, ".")) {
goto fail;
}
}
if (cl->color) {
cl->colors = parse_colors(getenv("LS_COLORS"));
}
return cl;
fail:
free_cmdline(cl);
return NULL;
}
|