write tool fixes

This commit is contained in:
Luxferre
2026-10-14 12:19:11 +03:00
parent 59c85e9217
commit 05bce7ca9c
4 changed files with 31 additions and 6 deletions
+6
View File
@@ -229,6 +229,8 @@ static int AL(Cfg &cfg, Messages &msgs, Usage &out, volatile int *interrupted, s
else if (!has_line && !has_byte && !has_del_lines && !has_del_bytes) {
/* No offset/deletion parameters: overwrite the whole file. */
std::string c = filter_text(cv);
/* Ensure the written file ends with a single trailing newline. */
if (!c.empty() && c.back() != '\n') c.push_back('\n');
auto sl = fp.find_last_of('/');
if (sl != std::string::npos) mkdir_p(std::string_view(fp).substr(0, sl));
else mkdir_p(".");
@@ -236,6 +238,10 @@ static int AL(Cfg &cfg, Messages &msgs, Usage &out, volatile int *interrupted, s
else { res = sfmt("Successfully wrote %zu bytes to %s", c.size(), fp.c_str()); }
} else {
int werr = 0; std::string c = filter_text(cv);
/* Line-based edits keep the text-file trailing-newline convention;
* byte-based edits are exact byte operations and must not have a
* newline injected. */
if (!(has_byte || has_del_bytes) && !c.empty() && c.back() != '\n') c.push_back('\n');
res = write_file(fp, line_off, byte_off, del_lines, del_bytes, c, &werr);
if (werr) sty = 31;
}