View Full Version : Pipe not working fully

03-04-2003, 06:05 PM
I have made a little program that works similar to cat, but it also decrypts/encrypts the stream, as well as printing to stdout. It does this by simple XOR encryption.

/* XOR each character in the stream, printing each XOR'ed char to stdout. */
while( (ch = getc(fp)) != EOF )
putc(ch^(*(argv[key_arg]+offset)), stdout);
if( ++offset >= length )
offset = 0;

Above is a fragment of my code; it XORs a character read from a file with a "key" value, and then prints that value to stdout.
It has a very specific problem: it won't pipe data read from stdin to other programs, it will only pipe "normal" files successfully.

When I run a command like this:
xor [file] [key] | nc 1234
the listening nc will recieve the XOR encrypted [file], no hassle at all.

But, when I run this,
xor [key] | nc 1234
the listening nc doesn't recieve any data that I send (entered via keyboard).

When I test it with cat,
cat | nc 1234
the listening nc recieves any data that I send.

When I just run,
xor [key]
It works just fine; it prints the XOR'ed value of each char that I enter via the keyboard.

This is only a problem when I try to use stdin as input and pipe the XOR'ed output to another program. The program will pipe a "normal" file to other programs, no trouble at all.
Why does it appear to work for stdin by itself, but not when used in conjunction with other programs?
I have tried reading the source for cat, but it is too complex for me to understand fully.

I am simply assigning stdin to fp like this:
fp = stdin;

03-05-2003, 10:09 AM
Thanks for the reply man, but I have just found out what the problem was: no "fflush(stdout);". I kicked myself after realising that I overlooked this. :)