From 1bc081398ebe58bb92f319550fdbd7b833bef5f3 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 9 Sep 2023 18:58:26 +0100 Subject: [PATCH] find: Add `-uid` option to filter by owning user ID --- Base/usr/share/man/man1/find.md | 2 ++ Userland/Utilities/find.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Base/usr/share/man/man1/find.md b/Base/usr/share/man/man1/find.md index acb65e084c..9e166b6608 100644 --- a/Base/usr/share/man/man1/find.md +++ b/Base/usr/share/man/man1/find.md @@ -78,6 +78,8 @@ by the current user. symbolic link is used. * `-gid [-|+]number`: Checks if the file is owned by a group with an ID less than, greater than or exactly `number`. +* `-uid [-|+]number`: Checks if the file is owned by a user with an ID less + than, greater than or exactly `number`. * `-print`: Outputs the file path, followed by a newline. Always evaluates to true. * `-print0`: Outputs the file path, followed by a zero byte. Always evaluates to diff --git a/Userland/Utilities/find.cpp b/Userland/Utilities/find.cpp index 8746c1ea92..a367d19623 100644 --- a/Userland/Utilities/find.cpp +++ b/Userland/Utilities/find.cpp @@ -511,7 +511,27 @@ private: return m_gid_range.contains(stat.st_gid); } - NumericRange m_gid_range; + NumericRange m_gid_range {}; +}; + +class UidCommand final : public StatCommand { +public: + UidCommand(char const* arg) + { + auto uid_or_error = NumericRange::parse({ arg, strlen(arg) }); + if (uid_or_error.is_error()) + fatal_error("find: Invalid argument '{}' to '-uid'", arg); + + m_uid_range = uid_or_error.release_value(); + } + +private: + virtual bool evaluate(struct stat const& stat) const override + { + return m_uid_range.contains(stat.st_uid); + } + + NumericRange m_uid_range {}; }; class PrintCommand final : public Command { @@ -737,6 +757,10 @@ static OwnPtr parse_simple_command(Vector& args) if (args.is_empty()) fatal_error("-gid: requires additional arguments"); return make(args.take_first()); + } else if (arg == "-uid") { + if (args.is_empty()) + fatal_error("-uid: requires additional arguments"); + return make(args.take_first()); } else if (arg == "-print") { g_have_seen_action_command = true; return make();