Write string to file, creating parent dirs if needed. */
| 1313 | |
| 1314 | /* Write string to file, creating parent dirs if needed. */ |
| 1315 | static int write_file_str(const char *path, const char *content) { |
| 1316 | /* Ensure parent directory */ |
| 1317 | char dir[CLI_BUF_1K]; |
| 1318 | snprintf(dir, sizeof(dir), "%s", path); |
| 1319 | char *last_slash = strrchr(dir, '/'); |
| 1320 | if (last_slash) { |
| 1321 | *last_slash = '\0'; |
| 1322 | mkdirp(dir, DIR_PERMS); |
| 1323 | } |
| 1324 | |
| 1325 | FILE *f = fopen(path, "w"); |
| 1326 | if (!f) { |
| 1327 | return CLI_ERR; |
| 1328 | } |
| 1329 | size_t len = strlen(content); |
| 1330 | size_t written = fwrite(content, CLI_ELEM_SIZE, len, f); |
| 1331 | (void)fclose(f); |
| 1332 | return written == len ? 0 : CLI_ERR; |
| 1333 | } |
| 1334 | |
| 1335 | int cbm_upsert_instructions(const char *path, const char *content) { |
| 1336 | if (!path || !content) { |
no test coverage detected