home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / dcopy12.arj / DCOPY.C next >
Encoding:
C/C++ Source or Header  |  1990-11-19  |  10.3 KB  |  276 lines

  1. /* Program DCOPY  V1.2
  2.     Written for Microsoft C 6.0, Nov 1990.
  3.     Copyright Richard Beal.
  4.     Medium memory model (essential for assembler routines).
  5.     Compile using: cl /AM dcopy.c rsecm wsecm
  6. */
  7. #include <stdio.h>              /* Standard I/O */
  8. #include <ctype.h>              /* Needed for string functions */
  9. #include <dos.h>                /* Needed for _dos_getdiskfree */
  10. #include <fcntl.h>              /* Needed for open() */
  11. #include <sys\types.h>          /* Needed for open() */
  12. #include <sys\stat.h>           /* Needed for open() */
  13. #include <io.h>                 /* Needed for open, close, read, write */
  14.  
  15. main(int argc,char *argv[]) {
  16.  
  17.     int _cdecl rsecm(char *data,unsigned drive,unsigned sector,
  18.     unsigned numsec);
  19.     int _cdecl wsecm(char *data,unsigned drive,unsigned sector,
  20.     unsigned numsec);
  21.  
  22.     static int        fht;           /* File handle for temporary file */
  23.     static char     tfname[21];    /* Path of temporary file */
  24.     static char     drives[3];       /* Drive specifier input */
  25.     static char     s[3];           /* Input string */
  26.     static unsigned drive;           /* Drive A=0 etc. */
  27.     static unsigned sector;        /* Sector on disk */
  28.     static char     rec[9216];       /* Record - max of 18 secs of 512 bytes */
  29.     static char     recv[9216];    /* Record for verify */
  30.     static unsigned err;           /* Error code from RSECM or WSECM */
  31.     static int        ret;           /* Return code */
  32.     static unsigned secend;        /* Last sector on disk */
  33.     static unsigned track;           /* Current track */
  34.     static unsigned side;           /* Current side */
  35.     static unsigned sectrk;        /* Sectors per track */
  36.     static unsigned trklen;        /* Bytes per track (512*sectrk) */
  37.     static struct    diskfree_t dfree;  /* Used for _dos_getdiskfree */
  38.     static unsigned totclust;       /* Total clusters on source disk */
  39.  
  40.     strcpy(tfname,"C:\\DCOPY.$$$");
  41.  
  42.     /* Initial message */
  43.     fprintf(stdout,"\nDCOPY V1.2 - Fast floppy disk copier\n");
  44.  
  45.     /* Check parameters and open instruction file */
  46.     if (argc != 2) {
  47.         fprintf(stdout,"Error - Correct usage is: DCOPY drive:\n");
  48.         exit(1);
  49.     }
  50.     strcpy(drives,argv[1]);
  51.     if (strlen(drives) != 2 || drives[1] != ':' || !isalpha(drives[0])) {
  52.        fprintf(stdout,"Error - Drive specification invalid: %s\n",drives);
  53.        exit(1);
  54.     }
  55.     drives[0]=toupper(drives[0]);
  56.     drive=drives[0]-65;         /* Convert A to 0, B to 1 etc. */
  57.  
  58.     /* Loop until no more disks to copy */
  59.     while (1) {
  60.  
  61.         /* Ask for source disk */
  62.         fprintf(stdout,"\nInsert SOURCE disk in drive %c:\n\n",drives[0]);
  63.         fprintf(stdout,"Press Enter when ready . . .\n");
  64.         fgets(s,2,stdin);
  65.  
  66.         /* Use _dos_getdiskfree to determine disk type */
  67.         if ((ret=_dos_getdiskfree(drive+1,&dfree)) != 0) {
  68.             fprintf(stdout,"Error - Invalid drive specified\n");
  69.             goto emore;
  70.         }
  71.         totclust=dfree.total_clusters;
  72.         switch (totclust) {
  73.         case 354:
  74.             secend=719;
  75.             sectrk=9;
  76.             fprintf(stdout,"Copying 360KB disk\n");
  77.             break;
  78.         case 2371:
  79.             secend=2399;
  80.             sectrk=15;
  81.             fprintf(stdout,"Copying 1.2MB disk\n");
  82.             break;
  83.         case 713:
  84.             secend=1439;
  85.             sectrk=9;
  86.             fprintf(stdout,"Copying 720KB disk\n");
  87.             break;
  88.         case 1415:
  89.             secend=1439;
  90.             sectrk=9;
  91.             fprintf(stdout,"Copying 720KB disk (old format)\n");
  92.             break;
  93.         case 2847:
  94.             secend=2879;
  95.             sectrk=18;
  96.             fprintf(stdout,"Copying 1.44MB disk\n");
  97.             break;
  98.         default:
  99.             fprintf(stdout,"Error - Invalid disk type - code = %u\n",
  100.             dfree.total_clusters);
  101.             goto emore;
  102.         }
  103.         trklen=512*sectrk;
  104.  
  105.         /* Open temporary file */
  106.         remove(tfname);
  107.         if ((fht=open(tfname,O_CREAT | O_WRONLY | O_BINARY,
  108.             S_IREAD | S_IWRITE)) == -1) {
  109.             fprintf(stdout,"\nError - Cannot create %s\n",tfname);
  110.             exit(1);
  111.         }
  112.  
  113.         /* Process each track (one side at a time) */
  114.         track=0;
  115.         side=0;
  116.         for (sector=0;sector <= secend;sector=sector+sectrk) {
  117.             fprintf(stdout,"\rTrack %u Side %u ",track,side);
  118.             err=1;
  119.             while (err != 0) {
  120.                 err=rsecm(rec,drive,sector,sectrk);
  121.                 if (err != 0) {
  122.                     fprintf(stdout,"\nError - Track %u Side %u "
  123.                         "unreadable - code = %04X\n",
  124.                         track,side,err);
  125.                     while (1) {
  126.                         fprintf(stdout,"Try again to read track (Y/N)? ");
  127.                         fgets(s,3,stdin);
  128.                         if ((s[0] == 'Y') || (s[0] == 'y')) break;
  129.                         if ((s[0] == 'N') || (s[0] == 'n')) goto eread;
  130.                     }
  131.                 }
  132.             }
  133.             if ((ret=write(fht,rec,trklen)) == -1) {
  134.                 fprintf(stdout,
  135.                     "\nError writing to %s - code %04X\n",tfname,ret);
  136.                 goto eread;
  137.             }
  138.             side=side+1;
  139.             if (side>1) {
  140.                 track=track+1;
  141.                 side=0;
  142.             }
  143.         }
  144.         if ((ret=close(fht)) != 0) {
  145.             fprintf(stdout,"\nError - Cannot close %s\n",tfname);
  146.             goto edel;
  147.         }
  148.         fprintf(stdout,"\n");
  149.  
  150.         /* Loop until no more disks to write */
  151.         while (1) {
  152.  
  153.             /* Ask for target disk */
  154.             fprintf(stdout,"\nInsert TARGET disk in drive %c:\n\n",drives[0]);
  155.             fprintf(stdout,"Press Enter when ready . . .\n");
  156.             fgets(s,2,stdin);
  157.  
  158.             /* Use _dos_getdiskfree to determine disk type */
  159.             if ((ret=_dos_getdiskfree(drive+1,&dfree)) != 0) {
  160.                 fprintf(stdout,"Error - Invalid drive specified\n");
  161.                 goto emore;
  162.             }
  163.             if (dfree.total_clusters != totclust) {
  164.                 if ((totclust == 354) && ((dfree.total_clusters == 713) ||
  165.                     (dfree.total_clusters == 1415)))
  166.                     fprintf(stdout,"Writing 360KB image onto 720KB disk\n");
  167.                 else {
  168.                     if (((totclust == 713) && (dfree.total_clusters == 1415))
  169.                       || ((totclust == 1415) && (dfree.total_clusters == 713)))
  170.                         fprintf(stdout,"Changing format of 720KB disk\n");
  171.                     else {
  172.                         fprintf(stdout,"Error - Target disk type incompatible "
  173.                             "- code = %u\n",dfree.total_clusters);
  174.                         goto enext;
  175.                     }
  176.                 }
  177.             }
  178.  
  179.             /* Open temporary file */
  180.             if ((fht=open(tfname,O_RDONLY | O_BINARY)) == -1) {
  181.                 fprintf(stdout,"\nError - Cannot open %s\n",tfname);
  182.                 goto edel;
  183.             }
  184.  
  185.             /* Process each track */
  186.             track=0;
  187.             side=0;
  188.             for (sector=0;sector <= secend;sector=sector+sectrk) {
  189.                 if ((ret=read(fht,rec,trklen)) <= 0) {
  190.                     fprintf(stdout,"\nError - Cannot read %s - code %04X\n",
  191.                         tfname,ret);
  192.                     goto ewrite;
  193.                 }
  194.                 fprintf(stdout,"\rTrack %u Side %u ",track,side);
  195.                 err=1;
  196.                 while (err != 0) {
  197.                     err=wsecm(rec,drive,sector,sectrk);     /* Write */
  198.                     if (err != 0) {
  199.                         fprintf(stdout,"\nError - Track %u Side %u "
  200.                             "unwritable - code = %04X\n",
  201.                             track,side,err);
  202.                         while (1) {
  203.                             fprintf(stdout,"\nTry again to write "
  204.                                 "track (Y/N)? ");
  205.                             fgets(s,3,stdin);
  206.                             if ((s[0] == 'Y') || (s[0] == 'y')) break;
  207.                             if ((s[0] == 'N') || (s[0] == 'n')) goto ewrite;
  208.                         }
  209.                     }
  210.                     if (err != 0) continue;
  211.                     err=rsecm(recv,drive,sector,sectrk);    /* Verify */
  212.                     if (err != 0) {
  213.                         fprintf(stdout,"\nError - Track %u Side %u "
  214.                             "unreadable - code = %04X\n",
  215.                             track,side,err);
  216.                         while (1) {
  217.                             fprintf(stdout,"\nTry again to write "
  218.                                 "track (Y/N)? ");
  219.                             fgets(s,3,stdin);
  220.                             if ((s[0] == 'Y') || (s[0] == 'y')) break;
  221.                             if ((s[0] == 'N') || (s[0] == 'n')) goto ewrite;
  222.                         }
  223.                     }
  224.                 }
  225.                 side=side+1;
  226.                 if (side>1) {
  227.                     track=track+1;
  228.                     side=0;
  229.                 }
  230.             }
  231.  
  232.             /* Close temporary file */
  233. ewrite:     if ((ret=close(fht)) != 0) {
  234.                 fprintf(stdout,"\nError - Cannot close %s\n",tfname);
  235.                 goto edel;
  236.             }
  237.             fprintf(stdout,"\n");
  238.  
  239.             /* Ask if more disks to write */
  240. enext:      while (1) {
  241.                 fprintf(stdout,"\nWrite another copy of the disk (Y/N)? ");
  242.                 fgets(s,3,stdin);
  243.                 if ((s[0] == 'Y') || (s[0] == 'y')) break;
  244.                 if ((s[0] == 'N') || (s[0] == 'n')) goto edel;
  245.             }
  246.         }
  247.  
  248.         /* Close file after an error */
  249. eread:  if ((ret=close(fht)) != 0) {
  250.             fprintf(stdout,"\nError - Cannot close %s\n",tfname);
  251.             exit(1);
  252.         }
  253.  
  254.         /* Delete temporary file */
  255. edel:    if ((ret=remove(tfname)) != 0) {
  256.             fprintf(stdout,"\nError - Cannot delete %s\n",tfname);
  257.             exit(1);
  258.         }
  259.  
  260.         /* Ask if more disks to copy */
  261. emore:  while (1) {
  262.             fprintf(stdout,"\nCopy another disk (Y/N)? ");
  263.             fgets(s,3,stdin);
  264.             if ((s[0] == 'Y') || (s[0] == 'y')) break;
  265.             if ((s[0] == 'N') || (s[0] == 'n')) break;
  266.         }
  267.         if ((s[0] != 'Y') && (s[0] != 'y')) break;
  268.     }
  269.  
  270.     /* Final messages */
  271.     fprintf(stdout,"IMPORTANT - Remove disk from drive %c:\n",
  272.         drives[0]);
  273.     fprintf(stdout,"End of program DCOPY\n");
  274.     exit(0);
  275. }
  276.