1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
/*
* search the database for matches
*/
void
usage(void)
{
fprint(2, "usage: query attr value [returned attribute]\n");
exits("usage");
}
void
search(Ndb *db, char *attr, char *val, char *rattr)
{
Ndbs s;
Ndbtuple *t;
Ndbtuple *nt;
char *p;
if(rattr){
p = ndbgetvalue(db, &s, attr, val, rattr, nil);
if(p){
print("%s\n", p);
free(p);
}
return;
}
t = ndbsearch(db, &s, attr, val);
while(t){
for(nt = t; nt; nt = nt->entry)
print("%s=%s ", nt->attr, nt->val);
print("\n");
ndbfree(t);
t = ndbsnext(&s, attr, val);
}
}
void
main(int argc, char **argv)
{
char *rattr = 0;
Ndb *db;
char *dbfile = 0;
int reps = 1;
ARGBEGIN{
case 'f':
dbfile = ARGF();
break;
}ARGEND;
switch(argc){
case 4:
reps = atoi(argv[3]);
/* fall through */
case 3:
rattr = argv[2];
break;
case 2:
rattr = 0;
break;
default:
usage();
}
db = ndbopen(dbfile);
if(db == 0){
fprint(2, "no db files\n");
exits("no db");
}
while(reps--)
search(db, argv[0], argv[1], rattr);
ndbclose(db);
exits(0);
}
|