home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / diskutil / tapebios / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-06  |  4.2 KB  |  166 lines

  1. /* Dump a partition to tape                */
  2. /* This program is part of the TapeBIOS distribution    */
  3. /* Written by Alan Hourihane 1992            */
  4. /* Needs TapeBIOS driver loaded                */
  5.  
  6. /* Include the necessary header files */
  7. #include <osbind.h>
  8. #include <stdio.h>
  9. #include "tapebind.h"
  10.  
  11. /* Declare externals */
  12. extern void strncpy();
  13. extern long get_cookie();
  14. extern char toupper();
  15. extern int strcmp();
  16. extern int strlen();
  17. extern void exit();
  18.  
  19. #define DMAMAX        64*1024     /* 64Kbyte buffer */
  20. #define DUMPNAME    20        /* Max. length of Dumpname */
  21. #define NUMLEN        10        /* Max. length of number */
  22. #define RABS        0        /* Read for RWABS */
  23. #define IDPOS        64        /* Position of dumpname in field */
  24.  
  25. typedef struct {            /* Structure for BPB */
  26.     short    recsiz;
  27.     short    clsiz;
  28.     short    clsizb;
  29.     short    rdlen;
  30.     short    fsiz;
  31.     short    fatrec;
  32.     short    datrec;
  33.     short    numcl;
  34.     short    bflags;
  35. } bpb;
  36.  
  37. int
  38. main(argc, argv)
  39. int argc;
  40. char *argv[];
  41. {
  42.     long maxspace;        /* Malloc area */
  43.     char drive;        /* Drive to be dumped */
  44.     char name[DUMPNAME];    /* name of dump */
  45.     bpb *srcbpb;        /* Structure for BPB */
  46.     long numsectors;    /* Number of sectors on drive */
  47.     int sectorsize;        /* Sector Size */
  48.     char buffer[512];    /* Header buffer */
  49.     char *obuf;        /* Buffer area for dump */
  50.     int status;        /* Status of tape command */
  51.     int s,n;        /* Temporary storage */
  52.     int nscts;        /* Calculated number of sectors */
  53.     int drivelist;        /* Drivelist for dump */
  54.  
  55.     printf("Dump Disk Drives to Tape. Copyright 1992, A. Hourihane.\r\n");
  56.     
  57.     if (argc < 3 || (argc < 4 && !strcmp(argv[1],"-a"))) {
  58.         printf("Usage: %s [-a] [drivelist] [dumpname_a] [dumpname_b] [etc..]\r\n",argv[0]);
  59.         printf("e.g.   %s cde boot apps general\r\n",argv[0]);
  60.         exit(1);
  61.     }
  62.  
  63.     if (!get_cookie("TAPE")) {
  64.         printf("TapeBIOS is not installed.\r\n");
  65.         exit(1);
  66.     }
  67.  
  68.     status = Tload(0L);
  69.     status = Trewind();
  70.     if (status == 2) {
  71.         fprintf(stderr, "Failed to initialise tape drive.");
  72.         status = Trewind();
  73.         status = Tunload(0L);
  74.         exit(4);
  75.     }    
  76.  
  77.     if (!strcmp(argv[1],"-a")) {
  78.         argv++;
  79.         argc--;
  80.         status = Tspace(50331648L);
  81.         if (status == 2) {
  82.             fprintf(stderr, "Error spacing to End-of-Recorded Media.");
  83.             status = Trewind();
  84.             status = Tunload(0L);
  85.             exit(4);
  86.         }
  87.     }
  88.  
  89.     for (drivelist=0;drivelist<strlen(argv[1]);drivelist++) {
  90.     
  91.     drive = toupper(argv[1][drivelist])-'A';
  92.  
  93.     if (drive<0 || drive>16) {
  94.         fprintf(stderr, "Invalid drive %s\r\n", argv[1]);
  95.         exit(1);
  96.     }
  97.  
  98.     if (strlen(argv[2+drivelist])>DUMPNAME) {
  99.         fprintf(stderr, "Maximum dump name length is %d characters\r\n",DUMPNAME);
  100.         exit(1);
  101.     }
  102.  
  103.     strncpy(name, argv[2+drivelist], DUMPNAME);
  104.  
  105.     srcbpb = (bpb*)Getbpb(drive);
  106.  
  107.     if (srcbpb==NULL) {
  108.         fprintf(stderr, "Drive %s does not exist\r\n", argv[1]);
  109.         exit(2);
  110.     }
  111.  
  112.     numsectors = srcbpb->datrec+srcbpb->clsiz*srcbpb->numcl;
  113.     sectorsize = srcbpb->recsiz;
  114.     printf("Dumping drive %c to tape. Size = %.1fK\r\n",drive+'A',(float)numsectors*(float)sectorsize/1024);
  115.  
  116.     maxspace = Malloc(-1L);
  117.     if (maxspace < DMAMAX) {
  118.         fprintf(stderr, "Not enough memory for buffer allocation.");
  119.         exit(3);
  120.     }
  121.     maxspace = DMAMAX;
  122.     obuf=(char*)Malloc(maxspace);
  123.     if (!obuf) {
  124.         fprintf(stderr, "Cannot malloc buffer\r\n");
  125.         exit(3);
  126.     }
  127.     if ((long)obuf&1) obuf++;    /* Word align it */
  128.     nscts = maxspace/sectorsize;
  129.  
  130.     buffer[0]=(char)(drive+'A');
  131.     sprintf(&buffer[1],"%d",sectorsize);
  132.     sprintf(&buffer[1+NUMLEN],"%d",numsectors);
  133.     strncpy(&buffer[1+NUMLEN*2],name,DUMPNAME);
  134.     sprintf(&buffer[IDPOS],"DUMPFORMAT");
  135.  
  136.     status = Twrite(buffer,1);
  137.     if (status != 0) {
  138.         fprintf(stderr, "Error writing header block to tape\r\n");
  139.         exit(4);
  140.     }
  141.  
  142.     s = 0;
  143.     do {
  144.         n = (numsectors>nscts ? nscts : numsectors);
  145.         status = Rwabs(RABS, &obuf[0], n, s, (int)drive);
  146.         if (status < 0) {
  147.             fprintf(stderr, "\nError %d reading from drive\r\n",status);
  148.             exit(4);
  149.         }
  150.         status = Twrite(obuf,(long)((n*sectorsize)/512));
  151.         printf("Dumped: %dK\r", (s*sectorsize/1024));
  152.         if (status != 0) {
  153.             fprintf(stderr, "\nError writing to tape.\r\n");
  154.             exit(4);
  155.         }
  156.         s+=n;
  157.         numsectors-=n;
  158.     } while(numsectors>0);
  159.     status = Mfree(obuf); 
  160.     }
  161.     printf("\r\n"); 
  162.     status = Trewind();
  163.     status = Tunload(0L);
  164.     return 0;        /* Keep -Wall happy */
  165. }
  166.