Capture stdout across one cbm_cmd_config invocation. Returns the command's * rc and copies captured stdout (NUL-terminated, truncated to cap) out. * tmpfile+dup2+rewind is the file's established portable capture idiom — * pread/mkstemp/setenv do not exist on the MinGW leg. */
| 11656 | * tmpfile+dup2+rewind is the file's established portable capture idiom — |
| 11657 | * pread/mkstemp/setenv do not exist on the MinGW leg. */ |
| 11658 | static int cli_config_cmd_capture(int argc, char **argv, char *out, size_t cap) { |
| 11659 | out[0] = '\0'; |
| 11660 | FILE *capture = tmpfile(); |
| 11661 | int saved = capture ? dup(fileno(stdout)) : -1; |
| 11662 | if (!capture || saved < 0) { |
| 11663 | if (capture) |
| 11664 | fclose(capture); |
| 11665 | if (saved >= 0) |
| 11666 | close(saved); |
| 11667 | return -1000; |
| 11668 | } |
| 11669 | fflush(stdout); |
| 11670 | if (dup2(fileno(capture), fileno(stdout)) < 0) { |
| 11671 | fclose(capture); |
| 11672 | close(saved); |
| 11673 | return -1000; |
| 11674 | } |
| 11675 | int rc = cbm_cmd_config(argc, argv); |
| 11676 | fflush(stdout); |
| 11677 | (void)dup2(saved, fileno(stdout)); |
| 11678 | close(saved); |
| 11679 | rewind(capture); |
| 11680 | size_t got = fread(out, 1, cap - 1, capture); |
| 11681 | out[got] = '\0'; |
| 11682 | fclose(capture); |
| 11683 | return rc; |
| 11684 | } |
| 11685 | |
| 11686 | /* The user-facing config contract (#1522 bug 2): `get` of an UNSET key prints |
| 11687 | * that key's real default — the same fallback the runtime readers use — with |
no test coverage detected