home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 9 Archive
/
09-Archive.zip
/
ARC521-2.ZIP
/
SCANDIR.C
< prev
Wrap
C/C++ Source or Header
|
1989-12-29
|
1KB
|
69 lines
/*
* SCANDIR
* Scan a directory, collecting all (selected) items into a an array.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/dir.h>
#define INITIAL_SIZE 20
#define DIRSIZ(d) (sizeof(struct direct))
int scandir(char *Name, struct direct ***List,
int (*Selector)(struct direct *), int (*Sorter)())
{
DIR *Dp;
struct direct **names, *E;
int i, size;
/* Get initial list space and open directory. */
size = INITIAL_SIZE;
if ( (names = malloc(size * sizeof names[0])) == NULL
|| (Dp = opendir(Name)) == NULL )
return -1;
/* Read entries in the directory. */
for ( i = 0; E = readdir(Dp); )
if ( Selector == NULL || (*Selector)(E) )
{
/* User wants them all, or he wants this one. */
if (++i >= size)
{
size <<= 1;
names = realloc(names, size * sizeof names[0]);
if (names == NULL)
{
closedir(Dp);
return -1;
}
}
/* Copy the struct direct. */
if ( (names[i - 1] = malloc(sizeof(struct direct))) == NULL)
{
closedir(Dp);
return -1;
}
*names[i - 1] = *E;
}
/* Close things off. */
names[i] = NULL;
*List = names;
closedir(Dp);
/* Sort? */
if (i && Sorter)
qsort((char *)names, i, sizeof names[0], Sorter);
return i;
}