Skip to content

Commit

Permalink
find: Add -uid option to filter by owning user ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tcl3 authored and ADKaster committed Sep 17, 2023
1 parent d3da8f9 commit 1bc0813
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Base/usr/share/man/man1/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion Userland/Utilities/find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,27 @@ class GidCommand final : public StatCommand {
return m_gid_range.contains(stat.st_gid);
}

NumericRange<gid_t> m_gid_range;
NumericRange<gid_t> m_gid_range {};
};

class UidCommand final : public StatCommand {
public:
UidCommand(char const* arg)
{
auto uid_or_error = NumericRange<uid_t>::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<uid_t> m_uid_range {};
};

class PrintCommand final : public Command {
Expand Down Expand Up @@ -737,6 +757,10 @@ static OwnPtr<Command> parse_simple_command(Vector<char*>& args)
if (args.is_empty())
fatal_error("-gid: requires additional arguments");
return make<GidCommand>(args.take_first());
} else if (arg == "-uid") {
if (args.is_empty())
fatal_error("-uid: requires additional arguments");
return make<UidCommand>(args.take_first());
} else if (arg == "-print") {
g_have_seen_action_command = true;
return make<PrintCommand>();
Expand Down

0 comments on commit 1bc0813

Please sign in to comment.