Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 4969696

Browse files
committed
Make write_lines add a newline when necessary
1 parent 73cf865 commit 4969696

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

src/ucd-data-fetch.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ static int write_lines(int out, FILE *f, size_t cl, const char *prefix)
227227
if (write(out, buf, len) < (ssize_t)len) {
228228
return 1;
229229
}
230+
231+
/* Make sure this line ends with a newline when we write it */
232+
if (buf[len-1] != '\n') {
233+
if (write(out, "\n", 1) < (ssize_t)1) {
234+
return 1;
235+
}
236+
}
230237
}
231238
}
232239

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

387-
/* Write an extra linefeed in case this didn't end with one */
388-
if (write(out, "\n", 1) < (ssize_t) 1) {
389-
close(out);
390-
fclose(f);
391-
unlink(outpath);
392-
FAIL("write()");
393-
}
394394
close(sockfd);
395395

396396
/* reopen socket */
@@ -445,16 +445,11 @@ int main(int argc, char *argv[]) {
445445
}
446446

447447
/* don't write part #2 if 404 or some non-error */
448-
if (result != 2) {
449-
if (write_lines(out, f, cl, "hostname: ") != 0) {
450-
close(out);
451-
fclose(f);
452-
unlink(outpath);
453-
FAIL("write_lines()");
454-
}
455-
456-
/* Write an extra linefeed in case userdata hostname didn't end with one */
457-
write(out, "\n", 1);
448+
if ((result != 2) && (write_lines(out, f, cl, "hostname: ") != 0)) {
449+
close(out);
450+
fclose(f);
451+
unlink(outpath);
452+
FAIL("write_lines()");
458453
}
459454

460455
/* cleanup */

tests/fetch_data/expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ ssh_authorized_keys:
66
- SSH_TEST_KEY_STRING_1
77
- SSH_TEST_KEY_STRING_2
88
- SSH_TEST_KEY_STRING_3
9-
109
hostname: myhostname
1110
#cloud-config
1211

0 commit comments

Comments
 (0)