APUE Learning Example Source Code
guowenxue
2020-06-06 cd10eef059e9d9c34c927f3ac53ac43f4a9716c1
ch6_ipc/fifo_chat.c
@@ -31,8 +31,6 @@
    fd_set         rdset;
    char           buf[1024];
    int            mode = 0;
    char          *rd_fifo = NULL;
    char          *wr_fifo = NULL;
    if( argc != 2 )
    {
@@ -42,17 +40,6 @@
    }
    mode = atoi(argv[1]);
    if( 0 == mode )
    {
        rd_fifo = FIFO_FILE1;
        wr_fifo = FIFO_FILE2;
    }
    else
    {
        rd_fifo = FIFO_FILE2;
        wr_fifo = FIFO_FILE1;
    }
    if( access(FIFO_FILE1 , F_OK) )
    {
@@ -70,33 +57,33 @@
    if( 0 == mode )
    {
        printf("start open '%s' for read and it will blocked untill write endpoint opened...\n", rd_fifo);
        if( (fdr_fifo=open(rd_fifo, O_RDONLY)) < 0 )
        printf("start open '%s' for read and it will blocked untill write endpoint opened...\n", FIFO_FILE1);
        if( (fdr_fifo=open(FIFO_FILE1, O_RDONLY)) < 0 )
        {
            printf("Open fifo[%s] for chat read endpoint failure: %s\n", rd_fifo, strerror(errno));
            printf("Open fifo[%s] for chat read endpoint failure: %s\n", FIFO_FILE1, strerror(errno));
            return -1;
        } 
        
        printf("start open '%s' for write...\n", wr_fifo);
        if( (fdw_fifo=open(wr_fifo, O_WRONLY)) < 0 )
        printf("start open '%s' for write...\n", FIFO_FILE2);
        if( (fdw_fifo=open(FIFO_FILE2, O_WRONLY)) < 0 )
        {
            printf("Open fifo[%s] for chat write endpoint failure: %s\n", wr_fifo, strerror(errno));
            printf("Open fifo[%s] for chat write endpoint failure: %s\n", FIFO_FILE2, strerror(errno));
            return -1;
        }
    }
    else
    {
        printf("start open '%s' for write and it will blocked untill read endpoint opened...\n", wr_fifo);
        if( (fdw_fifo=open(wr_fifo, O_WRONLY)) < 0 )
        printf("start open '%s' for write and it will blocked untill read endpoint opened...\n", FIFO_FILE1);
        if( (fdw_fifo=open(FIFO_FILE1, O_WRONLY)) < 0 )
        {
            printf("Open fifo[%s] for chat write endpoint failure: %s\n", wr_fifo, strerror(errno));
            printf("Open fifo[%s] for chat write endpoint failure: %s\n", FIFO_FILE1, strerror(errno));
            return -1;
        }
        printf("start open '%s' for read...\n", rd_fifo);
        if( (fdr_fifo=open(rd_fifo, O_RDONLY)) < 0 )
        printf("start open '%s' for read...\n", FIFO_FILE2);
        if( (fdr_fifo=open(FIFO_FILE2, O_RDONLY)) < 0 )
        {
            printf("Open fifo[%s] for chat read endpoint failure: %s\n", rd_fifo, strerror(errno));
            printf("Open fifo[%s] for chat read endpoint failure: %s\n", FIFO_FILE2, strerror(errno));
            return -1;
        } 
    }