Chinaunix

标题: 2_4_2___test.c [打印本页]

作者: happyzlz    时间: 2008-05-08 20:58
标题: 2_4_2___test.c

                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




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2