文件权限的解读实现如下:
1 void mode_to_char (mode_t mode, char *buf) 2 { 3 memset(buf, '-', 10); 4 if(S_ISDIR(mode)) 5 buf[0] = 'd'; 6 if(mode & S_IRUSR) 7 buf[1] = 'r'; 8 if(mode & S_IWUSR) 9 buf[2] = 'w'; 10 if(mode & S_IXUSR) 11 buf[3] = 'x'; 12 if(mode & S_IRGRP) 13 buf[4] = 'r'; 14 if(mode & S_IWGRP) 15 buf[5] = 'w'; 16 if(mode & S_IXGRP) 17 buf[6] = 'x'; 18 if(mode & S_IROTH) 19 buf[7] = 'r'; 20 if(mode & S_IWOTH) 21 buf[8] = 'w'; 22 if(mode & S_IXOTH) 23 buf[9] = 'x'; 24 } |
想应的,时间的显示不需要那么精确,所以我们应适当的缩短时间精确度。
时间的显示实现如下:
1 char *time_change(char *time)
2 {
3 int index = strlen(time) - 1;
4 for(; time[index] != ':'; index --);
5 time[index] = '\0';
6 return time + 4;
7 }
然后,我们需要根据文件名称按照字典序排序。
排序的实现如下:
1 void str_sort(char name[][CNT], int cnt) 2 { 3 int index, pos; 4 char str[CNT]; 5 for(pos = 1; pos < cnt; pos ++) 6 { 7 strcpy(str, name[pos]); 8 for(index = pos - 1; index >= 0; index --) 9 if(strcmp(name[index], str) > 0) 10 strcpy(name[index + 1], name[index]); 11 else 12 break; 13 strcpy(name[index + 1], str); 14 } 15 } |