你如何确定使用统计()的文件是否是一个符号链接? [英] How do you determine using stat() whether a file is a symbolic link?

查看:126
本文介绍了你如何确定使用统计()的文件是否是一个符号链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上都写的UNIX ls命令的克隆一类,我已经得到了几乎所有的工作。有一件事我似乎无法弄清楚如何做的是检查文件是否是一个符号链接或没有。从 STAT(),我看到有定义的 mode_t 值, S_IFLNK

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.

这是如何我试图检查文件是否是一个符号链接,没有运气(注意,stbuf是缓冲STAT()返回inode的数据转换成):

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):

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;
}

我的code始终打印这不是一个链接就算是,我知道一个事实,即该文件是因为实际的符号链接ls命令是这么说的,再加上我创建符号链接...

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...

任何人能发现我可能做错了什么?感谢您的帮助!

Can anyone spot what I may be doing wrong? Thanks for the help!

推荐答案

您不能。

您需要使用 LSTAT() 来统计指标的链接本身,纯 STAT()将遵循链接,因此从来没有看的链接本身。

You need to use lstat() to stat the link itself, plain stat() will follow the link, and thus never "see" the link itself.

这篇关于你如何确定使用统计()的文件是否是一个符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆