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返回该符号链接的有关信息,而不是由该符号链接引用的文件的信息。
(2)目录文件 #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;
}
对每个命令行参数打印文件类型 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |