2015-02-20 01:47:21 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
2016-03-30 01:04:45 +00:00
|
|
|
#include <errno.h>
|
2015-02-20 01:47:21 +00:00
|
|
|
#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 */
|
2015-03-17 17:59:16 +00:00
|
|
|
union extra {
|
2015-02-20 01:47:21 +00:00
|
|
|
void *p;
|
2015-04-14 15:40:31 +00:00
|
|
|
intmax_t i;
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* Argument passed into a primary's function */
|
2015-03-17 17:59:16 +00:00
|
|
|
struct arg {
|
2015-02-20 01:47:21 +00:00
|
|
|
char *path;
|
|
|
|
struct stat *st;
|
2015-03-17 17:59:16 +00:00
|
|
|
union extra extra;
|
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* Information about each primary, for lookup table */
|
2015-03-17 17:59:16 +00:00
|
|
|
struct pri_info {
|
2015-02-20 01:47:21 +00:00
|
|
|
char *name;
|
2015-03-17 17:59:16 +00:00
|
|
|
int (*func)(struct arg *arg);
|
|
|
|
char **(*getarg)(char **argv, union extra *extra);
|
|
|
|
void (*freearg)(union extra extra);
|
2015-02-25 03:34:36 +00:00
|
|
|
char narg; /* -xdev, -depth, -print don't take args but have getarg() */
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* Information about operators, for lookup table */
|
2015-03-17 17:59:16 +00:00
|
|
|
struct op_info {
|
2015-02-20 01:47:21 +00:00
|
|
|
char *name; /* string representation of op */
|
2015-03-17 17:59:16 +00:00
|
|
|
char type; /* from tok.type */
|
2015-02-21 01:30:28 +00:00
|
|
|
char prec; /* precedence */
|
|
|
|
char nargs; /* number of arguments (unary or binary) */
|
|
|
|
char lassoc; /* left associative */
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* Token when lexing/parsing
|
|
|
|
* (although also used for the expression tree) */
|
2015-03-17 17:59:16 +00:00
|
|
|
struct tok {
|
|
|
|
struct tok *left, *right; /* if (type == NOT) left = NULL */
|
|
|
|
union extra extra;
|
2015-02-20 01:47:21 +00:00
|
|
|
union {
|
2015-03-17 17:59:16 +00:00
|
|
|
struct pri_info *pinfo; /* if (type == PRIM) */
|
|
|
|
struct op_info *oinfo;
|
2015-02-20 01:47:21 +00:00
|
|
|
} u;
|
2015-02-21 01:30:28 +00:00
|
|
|
enum {
|
|
|
|
PRIM = 0, LPAR, RPAR, NOT, AND, OR, END
|
|
|
|
} type;
|
2015-02-20 01:47:21 +00:00
|
|
|
};
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
/* structures used for arg.extra.p and tok.extra.p */
|
|
|
|
struct permarg {
|
2015-02-21 01:30:28 +00:00
|
|
|
mode_t mode;
|
|
|
|
char exact;
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
struct okarg {
|
2015-02-20 01:47:21 +00:00
|
|
|
char ***braces;
|
|
|
|
char **argv;
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* for all arguments that take a number
|
|
|
|
* +n, n, -n mean > n, == n, < n respectively */
|
2015-03-17 17:59:16 +00:00
|
|
|
struct narg {
|
2015-02-25 02:19:49 +00:00
|
|
|
int (*cmp)(int a, int b);
|
2015-02-20 01:47:21 +00:00
|
|
|
int n;
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
struct sizearg {
|
|
|
|
struct narg n;
|
2015-02-21 01:30:28 +00:00
|
|
|
char bytes; /* size is in bytes, not 512 byte sectors */
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
struct execarg {
|
2015-02-20 01:47:21 +00:00
|
|
|
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;
|
2015-02-21 01:30:28 +00:00
|
|
|
char **argv; /* NULL terminated list of arguments (allocated if isplus) */
|
|
|
|
char isplus; /* -exec + instead of -exec ; */
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* used to find loops while recursing through directory structure */
|
2015-03-17 17:59:16 +00:00
|
|
|
struct findhist {
|
|
|
|
struct findhist *next;
|
|
|
|
char *path;
|
|
|
|
dev_t dev;
|
|
|
|
ino_t ino;
|
2015-02-20 01:47:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Primaries */
|
2015-03-17 17:59:16 +00:00
|
|
|
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);
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* Getargs */
|
2015-03-17 17:59:16 +00:00
|
|
|
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);
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* Freeargs */
|
2015-03-17 17:59:16 +00:00
|
|
|
static void free_extra (union extra extra);
|
|
|
|
static void free_exec_arg(union extra extra);
|
|
|
|
static void free_ok_arg (union extra extra);
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* Parsing/Building/Running */
|
2015-03-17 17:59:16 +00:00
|
|
|
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);
|
2015-02-20 01:47:21 +00:00
|
|
|
static void parse(int argc, char **argv);
|
2015-03-17 17:59:16 +00:00
|
|
|
static int eval(struct tok *tok, struct arg *arg);
|
|
|
|
static void find(char *path, struct findhist *hist);
|
2015-02-20 01:47:21 +00:00
|
|
|
static void usage(void);
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
/* for comparisons with narg */
|
2015-02-20 01:47:21 +00:00
|
|
|
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 */
|
2015-03-17 17:59:16 +00:00
|
|
|
static struct pri_info primaries[] = {
|
2015-02-25 03:34:36 +00:00
|
|
|
{ "-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 }
|
2015-02-20 01:47:21 +00:00
|
|
|
};
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
static struct op_info ops[] = {
|
2015-02-20 01:47:21 +00:00
|
|
|
{ "(" , 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;
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
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 */
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-02-21 01:30:28 +00:00
|
|
|
static struct timespec start; /* time find was started, used for -[acm]time */
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-02-21 01:30:28 +00:00
|
|
|
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) */
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-02-21 01:30:28 +00:00
|
|
|
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 */
|
2015-02-25 03:34:36 +00:00
|
|
|
char print; /* whether we will need -print when parsing */
|
2015-02-20 01:47:21 +00:00
|
|
|
} gflags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Primaries
|
|
|
|
*/
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_name(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2016-10-05 22:37:34 +00:00
|
|
|
int ret;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = estrdup(arg->path);
|
|
|
|
ret = !fnmatch((char *)arg->extra.p, basename(path), 0);
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
return ret;
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_path(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
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
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_nouser(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
return !getpwuid(arg->st->st_uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_nogroup(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
return !getgrgid(arg->st->st_gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_xdev(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_prune(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
return gflags.prune = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_perm(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct permarg *p = (struct permarg *)arg->extra.p;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
return (arg->st->st_mode & 07777 & (p->exact ? -1U : p->mode)) == p->mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_type(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
switch ((char)arg->extra.i) {
|
2015-02-20 12:27:57 +00:00
|
|
|
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);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_links(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct narg *n = arg->extra.p;
|
2015-02-25 02:19:49 +00:00
|
|
|
return n->cmp(arg->st->st_nlink, n->n);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_user(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
return arg->st->st_uid == (uid_t)arg->extra.i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_group(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
return arg->st->st_gid == (gid_t)arg->extra.i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_size(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct sizearg *s = arg->extra.p;
|
2015-02-20 01:47:21 +00:00
|
|
|
off_t size = arg->st->st_size;
|
|
|
|
|
|
|
|
if (!s->bytes)
|
|
|
|
size = size / 512 + !!(size % 512);
|
|
|
|
|
2015-02-25 02:19:49 +00:00
|
|
|
return s->n.cmp(size, s->n.n);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: ignoring nanoseconds in atime, ctime, mtime */
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_atime(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct narg *n = arg->extra.p;
|
2016-03-30 01:04:45 +00:00
|
|
|
return n->cmp((start.tv_sec - arg->st->st_atime) / 86400, n->n);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_ctime(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct narg *n = arg->extra.p;
|
2016-03-30 01:04:45 +00:00
|
|
|
return n->cmp((start.tv_sec - arg->st->st_ctime) / 86400, n->n);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_mtime(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct narg *n = arg->extra.p;
|
2016-03-30 01:04:45 +00:00
|
|
|
return n->cmp((start.tv_sec - arg->st->st_mtime) / 86400, n->n);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_exec(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-02-25 03:34:36 +00:00
|
|
|
int status;
|
2015-03-06 17:47:07 +00:00
|
|
|
size_t len;
|
2015-02-25 03:34:36 +00:00
|
|
|
pid_t pid;
|
2015-03-06 17:47:07 +00:00
|
|
|
char **sp, ***brace;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct execarg *e = arg->extra.p;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
if (e->isplus) {
|
2015-03-06 17:47:07 +00:00
|
|
|
len = strlen(arg->path) + 1;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
/* if we reached ARG_MAX, fork, exec, wait, free file names, reset list */
|
2015-02-20 01:47:21 +00:00
|
|
|
if (len + e->u.p.arglen + e->u.p.filelen + envlen > argmax) {
|
|
|
|
e->argv[e->u.p.next] = NULL;
|
|
|
|
|
2015-03-06 17:47:07 +00:00
|
|
|
switch((pid = fork())) {
|
|
|
|
case -1:
|
|
|
|
eprintf("fork:");
|
|
|
|
case 0:
|
2015-02-21 01:30:28 +00:00
|
|
|
execvp(*e->argv, e->argv);
|
2015-03-06 17:47:07 +00:00
|
|
|
weprintf("exec %s failed:", *e->argv);
|
|
|
|
_exit(1);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
waitpid(pid, &status, 0);
|
2015-03-06 17:47:07 +00:00
|
|
|
gflags.ret = gflags.ret || status;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-03-06 17:47:07 +00:00
|
|
|
for (sp = e->argv + e->u.p.first; *sp; sp++)
|
|
|
|
free(*sp);
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
e->u.p.next = e->u.p.first;
|
|
|
|
e->u.p.filelen = 0;
|
|
|
|
}
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
/* if we have too many files, realloc (with space for NULL termination) */
|
2015-02-20 01:47:21 +00:00
|
|
|
if (e->u.p.next + 1 == e->u.p.cap)
|
2015-03-10 20:16:21 +00:00
|
|
|
e->argv = ereallocarray(e->argv, e->u.p.cap *= 2, sizeof(*e->argv));
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-03-06 17:47:07 +00:00
|
|
|
switch((pid = fork())) {
|
|
|
|
case -1:
|
|
|
|
eprintf("fork:");
|
|
|
|
case 0:
|
2015-02-21 01:30:28 +00:00
|
|
|
execvp(*e->argv, e->argv);
|
2015-03-06 17:47:07 +00:00
|
|
|
weprintf("exec %s failed:", *e->argv);
|
|
|
|
_exit(1);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
2016-03-30 01:04:45 +00:00
|
|
|
/* FIXME: proper course of action for all waitpid() on EINTR? */
|
2015-02-20 01:47:21 +00:00
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
return !!status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_ok(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-27 16:08:25 +00:00
|
|
|
int status, reply;
|
2015-02-20 01:47:21 +00:00
|
|
|
pid_t pid;
|
2015-06-15 19:27:37 +00:00
|
|
|
char ***brace, c;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct okarg *o = arg->extra.p;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-06-15 19:27:38 +00:00
|
|
|
fprintf(stderr, "%s: %s ? ", *o->argv, arg->path);
|
2015-02-20 01:47:21 +00:00
|
|
|
reply = fgetc(stdin);
|
|
|
|
|
|
|
|
/* throw away rest of line */
|
2015-06-15 19:27:37 +00:00
|
|
|
while ((c = fgetc(stdin)) != '\n' && c != EOF)
|
2015-02-20 01:47:21 +00:00
|
|
|
/* FIXME: what if the first character of the rest of the line is a null
|
2015-06-15 19:27:37 +00:00
|
|
|
* byte? */
|
2015-02-20 01:47:21 +00:00
|
|
|
;
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
if (feof(stdin)) /* FIXME: ferror()? */
|
2015-02-20 01:47:21 +00:00
|
|
|
clearerr(stdin);
|
|
|
|
|
|
|
|
if (reply != 'y' && reply != 'Y')
|
|
|
|
return 0;
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
/* insert filename everywhere user gave us {} */
|
2015-02-20 01:47:21 +00:00
|
|
|
for (brace = o->braces; *brace; brace++)
|
|
|
|
**brace = arg->path;
|
|
|
|
|
2015-03-06 17:47:07 +00:00
|
|
|
switch((pid = fork())) {
|
|
|
|
case -1:
|
|
|
|
eprintf("fork:");
|
|
|
|
case 0:
|
2015-02-21 01:30:28 +00:00
|
|
|
execvp(*o->argv, o->argv);
|
2015-03-06 17:47:07 +00:00
|
|
|
weprintf("exec %s failed:", *o->argv);
|
|
|
|
_exit(1);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
return !!status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_print(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-02-25 03:34:36 +00:00
|
|
|
if (puts(arg->path) == EOF)
|
|
|
|
eprintf("puts failed:");
|
2015-02-20 01:47:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: ignoring nanoseconds */
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_newer(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-02-20 15:47:50 +00:00
|
|
|
return arg->st->st_mtime > (time_t)arg->extra.i;
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-17 17:59:16 +00:00
|
|
|
pri_depth(struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
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 **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_name_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
extra->p = *argv;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_path_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
extra->p = *argv;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_xdev_arg(char *argv[], union extra *extra)
|
2015-02-25 03:34:36 +00:00
|
|
|
{
|
|
|
|
gflags.xdev = 1;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:47:21 +00:00
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_perm_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct permarg *p = extra->p = emalloc(sizeof(*p));
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
if (**argv == '-')
|
|
|
|
(*argv)++;
|
|
|
|
else
|
|
|
|
p->exact = 1;
|
|
|
|
|
|
|
|
p->mode = parsemode(*argv, 0, 0);
|
|
|
|
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_type_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
if (!strchr("bcdlpfs", **argv))
|
|
|
|
eprintf("invalid type %c for -type primary\n", **argv);
|
|
|
|
|
|
|
|
extra->i = **argv;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_n_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct narg *n = extra->p = emalloc(sizeof(*n));
|
2015-02-25 03:34:36 +00:00
|
|
|
fill_narg(*argv, n);
|
2015-02-20 01:47:21 +00:00
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_user_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
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 **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_group_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
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 **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_size_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
char *p = *argv + strlen(*argv);
|
2015-03-17 17:59:16 +00:00
|
|
|
struct sizearg *s = extra->p = emalloc(sizeof(*s));
|
2015-02-20 01:47:21 +00:00
|
|
|
/* if the number is followed by 'c', the size will by in bytes */
|
2015-02-25 03:34:36 +00:00
|
|
|
if ((s->bytes = (p > *argv && *--p == 'c')))
|
2015-02-20 01:47:21 +00:00
|
|
|
*p = '\0';
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
fill_narg(*argv, &s->n);
|
2015-02-20 01:47:21 +00:00
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_exec_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-06 17:47:07 +00:00
|
|
|
char **arg, **new, ***braces;
|
2015-02-20 01:47:21 +00:00
|
|
|
int nbraces = 0;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct execarg *e = extra->p = emalloc(sizeof(*e));
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
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;
|
2015-03-11 09:50:18 +00:00
|
|
|
e->argv = ereallocarray(NULL, e->u.p.cap, sizeof(*e->argv));
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
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;
|
2015-03-11 09:50:18 +00:00
|
|
|
e->u.s.braces = ereallocarray(NULL, ++nbraces, sizeof(*e->u.s.braces)); /* ++ for NULL */
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
for (arg = argv, braces = e->u.s.braces; *arg; arg++)
|
|
|
|
if (!strcmp(*arg, "{}"))
|
|
|
|
*braces++ = arg;
|
2015-07-11 18:17:22 +00:00
|
|
|
*braces = NULL;
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
2015-02-25 03:34:36 +00:00
|
|
|
gflags.print = 0;
|
2015-02-20 01:47:21 +00:00
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_ok_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
char **arg, ***braces;
|
|
|
|
int nbraces = 0;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct okarg *o = extra->p = emalloc(sizeof(*o));
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
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;
|
2015-03-11 09:50:18 +00:00
|
|
|
o->braces = ereallocarray(NULL, ++nbraces, sizeof(*o->braces));
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
for (arg = argv, braces = o->braces; *arg; arg++)
|
|
|
|
if (!strcmp(*arg, "{}"))
|
|
|
|
*braces++ = arg;
|
2015-06-15 20:49:38 +00:00
|
|
|
*braces = NULL;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
gflags.print = 0;
|
2015-02-20 01:47:21 +00:00
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_print_arg(char *argv[], union extra *extra)
|
2015-02-25 03:34:36 +00:00
|
|
|
{
|
|
|
|
gflags.print = 0;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:47:21 +00:00
|
|
|
/* FIXME: ignoring nanoseconds */
|
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_newer_arg(char *argv[], union extra *extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(*argv, &st))
|
|
|
|
eprintf("failed to stat '%s':", *argv);
|
|
|
|
|
2015-02-20 15:47:50 +00:00
|
|
|
extra->i = st.st_mtime;
|
2015-02-20 01:47:21 +00:00
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
static char **
|
2015-03-17 17:59:16 +00:00
|
|
|
get_depth_arg(char *argv[], union extra *extra)
|
2015-02-25 03:34:36 +00:00
|
|
|
{
|
|
|
|
gflags.depth = 1;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:47:21 +00:00
|
|
|
/*
|
|
|
|
* Freeargs
|
|
|
|
*/
|
|
|
|
static void
|
2015-03-17 17:59:16 +00:00
|
|
|
free_extra(union extra extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
free(extra.p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-17 17:59:16 +00:00
|
|
|
free_exec_arg(union extra extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-06 17:47:07 +00:00
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
char **arg;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct execarg *e = extra.p;
|
2015-02-25 03:34:36 +00:00
|
|
|
|
2015-02-20 01:47:21 +00:00
|
|
|
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) {
|
2015-03-06 17:47:07 +00:00
|
|
|
switch((pid = fork())) {
|
|
|
|
case -1:
|
|
|
|
eprintf("fork:");
|
|
|
|
case 0:
|
2015-02-21 01:30:28 +00:00
|
|
|
execvp(*e->argv, e->argv);
|
2015-03-06 17:47:07 +00:00
|
|
|
weprintf("exec %s failed:", *e->argv);
|
|
|
|
_exit(1);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
waitpid(pid, &status, 0);
|
2015-03-06 17:47:07 +00:00
|
|
|
gflags.ret = gflags.ret || status;
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
for (arg = e->argv + e->u.p.first; *arg; arg++)
|
|
|
|
free(*arg);
|
|
|
|
free(e->argv);
|
|
|
|
}
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-17 17:59:16 +00:00
|
|
|
free_ok_arg(union extra extra)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct okarg *o = extra.p;
|
2015-02-25 03:34:36 +00:00
|
|
|
|
2015-02-20 01:47:21 +00:00
|
|
|
free(o->braces);
|
|
|
|
free(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parsing/Building/Running
|
|
|
|
*/
|
2015-02-25 03:34:36 +00:00
|
|
|
static void
|
2015-03-17 17:59:16 +00:00
|
|
|
fill_narg(char *s, struct narg *n)
|
2015-02-25 03:34:36 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
static struct pri_info *
|
2015-02-20 01:47:21 +00:00
|
|
|
find_primary(char *name)
|
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct pri_info *p;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
for (p = primaries; p->name; p++)
|
|
|
|
if (!strcmp(name, p->name))
|
|
|
|
return p;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
static struct op_info *
|
2015-02-20 01:47:21 +00:00
|
|
|
find_op(char *name)
|
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct op_info *o;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
for (o = ops; o->name; o++)
|
|
|
|
if (!strcmp(name, o->name))
|
|
|
|
return o;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given the expression from the command line
|
2015-03-17 17:59:16 +00:00
|
|
|
* 1) convert arguments from strings to tok and place in an array duplicating
|
2015-02-20 01:47:21 +00:00
|
|
|
* the infix expression given, inserting "-a" where it was omitted
|
2015-03-17 17:59:16 +00:00
|
|
|
* 2) allocate an array to hold the correct number of tok, and convert from
|
2015-02-20 01:47:21 +00:00
|
|
|
* 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
|
2015-03-17 17:59:16 +00:00
|
|
|
* expression tree (tok are still all contained in the rpn array, just
|
2015-02-20 01:47:21 +00:00
|
|
|
* pointing at eachother)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
parse(int argc, char **argv)
|
|
|
|
{
|
2016-10-05 22:34:52 +00:00
|
|
|
struct tok *tok, *rpn, *out, **top, *infix, **stack;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct op_info *op;
|
|
|
|
struct pri_info *pri;
|
2015-02-20 01:47:21 +00:00
|
|
|
char **arg;
|
2015-03-06 17:47:07 +00:00
|
|
|
int lasttype = -1;
|
2015-02-20 01:47:21 +00:00
|
|
|
size_t ntok = 0;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct tok and = { .u.oinfo = find_op("-a"), .type = AND };
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2016-10-05 22:34:52 +00:00
|
|
|
infix = ereallocarray(NULL, 2 * argc + 1, sizeof(*infix));
|
|
|
|
stack = ereallocarray(NULL, argc, sizeof(*stack));
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
gflags.print = 1;
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
/* convert argv to infix expression of tok, inserting in *tok */
|
2015-02-20 01:47:21 +00:00
|
|
|
for (arg = argv, tok = infix; *arg; arg++, tok++) {
|
2015-03-06 17:47:07 +00:00
|
|
|
pri = find_primary(*arg);
|
2015-02-20 01:47:21 +00:00
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
if (pri) { /* token is a primary, fill out tok and get arguments */
|
2015-02-20 01:47:21 +00:00
|
|
|
if (lasttype == PRIM || lasttype == RPAR) {
|
2015-02-21 01:51:04 +00:00
|
|
|
*tok++ = and;
|
2015-02-20 01:47:21 +00:00
|
|
|
ntok++;
|
|
|
|
}
|
|
|
|
if (pri->getarg) {
|
2015-02-25 03:34:36 +00:00
|
|
|
if (pri->narg && !*++arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
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");
|
2015-05-13 21:18:13 +00:00
|
|
|
if ((lasttype == PRIM || lasttype == RPAR) && op->type == NOT) { /* need another implicit -a */
|
2015-02-21 01:51:04 +00:00
|
|
|
*tok++ = and;
|
|
|
|
ntok++;
|
|
|
|
}
|
2015-02-20 01:47:21 +00:00
|
|
|
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++;
|
|
|
|
|
2015-02-25 03:34:36 +00:00
|
|
|
if (gflags.print && (arg != argv)) /* need to add -a -print (not just -print) */
|
|
|
|
gflags.print++;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
/* 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
|
2015-02-25 03:34:36 +00:00
|
|
|
* push operators onto stack, next position in stack is top */
|
2015-03-11 09:50:18 +00:00
|
|
|
rpn = ereallocarray(NULL, ntok + gflags.print, sizeof(*rpn));
|
2015-02-20 01:47:21 +00:00
|
|
|
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
|
2015-02-25 03:34:36 +00:00
|
|
|
* in rpn, not infix */
|
|
|
|
if (gflags.print)
|
2015-03-17 17:59:16 +00:00
|
|
|
*out++ = (struct tok){ .u.pinfo = find_primary("-print"), .type = PRIM };
|
2015-02-25 03:34:36 +00:00
|
|
|
if (gflags.print == 2)
|
2015-02-21 01:51:04 +00:00
|
|
|
*out++ = and;
|
|
|
|
|
2015-02-20 01:47:21 +00:00
|
|
|
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
|
2015-02-25 03:34:36 +00:00
|
|
|
* code IMO */
|
2015-02-20 01:47:21 +00:00
|
|
|
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;
|
2016-10-05 22:34:52 +00:00
|
|
|
|
|
|
|
free(infix);
|
|
|
|
free(stack);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
2015-03-17 17:59:16 +00:00
|
|
|
eval(struct tok *tok, struct arg *arg)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
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
|
2015-03-17 17:59:16 +00:00
|
|
|
find(char *path, struct findhist *hist)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *de;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct findhist *f, cur;
|
2016-10-05 22:34:52 +00:00
|
|
|
size_t namelen, pathcap = 0, len;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct arg arg = { path, &st, { NULL } };
|
2016-10-05 22:34:52 +00:00
|
|
|
char *p, *pathbuf = NULL;
|
|
|
|
|
|
|
|
len = strlen(path) + 2; /* \0 and '/' */
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-30 01:04:45 +00:00
|
|
|
while (errno = 0, (de = readdir(dir))) {
|
2015-02-20 01:47:21 +00:00
|
|
|
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
|
|
|
|
continue;
|
2016-10-05 22:34:52 +00:00
|
|
|
namelen = strlen(de->d_name);
|
|
|
|
if (len + namelen > pathcap) {
|
|
|
|
pathcap = len + namelen;
|
|
|
|
pathbuf = erealloc(pathbuf, pathcap);
|
|
|
|
}
|
2015-03-17 10:24:49 +00:00
|
|
|
p = pathbuf + estrlcpy(pathbuf, path, pathcap);
|
2015-02-20 01:47:21 +00:00
|
|
|
if (*--p != '/')
|
2015-03-17 10:24:49 +00:00
|
|
|
estrlcat(pathbuf, "/", pathcap);
|
|
|
|
estrlcat(pathbuf, de->d_name, pathcap);
|
2015-02-20 01:47:21 +00:00
|
|
|
find(pathbuf, &cur);
|
|
|
|
}
|
2016-10-05 22:34:52 +00:00
|
|
|
free(pathbuf);
|
2016-03-30 01:04:45 +00:00
|
|
|
if (errno) {
|
|
|
|
weprintf("readdir %s:", path);
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
if (gflags.depth)
|
|
|
|
eval(root, &arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-12-21 17:36:28 +00:00
|
|
|
eprintf("usage: %s [-H | -L] path ... [expression ...]\n", argv0);
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 10:24:11 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2015-02-20 01:47:21 +00:00
|
|
|
{
|
|
|
|
char **paths;
|
|
|
|
int npaths;
|
2015-03-17 17:59:16 +00:00
|
|
|
struct tok *t;
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'H': gflags.l = !(gflags.h = 1); break;
|
|
|
|
case 'L': gflags.h = !(gflags.l = 1); break;
|
|
|
|
default : usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
paths = argv;
|
|
|
|
|
|
|
|
for (; *argv && **argv != '-' && strcmp(*argv, "!") && strcmp(*argv, "("); argv++)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (!(npaths = argv - paths))
|
|
|
|
eprintf("must specify a path\n");
|
|
|
|
|
|
|
|
parse(argc - npaths, argv);
|
|
|
|
|
|
|
|
/* calculate number of bytes in environ for -exec {} + ARG_MAX avoidance
|
|
|
|
* libc implementation defined whether null bytes, pointers, and alignment
|
|
|
|
* are counted, so count them */
|
|
|
|
for (argv = environ; *argv; argv++)
|
|
|
|
envlen += strlen(*argv) + 1 + sizeof(*argv);
|
|
|
|
|
|
|
|
if ((argmax = sysconf(_SC_ARG_MAX)) == (size_t)-1)
|
|
|
|
argmax = _POSIX_ARG_MAX;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &start) < 0)
|
|
|
|
weprintf("clock_gettime() failed:");
|
|
|
|
|
|
|
|
while (npaths--)
|
2015-05-15 10:55:59 +00:00
|
|
|
find(*paths++, NULL);
|
2015-02-20 01:47:21 +00:00
|
|
|
|
|
|
|
for (t = toks; t->type != END; t++)
|
|
|
|
if (t->type == PRIM && t->u.pinfo->freearg)
|
|
|
|
t->u.pinfo->freearg(t->extra);
|
|
|
|
free(toks);
|
|
|
|
|
2015-05-24 23:33:19 +00:00
|
|
|
gflags.ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
|
|
|
|
|
|
|
|
return gflags.ret;
|
2015-02-20 01:47:21 +00:00
|
|
|
}
|