ref: dbfb32056693662b9843b47c70a672f32ea55d8b
parent: 5aae3d344b9f362539f06d20ea5ca80a0c8b3a78
author: cinap_lenrek <[email protected]>
date: Sat Apr 28 00:38:34 EDT 2018
libndb: cleanup
--- a/sys/src/libndb/csgetval.c
+++ b/sys/src/libndb/csgetval.c
@@ -14,15 +14,12 @@
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);
@@ -30,17 +27,17 @@
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)
@@ -49,35 +46,22 @@
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
--- a/sys/src/libndb/dnsquery.c
+++ b/sys/src/libndb/dnsquery.c
@@ -129,17 +129,14 @@
t = _ndbparseline(buf);
if(t != nil){
- if(first)
+ if(first != nil)
last->entry = t;
else
first = t;
last = t;
-
- while(last->entry)
+ while(last->entry != nil)
last = last->entry;
}
}
-
- ndbsetmalloctag(first, getcallerpc(&fd));
return first;
}
--- a/sys/src/libndb/ndbdiscard.c
+++ b/sys/src/libndb/ndbdiscard.c
@@ -25,6 +25,5 @@
a->entry = nil;
ndbfree(a);
- ndbsetmalloctag(t, getcallerpc(&t));
return t;
}
--- a/sys/src/libndb/ndbfree.c
+++ b/sys/src/libndb/ndbfree.c
@@ -71,6 +71,6 @@
void
ndbsetmalloctag(Ndbtuple *t, uintptr tag)
{
- for(; t; t=t->entry)
+ for(; t != nil; t=t->entry)
setmalloctag(t, tag);
}
--- a/sys/src/libndb/ndbgetval.c
+++ b/sys/src/libndb/ndbgetval.c
@@ -7,7 +7,7 @@
* 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.
+ * return nil if not found.
*/
char*
ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp)
@@ -21,32 +21,15 @@
if(pp)
*pp = nil;
t = ndbsearch(db, s, attr, val);
- while(t){
- /* first look on same line (closer binding) */
- nt = s->t;
- for(;;){
- if(strcmp(rattr, nt->attr) == 0){
- rv = strdup(nt->val);
- if(pp != nil)
- *pp = t;
- else
- ndbfree(t);
- return rv;
- }
- nt = nt->line;
- if(nt == s->t)
- break;
- }
- /* search whole tuple */
- for(nt = t; nt; nt = nt->entry){
- if(strcmp(rattr, nt->attr) == 0){
- rv = strdup(nt->val);
- if(pp != nil)
- *pp = t;
- else
- ndbfree(t);
- return rv;
- }
+ while(t != nil){
+ nt = ndbfindattr(t, s->t, rattr);
+ if(nt != nil){
+ rv = strdup(nt->val);
+ if(pp != nil)
+ *pp = t;
+ else
+ ndbfree(t);
+ return rv;
}
ndbfree(t);
t = ndbsnext(s, attr, val);
--- a/sys/src/libndb/ndbhash.c
+++ b/sys/src/libndb/ndbhash.c
@@ -123,26 +123,22 @@
memset(s, 0, sizeof(*s));
if(_ndbcachesearch(db, s, attr, val, &t) == 0){
/* found in cache */
- if(t != nil){
- ndbsetmalloctag(t, getcallerpc(&db));
- return t; /* answer from this file */
- }
+ if(t != nil)
+ goto out;
if(db->next == nil)
return nil;
t = ndbsearch(db->next, s, attr, val);
- ndbsetmalloctag(t, getcallerpc(&db));
- return t;
+ goto out;
}
s->db = db;
s->hf = hf;
- if(s->hf){
+ if(s->hf != nil){
s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN;
p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN);
- if(p == 0){
+ if(p == nil){
t = _ndbcacheadd(db, s, attr, val, nil);
- ndbsetmalloctag(t, getcallerpc(&db));
- return t;
+ goto out;
}
s->ptr = NDBGETP(p);
s->type = Cptr1;
@@ -153,11 +149,10 @@
/* advance search to next db file */
s->ptr = NDBNAP;
_ndbcacheadd(db, s, attr, val, nil);
- if(db->next == 0)
+ if(db->next == nil)
return nil;
t = ndbsearch(db->next, s, attr, val);
- ndbsetmalloctag(t, getcallerpc(&db));
- return t;
+ goto out;
} else {
s->ptr = 0;
s->type = Dptr;
@@ -164,6 +159,7 @@
}
t = ndbsnext(s, attr, val);
_ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil);
+out:
ndbsetmalloctag(t, getcallerpc(&db));
return t;
}
@@ -173,7 +169,7 @@
{
Ndbtuple *nt;
- for(nt = t; nt; nt = nt->entry)
+ for(nt = t; nt != nil; nt = nt->entry)
if(strcmp(attr, nt->attr) == 0
&& strcmp(val, nt->val) == 0)
return nt;
@@ -200,12 +196,10 @@
break;
t = ndbparse(db);
s->ptr = Boffset(&db->b);
- if(t == 0)
+ if(t == nil)
break;
- if(s->t = match(t, attr, val)){
- ndbsetmalloctag(t, getcallerpc(&s));
- return t;
- }
+ if((s->t = match(t, attr, val)) != nil)
+ goto out;
ndbfree(t);
} else if(s->type == Cptr){
if(Bseek(&db->b, s->ptr, 0) < 0)
@@ -213,18 +207,16 @@
s->ptr = s->ptr1;
s->type = Cptr1;
t = ndbparse(db);
- if(t == 0)
+ if(t == nil)
break;
- if(s->t = match(t, attr, val)){
- ndbsetmalloctag(t, getcallerpc(&s));
- return t;
- }
+ if((s->t = match(t, attr, val)) != nil)
+ goto out;
ndbfree(t);
} else if(s->type == Cptr1){
if(s->ptr & NDBCHAIN){ /* hash chain continuation */
s->ptr &= ~NDBCHAIN;
p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
- if(p == 0)
+ if(p == nil)
break;
s->ptr = NDBGETP(p);
s->ptr1 = NDBGETP(p+NDBPLEN);
@@ -234,12 +226,10 @@
break;
s->ptr = NDBNAP;
t = ndbparse(db);
- if(t == 0)
+ if(t == nil)
break;
- if(s->t = match(t, attr, val)){
- ndbsetmalloctag(t, getcallerpc(&s));
- return t;
- }
+ if((s->t = match(t, attr, val)) != nil)
+ goto out;
ndbfree(t);
break;
}
@@ -247,14 +237,14 @@
}
nextfile:
-
/* nothing left to search? */
s->ptr = NDBNAP;
- if(db->next == 0)
- return 0;
+ if(db->next == nil)
+ return nil;
/* advance search to next db file */
t = ndbsearch(db->next, s, attr, val);
+out:
ndbsetmalloctag(t, getcallerpc(&s));
return t;
}
--- a/sys/src/libndb/ndbparse.c
+++ b/sys/src/libndb/ndbparse.c
@@ -30,9 +30,9 @@
Ndbtuple *first, *last;
int len;
- first = last = 0;
+ first = last = nil;
for(;;){
- if((line = Brdline(&db->b, '\n')) == 0)
+ if((line = Brdline(&db->b, '\n')) == nil)
break;
len = Blinelen(&db->b);
if(line[len-1] != '\n')
@@ -42,15 +42,15 @@
break;
}
t = _ndbparseline(line);
- if(t == 0)
+ if(t == nil)
continue;
setmalloctag(t, getcallerpc(&db));
- if(first)
+ if(first != nil)
last->entry = t;
else
first = t;
last = t;
- while(last->entry)
+ while(last->entry != nil)
last = last->entry;
}
ndbsetmalloctag(first, getcallerpc(&db));
--- a/sys/src/libndb/ndbreorder.c
+++ b/sys/src/libndb/ndbreorder.c
@@ -4,7 +4,7 @@
#include <ndb.h>
/*
- * reorder the tuple to put x's line first in the entry and x fitst in its line
+ * reorder the tuple to put x's line first in the entry and x first in its line
*/
Ndbtuple*
ndbreorder(Ndbtuple *t, Ndbtuple *x)