about summary refs log tree commit diff
path: root/tmpfd.c
blob: 193fa9065b7a1c78a71722e3fba42b27f245f829 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}