#2
pangding2012-08-30 01:55
|
程序代码:
#include <fcntl.h>
int dup2(int fd1, int fd2) {
if( fd1 != fd2 ) {
/* check to make sure that fd1 is a valid open
* file descriptor
*/
if ( fcntl( fd1, F_GETFL) < 0 )
return -1;
/* check to see if fd2 is already open;
* if so, close it
*/
if( fcntl( fd2, F_GETFL) >= 0 )
close ( fd2 );
if(fcntl ( fd1, F_DUPFD, fd2 ) < 0)
return -1;
}
return fd2;
}
int dup2(int fd1, int fd2) {
if( fd1 != fd2 ) {
/* check to make sure that fd1 is a valid open
* file descriptor
*/
if ( fcntl( fd1, F_GETFL) < 0 )
return -1;
/* check to see if fd2 is already open;
* if so, close it
*/
if( fcntl( fd2, F_GETFL) >= 0 )
close ( fd2 );
if(fcntl ( fd1, F_DUPFD, fd2 ) < 0)
return -1;
}
return fd2;
}