Added a sort option to find(1).

The sort option make it possible to build the locate
database without large (usually 20-100MB) temp files.
This commit is contained in:
Wolfram Schneider 1998-11-29 00:54:21 +00:00
parent 07bab7c6a4
commit 6d0c7e1378
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=41391
4 changed files with 37 additions and 5 deletions

View File

@ -83,4 +83,4 @@ PLAN *c_mtime __P((char *));
PLAN *c_not __P((void));
PLAN *c_or __P((void));
extern int ftsoptions, isdeprecated, isdepth, isoutput, isxargs;
extern int ftsoptions, isdeprecated, isdepth, isoutput, isxargs, issort;

View File

@ -33,7 +33,7 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)find.1 8.7 (Berkeley) 5/9/95
.\" $Id: find.1,v 1.14 1997/10/27 14:25:54 steve Exp $
.\" $Id: find.1,v 1.15 1998/05/15 11:22:36 jkoshy Exp $
.\"
.Dd May 9, 1995
.Dt FIND 1
@ -44,7 +44,7 @@
.Sh SYNOPSIS
.Nm find
.Op Fl H | Fl L | Fl P
.Op Fl Xdx
.Op Fl Xdsx
.Op Fl f Ar file
.Op Ar file ...
.Ar expression
@ -121,6 +121,19 @@ option specifies a file hierarchy for
to traverse.
File hierarchies may also be specified as the operands immediately
following the options.
.It Fl s
The
.Fl s
option cause
.Nm find
to traverse the file hierarchies in lexicographical order. The
output will be sorted too. Note:
.Sq find -s
and
.So
find | sort
.Sc
may give different results.
.It Fl x
The
.Fl x

View File

@ -50,6 +50,8 @@ static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94";
#include "find.h"
static int find_compare(const FTSENT **s1, const FTSENT **s2);
/*
* find_formplan --
* process the command line and create a "plan" corresponding to the
@ -141,6 +143,18 @@ find_formplan(argv)
FTS *tree; /* pointer to top of FTS hierarchy */
/*
* find_compare --
* A function which be used in fts_open() to order the
* traversal of the hierarchy.
* This function give you a lexicographical sorted output.
*/
static int find_compare(s1, s2)
const FTSENT **s1, **s2;
{
return strcoll( (*s1)->fts_name, (*s2)->fts_name );
}
/*
* find_execute --
* take a search plan and an array of search paths and executes the plan
@ -155,7 +169,8 @@ find_execute(plan, paths)
PLAN *p;
int rval;
if ((tree = fts_open(paths, ftsoptions, (int (*)())NULL)) == NULL)
if ((tree = fts_open(paths, ftsoptions,
(issort ? find_compare : NULL) )) == NULL)
err(1, "ftsopen");
for (rval = 0; (entry = fts_read(tree)) != NULL;) {

View File

@ -66,6 +66,7 @@ int isdeprecated; /* using deprecated syntax */
int isdepth; /* do directories on post-order visit */
int isoutput; /* user specified output operator */
int isxargs; /* don't permit xargs delimiting chars */
int issort; /* travel the file hierarchy lexicographical order */
static void usage __P((void));
@ -84,7 +85,7 @@ main(argc, argv)
p = start = argv;
Hflag = Lflag = 0;
ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "HLPXdf:x")) != -1)
while ((ch = getopt(argc, argv, "HLPXdf:sx")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
@ -106,6 +107,9 @@ main(argc, argv)
case 'f':
*p++ = optarg;
break;
case 's':
issort = 1;
break;
case 'x':
ftsoptions |= FTS_XDEV;
break;