55 #if defined(LIBC_SCCS) && !defined(lint) 56 static char sccsid[] =
"@(#)scandir.c 5.10 (Berkeley) 2/23/91";
72 #include <sys/types.h> 81 #ifdef _DIRENT_HAVE_D_NAMLEN 83 ((sizeof(struct dirent) - (MAXNAMLEN + 1)) + \ 84 (((dp)->d_namlen + 1 + 3) & ~3)) 87 ((sizeof(struct dirent) - (MAXNAMLEN + 1)) + \ 88 ((strlen((dp)->d_name) + 1 + 3) & ~3)) 95 int scandir(dirname, namelist, select, dcomp) const
char *dirname;
96 struct dirent ***namelist;
97 int(*select) __P((struct dirent *) );
98 int(*dcomp) __P((const
void *, const
void *) );
100 register struct dirent *d, *p, **names;
101 register size_t nitems;
106 if ((dirp = opendir(dirname)) == NULL)
108 if (fstat(dirp->fd, &stb) < 0)
115 arraysz = (stb.st_size / 24);
116 names = (
struct dirent **) malloc(arraysz *
sizeof(
struct dirent *));
121 while ((d = readdir(dirp)) != NULL)
123 if (select != NULL && !(*select)(d))
128 p = (
struct dirent *) malloc(DIRSIZ(d));
133 #ifdef _DIRENT_HAVE_D_NAMLEN 134 p->d_namlen = d->d_namlen;
135 bcopy(d->d_name, p->d_name, p->d_namlen + 1);
137 strcpy(p->d_name, d->d_name);
143 if (++nitems >= arraysz)
145 if (fstat(dirp->fd, &stb) < 0)
147 arraysz = stb.st_size / 12;
148 names = (
struct dirent **) realloc(
149 (
char *) names, arraysz *
sizeof(
struct dirent *));
153 names[nitems - 1] = p;
156 if (nitems && dcomp != NULL)
157 qsort(names, nitems,
sizeof(
struct dirent *), dcomp);
165 int alphasort(d1, d2) const struct dirent **d1;
166 const struct dirent **d2;
168 return (strcmp((*d1)->d_name, (*d2)->d_name));