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

  1.  
  2.  
  3. /**************************************************************************/
  4. /*                  Format64                  */
  5. /*                   Version 1.00                  */
  6. /*                                      */
  7. /* This is a replacement for the AmigaDOS "Format" command.  It sports    */
  8. /* (for the Workbench user) more friendly user-interface, although it is  */
  9. /* virtually identical to the standard Format command where the CLI      */
  10. /* interface is concerned.                          */
  11. /*                                      */
  12. /* NewFormat v1.00 is Copyright 1992 by Dave Schreiber, All Rights Reserved.*/
  13. /* Format64  v1.00 is Copyright 1998 by Jarkko Vatjus-Anttila*/
  14. /* This program may not be sold for more than a small copying and shipping*/
  15. /* and handling fee, except by written permission of Dave Schreiber.      */
  16. /*                                      */
  17. /* Version list:                              */
  18. /*    1.00 - First release (August 31, 1992)                              */
  19. /*    1.00 - Update to 64 bit command set. Support for disks >4GB */
  20. /**************************************************************************/
  21.  
  22.  
  23. /*System header files*/
  24. #include <exec/types.h>
  25. #include <exec/exec.h>
  26. #include <intuition/intuition.h>
  27. #include <dos/dos.h>
  28. #include <dos/filehandler.h>
  29. #include <workbench/startup.h>
  30. #include <libraries/gadtools.h>
  31. #include <workbench/icon.h>
  32. #include <devices/trackdisk.h>
  33. #include <dos/rdargs.h>
  34.  
  35. /*Prototypes for system functions*/
  36. #include <proto/exec.h>
  37. #include <proto/intuition.h>
  38. #include <proto/dos.h>
  39. #include <proto/gadtools.h>
  40. #include <proto/icon.h>
  41. #include <proto/graphics.h>
  42.  
  43. /*Other headers*/
  44. #include "Format.h"
  45. #include "GUI.h"
  46. #include "string.h"
  47. #include "stdio.h"
  48.  
  49. /* Global Variables */
  50.  
  51. BOOL FFS = FALSE;
  52. BOOL QuickFmt = FALSE;
  53. BOOL intl = FALSE;
  54. BOOL Verify = TRUE;
  55. BOOL Icon = TRUE;
  56. struct Library *ibase = NULL;
  57. struct Library *gbase = NULL;
  58. struct Library *GadToolsBase = NULL;
  59. struct Library *IconBase = NULL;
  60. BPTR StdErr = NULL;
  61.  
  62. LONG args[7] = { NULL,NULL,0,0,0,0,0 };
  63. ULONG ffs = ID_DOS_DISK;
  64.  
  65. extern Rect box;
  66. struct WBStartup *WBenchMsg = NULL;
  67.  
  68. char *Version = "$VER: Format64 V1.01 (10.4.98)";
  69. char temp[32];
  70.  
  71. //int main(int a, char **arg);
  72.  
  73. main()
  74.     {
  75.    UWORD disk;
  76.     char volumeName[256], statusString[80], driveName[64], temp2[4];
  77.     char newName[256];
  78.     BPTR driveLock;
  79.     struct Process *process;
  80.     APTR oldWdw;
  81.     DriveLayout junk;
  82.    prepResult stat;
  83.  
  84.  
  85.    /*Open the various shared libraries that are needed*/
  86.    ibase = (struct Library *)OpenLibrary("intuition.library",37L);
  87.    gbase = (struct Library *)OpenLibrary("graphics.library",37L);
  88.    IconBase = (struct Library *)OpenLibrary("icon.library",37L);
  89.    GadToolsBase = (struct Library *)OpenLibrary("gadtools.library",37L);
  90.  
  91.    if(IntuitionBase==NULL || GfxBase==NULL || IconBase==NULL || GadToolsBase==NULL)
  92.       cleanup(100);
  93.  
  94. //    WBenchMsg = (struct WBStartup *)argv;
  95.    if(WBenchMsg != NULL)
  96.         {
  97.       if(WBenchMsg->sm_NumArgs==1)
  98.             {
  99.             printError("Please select a drive to format and try again.",NULL,NULL);
  100.             cleanup(100);
  101.             }
  102.  
  103.         SetupScreen();
  104.  
  105.       for(disk=1;disk<WBenchMsg->sm_NumArgs && stat!=eQuit;disk++)
  106.             {
  107.             /*Get the volume/device name of the disk to format*/
  108.             getVolumeName(volumeName,WBenchMsg->sm_ArgList,disk);
  109.  
  110.             /*If there is no lock to the volume, that means it is */
  111.             /*unformatted, so just use the name given to us by Workbench*/
  112.             if (WBenchMsg->sm_ArgList[disk].wa_Lock==NULL)
  113.                 strcpy(volumeName,WBenchMsg->sm_ArgList[disk].wa_Name);
  114.  
  115.             /*Open the options window*/
  116.             if ((stat=OpenPrepWindow(volumeName))==0)
  117.                 {
  118.                 /* Get the users input */
  119.                 stat=getPrepInput();
  120.  
  121.                 /*And close the window*/
  122.                 ClosePrepWindow();
  123.  
  124.                 /*If the user selected 'OK'*/
  125.                 if (stat == eOK)
  126.                     {
  127.                     /*Get the new name of the disk*/
  128.                     strcpy(newName,((struct StringInfo *)
  129.                         (PrepGadgets[GD_NameGadget]->SpecialInfo))->Buffer);
  130.  
  131.                     /*Ask the user for verification*/
  132.                     if (askAreYouSure(volumeName,
  133.                         (WBenchMsg->sm_ArgList[disk].wa_Lock!=NULL)))
  134.                         {
  135.  
  136.                         /*If everythings OK, open the status window*/
  137.                         if ((stat=OpenStatusWindow(statusString))==0)
  138.                             {
  139.                             /*And format the disk*/
  140.                             formatVolume(&(WBenchMsg->sm_ArgList[disk].wa_Lock),
  141.                                 volumeName,newName,FFS,QuickFmt,Verify,Icon,
  142.                                 statusString);
  143.  
  144.                             /*Were done, so close the status window*/
  145.                             CloseStatusWindow();
  146.                             }
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.         /*Free the visual info, etc.*/
  152.         CloseDownScreen();
  153.         }
  154.  
  155.    else     /*Weve been run from the CLI*/
  156.  
  157.         {
  158.       /*Open a 'stderr' I/O channel to the shell*/
  159.       /*(the normal stderr was not opened since we used _main() ) */
  160.       StdErr=Open("CONSOLE:",MODE_OLDFILE);
  161.  
  162.       /*Make sure requestors dont open*/
  163.       process=(struct Process *)FindTask(0L);
  164.       oldWdw=process->pr_WindowPtr;
  165.       process->pr_WindowPtr=(APTR)(-1);
  166.  
  167.       /*Get the command-line arguments*/
  168.       parseArgs(driveName,volumeName,&FFS,&intl,&Icon,&QuickFmt,&Verify);
  169.  
  170.         if (FFS == TRUE)
  171.           {
  172.             if (intl == TRUE) ffs = ID_INTER_FFS_DISK;
  173.             else ffs = ID_FFS_DISK;
  174.             }
  175.         else
  176.           {
  177.             if (intl == TRUE) ffs = ID_INTER_DOS_DISK;
  178.             else ffs = ID_DOS_DISK;
  179.             }
  180.  
  181. //    printf("%ld %ld $%lx\n", FFS, intl, ffs);
  182.  
  183.       /*Get a lock on the selected drive*/
  184.       /*(note:  NULL is a valid return value;  it means that the disk we*/
  185.       /*want to format is itself unformatted)*/
  186.       driveLock=Lock(driveName,ACCESS_READ);
  187.         strcpy(temp,driveName);
  188.  
  189.       /*Get the volume/drive name*/
  190.       if (volumeToDevName(driveLock,temp,&junk))
  191.           {
  192.             /*Wait for the user to tell us to go ahead*/
  193. /*            Write(Output(),"Insert the disk to be formatted in drive ",41);
  194.             Write(Output(),temp,strlen(temp));
  195.             Write(Output()," and press RETURN ",18);
  196.             Read(Input(),temp2,1);
  197. */
  198.             /*Format the disk*/
  199.             formatVolume(&driveLock,temp,volumeName,ffs,QuickFmt,Verify,Icon,statusString);
  200.             }
  201.       else Write(Output(),"Cannot find the specified drive!\n",33);
  202.  
  203.       Close(StdErr);
  204.  
  205.       /*Restore the old contents of this pointer*/
  206.       process->pr_WindowPtr=(APTR)(-1);
  207.  
  208.       Write(Output(),"\n",1);
  209.         }
  210.     cleanup(0);
  211.     return(0);
  212.     }
  213.  
  214. /*Exit from the program, closing any open libraries*/
  215. void cleanup(ULONG err)
  216.     {
  217.    if(ibase != NULL) CloseLibrary(ibase);
  218.    if(gbase != NULL) CloseLibrary(gbase);
  219.    if(GadToolsBase != NULL) CloseLibrary(GadToolsBase);
  220.    if(IconBase != NULL) CloseLibrary(IconBase);
  221.    exit(err);
  222.     }
  223.  
  224.  
  225. /*Update the status window (from Workbench), or CLI output (from the CLI)*/
  226. /*If running from the CLI, this also checks to see if the user has pressed*/
  227. /*control-C*/
  228. BOOL updateStatWindow(char *string,UWORD percent)
  229.     {
  230.    UWORD width;
  231.    ULONG class1;
  232.    UWORD code;
  233.    struct TagItem tags[3];
  234.    static char msg[80];
  235.     char CR = 0x0d;
  236.  
  237.    struct IntuiMessage *mesg;
  238.  
  239.    /*If this is NULL, were running from the CLI*/
  240.    if(WBenchMsg==NULL)
  241.         {
  242.       /*Write the string to the CLI, followed by a carriage return (but*/
  243.       /*not a line feed)*/
  244.       Write(Output(),string,strlen(string));
  245.       Write(Output(),&CR,1);
  246.  
  247.       /*Check to see if the user pressed Control-C*/
  248.       if( (SetSignal(0,0) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  249.             {
  250.             /*If he did, print "***Break" and return TRUE, to signal that*/
  251.             /*the user aborted the formatting process*/
  252.             Write(Output(),"\n***Break\n",10);
  253.             return(TRUE);
  254.             }
  255.       /*Otherwise, continue*/
  256.       return(FALSE);
  257.         }
  258.  
  259.    /*This code is used to update the status window that is displayed when*/
  260.    /*the user runs NewFormat from the Workbench*/
  261.  
  262.    /*This puts the message into the text-display gadget*/
  263.    /*This copy is so that the caller can change the contents of 'string'*/
  264.    /*upon return (which cant be done without the copy, since GadTools*/
  265.    /*wont copy the contents of string  to an internal buffer*/
  266.    strcpy(msg,string);
  267.    tags[0].ti_Tag=GTTX_Text;
  268.    tags[0].ti_Data=(ULONG)msg;
  269.  
  270.    tags[1].ti_Tag=TAG_DONE;
  271.    tags[1].ti_Data=NULL;
  272.  
  273.    GT_SetGadgetAttrsA(StatusGadgets[GD_StatusGadget], StatusWnd,NULL, tags);
  274.  
  275.    /*Fill the status box with the current percentage of completion*/
  276.    SetAPen(StatusWnd->RPort,3);
  277.    width=box.left+((box.width-3)*percent)/1000;
  278.    RectFill(StatusWnd->RPort,box.left+2,box.top+1,width,box.top+box.height-2);
  279.  
  280.    /*Check user input*/
  281.    mesg=GT_GetIMsg(StatusWnd->UserPort);
  282.  
  283.    /*Loop while there are messages*/
  284.    while(mesg!=NULL)
  285.         {
  286.       class1 = mesg->Class;
  287.       code = mesg->Code;
  288.       GT_ReplyIMsg(mesg);
  289.  
  290.       switch(class1)
  291.             {
  292.             /*Return TRUE (user abort) if the user pressed 's', 'S'*/
  293.             case IDCMP_VANILLAKEY:
  294.                 if (code == 's' || code == 'S') return(TRUE);
  295.                 break;
  296.  
  297.             /*Or if the user pressed the 'Stop' gadget*/
  298.             case IDCMP_GADGETUP:
  299.                 return(TRUE);
  300.             }
  301.       /*Get the next message*/
  302.       mesg=GT_GetIMsg(StatusWnd->UserPort);
  303.         }
  304.    return(FALSE);
  305.     }
  306.  
  307. /*End of Format.c*/
  308.  
  309.