Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/stdbuf_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,16 @@ static int stdbuf_v2(unsigned int node, unsigned int timeout, char * logfile) {

#define LF '\n' /* ASCII 10d */
#define CR '\r' /* ASCII 13d */
bool lf_missing = false;
int ignore __attribute__((unused));
while ((packet = csp_read(conn, timeout))) {
int again = packet->data[0];
int ignore __attribute__((unused));
ignore = write(fileno(stdout), &packet->data[1], packet->length - 1);
if (again == 0 && packet->data[packet->length - 1] != LF) {
/* If this was the last packet and there was no LF we will add one here to flush the stdout file */
ignore = write(fileno(stdout), "\n", 1);
if (packet->length > 1) {
ignore = write(fileno(stdout), &packet->data[1], packet->length - 1);
lf_missing = false;
if (packet->data[packet->length - 1] != LF) {
lf_missing = true;
}
}
if (log_f) {
int i = 1;
Expand Down Expand Up @@ -230,6 +233,16 @@ static int stdbuf_v2(unsigned int node, unsigned int timeout, char * logfile) {
}
}

/**
* We might have something lingering in the stdout due to a missing Line Feed.
* This could be caused if we experienced a time out during reception of stdbuf
* packets in the while-loop, or if the last packet did not contain a Line Feed
* as its last character. We then need to flush the stdout by injecting a LF.
*/
if (lf_missing) {
ignore = write(fileno(stdout), "\n", 1);
}

csp_close(conn);

return SLASH_SUCCESS;
Expand Down
Loading