Skip to content

Commit

Permalink
Make write_lines add a newline when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
bwarden committed Jan 26, 2023
1 parent 73cf865 commit 4969696
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
29 changes: 12 additions & 17 deletions src/ucd-data-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ static int write_lines(int out, FILE *f, size_t cl, const char *prefix)
if (write(out, buf, len) < (ssize_t)len) {
return 1;
}

/* Make sure this line ends with a newline when we write it */
if (buf[len-1] != '\n') {
if (write(out, "\n", 1) < (ssize_t)1) {
return 1;
}
}
}
}

Expand Down Expand Up @@ -384,13 +391,6 @@ int main(int argc, char *argv[]) {
FAIL("write_lines()");
}

/* Write an extra linefeed in case this didn't end with one */
if (write(out, "\n", 1) < (ssize_t) 1) {
close(out);
fclose(f);
unlink(outpath);
FAIL("write()");
}
close(sockfd);

/* reopen socket */
Expand Down Expand Up @@ -445,16 +445,11 @@ int main(int argc, char *argv[]) {
}

/* don't write part #2 if 404 or some non-error */
if (result != 2) {
if (write_lines(out, f, cl, "hostname: ") != 0) {
close(out);
fclose(f);
unlink(outpath);
FAIL("write_lines()");
}

/* Write an extra linefeed in case userdata hostname didn't end with one */
write(out, "\n", 1);
if ((result != 2) && (write_lines(out, f, cl, "hostname: ") != 0)) {
close(out);
fclose(f);
unlink(outpath);
FAIL("write_lines()");
}

/* cleanup */
Expand Down
1 change: 0 additions & 1 deletion tests/fetch_data/expected
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ ssh_authorized_keys:
- SSH_TEST_KEY_STRING_1
- SSH_TEST_KEY_STRING_2
- SSH_TEST_KEY_STRING_3

hostname: myhostname
#cloud-config

Expand Down

0 comments on commit 4969696

Please sign in to comment.