4 * Do not use this for inspecting *tracked* content. When path is a
5 * symlink to a directory, we do not want to say it is a directory when
6 * dealing with tracked content in the working tree.
8 int is_directory(const char *path
)
11 return (!stat(path
, &st
) && S_ISDIR(st
.st_mode
));
14 /* We allow "recursive" symbolic links. Only within reason, though. */
18 * Use this to get the real path, i.e. resolve links. If you want an
19 * absolute path but don't mind links, use absolute_path.
21 * If path is our buffer, then return path, as it's already what the
24 const char *real_path(const char *path
)
26 static char bufs
[2][PATH_MAX
+ 1], *buf
= bufs
[0], *next_buf
= bufs
[1];
31 char *last_elem
= NULL
;
34 /* We've already done it */
35 if (path
== buf
|| path
== next_buf
)
39 die("The empty string is not a valid path");
41 if (strlcpy(buf
, path
, PATH_MAX
) >= PATH_MAX
)
42 die ("Too long path: %.*s", 60, path
);
45 if (!is_directory(buf
)) {
46 char *last_slash
= find_last_dir_sep(buf
);
48 last_elem
= xstrdup(last_slash
+ 1);
51 last_elem
= xstrdup(buf
);
57 if (!*cwd
&& !getcwd(cwd
, sizeof(cwd
)))
58 die_errno ("Could not get current working directory");
61 die_errno ("Could not switch to '%s'", buf
);
63 if (!getcwd(buf
, PATH_MAX
))
64 die_errno ("Could not get current working directory");
67 size_t len
= strlen(buf
);
68 if (len
+ strlen(last_elem
) + 2 > PATH_MAX
)
69 die ("Too long path name: '%s/%s'",
71 if (len
&& !is_dir_sep(buf
[len
-1]))
73 strcpy(buf
+ len
, last_elem
);
78 if (!lstat(buf
, &st
) && S_ISLNK(st
.st_mode
)) {
79 ssize_t len
= readlink(buf
, next_buf
, PATH_MAX
);
81 die_errno ("Invalid symlink '%s'", buf
);
83 die("symbolic link too long: %s", buf
);
86 buf_index
= 1 - buf_index
;
87 next_buf
= bufs
[buf_index
];
92 if (*cwd
&& chdir(cwd
))
93 die_errno ("Could not change back to '%s'", cwd
);
98 static const char *get_pwd_cwd(void)
100 static char cwd
[PATH_MAX
+ 1];
102 struct stat cwd_stat
, pwd_stat
;
103 if (getcwd(cwd
, PATH_MAX
) == NULL
)
106 if (pwd
&& strcmp(pwd
, cwd
)) {
107 stat(cwd
, &cwd_stat
);
108 if ((cwd_stat
.st_dev
|| cwd_stat
.st_ino
) &&
109 !stat(pwd
, &pwd_stat
) &&
110 pwd_stat
.st_dev
== cwd_stat
.st_dev
&&
111 pwd_stat
.st_ino
== cwd_stat
.st_ino
) {
112 strlcpy(cwd
, pwd
, PATH_MAX
);
119 * Use this to get an absolute path from a relative one. If you want
120 * to resolve links, you should use real_path.
122 * If the path is already absolute, then return path. As the user is
123 * never meant to free the return value, we're safe.
125 const char *absolute_path(const char *path
)
127 static char buf
[PATH_MAX
+ 1];
130 die("The empty string is not a valid path");
131 } else if (is_absolute_path(path
)) {
132 if (strlcpy(buf
, path
, PATH_MAX
) >= PATH_MAX
)
133 die("Too long path: %.*s", 60, path
);
137 const char *cwd
= get_pwd_cwd();
139 die_errno("Cannot determine the current working directory");
141 fmt
= (len
> 0 && is_dir_sep(cwd
[len
-1])) ? "%s%s" : "%s/%s";
142 if (snprintf(buf
, PATH_MAX
, fmt
, cwd
, path
) >= PATH_MAX
)
143 die("Too long path: %.*s", 60, path
);
149 * Unlike prefix_path, this should be used if the named file does
150 * not have to interact with index entry; i.e. name of a random file
153 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
155 static char path
[PATH_MAX
];
157 if (!pfx_len
|| is_absolute_path(arg
))
159 memcpy(path
, pfx
, pfx_len
);
160 strcpy(path
+ pfx_len
, arg
);
163 /* don't add prefix to absolute paths, but still replace '\' by '/' */
164 if (is_absolute_path(arg
))
167 memcpy(path
, pfx
, pfx_len
);
168 strcpy(path
+ pfx_len
, arg
);
169 for (p
= path
+ pfx_len
; *p
; p
++)