1 Star 0 Fork 0

20191318王泽文/系统作业

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
mystat.c 4.98 KB
Copy Edit Raw Blame History
20191318王泽文 authored 2021-11-07 17:55 +08:00 . mystat
#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>
// File: `testfile'
// Size: 102 Blocks: 8 IO Block: 4096 regular file
// Device: 807h/2055d Inode: 1265161 Links: 1
// Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
// Access: 2014-08-13 14:07:20.000000000 +0800
// Modify: 2014-08-13 14:07:07.000000000 +0800
// Change: 2014-08-13 14:07:07.000000000 +0800
// 文件:bin/mys
// 大小:17016 块:40 IO 块:4096 普通文件
// 设备:fd00h/64768d Inode:2119409 硬链接:1
// 权限:(0700/-rwx------) Uid:( 0/ root) Gid:( 0/ root)
// 环境:unconfined_u:object_r:default_t:s0
// 最近访问:2021-11-06 10:52:42.717439983 +0800
// 最近更改:2021-11-06 10:52:34.182337354 +0800
// 最近改动:2021-11-06 10:52:34.182337354 +0800
// 创建时间:2021-11-06 10:52:34.170337210 +0800
// struct stat
// {
// dev_t st_dev; //device 文件的设备编号
// ino_t st_ino; //inode 文件的i-node
// mode_t st_mode; //protection 文件的类型和存取的权限
// nlink_t st_nlink; //number of hard links 连到该文件的硬连接数目, 刚建立的文件值为1.
// uid_t st_uid; //user ID of owner 文件所有者的用户识别码
// gid_t st_gid; //group ID of owner 文件所有者的组识别码
// dev_t st_rdev; //device type 若此文件为装置设备文件, 则为其设备编号
// off_t st_size; //total size, in bytes 文件大小, 以字节计算
// unsigned long st_blksize; //blocksize for filesystem I/O 文件系统的I/O 缓冲区大小.
// unsigned long st_blocks; //number of blocks allocated 占用文件区块的个数, 每一区块大小为512 个字节.
// time_t st_atime; //time of lastaccess 文件最近一次被存取或被执行的时间, 一般只有在用mknod、utime、read、write 与tructate 时改变.
// time_t st_mtime; //time of last modification 文件最后一次被修改的时间, 一般只有在用mknod、utime 和write 时才会改变
// time_t st_ctime; //time of last change i-node 最近一次被更改的时间, 此参数会在文件所有者、组、权限被更改时更新
// };
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);
// char h[2]={'-','\0'};
// if(j-i>=0){h[0]='+';}
// printf("%d-%d-%.2d %d:%d:%d.",cp->tm_year+1900,cp->tm_mon+1,cp->tm_mday,(cp->tm_hour+8)%24,cp->tm_min,cp->tm_sec);
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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Yushino/system-job.git
git@gitee.com:Yushino/system-job.git
Yushino
system-job
系统作业
master

Search