forked from michaelforney/sbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.c
1070 lines (915 loc) · 25.9 KB
/
find.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
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* See LICENSE file for copyright and license details. */
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <grp.h>
#include <libgen.h>
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "util.h"
/* because putting integers in pointers is undefined by the standard */
union extra {
void *p;
intmax_t i;
};
/* Argument passed into a primary's function */
struct arg {
char *path;
struct stat *st;
union extra extra;
};
/* Information about each primary, for lookup table */
struct pri_info {
char *name;
int (*func)(struct arg *arg);
char **(*getarg)(char **argv, union extra *extra);
void (*freearg)(union extra extra);
char narg; /* -xdev, -depth, -print don't take args but have getarg() */
};
/* Information about operators, for lookup table */
struct op_info {
char *name; /* string representation of op */
char type; /* from tok.type */
char prec; /* precedence */
char nargs; /* number of arguments (unary or binary) */
char lassoc; /* left associative */
};
/* Token when lexing/parsing
* (although also used for the expression tree) */
struct tok {
struct tok *left, *right; /* if (type == NOT) left = NULL */
union extra extra;
union {
struct pri_info *pinfo; /* if (type == PRIM) */
struct op_info *oinfo;
} u;
enum {
PRIM = 0, LPAR, RPAR, NOT, AND, OR, END
} type;
};
/* structures used for arg.extra.p and tok.extra.p */
struct permarg {
mode_t mode;
char exact;
};
struct okarg {
char ***braces;
char **argv;
};
/* for all arguments that take a number
* +n, n, -n mean > n, == n, < n respectively */
struct narg {
int (*cmp)(int a, int b);
int n;
};
struct sizearg {
struct narg n;
char bytes; /* size is in bytes, not 512 byte sectors */
};
struct execarg {
union {
struct {
char ***braces; /* NULL terminated list of pointers into argv where {} were */
} s; /* semicolon */
struct {
size_t arglen; /* number of bytes in argv before files are added */
size_t filelen; /* numer of bytes in file names added to argv */
size_t first; /* index one past last arg, where first file goes */
size_t next; /* index where next file goes */
size_t cap; /* capacity of argv */
} p; /* plus */
} u;
char **argv; /* NULL terminated list of arguments (allocated if isplus) */
char isplus; /* -exec + instead of -exec ; */
};
/* used to find loops while recursing through directory structure */
struct findhist {
struct findhist *next;
char *path;
dev_t dev;
ino_t ino;
};
/* Primaries */
static int pri_name (struct arg *arg);
static int pri_path (struct arg *arg);
static int pri_nouser (struct arg *arg);
static int pri_nogroup(struct arg *arg);
static int pri_xdev (struct arg *arg);
static int pri_prune (struct arg *arg);
static int pri_perm (struct arg *arg);
static int pri_type (struct arg *arg);
static int pri_links (struct arg *arg);
static int pri_user (struct arg *arg);
static int pri_group (struct arg *arg);
static int pri_size (struct arg *arg);
static int pri_atime (struct arg *arg);
static int pri_ctime (struct arg *arg);
static int pri_mtime (struct arg *arg);
static int pri_exec (struct arg *arg);
static int pri_ok (struct arg *arg);
static int pri_print (struct arg *arg);
static int pri_newer (struct arg *arg);
static int pri_depth (struct arg *arg);
/* Getargs */
static char **get_name_arg (char *argv[], union extra *extra);
static char **get_path_arg (char *argv[], union extra *extra);
static char **get_xdev_arg (char *argv[], union extra *extra);
static char **get_perm_arg (char *argv[], union extra *extra);
static char **get_type_arg (char *argv[], union extra *extra);
static char **get_n_arg (char *argv[], union extra *extra);
static char **get_user_arg (char *argv[], union extra *extra);
static char **get_group_arg(char *argv[], union extra *extra);
static char **get_size_arg (char *argv[], union extra *extra);
static char **get_exec_arg (char *argv[], union extra *extra);
static char **get_ok_arg (char *argv[], union extra *extra);
static char **get_print_arg(char *argv[], union extra *extra);
static char **get_newer_arg(char *argv[], union extra *extra);
static char **get_depth_arg(char *argv[], union extra *extra);
/* Freeargs */
static void free_extra (union extra extra);
static void free_exec_arg(union extra extra);
static void free_ok_arg (union extra extra);
/* Parsing/Building/Running */
static void fill_narg(char *s, struct narg *n);
static struct pri_info *find_primary(char *name);
static struct op_info *find_op(char *name);
static void parse(int argc, char **argv);
static int eval(struct tok *tok, struct arg *arg);
static void find(char *path, struct findhist *hist);
static void usage(void);
/* for comparisons with narg */
static int cmp_gt(int a, int b) { return a > b; }
static int cmp_eq(int a, int b) { return a == b; }
static int cmp_lt(int a, int b) { return a < b; }
/* order from find(1p), may want to alphabetize */
static struct pri_info primaries[] = {
{ "-name" , pri_name , get_name_arg , NULL , 1 },
{ "-path" , pri_path , get_path_arg , NULL , 1 },
{ "-nouser" , pri_nouser , NULL , NULL , 1 },
{ "-nogroup", pri_nogroup, NULL , NULL , 1 },
{ "-xdev" , pri_xdev , get_xdev_arg , NULL , 0 },
{ "-prune" , pri_prune , NULL , NULL , 1 },
{ "-perm" , pri_perm , get_perm_arg , free_extra , 1 },
{ "-type" , pri_type , get_type_arg , NULL , 1 },
{ "-links" , pri_links , get_n_arg , free_extra , 1 },
{ "-user" , pri_user , get_user_arg , NULL , 1 },
{ "-group" , pri_group , get_group_arg, NULL , 1 },
{ "-size" , pri_size , get_size_arg , free_extra , 1 },
{ "-atime" , pri_atime , get_n_arg , free_extra , 1 },
{ "-ctime" , pri_ctime , get_n_arg , free_extra , 1 },
{ "-mtime" , pri_mtime , get_n_arg , free_extra , 1 },
{ "-exec" , pri_exec , get_exec_arg , free_exec_arg, 1 },
{ "-ok" , pri_ok , get_ok_arg , free_ok_arg , 1 },
{ "-print" , pri_print , get_print_arg, NULL , 0 },
{ "-newer" , pri_newer , get_newer_arg, NULL , 1 },
{ "-depth" , pri_depth , get_depth_arg, NULL , 0 },
{ NULL, NULL, NULL, NULL, 0 }
};
static struct op_info ops[] = {
{ "(" , LPAR, 0, 0, 0 }, /* parens are handled specially */
{ ")" , RPAR, 0, 0, 0 },
{ "!" , NOT , 3, 1, 0 },
{ "-a", AND , 2, 2, 1 },
{ "-o", OR , 1, 2, 1 },
{ NULL, 0, 0, 0, 0 }
};
extern char **environ;
static struct tok *toks; /* holds allocated array of all toks created while parsing */
static struct tok *root; /* points to root of expression tree, inside toks array */
static struct timespec start; /* time find was started, used for -[acm]time */
static size_t envlen; /* number of bytes in environ, used to calculate against ARG_MAX */
static size_t argmax; /* value of ARG_MAX retrieved using sysconf(3p) */
static struct {
char ret ; /* return value from main */
char depth; /* -depth, directory contents before directory itself */
char h ; /* -H, follow symlinks on command line */
char l ; /* -L, follow all symlinks (command line and search) */
char prune; /* hit -prune */
char xdev ; /* -xdev, prune directories on different devices */
char print; /* whether we will need -print when parsing */
} gflags;
/*
* Primaries
*/
static int
pri_name(struct arg *arg)
{
int ret;
char *path;
path = estrdup(arg->path);
ret = !fnmatch((char *)arg->extra.p, basename(path), 0);
free(path);
return ret;
}
static int
pri_path(struct arg *arg)
{
return !fnmatch((char *)arg->extra.p, arg->path, 0);
}
/* FIXME: what about errors? find(1p) literally just says
* "for which the getpwuid() function ... returns NULL" */
static int
pri_nouser(struct arg *arg)
{
return !getpwuid(arg->st->st_uid);
}
static int
pri_nogroup(struct arg *arg)
{
return !getgrgid(arg->st->st_gid);
}
static int
pri_xdev(struct arg *arg)
{
return 1;
}
static int
pri_prune(struct arg *arg)
{
return gflags.prune = 1;
}
static int
pri_perm(struct arg *arg)
{
struct permarg *p = (struct permarg *)arg->extra.p;
return (arg->st->st_mode & 07777 & (p->exact ? -1U : p->mode)) == p->mode;
}
static int
pri_type(struct arg *arg)
{
switch ((char)arg->extra.i) {
default : return 0; /* impossible, but placate warnings */
case 'b': return S_ISBLK (arg->st->st_mode);
case 'c': return S_ISCHR (arg->st->st_mode);
case 'd': return S_ISDIR (arg->st->st_mode);
case 'l': return S_ISLNK (arg->st->st_mode);
case 'p': return S_ISFIFO(arg->st->st_mode);
case 'f': return S_ISREG (arg->st->st_mode);
case 's': return S_ISSOCK(arg->st->st_mode);
}
}
static int
pri_links(struct arg *arg)
{
struct narg *n = arg->extra.p;
return n->cmp(arg->st->st_nlink, n->n);
}
static int
pri_user(struct arg *arg)
{
return arg->st->st_uid == (uid_t)arg->extra.i;
}
static int
pri_group(struct arg *arg)
{
return arg->st->st_gid == (gid_t)arg->extra.i;
}
static int
pri_size(struct arg *arg)
{
struct sizearg *s = arg->extra.p;
off_t size = arg->st->st_size;
if (!s->bytes)
size = size / 512 + !!(size % 512);
return s->n.cmp(size, s->n.n);
}
/* FIXME: ignoring nanoseconds in atime, ctime, mtime */
static int
pri_atime(struct arg *arg)
{
struct narg *n = arg->extra.p;
return n->cmp((start.tv_sec - arg->st->st_atime) / 86400, n->n);
}
static int
pri_ctime(struct arg *arg)
{
struct narg *n = arg->extra.p;
return n->cmp((start.tv_sec - arg->st->st_ctime) / 86400, n->n);
}
static int
pri_mtime(struct arg *arg)
{
struct narg *n = arg->extra.p;
return n->cmp((start.tv_sec - arg->st->st_mtime) / 86400, n->n);
}
static int
pri_exec(struct arg *arg)
{
int status;
size_t len;
pid_t pid;
char **sp, ***brace;
struct execarg *e = arg->extra.p;
if (e->isplus) {
len = strlen(arg->path) + 1;
/* if we reached ARG_MAX, fork, exec, wait, free file names, reset list */
if (len + e->u.p.arglen + e->u.p.filelen + envlen > argmax) {
e->argv[e->u.p.next] = NULL;
switch((pid = fork())) {
case -1:
eprintf("fork:");
case 0:
execvp(*e->argv, e->argv);
weprintf("exec %s failed:", *e->argv);
_exit(1);
}
waitpid(pid, &status, 0);
gflags.ret = gflags.ret || status;
for (sp = e->argv + e->u.p.first; *sp; sp++)
free(*sp);
e->u.p.next = e->u.p.first;
e->u.p.filelen = 0;
}
/* if we have too many files, realloc (with space for NULL termination) */
if (e->u.p.next + 1 == e->u.p.cap)
e->argv = ereallocarray(e->argv, e->u.p.cap *= 2, sizeof(*e->argv));
e->argv[e->u.p.next++] = estrdup(arg->path);
e->u.p.filelen += len + sizeof(arg->path);
return 1;
} else {
/* insert path everywhere user gave us {} */
for (brace = e->u.s.braces; *brace; brace++)
**brace = arg->path;
switch((pid = fork())) {
case -1:
eprintf("fork:");
case 0:
execvp(*e->argv, e->argv);
weprintf("exec %s failed:", *e->argv);
_exit(1);
}
/* FIXME: proper course of action for all waitpid() on EINTR? */
waitpid(pid, &status, 0);
return !!status;
}
}
static int
pri_ok(struct arg *arg)
{
int status, reply;
pid_t pid;
char ***brace, c;
struct okarg *o = arg->extra.p;
fprintf(stderr, "%s: %s ? ", *o->argv, arg->path);
reply = fgetc(stdin);
/* throw away rest of line */
while ((c = fgetc(stdin)) != '\n' && c != EOF)
/* FIXME: what if the first character of the rest of the line is a null
* byte? */
;
if (feof(stdin)) /* FIXME: ferror()? */
clearerr(stdin);
if (reply != 'y' && reply != 'Y')
return 0;
/* insert filename everywhere user gave us {} */
for (brace = o->braces; *brace; brace++)
**brace = arg->path;
switch((pid = fork())) {
case -1:
eprintf("fork:");
case 0:
execvp(*o->argv, o->argv);
weprintf("exec %s failed:", *o->argv);
_exit(1);
}
waitpid(pid, &status, 0);
return !!status;
}
static int
pri_print(struct arg *arg)
{
if (puts(arg->path) == EOF)
eprintf("puts failed:");
return 1;
}
/* FIXME: ignoring nanoseconds */
static int
pri_newer(struct arg *arg)
{
return arg->st->st_mtime > (time_t)arg->extra.i;
}
static int
pri_depth(struct arg *arg)
{
return 1;
}
/*
* Getargs
* consume any arguments for given primary and fill extra
* return pointer to last argument, the pointer will be incremented in parse()
*/
static char **
get_name_arg(char *argv[], union extra *extra)
{
extra->p = *argv;
return argv;
}
static char **
get_path_arg(char *argv[], union extra *extra)
{
extra->p = *argv;
return argv;
}
static char **
get_xdev_arg(char *argv[], union extra *extra)
{
gflags.xdev = 1;
return argv;
}
static char **
get_perm_arg(char *argv[], union extra *extra)
{
struct permarg *p = extra->p = emalloc(sizeof(*p));
if (**argv == '-')
(*argv)++;
else
p->exact = 1;
p->mode = parsemode(*argv, 0, 0);
return argv;
}
static char **
get_type_arg(char *argv[], union extra *extra)
{
if (!strchr("bcdlpfs", **argv))
eprintf("invalid type %c for -type primary\n", **argv);
extra->i = **argv;
return argv;
}
static char **
get_n_arg(char *argv[], union extra *extra)
{
struct narg *n = extra->p = emalloc(sizeof(*n));
fill_narg(*argv, n);
return argv;
}
static char **
get_user_arg(char *argv[], union extra *extra)
{
char *end;
struct passwd *p = getpwnam(*argv);
if (p) {
extra->i = p->pw_uid;
} else {
extra->i = strtol(*argv, &end, 10);
if (end == *argv || *end)
eprintf("unknown user '%s'\n", *argv);
}
return argv;
}
static char **
get_group_arg(char *argv[], union extra *extra)
{
char *end;
struct group *g = getgrnam(*argv);
if (g) {
extra->i = g->gr_gid;
} else {
extra->i = strtol(*argv, &end, 10);
if (end == *argv || *end)
eprintf("unknown group '%s'\n", *argv);
}
return argv;
}
static char **
get_size_arg(char *argv[], union extra *extra)
{
char *p = *argv + strlen(*argv);
struct sizearg *s = extra->p = emalloc(sizeof(*s));
/* if the number is followed by 'c', the size will by in bytes */
if ((s->bytes = (p > *argv && *--p == 'c')))
*p = '\0';
fill_narg(*argv, &s->n);
return argv;
}
static char **
get_exec_arg(char *argv[], union extra *extra)
{
char **arg, **new, ***braces;
int nbraces = 0;
struct execarg *e = extra->p = emalloc(sizeof(*e));
for (arg = argv; *arg; arg++)
if (!strcmp(*arg, ";"))
break;
else if (arg > argv && !strcmp(*(arg - 1), "{}") && !strcmp(*arg, "+"))
break;
else if (!strcmp(*arg, "{}"))
nbraces++;
if (!*arg)
eprintf("no terminating ; or {} + for -exec primary\n");
e->isplus = **arg == '+';
*arg = NULL;
if (e->isplus) {
*(arg - 1) = NULL; /* don't need the {} in there now */
e->u.p.arglen = e->u.p.filelen = 0;
e->u.p.first = e->u.p.next = arg - argv - 1;
e->u.p.cap = (arg - argv) * 2;
e->argv = ereallocarray(NULL, e->u.p.cap, sizeof(*e->argv));
for (arg = argv, new = e->argv; *arg; arg++, new++) {
*new = *arg;
e->u.p.arglen += strlen(*arg) + 1 + sizeof(*arg);
}
arg++; /* due to our extra NULL */
} else {
e->argv = argv;
e->u.s.braces = ereallocarray(NULL, ++nbraces, sizeof(*e->u.s.braces)); /* ++ for NULL */
for (arg = argv, braces = e->u.s.braces; *arg; arg++)
if (!strcmp(*arg, "{}"))
*braces++ = arg;
*braces = NULL;
}
gflags.print = 0;
return arg;
}
static char **
get_ok_arg(char *argv[], union extra *extra)
{
char **arg, ***braces;
int nbraces = 0;
struct okarg *o = extra->p = emalloc(sizeof(*o));
for (arg = argv; *arg; arg++)
if (!strcmp(*arg, ";"))
break;
else if (!strcmp(*arg, "{}"))
nbraces++;
if (!*arg)
eprintf("no terminating ; for -ok primary\n");
*arg = NULL;
o->argv = argv;
o->braces = ereallocarray(NULL, ++nbraces, sizeof(*o->braces));
for (arg = argv, braces = o->braces; *arg; arg++)
if (!strcmp(*arg, "{}"))
*braces++ = arg;
*braces = NULL;
gflags.print = 0;
return arg;
}
static char **
get_print_arg(char *argv[], union extra *extra)
{
gflags.print = 0;
return argv;
}
/* FIXME: ignoring nanoseconds */
static char **
get_newer_arg(char *argv[], union extra *extra)
{
struct stat st;
if (stat(*argv, &st))
eprintf("failed to stat '%s':", *argv);
extra->i = st.st_mtime;
return argv;
}
static char **
get_depth_arg(char *argv[], union extra *extra)
{
gflags.depth = 1;
return argv;
}
/*
* Freeargs
*/
static void
free_extra(union extra extra)
{
free(extra.p);
}
static void
free_exec_arg(union extra extra)
{
int status;
pid_t pid;
char **arg;
struct execarg *e = extra.p;
if (!e->isplus) {
free(e->u.s.braces);
} else {
e->argv[e->u.p.next] = NULL;
/* if we have files, do the last exec */
if (e->u.p.first != e->u.p.next) {
switch((pid = fork())) {
case -1:
eprintf("fork:");
case 0:
execvp(*e->argv, e->argv);
weprintf("exec %s failed:", *e->argv);
_exit(1);
}
waitpid(pid, &status, 0);
gflags.ret = gflags.ret || status;
}
for (arg = e->argv + e->u.p.first; *arg; arg++)
free(*arg);
free(e->argv);
}
free(e);
}
static void
free_ok_arg(union extra extra)
{
struct okarg *o = extra.p;
free(o->braces);
free(o);
}
/*
* Parsing/Building/Running
*/
static void
fill_narg(char *s, struct narg *n)
{
char *end;
switch (*s) {
case '+': n->cmp = cmp_gt; s++; break;
case '-': n->cmp = cmp_lt; s++; break;
default : n->cmp = cmp_eq; break;
}
n->n = strtol(s, &end, 10);
if (end == s || *end)
eprintf("bad number '%s'\n", s);
}
static struct pri_info *
find_primary(char *name)
{
struct pri_info *p;
for (p = primaries; p->name; p++)
if (!strcmp(name, p->name))
return p;
return NULL;
}
static struct op_info *
find_op(char *name)
{
struct op_info *o;
for (o = ops; o->name; o++)
if (!strcmp(name, o->name))
return o;
return NULL;
}
/* given the expression from the command line
* 1) convert arguments from strings to tok and place in an array duplicating
* the infix expression given, inserting "-a" where it was omitted
* 2) allocate an array to hold the correct number of tok, and convert from
* infix to rpn (using shunting-yard), add -a and -print if necessary
* 3) evaluate the rpn filling in left and right pointers to create an
* expression tree (tok are still all contained in the rpn array, just
* pointing at eachother)
*/
static void
parse(int argc, char **argv)
{
struct tok *tok, *rpn, *out, **top, *infix, **stack;
struct op_info *op;
struct pri_info *pri;
char **arg;
int lasttype = -1;
size_t ntok = 0;
struct tok and = { .u.oinfo = find_op("-a"), .type = AND };
gflags.print = 1;
/* convert argv to infix expression of tok, inserting in *tok */
infix = ereallocarray(NULL, 2 * argc + 1, sizeof(*infix));
for (arg = argv, tok = infix; *arg; arg++, tok++) {
pri = find_primary(*arg);
if (pri) { /* token is a primary, fill out tok and get arguments */
if (lasttype == PRIM || lasttype == RPAR) {
*tok++ = and;
ntok++;
}
if (pri->getarg) {
if (pri->narg && !*++arg)
eprintf("no argument for primary %s\n", pri->name);
arg = pri->getarg(arg, &tok->extra);
}
tok->u.pinfo = pri;
tok->type = PRIM;
} else if ((op = find_op(*arg))) { /* token is an operator */
if (lasttype == LPAR && op->type == RPAR)
eprintf("empty parens\n");
if ((lasttype == PRIM || lasttype == RPAR) && op->type == NOT) { /* need another implicit -a */
*tok++ = and;
ntok++;
}
tok->type = op->type;
tok->u.oinfo = op;
} else { /* token is neither primary nor operator, must be path in the wrong place */
eprintf("paths must precede expression: %s\n", *arg);
}
if (tok->type != LPAR && tok->type != RPAR)
ntok++; /* won't have parens in rpn */
lasttype = tok->type;
}
tok->type = END;
ntok++;
if (gflags.print && (arg != argv)) /* need to add -a -print (not just -print) */
gflags.print++;
/* use shunting-yard to convert from infix to rpn
* https://en.wikipedia.org/wiki/Shunting-yard_algorithm
* read from infix, resulting rpn ends up in rpn, next position in rpn is out
* push operators onto stack, next position in stack is top */
rpn = ereallocarray(NULL, ntok + gflags.print, sizeof(*rpn));
stack = ereallocarray(NULL, argc + gflags.print, sizeof(*stack));
for (tok = infix, out = rpn, top = stack; tok->type != END; tok++) {
switch (tok->type) {
case PRIM: *out++ = *tok; break;
case LPAR: *top++ = tok; break;
case RPAR:
while (top-- > stack && (*top)->type != LPAR)
*out++ = **top;
if (top < stack)
eprintf("extra )\n");
break;
default:
/* this expression can be simplified, but I decided copy the
* verbage from the wikipedia page in order to more clearly explain
* what's going on */
while (top-- > stack &&
(( tok->u.oinfo->lassoc && tok->u.oinfo->prec <= (*top)->u.oinfo->prec) ||
(!tok->u.oinfo->lassoc && tok->u.oinfo->prec < (*top)->u.oinfo->prec)))
*out++ = **top;
/* top now points to either an operator we didn't pop, or stack[-1]
* either way we need to increment it before using it, then
* increment again so the stack works */
top++;
*top++ = tok;
break;
}
}
while (top-- > stack) {
if ((*top)->type == LPAR)
eprintf("extra (\n");
*out++ = **top;
}
/* if there was no expression, use -print
* if there was an expression but no -print, -exec, or -ok, add -a -print
* in rpn, not infix */
if (gflags.print)
*out++ = (struct tok){ .u.pinfo = find_primary("-print"), .type = PRIM };
if (gflags.print == 2)
*out++ = and;
out->type = END;
/* rpn now holds all operators and arguments in reverse polish notation
* values are pushed onto stack, operators pop values off stack into left
* and right pointers, pushing operator node back onto stack
* could probably just do this during shunting-yard, but this is simpler
* code IMO */
for (tok = rpn, top = stack; tok->type != END; tok++) {
if (tok->type == PRIM) {
*top++ = tok;
} else {
if (top - stack < tok->u.oinfo->nargs)
eprintf("insufficient arguments for operator %s\n", tok->u.oinfo->name);
tok->right = *--top;
tok->left = tok->u.oinfo->nargs == 2 ? *--top : NULL;
*top++ = tok;
}
}
if (--top != stack)
eprintf("extra arguments\n");
toks = rpn;
root = *top;
free(infix);
free(stack);
}
/* for a primary, run and return result
* for an operator evaluate the left side of the tree, decide whether or not to
* evaluate the right based on the short-circuit boolean logic, return result
* NOTE: operator NOT has NULL left side, expression on right side
*/
static int
eval(struct tok *tok, struct arg *arg)
{
int ret;
if (!tok)
return 0;
if (tok->type == PRIM) {
arg->extra = tok->extra;
return tok->u.pinfo->func(arg);
}
ret = eval(tok->left, arg);
if ((tok->type == AND && ret) || (tok->type == OR && !ret) || tok->type == NOT)
ret = eval(tok->right, arg);
return ret ^ (tok->type == NOT);
}
/* evaluate path, if it's a directory iterate through directory entries and
* recurse
*/
static void
find(char *path, struct findhist *hist)
{
struct stat st;
DIR *dir;
struct dirent *de;
struct findhist *f, cur;
size_t namelen, pathcap = 0, len;
struct arg arg = { path, &st, { NULL } };
char *p, *pathbuf = NULL;
len = strlen(path) + 2; /* \0 and '/' */
if ((gflags.l || (gflags.h && !hist) ? stat(path, &st) : lstat(path, &st)) < 0) {
weprintf("failed to stat %s:", path);
return;
}
gflags.prune = 0;
/* don't eval now iff we will hit the eval at the bottom which means
* 1. we are a directory 2. we have -depth 3. we don't have -xdev or we are
* on same device (so most of the time we eval here) */
if (!S_ISDIR(st.st_mode) ||
!gflags.depth ||
(gflags.xdev && hist && st.st_dev != hist->dev))
eval(root, &arg);
if (!S_ISDIR(st.st_mode) ||
gflags.prune ||
(gflags.xdev && hist && st.st_dev != hist->dev))
return;
for (f = hist; f; f = f->next) {
if (f->dev == st.st_dev && f->ino == st.st_ino) {
weprintf("loop detected '%s' is '%s'\n", path, f->path);
return;
}
}
cur.next = hist;
cur.path = path;
cur.dev = st.st_dev;
cur.ino = st.st_ino;
if (!(dir = opendir(path))) {
weprintf("failed to opendir %s:", path);
/* should we just ignore this since we hit an error? */
if (gflags.depth)
eval(root, &arg);
return;
}
while (errno = 0, (de = readdir(dir))) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
namelen = strlen(de->d_name);
if (len + namelen > pathcap) {
pathcap = len + namelen;
pathbuf = erealloc(pathbuf, pathcap);
}
p = pathbuf + estrlcpy(pathbuf, path, pathcap);
if (*--p != '/')
estrlcat(pathbuf, "/", pathcap);
estrlcat(pathbuf, de->d_name, pathcap);
find(pathbuf, &cur);
}
free(pathbuf);
if (errno) {