写一个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 ...
随机推荐
- Android新特性Instant Run详解
关于 Instant Run Android Studio 2.0 中引入的 Instant Run 是 Run 和 Debug 命令的行为,可以大幅缩短应用更新的时间.尽管首次构建可能需要花费较长的 ...
- Android Multimedia框架总结(八)Stagefright框架之AwesomePlayer及数据解析器
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼:http://blog.csdn.net/hejjunlin/article/details/52503057 前言:前面一篇分析了medi ...
- 19 主线程向子线程发送信息(handler)
package com.fmy.handler; import android.app.Activity; import android.os.Bundle; import android.os.Ha ...
- EBS应付(AP)模块常用表
select * from ap_invoices_all INVOICE头 select * from ap_invoice_distributions_all INVOICE行 select ...
- EBS库存(INV)模块常用表
select * from org_organization_definitions库存组织 select * from mtl_parameters组织参数 select * from mtl ...
- EBS开发常用编译命令
一.编译FORM 1.将脚本写成shell脚本 cd $AU_TOP/forms/ZHS export FORMS_PATH=.:$FORMS_PATH:$AU_TOP/forms/ZHS frmcm ...
- 使用java命令重启tomcat
public static void restartMyTomcat() { String location=ConfigUtil.getRestartLocation(); createCmdFil ...
- Spark集群模式&Spark程序提交
Spark集群模式&Spark程序提交 1. 集群管理器 Spark当前支持三种集群管理方式 Standalone-Spark自带的一种集群管理方式,易于构建集群. Apache Mesos- ...
- Oracle使用游标为所有用户表添加主键语句
应用场合:数据表新增自增一主键能加快数据表的访问速度,而且是整形的索引速度最快.本程序适合在导入Oracle数据库时删除不存在主键的情况下运行. 代码说明:所有的表主键字段名都设置为ID,如果已存在I ...
- J2EE进阶(十)SSH框架整合常见问题汇总(一)
SSH框架整合常见问题汇总(一) 前言 以下所列问题具有针对性,但是遇到同类型问题时均可按照此思路进行解决. HTTP Status 404 - No result defined for actio ...