COHERENT manpages
This page displays the COHERENT manpage for getpwent() [Get password file information].
List of available manpages
Index
getpwent() -- General Function (libc)
Get password file information
#include <pwd.h>
struct passwd *getpwent()
The COHERENT system has five routines that search the file /etc/passwd,
which contains information about every user of the system. The returned
structure passwd is defined in the header file pwd.h. For a description of
this structure, see pwd.h.
getpwent() returns the next entry from /etc/passwd.
Example
The following example demonstrates getpwent(), getpwnam(), getpwuid(),
setpwent(), and endpwent().
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
main()
{
int euid, /* Effective user id */
ruid; /* Real user id */
struct passwd *pstp;
int i;
/* Print out all users and home directories */
i = 0;
setpwent(); /* Rewind file /etc/passwd */
while ((pstp = getpwent()) != NULL)
printf("%d: user name is %s, home directory is %s.\n",
++i, pstp->pw_name, pstp->pw_dir);
/* Find real user name.
* NOTE: functions getpwuid and getpwnam rewind /etc/passwd
* by calling setpwent().
*/
ruid = getuid();
if ((pstp = getpwuid(ruid)) == NULL) {
/* If this message appears, something's wrong */
fprintf(stderr, "Cannot find user with id number %d\n", pstp);
exit (EXIT_FAILURE);
} else
printf("User's real name is %s\n", pstp->pw_name);
/* Find the user id for superuser root */
((pstp = getpwnam("root")) == NULL) ?
fprintf(stderr, "Do you have user root on your system?\n") :
printf("root id is %d\n", pstp->pw_uid);
/* Check if the effective process id is the superuser id.
*
* NOTE: if you wish to see how to enable the root
* privileges, you can run this command:
* cc pwfun.c
* su root chown root pwfun
* su root chmod 4511 pwfun
*/
euid = geteuid(); /* Get effective user id. */
printf("Process ");
(euid == pstp->pw_uid) ? printf("has ") : printf("doesn't have ");
printf("the root privileges\n");
exit(EXIT_SUCCESS);
}
Files
/etc/passwd
pwd.h
See Also
endpwent(),
getpwnam(),
getpwuid(),
libc,
pwd.h,
setpwent()
Diagnostics
getpwent() returns NULL for any error or on end of file.
Notes
All structures and information returned are in static areas internal to
getpwent(). Therefore, information from a previous call is overwritten by
each subsequent call.
If your system has implemented shadow passwords, you must use the shadow-
password routine getspent() to retrieve records that contain passwords.
For details, see this function's entry in the Lexicon.