A summary of some of the functions used in C to manipulate password files

  • 2020-04-02 03:20:00
  • OfStack

C setpwent() function: reads the account data in the password file from scratch

The header file:


 #include <pwd.h> #include <sys/types.h>

Definition function:


void setpwent(void);

Setpwent () is used to refer the read and write address of getpwent() back to the beginning of the password file.

sample


#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:%sn", 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:%sn", 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 getpwent() function: gets the data of the account from the password file

The header file:


#include <pwd.h> #include <sys/types.h>

Definition function:


strcut passwd * getpwent(void);

Getpwent () is used to read an item of user data from the password file (/etc/passwd), whose data is returned as the passwd structure.

The passwd structure is defined as follows:


struct passwd
{
 char * pw_name; //The user account
 char * pw_passwd; //The user password
 uid_t pw_uid; //User identification code
 gid_t pw_gid; //Group identification number
 char * pw_gecos; //The user name
 char * pw_dir; //The home directory
 char * pw_shell; //The shell path used
};

Return value: returns the passwd structure data. If NULL is returned, there is no data or an error has occurred.

Note: getpwent() opens the password file on the first call and can be closed using endpwent() after reading the data.

Error code:
ENOMEM: out of memory to configure the passwd structure.

sample


#include <pwd.h>
#include <sys/types.h>
main()
{
 struct passwd *user;
 while((user = getpwent()) != 0)
 {
   printf("%s:%d:%d:%s:%s:%sn", 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 endpwent() function: close file (close password file)

The header file:


#include <pwd.h> #include <sys/types.h>

Definition function:


void endpwent(void);

Endpwent () is used to close the password file opened by getpwent().


Related articles: