Linux C获取文件属性

From: http://www.tianya360.com/html/xitonganzhuang/caozuoxitong/2010/0524/924.html

Linux下如何在C下面判断一个文件是不是连接?
  

判断上面生成的软连接文件link,执行后,程序却说是目录,不知道是为什么?源代码如下:(文件名:mstat.c)
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
struct stat buf;
mode_t mode;
char type[80];
int fd;

if(argc != 2)
{
puts("USAGE: mstat {file}");
exit(EXIT_FAILURE);
}

if((fd = open(argv[1], O_RDONLY)) < 0)
{
perror("open");
exit(EXIT_FAILURE);
}

if((fstat(fd, &buf)) < 0)
{
perror("fstat");
exit(EXIT_FAILURE);
}

mode = buf.st_mode;
printf(" FILE: %s\n", argv[1]);
printf(" INODE: %ld\n", buf.st_ino);
printf(" DEVICE: %d,%d\n", major(buf.st_dev), minor(buf.st_dev));
printf(" SMODE: %#o\n", mode);
printf(" MODE: %#o\n", mode & ~(S_IFMT));
printf(" LINKS: %d\n", buf.st_nlink);
printf(" UID: %d\n", buf.st_uid);
printf(" GID: %d\n", buf.st_gid);

if(S_ISLNK(mode))
strcpy(type, "Symbolic line");
else if(S_ISREG(mode))
strcpy(type, "Regular file");
else if(S_ISDIR(mode))
strcpy(type, "Directory");
else if(S_ISCHR(mode))
strcpy(type, "Character device");
else if(S_ISBLK(mode))
strcpy(type, "Block device");
else if(S_ISFIFO(mode))
strcpy(type, "FIFO");
else if(S_ISSOCK(mode))
strcpy(type, "Socket");
else
strcpy(type, "Unknown type");

printf(" TYPE: %s\n", type);
printf(" SIZE: %d\n", buf.st_size);
printf("BLK SIZE: %ld\n", buf.st_blksize);
printf(" BLOCKS: %d\n", (int)buf.st_blocks);
printf("ACCESSED: %s", ctime(&buf.st_atime));
printf("MODIFIED: %s", ctime(&buf.st_mtime));
printf(" CHANGED: %s", ctime(&buf.st_ctime));

// close the file
if(close(fd) < 0)
{
perror("close");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

执行makd mstat生成执行文件mstat,然后执行./mstat link, 执行后提示link是一个目录,请大家试试,并帮我找找是什么原因。谢谢!

========================================================
如果在SCO上,使用lstat
man stat/lstat/fstat

if((fstat(fd, &buf)) < 0) --> if( (lstat(argv[0],&buf)) <0 )

stat系列调用的法则具体要看你的系统使用手册(man)Top

============================================================

[root@localhost other]# ./mstat link
FILE: link
INODE: 260649
DEVICE: 3,2
SMODE: 0100644
MODE: 0644
LINKS: 1
UID: 0
GID: 0
TYPE: Regular file
SIZE: 98
BLK SIZE: 4096
BLOCKS: 8
ACCESSED: Wed Jul 2 05:10:20 2003
MODIFIED: Wed Jul 2 05:09:49 2003
CHANGED: Wed Jul 2 05:09:49 2003
[root@localhost other]#

=============================================================

lstat(由文件描述词取得文件状态)
相关函数
stat,fstat,chmod,chown,readlink,utime
表头文件
#include<sys/stat.h>
#include<unistd.h>
定义函数
int lstat (const char * file_name.struct stat * buf);
函数说明
lstat()与stat()作用完全相同,都是取得参数file_name所指的文件状态,其差别在于,当文件为符号连接时,lstat()会返回该link本身的状态。详细内容请参考stat()。
返回值
执行成功则返回0,失败返回-1,错误代码存于errno。
范例
参考stat()。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值