/* read_dir.c 25.7             */
/* From Amiga C for Beginners */
/* by Abacus                  */

#include <libraries/dos.h>

struct FileInfoBlock fi;

main(argc, argv)
int argc;
char *argv[];
{
   long lock;
   int error;
   char filepath[100];

   if(argc == 2)   /* parameter present ? */
      strcpy(filepath, argv[1]);
   else
      strcpy(filepath, "sys:");

   lock = Lock(filepath, ACCESS_READ);
   printf("Lock value %d\n", lock);
   if(!lock)
      {
         printf("No Lock! ERROR!\n");
         exit(FALSE);
      }

   if(Examine(lock, &fi))  /* First call successful? */
      do
        output(); /* Return value not of interest now */
      while(ExNext(lock, &fi)); /* until error occurs */

   error = IoErr();   /* What Error? */
   if(error != ERROR_NO_MORE_ENTRIES) /* "real" Error! */
      printf("Error %d occurred!\n", error);

   exit(TRUE);
}

output()
{
   if(!*fi.fib_FileName) /* strlen = 0 */
     {
       printf("Empty!\n");
        /* for example Root-directory of RAM Disk */
       return(0); /* That's directory without name */
     }
   if(fi.fib_DirEntryType > 0)
     printf("Directory name");
   else
     printf("Filename      ");

   printf(": >%20s< RWXD %lx bytes: %-6ld Blocks %-4ld\n",
          fi.fib_FileName, fi.fib_Protection,
          fi.fib_Size, fi.fib_NumBlocks);

   if(*fi.fib_Comment) /* If comment present, output! */
      printf("Comment: >%s<\n", fi.fib_Comment);
   return(fi.fib_DirEntryType > 0); /*Return File Type */
}

