non-nul terminated messages on the PIPE will be accepted so that messages
can be sent with echo. But such message need to be parsed by the daemon
before a new message come in, else the behaviour is unspecified.
char* bp = &buf[0];
do {
char* cp = strchr(bp, '\0');
- if (cp == &buf[n]) {
+ if (bp != &buf[0] && cp == &buf[n]) {
/*
* We reached the end of the read buffer, but the last message
* is missing it's NUL terminator. The message must be
u_int left = cp-bp;
memmove(buf, bp, left);
n = Sys::read(fd, &buf[left], sizeof (buf)-1-left);
- n += left;
+ if (n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
+ /* There is nothing left to read. The message must not be
+ NUL terminated. (maybe sent with "echo") */
+ n = left + 1;
+ buf[n-1] = '\0';
+ } else
+ n += left;
buf[n] = '\0';
bp = &buf[0];
} else if (cp > bp) {