1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <dirent.h>
#include <io.h>
typedef struct DIR_s
{
int first;
struct dirent dir;
long handle;
} DIR;
DIR* opendir(const char* dirname)
{
struct _finddata_t data;
long handle;
char buf[1024];
strcpy(buf, dirname);
if(buf[strlen(buf)-1] != '\\')
{
strcat(buf, "\\");
}
strcat(buf, "*.*");
if((handle = _findfirst(buf, &data)) >= 0)
{
DIR* d = (DIR*)malloc(sizeof(DIR));
d->handle = handle;
d->first = 1;
strcpy(d->dir.d_name, data.name);
return d;
}
return 0;
}
struct dirent* readdir(DIR* d)
{
if(d)
{
struct _finddata_t data;
if(d->first)
{
d->first = 0;
return &d->dir;
}
else if(_findnext(d->handle, &data) == 0)
{
strcpy(d->dir.d_name, data.name);
return &d->dir;
}
}
return 0;
}
int closedir(DIR* d)
{
if(d)
{
return _findclose(d->handle);
}
return 0;
}
|