写一个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 ... 
随机推荐
- Scala actor的使用
			Actor 为什么需要Actor? Actor的本质即万物皆Actor, Actor之间只有发送消息这一种通信方式.例如,无论是管理员让工作者干活,还是工作者把成果交还给管理员,它们之间也要通过发送消 ... 
- VIM编辑器操作命令积累
			开始学习VIM编辑器了,计划着每周学习几个新命令,记录在本篇博客中. 1.第一次接触 vi demo.txt 进入Normal模式查看文本 i 进入Insert模式插入内容,编辑文本 nG n代表行号 ... 
- 【Android应用开发】Android 蓝牙低功耗 (BLE)   ( 第一篇 . 概述 . 蓝牙低功耗文档 翻译)
			转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50515359 参考 : -- 官方文档 : https://develope ... 
- JDBC-数据库的编程(一)
			因为我使用的mysql数据库客户端程序是workBench,所以会用workBench来进行讲解. create table tbl_user( id int(11) unsigned not nul ... 
- 查看apk签名信息
			经常在注册开发者的时候会遇到要求填写申请应用的应用签名: 有两种很方便的方法: 1.如果没有源码或者没有打开eclipse,直接下载这个应用应用下载链接 使用截图,只要把包名输入,自动会出现签名信息. ... 
- Android View的绘制过程
			首先是view的绘制过程~最主要的分三部分 measure layout draw 看字面意思,计算,布局,画~ android中控件相当于是画在一个无限大的画布上的,那就产生了几个问题 画布无限大, ... 
- jar包执行报ClassNotFoundException
			使用Eclipse打包jar包,指定了main class. java -jar mongoCluster.jar 但是运行的时候报ClassNotFoundException NoClassDefF ... 
- Android项目开发填坑记-Fragment的onAttach
			背景 现在Android开发多使用一个Activity管理多个Fragment进行开发,不免需要两者相互传递数据,一般是给Fragment添加回调接口,让Activity继承并实现. 回调接口一般都写 ... 
- Android开发学习之路--网络编程之xml、json
			一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ... 
- Android初级教程实现电话录音
			需求:设置来电后自动录音. 首先设置一个按钮,代码很简单这里就不再给出. 建一个类,RecorderServicer extends Service package com.ydl.recorder; ... 
