
/***********************************************************/
/*                                                         */
/* Dirlist                                                 */
/* -------------------------------      ------------------ */
/* last changes - implement remember structures            */
/*                                                         */
/***********************************************************/

#include <libraries/dos.h>
#include <exec/memory.h>

#include <inline/dos.h>
#include <inline/exec.h>

#include <stdio.h>

#define MAXFILELEN      30      /* max length of filename */
#define MAXFILES        256     /* max number of files */

#define MEMTYPE         MEMF_PUBLIC | MEMF_CLEAR

/* external definitions */
extern struct Remember  *RememberFilesPtr;

/* external functions */
extern int strindex();

/* function prototypes */
UBYTE *AllocRemember();
/*
 * int dirlist(void)
 *
 * list .html files in current directory, return number found
 *
*/

int dirlist(void)

{
    struct FileLock         *lock;      /* Declare a FileInfoBlock */
    struct FileInfoBlock    *fib_ptr;   /* pointer called fib_ptr. */
                                 
    char dirname[MAXFILELEN];           /* current directory */
    extern char *filesptr[MAXFILES];    /* list of filenames */
    int nfiles = 0;                     /* number of .html files */

    /* Get current directory */
    if (!GetCurrentDirName(dirname,MAXFILELEN)) {
        printf("Error finding current directory!\n");
        exit();
    }

    /* Allocate enough memory for a FileInfoBlock structure: */
    fib_ptr = (struct FileInfoBlock *)
              AllocMem( sizeof( struct FileInfoBlock ), MEMTYPE );

    /* Check if we have allocated the memory successfully: */
    if( fib_ptr == NULL )
    {
      printf("Not enough memory!\n");
      exit();
    };

    /* Try to lock the current directory */
    lock = (struct FileLock *) Lock( dirname, SHARED_LOCK );

    /* Colud we lock it */
    if( lock == NULL )
    {
      printf("Could not lock the current directory!\n");

      /* Deallocate the memory we have allocated: */
      FreeMem( fib_ptr, sizeof( struct FileInfoBlock ) );
      exit();
    }

    /* Try to examine the directory/device */
    if( Examine( lock, fib_ptr ) ) {

        /* As long as we can examine files/directories we continue: */
        while( ExNext( lock, fib_ptr ) )
        {
          /* If it is a file we need to check it out */
          if( fib_ptr->fib_DirEntryType < 0 )

            /* check if entry is an .html or .htm file */
              if (strindex(fib_ptr->fib_FileName, ".htm", 0) > 0) {

                  /* found .htm(l) file: add to filesptr */
                  if (!(filesptr[nfiles] = AllocRemember(&RememberFilesPtr, (ULONG)strlen(fib_ptr->fib_FileName), MEMTYPE))) {
                      printf("Out of Memory!\n");
                      exit();
                  }

                  strcpy(filesptr[nfiles++], fib_ptr->fib_FileName);

              }

        }

        /* Check what went wrong. If it was not because there were no more */
        /* files in the directory (ERROR_NO_MORE_ENTRIES), something       */
        /* terrible has happened!                                          */
        if( IoErr() != ERROR_NO_MORE_ENTRIES )
          printf("ERROR WHILE READING!!!\n");

    }
    else
      printf("Could not examine %s!\n", dirname );

    /* Unlock the file: */
    UnLock( lock );

    /* Deallocate the memory we have allocated: */
    FreeMem( fib_ptr, sizeof( struct FileInfoBlock ) );

    /* return number of html files found */
    return (nfiles);
}


