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

  1.  
  2. #include <exec/types.h>
  3. #include <exec/exec.h>
  4. #include <intuition/intuition.h>
  5. #include <dos/dos.h>
  6. #include <dos/filehandler.h>
  7. #include <workbench/startup.h>
  8. #include <libraries/gadtools.h>
  9. #include <workbench/icon.h>
  10. #include <devices/trackdisk.h>
  11. #include <dos/rdargs.h>
  12. #include <proto/exec.h>
  13. #include <proto/intuition.h>
  14. #include <proto/dos.h>
  15. #include <proto/gadtools.h>
  16. #include <proto/icon.h>
  17. #include <proto/graphics.h>
  18. #include "Format.h"
  19. #include "string.h"
  20. #include "stdio.h"
  21.  
  22. extern BOOL FFS = FALSE;
  23. extern BOOL QuickFmt = FALSE;
  24. extern BOOL Verify = TRUE;
  25. extern BOOL Icon = TRUE;
  26. extern struct Library *ibase = NULL;
  27. extern struct Library *gbase = NULL;
  28. extern struct Library *GadToolsBase = NULL;
  29. extern struct Library *IconBase = NULL;
  30. extern BPTR StdErr = NULL;
  31. extern LONG args[6] = { NULL,NULL,0,0,0,0 };
  32. extern struct WBStartup *WBenchMsg;
  33. extern char temp[];
  34.  
  35. checkfor64bitcommandset(struct IOStdReq *io);
  36.  
  37.  
  38. /*This functions handles the low-level format, the high-level format, the*/
  39. /*creation of icons, etc.*/
  40. void formatVolume(BPTR *volumeLock,char *volumeName,char *newName,ULONG ffs,
  41.           BOOL quick,BOOL verify,BOOL icon,char *statString)
  42.     {
  43.    struct IOExtTD *io1;
  44.    BOOL fmtResult=TRUE;
  45.    int result=0;
  46.    char deviceName[64], temp2[4];
  47.    DriveLayout layout;
  48.    struct MsgPort *devPort;
  49.    BOOL writeProtCont=TRUE;
  50.  
  51.    /*If the volume lock is NULL, assume that the volumeName string holds*/
  52.    /*a valid device pointer*/
  53.    if(*volumeLock==NULL) strcpy(deviceName,volumeName);
  54.  
  55.    /*Get the drive name (if possible)*/
  56.    if(!volumeToDevName(*volumeLock,deviceName,&layout))
  57.         {
  58.       printError("Can't find the drive!",NULL,NULL);
  59.       return;
  60.         }
  61.  
  62.    /*This port will be used to communicate with the filesystem*/
  63.    devPort=DeviceProc(deviceName);
  64.    if(devPort==NULL)
  65.         {
  66.       printError("Can't find the drive",NULL,NULL);
  67.       return;
  68.         }
  69.  
  70.    /*Inhibit the drive*/
  71.    DoPkt(devPort,ACTION_INHIBIT,DOSTRUE,NULL,NULL,NULL,NULL);
  72.  
  73.    /*If we got a lock to the volume that we're going to format it, destroy*/
  74.    /*it, since the volume that it points to is about to be erased anyway*/
  75.    if(*volumeLock != NULL)
  76.         {
  77.       UnLock(*volumeLock);
  78.       *volumeLock=NULL;
  79.         }
  80.  
  81.    /*Open the disk device*/
  82.    if((io1=OpenDrive(layout.devName,layout.unit,layout.flags))!=NULL)
  83.         {
  84.       /*Determine the write protect status*/
  85.       io1->iotd_Req.io_Data=NULL;
  86.       io1->iotd_Req.io_Length=0;
  87.       io1->iotd_Req.io_Command=TD_PROTSTATUS;
  88.       DoIO((struct IORequest *)io1);
  89.  
  90.       /*Loop while the disk stays protected and user keeps pressing Retry*/
  91.       while(io1->iotd_Req.io_Actual!=0 && (writeProtCont=alertIsWriteProtected(volumeName)))
  92.             DoIO((struct IORequest *)io1);
  93.  
  94.       /*If the disk is not write-protected*/
  95.         if(writeProtCont)
  96.             {
  97.             /*Do a full format if the user didnt select the Quick option*/
  98.             if (checkfor64bitcommandset((struct IOStdReq *)io1) == TRUE)
  99.                 printf("***Device supports 64bit commands! No size limit!!\n");
  100.             else printf("***Device does NOT support 64bit commands. 4GB limit activated!!\n");
  101.  
  102.             if (WBenchMsg == NULL)
  103.                 {
  104.                 Write(Output(),"Insert the disk to be formatted in drive ",41);
  105.                 Write(Output(),temp,strlen(temp));
  106.                 Write(Output()," and press RETURN ",18);
  107.                 Read(Input(),temp2,1);
  108.                 }
  109.  
  110.           if( (SetSignal(0,0) & SIGBREAKF_CTRL_C) != SIGBREAKF_CTRL_C)
  111.                 {
  112.                 if (!quick) fmtResult=doFullFormat(&layout,statString,volumeName,io1);
  113.                 if (fmtResult)
  114.                     {
  115.                     /*Write an extra carriage return, if run from the CLI*/
  116.                     if(WBenchMsg==NULL) Write(Output(),"\n",1);
  117.                     /*Tell the user that were doing the high-level format*/
  118.                    fmtResult =! updateStatWindow("Initializing disk...",1000);
  119.                     /*If the user hasnt clicked on Stop, do the high-level format*/
  120.                     if (fmtResult) result=Format(deviceName,newName, ffs);
  121.                     }
  122.                 }
  123.             else printf("***Break\n");
  124.             }
  125.         else printError("Cannot format volume", volumeName, "because the disk is write protected");
  126.  
  127.         /*Close the disk device*/
  128.         CloseDrive(io1);
  129.        }
  130.    else
  131.        {
  132.       printError("Couldn't access the device",NULL,NULL);
  133.       return;
  134.        }
  135.  
  136.    /*Uninhibit the drive*/
  137.    DoPkt(devPort,ACTION_INHIBIT,DOSFALSE,NULL,NULL,NULL,NULL);
  138.  
  139.    /*Wait for the drive to come on line*/
  140.    Delay(50);
  141.  
  142.    /*If the disk was never unprotected, return*/
  143.    if(!writeProtCont) return;
  144.  
  145.    /*If the format was successful and the user wants icons created*/
  146.    if(icon && result==DOSTRUE)
  147.         {
  148.       struct DiskObject *trashIcon;
  149.       BPTR trashLock;
  150.  
  151.       /*Update the user*/
  152.       fmtResult=!updateStatWindow("Creating Trashcan  ",1000);
  153.  
  154.       /*If she didnt press Stop*/
  155.       if(fmtResult)
  156.             {
  157.             /*Create the trashcan name (<volume>:Trashcan)*/
  158.             strcpy(deviceName,newName);
  159.             strcat(deviceName,":");
  160.             strcat(deviceName,"Trashcan");
  161.  
  162.             /*Create the trashcan directory*/
  163.             trashLock=CreateDir(deviceName);
  164.  
  165.             /*If it was successfully created*/
  166.             if(trashLock!=NULL)
  167.                 {
  168.                 UnLock(trashLock);
  169.                 /*Get the trashcan icon*/
  170.                 trashIcon=GetDefDiskObject(WBGARBAGE);
  171.  
  172.                 /*If there wasnt an error*/
  173.                 if (trashIcon != NULL)
  174.                     {
  175.                     /*Write the icon to disk*/
  176.                     if(!PutDiskObject(deviceName,trashIcon))
  177.                         printError("There was an error while creating the trashcan", NULL,NULL);
  178.  
  179.                     /*and free it*/
  180.                     FreeDiskObject(trashIcon);
  181.                     }
  182.                 else printError("There was an error while creating the trashcan", NULL,NULL);
  183.                 }
  184.             else printError("There was an error while creating the trashcan directory", NULL,NULL);
  185.             }
  186.         }
  187.     return;
  188.     }
  189.  
  190.