MODULE FileCopy;   (* File copy *)

  (* From the book "Modula-2  A Seafarer's Manual and Shipyard Guide" *)
  (* Page 131   adapted "Amiga M2Modula-2"   09 Mar 1988 *)

FROM InOut IMPORT eol,		(* end of line *)
                  done,		(* status of operation
                                   TRUE => success
                                   FALSE => failure *)
                  OpenInput,	(* accept file name from terminal
                                   & open it for input *)
                  OpenOutput,	(* accept file name from terminal
                                   & open it for output *)
                  CloseInput,	(* close input file *)
                  CloseOutput,	(* close output file *)
                  Read,		(* read character *)
                  WriteLn,	(* Line feed *)
                  Write,	(* write character *)
                  WriteCard,	(* write cardinal value *)
                  WriteString;	(* write string *)
FROM ASCII IMPORT eof;		(* end of file *)                  

VAR
  LinesCopied : CARDINAL; 	(* counts lines copied *)
  ch : CHAR;			(* character read/written *)
  
BEGIN
  WriteString ("Enter input and output file names: ");
  
  REPEAT			(* get input file *)
    OpenInput ("IN");		(* supply default extension *)
  UNTIL done;
  
  REPEAT			(* get output file *)
    OpenOutput ("OUT");		(* supply default extension *)
  UNTIL done;
  
  LinesCopied := 0;		(* initialize lines copied counter *)
  
  LOOP
    Read(ch);			(* read character from in file *)
    IF (ch = eof) THEN		(* read operation successful? *)
      EXIT;			(* no - must be end of file. quit *)
    END;   (* IF *)
    Write (ch);			(* yes - write char to out file *)
    IF (ch = eol) THEN		(* character = end of line? *)
      INC (LinesCopied);	(* yes - increment lines copied *)
    END;   (* IF *)
  END;   (* LOOP *)		(* perform next read *)
  
  CloseOutput;
  CloseInput;
  WriteString ("File copy complete ");
  WriteCard (LinesCopied,5);
  WriteString (" lines copied");
  WriteLn; WriteLn;
  
END FileCopy.
