add -regex and -iregex primaries which, like GNU find's primaries of the

same name, match files' entire paths against regular expressions.
-regex is case sensitive, -iregex is case-insensitive.  Note that these
primaries are _not_ entirely compatible with the GNU find primaries,
because their BREs appear to support alternation with \| whereas our BREs
do not.  Also note there are no primaries which provide extended regular
expressions matching, though if they are desired they would be trivial
to implement.
This commit is contained in:
cgd 1999-07-20 01:28:41 +00:00
parent 988906cf52
commit 91ff0a1b87
5 changed files with 98 additions and 9 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.11 1999/01/16 13:27:30 simonb Exp $ */
/* $NetBSD: extern.h,v 1.12 1999/07/20 01:28:41 cgd Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -61,6 +61,7 @@ PLAN *c_follow __P((char ***, int));
PLAN *c_fstype __P((char ***, int));
PLAN *c_group __P((char ***, int));
PLAN *c_inum __P((char ***, int));
PLAN *c_iregex __P((char ***, int));
PLAN *c_links __P((char ***, int));
PLAN *c_ls __P((char ***, int));
PLAN *c_mmin __P((char ***, int));
@ -75,6 +76,7 @@ PLAN *c_print __P((char ***, int));
PLAN *c_print0 __P((char ***, int));
PLAN *c_printx __P((char ***, int));
PLAN *c_prune __P((char ***, int));
PLAN *c_regex __P((char ***, int));
PLAN *c_size __P((char ***, int));
PLAN *c_type __P((char ***, int));
PLAN *c_user __P((char ***, int));

View File

@ -1,4 +1,4 @@
.\" $NetBSD: find.1,v 1.21 1999/04/30 00:52:58 simonb Exp $
.\" $NetBSD: find.1,v 1.22 1999/07/20 01:28:41 cgd Exp $
.\"
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -239,6 +239,13 @@ is treated as a group id.
.It Ic -inum Ar n
True if the file has inode number
.Ar n .
.It Ic -iregex Ar regexp
True if the path name of the current file matches the case-insensitive
basic regular expression
.Pq see Xr re_format 7
.Ar regexp .
This is a match on the whole path, not a search for the regular expression
within the path.
.It Ic -links Ar n
True if the file has
.Ar n
@ -377,6 +384,13 @@ Note, the
primary has no effect if the
.Fl d
option was specified.
.It Ic -regex Ar regexp
True if the path name of the current file matches the case-sensitive
basic regular expression
.Pq see Xr re_format 7
.Ar regexp .
This is a match on the whole path, not a search for the regular expression
within the path.
.It Ic -size Ar n Ns Op Cm c
True if the file's size, rounded up, in 512\-byte blocks is
.Ar n .

View File

@ -1,4 +1,4 @@
/* $NetBSD: find.h,v 1.11 1999/01/16 13:27:30 simonb Exp $ */
/* $NetBSD: find.h,v 1.12 1999/07/20 01:28:41 cgd Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -38,14 +38,17 @@
* from: @(#)find.h 8.1 (Berkeley) 6/6/93
*/
#include <regex.h>
/* node type */
enum ntype {
N_AND = 1, /* must start > 0 */
N_AMIN, N_ATIME, N_CLOSEPAREN, N_CMIN, N_CTIME, N_DEPTH, N_EXEC,
N_EXPR, N_FLAGS, N_FOLLOW, N_FSTYPE, N_GROUP, N_INUM, N_LINKS, N_LS,
N_EXPR, N_FLAGS, N_FOLLOW, N_FSTYPE, N_GROUP, N_INUM, N_IREGEX,
N_LINKS, N_LS,
N_MMIN, N_MTIME, N_NAME, N_NEWER, N_NOGROUP, N_NOT, N_NOUSER, N_OK,
N_OPENPAREN, N_OR, N_PATH, N_PERM, N_PRINT, N_PRINT0, N_PRINTX,
N_PRUNE, N_SIZE, N_TYPE, N_USER, N_XDEV,
N_PRUNE, N_REGEX, N_SIZE, N_TYPE, N_USER, N_XDEV,
};
/* node definition */
@ -80,6 +83,7 @@ typedef struct _plandata {
} ex;
char *_a_data[2]; /* array of char pointers */
char *_c_data; /* char pointer */
regex_t _regexp_data; /* compiled regexp */
} p_un;
} PLAN;
#define a_data p_un._a_data
@ -97,6 +101,7 @@ typedef struct _plandata {
#define e_argv p_un.ex._e_argv
#define e_orig p_un.ex._e_orig
#define e_len p_un.ex._e_len
#define regexp_data p_un._regexp_data
typedef struct _option {
char *name; /* option name */

View File

@ -1,4 +1,4 @@
/* $NetBSD: function.c,v 1.30 1999/02/04 16:41:17 kleink Exp $ */
/* $NetBSD: function.c,v 1.31 1999/07/20 01:28:41 cgd Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: function.c,v 1.30 1999/02/04 16:41:17 kleink Exp $");
__RCSID("$NetBSD: function.c,v 1.31 1999/07/20 01:28:41 cgd Exp $");
#endif
#endif /* not lint */
@ -104,11 +104,13 @@ static int64_t find_parsenum __P((PLAN *, char *, char *, char *));
int f_print0 __P((PLAN *, FTSENT *));
int f_printx __P((PLAN *, FTSENT *));
int f_prune __P((PLAN *, FTSENT *));
int f_regex __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 *c_regex_common __P((char ***, int, enum ntype, int));
static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
/*
@ -1069,7 +1071,71 @@ c_prune(argvp, isok)
{
return (palloc(N_PRUNE, f_prune));
}
/*
* -regex regexp (and related) functions --
*
* True if the complete file path matches the regular expression regexp.
* For -regex, regexp is a case-sensitive (basic) regular expression.
* For -iregex, regexp is a case-insensitive (basic) regular expression.
*/
int
f_regex(plan, entry)
PLAN *plan;
FTSENT *entry;
{
return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
}
static PLAN *
c_regex_common(argvp, isok, type, regcomp_flags)
char ***argvp;
int isok, regcomp_flags;
enum ntype type;
{
char errbuf[LINE_MAX];
regex_t reg;
char *regexp = **argvp;
char *lineregexp;
PLAN *new;
int rv;
(*argvp)++;
lineregexp = alloca(strlen(regexp) + 1 + 6); /* max needed */
sprintf(lineregexp, "^%s(%s%s)$",
(regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
(regcomp_flags & REG_EXTENDED) ? "" : "\\");
rv = regcomp(&reg, lineregexp, REG_NOSUB|regcomp_flags);
if (rv != 0) {
regerror(rv, &reg, errbuf, sizeof errbuf);
errx(1, "regexp %s: %s", regexp, errbuf);
}
new = palloc(type, f_regex);
new->regexp_data = reg;
return (new);
}
PLAN *
c_regex(argvp, isok)
char ***argvp;
int isok;
{
return (c_regex_common(argvp, isok, N_REGEX, REG_BASIC));
}
PLAN *
c_iregex(argvp, isok)
char ***argvp;
int isok;
{
return (c_regex_common(argvp, isok, N_IREGEX, REG_BASIC|REG_ICASE));
}
/*
* -size n[c] functions --
*

View File

@ -1,4 +1,4 @@
/* $NetBSD: option.c,v 1.12 1999/01/16 13:27:31 simonb Exp $ */
/* $NetBSD: option.c,v 1.13 1999/07/20 01:28:42 cgd Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "from: @(#)option.c 8.2 (Berkeley) 4/16/94";
#else
__RCSID("$NetBSD: option.c,v 1.12 1999/01/16 13:27:31 simonb Exp $");
__RCSID("$NetBSD: option.c,v 1.13 1999/07/20 01:28:42 cgd Exp $");
#endif
#endif /* not lint */
@ -77,6 +77,7 @@ static OPTION const options[] = {
{ "-fstype", N_FSTYPE, c_fstype, 1 },
{ "-group", N_GROUP, c_group, 1 },
{ "-inum", N_INUM, c_inum, 1 },
{ "-iregex", N_IREGEX, c_iregex, 1 },
{ "-links", N_LINKS, c_links, 1 },
{ "-ls", N_LS, c_ls, 0 },
{ "-mmin", N_MMIN, c_mmin, 1 },
@ -94,6 +95,7 @@ static OPTION const options[] = {
{ "-print0", N_PRINT0, c_print0, 0 },
{ "-printx", N_PRINTX, c_printx, 0 },
{ "-prune", N_PRUNE, c_prune, 0 },
{ "-regex", N_REGEX, c_regex, 1 },
{ "-size", N_SIZE, c_size, 1 },
{ "-type", N_TYPE, c_type, 1 },
{ "-user", N_USER, c_user, 1 },