/* Program: GetInfo <device> [ERR|UNIT|STATE|BLOCKS|USED|TYPE]
   Desc: Program that gets info about a device and returns the
   result in a global env. variable: Info.
   Author: P Hutchison 3/12/93
   Modificatons:
   1. Use proto headers
   2. Use PutStr() instead of printf() and improve prototyping
*/

#include <proto/exec.h>
#include <proto/dos.h>
#include <exec/types.h>
#include <dos/dos.h>
#include <dos/var.h>
#include <string.h>

#define ID_MSDOS_DISK (0x4d534400L)

struct InfoData infodata =
{0, 0, 0, 0, 0, 0, 0, NULL, 0 };

char buf[14];

void FetchInfo(char *opt);

main(int argc, char *argv[])
{
   struct DOSBase *DOSBase;
   long __oslibversion = 37;
   char name[5];
   BPTR lock;
   
   strcpy(name, "Info");
   buf[0] = (char)NULL;
   
   if (DOSBase = (struct DOSBase *)OpenLibrary("dos.library", __oslibversion))
   {      
    if (argc == 3) /* NB: Command name counts as 1 also! */
    {
      if (lock = Lock(argv[1], ACCESS_READ))
      {
         if (Info(lock, &infodata))
         {
          FetchInfo(argv[2]);
          
          SetVar(name, buf, -1, GVF_GLOBAL_ONLY); /* Set env. variable */
         } 
         UnLock(lock); 
      }
    } else
      PutStr("GetInfo <device> [ERR|UNIT|STATE|BLOCKS|USED|TYPE]\n");
    CloseLibrary((struct Library *)DOSBase);
   } 
}
      
void FetchInfo(char *opt)
{
   long status, type;
   long result;
      
   if (stricmp(opt, "ERR") == 0)     /* Get Information */
   {  
      result = infodata.id_NumSoftErrors;
      stcl_d(buf, result);    
   }
   if (stricmp(opt, "UNIT") == 0) 
   {
      result = infodata.id_UnitNumber;
      stcl_d(buf, result);
   }
   if (stricmp(opt, "STATE") == 0)
   { 
       status = infodata.id_DiskState;
       if (status == ID_WRITE_PROTECTED)
            strcpy(buf, "PROTECTED");
       if (status == ID_VALIDATING)
            strcpy(buf, "VALIDATING");
       if (status == ID_VALIDATED)
            strcpy(buf, "VALIDATED");
   }
   
   if (stricmp(opt, "BLOCKS") == 0)
   {
      result= infodata.id_NumBlocks;
      stcl_d(buf, result);
   }
   if (stricmp(opt, "USED") == 0)
   {
      result = infodata.id_NumBlocksUsed;
      stcl_d(buf, result);
   }
   if (stricmp(opt, "TYPE") == 0)
   {
       type = infodata.id_DiskType;
       if (type == ID_NO_DISK_PRESENT)
           strcpy(buf, "NODISK");
       if (type == ID_UNREADABLE_DISK)
           strcpy(buf, "UNREAD");
       if (type == ID_DOS_DISK)
           strcpy(buf, "DOSDISK");
       if (type == ID_FFS_DISK)
           strcpy(buf, "FFSDISK");
       if (type == ID_NOT_REALLY_DOS)
           strcpy(buf, "NOTDOS");
       if (type == ID_KICKSTART_DISK)
           strcpy(buf, "KICKSTART");
       if (type == ID_MSDOS_DISK)
           strcpy(buf, "MSDOS");
    }
}
        
