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

# Dup2

Duplicate one filehandle to another

## Declaration

Source position: line 0

`function Dup2(`
`oldfile: LongInt;`
`newfile: LongInt`
`):Boolean; function Dup2(`
`var oldfile: text;`
`var newfile: text`
`):Boolean; function Dup2(`
`var oldfile: file;`
`var newfile: file`
`):Boolean;`

## Description

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

NewFile can be an assigned file. If newfile 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 fileposition in both files.

The function returns True if succesful, false otherwise.

## Errors

In case of error, Linuxerror is used to report errors.

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

## See also

Dup |  | Duplicate a file handle

## Example

    program Example31;

    { Program to demonstrate the Dup function. }

    uses oldlinux;

    var f : text;
        i : longint;

    begin
      Assign (f,'text.txt');
      Rewrite (F);
      For i:=1 to 10 do writeln (F,'Line : ',i);
      if not dup2 (output,f) 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.}
      Unlink ('text.txt');
    end.

Documentation generated on: Feb 26 2014