327
return buf;
328
}
329
330
-/* We allow "recursive" symbolic links. Only within reason, though. */
331
-#define MAXDEPTH 5
332
-
330
const char *make_relative_path(const char *abs, const char *base)
331
{
332
static char buf[PATH_MAX + 1];
343
strcpy(buf, abs + baselen);
344
return buf;
345
}
349
-
350
-const char *make_absolute_path(const char *path)
351
-{
352
- static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
353
- char cwd[1024] = "";
354
- int buf_index = 1, len;
355
-
356
- int depth = MAXDEPTH;
357
- char *last_elem = NULL;
358
- struct stat st;
359
-
360
- if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
361
- die ("Too long path: %.*s", 60, path);
362
-
363
- while (depth--) {
364
- if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
365
- char *last_slash = strrchr(buf, '/');
366
- if (last_slash) {
367
- *last_slash = '\0';
368
- last_elem = xstrdup(last_slash + 1);
369
- } else {
370
- last_elem = xstrdup(buf);
371
- *buf = '\0';
372
- }
373
- }
374
-
375
- if (*buf) {
376
- if (!*cwd && !getcwd(cwd, sizeof(cwd)))
377
- die ("Could not get current working directory");
378
-
379
- if (chdir(buf))
380
- die ("Could not switch to '%s'", buf);
381
- }
382
- if (!getcwd(buf, PATH_MAX))
383
- die ("Could not get current working directory");
384
-
385
- if (last_elem) {
386
- int len = strlen(buf);
387
- if (len + strlen(last_elem) + 2 > PATH_MAX)
388
- die ("Too long path name: '%s/%s'",
389
- buf, last_elem);
390
- buf[len] = '/';
391
- strcpy(buf + len + 1, last_elem);
392
- free(last_elem);
393
- last_elem = NULL;
394
- }
395
-
396
- if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
397
- len = readlink(buf, next_buf, PATH_MAX);
398
- if (len < 0)
399
- die ("Invalid symlink: %s", buf);
400
- next_buf[len] = '\0';
401
- buf = next_buf;
402
- buf_index = 1 - buf_index;
403
- next_buf = bufs[buf_index];
404
- } else
405
- break;
406
- }
407
-
408
- if (*cwd && chdir(cwd))
409
- die ("Could not change back to '%s'", cwd);
410
-
411
- return buf;
412
-}