写一个dup2功能同样的函数,不能调用 fcntl 函数,而且要有出错处理
实现的时候用到系统原来的dup函数
// mydup2.c
// 2015/08/17 Lucifer Zhang version1.0
// write my own dup2 function
// use dup() function when inplementation #include <unistd.h> // include dup()
#include <stdio.h>
#include <stdlib.h> #define OPEN_MAX 256
/*
* when the descriptor is negative or greater than OPEN_MAX, will make a errro
* */ int my_dup2(int fd, int newfd); int main(int argc, char *argv[])
{
int newfd, return_fd; if (argc != 2) {
printf("usage: a.out test.txt\n");
exit(0);
}
printf("Please input the descriptor than you want to set: ");
scanf("%d", &newfd); // open a file
int fd = open(argv[1], 0);
if (fd == -1) {
perror(argv[1]); // print error msg
exit(0);
} printf("old descriptor is: %d\n", fd);
return_fd = my_dup2(fd, newfd);
printf("new descriptor is: %d\n");
close(fd);
close(return_fd); exit(0);
} int my_dup2(int fd, int newfd)
{
int count = 0;
int fdarry[newfd]; // record opened descriptor if (newfd < 0 || newfd > OPEN_MAX) {
printf("the new descriptor error!\n");
exit(0);
} // dup() return the lowest-numbered available file descriptor
if ((fdarry[count] = dup(fd)) == -1) {
printf("dup() function error!\n");
exit(0);
} else { // test old file descriptor if can be used
close(fdarry[count]);
} // if fd equals newfd, then dup2 returns newfd without closing it
if (fd == newfd) {
return fd;
} close(newfd); // close // the main implementation
for (count = 0; count <= newfd; ++count) {
if ((fdarry[count] = dup(fd)) == -1) {
printf("dup() funciont error!\n");
exit(0);
} else {
printf("the descriptor is: %d\n", fdarry[count]);
if (fdarry[count] == newfd) {
break;
}
}
} for (count = 0; count <= newfd; ++count) {
if (fdarry[count] == newfd) {
return fdarry[count];
} else {
close(fdarry[count]);
}
}
}
写一个dup2功能同样的函数,不能调用 fcntl 函数,而且要有出错处理的更多相关文章
- 写一个dup2功能相同的函数,不能调用 fcntl 函数,并且要有出错处理
实现的时候用到系统原来的dup函数 // mydup2.c // 2015/08/17 Lucifer Zhang version1.0 // write my own dup2 function / ...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- Python基础(函数,函数的定义,函数的调用,函数的参数,递归函数)
1.函数 我们知道圆的面积计算公式为: S = πr2 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = ...
- python27期day09:函数的初始、函数的定义、函数的调用、函数的返回值、函数的参数、作业题。
1.函数的作用:封装代码.大量的减少了重复的代码. 2.全局空间:顶行写的就是全局空间. 图解 : 3.函数的定义: def 是一个关键字.申明要定义一个函数 my_len 函数的名字.遵循变量命名的 ...
- LR常用函数以及调用自定义函数
2.LR常用函数以及调用自定义函数 2.1.LR常用函数以及对信息的判断 2.1.1. LR内部自定义函数 在LR脚本中定义变量和编写自定义函数,需将变量的声明放在脚本其他内容的上方,否则会提示[il ...
- c++与js脚本交互,C++调用JS函数/JS调用C++函数
<!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h1> <p> Java ...
- js 匿名函数-立即调用的函数表达式
先提个问题, 单独写匿名函数为什么报错?return 匿名函数 为什么不报错? 如图: 第二种情况在 f 还没有执行的时候,就报错了,,,当然这得归因于函数声明语句声明提前(发生在代码执行之前)的原因 ...
- Python---7函数(调用&定义函数)
函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs(),只有一个参数.可以直接从Python的官方网站查看文档: http: ...
- 在成员函数中调用虚函数(关于多态的注意事项)------新标准c++程序设计
类的成员函数之间可以互相调用.在成员函数(静态成员函数.构造函数和析构函数除外)中调用其他虚成员函数的语句是多态的.例如: #include<iostream> using namespa ...
随机推荐
- Markdown,后缀MD
Markdown 算是一门新兴语言,现在 7-8 岁了吧.它设计的初衷就是让写字的人专注于写字,用纯文本简单的符号标记格式,最后再通过工具转换成鬼畜的 HTML/XHTML.如果你玩过 wikiped ...
- 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)
这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...
- [windows篇][关掉某些服务]
- [git 学习篇] --创建git创库
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...
- PHP 格式化显示时间 date()函数
Y 4位数字年,y为2位数字,如99即1999年 m 数字月份,前面有前导0,如01.n 为无前导0数字月份 F 月份,完整的文本格式,例如 January 或者 March M 三个字母缩写表示的月 ...
- springMVC 引入静态资源Js的方式
前两天项目出现了Js无法引入的情况,本篇博客先总结分析+批判自己犯的低级错,再说说几种访问静态资源的方式! 首先,由于在web.xml里面的servlet拦截匹配为<url-pattern> ...
- 微软工具Sysinternals Suite
工具:Sysinternals Suite 一个可以看进程的工具.
- 【转】深入JVM系列(一)之内存模型与内存分配
http://lovnet.iteye.com/blog/1825324 一.JVM内存区域划分 大多数 JVM 将内存区域划分为 Method Area(Non-Heap),Heap,Progr ...
- 【bzoj3166】[Heoi2013]Alo 可持久化Trie树+STL-set
题目描述 Welcome to ALO ( Arithmetic and Logistic Online).这是一个VR MMORPG ,如名字所见,到处充满了数学的谜题.现在你拥有n颗宝石,每颗宝石 ...
- 【Luogu】P3979遥远的国度(树链剖分)
题目链接 不会换根从暑假开始就困扰我了……拖到现在…… 会了还是很激动的. 换根操作事实上不需要(也不能)改树剖本来的dfs序……只是在query上动动手脚…… 设全树的集合为G,以root为根,u在 ...