[Previous] [Contents] [Next]

getgrent()

Return an entry from the group database

Synopsis:

#include <grp.h>

struct group* getgrent( void );

Library:

libc

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 getgrent() is first called, the group database is opened, and remains open until either a NULL is returned to signify end-of-file, or endgrent() is called.

Errors:

The getgrent() function uses the following functions, and as a result errno can be set to a valid 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:

Standard Unix

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

See also:

endgrent(), errno, getgrgid(), getgrnam(), getpwent(), setgrent()


[Previous] [Contents] [Next]