endpwent - Linux
Overview
The endpwent
command in Linux is used to terminate the iteration over entries in the system password database /etc/passwd
. It is called at the end of a program that has used the functions getpwent()
, getpwnam()
, or getpwuid()
.
Syntax
endpwent()
Options/Flags
endpwent
does not accept any options or flags.
Examples
To iterate over all user entries in the system password database:
#include <pwd.h>
#include <stdio.h>
int main() {
struct passwd *pwent;
setpwent();
while ((pwent = getpwent()) != NULL) {
printf("Username: %s\n", pwent->pw_name);
}
endpwent();
return 0;
}
Common Issues
- Error:
endpwent(): Invalid argument
This error usually occurs when endpwent()
is called without first calling setpwent()
. Ensure that setpwent()
is called before using getpwent()
and endpwent()
.
Integration
endpwent
can be used in conjunction with other commands that manipulate the system password database, such as passwd
and adduser
.
Related Commands
getpwent()
getpwnam()
getpwuid()
passwd
adduser