--- /dev/null
+#define _POSIX_C_SOURCE 2
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <unistd.h>
+
+#include "zpm.h"
+
+void usage(void) {
+ fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
+}
+
+int main(int ac, char **av){
+ int opt, argn;
+ struct zpm zpm;
+ char *dbfile = 0, *pkgstr = 0, *pkgid = 0;
+
+ char *sql;
+ sqlite3_str *include;
+ sqlite3_str *exclude;
+ sqlite3_str *query;
+
+ dbfile = getenv("ZPMDB");
+ if (!dbfile) {
+ dbfile = "/var/lib/zpm/local.db";
+ }
+
+ include = sqlite3_str_new(NULL);
+ exclude = sqlite3_str_new(NULL);
+
+ while ((opt = getopt(ac, av, "f:s:S:I")) != -1) {
+ switch (opt) {
+ case 'f': dbfile = optarg; break;
+ case 's': sqlite3_str_appendf(include,",%Q", optarg);
+ break;
+ case 'S': sqlite3_str_appendf(exclude,",%Q", optarg);
+ break;
+ case 'I': sqlite3_str_appendall(include,",'installed'");
+ break;
+ default:
+ usage();
+ exit(EXIT_FAILURE);
+ break;
+ }
+ }
+ argn = optind;
+
+ if (!dbfile) {
+ fprintf(stderr, "must specify db\n");
+ return 1;
+ }
+
+ query = sqlite3_str_new(NULL);
+
+ if (zpm_open(&zpm, dbfile)) {
+ char *excludes, *includes;
+
+ excludes = sqlite3_str_value(exclude);
+ includes = sqlite3_str_value(include);
+
+ if (includes) {
+ sqlite3_str_appendf(query, "status in (%s)", includes+1);
+ }
+ if (excludes) {
+ sqlite3_str_appendf(query, "%sstatus not in (%s)",
+ includes ? " and " : "",
+ excludes+1);
+ }
+
+ if (ac >= argn) {
+ pkgstr = av[argn];
+ }
+
+ sql = sqlite3_str_value(query);
+
+ pkgid = zpm_findpkg(&zpm, pkgstr, sql);
+
+ sqlite3_free(sql);
+ if (pkgid) {
+ printf("%s\n", pkgid);
+ }
+ zpm_close(&zpm);
+ }
+ return pkgid ? 0 : 1;
+}