Home | History | Annotate | Download | only in find
History log of /src/usr.bin/find/function.c
RevisionDateAuthorComments
 1.81  05-Feb-2024  andvar fix various typos in comments.
 1.80  01-Apr-2023  christos PR/57313: Timo Buhrmester: Don't bail if "." cannot be opened. From FreeBSD
 1.79  18-Mar-2021  cheusov branches: 1.79.6;
find: use POSIX strtoll(3) instead of legacy strtoq(3)
 1.78  18-Mar-2021  cheusov find: use POSIX type uint32_t instead of u_int32_t
 1.77  04-Sep-2018  kre branches: 1.77.2;
Inspired by PR pkg/53543

When calculating the length of the args that can be
appended in a "find .... -exec something {} +"
usage, remember to allow for the arg pointers, which
form part of what is allowed in ARG_MAX.

From a fairly empty installation of HEAD on amd64
and with a "/tmp/args" command that simply prints
its arg count, and the length of the arg strings,
with this mod I see ..

netbsd# find / -exec /tmp/args {} +
Argc 5000 Arglen 107645
Argc 5000 Arglen 151324
Argc 5000 Arglen 187725
Argc 5000 Arglen 206591
Argc 5000 Arglen 172909
Argc 5000 Arglen 186264
Argc 5000 Arglen 167906
Argc 2881 Arglen 98260

The upper limit of 5000 args is in the code.

Using the biggest of those, 5000
args, plus 206591 bytes of strings
uses 246591 bytes total (this excludes
the command name, so add a few more).
That's fairly close to the ARG_MAX
of 262144.

On another system (with longer paths) I see:
(this is just a small part of the output, using a
different version of the dummy command, and a
slightly different invocation)

Args: 4546 Len 218030
Args: 4878 Len 217991
Args: 4813 Len 218028
Args: 4803 Len 218029

There, 4878*8 + 217991 == 257015 which is about
as close as we'd want to come to the arg limit.

XXX pullup -8
 1.76  13-Jun-2017  christos branches: 1.76.4; 1.76.6;
PR/52295: Anthony Mallet: find -delete: "relative path not safe" with absolute
paths ... Fix from OpenBSD
 1.75  13-Jun-2016  pgoyette branches: 1.75.8;
Fix missing quote. Resolve automated test failure.
 1.74  13-Jun-2016  pgoyette Add new primaries -asince, -csince, and -since to compare file's
attributes against a user-specified timestamp (rather than the
attributes of a reference file).

Update the parse routines so they have access to the name of the
option being parsed. This enables accurate error reporting for
"aliases" of primaries.

Now that aliases work, introduce some aliases for consistency with
Gnu findutils.
 1.73  12-Jun-2016  dholland Extend the numeric handling for uids with -user to gids with -group, and
document it. Leftover bit of PR 46158.
 1.72  04-May-2013  uebayasi branches: 1.72.6;
find(1): Compare timestamp in nsec scale in -anewer/-cnewer/-newer.
 1.71  26-Aug-2012  wiz branches: 1.71.2;
Make order of words in comment consistent with that within other
comments (helps with search actions).

Patch from Bug Hunting.
 1.70  05-May-2012  dholland rest of previous.
BQS: 1. dholland: 0.
 1.69  05-May-2012  dholland typo in comment
 1.68  20-Mar-2012  matt Use C89 function definitions
 1.67  22-Sep-2011  christos branches: 1.67.2;
Fix unchecked malloc, check for overflow (Maksymilian Arciemowicz)
While here, remove unused casts, fix types.
 1.66  24-Feb-2011  jmcneill Don't error out while searching for empty directories, from FreeBSD:

http://lists.freebsd.org/pipermail/svn-src-head/2010-December/022913.html
 1.65  06-Jan-2011  dholland branches: 1.65.2;
Fix "-exec blah blah {} +" so it only matches when the {} is last, as
per the standard. Per (brief) discussion on tech-userlevel.

There should really be a form where you can do the equivalent of
"-exec blah {} blah +", but I think we're going to need to call it
something other than -exec. As it is it's sort of surprising that the
standards people didn't add a different name -- note what happens if
you try to do something like "find ... -exec expr {} + 2 \;".
 1.64  19-Jul-2007  daniel branches: 1.64.16;
Add the '-E' option to interpret regexes as extended regexes. While we
are here, fix ordering in usage information by putting '-X' in the proper
place.

Addition of '-E' was discussed on tech-userlevel.
 1.63  17-Jul-2007  christos eliminate MFSNAMELEN
 1.62  06-Feb-2007  elad Add -delete from FreeBSD.
 1.61  02-Feb-2007  christos fix spello (from Anon Ymous)
 1.60  14-Dec-2006  he Adapt find to the move of string_to_flags() and flags_to_string() to
libutil.
 1.59  09-Nov-2006  christos eliminate alloca use.
 1.58  12-Oct-2006  tacha All members of PLAN should be initialized.
 1.57  11-Oct-2006  apb De-__P(), remove trailing spaces, sprinkle a few const, WARNS=4.
 1.56  07-Oct-2006  apb Add support for "find ... -exec ... {} +".

The code is from John Hawkinson in PR 20470. I adapted it to current,
and made some KNF and comment changes.
 1.55  26-Aug-2006  christos More programs using efun.
 1.54  10-May-2006  mrg quell GCC 4.1 uninitialised variable warnings.

XXX: we should audit the tree for which old ones are no longer needed
after getting the older compilers out of the tree..
 1.53  20-Feb-2006  jschauma 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@.
 1.52  09-Nov-2005  reed Add -false switch. From man page:

-false This primary always evaluates to false. This can be used follow-
ing a primary that caused the expression to be true to make the
expression to be false. This can be useful after using a -fprint
primary so it can continue to the next expression (using an -or
operator, for example).

This was brought up on the tech-userlevel list in October.

Using -fprint on findutils or new NetBSD find(1) does not do what
I wanted. For example, if saving results of all files that start
with a vowel or saving results of all files owned by group operator,
then the list of files owned by group operator would not include
the files starting with a vowel.

findutils's find has a workaround for this with -false and also a
"," comma opeator. (I made add this comma operator later; you can use
the comma to perform multiple independent tests.)
 1.51  12-Oct-2005  reed This adds -fprint function. The primary name "-fprint" (but not the
code) comes from findutils; it behaves the same.

From my manpage addition:

-fprint filename
This primary always evaluates to true. This creates filename or
overwrites the file if it already exists. The file is created at
startup. It writes the pathname of the current file to this
file, followed by a newline character. The file will be empty if
no files are matched.

Here is an example usage:

find /etc \( -name "*pass*" -fprint file1 \) -o \( -group operator -fprint file2 \) -o -name "w*"

Note that this example will NOT include entry in file2 if it is
matched in first expression. (This also is same behaviour as
findutils, and I have implemented a -false primary to handle that.
I will commit it later.)

This creates the file as command line argument parsing time.
If there is an error somewhere on that line, such as missing values
or mismatched parenthesis, then a file may still be created.
(Even if a later -fprint filename is unwritable.) This is similar
behaviour to findutils. (It has been suggested that this find could
be code to create the files in an extra stage after the command-line
argument parsing and before the actual function processing.)

I will add -fprintx and -fprint0 soon.

This was discussed on tech-userlevel.
 1.50  01-Oct-2005  christos fix setmode error handling.
 1.49  28-Dec-2004  atatat Attack of the whiteout police, including the "fix whitespace problems"
department followed by the "and fix the the ifdefs as well" brigade,
leading to a janitorial "just combine those two" strike team.
 1.48  21-Apr-2004  christos Replace the statfs() family of system calls with statvfs().
Retain binary compatibility.
 1.47  30-Mar-2004  heas Whitespace nit
 1.46  07-Aug-2003  agc branches: 1.46.4;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22365, verified by myself.
 1.45  03-Aug-2003  provos Implement -iname for case insensitive matching on file names.
From freebsd/openbsd. Approved by jaromir@, manu@, perry@.
 1.44  12-Jul-2003  itojun strlcpy
 1.43  30-Jan-2003  jhawk Obey preceding - and + on -user when a numeric uid is specified (only).
Our behavior is now consistent with Solaris, and more useful than previous.

Unfortunately we end up strtol()-ing twice (once via atoi()) to avoid
changing find_parsenum().
 1.42  26-Jan-2003  matt With -printx also qoute $ and ` (since they are shell metacharaters).
 1.41  27-Sep-2002  provos support for -empty, -execdir, -mindepth, -maxdepth to match other UNIX-like
systems. based on work by tholo@openbsd.org. approved by perry.
 1.40  02-Dec-2001  kleink Since we've already been down that road with -cnewer, support GNU find(1)'s
-anewer as well.
 1.39  01-Dec-2001  kleink Add a "cnewer" primary which evaluates true if a file has a more recent
ctime than its argument.

From kre in PR bin/14802; originally suggested name was "updated" but
renamed due to GNU find(1) being prior art for this functionality.
 1.38  21-Sep-2001  enami Reallocate memory correctly while substituting the braces.
 1.37  18-Sep-2001  simonb Back out previous vfork->fork change now that the cause of the problem
(execvp) has been fixed.
 1.36  14-Sep-2001  simonb Use fork() instead of vfork(). The child calls execvp(), which calls
strdup(), which calls malloc()...

Fixes problem with "find .. -exec" growing as reported by Kazushi Marukawa
on current-users@.
 1.35  05-Feb-2001  christos fixed nested externs
 1.34  10-Oct-2000  enami - The type of return value of setmode is a void * and getmode takes it,
rather than mode_t *.
- Free the storage allocated by setmode unless it is obvious that program
exits immediately.
 1.33  16-Mar-2000  enami Cosmetic changes.
 1.32  09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.31  20-Jul-1999  cgd branches: 1.31.4;
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.
 1.30  04-Feb-1999  kleink branches: 1.30.2;
Don't choke on file size specifications (legitimately) exceeding LONG_MAX.
 1.29  16-Jan-1999  simonb Add GNU-style -{a,c,m}min primaries.
 1.28  12-Jan-1999  lukem Add support for -printx, which quotes pathnames in an xargs friendly way.
From [bin/6790] by Eric Fischer <eric@fudge.uchicago.edu>
 1.27  03-Jan-1999  lukem Add support for "-flags [-]flags", which matches the file flags in a
similar way that "-perm [-]mode" matches the file mode.
 1.26  06-Nov-1998  christos we don't need sys/ucred.h
 1.25  14-Oct-1998  wsanchez init mask to silence -Wall
 1.24  21-Feb-1998  christos Simplify the function calling code and warnsify.
 1.23  03-Feb-1998  mrg remove getvfsbyname cruft.
 1.22  02-Feb-1998  mrg merge lite2, sans getvfsbyname in functions.c (waiting on libc)
 1.21  19-Oct-1997  lukem fix .Nm usage, deprecate register, mostly WARNSify (needs minor rewrite to fix)
 1.20  01-Feb-1997  matthias remove second RCS-Id line.
 1.19  30-Jan-1997  matthias finding whiteouts didn't work.
 1.18  09-Jan-1997  tls RCS ID police
 1.17  21-May-1996  mrg add support for whiteouts. pr#2394 (Mike Long <mike.long@analog.com>)
 1.16  18-Jun-1995  cgd don't assume f_fstypename is larger than MFSNAMELEN or is nul-terminated
 1.15  18-Oct-1994  mycroft Get rid of a redundant sanity check.
 1.14  18-Oct-1994  mycroft Always save both the mount flags and the file system type when crossing a
mount point. From John Kohl.
 1.13  14-Apr-1994  cgd branches: 1.13.2;
fstypes will be strings soon
 1.12  16-Feb-1994  andrew -nouser and -nogroup tests were negated.
 1.11  21-Jan-1994  jtc Flush stdout and stderr before processing an -exec (or -ok), so the output
of the exec'd command and find's output will be correctly interspersed.
(Bug noticed by Terry Lambert, of FreeBSD. Fix by me).
 1.10  30-Dec-1993  jtc Merged our bugfixes with the 4.4BSD find from uunet.
 1.9  27-Oct-1993  jtc Added -print0 (like GNU find). This will allow file names that contain
newlines to be correctly interpreted by programs that process find
output.
 1.8  16-Sep-1993  cgd fix to the 'size' primary; -size <n>c didn't work. from
Thomas Eberhardt <thomas@mathematik.uni-Bremen.de>
 1.7  06-Aug-1993  deraadt for new fts library
 1.6  01-Aug-1993  mycroft Add RCS identifiers.
 1.5  16-Jun-1993  jtc Update source to use posix fnmatch
 1.4  10-Apr-1993  mycroft Reverse sense of fnmatch() to match POSIX.
 1.3  24-Mar-1993  cgd added support for "kernfs" and "fdesc" to -fstype
 1.2  24-Mar-1993  cgd fixed fact that "rdonly" wasn't supported by -fstype, but the man
page said it was. also fixed multiple fstype checks on the same
partition
 1.1  21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2  01-Sep-1995  jtc imported from 44lite2
 1.1.1.1  21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.2.1  18-Oct-1994  cgd from trunk.
 1.30.2.1  20-Aug-1999  cgd pull up rev 1.31 from trunk. (cgd)
 1.31.4.1  27-Dec-1999  wrstuden Pull up to last week's -current.
 1.46.4.2  11-Oct-2005  reed Revert changes. I accidently committed to netbsd-2.
 1.46.4.1  11-Oct-2005  reed This adds -fprint function. The primary name "-fprint" (but not the
code) comes from findutils; it behaves the same.

From my manpage addition:

-fprint filename
This primary always evaluates to true. This creates filename or
overwrites the file if it already exists. The file is created at
startup. It writes the pathname of the current file to this
file, followed by a newline character. The file will be empty if
no files are matched.

Here is an example usage:

find /etc \( -name "*pass*" -fprint file1 \) -o \( -group operator -fprint file2 \) -o -name "w*"

This was discussed on tech-userlevel.

This creates the file as command line argument parsing time.
If there is an error somewhere on that line, such as missing values
or mismatched parenthesis, then a file may still be created.
(Even if a later -fprint filename is unwritable.) This is similar
behaviour to findutils. (It has been suggested that this find could
be code to create the files in an extra stage after the command-line
argument parsing and before the actual function processing.)

I will add -fprintx and -fprint0 soon.
 1.64.16.2  19-Jul-2007  daniel Add the '-E' option to interpret regexes as extended regexes. While we
are here, fix ordering in usage information by putting '-X' in the proper
place.

Addition of '-E' was discussed on tech-userlevel.
 1.64.16.1  19-Jul-2007  daniel file function.c was added on branch matt-mips64 on 2007-07-19 07:49:31 +0000
 1.65.2.1  05-Mar-2011  bouyer Sync with HEAD
 1.67.2.4  22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.67.2.3  30-Oct-2012  yamt sync with head
 1.67.2.2  23-May-2012  yamt sync with head.
 1.67.2.1  17-Apr-2012  yamt sync with head
 1.71.2.1  23-Jun-2013  tls resync from head
 1.72.6.1  30-Oct-2018  sborrill Pull up the following revisions(s) (requested by mrg in ticket #1642):
usr.bin/find/function.c: revision 1.77

When calculating the length of the args that can be
appended in a "find .... -exec something {} +"
usage, remember to allow for the arg pointers, which
form part of what is allowed in ARG_MAX.
 1.75.8.3  04-Aug-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1879):

usr.bin/find/main.c: revision 1.32
usr.bin/find/function.c: revision 1.80

PR/57313: Timo Buhrmester: Don't bail if "." cannot be opened. From FreeBSD
 1.75.8.2  10-Sep-2018  martin Pull up following revision(s) (requested by kre in ticket #1016):

usr.bin/find/function.c: revision 1.77

Inspired by PR pkg/53543

When calculating the length of the args that can be
appended in a "find .... -exec something {} +"
usage, remember to allow for the arg pointers, which
form part of what is allowed in ARG_MAX.

From a fairly empty installation of HEAD on amd64
and with a "/tmp/args" command that simply prints
its arg count, and the length of the arg strings,
with this mod I see ..

netbsd# find / -exec /tmp/args {} +
Argc 5000 Arglen 107645
Argc 5000 Arglen 151324
Argc 5000 Arglen 187725
Argc 5000 Arglen 206591
Argc 5000 Arglen 172909
Argc 5000 Arglen 186264
Argc 5000 Arglen 167906
Argc 2881 Arglen 98260

The upper limit of 5000 args is in the code.

Using the biggest of those, 5000
args, plus 206591 bytes of strings
uses 246591 bytes total (this excludes
the command name, so add a few more).

That's fairly close to the ARG_MAX
of 262144.

On another system (with longer paths) I see:
(this is just a small part of the output, using a
different version of the dummy command, and a
slightly different invocation)

Args: 4546 Len 218030
Args: 4878 Len 217991
Args: 4813 Len 218028
Args: 4803 Len 218029

There, 4878*8 + 217991 == 257015 which is about
as close as we'd want to come to the arg limit.

XXX pullup -8
 1.75.8.1  15-Jun-2017  snj Pull up following revision(s) (requested by christos in ticket #40):
usr.bin/find/function.c: revision 1.76
PR/52295: Anthony Mallet: find -delete: "relative path not safe" with absolute
paths ... Fix from OpenBSD
 1.76.6.1  10-Jun-2019  christos Sync with HEAD
 1.76.4.1  06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.77.2.1  04-Aug-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1701):

usr.bin/find/main.c: revision 1.32
usr.bin/find/function.c: revision 1.80

PR/57313: Timo Buhrmester: Don't bail if "." cannot be opened. From FreeBSD
 1.79.6.1  04-Aug-2023  martin Pull up following revision(s) (requested by riastradh in ticket #305):

usr.bin/find/main.c: revision 1.32
usr.bin/find/function.c: revision 1.80

PR/57313: Timo Buhrmester: Don't bail if "." cannot be opened. From FreeBSD

RSS XML Feed