summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authorwosch <wosch@FreeBSD.org>1998-11-29 00:54:21 +0000
committerwosch <wosch@FreeBSD.org>1998-11-29 00:54:21 +0000
commitd13a028d3d91883f7904f1da506602a7585990de (patch)
tree34fdcb14518f32cb2556ae8ef2c24cedb9653bdc /usr.bin
parent083b0d7688fa9abeb57de744e997044767be0259 (diff)
downloadFreeBSD-src-d13a028d3d91883f7904f1da506602a7585990de.zip
FreeBSD-src-d13a028d3d91883f7904f1da506602a7585990de.tar.gz
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.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/find/extern.h2
-rw-r--r--usr.bin/find/find.117
-rw-r--r--usr.bin/find/find.c17
-rw-r--r--usr.bin/find/main.c6
4 files changed, 37 insertions, 5 deletions
diff --git a/usr.bin/find/extern.h b/usr.bin/find/extern.h
index a11183d..b436ea1 100644
--- a/usr.bin/find/extern.h
+++ b/usr.bin/find/extern.h
@@ -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;
diff --git a/usr.bin/find/find.1 b/usr.bin/find/find.1
index 9757888..0d23566 100644
--- a/usr.bin/find/find.1
+++ b/usr.bin/find/find.1
@@ -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
diff --git a/usr.bin/find/find.c b/usr.bin/find/find.c
index 8afadf8b..c63362a 100644
--- a/usr.bin/find/find.c
+++ b/usr.bin/find/find.c
@@ -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
@@ -142,6 +144,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
* over all FTSENT's returned for the given search paths.
@@ -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;) {
diff --git a/usr.bin/find/main.c b/usr.bin/find/main.c
index 49ad226..2ba3ea4 100644
--- a/usr.bin/find/main.c
+++ b/usr.bin/find/main.c
@@ -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;
OpenPOWER on IntegriCloud