| #include <assert.h> |
| #include <fcntl.h> |
| #include <io.h> |
| #include <process.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| |
| /* mingw-w64 headers */ |
| #include "libtest.h" |
| |
| int main(int argc, char *argv[]) { |
| mingw_test_init (); |
| |
| if (argc != 2 || strcmp(argv[1], "assert_test") != 0) { |
| int exit_code; |
| int pipefd[2]; |
| int back_errfd; |
| intptr_t process; |
| ssize_t size; |
| char buf[512]; |
| |
| assert(_pipe(pipefd, 0, O_NOINHERIT) == 0); |
| |
| /* set stderr fd to write side of pipe, will be used by _spawnl() */ |
| assert((back_errfd = dup(STDERR_FILENO)) >= 0); |
| assert(dup2(pipefd[1], STDERR_FILENO) == 0); |
| assert(close(pipefd[1]) == 0); |
| |
| process = _spawnl(_P_NOWAIT, _pgmptr, argv[0], "assert_test", NULL); |
| |
| /* revert back stderr fd */ |
| assert(dup2(back_errfd, STDERR_FILENO) == 0); |
| assert(close(back_errfd) == 0); |
| |
| assert(process != -1); |
| |
| size = read(pipefd[0], buf, sizeof(buf)); |
| assert(size > 0); /* some data were written by child process */ |
| assert(strnlen(buf, sizeof(buf)) > 0); |
| |
| /* wait until child process exits */ |
| assert(_cwait(&exit_code, process, _WAIT_CHILD) == process); |
| assert(exit_code != 0); |
| |
| /* read the rest of data in the pipe and write it to stdout */ |
| if (size < (ssize_t)sizeof(buf)) { |
| size += read(pipefd[0], &buf[size], sizeof(buf) - size); |
| } |
| close(pipefd[0]); |
| write(STDOUT_FILENO, buf, size); |
| |
| return 0; |
| } |
| |
| /* call assert, it prints to stderr, parent process will check that our stderr is not empty */ |
| assert(0); |
| |
| /* assert(0) does not return, this process pass when returns non-zero */ |
| return 0; |
| } |