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.

Chapter 12: A Library of awk Functions 213The group information is stored is several associative arrays. The arrays are indexedby group name (_gr_byname), by group ID number (_gr_bygid), and by positionin the database (_gr_bycount). There is an additional array indexed by user name(_gr_groupsbyuser), which is a space-separated list of groups to which each user belongs.Unlike the user database, it is possible to have multiple records in the database for thesame group. This is common when a group has a large number of members. A pair of suchentries might look like the following:tvpeople:*:101:johnny,jay,arseniotvpeople:*:101:david,conan,tom,joanFor this reason, _gr_init looks to see if a group name or group ID number is alreadyseen. If it is, then the user names are simply concatenated onto the previous list of users.(There is actually a subtle problem with the code just presented. Suppose that the firsttime there were no names. This code adds the names with a leading comma. It also doesn’tcheck that there is a $4.)Finally, _gr_init closes the pipeline to grcat, restores FS (and FIELDWIDTHS if necessary),RS, and $0, initializes _gr_count to zero (it is used later), and makes _gr_initednonzero.The getgrnam function takes a group name as its argument, and if that group exists, itis returned. Otherwise, getgrnam returns the null string:function getgrnam(group){_gr_init()if (group in _gr_byname)return _gr_byname[group]return ""}The getgrgid function is similar, it takes a numeric group ID and looks up the informationassociated with that group ID:function getgrgid(gid){_gr_init()if (gid in _gr_bygid)return _gr_bygid[gid]return ""}The getgruser function does not have a C counterpart. It takes a user name and returnsthe list of groups that have the user as a member:function getgruser(user){_gr_init()if (user in _gr_groupsbyuser)return _gr_groupsbyuser[user]return ""}

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

Saved successfully!

Ooh no, something went wrong!