]> pd.if.org Git - zpackage/blob - zpm-packagehash.c
add program to generate a hash of package contents
[zpackage] / zpm-packagehash.c
1 #define _POSIX_C_SOURCE 2 
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <unistd.h>
7
8 #include "zpm.h"
9
10 static int found = 0;
11
12 void usage(void) {
13         fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
14 }
15
16 int main(int ac, char **av){
17         int opt;
18         struct zpm pkg;
19         char *dbfile;
20
21         int set = 0;
22
23         dbfile = getenv("ZPMDB");
24         if (!dbfile) {
25                 dbfile = "/var/lib/zpm/local.db";
26         }
27
28         while ((opt = getopt(ac, av, "f:s")) != -1) {
29                 switch (opt) {
30                         case 'f': dbfile = optarg; break;
31                         case 's': set = 1; break;
32                         default:
33                                   usage();
34                                   exit(EXIT_FAILURE);
35                                   break;
36                 }
37         }
38         int argn = optind;
39
40         if (!dbfile) {
41                 fprintf(stderr, "must specify db\n");
42                 return 1;
43         }
44
45         char *pkgid = av[argn];
46         char hash[ZPM_HASH_STRLEN+1];
47
48         if (zpm_open(&pkg, dbfile)) {
49                 if (set) {
50                         found = zpm_package_sethash(&pkg, pkgid, hash);
51                 } else {
52                         found = zpm_package_hash(&pkg, pkgid, hash);
53                 }
54         }
55         zpm_close(&pkg);
56         if (found) {
57                 printf("%s\n", hash);
58         }
59         return found ? 0 : 1;
60 }