Hello all!
First off im pretty new to C and i need some help.
part of a project im working on requires that i get the members of a few groups from /etc/group.
i include grp.h
i tried to use getgrnam() but the problem i run into is that getgrnam always uses the same memory location to store the results. so on subsequent calls it overwrites the previous data.
Code:
eg.
// group1 is a pointer to string "root"
struct group *group1Ptr = getgrnam(group1);
//group2 is a pointer to string "users"
struct group *group2Ptr = getgrnam(group2);
char **group1mem;
char **group2mem;
printf("here are members of group1");
for(group1mem = group1Ptr->gr_mem;*group1mem;group1mem++)
{
printf("%s ",group1mem);
}
printf("here are members of group1");
for(group2mem = group2Ptr->gr_mem;*group2mem;group2mem++)
{
printf("%s ",group2mem);
the problem i run into here is that as soon as i declare:
Code:
struct group *group2Ptr = getgrnam(group2);
getgrnam writes its results to the same spot as it did when i first declared it for group1.
getgrnam() returns a pointer to struct group with the members set according to the line in etc/group with the group name inputed in getgrnam(groupname).
when i dereference group1mem->gr_mem to get the members of the group, it points to the same info as when i deref group2mem->gr_mem.
enter getgrnam_r() :
ok so i find that there is a re-entrant version of getgrnam. from what i understand it does the same thing as getgrnam except that it writes the results to a new memory space each time. i might be missunderstanding how this works.
here is the reference manual for grp.h and getgrnam_r:
i appologize for posting the whole page but the forum wont let me post a url (must have 5 posts to have a url in a message).
NAME
getgrnam, getgrnam_r - search group database for a name
SYNOPSIS
#include <sys/types.h>
#include <grp.h>
struct group *getgrnam(const char *name);
int getgrnam_r(const char *name, struct group *grp, char *buffer,
size_t bufsize, struct group **result);
DESCRIPTION
The getgrnam() function searches the group database for an entry with a matching name.
The getgrnam() interface need not be reentrant.
The getgrnam_r() function updates the group structure pointed to by grp and stores a pointer to that structure at the location pointed to by result. The structure contains an entry from the group database with a matching gid or name. Storage referenced by the group structure is allocated from the memory provided with the buffer parameter, which is bufsize characters in size. The maximum size needed for this buffer can be determined with the {_SC_GETGR_R_SIZE_MAX} sysconf() parameter. A NULL pointer is returned at the location pointed to by result on error or if the requested entry is not found.
RETURN VALUE
The getgrnam() function returns a pointer to a struct group with the structure defined in <grp.h> with a matching entry if one is found. The getgrnam() function returns a null pointer if either the requested entry was not found, or an error occurred. On error, errno will be set to indicate the error.
The return value may point to a static area which is overwritten by a subsequent call to getgrent(), getgrgid() or getgrnam().
If successful, the getgrnam_r() function returns zero. Otherwise, an error number is returned to indicate the error.
ERRORS
The getgrnam() function may fail if:
[EIO]
An I/O error has occurred.
[EINTR]
A signal was caught during getgrnam().
[EMFILE]
{OPEN_MAX} file descriptors are currently open in the calling process.
[ENFILE]
The maximum allowable number of files is currently open in the system.
The getgrnam_r() function may fail if:
[ERANGE]
Insufficient storage was supplied via buffer and bufsize to contain the data to be referenced by the resulting group structure.
EXAMPLES
None.
APPLICATION USAGE
Applications wishing to check for error situations should set errno to 0 before calling getgrnam(). If errno is set on return, an error occurred.
FUTURE DIRECTIONS
None.
SEE ALSO
endgrent(), getgrgid(), <grp.h>, <limits.h>, <sys/types.h>.
DERIVATION
getgrnam() derived from System V Release 2.0.
getgrnam_r() derived the POSIX Threads Extension (1003.1c-1995).
could someone help me with some examples of how i should call this so that i will end up with 2 char ** pointers each pointing to its own copy of the struct with its own data?
im pretty lost when it come to this pointer thing. its getting out of hand. im feeling like i have pointers to pointers to pointers to mars here.
humbly thanking you in advance for any help you can send my way.
- paradoxicalogic