]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/csipinfo.c
Import sources from 2011-03-30 iso image - lib
[plan9front.git] / sys / src / libndb / csipinfo.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ndb.h>
5 #include <ndbhf.h>
6
7 /*
8  *  look up the ip attributes 'list' for an entry that has the
9  *  given 'attr=val' and a 'ip=' tuples.
10  *
11  *  return nil if not found.
12  */
13 Ndbtuple*
14 csipinfo(char *netroot, char *attr, char *val, char **list, int n)
15 {
16         Ndbtuple *t, *first, *last;
17         int i;
18         char line[1024];
19         int fd;
20         char *p, *e;
21
22         if(netroot)
23                 snprint(line, sizeof(line), "%s/cs", netroot);
24         else
25                 strcpy(line, "/net/cs");
26         fd = open(line, ORDWR);
27         if(fd < 0)
28                 return 0;
29         seek(fd, 0, 0);
30         e = line + sizeof(line);
31         p = seprint(line, e, "!ipinfo %s=%s", attr, val);
32         for(i = 0; i < n; i++){
33                 if(*list == nil)
34                         break;
35                 p = seprint(p, e, " %s", *list++);
36         }
37         
38         if(write(fd, line, strlen(line)) < 0){
39                 close(fd);
40                 return 0;
41         }
42         seek(fd, 0, 0);
43
44         first = last = 0;
45         for(;;){
46                 n = read(fd, line, sizeof(line)-2);
47                 if(n <= 0)
48                         break;
49                 line[n] = '\n';
50                 line[n+1] = 0;
51
52                 t = _ndbparseline(line);
53                 if(t == 0)
54                         continue;
55                 if(first)
56                         last->entry = t;
57                 else
58                         first = t;
59                 last = t;
60
61                 while(last->entry)
62                         last = last->entry;
63         }
64         close(fd);
65
66         ndbsetmalloctag(first, getcallerpc(&netroot));
67         return first;
68 }