I'm writing a program that exec's another program and using two
unidirectional pipes and redirected stdin and stdout to provide
communication between the two programs. The problem I am having is data
will sit in the stdout buffer and not travel over the pipe. The reason I
think this is if I do a fflush(stdout) after the printf statement the
parent gets the data. Is there anyway I can automatically have it flush
stdout when it gets a newline? Or not have it buffer at all, just go
straight to the pipe.
Here is the code so far:
/* BEGIN READ.C */
#include <stdio.h>
/* #include <sys/uio.h> */
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
int readl( int, char *, size_t );
main( int argc, char **argv )
{
int pipe1[2], pipe2[2];
FILE *hlout, *hlin;
pid_t child;
char inbuf[256];
char obuf[256];
pipe( pipe1 );
pipe( pipe2 );
if ((child = fork() ) == 0 )
{
dup2( pipe1[1], fileno(stdout) );
dup2( pipe2[0], fileno(stdin) );
if ( execlp("/usr/home/leadzero/echo", NULL ) == -1 )
{
perror( "execle" );
exit(0);
}
} else {
readl( pipe1[0], obuf, 256 );
puts( obuf );
}
return 1;
int readl( int fd, char *buf, size_t nbytes )Quote:}
{
static int b, rb;
for ( b = 0; b < nbytes; b++ )
{
rb = read( fd, (buf + b), 1);
if ( rb == 0 || buf[b] == '\n' )
return b;
else if ( rb == -1 )
return rb;
}
return b;
/* END READ.C */Quote:}
/* BEGIN ECHO.C */
#include <stdio.h>
#include <string.h>
main()
{
char ibuf[256], obuf[256];
unsigned int p;
printf("Starting echo...\n");
/* Without this line read (the parent) will stay blocked in read() */
fflush(stdout);
fgets( ibuf, 256, stdin );
for ( p = 0; p < 256; p++ )
{
obuf[p] = toupper( ibuf[p] );
}
puts( obuf );
return 1;
/* END ECHO.C */Quote:}
Any help is appreciated.
--
Ryan "leadZERO" Sommers
ICQ: 1019590