/* DIRectory UTILity II -
        This program is in the public domain. Nicotra source downloaded
        from local BBS and modified by Jobusch. Word is Nicotra recently
        released an improved version (1.3). Hope his code is better than
        it was in 1.0. NO guarentees...if you blow up a disk with this
        program, it's your own damn fault.

        Original Version (Directory Utilitity 1.0) by Chris Nicotra

        10/6/86  Rich Medved, CIS 72366,540.  Added TYPE function. Added
        support for df2:.  Cleaned up some of the warning msg. from the
        compilier.  Come on guys, let's start using revision numbers.
        Seeing "this is a new version" on the bds. is getting confusing.
        For a start, this is now version 1.4

        Revisions/Enhancements/Fixes by David L. Jobusch
                                   ...umn-cs!isucs1!jobusch

       */

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <stdio.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <workbench/startup.h>

#define INTUITION                0x00000001
#define GRAPHICS                 0x00000002
#define WINDOW                   0x00000004
#define INITDIRLOCKOK            0x00000008
#define  BRK                     0x3
#define  ESC                     0x1b
#ifdef SHOW
#define SHOWDIR "sys:c"         /* What directory its in        */
#define SHOWCMD "show"          /* Name of the actual program   */
#define SHOWNAMELENGTH 10       /* Make changes? Set this!      */
#define RAMSHOW "ram:DIRUTIL.SHOW" /* Where it will end up      */
#define RAMSHOWLEN  16          /* CHANGE THIS IF YOU CHANGE NAME!*/
#endif

#define FBSIZE  (long)(sizeof (struct FileInfoBlock))
#define MAXFILE 100             /* Maximum files to look at in directory */
#define MAXFNAME 10             /* Maximum files to show at once         */
#define MAXCHAR  30             /* Max file name length                  */
#define MAXCD    255            /* Max current dir name length           */

#define SLIDE_GADGET    0
#define ERR_GADGET      3
#define CURDIR_GADGET   4       /* Gadget labels                         */
#define CPDIR_GADGET    5
#define DF0_GADGET      6
#define DF1_GADGET      7
#define DF2_GADGET      8
#define HD0_GADGET      9
#define RAM_GADGET      10
#define ALL_GADGET      11
#define CLEAR_GADGET    12
#define COPY_GADGET     13
#define DELETE_GADGET   14
#define RENAME_GADGET   15
#define GETDIR_GADGET   16
#define MAKEDIR_GADGET  17
#define DELETEDIR_GADGET 18
#define PARENT_GADGET   19
#define ROOT_GADGET     20
#define LIST_GADGET     21
#define TYPE_GADGET     22
#define BUT_FIRST DF0_GADGET

#ifdef SHOW
#define SHOW_GADGET     23
#define FIRST_GADGET    But_gad[17]
#define KILL_SHOW       But_gad[17]
#define BUT_LAST        SHOW_GADGET
#endif
#ifndef SHOW
#define FIRST_GADGET    But_gad[16]
#define BUT_LAST        TYPE_GADGET
#endif

#define WinIDCMP  CLOSEWINDOW | REFRESHWINDOW | GADGETUP | MOUSEBUTTONS


extern struct FileHandle  *Open();
extern APTR AllocMem();
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct IntuiMessage *message;
struct RastPort *rp;
struct Window *w;

/************************************************************************/
/*              Source/Destination string gadgets                       */
/************************************************************************/
USHORT str_pairs [] = { 0,0,  590,0,  590,9,  0,9,  0,0,};
struct Border str_border =
{
    -1,-1,              /* Leftedge, Topedge */
     1, 1, JAM1,        /* FrontPen, BackPen, Drawmode */
     5,                 /* Number of Pairs */
    (APTR) &str_pairs,  /* XY, Pointer to XY pairs */
    NULL,               /* More borders */
};

/* Error display */
char err_buf[MAXCD+1];
struct StringInfo err_str = 
{ &err_buf, NULL, 0, MAXCD+1,   0, 0, 0, 0, 2,0, NULL, 0, NULL, };

struct Gadget Err_gad =
{
        NULL, /* last in the list of gadgets */
        21,122,
        588, 9,
        GADGHCOMP, GADGIMMEDIATE | RELVERIFY | STRINGCENTER,
        STRGADGET,
        (APTR) &str_border,
        NULL,
        NULL,   /* Intuitext pointer */
        0,
        &err_str,
        ERR_GADGET,
        NULL,
};

struct IntuiText Curdir_text = {1,0,JAM1, -10,0,NULL,"S",NULL,};
char cd_buf[MAXCD+1];
struct StringInfo Curdir_str = 
{ &cd_buf, NULL, 0, MAXCD+1,   0, 0, 0, 0, 2,0, NULL, 0, NULL, };
struct Gadget Curdir_gad =
{
        &Err_gad,
        21,100,
        588, 9,
        GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
        STRGADGET,
        (APTR) &str_border,
        NULL,
        &Curdir_text,
        0,
        &Curdir_str,
        CURDIR_GADGET,
        NULL,
};

/* Destination definitions */
char cp_buf[MAXCD+1];
struct IntuiText Cpdir_text = {1,0,JAM1,-10,0,NULL,"D",NULL,};
struct StringInfo Cpdir_str = 
{ &cp_buf, NULL, 0, MAXCD+1,   0, 0, 0, 0, 2,0, NULL, 0, NULL, };

struct Gadget Cpdir_gad =
{
        &Curdir_gad,
        21,111,
        588, 9,
        GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
        STRGADGET,
        (APTR) &str_border,
        NULL,
        &Cpdir_text,
        0,
        &Cpdir_str,
        CPDIR_GADGET,
        NULL,
};

/************************************************************************/
/*              File Select Gadget Declarations                         */
/************************************************************************/
struct Image slide_img;
struct PropInfo slide_prop;
struct Gadget Slide_gad =
{
    &Cpdir_gad,         /* Next gadget */
    20, 14,             /* Left, Top edge */
    20, 84,             /* Width Hieght */
    GADGHNONE,          /* Flags */
    GADGIMMEDIATE       /* More flags */
    | RELVERIFY,
    PROPGADGET,         /* Gadget type */
    (APTR)&slide_img,   /* Border definition */
    NULL,               /* Select render */
    NULL,               /* Gadget text */
    0,                  /* MutualExclude */
    (APTR) &slide_prop, /* Special Info - proportional specification */
    SLIDE_GADGET,
    NULL,
};

/* Button Gadget IDs */

struct IntuiText But_text[] =
{
        3,2,JAM2,0,0,NULL,"  df0:   ",NULL,
        3,2,JAM2,0,0,NULL,"  df1:   ",NULL,
        3,2,JAM2,0,0,NULL,"  df2:   ",NULL,
        3,2,JAM2,0,0,NULL,"  dh0:   ",NULL,
        3,2,JAM2,0,0,NULL,"  ram:   ",NULL,
        3,2,JAM2,0,0,NULL,"   ALL   ",NULL,
        3,2,JAM2,0,0,NULL,"  CLEAR  ",NULL,
        3,2,JAM2,0,0,NULL,"  COPY   ",NULL,
        3,2,JAM2,0,0,NULL," DELETE  ",NULL,
        3,2,JAM2,0,0,NULL," RENAME  ",NULL,
        3,2,JAM2,0,0,NULL," GETDIR  ",NULL,
        3,2,JAM2,0,0,NULL," MAKEDIR ",NULL,
        3,2,JAM2,0,0,NULL,"DELETEDIR",NULL,
        3,2,JAM2,0,0,NULL," PARENT  ",NULL,
        3,2,JAM2,0,0,NULL,"  ROOT   ",NULL,
        3,2,JAM2,0,0,NULL,"  LIST   ",NULL,
        3,2,JAM2,0,0,NULL,"  TYPE   ",NULL,
#ifdef SHOW
        3,2,JAM2,0,0,NULL,"SHOW ILBM",NULL,
#endif
};

struct Gadget But_gad[] =
{
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,DF0_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,DF1_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,DF2_GADGET,NULL,
     NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,HD0_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,RAM_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,ALL_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,CLEAR_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,COPY_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,DELETE_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,RENAME_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL, GETDIR_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,MAKEDIR_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,DELETEDIR_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,PARENT_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,ROOT_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,LIST_GADGET,NULL,
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,TYPE_GADGET,NULL,
 #ifdef SHOW
    NULL,5,174,72,8,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,BOOLGADGET,
            NULL,NULL,NULL,NULL,NULL,SHOW_GADGET,NULL,
#endif
};

/* BORDER DEFINITION FOR FILE SELECT AREA */
USHORT Fil_pairs [] = { 40,15,  356,15,  356,98, 40,98, 40,15, };
struct Border Fil_border =
{
        -1,-1,              /* Leftedge, Topedge */
        1, 0, JAM1,        /* FrontPen, BackPen, Drawmode */
        5,                 /* Number of Pairs */
        (APTR) Fil_pairs,   /* XY, Pointer to XY pairs */
        NULL,               /* More borders */
};


struct NewWindow nw =
{
        0,0,                    /* Start position */
        640,134,                /* Width, height */
        0,1,                    /* Detail, block pens */
        WinIDCMP,               /* IDCMP flags */
        WINDOWSIZING
        | WINDOWDEPTH
        | WINDOWDRAG
        | REPORTMOUSE
        | WINDOWCLOSE
        | ACTIVATE
        | RMBTRAP
        | SMART_REFRESH,
        &FIRST_GADGET,      /* First gadget in the list */
        NULL,               /* User checkmark */
        "DirUtil II ver. 1.4: original author - Chris Nicotra",
        NULL,   
        NULL,               /* Pointer to superbitmap */
        100, 40, 640, 125,      /* Now sizeable */
        WBENCHSCREEN,       /* Using the Workbench screen */
};

/* The following structure is used to hold the directory entries */
struct DirTable
{
        char  dt_fname[MAXCHAR];        /* File name */
        short dt_select;                /* Select flag */
        short dt_dir;                   /* Directory flag */
        long  dt_size;                  /* File size */
};

struct DirTable dirtable[MAXFILE+1];
struct FileLock *InitialCurrentDirLock;
struct FileLock *CurrentDirLock;
struct FileLock *RAMSHOWLOCK;
int cmp_dt();
int numdir;
int cur_index;
int NILFD = 0;
unsigned int mask = 0;
char curdir[MAXCD+1],requestedcd[MAXCD+1];
void copy(), new_dir(), sel_file(), par_dir(), Print_to_screen();
struct FileLock *oldlock;  /* lock to old directory */

main(argc,argv)
char *argv[];
{
        struct Gadget *gad_ptr;
        struct IntuiText *text_ptr;
        ULONG MessageClass;
        USHORT code;
        int i;
#ifdef SHOW
        char SHOWNAME[SHOWNAMELENGTH];
#endif

        if(argc>2) {
                printf("usage: %s [SHOW]\n",*argv);
                exit(1);
        }

#ifdef SHOW
        if((argc==0)||((argc==2)&&(!strcmp(argv[1],"SHOW")))) {
        /*  WBENCH      CLI AND SHOW OPTION                                                                     */
                if(!(NILFD = Open("nil:",MODE_OLDFILE))) {
                        puts("DirUtil FATAL: Can't open NIL: in main\n");
                        exit(2);
                }
        /* If SHOW option was given, the default show command is loaded */
        /* into the RAMdisk. It will will show up in DIRUTIL directory  */
        /* listings of RAM: but it cant be deleted while you are        */
        /* still in DIRUTIL. Create the path name for your show cmd,    */
        /* and put a copy of it into RAMDisk. Then rename it to RAMSHOW */
        /* Sure its a kludge, but this way I didn't have to change copy */
        /* or do an Execute. If for some reason you are running two     */
        /* DirUtil's, the Rename will fail, but there will still be     */
        /* a RAM:DIRUTIL.SHOW file...I hope.                            */
                sprintf(SHOWNAME,"%s/%s",SHOWDIR,SHOWCMD);
                copy(SHOWNAME,"ram:");
                sprintf(SHOWNAME,"%s%s","ram:",SHOWCMD);
                Rename(SHOWNAME,RAMSHOW);
                if(!(RAMSHOWLOCK = Lock(RAMSHOW,ACCESS_READ))) {
                        printf("DirUtil FATAL: Can't get Lock on %s\n",RAMSHOW);                        Close(NILFD);
                        exit(3);
                }
        } 
#endif

        *cp_buf = 0;
        strcpy(curdir,"ram:");
        strcpy(requestedcd,curdir);
        if(!(CurrentDirLock = Lock(curdir,ACCESS_READ))) {
                printf("DirUtil FATAL: Could not lock RAMdisk!\n");
                close_things();
                exit(4);
        }
        mask |= INITDIRLOCKOK;
        InitialCurrentDirLock = CurrentDir(CurrentDirLock);

        /* Gadget initialization */
        slide_prop.Flags = FREEVERT | AUTOKNOB;
        slide_prop.VertBody = 0;
        slide_prop.VertPot = 0x8000;

        /* Setup the button gadgets */
        for (i=0;i<(BUT_LAST-BUT_FIRST)+1;i++) {
                gad_ptr = &But_gad[i];
                text_ptr = &But_text[i];

                gad_ptr->LeftEdge = ((i%3) * 90) + 360;
                gad_ptr->TopEdge = ((i/3) * 15) + 14;

                if (i)
                    gad_ptr->NextGadget = &But_gad[i-1];
                else
                    gad_ptr->NextGadget = &Slide_gad;

                gad_ptr->GadgetText = text_ptr;

                text_ptr->LeftEdge = 0;
                text_ptr->TopEdge = 0;
        }

    if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0))) {
        puts("DirUtil FATAL : no graphics library!!!\n");
        close_things();
        exit(5);
    }
    mask |= GRAPHICS;

    if (!(IntuitionBase = (struct IntuitionBase *)
        OpenLibrary("intuition.library",0))) {
        puts("DirUtil FATAL : no intuition here!!\n");
        close_things();
        exit(6);
    }
    mask |= INTUITION;

    if (!(w = (struct Window *) OpenWindow(&nw) )) {
        puts("DirUtil FATAL : Could not open the window\n");
        close_things();
        exit(7);
    }
    mask |= WINDOW;

    /************ INITIALIZATION BEFORE main() loop *****************/
        rp = w->RPort;
        DrawBorder(rp,&Fil_border,0,0);

        getdir(curdir);
        new_dir();

        display_error("O.K.",0);
        RefreshGadgets(&FIRST_GADGET,w,NULL);
#ifdef SHOW
        if(!NILFD) OffGadget(&KILL_SHOW,w,NULL);
#endif

        for (;;) {
                WaitPort(w->UserPort); 
                message = (struct IntuiMessage *) GetMsg(w->UserPort);
                MessageClass = message->Class;
                code = message->Code;
                ReplyMsg(message);
                switch (MessageClass) {
                        case GADGETUP:
                        case GADGETDOWN:
                                display_error("O.K.",0);
                                do_gadgets(message,w);
                                break;

                        case REFRESHWINDOW:
                                BeginRefresh(w); EndRefresh(w);
                                dis_files(cur_index);
                                DrawBorder(rp,&Fil_border,0,0);
                                break;

                        case CLOSEWINDOW:
                                close_things();
                                exit(0);
                                break;

                        case MOUSEBUTTONS:
                                if (code == SELECTDOWN)
                                    sel_file();
                                break;

                        default:
                                break;
                } /*end switch*/
        } /*end for*/
}

do_gadgets(mes,win)
struct IntuiMessage *mes;
struct Window *win;
{
        struct FileLock *MakeDirLock;
        struct Gadget *igad;
        int gadgid;                 /* ID Code identifying which gadget */
        int i;

        igad = (struct Gadget *) mes->IAddress;
        gadgid = igad->GadgetID;
        switch (gadgid) {
                case CURDIR_GADGET:
                        ModifyIDCMP(w,0);
                        nullterm(cd_buf,MAXCD);
                        Curdir_str.NumChars = strlen(cd_buf);
                        Curdir_str.BufferPos = strlen(cd_buf);
                        getdir(cd_buf);
                        new_dir();
                        ModifyIDCMP(w,WinIDCMP);
                        break;

                case ERR_GADGET:
                        nullterm(err_buf,MAXCD);
                        err_str.NumChars = strlen(err_buf);
                        err_str.BufferPos = strlen(err_buf);
                        break;

                case CPDIR_GADGET:      
                        nullterm(cp_buf,MAXCD);
                        Cpdir_str.NumChars = strlen(cp_buf);
                        Cpdir_str.BufferPos = strlen(cp_buf);
                        break;

                case SLIDE_GADGET:
                        rdis_files();
                        break;

                case ROOT_GADGET:
                case PARENT_GADGET:
                        par_dir(gadgid);
                        break;

#ifdef SHOW
                case SHOW_GADGET:
                        display_error("SHOW : Now showing selected pictures.",0);
#endif
                case DELETE_GADGET:
                case COPY_GADGET:
                case LIST_GADGET:
                case RENAME_GADGET:
                case TYPE_GADGET:
                case DELETEDIR_GADGET:
                        action_on_selected(gadgid);
                        break;

                case MAKEDIR_GADGET:
                /* Attempt to create the named directory. Note that since
                   the CurrentDir is whatever is in the CurrentDir gadget,
                   if you don't give a complete path name, it will assume
                   to create the directory in the current directory.
                   Also, 1.1 CreateDir does not care if you try to create
                   a directory entry on top of a file of the same name.
                   Code around this...                                 */
                        if(MakeDirLock = Lock(cp_buf,ACCESS_READ)) {
                                UnLock(MakeDirLock);
                                display_error("MAKEDIR ERROR : DIRECTORY / FILE ALREADY EXISTS. " ,IoErr());
                                break;
                        } 
                        if (!(MakeDirLock = CreateDir(cp_buf))) {
                                display_error("MAKEDIR ERROR : CreateDir FAILED. ", IoErr());
                                break;
                        }
                        UnLock(MakeDirLock);
                        getdir(curdir);
                        new_dir();
                        break;

                case DF0_GADGET:
                        getdir("df0:"); new_dir(); break;
                case DF1_GADGET:
                        getdir("df1:"); new_dir(); break;
                case DF2_GADGET:
                        getdir("df2:"); new_dir(); break;
                case RAM_GADGET:
                        getdir("ram:"); new_dir(); break;
                case HD0_GADGET:
                        getdir("dh0:"); new_dir(); break;

                case GETDIR_GADGET: 
                        getdir(requestedcd);
                        new_dir();
                        break;

                case CLEAR_GADGET:
                        for (i=0;i<numdir;i++)
                            dirtable[i].dt_select = 0;
                        dis_files(cur_index);
                        break;

                case ALL_GADGET:
                        for (i=0;i<numdir;i++)
                                if (!dirtable[i].dt_dir)
                                        dirtable[i].dt_select = 1;
                        dis_files(cur_index);
                        break;

                default:
                        break;
        }
}


getdir(DirToGet) 
char *DirToGet;
{
        struct FileLock *filelock;
        struct FileInfoBlock *fb;
        char EffectiveDir[MAXCD+1];

        if (!(*DirToGet))
                strcpy(EffectiveDir,":");
        else
                strcpy(EffectiveDir,DirToGet);

        /* If we cant open requested dir, don't change */
TRYAGAIN:
        if (!(filelock = Lock(EffectiveDir,ACCESS_READ))) {
                strcpy(EffectiveDir,curdir);
                display_error("ERROR : CAN'T LOCK REQUESTED DIRECTORY. ", IoErr());
                if(!(filelock = Lock(EffectiveDir,ACCESS_READ))) {
                        display_error("FATAL : CAN'T LOCK CURRENT DIR. SWITCHING TO RAM: ", IoErr());
                        strcpy(EffectiveDir,"RAM:");
                        goto TRYAGAIN;
                }
        }
        /* Allocate the file info block */
        fb = AllocMem(FBSIZE,MEMF_CLEAR | MEMF_PUBLIC);
        if (!Examine(filelock,fb)) {
                FreeMem(fb,FBSIZE);
                UnLock(filelock);
                display_error("FATAL : CAN'T READ INFO BLOCK. SWITCHING TO RAM: ",IoErr());
                strcpy(EffectiveDir,"RAM:");
                goto TRYAGAIN;
        }
        if(fb->fib_DirEntryType < 0) {  /* ITS A FILE- NOT A DIRECTORY  */
                strcpy(EffectiveDir,curdir);/* Forget request, use current*/
                FreeMem(fb,FBSIZE);
                UnLock(filelock);
                display_error("WARNING : DIRECTORY REQUESTED WAS REGULAR FILE - NO CHANGES. ", IoErr());
                goto TRYAGAIN;
        }

        /* Copy directory string into gadget structure */
        strcpy(cd_buf,EffectiveDir);
        Curdir_str.NumChars = strlen(EffectiveDir);
        Curdir_str.BufferPos = strlen(EffectiveDir);
        strcpy(curdir,EffectiveDir);
        strcpy(requestedcd,EffectiveDir);
        RefreshGadgets(&Cpdir_gad,w,NULL);

        numdir = 0;
        cur_index = 0;

        /* Loop through each of the files in this directory */
        while (ExNext(filelock,fb)) {
                if (fb->fib_DirEntryType < 0)
                        dirtable[numdir].dt_dir = 0;
                else
                    dirtable[numdir].dt_dir = -1;
                dirtable[numdir].dt_select = 0;
                dirtable[numdir].dt_size = fb->fib_Size;
                strncpy(dirtable[numdir].dt_fname,fb->fib_FileName,MAXCHAR);
                nullterm(dirtable[numdir].dt_fname,MAXCHAR);

                if (++numdir == MAXFILE)
                    break;
        }

        /* Sort the table */
        qsort(dirtable,numdir,(sizeof (struct DirTable)),cmp_dt); 
        FreeMem(fb,FBSIZE);
        UnLock(CurrentDirLock);         /* Release old CD lock      */
        UnLock(filelock);               /* Release new request lock */
        CurrentDirLock = Lock(EffectiveDir);
        CurrentDir(CurrentDirLock);     /* MAKE IT THE CURRENT DIRECTORY */
}



rdis_files()
{
    USHORT Vpot;
        int numd;
        int pos;

        if (numdir > MAXFNAME) {
        Vpot = slide_prop.VertPot;
                Vpot >>= 1;
                Vpot &= 0x7fff;
                numd = numdir - MAXFNAME;
                pos = ((float) Vpot / (float) 0x7fff) * numd;
                cur_index = pos;
                dis_files(pos);
        }
}

/**********************************************************************
*    new_dir - New directory
*    Purpose: To display a new directory and initialize the slide gadget.
**********************************************************************/
void new_dir()
{
        USHORT Vbody;

        if (numdir <= MAXFNAME) 
                Vbody = 0xffff;
        else {
            Vbody = ((float) 0x7fff) / (((float) numdir) / ((float) MAXFNAME));
                Vbody <<= 1;
        }
        cur_index = 0;
        ModifyProp(&Slide_gad,w,NULL,slide_prop.Flags,0,0,0,Vbody);
        dis_files(0);
        return;
}

/**********************************************************************
*    dis_files - Display files
*    Purpose:    To display a page of file names
*    Parameters: pos - The position in the file list
**********************************************************************/
dis_files(pos)
int pos;
{
        int i;

        for (i=0;i<MAXFNAME;i++)
                dis_name(i+pos,i);
}

/***********************************************************************
*    dis_name - Display a file name
*    Purpose: To display a file name
*    Parameters:
*        file - File index (into dirtable) or -1 for blank entry
*        pos - The position in the file list
**********************************************************************/
dis_name(file,pos)
int file;
int pos;
{
        static char file_name[MAXCHAR+8];
        static struct IntuiText file_text = {
            2,0,JAM2,45,15,NULL,(UBYTE *)file_name, NULL };

        /* Set the top position */
        file_text.TopEdge = (pos*8)+16;

        if (file == -1 || file >= numdir) {
                sprintf(file_name,"%-38.38s","");
                file_text.FrontPen = 2;
                file_text.BackPen = 2;  /* black */
        } else {
                if (dirtable[file].dt_dir) {
                        sprintf(file_name,"%-30.30s%-8.8s",dirtable[file].dt_fname,"");
                        if (dirtable[file].dt_select) {
                                file_text.FrontPen = 2; /* Black on Orange */
                                file_text.BackPen = 3;
                        } else {
                                file_text.FrontPen = 3; /* Orange on Black */
                                file_text.BackPen = 2;
                        }
                } else {
                        sprintf(file_name,"%-30.30s %7ld",dirtable[file].dt_fname,
                        dirtable[file].dt_size);
                        if (dirtable[file].dt_select) {
                                file_text.FrontPen = 2; /* Black on White */
                                file_text.BackPen = 1;
                        } else {
                                file_text.FrontPen = 1; /* White on Black  */
                                file_text.BackPen = 2;
                        }
                }
        }

        /* Display the text */
        PrintIText(rp,&file_text,0,0);
}

/**********************************************************************
*    sel_file - Select file
*
*    Purpose: To check if a file has been selected and the select
*        or unselect it
**********************************************************************/
void sel_file()
{
        int file,i;
        char *ptr;

        if (message->MouseX < 45 || message->MouseX > 351)
            return;
        if (message->MouseY < 16 || message->MouseY >= (MAXFNAME*8+16))
            return;
        file = message->MouseY - 16;
        file /= 8;
        if((!numdir) || (file > MAXFNAME) || ((file+cur_index) > numdir))
            return;

        /* Check to see if selection is a directory, or regular file    */
        /* For now, only allow one directory to be selected at a time   */
        if (dirtable[file+cur_index].dt_dir) {
                for(i=0;i<numdir;i++)
                        if((dirtable[i].dt_dir)&&(i != file+cur_index))
                                dirtable[i].dt_select = 0;
                ptr = curdir + strlen(curdir) - 1;
                if(dirtable[file+cur_index].dt_select ^= 1) 
                        sprintf(requestedcd,"%s%s%s",curdir,((*ptr == ':') ? "":"/"),
                        dirtable[file+cur_index].dt_fname);
                else
                        strcpy(requestedcd,curdir);
                dis_files(cur_index);
                return;
        }
        dirtable[file+cur_index].dt_select ^= 1;
        dis_name(file+cur_index,file);
}

action_on_selected(gadgid) /* Copy,Delete,ShowILBM,List,Type,DELETEDIR */
int gadgid;
{
        struct IntuiMessage *message;
        int i;
        char filename[MAXCD+MAXCHAR+2];
        char *ptr;
#ifdef SHOW
        char command[MAXCD+MAXCHAR+2+RAMSHOWLEN];/* See comments in #defines*/
#endif

        /* At each iteration, check for any IntuiMessages that would signal
           user wants to halt operation: GADGETUP(he hit a gadget) or
           MOUSEBUTTONS(button press on something we dont recognize) */

        ModifyIDCMP(w,GADGETUP | MOUSEBUTTONS);
        for (i=0;i<numdir;i++) {
                if(message = (struct IntuiMessage *) GetMsg(w->UserPort)) {
                        ReplyMsg(message);
                        display_error("INTERRUPT : OPERATION HALTED.",0);
                        break; 
                }

                if (dirtable[i].dt_select) {
                        ptr = curdir + strlen(curdir)-1;
                        if (*ptr == ':')
                                sprintf(filename,"%s%s",curdir,dirtable[i].dt_fname);
                        else
                                sprintf(filename,"%s/%s",curdir,dirtable[i].dt_fname);

                        switch (gadgid) {
                                case DELETE_GADGET:
                                        if(dirtable[i].dt_dir) {
                                                display_error("DELETE ERROR : FILE IS A DIRECTORY.",0);
                                                break;
                                        }
                                        if(!(DeleteFile(filename)))
                                        display_error("DELETE ERROR : COULD NOT DELETE REQUESTED FILE. ", IoErr());
                                        break;

                                case DELETEDIR_GADGET:
                                        if(!dirtable[i].dt_dir) {
                                                display_error("DELETEDIR ERROR : FILE IS NOT A DIRECTORY. ",0);
                                                break;
                                        }
                                        if(!(DeleteFile(filename)))
                                                display_error("DELETEDIR ERROR : COULD NOT DELETE REQUESTED DIRECTORY. ", IoErr());
                                        break;

                                case COPY_GADGET:
                                        copy(filename,cp_buf);
                                        break;

#ifdef SHOW
                                case SHOW_GADGET:
                                        if(NILFD) {
                                                sprintf(command,"%s %s",RAMSHOW,filename);
                                                Execute(command,0,NILFD);
                                                dirtable[i].dt_select = 0;
                                                dis_files(cur_index);
                                        }
                                        break;
#endif

                                case LIST_GADGET:
                                /* K L U D G E */
                                        ptr = &filename;
                                        while((*ptr != ':')&&(*ptr != '\0')) ptr++;
                                        printf("%s\n",ptr);
                                        dirtable[i].dt_select = 0;
                                        break;

                                case TYPE_GADGET:
                                  Print_to_screen(filename);
                                  dirtable[i].dt_select = 0;
                                  break;

                                case RENAME_GADGET:
                                /* NOTE: you must select the file to be renamed -
                                   this doesnt check cd_buf for a filename!     */
                                        if(!(Rename(filename,cp_buf))) 
                                                display_error("RENAME ERROR : CANNOT RENAME TO REQUESTED PATH. ", IoErr());
                                        break;

                                default:
                                break;
                        } /*switch*/
                        if(RENAME_GADGET == gadgid)
                                break; /* RENAME FIRST FILE SELECTED ONLY! */
                } /* if selected */
        } /* for all */

#ifdef SHOW
        if((SHOW_GADGET == gadgid)||(LIST_GADGET == gadgid)) {
#endif
#ifndef SHOW
        if(LIST_GADGET == gadgid) {
#endif
                dis_files(cur_index);
        } else {
                getdir(curdir);
                new_dir();
        }
        ModifyIDCMP(w, WinIDCMP);
}

void par_dir(gadgid) /* Parent and Root */
int gadgid;
{
        char *ptr;                 /* With this, we may change curdir  */

        ModifyIDCMP(w,0);
        ptr = curdir+strlen(curdir)-1;
        if(PARENT_GADGET == gadgid)
                for (;ptr >= curdir;ptr--) {
                        if (*ptr == ':') { 
                                *(ptr+1) = 0; 
                                break;
                        }
                        if (*ptr == '/') { 
                                *ptr = 0;
                                break;
                        }
                }
        else    /* Gadget was root. Scan back to :  */
                for (;ptr >= curdir;ptr--) 
                        if (*ptr == ':') {
                                *(ptr+1) = 0;
                                break;
                        }

        getdir(curdir);
        new_dir();
        ModifyIDCMP(w,WinIDCMP);
        return;
}

/* The following function is used to compare two directory entries */
cmp_dt(dt1,dt2)
struct DirTable *dt1,*dt2;
{
   char *ptr1,*ptr2;
   int c1,c2;

   for (ptr1=dt1->dt_fname,ptr2=dt2->dt_fname;*ptr1 && *ptr2;ptr1++,ptr2++) {
        c1 = *ptr1;
        c2 = *ptr2;

        if (c1 >= 'A' && c1 <= 'Z') {
                c1 -= 'A';
                c1 += 'a';
        }

        if (c2 >= 'A' && c2 <= 'Z') {
                c2 -= 'A';
                c2 += 'a';
        }

        if (c1 > c2)
                return (-1);

        if (c1 < c2)
                return (1);
   }

   if (*ptr1)
        return (-1);

   if (*ptr2)
        return (1);

   return (0);
}

void copy(s,pc)
char *s;
char *pc;
{
        struct FileHandle  *copyin;
        struct FileHandle  *copyout;
        int iosize;
        int actual;
        char *copybuf;
        char work[300];
        struct FileLock *fl;
        char *ptr;
        char *ptr1;

        if (!(copyin = Open(s,MODE_OLDFILE)))
                return;

        if (!*pc) {
                display_error("COPY ERROR : NO DESTINATION PATH GIVEN.",0);
                return;
        }

        if ((fl = Lock(pc,ACCESS_READ))) {
                UnLock(fl);
                ptr = strlen(pc) + pc - 1;
                for (ptr1=s+strlen(s)-1;ptr1 > s;ptr1--) {
                        if (*ptr1 == '/' || *ptr1 == ':')
                                break;
                }
                if (*ptr1 == '/' || *ptr1 == ':')
                        ptr1++;
                if (*ptr == ':')
                        sprintf(work,"%s%s",pc,ptr1);
                else
                    sprintf(work,"%s/%s",pc,ptr1);
        } else {
                display_error("COPY ERROR : ILLEGAL DESTINATION PATH. ", IoErr());
                Close(copyin);
                return;
        }

        if (!strcmp(work,s)) {
                display_error("COPY ERROR : CANNOT COPY FILE TO ITSELF. ",0);
                Close(copyin);
                return;
        }

        if (!(copyout = Open(work, MODE_NEWFILE))) {
                display_error("COPY ERROR : COPY CANNOT WRITE TO REQUESTED PATH. ", IoErr());
                Close(copyin);
                return;
        } else {
                /* Determine the length of the file */
                iosize = Seek(copyin, 0, OFFSET_END);
                iosize = Seek(copyin, 0, OFFSET_BEGINING);

                /* Allocate memory for the copy buffer */
                do {
                        copybuf = AllocMem(iosize, MEMF_PUBLIC|MEMF_CLEAR);
                        if (copybuf == 0)
                                iosize = iosize/2;
                } while (copybuf == 0 & iosize > 512);

                /* Copy the file */
                do {
                        actual = Read(copyin, copybuf, iosize);
                        if (Write(copyout, copybuf, actual) != actual)
                                break;
                } while (actual == iosize);

                /* Free the copy buffer */
                FreeMem(copybuf, iosize);

                /* Close the input and output buffers */
                Close(copyout);
                Close(copyin);
        }
}

nullterm(str,len)       /* End a string with a null */
char *str;
int len;
{
        int i;

        str[len] = 0; 
        for (i=len;len >= 0;len--) {
            if (str[i] != ' ' && str[i])
                    break;
                str[i] = 0;
        }
}

display_error(errormsg,doserrnum)
char *errormsg;
long doserrnum;
{
        strcpy(err_buf,errormsg);
        if(doserrnum)
                sprintf(err_buf,"%s (Dos Error %d)",err_buf,doserrnum);
        err_str.NumChars = strlen(err_buf);
        err_str.BufferPos = strlen(err_buf);
        RefreshGadgets(&Err_gad,w,NULL);
}

close_things()
{
    if (mask & WINDOW)
        CloseWindow(w);

    if (mask & GRAPHICS)
        CloseLibrary(GfxBase);

    (void) OpenWorkBench();

    if (mask & INTUITION)
        CloseLibrary(IntuitionBase);

#ifdef SHOW
        if(NILFD) {
                UnLock(RAMSHOWLOCK);
                DeleteFile(RAMSHOW);
                Close(NILFD);
        }
#endif

    if(mask & INITDIRLOCKOK) {
        CurrentDir(InitialCurrentDirLock);
        UnLock(CurrentDirLock);
    }
}

qsort( v, n, size, comp)
char *v;
int n;
int size;
int (*comp)();
{
        int gap, i, j, x, cnt;
        char temp, *p1, *p2;

        cnt = 0;
        for (gap=n/2; gap > 0 ; gap /= 2)
                for (i=gap; i<n; i++)
                        for (j = i-gap; j >= 0; j -= gap)
                                if ( (*comp) ( (p1=v+j*size), (p2=v+(j+gap)*size) ) < 0) {  
                                        cnt++;
                                        /* exchange them */
                                        for (x=0; x<size; x++) {
                                                temp = *p1;
                                                *p1++ = *p2;
                                                *p2++ = temp;
                                        }
                                }
        return(cnt);
}
void Print_to_screen(ptr)
char *ptr[];
{

int i, len, len2;    /* loop and counter variables */
int linecount = 1;      /* count of lines displayed to screen */
int done = FALSE;    /* boolean variables */
unsigned char c;     /* character read from keyboard */
unsigned char work[80];    /* string space used for messages */
unsigned char buf[512];       /* buffer to hold one block of the file */

struct FileHandle *window = NULL;    /* file handle for raw window to screen */
struct FileHandle *workfp = NULL;    /* file handle to file to be displayed*/

  workfp = Open(ptr, MODE_OLDFILE); /*Try to open the file to display. */
  if(workfp == NULL)
     done = TRUE;
  else{
   /* Open a Workbench window.  If there is an error, stop.*/
     strcpy(work, "Raw:0/0/640/199/Listing of ");
     strcat(work, ptr);
     strcat(work, "");
  window = Open(work, MODE_NEWFILE);
  if(window == NULL)
     done = TRUE;
      }
  strcpy(work, "\x1b[33m<CR>\x1b[2m");
  strcat(work, " advances one line, ");
  strcat(work, "\x1b[33mSpace Bar\x1b[2m");
  Write(window, work, strlen(work));
  strcpy(work, " advances one page, ");
  strcat(work, "and ");
  strcat(work, "\x1b[33mESC\x1b[2m");
  strcat(work, " aborts.\n\x1b[0m");
  Write(window, work, strlen(work));
  if (!done)
     do{
         /* Read a block of the file, search for the end of line.*/
       len = Read(workfp, buf, 512);
       len2 = 0;
     while (len2<len){
       for (i=len2; buf[i] != '\n' && i<511; i++)
          ;
     Write(window, &buf[len2], i-len2+1);
       len2 = i + 1;
     if (++linecount >= 22 && buf[i] == '\n'){
       linecount = 1;
                  /* print the messages in color 3 */
     strcpy(work, "\x1b[33mPress SPACE for more...\x1b[0m");
     Write(window, work, strlen(work));
                  /* wait for a key */
     while(Read(window, &c, 1),c != ' ')
     if (c == BRK || c == ESC){
        len = 0;    /* short circuit the loop */
        break;
     }
                  /* RETURN scrolls by one line */
     else if (c == '\r'){
        linecount = 21;
        break;
     }

                  /* erase the message */

     strcpy(work, "\r                       \r");
     Write(window, work, strlen(work));
      }
     }
    }while(len == 512);

              /* If not aborting the program */
    if (!done){
       strcpy(work, "\n\x1b[33mEnd of File.  Press SPACE\x1b[0m");
       Write(window, work, strlen(work));
            /* Wait for spacebar to quit */
       while(Read(window, &c, 1), c != ' ')
           ;
       }

    if(workfp)
       Close(workfp);
    if(window)
       Close(window);
    return;
}
                                                                 
