commit f48e15f2a3d23201a0d59925e7b0441876b9cce6
parent 6d873d376040628040220efd3e66be31fed88e7f
Author: Benno Schulenberg <bensberg@justemail.net>
Date: Mon, 20 Mar 2017 12:24:42 +0100
tweaks: rename and shorten a small helper function
Diffstat:
2 files changed, 11 insertions(+), 15 deletions(-)
diff --git 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
@@ -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. */