/****************************************************************************/
/*   dfree v1.0   Show free space on a disk - Shell command                 */
/*                                                                          */
/*   USAGE: dfree <device/name>                                             */
/*                                                                          */
/*   Max Francis                                    started: 12/02/1994     */
/****************************************************************************/

#include <dos/dos.h>
#include <stdio.h>

void main( int argc, char *argv[] );
void helpscreen( void );
void cleanup( void );

struct InfoData *infodata_p;
struct InfoData infodata;
BPTR lock_p;

/* Formatted version string for the 2.0 VERSION command */ 
UBYTE *vers = "\0$VER: MF DFree v1.0 (12/02/94)";


void main( int argc, char *argv[] )
{
        BOOL success;
        LONG disk_blocks;
        LONG used_blocks;
        LONG block_bytes;
        LONG disk_type;
        LONG free_space;
        int c;

        if( argc != 2) 
                helpscreen();
        if( strcmp("?",argv[1]) == 0 )
                helpscreen();
        if( strcmp("help",argv[1]) == 0 )
                helpscreen();
        if( strcmp("HELP",argv[1]) == 0 )
                helpscreen();

        /* Convert user's device to upper case (just coz it looks nice) */
        for(c=0; c <= strlen(argv[1]); c++)
                argv[1][c] = toupper(argv[1][c]);

        /* Attempt to lock the device/disk */
        lock_p = Lock( argv[1], ACCESS_READ );

        if( lock_p == NULL )
        {
                printf("Can't examine %s\n", argv[1] );
                cleanup();
        }

        /* Get the Information block (struct InfoData) of the device */
        /* First, point the pointer to the structure... */
        infodata_p = &infodata;
        success = Info( lock_p, infodata_p );
        if( !success )
        {
                printf("Can't examine %s\n", argv[1] );
                cleanup();
        }

        disk_blocks = infodata_p->id_NumBlocks;
        used_blocks = infodata_p->id_NumBlocksUsed;
        block_bytes = infodata_p->id_BytesPerBlock;
        disk_type   = infodata_p->id_DiskType;

        free_space = (disk_blocks - used_blocks) * block_bytes;

        if( free_space > (5*1048576) )
                printf("%s %dM free\n", argv[1], free_space/1048576);
        else
                if( free_space == 1)
                        printf("%s %d byte free\n", argv[1], free_space);
                else
                        if( free_space < 1024)
                                printf("%s %d bytes free\n", argv[1], free_space);
                        else
                                printf("%s %dK free\n", argv[1], free_space/1024);

        cleanup();
}




void helpscreen( void )
{
        printf("MF \"dfree\"\nUSAGE: dfree <DEVICE/DISK>\n");
        printf("Show free disk space on DEVICE or DISK specified\n");
        printf("Eg. \"dfree df0:\"\n");
        cleanup();
}



void cleanup( void )
{
        if( lock_p )  /* 'spose we'd better unlock the directory */
        {
                UnLock( lock_p );
        }
        exit( 0 );

}


