實現ls -l
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void
file_type
(unsigned
char type)
//獲取檔案型別
}void
dir_count
(const
char
* pathname)
//獲取目錄中有多少個檔案,空目錄為2
int count =0;
struct dirent* dp =
null
;while
(dp =
readdir
(dir))}
printf
(" %d"
,count);}
void
user_name
(uid_t uid)
//獲取使用者名稱
void
group_name
(gid_t gid)
//獲取組員名
void
last_mtime
(time_t time)
//獲取檔案最後的修改時間
void
file_mode
(const
char
* path,
const
char
* name)
;sprintf
(pathname,
"%s/%s"
,path,name)
;struct stat buf =
;stat
(pathname,
&buf)
;putchar
(buf.st_mode&s_irusr?
'r':
'-')
;putchar
(buf.st_mode&s_iwusr?
'w':
'-')
;putchar
(buf.st_mode&s_ixusr?
'x':
'-')
;putchar
(buf.st_mode&s_irgrp?
'r':
'-')
;putchar
(buf.st_mode&s_iwgrp?
'w':
'-')
;putchar
(buf.st_mode&s_ixgrp?
'x':
'-')
;putchar
(buf.st_mode&s_iroth?
'r':
'-')
;putchar
(buf.st_mode&s_iwoth?
'w':
'-')
;putchar
(buf.st_mode&s_ixoth?
'x':
'-');if
(s_isdir
(buf.st_mode)
)dir_count
(pathname)
;else
printf
(" 1");
user_name
(buf.st_uid)
;group_name
(buf.st_gid)
;printf
(" %4lu"
,buf.st_size)
;last_mtime
(buf.st_mtime);}
void
list_path
(const
char
* path)
struct dirent* dp =
null
;while
(dp =
readdir
(dir))}
}int
main
(int argc,
const
char
* ar**)
當獲取到許可權、型別標識位後,也可使用以下巨集來判斷檔案的型別:
s_isreg
(m) 普通檔案 -
s_isdir
(m) 目錄檔案 d
s_ischr
(m) 字元裝置檔案 c
s_isblk
(m) 塊裝置檔案 b
s_isfifo
(m) 管道裝置檔案 p
s_islnk
(m) 軟鏈結 l
s_issock
(m) socket檔案 s
Linux 簡單實現 ls l 命令
ls l命令 系統效果 列出檔案屬性,許可權,使用者等資訊 簡單實現 include include include include include include include include include struct stat sbuf struct stat sbuf dst struc...
Linux命令實現(4) ls l
使用了stat 終於實現讀取檔案的詳細資訊 include include include include include include include void do ls char void dostat char void show file info char struct stat vo...
python實現linux下ls l的命令
usr bin env python3 coding utf 8 import os import shutil from stat import import pwd import grp import traceback import time defllist path try for nam...