Program Copy;

{$I "Include/DOS.i"}

var
    InputFileName  : String;
    OutputFileName : String;
    Infile         : FileHandle;
    Outfile        : FileHandle;
    Position       : Integer;
    Buffer	   : ^array [1..1000] of char;

Function GetFileName(var index : Integer): String;

{
	This function requires that each file name in the command
	line have at least one character of disposable delimeter
	after it.  It starts looking from index, and returns a
	pointer into the command line.  Index ends up as the next
	unused character.
}

var
   name : String;
begin
    while ((CommandLine[index] = ' ') or (CommandLine[index] = chr(9))) and
	(index < 128) do
	index := index + 1;
    if index >= 128 then begin
	writeln('Bad file name.');
	exit(20);
    end;
    name := String(adr(CommandLine[index]));
    while (ord(CommandLine[index]) > ord(' ')) and
	  (ord(CommandLine[index]) < 128) and
	(index < 128) do
	index := index + 1;
    if index >= 128 then begin
	writeln('Bad file name.');
	exit(20);
    end;
    CommandLine[index] := chr(0);
    index := index + 1;
    GetFileName := name;
end;

begin
    Position := 1;
    InputFileName := GetFileName(Position);
    OutputFileName := GetFileName(Position);

    Infile := DOSOpen(InputFileName, ModeOldFile);
    if Infile <> nil then begin
	Outfile := DOSOpen(OutputFileName, ModeNewFile);
	if Outfile <> nil then begin
	    New(Buffer);
	    repeat
		Position := DOSRead(Infile, Buffer, 1000);
		if Position > 0 then begin
		    Position := DOSWrite(Outfile, Buffer, Position);
		    if Position = 0 then begin
			writeln('Write error');
			exit(20);
		    end;
		end;
	    until Position = 0;
	    Dispose(Buffer);
	    DOSClose(Outfile);
	end else
	    writeln('Could not open output file.');
	DOSClose(Infile);
    end else
	writeln('Could not open input file.');
end.
