Add a new primary '-exit n':

This primary causes find to stop traversing the filesystem and
exit immediately if a previous condition was met.  If no value is
specified, the exit value will be 0, else n.  Note that other
primaries will be evaluated and acted upon before exiting.

Ok matt@, garbled@.
This commit is contained in:
jschauma 2006-02-20 16:31:02 +00:00
parent 6f20259365
commit a32f19d465
6 changed files with 69 additions and 17 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.22 2005/11/09 00:47:16 reed Exp $ */ /* $NetBSD: extern.h,v 1.23 2006/02/20 16:31:02 jschauma Exp $ */
/*- /*-
* Copyright (c) 1991, 1993, 1994 * Copyright (c) 1991, 1993, 1994
@ -56,6 +56,7 @@ PLAN *c_depth __P((char ***, int));
PLAN *c_empty __P((char ***, int)); PLAN *c_empty __P((char ***, int));
PLAN *c_exec __P((char ***, int)); PLAN *c_exec __P((char ***, int));
PLAN *c_execdir __P((char ***, int)); PLAN *c_execdir __P((char ***, int));
PLAN *c_exit __P((char ***, int));
PLAN *c_false __P((char ***, int)); PLAN *c_false __P((char ***, int));
PLAN *c_flags __P((char ***, int)); PLAN *c_flags __P((char ***, int));
PLAN *c_follow __P((char ***, int)); PLAN *c_follow __P((char ***, int));

View File

@ -1,4 +1,4 @@
.\" $NetBSD: find.1,v 1.55 2005/11/09 00:47:16 reed Exp $ .\" $NetBSD: find.1,v 1.56 2006/02/20 16:31:02 jschauma Exp $
.\" .\"
.\" Copyright (c) 1990, 1993 .\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
@ -32,7 +32,7 @@
.\" .\"
.\" from: @(#)find.1 8.7 (Berkeley) 5/9/95 .\" from: @(#)find.1 8.7 (Berkeley) 5/9/95
.\" .\"
.Dd November 8, 2005 .Dd February 20, 2006
.Dt FIND 1 .Dt FIND 1
.Os .Os
.Sh NAME .Sh NAME
@ -232,6 +232,14 @@ the current file.
The filename substituted for the string The filename substituted for the string
.Dq {} .Dq {}
is not qualified. is not qualified.
.It Ic -exit Op Ar n
This primary causes
.Nm
to stop traversing the filesystem and exit immediately if a
previous condition was met.
If no value is specified, the exit value will be 0, else
.Ar n .
Note that other primaries will be evaluated and acted upon before exiting.
.It Ic -false .It Ic -false
This primary always evaluates to false. This primary always evaluates to false.
This can be used following a primary that caused the This can be used following a primary that caused the
@ -454,6 +462,7 @@ It prints the pathname of the current file to standard output, followed
by a newline character. by a newline character.
If none of If none of
.Ic -exec , .Ic -exec ,
.Ic -exit ,
.Ic -fprint , .Ic -fprint ,
.Ic -ls , .Ic -ls ,
.Ic -ok , .Ic -ok ,
@ -611,6 +620,16 @@ Print out a list of all the files that are either owned by
.Dq wnj .Dq wnj
or that are newer than or that are newer than
.Dq ttt . .Dq ttt .
.It Li "find / \e( -newer ttt -or -user wnj \e) -exit 1"
Return immediately with a value of 1 if any files are found that are either
owned by
.Dq wnj
or that are newer than
.Dq ttt ,
but do not print them.
.It Li "find / \e( -newer ttt -or -user wnj \e) -ls -exit 1"
Same as above, but list the first file matching the criteria before exiting
with a value of 1.
.El .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr chflags 1 , .Xr chflags 1 ,

View File

@ -1,4 +1,4 @@
/* $NetBSD: find.c,v 1.20 2005/10/12 20:03:59 reed Exp $ */ /* $NetBSD: find.c,v 1.21 2006/02/20 16:31:02 jschauma Exp $ */
/*- /*-
* Copyright (c) 1991, 1993, 1994 * Copyright (c) 1991, 1993, 1994
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "from: @(#)find.c 8.5 (Berkeley) 8/5/94"; static char sccsid[] = "from: @(#)find.c 8.5 (Berkeley) 8/5/94";
#else #else
__RCSID("$NetBSD: find.c,v 1.20 2005/10/12 20:03:59 reed Exp $"); __RCSID("$NetBSD: find.c,v 1.21 2006/02/20 16:31:02 jschauma Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -98,9 +98,9 @@ find_formplan(argv)
} }
/* /*
* if the user didn't specify one of -print, -ok, -fprint, or -exec, * if the user didn't specify one of -print, -ok, -fprint, -exec, or
* then -print is assumed so we bracket the current expression with * -exit, then -print is assumed so we bracket the current expression
* parens, if necessary, and add a -print node on the end. * with parens, if necessary, and add a -print node on the end.
*/ */
if (!isoutput) { if (!isoutput) {
if (plan == NULL) { if (plan == NULL) {
@ -189,14 +189,16 @@ find_execute(plan, paths)
char **paths; /* array of pathnames to traverse */ char **paths; /* array of pathnames to traverse */
{ {
PLAN *p; PLAN *p;
int rval; int rval, cval;
sigset_t s; sigset_t s;
cval = 1;
if (!(tree = fts_open(paths, ftsoptions, issort ? ftscompare : NULL))) if (!(tree = fts_open(paths, ftsoptions, issort ? ftscompare : NULL)))
err(1, "ftsopen"); err(1, "ftsopen");
sig_lock(&s); sig_lock(&s);
for (rval = 0; (g_entry = fts_read(tree)) != NULL; sig_lock(&s)) { for (rval = 0; cval && (g_entry = fts_read(tree)) != NULL; sig_lock(&s)) {
sig_unlock(&s); sig_unlock(&s);
switch (g_entry->fts_info) { switch (g_entry->fts_info) {
case FTS_D: case FTS_D:
@ -230,8 +232,12 @@ find_execute(plan, paths)
* the work specified by the user on the command line. * the work specified by the user on the command line.
*/ */
for (p = plan; p && (p->eval)(p, g_entry); p = p->next) for (p = plan; p && (p->eval)(p, g_entry); p = p->next)
; if (p->type == N_EXIT) {
rval = p->exit_val;
cval = 0;
} }
}
sig_unlock(&s); sig_unlock(&s);
if (errno) if (errno)
err(1, "fts_read"); err(1, "fts_read");

View File

@ -1,4 +1,4 @@
/* $NetBSD: find.h,v 1.20 2005/11/09 00:47:16 reed Exp $ */ /* $NetBSD: find.h,v 1.21 2006/02/20 16:31:02 jschauma Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -40,7 +40,7 @@
enum ntype { enum ntype {
N_AND = 1, /* must start > 0 */ N_AND = 1, /* must start > 0 */
N_AMIN, N_ANEWER, N_ATIME, N_CLOSEPAREN, N_CMIN, N_CNEWER, N_CTIME, N_AMIN, N_ANEWER, N_ATIME, N_CLOSEPAREN, N_CMIN, N_CNEWER, N_CTIME,
N_DEPTH, N_EMPTY, N_EXEC, N_EXECDIR, N_EXPR, N_FALSE, N_FLAGS, N_DEPTH, N_EMPTY, N_EXEC, N_EXECDIR, N_EXIT, N_EXPR, N_FALSE, N_FLAGS,
N_FOLLOW, N_FPRINT, N_FSTYPE, N_GROUP, N_FOLLOW, N_FPRINT, N_FSTYPE, N_GROUP,
N_INAME, N_INUM, N_IREGEX, N_LINKS, N_LS, N_MINDEPTH, N_MAXDEPTH, N_INAME, N_INUM, N_IREGEX, N_LINKS, N_LS, N_MINDEPTH, N_MAXDEPTH,
N_MMIN, N_MTIME, N_NAME, N_NEWER, N_NOGROUP, N_NOT, N_NOUSER, N_OK, N_MMIN, N_MTIME, N_NAME, N_NEWER, N_NOGROUP, N_NOT, N_NOUSER, N_OK,
@ -80,6 +80,7 @@ typedef struct _plandata {
} ex; } ex;
char *_a_data[2]; /* array of char pointers */ char *_a_data[2]; /* array of char pointers */
char *_c_data; /* char pointer */ char *_c_data; /* char pointer */
int _exit_val; /* exit value */
int _max_data; /* tree depth */ int _max_data; /* tree depth */
int _min_data; /* tree depth */ int _min_data; /* tree depth */
regex_t _regexp_data; /* compiled regexp */ regex_t _regexp_data; /* compiled regexp */
@ -101,6 +102,7 @@ typedef struct _plandata {
#define e_argv p_un.ex._e_argv #define e_argv p_un.ex._e_argv
#define e_orig p_un.ex._e_orig #define e_orig p_un.ex._e_orig
#define e_len p_un.ex._e_len #define e_len p_un.ex._e_len
#define exit_val p_un._exit_val
#define max_data p_un._max_data #define max_data p_un._max_data
#define min_data p_un._min_data #define min_data p_un._min_data
#define regexp_data p_un._regexp_data #define regexp_data p_un._regexp_data

View File

@ -1,4 +1,4 @@
/* $NetBSD: function.c,v 1.52 2005/11/09 00:47:16 reed Exp $ */ /* $NetBSD: function.c,v 1.53 2006/02/20 16:31:02 jschauma Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95"; static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95";
#else #else
__RCSID("$NetBSD: function.c,v 1.52 2005/11/09 00:47:16 reed Exp $"); __RCSID("$NetBSD: function.c,v 1.53 2006/02/20 16:31:02 jschauma Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -648,6 +648,29 @@ c_execdir(argvp, isok)
return (new); return (new);
} }
PLAN *
c_exit(argvp, isok)
char ***argvp;
int isok;
{
char *arg = **argvp;
PLAN *new;
/* not technically true, but otherwise '-print' is implied */
isoutput = 1;
new = palloc(N_EXIT, f_always_true);
if (arg) {
(*argvp)++;
new->exit_val = find_parsenum(new, "-exit", arg, NULL);
} else
new->exit_val = 0;
return (new);
}
/* /*
* -false function * -false function
*/ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: option.c,v 1.22 2005/11/09 00:47:16 reed Exp $ */ /* $NetBSD: option.c,v 1.23 2006/02/20 16:31:03 jschauma Exp $ */
/*- /*-
* Copyright (c) 1990, 1993, 1994 * Copyright (c) 1990, 1993, 1994
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "from: @(#)option.c 8.2 (Berkeley) 4/16/94"; static char sccsid[] = "from: @(#)option.c 8.2 (Berkeley) 4/16/94";
#else #else
__RCSID("$NetBSD: option.c,v 1.22 2005/11/09 00:47:16 reed Exp $"); __RCSID("$NetBSD: option.c,v 1.23 2006/02/20 16:31:03 jschauma Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -72,6 +72,7 @@ static OPTION const options[] = {
{ "-empty", N_EMPTY, c_empty, 0 }, { "-empty", N_EMPTY, c_empty, 0 },
{ "-exec", N_EXEC, c_exec, 1 }, { "-exec", N_EXEC, c_exec, 1 },
{ "-execdir", N_EXECDIR, c_execdir, 1 }, { "-execdir", N_EXECDIR, c_execdir, 1 },
{ "-exit", N_EXIT, c_exit, 0 },
{ "-false", N_FALSE, c_false, 0 }, { "-false", N_FALSE, c_false, 0 },
{ "-flags", N_FLAGS, c_flags, 1 }, { "-flags", N_FLAGS, c_flags, 1 },
{ "-follow", N_FOLLOW, c_follow, 0 }, { "-follow", N_FOLLOW, c_follow, 0 },