首页 > 其他分享 >stat命令的实现-mystat

stat命令的实现-mystat

时间:2022-10-13 13:45:41浏览次数:26  
标签:case stat struct st break 命令 mystat printf

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)对比,提交截图

标签:case,stat,struct,st,break,命令,mystat,printf
From: https://www.cnblogs.com/yu15141310373/p/16787885.html

相关文章

  • Linux/Mac 编辑文件vim 涉及的命令(vim分页命令)
    整页翻页ctrl-fctrl-bctrl-f:下一页ctrl-b:上一页f就是forwordb就是backward翻半页ctrl-d:向下翻半页ctlr-u:向上翻半页d=downu=up滚一行ctrl-e:向下滚一行c......
  • 数据库维护常用命令
    链接数据库:mysql-hlocalhost-uusername-ppassword显示所有数据库:showdatabases;选定数据库:usedatabase_name;查看所有表:showtables;查看表中内容:se......
  • GO命令基础
    gorun命令简介用于运行命令源码文件只能接受一个命令源码文件以及若干个库源码文件作为文件参数其内部操作步骤:先编译源码文件再运行gorun常用标记的使用-a:强制编译相......
  • windows系统,自动设置--shutdown命令了解
     参考:https://baike.baidu.com/item/shutdown/10469108?fr=aladdin 比如你的电脑要在24:00关机,可以选择“开始运行”,输入“at24:00Shutdown-s”,这样,到了24点,电脑就......
  • Linux 防火墙相关命令
    一、防火墙的开启、关闭、禁用命令(1)设置开机启用防火墙:systemctlenablefirewalld.service(2)设置开机禁用防火墙:systemctldisablefirewalld.service(3)启动防火墙:system......
  • 常用adb命令
    准备工作:1、手机用数据线连上电脑(注意有的数据线只能充电,连上电脑没反应),并打开开发者模式。2、打开cmd3、输入:adbdevices如果连接成功,下面会检测出设备列表。 常用......
  • java学习心得——Linux服务器操作命令整理
     基础操作:cd/  返回根目录mkdir wwwroot创建文件夹cdwwwroot跳转到文件夹bashstart.sh运行脚本命令cd..返回上级目录ls查看当前目录文件ll查看当......
  • 记录下linux命令关于chown
    题目:关于对其他用户(自己创建的user1)下的目录test及其下的所有文件的所有者改成bin,所属组改成daemon。一开始切换到user1下却操作不了修改文件为bin用户,chown-R也没用......
  • typeorm-model-generator 数据库映射Model 命令 - nest
    typeorm-model-generator数据库映射Model命令NestJs中的控制器、路由、Get、Post方法参数装饰器Nest.js从零到壹系列(二):使用Sequelize操作数据库这里说不要用typeorm......
  • 关于Gitlab-配置SSH Keys-及使用Git命令行进行登录克隆
    如果是需要使用Git对自己Gitlab上的代码进行clone,过程如下:1、首先需要在Gitlab上配置sshkeys  配置方法:设置--> SSHKeys-->添加公钥信息,输入一个Title-->Addkey......