Linux--基礎篇--DIR dirent stat

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   sp   

Linux下 DIR結構體定義為

 1 struct __dirstream 2    { 3     void *__fd; 4     char *__data; 5     int __entry_data; 6     char *__ptr; 7     int __entry_ptr; 8     size_t __allocation; 9     size_t __size;10     __libc_lock_define (, __lock)11    };12 typedef struct __dirstream DIR;

該結構體儲存目錄相關的資訊,比如opendir(const char *);返回的就是DIR * 的類型;

用到該結構體的函數還有:

1 struct dirent *readdir(DIR *dp);2 void rewinddir(DIR *dp);3 int closedir(DIR *dp);4 long telldir(DIR *dp);5 void seekdir(DIR *dp,long loc);

 

得到DIR * 後,傳給readdir(DIR *)函數 就可以得到dirent類型的指標 dirent結構體不僅儲存了目錄資訊,還儲存了某個檔案的具體資訊

dirent 結構體定義為

1 struct dirent2 {3   long d_ino; /* inode number 索引節點號 */4     off_t d_off; /* offset to this dirent 在目錄檔案中的位移 */5     unsigned short d_reclen; /* length of this d_name 檔案名稱長 */6     unsigned char d_type; /* the type of d_name 檔案類型 */7     char d_name [NAME_MAX+1]; /* file name (null-terminated) 檔案名稱,最長255字元 */8 }

通過dirent 的d_name 藉助下面這個函數

int stat(const char *file_name, struct stat *buf);

可以得到某個檔案更詳細的資訊;也就可以實現linux中ls的功能了

stat結構體的定義為

 1 struct stat { 2         mode_t     st_mode;       //檔案存取權限 3         ino_t      st_ino;       //索引節點號 4         dev_t      st_dev;        //檔案使用的裝置號 5         dev_t      st_rdev;       //裝置檔案的裝置號 6         nlink_t    st_nlink;      //檔案的硬串連數 7         uid_t      st_uid;        //所有者使用者識別號 8         gid_t      st_gid;        //組識別號 9         off_t      st_size;       //以位元組為單位的檔案容量10         time_t     st_atime;      //最後一次訪問該檔案的時間11         time_t     st_mtime;      //最後一次修改該檔案的時間12         time_t     st_ctime;      //最後一次改變該檔案狀態的時間13         blksize_t st_blksize;    //包含該檔案的磁碟塊的大小14         blkcnt_t   st_blocks;     //該檔案所佔的磁碟塊15       };

 

 

這裡給出一個ls的簡單實現(查考UNIX 環境進階編程):

 

查考:http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/

Linux--基礎篇--DIR dirent stat

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.