home *** CD-ROM | disk | FTP | other *** search
- /* Program DCOPY V1.2
- Written for Microsoft C 6.0, Nov 1990.
- Copyright Richard Beal.
- Medium memory model (essential for assembler routines).
- Compile using: cl /AM dcopy.c rsecm wsecm
- */
- #include <stdio.h> /* Standard I/O */
- #include <ctype.h> /* Needed for string functions */
- #include <dos.h> /* Needed for _dos_getdiskfree */
- #include <fcntl.h> /* Needed for open() */
- #include <sys\types.h> /* Needed for open() */
- #include <sys\stat.h> /* Needed for open() */
- #include <io.h> /* Needed for open, close, read, write */
-
- main(int argc,char *argv[]) {
-
- int _cdecl rsecm(char *data,unsigned drive,unsigned sector,
- unsigned numsec);
- int _cdecl wsecm(char *data,unsigned drive,unsigned sector,
- unsigned numsec);
-
- static int fht; /* File handle for temporary file */
- static char tfname[21]; /* Path of temporary file */
- static char drives[3]; /* Drive specifier input */
- static char s[3]; /* Input string */
- static unsigned drive; /* Drive A=0 etc. */
- static unsigned sector; /* Sector on disk */
- static char rec[9216]; /* Record - max of 18 secs of 512 bytes */
- static char recv[9216]; /* Record for verify */
- static unsigned err; /* Error code from RSECM or WSECM */
- static int ret; /* Return code */
- static unsigned secend; /* Last sector on disk */
- static unsigned track; /* Current track */
- static unsigned side; /* Current side */
- static unsigned sectrk; /* Sectors per track */
- static unsigned trklen; /* Bytes per track (512*sectrk) */
- static struct diskfree_t dfree; /* Used for _dos_getdiskfree */
- static unsigned totclust; /* Total clusters on source disk */
-
- strcpy(tfname,"C:\\DCOPY.$$$");
-
- /* Initial message */
- fprintf(stdout,"\nDCOPY V1.2 - Fast floppy disk copier\n");
-
- /* Check parameters and open instruction file */
- if (argc != 2) {
- fprintf(stdout,"Error - Correct usage is: DCOPY drive:\n");
- exit(1);
- }
- strcpy(drives,argv[1]);
- if (strlen(drives) != 2 || drives[1] != ':' || !isalpha(drives[0])) {
- fprintf(stdout,"Error - Drive specification invalid: %s\n",drives);
- exit(1);
- }
- drives[0]=toupper(drives[0]);
- drive=drives[0]-65; /* Convert A to 0, B to 1 etc. */
-
- /* Loop until no more disks to copy */
- while (1) {
-
- /* Ask for source disk */
- fprintf(stdout,"\nInsert SOURCE disk in drive %c:\n\n",drives[0]);
- fprintf(stdout,"Press Enter when ready . . .\n");
- fgets(s,2,stdin);
-
- /* Use _dos_getdiskfree to determine disk type */
- if ((ret=_dos_getdiskfree(drive+1,&dfree)) != 0) {
- fprintf(stdout,"Error - Invalid drive specified\n");
- goto emore;
- }
- totclust=dfree.total_clusters;
- switch (totclust) {
- case 354:
- secend=719;
- sectrk=9;
- fprintf(stdout,"Copying 360KB disk\n");
- break;
- case 2371:
- secend=2399;
- sectrk=15;
- fprintf(stdout,"Copying 1.2MB disk\n");
- break;
- case 713:
- secend=1439;
- sectrk=9;
- fprintf(stdout,"Copying 720KB disk\n");
- break;
- case 1415:
- secend=1439;
- sectrk=9;
- fprintf(stdout,"Copying 720KB disk (old format)\n");
- break;
- case 2847:
- secend=2879;
- sectrk=18;
- fprintf(stdout,"Copying 1.44MB disk\n");
- break;
- default:
- fprintf(stdout,"Error - Invalid disk type - code = %u\n",
- dfree.total_clusters);
- goto emore;
- }
- trklen=512*sectrk;
-
- /* Open temporary file */
- remove(tfname);
- if ((fht=open(tfname,O_CREAT | O_WRONLY | O_BINARY,
- S_IREAD | S_IWRITE)) == -1) {
- fprintf(stdout,"\nError - Cannot create %s\n",tfname);
- exit(1);
- }
-
- /* Process each track (one side at a time) */
- track=0;
- side=0;
- for (sector=0;sector <= secend;sector=sector+sectrk) {
- fprintf(stdout,"\rTrack %u Side %u ",track,side);
- err=1;
- while (err != 0) {
- err=rsecm(rec,drive,sector,sectrk);
- if (err != 0) {
- fprintf(stdout,"\nError - Track %u Side %u "
- "unreadable - code = %04X\n",
- track,side,err);
- while (1) {
- fprintf(stdout,"Try again to read track (Y/N)? ");
- fgets(s,3,stdin);
- if ((s[0] == 'Y') || (s[0] == 'y')) break;
- if ((s[0] == 'N') || (s[0] == 'n')) goto eread;
- }
- }
- }
- if ((ret=write(fht,rec,trklen)) == -1) {
- fprintf(stdout,
- "\nError writing to %s - code %04X\n",tfname,ret);
- goto eread;
- }
- side=side+1;
- if (side>1) {
- track=track+1;
- side=0;
- }
- }
- if ((ret=close(fht)) != 0) {
- fprintf(stdout,"\nError - Cannot close %s\n",tfname);
- goto edel;
- }
- fprintf(stdout,"\n");
-
- /* Loop until no more disks to write */
- while (1) {
-
- /* Ask for target disk */
- fprintf(stdout,"\nInsert TARGET disk in drive %c:\n\n",drives[0]);
- fprintf(stdout,"Press Enter when ready . . .\n");
- fgets(s,2,stdin);
-
- /* Use _dos_getdiskfree to determine disk type */
- if ((ret=_dos_getdiskfree(drive+1,&dfree)) != 0) {
- fprintf(stdout,"Error - Invalid drive specified\n");
- goto emore;
- }
- if (dfree.total_clusters != totclust) {
- if ((totclust == 354) && ((dfree.total_clusters == 713) ||
- (dfree.total_clusters == 1415)))
- fprintf(stdout,"Writing 360KB image onto 720KB disk\n");
- else {
- if (((totclust == 713) && (dfree.total_clusters == 1415))
- || ((totclust == 1415) && (dfree.total_clusters == 713)))
- fprintf(stdout,"Changing format of 720KB disk\n");
- else {
- fprintf(stdout,"Error - Target disk type incompatible "
- "- code = %u\n",dfree.total_clusters);
- goto enext;
- }
- }
- }
-
- /* Open temporary file */
- if ((fht=open(tfname,O_RDONLY | O_BINARY)) == -1) {
- fprintf(stdout,"\nError - Cannot open %s\n",tfname);
- goto edel;
- }
-
- /* Process each track */
- track=0;
- side=0;
- for (sector=0;sector <= secend;sector=sector+sectrk) {
- if ((ret=read(fht,rec,trklen)) <= 0) {
- fprintf(stdout,"\nError - Cannot read %s - code %04X\n",
- tfname,ret);
- goto ewrite;
- }
- fprintf(stdout,"\rTrack %u Side %u ",track,side);
- err=1;
- while (err != 0) {
- err=wsecm(rec,drive,sector,sectrk); /* Write */
- if (err != 0) {
- fprintf(stdout,"\nError - Track %u Side %u "
- "unwritable - code = %04X\n",
- track,side,err);
- while (1) {
- fprintf(stdout,"\nTry again to write "
- "track (Y/N)? ");
- fgets(s,3,stdin);
- if ((s[0] == 'Y') || (s[0] == 'y')) break;
- if ((s[0] == 'N') || (s[0] == 'n')) goto ewrite;
- }
- }
- if (err != 0) continue;
- err=rsecm(recv,drive,sector,sectrk); /* Verify */
- if (err != 0) {
- fprintf(stdout,"\nError - Track %u Side %u "
- "unreadable - code = %04X\n",
- track,side,err);
- while (1) {
- fprintf(stdout,"\nTry again to write "
- "track (Y/N)? ");
- fgets(s,3,stdin);
- if ((s[0] == 'Y') || (s[0] == 'y')) break;
- if ((s[0] == 'N') || (s[0] == 'n')) goto ewrite;
- }
- }
- }
- side=side+1;
- if (side>1) {
- track=track+1;
- side=0;
- }
- }
-
- /* Close temporary file */
- ewrite: if ((ret=close(fht)) != 0) {
- fprintf(stdout,"\nError - Cannot close %s\n",tfname);
- goto edel;
- }
- fprintf(stdout,"\n");
-
- /* Ask if more disks to write */
- enext: while (1) {
- fprintf(stdout,"\nWrite another copy of the disk (Y/N)? ");
- fgets(s,3,stdin);
- if ((s[0] == 'Y') || (s[0] == 'y')) break;
- if ((s[0] == 'N') || (s[0] == 'n')) goto edel;
- }
- }
-
- /* Close file after an error */
- eread: if ((ret=close(fht)) != 0) {
- fprintf(stdout,"\nError - Cannot close %s\n",tfname);
- exit(1);
- }
-
- /* Delete temporary file */
- edel: if ((ret=remove(tfname)) != 0) {
- fprintf(stdout,"\nError - Cannot delete %s\n",tfname);
- exit(1);
- }
-
- /* Ask if more disks to copy */
- emore: while (1) {
- fprintf(stdout,"\nCopy another disk (Y/N)? ");
- fgets(s,3,stdin);
- if ((s[0] == 'Y') || (s[0] == 'y')) break;
- if ((s[0] == 'N') || (s[0] == 'n')) break;
- }
- if ((s[0] != 'Y') && (s[0] != 'y')) break;
- }
-
- /* Final messages */
- fprintf(stdout,"IMPORTANT - Remove disk from drive %c:\n",
- drives[0]);
- fprintf(stdout,"End of program DCOPY\n");
- exit(0);
- }
-