From fd9ff672b0010650297e645c4fd1bd1cc320e3ef Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Thu, 18 Jun 2015 19:27:51 +1000 Subject: Add regex search option to sync database file searching e.g. pacman -Fsx kcm.*print.*\.so Signed-off-by: Allan McRae --- src/pacman/files.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/pacman/files.c') diff --git a/src/pacman/files.c b/src/pacman/files.c index fc06ae7d..5156e466 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -19,6 +19,7 @@ #include #include +#include /* pacman */ #include "pacman.h" @@ -83,24 +84,33 @@ notfound: return 0; } -static int files_search(alpm_list_t *syncs, alpm_list_t *targets) { +static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int ret = 0; alpm_list_t *t; const colstr_t *colstr = &config->colstr; for(t = targets; t; t = alpm_list_next(t)) { - char *filename = NULL; + char *targ = NULL; alpm_list_t *s; int found = 0; + regex_t reg; - if((filename = strdup(t->data)) == NULL) { + if((targ = strdup(t->data)) == NULL) { goto notfound; } + if(regex) { + if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) { + /* TODO: error message */ + goto notfound; + } + } + for(s = syncs; s; s = alpm_list_next(s)) { alpm_list_t *p; alpm_db_t *repo = s->data; alpm_list_t *packages = alpm_db_get_pkgcache(repo); + int m; for(p = packages; p; p = alpm_list_next(p)) { size_t f = 0; @@ -112,7 +122,12 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets) { while(f < files->count) { c = strrchr(files->files[f].name, '/'); if(c && *(c + 1)) { - if(strcmp(c + 1, filename) == 0) { + if(regex) { + m = regexec(®, (c + 1), 0, 0, 0); + } else { + m = strcmp(c + 1, targ); + } + if(m == 0) { match = alpm_list_add(match, strdup(files->files[f].name)); found = 1; } @@ -138,7 +153,8 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets) { } } } - free(filename); + + free(targ); notfound: if(!found) { @@ -219,7 +235,7 @@ int pacman_files(alpm_list_t *targets) /* search for a file */ if(config->op_s_search) { - return files_search(files_dbs, targets); + return files_search(files_dbs, targets, config->op_f_regex); } /* get a listing of files in sync DBs */ -- cgit v1.2.3-54-g00ecf