ndbsubstitute.c 692 B

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