#define _POSIX_C_SOURCE 2 #include #include #include #include #include "zpm.h" void usage(void) { fprintf(stderr, "zpm-findpkg [-I] [-s ...] [-S ] [package]\n"); } int main(int ac, char **av){ int opt, argn; struct zpm zpm; char *dbfile = 0, *pkgstr = 0, *pkgid = 0; int range = 0, wantleast = 0, argismax = 0; char *minpkg = 0, *maxpkg = 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:Imrl")) != -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; case 'l': wantleast = 1; break; case 'r': range = 1; break; case 'm': argismax = 1; range = 1; break; default: usage(); exit(EXIT_FAILURE); break; } } argn = optind; if (ac < argn) { usage(); exit(EXIT_FAILURE); } pkgstr = minpkg = av[argn]; if (ac > argn) { maxpkg = av[argn+1]; } if (maxpkg) { range = 1; } 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); } sql = sqlite3_str_value(query); if (range) { if (argismax) { /* swap the sense of the arguments */ minpkg = maxpkg; maxpkg = pkgstr; } pkgid = zpm_findpkg_range(&zpm, minpkg, maxpkg, sql, wantleast); } else { pkgid = zpm_findpkg(&zpm, pkgstr, sql); } sqlite3_free(sql); if (pkgid) { printf("%s\n", pkgid); } zpm_close(&zpm); } return pkgid ? 0 : 1; }