/* 

This file is part of RCS.

RCS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RCS; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

Report problems and direct all questions to:

    rcs-bugs@cs.purdue.edu

vms questions go to:
 
Richard T. Gregory
1615 Hardwood Avenue
Charlottesville VA 22901
(804) 977-0000 - work

*/

#ifdef VMS

#include <stdio.h>
#include "rcsbase.h"

#define BUFFER_COPY_FOR_VMS
#define FILE_BUFFER_SIZE	(int) 40960

void fastcopy( FILE *inf, FILE *outf)

/* Function: copies the remainder of file inf to outf. */
{


char buf[ FILE_BUFFER_SIZE ];

#ifdef BUFFER_COPY_FOR_VMS
	register int in, out;
	int i;

	in = fileno( inf);
	out = fileno( outf);
	i = read( in, buf, FILE_BUFFER_SIZE); 
	while( i > 0)
	{
	  write( out, buf, i);
	  i = read( in, buf, FILE_BUFFER_SIZE); 
	}

#else  /* not  BUFFER_COPY_FOR_VMS */

	fgets( buf, BUFSIZ-1, inf);
	fprintf( outf, "%s", buf);
	while( feof( inf) == 0)
	{
	  fgets( buf, BUFSIZ-1, inf);
	  fprintf( outf, "%s", buf);
	}

#endif /*  BUFFER_COPY_FOR_VMS */

} /* fastcopy */


char command_string[ 120];
/* determine whether the two file names from and to are on the same
** physical disk drive.  If they are, the calling program can do a rename,
** else a copy is required.....
*/
int same_device( char *from, char *to)
{

char dev1[ 100 ], dev2[ 100 ];
FILE *tmp;
int rc = false;

	tmp = fopen( from, "r");
	strcpy( command_string, fgetname( tmp, command_string));
	strtok( command_string, ":");
	strcpy( dev1, command_string);
	fclose( tmp);

	/* new file - it may not exist, so open a new version */
	tmp = fopen( to, "w" VMS_TEMPORARY_FILE);  
			    /* ^^^ delete the file on close - vms specific */
	if( tmp != NULL)
	{
	  strcpy( command_string, fgetname( tmp, command_string));
	  strtok( command_string, ":");
	  strcpy( dev2, command_string);
	}
	else
	{
	  strcpy( dev2, " ");
	}
	fclose( tmp);

	if( 0 == strcmp( dev1,dev2))
	{
	  rc = true;
	}
	return( rc);
} /* same device */
 

#ifdef SLOW_VMS


/* for the VAX, a vms rename can only be done for two files on the same
** device.  */	
int rcs_rename( char *from, char *to)
{

	if( same_device( from, to))
	{
	  sprintf( command_string, "%s %s %s", SYSTEM_RENAME_PROGRAM,
		from, to);
	  system( command_string);
	}
	else
	{
	  FILE *fin, *fout;
	  fin = fopen( from, "r");
	  fout = fopen( to, "w");
	  fastcopy( fin, fout);
	  fclose( fin);
	  fclose( fout);
	}

} /* rename */

#endif /* slow_vms */

#define FAST_WITH_NO_SPAWNED_PROCESSES 1

#ifdef FAST_WITH_NO_SPAWNED_PROCESSES

/* for the VAX, a vms rename can only be done for two files on the same
** device.  */	
int rcs_rename( char *from, char *to)
{

	if( same_device( from, to))
	{
	  rename( from, to);
	}
	else
	{
	  FILE *fin, *fout;
	  fin = fopen( from, "r");
	  fout = fopen( to, "w");
	  fastcopy( fin, fout);
	  fclose( fin);
	  fclose( fout);
	}

} /* rename */

#endif /*  FAST_WITH_NO_SPAWNED_PROCESSES */

#ifdef TESTING

main( argc, argv)
  int argc;
  char *argv[];
{
FILE *in, *out;
char str[200];

	in = fopen( argv[1], "r");
	out = fopen( argv[2], "w");

	fastcopy( in, out);
	fclose( in);
	fclose( out);
	rcs_rename( argv[2], argv[1] );
}

#endif /* testing */

#endif /* vms */
