stat 函式詳解

2021-09-29 22:03:07 字數 1559 閱讀 9241

stat函式

作用:獲取檔案資訊

標頭檔案:include #include #include ​函式原型:int stat(const char *path, struct stat *buf)

​返回值:成功返回0,失敗返回-1;

​引數:檔案路徑(名),struct stat 型別的結構體

struct stat 結構體詳解:
struct stat 

;

stat結構體中的st_mode 則定義了下列數種情況:
s_ifmt   0170000    檔案型別的位遮罩

s_ifsock 0140000 套接字

s_iflnk 0120000 符號連線

s_ifreg 0100000 一般檔案

s_ifblk 0060000 區塊裝置

s_ifdir 0040000 目錄

s_ifchr 0020000 字元裝置

s_ififo 0010000 先進先出

​ s_isuid 04000 檔案的(set user-id on execution)位

s_isgid 02000 檔案的(set group-id on execution)位

s_isvtx 01000 檔案的sticky位

​ s_irusr

(s_iread)

00400 檔案所有者具可讀取許可權

s_iwusr

(s_iwrite)

00200 檔案所有者具可寫入許可權

s_ixusr

(s_iexec)

00100 檔案所有者具可執行許可權

​ s_irgrp 00040 使用者組具可讀取許可權

s_iwgrp 00020 使用者組具可寫入許可權

s_ixgrp 00010 使用者組具可執行許可權

​ s_iroth 00004 其他使用者具可讀取許可權

s_iwoth 00002 其他使用者具可寫入許可權

s_ixoth 00001 其他使用者具可執行許可權

​ 上述的檔案型別在posix中定義了檢查這些型別的巨集定義:

s_islnk (st_mode) 判斷是否為符號連線

s_isreg (st_mode) 是否為一般檔案

s_isdir (st_mode) 是否為目錄

s_ischr (st_mode) 是否為字元裝置檔案

s_isblk (s3e) 是否為先進先出

s_issock (st_mode) 是否為socket

若一目錄具有sticky位(s_isvtx),則表示在此目錄下的檔案只能被該檔案所有者

此目錄所有者或root來刪除或改名,在linux中,最典型的就是這個/tmp目錄啦。

st_mode 主要包含了 3 部分資訊:

stat函式與stat結構體

在linux中,可以利用stat 函式來獲取乙個檔案的狀態 include include int stat const char file name,struct stat buf 這個函式執行成功返回0,失敗返回 1。取得的檔案狀態存放在buf指標指向的struct stat結構提中,struc...

stat 函式解析

stat 函式是用來獲取檔案的各種屬性的乙個linux下的常用api函式。函式原型為int stat const char path,struct stat buf stat定義如下 struct stat st mode 的定義 16位整數 0 2 其他人許可權 8進製 s iroth 00004...

stat函式的用法

stat函式的用法 標頭檔案 include 函式定義 int stat const char file name,struct stat buf 函式說明 通過檔名filename獲取檔案資訊,並儲存在buf所指的結構體stat中 返回值 執行成功則返回0,失敗返回 1,錯誤 存於errno 需要...