`rel` (a relative path: ./ and ../ allowed) inside repository directory * `base` ("" = the root), normalized; NULL when it leaves the repository. */
| 275 | /* `rel` (a relative path: ./ and ../ allowed) inside repository directory |
| 276 | * `base` ("" = the root), normalized; NULL when it leaves the repository. */ |
| 277 | static char *sh_join(CBMArena *a, const char *base, const char *rel) { |
| 278 | size_t bl = strlen(base); |
| 279 | char *out = cbm_arena_alloc(a, bl + strlen(rel) + PAIR_LEN); |
| 280 | if (!out) { |
| 281 | return NULL; |
| 282 | } |
| 283 | memcpy(out, base, bl); |
| 284 | size_t n = bl; |
| 285 | out[n] = '\0'; |
| 286 | for (const char *p = rel; *p;) { |
| 287 | while (*p == '/') { |
| 288 | p++; |
| 289 | } |
| 290 | size_t sl = strcspn(p, "/"); |
| 291 | if (sl == 0) { |
| 292 | break; |
| 293 | } |
| 294 | if (sl == SKIP_ONE && p[0] == '.') { |
| 295 | p += sl; |
| 296 | continue; |
| 297 | } |
| 298 | if (sl == PAIR_LEN && p[0] == '.' && p[SKIP_ONE] == '.') { |
| 299 | if (n == 0) { |
| 300 | return NULL; |
| 301 | } |
| 302 | while (n > 0 && out[n - SKIP_ONE] != '/') { |
| 303 | n--; |
| 304 | } |
| 305 | if (n > 0) { |
| 306 | n--; |
| 307 | } |
| 308 | out[n] = '\0'; |
| 309 | p += sl; |
| 310 | continue; |
| 311 | } |
| 312 | if (n) { |
| 313 | out[n++] = '/'; |
| 314 | } |
| 315 | memcpy(out + n, p, sl); |
| 316 | n += sl; |
| 317 | out[n] = '\0'; |
| 318 | p += sl; |
| 319 | } |
| 320 | return out; |
| 321 | } |
| 322 | |
| 323 | static const char *sh_anchor(CBMArena *a, const char *repo_path) { |
| 324 | return repo_path ? cbm_arena_sprintf(a, "%c%s", SH_ANCHOR, repo_path) : NULL; |
no test coverage detected