/* IconDir.c              */
/* Runs from work bench  Compiled with Manx 3.4b 16 bit */
/* click on disk icon or drawer icon to see list of files in the selected drawer */

#include <workbench/startup.h>
#include <libraries/dosextens.h>
#include <functions.h>

#define LL 80              /* lines across   */
#define HNUM 4            /* Names per line */
#define VNUM  (8 *HNUM)   /* Names per screen */
BOOL cli = TRUE;
extern struct WBStartup *WBenchMsg;
struct FileHandle *win = NL;


UBYTE go_on(f)
struct FileHandle *f;
{
 char dummy;

 Write(f,"\n\2337mType RETURN to Continue\2330m",30L);
 Read(f,&dummy,1L);
 Write(f,"\233H\233J",4L);
 return(UBYTE) 0;
}

struct WBArg *arg;

main(argc,argv)
int argc; unsigned char *argv[];
{
 struct FileLock *oldlock = NL;
 int x,myargc = 0;
 char *malloc(),*s,*p,wname[256];

                                        /*   Open Libraries       */
 /* check if invoked from work bench or cli */
 if(argc == 0) {      /* if from work bench */
     strcpy(wname,"CON:0/0/640/95/");
     strcat(wname,"    IconDir (c) 1987 Ron Shaw");
     if(!(win = Open(wname,MODE_OLDFILE))) exit();
	 myargc = 0;
     cli = FALSE;
     argc = (int) WBenchMsg->sm_NumArgs;   /* # of wb arguments */
	 arg = WBenchMsg->sm_ArgList;
	 /* Ptr to standard wb argument list */
	 for(x = 0; x < argc; x++, arg++) {
		  /* do stuff to this directory */
          oldlock = (struct FileLock *) CurrentDir(arg->wa_Lock);
		  if(x > 0) dodir(arg->wa_Lock);   /* skip program directory */
	      myargc++;
      }
	  if(x == 1 ) {
		  arg--;
          oldlock = (struct FileLock *) CurrentDir(arg->wa_Lock);
          dodir(arg->wa_Lock);   /* program directory */
#ifdef RTS
	      Write(win,"No ITems Clicked\n",17L);
#endif
      }
 } else {
     puts("Only Runs From Workbench");       /* loaded from cli */
     puts("Select Drawers to View Files");       /* loaded from cli */
     puts("Click on IconDir to display FIles in these drawers");
	 puts("(c) 1987 Ronald T. Shaw sr");

 }
 if(win) Close(win);
 Exit(RETURN_ERROR);
}

dodir(dir)
struct FileLock *dir;
{
  struct FileLock *Lock();
  struct FileInfoBlock *FIB;
  UBYTE count = 0;                    /* line count */
  long len;
  BOOL z = FALSE;

  if((FIB=AllocMem((long)sizeof(struct FileInfoBlock),MEMF_PUBLIC))==0) {
da:   Write(win,"Disk Access Error",18L); goto sz;
  }
  if((Examine(dir,FIB))==0) goto da; 
  Write(win,FIB->fib_FileName,(long) strlen(FIB->fib_FileName));
  Write(win,"(Dir)\n",6L);
#ifdef RTS
  if(FIB->fib_Comment) {          /* check for filenote */
      Write(win,FIB->fib_Comment,(long) strlen(FIB->fib_Comment));
      Write(win,"\n",1L);
  }
#endif
  while(ExNext(dir,FIB) != NL || IoErr() != ERROR_NO_MORE_ENTRIES) {
      if(FIB->fib_DirEntryType > 0) Write(win,"\2333m",3L); /* dir */
	  len = (long)strlen(FIB->fib_FileName);
      Write(win,FIB->fib_FileName,(long)strlen(FIB->fib_FileName));
	  Write(win,"\2330m                      ",22L - len);
      if(++count % HNUM == 0) {
	     Write(win,"\n",1L);
		 if(count == VNUM) count = go_on(win);
      }
  }
  if(count != NL) {
      if(count % HNUM != 0) Write(win,"\n",1L);
	  go_on(win);
  }
  z = TRUE;
sz: if(dir) UnLock(dir);
  if(FIB) FreeMem(FIB,(long) sizeof(struct FileInfoBlock));
  return(z); 
}

