#include <string.h>

#include <exec/types.h>
#include <exec/memory.h>

#include <dos/dos.h>
#include <dos/dosextens.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>

#include <dos/stdio.h>

struct Library *DosBase = NULL;
BPTR stdout;

LONG SizeOfFile( char *Name )
{
   BPTR                    lock;
   struct FileInfoBlock   *InfoBlock;
   LONG                    Size = 0L;

   if( InfoBlock = (struct FileInfoBlock *)
                   AllocVec(sizeof(struct FileInfoBlock),MEMF_CLEAR) )
   {
      if( lock = Lock( (STRPTR)Name,ACCESS_READ ) )
      {
         if( Examine( lock,InfoBlock ) )
            Size = InfoBlock->fib_Size;

         UnLock( lock );
      }

      FreeVec( InfoBlock );
   }

   return( Size );
}

void
main(int argc,char *argv[])
{
   BPTR out,in;
   UBYTE *buffer;
   long size, written;

   if(DosBase = OpenLibrary((STRPTR)"dos.library",37))
   {
      stdout = Output();

      if(argc == 3)
      {
         if(in = Open((UBYTE *)argv[1], MODE_OLDFILE))
         {
            if(out = Open((UBYTE *)argv[2], MODE_NEWFILE))
            {
               size = SizeOfFile(argv[1]);

               buffer = AllocVec(size+1,MEMF_CLEAR|MEMF_ANY);
               if(buffer)
               {
                  if(Read(in,buffer,size) != (long)(-1))
                  {
                     if((written = Write(out,buffer,size)) == (long)(-1))
                        PutStr((STRPTR)"Error while writing input file!\n");

                     if(written != size)
                        PutStr((STRPTR)"File sizes differs!\n");
                  }
                  else
                     PutStr((STRPTR)"Error while reading input file!\n");

                  FreeVec(buffer);

                  Printf((STRPTR)"File %s copied...\n",(ULONG)argv[1]);
               }
               else
                  PutStr((STRPTR)"No memory available!\n");

               Close(out);
            }
            else
               PutStr((STRPTR)"Can't open output file!\n");

            Close(in);
         }
         else
            PutStr((STRPTR)"Can't open input file!\n");
      }
      else
         Printf((STRPTR)"Call: %s <FROM-File> <TO-File>\n",(ULONG)argv[0]);

      CloseLibrary(DosBase);
   }
}

