高仿linux集群,高仿linux下的ls -l命令——C语言实现

1 #include

2 #include

3 #include

4 #include

5 #include

6 #include

7 #include

8 #include

9 #include

10 #include

11 #include

12

13 #define MAX_FILE_NUM 200

14

15

16 //可能还有一些小问题没有解决,功能基本已经实现,如有建议,望大佬赐教

17

18 typedef structLS19 {20 char mode[15]; //文件的模式

21 int dir_num; //是否目录或目录中包含目录的数量

22 char user[20]; //文件的用户名

23 char group[20]; //文件的组名

24 long size; //文件的字节数

25 char time[30]; //文件的最后修改时间

26 char year[5]; //拓展用,年份

27 char mon[5]; //月份

28 char hour[5]; //时

29 char min[5]; //分

30 int st_mode; //文件类型和权限

31 char name[20]; //文件名

32 }LS;33

34 //获取文件的模式

35 char* file_mode(mode_t m,char*str)36 {37 if(S_ISREG(m))38 str[0] = '-';39 else if(S_ISDIR(m))40 str[0] = 'd';41 else if(S_ISCHR(m))42 str[0] = 'c';43 else if(S_ISBLK(m))44 str[0] = 'b';45 else if(S_ISFIFO(m))46 str[0] = 'q';47 else if(S_ISLNK(m))48 str[0] = 'l';49 //else if(S_ISSOCK(m))50 //str[0] = 's';

51 else

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

68 returnstr;69 }70

71 //获取目录的数量

72 int dir_count(char*path)73 {74 DIR *dir;75 dir =opendir(path);76 struct dirent *dirent;77 int count = 0;78 while((dirent = readdir(dir)) !=NULL)79 {80 if(dirent->d_type == 4)81 count++;82 }83 closedir(dir);84 returncount;85 }86

87 //是否是目录或目录下有目录

88 int is_dir(struct dirent *dirent)89 {90 char* a = dirent->d_name;91 if(dirent->d_type == 8)92 return 1;93 if(dirent->d_type == 4)94 {95 if(dir_count(a) == 0)96 return 2;97 else

98 returndir_count(a);99 }100 }101

102 //获取用户名

103 char* file_user(uid_t st_uid,char*str)104 {105 struct passwd *user;106 user =getpwuid(st_uid);107 sprintf(str,"%s",user->pw_name);108 returnstr;109 }110

111 //获取组名

112 char* file_group(uid_t st_uid,char*str)113 {114 struct passwd *user;115 user =getpwuid(st_uid);116 struct group *grp;117 grp = getgrgid(user->pw_gid);118 sprintf(str,"%s",grp->gr_name);119 returnstr;120 }121

122 //获取文件大小

123 off_t file_size(structstat buf)124 {125 off_t size =buf.st_size;126 returnsize;127 }128

129 //获取最后修改时间

130 char* file_time(time_t mt,char*str)131 {132 struct tm* t = localtime(&mt);133 sprintf(str,"%d月 %02d %02d:%02d",t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);134 returnstr;135 }136

137 //获取文件的数量

138 int file_count(char*path)139 {140 DIR *dir;141 dir =opendir(path);142 struct dirent *dirent;143 int count = 0;144 while((dirent = readdir(dir)) !=NULL)145 {146 count++;147 }148 closedir(dir);149 returncount;150 }151

152 //交换

153 void 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 }164

165 //排序

166 void sort(LS *info,intindex)167 {168 LS *temp = (LS*)malloc(sizeof(LS));169 for(int i=index-1; i>0; i--)170 {171 for(int j=0; j

183 //输出结构体

184 void show_ls(LS *info,intindex)185 {186 for(int i=0; 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);

196 if(16893 ==info[i].st_mode)197 {198 //颜色

199 printf("\033[34m\033[1m%s\033[0m",info[i].name);200 }201 else if(33277 ==info[i].st_mode)202 {203 printf("\033[32m\033[1m%s\033[0m",info[i].name);204 }205 else

206 {207 printf("%s",info[i].name);208 }209 if(i

213 }214

215 //创建结构体,赋值

216 LS *create(struct stat buf,struct dirent *dirent)217 {218 LS* info = (LS*)malloc(sizeof(LS));219 char 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);230

231 returninfo;232 }233

234 int main(int argc,char*argv[])235 {236 LS info[MAX_FILE_NUM];237 char* l = "-l";238 if(strcmp(argv[1],l) != 0)239 {240 printf("\"ls:无法识别的选项\"%s\"\n",argv[1]);241 printf("请尝试执行\"ls --help\"来获取更多信息。\n");242 return 0;243 }244 char* a = ".";245 char* b = "..";246 char* path = malloc(10000);247 strcpy(path,"./"); //只支持当前路径

248 int count =file_count(path);249

250 DIR *dir;251 dir =opendir(path);252 struct dirent *dirent;253 int index = 0; //结构体下标

254 int blocks = 0;255 for(int i=0; id_name,&buf))260 {261 perror("stat");262 return -1;263 }264

265 //跳过特殊情况

266 if(strcmp(dirent->d_name,a)==0 || strcmp(dirent->d_name,b)==0)267 continue;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);

275

276 printf("总用量 %d\n",blocks/2);277 sort(info,index);278 show_ls(info,index);279 return 0;280 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值