#include #include "dirlist.h" int mystrcmp(dfileinfo* f1, dfileinfo* f2) { if((f1->isdir && f2->isdir) || (!(f1->isdir) && !(f2->isdir))) return strcmp(f1->filename,f2->filename); else { if(f1->isdir) return -1; else return 1; } } void sortlist(dfileinfo** result, int n) { if(n<=1) return; dfileinfo* ref = result[0]; dfileinfo* tmp; int c,i; int start = 0; int end = n-1; for(i=1;i<=end;i++) { c=mystrcmp(ref,result[i]); if(c<0) { tmp=result[end]; result[end]=result[i]; result[i]=tmp; end--; i--; } else if(c>0) { tmp=result[start]; result[start]=result[i]; result[i]=tmp; start++; } } sortlist(result,start); sortlist(result+start+1,n-start-1); } int dirlist(char* path, dfileinfo** result) { DIR* dir; struct dirent *dent; if (!(dir = opendir(path))) return -1; chdir(path); int i = 0; while((dent=readdir(dir))!=NULL) { if(strcmp(dent->d_name,".") && strcmp(dent->d_name,"..")) { dfileinfo* dirname = (dfileinfo*) malloc(sizeof(dfileinfo)); dirname->isdir=0; strcpy(dirname->filename, (char*) dent->d_name); result[i] = dirname; FILE* tmp=fopen(dent->d_name,"rb"); if(tmp) fclose(tmp); else dirname->isdir=1; i++; } } closedir(dir); sortlist(result,i); return i; }