C语言 stat函数实现ls打印文件信息功能

stat函数:取得指定文件的文件属性。

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

在使用时需要以上三个头文件。

原型:int stat(const char *pathname, struct stat *statbuf);

在linux系统中可以看到stat结构体如下:

struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               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;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

               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_dev该字段描述文件所在的设备。

st_ino该字段包含文件的inode号。

st_uid该字段包含文件所有者的用户ID。

st_gid该字段包含文件所属组的ID。

ST_rdev该字段描述该文件(inode)所代表的设备

ls命令是linux下最常用的命令,ls命令就是list的缩写,

st_size该字段以字节为单位给出文件(如果是常规文件或符号链接)的大小。

st_blksize符号链接的大小是它包含的路径名的长度,不包含结束的空字节。

st_blocks该字段给出高效文件系统I/0的“首选”块大小。该字段表示分配给文件的块数,以512字节为单位。(当文件有孔时,这个值可能小于st size/512。)

st_atime这是文件的最后一次访问时间戳。

st_mtime这是文件的最后一次修改时间戳。

st_ctime这是文件最后一次状态更改的 时间戳。

文件的用户名:

 struct passwd *getpwuid(uid_t uid);

文件的权限:

 S_IRUSR     00400  用户的读权限     8
 S_IWUSR     00200  用户的写权限     7
 S_IXUSR     00100  用户的执行权限   6
 S_IRGRP     00040  组的读权限       5
 S_IWGRP     00020  组的写权限       4
 S_IXGRP     00010  组的执行权限     3
 S_IROTH     00004  其他读权限       2
 S_IWOTH     00002  其他写权限       1
 S_IXOTH     00001  其他执行权限     0

文件的组名:

 struct group *getgrgid(gid_t gid);

文件的最后修改时间

struct stat{

	 struct timespec st_mtim
  
}

ls -l <文件名>而且可以查看文件权限,文件大小,时间信息等

ubuntu:~/IO/$ ll app
-rwxrwxr-x 1 hqyj hqyj 17032 11月  4 17:16 app*

代码实现:

    int main(int argc, char *argv[])
    {
       if(argc < 2)
          {
          printf("please enter %s file\n", argv[0]);
          return -1;
          }                                //判断命令行的参数个数 

     struct stat statbuf;                 //创建一个结构体变量statbuf
     if (-1 == lstat(argv[1],&statbuf))
        {
        perror("stat");
        return -1;
        }                                  //判断命令行参数的合法性
 
     switch(statbuf.st_mode & S_IFMT)      //判断文件的类型
         {
         case S_IFSOCK:
             printf("s");
             break;
         case S_IFLNK:
             printf("l");
             break;
         case S_IFREG:
             printf("-");
             break;
         case S_IFBLK:
             printf("b");
             break;
         case S_IFDIR:
             printf("d");
             break;
         case S_IFCHR:
             printf("c");
             break;
         case S_IFIFO:
             printf("p");

    int n = 8;            //定义一个整型变量n  文件权限最高位 三个二进制数 111 111 111
    while(n >= 0)         //判断文件的权限
     {
         if(statbuf.st_mode &  1<< n)
         {  
             switch(n % 3)
            {
                 case 2:
                     printf("r");
                     break;
                 case 1:
                     printf("w");
                     break;
                 case 0:
                     printf("x");
                    break;
            }
         }        
         else
         {
             printf("-");
         }
 
         n--;
         n--;
    }
 
    printf("%ld", statbuf.st_nlink);

    struct passwd *p_uid;
    p_uid = getpwuid(statbuf.st_uid);
    printf("%s", p_uid->pw_name);            //文件所在的组名
      
    printf("%ld", statbuf.st_size);          //文件大小 
 

    printf("%d\n", statbuf.st_mtim.tv_sec);   //时间
 
    struct tm *tp = localtime(&statbuf.st_mtim.tv_sec);
    struct tm *tp = localtime(&statbuf.st_mtime);

    printf("%d年%d月%d日%d时:%d分:%d秒",
                            tp->tm_year + 1900,
                            tp->tm_mon + 1,
                            tp->tm_mday,
                            tp->tm_hour,
                            tp->tm_min,
                            tp->tm_sec
                            );
 
     printf("%s\n", argv[1]);
     return 0;
}


文件运行结果:

ubuntu:./a.out date.txt 

-rw-rw-r-- 1 hqyj 69 2022年11月4日 19时:53分:22秒 date.txt

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值