Solved

Repeated use of getgrent fails

Posted on 2003-12-12
3
183 Views
Last Modified: 2008-03-06
I am writing an administration tool and I thought perl would be more interesting than ksh, but I am running before I can walk.

To begin with I am loading up some data structures, so I use getgrent within a loop to read the group data. No problem.

At some stage I may modify the group data. I then want to reload the data.

My problem is that the second attempt to run a getgrent loop returns an empty structure. I guess that somhow I need to reset whatever pointer the getgrent function uses.

Any takers?

0
Comment
Question by:glassd
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
kandura earned 125 total points
ID: 9927692
I guess the manpage tells you enough:

       The  getgrent()  function returns a pointer to a structure
       containing the group  information  from  /etc/group.   The
       first time it is called it returns the first entry; there­
       after, it returns successive entries.

       The setgrent() function rewinds the file  pointer  to  the
       beginning of the /etc/group file.

       The endgrent() function closes the /etc/group file.


In other words, either call setgrent() or endgrent() before restarting your loop.
0
 
LVL 7

Author Comment

by:glassd
ID: 9928117
Ah, those functions are not on my book (Programming Perl). Looks like I need a bigger book.

Tried setgrent and it seems to work fine.

Thanks for the prompt reply.
0
 
LVL 18

Expert Comment

by:kandura
ID: 9928178
They're listed in perlfunc, but I got these descriptions from my linux manpages:

man perlfunc   --- for standard perl functions
man getgrent  --- for linux system call

HTH :-)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question