Linux--用stat函数实现ls -l功能

stat函数

函数常用的定义:

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

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

第一个参数是文件名,第二个参数是stat的结构体,用来存放stat取出来的元数据

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 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 */
       };
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<string.h>
#include<pwd.h>
#include<time.h>
#include<grp.h>
void file_type(mode_t mode) {
 char buf[11] = "?---------";
 switch (mode & S_IFMT) {
  case S_IFSOCK: buf[0] = 's'; break;
  case S_IFLNK:buf[0] = 'l'; break;
  case S_IFREG:buf[0] = '-'; break;
  case S_IFBLK:buf[0] = 'b'; break;
  case S_IFDIR:buf[0] = 'd'; break;
  case S_IFCHR:buf[0] = 'c'; break;
  case S_IFIFO:buf[0] = 'p'; break;
}
 if (mode & S_IRUSR) buf[1] = 'r';
 if (mode & S_IWUSR) buf[2] = 'w';
 if (mode & S_IXUSR) buf[3] = 'x';
 if (mode & S_IRGRP) buf[4] = 'r';
 if (mode & S_IWGRP) buf[5] = 'w';
 if (mode & S_IXGRP) buf[6] = 'x';
 if (mode & S_IROTH) buf[7] = 'r';
 if (mode & S_IWOTH) buf[8] = 'w';
 if (mode & S_IXOTH) buf[9] = 'x';
 printf("%s. ", buf);
}
int main(int argc, char* argv[]) {
 if (argc == 1)
  printf("usage : %s file\n", argv[0]), exit(0);
 struct stat buf;
 if (stat(argv[1], &buf) == -1)
  perror("stat"), exit(1);
 file_type(buf.st_mode);
 printf("%d ", buf.st_nlink);
 //getpwuid函数根据uid号获取文件拥有者的名字
 struct passwd* pw = getpwuid(buf.st_uid);
 printf("%s ", pw->pw_name);
 //getgrgid函数根据gid号获得属组的名字
 struct group* gp = getgrgid(buf.st_gid);
 printf("%s ", gp->gr_name);
 printf("%d ", buf.st_size);
 
 struct tm* ptm = localtime(&buf.st_mtime);
 printf("%04d-%02d-%02d %02d:%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
                      ptm->tm_hour, ptm->tm_min);
 printf("%s\n ", argv[1]);
 return 0;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现ls -l命令的关键是要读取当前目录下的文件和文件夹的信息,并将其格式化输出。下面是一个简单的C语言程序,实现了类似于ls -l功能: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> #include <pwd.h> #include <grp.h> #include <time.h> int main(int argc, char *argv[]) { DIR *dp; struct dirent *entry; struct stat statbuf; struct passwd *pwd; struct group *grp; char date[20]; if ((dp = opendir(".")) == NULL) { fprintf(stderr, "Cannot open directory.\n"); exit(1); } while ((entry = readdir(dp)) != NULL) { if (entry->d_name[0] == '.') continue; if (lstat(entry->d_name, &statbuf) < 0) continue; printf((S_ISDIR(statbuf.st_mode)) ? "d" : "-"); printf((statbuf.st_mode & S_IRUSR) ? "r" : "-"); printf((statbuf.st_mode & S_IWUSR) ? "w" : "-"); printf((statbuf.st_mode & S_IXUSR) ? "x" : "-"); printf((statbuf.st_mode & S_IRGRP) ? "r" : "-"); printf((statbuf.st_mode & S_IWGRP) ? "w" : "-"); printf((statbuf.st_mode & S_IXGRP) ? "x" : "-"); printf((statbuf.st_mode & S_IROTH) ? "r" : "-"); printf((statbuf.st_mode & S_IWOTH) ? "w" : "-"); printf((statbuf.st_mode & S_IXOTH) ? "x" : "-"); printf(" %2lu", statbuf.st_nlink); if ((pwd = getpwuid(statbuf.st_uid)) != NULL) { printf(" %s", pwd->pw_name); } else { printf(" %d", statbuf.st_uid); } if ((grp = getgrgid(statbuf.st_gid)) != NULL) { printf(" %s", grp->gr_name); } else { printf(" %d", statbuf.st_gid); } strftime(date, 20, "%b %d %H:%M", localtime(&statbuf.st_mtime)); printf(" %s", date); printf(" %s\n", entry->d_name); } closedir(dp); return 0; } ``` 这个程序使用了许多系统调用和库函数,比如opendir、readdir、lstat、getpwuid、getgrgid等等。它首先打开当前目录("."),然后循环读取目录下的每个文件和文件夹的信息,使用lstat函数获取文件的详细信息,再利用各种库函数将这些信息格式化输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值