stat命令的实现-mystat

stat命令的实现-mystat

任务列表:

  1. 提交学习stat(1)的截图
  2. man -k ,grep -r的使用
  3. 伪代码
  4. 产品代码 mystate.c,提交码云链接
  5. 测试代码,mystat 与stat(1)对比,提交截图

1. 提交学习stat(1)的截图

2. man -k ,grep -r的使用


3.伪代码

1.运行程序,用户输入文件名,程序读取文件名
2.调用函数stat(),给结构体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 */

               /* 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
           };

3.读取结构体中的值,并按格式打印

4. 产品代码 mystat.c,提交链接

 #include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<time.h>
#include<sys/stat.h>
#define MAXLEN 256
struct stat *s;

int main(int argc,char **argv){
	s=(struct stat *)malloc(sizeof (struct stat));
	stat(argv[1],s);
	printf("file:%s\n",argv[1]);
	printf("size:%-10ld	block:%-10ld IOblock:%-7ld ",s->st_size,s->st_blocks,s->st_blksize);
	switch (s->st_mode & S_IFMT) 
	{ 
		case S_IFBLK: printf("Block devices\n"); 
			      break; 
		case S_IFCHR: printf("character device\n");
			      break; 
		case S_IFDIR: printf("directory\n");
			      break;
	       	case S_IFIFO: printf("FIFO/pipeline\n");
			      break;
	       	case S_IFLNK: printf("Symbolic links\n");
			      break;
	       	case S_IFREG: printf("Normal file\n");
			      break;
	       	case S_IFSOCK: printf("socket\n");
			       break;
	       	default: printf("Unknown?\n"); break;
       	}
	printf("equipment:%-10ld	Inode:%-10ld Hard links:%ld\n",s->st_dev,s->st_ino,s->st_nlink);
	printf("Permissions:		   Uid:%d Gid:%d\n",s->st_uid,s->st_gid);
	printf("Recently visited:%s",ctime(&s->st_ctime));
	printf("Recent changes:%s",ctime(&s->st_atime));
	printf("Recently modified:%s",ctime(&s->st_mtime));
	printf("The creation time:-\n");
	return 0;
}

5.测试代码,mystat 与stat(1)对比,提交截图

posted @ 2022-10-13 13:30  20201307梁辰鱼  阅读(21)  评论(0编辑  收藏  举报