LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2003, 10:15 PM   #1
paradoxicalogic
LQ Newbie
 
Registered: Aug 2003
Posts: 6

Rep: Reputation: 0
Help with getgrnam_r and char ** pointers


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
 
Old 08-20-2003, 12:03 AM   #2
paradoxicalogic
LQ Newbie
 
Registered: Aug 2003
Posts: 6

Original Poster
Rep: Reputation: 0
Yay! i figured it out. i knew that enough RTFMing and tinkering would eventually lead to my answer.

take care all!
peace
'Dox
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration