#define _POSIX_C_SOURCE 2 #include #include #include #include #include #include "zpm.h" static int found = 0; void usage(void) { fprintf(stderr, "zpm-findpkg [-I] [-s ...] [-S ] [package]\n"); } int main(int ac, char **av){ int opt; struct zpm pkg; char *dbfile; int set = 0; int check = 0; int quiet = 0; dbfile = getenv("ZPMDB"); if (!dbfile) { dbfile = "/var/lib/zpm/local.db"; } while ((opt = getopt(ac, av, "f:scq")) != -1) { switch (opt) { case 'f': dbfile = optarg; break; case 's': set = 1; break; case 'c': check = 1; break; case 'q': quiet = 1; break; default: usage(); exit(EXIT_FAILURE); break; } } int argn = optind; if (!dbfile) { fprintf(stderr, "must specify db\n"); return 1; } char *pkgid = av[argn]; char hash[ZPM_HASH_STRLEN+1]; if (zpm_open(&pkg, dbfile)) { if (check) { found = zpm_package_hash(&pkg, pkgid, hash); char *ehash = zpm_db_string(&pkg, "select hash from packages_pkgid where pkgid = %Q", pkgid); if (ehash && found && !strcmp(ehash, hash)) { found = 1; } else { found = 0; } } else if (set) { found = zpm_package_sethash(&pkg, pkgid, hash); } else { found = zpm_package_hash(&pkg, pkgid, hash); } } zpm_close(&pkg); if (found && !quiet) { printf("%s\n", hash); } return found ? 0 : 1; }