~kaction/config

ref: 569cada1bc5709b76d4fd05eb2696044ad68c338 config/universe/tempfile/tempfile.c -rw-r--r-- 722 bytes
569cada1 — Dmitry Bogatov Configure email fetching 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#define _GNU_SOURCE
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
	const char *name;
	const char *tempdir;
	char path[PATH_MAX];
	int fd;

	if (argc < 3) {
		fputs("usage: tempfile NAME PROG [ARGS...]\n", stderr);
		return 100;
	}
	name = argv[1];
	
	tempdir = getenv("TEMPDIR");
	if (!tempdir) tempdir = "/tmp";

	fd = open(tempdir, O_RDWR|O_TMPFILE, 0600);
	if (fd < 0) {
		perror("Failed to create temporary file");
		return 111;
	}

	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
	setenv(name, path, 1);

	argv += 2;
	execvp(*argv, argv);
	perror("exec failed");
	return 111;
}