|
System databases
The C library calls which query the databases are, amongst others,
getpwnam get password data by
name
getpwuid get password data by
uid
getgrnam get group data by
name
gethostent get entry in hosts
database
getnetgrent get entry in netgroups
database
getservbyname get servive by
name
getservbyport get service by
port
get protobyname get protocol by
name
For a complete list and how to use these, see the UNIX manual.
The following example shows how to read the password file of the system.
The functions used here can be used regardless of whether the network
information service (NIS) is in use. The data are returned in a structure which
is defined in
´/usr/include/pwd.h'.
/******************************************************************/
/*
*/
/* Read the passwd file by name and
sequentially */
/*
*/
/******************************************************************/
#include
<unistd.h>
#include <pwd.h>
main ()
{ uid_t uid;
struct passwd *pw;
uid = getuid();
pw = getpwuid(uid);
printf ("Your login name is
%s\n",pw->pw_name);
printf ("Now here comes the whole
file!\n\n");
setpwent();
while (getpwent())
{
printf
("%s:%s:%s\n",pw->pw_name,pw->pw_gecos,pw->pw_dir);
}
endpwent();
}
|