免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 710 | 回复: 0
打印 上一主题 下一主题

2_4_2___test.c [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-08 20:58 |只看该作者 |倒序浏览

                2_4_2___test.c
#include stdio.h>
#include stdlib.h>
#include string.h>
#include strings.h>
#include getopt.h>
#include sys/select.h>
#include sys/types.h>
#include sys/stat.h>
#include errno.h>
#include fcntl.h>
#include unistd.h>
void read_line( int file , char * buff )
{
        while( 1 ){
                if( read( file, buff, 1 ) == 1 ){
                        if( *buff == '\n' ){
                                break;
                        }
                        buff++;
                }
        }
}
void write_line( int file , char * buff )
{
        while( 1 ){
                if( write( file, buff, 1 ) == 1 ){
                        if( *buff  == '\n' ){
                                break;
                        }
                        buff++;
                }
        }
}
void read_stdin( int pipe )
{
        char    buff[ 512 ];
        fcntl( 0, F_SETFL, O_NONBLOCK );
      
        read_line( 0,buff );
        write_line( pipe, buff );
}
void read_pipe( int pipe )
{
        char    buff[ 512 ];
        
        fcntl( pipe, F_SETFL, O_NONBLOCK );
        
        read_line( pipe,buff );
        write_line( 1, buff );
}
void write_pipe( void )
{
        printf("write pipe\n");
}
void usage( void )
{
        printf( "Usage :\n" );
        printf( "\ttestpipe -f /dev/pipe0 \n" );
        printf( "\tor\n" );
        printf( "\ttestpipe -f /dev/pipe1 \n" );
}
int main( int argc, char * argv[] )
{
        fd_set  rfds;   /* read files */
        fd_set  wfds;   /* write files */
        int     ret;
        char  * path = "";
        char  * opts = "f:";
        int     pipe;
        int     op;
        
        /* get arguments and check it*/
        if( argc != 3 ){
                usage();
                return 0;
        }
        while( ( op = getopt( argc, argv, opts ) ) != EOF ){
                switch( op ){
                        case 'f':
                                path = optarg;
                                break;
                                
                        default:
                                usage();
                                return 0;
                                break;
                }
        }
      
        if( ( strcmp( path, "/dev/pipe0" )  == 0 ) ||
            ( strcmp( path, "/dev/pipe1" )  == 0 ) ){
        }
        else{
                usage();
                return 0;
        }
        
               
        /* open pipe-file and select it */
        pipe = open( path, O_RDWR );
        if( pipe == -1 ){
                printf( "open %s failed : %d \n", path, errno );
                return 0;
        }
      
        /* main loop */
        while( 1 ){
                FD_ZERO( &rfds );
                FD_SET( 0, &rfds );
                FD_SET( pipe, &rfds );
               
                FD_ZERO( &wfds );
                FD_SET( pipe, &wfds );
                ret = select( pipe + 1, &rfds, NULL, NULL, NULL );
                if( ret == -1 ){
                        printf( "select error : %d\n", errno );
                        break;
                }
                else if( ret > 0 ){
                        if( FD_ISSET( 0, &rfds ) ){
                                read_stdin( pipe );
                        }
                        else if( FD_ISSET( pipe, &rfds ) ){
                                read_pipe( pipe );
                        }
                }
        }
        close( pipe );
        
        return 0;
}
               
               
返回目录


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/68424/showart_681415.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP