about summary refs log tree commit diff
path: root/tmpfd.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmpfd.c')
-rw-r--r--tmpfd.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/tmpfd.c b/tmpfd.c
new file mode 100644
index 0000000..193fa90
--- /dev/null
+++ b/tmpfd.c
@@ -0,0 +1,17 @@
+// SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>
+// SPDX-License-Identifier: EUPL-1.2
+
+#include <stdio.h>
+#include <unistd.h>
+
+int tmpfd(void)
+{
+	int fd = -1;
+	FILE *f = tmpfile();
+	if (!f)
+		return -1;
+	if ((fd = fileno(f)) != -1)
+		fd = dup(fd);
+	fclose(f);
+	return fd;
+}