Commit 6a9e2a4d authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

files: handle systems that disallow NULL as first parameter of getcwd

(This change will be made superfluous when we start using gnulib.)

This prevents getcwd() from failing on Android and thus completes the
fix for https://savannah.gnu.org/bugs/index.php?47659

.
Reported-by: default avatarChris Renshaw <osm0sis@outlook.com>
Signed-off-by: default avatarBenno Schulenberg <bensberg@justemail.net>
No related merge requests found
Showing with 12 additions and 8 deletions
+12 -8
......@@ -1412,7 +1412,7 @@ char *get_full_path(const char *origpath)
int attempts = 0;
/* How often we've tried climing back up the tree. */
struct stat fileinfo;
char *d_here, *d_there, *d_there_file = NULL;
char *currentdir, *d_here, *d_there, *d_there_file = NULL;
const char *last_slash;
bool path_only;
......@@ -1422,7 +1422,8 @@ char *get_full_path(const char *origpath)
/* Get the current directory. If it doesn't exist, back up and try
* again until we get a directory that does, and use that as the
* current directory. */
d_here = getcwd(NULL, PATH_MAX + 1);
currentdir = charalloc(PATH_MAX + 1);
d_here = getcwd(currentdir, PATH_MAX + 1);
while (d_here == NULL && attempts < 20) {
IGNORE_CALL_RESULT(chdir(".."));
......@@ -1441,8 +1442,10 @@ char *get_full_path(const char *origpath)
strcat(d_here, "/");
}
/* Otherwise, set d_here to "". */
} else
} else {
d_here = mallocstrcpy(NULL, "");
free(currentdir);
}
d_there = real_dir_from_tilde(origpath);
......@@ -1488,7 +1491,8 @@ char *get_full_path(const char *origpath)
free(d_there);
/* Get the full path. */
d_there = getcwd(NULL, PATH_MAX + 1);
currentdir = charalloc(PATH_MAX + 1);
d_there = getcwd(currentdir, PATH_MAX + 1);
/* If we succeeded, canonicalize it in d_there. */
if (d_there != NULL) {
......@@ -1500,11 +1504,11 @@ char *get_full_path(const char *origpath)
d_there = charealloc(d_there, strlen(d_there) + 2);
strcat(d_there, "/");
}
} else
/* Otherwise, set path_only to TRUE, so that we clean up
* correctly, free all allocated memory, and return
* NULL. */
/* Otherwise, make sure that we return NULL. */
} else {
path_only = TRUE;
free(currentdir);
}
/* Finally, go back to the path specified in d_here,
* where we were before. We don't check for a chdir()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment