plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

ndbsubstitute.c (692B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <bio.h>
      4 #include <ndb.h>
      5 
      6 /* replace a in t with b, the line structure in b is lost, c'est la vie */
      7 Ndbtuple*
      8 ndbsubstitute(Ndbtuple *t, Ndbtuple *a, Ndbtuple *b)
      9 {
     10 	Ndbtuple *nt;
     11 
     12 	if(a == b)
     13 		return t;
     14 	if(b == nil)
     15 		return ndbdiscard(t, a);
     16 
     17 	/* all pointers to a become pointers to b */
     18 	for(nt = t; nt != nil; nt = nt->entry){
     19 		if(nt->line == a)
     20 			nt->line = b;
     21 		if(nt->entry == a)
     22 			nt->entry = b;
     23 	}
     24 
     25 	/* end of b chain points to a's successors */
     26 	for(nt = b; nt->entry; nt = nt->entry){
     27 		nt->line = nt->entry;
     28 	}
     29 	nt->line = a->line;
     30 	nt->entry = a->entry;
     31 
     32 	a->entry = nil;
     33 	ndbfree(a);
     34 
     35 	if(a == t)
     36 		return b;
     37 	else
     38 		return t;
     39 }