home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / disk / misc / TransADF.lha / TransADF / Source / write_disk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  5.7 KB  |  187 lines

  1. /* write-disk.c - Write a file straight onto a disk
  2. ** Copyright (C) 1997,1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <dos/dos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/dos_protos.h>
  24.  
  25. #include <string.h>
  26.  
  27. #include "write_disk.h"
  28. #include "main.h"
  29. #include "device.h"
  30. #include "mem_chunks.h"
  31. #include "util.h"
  32. #include "errors.h"
  33.  
  34.  
  35. #define WD_TBTRACKS 1                           /* Tracks in wd_TrackBuf */
  36. #define WD_TBSIZE   (WD_TBTRACKS * trackSize)   /* Bytes in wd_TrackBuf  */
  37.  
  38. UBYTE *wd_TrackBuf;    /* Our input buffer - reads from file.    */
  39. UBYTE *wd_VTrackBuf;   /* Buffer for verifying data if required. */
  40.  
  41. /*
  42. ** Writes a file into a disk.
  43. ** Expects all fields of adfPkt to be set.
  44. */
  45. void writeDisk (struct ADF_Packet *adfPkt)
  46. {
  47.   BYTE IOError;
  48.   LONG DOSError, RWSize;
  49.   int i;
  50.   
  51.   
  52.   /* Output info header */
  53.   FPrintf (StdOut, "Writing from %s to %s unit %ld (%s:).\n", 
  54.                    adfPkt->ADFileName,
  55.                    adfPkt->devInfo->deviceName,
  56.                    adfPkt->devInfo->deviceUnit,
  57.                    adfPkt->devInfo->dosName);
  58.   
  59.   FPrintf (StdOut, "Starting at track %ld, Ending at track %ld.\n",
  60.                    (adfPkt->startTrack/adfPkt->devInfo->numHeads),
  61.                    (adfPkt->endTrack/adfPkt->devInfo->numHeads));
  62.   
  63.   /* Allocate track buffers */
  64.   wd_TrackBuf = (UBYTE *) myAllocMem (WD_TBSIZE, MEMF_CLEAR);
  65.   if (!wd_TrackBuf)
  66.   {
  67.     /* Out of memory! */
  68.     FPrintf (StdErr, "%s: Out of memory.\n", ProgName);
  69.     cleanExit (RETURN_FAIL, ERROR_NO_FREE_STORE);
  70.   }
  71.   
  72.   if (adfPkt->verify)
  73.   {
  74.     wd_VTrackBuf = (UBYTE *) myAllocMem (WD_TBSIZE, MEMF_CLEAR);
  75.     if (!wd_VTrackBuf)
  76.     {
  77.       /* Out of memory! */
  78.       FPrintf (StdErr, "%s: Out of memory.\n", ProgName);
  79.       cleanExit (RETURN_FAIL, ERROR_NO_FREE_STORE);
  80.     }
  81.   }
  82.   
  83.   /* Start writing */
  84.   for (i = adfPkt->startTrack; i <= adfPkt->endTrack; i++)
  85.   {
  86.     /* Check for Control-C break */
  87.     if (CTRL_C)
  88.     {
  89.       FPutC (StdOut, '\n');
  90.       FPrintf (StdErr, "%s - %s\n", breakText, ProgName);
  91.       cleanExit (RETURN_WARN, NULL);
  92.     }
  93.     
  94.     /* Fill the buffer from file */
  95.     RWSize = Read (adfPkt->ADFile, wd_TrackBuf, WD_TBSIZE);
  96.     if (RWSize != WD_TBSIZE)
  97.     {
  98.       DOSError = IoErr();
  99.       
  100.       FPutC (StdOut, '\n');
  101.       FPrintf (StdErr, "%s: Error reading from %s - ", ProgName,
  102.                                                        adfPkt->ADFileName);
  103.       if (DOSError)
  104.         reportDOSError (DOSError);
  105.       else
  106.         FPuts (StdErr, "File to short.\n");
  107.       
  108.       cleanExit (RETURN_ERROR, DOSError);
  109.     }
  110.     
  111.     if (adfPkt->format)
  112.     {
  113.       /* Format the track */
  114.       
  115.       /* Update progress information */
  116.       FPrintf (StdOut, "\rFormatting track %ld side %ld", 
  117.                        (i/adfPkt->devInfo->numHeads),
  118.                        (i%adfPkt->devInfo->numHeads));
  119.       Flush (StdOut);
  120.       
  121.       /* Format the current track */
  122.       IOError = formatTrack (wd_TrackBuf, WD_TBTRACKS, i, adfPkt->diskReq);
  123.       if (IOError)
  124.       {
  125.         FPutC (StdOut, '\n');
  126.         FPrintf (StdErr, "%s: Error formatting %s: - ",ProgName,
  127.                                                        adfPkt->devInfo->dosName);
  128.         reportIOError (IOError);
  129.         cleanExit (RETURN_ERROR, NULL);
  130.       }
  131.     }
  132.     
  133.     /* Update progress information */
  134.     FPrintf (StdOut, "\rWriting    track %ld side %ld", 
  135.                      (i/adfPkt->devInfo->numHeads),
  136.                      (i%adfPkt->devInfo->numHeads));
  137.     Flush (StdOut);
  138.     
  139.     /* Write the buffer to the disk */
  140.     IOError = writeTrack (wd_TrackBuf, WD_TBTRACKS, i, adfPkt->diskReq);
  141.     if (IOError)
  142.     {
  143.       FPutC (StdOut, '\n');
  144.       FPrintf (StdErr, "%s: Error writing to %s: - ",ProgName,
  145.                                                      adfPkt->devInfo->dosName);
  146.       reportIOError (IOError);
  147.       cleanExit (RETURN_ERROR, NULL);
  148.     }
  149.     
  150.     
  151.     if (adfPkt->verify)
  152.     {
  153.       /* Verify that the data was written correctly */
  154.       FPrintf (StdOut, "\rVerifying  track %ld side %ld", 
  155.                        (i/adfPkt->devInfo->numHeads),
  156.                        (i%adfPkt->devInfo->numHeads));
  157.       Flush (StdOut);
  158.       
  159.       flushTrack (adfPkt->diskReq);
  160.       IOError = readTrack (wd_VTrackBuf, WD_TBTRACKS, i, adfPkt->diskReq);
  161.       if (IOError)
  162.       {
  163.         FPutC (StdOut, '\n');
  164.         FPrintf (StdErr, "%s: Error reading from %s - ", ProgName,
  165.                                                          adfPkt->devInfo->dosName);
  166.         reportIOError (IOError);
  167.         cleanExit (RETURN_ERROR, NULL);
  168.       }
  169.       
  170.       /* Check that the two buffers are identical */
  171.       if (memcmp (wd_TrackBuf, wd_VTrackBuf, WD_TBSIZE) != 0)
  172.       {
  173.         /* Verification error */
  174.         FPutC (StdOut, '\n');
  175.         FPrintf (StdErr, "%s: Verification Error on %s:\n", ProgName,
  176.                                                             adfPkt->devInfo->dosName);
  177.         cleanExit (RETURN_ERROR, NULL);
  178.       }
  179.     }
  180.   }
  181.   
  182.   /* Free buffers */
  183.   myFreeMem (wd_TrackBuf);
  184.   if (wd_VTrackBuf) 
  185.     myFreeMem (wd_VTrackBuf); 
  186. }
  187.