mystat
作者:互联网
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,提交码云链接#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); }
https://gitee.com/l20191305_li_tianqi/openssl/blob/master/mystat.c
5. 测试代码,mystat 与stat(1)对比,提交截图
标签:mystat,timeValue,2d,tm,lccp,printf,include 来源: https://www.cnblogs.com/ltq20191305/p/15584850.html