getgrent()

Updated: April 19, 2023

Return an entry from the group database

Synopsis:

#include <grp.h>

struct group* getgrent( void );

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The getgrent() function returns the next entry from the group database, although no particular order is guaranteed. This function uses a static buffer that's overwritten by each call.

Note: The getgrent(), getgrgid(), and getgrnam() function share the same static buffer.

Returns:

The next entry from the group database. When you first call getgrent(), the group database is opened. It remains open until either getgrent() returns NULL to signify end-of-file, or you call endgrent().

Errors:

The getgrent() function uses the following functions, and as a result, errno can be set to an error for any of these calls:

Examples:

/*
 * This program loops, reading a group name from
 * standard input and checking to see if it is a valid 
 * group. If it isn't valid, the entire contents of the 
 * group database are printed.
 */
#include <stdio.h>
#include <stdlib.h>
#include <grp.h>
#include <limits.h>

int main( void )
  {
    struct group* gr;
    char    buf[80];

    setgrent();
    while( gets(buf) != NULL) {
      if( (gr=getgrnam(buf)) != (struct group *)0) {
        printf("Valid group is: %s\n",gr->gr_name);
      } else {
        setgrent();
        while( (gr=getgrent()) != (struct group *)0 )
          printf("%s\n",gr->gr_name);
      }
    }
    endgrent();
    return( EXIT_SUCCESS );
  }

Classification:

POSIX 1003.1

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread No