免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3117 | 回复: 2
打印 上一主题 下一主题

S_ISLNK测试的问题(stat.h) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-20 13:26 |只看该作者 |倒序浏览
[root@localhost work]# ll slink prog
-rwxr-xr-x 1 root root 12752 4月 19 23:25 prog
lrwxrwxrwx 1 root root 4 4月 20 12:44 slink -> prog

源文件file.c
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
int filedes;
struct stat statbuf;
unsigned short int modes;

stat("slink",&statbuf);
modes=statbuf.st_mode;

printf("slink mode is %o \n",modes);
printf("is link %d\n",S_ISLNK(modes));

}

gcc file.c -o file
./file
运行结果:
slink mode is 100755
is link 0
显示的竟然是prog文件的信息.
而不是链接文件slink的信息..
请高手解答.

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2007-04-20 13:34 |只看该作者
原帖由 天外飞客 于 2007-4-20 13:26 发表
[root@localhost work]# ll slink prog
-rwxr-xr-x 1 root root 12752 4月 19 23:25 prog
lrwxrwxrwx 1 root root 4 4月 20 12:44 slink -> prog

stat("slink",&statbuf);

显示的竟然是prog文件的信息.
而不是链接文件slink的信息..


如果你要查看 symbolic link 本身的信息,请用 lstat(3)。

如下是 stat(3) 手册中的一段:
If  the  named file is a symbolic link, the stat() function shall continue pathname resolution using the contents of the symbolic link, and shall return information pertaining to the resulting file if the file exists.


发贴前请先查阅文档。

论坛徽章:
0
3 [报告]
发表于 2007-04-20 14:33 |只看该作者
谢谢噢~~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP