home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 139 / c / ramflop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-13  |  9.1 KB  |  175 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     ramflop.c - Track copy the RAMdisk onto a floppy             */
  4. /*                                                                          */
  5. /* Programmer: George R. Woodside                                           */
  6. /*                                                                          */
  7. /* Date:       February 22, 1987                                            */
  8. /*                                                                          */
  9. /****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <osbind.h>
  13.  
  14. #define  RMAGIC  0x1234543L
  15. #define  OHEAD   8704
  16.  
  17. /****************************************************************************/
  18. /*                                                                          */
  19. /* Structure returned from a Dfree() call                                   */
  20. /*                                                                          */
  21. /****************************************************************************/
  22.  
  23. struct frees 
  24. {
  25.   long  sp_clust;                       /* number of free clusters          */
  26.   long  sp_totc;                        /* total number of clusters         */
  27.   long  sp_ssize;                       /* sector size in bytes             */
  28.   long  sp_csize;                       /* cluster size in sectors          */
  29. };
  30.  
  31. /****************************************************************************/
  32. /*                                                                          */
  33. /* Structure of a BIOS Parameter Block                                      */
  34. /*                                                                          */
  35. /****************************************************************************/
  36.  
  37. struct bpb 
  38. {
  39.   int       byt_sect;                   /* bytes per sector                 */
  40.   int       sec_clust;                  /* sectors per cluster              */
  41.   int       byt_clust;                  /* bytes per cluster                */
  42.   int       dir_len;                    /* directory length                 */
  43.   int       fat_size;                   /* file allocation table size       */
  44.   int       fat2_start;                 /* start of second copy of fat      */
  45.   int       clust_start;                /* start of data clusters           */
  46.   int       clusters;                   /* number of clusters               */
  47.   int       flags;                      /* parameter flags                  */
  48.   long      ram_start;                  /* RAMdisk only! RAM start address  */
  49.   long      ram_magic;                  /* RAMdisk only! Magic number       */
  50. };
  51.  
  52.   struct  bpb  *bpb_at;                 /* assign a pointer                 */
  53.  
  54. /****************************************************************************/
  55. /*                                                                          */
  56. /* Main - Test for a drive designator.                                      */
  57. /*        If valid, be sure the contents of the floppy                      */
  58. /*        will fit into the active RAMdisk.                                 */
  59. /*        If they will, copy them.                                          */
  60. /*                                                                          */
  61. /****************************************************************************/
  62.  
  63. main(argc,argv)
  64. int    argc;
  65. char  *argv[];
  66. {
  67.  
  68.   register  int  drive = -1;            /* drive to read                    */
  69.   register  int  sectors;               /* number of sectors to read        */
  70.   register  long start;                 /* save RAMdisk data start here     */
  71.  
  72.   if(argc > 1)                          /* if a drive entered,              */
  73.     {
  74.       if( ( *argv[1] == 'a') || ( *argv[1] == 'A') ) /* if drive a,         */
  75.         drive = 0;                      /* set it                           */
  76.  
  77.       if( ( *argv[1] == 'b') || ( *argv[1] == 'B') ) /* if drive b,         */
  78.         drive = 1;                      /* set it                           */
  79.     }                                   /* end argument entered             */
  80.   else                                  /* if no argument entered,          */
  81.     {
  82.       printf("Which floppy drive (a or b)? \n"); /* prompt for drive        */
  83.       drive = Cconin();                 /* read a key                       */
  84.  
  85.       if( ( drive == 'a') || ( drive == 'A') ) /* if drive a,               */
  86.         drive = 0;                      /* set it                           */
  87.  
  88.       if( ( drive == 'b') || ( drive == 'B') ) /* if drive b,               */
  89.         drive = 1;                      /* set it                           */
  90.     }                                   /* end prompt for drive             */
  91.  
  92.   if( (drive != 0) && (drive != 1) )    /* if drive still not known,        */
  93.     {
  94.       printf("flopram: Invalid floppy drive.\n"); /* log an error           */
  95.       exit(1);                          /* and punt                         */
  96.     }
  97.  
  98.   bpb_at = (struct bpb *)Getbpb(12);    /* get RAMdisk BPB                  */
  99.  
  100.   if(bpb_at == (struct bpb *) NULL)     /* if no RAMdisk,                   */
  101.     {
  102.       printf("flopram: No active RAMdisk.\n"); /* log an error              */
  103.       exit(1);                          /* and punt                         */
  104.     }
  105.  
  106.   if(bpb_at->ram_magic != RMAGIC)       /* if wrong RAmdisk,                */
  107.     {
  108.       printf("flopram: Wrong RAMdisk running.\n"); /* log an error          */
  109.       exit(1);                          /* and punt                         */
  110.     }
  111.  
  112.   start = bpb_at->ram_start;            /* get data address                 */
  113.  
  114.   Getbpb(drive);                        /* insure drive type known          */
  115.  
  116.   sectors = sp_used(start+512L);        /* compute sectors used on RAMdisk  */
  117.  
  118.   put_some(drive,1,start + 512L,sectors + 17); /* write the data            */
  119.  
  120.   start = Rwabs(0,0L,2,0,drive);        /* mark media as changed            */
  121. }                                       /* end main                         */
  122.  
  123. /****************************************************************************/
  124. /*                                                                          */
  125. /* Compute sectors used in data area of drive.                              */
  126. /* FATs are 12 bits on a floppy, or floppy compatible RAmdisk.              */
  127. /*                                                                          */
  128. /* Start at the end of the FAT table, and back up to the first              */
  129. /* non-zero int. This is the last FAT used. Convert ints to bits, and       */
  130. /* round up to insure a full FAT entry. Divide by 12 to get FATs, or        */
  131. /* clusters, in use. Multiply clusters * 2 for sectors.                     */
  132. /*                                                                          */
  133. /****************************************************************************/
  134.  
  135. sp_used(dstart)
  136. long   dstart;                          /* overhead start address           */
  137. {
  138.   register  int  *fatptr = (int *)(dstart + 1066L); /* end of FATs          */
  139.   register  int   i = 534;              /* ints in a FAT table + 1          */
  140.  
  141.   while(*fatptr-- == 0)                 /* until an active entry,           */
  142.     i--;                                /* count down the FATs              */
  143.  
  144.   i *= sizeof(int);                     /* convert to bytes                 */
  145.   i *= 8;                               /* convert to bits                  */
  146.   i += 11;                              /* round up for divide              */
  147.   i /= 12;                              /* number of FATs or clusters       */
  148.   i *= 2;                               /* convert FATs to sectors          */
  149.  
  150.   return(i-4);                          /* FATs 0 & 1 do not exist, so -4   */
  151. }
  152.  
  153. /****************************************************************************/
  154. /*                                                                          */
  155. /* Write a portion of the floppy.                                           */
  156. /* Using Rwabs eliminates track and sector counting, but requires           */
  157. /* that Mediach not be set. Mediach is checked before this call.            */
  158. /*                                                                          */
  159. /****************************************************************************/
  160.  
  161. put_some(drv,strt,buf,cnt)
  162. int   drv;                              /* drive to write                   */
  163. int   strt;                             /* starting sector number           */
  164. char  *buf;                             /* buffer address                   */
  165. int   cnt;                              /* sector count                     */
  166. {
  167.   register  long  reply;
  168.  
  169.   if((reply = Rwabs(1,buf,cnt,strt,drv) ) != 0L) /* if an error             */
  170.     {
  171.       printf("flopram: I/O error %D.\n"); /* log an error                   */
  172.       exit(1);                          /* and punt                         */
  173.     }
  174. }
  175.