diff --git a/src/browser.c b/src/browser.c index 5f84060c..b61bfdd5 100644 --- a/src/browser.c +++ b/src/browser.c @@ -297,7 +297,7 @@ char *do_browser(char *path) /* If we are moving up one level, remember where we came from, so * this directory can be highlighted and easily reentered. */ if (strcmp(tail(filelist[selected]), "..") == 0) - present_name = striponedir(filelist[selected]); + present_name = strip_last_component(filelist[selected]); /* Try opening and reading the selected directory. */ path = mallocstrcpy(path, filelist[selected]); @@ -353,7 +353,7 @@ char *do_browse_from(const char *inpath) * at all. If so, we'll just pass the current directory to * do_browser(). */ if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { - path = free_and_assign(path, striponedir(path)); + path = free_and_assign(path, strip_last_component(path)); if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { char * currentdir = charalloc(PATH_MAX + 1); @@ -781,21 +781,17 @@ void do_last_file(void) selected = filelist_len - 1; } -/* Strip one directory from the end of path, and return the stripped - * path. The returned string is dynamically allocated, and should be - * freed. */ -char *striponedir(const char *path) +/* Strip one element from the end of path, and return the stripped path. + * The returned string is dynamically allocated, and should be freed. */ +char *strip_last_component(const char *path) { - char *retval, *tmp; + char *copy = mallocstrcpy(NULL, path); + char *last_slash = strrchr(copy, '/'); - retval = mallocstrcpy(NULL, path); + if (last_slash != NULL) + *last_slash = '\0'; - tmp = strrchr(retval, '/'); - - if (tmp != NULL) - null_at(&retval, tmp - retval); - - return retval; + return copy; } #endif /* !DISABLE_BROWSER */ diff --git a/src/proto.h b/src/proto.h index 365388e2..3a917a40 100644 --- a/src/proto.h +++ b/src/proto.h @@ -178,7 +178,7 @@ void do_filesearch(void); void do_fileresearch(void); void do_first_file(void); void do_last_file(void); -char *striponedir(const char *path); +char *strip_last_component(const char *path); #endif /* Most functions in chars.c. */