summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2011-05-25 00:54:32 +0200
committerLennart Poettering <lennart@poettering.net>2011-06-21 19:29:44 +0200
commit5a3ab509b56bd79bf2be53cc259ce45c0be269e6 (patch)
treea10d9ac0e147efae9b3093eb51a8a2048e692e72 /src/util.c
parent5eda94dda25bccda928c4b33c790dbe748573a22 (diff)
util: add fopen_temporary()
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index ad1ca06534..156d32a2e0 100644
--- a/src/util.c
+++ b/src/util.c
@@ -4455,7 +4455,49 @@ int pipe_eof(int fd) {
return pollfd.revents & POLLHUP;
}
+int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
+ FILE *f;
+ char *t;
+ const char *fn;
+ size_t k;
+ int fd;
+
+ assert(path);
+ assert(_f);
+ assert(_temp_path);
+
+ t = new(char, strlen(path) + 1 + 6 + 1);
+ if (!t)
+ return -ENOMEM;
+
+ fn = file_name_from_path(path);
+ k = fn-path;
+ memcpy(t, path, k);
+ t[k] = '.';
+ stpcpy(stpcpy(t+k+1, fn), "XXXXXX");
+
+ fd = mkostemp(t, O_WRONLY|O_CLOEXEC);
+ if (fd < 0) {
+ free(t);
+ return -errno;
+ }
+
+ f = fdopen(fd, "we");
+ if (!f) {
+ unlink(t);
+ free(t);
+ return -errno;
+ }
+
+ *_f = f;
+ *_temp_path = t;
+
+ return 0;
+}
+
int terminal_vhangup_fd(int fd) {
+ assert(fd >= 0);
+
if (ioctl(fd, TIOCVHANGUP) < 0)
return -errno;