A passwd structure related to system data is defined as follows: /* The passwd structure .*/ Struct passwd { Char * pw_name;/* User name */ Char * pw_passwd;/* password .*/ _ Uid_t pw_uid;/* User ID .*/ _ Gid_t pw_gid;/* Group ID .*/ Char * pw_gecos;/* real name */ Char * pw_dir;/* main directory .*/ Char * pw_shell;/* shell used */ }; This structure describes the/etc/passwd file record lines, including the user name, password, user ID, user group ID, real name, user home directory, and shell Related APIs include Struct passwd * getpwuid (UID) Struct paswd * getpwnam (const char * name) Getpwuid returns a structure pointing to passwd based on the input user ID. This struct initializes all the members in it. Getpwnam is the same as getpwuid, but the input parameter is the user name. Test Tool: This program only demonstrates and does not handle errors. # Include <stdio. h> # Include <stdlib. h> # Include <sys/types. h> # Include <sys/STAT. h> # Include <fcntl. h> # Include <unistd. h> # Include <PWD. h> Int main (void) { Struct passwd * passwd; Passwd = getpwuid (getuid ()); /** Passwd = getcwnam ("phpos ");**/ Printf ("username: % s/n", passwd-> pw_name ); Printf ("Password: % s/n", passwd-> pw_passwd ); Printf ("uid: % d/N", passwd-> pw_uid ); Printf ("GID: % d/N", passwd-> pw_gid ); Printf ("shell: % s/n", passwd-> pw_shell ); Printf ("dir: % s/n", passwd-> pw_dir ); Return 0; }
|