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