| /* libnih |
| * |
| * Copyright © 2009 Scott James Remnant <scott@netsplit.com>. |
| * Copyright © 2009 Canonical Ltd. |
| * |
| * Permission is hereby granted, free of charge, to any person obtaining |
| * a copy of this software and associated documentation files (the |
| * "Software"), to deal in the Software without restriction, including |
| * without limitation the rights to use, copy, modify, merge, publish, |
| * distribute, sublicense, and/or sell copies of the Software, and to |
| * permit persons to whom the Software is furnished to do so, subject to |
| * the following conditions: |
| * |
| * The above copyright notice and this permission notice shall be |
| * included in all copies or substantial portions of the Software. |
| * |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| */ |
| |
| #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 */ |