我是靠谱客的博主 笨笨豌豆,这篇文章主要介绍要求输入目录路径以及名字,能够将该路径下的所有文件的属性打印出来,类似ls -la,现在分享给大家,希望可以做个参考。

atat.h文件代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef __STAT_H__ #define __STAT_H__ //文件类型 char get_fileType(mode_t mode); //文件权限 char* get_filePermission(mode_t mode, char* pstr); //硬链接 void get_filenlink(nlink_t nlink); //文件所属用户 void get_filepasswd(uid_t uid); //文件所属组用户 void get_filegroup(gid_t gid); //文件大小 void get_filesize(off_t size); //文件时间 void get_filetime(time_t time); #endif

stat.c文件代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <pwd.h> #include <grp.h> #include<time.h> #include"stat.h" //定义文件类型函数 char get_fileType(mode_t mode) { if(S_ISREG(mode)) return '-'; else if(S_ISDIR(mode)) return 'd'; else if(S_ISCHR(mode)) return 'c'; else if(S_ISBLK(mode)) return 'b'; else if(S_ISFIFO(mode)) return 'p'; else if(S_ISLNK(mode)) return 'l'; else if(S_ISSOCK(mode)) return 's'; } //定义文件权限函数 char* get_filePermission(mode_t mode, char* pstr) { char str[4] = {'r','w','x'}; for(int i=0; i<9; i++) { if((mode & (0400>>i)) == 0) { pstr[i] = '-'; continue; } //能运行到当前位置,则代表mode&(0400>>i)是不等于0; pstr[i] = str[i%3]; } return pstr; } //文件硬链接数函数 void get_filenlink(nlink_t nlink) { printf(" %ld", nlink); } //定义文件所属用户函数 void get_filepasswd(uid_t uid) { struct passwd *pwd = getpwuid(uid); if(NULL == pwd) { perror("getpwuid"); return; } printf(" %s", pwd->pw_name); } //文件所属组用户函数 void get_filegroup(gid_t gid) { struct group* grp = getgrgid(gid); if(NULL == grp) { perror("getgrgid"); return ; } printf(" %s", grp->gr_name); } //文件大小函数 void get_filesize(off_t size) { printf(" %6ld",size); } //文件的时间 void get_filetime(time_t time) { char *mon[12] = {"一","二","三","四","五","六","七","八","九","十","十一","十二"}; struct tm* info = localtime(&time); printf(" %s %02d %02d:%02d:%02d",mon[info->tm_mon],info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec); }

主函数代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <unistd.h> #include<errno.h> #include"stat.h" int main(int argc, const char *argv[]) { //打开一个目录 DIR *dp = opendir("./"); if(NULL == dp) { perror("opendir"); return -1; } //读取目录 struct dirent *rp = NULL; int i = 1; while(1) { rp = readdir(dp); if(NULL == rp) { if(errno != 0) { perror("readdir"); return -1; } else { break; } } struct stat buf; if(stat(rp->d_name,&buf) < 0) { perror("stat"); return -1; } //调用文件类型函数 char type = get_fileType(buf.st_mode); printf("%c",type); //调用文件权限函数 char per[10] = ""; get_filePermission(buf.st_mode,per); printf("%s",per); //调用文件硬链接数函数 get_filenlink(buf.st_nlink); //调用文件所属用户函数 get_filepasswd(buf.st_uid); //调用文件所属组用户函数 get_filegroup(buf.st_gid); //调用文件大小函数 get_filesize(buf.st_size); //调用文件的时间函数 get_filetime(buf.st_ctime); //文件名 printf(" %sn",rp->d_name); } //关闭目录 if(closedir(dp)<0) { perror("closedir"); return -1; } return 0; }

运行结果

 

最后

以上就是笨笨豌豆最近收集整理的关于要求输入目录路径以及名字,能够将该路径下的所有文件的属性打印出来,类似ls -la的全部内容,更多相关要求输入目录路径以及名字,能够将该路径下的所有文件的属性打印出来,类似ls内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部