please help the pathetic ones ...
Gert Doering (gert@greenie.muc.de)
Sun, 24 Jan 1999 15:03:12 +0100
Hi,
On Sun, Jan 24, 1999 at 01:34:58AM -0500, Jan Vicherek wrote:
> chBuf = getc(fsend);
> putc(chBuf, frec);
> putc(chBuf, lsend);
> putc(chBuf, lall);
This is the main problem - you have to make sure that the device doesn't
block, and uses the proper baud rate, etc. - see a POSIX book about the
"termios" interface.
(For now, just use Marc's tool :), but if you want to write one yourself,
you need to do "raw" IO to the port first).
Here is what I wrote, for about the same purpose, some years ago - is also
fairly pathetic, needs GOBS of CPU, but was enough to find some nasties in
a commercial Windows fax program :)
gert
----
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/times.h>
#include <ctype.h>
#ifndef linux
#include <sys/select.h>
#endif
char * tty1 = "tty4b";
char * tty2 = "tty1a";
#define SPEED1 B19200
#define LINPAR1 CS8
#define SPEED2 B19200
#define LINPAR2 CS8
#include "mgetty.h"
int verbose = 1;
int log_level;
void display( char * buf, int len, int chan )
{
static int last_chan = -1;
int i;
static int linelen = 0;
if ( chan != last_chan )
{
printf( "\n%c ", (chan == 1 )? '>' : '<' );
last_chan = chan;
lprintf( L_NOISE, "%c ", (chan == 1)? '>': '<' );
linelen = 0;
}
for ( i=0; i<len; i++ )
{
if ( isprint( buf[i] ) )
{
putc( buf[i], stdout );
linelen++;
}
else
{
printf( "[%02x]", (unsigned char) buf[i]);
linelen += 4;
}
lputc( L_NOISE, buf[i] );
if ( linelen > 75 || buf[i] == '\n' )
{
printf( "\n%c ", (chan == 1 )? '>' : '<' );
lprintf( L_NOISE, "%c ", (chan == 1)? '>': '<' );
linelen = 0;
}
}
if ( log_level >= L_NOISE ) fflush( stdout );
}
main( int argc, char ** argv )
{
int fd1, fd2;
char Device[MAXPATH];
struct termio termio1, termio2;
log_init_paths( argv[0], "/u/gert/mgetty/sermon/Log", NULL );
log_set_llevel( log_level = L_NOISE );
if ( verbose ) printf( "opening %s: ('>')\n", tty1 );
if ( makelock( tty1 ) == FAIL )
{
fprintf( stderr, "cannot lock %s\n", tty1 );
exit(1);
}
sprintf( Device, "/dev/%s", tty1 );
if ( ( fd1 = open( Device, O_RDWR | O_NDELAY ) ) == -1 )
{
perror( "open" );
exit(2);
}
if ( verbose ) printf( "opening %s: ('<')\n", tty2 );
if ( makelock( tty2 ) == FAIL )
{
fprintf( stderr, "cannot lock %s\n", tty2 );
exit(3);
}
sprintf( Device, "/dev/%s", tty2 );
if ( ( fd2 = open( Device, O_RDWR | O_NDELAY ) ) == -1 )
{
perror( "open" );
exit(4);
}
if ( verbose) printf( "doing fcntl()s\n" );
fcntl( fd1, F_SETFL, O_RDWR);
fcntl( fd2, F_SETFL, O_RDWR);
if ( verbose ) printf( "doing ioctl()s\n" );
ioctl( fd1, TCGETA, &termio1 );
termio1.c_iflag = 0;
termio1.c_oflag = 0;
termio1.c_cflag = SPEED1 | LINPAR1 | CREAD | CLOCAL | HUPCL;
termio1.c_lflag = 0;
termio1.c_cc[VMIN] = 1;
termio1.c_cc[VTIME] = 0;
if ( ioctl( fd1, TCSETA, &termio1 ) < 0 )
{
perror( "ioctl set 1" ); exit(6);
}
ioctl( fd2, TCGETA, &termio2 );
termio2.c_iflag = 0;
termio2.c_oflag = 0;
termio2.c_cflag = SPEED2 | LINPAR2 | CREAD | CLOCAL | HUPCL;
termio2.c_lflag = 0;
termio2.c_cc[VMIN] = 1;
termio2.c_cc[VTIME] = 0;
if ( ioctl( fd2, TCSETA, &termio2 ) < 0 )
{
perror( "ioctl set 2" ); exit(7);
}
if ( verbose ) printf( "WAITING FOR DATA\n" );
while( 1 )
{
fd_set fds;
char buf[1000];
int r, l;
FD_ZERO( &fds );
FD_SET( fd1, &fds );
FD_SET( fd2, &fds );
FD_SET( 0, &fds );
r = select( FD_SETSIZE, &fds, NULL, NULL, NULL );
if ( FD_ISSET( fd1, &fds ) )
{
l = read( fd1, buf, sizeof( buf ));
write( fd2, buf, l );
display( buf, l, 1 );
}
if ( FD_ISSET( fd2, &fds ) )
{
l = read( fd2, buf, sizeof( buf ));
write( fd1, buf, l );
display( buf, l, 2 );
}
if ( FD_ISSET( 0, &fds ) )
{
printf( "data entered -> exiting\n" );
exit(5);
}
}
}
--
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany gert@greenie.muc.de
fax: +49-89-35655025 gert.doering@physik.tu-muenchen.de