read all file's information (filename , permission , size ..etc)
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
struct stat attr;
struct dirent *entry = NULL;
DIR *mydir = opendir( "/var/web/mount" );
stat ( "/var/web/mount" , &attr );
while((entry = readdir(mydir))) /* If we get EOF, the expression is 0 and
* the loop stops. */
{
printf ("Filename = %s\n" ,entry->d_name );
printf ("FileType = %d\n", entry->d_type);
printf("Size: %d K\n", (unsigned)attr.st_size/1024 +1 );
printf("Permissions: %o\n", (int)attr.st_mode & 07777);
printf("Is directory? %d\n", attr.st_mode & ST_ISDIR);
}
===============================================
struct--stat
----------------------------------------------------
struct stat {
mode_t st_mode; // file type & mode(permissions)
ino_t st_ino; // i-node number(serial number)
dev_t st_dev; // device number(filesystem)
dev_t st_rdev; // device number for specials files
nlink_t st_nlink; // number of links
uid_t st_uid; // user ID of owner
gid_t st_gid; // group ID of owner
off_t st_size; // size in bytes, for regular files
time_t st_atime; // time of last access
time_t st_mtime; // time of last modification
time_t st_ctime; // time of last file status change
long st_blksize; // best I/O block size
long st_blocks; -// number of 512-byte blocks allocated
};
===============================================
配合struct dirent,定義如下:
struct dirent { ino_t d_ino; off_t d_off; unsigned short int d_reclen; unsigned char d_type; char d_name[256]; };
d_ino 此目錄進入點的inode
d_off 目錄文件開頭至此目錄進入點的位移
d_reclen d_name的長度,不包含NULL字符
d_type d_name所指的檔案類型
d_name 檔名
其中unsigned cahr d_type;可以來判斷是子目錄或是一般的檔案。
沒有留言:
張貼留言