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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| void ls_main(const char *path,bool *make){ DIR *dir; struct dirent *ent; struct stat res; int filecount = 0; char **files=(char**)malloc(sizeof(char*)*100000); dir = opendir(path); if(!dir){ free(files); return ; } while((ent=readdir(dir))!=NULL){ if(!make[_a]&&ent->d_name[0]=='.'){ continue; } files[filecount]=(char*)malloc(sizeof(char)*512); strcpy(files[filecount++],ent->d_name); } if(make[_t]){ for(int i=0;i<filecount;i++){ for(int j=0;j<filecount-i-1;j++){ struct stat a,b; time_t a1,b1; lstat(files[j],&a); a1=a.st_mtime; lstat(files[j+1],&b); b1=b.st_mtime; if(a1<b1){ char t[1024]; strcpy(t,files[j]); strcpy(files[j],files[j+1]); strcpy(files[j+1],t); } } } } if(make[_r]){ for(int i=0;i<filecount;i++){ for(int j=0;j<filecount-i-1;j++){ if(strcmp(files[j],files[j+1])<0){ char t[1024]; strcpy(t,files[j]); strcpy(files[j],files[j+1]); strcpy(files[j+1],t); } } } } if(make[_R]){ printf("%s:\n",path); } if(make[_s]||make[_l]){ size_t size = 0; for(int i=0;i<filecount;i++){ char *absl=(char*)malloc(sizeof(char)*1024); if(!strcmp(path,"/"))sprintf(absl,"/%s",files[i]); else sprintf(absl,"%s/%s",path,files[i]); if((lstat(absl,&res)==-1)){ continue; perror("lstat"); exit(1); } size+=res.st_blocks/2; free(absl); } printf("总用量 %ld\n",size); } for(int i=0;i<filecount;i++){ char *absl=(char*)malloc(sizeof(char)*1024); if(!strcmp(path,"/"))sprintf(absl,"/%s",files[i]); else sprintf(absl,"%s/%s",path,files[i]); if((lstat(absl,&res)==-1)){ continue; perror("lstat"); exit(1); } if(make[_i]){ printf("%ld ",res.st_ino); } if(make[_s]){ printf("%3ld ",res.st_blocks/2); } if(make[_l]){ print_file_info(absl,files[i]); } else{ print(files[i],res); printf(" "); } free(absl); } for(int i=0;i<filecount;i++){ char *absl=(char*)malloc(sizeof(char)*1024); if(!strcmp(path,"/"))sprintf(absl,"/%s",files[i]); else sprintf(absl,"%s/%s",path,files[i]); if((lstat(absl,&res)==-1)){ continue; perror("lstat"); exit(1); } if(make[_R]&&!(S_ISLNK(res.st_mode))&&S_ISDIR(res.st_mode)&&(strcmp(files[i],".")&&strcmp(files[i],".."))){ putchar('\n'); if(!make[_l]) putchar('\n'); ls_main(absl,make); } free(absl); } for(int i=0;i<filecount;i++){ free(files[i]); } free(files); closedir(dir); }
|