Skip to content

Commit

Permalink
add trailing /
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Apr 6, 2024
1 parent 56f9066 commit e82bb54
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions gplaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit e82bb54

Please sign in to comment.