您的位置:首页 > 产品设计 > UI/UE

APUE习题3.2----自己实现的dup2( )函数的源码

2017-05-18 21:56 344 查看
APUE习题3.2----自己实现的dup2( )函数的源码

转载的,不清楚出处在哪里,对于我这个菜鸟来说,写的很好。

原 dup2()函数:

#include <unistd.h>

int dup2( int fd, int fd2 );

对于 dup2,可以用 fd2 参数指定新描述符的值。如果 fd2 已经打开,则先将其关闭。如若 fd 等于 fd2,则 dup2 返回 fd2,而不关闭它。否则,fd2 的 FD_CLOEXEC 文件描述符标志就被清除,这样 fd2 在进程调用 exec 时是打开状态。该函数返回的新文件描述符与参数 fd 共享同一个文件表项。

下面是自己实现的 dup2函数:

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

//检查文件描述符是否有效
int isFileDescriptor( int fd )
{
struct stat st;
if( (-1 == fstat(fd,&st)) && (EBADF == errno) )
return -1;
return 0;
}

int my_dup2( int oldfd, int newfd )
{
int tempfd;
int fd_count;
int fdarray[newfd];
int res;

if( -1 == isFileDescriptor( oldfd ) ) {
printf("the file descriptor is invalid.\n");
return -1;
}

//如果newfd等于oldfd,则直接返回newfd,而不关闭它
if( oldfd == newfd )
return newfd;

//否则,关闭newfd
if( 0 == isFileDescriptor( newfd ) ) {
res = close( newfd );
if( -1 == res ) {
perror("close file descriptor failed");
return -1;
}
}

//复制文件描述符
for( fd_count=0; fd_count<newfd; fd_count++ )
fdarray[fd_count] = 0;

for( fd_count=0; fd_count<newfd; fd_count++ ) {
tempfd = dup( oldfd );
if( -1 == tempfd )
return -1;
if( tempfd == newfd )
break;
else
fdarray[fd_count] = 1;

}

//关闭之前寻找指定描述符时打开的描述符
for( fd_count=0; fd_count<newfd; fd_count++ ) {
if( 1 == fdarray[fd_count] ) {
res = close( fd_count );
if( -1 ==res ) {
perror("close file descriptor failed");
return -1;
}
}
}

return tempfd;
}

//测试代码
int main()
{
int fd;
int testfd;
int res;
char *buffer = (char *)malloc(sizeof(char)*32);
fd = open("/tmp/dup2test1.txt", O_RDWR | O_CREAT, 0666);
if( -1 == fd ) {
perror("file open failed");
exit( EXIT_SUCCESS );
}

testfd = my_dup2( fd, 5 );

res = write( testfd, "Hey man!", strlen("Hey man!") ); //通过复制得到的文件描述符 testfd 写数据
if( -1 == res ) {
perror("write to testfd failed");
exit( EXIT_FAILURE );
}

printf("write to testfd %d successfully\n", testfd);

memset( buffer, '\0', 32 );
lseek( testfd, 0, SEEK_SET );
res = read( fd, buffer, 30 );  //通过初始的文件描述符 fd 读取数据
if( -1 == res ) {
perror("read from testfd failed");
exit( EXIT_FAILURE );
}
printf("read from initial fd %d is: %s\n", fd, buffer );
exit( EXIT_SUCCESS );
}


程序运行结果:

[zhang@localhost APUE]$ ./my_dup2

write to testfd 5 successfully

read from initial fd 3 is: Hey man!

测试通过。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: