forked from WHC/Hermit
Vendored sashidown, OpenAI-compatible env (OPENAI_*), and bootstrap scripts. Co-authored-by: Cursor <cursoragent@cursor.com>
62 lines
1.7 KiB
C
62 lines
1.7 KiB
C
#define _DEFAULT_SOURCE
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "cst_agent.h"
|
|
#include "os_compat.h"
|
|
|
|
int cst_sandbox_resolve(const char *workspace_root, const char *rel_or_abs,
|
|
char *out, size_t out_sz) {
|
|
if (!workspace_root || !rel_or_abs || !out || out_sz < 2)
|
|
return -1;
|
|
|
|
char root_real[OS_PATH_MAX];
|
|
if (os_realpath(workspace_root, root_real, sizeof(root_real)) != 0)
|
|
return -1;
|
|
os_path_norm_slashes(root_real);
|
|
size_t root_len = strlen(root_real);
|
|
|
|
char candidate[OS_PATH_MAX];
|
|
if (os_path_is_abs(rel_or_abs)) {
|
|
snprintf(candidate, sizeof(candidate), "%s", rel_or_abs);
|
|
} else if (os_path_join(candidate, sizeof(candidate), root_real, rel_or_abs) !=
|
|
0) {
|
|
return -1;
|
|
}
|
|
os_path_norm_slashes(candidate);
|
|
|
|
char resolved[OS_PATH_MAX];
|
|
if (os_realpath(candidate, resolved, sizeof(resolved)) == 0) {
|
|
os_path_norm_slashes(resolved);
|
|
if (!os_path_under(root_real, resolved))
|
|
return -1;
|
|
if (strlen(resolved) + 1 > out_sz)
|
|
return -1;
|
|
memcpy(out, resolved, strlen(resolved) + 1);
|
|
return 0;
|
|
}
|
|
|
|
/* Parent exists, leaf may be new file. */
|
|
char tmp[OS_PATH_MAX];
|
|
snprintf(tmp, sizeof(tmp), "%s", candidate);
|
|
char *slash = strrchr(tmp, '/');
|
|
if (!slash)
|
|
return -1;
|
|
*slash = 0;
|
|
const char *base = slash + 1;
|
|
char parent_real[OS_PATH_MAX];
|
|
if (os_realpath(tmp, parent_real, sizeof(parent_real)) != 0)
|
|
return -1;
|
|
os_path_norm_slashes(parent_real);
|
|
if (!os_path_under(root_real, parent_real) &&
|
|
strcmp(parent_real, root_real) != 0)
|
|
return -1;
|
|
(void)root_len;
|
|
int n = snprintf(out, out_sz, "%s/%s", parent_real, base);
|
|
if (n < 0 || (size_t)n >= out_sz)
|
|
return -1;
|
|
return 0;
|
|
}
|