home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / tools / system / format64 / source / device.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-20  |  5.5 KB  |  207 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <intuition/intuition.h>
  4. #include <dos/dos.h>
  5. #include <dos/filehandler.h>
  6. #include <workbench/startup.h>
  7. #include <libraries/gadtools.h>
  8. #include <workbench/icon.h>
  9. #include <devices/trackdisk.h>
  10. #include <dos/rdargs.h>
  11.  
  12. /*Prototypes for system functions*/
  13. #include <proto/exec.h>
  14. #include <proto/intuition.h>
  15. #include <proto/dos.h>
  16. #include <proto/gadtools.h>
  17. #include <proto/icon.h>
  18. #include <proto/graphics.h>
  19.  
  20. /*Other headers*/
  21. #include "Format.h"
  22. #include "string.h"
  23. #include "stdio.h"
  24.  
  25. extern BOOL FFS = FALSE;
  26. extern BOOL QuickFmt = FALSE;
  27. extern BOOL Verify = TRUE;
  28. extern BOOL Icon = TRUE;
  29. extern struct Library *ibase = NULL;
  30. extern struct Library *gbase = NULL;
  31. extern struct Library *GadToolsBase = NULL;
  32. extern struct Library *IconBase = NULL;
  33. extern BPTR StdErr = NULL;
  34. extern LONG args[6] = { NULL,NULL,0,0,0,0 };
  35. //extern Rect box;
  36. extern struct WBStartup *WBenchMsg;
  37.  
  38. /*Convert a volume name to a device name, and get information*/
  39. /*on the layout of the disk*/
  40. BOOL volumeToDevName(BPTR volumeLock,char *dev,DriveLayout *layout)
  41.     {
  42.    BOOL stat;
  43.    struct DosList *dosList;
  44.    struct DeviceNode *devNode;
  45.    char name[36];
  46.    char *temp;
  47.    int c;
  48.    BPTR tempLock=NULL;
  49.    struct DosEnvec *driveEnv;
  50.    struct Process *process;
  51.    APTR oldWdw;
  52.    struct FileSysStartupMsg *startup;
  53.    char *devName;
  54.  
  55.  
  56.    /*Disable requestors during the execution of this function*/
  57.    process=(struct Process *)FindTask(0L);
  58.    oldWdw=process->pr_WindowPtr;
  59.    process->pr_WindowPtr=(APTR)(-1);
  60.  
  61.    /*Get the DOS device list*/
  62.    dosList=LockDosList(LDF_DEVICES|LDF_READ);
  63.  
  64.    /*Go through each entry*/
  65.    while(dosList != NULL)
  66.         {
  67.         dosList = NextDosEntry(dosList,LDF_DEVICES|LDF_READ);
  68.         if (dosList == NULL) break;
  69.       devNode=(struct DeviceNode *)dosList;
  70.  
  71.       /*If the node in the list is a volume*/
  72.       if (devNode->dn_Startup > 1000 &&
  73.             (devNode->dn_Task || devNode->dn_Handler || devNode->dn_SegList))
  74.             {
  75.             /*Get the name of the device*/
  76.             temp=(char *)BADDR(devNode->dn_Name);
  77.  
  78.             for(c=0;c<temp[0];c++) name[c]=temp[c+1];
  79.  
  80.             name[c]=':';
  81.             name[c+1]=0;
  82.  
  83.             /*Get the information on the device*/
  84.             /*       driveEnv=(struct DosEnvec *)
  85.             BADDR(((struct FileSysStartupMsg *)
  86.             BADDR(devNode->dn_Startup))->fssm_Environ);*/
  87.  
  88.      /*If the volume lock is NULL, the 'dev' is assumed to be the*/
  89.      /*name of a device holding an unformatted disk, so compare the*/
  90.      /*name to the name of the current node*/
  91.             if (volumeLock == NULL) stat = (stricmp(dev,name)==0);
  92.             else
  93.                 {
  94.                 /*Otherwise, since 'dev' could hold a volume rather than*/
  95.                 /*device name, get a lock on it and compare it to the lock*/
  96.                 /*on the device that was given;  if they're the same, we've*/
  97.                 /*found the drive*/
  98.  
  99.                 tempLock=Lock(name,ACCESS_READ);
  100.                 stat=(SameLock(tempLock,volumeLock)==LOCK_SAME);
  101.                 }
  102.  
  103.             /*If weve found the drive, get the information on it*/
  104.             if(stat)
  105.                 {
  106.  
  107.                 /*Get a pointer to the structure that holds the needed info*/
  108.                 startup=(struct FileSysStartupMsg *)BADDR(devNode->dn_Startup);
  109.                 driveEnv=(struct DosEnvec *)BADDR(startup->fssm_Environ);
  110.  
  111.                 /*Get the information*/
  112.                 layout->unit=startup->fssm_Unit;
  113.  
  114.                 devName=(char *)BADDR(startup->fssm_Device);
  115.  
  116.                 /*Copy the device name to layout->devName*/
  117.                 for(c=0;c<devName[0];c++) layout->devName[c]=devName[c+1];
  118.                 layout->devName[c+1]=0;
  119.  
  120.                 layout->flags=startup->fssm_Flags;
  121.                 layout->memType=driveEnv->de_BufMemType;
  122.                 layout->lowCyl=driveEnv->de_LowCyl;
  123.                 layout->highCyl=driveEnv->de_HighCyl;
  124.                 layout->surfaces=driveEnv->de_Surfaces;
  125.                 layout->BPT=driveEnv->de_BlocksPerTrack;
  126.                 layout->blockSize=driveEnv->de_SizeBlock;
  127.  
  128.                 /*Copy the device name back to 'dev'*/
  129.                 strcpy(dev,name);
  130.  
  131.                 /*UnLock the drive lock, if it exists*/
  132.                 if(tempLock != NULL) UnLock(tempLock);
  133.  
  134.                 /*Unlock the DOS list*/
  135.                 UnLockDosList(LDF_DEVICES|LDF_READ);
  136.  
  137.                 /*Restore the requester pointer*/
  138.                 process->pr_WindowPtr=oldWdw;
  139.  
  140.                 /*And return TRUE*/
  141.                 return(TRUE);
  142.                 }
  143.  
  144.             /*We didnt find the drive, so unlock the lock and try again*/
  145.             if(tempLock != NULL) UnLock(tempLock);
  146.             }
  147.         }
  148.  
  149.    /*We didnt find the drive in the list, so unlock the list*/
  150.    UnLockDosList(LDF_DEVICES|LDF_READ);
  151.  
  152.    /*Restore the requester pointer*/
  153.    process->pr_WindowPtr=oldWdw;
  154.  
  155.    /*And return*/
  156.    return(FALSE);
  157.     }
  158.  
  159. /*Create a communications port linking this process to the drive*/
  160. struct IOExtTD *OpenDrive(char *driveDevName,ULONG unit,ULONG flags)
  161.     {
  162.    struct MsgPort *diskPort;
  163.    struct IOExtTD *diskRequest;
  164.  
  165.    /*Create the message port*/
  166.    if ((diskPort = CreateMsgPort()) != NULL)
  167.         {
  168.       /*Create the IORequest*/
  169.       diskRequest = (struct IOExtTD *)
  170.           CreateIORequest(diskPort,sizeof(struct IOExtTD));
  171.       if(diskRequest != NULL)
  172.             {
  173.             /*Open the device, and return the IORequest if the device*/
  174.             /*opened successfully*/
  175.             if(!OpenDevice(driveDevName,unit,(struct IORequest *)diskRequest,flags))
  176.                 return(diskRequest);
  177.  
  178.             /*The device didnt open, so clean up*/
  179.             DeleteIORequest(diskRequest);
  180.             }
  181.  
  182.         DeleteMsgPort(diskPort);
  183.         }
  184.  
  185.    /*Return NULL to indicate that an error occurred*/
  186.    return(NULL);
  187.     }
  188.  
  189. /*Close an open device and delete the accompanying port, etc.*/
  190. void CloseDrive(struct IOExtTD *diskRequest)
  191.     {
  192.    CloseDevice((struct IORequest *)diskRequest);
  193.    DeleteMsgPort(diskRequest->iotd_Req.io_Message.mn_ReplyPort);
  194.    DeleteIORequest(diskRequest);
  195.     }
  196.  
  197. /*Convert a CSTR to a BSTR*/
  198. BSTR makeBSTR(char *in,char *out)
  199.     {
  200.    int c;
  201.  
  202.    out[0]=strlen(in);
  203.    for(c=0;c<out[0];c++) out[c+1]=in[c];
  204.  
  205.    return(MKBADDR(out));
  206.     }
  207.