开发者

How do you determine using stat() whether a file is a symbolic link?

开发者 https://www.devze.com 2022-12-26 14:50 出处:网络
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 lin

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.

This is how I'm trying to check whether a file is a sym-link, with no luck (note, stb开发者_运维问答uf 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;
}

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消