]> pd.if.org Git - zpackage/blob - src/packagehash.c
remove stray debug fprintf
[zpackage] / src / 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 #include <string.h>
8
9 #include "zpm.h"
10
11 void usage(void) {
12         fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
13 }
14
15 int main(int ac, char **av){
16         int opt, argn;
17         struct zpm pkg;
18         char *dbfile;
19         int fail = 0;
20
21         int set = 0, clear = 0, showcurrent = 0;
22         int check = 0, quiet = 0, checkfail = 0, verbose = 0;
23         char hash[ZPM_HASH_STRLEN+1] = { 0 };
24         char *pkgid = 0, *current = 0, *display = hash;
25
26         dbfile = getenv("ZPMDB");
27         if (!dbfile) {
28                 dbfile = "/var/lib/zpm/local.db";
29         }
30
31         /* set -s
32          * clear -S
33          * show current -e
34          * check -c
35          */ 
36         while ((opt = getopt(ac, av, "f:sScqev")) != -1) {
37                 switch (opt) {
38                         case 'f': dbfile = optarg; break;
39                         case 's': set = 1; break;
40                         case 'S': clear = 1; break;
41                         case 'c': check = 1; break;
42                         case 'q': quiet = 1; break;
43                         case 'e': showcurrent = 1; break;
44                         case 'v': verbose = 1; break;
45                         default:
46                                   usage();
47                                   exit(EXIT_FAILURE);
48                                   break;
49                 }
50         }
51         argn = optind;
52
53         if (!dbfile) {
54                 fprintf(stderr, "must specify db\n");
55                 return 1;
56         }
57
58         if (zpm_open(&pkg, dbfile)) {
59                 pkgid = zpm_findpkg(&pkg, av[argn], NULL);
60                 if (!pkgid) {
61                         exit(EXIT_FAILURE);
62                 }
63                 if (check || showcurrent) {
64                         current = zpm_package_gethash(&pkg, pkgid, 0);
65                 }
66
67                 if (check) {
68                         fail = zpm_package_checkhash(&pkg, pkgid, 0);
69                 } else if (set) {
70                         fail = zpm_package_sethash(&pkg, pkgid, hash);
71                 } else if (clear) {
72                         fail = zpm_package_clearhash(&pkg, pkgid);
73                 } else if (showcurrent) {
74                         display = current;
75                 } else {
76                         fail = zpm_package_hash(&pkg, pkgid, hash);
77                 }
78
79                 zpm_close(&pkg);
80                 if (display && !quiet && !fail) {
81                         if (verbose) {
82                                 printf("%s ", pkgid);
83                         }
84                         printf("%s\n", display);
85                 }
86                 free(current);
87         }
88
89         return checkfail ? EXIT_FAILURE : 0;
90 }