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