Add ndblib and iplib
[akaros.git] / user / ndblib / ndbgetval.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 #include <stdlib.h>
10
11 #include <stdio.h>
12 #include <parlib.h>
13 #include <unistd.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <libip.h>
17 #include <ndb.h>
18
19 /*
20  *  search for a tuple that has the given 'attr=val' and also 'rattr=x'.
21  *  copy 'x' into 'buf' and return the whole tuple.
22  *
23  *  return 0 if not found.
24  */
25 char*
26 ndbgetvalue(struct ndb *db, struct ndbs *s, char *attr, char *val, char *rattr,
27             struct ndbtuple **pp)
28 {
29         struct ndbtuple *t, *nt;
30         char *rv;
31         struct ndbs temps;
32
33         if(s == NULL)
34                 s = &temps;
35         if(pp)
36                 *pp = NULL;
37         t = ndbsearch(db, s, attr, val);
38         while(t){
39                 /* first look on same line (closer binding) */
40                 nt = s->t;
41                 for(;;){
42                         if(strcmp(rattr, nt->attr) == 0){
43                                 rv = strdup(nt->val);
44                                 if(pp != NULL)
45                                         *pp = t;
46                                 else
47                                         ndbfree(t);
48                                 return rv;
49                         }
50                         nt = nt->line;
51                         if(nt == s->t)
52                                 break;
53                 }
54                 /* search whole tuple */
55                 for(nt = t; nt; nt = nt->entry){
56                         if(strcmp(rattr, nt->attr) == 0){
57                                 rv = strdup(nt->val);
58                                 if(pp != NULL)
59                                         *pp = t;
60                                 else
61                                         ndbfree(t);
62                                 return rv;
63                         }
64                 }
65                 ndbfree(t);
66                 t = ndbsnext(s, attr, val);
67         }
68         return NULL;
69 }
70
71 struct ndbtuple*
72 ndbgetval(struct ndb *db,
73           struct ndbs *s, char *attr, char *val, char *rattr, char *buf)
74 {
75         struct ndbtuple *t;
76         char *p;
77
78         p = ndbgetvalue(db, s, attr, val, rattr, &t);
79         if(p == NULL){
80                 if(buf != NULL)
81                         *buf = 0;
82         } else {
83                 if(buf != NULL){
84                         strncpy(buf, p, Ndbvlen-1);
85                         buf[Ndbvlen-1] = 0;
86                 }
87                 free(p);
88         }
89         ndbsetmalloctag(t, getcallerpc(&db));
90         return t;
91 }