lstat函数用于获取符号链接的属性信息(例如文件类型、访问权限和访问时间等)。函数原型如下:
#include <sys/sta.h>
int lstat(const char *restrict pathname, struct stat *restrict buf);
其中 struct stat结构定义在文件 <sys/stat.h>中。(参考 4.2 stat函数)
返回值:成功返回0,出错返回-1。
参数:
1、pathname 文件路径
2、buf存储struct stat结构信息的缓冲区。
实例 x.4.2.3.c
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char pathname[] = "/tmp/mylink"; /*文件路径*/
struct stat buf; /*属性信息*/
if (lstat(pathname, &buf) == -1) { /*调用lstat将链接的属性信息放入buf*/
printf("lstat error for %s\n",pathname);
exit(1);
} else {
printf("lstat OK for %s\n",pathname);
exit(0);
}
exit(0);
}
编译与执行:
[root@localhost unixc]# echo 'This is my tempfile!' > /tmp/myfile
[root@localhost unixc]# cat /tmp/myfile
This is my tempfile!
[root@localhost unixc]# cc x.4.2.1.c
[root@localhost unixc]# ./a.out
stat OK for /tmp/myfile
[root@localhost unixc]#