#include <sys/stat.h>
int stat( const char *restrict pathname, struct stat *restrict buf );
int fstat( int filedes, struct stat *buf );
int lstat( const char *restrict pathname, struct stat *restrict buf );
三个函数的返回值:若成功则返回0,若出错则返回-1

关于上面函数声明中的restrict关键字的解释,可参考:http://blog.csdn.net/lovekatherine/article/details/1891806(具体如下)

今天读APUE,看到某个函数原型的声明:

int stat (  const char * restrict  pathname,struct stat * restrict buf);

这里的restrict让我觉得有些疑惑,一查原来是C99中增加的关键字那么restrict的意义是什么呢?

One of the new features in the recently approved C standard C99, is the restrict pointer qualifier. This qualifier can be applied to a data pointer to indicate that, during the scope of that pointer declaration, all data accessed through it will be accessed only through that pointer but not through any other pointer. The 'restrict' keyword thus enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed through another pointer. Now you're probably asking yourself, "doesn't const already guarantee that?" No, it doesn't. The qualifier const ensures that a variable cannot be changed through a particular pointer. However, it's still possible to change the variable through a different pointer.

概括的说,关键字restrict只用于限定指针;该关键字用于告知编译器,所有修改该指针所指向内容的操作全部都是基于(base on)该指针的,即不存在其它进行修改操作的途径;这样的后果是帮助编译器进行更好的代码优化,生成更有效率的汇编代码。

举个简单的例子

很显然函数foo()的返回值是0,除非参数x和y的值相同。可以想象,99%的情况下该函数都会返回0而不是1。然而编译起必须保证生成100%正确的代码,因此,编译器不能将原有代码替换成下面的更优版本

啊哈,现在我们有了restrict这个关键字,就可以利用它来帮助编译器安全的进行代码优化了

此时,由于指针 x 是修改 *x的唯一途径,编译起可以确认 “*y=1; ”这行代码不会修改 *x的内容,因此可以安全的优化为

最后注意一点,restrict是C99中定义的关键字,C++目前并未引入;在GCC可通过使用参数" -std=c99"来开启对C99的支持。

一旦给出pathname,stat函数就返回与此命名文件有关的信息结构。fstat函数获取已在描述符filedes上打开的有关信息。lstat函数类似于stat,但是当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引用文件的信息。

第二个参数buf是指针,它指向一个我们必须提供的结构。这些函数填写由buf指向的结构。该结构的实际定义可能随实现有所不同,但其基本形式是:

struct stat
{mode_t     st_mode;    /* file type & mode (permissions) */ino_t      st_ino;     /* i-node number (serial number) */dev_t      st_dev;     /* device number (file system) */dev_t      st_rdev;    /* device number for special files */nlink_t    st_nlink;   /* number of links */uid_t      st_uid;     /* user ID of owner */gid_t      st_gid;     /* group ID of owner */off_t      st_size;    /* size in bytes, for regular files */time_t     st_atime;   /* time of last access */time_t     st_mtime;   /* time of last modification */time_t     st_ctime;   /* time of last file status change */blksize_t  st_blksize; /* best I/O block size */blkcnt_t   st_blocks;  /* number of disk blocks allocated */
};

POSIX.1未要求st_rdev、st_blksize和st_blocks字段,Single UNIX Specification XSI扩展则定义了这些字段。

注意,该结构中的每一个成员都是基本系统数据类型。

使用stat函数最多的可能是ls -l命令,用其可以获得有关一个文件的所有信息。

文件和目录之stat、fstat和lstat函数相关推荐

  1. UNIX--stat、fstat和lstat函数

    2019独角兽企业重金招聘Python工程师标准>>> stat.fstat和lstat函数(UNIX) #include #include int stat(const char ...

  2. c语言中stat函数,C语言:stat,fstat和lstat函数

    这三个函数的功能是一致的,都用于获取文件相关信息,但应用于不同的文件对象.对于函数中给出pathname参数,stat函数返回与此命名文件有关的信息结构,fstat函数获取已在描述符fields上打开 ...

  3. [单刷APUE系列]第四章——文件和目录[1]

    原文来自静雅斋,转载请注明出处. stat函数族 int stat(const char *restrict path, struct stat *restrict buf); int fstat(i ...

  4. unix环境高级编程-文件和目录(1)

    stat.fstat和lstat函数: 一旦给出pathname,stat函数返回与此函数命令文件有关的信息结构.第二个参数buf是指针,指向一个我们必须提供的结构体.该结构体的基本结构是: 文件类型 ...

  5. Unix环境高级编程(二)文件和目录

    本章主要介绍的是文件结构及目录.重点是通过stat函数获取文件的结构信息,然后是文件目录及其遍历.学完本章后,编写了一个输出给的目录下的文件信息的程序. 首先是包含在<sys/stat.h> ...

  6. fstat/stat/lstat

    <script type=text/javascript> </script> <script src="http://pagead2.googlesyndic ...

  7. APUE(第四章)文件和目录

    重点理解文件系统 函数stat.fstat.fstatat和lstat #include <sys/stat.h> int stat(const char *restrict pathna ...

  8. apue 第4章 文件和目录

    获取文件属性 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>int stat(co ...

  9. UNIX 环境高级编程 文件和目录

    函数stat , fstat , fstatat , lstat stat函数返回与此文件有关的信息结构. fstat函数使用已打开的文件描述符(而stat则使用文件名) fstatat函数 为一个相 ...

最新文章

  1. java动态代理【一】
  2. POJ 1001 Exponentiation C++解题报告 JAVA解题报告
  3. “面试不败计划”:多线程
  4. VTK:Medical之GenerateCubesFromLabels
  5. 基于JAVA+Servlet+JSP+MYSQL的旅游酒店机票预订管理系统
  6. JavaScript 框架之战结束:React 是最终赢家?
  7. Lua云验证卡密破解工具
  8. 华为设备无线环境中的Portal认证
  9. 用命令从FTP服务器下载文件
  10. 论文阅读笔记(IRCNN):Learning Deep CNN Denoiser Prior for Image Restoration
  11. RobotStudio知识你知多少?
  12. 阿里云跨账号迁移云服务器实例
  13. 小龙女,杨过跳崖真相
  14. python打擂台法_一起来捉妖:这种打擂台方法不可用,玩家试了一下,被无情封禁...
  15. 紧急通知,1秒下达给员工 | 巴别鸟 V5.0上线
  16. Git fatal: Authentication failed
  17. 中南大学计算机学院闭委,2019-2020计算机学院第一学期学委会议顺利召开
  18. select句柄数限制 及总结
  19. android 打印小票格式,安卓端小票机设置
  20. execve系统调用_系统调用execve的入口sys_execve() | 学步园

热门文章

  1. mysql设计数据集市_数据集市设计
  2. 开发转测试好转吗_月薪15K+的高级测试开发工程师基础面试题,你要来试试吗?...
  3. 2021年春季学期-信号与系统-第二次作业参考答案-第四小题
  4. 旋转质量轮实验对象上的传感器
  5. linux c语言显示器api,API级别的Unicode GUI用于Windows / Linux / Mac的C语言本机应用程序...
  6. centos php memcache扩展,CentOS安装php5的memcache扩展
  7. PHP tcp短链接,http请求怎样实现TCP长连接、短连接
  8. java实时汇率的接口_eoLinker-API_Shop_汇率查询_API接口_Java调用示例代码
  9. android heic图片,如何在Windows / MacOS / Android上打開HEIC照片
  10. 电源稳定性测试软件,电源测试:电源设计的稳定性测量