From 3eeb0a833207ecd96f44ad5882b881f8b196f527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 19 Apr 2022 10:08:37 +0300 Subject: [PATCH] getgrgid --- lib/DB.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/DB.zig b/lib/DB.zig index 36f09e3..78c9462 100644 --- a/lib/DB.zig +++ b/lib/DB.zig @@ -276,6 +276,15 @@ fn getgrnam(self: *const DB, name: []const u8, buf: *[]u8) error{OutOfMemory}!?C return try self.getGroup(group, buf); } +// get a CGroup entry by it's gid. +fn getgrgid(self: *const DB, gid: u32, buf: *[]u8) error{OutOfMemory}!?CGroup { + const idx = bdz.search_u32(self.bdz_gid, gid); + const offset = self.idx_gid2group[idx]; + const group = PackedGroup.fromBytes(self.groups[offset << 3 ..]).group; + if (gid != group.gid()) return null; + return try self.getGroup(group, buf); +} + fn shellSections( allocator: Allocator, corpus: *const Corpus, @@ -610,6 +619,11 @@ test "high-level API" { try testing.expectEqual(cgroup.name[1], 'l'); try testing.expectEqual(cgroup.name[2], 'l'); try testing.expectEqual(cgroup.name[3], 0); + + const all_gid = try db.getgrgid(9999, &buf); + try testing.expect(all_gid != null); + try testing.expectEqual(all_gid.?.gid, 9999); + try testing.expectEqualStrings(mem.sliceTo(all_gid.?.name, 0), "all"); } test "getgr_bufsize" {