加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Advanced Programming in UNIX Environment Episode 15

发布时间:2020-12-15 09:17:21 所属栏目:安全 来源:网络整理
导读:函数stat、fstat、fstatat和lstat include sys/stat.h int stat( const char * restrict pathname, struct stat * restrict buf); int fstat( int fd, struct stat *buf); int lstat( const char * restrict pathname, struct stat *buf); int fstatat( int

函数stat、fstat、fstatat和lstat

include <sys/stat.h>

int stat(const char *restrict pathname,struct stat *restrict buf);
int fstat(int fd,struct stat *buf);
int lstat(const char *restrict pathname,struct stat *buf);
int fstatat(int fd,const char *restrict pathname,struct stat *restrict buf,int flag);

一旦给出pathname,stat函数将返回与此命名文件有关的信息结构。fstat函数获得已在描述符fd上打开文件的有关信息。lstat函数类似于stat,但是当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引用的文件的信息。
fstatat函数为一个相对于当前打开目录(由fd参数指向)的路径名返回文件统计信息。
文件类型
(1)普通文件

二进制可执行文件是一个例外。为了执行程序,内核必须理解其格式。所有二进制可执行文件都遵循一种标准化的格式,这种格式是内核能够确定程序文本和数据的加载位置。

(2)目录文件
(3)快特殊文件
(4)字符特殊文件
(5)FIFO
(6)套接字
(7)符号链接

#include "apue.h"

int main(int argc,char *argv[])
{
    int i;
    struct stat buf;
    char *ptr;

    for(i=1;i<argc;i++)
    {
        printf("%s: ",argv[i]);
        if(lstat(argv[i],&buf)<0)
        {
            err_ret("lstat error");
            continue;
        }
        if(S_ISREG(buf.st_mode))
            ptr="regular";
        else if(S_ISDIR(buf.st_mode))
            ptr="directory";
        else if(S_ISCHR(buf.st_mode))
            ptr="character special";
        else if(S_ISBLK(buf.st_mode))
            ptr="block special";
        else if(S_ISFIFO(buf.st_mode))
            ptr="fifo"
        else if(S_ISLINK(buf.st_mode))
            ptr="symbolic link";
        else if(S_ISSOCK(buf.st_mode))
            ptr="socket";
        else
            ptr="** unknown mode **";
        printf("%sn",ptr);
    }
    return 0;
}

对每个命令行参数打印文件类型

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读