explain_dup2_or_die(3) Library Functions Manual explain_dup2_or_die(3)NAME
explain_dup2_or_die - duplicate a file descriptor and report errors
SYNOPSIS
#include <libexplain/dup2.h>
void explain_dup2_or_die(int oldfd, int newfd);
DESCRIPTION
The explain_dup2_or_die function is used to call the dup2(2) system call. On failure an explanation will be printed to stderr, obtained
from explain_dup2(3), and then the process terminates by calling exit(EXIT_FAILURE).
This function is intended to be used in a fashion similar to the following example:
explain_dup2_or_die(oldfd, newfd);
oldfd The oldfd, exactly as to be passed to the dup2(2) system call.
newfd The newfd, exactly as to be passed to the dup2(2) system call.
Returns:
This function only returns on success. On failure, prints an explanation and exits.
SEE ALSO dup2(2) duplicate a file descriptor
explain_dup2(3)
explain dup2(2) errors
exit(2) terminate the calling process
COPYRIGHT
libexplain version 0.52
Copyright (C) 2008 Peter Miller
explain_dup2_or_die(3)
Check Out this Related Man Page
DUP(2) Linux Programmer's Manual DUP(2)NAME
dup, dup2 - duplicate a file descriptor
SYNOPSIS
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
DESCRIPTION
dup and dup2 create a copy of the file descriptor oldfd.
After successful return of dup or dup2, the old and new descriptors may be used interchangeably. They share locks, file position pointers
and flags; for example, if the file position is modified by using lseek on one of the descriptors, the position is also changed for the
other.
The two descriptors do not share the close-on-exec flag, however.
dup uses the lowest-numbered unused descriptor for the new descriptor.
dup2 makes newfd be the copy of oldfd, closing newfd first if necessary.
RETURN VALUE
dup and dup2 return the new descriptor, or -1 if an error occurred (in which case, errno is set appropriately).
ERRORS
EBADF oldfd isn't an open file descriptor, or newfd is out of the allowed range for file descriptors.
EMFILE The process already has the maximum number of file descriptors open and tried to open a new one.
WARNING
The error returned by dup2 is different to that returned by fcntl(..., F_DUPFD, ...) when newfd is out of range. On some systems dup2 also
sometimes returns EINVAL like F_DUPFD.
CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, BSD 4.3. SVr4 documents additional EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
SEE ALSO fcntl(2), open(2), close(2)Linux 1.1.46 1994-08-21 DUP(2)
Hey,
Im a complete noob in UNIX and this problem is killing me.
Im trying to write the stdin (which i receive from a pipe) to a file, but as always C crashes without no explanation. Here is what i have so far:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>... (3 Replies)
int redirect()
{
int fd,rc;
fd = open("sample.DAT",O_CREAT | O_RDWR , 00777 );
rc = dup2(fd , 1 ) ;
close (fd );
return 0;
}
I used the above to redirect all the cout statements to sample.DAT. process is redirecting the output and I had two questions
1. All stdout/cout statements... (2 Replies)
Hi all,I'm reading <Advanced programming in the UNIX environment>,that book asked the reader to implement a function which has same functions with dup2 without calling fcntl.Could anyone give me a tip?Any help will be appreciated.:) (8 Replies)
Hi i have a file with one column and want to report the value that has duplicate like
rag
rag
hi
hi
hi
123
456
output should be
rag
hi
thank you (6 Replies)