当前位置:网站首页>Homework 7.29 correlation function directory and file attributes related functions
Homework 7.29 correlation function directory and file attributes related functions
2022-07-30 12:11:00 【Unknown college student M】
题目:Please use directory-related functions and file attribute-related functions to implement:The function to display the attributes of all files under the specified path(Except for hidden files),类似 ls -l 一个目录
函数代码
Extract file permission functions
void mode(mode_t a)
{
char s[3]={
'r','w','x'};
int index=0;
for(int i=8;i>=0;i--)
{
if(a&1<<i)
{
printf("%c",s[(i+1)%3]);
}
else
{
printf("-");
}
}
}
Extract file type function
int get_fileType(mode_t m)
{
switch (m & S_IFMT)
{
case S_IFBLK:
printf("b");break;
case S_IFCHR:
printf("c");break;
case S_IFDIR:
printf("d");break;
case S_IFIFO:
printf("p");break;
case S_IFLNK:
printf("l");break;
case S_IFREG:
printf("-");break;
case S_IFSOCK:
printf("s");break;
default:
printf("?");break;
}
}
Get the user name function to which the file belongs
int get_usrname(uid_t uid)
{
struct passwd *pwd=getpwuid(uid);
if(NULL==pwd)
{
printf("getpwuid failed\n");
return -1;
}
printf("%s",pwd->pw_name);
return 0;
}
The function to get the user name of the group to which the file belongs
int get_grpname(gid_t gid)
{
struct group*grp = getgrgid(gid);
if(NULL ==grp)
{
printf( "getgrgid failed\n" );
return -1;
}
printf( "%s" , grp->gr_name);
return 0;
}
Extract file last modified time
int timeset(__time_t *t)
{
struct tm *info=NULL;
info=localtime(t);
fprintf(stdout,"%d月 %02d %02d:%02d", \
info->tm_mon+1, info->tm_mday,\
info->tm_hour, info->tm_min);
fflush(stdout);
return 0;
}
主函数代码
int main(int argc, const char *argv[])
{
DIR* dp =opendir(argv[1]);
if(NULL ==dp)
{
perror( "opendir" );
return -1;
}
struct dirent *rp = NULL;
while(1)
{
rp = readdir(dp);
if(NULL ==rp)
{
if(0 == errno)//Error codes are not updated,The directory is read to the end
{
//printf("The directory has been read\n" );
break;
}
else
{
perror( "readdir" );
return-1;
}
}
if(rp->d_name[0]=='.')
{
continue;
}
struct stat buf;
char str[100];
strcpy(str,argv[1]);
strcat(str,rp->d_name);
if(stat(str,&buf)<0)
{
perror("stat");
return -1;
}
//文件类型以及权限
get_fileType(buf.st_mode);
mode(buf.st_mode);
printf(" ");
//硬链接数
printf("%ld ", buf.st_nlink);
//文件所属用户
get_usrname(buf.st_uid);
printf(" ");
//文件所属组用户
get_grpname(buf.st_gid);
printf(" ");
//文件大小
printf( "%6ld ", buf.st_size);
//文件创建日期
timeset(&buf.st_mtime);
printf(" ");
printf("%s\n",rp->d_name);
}
closedir(dp);
return 0;
}
运行结果
The function to display the attributes of all files under the specified path(Except for hidden files)


使用ls -lcommand to view the file attributes of the specified directory,Verify the correctness of the above code

源代码
https://gitee.com/Wei_JiaMin/directory-and-file-properties.git
边栏推荐
猜你喜欢

Rust 从入门到精通02-安装

PyQt5快速开发与实战 8.2 绘图 && 8.3 QSS的UI美化

明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级

24. 两两交换链表中的节点

概率论的学习整理2:如何对随机实验的对象:“事件” 进行计数呢? 四种计数方法,不只是排列组合

Current relay JL-8GB/11/AC220V

OneNote如何修改已有的笔记本为默认的快速笔记?

Explain the problem of change exchange in simple terms - the shell of the backpack problem

Voltage relay HDY - vac - 1 A / 1-220

备战金九银十!2022面试必刷大厂架构面试真题汇总+阿里七面面经+架构师简历模板分享
随机推荐
Matlab基础(2)——向量与多项式
IO/multiplexing (select/poll/epoll)
域名怎么注册备案解析?
EA中的业务对象和业务实体你分得清吗?
开源出来的fuse版pfs文件系统主要就是解决缓存问题吧。nfs挂载参数带sync规避缓存问题是不是
[SCTF2019]Flag Shop
Get the original data API on 1688app
Microsoft SQL服务器被黑客入侵 带宽被窃取
saltstack学习3模块
[BJDCTF2020]Cookie is so stable-1|SSTI注入
淘宝/天猫淘宝评论问答列表接口 API
2022-07-29 Gu Yujia Study Notes Exception Handling
Current relay JL-8GB/11/AC220V
High energy output!Tencent's internal MyCat middleware manual, both theoretical and practical
基于空间特征选择的水下目标检测方法
Hu-cang integrated e-commerce project (1): project background and structure introduction
Matlab基础(1)——基础知识
概率论的学习整理1: 集合和事件
Redis master-slave replication
Static LED display developed by single chip microcomputer