From 553b7af355ab9fd313f79c11ebd2e4227e184469 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sat, 23 Apr 2016 14:48:34 +0200 Subject: [PATCH] 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. --- src/files.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/files.c b/src/files.c index c05501c4..2b585116 100644 --- a/src/files.c +++ b/src/files.c @@ -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 *num_matches, size_t buf_len) { - char *dirname = mallocstrcpy(NULL, buf), *filename; + char *dirname = mallocstrcpy(NULL, buf); + char *slash, *filename; size_t filenamelen; char **matches = NULL; DIR *dir; const struct dirent *nextdir; - assert(dirname != NULL && num_matches != NULL); - *num_matches = 0; null_at(&dirname, buf_len); - /* Okie, if there's a / in the buffer, strip out the directory part. */ - filename = strrchr(dirname, '/'); - if (filename != NULL) { - char *tmpdirname = filename + 1; + /* If there's a / in the name, strip out the directory part. */ + slash = strrchr(dirname, '/'); + if (slash != NULL) { + char *wasdirname = dirname; - filename = mallocstrcpy(NULL, tmpdirname); - *tmpdirname = '\0'; - tmpdirname = dirname; + filename = mallocstrcpy(NULL, slash + 1); + *slash = '\0'; dirname = real_dir_from_tilde(dirname); - free(tmpdirname); + free(wasdirname); } else { filename = dirname; dirname = mallocstrcpy(NULL, "./");