stat函数获取分析文件属性

好记性不如烂笔头
linux 文件属性,终端下ls和stat命令查看,在c程序中怎么处理呢

lala0903@lala0903-virtual-machine:/var/cache/apt/archives/stlink$ ls -l
total 128
drwxr-xr-x 5 root root  4096 1217 21:58 build
-rw-r--r-- 1 root root 40768 1217 00:45 CHANGELOG.md
drwxr-xr-x 4 root root  4096 1217 00:45 cmake
-rw-r--r-- 1 root root 10247 1217 00:45 CMakeLists.txt
-rw-r--r-- 1 root root   912 1217 00:45 cmake_uninstall.cmake.in
lala0903@lala0903-virtual-machine:/var/cache/apt/archives/stlink$ stat build/
  File: 'build/'
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: 801h/2049d	Inode: 1969552     Links: 5
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-17 21:59:17.981686537 +0800
Modify: 2020-12-17 21:58:52.949478757 +0800
Change: 2020-12-17 21:58:52.949478757 +0800
 Birth: -

man 2 stat查看函数的介绍
struct stat 结构体:

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 filesystem I/O */
      blkcnt_t  st_blocks;      /* number of 512B blocks allocated */

      /* Since Linux 2.6, the kernel supports nanosecond
         precision for the following timestamp fields.
         For the details before Linux 2.6, see NOTES. */

      struct timespec st_atim;  /* time of last access */
      struct timespec st_mtim;  /* time of last modification */
      struct timespec st_ctim;  /* time of last status change */

  #define st_atime st_atim.tv_sec      /* Backward compatibility */
  #define st_mtime st_mtim.tv_sec
  #define st_ctime st_ctim.tv_sec
  };

其中用的多的是

st_mode:文件的类型和权限,共16位,如下图。

0-11位控制文件的权限

12-15位控制文件的类型

0-2比特位:其他用户权限 4 读 2 写 1 执行

3-5比特位:组用户权限 4 读 2 写 1 执行

6-8比特位:本用户权限 4 读 2 写 1 执行

9-11比特位:特殊权限 U G T

12-15比特位:文件类型(因为文件类型只有7中,所以用12-14位就够了

 	S_IFMT     0170000   bit mask for the file type bit field
    S_IFSOCK   0140000   socket
    S_IFLNK    0120000   symbolic link
    S_IFREG    0100000   regular file
    S_IFBLK    0060000   block device
    S_IFDIR    0040000   directory
    S_IFCHR    0020000   character device
    S_IFIFO    0010000   FIFO
//判断文件类型的函数,返回true,false       
   S_ISREG(stat.st_mode)  is it a regular file?
   S_ISDIR(stat.st_mode)  directory?
   S_ISCHR(stat.st_mode)  character device?
   S_ISBLK(stat.st_mode)  block device?
   S_ISFIFO(stat.st_mode) FIFO (named pipe)?
   S_ISLNK(stat.st_mode)  symbolic link?  (Not in POSIX.1-1996.)
   S_ISSOCK(stat.st_mode) socket?  (Not in POSIX.1-1996.)

函数

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

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

例:
判断一个函数是否为字符设备

int main(int argc, char* argv[])
{
	char path[20] = "/dev/sda1";
	struct stat st;
	
	int fd = open(path);
	if (fd < 0) {
		return -1;
	}
	int ret = fstat(fd, &st);
	if (ret < 0) {
		close(fd);
		return -1;
	}
	if (!S_ISCHR(st.st_mode)) {
		printf("file is not a character device\n");
	} else {
		printf("file is a character device\n");
	}
	close(fd);
	
	return 0;
}

bit 0 到 11 文件的权限,owner group other 的可读可写可执行权限

       S_ISUID     04000   set-user-ID bit
       S_ISGID     02000   set-group-ID bit (see below)
       S_ISVTX     01000   sticky bit (see below)

       S_IRWXU     00700   owner has read, write, and execute permission
       S_IRUSR     00400   owner has read permission
       S_IWUSR     00200   owner has write permission
       S_IXUSR     00100   owner has execute permission

       S_IRWXG     00070   group has read, write, and execute permission
       S_IRGRP     00040   group has read permission
       S_IWGRP     00020   group has write permission
       S_IXGRP     00010   group has execute permission

       S_IRWXO     00007   others (not in group) have read, write, and  exe‐
                           cute permission
       S_IROTH     00004   others have read permission
       S_IWOTH     00002   others have write permission
       S_IXOTH     00001   others have execute permission
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(int argc, char* argv[]){

	 struct stat st;
	 lstat(argv[1], &st);
	
	 char str[11] = {0};
	 memset(str, '-', (sizeof str - 1));
	   
	 //文件类型
	 if(S_ISREG(st.st_mode))  str[0] = '-';
	 if(S_ISDIR(st.st_mode))  str[0] = 'd';
	 if(S_ISCHR(st.st_mode))  str[0] = 'c';
	 if(S_ISBLK(st.st_mode))  str[0] = 'b';
	 if(S_ISFIFO(st.st_mode)) str[0] = 'p';
	 if(S_ISLNK(st.st_mode))  str[0] = 'l';
	 if(S_ISSOCK(st.st_mode)) str[0] = 's';
	
	 //本用户的文件权限
	 if(st.st_mode & S_IRUSR) str[1] = 'r';
	 if(st.st_mode & S_IWUSR) str[2] = 'w';
	 if(st.st_mode & S_IXUSR) str[3] = 'x';
	 
	 //本用户的组的文件权限
	 if(st.st_mode & S_IRGRP) str[4] = 'r';
	 if(st.st_mode & S_IWGRP) str[5] = 'w';
	 if(st.st_mode & S_IXGRP) str[6] = 'x';
	 
	 //其他用户的文件权限
	 if(st.st_mode & S_IROTH) str[7] = 'r';
	 if(st.st_mode & S_IWOTH) str[8] = 'w';
	 if(st.st_mode & S_IXOTH) str[9] = 'x';

	 printf("file permission is %s", str);

	return 0;
}

参考 man 2 stat 及博客添加链接描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值