#include "cdir.h"

#ifndef _WIN32

DIR *curr_dir;
struct dirent *de;

DIR* open_dir(char *name){
  return curr_dir = opendir(name);
}   
 
char* next_fname(){
  de = readdir(curr_dir);
  if (!de) return 0;
  return de->d_name;
} 

#else

#include <string.h>
#include <io.h>

char *filespec;
struct _finddata_t fileinfo;
int handle, error = 0;

int open_dir(char *name){
  char *filespec = malloc(strlen(name)+3);
  strcpy(filespec, name);
  strcat(filespec, "/*");
  handle = _findfirst(filespec, &fileinfo);
  if (handle == -1) error = 1;
  free(filespec);
}

char* next_fname(){
  char* result;
  if (error) return 0;
  result = _strdup(fileinfo.name);
  error = _findnext(handle, &fileinfo);
  return result;
}

#endif
