tweaks: rename and shorten a small helper function

master
Benno Schulenberg 2017-03-20 12:24:42 +01:00
parent 6d873d3760
commit f48e15f2a3
2 changed files with 11 additions and 15 deletions

View File

@ -297,7 +297,7 @@ char *do_browser(char *path)
/* If we are moving up one level, remember where we came from, so /* If we are moving up one level, remember where we came from, so
* this directory can be highlighted and easily reentered. */ * this directory can be highlighted and easily reentered. */
if (strcmp(tail(filelist[selected]), "..") == 0) 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. */ /* Try opening and reading the selected directory. */
path = mallocstrcpy(path, filelist[selected]); 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 * at all. If so, we'll just pass the current directory to
* do_browser(). */ * do_browser(). */
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { 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)) { if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
char * currentdir = charalloc(PATH_MAX + 1); char * currentdir = charalloc(PATH_MAX + 1);
@ -781,21 +781,17 @@ void do_last_file(void)
selected = filelist_len - 1; selected = filelist_len - 1;
} }
/* Strip one directory from the end of path, and return the stripped /* Strip one element from the end of path, and return the stripped path.
* path. The returned string is dynamically allocated, and should be * The returned string is dynamically allocated, and should be freed. */
* freed. */ char *strip_last_component(const char *path)
char *striponedir(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, '/'); return copy;
if (tmp != NULL)
null_at(&retval, tmp - retval);
return retval;
} }
#endif /* !DISABLE_BROWSER */ #endif /* !DISABLE_BROWSER */

View File

@ -178,7 +178,7 @@ void do_filesearch(void);
void do_fileresearch(void); void do_fileresearch(void);
void do_first_file(void); void do_first_file(void);
void do_last_file(void); void do_last_file(void);
char *striponedir(const char *path); char *strip_last_component(const char *path);
#endif #endif
/* Most functions in chars.c. */ /* Most functions in chars.c. */