1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright (C) 2018 IBM Corp. 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 12 static const char *tmpf_dir = "/tmp/"; 13 14 int tmpf_init(struct tmpf *tmpf, const char *template) 15 { 16 strcpy(tmpf->path, tmpf_dir); 17 strncat(tmpf->path, template, sizeof(tmpf->path) - sizeof(tmpf_dir)); 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 } 36