[Overview][Constants][Types][Procedures and functions][Index] Reference for unit 'BaseUnix' (#rtl)

FpDup2

Duplicate one file handle to another

Declaration

Source position: bunxh.inc line 39

function FpDup2(

  fildes: cint;

  fildes2: cint

):cint;

function FpDup2(

  var oldfile: text;

  var newfile: text

):cint;

function FpDup2(

  var oldfile: file;

  var newfile: file

):cint;

Description

Makes fildes2 or NewFile an exact copy of fildes or OldFile, after having flushed the buffer of OldFile in the case of text or untyped files.

After a call to fdup2, the 2 file descriptors point to the same physical device (a file, socket, or a terminal).

NewFile can be an assigned file. If newfile or fildes was open, it is closed first. Due to the buffering mechanism of Pascal, this has not the same functionality as the dup2 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 the new file descriptor number, on error -1 is returned, and the error can be retrieved with fpgeterrno

Errors

In case of error, the following error codes can be reported:

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

See also

fpDup

  

Duplicate a file handle

Example

program Example32;

{ Program to demonstrate the FpDup2 function. }

uses BaseUnix;

var f : text;
    i : longint;

begin
  Assign (f,'text.txt');
  Rewrite (F);
  For i:=1 to 10 do writeln (F,'Line : ',i);
  if fpdup2 (output,f)=-1 then
    Writeln ('Dup2 Failed !');
  writeln ('This is written to stdout.');
  writeln (f,'This is written to the dup file, and flushed');
  flush(f);
  writeln;
  { Remove file. Comment this if you want to check flushing.}
  fpUnlink ('text.txt');
end.

Documentation generated on: May 14 2021