linux系统编程之文件与IO(六):实现ls
linux系统编程之文件与IO(六):实现ls
本文利用以下系统调用实现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 &= = = =