Skip to content
果冻虾仁 edited this page Aug 22, 2017 · 4 revisions

函数原型

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

int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

三个函数的关系与chown,fchown,lchown的关系相同。

  • stat检查符号链接时,实际检查的是符号链接所引用的文件
  • lstat检查符号链接时,检查的是符号链接本身
  • fstat功能与stat相同,不过它的参数是文件的描述符

返回值

成功返回0;失败返回**-1**,并设置相应errno的值。

结构体stat的内容

struct stat {
    dev_t     st_dev;     /* 设备号(主设备号,次设备号)*/
    ino_t     st_ino;     /* inode的数量 */
    mode_t    st_mode;    /* 文件的类型和存取的权限 */
    nlink_t   st_nlink;   /* 硬链接的数量 */
    uid_t     st_uid;     /* 所用者的uid */
    gid_t     st_gid;     /* 所有者的组id */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* 总大小,字节数 */
    blksize_t st_blksize; /* blocksize for filesystem I/O */
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
    time_t    st_atime;   /* 最后访问时间(access)*/
    time_t    st_mtime;   /* 最后修改时间(modification)文件内容的改动时间 */
    time_t    st_ctime;   /* 最后改动时间(change)文件属性的改动时间 */
};

Linux环境编程API

C语言API包含部分标准C的API、POSIX标准的系统编程API(一些Linux独有的系统API会单独注明)。

头文件源码

大部分头文件源码在/usr/include目录下。

安装man手册

因为涉及到大量的POSIX编程。所以最好下载POSIX函数的man手册。

Ubuntu

apt-get install manpages-posix        
apt-get install manpages-posix-dev

默认安装了manpages-dev,所以不装POSIX的man手册是可以查看绝大部分API的。
但是不装的话,有些API是不能看到的,比如posix_spawn函数。

CentOS

yum install man-pages.noarch

关于目录

左侧的目录并非以函数为索引依据,主要是以系统的man手册页面为索引依据。
比如exec里面包含6个函数、pipe里面包含pipe()和pipe2()两个函数,但是它们都是属于一个man页面中。

Clone this wiki locally