abspath: use strbuf_getcwd() to remember original working directory
Store the original working directory in a strbuf instead of in a fixed-sized buffer, in order to be able to handle longer paths. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 28, 2014 at 20:27 UTC
251277acdf8c8dee59bdd0e9e7b7e3502226cf9d
1 file changed
+5
-4
abspath.c
+5
-4
@@ -41,7 +41,7 @@ static const char *real_path_internal(const char *path, int die_on_error)
41
* here so that we can chdir() back to it at the end of the
42
* function:
43
*/
44
- char cwd[1024] = "";
44
+ struct strbuf cwd = STRBUF_INIT;
45
46
int buf_index = 1;
47
@@ -80,7 +80,7 @@ static const char *real_path_internal(const char *path, int die_on_error)
80
}
81
82
if (*buf) {
83
- if (!*cwd && !getcwd(cwd, sizeof(cwd))) {
83
+ if (!cwd.len && strbuf_getcwd(&cwd)) {
84
if (die_on_error)
85
die_errno("Could not get current working directory");
86
else
@@ -142,8 +142,9 @@ static const char *real_path_internal(const char *path, int die_on_error)
142
retval = buf;
143
error_out:
144
free(last_elem);
145
- if (*cwd && chdir(cwd))
146
- die_errno("Could not change back to '%s'", cwd);
145
+ if (cwd.len && chdir(cwd.buf))
146
+ die_errno("Could not change back to '%s'", cwd.buf);
147
+ strbuf_release(&cwd);
148
149
return retval;
150
}