实现的时候用到系统原来的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. Android通知Notification全面剖析

    通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通知栏查看通知的详细信息. 通知区域和抽屉式通知栏均是由系统控 ...

  2. Android的5层平台架构

    Android 是一种基于 Linux 的开放源代码软件栈,为广泛的设备和机型而创建.下图所示为 Android 平台的主要组件. Android 软件栈 Linux 内核 Android 平台的基础 ...

  3. springMVC源码分析--SimpleServletHandlerAdapter(二)

    上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...

  4. 查看4k对齐,激活.net framework 3.5

    查看是否4k对齐 Win+R,打开运行窗口,在窗口中输入“msinfo32",组件”--“存储”--“磁盘”.然后可以在右边栏看到“分区起始偏移”,我们图例中有2个数值,分别是:32256字 ...

  5. 【安卓开发】Layout Inflation不能这么用

    Layout inflation在Android上下文环境下转换XML文件成View结构对象的时候需要用到. LayoutInflater这个对象在Android的SDK中很常见,但是你绝对没想到竟然 ...

  6. 仿爱奇艺视频,腾讯视频,搜狐视频首页推荐位轮播图(二)之SuperIndicator源码分析

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼:http://blog.csdn.net/hejjunlin/article/details/52510431 背景:仿爱奇艺视频,腾讯视频 ...

  7. Matlab:如何查找给定目录下的文件

    我们有很多目录,每个目录下都有些有用的文件,比如图像文件,如何自动的扫描这些文件呢? 可以使用dir函数来完成这个任务. 比如假设给定目录 baseDir,它是一个字符串,包含的是某个目录,例如'./ ...

  8. Android计时器Chronometer-android学习之旅(二十一)

    Chronometer简介 Chronometer和DigitalColok都继承与TextView,但是Chronometer不是显示的当前时间,而是从某个时间开始又过去了多少时间,是一个时间差. ...

  9. AsyncTask(异步任务)讲解-android的学习之旅(四十六)

    AsyncTask简介 Android的UI线程主要处理用户的按键,触屏和View的绘制等,不能在里面处理耗时的操作,否则会出现ANR,因此耗时的操作要单独开一个线程处理,但是新线程不能直接处理UI线 ...

  10. iOS中 UISearchController 搜索栏 UI技术分享

    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...