Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Andrew Jeffery | 45cfc38 | 2017-04-12 14:15:50 +0930 | [diff] [blame] | 3 | |
| 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 Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 12 | static const char *tmpf_dir = "/tmp/"; |
| 13 | |
Andrew Jeffery | 45cfc38 | 2017-04-12 14:15:50 +0930 | [diff] [blame] | 14 | int tmpf_init(struct tmpf *tmpf, const char *template) |
| 15 | { |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 16 | strcpy(tmpf->path, tmpf_dir); |
| 17 | strncat(tmpf->path, template, sizeof(tmpf->path) - sizeof(tmpf_dir)); |
Andrew Jeffery | 45cfc38 | 2017-04-12 14:15:50 +0930 | [diff] [blame] | 18 | |
| 19 | tmpf->fd = mkstemp(tmpf->path); |
| 20 | if (tmpf->fd < 0) { |
| 21 | perror("mkstemp"); |
| 22 | return -1; |
| 23 | } |
| 24 | |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | void tmpf_destroy(struct tmpf *tmpf) |
| 29 | { |
| 30 | if (tmpf->fd) |
| 31 | close(tmpf->fd); |
| 32 | |
| 33 | if (tmpf->path) |
| 34 | unlink(tmpf->path); |
| 35 | } |