FpDup

Duplicate a file handle

Declaration

Source position: bunxh.inc line 38

  function FpDup(fildes: cint) : cint;
  function FpDup(var oldfile: text; var newfile: text) : cint;
  function FpDup(var oldfile: File; var newfile: File) : cint;

Description

FpDup returns a file descriptor that is a duplicate of the file descriptor fildes.

The second and third forms make NewFile an exact copy of OldFile, after having flushed the buffer of OldFile in case it is a Text file or untyped file. Due to the buffering mechanism of Pascal, these calls do not have the same functionality as the dup call in C. The internal Pascal buffers are not the same after this call, but when the buffers are flushed (e.g. after output), the output is sent to the same file. Doing an lseek will, however, work as in C, i.e. doing a lseek will change the file position in both files.

The function returns a negative value in case of an error, a positive value is a file handle, and indicates success.

Errors

A negative value can be one of the following error codes:

sys_ebadf
OldFile hasn't been assigned.
sys_emfile
Maximum number of open files for the process is reached.

See also

Name Description
fpDup2 Duplicate one file handle to another

Example

program Example31;
{ Program to demonstrate the Dup function. }
uses baseunix;
var f : text;
begin
  if  fpdup (output,f)=-1 then
    Writeln ('Dup Failed !');
  writeln ('This is written to stdout.');
  writeln (f,'This is written to the dup file, and flushed');flush(f);
  writeln
end.