#include <pwd.h>#include <stdio.h>
struct passwd
getpwent (void);
struct passwd
getpwuid (uid_t uid);
struct passwd
getpwnam (const char
name);
void setpwent (void);
void endpwent (void);
struct passwd
fgetpwent (FILE
f);
getpwnam- return pointer to password entry name
setpwent- rewind password file for further searches
endpwent- close password file
fgetpwent- return pointer to next password structure in stream
getpwent, getpwuid, and getpwnam each returns a pointer to an object with the following structure containing the broken-out fields of a line in the /etc/passwd file. Each line in the file contains a passwd structure, declared in the ``pwd.h'' header file:
struct passwd { charpw_name; char
pw_passwd; uid_t pw_uid; gid_t pw_gid; char
pw_age; char
pw_comment; char
pw_gecos; char
pw_dir; char
pw_shell; };
When first called, getpwent returns a pointer to the first passwd structure in the file; thereafter, it returns a pointer to the next passwd structure in the file. Thus successive calls can be used to search the entire file. getpwuid searches from the beginning of the file until a numerical user ID matching uid is found and returns a pointer to the particular structure in which it was found. getpwnam searches from the beginning of the file until a login name matching name is found, and returns a pointer to the particular structure in which it was found. If an end-of-file or an error is encountered on reading, these functions return a null pointer.
A call to setpwent has the effect of rewinding the password file to allow repeated searches. endpwent may be called to close the password file when processing is complete.
fgetpwent returns a pointer to the next passwd structure in the stream f, which matches the format of /etc/passwd.
Passwd structures will be obtained from NIS, when the dynamic versions of these routines are used and NIS is installed and running. See passwd(4) for the formats of NIS entries.