本文利用以下系統調用實現ls -l命令的功能:
1,lstat:獲得檔案狀態,
2,getpwuid:
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
描述:
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.
傳回值:
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 */
};
樣本:
#include <unistd.h><stdlib.h><stdio.h><pwd.h> ERR_EXIT(m) \ ( main( passwd *=((pw = getpwuid(uid)) ==,pw->,pw->,pw->,pw-> printf(,pw->,pw->
運行結果:
<stdlib.h><stdio.h> ERR_EXIT(m) \ ( main( argc, **(argc != , argv[ buf[(readlink(argv[],buf,) ==-,argv[
運行結果:
<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( * 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 &] = (mode &] = (mode &] = (mode &] = (mode &] = (mode &] = (mode &] = (mode &] =
運行結果: