Solved

Repeated use of getgrent fails

Posted on 2003-12-12
3
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

617 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