This article uses the following system calls to implement the ls-l command function:
1. lstat: Get the File status,
2, getpwuid:
# Include <pwd. h>
Struct passwd * getpwuid (uid_t uid );
Description:
The getpwuid () function returns a pointer to a structure containing the broken-out fields of the record in the password database that matches the user ID uid.
Return Value:
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;/* real name */
Char * pw_dir;/* home directory */
Char * pw_shell;/* shell program */
};
Example:
#include <unistd.h><stdlib.h><stdio.h><pwd.h> ERR_EXIT(m) \ ( main( passwd *=((pw = getpwuid(uid)) ==,pw->,pw->,pw->,pw-> printf(,pw->,pw->
Running result:
<Stdlib. h> <stdio. h> ERR_EXIT (m) \ (main (argc, ** (argc! =, Argv [buf [(readlink (argv [], buf,) =-, argv [
Running result:
<Unistd. h> <stdlib. h> <dirent. h> <sys/types. h> <sys/stat. h> <. h> <time. h> <pwd. h> <grp. h> <libgen. h> ERR_EXIT (m) \ (lsdir (* lsfile (* getFileType (stat * getFilePerm (stat * st, * main (argc, ** (argc! =, Argv [(lstat (argv [], & fstat) =-lsdir (** filename [] = {= (dir = dirent * (dentry = readdir (dir ))! = * = Dentry-> (strncmp (fname,) = lsfile (* (lstat (filename, & tmpstat) ==- buf [] ={= getFileType (& * bname =] = (type = content [(readlink (filename, content ,) ==- & tm * = localtime (&-> tm_mon +-> getFileType (stat * type = (st-> st_mode & == === getFilePerm (stat * st, * = st-> (mode &] = (mode &] =
Running result: