實(shí)現(xiàn)的dup2()函數(shù)的源碼怎么寫

這篇文章將為大家詳細(xì)講解有關(guān)實(shí)現(xiàn)的dup2( )函數(shù)的源碼怎么寫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

成都創(chuàng)新互聯(lián)公司作為成都網(wǎng)站建設(shè)公司,專注網(wǎng)站建設(shè)公司、網(wǎng)站設(shè)計(jì),有關(guān)企業(yè)網(wǎng)站設(shè)計(jì)方案、改版、費(fèi)用等問題,行業(yè)涉及成都白烏魚等多個(gè)領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認(rèn)可。

原 dup2()函數(shù):

#include <unistd.h>

int dup2( int fd, int fd2 );

對(duì)于 dup2,可以用 fd2 參數(shù)指定新描述符的值。如果 fd2 已經(jīng)打開,則先將其關(guān)閉。如若 fd 等于 fd2,則 dup2 返回 fd2,而不關(guān)閉它。否則,fd2 的 FD_CLOEXEC 文件描述符標(biāo)志就被清除,這樣 fd2 在進(jìn)程調(diào)用 exec 時(shí)是打開狀態(tài)。該函數(shù)返回的新文件描述符與參數(shù) fd 共享同一個(gè)文件表項(xiàng)。

下面是自己實(shí)現(xiàn)的 dup2函數(shù):

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> 
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

//檢查文件描述符是否有效
int isFileDescriptor( int fd )
{
    struct stat st;
    if( (-1 == fstat(fd,&st)) && (EBADF == errno) )
        return -1;
    return 0;
}

int my_dup2( int oldfd, int newfd )
{
    int tempfd;
    int fd_count;
    int fdarray[newfd];
    int res;

    if( -1 == isFileDescriptor( oldfd ) ) {
        printf("the file descriptor is invalid.\n");
        return -1;
    }

    //如果newfd等于oldfd,則直接返回newfd,而不關(guān)閉它
    if( oldfd == newfd ) 
        return newfd;
    
    //否則,關(guān)閉newfd
    if( 0 == isFileDescriptor( newfd ) ) {
        res = close( newfd );  
        if( -1 == res ) {
            perror("close file descriptor failed");
            return -1;
        }
    }

    //復(fù)制文件描述符
    for( fd_count=0; fd_count<newfd; fd_count++ )
        fdarray[fd_count] = 0;
    
    for( fd_count=0; fd_count<newfd; fd_count++ ) {
        tempfd = dup( oldfd );
        if( -1 == tempfd )
            return -1;
        if( tempfd == newfd )
            break;
        else
            fdarray[fd_count] = 1;

    }

    //關(guān)閉之前尋找指定描述符時(shí)打開的描述符
    for( fd_count=0; fd_count<newfd; fd_count++ ) {
        if( 1 == fdarray[fd_count] ) {
            res = close( fd_count );
            if( -1 ==res ) {
                perror("close file descriptor failed");
                return -1;
            }
        }
    }

    return tempfd;
}

//測試代碼
int main()
{
    int fd;
    int testfd;
    int res;
    char *buffer = (char *)malloc(sizeof(char)*32);
    fd = open("/tmp/dup2test1.txt", O_RDWR | O_CREAT, 0666);
    if( -1 == fd ) {
        perror("file open failed");
        exit( EXIT_SUCCESS );
    }
    
    testfd = my_dup2( fd, 5 );

    res = write( testfd, "Hey man!", strlen("Hey man!") ); //通過復(fù)制得到的文件描述符 testfd 寫數(shù)據(jù)
    if( -1 == res ) {
        perror("write to testfd failed");
        exit( EXIT_FAILURE );
    }

    printf("write to testfd %d successfully\n", testfd);

    memset( buffer, '\0', 32 );
    lseek( testfd, 0, SEEK_SET );
    res = read( fd, buffer, 30 );  //通過初始的文件描述符 fd 讀取數(shù)據(jù)
    if( -1 == res ) {
        perror("read from testfd failed");
        exit( EXIT_FAILURE );
    }
    printf("read from initial fd %d is: %s\n", fd, buffer );
    exit( EXIT_SUCCESS );
}

程序運(yùn)行結(jié)果:

[zhang@localhost APUE]$ ./my_dup2
write to testfd 5 successfully
read from initial fd 3 is: Hey man!

測試通過。

關(guān)于實(shí)現(xiàn)的dup2( )函數(shù)的源碼怎么寫就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文標(biāo)題:實(shí)現(xiàn)的dup2()函數(shù)的源碼怎么寫
轉(zhuǎn)載源于:http://bm7419.com/article14/pcside.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、定制網(wǎng)站用戶體驗(yàn)、服務(wù)器托管商城網(wǎng)站、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站