#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
if(argc != 2)
return 1;
struct stat buf;
memset(&buf, 0, sizeof(struct stat));
lstat(argv[1], &buf);
//lstat可以检测符号链接,使用stat无法观察到符号链接
if(1 == S_ISREG(buf.st_mode))
printf("%s is regular file\r\n", argv[1]);
if(1 == S_ISDIR(buf.st_mode))
printf("%s is dir\r\n", argv[1]);
if(1 == S_ISBLK(buf.st_mode))
printf("%s is block special file\r\n", argv[1]);
if(1 == S_ISCHR(buf.st_mode))
printf("%s is character special file\r\n", argv[1]);
if(1 == S_ISFIFO(buf.st_mode))
printf("%s is fifo file\r\n", argv[1]);
if(1 == S_ISLNK(buf.st_mode))
printf("%s is link file\r\n", argv[1]);
if(1 == S_ISSOCK(buf.st_mode))
printf("%s is socket\r\n", argv[1]);
return 0;
}
linux判断文件/目录类型
最新推荐文章于 2025-02-03 08:00:00 发布