abspath.c 883 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "cache.h"
  2. static const char *get_pwd_cwd(void)
  3. {
  4. static char cwd[PATH_MAX + 1];
  5. char *pwd;
  6. struct stat cwd_stat, pwd_stat;
  7. if (getcwd(cwd, PATH_MAX) == NULL)
  8. return NULL;
  9. pwd = getenv("PWD");
  10. if (pwd && strcmp(pwd, cwd)) {
  11. stat(cwd, &cwd_stat);
  12. if (!stat(pwd, &pwd_stat) &&
  13. pwd_stat.st_dev == cwd_stat.st_dev &&
  14. pwd_stat.st_ino == cwd_stat.st_ino) {
  15. strlcpy(cwd, pwd, PATH_MAX);
  16. }
  17. }
  18. return cwd;
  19. }
  20. const char *make_nonrelative_path(const char *path)
  21. {
  22. static char buf[PATH_MAX + 1];
  23. if (is_absolute_path(path)) {
  24. if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
  25. die("Too long path: %.*s", 60, path);
  26. } else {
  27. const char *cwd = get_pwd_cwd();
  28. if (!cwd)
  29. die("Cannot determine the current working directory");
  30. if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
  31. die("Too long path: %.*s", 60, path);
  32. }
  33. return buf;
  34. }