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. */
| 11990 | * tmpfile+dup2+rewind is the file's established portable capture idiom — |
| 11991 | * pread/mkstemp/setenv do not exist on the MinGW leg. */ |
| 11992 | static int cli_config_cmd_capture(int argc, char **argv, char *out, size_t cap) { |
| 11993 | out[0] = '\0'; |
| 11994 | FILE *capture = tmpfile(); |
| 11995 | int saved = capture ? dup(fileno(stdout)) : -1; |
| 11996 | if (!capture || saved < 0) { |
| 11997 | if (capture) |
| 11998 | fclose(capture); |
| 11999 | if (saved >= 0) |
| 12000 | close(saved); |
| 12001 | return -1000; |
| 12002 | } |
| 12003 | fflush(stdout); |
| 12004 | if (dup2(fileno(capture), fileno(stdout)) < 0) { |
| 12005 | fclose(capture); |
| 12006 | close(saved); |
| 12007 | return -1000; |
| 12008 | } |
| 12009 | int rc = cbm_cmd_config(argc, argv); |
| 12010 | fflush(stdout); |
| 12011 | (void)dup2(saved, fileno(stdout)); |
| 12012 | close(saved); |
| 12013 | rewind(capture); |
| 12014 | size_t got = fread(out, 1, cap - 1, capture); |
| 12015 | out[got] = '\0'; |
| 12016 | fclose(capture); |
| 12017 | return rc; |
| 12018 | } |
| 12019 | |
| 12020 | /* The user-facing config contract (#1522 bug 2): `get` of an UNSET key prints |
| 12021 | * that key's real default — the same fallback the runtime readers use — with |
no test coverage detected