Read an open stream fully into a heap, NUL-terminated string. Caller frees. * Returns NULL on allocation failure. Reads binary-clean (UTF-8 JSON, no shell * quoting needed). */
| 783 | * Returns NULL on allocation failure. Reads binary-clean (UTF-8 JSON, no shell |
| 784 | * quoting needed). */ |
| 785 | static char *cli_slurp_stream(FILE *f) { |
| 786 | size_t cap = CLI_SLURP_CHUNK; |
| 787 | size_t len = 0; |
| 788 | char *buf = malloc(cap); |
| 789 | if (!buf) { |
| 790 | return NULL; |
| 791 | } |
| 792 | char tmp[CLI_SLURP_CHUNK]; |
| 793 | size_t n; |
| 794 | while ((n = fread(tmp, 1, sizeof(tmp), f)) > 0) { |
| 795 | if (len + n + 1 > cap) { |
| 796 | while (len + n + 1 > cap) { |
| 797 | cap *= 2; |
| 798 | } |
| 799 | char *nb = realloc(buf, cap); |
| 800 | if (!nb) { |
| 801 | free(buf); |
| 802 | return NULL; |
| 803 | } |
| 804 | buf = nb; |
| 805 | } |
| 806 | memcpy(buf + len, tmp, n); |
| 807 | len += n; |
| 808 | } |
| 809 | buf[len] = '\0'; |
| 810 | return buf; |
| 811 | } |
| 812 | |
| 813 | /* Slurp a file path into a heap, NUL-terminated string. Caller frees. */ |
| 814 | static char *cli_slurp_file(const char *path) { |
no test coverage detected