#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf);
返回值:
RETURN VALUE:
On success, zero is returned. On error, -1 is returned, and errno is set appropri‐ately.
详细的 man 2 stat 查看
All of these system calls return a stat structure, which contains the following fields:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
The following POSIX macros are defined to check the file type using the st_mode field:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)<pre name="code" class="cpp">
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct stat buf;
int n;
if (argc != 2) {
fprintf(stderr, "Use:%s <pathname>\n", argv[0]);
exit(1);
}
n = stat(argv[1], &buf);
if (n == -1) {
perror("stat error");
}
switch (buf.st_mode & S_IFMT) {
case S_IFBLK:
printf("block device\n"); //块设备
break;
case S_IFCHR:
printf("character device\n"); //字符设备
break;
case S_IFDIR:
printf("directory\n"); //目录
break;
case S_IFIFO:
printf("FIFO/pipe\n"); //FIFO 管道
break;
case S_IFLNK:
printf("symlink\n"); //指的是软连接
break;
case S_IFREG:
printf("regular file\n"); //普通文件
break;
case S_IFSOCK:
printf("sock\n"); //嵌套字
break;
default:
printf("unknown\n");
break;
}
printf("Inode number:%ld\n", (long) buf.st_ino);
printf("ID dev:%ld\n", (long) buf.st_dev); //
printf("nlink:%ld\n", (long) buf.st_nlink); //硬链接数
printf("UID:%ld\n", (long) buf.st_uid);
printf("GID:%ld\n", (long) buf.st_gid);
printf("Rdev:%ld\n", (long) buf.st_rdev);
printf("Total size(byte):%ld\n", (long) buf.st_size);
printf("number of 512B block allocated:%ld\n", (long) buf.st_blocks);
printf("atime:%s", ctime(&buf.st_atime));
printf("mtime:%s", ctime(&buf.st_mtime));
printf("ctime:%s", ctime(&buf.st_ctime));
return 0;
}
#if 0
struct stat {
dev_t st_dev; /* ID of device containing file */ 设备id
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */ 权限,类型
nlink_t st_nlink; /* number of hard links */ 硬链接
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */ 从设备id
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */ 块大小
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */ 最后访问存储时间
time_t st_mtime; /* time of last modification */ 最后内容修改时间
time_t st_ctime; /* time of last status change */ 最后权限修改时间
}
#endif
/*
akaedu@akaedu-G41MT-D3:~/lin/20140822_文件系统_stat$ ./1 file
regular file
Inode number:8782005
ID dev:2055
nlink:2
UID:1000
GID:1000
Rdev:0
Total size(byte):19
number of 512B block allocated:8
atime:Fri Aug 22 20:34:27 2014
mtime:Fri Aug 22 20:34:18 2014
ctime:Fri Aug 22 20:34:18 2014
akaedu@akaedu-G41MT-D3:~/lin/20140822_文件系统_stat$ ./1 a
directory
Inode number:8782007
ID dev:2055
nlink:2
UID:1000
GID:1000
Rdev:0
Total size(byte):4096
number of 512B block allocated:8
atime:Fri Aug 22 20:00:10 2014
mtime:Fri Aug 22 20:00:10 2014
ctime:Fri Aug 22 20:00:10 2014
akaedu@akaedu-G41MT-D3:~/lin/20140822_文件系统_stat$
*/