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. */
| 11465 | * tmpfile+dup2+rewind is the file's established portable capture idiom — |
| 11466 | * pread/mkstemp/setenv do not exist on the MinGW leg. */ |
| 11467 | static int cli_config_cmd_capture(int argc, char **argv, char *out, size_t cap) { |
| 11468 | out[0] = '\0'; |
| 11469 | FILE *capture = tmpfile(); |
| 11470 | int saved = capture ? dup(fileno(stdout)) : -1; |
| 11471 | if (!capture || saved < 0) { |
| 11472 | if (capture) |
| 11473 | fclose(capture); |
| 11474 | if (saved >= 0) |
| 11475 | close(saved); |
| 11476 | return -1000; |
| 11477 | } |
| 11478 | fflush(stdout); |
| 11479 | if (dup2(fileno(capture), fileno(stdout)) < 0) { |
| 11480 | fclose(capture); |
| 11481 | close(saved); |
| 11482 | return -1000; |
| 11483 | } |
| 11484 | int rc = cbm_cmd_config(argc, argv); |
| 11485 | fflush(stdout); |
| 11486 | (void)dup2(saved, fileno(stdout)); |
| 11487 | close(saved); |
| 11488 | rewind(capture); |
| 11489 | size_t got = fread(out, 1, cap - 1, capture); |
| 11490 | out[got] = '\0'; |
| 11491 | fclose(capture); |
| 11492 | return rc; |
| 11493 | } |
| 11494 | |
| 11495 | /* The user-facing config contract (#1522 bug 2): `get` of an UNSET key prints |
| 11496 | * that key's real default — the same fallback the runtime readers use — with |
no test coverage detected