blob: 483231bebc32ad5ae60e50dbcee87ad82cdd46ea [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery45cfc382017-04-12 14:15:50 +09303
4#define _GNU_SOURCE
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10#include "test/tmpf.h"
11
Andrew Jefferyc3144042018-02-26 13:24:52 +103012static const char *tmpf_dir = "/tmp/";
13
Andrew Jeffery45cfc382017-04-12 14:15:50 +093014int tmpf_init(struct tmpf *tmpf, const char *template)
15{
Andrew Jefferyc3144042018-02-26 13:24:52 +103016 strcpy(tmpf->path, tmpf_dir);
17 strncat(tmpf->path, template, sizeof(tmpf->path) - sizeof(tmpf_dir));
Andrew Jeffery45cfc382017-04-12 14:15:50 +093018
19 tmpf->fd = mkstemp(tmpf->path);
20 if (tmpf->fd < 0) {
21 perror("mkstemp");
22 return -1;
23 }
24
25 return 0;
26}
27
28void tmpf_destroy(struct tmpf *tmpf)
29{
30 if (tmpf->fd)
31 close(tmpf->fd);
32
33 if (tmpf->path)
34 unlink(tmpf->path);
35}