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

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