#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dos.h>
#include <ndir.h>
#include "xalloc.h"
static void free_dircontents (struct _dircontents *);
#define ATTRIBUTES (_A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR)
DIR *
opendir (const char *name)
{
struct _finddata_t find_buf;
DIR *dirp;
struct _dircontents *dp;
char name_buf[_MAX_PATH + 1];
char *slash = "";
long hFile;
if (!name)
name = "";
else if (*name)
{
const char *s;
int l = strlen (name);
s = name + l - 1;
if ( !(l == 2 && *s == ':') && *s != '\\' && *s != '/')
slash = "/";
}
strcat (strcat (strcpy (name_buf, name), slash), "*.*");
dirp = xmalloc (sizeof (DIR));
if (!dirp) return NULL;
dirp->dd_loc = 0;
dirp->dd_contents = dirp->dd_cp = NULL;
if ((hFile = _findfirst (name_buf, &find_buf)) < 0)
{
free (dirp);
return NULL;
}
do
{
dp = xmalloc (sizeof (struct _dircontents));
if (!dp)
{
free_dircontents (dirp->dd_contents);
return NULL;
}
dp->_d_entry = xmalloc (strlen (find_buf.name) + 1);
if (!dp->_d_entry)
{
free (dp);
free_dircontents (dirp->dd_contents);
return NULL;
}
if (dirp->dd_contents)
dirp->dd_cp = dirp->dd_cp->_d_next = dp;
else
dirp->dd_contents = dirp->dd_cp = dp;
strcpy (dp->_d_entry, find_buf.name);
dp->_d_next = NULL;
} while (!_findnext (hFile, &find_buf));
dirp->dd_cp = dirp->dd_contents;
_findclose(hFile);
return dirp;
}
void
closedir (DIR *dirp)
{
free_dircontents (dirp->dd_contents);
free (dirp);
}
struct direct *
readdir (DIR *dirp)
{
static struct direct dp;
if (!dirp->dd_cp) return NULL;
dp.d_namlen = dp.d_reclen =
strlen (strcpy (dp.d_name, dirp->dd_cp->_d_entry));
#if 0
strlwr (dp.d_name);
#endif
dp.d_ino = 0;
dirp->dd_cp = dirp->dd_cp->_d_next;
dirp->dd_loc++;
return &dp;
}
void
seekdir (DIR *dirp, long off)
{
long i = off;
struct _dircontents *dp;
if (off < 0)
return;
for (dp = dirp->dd_contents; --i >= 0 && dp; dp = dp->_d_next)
;
dirp->dd_loc = off - (i + 1);
dirp->dd_cp = dp;
}
long
telldir (DIR *dirp)
{
return dirp->dd_loc;
}
static void
free_dircontents (struct _dircontents *dp)
{
struct _dircontents *odp;
while (dp)
{
if (dp->_d_entry)
free (dp->_d_entry);
dp = (odp = dp)->_d_next;
free (odp);
}
}
#ifdef TEST
void main (int argc, char *argv[]);
void
main (int argc, char *argv[])
{
static DIR *directory;
struct direct *entry = NULL;
char *name = "";
if (argc > 1)
name = argv[1];
directory = opendir (name);
if (!directory)
{
fprintf (stderr, "can't open directory `%s'.\n", name);
exit (2);
}
while (entry = readdir (directory))
printf ("> %s\n", entry->d_name);
printf ("done.\n");
}
#endif