real_path: do not assume '/' is the path seperator

real_path currently assumes it's input had '/' as path seperator. This assumption does not hold true for the code-path from prefix_path (on Windows), where real_path can be called before normalize_path_copy. Fix real_path so it doesn't make this assumption. Create a helper function to reverse-search for the last path-seperator in a string. Signed-off-by: Theo Niessink <theo@taletn.com> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Theo Niessink committed May 27, 2011 at 18:00 UTC d1c69255a1014ccaeb9841f2114e20f048556391
3 files changed +15 -2
abspath.c
+2 -2
@@ -33,7 +33,7 @@ const char *make_absolute_path(const char *path)
33
34 while (depth--) {
35 if (!is_directory(buf)) {
36 - char *last_slash = strrchr(buf, '/');
36 + char *last_slash = find_last_dir_sep(buf);
37 if (last_slash) {
38 *last_slash = '\0';
39 last_elem = xstrdup(last_slash + 1);
@@ -58,7 +58,7 @@ const char *make_absolute_path(const char *path)
58 if (len + strlen(last_elem) + 2 > PATH_MAX)
59 die ("Too long path name: '%s/%s'",
60 buf, last_elem);
61 - if (len && buf[len-1] != '/')
61 + if (len && !is_dir_sep(buf[len-1]))
62 buf[len++] = '/';
63 strcpy(buf + len, last_elem);
64 free(last_elem);
compat/mingw.h
+9
@@ -299,6 +299,15 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format
299
300 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
301 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
302 +static inline char *mingw_find_last_dir_sep(const char *path)
303 +{
304 + char *ret = NULL;
305 + for (; *path; ++path)
306 + if (is_dir_sep(*path))
307 + ret = (char *)path;
308 + return ret;
309 +}
310 +#define find_last_dir_sep mingw_find_last_dir_sep
311 #define PATH_SEP ';'
312 #define PRIuMAX "I64u"
313
git-compat-util.h
+4
@@ -215,6 +215,10 @@ extern char *gitbasename(char *);
215 #define is_dir_sep(c) ((c) == '/')
216 #endif
217
218 +#ifndef find_last_dir_sep
219 +#define find_last_dir_sep(path) strrchr(path, '/')
220 +#endif
221 +
222 #if __HP_cc >= 61000
223 #define NORETURN __attribute__((noreturn))
224 #define NORETURN_PTR