写一个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]);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dup函数Linux系统中的一个系统调用函数,其原型如下: ```c #include <unistd.h> int dup(int oldfd); ``` 该函数的作用是复制文件描述符,将oldfd指向的文件描述符复制到一个新的文件描述符中,并返回新的文件描述符。新的文件描述符是系统中当前可用的最小非负整数。 使用dup函数可以实现以下功能: 1. 文件重定向:可以使用dup函数将标准输入、标准输出或标准错误重定向到其他文件或设备。例如,可以通过`dup2(fd, STDOUT_FILENO)`将文件描述符fd复制到标准输出,实现输出重定向。 2. 管道通信:可以使用dup函数一个进程的标准输出与另一个进程的标准输入连接起来,实现进程间的通信。通过创建管道并使用dup函数将管道的一端复制到标准输入或标准输出,可以实现进程间的数据传输。 3. 复制文件描述符:在多进程编程中,可以使用dup函数一个文件描述符从父进程复制到子进程,使得子进程可以操作相同的文件或设备。 需要注意的是,dup函数只会复制文件描述符本身,不会复制文件状态标志(如文件偏移量、打开方式等)。如果需要复制文件状态标志,可以使用fcntl函数进行复制。 使用dup函数时,可能会出现一些错误情况,例如:文件描述符超出系统限制、文件描述符无效等。在出错时,dup函数会返回-1,并设置errno变量来指示具体的错误原因。 总之,dup函数Linux系统中非常常用的一个函数,可以实现文件重定向、管道通信和文件描述符复制等功能,对于Linux系统的开发工程师来说是必备的基础知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值