|
发表于 2010-1-30 08:24:50
|
显示全部楼层
/dev/fd/n有些系统只有0,1,2,3,如果fd超过3就没有办法用这个方法了。dup递归实现
#include <unistd.h>
#include <errno.h>
int mydup2(int oldfd, int newfd)
{
int fd;
if (oldfd < 0 || newfd < 0
#ifdef OPEN_MAX
|| newfd >= OPEN_MAX
#endif
)
{
errno = EBADF;
return -1;
}
if (oldfd == newfd)
return newfd;
if ((fd = dup(oldfd)) == -1)
{
errno = EBADF;
return -1;
}
if (fd == newfd)
return 0;
if (fd > newfd)
{
close(fd);
close(newfd);
fd = dup(oldfd);
return fd;
}
newfd = mydup2(oldfd, newfd);
close(fd);
return newfd;
} |
|