Linux系统编程之获取文件详细信息的stat函数介绍

Linux系统编程之stat函数主要用于获取文件的详细信息,将其结果保存在buf所指向的结构体struct stat中。执行成功则返回0,失败返回-1,错误代码存于errno。

一、 stat函数定义

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

#include <fcntl.h>	    /* Definition of AT_* constants */
#include <sys/stat.h>

int fstatat(int dirfd, const char *pathname, struct stat *statbuf,
		   int flags);

二、 struct stat结构体定义

struct stat {
	dev_t	 st_dev;	 /* ID of device containing file */
	ino_t	 st_ino;	 /* Inode number */
	mode_t	 st_mode;	 /* File type and mode */
	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;	 /* Block size for filesystem I/O */
	blkcnt_t	 st_blocks;	 /* Number of 512B blocks allocated */

	/* Since Linux 2.6, the kernel supports nanosecond
	precision for the following timestamp fields.
	For the details before Linux 2.6, see NOTES. */

	struct timespec st_atim;	 /* Time of last access */
	struct timespec st_mtim;	 /* Time of last modification */
	struct timespec st_ctim;	 /* Time of last status change */

#define st_atime st_atim.tv_sec	/* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

三、 测试实例

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysmacros.h>

int main(void)
{
	struct stat sb;

	if (stat("statdemo.c", &sb) == -1)
	{
		perror("stat error\n");
		return EXIT_FAILURE;
	}

	printf("ID of containing device:  [%lx,%lx]\n",
		(long) major(sb.st_dev), (long) minor(sb.st_dev));

	printf("File type:  ");

	switch (sb.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");
			break;
		case S_IFLNK:
			printf("symlink\n");
			break;
		case S_IFREG:
			printf("regular file\n");
			break;
		case S_IFSOCK:
			printf("socket\n");
			break;
		default:
			printf("unknown?\n");
			break;
	}

	printf("I-node number:  %ld\n", (long) sb.st_ino);

	printf("Mode:  %lo (octal)\n", (unsigned long) sb.st_mode);

	printf("Link count:  %ld\n", (long) sb.st_nlink);
	printf("Ownership:  UID=%ld   GID=%ld\n",
		(long) sb.st_uid, (long) sb.st_gid);

	printf("Preferred I/O block size: %ld bytes\n",
		(long) sb.st_blksize);
	printf("File size:  %lld bytes\n", (long long) sb.st_size);
	printf("Blocks allocated:  %lld\n", (long long) sb.st_blocks);

	printf("Last status change:  %s", ctime(&sb.st_ctime));
	printf("Last file access:  %s", ctime(&sb.st_atime));
	printf("Last file modification:  %s", ctime(&sb.st_mtime));

	return EXIT_SUCCESS;
}

四、 运行结果

[ycxie@fedora Workspace]$ gcc statdemo.c -o statdemo
[ycxie@fedora Workspace]$ ./statdemo
ID of containing device:  [fd,0]
File type:  regular file
I-node number:  1442428
Mode:  100644 (octal)
Link count:  1
Ownership:  UID=1000   GID=1000
Preferred I/O block size: 4096 bytes
File size:  1550 bytes
Blocks allocated:  8
Last status change:  Fri Aug 24 11:24:54 2018
Last file access:  Fri Aug 24 11:24:57 2018
Last file modification:  Fri Aug 24 11:24:54 2018
[ycxie@fedora Workspace]$ ls -l statdemo.c
-rw-r--r-- 1 ycxie ycxie 1550 Aug 24 11:24 statdemo.c

Leave a Reply

Your email address will not be published. Required fields are marked *