void sort_time(char a[256][PATH_MAX+1], int left, int right,char mm[256][PATH_MAX]) { int i=left; int j=right; char key[80]; char key1[80]; strcpy(key,mm[left]); strcpy(key1,a[left]); if(left >= right){ return ; } while( i<j ){ while( i<j && strcmp(key, mm[j]) <0){ j--; } strcpy(a[i], a[j]); strcpy(mm[i],mm[j]); while(i<j && strcmp(mm[i], key) <=0){ i++; } strcpy(a[j], a[i]); strcpy(mm[j],mm[i]); } strcpy(mm[i], key); strcpy(a[i],key1); sort_time(a, left, i-1, mm); sort_time(a, i+1, right, mm); }
// Print a timing report of the time the sorting algorithms in sorters take to sort the // list generated by the generators in gens of the sizes in sizes void sorting_report(std::vector<sorter_t<int>> sorters, std::vector<std::string> sorter_names, unsigned int num_runs, std::vector<unsigned int> sizes, std::vector<generator> gens, std::vector<std::string> gen_names) { // For each sorter, generator, and size run a test for (int i = 0; i < sorters.size(); i++) { std::cout << "=====" << sorter_names[i] << "=====" << std::endl; sorter_t<int> sorter = sorters[i]; for (int j = 0; j < gens.size(); j++) { std::vector<double> times = std::vector<double>(sizes.size()); std::cout << gen_names[j] << " (size : time)" << std::endl; generator gen = gens[j]; for (int k = 0; k < sizes.size(); k++) { unsigned int size = sizes[k]; double time = sort_time(sorter, num_runs, size, gen); times[k] = time; } // Print the test results for this sorter and generator formatted_print(sizes,times); } } }
int main(int ac, char **av) { t_file *myfile; t_file *next; t_file *before; DIR *directory; char *dirname; t_dir *dirstruct; ssize_t ret; int block; block = 0; before = init_list(before); myfile = init_list(myfile); myfile = myfile; myfile->next = before; dirname = "."; if (ac > 1) dirname = ft_strdup(av[1]); directory = opendir(dirname); if (errno != 0) { if (errno == ENOTDIR) { ft_putinfo(myfile, dirname, "", 0); ft_printinfo(myfile); } else ft_putstr_endl(strerror(errno)); return (-1); } if ((ret = readlink(av[1], dirname, 256)) != -1) { dirname[ret] = '\0'; return (0); } while ((dirstruct = readdir(directory))) { before = ft_putinfo(before, dirstruct->d_name, dirname, 0); block += before->blocks; //printf("%ld\n",before->laccess.tv_sec); before = new_link(next, before); } printf("total %d\n", block); myfile = myfile->next; myfile = sort_time(myfile); while (myfile->next != NULL) { ft_printinfo(myfile); myfile = myfile->next; } closedir(directory); return (0); }
/*time=1表示按时间排序,file=1表示按文件名正=2反,sT_INO表示是否有-i*/ void display_dir(int flag_param, char *path, int time, int file,int sT_INO) { DIR *dir; struct dirent *ptr; int count=0,i,j,len; char filenames[256][PATH_MAX+1],temp[PATH_MAX+1]; struct stat buf; char mm[256][PATH_MAX]; char hh[256][PATH_MAX]; char buf_time[32]; /*存时间*/ /*获取该目录下文件总数和最长的路径名*/ dir=opendir(path); if(dir==NULL){ my_err("opendir",__LINE__); } while( (ptr=readdir(dir)) !=NULL ){ if(g_maxlen < strlen(ptr->d_name)) g_maxlen=strlen(ptr->d_name); count++; } closedir(dir); len=strlen(path); if(count>256){ my_err("readdir",__LINE__); } //获取该目录下所有的文件名 dir=opendir(path); for(i=0; i<count; i++){ ptr=readdir(dir); if(ptr==NULL){ my_err("readdir", __LINE__); } strncpy(filenames[i],path,len); filenames[i][len]='\0'; strcat(filenames[i], ptr->d_name); filenames[i][len+strlen(ptr->d_name)]='\0'; } for(i=0 ;i<count; i++){ j=0; if(lstat(filenames[i], &buf)==-1){ my_err("stst",__LINE__); } //将文件文件解析,时间保存在mm里面 strcpy(buf_time, ctime(&buf.st_mtime) ); buf_time[strlen(buf_time)-1]='\0'; // qudiaohuanhang strcpy(mm[i], buf_time); // update(i, hh, mm); //将mm按指定方式保存在hh里面用于排序 // printf("%s",hh[i]); // getchar(); } if(time==1){ sort_time(filenames, 0, count-1, mm); for(i=0; i<count; i++){ display(flag_param, filenames[i],sT_INO); }//将排序好的文件输出 return ; } else{ if(file !=1 && file !=2){ for(i=0; i<count; i++) display(flag_param, filenames[i],sT_INO); } else sort(filenames,0, count-1); //按文件名排序 if(file==1){ for(i=0; i<count; i++) display(flag_param, filenames[i],sT_INO); } //正输 if(file==2){ for(i=count-1; i>=0; i--){ display(flag_param, filenames[i], sT_INO); } } //反输 } closedir(dir); /*如果命令行中没有-l选项,打印一个换行符*/ if( (flag_param & PARAM_L)==0) printf("\n"); }