# fix: allow std.linux.getgroups to accept null · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/0f17cbfc6a19e639d6cb3f764e32b77e8c2869e7)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 0f17cbfc6a19e639d6cb3f764e32b77e8c2869e7

Requested commit: 0f17cbfc6a19e639d6cb3f764e32b77e8c2869e7

Commit: 0f17cbfc6a19e639d6cb3f764e32b77e8c2869e7

Tree: f9a33a952141560d0972b955a25995e56330e180

Author: Meili C

Committer: Alex Rønne Petersen

## Message

````
fix: allow std.linux.getgroups to accept null

looking at `man getgroups` and `info getgroups` this is given as an
example:

  ```c
       // Here's how to use ‘getgroups’ to read all the supplementary group
       // IDs:

            gid_t *
            read_all_groups (void)
            {
              int ngroups = getgroups (0, NULL);
              gid_t *groups
                = (gid_t *) xmalloc (ngroups * sizeof (gid_t));
              int val = getgroups (ngroups, groups);
              if (val < 0)
                {
                  free (groups);
                  return NULL;
                }
              return groups;
            }
  ```

getgroups(0, NULL) is used to get the count of groups so that the
correct count can be used to allocate a list of gid_t. This small changes makes this
possible.

equivalent example in Zig after the change:

  ```zig
    // get the group count
    const ngroups: usize = std.os.linux.getgroups(0, null);
    if (ngroups <= 0) {
        return error.GetGroupsError;
    }

    std.debug.print("number of groups: {d}\n", .{ngroups});
    const groups_gids: []u32 = try alloc.alloc(u32, ngroups);

    // populate an array of gid_t
    _ = std.os.linux.getgroups(ngroups, @ptrCast(groups_gids));
  ```

````

## Parents

- [77c63ac36034db577a9287c54fb6771429a7428f](https://git.cafe/gitcafe/zig/commit/77c63ac36034db577a9287c54fb6771429a7428f?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/0f17cbfc6a19e639d6cb3f764e32b77e8c2869e7?format=markdown)
