I basically have to write a clone of the UNIX ls command for a class, and I've got almost everything working. One thing I can't seem to figure out how to do is check whether a file is a symbolic link or not. From the man page for stat()
, I see that there is a mode_t
value defined, S_IFLNK
.
我基本上必须为一个类编写一个UNIX ls命令的克隆,而且几乎所有东西都在工作。我似乎无法弄清楚如何做的一件事是检查文件是否是符号链接。从stat()的手册页中,我看到定义了一个mode_t值,S_IFLNK。
This is how I'm trying to check whether a file is a sym-link, with no luck (note, stbuf is the buffer that stat()
returned the inode data into):
这就是我试图检查一个文件是否是一个sym-link,没有运气(注意,stbuf是stat()返回inode数据的缓冲区):
switch(stbuf.st_mode & S_IFMT){
case S_IFLNK:
printf("this is a link\n");
break;
case S_IFREG:
printf("this is not a link\n");
break;
}
My code ALWAYS prints this is not a link
even if it is, and I know for a fact that the said file is a symbolic link since the actual ls command says so, plus I created the sym-link...
我的代码ALWAYS打印这不是一个链接,即使它是,并且我知道一个事实,所述文件是一个符号链接,因为实际的ls命令这样说,再加上我创建了sym-link ...
Can anyone spot what I may be doing wrong? Thanks for the help!
谁能发现我可能做错了什么?谢谢您的帮助!
1 个解决方案
#1
23
You can't.
你不能。
You need to use lstat()
to stat the link itself, plain stat()
will follow the link, and thus never "see" the link itself.
你需要使用lstat()来统计链接本身,plain stat()将跟随链接,因此永远不会“看到”链接本身。
#1
23
You can't.
你不能。
You need to use lstat()
to stat the link itself, plain stat()
will follow the link, and thus never "see" the link itself.
你需要使用lstat()来统计链接本身,plain stat()将跟随链接,因此永远不会“看到”链接本身。