aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

find.c (36402B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #if FEATURE_CASEFOLD
      4 #ifndef _GNU_SOURCE
      5 #define _GNU_SOURCE
      6 #endif
      7 #endif
      8 #include "config.h"
      9 #include "util.h"
     10 #include "wexec.h"
     11 
     12 #include <dirent.h>
     13 #include <errno.h>
     14 #include <fnmatch.h>
     15 #include <grp.h>
     16 #include <libgen.h>
     17 #include <pwd.h>
     18 #include <regex.h>
     19 #include <stdint.h>
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <string.h>
     23 #include <sys/stat.h>
     24 #include <sys/wait.h>
     25 #include <time.h>
     26 #include <unistd.h>
     27 
     28 /* because putting integers in pointers is undefined by the standard */
     29 union extra {
     30   void    *p;
     31   intmax_t i;
     32 };
     33 
     34 /* argument passed into a primary's function */
     35 struct arg {
     36   char        *path;
     37   struct stat *st;
     38   union extra  extra;
     39 };
     40 
     41 /* information about each primary, for lookup table */
     42 struct pri_info {
     43   char *name;
     44   int (*func)(struct arg *arg);
     45   char **(*getarg)(char **argv, union extra *extra);
     46   void (*freearg)(union extra extra);
     47   char narg; /* -xdev, -depth, -print dont take args but have getarg() */
     48 };
     49 
     50 /* information about operators, for lookup table */
     51 struct op_info {
     52   char *name;   /* string representation of op */
     53   char  type;   /* from tok.type */
     54   char  prec;   /* precedence */
     55   char  nargs;  /* number of arguments (unary or binary) */
     56   char  lassoc; /* left associative */
     57 };
     58 
     59 /* token when lexing/parsing
     60  * (although also used for the expression tree) */
     61 struct tok {
     62   struct tok *left, *right; /* if (type == NOT) left = NULL */
     63   union extra extra;
     64   union {
     65     struct pri_info *pinfo; /* if (type == PRIM) */
     66     struct op_info  *oinfo;
     67   } u;
     68   enum { PRIM = 0, LPAR, RPAR, NOT, AND, OR, END } type;
     69 };
     70 
     71 /* structures used for arg.extra.p and tok.extra.p */
     72 struct permarg {
     73   mode_t mode;
     74   char   exact;
     75 };
     76 
     77 struct okarg {
     78   char ***braces;
     79   char  **argv;
     80 };
     81 
     82 /* for all arguments that take a number
     83  * +n, n, -n mean > n, == n, < n respectively */
     84 struct narg {
     85   int (*cmp)(int a, int b);
     86   int n;
     87 };
     88 
     89 struct sizearg {
     90   struct narg n;
     91   char        bytes; /* size is in bytes, not 512 byte sectors */
     92 };
     93 
     94 struct execarg {
     95   union {
     96     struct {
     97       char ***braces; /* NULL terminated list of pointers into
     98                        * argv where {} were */
     99     } s;              /* semicolon */
    100     struct {
    101       size_t arglen;  /* number of bytes in argv before files
    102                        * are added */
    103       size_t filelen; /* numer of bytes in file names added to
    104                        * argv */
    105       size_t first;   /* index one past last arg, where first
    106                        * file goes */
    107       size_t next;    /* index where next file goes */
    108       size_t cap;     /* capacity of argv */
    109     } p;              /* plus */
    110   } u;
    111   char **argv; /* NULL terminated list of arguments (allocated if isplus)
    112                 */
    113   char isplus; /* -exec + instead of -exec ; */
    114 };
    115 
    116 /* used to find loops while recursing through directory structure */
    117 struct findhist {
    118   struct findhist *next;
    119   char            *path;
    120   dev_t            dev;
    121   ino_t            ino;
    122 };
    123 
    124 /* utility */
    125 static int spawn(char *argv[]);
    126 static int do_stat(char *path, struct stat *sb, struct findhist *hist);
    127 
    128 /* primaries */
    129 static int pri_name(struct arg *arg);
    130 #if FEATURE_FIND_INAME
    131 static int pri_iname(struct arg *arg);
    132 #endif
    133 #if FEATURE_FIND_IPATH
    134 static int pri_ipath(struct arg *arg);
    135 #endif
    136 #if FEATURE_FIND_REGEX
    137 static int    pri_regex(struct arg *arg);
    138 static char **get_regex_arg(char *argv[], union extra *extra);
    139 static void   free_regex_arg(union extra extra);
    140 #endif
    141 #if FEATURE_FIND_INUM
    142 static int    pri_inum(struct arg *arg);
    143 static char **get_inum_arg(char *argv[], union extra *extra);
    144 #endif
    145 #if FEATURE_FIND_SAMEFILE
    146 static int    pri_samefile(struct arg *arg);
    147 static char **get_samefile_arg(char *argv[], union extra *extra);
    148 #endif
    149 #if FEATURE_FIND_MAXDEPTH
    150 static int    pri_maxdepth(struct arg *arg);
    151 static char **get_maxdepth_arg(char *argv[], union extra *extra);
    152 #endif
    153 #if FEATURE_FIND_MINDEPTH
    154 static int    pri_mindepth(struct arg *arg);
    155 static char **get_mindepth_arg(char *argv[], union extra *extra);
    156 #endif
    157 #if FEATURE_FIND_DELETE
    158 static int    pri_delete(struct arg *arg);
    159 static char **get_delete_arg(char *argv[], union extra *extra);
    160 #endif
    161 #if FEATURE_FIND_QUIT
    162 static int    pri_quit(struct arg *arg);
    163 static char **get_quit_arg(char *argv[], union extra *extra);
    164 #endif
    165 #if FEATURE_FIND_EMPTY
    166 static int pri_empty(struct arg *arg);
    167 #endif
    168 #if FEATURE_FIND_MMIN
    169 static int pri_mmin(struct arg *arg);
    170 #endif
    171 #if FEATURE_FIND_AMIN
    172 static int pri_amin(struct arg *arg);
    173 #endif
    174 #if FEATURE_FIND_CMIN
    175 static int pri_cmin(struct arg *arg);
    176 #endif
    177 static int pri_path(struct arg *arg);
    178 static int pri_nouser(struct arg *arg);
    179 static int pri_nogroup(struct arg *arg);
    180 static int pri_xdev(struct arg *arg);
    181 static int pri_prune(struct arg *arg);
    182 static int pri_perm(struct arg *arg);
    183 static int pri_type(struct arg *arg);
    184 static int pri_links(struct arg *arg);
    185 static int pri_user(struct arg *arg);
    186 static int pri_group(struct arg *arg);
    187 static int pri_size(struct arg *arg);
    188 static int pri_atime(struct arg *arg);
    189 static int pri_ctime(struct arg *arg);
    190 static int pri_mtime(struct arg *arg);
    191 static int pri_exec(struct arg *arg);
    192 static int pri_ok(struct arg *arg);
    193 static int pri_print(struct arg *arg);
    194 #if FEATURE_FIND_PRINT0
    195 static int pri_print0(struct arg *arg);
    196 #endif
    197 static int pri_newer(struct arg *arg);
    198 static int pri_depth(struct arg *arg);
    199 
    200 /* getargs */
    201 static char **get_name_arg(char *argv[], union extra *extra);
    202 static char **get_path_arg(char *argv[], union extra *extra);
    203 static char **get_xdev_arg(char *argv[], union extra *extra);
    204 static char **get_perm_arg(char *argv[], union extra *extra);
    205 static char **get_type_arg(char *argv[], union extra *extra);
    206 static char **get_n_arg(char *argv[], union extra *extra);
    207 static char **get_user_arg(char *argv[], union extra *extra);
    208 static char **get_group_arg(char *argv[], union extra *extra);
    209 static char **get_size_arg(char *argv[], union extra *extra);
    210 static char **get_exec_arg(char *argv[], union extra *extra);
    211 static char **get_ok_arg(char *argv[], union extra *extra);
    212 static char **get_print_arg(char *argv[], union extra *extra);
    213 static char **get_newer_arg(char *argv[], union extra *extra);
    214 static char **get_depth_arg(char *argv[], union extra *extra);
    215 
    216 /* freeargs */
    217 static void free_extra(union extra extra);
    218 static void free_exec_arg(union extra extra);
    219 static void free_ok_arg(union extra extra);
    220 
    221 /* parsing/building/running */
    222 static void             fill_narg(char *s, struct narg *n);
    223 static struct pri_info *find_primary(char *name);
    224 static struct op_info  *find_op(char *name);
    225 static void             parse(int argc, char **argv);
    226 static int              eval(struct tok *tok, struct arg *arg);
    227 static void             find(char *path, struct findhist *hist);
    228 static void             usage(void);
    229 
    230 /* for comparisons with narg */
    231 static int
    232 cmp_gt(int a, int b)
    233 {
    234   return a > b;
    235 }
    236 static int
    237 cmp_eq(int a, int b)
    238 {
    239   return a == b;
    240 }
    241 static int
    242 cmp_lt(int a, int b)
    243 {
    244   return a < b;
    245 }
    246 
    247 /* order from find(1p), may want to alphabetize */
    248 static struct pri_info primaries[] = {
    249 #if FEATURE_FIND_INAME
    250     {"-iname", pri_iname, get_name_arg, NULL, 1},
    251 #endif
    252 #if FEATURE_FIND_IPATH
    253     {"-ipath", pri_ipath, get_path_arg, NULL, 1},
    254 #endif
    255 #if FEATURE_FIND_REGEX
    256     {"-regex", pri_regex, get_regex_arg, free_regex_arg, 1},
    257 #endif
    258 #if FEATURE_FIND_INUM
    259     {"-inum", pri_inum, get_inum_arg, NULL, 1},
    260 #endif
    261 #if FEATURE_FIND_SAMEFILE
    262     {"-samefile", pri_samefile, get_samefile_arg, free_extra, 1},
    263 #endif
    264 #if FEATURE_FIND_MAXDEPTH
    265     {"-maxdepth", pri_maxdepth, get_maxdepth_arg, NULL, 1},
    266 #endif
    267 #if FEATURE_FIND_MINDEPTH
    268     {"-mindepth", pri_mindepth, get_mindepth_arg, NULL, 1},
    269 #endif
    270 #if FEATURE_FIND_DELETE
    271     {"-delete", pri_delete, get_delete_arg, NULL, 0},
    272 #endif
    273 #if FEATURE_FIND_QUIT
    274     {"-quit", pri_quit, get_quit_arg, NULL, 0},
    275 #endif
    276 #if FEATURE_FIND_MMIN
    277     {"-mmin", pri_mmin, get_n_arg, free_extra, 1},
    278 #endif
    279 #if FEATURE_FIND_AMIN
    280     {"-amin", pri_amin, get_n_arg, free_extra, 1},
    281 #endif
    282 #if FEATURE_FIND_CMIN
    283     {"-cmin", pri_cmin, get_n_arg, free_extra, 1},
    284 #endif
    285 #if FEATURE_FIND_EMPTY
    286     {"-empty", pri_empty, NULL, NULL, 1},
    287 #endif
    288     {"-name", pri_name, get_name_arg, NULL, 1},
    289     {"-path", pri_path, get_path_arg, NULL, 1},
    290     {"-nouser", pri_nouser, NULL, NULL, 1},
    291     {"-nogroup", pri_nogroup, NULL, NULL, 1},
    292     {"-xdev", pri_xdev, get_xdev_arg, NULL, 0},
    293     {"-prune", pri_prune, NULL, NULL, 1},
    294     {"-perm", pri_perm, get_perm_arg, free_extra, 1},
    295     {"-type", pri_type, get_type_arg, NULL, 1},
    296     {"-links", pri_links, get_n_arg, free_extra, 1},
    297     {"-user", pri_user, get_user_arg, NULL, 1},
    298     {"-group", pri_group, get_group_arg, NULL, 1},
    299     {"-size", pri_size, get_size_arg, free_extra, 1},
    300     {"-atime", pri_atime, get_n_arg, free_extra, 1},
    301     {"-ctime", pri_ctime, get_n_arg, free_extra, 1},
    302     {"-mtime", pri_mtime, get_n_arg, free_extra, 1},
    303     {"-exec", pri_exec, get_exec_arg, free_exec_arg, 1},
    304     {"-ok", pri_ok, get_ok_arg, free_ok_arg, 1},
    305     {"-print", pri_print, get_print_arg, NULL, 0},
    306 #if FEATURE_FIND_PRINT0
    307     {"-print0", pri_print0, get_print_arg, NULL, 0},
    308 #endif
    309     {"-newer", pri_newer, get_newer_arg, NULL, 1},
    310     {"-depth", pri_depth, get_depth_arg, NULL, 0},
    311 
    312     {NULL, NULL, NULL, NULL, 0}
    313 };
    314 
    315 static struct op_info ops[] = {
    316     {"(", LPAR, 0, 0, 0}, /* parens are handled specially */
    317     {")", RPAR, 0, 0, 0},
    318     {"!", NOT, 3, 1, 0},
    319     {"-a", AND, 2, 2, 1},
    320     {"-o", OR, 1, 2, 1},
    321 
    322     {NULL, 0, 0, 0, 0}
    323 };
    324 
    325 extern char **environ;
    326 
    327 static struct tok *toks; /* holds allocated array of all toks created while parsing */
    328 static struct tok *root; /* points to root of expression tree, inside toks array */
    329 
    330 static struct timespec start; /* time find was started, used for -[acm]time */
    331 
    332 static size_t envlen; /* number of bytes in environ, used to calculate against
    333                        * ARG_MAX */
    334 static size_t argmax; /* value of ARG_MAX retrieved using sysconf(3p) */
    335 
    336 static struct {
    337   char ret;      /* return value from main */
    338   char depth;    /* -depth, directory contents before directory itself */
    339   char h;        /* -H, follow symlinks on command line */
    340   char l;        /* -L, follow all symlinks (command line and search) */
    341   char prune;    /* hit -prune */
    342   char xdev;     /* -xdev, prune directories on different devices */
    343   char print;    /* whether we will need -print when parsing */
    344   char quit;     /* quit execution immediately */
    345   long maxdepth; /* max depth of recursion */
    346   long mindepth; /* min depth of recursion */
    347 } gflags;
    348 
    349 /*
    350  * utility
    351  */
    352 static int
    353 spawn(char *argv[])
    354 {
    355   pid_t pid;
    356   int   status;
    357 
    358   /* flush stdout so that -print output always appears before
    359    * any output from the command and does not get cut-off in
    360    * the middle of a line */
    361   fflush(stdout);
    362 
    363   switch ((pid = fork())) {
    364     case -1:
    365       eprintf("fork:");
    366       break;
    367     case 0:
    368       wexecvp_self(*argv, argv);
    369       weprintf("exec %s failed:", *argv);
    370       _exit(1);
    371   }
    372 
    373   while (waitpid(pid, &status, 0) < 0) {
    374     if (errno != EINTR) {
    375       status = -1;
    376       break;
    377     }
    378   }
    379   return status;
    380 }
    381 
    382 static int
    383 do_stat(char *path, struct stat *sb, struct findhist *hist)
    384 {
    385   if (gflags.l || (gflags.h && !hist)) {
    386     if (stat(path, sb) == 0) {
    387       return 0;
    388     } else if (errno != ENOENT && errno != ENOTDIR) {
    389       return -1;
    390     }
    391   }
    392 
    393   return lstat(path, sb);
    394 }
    395 
    396 /*
    397  * primaries
    398  */
    399 static int
    400 pri_name(struct arg *arg)
    401 {
    402   int   ret;
    403   char *path;
    404 
    405   path = estrdup(arg->path);
    406   ret  = !fnmatch((char *)arg->extra.p, basename(path), 0);
    407   free(path);
    408 
    409   return ret;
    410 }
    411 
    412 static int
    413 pri_path(struct arg *arg)
    414 {
    415   return !fnmatch((char *)arg->extra.p, arg->path, 0);
    416 }
    417 
    418 /* FIXME: what about errors? find(1p) literally just says
    419  * "for which the getpwuid() function ... returns NULL" */
    420 static int
    421 pri_nouser(struct arg *arg)
    422 {
    423   return !getpwuid(arg->st->st_uid);
    424 }
    425 
    426 static int
    427 pri_nogroup(struct arg *arg)
    428 {
    429   return !getgrgid(arg->st->st_gid);
    430 }
    431 
    432 static int
    433 pri_xdev(struct arg *arg)
    434 {
    435   (void)arg;
    436   return 1;
    437 }
    438 
    439 static int
    440 pri_prune(struct arg *arg)
    441 {
    442   (void)arg;
    443   return gflags.prune = 1;
    444 }
    445 
    446 static int
    447 pri_perm(struct arg *arg)
    448 {
    449   struct permarg *p = (struct permarg *)arg->extra.p;
    450 
    451   return (arg->st->st_mode & 07777 & (p->exact ? -1U : p->mode)) == p->mode;
    452 }
    453 
    454 static int
    455 pri_type(struct arg *arg)
    456 {
    457   switch ((char)arg->extra.i) {
    458     default:
    459       return 0; /* impossible, but placate warnings */
    460     case 'b':
    461       return S_ISBLK(arg->st->st_mode);
    462     case 'c':
    463       return S_ISCHR(arg->st->st_mode);
    464     case 'd':
    465       return S_ISDIR(arg->st->st_mode);
    466     case 'l':
    467       return S_ISLNK(arg->st->st_mode);
    468     case 'p':
    469       return S_ISFIFO(arg->st->st_mode);
    470     case 'f':
    471       return S_ISREG(arg->st->st_mode);
    472     case 's':
    473       return S_ISSOCK(arg->st->st_mode);
    474   }
    475 }
    476 
    477 static int
    478 pri_links(struct arg *arg)
    479 {
    480   struct narg *n = arg->extra.p;
    481   return n->cmp(arg->st->st_nlink, n->n);
    482 }
    483 
    484 static int
    485 pri_user(struct arg *arg)
    486 {
    487   return arg->st->st_uid == (uid_t)arg->extra.i;
    488 }
    489 
    490 static int
    491 pri_group(struct arg *arg)
    492 {
    493   return arg->st->st_gid == (gid_t)arg->extra.i;
    494 }
    495 
    496 static int
    497 pri_size(struct arg *arg)
    498 {
    499   struct sizearg *s    = arg->extra.p;
    500   off_t           size = arg->st->st_size;
    501 
    502   if (!s->bytes)
    503     size = size / 512 + !!(size % 512);
    504 
    505   return s->n.cmp(size, s->n.n);
    506 }
    507 
    508 /* FIXME: ignoring nanoseconds in atime, ctime, mtime */
    509 static int
    510 pri_atime(struct arg *arg)
    511 {
    512   struct narg *n = arg->extra.p;
    513   return n->cmp((start.tv_sec - arg->st->st_atime) / 86400, n->n);
    514 }
    515 
    516 static int
    517 pri_ctime(struct arg *arg)
    518 {
    519   struct narg *n = arg->extra.p;
    520   return n->cmp((start.tv_sec - arg->st->st_ctime) / 86400, n->n);
    521 }
    522 
    523 static int
    524 pri_mtime(struct arg *arg)
    525 {
    526   struct narg *n = arg->extra.p;
    527   return n->cmp((start.tv_sec - arg->st->st_mtime) / 86400, n->n);
    528 }
    529 
    530 static int
    531 pri_exec(struct arg *arg)
    532 {
    533   int             status;
    534   size_t          len;
    535   char          **sp, ***brace;
    536   struct execarg *e = arg->extra.p;
    537 
    538   if (e->isplus) {
    539     len = strlen(arg->path) + 1;
    540 
    541     /* if we reached ARG_MAX, fork, exec, wait, free file names,
    542      * reset list */
    543     if (len + e->u.p.arglen + e->u.p.filelen + envlen > argmax) {
    544       e->argv[e->u.p.next] = NULL;
    545 
    546       status     = spawn(e->argv);
    547       gflags.ret = gflags.ret || status;
    548 
    549       for (sp = e->argv + e->u.p.first; *sp; sp++)
    550         free(*sp);
    551 
    552       e->u.p.next    = e->u.p.first;
    553       e->u.p.filelen = 0;
    554     }
    555 
    556     /* if we have too many files, realloc (with space for NULL
    557      * termination) */
    558     if (e->u.p.next + 1 == e->u.p.cap)
    559       e->argv = ereallocarray(e->argv, e->u.p.cap *= 2, sizeof(*e->argv));
    560 
    561     e->argv[e->u.p.next++] = estrdup(arg->path);
    562     e->u.p.filelen += len + sizeof(arg->path);
    563 
    564     return 1;
    565   } else {
    566     /* insert path everywhere user gave us {} */
    567     for (brace = e->u.s.braces; *brace; brace++)
    568       **brace = arg->path;
    569 
    570     status = spawn(e->argv);
    571     return !status;
    572   }
    573 }
    574 
    575 static int
    576 pri_ok(struct arg *arg)
    577 {
    578   int           status, reply;
    579   char       ***brace, c;
    580   struct okarg *o = arg->extra.p;
    581 
    582   fprintf(stderr, "%s: %s ? ", *o->argv, arg->path);
    583   reply = fgetc(stdin);
    584 
    585   /* throw away rest of line */
    586   while ((c = fgetc(stdin)) != '\n' && c != EOF)
    587     /* FIXME: what if the first character of the rest of the line is
    588      * a null byte? */
    589     ;
    590 
    591   if (feof(stdin) || ferror(stdin))
    592     clearerr(stdin);
    593 
    594   if (reply != 'y' && reply != 'Y')
    595     return 0;
    596 
    597   /* insert filename everywhere user gave us {} */
    598   for (brace = o->braces; *brace; brace++)
    599     **brace = arg->path;
    600 
    601   status = spawn(o->argv);
    602   return !!status;
    603 }
    604 
    605 static int
    606 pri_print(struct arg *arg)
    607 {
    608   if (puts(arg->path) == EOF)
    609     eprintf("puts failed:");
    610   return 1;
    611 }
    612 
    613 #if FEATURE_FIND_PRINT0
    614 static int
    615 pri_print0(struct arg *arg)
    616 {
    617   if (fwrite(arg->path, strlen(arg->path) + 1, 1, stdout) != 1)
    618     eprintf("fwrite failed:");
    619   return 1;
    620 }
    621 #endif
    622 
    623 /* FIXME: ignoring nanoseconds */
    624 static int
    625 pri_newer(struct arg *arg)
    626 {
    627   return arg->st->st_mtime > (time_t)arg->extra.i;
    628 }
    629 
    630 static int
    631 pri_depth(struct arg *arg)
    632 {
    633   (void)arg;
    634   return 1;
    635 }
    636 
    637 /*
    638  * getargs
    639  * consume any arguments for given primary and fill extra
    640  * return pointer to last argument, the pointer will be incremented in parse()
    641  */
    642 static char **
    643 get_name_arg(char *argv[], union extra *extra)
    644 {
    645   extra->p = *argv;
    646   return argv;
    647 }
    648 
    649 static char **
    650 get_path_arg(char *argv[], union extra *extra)
    651 {
    652   extra->p = *argv;
    653   return argv;
    654 }
    655 
    656 static char **
    657 get_xdev_arg(char *argv[], union extra *extra)
    658 {
    659   (void)extra;
    660   gflags.xdev = 1;
    661   return argv;
    662 }
    663 
    664 static char **
    665 get_perm_arg(char *argv[], union extra *extra)
    666 {
    667   mode_t          mask;
    668   struct permarg *p = extra->p = emalloc(sizeof(*p));
    669 
    670   if (**argv == '-')
    671     (*argv)++;
    672   else
    673     p->exact = 1;
    674 
    675   mask = umask(0);
    676   umask(mask);
    677 
    678   p->mode = parsemode(*argv, 0, mask);
    679 
    680   return argv;
    681 }
    682 
    683 static char **
    684 get_type_arg(char *argv[], union extra *extra)
    685 {
    686   if (!strchr("bcdlpfs", **argv))
    687     eprintf("invalid type %c for -type primary\n", **argv);
    688 
    689   extra->i = **argv;
    690   return argv;
    691 }
    692 
    693 static char **
    694 get_n_arg(char *argv[], union extra *extra)
    695 {
    696   struct narg *n = extra->p = emalloc(sizeof(*n));
    697   fill_narg(*argv, n);
    698   return argv;
    699 }
    700 
    701 static char **
    702 get_user_arg(char *argv[], union extra *extra)
    703 {
    704   char          *end;
    705   struct passwd *p = getpwnam(*argv);
    706 
    707   if (p) {
    708     extra->i = p->pw_uid;
    709   } else {
    710     extra->i = strtol(*argv, &end, 10);
    711     if (end == *argv || *end)
    712       eprintf("unknown user '%s'\n", *argv);
    713   }
    714   return argv;
    715 }
    716 
    717 static char **
    718 get_group_arg(char *argv[], union extra *extra)
    719 {
    720   char         *end;
    721   struct group *g = getgrnam(*argv);
    722 
    723   if (g) {
    724     extra->i = g->gr_gid;
    725   } else {
    726     extra->i = strtol(*argv, &end, 10);
    727     if (end == *argv || *end)
    728       eprintf("unknown group '%s'\n", *argv);
    729   }
    730   return argv;
    731 }
    732 
    733 static char **
    734 get_size_arg(char *argv[], union extra *extra)
    735 {
    736   char           *p = *argv + strlen(*argv);
    737   struct sizearg *s = extra->p = emalloc(sizeof(*s));
    738   /* if the number is followed by 'c', the size will by in bytes */
    739   if ((s->bytes = (p > *argv && *--p == 'c')))
    740     *p = '\0';
    741 
    742   fill_narg(*argv, &s->n);
    743   return argv;
    744 }
    745 
    746 static char **
    747 get_exec_arg(char *argv[], union extra *extra)
    748 {
    749   char          **arg, **new, ***braces;
    750   int             nbraces = 0;
    751   struct execarg *e = extra->p = emalloc(sizeof(*e));
    752 
    753   for (arg = argv; *arg; arg++)
    754     if (!strcmp(*arg, ";"))
    755       break;
    756     else if (arg > argv && !strcmp(*(arg - 1), "{}") && !strcmp(*arg, "+"))
    757       break;
    758     else if (!strcmp(*arg, "{}"))
    759       nbraces++;
    760 
    761   if (!*arg)
    762     eprintf("no terminating ; or {} + for -exec primary\n");
    763 
    764   e->isplus = **arg == '+';
    765   *arg      = NULL;
    766 
    767   if (e->isplus) {
    768     *(arg - 1)    = NULL; /* dont need the {} in there now */
    769     e->u.p.arglen = e->u.p.filelen = 0;
    770     e->u.p.first = e->u.p.next = arg - argv - 1;
    771     e->u.p.cap                 = (arg - argv) * 2;
    772     e->argv                    = ereallocarray(NULL, e->u.p.cap, sizeof(*e->argv));
    773 
    774     for (arg = argv, new = e->argv; *arg; arg++, new ++) {
    775       *new = *arg;
    776       e->u.p.arglen += strlen(*arg) + 1 + sizeof(*arg);
    777     }
    778     arg++; /* due to our extra NULL */
    779   } else {
    780     e->argv       = argv;
    781     e->u.s.braces = ereallocarray(NULL, ++nbraces, sizeof(*e->u.s.braces)); /* ++ for NULL */
    782 
    783     for (arg = argv, braces = e->u.s.braces; *arg; arg++)
    784       if (!strcmp(*arg, "{}"))
    785         *braces++ = arg;
    786     *braces = NULL;
    787   }
    788   gflags.print = 0;
    789   return arg;
    790 }
    791 
    792 static char **
    793 get_ok_arg(char *argv[], union extra *extra)
    794 {
    795   char        **arg, ***braces;
    796   int           nbraces = 0;
    797   struct okarg *o = extra->p = emalloc(sizeof(*o));
    798 
    799   for (arg = argv; *arg; arg++)
    800     if (!strcmp(*arg, ";"))
    801       break;
    802     else if (!strcmp(*arg, "{}"))
    803       nbraces++;
    804 
    805   if (!*arg)
    806     eprintf("no terminating ; for -ok primary\n");
    807   *arg = NULL;
    808 
    809   o->argv   = argv;
    810   o->braces = ereallocarray(NULL, ++nbraces, sizeof(*o->braces));
    811 
    812   for (arg = argv, braces = o->braces; *arg; arg++)
    813     if (!strcmp(*arg, "{}"))
    814       *braces++ = arg;
    815   *braces = NULL;
    816 
    817   gflags.print = 0;
    818   return arg;
    819 }
    820 
    821 static char **
    822 get_print_arg(char *argv[], union extra *extra)
    823 {
    824   (void)extra;
    825   gflags.print = 0;
    826   return argv;
    827 }
    828 
    829 /* FIXME: ignoring nanoseconds */
    830 static char **
    831 get_newer_arg(char *argv[], union extra *extra)
    832 {
    833   struct stat st;
    834 
    835   if (do_stat(*argv, &st, NULL))
    836     eprintf("failed to stat '%s':", *argv);
    837 
    838   extra->i = st.st_mtime;
    839   return argv;
    840 }
    841 
    842 static char **
    843 get_depth_arg(char *argv[], union extra *extra)
    844 {
    845   (void)extra;
    846   gflags.depth = 1;
    847   return argv;
    848 }
    849 
    850 /*
    851  * freeargs
    852  */
    853 static void
    854 free_extra(union extra extra)
    855 {
    856   free(extra.p);
    857 }
    858 
    859 static void
    860 free_exec_arg(union extra extra)
    861 {
    862   int             status;
    863   char          **arg;
    864   struct execarg *e = extra.p;
    865 
    866   if (!e->isplus) {
    867     free(e->u.s.braces);
    868   } else {
    869     e->argv[e->u.p.next] = NULL;
    870 
    871     /* if we have files, do the last exec */
    872     if (e->u.p.first != e->u.p.next) {
    873       status     = spawn(e->argv);
    874       gflags.ret = gflags.ret || status;
    875     }
    876     for (arg = e->argv + e->u.p.first; *arg; arg++)
    877       free(*arg);
    878     free(e->argv);
    879   }
    880   free(e);
    881 }
    882 
    883 static void
    884 free_ok_arg(union extra extra)
    885 {
    886   struct okarg *o = extra.p;
    887 
    888   free(o->braces);
    889   free(o);
    890 }
    891 
    892 /*
    893  * parsing/building/running
    894  */
    895 static void
    896 fill_narg(char *s, struct narg *n)
    897 {
    898   char *end;
    899 
    900   switch (*s) {
    901     case '+':
    902       n->cmp = cmp_gt;
    903       s++;
    904       break;
    905     case '-':
    906       n->cmp = cmp_lt;
    907       s++;
    908       break;
    909     default:
    910       n->cmp = cmp_eq;
    911       break;
    912   }
    913   n->n = strtol(s, &end, 10);
    914   if (end == s || *end)
    915     eprintf("bad number '%s'\n", s);
    916 }
    917 
    918 static struct pri_info *
    919 find_primary(char *name)
    920 {
    921   struct pri_info *p;
    922 
    923   for (p = primaries; p->name; p++)
    924     if (!strcmp(name, p->name))
    925       return p;
    926   return NULL;
    927 }
    928 
    929 static struct op_info *
    930 find_op(char *name)
    931 {
    932   struct op_info *o;
    933 
    934   for (o = ops; o->name; o++)
    935     if (!strcmp(name, o->name))
    936       return o;
    937   return NULL;
    938 }
    939 
    940 /* given the expression from the command line
    941  * 1) convert arguments from strings to tok and place in an array duplicating
    942  * the infix expression given, inserting "-a" where it was omitted
    943  * 2) allocate an array to hold the correct number of tok, and convert from
    944  * infix to rpn (using shunting-yard), add -a and -print if necessary
    945  * 3) evaluate the rpn filling in left and right pointers to create an
    946  * expression tree (tok are still all contained in the rpn array, just
    947  * pointing at eachother)
    948  */
    949 static void
    950 parse(int argc, char **argv)
    951 {
    952   struct tok      *tok, *rpn, *out, **top, *infix, **stack;
    953   struct op_info  *op;
    954   struct pri_info *pri;
    955   char           **arg;
    956   int              lasttype = -1;
    957   size_t           ntok     = 0;
    958   struct tok and            = {.u.oinfo = find_op("-a"), .type = AND};
    959 
    960   gflags.print = 1;
    961 
    962   /* convert argv to infix expression of tok, inserting in *tok */
    963   infix = ereallocarray(NULL, 2 * argc + 1, sizeof(*infix));
    964   for (arg = argv, tok = infix; *arg; arg++, tok++) {
    965     pri = find_primary(*arg);
    966 
    967     if (pri) { /* token is a primary, fill out tok and get arguments
    968                 */
    969       if (lasttype == PRIM || lasttype == RPAR) {
    970         *tok++ = and;
    971         ntok++;
    972       }
    973       if (pri->getarg) {
    974         if (pri->narg && !*++arg)
    975           eprintf("no argument for primary %s\n", pri->name);
    976         arg = pri->getarg(arg, &tok->extra);
    977       }
    978       tok->u.pinfo = pri;
    979       tok->type    = PRIM;
    980     } else if ((op = find_op(*arg))) { /* token is an operator */
    981       if (lasttype == LPAR && op->type == RPAR)
    982         eprintf("empty parens\n");
    983       if ((lasttype == PRIM || lasttype == RPAR)
    984           && (op->type == NOT || op->type == LPAR)) { /* need another implicit -a */
    985         *tok++ = and;
    986         ntok++;
    987       }
    988       tok->type    = op->type;
    989       tok->u.oinfo = op;
    990     } else {
    991       /* token is neither primary nor operator, must be */
    992       if ((*arg)[0] == '-') /* an unsupported option */
    993         eprintf("unknown operand: %s\n", *arg);
    994       else /* or a path in the wrong place */
    995         eprintf("paths must precede expression: %s\n", *arg);
    996     }
    997     if (tok->type != LPAR && tok->type != RPAR)
    998       ntok++; /* wont have parens in rpn */
    999     lasttype = tok->type;
   1000   }
   1001   tok->type = END;
   1002   ntok++;
   1003 
   1004   if (gflags.print && (arg != argv)) /* need to add -a -print (not just -print) */
   1005     gflags.print++;
   1006 
   1007   /* use shunting-yard to convert from infix to rpn
   1008    * https:// en.wikipedia.org/wiki/shunting-yard_algorithm
   1009    * read from infix, resulting rpn ends up in rpn, next position in rpn
   1010    * is out push operators onto stack, next position in stack is top */
   1011   rpn   = ereallocarray(NULL, ntok + gflags.print, sizeof(*rpn));
   1012   stack = ereallocarray(NULL, argc + gflags.print, sizeof(*stack));
   1013   for (tok = infix, out = rpn, top = stack; tok->type != END; tok++) {
   1014     switch (tok->type) {
   1015       case PRIM:
   1016         *out++ = *tok;
   1017         break;
   1018       case LPAR:
   1019         *top++ = tok;
   1020         break;
   1021       case RPAR:
   1022         while (top-- > stack && (*top)->type != LPAR)
   1023           *out++ = **top;
   1024         if (top < stack)
   1025           eprintf("extra )\n");
   1026         break;
   1027       default:
   1028         /* this expression can be simplified, but i decided copy
   1029          * the verbage from the wikipedia page in order to more
   1030          * clearly explain whats going on */
   1031         while (top-- > stack
   1032                && ((tok->u.oinfo->lassoc && tok->u.oinfo->prec <= (*top)->u.oinfo->prec)
   1033                    || (!tok->u.oinfo->lassoc && tok->u.oinfo->prec < (*top)->u.oinfo->prec)))
   1034           *out++ = **top;
   1035 
   1036         /* top now points to either an operator we didnt pop,
   1037          * or stack[-1] either way we need to increment it
   1038          * before using it, then increment again so the stack
   1039          * works */
   1040         top++;
   1041         *top++ = tok;
   1042         break;
   1043     }
   1044   }
   1045   while (top-- > stack) {
   1046     if ((*top)->type == LPAR)
   1047       eprintf("extra (\n");
   1048     *out++ = **top;
   1049   }
   1050 
   1051   /* if there was no expression, use -print
   1052    * if there was an expression but no -print, -exec, or -ok, add -a
   1053    * -print in rpn, not infix */
   1054   if (gflags.print)
   1055     *out++ = (struct tok){.u.pinfo = find_primary("-print"), .type = PRIM};
   1056   if (gflags.print == 2)
   1057     *out++ = and;
   1058 
   1059   out->type = END;
   1060 
   1061   /* rpn now holds all operators and arguments in reverse polish notation
   1062    * values are pushed onto stack, operators pop values off stack into
   1063    * left and right pointers, pushing operator node back onto stack could
   1064    * probably just do this during shunting-yard, but this is simpler code
   1065    * IMO */
   1066   for (tok = rpn, top = stack; tok->type != END; tok++) {
   1067     if (tok->type == PRIM) {
   1068       *top++ = tok;
   1069     } else {
   1070       if (top - stack < tok->u.oinfo->nargs)
   1071         eprintf(
   1072             "insufficient arguments for operator "
   1073             "%s\n",
   1074             tok->u.oinfo->name
   1075         );
   1076       tok->right = *--top;
   1077       tok->left  = tok->u.oinfo->nargs == 2 ? *--top : NULL;
   1078       *top++     = tok;
   1079     }
   1080   }
   1081   if (--top != stack)
   1082     eprintf("extra arguments\n");
   1083 
   1084   toks = rpn;
   1085   root = *top;
   1086 
   1087   free(infix);
   1088   free(stack);
   1089 }
   1090 
   1091 /* for a primary, run and return result
   1092  * for an operator evaluate the left side of the tree, decide whether or not to
   1093  * evaluate the right based on the short-circuit boolean logic, return result
   1094  * NOTE: operator NOT has NULL left side, expression on right side
   1095  */
   1096 static int
   1097 eval(struct tok *tok, struct arg *arg)
   1098 {
   1099   int ret;
   1100 
   1101   if (!tok)
   1102     return 0;
   1103 
   1104   if (tok->type == PRIM) {
   1105     arg->extra = tok->extra;
   1106     return tok->u.pinfo->func(arg);
   1107   }
   1108 
   1109   ret = eval(tok->left, arg);
   1110 
   1111   if ((tok->type == AND && ret) || (tok->type == OR && !ret) || tok->type == NOT)
   1112     ret = eval(tok->right, arg);
   1113 
   1114   return ret ^ (tok->type == NOT);
   1115 }
   1116 
   1117 /* evaluate path, if its a directory iterate through directory entries and
   1118  * recurse
   1119  */
   1120 #if FEATURE_FIND_INAME
   1121 static int
   1122 pri_iname(struct arg *arg)
   1123 {
   1124   int   ret;
   1125   char *path;
   1126 
   1127   path = estrdup(arg->path);
   1128   ret  = !fnmatch((char *)arg->extra.p, basename(path), FNM_CASEFOLD);
   1129   free(path);
   1130 
   1131   return ret;
   1132 }
   1133 #endif
   1134 
   1135 #if FEATURE_FIND_IPATH
   1136 static int
   1137 pri_ipath(struct arg *arg)
   1138 {
   1139   return !fnmatch((char *)arg->extra.p, arg->path, FNM_CASEFOLD);
   1140 }
   1141 #endif
   1142 
   1143 #if FEATURE_FIND_REGEX
   1144 static int
   1145 pri_regex(struct arg *arg)
   1146 {
   1147   regex_t   *re = arg->extra.p;
   1148   regmatch_t match;
   1149   if (regexec(re, arg->path, 1, &match, 0) == 0) {
   1150     return match.rm_so == 0 && (size_t)match.rm_eo == strlen(arg->path);
   1151   }
   1152   return 0;
   1153 }
   1154 
   1155 static char **
   1156 get_regex_arg(char *argv[], union extra *extra)
   1157 {
   1158   regex_t *re = emalloc(sizeof(*re));
   1159   eregcomp(re, *argv, 0);
   1160   extra->p = re;
   1161   return argv;
   1162 }
   1163 
   1164 static void
   1165 free_regex_arg(union extra extra)
   1166 {
   1167   regex_t *re = extra.p;
   1168   regfree(re);
   1169   free(re);
   1170 }
   1171 #endif
   1172 
   1173 #if FEATURE_FIND_INUM
   1174 static int
   1175 pri_inum(struct arg *arg)
   1176 {
   1177   ino_t ino = (ino_t)arg->extra.i;
   1178   return arg->st->st_ino == ino;
   1179 }
   1180 
   1181 static char **
   1182 get_inum_arg(char *argv[], union extra *extra)
   1183 {
   1184   char *end;
   1185   extra->i = strtol(*argv, &end, 10);
   1186   if (end == *argv || *end)
   1187     eprintf("bad number '%s'\n", *argv);
   1188   return argv;
   1189 }
   1190 #endif
   1191 
   1192 #if FEATURE_FIND_SAMEFILE
   1193 struct SameFileArg {
   1194   ino_t ino;
   1195   dev_t dev;
   1196 };
   1197 
   1198 static int
   1199 pri_samefile(struct arg *arg)
   1200 {
   1201   struct SameFileArg *s = arg->extra.p;
   1202   return arg->st->st_ino == s->ino && arg->st->st_dev == s->dev;
   1203 }
   1204 
   1205 static char **
   1206 get_samefile_arg(char *argv[], union extra *extra)
   1207 {
   1208   struct stat         st;
   1209   struct SameFileArg *s = emalloc(sizeof(*s));
   1210   if (do_stat(*argv, &st, NULL))
   1211     eprintf("failed to stat '%s':", *argv);
   1212   s->ino   = st.st_ino;
   1213   s->dev   = st.st_dev;
   1214   extra->p = s;
   1215   return argv;
   1216 }
   1217 #endif
   1218 
   1219 #if FEATURE_FIND_MAXDEPTH
   1220 static int
   1221 pri_maxdepth(struct arg *arg)
   1222 {
   1223   (void)arg;
   1224   return 1;
   1225 }
   1226 
   1227 static char **
   1228 get_maxdepth_arg(char *argv[], union extra *extra)
   1229 {
   1230   (void)extra;
   1231   char *end;
   1232   gflags.maxdepth = strtol(*argv, &end, 10);
   1233   if (end == *argv || *end || gflags.maxdepth < 0)
   1234     eprintf("bad number '%s'\n", *argv);
   1235   return argv;
   1236 }
   1237 #endif
   1238 
   1239 #if FEATURE_FIND_MINDEPTH
   1240 static int
   1241 pri_mindepth(struct arg *arg)
   1242 {
   1243   (void)arg;
   1244   return 1;
   1245 }
   1246 
   1247 static char **
   1248 get_mindepth_arg(char *argv[], union extra *extra)
   1249 {
   1250   (void)extra;
   1251   char *end;
   1252   gflags.mindepth = strtol(*argv, &end, 10);
   1253   if (end == *argv || *end || gflags.mindepth < 0)
   1254     eprintf("bad number '%s'\n", *argv);
   1255   return argv;
   1256 }
   1257 #endif
   1258 
   1259 #if FEATURE_FIND_DELETE
   1260 static int
   1261 pri_delete(struct arg *arg)
   1262 {
   1263   if (remove(arg->path) < 0) {
   1264     weprintf("remove %s failed:", arg->path);
   1265     gflags.ret = 1;
   1266     return 0;
   1267   }
   1268   return 1;
   1269 }
   1270 
   1271 static char **
   1272 get_delete_arg(char *argv[], union extra *extra)
   1273 {
   1274   (void)extra;
   1275   gflags.depth = 1;
   1276   gflags.print = 0;
   1277   return argv;
   1278 }
   1279 #endif
   1280 
   1281 #if FEATURE_FIND_QUIT
   1282 static int
   1283 pri_quit(struct arg *arg)
   1284 {
   1285   (void)arg;
   1286   gflags.quit = 1;
   1287   return 1;
   1288 }
   1289 
   1290 static char **
   1291 get_quit_arg(char *argv[], union extra *extra)
   1292 {
   1293   (void)extra;
   1294   gflags.print = 0;
   1295   return argv;
   1296 }
   1297 #endif
   1298 
   1299 #if FEATURE_FIND_EMPTY
   1300 static int
   1301 pri_empty(struct arg *arg)
   1302 {
   1303   DIR           *dir;
   1304   struct dirent *de;
   1305   int            empty = 1;
   1306 
   1307   if (S_ISREG(arg->st->st_mode)) {
   1308     return arg->st->st_size == 0;
   1309   } else if (S_ISDIR(arg->st->st_mode)) {
   1310     dir = opendir(arg->path);
   1311     if (!dir)
   1312       return 0;
   1313     while ((de = readdir(dir))) {
   1314       if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
   1315         empty = 0;
   1316         break;
   1317       }
   1318     }
   1319     closedir(dir);
   1320     return empty;
   1321   }
   1322   return 0;
   1323 }
   1324 #endif
   1325 
   1326 #if FEATURE_FIND_MMIN
   1327 static int
   1328 pri_mmin(struct arg *arg)
   1329 {
   1330   struct narg *n = arg->extra.p;
   1331   return n->cmp((start.tv_sec - arg->st->st_mtime) / 60, n->n);
   1332 }
   1333 #endif
   1334 
   1335 #if FEATURE_FIND_AMIN
   1336 static int
   1337 pri_amin(struct arg *arg)
   1338 {
   1339   struct narg *n = arg->extra.p;
   1340   return n->cmp((start.tv_sec - arg->st->st_atime) / 60, n->n);
   1341 }
   1342 #endif
   1343 
   1344 #if FEATURE_FIND_CMIN
   1345 static int
   1346 pri_cmin(struct arg *arg)
   1347 {
   1348   struct narg *n = arg->extra.p;
   1349   return n->cmp((start.tv_sec - arg->st->st_ctime) / 60, n->n);
   1350 }
   1351 #endif
   1352 
   1353 static int
   1354 get_depth(struct findhist *hist)
   1355 {
   1356   int d = 0;
   1357   while (hist) {
   1358     d++;
   1359     hist = hist->next;
   1360   }
   1361   return d;
   1362 }
   1363 
   1364 static void
   1365 find(char *path, struct findhist *hist)
   1366 {
   1367   struct stat      st;
   1368   DIR             *dir;
   1369   struct dirent   *de;
   1370   struct findhist *f, cur;
   1371   size_t           namelen, pathcap = 0, len;
   1372   struct arg       arg = {path, &st, {NULL}};
   1373   char            *p, *pathbuf = NULL;
   1374   int              depth = get_depth(hist);
   1375 
   1376   if (gflags.quit)
   1377     return;
   1378 
   1379   len = strlen(path) + 2; /* \0 and '/' */
   1380 
   1381   if (do_stat(path, &st, hist) < 0) {
   1382     weprintf("failed to stat %s:", path);
   1383     gflags.ret = 1;
   1384     return;
   1385   }
   1386 
   1387   gflags.prune = 0;
   1388 
   1389   if (gflags.maxdepth >= 0 && depth > gflags.maxdepth)
   1390     return;
   1391 
   1392   if (gflags.mindepth < 0 || depth >= gflags.mindepth) {
   1393     /* dont eval now iff we will hit the eval at the bottom which
   1394      * means
   1395      * 1. we are a directory 2. we have -depth 3. we dont have
   1396      * -xdev or we are on same device (so most of the time we eval
   1397      * here) */
   1398     if (!S_ISDIR(st.st_mode) || !gflags.depth || (gflags.xdev && hist && st.st_dev != hist->dev))
   1399       eval(root, &arg);
   1400   }
   1401 
   1402   if (gflags.maxdepth >= 0 && depth >= gflags.maxdepth) {
   1403     if (gflags.depth && (gflags.mindepth < 0 || depth >= gflags.mindepth)) {
   1404       if (S_ISDIR(st.st_mode) && (!gflags.xdev || !hist || st.st_dev == hist->dev))
   1405         eval(root, &arg);
   1406     }
   1407     return;
   1408   }
   1409 
   1410   if (!S_ISDIR(st.st_mode) || gflags.prune || (gflags.xdev && hist && st.st_dev != hist->dev))
   1411     return;
   1412 
   1413   for (f = hist; f; f = f->next) {
   1414     if (f->dev == st.st_dev && f->ino == st.st_ino) {
   1415       weprintf("loop detected '%s' is '%s'\n", path, f->path);
   1416       gflags.ret = 1;
   1417       return;
   1418     }
   1419   }
   1420   cur.next = hist;
   1421   cur.path = path;
   1422   cur.dev  = st.st_dev;
   1423   cur.ino  = st.st_ino;
   1424 
   1425   if (!(dir = opendir(path))) {
   1426     weprintf("failed to opendir %s:", path);
   1427     gflags.ret = 1;
   1428     /* should we just ignore this since we hit an error? */
   1429     if (gflags.depth && (gflags.mindepth < 0 || depth >= gflags.mindepth))
   1430       eval(root, &arg);
   1431     return;
   1432   }
   1433 
   1434   while (errno = 0, (de = readdir(dir))) {
   1435     if (gflags.quit)
   1436       break;
   1437     if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
   1438       continue;
   1439     namelen = strlen(de->d_name);
   1440     if (len + namelen > pathcap) {
   1441       pathcap = len + namelen;
   1442       pathbuf = erealloc(pathbuf, pathcap);
   1443     }
   1444     p = pathbuf + estrlcpy(pathbuf, path, pathcap);
   1445     if (*--p != '/')
   1446       estrlcat(pathbuf, "/", pathcap);
   1447     estrlcat(pathbuf, de->d_name, pathcap);
   1448     find(pathbuf, &cur);
   1449   }
   1450   free(pathbuf);
   1451   if (errno) {
   1452     weprintf("readdir %s:", path);
   1453     gflags.ret = 1;
   1454     closedir(dir);
   1455     return;
   1456   }
   1457   closedir(dir);
   1458 
   1459   if (gflags.depth && (gflags.mindepth < 0 || depth >= gflags.mindepth))
   1460     eval(root, &arg);
   1461 }
   1462 
   1463 static void
   1464 usage(void)
   1465 {
   1466   eprintf("usage: %s [-H | -L] path ... [expression ...]\n", argv0);
   1467 }
   1468 
   1469 // ?man find: search for files
   1470 // ?man arguments: path ... [expression ...]
   1471 // ?man search for files in a directory hierarchy
   1472 int
   1473 main(int argc, char **argv)
   1474 {
   1475   char      **paths;
   1476   int         npaths;
   1477   struct tok *t;
   1478 
   1479   gflags.maxdepth = -1;
   1480   gflags.mindepth = -1;
   1481 
   1482   ARGBEGIN
   1483   {
   1484     // ?man -H: specify option flag
   1485     case 'H':
   1486       gflags.h = 1;
   1487       gflags.l = 0;
   1488       break;
   1489     // ?man -L: specify option flag
   1490     case 'L':
   1491       gflags.l = 1;
   1492       gflags.h = 0;
   1493       break;
   1494     default:
   1495       usage();
   1496   }
   1497   ARGEND
   1498 
   1499   paths = argv;
   1500 
   1501   for (; *argv && **argv != '-' && strcmp(*argv, "!") && strcmp(*argv, "("); argv++)
   1502     ;
   1503 
   1504   if (!(npaths = argv - paths))
   1505     eprintf("must specify a path\n");
   1506 
   1507   parse(argc - npaths, argv);
   1508 
   1509   /* calculate number of bytes in environ for -exec {} + ARG_MAX avoidance
   1510    * libc implementation defined whether null bytes, pointers, and
   1511    * alignment are counted, so count them */
   1512   for (argv = environ; *argv; argv++)
   1513     envlen += strlen(*argv) + 1 + sizeof(*argv);
   1514 
   1515   if ((argmax = sysconf(_SC_ARG_MAX)) == (size_t)-1)
   1516     argmax = _POSIX_ARG_MAX;
   1517 
   1518   if (clock_gettime(CLOCK_REALTIME, &start) < 0)
   1519     weprintf("clock_gettime() failed:");
   1520 
   1521   while (npaths--)
   1522     find(*paths++, NULL);
   1523 
   1524   for (t = toks; t->type != END; t++)
   1525     if (t->type == PRIM && t->u.pinfo->freearg)
   1526       t->u.pinfo->freearg(t->extra);
   1527   free(toks);
   1528 
   1529   gflags.ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
   1530 
   1531   return gflags.ret;
   1532 }