Solved

Repeated use of getgrent fails

Posted on 2003-12-12
3
180 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now