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. */
| 11771 | * tmpfile+dup2+rewind is the file's established portable capture idiom — |
| 11772 | * pread/mkstemp/setenv do not exist on the MinGW leg. */ |
| 11773 | static int cli_config_cmd_capture(int argc, char **argv, char *out, size_t cap) { |
| 11774 | out[0] = '\0'; |
| 11775 | FILE *capture = tmpfile(); |
| 11776 | int saved = capture ? dup(fileno(stdout)) : -1; |
| 11777 | if (!capture || saved < 0) { |
| 11778 | if (capture) |
| 11779 | fclose(capture); |
| 11780 | if (saved >= 0) |
| 11781 | close(saved); |
| 11782 | return -1000; |
| 11783 | } |
| 11784 | fflush(stdout); |
| 11785 | if (dup2(fileno(capture), fileno(stdout)) < 0) { |
| 11786 | fclose(capture); |
| 11787 | close(saved); |
| 11788 | return -1000; |
| 11789 | } |
| 11790 | int rc = cbm_cmd_config(argc, argv); |
| 11791 | fflush(stdout); |
| 11792 | (void)dup2(saved, fileno(stdout)); |
| 11793 | close(saved); |
| 11794 | rewind(capture); |
| 11795 | size_t got = fread(out, 1, cap - 1, capture); |
| 11796 | out[got] = '\0'; |
| 11797 | fclose(capture); |
| 11798 | return rc; |
| 11799 | } |
| 11800 | |
| 11801 | /* The user-facing config contract (#1522 bug 2): `get` of an UNSET key prints |
| 11802 | * that key's real default — the same fallback the runtime readers use — with |
no test coverage detected