summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/Group.cpp
diff options
context:
space:
mode:
authorne0ndrag0n <ne0ndrag0n@users.noreply.github.com>2022-10-15 00:40:51 -0400
committerAndrew Kaster <andrewdkaster@gmail.com>2022-11-13 17:49:03 -0700
commit9ddb86f7db59ffb631e0d99486dfb104345a9ad4 (patch)
tree98b7df3d4fd03a42779abd062e1df0dd0fd7e8c1 /Userland/Libraries/LibCore/Group.cpp
parent58e9262ff1665123bc58b2c99efbbe7341e00f78 (diff)
downloadserenity-9ddb86f7db59ffb631e0d99486dfb104345a9ad4.zip
LibCore: Add query for all accounts and groups
Diffstat (limited to 'Userland/Libraries/LibCore/Group.cpp')
-rw-r--r--Userland/Libraries/LibCore/Group.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Group.cpp b/Userland/Libraries/LibCore/Group.cpp
index e38aace24e..e97d1eaf2e 100644
--- a/Userland/Libraries/LibCore/Group.cpp
+++ b/Userland/Libraries/LibCore/Group.cpp
@@ -6,8 +6,10 @@
#include <AK/CharacterTypes.h>
#include <AK/ScopeGuard.h>
+#include <LibCore/File.h>
#include <LibCore/Group.h>
#include <LibCore/System.h>
+#include <errno.h>
namespace Core {
@@ -60,6 +62,27 @@ ErrorOr<void> Group::add_group(Group& group)
}
#endif
+ErrorOr<Vector<Group>> Group::all()
+{
+ Vector<Group> groups;
+
+ ScopeGuard grent_guard([] { endgrent(); });
+ setgrent();
+ errno = 0;
+ for (auto const* gr = getgrent(); gr; gr = getgrent()) {
+ Vector<String> members;
+ for (size_t i = 0; gr->gr_mem[i]; ++i)
+ members.append(*gr->gr_mem);
+
+ groups.append({ gr->gr_name, gr->gr_gid, members });
+ }
+
+ if (errno)
+ return Error::from_errno(errno);
+
+ return groups;
+}
+
Group::Group(String name, gid_t id, Vector<String> members)
: m_name(move(name))
, m_id(id)