From: Alejandro Colomar Date: Thu, 21 Nov 2024 23:52:18 +0000 (+0100) Subject: getgroups.2: EXAMPLES: Add example program X-Git-Tag: man-pages-6.10~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f7606a15faac8c5b7fe07b35f5ebcb9e00afb11;p=thirdparty%2Fman-pages.git getgroups.2: EXAMPLES: Add example program Signed-off-by: Alejandro Colomar --- diff --git a/man/man2/getgroups.2 b/man/man2/getgroups.2 index efac39a02..f5279a433 100644 --- a/man/man2/getgroups.2 +++ b/man/man2/getgroups.2 @@ -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 +#include +#include +#include +#include +#include +#include +\& +#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),