MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / read_file_str

Function read_file_str

src/cli/cli.c:3187–3218  ·  view source on GitHub ↗

Read entire file into malloc'd buffer. Returns NULL on error. */

Source from the content-addressed store, hash-verified

3185
3186/* Read entire file into malloc'd buffer. Returns NULL on error. */
3187static char *read_file_str(const char *path, size_t *out_len) {
3188 FILE *f = fopen(path, "r");
3189 if (!f) {
3190 if (out_len) {
3191 *out_len = 0;
3192 }
3193 return NULL;
3194 }
3195 (void)fseek(f, 0, SEEK_END);
3196 long size = ftell(f);
3197 (void)fseek(f, 0, SEEK_SET);
3198 if (size < 0 || size > (long)CLI_MB_10 * CLI_MB_FACTOR) { /* cap at 10 MB */
3199 (void)fclose(f);
3200 return NULL;
3201 }
3202
3203 char *buf = malloc((size_t)size + CLI_SKIP_ONE);
3204 if (!buf) {
3205 (void)fclose(f);
3206 return NULL;
3207 }
3208 size_t nread = fread(buf, CLI_ELEM_SIZE, (size_t)size, f);
3209 (void)fclose(f);
3210 if (nread > (size_t)size) {
3211 nread = (size_t)size;
3212 }
3213 buf[nread] = '\0';
3214 if (out_len) {
3215 *out_len = nread;
3216 }
3217 return buf;
3218}
3219
3220static int ensure_parent_dir(const char *path) {
3221 if (!path || !path[0]) {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected