summaryrefslogtreecommitdiff
path: root/src/file.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2019-04-07 09:31:36 +1000
committerMaxime Coste <mawww@kakoune.org>2019-04-07 09:32:17 +1000
commit744778be30f2f78bea4af5523f00da98480cda4d (patch)
treee3f6032a644c471a8959da4c0d964b65785110dc /src/file.cc
parent835f2239a7428d3457e277c95ad39e2cdd5211d5 (diff)
Add a -to-file <filename> switch to the echo command
As discussed in #2836
Diffstat (limited to 'src/file.cc')
-rw-r--r--src/file.cc9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/file.cc b/src/file.cc
index 7b49595e..52a0f288 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -257,6 +257,15 @@ void write(int fd, StringView data)
}
}
+void write_to_file(StringView filename, StringView data)
+{
+ const int fd = open(filename.zstr(), O_CREAT | O_WRONLY | O_TRUNC, 0644);
+ if (fd == -1)
+ throw file_access_error(filename, strerror(errno));
+ auto close_fd = on_scope_end([fd]{ close(fd); });
+ write(fd, data);
+}
+
struct BufferedWriter
{
BufferedWriter(int fd) : fd{fd} {}