diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-12-13 15:08:58 -0500 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-12-14 07:25:36 -0500 |
commit | d09266237d07924f382fb193159e0b5504b18135 (patch) | |
tree | 2cd09c687b1445eef735a83353b67b0de7019fe3 /Userland/Libraries/LibCore/Group.cpp | |
parent | 1ee808fae68ce1ebbe9eebd5924b87787317e617 (diff) | |
download | serenity-d09266237d07924f382fb193159e0b5504b18135.zip |
LibCore: Define and use a fallible, OS-independent getgrent(_r) wrapper
Rather than maintaining a list of #ifdef guards to check systems that do
not provide the reentrant version of getgrent, we can use C++ concepts
to let the compiler perform this check for us.
While we're at it, we can also provide this wrapper as fallible to let
the caller TRY calling it.
Diffstat (limited to 'Userland/Libraries/LibCore/Group.cpp')
-rw-r--r-- | Userland/Libraries/LibCore/Group.cpp | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/Userland/Libraries/LibCore/Group.cpp b/Userland/Libraries/LibCore/Group.cpp index 3ebc090fe3..6dc3c172a3 100644 --- a/Userland/Libraries/LibCore/Group.cpp +++ b/Userland/Libraries/LibCore/Group.cpp @@ -19,32 +19,27 @@ namespace Core { ErrorOr<DeprecatedString> Group::generate_group_file() const { StringBuilder builder; + char buffer[1024] = { 0 }; ScopeGuard grent_guard([] { endgrent(); }); setgrent(); - errno = 0; -#ifndef AK_OS_MACOS - struct group group; - struct group* gr = nullptr; - char buffer[1024] = { 0 }; - while (getgrent_r(&group, buffer, sizeof(buffer), &gr) == 0 && gr) { -#else - for (auto const* gr = getgrent(); gr; gr = getgrent()) { -#endif - if (gr->gr_name == m_name) + + while (true) { + auto group = TRY(Core::System::getgrent({ buffer, sizeof(buffer) })); + if (!group.has_value()) + break; + + if (group->gr_name == m_name) builder.appendff("{}:x:{}:{}\n", m_name, m_id, DeprecatedString::join(',', m_members)); else { Vector<DeprecatedString> members; - for (size_t i = 0; gr->gr_mem[i]; ++i) - members.append(gr->gr_mem[i]); + for (size_t i = 0; group->gr_mem[i]; ++i) + members.append(group->gr_mem[i]); - builder.appendff("{}:x:{}:{}\n", gr->gr_name, gr->gr_gid, DeprecatedString::join(',', members)); + builder.appendff("{}:x:{}:{}\n", group->gr_name, group->gr_gid, DeprecatedString::join(',', members)); } } - if (errno) - return Error::from_errno(errno); - return builder.to_deprecated_string(); } @@ -123,28 +118,23 @@ ErrorOr<void> Group::add_group(Group& group) ErrorOr<Vector<Group>> Group::all() { Vector<Group> groups; + char buffer[1024] = { 0 }; ScopeGuard grent_guard([] { endgrent(); }); setgrent(); - errno = 0; -#ifndef AK_OS_MACOS - struct group group; - struct group* gr = nullptr; - char buffer[1024] = { 0 }; - while (getgrent_r(&group, buffer, sizeof(buffer), &gr) == 0 && gr) { -#else - for (auto const* gr = getgrent(); gr; gr = getgrent()) { -#endif + + while (true) { + auto group = TRY(Core::System::getgrent({ buffer, sizeof(buffer) })); + if (!group.has_value()) + break; + Vector<DeprecatedString> members; - for (size_t i = 0; gr->gr_mem[i]; ++i) - members.append(gr->gr_mem[i]); + for (size_t i = 0; group->gr_mem[i]; ++i) + members.append(group->gr_mem[i]); - groups.append({ gr->gr_name, gr->gr_gid, members }); + groups.append({ group->gr_name, group->gr_gid, members }); } - if (errno) - return Error::from_errno(errno); - return groups; } |