I’ve got kind of strange problem. I was working on a simple program which was supposed to send data taken from stdin over the net to another instance of this program, which should send the data directly to stdout. But it didn’t work as it should, sending just a part of the data. After that, I managed to find out where problem is in the reading from stdin:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE 1024
int main(void) {
char *buffer;
int read_bytes;
buffer = (char *) malloc(sizeof(char)*SIZE);
while ((read_bytes = read(STDIN_FILENO,buffer,SIZE)) == SIZE) {
write(STDOUT_FILENO,buffer,SIZE);
fprintf(stderr,"read %d bytes\n",read_bytes);
}
fprintf(stderr,"read %d bytes\n",read_bytes);
write(STDOUT_FILENO,buffer,read_bytes);
return 0;
}
(this is just to simply demostrate the problem, not actual part of original code)
It all works as it should (-> resending data from stdin to stdout) until I sourced from something which took more time, for example:
find / | ./the_programme > output.file
Output stopped after some thousand bytes, and I’m really confused why (it definitely wasn’t finished). I tried clearing O_NONBLOCK flag on STDIN_FILENO with fcntl, but it didn’t help at all. I am probably missing something terribly basic here, but neither man pages nor googling helped me.
From the manual (
man 2 read):