Linux System Programming 27 System Data File and Information-/etc/passwd User Information File and Function getpwuid() getpwnam()

One, /etc/passwd

We found that
ls -l displays the user name and group name of the
thread ls -n displays the user id and group id of the thread

mhr@ubuntu:~$ ls -l
total 52
drwxr-xr-x 3 mhr mhr 4096 Sep  2  2019 Desktop
drwxr-xr-x 2 mhr mhr 4096 Sep  2  2019 Documents
drwxr-xr-x 2 mhr mhr 4096 Sep  2  2019 Downloads
-rw-r--r-- 1 mhr mhr 8980 Sep  2  2019 examples.desktop
drwxr-xr-x 2 mhr mhr 4096 Sep  2  2019 Music
drwxr-xr-x 2 mhr mhr 4096 Sep  2  2019 Pictures
drwxr-xr-x 2 mhr mhr 4096 Sep  2  2019 Public
drwxrwxr-x 2 mhr mhr 4096 Nov  9  2019 shiyan
drwxr-xr-x 2 mhr mhr 4096 Sep  2  2019 Templates
drwxr-xr-x 2 mhr mhr 4096 Sep  2  2019 Videos
drwxrwxr-x 8 mhr mhr 4096 Apr 16 08:17 work
mhr@ubuntu:~$ 
mhr@ubuntu:~$ 
mhr@ubuntu:~$ ls -n
total 52
drwxr-xr-x 3 1000 1000 4096 Sep  2  2019 Desktop
drwxr-xr-x 2 1000 1000 4096 Sep  2  2019 Documents
drwxr-xr-x 2 1000 1000 4096 Sep  2  2019 Downloads
-rw-r--r-- 1 1000 1000 8980 Sep  2  2019 examples.desktop
drwxr-xr-x 2 1000 1000 4096 Sep  2  2019 Music
drwxr-xr-x 2 1000 1000 4096 Sep  2  2019 Pictures
drwxr-xr-x 2 1000 1000 4096 Sep  2  2019 Public
drwxrwxr-x 2 1000 1000 4096 Nov  9  2019 shiyan
drwxr-xr-x 2 1000 1000 4096 Sep  2  2019 Templates
drwxr-xr-x 2 1000 1000 4096 Sep  2  2019 Videos
drwxrwxr-x 8 1000 1000 4096 Apr 16 08:17 work
mhr@ubuntu:~$ 

But when we implement the ls series of commands ourselves, we need to use information such as user name and group name, user id and group id, where is this information stored? In linux and unix systems, this information is stored in /etc/passwd. On FreeBSD systems, this information is stored in a database. In the HPUnix system, this information is stored in the file system.

Insert picture description here

mhr❌1000:1000:MHR,:/home/mhr:/bin/bash,
so you can find the corresponding user name or di in the /etc/passwd file in unix and linux systems.

Insert picture description here

Then each system stores the above information in different locations, and it is more troublesome for users to use, so there is a standard, and the standard is out of the mud~ The
standard says: Since your storage of these information is different, then I will be unified Let's implement the standard.

getpwuid()
getpwnam()

NAME
getpwnam, getpwnam_r, getpwuid, getpwuid_r-get password file entry
to query user information by uid or user name.
SYNOPSIS
#include <sys/types.h>
#include <pwd.h>

struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);

   The passwd structure is defined in <pwd.h> as follows:

       struct passwd {
           char   *pw_name;       /* username */
           char   *pw_passwd;     /* user password */
           uid_t   pw_uid;        /* user ID */
           gid_t   pw_gid;        /* group ID */
           char   *pw_gecos;      /* user information */
           char   *pw_dir;        /* home directory */
           char   *pw_shell;      /* shell program */
       };

Note that the fields in the passed of different systems may be different, please refer to the manual for details!

Experiment 1: Use getpwuid() to get passwd related information

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

int main(int argc,char *argv[])
{
	struct passwd *pwdline;
	if(argc < 2)
	{
		fprintf(stderr,"Usage:%s <src_file> <dest_file>\n",argv[0]);
		exit(1);
	}

	//atoi() : string --> int 转换
	pwdline = getpwuid(atoi(argv[1]));

	//puts string  打印用户名,打印字符换 直接而用 puts()
	puts(pwdline->pw_name);

	exit(0);
}


mhr@ubuntu:~/work/linux/muluheyonghucaozuo/27$ gcc getpwuid.c 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/27$ 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/27$ ./a.out 1000
mhr //我自己
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/27$ 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/27$ 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/27$ ./a.out 0
root // uid 为0  是 root 用户
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/27$ 

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/106033889