Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Repeated use of getgrent fails

Posted on 2003-12-12
3
Medium Priority
?
190 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 500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

927 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