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