summaryrefslogtreecommitdiffstats
path: root/src/libndb/csgetval.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2005-02-11 19:41:16 +0000
committerrsc <devnull@localhost>2005-02-11 19:41:16 +0000
commitd957951b75df08a9bb0293e3e13ff87759afbb92 (patch)
tree4d7868b0d223956217cbc8819d7afb3bec532cca /src/libndb/csgetval.c
parentad017cfbf5530cfc3ae2fafd723cdade2a4405f6 (diff)
downloadplan9port-d957951b75df08a9bb0293e3e13ff87759afbb92.tar.gz
plan9port-d957951b75df08a9bb0293e3e13ff87759afbb92.zip
new
Diffstat (limited to 'src/libndb/csgetval.c')
-rw-r--r--src/libndb/csgetval.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/libndb/csgetval.c b/src/libndb/csgetval.c
new file mode 100644
index 00000000..22705ade
--- /dev/null
+++ b/src/libndb/csgetval.c
@@ -0,0 +1,107 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <ndb.h>
+#include <ndbhf.h>
+
+/*
+ * search for a tuple that has the given 'attr=val' and also 'rattr=x'.
+ * copy 'x' into 'buf' and return the whole tuple.
+ *
+ * return 0 if not found.
+ */
+char*
+csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp)
+{
+ Ndbtuple *t, *first, *last;
+ int n, linefound;
+ char line[1024];
+ int fd;
+ int oops = 0;
+ char *rv;
+
+ if(pp)
+ *pp = nil;
+ rv = nil;
+
+ if(netroot)
+ snprint(line, sizeof(line), "%s/cs", netroot);
+ else
+ strcpy(line, "/net/cs");
+ fd = open(line, ORDWR);
+ if(fd < 0)
+ return 0;
+ seek(fd, 0, 0);
+ snprint(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr);
+ if(write(fd, line, strlen(line)) < 0){
+ close(fd);
+ return 0;
+ }
+ seek(fd, 0, 0);
+
+ first = last = 0;
+ linefound = 0;
+ for(;;){
+ n = read(fd, line, sizeof(line)-2);
+ if(n <= 0)
+ break;
+ line[n] = '\n';
+ line[n+1] = 0;
+
+ t = _ndbparseline(line);
+ if(t == 0)
+ continue;
+ if(first)
+ last->entry = t;
+ else
+ first = t;
+ last = t;
+
+ while(last->entry)
+ last = last->entry;
+
+ for(; t; t = t->entry){
+ if(linefound == 0){
+ if(strcmp(rattr, t->attr) == 0){
+ linefound = 1;
+ rv = strdup(t->val);
+ }
+ }
+ }
+ }
+ close(fd);
+
+ if(oops){
+ werrstr("buffer too short");
+ ndbfree(first);
+ return nil;
+ }
+
+ if(pp){
+ setmalloctag(first, getcallerpc(&netroot));
+ *pp = first;
+ } else
+ ndbfree(first);
+
+ return rv;
+}
+
+Ndbtuple*
+csgetval(char *netroot, char *attr, char *val, char *rattr, char *buf)
+{
+ Ndbtuple *t;
+ char *p;
+
+ p = csgetvalue(netroot, attr, val, rattr, &t);
+ if(p == nil){
+ if(buf != nil)
+ *buf = 0;
+ } else {
+ if(buf != nil){
+ strncpy(buf, p, Ndbvlen-1);
+ buf[Ndbvlen-1] = 0;
+ }
+ free(p);
+ }
+ return t;
+}