home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Cruncher / ulib4271.lha / UnpackLib / For_Programmers_Only / Programmers_Tools / Examples / unpTrack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-15  |  4.8 KB  |  176 lines

  1. /*************************************************************************
  2. *                                unpTrack                                *
  3. *                                                                        *
  4. *               This program can unpack track packed files.              *
  5. *                                                                        *
  6. *************************************************************************/
  7.  
  8. // First we include a lot of things
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <dos/rdargs.h>
  17. #include <dos/dosextens.h>
  18. #include <devices/trackdisk.h>
  19. #include <libraries/unpack.h>
  20.  
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <proto/unpack.h>
  24.  
  25. // Global Variables
  26. struct  Library *UnpackBase;
  27. struct  UnpackInfo *cinfo=NULL;
  28. struct  DriveStruct *dinfo=NULL;
  29. struct  RDArgs *rda=NULL;
  30. LONG    arguments[4] = {0,0,0,0};
  31.  
  32. #ifdef LATTICE
  33. void __regargs _CXBRK(void)               // Catch CTRL-C Handling
  34. {
  35.   fprintf(stderr,"*** User breaked\n");   // Print out break message
  36.   upUnuseDrive(dinfo);                    // and clean up after us
  37.   upFreeCInfo(cinfo);
  38.   CloseLibrary(UnpackBase);
  39.   FreeArgs(rda);
  40.   exit(0);                                // then we exit our program.
  41. }
  42. #endif
  43.  
  44.  
  45. // Prototypes
  46. __asm WORD Saver(register __a1 ULONG userdata, register __a2 struct UnpackInfo *ui);
  47. __asm WORD SaverError(register __a1 ULONG userdata, register __a2 struct UnpackInfo *ui);
  48.  
  49. // Main program
  50. void main (void)
  51. {
  52. extern  struct ExecBase *SysBase;
  53. BOOL    error=0;
  54.  
  55.  
  56.   if( ((struct Library*) DOSBase)->lib_Version < 37 )
  57.   {
  58.       fprintf(stderr,"unpTrack requires at least Kickstart 2.04!\n");
  59.       exit(0);
  60.   }
  61.  
  62.   if (rda = ReadArgs("FILE/A,DRIVE/A,VERIFY/S,NOBANNER/S",arguments,NULL))
  63.   {
  64.     if (UnpackBase = OpenLibrary("unpack.library",42L))
  65.     {
  66.       if (cinfo = upAllocCInfo())
  67.       {
  68.         printf("unpTrack 1.0 (5/10-1995) by Thomas Neumann\n\n");
  69.  
  70.         cinfo->ui_TrackJump = (void (*)(void *, void *))Saver;
  71.         cinfo->ui_TrkErrJump = (void (*)(void *, void *))SaverError;
  72.         cinfo->ui_Flag = UFN_NoA4;
  73.         if (arguments[3] == 0)
  74.           cinfo->ui_Flag |= UFN_Banner;
  75.  
  76.         if ((upDetermineFile(cinfo,(char *)arguments[0])) == NULL)
  77.           error=1;
  78.         else
  79.           if ((cinfo->ui_CrunchType & 0x7f) != CRU_Track)
  80.             fprintf(stderr,"File not a track crunched file\n");
  81.           else
  82.             if ((dinfo = upUseDrive(cinfo,(char *)arguments[1])) == NULL)
  83.               error=1;
  84.             else
  85.             {
  86.               printf("Insert a disk in drive %s and press return.",arguments[1]);
  87.               while ((getchar() != 10)){}
  88.               printf("\nUnpacking track:  0\n");
  89.               if (upUnpack(cinfo) == 0)
  90.                 error=1;
  91.  
  92.               upUnuseDrive(dinfo);
  93.             }
  94.  
  95.         if (error != 0)
  96.           fprintf(stderr,"Unpack Error: %s\n",cinfo->ui_ErrorMsg);
  97.  
  98.         upFreeCInfo(cinfo);
  99.       }
  100.       CloseLibrary(UnpackBase);
  101.     }
  102.     else
  103.       fprintf(stderr,"You need the unpack.library version 42 or higher\n");
  104.  
  105.   FreeArgs(rda);
  106.   }
  107.   else
  108.     PrintFault(IoErr(),NULL);
  109. }
  110.  
  111.  
  112. __asm WORD Saver(register __a1 ULONG userdata, register __a2 struct UnpackInfo *ui)
  113. {
  114. UWORD track;
  115. UBYTE err;
  116. ULONG length;
  117. UBYTE *vermem;
  118. UBYTE *unpmem;
  119. int   i;
  120.  
  121.   track=ui->ui_Track;
  122.   length=ui->ui_DecrunchLen;
  123.   if (ui->ui_Offset == -1)          // Test to see if it's a banner
  124.   {
  125.     printf("%c%c%c\n",0x0b,0x9b,0x4b);
  126.     Write(Output(),ui->ui_DecrunchAdr,length);
  127.     printf("\n\n");
  128.   }
  129.   else
  130.   {
  131.     printf("%cWriting   track: %2d\n",0x0b,track);
  132.     err=upSendCmd(dinfo,ui->ui_DecrunchAdr,ui->ui_Offset,length,TD_FORMAT);
  133.  
  134.     if (err == 0)
  135.       if (arguments[2] != 0)
  136.       {
  137.         printf("%cVerifying track: %2d\n",0x0b,track);
  138.         if ((vermem=AllocMem(length,MEMF_CHIP)) != NULL)
  139.         {
  140.           err=upSendCmd(dinfo,vermem,ui->ui_Offset,length,CMD_READ);
  141.           unpmem=ui->ui_DecrunchAdr;
  142.  
  143.           if (err == 0)
  144.             for (i=0;i<length;i++)
  145.               if (unpmem[i] != vermem[i])   // Compare the two memory blocks
  146.                 err=255;
  147.  
  148.           FreeMem(vermem,length);
  149.           if (err == 255)
  150.           {
  151.             fprintf(stderr,"Verify error on track %d\n",track);
  152.             return(-1);
  153.           }
  154.         }
  155.       }
  156.  
  157.     if (err == 0)
  158.       if (track != 79)
  159.         printf("%cUnpacking track: %2d\n",0x0b,track+1);
  160.       else
  161.         printf("%c%c%c",0x0b,0x9b,0x4b);
  162.     else
  163.     {
  164.       fprintf(stderr,"We got error number %d from the device\n",err);
  165.       return(-1);
  166.     }
  167.   }
  168.   return(0);      // Note that you has to be sure your compile will return
  169. }                 // this value in register d0.
  170.  
  171.  
  172. __asm WORD SaverError(register __a1 ULONG userdata, register __a2 struct UnpackInfo *ui)
  173. {
  174.   return(-1);
  175. }
  176.