[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/3] Add find -s (sort) global option
From: |
Diego Ongaro |
Subject: |
[PATCH 1/3] Add find -s (sort) global option |
Date: |
Tue, 18 Aug 2020 17:51:36 -0700 |
This commit contains just the code changes for ftsfind. Docs and tests to
follow.
---
find/defs.h | 5 +++++
find/ftsfind.c | 7 ++++++-
find/util.c | 8 +++++++-
3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/find/defs.h b/find/defs.h
index 4cba5b45..71a622e1 100644
--- a/find/defs.h
+++ b/find/defs.h
@@ -554,6 +554,11 @@ enum DebugOption
struct options
{
+ /* If true, process files within each directory in the order defined by
+ * LC_COLLATE. If false (default), follow default FTS order.
+ */
+ bool sort;
+
/* If true, process directory before contents. True unless -depth given. */
bool do_dir_first;
/* If true, -depth was EXPLICITLY set (as opposed to having been turned
diff --git a/find/ftsfind.c b/find/ftsfind.c
index 783148c5..aa27666c 100644
--- a/find/ftsfind.c
+++ b/find/ftsfind.c
@@ -34,6 +34,7 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
+#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -514,6 +515,10 @@ consider_visiting (FTS *p, FTSENT *ent)
}
}
+static int compare(FTSENT const **a, FTSENT const **b) {
+ assert ((*a)->fts_parent == (*b)->fts_parent);
+ return strcoll((*a)->fts_name, (*b)->fts_name);
+}
static bool
@@ -547,7 +552,7 @@ find (char *arg)
if (options.stay_on_filesystem)
ftsoptions |= FTS_XDEV;
- p = fts_open (arglist, ftsoptions, NULL);
+ p = fts_open (arglist, ftsoptions, options.sort ? compare : NULL);
if (NULL == p)
{
error (0, errno, _("cannot search %s"),
diff --git a/find/util.c b/find/util.c
index 71db7f1c..805851a3 100644
--- a/find/util.c
+++ b/find/util.c
@@ -167,7 +167,7 @@ usage (int status)
#define HTL(t) fputs (t, stdout);
fprintf (stdout, _("\
-Usage: %s [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]\n"),
+Usage: %s [-H] [-L] [-P] [-s] [-Olevel] [-D debugopts] [path...]
[expression]\n"),
program_name);
HTL (_("\n\
@@ -944,6 +944,11 @@ process_leading_options (int argc, char *argv[])
/* Meaning: never dereference symbolic links (default). */
set_follow_state (SYMLINK_NEVER_DEREF);
}
+ else if (0 == strcmp ("-s", argv[i]))
+ {
+ /* Meaning: sort files by name within each directory */
+ options.sort = true;
+ }
else if (0 == strcmp ("--", argv[i]))
{
/* -- signifies the end of options. */
@@ -1034,6 +1039,7 @@ set_option_defaults (struct options *p)
p->warnings = false;
}
+ p->sort = false;
p->do_dir_first = true;
p->explicit_depth = false;
p->maxdepth = p->mindepth = -1;
--
2.27.0