当前位置:网站首页>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
边栏推荐
- saltstack学习2grains&pillar
- 概率论的学习和整理--番外4: 关于各种平均数:算术平均数,几何平均数,调和平均数,以及加权平均数和平方平均数 (未完成)
- pg_rewind 修复主备环境的时间线
- 概率论得学习和整理6:概率的分布
- 文本的对齐方式、行高、空间 等总结
- LeetCode_236_二叉树的最近公共祖先
- Redis 主从复制
- Concepts of cloud-native applications and 15 characteristics of cloud-native applications
- EXCEL解决问题:如何查找目标区域,是否包含指定字符串?
- [Database basics] redis usage summary
猜你喜欢
横向对比5种常用的注册中心,无论是用于面试还是技术选型,都非常有帮助
Program environment and preprocessing (detailed)
Typroa alternative tool marktext
概率论的学习和整理--番外4: 关于各种平均数:算术平均数,几何平均数,调和平均数,以及加权平均数和平方平均数 (未完成)
概率论得学习和整理6:概率的分布
Explain the problem of change exchange in simple terms - the shell of the backpack problem
STM32F1 reads MLX90632 non-contact infrared temperature sensor
嵌入式环境下并发控制与线程安全
文本的对齐方式、行高、空间 等总结
Verilog grammar basics HDL Bits training 08
随机推荐
Meituan internal push + school recruitment written test + summary of knowledge points
The method of judging the same variable without the if branch
Niuke-TOP101-BM42
Get the original data API on 1688app
概率论的学习整理--番外2:和二项式,组合相关的杨辉三角
Rust 从入门到精通02-安装
LinkedList与链表
Taobao/Tmall taobao comments q&a list interface API
2022-07-29 Gu Yujia Study Notes Exception Handling
嵌入式环境下并发控制与线程安全
Zhou Hongyi: Microsoft copied the 360 security model and became the largest security company in the United States
从“校园贷”到“直播带货”,追风少年罗敏一直行走在风口浪尖
[Cloud-Building Co-creation] Huawei Cloud and Hongmeng collaborate to cultivate innovative developers
向上管理读书笔记
EXCEL解决问题:如何查找目标区域,是否包含指定字符串?
Verilog grammar basics HDL Bits training 07
unity初学6——简易的UI制作(血条制作)和音频加入以及NPC的对话气泡(2d)
概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
C# 枚举类型 于xaml 中区别
Based on sliding mode control of uncertain neutral system finite time stable