From 96360428586f3d5722bb9fdc9baf1b1cc0b5dbc6 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 22 Feb 2020 16:05:05 -0800 Subject: Initial working version using globbing. --- bb.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 23 deletions(-) (limited to 'bb.c') diff --git a/bb.c b/bb.c index d5cda7a..fad458f 100644 --- a/bb.c +++ b/bb.c @@ -460,27 +460,21 @@ int populate_files(bb_t *bb, const char *path) bb->files[bb->nfiles++] = e; } } else { - DIR *dir = opendir(bb->path); - if (!dir) - err("Couldn't open dir: %s", bb->path); - - for (struct dirent *dp; (dp = readdir(dir)) != NULL; ) { - if (dp->d_name[0] == '.') { - if (dp->d_name[1] == '.' && dp->d_name[2] == '\0') { - if (!bb->show_dotdot || strcmp(bb->path, "/") == 0) continue; - } else if (dp->d_name[1] == '\0') { - if (!bb->show_dot) continue; - } else if (!bb->show_dotfiles) continue; - } + glob_t globbuf = {0}; + char *pat, *tmpglob = memcheck(strdup(bb->globpats)); + while ((pat = strsep(&tmpglob, " ")) != NULL) + glob(pat, GLOB_NOSORT|GLOB_APPEND, NULL, &globbuf); + free(tmpglob); + for (size_t i = 0; i < globbuf.gl_pathc; i++) { if ((size_t)bb->nfiles + 1 > space) bb->files = memcheck(realloc(bb->files, (space += 100)*sizeof(void*))); // Don't normalize path so we can get "." and ".." - entry_t *entry = load_entry(bb, dp->d_name); - if (!entry) err("Failed to load entry: '%s'", dp->d_name); + entry_t *entry = load_entry(bb, globbuf.gl_pathv[i]); + if (!entry) err("Failed to load entry: '%s'", globbuf.gl_pathv[i]); entry->index = bb->nfiles; bb->files[bb->nfiles++] = entry; } - closedir(dir); + globfree(&globbuf); } for (int i = 0; i < bb->nfiles; i++) { @@ -551,10 +545,10 @@ void run_bbcmd(bb_t *bb, const char *cmd) if (value) ++value; #define set_bool(target) do { if (!value) { target = !target; } else { target = value[0] == '1'; } } while (0) if (matches_cmd(cmd, ".")) { // +. - set_bool(bb->show_dot); + set_globs(bb, ". *"); populate_files(bb, bb->path); } else if (matches_cmd(cmd, "..")) { // +.. - set_bool(bb->show_dotdot); + set_globs(bb, ".. *"); populate_files(bb, bb->path); } else if (matches_cmd(cmd, "bind:")) { // +bind::