#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int mydup(int i_OldFd, int i_NewFd);

int main(void)
{
int fd = -1;
fd = open("tests.c", O_RDONLY);

if(fd < 0)
{
printf("error.\n");
return -1;
}

printf("the old_fd:%d.\n", fd);

int tmp = mydup(fd, 100);

printf("the new_fd:%d.\n",tmp);
return 1;
}

int mydup(int i_OldFd, int i_NewFd)
{
if(i_OldFd < 0 || i_OldFd >256)
{
printf("Oldfd is wrong.\n");
return -1;
}

if(i_NewFd < 0 || i_NewFd > 256)
{
printf("Newfd is wrong.\n");
return -1;
}

if( i_NewFd == i_OldFd)
return i_OldFd;

int i_Arrayfd[256];
int count = 0;
close(i_NewFd);

do
{
i_Arrayfd[count] = dup(i_OldFd);
if(i_Arrayfd[count] == -1)
{
printf("dup error.\n");
break;
}
count++;

}while(i_Arrayfd[count-1] != i_NewFd);

for(int i = 0; i < count-1 ; i++)
{
close(i_Arrayfd[i]);
}

return i_Arrayfd[count-1];
}

最新文章

  1. Android之解析XML
  2. Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架
  3. ACM 另一种阶乘问题
  4. 负margin一些奇葩的布局技巧
  5. Google Guava官方教程(中文版)地址
  6. firefox关于about:config的常用配置
  7. Docker 架构详解 - 每天5分钟玩转容器技术(7)
  8. 使用Docker搭建简易的 Java Web 环境
  9. Python 接口测试(二)
  10. Go基础篇【第5篇】: 内置库模块 exec
  11. 查找第k小的元素(O(n)递归解法)
  12. Maven集成SSM
  13. canvas实例_在线画图工具
  14. golang基础--类型与变量
  15. Nginx 防盗链
  16. SVN: is scheduled for addition, but is missing
  17. 第一个struct2程序(2)
  18. bzoj P4825 [Hnoi2017]单旋——solution
  19. SHELL 中的变量
  20. 使用NuGet发布自己的.NET NuGet 包( .NET Standard &amp; Windows)

热门文章

  1. HTML学习笔记之二(回到顶部 与 回究竟部)
  2. Java内部类实现伪方法级多线程
  3. tomcat7源代码Bootstrap
  4. LA 6448 Credit Card Payment
  5. Android布局文件-错误
  6. GUID的广泛使用
  7. UGUI之UI的深度问题
  8. maven发布时在不同的环境使用不同的配置文件
  9. Swift中可选型的Optional Chaining 和 Nil-Coalesce(Swift2.1)
  10. hdu 2480 贪心+简单并查集