commit 8b300b6d3278953cbfdab66841b88e70f7d5a191
parent 3e37c72f3a9b2b1ec279336eeeaec8a1f671e396
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Wed, 17 Feb 2021 13:08:32 -0600
pkg.*: add pkg_match, which matches packages by glob
Diffstat:
M | pkg.c | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
M | pkg.h | | | 1 | + |
2 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/pkg.c b/pkg.c
@@ -1,3 +1,6 @@
+#include <dirent.h>
+#include <fnmatch.h>
+#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -25,3 +28,39 @@ pkg_version(char *repo, char *name, char *ver)
fclose(vfile);
}
+
+int
+pkg_match(char *repo, char pkgs[][64], const char *pattern)
+{
+ DIR *db;
+ int index = 0;
+
+ if ((db = opendir(repo)) == NULL)
+ die("couldn't open package database:");
+
+ for (struct dirent *pkg = readdir(db); pkg != NULL; pkg = readdir(db)) {
+ if (fnmatch(pattern, pkg->d_name, 0) == FNM_NOMATCH)
+ continue;
+ else if (fnmatch(pattern, pkg->d_name, 0) != 0) {
+ closedir(db);
+ die("%s: fnmatch failed for '%s'", __func__, pkg->d_name);
+ }
+
+ if (strlen(pkg->d_name) >= 64) {
+ closedir(db);
+ die("%s: '%s' is too long of a package name", __func__,
+ pkg->d_name);
+ }
+
+ strcpy(pkgs[index++], pkg->d_name);
+
+ if (index > 1024) {
+ closedir(db);
+ die("%s: too many packages to list!", __func__);
+ }
+ }
+ closedir(db);
+ qsort(pkgs, index, LEN(pkgs[0]), &vstrcmp);
+
+ return index;
+}
diff --git a/pkg.h b/pkg.h
@@ -2,3 +2,4 @@
#define PKG_VERSION_MAX 256
void pkg_version(char *repo, char *name, char *ver);
+int pkg_match(char *repo, char pkgs[][64], const char *pattern);