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