Skip to content

Commit

Permalink
abspath: use strbuf_getcwd() to remember original working directory
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Aug 26, 2014
1 parent 7333ed1 commit 251277a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static const char *real_path_internal(const char *path, int die_on_error)
* here so that we can chdir() back to it at the end of the
* function:
*/
char cwd[1024] = "";
struct strbuf cwd = STRBUF_INIT;

int buf_index = 1;

Expand Down Expand Up @@ -80,7 +80,7 @@ static const char *real_path_internal(const char *path, int die_on_error)
}

if (*buf) {
if (!*cwd && !getcwd(cwd, sizeof(cwd))) {
if (!cwd.len && strbuf_getcwd(&cwd)) {
if (die_on_error)
die_errno("Could not get current working directory");
else
Expand Down Expand Up @@ -142,8 +142,9 @@ static const char *real_path_internal(const char *path, int die_on_error)
retval = buf;
error_out:
free(last_elem);
if (*cwd && chdir(cwd))
die_errno("Could not change back to '%s'", cwd);
if (cwd.len && chdir(cwd.buf))
die_errno("Could not change back to '%s'", cwd.buf);
strbuf_release(&cwd);

return retval;
}
Expand Down

0 comments on commit 251277a

Please sign in to comment.