]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
getgroups.2: EXAMPLES: Add example program
authorAlejandro Colomar <alx@kernel.org>
Thu, 21 Nov 2024 23:52:18 +0000 (00:52 +0100)
committerAlejandro Colomar <alx@kernel.org>
Fri, 22 Nov 2024 00:14:37 +0000 (01:14 +0100)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
man/man2/getgroups.2

index efac39a02087b55be5819b1676a019f22e4f6c74..f5279a4338d2449d80e58c397382b628c21508c5 100644 (file)
@@ -209,6 +209,67 @@ cannot be larger than one more than this value.
 Since Linux 2.6.4, the maximum number of supplementary group IDs is also
 exposed via the Linux-specific read-only file,
 .IR /proc/sys/kernel/ngroups_max .
+.SH EXAMPLES
+.\" SRC BEGIN (agetgroups.c)
+.EX
+#include <err.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+\&
+#define MALLOC(n, T)  ((T *) reallocarray(NULL, n, sizeof(T)))
+\&
+static gid_t *agetgroups(size_t *ngids);
+\&
+int
+main(void)
+{
+    gid_t   *gids;
+    size_t  n;
+\&
+    gids = agetgroups(&n);
+    if (gids == NULL)
+        err(EXIT_FAILURE, "agetgroups");
+\&
+    if (n != 0) {
+        printf("%jd", (intmax_t) gids[0]);
+        for (size_t i = 1; i < n; i++)
+            printf(" %jd", (intmax_t) gids[i]);
+    }
+    puts("");
+\&
+    free(gids);
+    exit(EXIT_SUCCESS);
+}
+\&
+static gid_t *
+agetgroups(size_t *ngids)
+{
+    int    n;
+    gid_t  *gids;
+\&
+    n = getgroups(0, NULL);
+    if (n == \-1)
+        return NULL;
+\&
+    gids = MALLOC(n, gid_t);
+    if (gids == NULL)
+        return NULL;
+\&
+    n = getgroups(n, gids);
+    if (n == \-1) {
+        free(gids);
+        return NULL;
+    }
+\&
+    *ngids = n;
+    return gids;
+}
+.EE
+.\" SRC END
 .SH SEE ALSO
 .BR getgid (2),
 .BR setgid (2),