stat函数用于获取文件的属性信息(例如文件类型、访问权限和访问时间等)。函数原型如下:
#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf);
返回值:成功返回0,出错返回-1。
参数:
1、pathname 文件路径
2、buf存储struct stat结构信息的缓冲区。
其中struct stat结构定义在文件<sys/stat.h>中。实际定义在<bits/stat.h>中,因为<sys/stat.h>包含了<bits/stat.h>
头文件中对struct stat结构的定义很复杂,但实际我们比较关心其中一些常用数据成员:st_mode(文件权限方面的属性)、st_uid(文件用户ID)、st_gid(文件组ID)、st_atime(数据的最后访问时间)、st_mtime(数据的最后修改时间)、st_ctime(i节点最后更改时间)、st_blksize(文件的块大小)、st_blocks(文件包含的块数)和st_size(文件大小字节数)
简化后的struct stat结构是这样:
struct stat
{
__dev_t st_dev; /* Device. */
unsigned short int __pad1;
__ino_t __st_ino; /* 32bit file serial number. */
__mode_t st_mode; /* File mode. */
__nlink_t st_nlink; /* Link count. */
__uid_t st_uid; /* User ID of the file's owner. */
__gid_t st_gid; /* Group ID of the file's group.*/
__dev_t st_rdev; /* Device number, if device. */
unsigned short int __pad2;
__off_t st_size; /* Size of file, in bytes. */
__blksize_t st_blksize; /* Optimal block size for I/O. */
__blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */
__time_t st_atime; /* Time of last access. */
unsigned long int st_atimensec; /* Nscecs of last access. */
__time_t st_mtime; /* Time of last modification. */
unsigned long int st_mtimensec; /* Nsecs of last modification. */
__time_t st_ctime; /* Time of last status change. */
unsigned long int st_ctimensec; /* Nsecs of last status change. */
};
<bits/stat.h>对struct stat结构的定义原文如下:
struct stat
{
__dev_t st_dev; /* Device. */
unsigned short int __pad1;
#ifndef __USE_FILE_OFFSET64
__ino_t st_ino; /* File serial number. */
#else
__ino_t __st_ino; /* 32bit file serial number. */
#endif
__mode_t st_mode; /* File mode. */
__nlink_t st_nlink; /* Link count. */
__uid_t st_uid; /* User ID of the file's owner. */
__gid_t st_gid; /* Group ID of the file's group.*/
__dev_t st_rdev; /* Device number, if device. */
unsigned short int __pad2;
#ifndef __USE_FILE_OFFSET64
__off_t st_size; /* Size of file, in bytes. */
#else
__off64_t st_size; /* Size of file, in bytes. */
#endif
__blksize_t st_blksize; /* Optimal block size for I/O. */
#ifndef __USE_FILE_OFFSET64
__blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */
#else
__blkcnt64_t st_blocks; /* Number 512-byte blocks allocated. */
#endif
#if defined __USE_MISC || defined __USE_XOPEN2K8
/* Nanosecond resolution timestamps are stored in a format
equivalent to 'struct timespec'. This is the type used
whenever possible but the Unix namespace rules do not allow the
identifier 'timespec' to appear in the <sys/stat.h> header.
Therefore we have to handle the use of this header in strictly
standard-compliant sources special. */
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
#else
__time_t st_atime; /* Time of last access. */
unsigned long int st_atimensec; /* Nscecs of last access. */
__time_t st_mtime; /* Time of last modification. */
unsigned long int st_mtimensec; /* Nsecs of last modification. */
__time_t st_ctime; /* Time of last status change. */
unsigned long int st_ctimensec; /* Nsecs of last status change. */
#endif
#ifndef __USE_FILE_OFFSET64
unsigned long int __unused4;
unsigned long int __unused5;
#else
__ino64_t st_ino; /* File serial number. */
#endif
};
#ifdef __USE_LARGEFILE64
struct stat64
{
__dev_t st_dev; /* Device. */
unsigned int __pad1;
__ino_t __st_ino; /* 32bit file serial number. */
__mode_t st_mode; /* File mode. */
__nlink_t st_nlink; /* Link count. */
__uid_t st_uid; /* User ID of the file's owner. */
__gid_t st_gid; /* Group ID of the file's group.*/
__dev_t st_rdev; /* Device number, if device. */
unsigned int __pad2;
__off64_t st_size; /* Size of file, in bytes. */
__blksize_t st_blksize; /* Optimal block size for I/O. */
__blkcnt64_t st_blocks; /* Number 512-byte blocks allocated. */
# if defined __USE_MISC || defined __USE_XOPEN2K8
/* Nanosecond resolution timestamps are stored in a format
equivalent to 'struct timespec'. This is the type used
whenever possible but the Unix namespace rules do not allow the
identifier 'timespec' to appear in the <sys/stat.h> header.
Therefore we have to handle the use of this header in strictly
standard-compliant sources special. */
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. */
# else
__time_t st_atime; /* Time of last access. */
unsigned long int st_atimensec; /* Nscecs of last access. */
__time_t st_mtime; /* Time of last modification. */
unsigned long int st_mtimensec; /* Nsecs of last modification. */
__time_t st_ctime; /* Time of last status change. */
unsigned long int st_ctimensec; /* Nsecs of last status change. */
# endif
__ino64_t st_ino; /* File serial number. */
};
#endif
实例 x.4.2.1.c
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char pathname[] = "/tmp/myfile"; /*文件路径*/
struct stat buf; /*属性信息*/
if (stat(pathname, &buf) == -1) { /*调用stat将文件属性信息放入buf*/
printf("stat error for %s\n",pathname);
exit(1);
} else {
printf("stat OK for %s\n",pathname);
exit(0);
}
exit(0);
}
编译与执行:
[root@localhost unixc]# echo 'This is my tempfile!' > /tmp/myfile
[root@localhost unixc]# cat /tmp/myfile
This is my tempfile!
[root@localhost unixc]# cc x.4.2.1.c
[root@localhost unixc]# ./a.out
stat OK for /tmp/myfile
[root@localhost unixc]#