21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

206 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>error x -- invalid option⊣ c = , optarg = ⊣ non-option arguments:⊣ ARGV[4] = ⊣ ARGV[5] = In both runs, the first ‘--’ terminates the arguments to awk, so that it does not try tointerpret the ‘-a’, etc., as its own options.NOTE: After getopt is through, it is the responsibility of the user level codeto clear out all the elements of ARGV from 1 to Optind, so that awk does not tryto process the command-line options as file names.Several of the sample programs presented in Chapter 13 [Practical awk Programs],page 215, use getopt to process their arguments.12.5 Reading the User DatabaseThe PROCINFO array (see Section 6.5 [Built-in Variables], page 110) provides access to thecurrent user’s real and effective user and group ID numbers, and if available, the user’ssupplementary group set. However, because these are numbers, they do not provide veryuseful information to the average user. There needs to be some way to find the userinformation associated with the user and group ID numbers. This section presents a suiteof functions for retrieving information from the user database. See Section 12.6 [Readingthe Group Database], page 210, for a similar suite that retrieves information from the groupdatabase.The POSIX standard does not define the file where user information is kept. Instead,it provides the header file and several C language subroutines for obtaining userinformation. The primary function is getpwent, for “get password entry.” The “password”comes from the original user database file, ‘/etc/passwd’, which stores user information,along with the encrypted passwords (hence the name).While an awk program could simply read ‘/etc/passwd’ directly, this file may not containcomplete information about the system’s set of users. 7 To be sure you are able to producea readable and complete version of the user database, it is necessary to write a small Cprogram that calls getpwent. getpwent is defined as returning a pointer to a structpasswd. Each time it is called, it returns the next entry in the database. When thereare no more entries, it returns NULL, the null pointer. When this happens, the C programshould call endpwent to close the database. Following is pwcat, a C program that “cats”the password database:/** pwcat.c** Generate a printable version of the password database*/#include #include 7 It is often the case that password information is stored in a network database.

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

Saved successfully!

Ooh no, something went wrong!