summaryrefslogtreecommitdiffstats
path: root/test/tmpf.c
blob: 483231bebc32ad5ae60e50dbcee87ad82cdd46ea (plain)
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2018 IBM Corp.

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "test/tmpf.h"

static const char *tmpf_dir = "/tmp/";

int tmpf_init(struct tmpf *tmpf, const char *template)
{
	strcpy(tmpf->path, tmpf_dir);
	strncat(tmpf->path, template, sizeof(tmpf->path) - sizeof(tmpf_dir));

	tmpf->fd = mkstemp(tmpf->path);
	if (tmpf->fd < 0) {
		perror("mkstemp");
		return -1;
	}

	return 0;
}

void tmpf_destroy(struct tmpf *tmpf)
{
	if (tmpf->fd)
		close(tmpf->fd);

	if (tmpf->path)
		unlink(tmpf->path);
}
OpenPOWER on IntegriCloud