当前位置:网站首页>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
边栏推荐
- 限时招募!淘宝无货源副业,800/天,不限经验,男女皆可,仅限前200名!
- LeetCode_235_Last Common Ancestor of Binary Search Tree
- saltstack学习2grains&pillar
- Zhou Hongyi: Microsoft copied the 360 security model and became the largest security company in the United States
- Farmers on the assembly line: I grow vegetables in a factory
- Digital input and output module DAM-5088
- 单片机工程师笔试题目归纳汇总
- Summary of text alignment, line height, space, etc.
- ECCV 2022 | 新加坡国立大学提出:全新可恢复型模型遗忘框架LIRF!
- 时间序列曲线相似性
猜你喜欢

Interviewer: Redis bloom filter and the cuckoo in the filter, how much do you know?

stm32 RTC闹钟唤醒低功耗模式

Rust 从入门到精通02-安装

contentDocument contentWindow,canvas 、svg,iframe

ECCV 2022 | 新加坡国立大学提出:全新可恢复型模型遗忘框架LIRF!

VSCode更改插件的安装位置

contentDocument contentWindow, canvas, svg, iframe

2022-07-29 Gu Yujia Study Notes Exception Handling

IO/multiplexing (select/poll/epoll)

A tutorial on how to build a php environment under win
随机推荐
Kubernetes之本地存储
电脑奔溃的时候,到底发生了什么?
淘宝/天猫淘宝评论问答列表接口 API
历时两月,终拿字节跳动offer,算法面试题分享「带答案」
Matlab基础(0)——命令行常用指令
嵌入式环境下并发控制与线程安全
Based on the analysis of the acoustic channel cable tunnel positioning technology
Flexible distribution parameters of mechanical system modeling and control of research and development
【CVA估值训练营】如何快速读懂上市公司年报——第五讲
Hu-cang integrated e-commerce project (1): project background and structure introduction
反转链表-迭代反转法
[SCTF2019]Flag Shop
24. 两两交换链表中的节点
云原生应用的概念和云原生应用的 15 个特征
Niuke-TOP101-BM42
C# 枚举类型 于xaml 中区别
I built another wheel: GrpcGateway
Meituan internal push + school recruitment written test + summary of knowledge points
ansible学习笔记01
Concepts of cloud-native applications and 15 characteristics of cloud-native applications