【发布时间】:2016-10-28 00:44:25
【问题描述】:
基本上,这应该是一段简单的代码,用于打开目录流并查找符号链接。每当找到符号链接时,它应该打印 ("Symbolic link found");
但是,lstat(dirp->d_name,&bufcall 总是返回值 ln -s ciao.txt link1 和
ln -s ciao2.txt link2
我知道我应该稍后在我的代码中调用closedir(),请不要关心这个。
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
void main (int argc, char* argv[])
{
char buffer[100],dir[100];
struct stat buf;
int x;
DIR *dp;
struct dirent *dirp;
if((dp=opendir(argv[1]))==NULL)
{
printf("\nError opening directory stream, now exiting...\n");
exit(-1);
}
while((dirp=readdir(dp))!=NULL)
{
lstat(dirp->d_name,&buf);
if(S_ISLNK(buf.st_mode))
printf("\n%s Is a symbolic link\n",dirp->d_name);
else
printf("\n%s Is not a symbolic link\n",dirp->d_name);
}
}
我们将不胜感激。谢谢。
【问题讨论】:
-
我检查了 lstat 调用,它返回
标签: c file stream macros directory