MCPcopy Create free account
hub / github.com/ElementsProject/lightning / path_rel

Function path_rel

ccan/ccan/tal/path/path.c:233–312  ·  view source on GitHub ↗

Symlinks make this hard! */

Source from the content-addressed store, hash-verified

231
232/* Symlinks make this hard! */
233char *path_rel(const tal_t *ctx, const char *from, const char *to)
234{
235 char *cfrom, *cto, *ret, *p;
236 tal_t *tmpctx;
237 size_t common, num_back, i, postlen;
238
239 /* This frees from if we're supposed to take it. */
240 tmpctx = cfrom = path_canon(ctx, from);
241 if (!cfrom)
242 goto fail_take_to;
243
244 /* From is a directory, so we append / to it. */
245 if (!streq(cfrom, PATH_SEP_STR)) {
246 if (!tal_resize(&cfrom, strlen(cfrom)+2))
247 goto fail_take_to;
248 tmpctx = cfrom;
249 strcat(cfrom, PATH_SEP_STR);
250 }
251
252 /* This frees to if we're supposed to take it. */
253 cto = path_canon(tmpctx, to);
254 if (!cto) {
255 ret = NULL;
256 goto out;
257 }
258
259 /* How much is in common? */
260 for (common = i = 0; cfrom[i] && cto[i]; i++) {
261 if (cfrom[i] != cto[i])
262 break;
263 if (cfrom[i] == PATH_SEP)
264 common = i + 1;
265 }
266
267 /* Skip over / if matches end of other path. */
268 if (!cfrom[i] && cto[i] == PATH_SEP) {
269 cto++;
270 common = i;
271 } else if (!cto[i] && cfrom[i] == PATH_SEP) {
272 cfrom++;
273 common = i;
274 }
275
276 /* Normalize so strings point past common area. */
277 cfrom += common;
278 cto += common;
279
280 /* One .. for every path element remaining in 'from', to get
281 * back to common prefix. Then the rest of 'to'. */
282 num_back = strcount(cfrom, PATH_SEP_STR);
283 postlen = strlen(cto) + 1;
284
285 /* Nothing left? That's ".". */
286 if (num_back == 0 && postlen == 1) {
287 ret = tal_strdup(ctx, ".");
288 goto out;
289 }
290

Callers 1

mainFunction · 0.85

Calls 4

path_canonFunction · 0.85
strcountFunction · 0.85
tal_freeFunction · 0.85
takenFunction · 0.85

Tested by 1

mainFunction · 0.68