#define _POSIX_C_SOURCE 201800 /* still the same */ #include /* getgrent */ #include /* printf */ /* This program prints info about «groups» (which are stored in * ‹/etc/group›). The functions to access group information are * analogous to those for accessing user (passwd) information. This * program basically prints info from lines 10-13 of ‹/etc/group›. * */ int main() { int i, j; /* We first skip the first 10 entries because they are quite * boring. */ for ( i = 0; i < 10; ++i ) getgrent(); for ( i = 0; i < 3; ++i ) { struct group *grp = getgrent(); /* Groups have names and identifiers, just like users... * let's print those first. */ printf( "name: %s\n", grp->gr_name ); printf( "gid: %d\n", grp->gr_gid ); /* However, users only have a fixed number of fields * attached to them. The situation with groups is somewhat * different, because they contain a list of all users which * belong to the group. We don't know beforehand how many * such entries there are. The information is stored just * like ‹argv›: an array of strings. We know we hit the end * when we encounter a NULL string (pointer). Let's print * all the member users then: */ for ( j = 0; grp->gr_mem[j]; ++j ) printf( "member: %s\n", grp->gr_mem[j] ); printf( "\n" ); } } /* Next: ‹3.txt›. */