?
Solved

Repeated use of getgrent fails

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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

718 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