diff --git a/gplaces.c b/gplaces.c index c8b3a0e..3f12c4c 100644 --- a/gplaces.c +++ b/gplaces.c @@ -1634,13 +1634,14 @@ static void eval(const char *input, const char *filename, int line_no) { static void shell_name_completion(const char *text, bestlineCompletions *lc) { static char buffer[1024]; + struct stat stbuf; URL url = {0}; const Command *cmd; const Variable *var; const Selector *sel; long index; char *end; - int len; + int len, fd; size_t namelen; char *sep; DIR *dir; @@ -1664,14 +1665,19 @@ static void shell_name_completion(const char *text, bestlineCompletions *lc) { buffer[sep - text] = '\0'; if ((dir = opendir(buffer)) == NULL) return; + if ((fd = dirfd(dir)) == -1) { closedir(dir); return; } buffer[sep - text] = '/'; while ((ent = readdir(dir)) != NULL) { if (ent->d_name[0] == '.') continue; + if (fstatat(fd, ent->d_name, &stbuf, 0) == -1) continue; if (strncmp(ent->d_name, sep + 1, len - (sep - text) - 1) != 0) continue; namelen = strlen(ent->d_name); - if (sizeof(buffer) <= (size_t)(sep - text + 1 + namelen)) continue; + if (sizeof(buffer) <= (size_t)(sep - text + 1 + namelen + S_ISDIR(stbuf.st_mode) ? 1 : 0)) continue; memcpy(&buffer[sep - text + 1], ent->d_name, namelen); - buffer[sep - text + 1 + namelen] = '\0'; + if (S_ISDIR(stbuf.st_mode)) { + buffer[sep - text + 1 + namelen] = '/'; + buffer[sep - text + 1 + namelen + 1] = '\0'; + } else buffer[sep - text + 1 + namelen] = '\0'; bestlineAddCompletion(lc, buffer); } closedir(dir);