]> pd.if.org Git - zpackage/blob - zpm-findpkg.c
fix incorrect sqlite vercmp function
[zpackage] / zpm-findpkg.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 void usage(void) {
11         fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
12 }
13
14 int main(int ac, char **av){
15         int opt, argn;
16         struct zpm zpm;
17         char *dbfile = 0, *pkgstr = 0, *pkgid = 0;
18
19         char *sql;
20         sqlite3_str *include;
21         sqlite3_str *exclude;
22         sqlite3_str *query;
23
24         dbfile = getenv("ZPMDB");
25         if (!dbfile) {
26                 dbfile = "/var/lib/zpm/local.db";
27         }
28
29         include = sqlite3_str_new(NULL);
30         exclude = sqlite3_str_new(NULL);
31
32         while ((opt = getopt(ac, av, "f:s:S:I")) != -1) {
33                 switch (opt) {
34                         case 'f': dbfile = optarg; break;
35                         case 's': sqlite3_str_appendf(include,",%Q", optarg);
36                                   break;
37                         case 'S': sqlite3_str_appendf(exclude,",%Q", optarg);
38                                   break;
39                         case 'I': sqlite3_str_appendall(include,",'installed'");
40                                   break;
41                         default:
42                                   usage();
43                                   exit(EXIT_FAILURE);
44                                   break;
45                 }
46         }
47         argn = optind;
48
49         if (!dbfile) {
50                 fprintf(stderr, "must specify db\n");
51                 return 1;
52         }
53
54         query = sqlite3_str_new(NULL);
55
56         if (zpm_open(&zpm, dbfile)) {
57                 char *excludes, *includes;
58
59                 excludes = sqlite3_str_value(exclude);
60                 includes = sqlite3_str_value(include);
61
62                 if (includes) {
63                         sqlite3_str_appendf(query, "status in (%s)", includes+1);
64                 }
65                 if (excludes) {
66                         sqlite3_str_appendf(query, "%sstatus not in (%s)",
67                                        includes ? " and " : "",
68                                        excludes+1);
69                 }
70
71                 if (ac >= argn) {
72                         pkgstr = av[argn];
73                 }
74
75                 sql = sqlite3_str_value(query);
76
77                 pkgid = zpm_findpkg(&zpm, pkgstr, sql);
78
79                 sqlite3_free(sql);
80                 if (pkgid) {
81                         printf("%s\n", pkgid);
82                 }
83                 zpm_close(&zpm);
84         }
85         return pkgid ? 0 : 1;
86 }