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
};
1、获取文件属性(也可以通过stat命令获取文件属性)
int stat(cosnt char *restrict pathname, struct stat *restrict buf);
int fstat(int fd, struct stat *buf)
int lstat(const char path, struct stat *buf); 获取符号链接属性
获取文件大小
static int flen(const char *fname)
{
struct stat statres;
if(stat(fname, &statres) < 0) exit(0);
return statres.st_size;
}
int main(int argc, char ** argv)
{
if(argc < 2) exit(1);
printf(“%lld”, (long long)flen(argv[1]));
return 0;
}
st_mode成员中的参数:
S_ISREG() 普通文件
S_ISDIR() 目录文件
S_ISCHR() 字符特殊文件
S_ISBLK() 块特殊文件
S_ISFIFO() 管道或者FIFO
S_ISLNK() 符号链接
S_ISSOCK() 套接字
int main(int argc, char **argv)
{
int i;
struct stat buf;
char *ptr;
for(i=1; i <argc; i++)
{
if(lstat(argv[i], &buf) < 0)
continue;
}
if(S_ISREG(buf.st_mode)) ptr = "regular";
return 0;
}