| 752 | } cli_fd_capture_t; |
| 753 | |
| 754 | static void cli_fd_capture_begin(cli_fd_capture_t *capture, FILE *stream, int target_fd) { |
| 755 | memset(capture, 0, sizeof(*capture)); |
| 756 | capture->stream = stream; |
| 757 | capture->target_fd = target_fd; |
| 758 | capture->saved_fd = -1; |
| 759 | capture->file = tmpfile(); |
| 760 | if (!capture->file) { |
| 761 | return; |
| 762 | } |
| 763 | capture->saved_fd = dup(target_fd); |
| 764 | if (capture->saved_fd < 0) { |
| 765 | return; |
| 766 | } |
| 767 | fflush(stream); |
| 768 | capture->redirected = dup2(fileno(capture->file), target_fd) >= 0; |
| 769 | } |
| 770 | |
| 771 | /* Returns the captured text (heap, "" when nothing was written) or NULL when |
| 772 | * the capture never engaged. */ |