#include <grp.h>
|
struct group *
getgrent (void); |
struct group *
getgrnam (const char *name); |
int
getgrnam_r (const char *name, struct group *grp, char *buffer, size_t bufsize, struct group **result); |
struct group *
getgrgid (gid_t gid); |
int
getgrgid_r (gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result); |
int
setgroups (int n, const gid_t* ngroups); |
int
setgrent (void); |
void
endgrent (void); |
The setgrent return the value 1 if successful, otherwise the value 0 is returned. The endgrent API has no return value.
We refer to the structure group found in the include file
#include <grp.h>
struct group { char *gr_name; /* group name */ char *gr_passwd; /* group password */ gid_t gr_gid; /* group id */ char **gr_mem; /* group members */ }; The functions getgrnam and getgrgid returns the default values for a single user system i.e., gr_name,gr_passwd,gr_gid set to root,NULL and 0 respectively and gr_mem has just one entry,that of root as a single user in case the input parameter gid/name is 0/ root else the error number is set and NULL is returned by the APIs. The getgrent when called the first time by an application returns the pointer to a valid group, that of the simulated single-user group.Subsequent calls return NULL until setgrent is called The functions getgrnam_r, and getgrgid_r are thread-safe versions of getgrnam, and getgrgid respectively. The caller must provide storage for the results of the search in the grp, buffer, bufsize, and result arguments. When these functions are successful, the grp argument will be filled-in, and a pointer to that argument will be stored in result. If an entry is not found or an error occurs, result will be set to NULL. The setgrent function sets the library such that subsequent getgrent call returns a filled group structure. The endgrent function sets up the library such that the subsequent getgrent call returns NULL. The setgroups API returns success status just for the sake of build support.The actual function cannot be supported.
|
#include <grp.h> #include <string.h> #include <stdio.h> #include <stdlib.h> /*A user function that takes gid as parameter to call getgrgid Can be replaced by function that takes char* grnam to call getgrnam*/ void grgid_user(int gid) { struct group* gr_gid; /*This struct is filled by the getgrgid call*/ setgrent (); /*Set read to start of database*/ /*Call to getgrgid with gid as parameter..could also be replaced with getgrnam with grnam as parameter*/ gr_gid = getgrgid (gid); if(gr_gid == NULL) /*End of database reached*/ return; else printf ("For gid = %d ,group name = %s", gid, gr_gid->gr_name); /*Print a valid group name*/ endgrent (); /*Set the read pointer to the end of database*/ /*In case of our library,the user has to free the resources allocated by the API call itself*/ free (gr_gid->gr_mem); free (gr); }
Output
For gid = 0 ,group name = root
#include <grp.h> #include <string.h> #include <stdio.h> #include <stdlib.h> /*A user function that takes char* grnam as parameter to call getgrnam_r Can be replaced by function that takes int gid to call getgrgid_r*/ void grgid_r_user(char* grnam) { /*We have to allocate memory for the structures for the getgrnam_r thread-safe call*/ struct group* gr_nam = (struct group*)malloc (sizeof (struct group)); gr_nam->gr_mem = (char**)malloc (sizeof(char**)); char* buffer=(char*)malloc(sizeof(struct group)); size_t bufsize=sizeof(buffer); struct group* tempresult; int result; setgrent (); /*Set read to start of database*/ /*Call to getgrnam_r with grnam as parameter..could also be replaced with getgrgid_r with grgid as parameter*/ int result=getgrnam_r(grnam, gr_nam, buffer, bufsize, &tempresult); if(result != 0) /*End of database reached*/ return; else printf ("For grnam = %s ,group id = "%d", grnam, gr_nam->gr_gid); /*Print a valid group name*/ endgrent (); /*Set the read pointer to the end of database*/ /*The user has to free the resources allocated*/ free(buffer); free (gr_gid->gr_mem); free (gr); }
Output
For grnam = root ,group id = 0
#include <grp.h> #include <string.h> #include <stdio.h> #include <stdlib.h> /*A user function that takes calls getgrent API*/ void getgrent_user() { struct group* gr_ent; /*This struct is filled by the getgrent call*/ setgrent (); /*Set read to start of database*/ /*Call to getgrent */ gr_ent = getgrent (); if(gr_ent == NULL) /*End of database reached*/ return; else /*Print a valid group name*/ printf ("gid = %d ,group name = %s", gr_ent->gid, gr_ent->gr_name); endgrent (); /*Set the read pointer to the end of database*/ /*In case of our library,the user has to free the resources allocated by the API call itself*/ free (gr_ent->gr_mem); free (gr_ent); }
Output
gid = 0 ,group name = root
[ERANGE] | |
Not enough memory to allocate the structures. | |
[ENOENT] | |
If an invalid
gid
or
grnam
is passed to the APIs.
The getgrgid_r,getgrnam_r will fail if: | |
[ENOENT] | |
If an invalid gid or grnam is passed to the APIs. | |
© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user. |