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. */
| 16611 | * tmpfile+dup2+rewind is the file's established portable capture idiom — |
| 16612 | * pread/mkstemp/setenv do not exist on the MinGW leg. */ |
| 16613 | static int cli_config_cmd_capture(int argc, char **argv, char *out, size_t cap) { |
| 16614 | out[0] = '\0'; |
| 16615 | FILE *capture = tmpfile(); |
| 16616 | int saved = capture ? dup(fileno(stdout)) : -1; |
| 16617 | if (!capture || saved < 0) { |
| 16618 | if (capture) |
| 16619 | fclose(capture); |
| 16620 | if (saved >= 0) |
| 16621 | close(saved); |
| 16622 | return -1000; |
| 16623 | } |
| 16624 | fflush(stdout); |
| 16625 | if (dup2(fileno(capture), fileno(stdout)) < 0) { |
| 16626 | fclose(capture); |
| 16627 | close(saved); |
| 16628 | return -1000; |
| 16629 | } |
| 16630 | int rc = cbm_cmd_config(argc, argv); |
| 16631 | fflush(stdout); |
| 16632 | (void)dup2(saved, fileno(stdout)); |
| 16633 | close(saved); |
| 16634 | rewind(capture); |
| 16635 | size_t got = fread(out, 1, cap - 1, capture); |
| 16636 | out[got] = '\0'; |
| 16637 | fclose(capture); |
| 16638 | return rc; |
| 16639 | } |
| 16640 | |
| 16641 | /* The user-facing config contract (#1522 bug 2): `get` of an UNSET key prints |
| 16642 | * that key's real default — the same fallback the runtime readers use — with |
no test coverage detected