Linux中用st_mode判斷檔案型別

透明水晶發表於2017-03-03

  來自:http://blog.csdn.net/simmerlee/article/details/8281399


在Linux中,可以利用stat()函式來獲取一個檔案的狀態

[cpp] view plain copy
  1. #include <sys/stat.h>  
  2. #include <unistd.h>  
  3.   
  4. int stat(const char *file_name, struct stat *buf);  
這個函式執行成功返回0,失敗返回-1。取得的檔案狀態存放在buf指標指向的struct stat結構提中, struct stat的定義如下:
[cpp] view plain copy
  1. struct stat    
  2. {    
  3.     dev_t       st_dev;     /* ID of device containing file -檔案所在裝置的ID*/    
  4.     ino_t       st_ino;     /* inode number -inode節點號*/  
  5.     mode_t      st_mode;    /* 檔案的型別和存取的許可權*/    
  6.     nlink_t     st_nlink;   /* number of hard links -鏈向此檔案的連線數(硬連線)*/    
  7.     uid_t       st_uid;     /* user ID of owner -user id*/    
  8.     gid_t       st_gid;     /* group ID of owner - group id*/    
  9.     dev_t       st_rdev;    /* device ID (if special file) -裝置號,針對裝置檔案*/    
  10.     off_t       st_size;    /* total size, in bytes -檔案大小,位元組為單位*/    
  11.     blksize_t   st_blksize; /* blocksize for filesystem I/O -系統塊的大小*/    
  12.     blkcnt_t    st_blocks;  /* number of blocks allocated -檔案所佔塊數*/    
  13.     time_t      st_atime;   /* time of last access -最近存取時間*/    
  14.     time_t      st_mtime;   /* time of last modification -最近修改時間*/    
  15.     time_t      st_ctime;   /* time of last status change - */    
  16. };    

其中, st_mode這個變數用來判斷檔案型別。

st_mode是用特徵位來表示檔案型別的,特徵位的定義如下:

[cpp] view plain copy
  1. S_IFMT      0170000     檔案型別的位遮罩  
  2. S_IFSOCK    0140000     socket  
  3. S_IFLNK     0120000     符號連結(symbolic link)  
  4. S_IFREG     0100000     一般檔案  
  5. S_IFBLK     0060000     區塊裝置(block device)  
  6. S_IFDIR     0040000     目錄  
  7. S_IFCHR     0020000     字元裝置(character device)  
  8. S_IFIFO     0010000     先進先出(fifo)  
  9. S_ISUID     0004000     檔案的(set user-id on execution)位  
  10. S_ISGID     0002000     檔案的(set group-id on execution)位  
  11. S_ISVTX     0001000     檔案的sticky位  
  12. S_IRWXU     00700       檔案所有者的遮罩值(即所有許可權值)  
  13. S_IRUSR     00400       檔案所有者具可讀取許可權  
  14. S_IWUSR     00200       檔案所有者具可寫入許可權  
  15. S_IXUSR     00100       檔案所有者具可執行許可權  
  16. S_IRWXG     00070       使用者組的遮罩值(即所有許可權值)  
  17. S_IRGRP     00040       使用者組具可讀取許可權  
  18. S_IWGRP     00020       使用者組具可寫入許可權  
  19. S_IXGRP     00010       使用者組具可執行許可權  
  20. S_IRWXO     00007       其他使用者的遮罩值(即所有許可權值)  
  21. S_IROTH     00004       其他使用者具可讀取許可權  
  22. S_IWOTH     00002       其他使用者具可寫入許可權  
  23. S_IXOTH     00001       其他使用者具可執行許可權  
  24. 摘自《Linux C 函式庫參考手冊》  

判斷檔案型別時,用對檔案的st_mode的值與上面給出的值相與,再比較。比如:

[cpp] view plain copy
  1. #include <sys/stat.h>  
  2. #include <unistd.h>  
  3. #include <stdio.h>  
  4.   
  5. int main()  
  6. {  
  7.     int abc;  
  8.     struct stat buf;  
  9.     stat("/home", &buf);  
  10.     abc = buf.st_mode & S_IFDIR;//與對應的標誌位相與  
  11.     if(abc == S_IFDIR)          //結果與標誌位比較  
  12.         printf("It's a directory.\n");  
  13.     return 0;  
  14. }  
執行結果:

It's a directory.


其實還有一個簡單的方法,檔案型別在POSIX中定義了檢查這些型別的巨集定義:

[cpp] view plain copy
  1. S_ISLINGK(st_mode)      判斷是否位符號連結  
  2. S_ISREG(st_mode)        是否為一般檔案  
  3. S_ISDIR(st_mode)        是否為目錄  
  4. S_ISCHR(st_mode)        是否位字元裝置檔案  
  5. S_ISBLK(s3e)            是否先進先出  
  6. S_ISSOCK(st_mode)       是否為socket  
可以根據這些函式的返回值判斷,如果是,則返回1。(我試了一下,好像是這樣的)

相關文章