文件时间戳
st_time、st_mtime和st_ctime字段,分别记录了对文件的上次访问时间、上次修改时间以及文件状态发送改变的上次时间。这3个字段的类型都是time_t,记录了自新纪元(Epoch)以来的秒数。
示例程序
(获取并解释文件的stat信息)
#define _BSD_SOURCE
#include
#include
#include
#include
#include
#include
#include
#define FP_SPECIAL 1
#define STR_SIZE sizeof("rwxrwxrwx")
typedef enum { FALSE, TRUE } Boolean;
static char *filePermStr(mode_t perm, int flags)
{
static char str[STR_SIZE];
snprintf(str, STR_SIZE, "%c%c%c%c%c%c%c%c%c",
(perm & S_IRUSR) ? 'r' : '-', (perm & S_IWUSR) ? 'w' : '-',
(perm & S_IXUSR) ?
(((perm & S_ISUID) && (flags & FP_SPECIAL)) ? 's' : 'x') :
(((perm & S_ISUID) && (flags & FP_SPECIAL)) ? 'S' : '-'),
(perm & S_IRGRP) ? 'r' : '-', (perm & S_IWGRP) ? 'w' : '-',
(perm & S_IXGRP) ?
(((perm & S_ISGID) && (flags & FP_SPECIAL)) ? 's' : 'x') :
(((perm & S_ISGID) && (flags & FP_SPECIAL)) ? 'S' : '-'),
(perm & S_IROTH) ? 'r' : '-', (perm & S_IWOTH) ? 'w' : '-',
(perm & S_IXOTH) ?
(((perm & S_ISVTX) && (flags & FP_SPECIAL)) ? 't' : 'x') :
(((perm & S_ISVTX) && (flags & FP_SPECIAL)) ? 'T' : '-'));
return str;
}
static void showStatInfo(struct stat *st)
{
printf("File type: ");
switch(st->st_mode & S_IFMT)
{
case S_IFREG: printf("regular file\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFBLK: printf("block device\n"); break;
case S_IFLNK: printf("symbolic (soft) link\n"); break;
case S_IFIFO: printf("FIFO or pipe\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown file type?\n"); break;
}
printf("Device containing i-node: major=%ld minor=%ld\n",
(long) major(st->st_dev), (long) minor(st->st_dev));
printf("I-node number: %ld\n", (long) st->st_ino);
printf("Mode: %lo (%s)\n", (unsigned long) st->st_mode,
filePermStr(st->st_mode, 0));
if (st->st_mode & (S_ISUID| S_ISGID| S_ISVTX))
printf(" special bits set: %s%s%s\n",
(st->st_mode & S_ISUID) ? "set-UID " : "",
(st->st_mode & S_ISGID) ? "set-GID " : "",
(st->st_mode & S_ISVTX) ? "sticky " : "");
printf("Number of (hard) links: %ld\n", (long) st->st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long) st->st_uid, (long) st->st_gid);
if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
printf("Device number (st_rdev): major=%ld; minor=%ld\n",
(long) major(st->st_rdev), (long) minor(st->st_rdev));
printf("File size: %lld bytes\n", (long long) st->st_size);
printf("Optimal I/O block size: %ld bytes\n", (long) st->st_blksize);
printf("512B blocks allocated: %lld\n", (long long) st->st_blocks);
printf("Last file access: %s\n", ctime(&st->st_atime));
printf("Last file modification: %s\n", ctime(&st->st_mtime));
printf("Last status change: %s\n", ctime(&st->st_ctime));
}
int main(int argc, char *argv[])
{
struct stat st;
Boolean statLink;
int fname;
statLink = (argc > 1) && strcmp(argv[1], "-l") == 0;
fname = statLink ? 2: 1;
if (fname >= argc || (argc > 1 && strcmp(argv[1], "--help") == 0))
printf("%s [-l] file\n -l = use lstat() instead of stat()\n", argv[0]);
if (statLink)
{
if (lstat(argv[fname], &st) == -1)
{
printf("lstat() operate failed!\n");
}
}
else
{
if (stat(argv[fname], &st) == -1)
printf("stat() operate failed!\n");
}
showStatInfo(&st);
exit(1);
}
以下是运行结果: