/*	DESKTOP.PRG Program		   by W. Alan B. Evans,
					   [wabe@ukc.ac.uk]  March 1992.

   If put in AUTO-FOLDER of "booting" RDY-ramdisk will ensure that any
DESKTOP.INF file in the ramdisk's root directory will be copied to C:\
(if C:\ exists) where the newer TOS-Versions will look for it.

*/

#include <osbind.h>

extern	char *strcpy(),*gts();

main()

{	long st;	int drm;	char file[60];

/*   This is escape from "BOOT & WARMSTART" loop caused by faulty ACC's etc.
     since it enables one to invoke a simple non-GEM shell e.g ME.TOS,
     COMMAND.PRG, RDYSH.PRG or GULAM.PRG etc. to rename the offending ACC
     or boot program
*/
	if (Cconis()== -1 && (Crawcin() & 0x5f) == 'X') {
	Cconws("\r\n  FULL PATH\\NAME OF PROGRAM TO BE EXECUTED: ");
	Pexec(0,gts(file),"\0","\0");
	}

	if (drm & 04)
	if (Sversion() >= 0x1400)      /*  STE TOS Present!	*/
/*  Ensure the proper DESKTOP.INF file is looked at by new TOS  */
	st= fc("DESKTOP.INF\0","C:\\DESKTOP.INF\0");
/*  Execute the 40-Folder Patch Prog if found on C:\ with old TOS  */
	else  Pexec(0,"C:\\FOLDR???.PRG\0","\0","\0");

Pterm(st);
}


/*
*   A useful File_Copying subroutine - preserves date_stamp on old TOS  
*/

int fc(file1,file2)
char *file1,*file2;

{	register char *buf;
	int i,handle1,handle2,nsects,nsecmax,erc,no_tms,info[2];
	long lo,nrw,flngth;

	erc= 0;	buf= 0;
	if ((nsecmax = (int)(Malloc(-1L)/0x200L)-4) < 0)
	{ gemdos(9,"\r\n  INSUFFICIENT MEMORY!! ");  goto end;	}

	if ((handle1 = Fopen(file1,0)) < 0) return(0);
	Fdatime(info,handle1,0);
	flngth= Fseek(0L,handle1,2);
	nsects= 1+(int)(flngth/0x200L);
	if ((no_tms= nsects/nsecmax) == 0) nsecmax= nsects;

	if ((handle2 = Fopen(file2,0)) > 0)
	{ Fclose(handle2); Fdelete(file2);	}
	if ((handle2 = Fcreate(file2,0)) < 0 )
	{ gemdos(9,"\r\n Fcreate FAILURE - COULD NOT OPEN FILE: ");
	gemdos(9,file2);	--erc; goto end; }

	if ((buf= (char *)Malloc((long)nsecmax*0x200L+0x200L)) == 0)
	{ gemdos(9,"\r\n Malloc FAILURE!!! "); --erc; goto end; }
	Cconws("\r  Copying "); Cconws(file1);
	Cconws(" to "); Cconws(file2);  Cconws("\r\n ");
	i=-1; while (i++ < no_tms)
	{ if (i == no_tms) nrw= flngth- (long)nsecmax*0x200L*i;
	else  nrw= (long)nsecmax*0x200;

	{ if ((lo= Fseek((long)nsecmax*0x200L*i,handle1,0) < 0) 
	|| (lo=Fread(handle1,nrw,buf)) != nrw)
	{ gemdos(9,"\r\n Fseek or Fread FAILURE");  --erc; goto end;	}
	}

	{ if ((lo= Fseek((long)(nsecmax*0x200L*i),handle2,0) < 0) 
	|| (lo= Fwrite(handle2,nrw,buf)) != nrw)
	{ gemdos(9,"\r\n Fseek or Fwrite FAILURE!  DISK FULL? "); --erc; goto end; }
	}
	}    /*  end of i loop  */

end:	if (!Fclose(handle2))
	{ if (erc < 0) Fdelete(file2); else
	{ handle2=Fopen(file2,0); Fdatime(info,handle2,1); Fclose(handle2); }
	}
	if (buf != 0) Mfree(buf);
return(erc);
}

/*
		Cheap & minimal gets alternative
	(  An economical alternative to "gets" - does not, however
 	respond to CNTRL-C on the Older TOSes	)
*/

#include <osbind.h>

char *gts(data)
char *data;

{	register char *p = data;
	while ((*p=gemdos(1)) != '\r')
	if (*p == '\010') { --p; Cconws(" \010"); } else p++;
	*p= '\0';
	if (*data)	return(data);	else	return((char *)0);
}
