Index: stable/4/usr.bin/find/extern.h =================================================================== --- stable/4/usr.bin/find/extern.h (revision 62017) +++ stable/4/usr.bin/find/extern.h (revision 62018) @@ -1,88 +1,91 @@ /*- * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)extern.h 8.3 (Berkeley) 4/16/94 * $FreeBSD$ */ #include void brace_subst __P((char *, char **, char *, int)); void *emalloc __P((unsigned int)); PLAN *find_create __P((char ***)); int find_execute __P((PLAN *, char **)); PLAN *find_formplan __P((char **)); PLAN *not_squish __P((PLAN *)); PLAN *or_squish __P((PLAN *)); PLAN *paren_squish __P((PLAN *)); struct stat; void printlong __P((char *, char *, struct stat *)); int queryuser __P((char **)); PLAN *c_amin __P((char *)); PLAN *c_atime __P((char *)); PLAN *c_cmin __P((char *)); PLAN *c_ctime __P((char *)); PLAN *c_delete __P((void)); PLAN *c_depth __P((void)); PLAN *c_exec __P((char ***, int)); PLAN *c_flags __P((char *)); PLAN *c_execdir __P((char ***)); PLAN *c_follow __P((void)); #if !defined(__NetBSD__) PLAN *c_fstype __P((char *)); #endif PLAN *c_group __P((char *)); PLAN *c_inum __P((char *)); PLAN *c_links __P((char *)); PLAN *c_ls __P((void)); PLAN *c_name __P((char *)); PLAN *c_newer __P((char *)); PLAN *c_nogroup __P((void)); PLAN *c_nouser __P((void)); PLAN *c_path __P((char *)); PLAN *c_perm __P((char *)); PLAN *c_print __P((void)); PLAN *c_print0 __P((void)); PLAN *c_prune __P((void)); PLAN *c_size __P((char *)); PLAN *c_type __P((char *)); PLAN *c_user __P((char *)); PLAN *c_xdev __P((void)); PLAN *c_openparen __P((void)); PLAN *c_closeparen __P((void)); +PLAN *c_maxdepth __P((char *)); +PLAN *c_mindepth __P((char *)); PLAN *c_mmin __P((char *)); PLAN *c_mtime __P((char *)); PLAN *c_not __P((void)); PLAN *c_or __P((void)); extern int ftsoptions, isdeprecated, isdepth, isoutput, issort, isxargs; +extern int mindepth, maxdepth; Index: stable/4/usr.bin/find/find.1 =================================================================== --- stable/4/usr.bin/find/find.1 (revision 62017) +++ stable/4/usr.bin/find/find.1 (revision 62018) @@ -1,554 +1,560 @@ .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the Institute of Electrical and Electronics Engineers, Inc. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 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. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)find.1 8.7 (Berkeley) 5/9/95 .\" $FreeBSD$ .\" .Dd May 9, 1995 .Dt FIND 1 .Os .Sh NAME .Nm find .Nd walk a file hierarchy .Sh SYNOPSIS .Nm find .Op Fl H | Fl L | Fl P .Op Fl Xdsx .Op Fl f Ar pathname .Op Ar pathname ... .Ar expression .Sh DESCRIPTION .Nm Find recursively descends the directory tree for each .Ar pathname listed, evaluating an .Ar expression (composed of the ``primaries'' and ``operands'' listed below) in terms of each file in the tree. .Pp The options are as follows: .Pp .Bl -tag -width Ds .It Fl H The .Fl H option causes the file information and file type (see .Xr stat 2 ) returned for each symbolic link specified on the command line to be those of the file referenced by the link, not the link itself. If the referenced file does not exist, the file information and type will be for the link itself. File information of all symbolic links not on the command line is that of the link itself. .It Fl L The .Fl L option causes the file information and file type (see .Xr stat 2 ) returned for each symbolic link to be those of the file referenced by the link, not the link itself. If the referenced file does not exist, the file information and type will be for the link itself. .It Fl P The .Fl P option causes the file information and file type (see .Xr stat 2 ) returned for each symbolic link to be those of the link itself. This is the default. .It Fl X The .Fl X option is a modification to permit .Nm to be safely used in conjunction with .Xr xargs 1 . If a file name contains any of the delimiting characters used by .Xr xargs , a diagnostic message is displayed on standard error, and the file is skipped. The delimiting characters include single (`` ' '') and double (`` " '') quotes, backslash (``\e''), space, tab and newline characters. .It Fl d The .Fl d option causes .Nm find to perform a depth\-first traversal, i.e. directories are visited in post\-order and all entries in a directory will be acted on before the directory itself. By default, .Nm find visits directories in pre\-order, i.e. before their contents. Note, the default is .Ar not a breadth\-first traversal. .It Fl f The .Fl f option specifies a file hierarchy for .Nm find to traverse. File hierarchies may also be specified as the operands immediately following the options. .It Fl s The .Fl s option causes .Nm find to traverse the file hierarchies in lexicographical order, i.e., alphabetical order within each directory. Note: .Sq find -s and .So find | sort .Sc may give different results. .It Fl x The .Fl x option prevents .Nm find from descending into directories that have a device number different than that of the file from which the descent began. .El .Sh PRIMARIES .Bl -tag -width Ds .It Ic -amin Ar n True if the difference between the file last access time and the time .Nm find was started, rounded up to the next full minutes period, is .Ar n minutes periods. .It Ic -atime Ar n True if the difference between the file last access time and the time .Nm find was started, rounded up to the next full 24\-hour period, is .Ar n 24\-hour periods. .It Ic -cmin Ar n True if the difference between the time of last change of file status information and the time .Nm find was started, rounded up to the next full minutes period, is .Ar n minutes periods. .It Ic -ctime Ar n True if the difference between the time of last change of file status information and the time .Nm find was started, rounded up to the next full 24\-hour period, is .Ar n 24\-hour periods. .It Ic -delete Delete found files and/or directories. Always returns true. This executes from the current working directory as .Nm recurses down the tree. It will not attempt to delete a filename with a ``/'' character in its pathname relative to "." for security reasons. Depth\-first traversal processing is implied by this option. .It Ic -exec Ar utility Op argument ... ; True if the program named .Ar utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (``;''). If the string ``{}'' appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. .Ar Utility will be executed from the directory from which .Nm find was executed. .It Ic -execdir Ar utility Op argument ... ; The .Ic \&-execdir primary is identical to the .Ic -exec primary with the exception that .Ar Utility will be executed from the directory that holds the current file. The filename substituted for the string ``{}'' is not qualified. .It Ic -fstype Ar type True if the file is contained in a file system of type .Ar type . The .Xr sysctl 8 command can be used to find out the types of filesystems that are available on the system: .Bd -literal -offset indent sysctl vfs .Ed In addition, there are two pseudo-types, ``local'' and ``rdonly''. The former matches any file system physically mounted on the system where the .Nm find is being executed and the latter matches any file system which is mounted read-only. .It Ic -group Ar gname True if the file belongs to the group .Ar gname . If .Ar gname is numeric and there is no such group name, then .Ar gname is treated as a group id. .It Ic -inum Ar n True if the file has inode number .Ar n . .It Ic -links Ar n True if the file has .Ar n links. .It Ic -ls This primary always evaluates to true. The following information for the current file is written to standard output: its inode number, size in 512\-byte blocks, file permissions, number of hard links, owner, group, size in bytes, last modification time, and pathname. If the file is a block or character special file, the major and minor numbers will be displayed instead of the size in bytes. If the file is a symbolic link, the pathname of the linked\-to file will be displayed preceded by ``\->''. The format is identical to that produced by ``ls \-dgils''. +.It Ic -maxdepth Ar n +True if the depth of the current file into the tree is less than or equal to +.Ar n . +.It Ic -mindepth Ar n +True if the depth of the current file into the tree is greater than or equal to +.Ar n . .It Ic -mmin Ar n True if the difference between the file last modification time and the time .Nm find was started, rounded up to the next full minutes period, is .Ar n minutes periods. .It Ic -mtime Ar n True if the difference between the file last modification time and the time .Nm find was started, rounded up to the next full 24\-hour period, is .Ar n 24\-hour periods. .It Ic \&-ok Ar utility Op argument ... ; The .Ic \&-ok primary is identical to the .Ic -exec primary with the exception that .Nm find requests user affirmation for the execution of the utility by printing a message to the terminal and reading a response. If the response is other than ``y'' the command is not executed and the value of the .Ar \&ok expression is false. .It Ic -name Ar pattern True if the last component of the pathname being examined matches .Ar pattern . Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') may be used as part of .Ar pattern . These characters may be matched explicitly by escaping them with a backslash (``\e''). .It Ic -newer Ar file True if the current file has a more recent last modification time than .Ar file . .It Ic -nouser True if the file belongs to an unknown user. .It Ic -nogroup True if the file belongs to an unknown group. .It Ic -path Ar pattern True if the pathname being examined matches .Ar pattern . Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') may be used as part of .Ar pattern . These characters may be matched explicitly by escaping them with a backslash (``\e''). Slashes (``/'') are treated as normal characters and do not have to be matched explicitly. .It Xo .Ic -perm .Op Fl .Ar mode .Xc The .Ar mode may be either symbolic (see .Xr chmod 1 ) or an octal number. If the mode is symbolic, a starting value of zero is assumed and the mode sets or clears permissions without regard to the process' file mode creation mask. If the mode is octal, only bits 07777 .Pf ( Dv S_ISUID | .Dv S_ISGID | .Dv S_ISTXT | .Dv S_IRWXU | .Dv S_IRWXG | .Dv S_IRWXO ) of the file's mode bits participate in the comparison. If the mode is preceded by a dash (``\-''), this primary evaluates to true if at least all of the bits in the mode are set in the file's mode bits. If the mode is preceded by a plus (``\+''), this primary evaluates to true if any of the bits in the mode are set in the file's mode bits. Otherwise, this primary evaluates to true if the bits in the mode exactly match the file's mode bits. Note, the first character of a symbolic mode may not be a dash (``\-''). .It Ic -flags Op Fl Ns Ar flags This primary evaluates to true if exactly those flags of the file are set which are also set using the specified .Ar flags (if these are not preceded by a dash (``\-''), or if they match the specified flags (if these are preceded by a dash). The .Ar flags are specified using symbolic names (see .Xr chflags 1 ). Note that this is different from .Ic -perm , which only allows you to specify flags which are set. .It Ic -print This primary always evaluates to true. It prints the pathname of the current file to standard output. If none of .Ic -exec , .Ic -ls , .Ic -print0 , or .Ic \&-ok is specified, the given expression shall be effectively replaced by .Cm \&( Ns Ar given\& expression Ns Cm \&) .Ic -print . .It Ic -print0 This primary always evaluates to true. It prints the pathname of the current file to standard output, followed by an .Tn ASCII .Tn NUL character (character code 0). .It Ic -prune This primary always evaluates to true. It causes .Nm find to not descend into the current file. Note, the .Ic -prune primary has no effect if the .Fl d option was specified. .It Ic -size Ar n Ns Op Cm c True if the file's size, rounded up, in 512\-byte blocks is .Ar n . If .Ar n is followed by a ``c'', then the primary is true if the file's size is .Ar n bytes. .It Ic -type Ar t True if the file is of the specified type. Possible file types are as follows: .Pp .Bl -tag -width flag -offset indent -compact .It Cm b block special .It Cm c character special .It Cm d directory .It Cm f regular file .It Cm l symbolic link .It Cm p FIFO .It Cm s socket .El .Pp .It Ic -user Ar uname True if the file belongs to the user .Ar uname . If .Ar uname is numeric and there is no such user name, then .Ar uname is treated as a user id. .El .Pp All primaries which take a numeric argument allow the number to be preceded by a plus sign (``+'') or a minus sign (``\-''). A preceding plus sign means ``more than n'', a preceding minus sign means ``less than n'' and neither means ``exactly n'' . .Sh OPERATORS The primaries may be combined using the following operators. The operators are listed in order of decreasing precedence. .Bl -tag -width (expression) .It Cm \&( Ar expression Cm \&) This evaluates to true if the parenthesized expression evaluates to true. .Pp .It Cm \&! Ar expression This is the unary .Tn NOT operator. It evaluates to true if the expression is false. .Pp .It Ar expression Cm -and Ar expression .It Ar expression expression The .Cm -and operator is the logical .Tn AND operator. As it is implied by the juxtaposition of two expressions it does not have to be specified. The expression evaluates to true if both expressions are true. The second expression is not evaluated if the first expression is false. .Pp .It Ar expression Cm -or Ar expression The .Cm -or operator is the logical .Tn OR operator. The expression evaluates to true if either the first or the second expression is true. The second expression is not evaluated if the first expression is true. .El .Pp All operands and primaries must be separate arguments to .Nm find . Primaries which themselves take arguments expect each argument to be a separate argument to .Nm find . .Sh EXAMPLES .Pp The following examples are shown as given to the shell: .Bl -tag -width findx .It Li "find / \e! -name \*q*.c\*q -print" Print out a list of all the files whose names do not end in ``.c''. .It Li "find / -newer ttt -user wnj -print" Print out a list of all the files owned by user ``wnj'' that are newer than the file ``ttt''. .It Li "find / \e! \e( -newer ttt -user wnj \e) -print" Print out a list of all the files which are not both newer than ``ttt'' and owned by ``wnj''. .It Li "find / \e( -newer ttt -or -user wnj \e) -print" Print out a list of all the files that are either owned by ``wnj'' or that are newer than ``ttt''. .El .Sh SEE ALSO .Xr chflags 1 , .Xr chmod 1 , .Xr locate 1 , .Xr whereis 1 , .Xr which 1 , .Xr stat 2 , .Xr fts 3 , .Xr getgrent 3 , .Xr getpwent 3 , .Xr strmode 3 , .Xr symlink 7 .Sh STANDARDS The .Nm find utility syntax is a superset of the syntax specified by the .St -p1003.2 standard. .Pp All the single character options as well as the .Ic -inum , .Ic -print0 , .Ic -delete , and .Ic -ls primaries are extensions to .St -p1003.2 . .Pp Historically, the .Fl d , .Fl h and .Fl x options were implemented using the primaries ``\-depth'', ``\-follow'', and ``\-xdev''. These primaries always evaluated to true. As they were really global variables that took effect before the traversal began, some legal expressions could have unexpected results. An example is the expression ``\-print \-o \-depth''. As \-print always evaluates to true, the standard order of evaluation implies that \-depth would never be evaluated. This is not the case. .Pp The operator ``-or'' was implemented as ``\-o'', and the operator ``-and'' was implemented as ``\-a''. .Pp Historic implementations of the .Ic exec and .Ic ok primaries did not replace the string ``{}'' in the utility name or the utility arguments if it had preceding or following non-whitespace characters. This version replaces it no matter where in the utility name or arguments it appears. .Sh BUGS The special characters used by .Nm find are also special characters to many shell programs. In particular, the characters ``*'', ``['', ``]'', ``?'', ``('', ``)'', ``!'', ``\e'' and ``;'' may have to be escaped from the shell. .Pp As there is no delimiter separating options and file names or file names and the .Ar expression , it is difficult to specify files named ``-xdev'' or ``!''. These problems are handled by the .Fl f option and the .Xr getopt 3 ``--'' construct. .Pp The .Ic -delete primary does not interact well with other options that cause the filesystem tree traversal options to be changed. .Sh HISTORY A .Nm command appeared in .At v1 . Index: stable/4/usr.bin/find/find.c =================================================================== --- stable/4/usr.bin/find/find.c (revision 62017) +++ stable/4/usr.bin/find/find.c (revision 62018) @@ -1,219 +1,233 @@ /*- * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Cimarron D. Taylor of the University of California, Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint +#if 0 static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* not lint */ #include #include #include #include #include #include #include #include #include "find.h" static int find_compare __P((const FTSENT **s1, const FTSENT **s2)); /* * find_compare -- * tell fts_open() how to order the traversal of the hierarchy. * This variant gives lexicographical order, i.e., alphabetical * order within each directory. */ static int find_compare(s1, s2) const FTSENT **s1, **s2; { return (strcoll((*s1)->fts_name, (*s2)->fts_name)); } /* * find_formplan -- * process the command line and create a "plan" corresponding to the * command arguments. */ PLAN * find_formplan(argv) char **argv; { PLAN *plan, *tail, *new; /* * for each argument in the command line, determine what kind of node * it is, create the appropriate node type and add the new plan node * to the end of the existing plan. The resulting plan is a linked * list of plan nodes. For example, the string: * * % find . -name foo -newer bar -print * * results in the plan: * * [-name foo]--> [-newer bar]--> [-print] * * in this diagram, `[-name foo]' represents the plan node generated * by c_name() with an argument of foo and `-->' represents the * plan->next pointer. */ for (plan = tail = NULL; *argv;) { if (!(new = find_create(&argv))) continue; if (plan == NULL) tail = plan = new; else { tail->next = new; tail = new; } } /* * if the user didn't specify one of -print, -ok or -exec, then -print * is assumed so we bracket the current expression with parens, if * necessary, and add a -print node on the end. */ if (!isoutput) { if (plan == NULL) { new = c_print(); tail = plan = new; } else { new = c_openparen(); new->next = plan; plan = new; new = c_closeparen(); tail->next = new; tail = new; new = c_print(); tail->next = new; tail = new; } } /* * the command line has been completely processed into a search plan * except for the (, ), !, and -o operators. Rearrange the plan so * that the portions of the plan which are affected by the operators * are moved into operator nodes themselves. For example: * * [!]--> [-name foo]--> [-print] * * becomes * * [! [-name foo] ]--> [-print] * * and * * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] * * becomes * * [expr [-depth]-->[-name foo] ]--> [-print] * * operators are handled in order of precedence. */ plan = paren_squish(plan); /* ()'s */ plan = not_squish(plan); /* !'s */ plan = or_squish(plan); /* -o's */ return (plan); } FTS *tree; /* pointer to top of FTS hierarchy */ /* * find_execute -- * take a search plan and an array of search paths and executes the plan * over all FTSENT's returned for the given search paths. */ int find_execute(plan, paths) PLAN *plan; /* search plan */ char **paths; /* array of pathnames to traverse */ { register FTSENT *entry; PLAN *p; int rval; tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL)); if (tree == NULL) err(1, "ftsopen"); for (rval = 0; (entry = fts_read(tree)) != NULL;) { switch (entry->fts_info) { case FTS_D: if (isdepth) continue; break; case FTS_DP: if (!isdepth) continue; break; case FTS_DNR: case FTS_ERR: case FTS_NS: (void)fflush(stdout); warnx("%s: %s", entry->fts_path, strerror(entry->fts_errno)); rval = 1; continue; #ifdef FTS_W case FTS_W: continue; #endif /* FTS_W */ } #define BADCH " \t\n\\'\"" if (isxargs && strpbrk(entry->fts_path, BADCH)) { (void)fflush(stdout); warnx("%s: illegal path", entry->fts_path); rval = 1; continue; } + if (mindepth != -1 && entry->fts_level < mindepth) + continue; + /* * Call all the functions in the execution plan until one is * false or all have been executed. This is where we do all * the work specified by the user on the command line. */ for (p = plan; p && (p->eval)(p, entry); p = p->next); + + if (maxdepth != -1 && entry->fts_level >= maxdepth) { + if (fts_set(tree, entry, FTS_SKIP)) + err(1, "%s", entry->fts_path); + continue; + } } if (errno) err(1, "fts_read"); return (rval); } Index: stable/4/usr.bin/find/find.h =================================================================== --- stable/4/usr.bin/find/find.h (revision 62017) +++ stable/4/usr.bin/find/find.h (revision 62018) @@ -1,118 +1,118 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Cimarron D. Taylor of the University of California, Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)find.h 8.1 (Berkeley) 6/6/93 * $FreeBSD$ */ /* 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_EXECDIR, N_EXPR, N_FLAGS, N_FOLLOW, N_FSTYPE, N_GROUP, N_INUM, 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_PRUNE, N_SIZE, N_TYPE, N_USER, N_XDEV, - N_PRINT0, N_DELETE + N_PRINT0, N_DELETE, N_MAXDEPTH, N_MINDEPTH }; /* node definition */ typedef struct _plandata { struct _plandata *next; /* next node */ int (*eval) /* node evaluation function */ __P((struct _plandata *, FTSENT *)); #define F_EQUAL 1 /* [acm]time inum links size */ #define F_LESSTHAN 2 #define F_GREATER 3 #define F_NEEDOK 1 /* exec ok */ #define F_MTFLAG 1 /* fstype */ #define F_MTTYPE 2 #define F_ATLEAST 1 /* perm */ #define F_ANY 2 /* perm */ int flags; /* private flags */ enum ntype type; /* plan node type */ union { gid_t _g_data; /* gid */ ino_t _i_data; /* inode */ mode_t _m_data; /* mode mask */ struct { u_long _f_flags; u_long _f_mask; } fl; nlink_t _l_data; /* link count */ off_t _o_data; /* file size */ time_t _t_data; /* time value */ uid_t _u_data; /* uid */ short _mt_data; /* mount flags */ struct _plandata *_p_data[2]; /* PLAN trees */ struct _ex { char **_e_argv; /* argv array */ char **_e_orig; /* original strings */ int *_e_len; /* allocated length */ } ex; char *_a_data[2]; /* array of char pointers */ char *_c_data; /* char pointer */ } p_un; } PLAN; #define a_data p_un._a_data #define c_data p_un._c_data #define fl_flags p_un.fl._f_flags #define fl_mask p_un.fl._f_mask #define g_data p_un._g_data #define i_data p_un._i_data #define l_data p_un._l_data #define m_data p_un._m_data #define mt_data p_un._mt_data #define o_data p_un._o_data #define p_data p_un._p_data #define t_data p_un._t_data #define u_data p_un._u_data #define e_argv p_un.ex._e_argv #define e_orig p_un.ex._e_orig #define e_len p_un.ex._e_len typedef struct _option { char *name; /* option name */ enum ntype token; /* token type */ PLAN *(*create)(); /* create function: DON'T PROTOTYPE! */ #define O_NONE 0x01 /* no call required */ #define O_ZERO 0x02 /* pass: nothing */ #define O_ARGV 0x04 /* pass: argv, increment argv */ #define O_ARGVP 0x08 /* pass: *argv, N_OK || N_EXEC || N_EXECDIR */ int flags; } OPTION; #include "extern.h" Index: stable/4/usr.bin/find/function.c =================================================================== --- stable/4/usr.bin/find/function.c (revision 62017) +++ stable/4/usr.bin/find/function.c (revision 62018) @@ -1,1406 +1,1468 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Cimarron D. Taylor of the University of California, Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint -static char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95"; -static char rcsid[] = "$FreeBSD$"; +#if 0 +static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "find.h" #define COMPARE(a, b) { \ switch (plan->flags) { \ case F_EQUAL: \ return (a == b); \ case F_LESSTHAN: \ return (a < b); \ case F_GREATER: \ return (a > b); \ default: \ abort(); \ } \ } u_long setflags __P((char **, u_long *, u_long *)); static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *)))); /* * find_parsenum -- * Parse a string of the form [+-]# and return the value. */ static long long find_parsenum(plan, option, vp, endch) PLAN *plan; char *option, *vp, *endch; { 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; break; case '-': ++str; plan->flags = F_LESSTHAN; break; default: plan->flags = F_EQUAL; break; } /* * 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 = 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)) errx(1, "%s: %s: illegal trailing character", option, vp); if (endch) *endch = endchar[0]; 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". */ #define TIME_CORRECT(p, ttype) \ if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \ ++((p)->t_data); /* * -amin n functions -- * * True if the difference between the file access time and the * current time is n min periods. */ int f_amin(plan, entry) PLAN *plan; FTSENT *entry; { extern time_t now; COMPARE((now - entry->fts_statp->st_atime + 60 - 1) / 60, plan->t_data); } PLAN * c_amin(arg) char *arg; { PLAN *new; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_AMIN, f_amin); new->t_data = find_parsenum(new, "-amin", arg, NULL); TIME_CORRECT(new, N_AMIN); return (new); } /* * -atime n functions -- * * True if the difference between the file access time and the * current time is n 24 hour periods. */ int f_atime(plan, entry) PLAN *plan; FTSENT *entry; { extern time_t now; COMPARE((now - entry->fts_statp->st_atime + 86400 - 1) / 86400, plan->t_data); } PLAN * c_atime(arg) char *arg; { PLAN *new; 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); } /* * -cmin n functions -- * * True if the difference between the last change of file * status information and the current time is n min periods. */ int f_cmin(plan, entry) PLAN *plan; FTSENT *entry; { extern time_t now; COMPARE((now - entry->fts_statp->st_ctime + 60 - 1) / 60, plan->t_data); } PLAN * c_cmin(arg) char *arg; { PLAN *new; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_CMIN, f_cmin); new->t_data = find_parsenum(new, "-cmin", arg, NULL); TIME_CORRECT(new, N_CMIN); return (new); } /* * -ctime n functions -- * * True if the difference between the last change of file * status information and the current time is n 24 hour periods. */ int f_ctime(plan, entry) PLAN *plan; FTSENT *entry; { extern time_t now; COMPARE((now - entry->fts_statp->st_ctime + 86400 - 1) / 86400, plan->t_data); } PLAN * c_ctime(arg) char *arg; { PLAN *new; 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); } /* * -depth functions -- * * 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_always_true(plan, entry) PLAN *plan; FTSENT *entry; { return (1); } PLAN * c_depth() { isdepth = 1; return (palloc(N_DEPTH, f_always_true)); } /* * [-exec | -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. * * The primary -ok is different in that it requests affirmation of the * user before executing the utility. */ int f_exec(plan, entry) register PLAN *plan; FTSENT *entry; { extern int dotfd; register int cnt; pid_t pid; int status; 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 (plan->flags == F_NEEDOK && !queryuser(plan->e_argv)) return (0); /* make sure find output is interspersed correctly with subprocesses */ fflush(stdout); switch (pid = fork()) { case -1: err(1, "fork"); /* NOTREACHED */ case 0: if (fchdir(dotfd)) { warn("chdir"); _exit(1); } execvp(plan->e_argv[0], plan->e_argv); warn("%s", plan->e_argv[0]); _exit(1); } pid = waitpid(pid, &status, 0); return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); } /* * c_exec -- * 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; { PLAN *new; /* node returned */ register int cnt; register char **argv, **ap, *p; isoutput = 1; new = palloc(N_EXEC, f_exec); if (isok) new->flags = F_NEEDOK; for (ap = argv = *argvp;; ++ap) { if (!*ap) errx(1, "%s: no terminating \";\"", isok ? "-ok" : "-exec"); if (**ap == ';') break; } 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)); for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { new->e_orig[cnt] = *argv; for (p = *argv; *p; ++p) if (p[0] == '{' && p[1] == '}') { new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); new->e_len[cnt] = MAXPATHLEN; break; } if (!*p) { new->e_argv[cnt] = *argv; new->e_len[cnt] = 0; } } new->e_argv[cnt] = new->e_orig[cnt] = NULL; *argvp = argv + 1; return (new); } /* * -execdir 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 unqualified pathname. * The current directory for the execution of utility is the same as * the directory where the file lives. */ int f_execdir(plan, entry) register PLAN *plan; FTSENT *entry; { register int cnt; pid_t pid; int status; char *file; /* XXX - if file/dir ends in '/' this will not work -- can it? */ if ((file = strrchr(entry->fts_path, '/'))) file++; else file = entry->fts_path; 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]); /* don't mix output of command with find output */ fflush(stdout); fflush(stderr); switch (pid = fork()) { case -1: err(1, "fork"); /* NOTREACHED */ case 0: execvp(plan->e_argv[0], plan->e_argv); warn("%s", plan->e_argv[0]); _exit(1); } pid = waitpid(pid, &status, 0); return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); } /* * c_execdir -- * 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_execdir(argvp) char ***argvp; { PLAN *new; /* node returned */ register int cnt; register char **argv, **ap, *p; ftsoptions &= ~FTS_NOSTAT; isoutput = 1; new = palloc(N_EXECDIR, f_execdir); for (ap = argv = *argvp;; ++ap) { if (!*ap) errx(1, "-execdir: no terminating \";\""); if (**ap == ';') break; } 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)); for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { new->e_orig[cnt] = *argv; for (p = *argv; *p; ++p) if (p[0] == '{' && p[1] == '}') { new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); new->e_len[cnt] = MAXPATHLEN; break; } if (!*p) { new->e_argv[cnt] = *argv; new->e_len[cnt] = 0; } } new->e_argv[cnt] = new->e_orig[cnt] = NULL; *argvp = argv + 1; return (new); } /* * -follow functions -- * * Always true, causes symbolic links to be followed on a global * basis. */ PLAN * c_follow() { ftsoptions &= ~FTS_PHYSICAL; ftsoptions |= FTS_LOGICAL; return (palloc(N_FOLLOW, f_always_true)); } /* * -fstype functions -- * * True if the file is of a certain type. */ int f_fstype(plan, entry) PLAN *plan; FTSENT *entry; { static dev_t curdev; /* need a guaranteed illegal dev value */ static int first = 1; struct statfs sb; static int val_type, val_flags; char *p, save[2]; /* 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, * not where it points. */ if (entry->fts_info == FTS_SL || entry->fts_info == FTS_SLNONE) { if ((p = strrchr(entry->fts_accpath, '/')) != NULL) ++p; else p = entry->fts_accpath; save[0] = p[0]; p[0] = '.'; save[1] = p[1]; p[1] = '\0'; } else p = NULL; if (statfs(entry->fts_accpath, &sb)) err(1, "%s", entry->fts_accpath); if (p) { p[0] = save[0]; p[1] = save[1]; } first = 0; /* * Further tests may need both of these values, so * always copy both of them. */ val_flags = sb.f_flags; val_type = sb.f_type; } switch (plan->flags) { case F_MTFLAG: return (val_flags & plan->mt_data) != 0; case F_MTTYPE: return (val_type == plan->mt_data); default: abort(); } } #if !defined(__NetBSD__) PLAN * c_fstype(arg) char *arg; { register PLAN *new; struct vfsconf vfc; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_FSTYPE, f_fstype); /* * Check first for a filesystem name. */ if (getvfsbyname(arg, &vfc) == 0) { new->flags = F_MTTYPE; new->mt_data = vfc.vfc_typenum; return (new); } switch (*arg) { case 'l': if (!strcmp(arg, "local")) { new->flags = F_MTFLAG; new->mt_data = MNT_LOCAL; return (new); } break; case 'r': if (!strcmp(arg, "rdonly")) { new->flags = F_MTFLAG; new->mt_data = MNT_RDONLY; return (new); } break; } errx(1, "%s: unknown file type", arg); /* NOTREACHED */ } #endif /* * -group gname functions -- * * True if the file belongs to the group gname. If gname is numeric and * an equivalent of the getgrnam() function does not return a valid group * name, gname is taken as a group ID. */ int f_group(plan, entry) PLAN *plan; FTSENT *entry; { return (entry->fts_statp->st_gid == plan->g_data); } PLAN * c_group(gname) char *gname; { PLAN *new; struct group *g; gid_t gid; ftsoptions &= ~FTS_NOSTAT; g = getgrnam(gname); if (g == NULL) { gid = atoi(gname); if (gid == 0 && gname[0] != '0') errx(1, "-group: %s: no such group", gname); } else gid = g->gr_gid; new = palloc(N_GROUP, f_group); new->g_data = gid; return (new); } /* * -inum n functions -- * * True if the file has inode # n. */ int f_inum(plan, entry) PLAN *plan; FTSENT *entry; { COMPARE(entry->fts_statp->st_ino, plan->i_data); } PLAN * c_inum(arg) char *arg; { PLAN *new; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_INUM, f_inum); new->i_data = find_parsenum(new, "-inum", arg, NULL); return (new); } /* * -links n functions -- * * True if the file has n links. */ int f_links(plan, entry) PLAN *plan; FTSENT *entry; { COMPARE(entry->fts_statp->st_nlink, plan->l_data); } PLAN * c_links(arg) char *arg; { PLAN *new; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_LINKS, f_links); new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL); return (new); } /* * -ls functions -- * * Always true - prints the current entry to stdout in "ls" format. */ int f_ls(plan, entry) PLAN *plan; FTSENT *entry; { printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); return (1); } PLAN * c_ls() { ftsoptions &= ~FTS_NOSTAT; isoutput = 1; return (palloc(N_LS, f_ls)); } /* + * -maxdepth n functions -- + * + * Does the same as -prune if the level of the current file is greater + * than the specified maximum depth. + * + * Note that -maxdepth and -mindepth are handled specially in + * find_execute() so their f_* functions here do nothing. + */ +int +f_maxdepth(plan, entry) + PLAN *plan; + FTSENT *entry; +{ + return (1); +} + +PLAN * +c_maxdepth(arg) + char *arg; +{ + PLAN *new; + + if (*arg == '-') + /* all other errors handled by find_parsenum() */ + errx(1, "-maxdepth: %s: value must be positive", arg); + + new = palloc(N_MAXDEPTH, f_maxdepth); + maxdepth = find_parsenum(new, "-maxdepth", arg, NULL); + return (new); +} + +/* + * -mindepth n functions -- + * + * True if the current file is at or deeper than the specified minimum + * depth. + */ +int +f_mindepth(plan, entry) + PLAN *plan; + FTSENT *entry; +{ + return (1); +} + +PLAN * +c_mindepth(arg) + char *arg; +{ + PLAN *new; + + if (*arg == '-') + /* all other errors handled by find_parsenum() */ + errx(1, "-maxdepth: %s: value must be positive", arg); + + new = palloc(N_MINDEPTH, f_mindepth); + mindepth = find_parsenum(new, "-mindepth", arg, NULL); + return (new); +} + +/* * -mtime n functions -- * * True if the difference between the file modification time and the * current time is n 24 hour periods. */ int f_mtime(plan, entry) PLAN *plan; FTSENT *entry; { extern time_t now; COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) / 86400, plan->t_data); } PLAN * c_mtime(arg) char *arg; { PLAN *new; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_MTIME, f_mtime); new->t_data = find_parsenum(new, "-mtime", arg, NULL); TIME_CORRECT(new, N_MTIME); return (new); } /* * -mmin n functions -- * * True if the difference between the file modification time and the * current time is n min periods. */ int f_mmin(plan, entry) PLAN *plan; FTSENT *entry; { extern time_t now; COMPARE((now - entry->fts_statp->st_mtime + 60 - 1) / 60, plan->t_data); } PLAN * c_mmin(arg) char *arg; { PLAN *new; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_MMIN, f_mmin); new->t_data = find_parsenum(new, "-mmin", arg, NULL); TIME_CORRECT(new, N_MMIN); return (new); } /* * -name functions -- * * True if the basename of the filename being examined * matches pattern using Pattern Matching Notation S3.14 */ int f_name(plan, entry) PLAN *plan; FTSENT *entry; { return (!fnmatch(plan->c_data, entry->fts_name, 0)); } PLAN * c_name(pattern) char *pattern; { PLAN *new; new = palloc(N_NAME, f_name); new->c_data = pattern; return (new); } /* * -newer file functions -- * * True if the current file has been modified more recently * then the modification time of the file named by the pathname * file. */ int f_newer(plan, entry) PLAN *plan; FTSENT *entry; { return (entry->fts_statp->st_mtime > plan->t_data); } PLAN * c_newer(filename) char *filename; { PLAN *new; struct stat sb; 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); } /* * -nogroup functions -- * * True if file belongs to a user ID for which the equivalent * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. */ int f_nogroup(plan, entry) PLAN *plan; FTSENT *entry; { char *group_from_gid(); return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1); } PLAN * c_nogroup() { ftsoptions &= ~FTS_NOSTAT; return (palloc(N_NOGROUP, f_nogroup)); } /* * -nouser functions -- * * True if file belongs to a user ID for which the equivalent * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. */ int f_nouser(plan, entry) PLAN *plan; FTSENT *entry; { char *user_from_uid(); return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1); } PLAN * c_nouser() { ftsoptions &= ~FTS_NOSTAT; return (palloc(N_NOUSER, f_nouser)); } /* * -path functions -- * * True if the path of the filename being examined * matches pattern using Pattern Matching Notation S3.14 */ int f_path(plan, entry) PLAN *plan; FTSENT *entry; { return (!fnmatch(plan->c_data, entry->fts_path, 0)); } PLAN * c_path(pattern) char *pattern; { PLAN *new; new = palloc(N_NAME, f_path); new->c_data = pattern; return (new); } /* * -perm functions -- * * The mode argument is used to represent file mode bits. If it starts * with a leading digit, it's treated as an octal mode, otherwise as a * symbolic mode. */ int f_perm(plan, entry) 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); else if (plan->flags == F_ANY ) return (plan->m_data & mode); else return (mode == plan->m_data); /* NOTREACHED */ } PLAN * c_perm(perm) char *perm; { PLAN *new; mode_t *set; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_PERM, f_perm); if (*perm == '-') { new->flags = F_ATLEAST; ++perm; } else if (*perm == '+') { new->flags = F_ANY; ++perm; } if ((set = setmode(perm)) == NULL) errx(1, "-perm: %s: illegal mode string", perm); new->m_data = getmode(set, 0); free(set); return (new); } /* * -flags functions -- * * The flags argument is used to represent file flags bits. */ int f_flags(plan, entry) PLAN *plan; FTSENT *entry; { u_long flags; flags = entry->fts_statp->st_flags & (UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE | SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND); if (plan->flags == F_ATLEAST) /* note that plan->fl_flags always is a subset of plan->fl_mask */ return (flags & plan->fl_mask) == plan->fl_flags; else return flags == plan->fl_flags; /* NOTREACHED */ } PLAN * c_flags(flags_str) char *flags_str; { PLAN *new; u_long flags, notflags; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_FLAGS, f_flags); if (*flags_str == '-') { new->flags = F_ATLEAST; flags_str++; } if (setflags(&flags_str, &flags, ¬flags) == 1) errx(1, "-flags: %s: illegal flags string", flags_str); new->fl_flags = flags; new->fl_mask = flags | notflags; #if 0 printf("flags = %08x, mask = %08x (%08x, %08x)\n", new->fl_flags, new->fl_mask, flags, notflags); #endif return new; } - - /* - /* * -print functions -- * * Always true, causes the current pathame to be written to * standard output. */ int f_print(plan, entry) PLAN *plan; FTSENT *entry; { (void)puts(entry->fts_path); return (1); } PLAN * c_print() { isoutput = 1; return (palloc(N_PRINT, f_print)); } /* * -print0 functions -- * * Always true, causes the current pathame to be written to * standard output followed by a NUL character */ int f_print0(plan, entry) PLAN *plan; FTSENT *entry; { fputs(entry->fts_path, stdout); fputc('\0', stdout); return (1); } PLAN * c_print0() { isoutput = 1; return (palloc(N_PRINT0, f_print0)); } /* * -prune functions -- * * Prune a portion of the hierarchy. */ int f_prune(plan, entry) PLAN *plan; FTSENT *entry; { extern FTS *tree; if (fts_set(tree, entry, FTS_SKIP)) err(1, "%s", entry->fts_path); return (1); } PLAN * c_prune() { return (palloc(N_PRUNE, f_prune)); } /* * -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. */ #define FIND_SIZE 512 static int divsize = 1; int f_size(plan, entry) PLAN *plan; FTSENT *entry; { off_t size; size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / FIND_SIZE : entry->fts_statp->st_size; COMPARE(size, plan->o_data); } PLAN * c_size(arg) char *arg; { PLAN *new; char endch; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_SIZE, f_size); endch = 'c'; new->o_data = find_parsenum(new, "-size", arg, &endch); if (endch == 'c') divsize = 0; return (new); } /* * -type c functions -- * * True if the type of the file is c, where c is b, c, d, p, f or w * for block special file, character special file, directory, FIFO, * regular file or whiteout respectively. */ int f_type(plan, entry) PLAN *plan; FTSENT *entry; { return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); } PLAN * c_type(typestring) char *typestring; { PLAN *new; mode_t mask; ftsoptions &= ~FTS_NOSTAT; switch (typestring[0]) { case 'b': mask = S_IFBLK; break; case 'c': mask = S_IFCHR; break; case 'd': mask = S_IFDIR; break; case 'f': mask = S_IFREG; break; case 'l': mask = S_IFLNK; break; case 'p': mask = S_IFIFO; break; case 's': mask = S_IFSOCK; break; #ifdef FTS_WHITEOUT case 'w': mask = S_IFWHT; ftsoptions |= FTS_WHITEOUT; break; #endif /* FTS_WHITEOUT */ default: errx(1, "-type: %s: unknown type", typestring); } new = palloc(N_TYPE, f_type); new->m_data = mask; return (new); } /* * -delete functions -- * * True always. Makes it's best shot and continues on regardless. */ int f_delete(plan, entry) PLAN *plan; 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 */ !(ftsoptions & FTS_PHYSICAL) || /* physical off */ (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ errx(1, "-delete: insecure options got turned on"); /* 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) chflags(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() { ftsoptions &= ~FTS_NOSTAT; /* no optimise */ ftsoptions |= FTS_PHYSICAL; /* disable -follow */ ftsoptions &= ~FTS_LOGICAL; /* disable -follow */ isoutput = 1; /* possible output */ isdepth = 1; /* -depth implied */ return (palloc(N_DELETE, f_delete)); } /* * -user uname functions -- * * True if the file belongs to the user uname. If uname is numeric and * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not * return a valid user name, uname is taken as a user ID. */ int f_user(plan, entry) PLAN *plan; FTSENT *entry; { return (entry->fts_statp->st_uid == plan->u_data); } PLAN * c_user(username) char *username; { PLAN *new; struct passwd *p; uid_t uid; ftsoptions &= ~FTS_NOSTAT; p = getpwnam(username); if (p == NULL) { uid = atoi(username); if (uid == 0 && username[0] != '0') errx(1, "-user: %s: no such user", username); } else uid = p->pw_uid; new = palloc(N_USER, f_user); new->u_data = uid; return (new); } /* * -xdev functions -- * * Always true, causes find not to decend past directories that have a * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) */ PLAN * c_xdev() { ftsoptions |= FTS_XDEV; return (palloc(N_XDEV, f_always_true)); } /* * ( expression ) functions -- * * True if expression is true. */ int f_expr(plan, entry) PLAN *plan; FTSENT *entry; { register PLAN *p; register int state; state = 0; for (p = plan->p_data[0]; p && (state = (p->eval)(p, entry)); p = p->next); return (state); } /* * N_OPENPAREN and N_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. */ PLAN * c_openparen() { return (palloc(N_OPENPAREN, (int (*)())-1)); } PLAN * c_closeparen() { return (palloc(N_CLOSEPAREN, (int (*)())-1)); } /* * ! expression functions -- * * Negation of a primary; the unary NOT operator. */ int f_not(plan, entry) PLAN *plan; FTSENT *entry; { register PLAN *p; register int state; state = 0; for (p = plan->p_data[0]; p && (state = (p->eval)(p, entry)); p = p->next); return (!state); } PLAN * c_not() { return (palloc(N_NOT, f_not)); } /* * expression -o expression functions -- * * Alternation of primaries; the OR operator. The second expression is * not evaluated if the first expression is true. */ int f_or(plan, entry) PLAN *plan; FTSENT *entry; { register PLAN *p; register int state; state = 0; for (p = plan->p_data[0]; p && (state = (p->eval)(p, entry)); p = p->next); if (state) return (1); for (p = plan->p_data[1]; p && (state = (p->eval)(p, entry)); p = p->next); return (state); } PLAN * c_or() { return (palloc(N_OR, f_or)); } static PLAN * palloc(t, f) enum ntype t; int (*f) __P((PLAN *, FTSENT *)); { PLAN *new; if ((new = malloc(sizeof(PLAN))) == NULL) err(1, NULL); new->type = t; new->eval = f; new->flags = 0; new->next = NULL; return (new); } Index: stable/4/usr.bin/find/ls.c =================================================================== --- stable/4/usr.bin/find/ls.c (revision 62017) +++ stable/4/usr.bin/find/ls.c (revision 62018) @@ -1,116 +1,121 @@ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint +#if 0 static char sccsid[] = "@(#)ls.c 8.1 (Berkeley) 6/6/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* not lint */ #include #include #include #include #include #include #include #include #include /* Derived from the print routines in the ls(1) source code. */ static void printlink __P((char *)); static void printtime __P((time_t)); void printlong(name, accpath, sb) char *name; /* filename to print */ char *accpath; /* current valid path to filename */ struct stat *sb; /* stat buffer */ { char modep[15], *user_from_uid(), *group_from_gid(); (void)printf("%6lu %4qd ", (u_long)sb->st_ino, sb->st_blocks); (void)strmode(sb->st_mode, modep); (void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, UT_NAMESIZE, user_from_uid(sb->st_uid, 0), UT_NAMESIZE, group_from_gid(sb->st_gid, 0)); if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) (void)printf("%3d, %3d ", major(sb->st_rdev), minor(sb->st_rdev)); else (void)printf("%8qd ", sb->st_size); printtime(sb->st_mtime); (void)printf("%s", name); if (S_ISLNK(sb->st_mode)) printlink(accpath); (void)putchar('\n'); } static void printtime(ftime) time_t ftime; { int i; char longstring[80]; strftime(longstring, sizeof(longstring), "%c", localtime(&ftime)); for (i = 4; i < 11; ++i) (void)putchar(longstring[i]); #define SIXMONTHS ((365 / 2) * 86400) if (ftime + SIXMONTHS > time((time_t *)NULL)) for (i = 11; i < 16; ++i) (void)putchar(longstring[i]); else { (void)putchar(' '); for (i = 20; i < 24; ++i) (void)putchar(longstring[i]); } (void)putchar(' '); } static void printlink(name) char *name; { int lnklen; char path[MAXPATHLEN + 1]; if ((lnklen = readlink(name, path, MAXPATHLEN - 1)) == -1) { warn("%s", name); return; } path[lnklen] = '\0'; (void)printf(" -> %s", path); } Index: stable/4/usr.bin/find/main.c =================================================================== --- stable/4/usr.bin/find/main.c (revision 62017) +++ stable/4/usr.bin/find/main.c (revision 62018) @@ -1,160 +1,166 @@ /*- * Copyright (c) 1990, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Cimarron D. Taylor of the University of California, Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint char copyright[] = "@(#) Copyright (c) 1990, 1993, 1994\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint +#if 0 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include "find.h" time_t now; /* time find was run */ int dotfd; /* starting directory */ int ftsoptions; /* options for the ftsopen(3) call */ int isdeprecated; /* using deprecated syntax */ int isdepth; /* do directories on post-order visit */ int isoutput; /* user specified output operator */ int issort; /* do hierarchies in lexicographical order */ int isxargs; /* don't permit xargs delimiting chars */ +int mindepth = -1, maxdepth = -1; /* minimum and maximum depth */ static void usage __P((void)); int main(argc, argv) int argc; char *argv[]; { register char **p, **start; int Hflag, Lflag, ch; (void)setlocale(LC_ALL, ""); (void)time(&now); /* initialize the time-of-day */ p = start = argv; Hflag = Lflag = 0; ftsoptions = FTS_NOSTAT | FTS_PHYSICAL; while ((ch = getopt(argc, argv, "HLPXdf:sx")) != -1) switch (ch) { case 'H': Hflag = 1; Lflag = 0; break; case 'L': Lflag = 1; Hflag = 0; break; case 'P': Hflag = Lflag = 0; break; case 'X': isxargs = 1; break; case 'd': isdepth = 1; break; case 'f': *p++ = optarg; break; case 's': issort = 1; break; case 'x': ftsoptions |= FTS_XDEV; break; case '?': default: break; } argc -= optind; argv += optind; if (Hflag) ftsoptions |= FTS_COMFOLLOW; if (Lflag) { ftsoptions &= ~FTS_PHYSICAL; ftsoptions |= FTS_LOGICAL; } /* * Find first option to delimit the file list. The first argument * that starts with a -, or is a ! or a ( must be interpreted as a * part of the find expression, according to POSIX .2. */ for (; *argv != NULL; *p++ = *argv++) { if (argv[0][0] == '-') break; if ((argv[0][0] == '!' || argv[0][0] == '(') && argv[0][1] == '\0') break; } if (p == start) usage(); *p = NULL; if ((dotfd = open(".", O_RDONLY, 0)) < 0) err(1, "."); exit(find_execute(find_formplan(argv), start)); } static void usage() { (void)fprintf(stderr, "usage: find [-H | -L | -P] [-Xdsx] [-f file] [file ...] [expression]\n"); exit(1); } Index: stable/4/usr.bin/find/misc.c =================================================================== --- stable/4/usr.bin/find/misc.c (revision 62017) +++ stable/4/usr.bin/find/misc.c (revision 62018) @@ -1,127 +1,132 @@ /*- * Copyright (c) 1990, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Cimarron D. Taylor of the University of California, Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint +#if 0 static char sccsid[] = "@(#)misc.c 8.2 (Berkeley) 4/1/94"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* not lint */ #include #include #include #include #include #include #include #include #include "find.h" /* * brace_subst -- * Replace occurrences of {} in s1 with s2 and return the result string. */ void brace_subst(orig, store, path, len) char *orig, **store, *path; int len; { register int plen; register char ch, *p; plen = strlen(path); for (p = *store; (ch = *orig) != '\0'; ++orig) if (ch == '{' && orig[1] == '}') { while ((p - *store) + plen > len) if (!(*store = realloc(*store, len *= 2))) err(1, NULL); memmove(p, path, plen); p += plen; ++orig; } else *p++ = ch; *p = '\0'; } /* * queryuser -- * print a message to standard error and then read input from standard * input. If the input is 'y' then 1 is returned. */ int queryuser(argv) register char **argv; { int ch, first, nl; (void)fprintf(stderr, "\"%s", *argv); while (*++argv) (void)fprintf(stderr, " %s", *argv); (void)fprintf(stderr, "\"? "); (void)fflush(stderr); first = ch = getchar(); for (nl = 0;;) { if (ch == '\n') { nl = 1; break; } if (ch == EOF) break; ch = getchar(); } if (!nl) { (void)fprintf(stderr, "\n"); (void)fflush(stderr); } return (first == 'y'); } /* * emalloc -- * malloc with error checking. */ void * emalloc(len) u_int len; { void *p; if ((p = malloc(len)) == NULL) err(1, NULL); return (p); } Index: stable/4/usr.bin/find/option.c =================================================================== --- stable/4/usr.bin/find/option.c (revision 62017) +++ stable/4/usr.bin/find/option.c (revision 62018) @@ -1,168 +1,170 @@ /*- * Copyright (c) 1990, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Cimarron D. Taylor of the University of California, Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint /* static char sccsid[] = "@(#)option.c 8.2 (Berkeley) 4/16/94"; */ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include "find.h" static OPTION *option __P((char *)); /* NB: the following table must be sorted lexically. */ static OPTION const options[] = { { "!", N_NOT, c_not, O_ZERO }, { "(", N_OPENPAREN, c_openparen, O_ZERO }, { ")", N_CLOSEPAREN, c_closeparen, O_ZERO }, { "-a", N_AND, NULL, O_NONE }, { "-amin", N_AMIN, c_amin, O_ARGV }, { "-and", N_AND, NULL, O_NONE }, { "-atime", N_ATIME, c_atime, O_ARGV }, { "-cmin", N_CMIN, c_cmin, O_ARGV }, { "-ctime", N_CTIME, c_ctime, O_ARGV }, { "-delete", N_DELETE, c_delete, O_ZERO }, { "-depth", N_DEPTH, c_depth, O_ZERO }, { "-exec", N_EXEC, c_exec, O_ARGVP }, { "-execdir", N_EXECDIR, c_execdir, O_ARGVP }, { "-flags", N_FLAGS, c_flags, O_ARGV }, { "-follow", N_FOLLOW, c_follow, O_ZERO }, /* * NetBSD doesn't provide a getvfsbyname(), so this option * is not available if using a NetBSD kernel. */ #if !defined(__NetBSD__) { "-fstype", N_FSTYPE, c_fstype, O_ARGV }, #endif { "-group", N_GROUP, c_group, O_ARGV }, { "-inum", N_INUM, c_inum, O_ARGV }, { "-links", N_LINKS, c_links, O_ARGV }, { "-ls", N_LS, c_ls, O_ZERO }, + { "-maxdepth", N_MAXDEPTH, c_maxdepth, O_ARGV }, + { "-mindepth", N_MINDEPTH, c_mindepth, O_ARGV }, { "-mmin", N_MMIN, c_mmin, O_ARGV }, { "-mtime", N_MTIME, c_mtime, O_ARGV }, { "-name", N_NAME, c_name, O_ARGV }, { "-newer", N_NEWER, c_newer, O_ARGV }, { "-nogroup", N_NOGROUP, c_nogroup, O_ZERO }, { "-nouser", N_NOUSER, c_nouser, O_ZERO }, { "-o", N_OR, c_or, O_ZERO }, { "-ok", N_OK, c_exec, O_ARGVP }, { "-or", N_OR, c_or, O_ZERO }, { "-path", N_PATH, c_path, O_ARGV }, { "-perm", N_PERM, c_perm, O_ARGV }, { "-print", N_PRINT, c_print, O_ZERO }, { "-print0", N_PRINT0, c_print0, O_ZERO }, { "-prune", N_PRUNE, c_prune, O_ZERO }, { "-size", N_SIZE, c_size, O_ARGV }, { "-type", N_TYPE, c_type, O_ARGV }, { "-user", N_USER, c_user, O_ARGV }, { "-xdev", N_XDEV, c_xdev, O_ZERO }, }; /* * find_create -- * create a node corresponding to a command line argument. * * TODO: * add create/process function pointers to node, so we can skip * this switch stuff. */ PLAN * find_create(argvp) char ***argvp; { register OPTION *p; PLAN *new; char **argv; argv = *argvp; if ((p = option(*argv)) == NULL) errx(1, "%s: unknown option", *argv); ++argv; if (p->flags & (O_ARGV|O_ARGVP) && !*argv) errx(1, "%s: requires additional arguments", *--argv); switch(p->flags) { case O_NONE: new = NULL; break; case O_ZERO: new = (p->create)(); break; case O_ARGV: new = (p->create)(*argv++); break; case O_ARGVP: new = (p->create)(&argv, p->token == N_OK); break; default: abort(); } *argvp = argv; return (new); } static OPTION * option(name) char *name; { OPTION tmp; int typecompare __P((const void *, const void *)); tmp.name = name; return ((OPTION *)bsearch(&tmp, options, sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare)); } int typecompare(a, b) const void *a, *b; { return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name)); }