tweaks: avoid unneeded calls of free() by reallocating the full name

master
Benno Schulenberg 2020-06-26 09:53:55 +02:00
parent c0c4a1b2b5
commit 69b12d907e
1 changed files with 7 additions and 10 deletions

View File

@ -2407,9 +2407,10 @@ char **filename_completion(const char *buf, size_t length, size_t *num_matches)
char *dirname = copy_of(buf); char *dirname = copy_of(buf);
char *slash, *filename; char *slash, *filename;
size_t filenamelen; size_t filenamelen;
char *fullname = NULL;
char **matches = NULL; char **matches = NULL;
DIR *dir;
const struct dirent *entry; const struct dirent *entry;
DIR *dir;
/* If there's a / in the name, split out filename and directory parts. */ /* If there's a / in the name, split out filename and directory parts. */
slash = strrchr(dirname, '/'); slash = strrchr(dirname, '/');
@ -2448,22 +2449,17 @@ char **filename_completion(const char *buf, size_t length, size_t *num_matches)
if (strncmp(entry->d_name, filename, filenamelen) == 0 && if (strncmp(entry->d_name, filename, filenamelen) == 0 &&
strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, ".") != 0 &&
strcmp(entry->d_name, "..") != 0) { strcmp(entry->d_name, "..") != 0) {
char *fullname = charalloc(strlen(dirname) + strlen(entry->d_name) + 1); fullname = charealloc(fullname, strlen(dirname) +
strlen(entry->d_name) + 1);
sprintf(fullname, "%s%s", dirname, entry->d_name); sprintf(fullname, "%s%s", dirname, entry->d_name);
#ifdef ENABLE_OPERATINGDIR #ifdef ENABLE_OPERATINGDIR
if (outside_of_confinement(fullname, TRUE)) { if (outside_of_confinement(fullname, TRUE))
free(fullname);
continue; continue;
}
#endif #endif
if (currmenu == MGOTODIR && !is_dir(fullname)) { if (currmenu == MGOTODIR && !is_dir(fullname))
free(fullname);
continue; continue;
}
free(fullname);
matches = (char **)nrealloc(matches, (*num_matches + 1) * sizeof(char *)); matches = (char **)nrealloc(matches, (*num_matches + 1) * sizeof(char *));
matches[*num_matches] = copy_of(entry->d_name); matches[*num_matches] = copy_of(entry->d_name);
@ -2474,6 +2470,7 @@ char **filename_completion(const char *buf, size_t length, size_t *num_matches)
closedir(dir); closedir(dir);
free(dirname); free(dirname);
free(filename); free(filename);
free(fullname);
return matches; return matches;
} }