From 77b0c12adbc1d0d188ac032e29b5c985c510f119 Mon Sep 17 00:00:00 2001 From: Anthony Mallet Date: Sun, 31 Jul 2011 22:58:54 +0200 Subject: [PATCH 1/3] [pkgtools/pkg_install] Factorize code adding a version wildcard to a base name Instead of hardcoding at several places that a package basename can match any package version by adding the glob "-[0-9]*", do this is a common function. --- pkgtools/pkg_install/files/admin/check.c | 2 +- pkgtools/pkg_install/files/admin/main.c | 2 +- pkgtools/pkg_install/files/info/perform.c | 4 ++-- pkgtools/pkg_install/files/lib/lib.h | 1 + pkgtools/pkg_install/files/lib/str.c | 9 +++++++++ 5 files changed, 14 insertions(+), 4 deletions(-) diff --git pkgtools/pkg_install/files/admin/check.c pkgtools/pkg_install/files/admin/check.c index dd0012b..83b530b 100644 --- pkgtools/pkg_install/files/admin/check.c +++ pkgtools/pkg_install/files/admin/check.c @@ -223,7 +223,7 @@ check_pkg(const char *pkg, int *filecnt, int *pkgcnt, int allow_unmatched) errx(EXIT_FAILURE, "No matching pkg for %s.", pkg); } - pattern = xasprintf("%s-[0-9]*", pkg); + pattern = addpkgwildcard(pkg); if (match_installed_pkgs(pattern, checkpattern_fn, &arg) == -1) errx(EXIT_FAILURE, "Cannot process pkdbdb"); diff --git pkgtools/pkg_install/files/admin/main.c pkgtools/pkg_install/files/admin/main.c index 56770ac..dbfbfe2 100644 --- pkgtools/pkg_install/files/admin/main.c +++ pkgtools/pkg_install/files/admin/main.c @@ -729,7 +729,7 @@ set_unset_variable(char **argv, Boolean unset) warnx("no matching pkg for `%s'", *argv); ret++; } else { - pattern = xasprintf("%s-[0-9]*", *argv); + pattern = addpkgwildcard(*argv); if (match_installed_pkgs(pattern, set_installed_info_var, &arg) == -1) errx(EXIT_FAILURE, "Cannot process pkdbdb"); diff --git pkgtools/pkg_install/files/info/perform.c pkgtools/pkg_install/files/info/perform.c index 8278158..6478fcb 100644 --- pkgtools/pkg_install/files/info/perform.c +++ pkgtools/pkg_install/files/info/perform.c @@ -548,7 +548,7 @@ CheckForPkg(const char *pkgname) if (arg.got_match == 0 && !ispkgpattern(pkgname)) { char *pattern; - pattern = xasprintf("%s-[0-9]*", pkgname); + pattern = addpkgwildcard(pkgname); arg.pattern = pattern; arg.got_match = 0; @@ -583,7 +583,7 @@ CheckForBestPkg(const char *pkgname) if (ispkgpattern(pkgname)) return 1; - pattern = xasprintf("%s-[0-9]*", pkgname); + pattern = addpkgwildcard(pkgname); best_match = find_best_matching_installed_pkg(pattern); free(pattern); } diff --git pkgtools/pkg_install/files/lib/lib.h pkgtools/pkg_install/files/lib/lib.h index f79de96..13cb77a 100644 --- pkgtools/pkg_install/files/lib/lib.h +++ pkgtools/pkg_install/files/lib/lib.h @@ -282,6 +282,7 @@ const char *suffix_of(const char *); int pkg_match(const char *, const char *); int pkg_order(const char *, const char *, const char *); int ispkgpattern(const char *); +char * addpkgwildcard(const char *); int quick_pkg_match(const char *, const char *); /* Iterator functions */ diff --git pkgtools/pkg_install/files/lib/str.c pkgtools/pkg_install/files/lib/str.c index b190a1e..e2df4f1 100644 --- pkgtools/pkg_install/files/lib/str.c +++ pkgtools/pkg_install/files/lib/str.c @@ -100,3 +100,12 @@ ispkgpattern(const char *pkg) { return strpbrk(pkg, "<>[]?*{") != NULL; } + +/* + * Add a version wildcard to pkg + */ +char * +addpkgwildcard(const char *pkg) +{ + return xasprintf("%s-[0-9]*", pkg); +} -- 1.8.5.1