summaryrefslogtreecommitdiffstats
path: root/kernel/tests/lib/safe_stdio.c
blob: 966a039a5c94e8e16c50c1acd486a6f7ecd084c4 (plain) (blame)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include "test.h"
#include "safe_stdio_fn.h"

FILE *safe_fopen(const char *file, const int lineno, void (cleanup_fn)(void),
                 const char *path, const char *mode)
{
	FILE *f = fopen(path, mode);

	if (f == NULL) {
		tst_brkm(TBROK | TERRNO, cleanup_fn,
			 "%s:%d: fopen(%s,%s) failed",
			 file, lineno, path, mode);
	}

	return f;
}

int safe_fclose(const char *file, const int lineno, void (cleanup_fn)(void),
                FILE *f)
{
	int ret;

	ret = fclose(f);

	if (ret) {
		tst_brkm(TBROK | TERRNO, cleanup_fn,
			 "%s:%d: fclose(%p) failed", file, lineno, f);
	}

	return ret;
}

int safe_asprintf(const char *file, const int lineno, void (cleanup_fn)(void),
                  char **strp, const char *fmt, ...)
{
	int ret;
	va_list va;

	va_start(va, fmt);
	ret = vasprintf(strp, fmt, va);
	va_end(va);

	if (ret < 0) {
		tst_brkm(TBROK | TERRNO, cleanup_fn,
			 "%s:%d: asprintf(%s,...) failed", file, lineno, fmt);
	}

	return ret;
}

FILE *safe_popen(const char *file, const int lineno, void (cleanup_fn)(void),
		 const char *command, const char *type)
{
	FILE *stream;
	const int saved_errno = errno;

	errno = 0;
	stream = popen(command, type);

	if (stream == NULL) {
		if (errno != 0) {
			tst_brkm(TBROK | TERRNO, cleanup_fn,
				 "%s:%d: popen(%s,%s) failed",
				 file, lineno, command, type);
		} else {
			tst_brkm(TBROK, cleanup_fn,
				 "%s:%d: popen(%s,%s) failed: Out of memory",
				 file, lineno, command, type);
		}
	}

	errno = saved_errno;

	return stream;
}