aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_add_output_document.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-05-05 10:24:40 +0200
committerMattias Andrée <maandree@kth.se>2024-05-05 10:24:40 +0200
commit3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e (patch)
treeb7717583b99f29028c85c3423cf43b104dfa8943 /libexec_add_output_document.c
downloadlibexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.gz
libexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.bz2
libexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.xz
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libexec_add_output_document.c')
-rw-r--r--libexec_add_output_document.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/libexec_add_output_document.c b/libexec_add_output_document.c
new file mode 100644
index 0000000..9eb1f8d
--- /dev/null
+++ b/libexec_add_output_document.c
@@ -0,0 +1,45 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+int
+libexec_add_output_document(struct libexec_command *cmd, int fd, struct libexec_document *doc, int flags)
+{
+ int fd_flags, fl_flags, fds[2];
+
+ if (!cmd || fd < 0 || !doc) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ fd_flags = (flags & O_CLOEXEC);
+ fl_flags = (flags ^ fd_flags);
+ if (fd_flags)
+ fd_flags = FD_CLOEXEC;
+
+ if (pipe(fds))
+ return -1;
+ if (fd_flags && fcntl(fds[0], F_SETFD, fd_flags))
+ goto fail;
+ if (fl_flags && fcntl(fds[0], F_SETFL, fl_flags))
+ goto fail;
+
+ doc->fd = fds[0];
+
+ if (libexec_dup(cmd, fd, fds[1]))
+ goto fail;
+ cmd->plumings[cmd->nplumings - 1].type = LIBEXEC_PLUMING_PIPE;
+
+ return 0;
+
+fail:
+ close(fds[0]);
+ close(fds[1]);
+ return -1;
+}
+
+
+#else
+LIBEXEC_CONST__ int main(void) {return 0;} /* TODO test */
+#endif