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;
+ int fd, n;
char *rv;
- if(pp)
+ if(pp != nil)
*pp = nil;
- rv = nil;
if(netroot)
snprint(line, sizeof(line), "%s/cs", netroot);
strcpy(line, "/net/cs");
fd = open(line, ORDWR);
if(fd < 0)
- return 0;
+ return nil;
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;
+ return nil;
}
seek(fd, 0, 0);
- first = last = 0;
- linefound = 0;
+ rv = nil;
+ first = last = nil;
for(;;){
n = read(fd, line, sizeof(line)-2);
if(n <= 0)
line[n+1] = 0;
t = _ndbparseline(line);
- if(t == 0)
+ if(t == nil)
continue;
- if(first)
+ if(first != nil)
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);
- }
- }
- }
+ do {
+ last = t;
+ if(rv == nil && strcmp(rattr, t->attr) == 0)
+ rv = strdup(t->val);
+ t = t->entry;
+ } while(t != nil);
}
close(fd);
- if(oops){
- werrstr("buffer too short");
- ndbfree(first);
- return nil;
- }
-
- if(pp){
+ if(pp != nil){
setmalloctag(first, getcallerpc(&netroot));
*pp = first;
} else