diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libndb/csipinfo.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libndb/csipinfo.c')
-rwxr-xr-x | sys/src/libndb/csipinfo.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/sys/src/libndb/csipinfo.c b/sys/src/libndb/csipinfo.c new file mode 100755 index 000000000..53c33f328 --- /dev/null +++ b/sys/src/libndb/csipinfo.c @@ -0,0 +1,68 @@ +#include <u.h> +#include <libc.h> +#include <bio.h> +#include <ndb.h> +#include <ndbhf.h> + +/* + * look up the ip attributes 'list' for an entry that has the + * given 'attr=val' and a 'ip=' tuples. + * + * return nil if not found. + */ +Ndbtuple* +csipinfo(char *netroot, char *attr, char *val, char **list, int n) +{ + Ndbtuple *t, *first, *last; + int i; + char line[1024]; + int fd; + char *p, *e; + + 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); + e = line + sizeof(line); + p = seprint(line, e, "!ipinfo %s=%s", attr, val); + for(i = 0; i < n; i++){ + if(*list == nil) + break; + p = seprint(p, e, " %s", *list++); + } + + if(write(fd, line, strlen(line)) < 0){ + close(fd); + return 0; + } + seek(fd, 0, 0); + + first = last = 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; + } + close(fd); + + ndbsetmalloctag(first, getcallerpc(&netroot)); + return first; +} |