getgrnam()

Updated: April 19, 2023

Get information about the group with a given name

Synopsis:

#include <sys/types.h>
#include <grp.h>

struct group* getgrnam( const char* name );

Arguments:

name
The name of the group you want to get information about.

Library:

libc

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

Description:

The getgrnam() function lets a process gain more knowledge about the group named name. This function uses a static buffer that's overwritten by each call.

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

Returns:

A pointer to an object of type struct group containing an entry from the group database with a matching name, or NULL on error or failure to find an entry with a matching name.

Examples:

/*
 * Print the name of all users in the group given in 
 * argv[1]
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <grp.h>

int main( int argc, char** argv )
  {
    struct group* g;
    char** p;

    if( ( g = getgrnam( argv[1] ) ) == NULL ) {
       fprintf( stderr, "getgrnam: %s failed\n",
            argv[1] );
       return( EXIT_FAILURE );
    }
    printf( "group name:%s\n", g->gr_name );
    for( p = g->gr_mem; *p != NULL; p++ ) {
       printf( "\t%s\n", *p );
    }
    return( EXIT_SUCCESS );
  }

Classification:

POSIX 1003.1

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