实现的时候用到系统原来的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 函数,并且要有出错处理的更多相关文章

  1. 写一个dup2功能同样的函数,不能调用 fcntl 函数,而且要有出错处理

    实现的时候用到系统原来的dup函数 // mydup2.c // 2015/08/17 Lucifer Zhang version1.0 // write my own dup2 function / ...

  2. Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数

    Python第七天   函数  函数参数   函数里的变量   函数返回值  多类型传值     函数递归调用   匿名函数   内置函数 目录 Pycharm使用技巧(转载) Python第一天   ...

  3. Python基础(函数,函数的定义,函数的调用,函数的参数,递归函数)

    1.函数 我们知道圆的面积计算公式为: S = πr2 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = ...

  4. python27期day09:函数的初始、函数的定义、函数的调用、函数的返回值、函数的参数、作业题。

    1.函数的作用:封装代码.大量的减少了重复的代码. 2.全局空间:顶行写的就是全局空间. 图解 : 3.函数的定义: def 是一个关键字.申明要定义一个函数 my_len 函数的名字.遵循变量命名的 ...

  5. LR常用函数以及调用自定义函数

    2.LR常用函数以及调用自定义函数 2.1.LR常用函数以及对信息的判断 2.1.1. LR内部自定义函数 在LR脚本中定义变量和编写自定义函数,需将变量的声明放在脚本其他内容的上方,否则会提示[il ...

  6. c++与js脚本交互,C++调用JS函数/JS调用C++函数

    <!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h1> <p> Java ...

  7. js 匿名函数-立即调用的函数表达式

    先提个问题, 单独写匿名函数为什么报错?return 匿名函数 为什么不报错? 如图: 第二种情况在 f 还没有执行的时候,就报错了,,,当然这得归因于函数声明语句声明提前(发生在代码执行之前)的原因 ...

  8. Python---7函数(调用&定义函数)

    函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs(),只有一个参数.可以直接从Python的官方网站查看文档: http: ...

  9. 在成员函数中调用虚函数(关于多态的注意事项)------新标准c++程序设计

    类的成员函数之间可以互相调用.在成员函数(静态成员函数.构造函数和析构函数除外)中调用其他虚成员函数的语句是多态的.例如: #include<iostream> using namespa ...

随机推荐

  1. Selenium Webdriver元素定位的八种常用方法

    如果你只是想快速实现控件抓取,而不急于了解其原理,可直接看: http://blog.csdn.net/kaka1121/article/details/51878346 如果你想学习web端自动化, ...

  2. openresty 备忘

    The problem with: apt-get --yes install $something is that it will ask for a manual confirmation if ...

  3. C/C++与Matlab混合编程初探

    ================================================================== % 欢迎转载,尊重原创,所以转载请注明出处. % http://b ...

  4. collection 中对类排序

    首先 写出 一个person类 让他继承Comparable 构造函数和get/set不用说 我们要覆盖父类中的comparto方法 代码如下 省略get/set package a; public ...

  5. 4-sum问题

    给定一个整数数组,判断能否从中找出4个数a.b.c.d,使得他们的和为0,如果能,请找出所有满足和为0个4个数对. #define SIZE 10 void judgeAndPut(int* arr, ...

  6. [ExtJS5学习笔记]第二十七节 CMD打包错误 Error C2009: YUI Parse Error (identifier is a reserved word => debugger;)

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/41242993 本文作者:sushengmiyan ------------------ ...

  7. 验证码程序Demo

    小伙伴都有这样的经历,册各种网站,总是输不对验证码,双十一那天狂买的小伙伴是不是对输入验证码有着不一样的感触呢,以前觉得验证码真是个麻烦鬼,一个不小心,一个眼拙,哎呦,没有输入正确,又是一阵子大眼瞪小 ...

  8. 关于LT分发系统的设计构想

    git地址 https://github.com/cxyxd/LtDistribution 背景 对tomcat做集群,在多机多tomcat的情况下,如果要更新代码,只能手动的将代码复制,粘贴,然后下 ...

  9. Sharing The Application Tier File System in Oracle E-Business Suite Release 12.2

    The most current version of this document can be obtained in My Oracle Support Knowledge Document 13 ...

  10. Linux系统编程---守护进程

    守护进程是什么?就是在后台运行的进程. 那么如何创建守护进程呢? 1. 创建孤儿进程 2. setsid() 创建进程会话 3. 重定向标准输入, 标准输出 4. chdir, 改当当前进程的工作目录 ...