From 97f918d3642d74d9eb8f83f2f1425f91ef052f55 Mon Sep 17 00:00:00 2001 From: Thibault Godouet Date: Sun, 13 Apr 2014 17:54:05 +0100 Subject: [PATCH] fcrondyn -x no longer print null characters at the end of the command output --- doc/en/changes.sgml | 3 +++ fcrondyn.c | 17 +++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/doc/en/changes.sgml b/doc/en/changes.sgml index 2f38dd4..a2f8b5e 100644 --- a/doc/en/changes.sgml +++ b/doc/en/changes.sgml @@ -20,6 +20,9 @@ A copy of the license is included in gfdl.sgml. Fixed bug where LONG_MAX could potentially be used in functions like localtime(), which is too big for localtime(). This was unlikely to happen unless there was another problem in the first place. + + fcrondyn -x no longer adds null characters at the end of the output. This would cause problems if the output was piped to something as grep. Thanks Dimitri Semitsoglou-Tsiapos for pointing this out. + diff --git a/fcrondyn.c b/fcrondyn.c index d6cbb47..294768f 100644 --- a/fcrondyn.c +++ b/fcrondyn.c @@ -143,8 +143,7 @@ usage(void) " -d set up debug mode.\n" " -h display this help message.\n" " -V display version & infos about fcrondyn.\n" "\n" - "To list the available commands, run:\n" - " fcrondyn -x help\n"); + "To list the available commands, run:\n" " fcrondyn -x help\n"); exit(EXIT_ERR); } @@ -578,8 +577,10 @@ talk_fcron(char *cmd_str, int fd) return ERR; } - while ((read_len = (size_t) recv(fd, buf, sizeof(buf), 0)) >= 0 + + while ((read_len = (size_t) recv(fd, buf, sizeof(buf) - 1, 0)) >= 0 || errno == EINTR) { + if (errno == EINTR && debug_opt) fprintf(stderr, "got EINTR ...\n"); else if (read_len > sizeof(buf)) { @@ -595,16 +596,20 @@ talk_fcron(char *cmd_str, int fd) return ERR; } else { - if (write(STDOUT_FILENO, buf, read_len) < 0) - error_e("unable to write() to STDOUT_FILENO"); + /* ensure the string is terminated by a '\0' for when we'll printf() it */ + buf[read_len] = '\0'; + printf("%s", buf); + + /* check for the end of command output marker */ if (read_len >= sizeof(END_STR) && strncmp(&buf[read_len - sizeof(END_STR)], END_STR, sizeof(END_STR)) == 0) break; } } - if (read_len < 0) + if (read_len < 0) { error_e("error in recv()"); + } if (!existing_connection) xclose_check(&fd, "unix socket"); -- 2.47.3