files: do not use two variables for two different purposes each

Use 'slash' to point at a possible slash, use 'filename' just to
point at the real file name, and use 'wasdirname' just to point at
the dir's name before expanding it in order to be able to free it.

Also, remove two superfluous asserts: 'dirname' cannot be NULL
because it has just been mallocstrcpy'd, and checking 'num_matches'
is pointless as it would crash on the next statement anyway.
master
Benno Schulenberg 2016-04-23 14:48:34 +02:00
parent 2815eaa5f8
commit 553b7af355
1 changed files with 9 additions and 11 deletions

View File

@ -2667,27 +2667,25 @@ char **username_tab_completion(const char *buf, size_t *num_matches,
char **cwd_tab_completion(const char *buf, bool allow_files, size_t char **cwd_tab_completion(const char *buf, bool allow_files, size_t
*num_matches, size_t buf_len) *num_matches, size_t buf_len)
{ {
char *dirname = mallocstrcpy(NULL, buf), *filename; char *dirname = mallocstrcpy(NULL, buf);
char *slash, *filename;
size_t filenamelen; size_t filenamelen;
char **matches = NULL; char **matches = NULL;
DIR *dir; DIR *dir;
const struct dirent *nextdir; const struct dirent *nextdir;
assert(dirname != NULL && num_matches != NULL);
*num_matches = 0; *num_matches = 0;
null_at(&dirname, buf_len); null_at(&dirname, buf_len);
/* Okie, if there's a / in the buffer, strip out the directory part. */ /* If there's a / in the name, strip out the directory part. */
filename = strrchr(dirname, '/'); slash = strrchr(dirname, '/');
if (filename != NULL) { if (slash != NULL) {
char *tmpdirname = filename + 1; char *wasdirname = dirname;
filename = mallocstrcpy(NULL, tmpdirname); filename = mallocstrcpy(NULL, slash + 1);
*tmpdirname = '\0'; *slash = '\0';
tmpdirname = dirname;
dirname = real_dir_from_tilde(dirname); dirname = real_dir_from_tilde(dirname);
free(tmpdirname); free(wasdirname);
} else { } else {
filename = dirname; filename = dirname;
dirname = mallocstrcpy(NULL, "./"); dirname = mallocstrcpy(NULL, "./");