|
|
1.1 ! root 1: #include <sys/types.h> ! 2: #include <ndir.h> ! 3: #include <sys/stat.h> ! 4: #include "dirsel.h" ! 5: ! 6: /* almost like 4.2 scandir(), but the returned structs are different */ ! 7: #define MAXENTS 2000 /* hope we don't find a dir with more than MAXENTS entries */ ! 8: ! 9: int ! 10: dirsel(dirname, pents, select, compare) ! 11: char *dirname; ! 12: dirent **pents; ! 13: int (*select)(); ! 14: int (*compare)(); ! 15: { ! 16: dirent *ep; ! 17: DIR *ddp; ! 18: struct direct *dp; ! 19: struct stat statbuf; ! 20: unsigned long starttime; ! 21: int nents=0; ! 22: char fullname[MAXNAMLEN+1]; ! 23: char *p; ! 24: ! 25: ddp=opendir(dirname); ! 26: strcpy(fullname, dirname); ! 27: p= &fullname[strlen(dirname)]; ! 28: *p++ = '/'; ! 29: *p='\0'; ! 30: if (ddp==0) ! 31: return 0; ! 32: starttime=(unsigned long)time((long *)0); ! 33: *pents=ep=(dirent *)malloc(MAXENTS * sizeof(dirent)); ! 34: while ((dp=readdir(ddp))!=0){ ! 35: strcpy(p, dp->d_name); ! 36: if (stat(fullname, &statbuf)!=0) ! 37: continue; /* shouldn't happen */ ! 38: ep->name=(char *)malloc(dp->d_namlen+1); ! 39: strcpy(ep->name, dp->d_name); ! 40: ep->namelen=dp->d_namlen; ! 41: ep->isdir=((statbuf.st_mode&S_IFDIR) != 0); ! 42: ep->age=(long)(starttime-(unsigned long)statbuf.st_mtime); ! 43: if (select && !(*select)(ep)) ! 44: continue; /* don't count this entry */ ! 45: ep++, nents++; ! 46: if (nents==MAXENTS) ! 47: break; /* could realloc */ ! 48: } ! 49: closedir(ddp); ! 50: if (nents>1 && compare) ! 51: qsort(*pents, nents, sizeof(dirent), compare); ! 52: return nents; ! 53: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.