/************************************************************************/
/*									*/
/*        S l i c k o   F i l e   C o p y i n g   R o u t i n e		*/
/*									*/
/************************************************************************/

/* This program does a blind copy of one file into another, similar to the
   operation of the MS-DOS  COPY  command. It is different in several ways:
   
   1. This program will copy hidden and system files such as ibmbio.com
      and ibmdos.com whereas COPY will not.
   
   2. This program (at least the current version) will not permit you to
      verify the correctness of the copy, as the COPY command with the
      /V switch does.
   
   3. This program only allows one file to be copied into another file.
      Concatenation of files is not possible.
   
   As you can see, this is a Quick and Dirty program. I am only moderately
   sorry about that. It was never intended to be a thing of lovliness.
   
   Charlie Ros5e
   Box 1636
   Boulder, Colorado, 80306
   303-939-5448
*/
      
#include <stdio.h>
#include <stat.h>


main(nargs, arg)

char  *arg[];
int    nargs;
{
  static char *msg[] = { "Command line syntax error. Correct form is:\n",
                         "",
                         "         cpy   from_name   to_name\n",
                         "",
                         "                A B O R T !"
                       };
                       
  int    j;				/* Loop counter			*/
  
  long   size,				/* Size of file to copy		*/
         k,				/* Loop counter			*/
         filesize();
         
  FILE  *fp1, *fp2;			/* File pointers		*/


/* First, check for the correct number of arguments on the command line.
   If it's wrong, tell the user what it should be, and bail out.
*/

  if( nargs != 3 )
  {
    scr_clear();
    
    for( k = 0; k < sizeof(msg)/sizeof(char *); k++ )
      puts( msg[k] );
   }

/* Now check to see if the source file exists. If it doesn't, bitch.	*/

  else if( (size = filesize(arg[1])) == -1 )
    printf("File %s Doesn't Exist.\n\n", arg[1]);

/* Everything is dunky-horey. Go ahead and copy the file.		*/

  else
  {
    printf("Copying %s into %s:  %ld bytes.\n", arg[1], arg[2], size);
  
    fp1 = fopen(arg[1], "r");
    fp2 = fopen(arg[2], "w");
    
    for(k = 0L; k < size; k++)
      putc( getc(fp1), fp2 );
    
    fclose(fp1);
    fclose(fp2);
    
    printf("%s Has Been Created.\n\n", arg[2]);
  }
    
  exit(0);
}
/************************************************************************/
/*									*/
/*                         F i l e s i z e				*/
/*									*/
/************************************************************************/

/* Purpose:          To determine the size, in bytes, of a specified file.

   Calling Sequence: char *filename;
   		     long  size, filesize();
   		     
   		     size = filesize(filename);
*/   		     

long filesize( filename )

char *filename;
{
  long          size;
  struct stat   buf;
  
  if(access(filename, 0))
    size = -1L;
  
  else
  {
    stat(filename, &buf);
    size = buf.st_size;
  }
  
  return(size);
}
