ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mystat

2021-11-21 16:33:34  阅读:163  来源: 互联网

标签:mystat timeValue 2d tm lccp printf include


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

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

 

 

 

 

 

 3. 伪代码

首先判断输入中是否包含文件参数,如果有则继续,没有则提示用户输入错误。 然后声明结构体,调用stat()函数 打印输出节点ino、 文件类型mode、 文件的硬链接数nlink、 用户ID uid和组ID gid、 块大小blksize、 字节数size、 块数目blocks、 三个时间atime、mtime和ctime按顺序输出。

4. 产品代码 mystate.c,提交码云链接
https://gitee.com/l20191305_li_tianqi/openssl/blob/master/mystat.c
#include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <pwd.h> #include <grp.h> #include <time.h>   long showtime(long timeval);   int main(int argc,char *argv[]){ if (argc<2) exit(1); struct stat buf; stat(argv[1],&buf); printf(" 文件: %s\n",argv[1]);   printf(" 大小: %-16d块: %-11dIO 块: %-7d",buf.st_size,buf.st_blocks,buf.st_blksize); if(S_ISREG(buf.st_mode)) {char style[30]="普通文件";printf("%s\n",style);} //判断文件类型 else if(S_ISDIR(buf .st_mode)) {char style[30]="目录文件";printf("%s\n",style);} else if(S_ISCHR(buf.st_mode)) {char style[30]="字符设备文件";printf("%s\n",style);} else if(S_ISBLK(buf.st_mode)) {char style[30]="块设备文件";printf("%s\n",style);} else if(S_ISLNK(buf.st_mode)) {char style[30]="链接文件";printf("%s\n",style);} else if(S_ISFIFO(buf.st_mode)) {char style[30]="管理文件";printf("%s\n",style);} else if(S_ISSOCK(buf.st_mode)) {char style[30]="套接字文件";printf("%s\n",style);} else {char style[30]="未知文件";printf("%s\n",style);}   printf("设备: %-5ld/%ldd Inode: %-12d硬链接: %d\n",buf.st_rdev,buf.st_dev,buf.st_ino,buf.st_nlink);   char str[10]={'-','-','-','-','-','-','-','-','-','\0'};// 判断权限 int mode=buf.st_mode; str[0]=(mode&S_IRUSR)? 'r':'-'; str[1]=(mode&S_IWUSR)? 'w':'-'; str[2]=(mode&S_IXUSR)? 'x':'-'; str[3]=(mode&S_IRGRP)? 'r':'-'; str[4]=(mode&S_IWGRP)? 'w':'-'; str[5]=(mode&S_IXGRP)? 'x':'-'; str[6]=(mode&S_IROTH)? 'r':'-'; str[7]=(mode&S_IWOTH)? 'w':'-'; str[8]=(mode&S_IXOTH)? 'x':'-';   struct passwd *pw = getpwuid(buf.st_uid); struct group *gr = getgrgid(buf.st_gid);   printf("权限: (%.4o/-%s) Uid: (%5d/%8s) Gid: (%5d/%8s)\n",buf.st_mode-32768,str,buf.st_uid,pw->pw_name,buf.st_gid,gr->gr_name); printf("环境: \n"); printf("最近访问: "); showtime(buf.st_atime); printf("\n"); printf("最近更改: "); showtime(buf.st_ctime); printf("\n"); printf("最近改动: "); showtime(buf.st_mtime); printf("\n"); // printf("%s\n",style); } long showtime(long timeval) {   struct tm *lccp; lccp=localtime(&timeval); printf("%d-%.2d-%.2d %.2d:%.2d:%.2d. ",lccp->tm_year+1900,lccp->tm_mon+1,lccp->tm_mday,(lccp->tm_hour)%24,lccp->tm_min,lccp->tm_sec); time_t timeValue = 0; struct tm *p = NULL; time(&timeValue); p = gmtime(&timeValue); int i =p->tm_hour; p = localtime(&timeValue); int j =p->tm_hour; // p2 = localtime(&timeValue); printf("+%.2d00",((j-i)+24)%24); }
5. 测试代码,mystat 与stat(1)对比,提交截图

 

 

 

标签:mystat,timeValue,2d,tm,lccp,printf,include
来源: https://www.cnblogs.com/ltq20191305/p/15584850.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有