C language Setpwent () function: Read the account data from the password file from the beginning
Header file:
#include <pwd.h> #include <sys/types.h>
To define a function:
Function Description: Setpwent () is used to refer the read-write address of getpwent () to the beginning of the password file.
Example
#include <pwd.h>
#include <sys/types.h>
main ()
{
struct passwd *user;
int i;
for (i = 0; i < 4; i++)
{
user = Getpwent ();
printf ("%s:%d:%d:%s:%s:%s\n", User->pw_name, User->pw_uid, User->pw_gid,
User->pw_gecos, user- >pw_dir, User->pw_shell);
Setpwent ();
user = Getpwent ();
printf ("%s:%d:%d:%s:%s:%s\n", User->pw_name, User->pw_uid, User->pw_gid,
User->pw_gecos, user- >pw_dir, User->pw_shell);
Endpwent ();
}
Execution results:
Root:0:0:root:/root:/bin/bash
bin:1:1:bin:/bin
daemon:2:2:daemon:/sbin
adm:3:4:adm:/var/adm
Root:0:0:root:/root:/bin/bash
C language Getpwent () function: Obtain the account data from the password file
Header file:
#include <pwd.h> #include <sys/types.h>
To define a function:
Strcut passwd * getpwent (void);
Function Description: Getpwent () is used to read a user's data from a password file (/etc/passwd), and the user's data is returned in passwd structure. The first user data is obtained on the first call, and the next data is returned every time it is invoked, and Null is returned until no data is already in place.
The passwd structure is defined as follows:
struct passwd
{
char * pw_name;//user account
char * PW_PASSWD;//user password uid_t
pw_uid;//user ID code
gid_t pw_ Gid Group Identification Code
char * Pw_gecos//user full name
char * pw_dir//home directory
char * PW_SHELL;//The Shell path used
;
Return value: Returns the PASSWD structure data, if returned null indicates no data, or an error occurred.
Additional note: Getpwent () Opens the password file on the first call, and you can use Endpwent () to close the password file after reading the data.
Error code:
Enomem: There is not enough memory to configure the passwd structure.
Example
#include <pwd.h>
#include <sys/types.h>
main ()
{
struct passwd *user;
while (user = Getpwent ())!= 0)
{
printf ("%s:%d:%d:%s:%s:%s\n", User->pw_name, User->pw_uid, user-> Pw_gid,
User->pw_gecos, User->pw_dir, User->pw_shell);
Endpwent ();
}
Perform:
Root:0:0:root:/root:/bin/bash
bin:1:1:bin:/bin:
daemon:2:2:daemon:/sbin:
adm:3:4:adm:/var/adm:
LP:4:7:LP:/VAR/SPOOL/LPD:
sync:5:0:sync:/sbin:/bin/sync
shutdown:6:0:shutdown:/sbin:/sbin/ Shutdown
halt:7:0:halt:/sbin:/sbin/halt
mail:8:12:mail:/var/spool/mail:
news:9:13:news:var/spool/ News
UUCP:10:14:UUCP:/VAR/SPOOL/UUCP:
operator:11:0:operator:/root:
games:12:100:games:/usr/ Games:
gopher:13:30:gopher:/usr/lib/gopher-data:
ftp:14:50:ftp user:/home/ftp:
nobody:99:99: nobody:/:
xfs:100:101:x Font Server:/etc/xll/fs:/bin/false
gdm:42:42:/home/gdm:/bin/bash
Kids : 500:500::/home/kids:/bin/bash
C language Endpwent () function: Close file (Close password file)
Header file:
#include <pwd.h> #include <sys/types.h>
To define a function:
Function Description: Endpwent () is used to close the password file opened by Getpwent ().