File: test_divert.h

package info (click to toggle)
libnih 1.0.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,544 kB
  • sloc: ansic: 188,634; sh: 11,217; makefile: 1,116; yacc: 291; xml: 239; sed: 16
file content (89 lines) | stat: -rw-r--r-- 2,791 bytes parent folder | download | duplicates (6)
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
/* libnih
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will 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 to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef NIH_TEST_DIVERT_H
#define NIH_TEST_DIVERT_H

#ifndef NIH_IN_TEST_H
# error "This header may only be included by <nih/test.h>"
#endif /* NIH_IN_TEST_H */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>

/**
 * TEST_DIVERT_STDOUT_FD:
 * @_fd: fd to send standard output to.
 *
 * This macro diverts standard output to a different file descriptor
 * for the duration of a block of code that should follow it.
 **/
#define TEST_DIVERT_STDOUT_FD(_fd) \
	for (int _test_stdout = 0, _test_oldstdout = dup (STDOUT_FILENO); \
	     _test_stdout < 3; _test_stdout++) \
		if (_test_stdout < 1) { \
			fflush (stdout); \
			assert (dup2 ((_fd), STDOUT_FILENO) >= 0); \
		} else if (_test_stdout > 1) { \
			fflush (stdout); \
			assert (dup2 (_test_oldstdout, STDOUT_FILENO) >= 0); \
			close (_test_oldstdout); \
		} else

/**
 * TEST_DIVERT_STDOUT:
 * @_file: FILE to send standard output to.
 *
 * This macro diverts standard output to a different file for the duration
 * of a block of code that should follow it.
 */
#define TEST_DIVERT_STDOUT(_file) \
	TEST_DIVERT_STDOUT_FD (fileno (_file))

/**
 * TEST_DIVERT_STDERR_FD:
 * @_fd: fd to send standard error to.
 *
 * This macro diverts standard error to a different file descriptor for the
 * duration of a block of code that should follow it.
 */
#define TEST_DIVERT_STDERR_FD(_fd) \
	for (int _test_stderr = 0, _test_oldstderr = dup (STDERR_FILENO); \
	     _test_stderr < 3; _test_stderr++) \
		if (_test_stderr < 1) { \
			fflush (stderr); \
			assert (dup2 ((_fd), STDERR_FILENO) >= 0); \
		} else if (_test_stderr > 1) { \
			fflush (stderr); \
			assert (dup2 (_test_oldstderr, STDERR_FILENO) >= 0); \
			close (_test_oldstderr); \
		} else

/**
 * TEST_DIVERT_STDERR:
 * @_file: FILE to send standard error to.
 *
 * This macro diverts standard error to a different file for the duration
 * of a block of code that should follow it.
 */
#define TEST_DIVERT_STDERR(_file) \
	TEST_DIVERT_STDERR_FD (fileno (_file))

#endif /* NIH_TEST_DIVERT_H */