当前位置:网站首页>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
边栏推荐
- 京东二面痛遭中间件虐杀,30天学透这套中间件小册,挺进阿里
- External Force Estimation Based on Time Delay Estimation with Perturbed Kalman Filter
- Matlab绘图(1)——二维绘图
- Verilog语法基础HDL Bits训练 07
- contentDocument contentWindow,canvas 、svg,iframe
- 深入浅出零钱兑换问题——背包问题的套壳
- 程序环境和预处理(详解)
- 限时招募!淘宝无货源副业,800/天,不限经验,男女皆可,仅限前200名!
- Bagging-Blending Multi-Model Fusion Short-Term Electricity Load Forecasting Based on Weighted Grey Correlation Projection
- [Database basics] redis usage summary
猜你喜欢

Concepts of cloud-native applications and 15 characteristics of cloud-native applications

概率论的学习和整理7:理解期望和方差还是要回到随机试验本身,期望不是平均值,方差的公式不同情况不同

LeetCode_236_Last Common Ancestor of a Binary Tree

Microsoft SQL server hacked, bandwidth stolen

又爆神作!阿里爆款MySQL高级宝典开源,直抵P7

unity初学6——简易的UI制作(血条制作)和音频加入以及NPC的对话气泡(2d)

Verilog grammar basics HDL Bits training 07

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

GBJ2510-ASEMI电机专用25A整流桥GBJ2510

概率论的学习整理2:如何对随机实验的对象:“事件” 进行计数呢? 四种计数方法,不只是排列组合
随机推荐
概率论的学习整理4:全概率公式
Matlab基础(3)——元胞与结构体
牛客-TOP101-BM42
EXCEL解决问题:如何查找目标区域,是否包含指定字符串?
C#调用explorer.exe打开指定目录
获取1688app上原数据 API
C语言 — 位运算操作
【32. 图中的层次(图的广度优先遍历)】
淘宝/天猫淘宝评论问答列表接口 API
LeetCode_235_Last Common Ancestor of Binary Search Tree
[Database basics] redis usage summary
Verilog语法基础HDL Bits训练 08
contentDocument contentWindow, canvas, svg, iframe
基于空间特征选择的水下目标检测方法
基于多目标两阶段随机规划方法的电热联合系统调度
Flexible distribution parameters of mechanical system modeling and control of research and development
分布式限流 redission RRateLimiter 的使用及原理
Static LED display developed by single chip microcomputer
PyQt5快速开发与实战 8.4 设置窗口背景 && 8.5 不规则窗口的显示
概率论的学习和整理7:理解期望和方差还是要回到随机试验本身,期望不是平均值,方差的公式不同情况不同