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

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