/*

  ListDirs - Example of how to list directiories under the current
  directory.

*/
#include <vo_stdio.h>
#include <volink.h>     /* Linker directives like DLLENTRY */
#include <apploader.h>  /* RunLibraryFunction etc */
#include <string.h>
#include <stdlib.h>
#include <audio.h>
#include <consolestate.h>
#include <saturate.h>
#include <vo_fat.h>
#include <kernel.h>

#define NAMELENGTH 80

int ListDirs(void) {
  ioresult retCode = S_ERROR;
  static char filename[NAMELENGTH];
  FILE *fp = NULL;

  if (!(fp = fopen(currentDirectory, "s"))) {
    SysError("Couldn't open %s", currentDirectory);
    goto finally;
  }

  if (FatFindFirst(fp, currentDirectory+2, filename, NAMELENGTH) == S_OK) {
      printf("Entries in %s \"%s\"\n",
	     fp->dev->Identify(fp->dev, NULL, 0), currentDirectory);
      do {
	if (!(fp->ungetc_buffer & __ATTR_VOLUMEID)) {
	  char *typ = (fp->ungetc_buffer & __ATTR_DIRECTORY) ? "DIR " : "file";
	  printf("%s, attr: %02x, short: %-12s, long: %s\n",
		 typ, fp->ungetc_buffer, fp->extraInfo, filename);
	}
      } while (S_OK == FatFindNext(fp, filename, NAMELENGTH));
  }

  retCode = S_OK;
 finally:
  if (fp) {
    fclose(fp);
    fp = NULL;
  }
  return retCode;
}



ioresult main(char *parameters) {
  ListDirs();
 finally:
  return S_OK;
}
