你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux系统编程之获取文件信息

[日期:2014-06-11] 来源:Linux社区  作者:davidsky11 [字体:大 中 小]

文件时间戳

st_time、st_mtime和st_ctime字段,分别记录了对文件的上次访问时间、上次修改时间以及文件状态发送改变的上次时间。这3个字段的类型都是time_t,记录了自新纪元(Epoch)以来的秒数。

 

示例程序

(获取并解释文件的stat信息)

#define _BSD_SOURCE
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>

#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);
}

以下是运行结果:

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103005.htm

linux
相关资讯       Linux系统编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款