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. */
| 14772 | * tmpfile+dup2+rewind is the file's established portable capture idiom — |
| 14773 | * pread/mkstemp/setenv do not exist on the MinGW leg. */ |
| 14774 | static int cli_config_cmd_capture(int argc, char **argv, char *out, size_t cap) { |
| 14775 | out[0] = '\0'; |
| 14776 | FILE *capture = tmpfile(); |
| 14777 | int saved = capture ? dup(fileno(stdout)) : -1; |
| 14778 | if (!capture || saved < 0) { |
| 14779 | if (capture) |
| 14780 | fclose(capture); |
| 14781 | if (saved >= 0) |
| 14782 | close(saved); |
| 14783 | return -1000; |
| 14784 | } |
| 14785 | fflush(stdout); |
| 14786 | if (dup2(fileno(capture), fileno(stdout)) < 0) { |
| 14787 | fclose(capture); |
| 14788 | close(saved); |
| 14789 | return -1000; |
| 14790 | } |
| 14791 | int rc = cbm_cmd_config(argc, argv); |
| 14792 | fflush(stdout); |
| 14793 | (void)dup2(saved, fileno(stdout)); |
| 14794 | close(saved); |
| 14795 | rewind(capture); |
| 14796 | size_t got = fread(out, 1, cap - 1, capture); |
| 14797 | out[got] = '\0'; |
| 14798 | fclose(capture); |
| 14799 | return rc; |
| 14800 | } |
| 14801 | |
| 14802 | /* The user-facing config contract (#1522 bug 2): `get` of an UNSET key prints |
| 14803 | * that key's real default — the same fallback the runtime readers use — with |
no test coverage detected