in get_full_path(), don't return NULL when the current directory doesn't
exist, as we can still recover from that, and also add various cleanups git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3752 35c25a1d-7b9e-4130-9fde-d3aeb78583b8master
parent
51b7b0e7d8
commit
85e35e67a2
|
@ -55,6 +55,11 @@ CVS code -
|
||||||
allocated, use null_at() to strip the directory from the
|
allocated, use null_at() to strip the directory from the
|
||||||
string. Also, return the stripped path instead of modifying
|
string. Also, return the stripped path instead of modifying
|
||||||
path. (DLR)
|
path. (DLR)
|
||||||
|
- files.c:
|
||||||
|
get_full_path()
|
||||||
|
- Don't return NULL when the current directory doesn't exist, as
|
||||||
|
we can still recover from that. (DLR, found by Mike Frysinger)
|
||||||
|
- Add various cleanups. (DLR)
|
||||||
- help.c:
|
- help.c:
|
||||||
do_help()
|
do_help()
|
||||||
- Simplify screen update handling and exiting. (DLR)
|
- Simplify screen update handling and exiting. (DLR)
|
||||||
|
|
69
src/files.c
69
src/files.c
|
@ -902,21 +902,28 @@ void do_insertfile_void(void)
|
||||||
* able to go there. */
|
* able to go there. */
|
||||||
char *get_full_path(const char *origpath)
|
char *get_full_path(const char *origpath)
|
||||||
{
|
{
|
||||||
char *d_here, *d_there = NULL;
|
struct stat fileinfo;
|
||||||
|
char *d_here, *d_there, *d_there_file = NULL;
|
||||||
|
const char *last_slash;
|
||||||
|
bool path_only;
|
||||||
|
|
||||||
if (origpath == NULL)
|
if (origpath == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* Get the current directory. */
|
/* Get the current directory. If it doesn't exist, back up and try
|
||||||
|
* again until we get a directory that does exist. */
|
||||||
d_here = charalloc(PATH_MAX + 1);
|
d_here = charalloc(PATH_MAX + 1);
|
||||||
d_here = getcwd(d_here, PATH_MAX + 1);
|
d_here = getcwd(d_here, PATH_MAX + 1);
|
||||||
|
|
||||||
if (d_here != NULL) {
|
while (d_here == NULL) {
|
||||||
const char *last_slash;
|
if (chdir("..") == -1)
|
||||||
char *d_there_file = NULL;
|
break;
|
||||||
bool path_only;
|
|
||||||
struct stat fileinfo;
|
|
||||||
|
|
||||||
|
d_here = getcwd(d_here, PATH_MAX + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we succeeded, canonicalize it in d_here. */
|
||||||
|
if (d_here != NULL) {
|
||||||
align(&d_here);
|
align(&d_here);
|
||||||
|
|
||||||
/* If the current directory isn't "/", tack a slash onto the end
|
/* If the current directory isn't "/", tack a slash onto the end
|
||||||
|
@ -925,16 +932,18 @@ char *get_full_path(const char *origpath)
|
||||||
d_here = charealloc(d_here, strlen(d_here) + 2);
|
d_here = charealloc(d_here, strlen(d_here) + 2);
|
||||||
strcat(d_here, "/");
|
strcat(d_here, "/");
|
||||||
}
|
}
|
||||||
|
/* Otherwise, set d_here to "". */
|
||||||
|
} else
|
||||||
|
d_here = mallocstrcpy(NULL, "");
|
||||||
|
|
||||||
d_there = real_dir_from_tilde(origpath);
|
d_there = real_dir_from_tilde(origpath);
|
||||||
|
|
||||||
assert(d_there != NULL);
|
assert(d_there != NULL);
|
||||||
|
|
||||||
/* Stat d_there. If stat() fails, assume that d_there refers to
|
/* If stat()ing d_there fails, assume that d_there refers to a new
|
||||||
* a new file that hasn't been saved to disk yet. Set path_only
|
* file that hasn't been saved to disk yet. Set path_only to TRUE
|
||||||
* to TRUE if d_there refers to a directory, and FALSE if
|
* if d_there refers to a directory, and FALSE otherwise. */
|
||||||
* d_there refers to a file. */
|
path_only = stat(d_there, &fileinfo) == 0 &&
|
||||||
path_only = !stat(d_there, &fileinfo) &&
|
|
||||||
S_ISDIR(fileinfo.st_mode);
|
S_ISDIR(fileinfo.st_mode);
|
||||||
|
|
||||||
/* If path_only is TRUE, make sure d_there ends in a slash. */
|
/* If path_only is TRUE, make sure d_there ends in a slash. */
|
||||||
|
@ -950,17 +959,16 @@ char *get_full_path(const char *origpath)
|
||||||
/* Search for the last slash in d_there. */
|
/* Search for the last slash in d_there. */
|
||||||
last_slash = strrchr(d_there, '/');
|
last_slash = strrchr(d_there, '/');
|
||||||
|
|
||||||
/* If we didn't find one, then make sure the answer is in the
|
/* If we didn't find one, then make sure the answer is in the format
|
||||||
* format "d_here/d_there". */
|
* "d_here/d_there". */
|
||||||
if (last_slash == NULL) {
|
if (last_slash == NULL) {
|
||||||
assert(!path_only);
|
assert(!path_only);
|
||||||
|
|
||||||
d_there_file = d_there;
|
d_there_file = d_there;
|
||||||
d_there = d_here;
|
d_there = d_here;
|
||||||
} else {
|
} else {
|
||||||
/* If path_only is FALSE, then save the filename portion of
|
/* If path_only is FALSE, then save the filename portion of the
|
||||||
* the answer, everything after the last slash, in
|
* answer (everything after the last slash) in d_there_file. */
|
||||||
* d_there_file. */
|
|
||||||
if (!path_only)
|
if (!path_only)
|
||||||
d_there_file = mallocstrcpy(NULL, last_slash + 1);
|
d_there_file = mallocstrcpy(NULL, last_slash + 1);
|
||||||
|
|
||||||
|
@ -968,46 +976,47 @@ char *get_full_path(const char *origpath)
|
||||||
* d_there. */
|
* d_there. */
|
||||||
null_at(&d_there, last_slash - d_there + 1);
|
null_at(&d_there, last_slash - d_there + 1);
|
||||||
|
|
||||||
/* Go to the path specified in d_there. */
|
/* chdir() to the path specified in d_there. */
|
||||||
if (chdir(d_there) == -1) {
|
if (chdir(d_there) == -1) {
|
||||||
free(d_there);
|
free(d_there);
|
||||||
d_there = NULL;
|
d_there = NULL;
|
||||||
} else {
|
} else {
|
||||||
/* Get the full path and save it in d_there. */
|
|
||||||
free(d_there);
|
free(d_there);
|
||||||
|
|
||||||
|
/* Get the full path. */
|
||||||
d_there = charalloc(PATH_MAX + 1);
|
d_there = charalloc(PATH_MAX + 1);
|
||||||
d_there = getcwd(d_there, PATH_MAX + 1);
|
d_there = getcwd(d_there, PATH_MAX + 1);
|
||||||
|
|
||||||
|
/* If we succeeded, canonicalize it in d_there. */
|
||||||
if (d_there != NULL) {
|
if (d_there != NULL) {
|
||||||
align(&d_there);
|
align(&d_there);
|
||||||
|
|
||||||
|
/* If the current directory isn't "/", tack a slash onto
|
||||||
|
* the end of it. */
|
||||||
if (strcmp(d_there, "/") != 0) {
|
if (strcmp(d_there, "/") != 0) {
|
||||||
/* Make sure d_there ends in a slash. */
|
d_there = charealloc(d_there, strlen(d_there) + 2);
|
||||||
d_there = charealloc(d_there,
|
|
||||||
strlen(d_there) + 2);
|
|
||||||
strcat(d_there, "/");
|
strcat(d_there, "/");
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
/* If we couldn't get the full path, set path_only
|
/* Otherwise, set path_only to TRUE, so that we clean up
|
||||||
* to TRUE so that we clean up correctly, free all
|
* correctly, free all allocated memory, and return
|
||||||
* allocated memory, and return NULL. */
|
* NULL. */
|
||||||
path_only = TRUE;
|
path_only = TRUE;
|
||||||
|
|
||||||
/* Finally, go back to the path specified in d_here,
|
/* Finally, go back to the path specified in d_here,
|
||||||
* where we were before. */
|
* where we were before. We don't check for a chdir()
|
||||||
|
* error, since we can do nothing then. */
|
||||||
chdir(d_here);
|
chdir(d_here);
|
||||||
}
|
|
||||||
|
|
||||||
/* Free d_here, since we're done using it. */
|
/* Free d_here, since we're done using it. */
|
||||||
free(d_here);
|
free(d_here);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* At this point, if path_only is FALSE and d_there exists,
|
/* At this point, if path_only is FALSE and d_there isn't NULL,
|
||||||
* d_there contains the path portion of the answer and
|
* d_there contains the path portion of the answer and
|
||||||
* d_there_file contains the filename portion of the answer. If
|
* d_there_file contains the filename portion of the answer. If
|
||||||
* this is the case, tack d_there_file onto the end of
|
* this is the case, tack the latter onto the end of the former.
|
||||||
* d_there, so that d_there contains the complete answer. */
|
* d_there will then contain the complete answer. */
|
||||||
if (!path_only && d_there != NULL) {
|
if (!path_only && d_there != NULL) {
|
||||||
d_there = charealloc(d_there, strlen(d_there) +
|
d_there = charealloc(d_there, strlen(d_there) +
|
||||||
strlen(d_there_file) + 1);
|
strlen(d_there_file) + 1);
|
||||||
|
|
Loading…
Reference in New Issue