1 #include <u.h> 2 #include <libc.h> 3 #include <bio.h> 4 #include <ndb.h> 5 #include <ndbhf.h> 6 7 /* 8 * search for a tuple that has the given 'attr=val' and also 'rattr=x'. 9 * copy 'x' into 'buf' and return the whole tuple. 10 * 11 * return 0 if not found. 12 */ 13 Ndbtuple* 14 csgetval(char *netroot, char *attr, char *val, char *rattr, char *buf) 15 { 16 Ndbtuple *t, *first, *last; 17 int n, linefound; 18 char line[1024]; 19 int fd; 20 21 buf[0] = 0; 22 23 if(netroot) 24 snprint(line, sizeof(line), "%s/cs", netroot); 25 else 26 strcpy(line, "/net/cs"); 27 fd = open(line, ORDWR); 28 if(fd < 0) 29 return 0; 30 seek(fd, 0, 0); 31 snprint(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr); 32 if(write(fd, line, strlen(line)) < 0){ 33 close(fd); 34 return 0; 35 } 36 seek(fd, 0, 0); 37 38 first = last = 0; 39 linefound = 0; 40 for(;;){ 41 n = read(fd, line, sizeof(line)-2); 42 if(n <= 0) 43 break; 44 line[n] = '\n'; 45 line[n+1] = 0; 46 47 t = _ndbparseline(line); 48 if(t == 0) 49 continue; 50 if(first) 51 last->entry = t; 52 else 53 first = t; 54 last = t; 55 56 while(last->entry) 57 last = last->entry; 58 59 for(; t; t = t->entry){ 60 if(buf[0] == 0 || linefound == 0) 61 if(strcmp(rattr, t->attr) == 0) 62 strcpy(buf, t->val); 63 if(linefound == 0) 64 if(strcmp(attr, t->attr) == 0) 65 linefound = 1; 66 } 67 } 68 close(fd); 69 70 return first; 71 } 72