/* List Directory Command by Dave Haynie */
/* Downloaded from the "Well" by Fred Fish */
 
 /* This command is designed to get a simple AmigaDOS directory and
    display it to the screen.  Its also an example of making AmigaDOS 
    filing calls.  Let's see if any of this stuff works.  */
 
 #include <stdio.h>
 #include <ctype.h>
 #include <libraries/dosextens.h>
 
 #define LL 80
 struct FileLock *Lock();  /* Lock doesn't seem declared yet. */
 
 main(argc,argv)
 int argc;
 char *argv[];
 {
    char *dirname;             /* Selected AmigaDOS directory */
    struct FileLock *dir;      /* Locked AmigaDOS directory */
    struct FileInfoBlock *fb;  /* File Info from locked directory */
    BOOL fail;                 /* Directory Access Failure */
    UBYTE count;               /* Line length count */
 
    if (argc == 1)
       dirname = "";
    else if (argc == 2)
       dirname = argv[1];
    else {
       printf("Usage: Ld [dirname]\n");
       Exit();
    }
    fb = (struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock),0);
    dir = Lock(dirname,ACCESS_READ);
    fail = (dir == NULL) | !Examine(dir,fb);
    if (fail) {
       printf("Invalid directory '%s' requested\n",dirname);
       FreeMem(fb,sizeof(struct FileInfoBlock));
       Exit();
    }
    printf("\033[7m\033[3m%s\033[0m\n",fb->fib_FileName);
    ExNext(dir,fb);
    count = 0;
    while (IoErr() != ERROR_NO_MORE_ENTRIES) {
       if (fb->fib_DirEntryType > 0)
          printf("\033[2m%-20s\033[0m",fb->fib_FileName);
       else
          printf("%-20s",fb->fib_FileName);
       count++;
       if (count == 3) {
          printf("\n");
          count = 0;
       }
       ExNext(dir,fb);
    }
    if (count != 0) printf("\n");
    FreeMem(fb,sizeof(struct FileInfoBlock));
    Exit();
 }
