-/* $NetBSD: function.c,v 1.24 1998/02/21 22:47:20 christos Exp $ */
-
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
#ifndef lint
#if 0
-static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95";
-#else
-__RCSID("$NetBSD: function.c,v 1.24 1998/02/21 22:47:20 christos Exp $");
+static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
#endif
#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: src/usr.bin/find/function.c,v 1.71 2011/06/13 05:22:07 avatar Exp $");
+
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/acl.h>
#include <sys/wait.h>
#include <sys/mount.h>
+#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fnmatch.h>
#include <fts.h>
#include <grp.h>
+#include <limits.h>
#include <pwd.h>
+#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <tzfile.h>
#include <unistd.h>
+#include <ctype.h>
+
+#ifdef __APPLE__
+#include <sys/sysctl.h>
+#include <sys/xattr.h>
+#include <libgen.h>
+#include <get_compat.h>
+#else
+#define COMPAT_MODE(func, mode) 1
+#endif
#include "find.h"
-#define COMPARE(a, b) { \
- switch (plan->flags) { \
+static PLAN *palloc(OPTION *);
+static long long find_parsenum(PLAN *, const char *, char *, char *);
+static long long find_parsetime(PLAN *, const char *, char *);
+static char *nextarg(OPTION *, char ***);
+
+extern char **environ;
+
+static PLAN *lastexecplus = NULL;
+int execplus_error;
+
+#define COMPARE(a, b) do { \
+ switch (plan->flags & F_ELG_MASK) { \
case F_EQUAL: \
return (a == b); \
case F_LESSTHAN: \
default: \
abort(); \
} \
-}
+} while(0)
+
+static PLAN *
+palloc(OPTION *option)
+{
+ PLAN *new;
-static long find_parsenum __P((PLAN *, char *, char *, char *));
- int f_always_true __P((PLAN *, FTSENT *));
- int f_atime __P((PLAN *, FTSENT *));
- int f_ctime __P((PLAN *, FTSENT *));
- int f_exec __P((PLAN *, FTSENT *));
- int f_fstype __P((PLAN *, FTSENT *));
- int f_group __P((PLAN *, FTSENT *));
- int f_inum __P((PLAN *, FTSENT *));
- int f_links __P((PLAN *, FTSENT *));
- int f_ls __P((PLAN *, FTSENT *));
- int f_mtime __P((PLAN *, FTSENT *));
- int f_name __P((PLAN *, FTSENT *));
- int f_newer __P((PLAN *, FTSENT *));
- int f_nogroup __P((PLAN *, FTSENT *));
- int f_nouser __P((PLAN *, FTSENT *));
- int f_path __P((PLAN *, FTSENT *));
- int f_perm __P((PLAN *, FTSENT *));
- int f_print __P((PLAN *, FTSENT *));
- int f_print0 __P((PLAN *, FTSENT *));
- int f_prune __P((PLAN *, FTSENT *));
- int f_size __P((PLAN *, FTSENT *));
- int f_type __P((PLAN *, FTSENT *));
- int f_user __P((PLAN *, FTSENT *));
- int f_not __P((PLAN *, FTSENT *));
- int f_or __P((PLAN *, FTSENT *));
-static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
+ if ((new = malloc(sizeof(PLAN))) == NULL)
+ err(1, NULL);
+ new->execute = option->execute;
+ new->flags = option->flags;
+ new->next = NULL;
+ return new;
+}
/*
* find_parsenum --
* Parse a string of the form [+-]# and return the value.
*/
-static long
-find_parsenum(plan, option, vp, endch)
- PLAN *plan;
- char *option, *vp, *endch;
+static long long
+find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
{
- long value;
+ long long value;
char *endchar, *str; /* Pointer to character ending conversion. */
-
+
/* Determine comparison from leading + or -. */
str = vp;
switch (*str) {
case '+':
++str;
- plan->flags = F_GREATER;
+ plan->flags |= F_GREATER;
break;
case '-':
++str;
- plan->flags = F_LESSTHAN;
+ plan->flags |= F_LESSTHAN;
break;
default:
- plan->flags = F_EQUAL;
+ plan->flags |= F_EQUAL;
break;
}
-
+
/*
- * Convert the string with strtol(). Note, if strtol() returns zero
+ * Convert the string with strtoq(). Note, if strtoq() returns zero
* and endchar points to the beginning of the string we know we have
* a syntax error.
*/
- value = strtol(str, &endchar, 10);
+ value = strtoq(str, &endchar, 10);
if (value == 0 && endchar == str)
errx(1, "%s: %s: illegal numeric value", option, vp);
- if (endchar[0] && (endch == NULL || endchar[0] != *endch))
+ if (endchar[0] && endch == NULL)
errx(1, "%s: %s: illegal trailing character", option, vp);
if (endch)
*endch = endchar[0];
- return (value);
+ return value;
}
/*
- * The value of n for the inode times (atime, ctime, and mtime) is a range,
- * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
- * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
- * user wanted. Correct so that -1 is "less than 1".
+ * find_parsetime --
+ * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
*/
-#define TIME_CORRECT(p, ttype) \
- if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
+static long long
+find_parsetime(PLAN *plan, const char *option, char *vp)
+{
+ long long secs, value;
+ char *str, *unit; /* Pointer to character ending conversion. */
+
+ /* Determine comparison from leading + or -. */
+ str = vp;
+ switch (*str) {
+ case '+':
+ ++str;
+ plan->flags |= F_GREATER;
+ break;
+ case '-':
+ ++str;
+ plan->flags |= F_LESSTHAN;
+ break;
+ default:
+ plan->flags |= F_EQUAL;
+ break;
+ }
+
+ value = strtoq(str, &unit, 10);
+ if (value == 0 && unit == str) {
+ errx(1, "%s: %s: illegal time value", option, vp);
+ /* NOTREACHED */
+ }
+ if (*unit == '\0')
+ return value;
+
+ /* Units syntax. */
+ secs = 0;
+ for (;;) {
+ switch(*unit) {
+ case 's': /* seconds */
+ secs += value;
+ break;
+ case 'm': /* minutes */
+ secs += value * 60;
+ break;
+ case 'h': /* hours */
+ secs += value * 3600;
+ break;
+ case 'd': /* days */
+ secs += value * 86400;
+ break;
+ case 'w': /* weeks */
+ secs += value * 604800;
+ break;
+ default:
+ errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
+ /* NOTREACHED */
+ }
+ str = unit + 1;
+ if (*str == '\0') /* EOS */
+ break;
+ value = strtoq(str, &unit, 10);
+ if (value == 0 && unit == str) {
+ errx(1, "%s: %s: illegal time value", option, vp);
+ /* NOTREACHED */
+ }
+ if (*unit == '\0') {
+ errx(1, "%s: %s: missing trailing unit", option, vp);
+ /* NOTREACHED */
+ }
+ }
+ plan->flags |= F_EXACTTIME;
+ return secs;
+}
+
+/*
+ * nextarg --
+ * Check that another argument still exists, return a pointer to it,
+ * and increment the argument vector pointer.
+ */
+static char *
+nextarg(OPTION *option, char ***argvp)
+{
+ char *arg;
+
+ if ((arg = **argvp) == 0)
+ errx(1, "%s: requires additional arguments", option->name);
+ (*argvp)++;
+ return arg;
+} /* nextarg() */
+
+/*
+ * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
+ * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
+ * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
+ * the user wanted. Correct so that -1 is "less than 1".
+ */
+#define TIME_CORRECT(p) \
+ if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
++((p)->t_data);
/*
- * -atime n functions --
+ * -[acm]min n functions --
*
- * True if the difference between the file access time and the
- * current time is n 24 hour periods.
+ * True if the difference between the
+ * file access time (-amin)
+ * file birth time (-Bmin)
+ * last change of file status information (-cmin)
+ * file modification time (-mmin)
+ * and the current time is n min periods.
*/
int
-f_atime(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_Xmin(PLAN *plan, FTSENT *entry)
{
- extern time_t now;
-
- COMPARE((now - entry->fts_statp->st_atime +
- SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
+ if (plan->flags & F_TIME_C) {
+ COMPARE((now - entry->fts_statp->st_ctime +
+ 60 - 1) / 60, plan->t_data);
+ } else if (plan->flags & F_TIME_A) {
+ COMPARE((now - entry->fts_statp->st_atime +
+ 60 - 1) / 60, plan->t_data);
+ } else if (plan->flags & F_TIME_B) {
+ COMPARE((now - entry->fts_statp->st_birthtime +
+ 60 - 1) / 60, plan->t_data);
+ } else {
+ COMPARE((now - entry->fts_statp->st_mtime +
+ 60 - 1) / 60, plan->t_data);
+ }
}
-
+
PLAN *
-c_atime(argvp, isok)
- char ***argvp;
- int isok;
+c_Xmin(OPTION *option, char ***argvp)
{
- char *arg = **argvp;
+ char *nmins;
PLAN *new;
- (*argvp)++;
+ nmins = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
- new = palloc(N_ATIME, f_atime);
- new->t_data = find_parsenum(new, "-atime", arg, NULL);
- TIME_CORRECT(new, N_ATIME);
- return (new);
+ new = palloc(option);
+ new->t_data = find_parsenum(new, option->name, nmins, NULL);
+ TIME_CORRECT(new);
+ return new;
}
+
/*
- * -ctime n functions --
+ * -[acm]time n functions --
*
- * True if the difference between the last change of file
- * status information and the current time is n 24 hour periods.
+ * True if the difference between the
+ * file access time (-atime)
+ * file birth time (-Btime)
+ * last change of file status information (-ctime)
+ * file modification time (-mtime)
+ * and the current time is n 24 hour periods.
*/
+
int
-f_ctime(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_Xtime(PLAN *plan, FTSENT *entry)
{
- extern time_t now;
+ time_t xtime;
+
+ if (plan->flags & F_TIME_A)
+ xtime = entry->fts_statp->st_atime;
+ else if (plan->flags & F_TIME_B)
+ xtime = entry->fts_statp->st_birthtime;
+ else if (plan->flags & F_TIME_C)
+ xtime = entry->fts_statp->st_ctime;
+ else
+ xtime = entry->fts_statp->st_mtime;
- COMPARE((now - entry->fts_statp->st_ctime +
- SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
+ if (plan->flags & F_EXACTTIME)
+ COMPARE(now - xtime, plan->t_data);
+ else
+ COMPARE((now - xtime + (COMPAT_MODE("bin/find", "unix2003") ? 0 : 86400 - 1)) / 86400, plan->t_data);
}
-
+
PLAN *
-c_ctime(argvp, isok)
- char ***argvp;
- int isok;
+c_Xtime(OPTION *option, char ***argvp)
{
- char *arg = **argvp;
+ char *value;
PLAN *new;
- (*argvp)++;
+ value = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
- new = palloc(N_CTIME, f_ctime);
- new->t_data = find_parsenum(new, "-ctime", arg, NULL);
- TIME_CORRECT(new, N_CTIME);
- return (new);
+ new = palloc(option);
+ new->t_data = find_parsetime(new, option->name, value);
+ if (!(new->flags & F_EXACTTIME) && !COMPAT_MODE("bin/find", "unix2003"))
+ TIME_CORRECT(new);
+ return new;
}
/*
- * -depth functions --
+ * -maxdepth/-mindepth n functions --
+ *
+ * Does the same as -prune if the level of the current file is
+ * greater/less than the specified maximum/minimum depth.
+ *
+ * Note that -maxdepth and -mindepth are handled specially in
+ * find_execute() so their f_* functions are set to f_always_true().
+ */
+PLAN *
+c_mXXdepth(OPTION *option, char ***argvp)
+{
+ char *dstr;
+ PLAN *new;
+
+ dstr = nextarg(option, argvp);
+ if (dstr[0] == '-')
+ /* all other errors handled by find_parsenum() */
+ errx(1, "%s: %s: value must be positive", option->name, dstr);
+
+ new = palloc(option);
+ if (option->flags & F_MAXDEPTH)
+ maxdepth = find_parsenum(new, option->name, dstr, NULL);
+ else
+ mindepth = find_parsenum(new, option->name, dstr, NULL);
+ return new;
+}
+
+/*
+ * -acl function --
*
- * Always true, causes descent of the directory hierarchy to be done
- * so that all entries in a directory are acted on before the directory
- * itself.
+ * Show files with EXTENDED ACL attributes.
*/
+#ifdef __APPLE__
int
-f_always_true(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_acl(PLAN *plan __unused, FTSENT *entry)
{
+ acl_t facl;
+ int match;
+ acl_entry_t ae;
+
+ match = 0;
+ if ((facl = acl_get_link_np(entry->fts_accpath, ACL_TYPE_EXTENDED)) != NULL) {
+ if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 0) {
+ match = 1;
+ }
+ acl_free(facl);
+ }
+ return match;
+}
+#else /* !__APPLE__ */
+int
+f_acl(PLAN *plan __unused, FTSENT *entry)
+{
+ acl_t facl;
+ acl_type_t acl_type;
+ int acl_supported = 0, ret, trivial;
+
+ if (S_ISLNK(entry->fts_statp->st_mode))
+ return 0;
+ ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
+ if (ret > 0) {
+ acl_supported = 1;
+ acl_type = ACL_TYPE_NFS4;
+ } else if (ret < 0 && errno != EINVAL) {
+ warn("%s", entry->fts_accpath);
+ return (0);
+ }
+ if (acl_supported == 0) {
+ ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
+ if (ret > 0) {
+ acl_supported = 1;
+ acl_type = ACL_TYPE_ACCESS;
+ } else if (ret < 0 && errno != EINVAL) {
+ warn("%s", entry->fts_accpath);
+ return (0);
+ }
+ }
+ if (acl_supported == 0)
+ return (0);
+
+ facl = acl_get_file(entry->fts_accpath, acl_type);
+ if (facl == NULL) {
+ warn("%s", entry->fts_accpath);
+ return (0);
+ }
+ ret = acl_is_trivial_np(facl, &trivial);
+ acl_free(facl);
+ if (ret) {
+ warn("%s", entry->fts_accpath);
+ acl_free(facl);
+ return (0);
+ }
+ if (trivial)
+ return (0);
return (1);
}
-
+#endif /* __APPLE__ */
+
PLAN *
-c_depth(argvp, isok)
- char ***argvp;
- int isok;
+c_acl(OPTION *option, char ***argvp __unused)
{
- isdepth = 1;
+#ifndef __APPLE__
+ ftsoptions &= ~FTS_NOSTAT;
+#endif /* !__APPLE__ */
+ return (palloc(option));
+}
- return (palloc(N_DEPTH, f_always_true));
+#ifdef __APPLE__
+int
+f_xattr(PLAN *plan __unused, FTSENT *entry)
+{
+ ssize_t xattr;
+ int match;
+
+ match = 0;
+ xattr = listxattr(entry->fts_accpath, NULL, 0, XATTR_NOFOLLOW);
+ if (xattr > 0) {
+ match = 1;
+ }
+ return match;
+}
+
+int
+f_xattrname(PLAN *plan, FTSENT *entry)
+{
+ ssize_t xattr;
+ int match;
+
+ match = 0;
+ xattr = getxattr(entry->fts_accpath, plan->c_data, NULL, 0, 0, XATTR_NOFOLLOW);
+ if (xattr > 0) {
+ match = 1;
+ }
+ return match;
+}
+#endif /* __APPLE__ */
+
+/*
+ * -delete functions --
+ *
+ * True always. Makes its best shot and continues on regardless.
+ */
+int
+f_delete(PLAN *plan __unused, FTSENT *entry)
+{
+ /* ignore these from fts */
+ if (strcmp(entry->fts_accpath, ".") == 0 ||
+ strcmp(entry->fts_accpath, "..") == 0)
+ return 1;
+
+ /* sanity check */
+ if (isdepth == 0 || /* depth off */
+ (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
+ errx(1, "-delete: insecure options got turned on");
+
+ if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
+ (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
+ errx(1, "-delete: forbidden when symlinks are followed");
+
+ /* Potentially unsafe - do not accept relative paths whatsoever */
+ if (strchr(entry->fts_accpath, '/') != NULL)
+ errx(1, "-delete: %s: relative path potentially not safe",
+ entry->fts_accpath);
+
+ /* Turn off user immutable bits if running as root */
+ if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
+ !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
+ geteuid() == 0)
+ lchflags(entry->fts_accpath,
+ entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
+
+ /* rmdir directories, unlink everything else */
+ if (S_ISDIR(entry->fts_statp->st_mode)) {
+ if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
+ warn("-delete: rmdir(%s)", entry->fts_path);
+ } else {
+ if (unlink(entry->fts_accpath) < 0)
+ warn("-delete: unlink(%s)", entry->fts_path);
+ }
+
+ /* "succeed" */
+ return 1;
+}
+
+PLAN *
+c_delete(OPTION *option, char ***argvp __unused)
+{
+
+ ftsoptions &= ~FTS_NOSTAT; /* no optimise */
+ isoutput = 1; /* possible output */
+ isdepth = 1; /* -depth implied */
+
+ return palloc(option);
+}
+
+
+/*
+ * always_true --
+ *
+ * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
+ */
+int
+f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
+{
+ return 1;
+}
+
+/*
+ * -depth functions --
+ *
+ * With argument: True if the file is at level n.
+ * Without argument: Always true, causes descent of the directory hierarchy
+ * to be done so that all entries in a directory are acted on before the
+ * directory itself.
+ */
+int
+f_depth(PLAN *plan, FTSENT *entry)
+{
+ if (plan->flags & F_DEPTH)
+ COMPARE(entry->fts_level, plan->d_data);
+ else
+ return 1;
+}
+
+PLAN *
+c_depth(OPTION *option, char ***argvp)
+{
+ PLAN *new;
+ char *str;
+
+ new = palloc(option);
+
+ str = **argvp;
+ if (str && !(new->flags & F_DEPTH)) {
+ /* skip leading + or - */
+ if (*str == '+' || *str == '-')
+ str++;
+ /* skip sign */
+ if (*str == '+' || *str == '-')
+ str++;
+ if (isdigit(*str))
+ new->flags |= F_DEPTH;
+ }
+
+ if (new->flags & F_DEPTH) { /* -depth n */
+ char *ndepth;
+
+ ndepth = nextarg(option, argvp);
+ new->d_data = find_parsenum(new, option->name, ndepth, NULL);
+ } else { /* -d */
+ isdepth = 1;
+ }
+
+ return new;
}
/*
- * [-exec | -ok] utility [arg ... ] ; functions --
+ * -empty functions --
+ *
+ * True if the file or directory is empty
+ */
+int
+f_empty(PLAN *plan __unused, FTSENT *entry)
+{
+ if (S_ISREG(entry->fts_statp->st_mode) &&
+ entry->fts_statp->st_size == 0)
+ return 1;
+ if (S_ISDIR(entry->fts_statp->st_mode)) {
+ struct dirent *dp;
+ int empty;
+ DIR *dir;
+
+ empty = 1;
+ dir = opendir(entry->fts_accpath);
+ if (dir == NULL)
+ return 0;
+ for (dp = readdir(dir); dp; dp = readdir(dir))
+ if (dp->d_name[0] != '.' ||
+ (dp->d_name[1] != '\0' &&
+ (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
+ empty = 0;
+ break;
+ }
+ closedir(dir);
+ return empty;
+ }
+ return 0;
+}
+
+PLAN *
+c_empty(OPTION *option, char ***argvp __unused)
+{
+ ftsoptions &= ~FTS_NOSTAT;
+
+ return palloc(option);
+}
+
+/*
+ * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
*
* True if the executed utility returns a zero value as exit status.
* The end of the primary expression is delimited by a semicolon. If
- * "{}" occurs anywhere, it gets replaced by the current pathname.
- * The current directory for the execution of utility is the same as
- * the current directory when the find utility was started.
+ * "{}" occurs anywhere, it gets replaced by the current pathname,
+ * or, in the case of -execdir, the current basename (filename
+ * without leading directory prefix). For -exec and -ok,
+ * the current directory for the execution of utility is the same as
+ * the current directory when the find utility was started, whereas
+ * for -execdir, it is the directory the file resides in.
*
- * The primary -ok is different in that it requests affirmation of the
- * user before executing the utility.
+ * The primary -ok differs from -exec in that it requests affirmation
+ * of the user before executing the utility.
*/
int
-f_exec(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_exec(PLAN *plan, FTSENT *entry)
{
- extern int dotfd;
int cnt;
pid_t pid;
int status;
+ char *file;
- for (cnt = 0; plan->e_argv[cnt]; ++cnt)
- if (plan->e_len[cnt])
- brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
- entry->fts_path, plan->e_len[cnt]);
+ if (entry == NULL && plan->flags & F_EXECPLUS) {
+ if (plan->e_ppos == plan->e_pbnum)
+ return (1);
+ plan->e_argv[plan->e_ppos] = NULL;
+ goto doexec;
+ }
- if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
- return (0);
+ /* XXX - if file/dir ends in '/' this will not work -- can it? */
+ if ((plan->flags & F_EXECDIR) && \
+ (file = strrchr(entry->fts_path, '/')))
+ file++;
+ else
+ file = entry->fts_path;
+
+ if (plan->flags & F_EXECPLUS) {
+ if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
+ err(1, NULL);
+ plan->e_len[plan->e_ppos] = strlen(file);
+ plan->e_psize += plan->e_len[plan->e_ppos];
+ if (++plan->e_ppos < plan->e_pnummax &&
+ plan->e_psize < plan->e_psizemax)
+ return (1);
+ plan->e_argv[plan->e_ppos] = NULL;
+ } else {
+ for (cnt = 0; plan->e_argv[cnt]; ++cnt)
+ if (plan->e_len[cnt])
+ brace_subst(plan->e_orig[cnt],
+ &plan->e_argv[cnt], file,
+ plan->e_len[cnt]);
+ }
+
+doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
+ return 0;
- /* don't mix output of command with find output */
+ /* make sure find output is interspersed correctly with subprocesses */
fflush(stdout);
fflush(stderr);
- switch (pid = vfork()) {
+ switch (pid = fork()) {
case -1:
err(1, "fork");
/* NOTREACHED */
case 0:
- if (fchdir(dotfd)) {
+ /* change dir back from where we started */
+ if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
warn("chdir");
_exit(1);
}
warn("%s", plan->e_argv[0]);
_exit(1);
}
+ if (plan->flags & F_EXECPLUS) {
+ while (--plan->e_ppos >= plan->e_pbnum)
+ free(plan->e_argv[plan->e_ppos]);
+ plan->e_ppos = plan->e_pbnum;
+ plan->e_psize = plan->e_pbsize;
+ }
pid = waitpid(pid, &status, 0);
+ if (plan->flags & F_EXECPLUS && WIFEXITED(status) && WEXITSTATUS(status) && !execplus_error) {
+ /* Test 140 (8907531, 10656525) */
+ execplus_error = WEXITSTATUS(status);
+ }
return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
}
-
+
/*
- * c_exec --
+ * c_exec, c_execdir, c_ok --
* build three parallel arrays, one with pointers to the strings passed
* on the command line, one with (possibly duplicated) pointers to the
* argv array, and one with integer values that are lengths of the
* strings, but also flags meaning that the string has to be massaged.
*/
PLAN *
-c_exec(argvp, isok)
- char ***argvp;
- int isok;
+c_exec(OPTION *option, char ***argvp)
{
PLAN *new; /* node returned */
- int cnt;
- char **argv, **ap, *p;
+ long argmax;
+ int cnt, i;
+ char **argv, **ap, **ep, *p;
+ /* XXX - was in c_execdir, but seems unnecessary!?
+ ftsoptions &= ~FTS_NOSTAT;
+ */
isoutput = 1;
-
- new = palloc(N_EXEC, f_exec);
- if (isok)
- new->flags = F_NEEDOK;
+
+ /* XXX - this is a change from the previous coding */
+ new = palloc(option);
for (ap = argv = *argvp;; ++ap) {
if (!*ap)
errx(1,
- "%s: no terminating \";\"", isok ? "-ok" : "-exec");
+ "%s: no terminating \";\" or \"+\"", option->name);
if (**ap == ';')
break;
+ if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
+ new->flags |= F_EXECPLUS;
+ break;
+ }
}
+ if (ap == argv)
+ errx(1, "%s: no command specified", option->name);
+
cnt = ap - *argvp + 1;
- new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
- new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
- new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
+ if (new->flags & F_EXECPLUS) {
+ new->e_ppos = new->e_pbnum = cnt - 2;
+ if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
+ warn("sysconf(_SC_ARG_MAX)");
+ argmax = _POSIX_ARG_MAX;
+ }
+ argmax -= 1024;
+ for (ep = environ; *ep != NULL; ep++)
+ argmax -= strlen(*ep) + 1 + sizeof(*ep);
+ argmax -= 1 + sizeof(*ep);
+ new->e_pnummax = argmax / 16;
+ argmax -= sizeof(char *) * new->e_pnummax;
+ if (argmax <= 0)
+ errx(1, "no space for arguments");
+ new->e_psizemax = argmax;
+ new->e_pbsize = 0;
+ cnt += new->e_pnummax + 1;
+ new->e_next = lastexecplus;
+ lastexecplus = new;
+ }
+ if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
+ err(1, NULL);
+ if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
+ err(1, NULL);
+ if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
+ err(1, NULL);
for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
new->e_orig[cnt] = *argv;
+ if (new->flags & F_EXECPLUS)
+ new->e_pbsize += strlen(*argv) + 1;
for (p = *argv; *p; ++p)
- if (p[0] == '{' && p[1] == '}') {
- new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
+ if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
+ p[1] == '}') {
+ if ((new->e_argv[cnt] =
+ malloc(MAXPATHLEN)) == NULL)
+ err(1, NULL);
new->e_len[cnt] = MAXPATHLEN;
break;
}
new->e_len[cnt] = 0;
}
}
+ if (new->flags & F_EXECPLUS) {
+ new->e_psize = new->e_pbsize;
+ cnt--;
+ for (i = 0; i < new->e_pnummax; i++) {
+ new->e_argv[cnt] = NULL;
+ new->e_len[cnt] = 0;
+ cnt++;
+ }
+ argv = ap;
+ goto done;
+ }
new->e_argv[cnt] = new->e_orig[cnt] = NULL;
- *argvp = argv + 1;
- return (new);
+done: *argvp = argv + 1;
+ return new;
}
-
+
+/* Finish any pending -exec ... {} + functions. */
+void
+finish_execplus(void)
+{
+ PLAN *p;
+
+ p = lastexecplus;
+ while (p != NULL) {
+ (p->execute)(p, NULL);
+ p = p->e_next;
+ }
+}
+
+int
+f_flags(PLAN *plan, FTSENT *entry)
+{
+ u_long flags;
+
+ flags = entry->fts_statp->st_flags;
+ if (plan->flags & F_ATLEAST)
+ return (flags | plan->fl_flags) == flags &&
+ !(flags & plan->fl_notflags);
+ else if (plan->flags & F_ANY)
+ return (flags & plan->fl_flags) ||
+ (flags | plan->fl_notflags) != flags;
+ else
+ return flags == plan->fl_flags &&
+ !(plan->fl_flags & plan->fl_notflags);
+}
+
+PLAN *
+c_flags(OPTION *option, char ***argvp)
+{
+ char *flags_str;
+ PLAN *new;
+ u_long flags, notflags;
+
+ flags_str = nextarg(option, argvp);
+ ftsoptions &= ~FTS_NOSTAT;
+
+ new = palloc(option);
+
+ if (*flags_str == '-') {
+ new->flags |= F_ATLEAST;
+ flags_str++;
+ } else if (*flags_str == '+') {
+ new->flags |= F_ANY;
+ flags_str++;
+ }
+ if (strtofflags(&flags_str, &flags, ¬flags) == 1)
+ errx(1, "%s: %s: illegal flags string", option->name, flags_str);
+
+ new->fl_flags = flags;
+ new->fl_notflags = notflags;
+ return new;
+}
+
/*
* -follow functions --
*
* basis.
*/
PLAN *
-c_follow(argvp, isok)
- char ***argvp;
- int isok;
+c_follow(OPTION *option, char ***argvp __unused)
{
ftsoptions &= ~FTS_PHYSICAL;
ftsoptions |= FTS_LOGICAL;
- return (palloc(N_FOLLOW, f_always_true));
+ return palloc(option);
}
-
+
/*
* -fstype functions --
*
* True if the file is of a certain type.
*/
int
-f_fstype(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_fstype(PLAN *plan, FTSENT *entry)
{
static dev_t curdev; /* need a guaranteed illegal dev value */
static int first = 1;
struct statfs sb;
- static short val;
- static char fstype[MFSNAMELEN];
- char *p, save[2];
+ static int val_flags;
+ static char fstype[sizeof(sb.f_fstypename)];
+ char *p, save[2] = {0,0};
+
+ if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
+ return 0;
/* Only check when we cross mount point. */
if (first || curdev != entry->fts_statp->st_dev) {
curdev = entry->fts_statp->st_dev;
/*
- * Statfs follows symlinks; find wants the link's file system,
+ * Statfs follows symlinks; find wants the link's filesystem,
* not where it points.
*/
if (entry->fts_info == FTS_SL ||
p[0] = '.';
save[1] = p[1];
p[1] = '\0';
-
- } else
+ } else
p = NULL;
if (statfs(entry->fts_accpath, &sb))
* Further tests may need both of these values, so
* always copy both of them.
*/
- val = sb.f_flags;
- strncpy(fstype, sb.f_fstypename, MFSNAMELEN);
+ val_flags = sb.f_flags;
+ strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
}
- switch (plan->flags) {
+ switch (plan->flags & F_MTMASK) {
case F_MTFLAG:
- return (val & plan->mt_data);
+ return val_flags & plan->mt_data;
case F_MTTYPE:
- return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
+ return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
default:
abort();
}
}
-
+
PLAN *
-c_fstype(argvp, isok)
- char ***argvp;
- int isok;
+c_fstype(OPTION *option, char ***argvp)
{
- char *arg = **argvp;
+ char *fsname;
PLAN *new;
-
- (*argvp)++;
+
+ fsname = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
-
- new = palloc(N_FSTYPE, f_fstype);
- switch (*arg) {
+ new = palloc(option);
+ switch (*fsname) {
case 'l':
- if (!strcmp(arg, "local")) {
- new->flags = F_MTFLAG;
+ if (!strcmp(fsname, "local")) {
+ new->flags |= F_MTFLAG;
new->mt_data = MNT_LOCAL;
- return (new);
+ return new;
}
break;
case 'r':
- if (!strcmp(arg, "rdonly")) {
- new->flags = F_MTFLAG;
+ if (!strcmp(fsname, "rdonly")) {
+ new->flags |= F_MTFLAG;
new->mt_data = MNT_RDONLY;
- return (new);
+ return new;
}
break;
}
- new->flags = F_MTTYPE;
- new->c_data = arg;
- return (new);
+ new->flags |= F_MTTYPE;
+ new->c_data = fsname;
+ return new;
}
-
+
/*
* -group gname functions --
*
* name, gname is taken as a group ID.
*/
int
-f_group(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_group(PLAN *plan, FTSENT *entry)
{
- return (entry->fts_statp->st_gid == plan->g_data);
+ COMPARE(entry->fts_statp->st_gid, plan->g_data);
}
-
+
PLAN *
-c_group(argvp, isok)
- char ***argvp;
- int isok;
+c_group(OPTION *option, char ***argvp)
{
- char *gname = **argvp;
+ char *gname;
PLAN *new;
struct group *g;
gid_t gid;
-
- (*argvp)++;
+
+ gname = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
+ new = palloc(option);
g = getgrnam(gname);
if (g == NULL) {
+ char* cp = gname;
+ if (gname[0] == '-' || gname[0] == '+')
+ gname++;
gid = atoi(gname);
if (gid == 0 && gname[0] != '0')
- errx(1, "-group: %s: no such group", gname);
+ errx(1, "%s: %s: no such group", option->name, gname);
+ gid = find_parsenum(new, option->name, cp, NULL);
} else
gid = g->gr_gid;
-
- new = palloc(N_GROUP, f_group);
+
new->g_data = gid;
- return (new);
+ return new;
}
/*
* True if the file has inode # n.
*/
int
-f_inum(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_inum(PLAN *plan, FTSENT *entry)
{
COMPARE(entry->fts_statp->st_ino, plan->i_data);
}
-
+
PLAN *
-c_inum(argvp, isok)
- char ***argvp;
- int isok;
+c_inum(OPTION *option, char ***argvp)
{
- char *arg = **argvp;
+ char *inum_str;
PLAN *new;
-
- (*argvp)++;
+
+ inum_str = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
-
- new = palloc(N_INUM, f_inum);
- new->i_data = find_parsenum(new, "-inum", arg, NULL);
- return (new);
+
+ new = palloc(option);
+ new->i_data = find_parsenum(new, option->name, inum_str, NULL);
+ return new;
}
-
+
/*
- * -links n functions --
+ * -samefile FN
*
- * True if the file has n links.
+ * True if the file has the same inode (eg hard link) FN
*/
-int
-f_links(plan, entry)
- PLAN *plan;
- FTSENT *entry;
-{
- COMPARE(entry->fts_statp->st_nlink, plan->l_data);
-}
-
+
+/* f_samefile is just f_inum */
PLAN *
-c_links(argvp, isok)
- char ***argvp;
- int isok;
+c_samefile(OPTION *option, char ***argvp)
{
- char *arg = **argvp;
+ char *fn;
PLAN *new;
-
- (*argvp)++;
+ struct stat sb;
+
+ fn = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
-
- new = palloc(N_LINKS, f_links);
- new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
- return (new);
+
+ new = palloc(option);
+ if (stat(fn, &sb))
+ err(1, "%s", fn);
+ new->i_data = sb.st_ino;
+ return new;
}
-
+
/*
- * -ls functions --
+ * -links n functions --
*
- * Always true - prints the current entry to stdout in "ls" format.
+ * True if the file has n links.
*/
int
-f_ls(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_links(PLAN *plan, FTSENT *entry)
{
- printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
- return (1);
+ COMPARE(entry->fts_statp->st_nlink, plan->l_data);
}
-
+
PLAN *
-c_ls(argvp, isok)
- char ***argvp;
- int isok;
+c_links(OPTION *option, char ***argvp)
{
+ char *nlinks;
+ PLAN *new;
+
+ nlinks = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
- isoutput = 1;
-
- return (palloc(N_LS, f_ls));
+
+ new = palloc(option);
+ new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
+ return new;
}
/*
- * -mtime n functions --
+ * -ls functions --
*
- * True if the difference between the file modification time and the
- * current time is n 24 hour periods.
+ * Always true - prints the current entry to stdout in "ls" format.
*/
int
-f_mtime(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_ls(PLAN *plan __unused, FTSENT *entry)
{
- extern time_t now;
-
- COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
- SECSPERDAY, plan->t_data);
+ printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
+ return 1;
}
-
+
PLAN *
-c_mtime(argvp, isok)
- char ***argvp;
- int isok;
+c_ls(OPTION *option, char ***argvp __unused)
{
- char *arg = **argvp;
- PLAN *new;
-
- (*argvp)++;
ftsoptions &= ~FTS_NOSTAT;
+ isoutput = 1;
- new = palloc(N_MTIME, f_mtime);
- new->t_data = find_parsenum(new, "-mtime", arg, NULL);
- TIME_CORRECT(new, N_MTIME);
- return (new);
+ return palloc(option);
}
/*
* matches pattern using Pattern Matching Notation S3.14
*/
int
-f_name(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_name(PLAN *plan, FTSENT *entry)
{
- return (!fnmatch(plan->c_data, entry->fts_name, 0));
+ char fn[PATH_MAX];
+ const char *name;
+ ssize_t len;
+
+ if (plan->flags & F_LINK) {
+ /*
+ * The below test both avoids obviously useless readlink()
+ * calls and ensures that symlinks with existent target do
+ * not match if symlinks are being followed.
+ * Assumption: fts will stat all symlinks that are to be
+ * followed and will return the stat information.
+ */
+ if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
+ entry->fts_info != FTS_SLNONE)
+ return 0;
+ len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
+ if (len == -1)
+ return 0;
+ fn[len] = '\0';
+ name = fn;
+ } else if (entry->fts_namelen == 0) {
+ name = basename(entry->fts_path);
+ } else
+ name = entry->fts_name;
+ return !fnmatch(plan->c_data, name,
+ plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
}
-
+
PLAN *
-c_name(argvp, isok)
- char ***argvp;
- int isok;
+c_name(OPTION *option, char ***argvp)
{
- char *pattern = **argvp;
+ char *pattern;
PLAN *new;
- (*argvp)++;
- new = palloc(N_NAME, f_name);
+ pattern = nextarg(option, argvp);
+ new = palloc(option);
new->c_data = pattern;
- return (new);
+ return new;
}
-
+
/*
* -newer file functions --
*
* file.
*/
int
-f_newer(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_newer(PLAN *plan, FTSENT *entry)
{
- return (entry->fts_statp->st_mtime > plan->t_data);
+ if (plan->flags & F_TIME_C)
+ return entry->fts_statp->st_ctime > plan->t_data;
+ else if (plan->flags & F_TIME_A)
+ return entry->fts_statp->st_atime > plan->t_data;
+ else if (plan->flags & F_TIME_B)
+ return entry->fts_statp->st_birthtime > plan->t_data;
+ else
+ return entry->fts_statp->st_mtime > plan->t_data;
}
-
+
PLAN *
-c_newer(argvp, isok)
- char ***argvp;
- int isok;
+c_newer(OPTION *option, char ***argvp)
{
- char *filename = **argvp;
+ char *fn_or_tspec;
PLAN *new;
struct stat sb;
-
- (*argvp)++;
+
+ fn_or_tspec = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
- if (stat(filename, &sb))
- err(1, "%s", filename);
- new = palloc(N_NEWER, f_newer);
- new->t_data = sb.st_mtime;
- return (new);
+ new = palloc(option);
+ /* compare against what */
+ if (option->flags & F_TIME2_T) {
+ new->t_data = get_date(fn_or_tspec);
+ if (new->t_data == (time_t) -1)
+ errx(1, "Can't parse date/time: %s", fn_or_tspec);
+ } else {
+ if (stat(fn_or_tspec, &sb))
+ err(1, "%s", fn_or_tspec);
+ if (option->flags & F_TIME2_C)
+ new->t_data = sb.st_ctime;
+ else if (option->flags & F_TIME2_A)
+ new->t_data = sb.st_atime;
+ else if (option->flags & F_TIME2_B)
+ new->t_data = sb.st_birthtime;
+ else
+ new->t_data = sb.st_mtime;
+ }
+ return new;
}
-
+
/*
* -nogroup functions --
*
* of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
*/
int
-f_nogroup(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_nogroup(PLAN *plan __unused, FTSENT *entry)
{
-
- return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
+ return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
}
-
+
PLAN *
-c_nogroup(argvp, isok)
- char ***argvp;
- int isok;
+c_nogroup(OPTION *option, char ***argvp __unused)
{
ftsoptions &= ~FTS_NOSTAT;
- return (palloc(N_NOGROUP, f_nogroup));
+ return palloc(option);
}
-
+
/*
* -nouser functions --
*
* of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
*/
int
-f_nouser(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_nouser(PLAN *plan __unused, FTSENT *entry)
{
-
- return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
+ return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
}
-
+
PLAN *
-c_nouser(argvp, isok)
- char ***argvp;
- int isok;
+c_nouser(OPTION *option, char ***argvp __unused)
{
ftsoptions &= ~FTS_NOSTAT;
- return (palloc(N_NOUSER, f_nouser));
+ return palloc(option);
}
-
+
/*
* -path functions --
*
* matches pattern using Pattern Matching Notation S3.14
*/
int
-f_path(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_path(PLAN *plan, FTSENT *entry)
{
- return (!fnmatch(plan->c_data, entry->fts_path, 0));
+ return !fnmatch(plan->c_data, entry->fts_path,
+ plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
}
-
-PLAN *
-c_path(argvp, isok)
- char ***argvp;
- int isok;
-{
- char *pattern = **argvp;
- PLAN *new;
- (*argvp)++;
- new = palloc(N_NAME, f_path);
- new->c_data = pattern;
- return (new);
-}
-
+/* c_path is the same as c_name */
+
/*
* -perm functions --
*
* symbolic mode.
*/
int
-f_perm(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_perm(PLAN *plan, FTSENT *entry)
{
mode_t mode;
mode = entry->fts_statp->st_mode &
(S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
- if (plan->flags == F_ATLEAST)
- return ((plan->m_data | mode) == mode);
+ if (plan->flags & F_ATLEAST)
+ return (plan->m_data | mode) == mode;
+ else if (plan->flags & F_ANY)
+ return (mode & plan->m_data);
else
- return (mode == plan->m_data);
+ return mode == plan->m_data;
/* NOTREACHED */
}
-
+
PLAN *
-c_perm(argvp, isok)
- char ***argvp;
- int isok;
+c_perm(OPTION *option, char ***argvp)
{
- char *perm = **argvp;
+ char *perm;
PLAN *new;
mode_t *set;
- (*argvp)++;
+ perm = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
- new = palloc(N_PERM, f_perm);
+ new = palloc(option);
if (*perm == '-') {
- new->flags = F_ATLEAST;
+ new->flags |= F_ATLEAST;
++perm;
+ } else if (*perm == '+') {
+ if ((set = setmode(perm + 1)) != NULL) {
+ new->flags |= F_ANY;
+ ++perm;
+ free(set);
+ }
}
if ((set = setmode(perm)) == NULL)
- err(1, "-perm: %s: illegal mode string", perm);
+ errx(1, "%s: %s: illegal mode string", option->name, perm);
new->m_data = getmode(set, 0);
- return (new);
+ free(set);
+ return new;
}
-
+
/*
* -print functions --
*
- * Always true, causes the current pathame to be written to
+ * Always true, causes the current pathname to be written to
* standard output.
*/
int
-f_print(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_print(PLAN *plan __unused, FTSENT *entry)
{
- (void)printf("%s\n", entry->fts_path);
- return (1);
+ (void)puts(entry->fts_path);
+ return 1;
}
-int
-f_print0(plan, entry)
- PLAN *plan;
- FTSENT *entry;
-{
- (void)fputs(entry->fts_path, stdout);
- (void)fputc('\0', stdout);
- return (1);
-}
-
PLAN *
-c_print(argvp, isok)
- char ***argvp;
- int isok;
+c_print(OPTION *option, char ***argvp __unused)
{
isoutput = 1;
- return (palloc(N_PRINT, f_print));
+ return palloc(option);
}
-PLAN *
-c_print0(argvp, isok)
- char ***argvp;
- int isok;
+/*
+ * -print0 functions --
+ *
+ * Always true, causes the current pathname to be written to
+ * standard output followed by a NUL character
+ */
+int
+f_print0(PLAN *plan __unused, FTSENT *entry)
{
- isoutput = 1;
-
- return (palloc(N_PRINT0, f_print0));
+ fputs(entry->fts_path, stdout);
+ fputc('\0', stdout);
+ return 1;
}
-
+
+/* c_print0 is the same as c_print */
+
/*
* -prune functions --
*
* Prune a portion of the hierarchy.
*/
int
-f_prune(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_prune(PLAN *plan __unused, FTSENT *entry)
{
- extern FTS *tree;
-
if (fts_set(tree, entry, FTS_SKIP))
err(1, "%s", entry->fts_path);
- return (1);
+ return 1;
}
-
+
+/* c_prune == c_simple */
+
+/*
+ * -regex functions --
+ *
+ * True if the whole path of the file matches pattern using
+ * regular expression.
+ */
+int
+f_regex(PLAN *plan, FTSENT *entry)
+{
+ char *str;
+ int len;
+ regex_t *pre;
+ regmatch_t pmatch;
+ int errcode;
+ char errbuf[LINE_MAX];
+ int matched;
+
+ pre = plan->re_data;
+ str = entry->fts_path;
+ len = strlen(str);
+ matched = 0;
+
+ pmatch.rm_so = 0;
+ pmatch.rm_eo = len;
+
+ errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
+
+ if (errcode != 0 && errcode != REG_NOMATCH) {
+ regerror(errcode, pre, errbuf, sizeof errbuf);
+ errx(1, "%s: %s",
+ plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
+ }
+
+ if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
+ matched = 1;
+
+ return matched;
+}
+
PLAN *
-c_prune(argvp, isok)
- char ***argvp;
- int isok;
+c_regex(OPTION *option, char ***argvp)
{
- return (palloc(N_PRUNE, f_prune));
+ PLAN *new;
+ char *pattern;
+ regex_t *pre;
+ int errcode;
+ char errbuf[LINE_MAX];
+
+ if ((pre = malloc(sizeof(regex_t))) == NULL)
+ err(1, NULL);
+
+ pattern = nextarg(option, argvp);
+
+ if ((errcode = regcomp(pre, pattern,
+ regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
+ regerror(errcode, pre, errbuf, sizeof errbuf);
+ errx(1, "%s: %s: %s",
+ option->flags & F_IGNCASE ? "-iregex" : "-regex",
+ pattern, errbuf);
+ }
+
+ new = palloc(option);
+ new->re_data = pre;
+
+ return new;
}
-
+
+/* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
+
+PLAN *
+c_simple(OPTION *option, char ***argvp __unused)
+{
+ return palloc(option);
+}
+
/*
* -size n[c] functions --
*
* True if the file size in bytes, divided by an implementation defined
* value and rounded up to the next integer, is n. If n is followed by
- * a c, the size is in bytes.
+ * one of c k M G T P, the size is in bytes, kilobytes,
+ * megabytes, gigabytes, terabytes or petabytes respectively.
*/
#define FIND_SIZE 512
static int divsize = 1;
int
-f_size(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_size(PLAN *plan, FTSENT *entry)
{
off_t size;
FIND_SIZE : entry->fts_statp->st_size;
COMPARE(size, plan->o_data);
}
-
+
PLAN *
-c_size(argvp, isok)
- char ***argvp;
- int isok;
+c_size(OPTION *option, char ***argvp)
{
- char *arg = **argvp;
+ char *size_str;
PLAN *new;
char endch;
-
- (*argvp)++;
+ off_t scale;
+
+ size_str = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
- new = palloc(N_SIZE, f_size);
+ new = palloc(option);
endch = 'c';
- new->o_data = find_parsenum(new, "-size", arg, &endch);
- if (endch == 'c')
+ new->o_data = find_parsenum(new, option->name, size_str, &endch);
+ if (endch != '\0') {
divsize = 0;
- return (new);
+
+ switch (endch) {
+ case 'c': /* characters */
+ scale = 0x1LL;
+ break;
+ case 'k': /* kilobytes 1<<10 */
+ scale = 0x400LL;
+ break;
+ case 'M': /* megabytes 1<<20 */
+ scale = 0x100000LL;
+ break;
+ case 'G': /* gigabytes 1<<30 */
+ scale = 0x40000000LL;
+ break;
+ case 'T': /* terabytes 1<<40 */
+ scale = 0x1000000000LL;
+ break;
+ case 'P': /* petabytes 1<<50 */
+ scale = 0x4000000000000LL;
+ break;
+ default:
+ errx(1, "%s: %s: illegal trailing character",
+ option->name, size_str);
+ break;
+ }
+ if (new->o_data > QUAD_MAX / scale)
+ errx(1, "%s: %s: value too large",
+ option->name, size_str);
+ new->o_data *= scale;
+ }
+ return new;
}
-
+
/*
* -type c functions --
*
* regular file or whiteout respectively.
*/
int
-f_type(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_type(PLAN *plan, FTSENT *entry)
{
- return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
+ return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
}
-
+
PLAN *
-c_type(argvp, isok)
- char ***argvp;
- int isok;
+c_type(OPTION *option, char ***argvp)
{
- char *typestring = **argvp;
+ char *typestring;
PLAN *new;
- mode_t mask = (mode_t)0;
-
- (*argvp)++;
+ mode_t mask;
+
+ typestring = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
switch (typestring[0]) {
-#ifdef S_IFWHT
- case 'W':
-#ifdef FTS_WHITEOUT
- ftsoptions |= FTS_WHITEOUT;
-#endif
- mask = S_IFWHT;
- break;
-#endif
case 'b':
mask = S_IFBLK;
break;
break;
#endif /* FTS_WHITEOUT */
default:
- errx(1, "-type: %s: unknown type", typestring);
+ errx(1, "%s: %s: unknown type", option->name, typestring);
}
-
- new = palloc(N_TYPE, f_type);
+
+ new = palloc(option);
new->m_data = mask;
- return (new);
+ return new;
}
-
+
/*
* -user uname functions --
*
* return a valid user name, uname is taken as a user ID.
*/
int
-f_user(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_user(PLAN *plan, FTSENT *entry)
{
- return (entry->fts_statp->st_uid == plan->u_data);
+ COMPARE(entry->fts_statp->st_uid, plan->u_data);
}
-
+
PLAN *
-c_user(argvp, isok)
- char ***argvp;
- int isok;
+c_user(OPTION *option, char ***argvp)
{
- char *username = **argvp;
+ char *username;
PLAN *new;
struct passwd *p;
uid_t uid;
-
- (*argvp)++;
+
+ username = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
+ new = palloc(option);
p = getpwnam(username);
if (p == NULL) {
+ char* cp = username;
+ if( username[0] == '-' || username[0] == '+' )
+ username++;
uid = atoi(username);
if (uid == 0 && username[0] != '0')
- errx(1, "-user: %s: no such user", username);
+ errx(1, "%s: %s: no such user", option->name, username);
+ uid = find_parsenum(new, option->name, cp, NULL);
} else
uid = p->pw_uid;
- new = palloc(N_USER, f_user);
new->u_data = uid;
- return (new);
+ return new;
}
-
+
/*
* -xdev functions --
*
- * Always true, causes find not to decend past directories that have a
+ * Always true, causes find not to descend past directories that have a
* different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
*/
PLAN *
-c_xdev(argvp, isok)
- char ***argvp;
- int isok;
+c_xdev(OPTION *option, char ***argvp __unused)
{
ftsoptions |= FTS_XDEV;
- return (palloc(N_XDEV, f_always_true));
+ return palloc(option);
}
/*
* True if expression is true.
*/
int
-f_expr(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_expr(PLAN *plan, FTSENT *entry)
{
PLAN *p;
- int state;
+ int state = 0;
- state = 0;
for (p = plan->p_data[0];
- p && (state = (p->eval)(p, entry)); p = p->next);
- return (state);
+ p && (state = (p->execute)(p, entry)); p = p->next);
+ return state;
}
-
+
/*
- * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
+ * f_openparen and f_closeparen nodes are temporary place markers. They are
* eliminated during phase 2 of find_formplan() --- the '(' node is converted
- * to a N_EXPR node containing the expression and the ')' node is discarded.
+ * to a f_expr node containing the expression and the ')' node is discarded.
+ * The functions themselves are only used as constants.
*/
-PLAN *
-c_openparen(argvp, isok)
- char ***argvp;
- int isok;
+
+int
+f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
{
- return (palloc(N_OPENPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
+ abort();
}
-
+
+int
+f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
+{
+ abort();
+}
+
+/* c_openparen == c_simple */
+/* c_closeparen == c_simple */
+
+/*
+ * AND operator. Since AND is implicit, no node is allocated.
+ */
PLAN *
-c_closeparen(argvp, isok)
- char ***argvp;
- int isok;
+c_and(OPTION *option __unused, char ***argvp __unused)
{
- return (palloc(N_CLOSEPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
+ return NULL;
}
-
+
/*
* ! expression functions --
*
* Negation of a primary; the unary NOT operator.
*/
int
-f_not(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_not(PLAN *plan, FTSENT *entry)
{
PLAN *p;
- int state;
+ int state = 0;
- state = 0;
for (p = plan->p_data[0];
- p && (state = (p->eval)(p, entry)); p = p->next);
- return (!state);
+ p && (state = (p->execute)(p, entry)); p = p->next);
+ return !state;
}
-
-PLAN *
-c_not(argvp, isok)
- char ***argvp;
- int isok;
-{
- return (palloc(N_NOT, f_not));
-}
-
+
+/* c_not == c_simple */
+
/*
* expression -o expression functions --
*
* not evaluated if the first expression is true.
*/
int
-f_or(plan, entry)
- PLAN *plan;
- FTSENT *entry;
+f_or(PLAN *plan, FTSENT *entry)
{
PLAN *p;
- int state;
+ int state = 0;
- state = 0;
for (p = plan->p_data[0];
- p && (state = (p->eval)(p, entry)); p = p->next);
+ p && (state = (p->execute)(p, entry)); p = p->next);
if (state)
- return (1);
+ return 1;
for (p = plan->p_data[1];
- p && (state = (p->eval)(p, entry)); p = p->next);
- return (state);
+ p && (state = (p->execute)(p, entry)); p = p->next);
+ return state;
}
-PLAN *
-c_or(argvp, isok)
- char ***argvp;
- int isok;
-{
- return (palloc(N_OR, f_or));
-}
+/* c_or == c_simple */
-PLAN *
-c_null(argvp, isok)
- char ***argvp;
- int isok;
+/*
+ * -false
+ *
+ * Always false.
+ */
+int
+f_false(PLAN *plan __unused, FTSENT *entry __unused)
{
- return NULL;
+ return 0;
}
-static PLAN *
-palloc(t, f)
- enum ntype t;
- int (*f) __P((PLAN *, FTSENT *));
-{
- PLAN *new;
+/* c_false == c_simple */
- if ((new = malloc(sizeof(PLAN))) == NULL)
- err(1, "%s", "");
- new->type = t;
- new->eval = f;
- new->flags = 0;
- new->next = NULL;
- return (new);
+/*
+ * -quit
+ *
+ * Exits the program
+ */
+int
+f_quit(PLAN *plan __unused, FTSENT *entry __unused)
+{
+ exit(0);
}
+
+/* c_quit == c_simple */