home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / utils.lbr / IBMREAD.CQ / IBMREAD.C
Encoding:
C/C++ Source or Header  |  1986-05-22  |  6.0 KB  |  249 lines

  1. /* -*-c,save,indent:8-*- */
  2. /* IBM disk reader R. heller aug 1984 */
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include "config.h"
  6. #define SPT 8
  7. #define SPC 2
  8. #define BPS 512
  9. #define BPC (BPS*SPC)
  10. #define FAT_START 2
  11. #define FAT_SIZE (BPS)
  12. #define DIR_START (FAT_START+1)
  13. #define DIR_LEN (7*BPS)
  14. #define NUM_FCBS 112
  15. #define DATA_START (DIR_START+7)
  16. #define FDEL 0xe5
  17. #define FDIR 0x2e
  18. #define EOF_CHAR (26)
  19. #define NIL_CLUSTER 0x0FFF
  20. typedef struct {
  21.     char fname[8];
  22.     char ftype[3];
  23.     char filler[15];
  24.     char firstclus[2];
  25.     char fsize[4];
  26.     } FCB;
  27. typedef union {
  28.     short int w;
  29.     char b[2];
  30.     } swword;
  31. typedef union {
  32.     long int l;
  33.     char bs[4];
  34.     } swl32;
  35.  
  36. #define CONFIGCHN 128
  37. #define BIOSCHMAP 1L
  38. #define BIOSCHSIZ 32
  39. #define BIOSCHTYP unsigned short int
  40. #define GENSYSCONF 0
  41.  
  42. static short int CPMDRCHN[16] = 
  43.     {4,5,9,10,11,12,13,14,15,16,17,18,19,20,21,22};
  44.  
  45. static BIOSCHTYP BIOS_CH_MAP[BIOSCHSIZ];
  46.  
  47.  
  48. main(argc,argv)
  49. int argc;
  50. char *argv[];
  51. {static char FAT[FAT_SIZE];
  52.  static FCB DIR[NUM_FCBS];
  53.  static CONFIGBLK sageconf,ibmconf;
  54.  int disk_unit;
  55.  char *out_prefix;
  56.  char filename[20];
  57.  int block_number,cluster;
  58.  register long int size;
  59.  static char block_buffer[BPC];
  60.  register int have_cluster;
  61.  int err_stat;
  62.  register int i,j;
  63.  register char *p,*q;
  64.  register FILE *outfile;
  65.  FILE *fopenb();
  66.  swword xcluster;
  67.  swl32 xsize;
  68.  register int text_flag;
  69.  BIOSCHTYP trans_phy();
  70.  
  71.     text_flag = (-1);
  72.     if (argc < 3 || argc > 4) {
  73.         fprintf(stderr,
  74.             "Usage: ibmread sourcedrv outdevice [textflag]\n");
  75.         abort(1);
  76.         }
  77.     disk_unit = **++argv;
  78.     disk_unit = toupper(disk_unit);
  79.     if (disk_unit < 'A' || disk_unit > 'P') {
  80.         fprintf(stderr,
  81.             "ibmread: illegal drive letter: %c\n",disk_unit);
  82.         abort(disk_unit);
  83.         }
  84.     i = trans_phy(disk_unit - 'A');
  85.     if (i != 4 && i != 5) {
  86.         fprintf(stderr,
  87.             "ibmread: drive not floppy - %c\n",disk_unit);
  88.         abort(disk_unit);
  89.         }
  90.     disk_unit = CPMDRCHN[disk_unit - 'A'];
  91.     out_prefix = *++argv;
  92.     if (argc == 4) text_flag = 0;
  93.     err_stat = getconfig(&sageconf,disk_unit);
  94.     if (err_stat != 0) {
  95.         fprintf(stderr,
  96.             "ibmread: Error getting disk config: %d\n",
  97.             err_stat);
  98.         abort(err_stat);
  99.         }
  100.     err_stat = getconfig(&ibmconf,disk_unit);
  101.     if (err_stat != 0) {
  102.         fprintf(stderr,
  103.             "ibmread: Error getting disk config: %d\n",
  104.             err_stat);
  105.         abort(err_stat);
  106.         }
  107.     ibmconf.num_cylinders = 40;
  108.     ibmconf.double_step = 1;
  109.     err_stat = setconfig(&ibmconf,disk_unit);
  110.     if (err_stat != 0) {
  111.         fprintf(stderr,
  112.             "ibmread: Error setting IBM disk config: %d\n",
  113.             err_stat);
  114.         abort(err_stat);
  115.         }
  116.     err_stat = read_block(disk_unit,FAT,sizeof(FAT),FAT_START);
  117.     if (err_stat != 0) {
  118.         fprintf(stderr,
  119.             "ibmread: Error reading in FAT: status is %d\n",
  120.             err_stat);
  121.         setconfig(&sageconf,disk_unit);
  122.         abort(2);
  123.         }
  124.     err_stat = read_block(disk_unit,DIR,sizeof(DIR),DIR_START);
  125.     if (err_stat != 0) {
  126.         fprintf(stderr,
  127.             "ibmread: Error reading in DIR: status is %d\n",
  128.             err_stat);
  129.         setconfig(&sageconf,disk_unit);
  130.         abort(3);
  131.         }
  132.     for(i=0;i<NUM_FCBS;i++) {
  133.       if (DIR[i].fname[0] != 0 &&
  134.           DIR[i].fname[0] != FDEL &&
  135.           DIR[i].fname[0] != FDIR) {
  136.         strcpy(filename,out_prefix);
  137.         p = &filename[strlen(out_prefix)];
  138.         q = &DIR[i].fname[0];
  139.         for (j=0;j<8;j++)
  140.             if (*q == ' ') break;
  141.             else *p++ = *q++;
  142.         *p++ = '.';
  143.         q = &DIR[i].ftype[0];
  144.         for (j=0;j<3;j++)
  145.             if (*q == ' ') break;
  146.             else *p++ = *q++;
  147.         *p = '\0';
  148.         printf("Creating %s\n",filename);
  149.         if (text_flag > 0) text_flag = 0;
  150.         outfile = fopenb(filename,"w");
  151.         if (outfile == NULL) {
  152.             perror("ibmread");
  153.             break;
  154.             }
  155.         xcluster.b[0] = DIR[i].firstclus[1];
  156.         xcluster.b[1] = DIR[i].firstclus[0];
  157.         cluster = xcluster.w;
  158.         xsize.bs[0] = DIR[i].fsize[3];
  159.         xsize.bs[1] = DIR[i].fsize[2];
  160.         xsize.bs[2] = DIR[i].fsize[1];
  161.         xsize.bs[3] = DIR[i].fsize[0];
  162.         size = xsize.l;
  163.         printf("    file size: %D\n",size);
  164.         have_cluster = NO;
  165.         while (size > 0) {
  166.             if (!have_cluster) {
  167.                 block_number = ((cluster-2)*2)+DATA_START;
  168.         printf("Reading Cluster %d (block %d; next cluster= ",
  169.             cluster,block_number);
  170.                 err_stat = read_block(disk_unit,
  171.                               block_buffer,
  172.                               sizeof(block_buffer),
  173.                               block_number);
  174.                 if (err_stat != 0) {
  175.                     fprintf(stderr,
  176.             "ibmread: Error reading cluster %d; status is %d\n",
  177.                         cluster,
  178.                         err_stat);
  179.                     setconfig(&sageconf,disk_unit);
  180.                     abort(4);
  181.                     }
  182.                 p = &block_buffer[0];
  183.                 cluster = 
  184.                     fat_cdr(fat_index(cluster),
  185.                         ((cluster & 01) == 01),
  186.                         FAT);
  187.                 printf("%d)\r",cluster);
  188.                 have_cluster = YES;
  189.             }
  190.             if (text_flag > 0 && *p == EOF_CHAR) 
  191.                 fputc('\n',outfile);
  192.             if (text_flag != 0) fputc(*p,outfile);
  193.             p++; size--; if (text_flag == 0) text_flag = 1;
  194.             have_cluster = p < &block_buffer[BPC];
  195.         }
  196.         printf("\n");
  197.         fclose(outfile);
  198.     }
  199.       }
  200.     setconfig(&sageconf,disk_unit);
  201.  
  202. }
  203. fat_index(cluster)
  204. register int cluster;
  205. {
  206.     return(cluster + (cluster >> 1));
  207. }
  208. fat_cdr(indx,odd_even,fat)
  209. register int indx,odd_even;
  210. register char *fat;
  211. {swword elt;
  212.  register int i;
  213.  
  214.     elt.b[0] = *(fat+indx+1);
  215.     elt.b[1] = *(fat+indx);
  216.     if (odd_even) { i = elt.w >> 4;
  217.                 return((i & 0x0fff));
  218.             }
  219.     else return((elt.w & 0x0fff));
  220. }
  221. BIOSCHTYP trans_phy(cpmdrv)
  222. register int cpmdrv;
  223. {
  224.     uread(CONFIGCHN,BIOS_CH_MAP,(long int)(sizeof(BIOSCHTYP)*BIOSCHSIZ),
  225.       BIOSCHMAP,GENSYSCONF);
  226.     return(BIOS_CH_MAP[CPMDRCHN[cpmdrv]]);
  227.     }
  228. read_block(chan,buff,buffsiz,blockno)
  229. int chan;
  230. char *buff;
  231. int buffsiz,blockno;
  232. {
  233.     return(uread(chan,buff,(long int)buffsiz,(long int)blockno,0));
  234.     }
  235. getconfig(configbuf,chan)
  236. CONFIGBLK *configbuf;
  237. int chan;
  238. {
  239.     return(uread(CONFIGCHN,configbuf,(long int)sizeof(CONFIGBLK),0L,
  240.              chan));
  241.     }
  242. setconfig(configbuf,chan)
  243. CONFIGBLK *configbuf;
  244. int chan;
  245. {
  246.     return(uwrite(CONFIGCHN,configbuf,(long int)sizeof(CONFIGBLK),0L,
  247.               chan));
  248.     }
  249.