21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

214 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>The getgrent function steps through the database one entry at a time. It uses _gr_count to track its position in the list:function getgrent(){_gr_init()if (++_gr_count in _gr_bycount)return _gr_bycount[_gr_count]return ""}The endgrent function resets _gr_count to zero so that getgrent can start over again:function endgrent(){_gr_count = 0}As with the user database routines, each function calls _gr_init to initialize the arrays.Doing so only incurs the extra overhead of running grcat if these functions are used (asopposed to moving the body of _gr_init into a BEGIN rule).Most of the work is in scanning the database and building the various associative arrays.The functions that the user calls are themselves very simple, relying on awk’s associativearrays to do work.The id program in Section 13.2.3 [Printing out User Information], page 224, uses thesefunctions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!