blob: d9a2529934fe19ee7869d5bd5aa236d43576d992 [file] [log] [blame]
Cyril Hrubise39f5a82013-07-11 17:50:44 +02001/*
2 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#define _GNU_SOURCE
20#include <stdarg.h>
21#include <stdio.h>
22#include "test.h"
23#include "safe_stdio.h"
24
25FILE *safe_fopen(const char *file, const int lineno, void (cleanup_fn)(void),
26 const char *path, const char *mode)
27{
28 FILE *f = fopen(path, mode);
29
30 if (f == NULL) {
31 tst_brkm(TBROK | TERRNO, cleanup_fn,
32 "fopen(%s) failed at %s:%d", path, file, lineno);
33 return NULL;
34 }
35
36 return f;
37}
38
39int safe_fclose(const char *file, const int lineno, void (cleanup_fn)(void),
40 FILE *f)
41{
42 int ret;
43
44 ret = fclose(f);
45
46 if (ret) {
47 tst_brkm(TBROK | TERRNO, cleanup_fn,
48 "fclose failed at %s:%d", file, lineno);
49 }
50
51 return ret;
52}
53
54int safe_asprintf(const char *file, const int lineno, void (cleanup_fn)(void),
55 char **strp, const char *fmt, ...)
56{
57 int ret;
58 va_list va;
59
60 va_start(va, fmt);
61 ret = vasprintf(strp, fmt, va);
62 va_end(va);
63
64 if (ret < 0) {
65 tst_brkm(TBROK | TERRNO, cleanup_fn,
66 "asprintf failed at %s:%d", file, lineno);
67 }
68
69 return ret;
70}