#include	<stdio.h>
#include	<dir.h>
#include    <fcntl.h>
#include    <stdio.h>
#include    <io.h>
#include    <ctype.h>

#define     BSIZE       1500                                                    /* Buffer Size to read Dirs.Stk into  */
#define     STDOUT      1                                                       /* Define StdOut for Write            */
#define     DIRSFILE    "C:\\Status\\DirS.Stk"

main(int argc, char *argv[]) {
    int  stat,
         HandleStackFile,                                                       /* Handle of stack file opened        */
         BytesRead,                                                             /* Count of Bytes read from stack file*/
         BytesWrite;                                                            /* Count of Bytes written to screen   */
    char dirname[MAXPATH],
         BufferStackFile[BSIZE];                                                /* Buffer to read stack file into     */

    if( argc > 1 ) {                                                            /* If passed parameter                */
        if( *argv[1] == '?' ) {                                                 /*    If ?                            */
            HandleStackFile = open( DIRSFILE, O_RDONLY );                       /*       Show the Directory Stack File*/
            if( HandleStackFile == -1 ) {                                       /*       Problem opening file         */
                puts( "PopD: Open Error" );
                exit( HandleStackFile );
            }
            BytesRead = read( HandleStackFile, BufferStackFile, BSIZE );
            if( BytesRead == -1 ) {                                             /*       Problem reading file         */
                puts( "PopD: Read Error" );
                exit( BytesRead );
            }
            if( BytesRead == 0 ) {                                              /*       Nothing to show!             */
                puts( "PopD: NO Directories PUSHeD." );
                exit( BytesRead );
            }
            BytesWrite = write( STDOUT, BufferStackFile, BytesRead );
            if( BytesRead != BytesWrite ) {                                     /*       Problem writing to screen    */
                puts( "PopD: Write Error" );
                exit( BytesWrite );
        }   }
        else {                                                                  /*    NOT ?, show command syntax      */
            puts( "PopD [?]                 ? shows directory stack." );
            puts( "               No argument changes to last directory PUSHeD." );
        }
        exit( 1 );
    }
    if(( stat = popstr( DIRSFILE, MAXPATH, dirname )) < 0 )                     /* If NO directory to be PopD         */
        exit( stat );
    if( dirname[1] == ':' ) {                                                   /* If drive colon 2nd character       */
        if( dirname[2] == '\0' )
            strcat( dirname, "." );
        setdisk( tolower( dirname[0] ) - 'a' );                                 /*    find drive by subtracting 'a'   */
    }
    stat = chdir( dirname );
    if( stat < 0 ) {                                                            /* If Directory doesn't exist         */
        puts( "PopD: Path NOT found" );
        exit( -1 );
}   }
