From 0ca63cfd6e932cfe3dfe03c09c00fa27e4ca08ee Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Tue, 23 Nov 2021 18:48:30 +0100 Subject: [PATCH] find: Implement support for multiple directories --- Base/usr/share/man/man1/find.md | 6 +++--- Userland/Utilities/find.cpp | 35 +++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Base/usr/share/man/man1/find.md b/Base/usr/share/man/man1/find.md index f7f2c575815..92cfe235be6 100644 --- a/Base/usr/share/man/man1/find.md +++ b/Base/usr/share/man/man1/find.md @@ -5,13 +5,13 @@ find - recursively search for files ## Synopsis ```**sh -$ find [-L] [root-path] [commands...] +$ find [-L] [root-paths...] [commands...] ``` ## Description -`find` recursively traverses the file hierarchy starting at the given root path -(or at the current working directory if the root path is not specified), and +`find` recursively traverses the file hierarchy starting at the given root paths +(or at the current working directory if no root paths have been specified), and evaluates the given commands for each found file. The commands can be used to both filter the set of files and to perform actions on them. diff --git a/Userland/Utilities/find.cpp b/Userland/Utilities/find.cpp index 500c9ecf60d..068bf11f9af 100644 --- a/Userland/Utilities/find.cpp +++ b/Userland/Utilities/find.cpp @@ -541,7 +541,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args.append(arguments.argv + 1, arguments.argc - 1); OwnPtr command; - LexicalPath root_path = LexicalPath("."); + Vector paths; while (!args.is_empty()) { char* raw_arg = args.take_first(); @@ -549,7 +549,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (arg == "-L") { g_follow_symlinks = true; } else if (!arg.starts_with('-')) { - root_path = LexicalPath(arg); + paths.append(LexicalPath(arg)); } else { // No special case, so add back the argument and try to parse a command. args.prepend(raw_arg); @@ -560,21 +560,26 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!command) command = make(); - String dirname = root_path.dirname(); - String basename = root_path.basename(); + if (paths.is_empty()) + paths.append(LexicalPath(".")); - int dirfd = TRY(Core::System::open(dirname, O_RDONLY | O_DIRECTORY | O_CLOEXEC)); + for (auto& path : paths) { + String dirname = path.dirname(); + String basename = path.basename(); - FileData file_data { - root_path, - dirfd, - basename.characters(), - (struct stat) {}, - false, - DT_UNKNOWN, - }; - walk_tree(file_data, *command); - close(dirfd); + int dirfd = TRY(Core::System::open(dirname, O_RDONLY | O_DIRECTORY | O_CLOEXEC)); + + FileData file_data { + path, + dirfd, + basename.characters(), + (struct stat) {}, + false, + DT_UNKNOWN, + }; + walk_tree(file_data, *command); + close(dirfd); + } return g_there_was_an_error ? 1 : 0; }