我正在尝试使用函数S_ISLNK(file.st_mode)来检查某个文件是符号链接还是目录或常规文件。然而,当我用符号链接文件检查它时,它似乎不起作用。
这是我的代码:
if(S_ISDIR(fileStat.st_mode))
{
// DIR - display files in the directory
printf(" DIR ");
fileType = 2;
}else if(S_ISLNK(fileStat.st_mode)){
// LNK - display the name of the file the link is pointing to
printf(" LNK ");
fileType = 3;
}else{
// Display general info only
printf(" REG ");
fileType = 1;
}
对目录的检查工作正常,但当我在符号链接文件上运行程序时,它会显示为常规文件。有人知道我做错了什么吗?
stat
统计链接的目标。如果要判断文件是否为符号链接,请使用lstat
:
lstat()
与stat()
相同,只是如果路径是符号链接,则链接本身是静态的,而不是它所引用的文件。