高仿linux下的ls -l命令——C语言实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了高仿linux下的ls -l命令——C语言实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6054字,纯文字阅读大概需要9分钟。
内容图文
主要用到的函数可以参考头文件,仅仅支持ls -l这功能,扩展就交给大家了0.0
相关测试图片:
?
?
话不多说,直接上码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/stat.h> 5 #include <sys/types.h> 6 #include <string.h> 7 #include <time.h> 8 #include <pwd.h> 9 #include <grp.h> 10 #include <time.h> 11 #include <dirent.h> 12 13#define MAX_FILE_NUM 200 14 15 16//可能还有一些小问题没有解决,功能基本已经实现,如有建议,望大佬赐教 17 18 typedef struct LS 19{ 20char mode[15]; // 文件的模式 21int dir_num; // 是否目录或目录中包含目录的数量 22char user[20]; // 文件的用户名 23char group[20]; // 文件的组名 24long size; // 文件的字节数 25char time[30]; // 文件的最后修改时间 26char year[5]; // 拓展用,年份 27char mon[5]; // 月份 28char hour[5]; // 时 29char min[5]; // 分 30int st_mode; // 文件类型和权限 31char name[20]; // 文件名 32}LS; 33 34// 获取文件的模式 35char* file_mode(mode_t m,char* str) 36{ 37if(S_ISREG(m)) 38 str[0] = ‘-‘; 39elseif(S_ISDIR(m)) 40 str[0] = ‘d‘; 41elseif(S_ISCHR(m)) 42 str[0] = ‘c‘; 43elseif(S_ISBLK(m)) 44 str[0] = ‘b‘; 45elseif(S_ISFIFO(m)) 46 str[0] = ‘q‘; 47elseif(S_ISLNK(m)) 48 str[0] = ‘l‘; 49// else if(S_ISSOCK(m)) 50// str[0] = ‘s‘; 51else 52 str[0] = ‘?‘; 53 54 str[1] = ‘\0‘; 55 56 strcat(str,S_IRUSR&m?"r":"-"); 57 strcat(str,S_IWUSR&m?"w":"-"); 58 strcat(str,S_IXUSR&m?"x":"-"); 59 60 strcat(str,S_IRGRP&m?"r":"-"); 61 strcat(str,S_IWGRP&m?"w":"-"); 62 strcat(str,S_IXGRP&m?"x":"-"); 63 64 strcat(str,S_IROTH&m?"r":"-"); 65 strcat(str,S_IWOTH&m?"w":"-"); 66 strcat(str,S_IXOTH&m?"x":"-"); 67 68return str; 69} 70 71// 获取目录的数量 72int dir_count(char* path) 73{ 74 DIR *dir; 75 dir = opendir(path); 76struct dirent *dirent; 77int count = 0; 78while((dirent = readdir(dir)) != NULL) 79 { 80if(dirent->d_type == 4) 81 count++; 82 } 83 closedir(dir); 84return count; 85} 86 87// 是否是目录或目录下有目录 88int is_dir(struct dirent *dirent) 89{ 90char* a = dirent->d_name; 91if(dirent->d_type == 8) 92return1; 93if(dirent->d_type == 4) 94 { 95if(dir_count(a) == 0) 96return2; 97else 98return dir_count(a); 99 } 100} 101102// 获取用户名103char* file_user(uid_t st_uid,char* str) 104{ 105struct passwd *user; 106 user = getpwuid(st_uid); 107 sprintf(str,"%s",user->pw_name); 108return str; 109} 110111// 获取组名112char* file_group(uid_t st_uid,char* str) 113{ 114struct passwd *user; 115 user = getpwuid(st_uid); 116struct group *grp; 117 grp = getgrgid(user->pw_gid); 118 sprintf(str,"%s",grp->gr_name); 119return str; 120} 121122// 获取文件大小123 off_t file_size(struct stat buf) 124{ 125 off_t size = buf.st_size; 126return size; 127} 128129// 获取最后修改时间130char* file_time(time_t mt,char* str) 131{ 132struct tm* t = localtime(&mt); 133 sprintf(str,"%d月 %02d %02d:%02d",t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min); 134return str; 135} 136137// 获取文件的数量138int file_count(char* path) 139{ 140 DIR *dir; 141 dir = opendir(path); 142struct dirent *dirent; 143int count = 0; 144while((dirent = readdir(dir)) != NULL) 145 { 146 count++; 147 } 148 closedir(dir); 149return count; 150} 151152// 交换153void equal(LS *a,LS *b) 154{ 155 strcpy(a->mode,b->mode); 156 a->dir_num = b->dir_num; 157 strcpy(a->user,b->user); 158 strcpy(a->group,b->group); 159 a->size = b->size; 160 strcpy(a->time,b->time); 161 a->st_mode = b->st_mode; 162 strcpy(a->name,b->name); 163} 164165// 排序166void sort(LS *info,int index) 167{ 168 LS *temp = (LS*)malloc(sizeof(LS)); 169for(int i=index-1; i>0; i--) 170 { 171for(int j=0; j<i; j++) 172 { 173if(strcmp(info[i].name,info[j].name)<0) 174 { 175 equal(temp,&info[i]); 176 equal(&info[i],&info[j]); 177 equal(&info[j],temp); 178 } 179 } 180 } 181} 182183// 输出结构体184void show_ls(LS *info,int index) 185{ 186for(int i=0; i<index; i++) 187 { 188//printf("%d: ",i);189 printf("%s \033[0m",info[i].mode); 190 printf("%d ",info[i].dir_num); 191 printf("%s ",info[i].user); 192 printf("%s ",info[i].group); 193 printf("%5ld ",info[i].size); 194 printf(" %s ",info[i].time); 195//printf("%d ",info[i].st_mode); 196if(16893 == info[i].st_mode) 197 { 198// 颜色199 printf("\033[34m\033[1m%s\033[0m",info[i].name); 200 } 201elseif(33277 == info[i].st_mode) 202 { 203 printf("\033[32m\033[1m%s\033[0m",info[i].name); 204 } 205else206 { 207 printf("%s",info[i].name); 208 } 209if(i < index) 210 printf("\n"); 211 } 212//printf("循环结束\n");213} 214215// 创建结构体,赋值216 LS *create(struct stat buf,struct dirent *dirent) 217{ 218 LS* info = (LS*)malloc(sizeof(LS)); 219char str[50] = {}; 220//puts(file_mode(buf.st_mode,str));221 strcpy(info->mode,file_mode(buf.st_mode,str)); 222//puts(info->mode);223 info->dir_num = is_dir(dirent); 224 strcpy(info->user,file_user(buf.st_uid,str)); 225 strcpy(info->group,file_group(buf.st_uid,str)); 226 info->size = file_size(buf); 227 strcpy(info->time,file_time(buf.st_mtime,str)); 228 info->st_mode = buf.st_mode; 229 strcpy(info->name,dirent->d_name); 230231return info; 232} 233234int main(int argc,char* argv[]) 235{ 236 LS info[MAX_FILE_NUM]; 237char* l = "-l"; 238if(strcmp(argv[1],l) != 0) 239 { 240 printf("\"ls:无法识别的选项\"%s\"\n",argv[1]); 241 printf("请尝试执行\"ls --help\"来获取更多信息。\n"); 242return0; 243 } 244char* a = "."; 245char* b = ".."; 246char* path = malloc(10000); 247 strcpy(path,"./"); // 只支持当前路径248int count = file_count(path); 249250 DIR *dir; 251 dir = opendir(path); 252struct dirent *dirent; 253int index = 0; // 结构体下标254int blocks = 0; 255for(int i=0; i<count; i++) 256 { 257 dirent = readdir(dir); 258struct stat buf = {}; 259if(stat(dirent->d_name,&buf)) 260 { 261 perror("stat"); 262return -1; 263 } 264265// 跳过特殊情况266if(strcmp(dirent->d_name,a)==0 || strcmp(dirent->d_name,b)==0) 267continue; 268 blocks += buf.st_blocks; 269//printf("%d\n",blocks);270 info[index++] = *create(buf,dirent); 271 } 272 closedir(dir); 273//printf("文件总数:%d\n",index); 274//show_ls(info,index); 275276 printf("总用量 %d\n",blocks/2); 277 sort(info,index); 278 show_ls(info,index); 279return0; 280 }
原文:https://www.cnblogs.com/ikaros-521/p/11254332.html
内容总结
以上是互联网集市为您收集整理的高仿linux下的ls -l命令——C语言实现全部内容,希望文章能够帮你解决高仿linux下的ls -l命令——C语言实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。