/**********************************************************************/
/* PROGRAM: COPYFT.COM.  Written in the small C programming language. */
/* by Keith M. Simons					   31.12.1989 */
/**********************************************************************/

/* Start by declaring some variables needed throughout the program */

#define ERROR	-1

char infcb[36];			/* File Control Blocks */
char outfcb[36];

char inbuff[128];		/* File Buffer for temp storage */


/* The main function... in fact, in this program the only function	*/

/* Uses the following library functions:-				*/
/* puts(string);		write a string on the screen		*/
/* strcpy(string1,string2);	copies string2 to string1		*/
/* open(fcb,filename);		open existing file using 36 byte fcb	*/
/* create(fcb,filename);	create a brand new file			*/
/* exit();			return to CP/M				*/
/* close(fcb);			close the file referred to by the fcb	*/
/* unlink(filename);		erase the file				*/
/* read(fcb,buffer,count);	read from the file referred to by the   */
/*				fcb, repeating 'count' times, 128 bytes */
/*				into the string called buffer		*/ 
/* write(fcb,buffer,count);	ditto - but write it!			*/


main(argc,argv)
int argc;
int argv[];
{
	char filename[132];	/* input file name */
	char filenama[132];	/* and... output file name!*/

	strcpy(filename,argv[1]);
	strcpy(filenama,argv[2]);
	puts("Program to copy file named ");
	puts(filename);puts(" to ");
	puts(filenama);puts(" by Keith M. Simons."); /*fame at last!*/
	puts("  31.12.1989\n");

	/* open input file if it exists */
	if (open(infcb,filename) == ERROR) {
		puts("Original filename is invalid or the file does not exist");
		puts(" - cannot copy file.");
		exit();
		}

	/* open output file (it shouldn't exist, so if we get an error we're */
	/* O.K., if we don't... help!).					     */
	if (open(outfcb,filenama)!=ERROR) {
		puts("New filename already exists - cannot copy file.");
		close(infcb);
		close(outfcb);
		exit();
		}

	/* thankfully, we got an error.  now create the brand new file */
	if (create(outfcb,filenama)==ERROR) {
		puts("Disk directory is full - cannot copy file.");
		/* I suppose I should have also put 'or new filename invalid' */
		exit();
		}

	/* the actual copying stage */
	while(read(infcb,inbuff,1)!= ERROR) {
		if (write(outfcb,inbuff,1) == ERROR) {
			/* something's gone wrong! */
			close(outfcb);
			unlink(filenama);
			puts("Disk is full - cannot copy file.");
			exit();}
		}

	/* finished copying */
	close(infcb);close(outfcb);
	puts("File appears to have successfully copied.");

	/* it's done! */
}


