Linux stat、fstat和lstat函数

stat


头文件

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


函数原型

linux

int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

unix

int stat(const char *restrict pathname, struct stat *restrict buf);

int fstat(int filedes, struct stat *buf);

int lstat(const char *restrict pathname, struct stat *restrict buf);


功能

      一旦给出path,stat函数就返回与命名文件有关的信息结构,如果是符号链接返回的是符号链接所指的文件的相关信息。fstat函数使用时fd必须是打开状态,返回文件的相关信息。lstat和stat相类似,不同之处就是当参数是文件链接时,lstat返回的是文件连接本身的信息。
      使用最多stat函数的命令应该就是"ls -l",用其可以得到有关文件的所有信息。


参数

buf是指针,指向一个我们自己提供的结构,此结构必须提供。结构的基本形式是:

linux

struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

unix高级环境编程中的例子

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	int i;
	struct stat buf;
	char *ptr;

	for(i = 1; i < argc; i++)
	{
		printf("%s: ", argv[i]);
		if(lstat(argv[i], &buf) < 0)
		{
			printf("lstat error\n");
			continue;
		}
		if(S_ISREG(buf.st_mode))
		{
			ptr = "regular";
		}
		else if(S_ISDIR(buf.st_mode))
		{
			ptr = "directory";
		}
		else if(S_ISCHR(buf.st_mode))
		{
			ptr = "character special";
		}
		else if(S_ISBLK(buf.st_mode))
		{
			ptr = "block special";
		}
		else if(S_ISFIFO(buf.st_mode))
		{
			ptr = "fifo";
		}
		else if(S_ISLNK(buf.st_mode))
		{
			ptr = "symbolic link";
		}
		else if(S_ISSOCK(buf.st_mode))
		{
			ptr = "socket";
		}
		else
		{
			ptr = "** unknown mode **";
		}

		printf("%s\n", ptr);
	}
	exit(0);
}

扩展

unix/linux中的七种文件类型

1. (-) 普通文件(regular file)      最常用的文件类型。

2. (d)目录文件(directory file)     此文件包含了其他文件的名字以及指向这些文件有关信息的指针。

3. (s)套接字(socket)                  此文件用于进程间的网路通信。也可用于同一主机上进程间的非网络同信。

4. (l)符号链接(symbolic link)      此种文件类型指向另一个文件。

5. (b)块特殊文件(block special file)  此文件类型提供对设备(如磁盘)带缓冲的访问,每次访问以固定长度为单位进行。

6. (c)字符设备文件(character special file) 此文件类型提供对设备不带缓冲的访问,每次的访问长度可变。

7. (p)FIFO/命名管道文件           此文件类型用于进程间通信。

ps: 系统中的所有设备要么是字符设备文件(有的叫字符特殊文件),要么是块文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值